summaryrefslogtreecommitdiffstats
path: root/pimd/pim_static.c
blob: 63762ef3270839c0a7a0262b734d328fee14726b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
 * PIM for Quagga: add the ability to configure multicast static routes
 * Copyright (C) 2014  Nathan Bahr, ATCorp
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; see the file COPYING; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <zebra.h>

#include "vty.h"
#include "if.h"
#include "log.h"
#include "memory.h"
#include "linklist.h"

#include "pimd.h"
#include "pim_oil.h"
#include "pim_static.h"
#include "pim_time.h"
#include "pim_str.h"
#include "pim_iface.h"

void pim_static_route_free(struct static_route *s_route)
{
  XFREE(MTYPE_PIM_STATIC_ROUTE, s_route);
}

static struct static_route * static_route_alloc()
{
   struct static_route *s_route;

   s_route = XCALLOC(MTYPE_PIM_STATIC_ROUTE, sizeof(*s_route));
   if (!s_route) {
     zlog_err("PIM XCALLOC(%zu) failure", sizeof(*s_route));
     return 0;
   }
   return s_route;
}

static struct static_route *static_route_new(unsigned int   iif,
                                             unsigned int   oif,
                                             struct in_addr group,
                                             struct in_addr source)
{
  struct static_route * s_route;
  s_route = static_route_alloc();
  if (!s_route) {
     return 0;
  }

  s_route->group             = group;
  s_route->source            = source;
  s_route->iif               = iif;
  s_route->oif_ttls[oif]     = 1;
  s_route->c_oil.oil_ref_count         = 1;
  s_route->c_oil.oil.mfcc_origin    = source;
  s_route->c_oil.oil.mfcc_mcastgrp  = group;
  s_route->c_oil.oil.mfcc_parent    = iif;
  s_route->c_oil.oil.mfcc_ttls[oif] = 1;
  s_route->c_oil.oif_creation[oif] = pim_time_monotonic_sec();

  return s_route;
}


