]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_oil.c
Merge pull request #5288 from SumitAgarwal123/bfd_docs
[mirror_frr.git] / pimd / pim_oil.c
1 /*
2 * PIM for Quagga
3 * Copyright (C) 2008 Everton da Silva Marques
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <zebra.h>
21
22 #include "log.h"
23 #include "memory.h"
24 #include "linklist.h"
25 #include "if.h"
26 #include "hash.h"
27 #include "jhash.h"
28
29 #include "pimd.h"
30 #include "pim_oil.h"
31 #include "pim_str.h"
32 #include "pim_iface.h"
33 #include "pim_time.h"
34
35 // struct list *pim_channel_oil_list = NULL;
36 // struct hash *pim_channel_oil_hash = NULL;
37
38 char *pim_channel_oil_dump(struct channel_oil *c_oil, char *buf, size_t size)
39 {
40 char *out;
41 struct interface *ifp;
42 struct prefix_sg sg;
43 int i;
44
45 sg.src = c_oil->oil.mfcc_origin;
46 sg.grp = c_oil->oil.mfcc_mcastgrp;
47 ifp = pim_if_find_by_vif_index(c_oil->pim, c_oil->oil.mfcc_parent);
48 snprintf(buf, size, "%s IIF: %s, OIFS: ", pim_str_sg_dump(&sg),
49 ifp ? ifp->name : "(?)");
50
51 out = buf + strlen(buf);
52 for (i = 0; i < MAXVIFS; i++) {
53 if (c_oil->oil.mfcc_ttls[i] != 0) {
54 ifp = pim_if_find_by_vif_index(c_oil->pim, i);
55 snprintf(out, buf + size - out, "%s ",
56 ifp ? ifp->name : "(?)");
57 out += strlen(out);
58 }
59 }
60
61 return buf;
62 }
63
64 static int pim_channel_oil_compare(struct channel_oil *c1,
65 struct channel_oil *c2)
66 {
67 if (ntohl(c1->oil.mfcc_mcastgrp.s_addr)
68 < ntohl(c2->oil.mfcc_mcastgrp.s_addr))
69 return -1;
70
71 if (ntohl(c1->oil.mfcc_mcastgrp.s_addr)
72 > ntohl(c2->oil.mfcc_mcastgrp.s_addr))
73 return 1;
74
75 if (ntohl(c1->oil.mfcc_origin.s_addr)
76 < ntohl(c2->oil.mfcc_origin.s_addr))
77 return -1;
78
79 if (ntohl(c1->oil.mfcc_origin.s_addr)
80 > ntohl(c2->oil.mfcc_origin.s_addr))
81 return 1;
82
83 return 0;
84 }
85
86 static bool pim_oil_equal(const void *arg1, const void *arg2)
87 {
88 const struct channel_oil *c1 = (const struct channel_oil *)arg1;
89 const struct channel_oil *c2 = (const struct channel_oil *)arg2;
90
91 if ((c1->oil.mfcc_mcastgrp.s_addr == c2->oil.mfcc_mcastgrp.s_addr)
92 && (c1->oil.mfcc_origin.s_addr == c2->oil.mfcc_origin.s_addr))
93 return true;
94
95 return false;
96 }
97
98 static unsigned int pim_oil_hash_key(const void *arg)
99 {
100 const struct channel_oil *oil = arg;
101
102 return jhash_2words(oil->oil.mfcc_mcastgrp.s_addr,
103 oil->oil.mfcc_origin.s_addr, 0);
104 }
105
106 void pim_oil_init(struct pim_instance *pim)
107 {
108 char hash_name[64];
109
110 snprintf(hash_name, 64, "PIM %s Oil Hash", pim->vrf->name);
111 pim->channel_oil_hash = hash_create_size(8192, pim_oil_hash_key,
112 pim_oil_equal, hash_name);
113
114 pim->channel_oil_list = list_new();
115 pim->channel_oil_list->del = (void (*)(void *))pim_channel_oil_free;
116 pim->channel_oil_list->cmp =
117 (int (*)(void *, void *))pim_channel_oil_compare;
118 }
119
120 void pim_oil_terminate(struct pim_instance *pim)
121 {
122 if (pim->channel_oil_list)
123 list_delete(&pim->channel_oil_list);
124
125 if (pim->channel_oil_hash)
126 hash_free(pim->channel_oil_hash);
127 pim->channel_oil_hash = NULL;
128 }
129
130 void pim_channel_oil_free(struct channel_oil *c_oil)
131 {
132 XFREE(MTYPE_PIM_CHANNEL_OIL, c_oil);
133 }
134
135 struct channel_oil *pim_find_channel_oil(struct pim_instance *pim,
136 struct prefix_sg *sg)
137 {
138 struct channel_oil *c_oil = NULL;
139 struct channel_oil lookup;
140
141 lookup.oil.mfcc_mcastgrp = sg->grp;
142 lookup.oil.mfcc_origin = sg->src;
143
144 c_oil = hash_lookup(pim->channel_oil_hash, &lookup);
145
146 return c_oil;
147 }
148
149 void pim_channel_oil_change_iif(struct pim_instance *pim,
150 struct channel_oil *c_oil,
151 int input_vif_index,
152 const char *name)
153 {
154 int old_vif_index = c_oil->oil.mfcc_parent;
155 struct prefix_sg sg = {.src = c_oil->oil.mfcc_mcastgrp,
156 .grp = c_oil->oil.mfcc_origin};
157
158 if (c_oil->oil.mfcc_parent == input_vif_index) {
159 if (PIM_DEBUG_MROUTE_DETAIL)
160 zlog_debug("%s(%s): Existing channel oil %pSG4 already using %d as IIF",
161 __PRETTY_FUNCTION__, name, &sg,
162 input_vif_index);
163
164 return;
165 }
166
167 if (PIM_DEBUG_MROUTE_DETAIL)
168 zlog_debug("%s(%s): Changing channel oil %pSG4 IIF from %d to %d installed: %d",
169 __PRETTY_FUNCTION__, name, &sg,
170 c_oil->oil.mfcc_parent, input_vif_index,
171 c_oil->installed);
172
173 c_oil->oil.mfcc_parent = input_vif_index;
174 if (c_oil->installed) {
175 if (input_vif_index == MAXVIFS)
176 pim_mroute_del(c_oil, name);
177 else
178 pim_mroute_add(c_oil, name);
179 } else
180 if (old_vif_index == MAXVIFS)
181 pim_mroute_add(c_oil, name);
182
183 return;
184 }
185
186 struct channel_oil *pim_channel_oil_add(struct pim_instance *pim,
187 struct prefix_sg *sg,
188 int input_vif_index, const char *name)
189 {
190 struct channel_oil *c_oil;
191 struct interface *ifp;
192
193 c_oil = pim_find_channel_oil(pim, sg);
194 if (c_oil) {
195 if (c_oil->oil.mfcc_parent != input_vif_index) {
196 c_oil->oil_inherited_rescan = 1;
197 if (PIM_DEBUG_MROUTE_DETAIL)
198 zlog_debug(
199 "%s: Existing channel oil %pSG4 points to %d, modifying to point at %d",
200 __PRETTY_FUNCTION__, sg,
201 c_oil->oil.mfcc_parent,
202 input_vif_index);
203 }
204 pim_channel_oil_change_iif(pim, c_oil, input_vif_index,
205 name);
206 ++c_oil->oil_ref_count;
207 /* channel might be present prior to upstream */
208 c_oil->up = pim_upstream_find(pim, sg);
209
210 if (PIM_DEBUG_MROUTE)
211 zlog_debug(
212 "%s(%s): Existing oil for %pSG4 Ref Count: %d (Post Increment)",
213 __PRETTY_FUNCTION__, name, sg,
214 c_oil->oil_ref_count);
215 return c_oil;
216 }
217
218 if (input_vif_index != MAXVIFS) {
219 ifp = pim_if_find_by_vif_index(pim, input_vif_index);
220 if (!ifp) {
221 /* warning only */
222 zlog_warn(
223 "%s:%s (S,G)=%pSG4 could not find input interface for input_vif_index=%d",
224 __PRETTY_FUNCTION__, name, sg, input_vif_index);
225 }
226 }
227
228 c_oil = XCALLOC(MTYPE_PIM_CHANNEL_OIL, sizeof(*c_oil));
229
230 c_oil->oil.mfcc_mcastgrp = sg->grp;
231 c_oil->oil.mfcc_origin = sg->src;
232 c_oil = hash_get(pim->channel_oil_hash, c_oil, hash_alloc_intern);
233
234 c_oil->oil.mfcc_parent = input_vif_index;
235 c_oil->oil_ref_count = 1;
236 c_oil->installed = 0;
237 c_oil->up = pim_upstream_find(pim, sg);
238 c_oil->pim = pim;
239
240 listnode_add_sort(pim->channel_oil_list, c_oil);
241
242 if (PIM_DEBUG_MROUTE)
243 zlog_debug(
244 "%s(%s): New oil for %pSG4 vif_index: %d Ref Count: 1 (Post Increment)",
245 __PRETTY_FUNCTION__, name, sg, input_vif_index);
246 return c_oil;
247 }
248
249 void pim_channel_oil_del(struct channel_oil *c_oil, const char *name)
250 {
251 if (PIM_DEBUG_MROUTE) {
252 struct prefix_sg sg = {.src = c_oil->oil.mfcc_mcastgrp,
253 .grp = c_oil->oil.mfcc_origin};
254
255 zlog_debug(
256 "%s(%s): Del oil for %pSG4, Ref Count: %d (Predecrement)",
257 __PRETTY_FUNCTION__, name, &sg, c_oil->oil_ref_count);
258 }
259 --c_oil->oil_ref_count;
260
261 if (c_oil->oil_ref_count < 1) {
262 /*
263 * notice that listnode_delete() can't be moved
264 * into pim_channel_oil_free() because the later is
265 * called by list_delete_all_node()
266 */
267 c_oil->up = NULL;
268 listnode_delete(c_oil->pim->channel_oil_list, c_oil);
269 hash_release(c_oil->pim->channel_oil_hash, c_oil);
270
271 pim_channel_oil_free(c_oil);
272 }
273 }
274
275 int pim_channel_del_oif(struct channel_oil *channel_oil, struct interface *oif,
276 uint32_t proto_mask)
277 {
278 struct pim_interface *pim_ifp;
279
280 zassert(channel_oil);
281 zassert(oif);
282
283 pim_ifp = oif->info;
284
285 /*
286 * Don't do anything if we've been asked to remove a source
287 * that is not actually on it.
288 */
289 if (!(channel_oil->oif_flags[pim_ifp->mroute_vif_index] & proto_mask)) {
290 if (PIM_DEBUG_MROUTE) {
291 char group_str[INET_ADDRSTRLEN];
292 char source_str[INET_ADDRSTRLEN];
293 pim_inet4_dump("<group?>",
294 channel_oil->oil.mfcc_mcastgrp,
295 group_str, sizeof(group_str));
296 pim_inet4_dump("<source?>",
297 channel_oil->oil.mfcc_origin, source_str,
298 sizeof(source_str));
299 zlog_debug(
300 "%s %s: no existing protocol mask %u(%u) for requested OIF %s (vif_index=%d, min_ttl=%d) for channel (S,G)=(%s,%s)",
301 __FILE__, __PRETTY_FUNCTION__, proto_mask,
302 channel_oil
303 ->oif_flags[pim_ifp->mroute_vif_index],
304 oif->name, pim_ifp->mroute_vif_index,
305 channel_oil->oil
306 .mfcc_ttls[pim_ifp->mroute_vif_index],
307 source_str, group_str);
308 }
309 return 0;
310 }
311
312 channel_oil->oif_flags[pim_ifp->mroute_vif_index] &= ~proto_mask;
313
314 if (channel_oil->oif_flags[pim_ifp->mroute_vif_index]) {
315 if (PIM_DEBUG_MROUTE) {
316 char group_str[INET_ADDRSTRLEN];
317 char source_str[INET_ADDRSTRLEN];
318 pim_inet4_dump("<group?>",
319 channel_oil->oil.mfcc_mcastgrp,
320 group_str, sizeof(group_str));
321 pim_inet4_dump("<source?>",
322 channel_oil->oil.mfcc_origin, source_str,
323 sizeof(source_str));
324 zlog_debug(
325 "%s %s: other protocol masks remain for requested OIF %s (vif_index=%d, min_ttl=%d) for channel (S,G)=(%s,%s)",
326 __FILE__, __PRETTY_FUNCTION__, oif->name,
327 pim_ifp->mroute_vif_index,
328 channel_oil->oil
329 .mfcc_ttls[pim_ifp->mroute_vif_index],
330 source_str, group_str);
331 }
332 return 0;
333 }
334
335 channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index] = 0;
336
337 if (pim_mroute_add(channel_oil, __PRETTY_FUNCTION__)) {
338 if (PIM_DEBUG_MROUTE) {
339 char group_str[INET_ADDRSTRLEN];
340 char source_str[INET_ADDRSTRLEN];
341 pim_inet4_dump("<group?>",
342 channel_oil->oil.mfcc_mcastgrp,
343 group_str, sizeof(group_str));
344 pim_inet4_dump("<source?>",
345 channel_oil->oil.mfcc_origin, source_str,
346 sizeof(source_str));
347 zlog_debug(
348 "%s %s: could not remove output interface %s (vif_index=%d) for channel (S,G)=(%s,%s)",
349 __FILE__, __PRETTY_FUNCTION__, oif->name,
350 pim_ifp->mroute_vif_index, source_str,
351 group_str);
352 }
353 return -1;
354 }
355
356 --channel_oil->oil_size;
357
358 if (PIM_DEBUG_MROUTE) {
359 char group_str[INET_ADDRSTRLEN];
360 char source_str[INET_ADDRSTRLEN];
361 pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp,
362 group_str, sizeof(group_str));
363 pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin,
364 source_str, sizeof(source_str));
365 zlog_debug(
366 "%s %s: (S,G)=(%s,%s): proto_mask=%u IIF:%d OIF=%s vif_index=%d",
367 __FILE__, __PRETTY_FUNCTION__, source_str, group_str,
368 proto_mask, channel_oil->oil.mfcc_parent, oif->name,
369 pim_ifp->mroute_vif_index);
370 }
371
372 return 0;
373 }
374
375
376 int pim_channel_add_oif(struct channel_oil *channel_oil, struct interface *oif,
377 uint32_t proto_mask)
378 {
379 struct pim_interface *pim_ifp;
380 int old_ttl;
381 bool allow_iif_in_oil = false;
382
383 /*
384 * If we've gotten here we've gone bad, but let's
385 * not take down pim
386 */
387 if (!channel_oil) {
388 zlog_warn("Attempt to Add OIF for non-existent channel oil");
389 return -1;
390 }
391
392 pim_ifp = oif->info;
393
394 #ifdef PIM_ENFORCE_LOOPFREE_MFC
395 /*
396 Prevent creating MFC entry with OIF=IIF.
397
398 This is a protection against implementation mistakes.
399
400 PIM protocol implicitely ensures loopfree multicast topology.
401
402 IGMP must be protected against adding looped MFC entries created
403 by both source and receiver attached to the same interface. See
404 TODO T22.
405 We shall allow igmp to create upstream when it is DR for the intf.
406 Assume RP reachable via non DR.
407 */
408 if ((channel_oil->up &&
409 PIM_UPSTREAM_FLAG_TEST_ALLOW_IIF_IN_OIL(channel_oil->up->flags)) ||
410 ((proto_mask == PIM_OIF_FLAG_PROTO_IGMP) && PIM_I_am_DR(pim_ifp))) {
411 allow_iif_in_oil = true;
412 }
413
414 if (!allow_iif_in_oil &&
415 pim_ifp->mroute_vif_index == channel_oil->oil.mfcc_parent) {
416 channel_oil->oil_inherited_rescan = 1;
417 if (PIM_DEBUG_MROUTE) {
418 char group_str[INET_ADDRSTRLEN];
419 char source_str[INET_ADDRSTRLEN];
420 pim_inet4_dump("<group?>",
421 channel_oil->oil.mfcc_mcastgrp,
422 group_str, sizeof(group_str));
423 pim_inet4_dump("<source?>",
424 channel_oil->oil.mfcc_origin, source_str,
425 sizeof(source_str));
426 zlog_debug(
427 "%s %s: refusing protocol mask %u request for IIF=OIF=%s (vif_index=%d) for channel (S,G)=(%s,%s)",
428 __FILE__, __PRETTY_FUNCTION__, proto_mask,
429 oif->name, pim_ifp->mroute_vif_index,
430 source_str, group_str);
431 }
432 return -2;
433 }
434 #endif
435
436 /* Prevent single protocol from subscribing same interface to
437 channel (S,G) multiple times */
438 if (channel_oil->oif_flags[pim_ifp->mroute_vif_index] & proto_mask) {
439 if (PIM_DEBUG_MROUTE) {
440 char group_str[INET_ADDRSTRLEN];
441 char source_str[INET_ADDRSTRLEN];
442 pim_inet4_dump("<group?>",
443 channel_oil->oil.mfcc_mcastgrp,
444 group_str, sizeof(group_str));
445 pim_inet4_dump("<source?>",
446 channel_oil->oil.mfcc_origin, source_str,
447 sizeof(source_str));
448 zlog_debug(
449 "%s %s: existing protocol mask %u requested OIF %s (vif_index=%d, min_ttl=%d) for channel (S,G)=(%s,%s)",
450 __FILE__, __PRETTY_FUNCTION__, proto_mask,
451 oif->name, pim_ifp->mroute_vif_index,
452 channel_oil->oil
453 .mfcc_ttls[pim_ifp->mroute_vif_index],
454 source_str, group_str);
455 }
456 return -3;
457 }
458
459 /* Allow other protocol to request subscription of same interface to
460 * channel (S,G), we need to note this information
461 */
462 if (channel_oil->oif_flags[pim_ifp->mroute_vif_index]
463 & PIM_OIF_FLAG_PROTO_ANY) {
464
465 /* Updating time here is not required as this time has to
466 * indicate when the interface is added
467 */
468
469 channel_oil->oif_flags[pim_ifp->mroute_vif_index] |= proto_mask;
470 /* Check the OIF really exists before returning, and only log
471 warning otherwise */
472 if (channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index] < 1) {
473 {
474 char group_str[INET_ADDRSTRLEN];
475 char source_str[INET_ADDRSTRLEN];
476 pim_inet4_dump("<group?>",
477 channel_oil->oil.mfcc_mcastgrp,
478 group_str, sizeof(group_str));
479 pim_inet4_dump("<source?>",
480 channel_oil->oil.mfcc_origin,
481 source_str, sizeof(source_str));
482 zlog_warn(
483 "%s %s: new protocol mask %u requested nonexistent OIF %s (vif_index=%d, min_ttl=%d) for channel (S,G)=(%s,%s)",
484 __FILE__, __PRETTY_FUNCTION__,
485 proto_mask, oif->name,
486 pim_ifp->mroute_vif_index,
487 channel_oil->oil.mfcc_ttls
488 [pim_ifp->mroute_vif_index],
489 source_str, group_str);
490 }
491 }
492
493 return 0;
494 }
495
496 old_ttl = channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index];
497
498 if (old_ttl > 0) {
499 if (PIM_DEBUG_MROUTE) {
500 char group_str[INET_ADDRSTRLEN];
501 char source_str[INET_ADDRSTRLEN];
502 pim_inet4_dump("<group?>",
503 channel_oil->oil.mfcc_mcastgrp,
504 group_str, sizeof(group_str));
505 pim_inet4_dump("<source?>",
506 channel_oil->oil.mfcc_origin, source_str,
507 sizeof(source_str));
508 zlog_debug(
509 "%s %s: interface %s (vif_index=%d) is existing output for channel (S,G)=(%s,%s)",
510 __FILE__, __PRETTY_FUNCTION__, oif->name,
511 pim_ifp->mroute_vif_index, source_str,
512 group_str);
513 }
514 return -4;
515 }
516
517 channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index] =
518 PIM_MROUTE_MIN_TTL;
519
520 /* channel_oil->oil.mfcc_parent != MAXVIFS indicate this entry is not
521 * valid to get installed in kernel.
522 */
523 if (channel_oil->oil.mfcc_parent != MAXVIFS) {
524 if (pim_mroute_add(channel_oil, __PRETTY_FUNCTION__)) {
525 if (PIM_DEBUG_MROUTE) {
526 char group_str[INET_ADDRSTRLEN];
527 char source_str[INET_ADDRSTRLEN];
528 pim_inet4_dump("<group?>",
529 channel_oil->oil.mfcc_mcastgrp,
530 group_str, sizeof(group_str));
531 pim_inet4_dump("<source?>",
532 channel_oil->oil.mfcc_origin, source_str,
533 sizeof(source_str));
534 zlog_debug(
535 "%s %s: could not add output interface %s (vif_index=%d) for channel (S,G)=(%s,%s)",
536 __FILE__, __PRETTY_FUNCTION__, oif->name,
537 pim_ifp->mroute_vif_index, source_str,
538 group_str);
539 }
540
541 channel_oil->oil.mfcc_ttls[pim_ifp->mroute_vif_index]
542 = old_ttl;
543 return -5;
544 }
545 }
546
547 channel_oil->oif_creation[pim_ifp->mroute_vif_index] =
548 pim_time_monotonic_sec();
549 ++channel_oil->oil_size;
550 channel_oil->oif_flags[pim_ifp->mroute_vif_index] |= proto_mask;
551
552 if (PIM_DEBUG_MROUTE) {
553 char group_str[INET_ADDRSTRLEN];
554 char source_str[INET_ADDRSTRLEN];
555 pim_inet4_dump("<group?>", channel_oil->oil.mfcc_mcastgrp,
556 group_str, sizeof(group_str));
557 pim_inet4_dump("<source?>", channel_oil->oil.mfcc_origin,
558 source_str, sizeof(source_str));
559 zlog_debug(
560 "%s %s: (S,G)=(%s,%s): proto_mask=%u OIF=%s vif_index=%d: DONE",
561 __FILE__, __PRETTY_FUNCTION__, source_str, group_str,
562 proto_mask, oif->name, pim_ifp->mroute_vif_index);
563 }
564
565 return 0;
566 }
567
568 int pim_channel_oil_empty(struct channel_oil *c_oil)
569 {
570 static uint32_t zero[MAXVIFS];
571 static int inited = 0;
572
573 if (!c_oil)
574 return 1;
575 /*
576 * Not sure that this is necessary, but I would rather ensure
577 * that this works.
578 */
579 if (!inited) {
580 memset(&zero, 0, sizeof(uint32_t) * MAXVIFS);
581 inited = 1;
582 }
583
584 return !memcmp(c_oil->oif_flags, zero, MAXVIFS * sizeof(uint32_t));
585 }