]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_iface.c
ead02d8bd753cfe14a3b1f49a7b792c874120f20
[mirror_frr.git] / pimd / pim_iface.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * PIM for Quagga
4 * Copyright (C) 2008 Everton da Silva Marques
5 */
6
7 #include <zebra.h>
8
9 #include "if.h"
10 #include "log.h"
11 #include "vty.h"
12 #include "memory.h"
13 #include "prefix.h"
14 #include "vrf.h"
15 #include "linklist.h"
16 #include "plist.h"
17 #include "hash.h"
18 #include "ferr.h"
19 #include "network.h"
20
21 #include "pimd.h"
22 #include "pim_instance.h"
23 #include "pim_zebra.h"
24 #include "pim_iface.h"
25 #include "pim_igmp.h"
26 #include "pim_mroute.h"
27 #include "pim_oil.h"
28 #include "pim_str.h"
29 #include "pim_pim.h"
30 #include "pim_neighbor.h"
31 #include "pim_ifchannel.h"
32 #include "pim_sock.h"
33 #include "pim_time.h"
34 #include "pim_ssmpingd.h"
35 #include "pim_rp.h"
36 #include "pim_nht.h"
37 #include "pim_jp_agg.h"
38 #include "pim_igmp_join.h"
39 #include "pim_vxlan.h"
40
41 #include "pim6_mld.h"
42
43 #if PIM_IPV == 4
44 static void pim_if_igmp_join_del_all(struct interface *ifp);
45 static int igmp_join_sock(const char *ifname, ifindex_t ifindex,
46 struct in_addr group_addr, struct in_addr source_addr,
47 struct pim_interface *pim_ifp);
48 #endif
49
50 void pim_if_init(struct pim_instance *pim)
51 {
52 int i;
53
54 for (i = 0; i < MAXVIFS; i++)
55 pim->iface_vif_index[i] = 0;
56 }
57
58 void pim_if_terminate(struct pim_instance *pim)
59 {
60 struct interface *ifp;
61
62 FOR_ALL_INTERFACES (pim->vrf, ifp) {
63 struct pim_interface *pim_ifp = ifp->info;
64
65 if (!pim_ifp)
66 continue;
67
68 pim_if_delete(ifp);
69 }
70 return;
71 }
72
73 static void pim_sec_addr_free(struct pim_secondary_addr *sec_addr)
74 {
75 XFREE(MTYPE_PIM_SEC_ADDR, sec_addr);
76 }
77
78 __attribute__((unused))
79 static int pim_sec_addr_comp(const void *p1, const void *p2)
80 {
81 const struct pim_secondary_addr *sec1 = p1;
82 const struct pim_secondary_addr *sec2 = p2;
83
84 if (sec1->addr.family == AF_INET && sec2->addr.family == AF_INET6)
85 return -1;
86
87 if (sec1->addr.family == AF_INET6 && sec2->addr.family == AF_INET)
88 return 1;
89
90 if (sec1->addr.family == AF_INET) {
91 if (ntohl(sec1->addr.u.prefix4.s_addr)
92 < ntohl(sec2->addr.u.prefix4.s_addr))
93 return -1;
94
95 if (ntohl(sec1->addr.u.prefix4.s_addr)
96 > ntohl(sec2->addr.u.prefix4.s_addr))
97 return 1;
98 } else {
99 return memcmp(&sec1->addr.u.prefix6, &sec2->addr.u.prefix6,
100 sizeof(struct in6_addr));
101 }
102
103 return 0;
104 }
105
106 struct pim_interface *pim_if_new(struct interface *ifp, bool gm, bool pim,
107 bool ispimreg, bool is_vxlan_term)
108 {
109 struct pim_interface *pim_ifp;
110
111 assert(ifp);
112 assert(!ifp->info);
113
114 pim_ifp = XCALLOC(MTYPE_PIM_INTERFACE, sizeof(*pim_ifp));
115
116 pim_ifp->pim = ifp->vrf->info;
117 pim_ifp->mroute_vif_index = -1;
118
119 pim_ifp->igmp_version = IGMP_DEFAULT_VERSION;
120 pim_ifp->mld_version = MLD_DEFAULT_VERSION;
121 pim_ifp->gm_default_robustness_variable =
122 GM_DEFAULT_ROBUSTNESS_VARIABLE;
123 pim_ifp->gm_default_query_interval = GM_GENERAL_QUERY_INTERVAL;
124 pim_ifp->gm_query_max_response_time_dsec =
125 GM_QUERY_MAX_RESPONSE_TIME_DSEC;
126 pim_ifp->gm_specific_query_max_response_time_dsec =
127 GM_SPECIFIC_QUERY_MAX_RESPONSE_TIME_DSEC;
128 pim_ifp->gm_last_member_query_count = GM_DEFAULT_ROBUSTNESS_VARIABLE;
129
130 /* BSM config on interface: true by default */
131 pim_ifp->bsm_enable = true;
132 pim_ifp->ucast_bsm_accept = true;
133 pim_ifp->am_i_dr = false;
134
135 /*
136 RFC 3376: 8.3. Query Response Interval
137 The number of seconds represented by the [Query Response Interval]
138 must be less than the [Query Interval].
139 */
140 assert(pim_ifp->gm_query_max_response_time_dsec <
141 pim_ifp->gm_default_query_interval);
142
143 pim_ifp->pim_enable = pim;
144 pim_ifp->pim_passive_enable = false;
145 pim_ifp->gm_enable = gm;
146
147 pim_ifp->gm_join_list = NULL;
148 pim_ifp->pim_neighbor_list = NULL;
149 pim_ifp->upstream_switch_list = NULL;
150 pim_ifp->pim_generation_id = 0;
151
152 /* list of struct gm_sock */
153 pim_igmp_if_init(pim_ifp, ifp);
154
155 /* list of struct pim_neighbor */
156 pim_ifp->pim_neighbor_list = list_new();
157 pim_ifp->pim_neighbor_list->del = (void (*)(void *))pim_neighbor_free;
158
159 pim_ifp->upstream_switch_list = list_new();
160 pim_ifp->upstream_switch_list->del =
161 (void (*)(void *))pim_jp_agg_group_list_free;
162 pim_ifp->upstream_switch_list->cmp = pim_jp_agg_group_list_cmp;
163
164 pim_ifp->sec_addr_list = list_new();
165 pim_ifp->sec_addr_list->del = (void (*)(void *))pim_sec_addr_free;
166 pim_ifp->sec_addr_list->cmp =
167 (int (*)(void *, void *))pim_sec_addr_comp;
168
169 pim_ifp->activeactive = false;
170
171 RB_INIT(pim_ifchannel_rb, &pim_ifp->ifchannel_rb);
172
173 ifp->info = pim_ifp;
174
175 pim_sock_reset(ifp);
176
177 pim_if_add_vif(ifp, ispimreg, is_vxlan_term);
178 pim_ifp->pim->mcast_if_count++;
179
180 return pim_ifp;
181 }
182
183 void pim_if_delete(struct interface *ifp)
184 {
185 struct pim_interface *pim_ifp;
186
187 assert(ifp);
188 pim_ifp = ifp->info;
189 assert(pim_ifp);
190
191 pim_ifp->pim->mcast_if_count--;
192 #if PIM_IPV == 4
193 if (pim_ifp->gm_join_list) {
194 pim_if_igmp_join_del_all(ifp);
195 }
196 #endif
197
198 pim_ifchannel_delete_all(ifp);
199 #if PIM_IPV == 4
200 igmp_sock_delete_all(ifp);
201 #endif
202 if (pim_ifp->pim_sock_fd >= 0)
203 pim_sock_delete(ifp, "Interface removed from configuration");
204
205 pim_if_del_vif(ifp);
206
207 pim_igmp_if_fini(pim_ifp);
208
209 list_delete(&pim_ifp->pim_neighbor_list);
210 list_delete(&pim_ifp->upstream_switch_list);
211 list_delete(&pim_ifp->sec_addr_list);
212
213 if (pim_ifp->bfd_config.profile)
214 XFREE(MTYPE_TMP, pim_ifp->bfd_config.profile);
215
216 XFREE(MTYPE_PIM_INTERFACE, pim_ifp->boundary_oil_plist);
217 XFREE(MTYPE_PIM_INTERFACE, pim_ifp);
218
219 ifp->info = NULL;
220 }
221
222 void pim_if_update_could_assert(struct interface *ifp)
223 {
224 struct pim_interface *pim_ifp;
225 struct pim_ifchannel *ch;
226
227 pim_ifp = ifp->info;
228 assert(pim_ifp);
229
230 RB_FOREACH (ch, pim_ifchannel_rb, &pim_ifp->ifchannel_rb) {
231 pim_ifchannel_update_could_assert(ch);
232 }
233 }
234
235 static void pim_if_update_my_assert_metric(struct interface *ifp)
236 {
237 struct pim_interface *pim_ifp;
238 struct pim_ifchannel *ch;
239
240 pim_ifp = ifp->info;
241 assert(pim_ifp);
242
243 RB_FOREACH (ch, pim_ifchannel_rb, &pim_ifp->ifchannel_rb) {
244 pim_ifchannel_update_my_assert_metric(ch);
245 }
246 }
247
248 static void pim_addr_change(struct interface *ifp)
249 {
250 struct pim_interface *pim_ifp;
251
252 pim_ifp = ifp->info;
253 assert(pim_ifp);
254
255 pim_if_dr_election(ifp); /* router's own DR Priority (addr) changes --
256 Done TODO T30 */
257 pim_if_update_join_desired(pim_ifp); /* depends on DR */
258 pim_if_update_could_assert(ifp); /* depends on DR */
259 pim_if_update_my_assert_metric(ifp); /* depends on could_assert */
260 pim_if_update_assert_tracking_desired(
261 ifp); /* depends on DR, join_desired */
262
263 /*
264 RFC 4601: 4.3.1. Sending Hello Messages
265
266 1) Before an interface goes down or changes primary IP address, a
267 Hello message with a zero HoldTime should be sent immediately
268 (with the old IP address if the IP address changed).
269 -- Done at the caller of the function as new ip already updated here
270
271 2) After an interface has changed its IP address, it MUST send a
272 Hello message with its new IP address.
273 -- DONE below
274
275 3) If an interface changes one of its secondary IP addresses, a
276 Hello message with an updated Address_List option and a non-zero
277 HoldTime should be sent immediately.
278 -- FIXME See TODO T31
279 */
280 PIM_IF_FLAG_UNSET_HELLO_SENT(pim_ifp->flags);
281 if (pim_ifp->pim_sock_fd < 0)
282 return;
283 pim_hello_restart_now(ifp); /* send hello and restart timer */
284 }
285
286 static int detect_primary_address_change(struct interface *ifp,
287 int force_prim_as_any,
288 const char *caller)
289 {
290 struct pim_interface *pim_ifp = ifp->info;
291 pim_addr new_prim_addr;
292 int changed;
293
294 if (force_prim_as_any)
295 new_prim_addr = PIMADDR_ANY;
296 else
297 new_prim_addr = pim_find_primary_addr(ifp);
298
299 changed = pim_addr_cmp(new_prim_addr, pim_ifp->primary_address);
300
301 if (PIM_DEBUG_ZEBRA)
302 zlog_debug("%s: old=%pPA new=%pPA on interface %s: %s",
303 __func__, &pim_ifp->primary_address, &new_prim_addr,
304 ifp->name, changed ? "changed" : "unchanged");
305
306 if (changed) {
307 /* Before updating pim_ifp send Hello time with 0 hold time */
308 if (pim_ifp->pim_enable) {
309 pim_hello_send(ifp, 0 /* zero-sec holdtime */);
310 }
311 pim_ifp->primary_address = new_prim_addr;
312 }
313
314 return changed;
315 }
316
317 static struct pim_secondary_addr *
318 pim_sec_addr_find(struct pim_interface *pim_ifp, struct prefix *addr)
319 {
320 struct pim_secondary_addr *sec_addr;
321 struct listnode *node;
322
323 for (ALL_LIST_ELEMENTS_RO(pim_ifp->sec_addr_list, node, sec_addr)) {
324 if (prefix_cmp(&sec_addr->addr, addr) == 0) {
325 return sec_addr;
326 }
327 }
328
329 return NULL;
330 }
331
332 static void pim_sec_addr_del(struct pim_interface *pim_ifp,
333 struct pim_secondary_addr *sec_addr)
334 {
335 listnode_delete(pim_ifp->sec_addr_list, sec_addr);
336 pim_sec_addr_free(sec_addr);
337 }
338
339 static int pim_sec_addr_add(struct pim_interface *pim_ifp, struct prefix *addr)
340 {
341 int changed = 0;
342 struct pim_secondary_addr *sec_addr;
343
344 sec_addr = pim_sec_addr_find(pim_ifp, addr);
345 if (sec_addr) {
346 sec_addr->flags &= ~PIM_SEC_ADDRF_STALE;
347 return changed;
348 }
349
350 sec_addr = XCALLOC(MTYPE_PIM_SEC_ADDR, sizeof(*sec_addr));
351
352 changed = 1;
353 sec_addr->addr = *addr;
354 listnode_add_sort(pim_ifp->sec_addr_list, sec_addr);
355
356 return changed;
357 }
358
359 static int pim_sec_addr_del_all(struct pim_interface *pim_ifp)
360 {
361 int changed = 0;
362
363 if (!list_isempty(pim_ifp->sec_addr_list)) {
364 changed = 1;
365 /* remove all nodes and free up the list itself */
366 list_delete_all_node(pim_ifp->sec_addr_list);
367 }
368
369 return changed;
370 }
371
372 static int pim_sec_addr_update(struct interface *ifp)
373 {
374 struct pim_interface *pim_ifp = ifp->info;
375 struct connected *ifc;
376 struct listnode *node;
377 struct listnode *nextnode;
378 struct pim_secondary_addr *sec_addr;
379 int changed = 0;
380
381 for (ALL_LIST_ELEMENTS_RO(pim_ifp->sec_addr_list, node, sec_addr)) {
382 sec_addr->flags |= PIM_SEC_ADDRF_STALE;
383 }
384
385 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
386 pim_addr addr = pim_addr_from_prefix(ifc->address);
387
388 if (pim_addr_is_any(addr))
389 continue;
390
391 if (!pim_addr_cmp(addr, pim_ifp->primary_address)) {
392 /* don't add the primary address into the secondary
393 * address list */
394 continue;
395 }
396
397 if (pim_sec_addr_add(pim_ifp, ifc->address)) {
398 changed = 1;
399 }
400 }
401
402 /* Drop stale entries */
403 for (ALL_LIST_ELEMENTS(pim_ifp->sec_addr_list, node, nextnode,
404 sec_addr)) {
405 if (sec_addr->flags & PIM_SEC_ADDRF_STALE) {
406 pim_sec_addr_del(pim_ifp, sec_addr);
407 changed = 1;
408 }
409 }
410
411 return changed;
412 }
413
414 static int detect_secondary_address_change(struct interface *ifp,
415 int force_prim_as_any,
416 const char *caller)
417 {
418 struct pim_interface *pim_ifp = ifp->info;
419 int changed = 0;
420
421 if (force_prim_as_any) {
422 /* if primary address is being forced to zero just flush the
423 * secondary address list */
424 changed = pim_sec_addr_del_all(pim_ifp);
425 } else {
426 /* re-evaluate the secondary address list */
427 changed = pim_sec_addr_update(ifp);
428 }
429
430 return changed;
431 }
432
433 static void detect_address_change(struct interface *ifp, int force_prim_as_any,
434 const char *caller)
435 {
436 int changed = 0;
437 struct pim_interface *pim_ifp;
438
439 pim_ifp = ifp->info;
440 if (!pim_ifp)
441 return;
442
443 if (detect_primary_address_change(ifp, force_prim_as_any, caller)) {
444 changed = 1;
445 }
446
447 if (detect_secondary_address_change(ifp, force_prim_as_any, caller)) {
448 changed = 1;
449 }
450
451
452 if (changed) {
453 if (!pim_ifp->pim_enable) {
454 return;
455 }
456
457 pim_addr_change(ifp);
458 }
459
460 /* XXX: if we have unnumbered interfaces we need to run detect address
461 * address change on all of them when the lo address changes */
462 }
463
464 int pim_update_source_set(struct interface *ifp, pim_addr source)
465 {
466 struct pim_interface *pim_ifp = ifp->info;
467
468 if (!pim_ifp) {
469 return PIM_IFACE_NOT_FOUND;
470 }
471
472 if (!pim_addr_cmp(pim_ifp->update_source, source)) {
473 return PIM_UPDATE_SOURCE_DUP;
474 }
475
476 pim_ifp->update_source = source;
477 detect_address_change(ifp, 0 /* force_prim_as_any */, __func__);
478
479 return PIM_SUCCESS;
480 }
481
482 void pim_if_addr_add(struct connected *ifc)
483 {
484 struct pim_interface *pim_ifp;
485 struct interface *ifp;
486 bool vxlan_term;
487
488 assert(ifc);
489
490 ifp = ifc->ifp;
491 assert(ifp);
492 pim_ifp = ifp->info;
493 if (!pim_ifp)
494 return;
495
496 if (!if_is_operative(ifp))
497 return;
498
499 if (PIM_DEBUG_ZEBRA)
500 zlog_debug("%s: %s ifindex=%d connected IP address %pFX %s",
501 __func__, ifp->name, ifp->ifindex, ifc->address,
502 CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY)
503 ? "secondary"
504 : "primary");
505 #if PIM_IPV != 4
506 if (IN6_IS_ADDR_LINKLOCAL(&ifc->address->u.prefix6) ||
507 IN6_IS_ADDR_LOOPBACK(&ifc->address->u.prefix6)) {
508 if (IN6_IS_ADDR_UNSPECIFIED(&pim_ifp->ll_lowest))
509 pim_ifp->ll_lowest = ifc->address->u.prefix6;
510 else if (IPV6_ADDR_CMP(&ifc->address->u.prefix6,
511 &pim_ifp->ll_lowest) < 0)
512 pim_ifp->ll_lowest = ifc->address->u.prefix6;
513
514 if (IPV6_ADDR_CMP(&ifc->address->u.prefix6,
515 &pim_ifp->ll_highest) > 0)
516 pim_ifp->ll_highest = ifc->address->u.prefix6;
517
518 if (PIM_DEBUG_ZEBRA)
519 zlog_debug(
520 "%s: new link-local %pI6, lowest now %pI6, highest %pI6",
521 ifc->ifp->name, &ifc->address->u.prefix6,
522 &pim_ifp->ll_lowest, &pim_ifp->ll_highest);
523 }
524 #endif
525
526 detect_address_change(ifp, 0, __func__);
527
528 // if (ifc->address->family != AF_INET)
529 // return;
530
531 #if PIM_IPV == 4
532 struct in_addr ifaddr = ifc->address->u.prefix4;
533
534 if (pim_ifp->gm_enable) {
535 struct gm_sock *igmp;
536
537 /* lookup IGMP socket */
538 igmp = pim_igmp_sock_lookup_ifaddr(pim_ifp->gm_socket_list,
539 ifaddr);
540 if (!igmp) {
541 /* if addr new, add IGMP socket */
542 if (ifc->address->family == AF_INET)
543 pim_igmp_sock_add(pim_ifp->gm_socket_list,
544 ifaddr, ifp, false);
545 } else if (igmp->mtrace_only) {
546 igmp_sock_delete(igmp);
547 pim_igmp_sock_add(pim_ifp->gm_socket_list, ifaddr, ifp,
548 false);
549 }
550
551 /* Replay Static IGMP groups */
552 if (pim_ifp->gm_join_list) {
553 struct listnode *node;
554 struct listnode *nextnode;
555 struct gm_join *ij;
556 int join_fd;
557
558 for (ALL_LIST_ELEMENTS(pim_ifp->gm_join_list, node,
559 nextnode, ij)) {
560 /* Close socket and reopen with Source and Group
561 */
562 close(ij->sock_fd);
563 join_fd = igmp_join_sock(
564 ifp->name, ifp->ifindex, ij->group_addr,
565 ij->source_addr, pim_ifp);
566 if (join_fd < 0) {
567 char group_str[INET_ADDRSTRLEN];
568 char source_str[INET_ADDRSTRLEN];
569 pim_inet4_dump("<grp?>", ij->group_addr,
570 group_str,
571 sizeof(group_str));
572 pim_inet4_dump(
573 "<src?>", ij->source_addr,
574 source_str, sizeof(source_str));
575 zlog_warn(
576 "%s: igmp_join_sock() failure for IGMP group %s source %s on interface %s",
577 __func__, group_str, source_str,
578 ifp->name);
579 /* warning only */
580 } else
581 ij->sock_fd = join_fd;
582 }
583 }
584 } /* igmp */
585 else {
586 struct gm_sock *igmp;
587
588 /* lookup IGMP socket */
589 igmp = pim_igmp_sock_lookup_ifaddr(pim_ifp->gm_socket_list,
590 ifaddr);
591 if (ifc->address->family == AF_INET) {
592 if (igmp)
593 igmp_sock_delete(igmp);
594 /* if addr new, add IGMP socket */
595 pim_igmp_sock_add(pim_ifp->gm_socket_list, ifaddr, ifp,
596 true);
597 }
598 } /* igmp mtrace only */
599 #endif
600
601 if (pim_ifp->pim_enable) {
602
603 if (!pim_addr_is_any(pim_ifp->primary_address)) {
604
605 /* Interface has a valid socket ? */
606 if (pim_ifp->pim_sock_fd < 0) {
607 if (pim_sock_add(ifp)) {
608 zlog_warn(
609 "Failure creating PIM socket for interface %s",
610 ifp->name);
611 }
612 }
613 struct pim_nexthop_cache *pnc = NULL;
614 struct pim_rpf rpf;
615 struct zclient *zclient = NULL;
616
617 zclient = pim_zebra_zclient_get();
618 /* RP config might come prior to (local RP's interface)
619 IF UP event.
620 In this case, pnc would not have pim enabled
621 nexthops.
622 Once Interface is UP and pim info is available,
623 reregister
624 with RNH address to receive update and add the
625 interface as nexthop. */
626 memset(&rpf, 0, sizeof(struct pim_rpf));
627 rpf.rpf_addr = pim_addr_from_prefix(ifc->address);
628 pnc = pim_nexthop_cache_find(pim_ifp->pim, &rpf);
629 if (pnc)
630 pim_sendmsg_zebra_rnh(pim_ifp->pim, zclient,
631 pnc,
632 ZEBRA_NEXTHOP_REGISTER);
633 }
634 } /* pim */
635
636 /*
637 PIM or IGMP is enabled on interface, and there is at least one
638 address assigned, then try to create a vif_index.
639 */
640 if (pim_ifp->mroute_vif_index < 0) {
641 vxlan_term = pim_vxlan_is_term_dev_cfg(pim_ifp->pim, ifp);
642 pim_if_add_vif(ifp, false, vxlan_term);
643 }
644 gm_ifp_update(ifp);
645 pim_ifchannel_scan_forward_start(ifp);
646 }
647
648 static void pim_if_addr_del_igmp(struct connected *ifc)
649 {
650 #if PIM_IPV == 4
651 struct pim_interface *pim_ifp = ifc->ifp->info;
652 struct gm_sock *igmp;
653 struct in_addr ifaddr;
654
655 if (ifc->address->family != AF_INET) {
656 /* non-IPv4 address */
657 return;
658 }
659
660 if (!pim_ifp) {
661 /* IGMP not enabled on interface */
662 return;
663 }
664
665 ifaddr = ifc->address->u.prefix4;
666
667 /* lookup IGMP socket */
668 igmp = pim_igmp_sock_lookup_ifaddr(pim_ifp->gm_socket_list, ifaddr);
669 if (igmp) {
670 /* if addr found, del IGMP socket */
671 igmp_sock_delete(igmp);
672 }
673 #endif
674 }
675
676 static void pim_if_addr_del_pim(struct connected *ifc)
677 {
678 struct pim_interface *pim_ifp = ifc->ifp->info;
679
680 if (ifc->address->family != PIM_AF) {
681 /* non-IPv4 address */
682 return;
683 }
684
685 if (!pim_ifp) {
686 /* PIM not enabled on interface */
687 return;
688 }
689
690 if (!pim_addr_is_any(pim_ifp->primary_address)) {
691 /* Interface keeps a valid primary address */
692 return;
693 }
694
695 if (pim_ifp->pim_sock_fd < 0) {
696 /* Interface does not hold a valid socket any longer */
697 return;
698 }
699
700 /*
701 pim_sock_delete() closes the socket, stops read and timer threads,
702 and kills all neighbors.
703 */
704 pim_sock_delete(ifc->ifp,
705 "last address has been removed from interface");
706 }
707
708 void pim_if_addr_del(struct connected *ifc, int force_prim_as_any)
709 {
710 struct interface *ifp;
711
712 assert(ifc);
713 ifp = ifc->ifp;
714 assert(ifp);
715
716 if (PIM_DEBUG_ZEBRA)
717 zlog_debug("%s: %s ifindex=%d disconnected IP address %pFX %s",
718 __func__, ifp->name, ifp->ifindex, ifc->address,
719 CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY)
720 ? "secondary"
721 : "primary");
722
723 #if PIM_IPV == 6
724 struct pim_interface *pim_ifp = ifc->ifp->info;
725
726 if (pim_ifp &&
727 (!IPV6_ADDR_CMP(&ifc->address->u.prefix6, &pim_ifp->ll_lowest) ||
728 !IPV6_ADDR_CMP(&ifc->address->u.prefix6, &pim_ifp->ll_highest))) {
729 struct listnode *cnode;
730 struct connected *cc;
731
732 memset(&pim_ifp->ll_lowest, 0xff, sizeof(pim_ifp->ll_lowest));
733 memset(&pim_ifp->ll_highest, 0, sizeof(pim_ifp->ll_highest));
734
735 for (ALL_LIST_ELEMENTS_RO(ifc->ifp->connected, cnode, cc)) {
736 if (!IN6_IS_ADDR_LINKLOCAL(&cc->address->u.prefix6) &&
737 !IN6_IS_ADDR_LOOPBACK(&cc->address->u.prefix6))
738 continue;
739
740 if (IPV6_ADDR_CMP(&cc->address->u.prefix6,
741 &pim_ifp->ll_lowest) < 0)
742 pim_ifp->ll_lowest = cc->address->u.prefix6;
743 if (IPV6_ADDR_CMP(&cc->address->u.prefix6,
744 &pim_ifp->ll_highest) > 0)
745 pim_ifp->ll_highest = cc->address->u.prefix6;
746 }
747
748 if (pim_ifp->ll_lowest.s6_addr[0] == 0xff)
749 memset(&pim_ifp->ll_lowest, 0,
750 sizeof(pim_ifp->ll_lowest));
751
752 if (PIM_DEBUG_ZEBRA)
753 zlog_debug(
754 "%s: removed link-local %pI6, lowest now %pI6, highest %pI6",
755 ifc->ifp->name, &ifc->address->u.prefix6,
756 &pim_ifp->ll_lowest, &pim_ifp->ll_highest);
757
758 gm_ifp_update(ifp);
759 }
760 #endif
761
762 detect_address_change(ifp, force_prim_as_any, __func__);
763
764 pim_if_addr_del_igmp(ifc);
765 pim_if_addr_del_pim(ifc);
766 }
767
768 void pim_if_addr_add_all(struct interface *ifp)
769 {
770 struct connected *ifc;
771 struct listnode *node;
772 struct listnode *nextnode;
773 int v4_addrs = 0;
774 int v6_addrs = 0;
775 struct pim_interface *pim_ifp = ifp->info;
776 bool vxlan_term;
777
778
779 /* PIM/IGMP enabled ? */
780 if (!pim_ifp)
781 return;
782
783 for (ALL_LIST_ELEMENTS(ifp->connected, node, nextnode, ifc)) {
784 struct prefix *p = ifc->address;
785
786 if (p->family != AF_INET)
787 v6_addrs++;
788 else
789 v4_addrs++;
790 pim_if_addr_add(ifc);
791 }
792
793 if (!v4_addrs && v6_addrs && !if_is_loopback(ifp) &&
794 pim_ifp->pim_enable && !pim_addr_is_any(pim_ifp->primary_address) &&
795 pim_ifp->pim_sock_fd < 0 && pim_sock_add(ifp)) {
796 /* Interface has a valid primary address ? */
797 /* Interface has a valid socket ? */
798 zlog_warn("Failure creating PIM socket for interface %s",
799 ifp->name);
800 }
801 /*
802 * PIM or IGMP/MLD is enabled on interface, and there is at least one
803 * address assigned, then try to create a vif_index.
804 */
805 if (pim_ifp->mroute_vif_index < 0) {
806 vxlan_term = pim_vxlan_is_term_dev_cfg(pim_ifp->pim, ifp);
807 pim_if_add_vif(ifp, false, vxlan_term);
808 }
809 gm_ifp_update(ifp);
810 pim_ifchannel_scan_forward_start(ifp);
811
812 pim_rp_setup(pim_ifp->pim);
813 pim_rp_check_on_if_add(pim_ifp);
814 }
815
816 void pim_if_addr_del_all(struct interface *ifp)
817 {
818 struct connected *ifc;
819 struct listnode *node;
820 struct listnode *nextnode;
821 struct pim_instance *pim;
822
823 pim = ifp->vrf->info;
824 if (!pim)
825 return;
826
827 /* PIM/IGMP enabled ? */
828 if (!ifp->info)
829 return;
830
831 for (ALL_LIST_ELEMENTS(ifp->connected, node, nextnode, ifc)) {
832 struct prefix *p = ifc->address;
833
834 if (p->family != PIM_AF)
835 continue;
836
837 pim_if_addr_del(ifc, 1 /* force_prim_as_any=true */);
838 }
839
840 pim_rp_setup(pim);
841 pim_i_am_rp_re_evaluate(pim);
842 }
843
844 void pim_if_addr_del_all_igmp(struct interface *ifp)
845 {
846 struct connected *ifc;
847 struct listnode *node;
848 struct listnode *nextnode;
849
850 /* PIM/IGMP enabled ? */
851 if (!ifp->info)
852 return;
853
854 for (ALL_LIST_ELEMENTS(ifp->connected, node, nextnode, ifc)) {
855 struct prefix *p = ifc->address;
856
857 if (p->family != AF_INET)
858 continue;
859
860 pim_if_addr_del_igmp(ifc);
861 }
862 }
863
864 pim_addr pim_find_primary_addr(struct interface *ifp)
865 {
866 struct connected *ifc;
867 struct listnode *node;
868 struct pim_interface *pim_ifp = ifp->info;
869
870 if (pim_ifp && !pim_addr_is_any(pim_ifp->update_source))
871 return pim_ifp->update_source;
872
873 #if PIM_IPV == 6
874 if (pim_ifp && !pim_addr_is_any(pim_ifp->ll_highest))
875 return pim_ifp->ll_highest;
876
877 pim_addr best_addr = PIMADDR_ANY;
878
879 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
880 pim_addr addr;
881
882 if (ifc->address->family != AF_INET6)
883 continue;
884
885 addr = pim_addr_from_prefix(ifc->address);
886 if (!IN6_IS_ADDR_LINKLOCAL(&addr))
887 continue;
888 if (pim_addr_cmp(addr, best_addr) > 0)
889 best_addr = addr;
890 }
891
892 return best_addr;
893 #else
894 int v4_addrs = 0;
895 int v6_addrs = 0;
896
897 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
898 switch (ifc->address->family) {
899 case AF_INET:
900 v4_addrs++;
901 break;
902 case AF_INET6:
903 v6_addrs++;
904 break;
905 default:
906 continue;
907 }
908
909 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY))
910 continue;
911
912 if (ifc->address->family != PIM_AF)
913 continue;
914
915 return pim_addr_from_prefix(ifc->address);
916 }
917
918 /*
919 * If we have no v4_addrs and v6 is configured
920 * We probably are using unnumbered
921 * So let's grab the loopbacks v4 address
922 * and use that as the primary address
923 */
924 if (!v4_addrs && v6_addrs) {
925 struct interface *lo_ifp;
926
927 // DBS - Come back and check here
928 if (ifp->vrf->vrf_id == VRF_DEFAULT)
929 lo_ifp = if_lookup_by_name("lo", ifp->vrf->vrf_id);
930 else
931 lo_ifp = if_lookup_by_name(ifp->vrf->name,
932 ifp->vrf->vrf_id);
933
934 if (lo_ifp && (lo_ifp != ifp))
935 return pim_find_primary_addr(lo_ifp);
936 }
937 return PIMADDR_ANY;
938 #endif
939 }
940
941 static int pim_iface_next_vif_index(struct interface *ifp)
942 {
943 struct pim_interface *pim_ifp = ifp->info;
944 struct pim_instance *pim = pim_ifp->pim;
945 int i;
946
947 /*
948 * The pimreg vif is always going to be in index 0
949 * of the table.
950 */
951 if (ifp->ifindex == PIM_OIF_PIM_REGISTER_VIF)
952 return 0;
953
954 for (i = 1; i < MAXVIFS; i++) {
955 if (pim->iface_vif_index[i] == 0)
956 return i;
957 }
958 return MAXVIFS;
959 }
960
961 /*
962 pim_if_add_vif() uses ifindex as vif_index
963
964 see also pim_if_find_vifindex_by_ifindex()
965 */
966 int pim_if_add_vif(struct interface *ifp, bool ispimreg, bool is_vxlan_term)
967 {
968 struct pim_interface *pim_ifp = ifp->info;
969 pim_addr ifaddr;
970 unsigned char flags = 0;
971
972 assert(pim_ifp);
973
974 if (pim_ifp->mroute_vif_index > 0) {
975 zlog_warn("%s: vif_index=%d > 0 on interface %s ifindex=%d",
976 __func__, pim_ifp->mroute_vif_index, ifp->name,
977 ifp->ifindex);
978 return -1;
979 }
980
981 if (ifp->ifindex < 0) {
982 zlog_warn("%s: ifindex=%d < 1 on interface %s", __func__,
983 ifp->ifindex, ifp->name);
984 return -2;
985 } else if ((ifp->ifindex == 0) &&
986 ((strncmp(ifp->name, "pimreg", 6)) &&
987 (strncmp(ifp->name, "pim6reg", 7)))) {
988 zlog_warn("%s: ifindex=%d == 0 on interface %s", __func__,
989 ifp->ifindex, ifp->name);
990 return -2;
991 }
992
993 ifaddr = pim_ifp->primary_address;
994 #if PIM_IPV != 6
995 /* IPv6 API is always by interface index */
996 if (!ispimreg && !is_vxlan_term && pim_addr_is_any(ifaddr)) {
997 zlog_warn(
998 "%s: could not get address for interface %s ifindex=%d",
999 __func__, ifp->name, ifp->ifindex);
1000 return -4;
1001 }
1002 #endif
1003
1004 pim_ifp->mroute_vif_index = pim_iface_next_vif_index(ifp);
1005
1006 if (pim_ifp->mroute_vif_index >= MAXVIFS) {
1007 zlog_warn(
1008 "%s: Attempting to configure more than MAXVIFS=%d on pim enabled interface %s",
1009 __func__, MAXVIFS, ifp->name);
1010 return -3;
1011 }
1012
1013 if (ifp->ifindex == PIM_OIF_PIM_REGISTER_VIF)
1014 flags = VIFF_REGISTER;
1015 #ifdef VIFF_USE_IFINDEX
1016 else
1017 flags = VIFF_USE_IFINDEX;
1018 #endif
1019
1020 if (pim_mroute_add_vif(ifp, ifaddr, flags)) {
1021 /* pim_mroute_add_vif reported error */
1022 return -5;
1023 }
1024
1025 pim_ifp->pim->iface_vif_index[pim_ifp->mroute_vif_index] = 1;
1026
1027 if (!ispimreg)
1028 gm_ifp_update(ifp);
1029
1030 /* if the device qualifies as pim_vxlan iif/oif update vxlan entries */
1031 pim_vxlan_add_vif(ifp);
1032 return 0;
1033 }
1034
1035 int pim_if_del_vif(struct interface *ifp)
1036 {
1037 struct pim_interface *pim_ifp = ifp->info;
1038
1039 if (pim_ifp->mroute_vif_index < 1) {
1040 zlog_warn("%s: vif_index=%d < 1 on interface %s ifindex=%d",
1041 __func__, pim_ifp->mroute_vif_index, ifp->name,
1042 ifp->ifindex);
1043 return -1;
1044 }
1045
1046 /* if the device was a pim_vxlan iif/oif update vxlan mroute entries */
1047 pim_vxlan_del_vif(ifp);
1048
1049 gm_ifp_teardown(ifp);
1050
1051 pim_mroute_del_vif(ifp);
1052
1053 /*
1054 Update vif_index
1055 */
1056 pim_ifp->pim->iface_vif_index[pim_ifp->mroute_vif_index] = 0;
1057
1058 pim_ifp->mroute_vif_index = -1;
1059 return 0;
1060 }
1061
1062 // DBS - VRF Revist
1063 struct interface *pim_if_find_by_vif_index(struct pim_instance *pim,
1064 ifindex_t vif_index)
1065 {
1066 struct interface *ifp;
1067
1068 FOR_ALL_INTERFACES (pim->vrf, ifp) {
1069 if (ifp->info) {
1070 struct pim_interface *pim_ifp;
1071 pim_ifp = ifp->info;
1072
1073 if (vif_index == pim_ifp->mroute_vif_index)
1074 return ifp;
1075 }
1076 }
1077
1078 return 0;
1079 }
1080
1081 /*
1082 pim_if_add_vif() uses ifindex as vif_index
1083 */
1084 int pim_if_find_vifindex_by_ifindex(struct pim_instance *pim, ifindex_t ifindex)
1085 {
1086 struct pim_interface *pim_ifp;
1087 struct interface *ifp;
1088
1089 ifp = if_lookup_by_index(ifindex, pim->vrf->vrf_id);
1090 if (!ifp || !ifp->info)
1091 return -1;
1092 pim_ifp = ifp->info;
1093
1094 return pim_ifp->mroute_vif_index;
1095 }
1096
1097 int pim_if_lan_delay_enabled(struct interface *ifp)
1098 {
1099 struct pim_interface *pim_ifp;
1100
1101 pim_ifp = ifp->info;
1102 assert(pim_ifp);
1103 assert(pim_ifp->pim_number_of_nonlandelay_neighbors >= 0);
1104
1105 return pim_ifp->pim_number_of_nonlandelay_neighbors == 0;
1106 }
1107
1108 uint16_t pim_if_effective_propagation_delay_msec(struct interface *ifp)
1109 {
1110 if (pim_if_lan_delay_enabled(ifp)) {
1111 struct pim_interface *pim_ifp;
1112 pim_ifp = ifp->info;
1113 return pim_ifp->pim_neighbors_highest_propagation_delay_msec;
1114 } else {
1115 return PIM_DEFAULT_PROPAGATION_DELAY_MSEC;
1116 }
1117 }
1118
1119 uint16_t pim_if_effective_override_interval_msec(struct interface *ifp)
1120 {
1121 if (pim_if_lan_delay_enabled(ifp)) {
1122 struct pim_interface *pim_ifp;
1123 pim_ifp = ifp->info;
1124 return pim_ifp->pim_neighbors_highest_override_interval_msec;
1125 } else {
1126 return PIM_DEFAULT_OVERRIDE_INTERVAL_MSEC;
1127 }
1128 }
1129
1130 int pim_if_t_override_msec(struct interface *ifp)
1131 {
1132 int effective_override_interval_msec;
1133 int t_override_msec;
1134
1135 effective_override_interval_msec =
1136 pim_if_effective_override_interval_msec(ifp);
1137
1138 t_override_msec =
1139 frr_weak_random() % (effective_override_interval_msec + 1);
1140
1141 return t_override_msec;
1142 }
1143
1144 uint16_t pim_if_jp_override_interval_msec(struct interface *ifp)
1145 {
1146 return pim_if_effective_propagation_delay_msec(ifp)
1147 + pim_if_effective_override_interval_msec(ifp);
1148 }
1149
1150 /*
1151 RFC 4601: 4.1.6. State Summarization Macros
1152
1153 The function NBR( I, A ) uses information gathered through PIM Hello
1154 messages to map the IP address A of a directly connected PIM
1155 neighbor router on interface I to the primary IP address of the same
1156 router (Section 4.3.4). The primary IP address of a neighbor is the
1157 address that it uses as the source of its PIM Hello messages.
1158 */
1159 struct pim_neighbor *pim_if_find_neighbor(struct interface *ifp, pim_addr addr)
1160 {
1161 struct listnode *neighnode;
1162 struct pim_neighbor *neigh;
1163 struct pim_interface *pim_ifp;
1164 struct prefix p;
1165
1166 assert(ifp);
1167
1168 pim_ifp = ifp->info;
1169 if (!pim_ifp) {
1170 zlog_warn("%s: multicast not enabled on interface %s", __func__,
1171 ifp->name);
1172 return 0;
1173 }
1174
1175 pim_addr_to_prefix(&p, addr);
1176
1177 for (ALL_LIST_ELEMENTS_RO(pim_ifp->pim_neighbor_list, neighnode,
1178 neigh)) {
1179
1180 /* primary address ? */
1181 if (!pim_addr_cmp(neigh->source_addr, addr))
1182 return neigh;
1183
1184 /* secondary address ? */
1185 if (pim_neighbor_find_secondary(neigh, &p))
1186 return neigh;
1187 }
1188
1189 if (PIM_DEBUG_PIM_TRACE)
1190 zlog_debug(
1191 "%s: neighbor not found for address %pPA on interface %s",
1192 __func__, &addr, ifp->name);
1193
1194 return NULL;
1195 }
1196
1197 long pim_if_t_suppressed_msec(struct interface *ifp)
1198 {
1199 struct pim_interface *pim_ifp;
1200 long t_suppressed_msec;
1201 uint32_t ramount = 0;
1202
1203 pim_ifp = ifp->info;
1204 assert(pim_ifp);
1205
1206 /* join suppression disabled ? */
1207 if (pim_ifp->pim_can_disable_join_suppression)
1208 return 0;
1209
1210 /* t_suppressed = t_periodic * rand(1.1, 1.4) */
1211 ramount = 1100 + (frr_weak_random() % (1400 - 1100 + 1));
1212 t_suppressed_msec = router->t_periodic * ramount;
1213
1214 return t_suppressed_msec;
1215 }
1216
1217 #if PIM_IPV == 4
1218 static void igmp_join_free(struct gm_join *ij)
1219 {
1220 XFREE(MTYPE_PIM_IGMP_JOIN, ij);
1221 }
1222
1223 static struct gm_join *igmp_join_find(struct list *join_list,
1224 struct in_addr group_addr,
1225 struct in_addr source_addr)
1226 {
1227 struct listnode *node;
1228 struct gm_join *ij;
1229
1230 assert(join_list);
1231
1232 for (ALL_LIST_ELEMENTS_RO(join_list, node, ij)) {
1233 if ((group_addr.s_addr == ij->group_addr.s_addr)
1234 && (source_addr.s_addr == ij->source_addr.s_addr))
1235 return ij;
1236 }
1237
1238 return 0;
1239 }
1240
1241 static int igmp_join_sock(const char *ifname, ifindex_t ifindex,
1242 struct in_addr group_addr, struct in_addr source_addr,
1243 struct pim_interface *pim_ifp)
1244 {
1245 int join_fd;
1246
1247 pim_ifp->igmp_ifstat_joins_sent++;
1248
1249 join_fd = pim_socket_raw(IPPROTO_IGMP);
1250 if (join_fd < 0) {
1251 pim_ifp->igmp_ifstat_joins_failed++;
1252 return -1;
1253 }
1254
1255 if (pim_igmp_join_source(join_fd, ifindex, group_addr, source_addr)) {
1256 char group_str[INET_ADDRSTRLEN];
1257 char source_str[INET_ADDRSTRLEN];
1258 pim_inet4_dump("<grp?>", group_addr, group_str,
1259 sizeof(group_str));
1260 pim_inet4_dump("<src?>", source_addr, source_str,
1261 sizeof(source_str));
1262 zlog_warn(
1263 "%s: setsockopt(fd=%d) failure for IGMP group %s source %s ifindex %d on interface %s: errno=%d: %s",
1264 __func__, join_fd, group_str, source_str, ifindex,
1265 ifname, errno, safe_strerror(errno));
1266
1267 pim_ifp->igmp_ifstat_joins_failed++;
1268
1269 close(join_fd);
1270 return -2;
1271 }
1272
1273 return join_fd;
1274 }
1275
1276 #if PIM_IPV == 4
1277 static struct gm_join *igmp_join_new(struct interface *ifp,
1278 struct in_addr group_addr,
1279 struct in_addr source_addr)
1280 {
1281 struct pim_interface *pim_ifp;
1282 struct gm_join *ij;
1283 int join_fd;
1284
1285 pim_ifp = ifp->info;
1286 assert(pim_ifp);
1287
1288 join_fd = igmp_join_sock(ifp->name, ifp->ifindex, group_addr,
1289 source_addr, pim_ifp);
1290 if (join_fd < 0) {
1291 char group_str[INET_ADDRSTRLEN];
1292 char source_str[INET_ADDRSTRLEN];
1293
1294 pim_inet4_dump("<grp?>", group_addr, group_str,
1295 sizeof(group_str));
1296 pim_inet4_dump("<src?>", source_addr, source_str,
1297 sizeof(source_str));
1298 zlog_warn(
1299 "%s: igmp_join_sock() failure for IGMP group %s source %s on interface %s",
1300 __func__, group_str, source_str, ifp->name);
1301 return 0;
1302 }
1303
1304 ij = XCALLOC(MTYPE_PIM_IGMP_JOIN, sizeof(*ij));
1305
1306 ij->sock_fd = join_fd;
1307 ij->group_addr = group_addr;
1308 ij->source_addr = source_addr;
1309 ij->sock_creation = pim_time_monotonic_sec();
1310
1311 listnode_add(pim_ifp->gm_join_list, ij);
1312
1313 return ij;
1314 }
1315 #endif /* PIM_IPV == 4 */
1316
1317 #if PIM_IPV == 4
1318 ferr_r pim_if_igmp_join_add(struct interface *ifp, struct in_addr group_addr,
1319 struct in_addr source_addr)
1320 {
1321 struct pim_interface *pim_ifp;
1322 struct gm_join *ij;
1323
1324 pim_ifp = ifp->info;
1325 if (!pim_ifp) {
1326 return ferr_cfg_invalid("multicast not enabled on interface %s",
1327 ifp->name);
1328 }
1329
1330 if (!pim_ifp->gm_join_list) {
1331 pim_ifp->gm_join_list = list_new();
1332 pim_ifp->gm_join_list->del = (void (*)(void *))igmp_join_free;
1333 }
1334
1335 ij = igmp_join_find(pim_ifp->gm_join_list, group_addr, source_addr);
1336
1337 /* This interface has already been configured to join this IGMP group
1338 */
1339 if (ij) {
1340 return ferr_ok();
1341 }
1342
1343 (void)igmp_join_new(ifp, group_addr, source_addr);
1344
1345 if (PIM_DEBUG_GM_EVENTS) {
1346 char group_str[INET_ADDRSTRLEN];
1347 char source_str[INET_ADDRSTRLEN];
1348 pim_inet4_dump("<grp?>", group_addr, group_str,
1349 sizeof(group_str));
1350 pim_inet4_dump("<src?>", source_addr, source_str,
1351 sizeof(source_str));
1352 zlog_debug(
1353 "%s: issued static igmp join for channel (S,G)=(%s,%s) on interface %s",
1354 __func__, source_str, group_str, ifp->name);
1355 }
1356
1357 return ferr_ok();
1358 }
1359 #endif /* PIM_IPV == 4 */
1360
1361 int pim_if_igmp_join_del(struct interface *ifp, struct in_addr group_addr,
1362 struct in_addr source_addr)
1363 {
1364 struct pim_interface *pim_ifp;
1365 struct gm_join *ij;
1366
1367 pim_ifp = ifp->info;
1368 if (!pim_ifp) {
1369 zlog_warn("%s: multicast not enabled on interface %s", __func__,
1370 ifp->name);
1371 return -1;
1372 }
1373
1374 if (!pim_ifp->gm_join_list) {
1375 zlog_warn("%s: no IGMP join on interface %s", __func__,
1376 ifp->name);
1377 return -2;
1378 }
1379
1380 ij = igmp_join_find(pim_ifp->gm_join_list, group_addr, source_addr);
1381 if (!ij) {
1382 char group_str[INET_ADDRSTRLEN];
1383 char source_str[INET_ADDRSTRLEN];
1384 pim_inet4_dump("<grp?>", group_addr, group_str,
1385 sizeof(group_str));
1386 pim_inet4_dump("<src?>", source_addr, source_str,
1387 sizeof(source_str));
1388 zlog_warn(
1389 "%s: could not find IGMP group %s source %s on interface %s",
1390 __func__, group_str, source_str, ifp->name);
1391 return -3;
1392 }
1393
1394 if (close(ij->sock_fd)) {
1395 char group_str[INET_ADDRSTRLEN];
1396 char source_str[INET_ADDRSTRLEN];
1397 pim_inet4_dump("<grp?>", group_addr, group_str,
1398 sizeof(group_str));
1399 pim_inet4_dump("<src?>", source_addr, source_str,
1400 sizeof(source_str));
1401 zlog_warn(
1402 "%s: failure closing sock_fd=%d for IGMP group %s source %s on interface %s: errno=%d: %s",
1403 __func__, ij->sock_fd, group_str, source_str, ifp->name,
1404 errno, safe_strerror(errno));
1405 /* warning only */
1406 }
1407 listnode_delete(pim_ifp->gm_join_list, ij);
1408 igmp_join_free(ij);
1409 if (listcount(pim_ifp->gm_join_list) < 1) {
1410 list_delete(&pim_ifp->gm_join_list);
1411 pim_ifp->gm_join_list = 0;
1412 }
1413
1414 return 0;
1415 }
1416
1417 __attribute__((unused))
1418 static void pim_if_igmp_join_del_all(struct interface *ifp)
1419 {
1420 struct pim_interface *pim_ifp;
1421 struct listnode *node;
1422 struct listnode *nextnode;
1423 struct gm_join *ij;
1424
1425 pim_ifp = ifp->info;
1426 if (!pim_ifp) {
1427 zlog_warn("%s: multicast not enabled on interface %s", __func__,
1428 ifp->name);
1429 return;
1430 }
1431
1432 if (!pim_ifp->gm_join_list)
1433 return;
1434
1435 for (ALL_LIST_ELEMENTS(pim_ifp->gm_join_list, node, nextnode, ij))
1436 pim_if_igmp_join_del(ifp, ij->group_addr, ij->source_addr);
1437 }
1438 #else /* PIM_IPV != 4 */
1439 ferr_r pim_if_igmp_join_add(struct interface *ifp, struct in_addr group_addr,
1440 struct in_addr source_addr)
1441 {
1442 return ferr_ok();
1443 }
1444
1445 int pim_if_igmp_join_del(struct interface *ifp, struct in_addr group_addr,
1446 struct in_addr source_addr)
1447 {
1448 return 0;
1449 }
1450 #endif /* PIM_IPV != 4 */
1451
1452 /*
1453 RFC 4601
1454
1455 Transitions from "I am Assert Loser" State
1456
1457 Current Winner's GenID Changes or NLT Expires
1458
1459 The Neighbor Liveness Timer associated with the current winner
1460 expires or we receive a Hello message from the current winner
1461 reporting a different GenID from the one it previously reported.
1462 This indicates that the current winner's interface or router has
1463 gone down (and may have come back up), and so we must assume it no
1464 longer knows it was the winner.
1465 */
1466 void pim_if_assert_on_neighbor_down(struct interface *ifp, pim_addr neigh_addr)
1467 {
1468 struct pim_interface *pim_ifp;
1469 struct pim_ifchannel *ch;
1470
1471 pim_ifp = ifp->info;
1472 assert(pim_ifp);
1473
1474 RB_FOREACH (ch, pim_ifchannel_rb, &pim_ifp->ifchannel_rb) {
1475 /* Is (S,G,I) assert loser ? */
1476 if (ch->ifassert_state != PIM_IFASSERT_I_AM_LOSER)
1477 continue;
1478 /* Dead neighbor was winner ? */
1479 if (pim_addr_cmp(ch->ifassert_winner, neigh_addr))
1480 continue;
1481
1482 assert_action_a5(ch);
1483 }
1484 }
1485
1486 void pim_if_update_join_desired(struct pim_interface *pim_ifp)
1487 {
1488 struct pim_ifchannel *ch;
1489
1490 /* clear off flag from interface's upstreams */
1491 RB_FOREACH (ch, pim_ifchannel_rb, &pim_ifp->ifchannel_rb) {
1492 PIM_UPSTREAM_FLAG_UNSET_DR_JOIN_DESIRED_UPDATED(
1493 ch->upstream->flags);
1494 }
1495
1496 /* scan per-interface (S,G,I) state on this I interface */
1497 RB_FOREACH (ch, pim_ifchannel_rb, &pim_ifp->ifchannel_rb) {
1498 struct pim_upstream *up = ch->upstream;
1499
1500 if (PIM_UPSTREAM_FLAG_TEST_DR_JOIN_DESIRED_UPDATED(up->flags))
1501 continue;
1502
1503 /* update join_desired for the global (S,G) state */
1504 pim_upstream_update_join_desired(pim_ifp->pim, up);
1505 PIM_UPSTREAM_FLAG_SET_DR_JOIN_DESIRED_UPDATED(up->flags);
1506 }
1507 }
1508
1509 void pim_if_update_assert_tracking_desired(struct interface *ifp)
1510 {
1511 struct pim_interface *pim_ifp;
1512 struct pim_ifchannel *ch;
1513
1514 pim_ifp = ifp->info;
1515 if (!pim_ifp)
1516 return;
1517
1518 RB_FOREACH (ch, pim_ifchannel_rb, &pim_ifp->ifchannel_rb) {
1519 pim_ifchannel_update_assert_tracking_desired(ch);
1520 }
1521 }
1522
1523 /*
1524 * PIM wants to have an interface pointer for everything it does.
1525 * The pimreg is a special interface that we have that is not
1526 * quite an interface but a VIF is created for it.
1527 */
1528 void pim_if_create_pimreg(struct pim_instance *pim)
1529 {
1530 char pimreg_name[INTERFACE_NAMSIZ];
1531
1532 if (!pim->regiface) {
1533 if (pim->vrf->vrf_id == VRF_DEFAULT)
1534 strlcpy(pimreg_name, PIMREG, sizeof(pimreg_name));
1535 else
1536 snprintf(pimreg_name, sizeof(pimreg_name), PIMREG "%u",
1537 pim->vrf->data.l.table_id);
1538
1539 pim->regiface = if_get_by_name(pimreg_name, pim->vrf->vrf_id,
1540 pim->vrf->name);
1541 pim->regiface->ifindex = PIM_OIF_PIM_REGISTER_VIF;
1542
1543 if (!pim->regiface->info)
1544 pim_if_new(pim->regiface, false, false, true,
1545 false /*vxlan_term*/);
1546
1547 /*
1548 * On vrf moves we delete the interface if there
1549 * is nothing going on with it. We cannot have
1550 * the pimregiface deleted.
1551 */
1552 pim->regiface->configured = true;
1553
1554 }
1555 }
1556
1557 struct prefix *pim_if_connected_to_source(struct interface *ifp, pim_addr src)
1558 {
1559 struct listnode *cnode;
1560 struct connected *c;
1561 struct prefix p;
1562
1563 if (!ifp)
1564 return NULL;
1565
1566 pim_addr_to_prefix(&p, src);
1567
1568 for (ALL_LIST_ELEMENTS_RO(ifp->connected, cnode, c)) {
1569 if (c->address->family != PIM_AF)
1570 continue;
1571 if (prefix_match(c->address, &p))
1572 return c->address;
1573 if (CONNECTED_PEER(c) && prefix_match(c->destination, &p))
1574 /* this is not a typo, on PtP we need to return the
1575 * *local* address that lines up with src.
1576 */
1577 return c->address;
1578 }
1579
1580 return NULL;
1581 }
1582
1583 bool pim_if_is_vrf_device(struct interface *ifp)
1584 {
1585 if (if_is_vrf(ifp))
1586 return true;
1587
1588 return false;
1589 }
1590
1591 int pim_if_ifchannel_count(struct pim_interface *pim_ifp)
1592 {
1593 struct pim_ifchannel *ch;
1594 int count = 0;
1595
1596 RB_FOREACH (ch, pim_ifchannel_rb, &pim_ifp->ifchannel_rb) {
1597 count++;
1598 }
1599
1600 return count;
1601 }
1602
1603 static int pim_ifp_create(struct interface *ifp)
1604 {
1605 struct pim_instance *pim;
1606
1607 pim = ifp->vrf->info;
1608 if (PIM_DEBUG_ZEBRA) {
1609 zlog_debug(
1610 "%s: %s index %d vrf %s(%u) flags %ld metric %d mtu %d operative %d",
1611 __func__, ifp->name, ifp->ifindex, ifp->vrf->name,
1612 ifp->vrf->vrf_id, (long)ifp->flags, ifp->metric,
1613 ifp->mtu, if_is_operative(ifp));
1614 }
1615
1616 if (if_is_operative(ifp)) {
1617 struct pim_interface *pim_ifp;
1618
1619 pim_ifp = ifp->info;
1620 /*
1621 * If we have a pim_ifp already and this is an if_add
1622 * that means that we probably have a vrf move event
1623 * If that is the case, set the proper vrfness.
1624 */
1625 if (pim_ifp)
1626 pim_ifp->pim = pim;
1627 pim_if_addr_add_all(ifp);
1628
1629 /*
1630 * Due to ordering issues based upon when
1631 * a command is entered we should ensure that
1632 * the pim reg is created for this vrf if we
1633 * have configuration for it already.
1634 *
1635 * this is a no-op if it's already been done.
1636 */
1637 pim_if_create_pimreg(pim);
1638 }
1639
1640 #if PIM_IPV == 4
1641 /*
1642 * If we are a vrf device that is up, open up the pim_socket for
1643 * listening
1644 * to incoming pim messages irrelevant if the user has configured us
1645 * for pim or not.
1646 */
1647 if (pim_if_is_vrf_device(ifp)) {
1648 struct pim_interface *pim_ifp;
1649
1650 if (!ifp->info) {
1651 pim_ifp = pim_if_new(ifp, false, false, false,
1652 false /*vxlan_term*/);
1653 ifp->info = pim_ifp;
1654 }
1655
1656 pim_sock_add(ifp);
1657 }
1658
1659 if (!strncmp(ifp->name, PIM_VXLAN_TERM_DEV_NAME,
1660 sizeof(PIM_VXLAN_TERM_DEV_NAME))) {
1661 if (pim->mcast_if_count < MAXVIFS)
1662 pim_vxlan_add_term_dev(pim, ifp);
1663 else
1664 zlog_warn(
1665 "%s: Cannot enable pim on %s. MAXVIFS(%d) reached. Deleting and readding the vxlan termimation device after unconfiguring pim from other interfaces may succeed.",
1666 __func__, ifp->name, MAXVIFS);
1667 }
1668 #endif
1669
1670 return 0;
1671 }
1672
1673 static int pim_ifp_up(struct interface *ifp)
1674 {
1675 uint32_t table_id;
1676 struct pim_interface *pim_ifp;
1677 struct pim_instance *pim;
1678
1679 if (PIM_DEBUG_ZEBRA) {
1680 zlog_debug(
1681 "%s: %s index %d vrf %s(%u) flags %ld metric %d mtu %d operative %d",
1682 __func__, ifp->name, ifp->ifindex, ifp->vrf->name,
1683 ifp->vrf->vrf_id, (long)ifp->flags, ifp->metric,
1684 ifp->mtu, if_is_operative(ifp));
1685 }
1686
1687 pim = ifp->vrf->info;
1688
1689 pim_ifp = ifp->info;
1690 /*
1691 * If we have a pim_ifp already and this is an if_add
1692 * that means that we probably have a vrf move event
1693 * If that is the case, set the proper vrfness.
1694 */
1695 if (pim_ifp)
1696 pim_ifp->pim = pim;
1697
1698 /*
1699 pim_if_addr_add_all() suffices for bringing up both IGMP and
1700 PIM
1701 */
1702 pim_if_addr_add_all(ifp);
1703
1704 /*
1705 * If we have a pimreg device callback and it's for a specific
1706 * table set the master appropriately
1707 */
1708 if (sscanf(ifp->name, "" PIMREG "%" SCNu32, &table_id) == 1) {
1709 struct vrf *vrf;
1710 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
1711 if ((table_id == vrf->data.l.table_id)
1712 && (ifp->vrf->vrf_id != vrf->vrf_id)) {
1713 struct interface *master = if_lookup_by_name(
1714 vrf->name, vrf->vrf_id);
1715
1716 if (!master) {
1717 zlog_debug(
1718 "%s: Unable to find Master interface for %s",
1719 __func__, vrf->name);
1720 return 0;
1721 }
1722 pim_zebra_interface_set_master(master, ifp);
1723 }
1724 }
1725 }
1726 return 0;
1727 }
1728
1729 static int pim_ifp_down(struct interface *ifp)
1730 {
1731 if (PIM_DEBUG_ZEBRA) {
1732 zlog_debug(
1733 "%s: %s index %d vrf %s(%u) flags %ld metric %d mtu %d operative %d",
1734 __func__, ifp->name, ifp->ifindex, ifp->vrf->name,
1735 ifp->vrf->vrf_id, (long)ifp->flags, ifp->metric,
1736 ifp->mtu, if_is_operative(ifp));
1737 }
1738
1739 if (!if_is_operative(ifp)) {
1740 pim_ifchannel_delete_all(ifp);
1741 /*
1742 pim_if_addr_del_all() suffices for shutting down IGMP,
1743 but not for shutting down PIM
1744 */
1745 pim_if_addr_del_all(ifp);
1746
1747 /*
1748 pim_sock_delete() closes the socket, stops read and timer
1749 threads,
1750 and kills all neighbors.
1751 */
1752 if (ifp->info) {
1753 pim_sock_delete(ifp, "link down");
1754 }
1755 }
1756
1757 if (ifp->info) {
1758 pim_if_del_vif(ifp);
1759 pim_ifstat_reset(ifp);
1760 }
1761
1762 return 0;
1763 }
1764
1765 static int pim_ifp_destroy(struct interface *ifp)
1766 {
1767 if (PIM_DEBUG_ZEBRA) {
1768 zlog_debug(
1769 "%s: %s index %d vrf %s(%u) flags %ld metric %d mtu %d operative %d",
1770 __func__, ifp->name, ifp->ifindex, ifp->vrf->name,
1771 ifp->vrf->vrf_id, (long)ifp->flags, ifp->metric,
1772 ifp->mtu, if_is_operative(ifp));
1773 }
1774
1775 if (!if_is_operative(ifp))
1776 pim_if_addr_del_all(ifp);
1777
1778 #if PIM_IPV == 4
1779 struct pim_instance *pim;
1780
1781 pim = ifp->vrf->info;
1782 if (pim && pim->vxlan.term_if == ifp)
1783 pim_vxlan_del_term_dev(pim);
1784 #endif
1785
1786 return 0;
1787 }
1788
1789 static int pim_if_new_hook(struct interface *ifp)
1790 {
1791 return 0;
1792 }
1793
1794 static int pim_if_delete_hook(struct interface *ifp)
1795 {
1796 if (ifp->info)
1797 pim_if_delete(ifp);
1798
1799 return 0;
1800 }
1801
1802 void pim_iface_init(void)
1803 {
1804 hook_register_prio(if_add, 0, pim_if_new_hook);
1805 hook_register_prio(if_del, 0, pim_if_delete_hook);
1806
1807 if_zapi_callbacks(pim_ifp_create, pim_ifp_up, pim_ifp_down,
1808 pim_ifp_destroy);
1809 }