int pim_static_add(struct interface *iif, struct interface *oif, struct in_addr group, struct in_addr source)
{
   struct listnode *node = NULL;
   struct static_route *s_route = NULL;
   struct static_route *original_s_route = NULL;
   struct pim_interface *pim_iif = iif ? iif->info : NULL;
   struct pim_interface *pim_oif = oif ? oif->info : NULL;
   ifindex_t iif_index = pim_iif ? pim_iif->mroute_vif_index : 0;
   ifindex_t oif_index = pim_oif ? pim_oif->mroute_vif_index : 0;

   if (!iif_index || !oif_index) {
      zlog_warn("%s %s: Unable to add static route: Invalid interface index(iif=%d,oif=%d)",
               __FILE__, __PRETTY_FUNCTION__,
               iif_index,
               oif_index);
      return -2;
   }

#ifdef PIM_ENFORCE_LOOPFREE_MFC
   if (iif_index == oif_index) {
      /* looped MFC entry */
      zlog_warn("%s %s: Unable to add static route: Looped MFC entry(iif=%d,oif=%d)",
               __FILE__, __PRETTY_FUNCTION__,
               iif_index,
               oif_index);
      return -4;
   }
#endif

   for (ALL_LIST_ELEMENTS_RO(qpim_static_route_list, node, s_route)) {
      if (s_route->group.s_addr == group.s_addr &&
          s_route->source.s_addr == source.s_addr) {
         if (s_route->iif == iif_index &&
             s_route->oif_ttls[oif_index]) {
            char gifaddr_str[INET_ADDRSTRLEN];
            char sifaddr_str[INET_ADDRSTRLEN];
            pim_inet4_dump("<ifaddr?>", group, gifaddr_str, sizeof(gifaddr_str));
            pim_inet4_dump("<ifaddr?>", source, sifaddr_str, sizeof(sifaddr_str));
            zlog_warn("%s %s: Unable to add static route: Route already exists (iif=%d,oif=%d,group=%s,source=%s)",
                     __FILE__, __PRETTY_FUNCTION__,
                     iif_index,
                     oif_index,
                     gifaddr_str,
                     sifaddr_str);
            return -3;
         }

         /* Ok, from here on out we will be making changes to the s_route structure, but if
          * for some reason we fail to commit these changes to the kernel, we want to be able
          * restore the state of the list. So copy the node data and if need be, we can copy
          * back if it fails.
          */
         original_s_route = static_route_alloc();
         if (!original_s_route) {
            return -5;
         }
         memcpy(original_s_route, s_route, sizeof(struct static_route));

         /* Route exists and has the same input interface, but adding a new output interface */
         if (s_route->iif == iif_index) {
            s_route->oif_ttls[oif_index] = 1;
            s_route->c_oil.oil.mfcc_ttls[oif_index] = 1;
            s_route->c_oil.oif_creation[oif_index] = pim_time_monotonic_sec();
            ++s_route->c_oil.oil_ref_count;
         } else {
            /* input interface changed */
            s_route->iif = iif_index;
            s_route->c_oil.oil.mfcc_parent = iif_index;

#ifdef PIM_ENFORCE_LOOPFREE_MFC
            /* check to make sure the new input was not an old output */
            if (s_route->oif_ttls[iif_index]) {
               s_route->oif_ttls[iif_index] = 0;
               s_route->c_oil.oif_creation[iif_index] = 0;
               s_route->c_oil.oil.mfcc_ttls[iif_index] = 0;
               --s_route->c_oil.oil_ref_count;
            }
#endif

            /* now add the new output, if it is new */
            if (!s_route->oif_ttls[oif_index]) {
               s_route->oif_ttls[oif_index] = 1;
               s_route->c_oil.oif_creation[oif_index] = pim_time_monotonic_sec();
               s_route->c_oil.oil.mfcc_ttls[oif_index] = 1;
               ++s_route->c_oil.oil_ref_count;
            }
         }

         break;
      }
   }

   /* If node is null then we reached the end of the list without finding a match */
   if (!node) {
      s_route = static_route_new(iif_index, oif_index, group, source);
      listnode_add(qpim_static_route_list, s_route);
   }

   if (pim_mroute_add(&s_route->c_oil, __PRETTY_FUNCTION__))
   {
      char gifaddr_str[INET_ADDRSTRLEN];
      char sifaddr_str[INET_ADDRSTRLEN];
      pim_inet4_dump("<ifaddr?>", group, gifaddr_str, sizeof(gifaddr_str));
      pim_inet4_dump("<ifaddr?>", source, sifaddr_str, sizeof(sifaddr_str));
      zlog_warn("%s %s: Unable to add static route(iif=%d,oif=%d,group=%s,source=%s)",
               __FILE__, __PRETTY_FUNCTION__,
               iif_index,
               oif_index,
               gifaddr_str,
               sifaddr_str);

      /* Need to put s_route back to the way it was */
      if (original_s_route) {
         memcpy(s_route, original_s_route, sizeof(struct static_route));
      } else {
         /* we never stored off a copy, so it must have been a fresh new route */
         listnode_delete(qpim_static_route_list, s_route);
         pim_static_route_free(s_route);
      }

      if (original_s_route) {
         pim_static_route_free(original_s_route);
      }

      return -1;
   }

   /* Make sure we free the memory for the route copy if used */
   if (original_s_route) {
      pim_static_route_free(original_s_route);
   }

   if (PIM_DEBUG_STATIC) {
     char gifaddr_str[INET_ADDRSTRLEN];
     char sifaddr_str[INET_ADDRSTRLEN];
     pim_inet4_dump("<ifaddr?>", group, gifaddr_str, sizeof(gifaddr_str));
     pim_inet4_dump("<ifaddr?>", source, sifaddr_str, sizeof(sifaddr_str));
     zlog_debug("%s: Static route added(iif=%d,oif=%d,group=%s,source=%s)",
           __PRETTY_FUNCTION__,
           iif_index,
           oif_index,
           gifaddr_str,
           sifaddr_str);
   }

   return 0;
}

int pim_static_del(struct interface *iif, struct interface *oif, struct in_addr group, struct in_addr source)
{
   struct listnode *node = NULL;
   struct listnode *nextnode = NULL;
   struct static_route *s_route = NULL;
   struct pim_interface *pim_iif = iif ? iif->info : 0;
   struct pim_interface *pim_oif = oif ? oif->info : 0;
   ifindex_t iif_index = pim_iif ? pim_iif->mroute_vif_index : 0;
   ifindex_t oif_index = pim_oif ? pim_oif->mroute_vif_index : 0;

   if (!iif_index || !oif_index) {
      zlog_warn("%s %s: Unable to remove static route: Invalid interface index(iif=%d,oif=%d)",
               __FILE__, __PRETTY_FUNCTION__,
               iif_index,
               oif_index);
      return -2;
   }

   for (ALL_LIST_ELEMENTS(qpim_static_route_list, node, nextnode, s_route)) {
      if (s_route->iif == iif_index &&
          s_route->group.s_addr == group.s_addr &&
          s_route->source.s_addr == source.s_addr &&
          s_route->oif_ttls[oif_index]) {
         s_route->oif_ttls[oif_index] = 0;
         s_route->c_oil.oil.mfcc_ttls[oif_index] = 0;
         --s_route->c_oil.oil_ref_count;

         /* If there are no more outputs then delete the whole route, otherwise set the route with the new outputs */
         if (s_route->c_oil.oil_ref_count <= 0 ?
	     pim_mroute_del(&s_route->c_oil, __PRETTY_FUNCTION__) : pim_mroute_add(&s_route->c_oil, __PRETTY_FUNCTION__)) {
	   char gifaddr_str[INET_ADDRSTRLEN];
	   char sifaddr_str[INET_ADDRSTRLEN];
	   pim_inet4_dump("<ifaddr?>", group, gifaddr_str, sizeof(gifaddr_str));
	   pim_inet4_dump("<ifaddr?>", source, sifaddr_str, sizeof(sifaddr_str));
	   zlog_warn("%s %s: Unable to remove static route(iif=%d,oif=%d,group=%s,source=%s)",
                     __FILE__, __PRETTY_FUNCTION__,
                     iif_index,
                     oif_index,
                     gifaddr_str,
                     sifaddr_str);

	   s_route->oif_ttls[oif_index] = 1;
	   s_route->c_oil.oil.mfcc_ttls[oif_index] = 1;
	   ++s_route->c_oil.oil_ref_count;

	   return -1;
         }

         s_route->c_oil.oif_creation[oif_index] = 0;

         if (s_route->c_oil.oil_ref_count <= 0) {
            listnode_delete(qpim_static_route_list, s_route);
            pim_static_route_free(s_route);
         }

         if (PIM_DEBUG_STATIC) {
           char gifaddr_str[INET_ADDRSTRLEN];
           char sifaddr_str[INET_ADDRSTRLEN];
           pim_inet4_dump("<ifaddr?>", group, gifaddr_str, sizeof(gifaddr_str));
           pim_inet4_dump("<ifaddr?>", source, sifaddr_str, sizeof(sifaddr_str));
           zlog_debug("%s: Static route removed(iif=%d,oif=%d,group=%s,source=%s)",
                 __PRETTY_FUNCTION__,
                 iif_index,
                 oif_index,
                 gifaddr_str,
                 sifaddr_str);
         }

         break;
      }
   }

   if (!node) {
      char gifaddr_str[INET_ADDRSTRLEN];
      char sifaddr_str[INET_ADDRSTRLEN];
      pim_inet4_dump("<ifaddr?>", group, gifaddr_str, sizeof(gifaddr_str));
      pim_inet4_dump("<ifaddr?>", source, sifaddr_str, sizeof(sifaddr_str));
      zlog_warn("%s %s: Unable to remove static route: Route does not exist(iif=%d,oif=%d,group=%s,source=%s)",
               __FILE__, __PRETTY_FUNCTION__,
               iif_index,
               oif_index,
               gifaddr_str,
               sifaddr_str);
      return -3;
   }

   return 0;
}

int
pim_static_write_mroute (struct vty *vty, struct interface *ifp)
{
  struct pim_interface *pim_ifp = ifp->info;
  struct listnode *node;
  struct static_route *sroute;
  int count = 0;
  char sbuf[INET_ADDRSTRLEN];
  char gbuf[INET_ADDRSTRLEN];

  if (!pim_ifp)
    return 0;

  for (ALL_LIST_ELEMENTS_RO (qpim_static_route_list, node, sroute))
    {
      pim_inet4_dump ("<ifaddr?>", sroute->group, gbuf, sizeof (gbuf));
      pim_inet4_dump ("<ifaddr?>", sroute->source, sbuf, sizeof (sbuf));
      if (sroute->iif == pim_ifp->mroute_vif_index)
        {
          int i;
          for (i = 0; i < MAXVIFS; i++)
            if (sroute->oif_ttls[i])
              {
                struct interface *oifp = pim_if_find_by_vif_index (i);
                if (sroute->source.s_addr == 0)
                  vty_out (vty, " ip mroute %s %s%s", oifp->name, gbuf, VTY_NEWLINE);
                else
                  vty_out (vty, " ip mroute %s %s %s%s", oifp->name, gbuf, sbuf, VTY_NEWLINE);
                count ++;
              }
        }
    }

  return count;
}