3 * Copyright (C) 2008 Everton da Silva Marques
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; see the file COPYING; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include "pim_instance.h"
34 #include "pim_zebra.h"
35 #include "pim_iface.h"
37 #include "pim_mroute.h"
41 #include "pim_neighbor.h"
42 #include "pim_ifchannel.h"
45 #include "pim_ssmpingd.h"
48 #include "pim_jp_agg.h"
49 #include "pim_igmp_join.h"
50 #include "pim_vxlan.h"
52 static void pim_if_igmp_join_del_all(struct interface
*ifp
);
53 static int igmp_join_sock(const char *ifname
, ifindex_t ifindex
,
54 struct in_addr group_addr
,
55 struct in_addr source_addr
);
57 void pim_if_init(struct pim_instance
*pim
)
61 for (i
= 0; i
< MAXVIFS
; i
++)
62 pim
->iface_vif_index
[i
] = 0;
65 void pim_if_terminate(struct pim_instance
*pim
)
67 struct interface
*ifp
;
69 FOR_ALL_INTERFACES (pim
->vrf
, ifp
) {
70 struct pim_interface
*pim_ifp
= ifp
->info
;
80 static void pim_sec_addr_free(struct pim_secondary_addr
*sec_addr
)
82 XFREE(MTYPE_PIM_SEC_ADDR
, sec_addr
);
85 static int pim_sec_addr_comp(const void *p1
, const void *p2
)
87 const struct pim_secondary_addr
*sec1
= p1
;
88 const struct pim_secondary_addr
*sec2
= p2
;
90 if (sec1
->addr
.family
== AF_INET
&& sec2
->addr
.family
== AF_INET6
)
93 if (sec1
->addr
.family
== AF_INET6
&& sec2
->addr
.family
== AF_INET
)
96 if (sec1
->addr
.family
== AF_INET
) {
97 if (ntohl(sec1
->addr
.u
.prefix4
.s_addr
)
98 < ntohl(sec2
->addr
.u
.prefix4
.s_addr
))
101 if (ntohl(sec1
->addr
.u
.prefix4
.s_addr
)
102 > ntohl(sec2
->addr
.u
.prefix4
.s_addr
))
105 return memcmp(&sec1
->addr
.u
.prefix6
, &sec2
->addr
.u
.prefix6
,
106 sizeof(struct in6_addr
));
112 struct pim_interface
*pim_if_new(struct interface
*ifp
, bool igmp
, bool pim
,
113 bool ispimreg
, bool is_vxlan_term
)
115 struct pim_interface
*pim_ifp
;
120 pim_ifp
= XCALLOC(MTYPE_PIM_INTERFACE
, sizeof(*pim_ifp
));
122 pim_ifp
->options
= 0;
123 pim_ifp
->pim
= pim_get_pim_instance(ifp
->vrf_id
);
124 pim_ifp
->mroute_vif_index
= -1;
126 pim_ifp
->igmp_version
= IGMP_DEFAULT_VERSION
;
127 pim_ifp
->igmp_default_robustness_variable
=
128 IGMP_DEFAULT_ROBUSTNESS_VARIABLE
;
129 pim_ifp
->igmp_default_query_interval
= IGMP_GENERAL_QUERY_INTERVAL
;
130 pim_ifp
->igmp_query_max_response_time_dsec
=
131 IGMP_QUERY_MAX_RESPONSE_TIME_DSEC
;
132 pim_ifp
->igmp_specific_query_max_response_time_dsec
=
133 IGMP_SPECIFIC_QUERY_MAX_RESPONSE_TIME_DSEC
;
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].
140 zassert(pim_ifp
->igmp_query_max_response_time_dsec
141 < pim_ifp
->igmp_default_query_interval
);
144 PIM_IF_DO_PIM(pim_ifp
->options
);
146 PIM_IF_DO_IGMP(pim_ifp
->options
);
148 PIM_IF_DO_IGMP_LISTEN_ALLROUTERS(pim_ifp
->options
);
150 pim_ifp
->igmp_join_list
= NULL
;
151 pim_ifp
->igmp_socket_list
= NULL
;
152 pim_ifp
->pim_neighbor_list
= NULL
;
153 pim_ifp
->upstream_switch_list
= NULL
;
154 pim_ifp
->pim_generation_id
= 0;
156 /* list of struct igmp_sock */
157 pim_ifp
->igmp_socket_list
= list_new();
158 pim_ifp
->igmp_socket_list
->del
= (void (*)(void *))igmp_sock_free
;
160 /* list of struct pim_neighbor */
161 pim_ifp
->pim_neighbor_list
= list_new();
162 pim_ifp
->pim_neighbor_list
->del
= (void (*)(void *))pim_neighbor_free
;
164 pim_ifp
->upstream_switch_list
= list_new();
165 pim_ifp
->upstream_switch_list
->del
=
166 (void (*)(void *))pim_jp_agg_group_list_free
;
167 pim_ifp
->upstream_switch_list
->cmp
= pim_jp_agg_group_list_cmp
;
169 pim_ifp
->sec_addr_list
= list_new();
170 pim_ifp
->sec_addr_list
->del
= (void (*)(void *))pim_sec_addr_free
;
171 pim_ifp
->sec_addr_list
->cmp
=
172 (int (*)(void *, void *))pim_sec_addr_comp
;
174 pim_ifp
->activeactive
= false;
176 RB_INIT(pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
);
182 pim_if_add_vif(ifp
, ispimreg
, is_vxlan_term
);
187 void pim_if_delete(struct interface
*ifp
)
189 struct pim_interface
*pim_ifp
;
190 struct pim_ifchannel
*ch
;
196 if (pim_ifp
->igmp_join_list
) {
197 pim_if_igmp_join_del_all(ifp
);
200 pim_ifchannel_delete_all(ifp
);
201 igmp_sock_delete_all(ifp
);
203 pim_neighbor_delete_all(ifp
, "Interface removed from configuration");
207 list_delete(&pim_ifp
->igmp_socket_list
);
208 list_delete(&pim_ifp
->pim_neighbor_list
);
209 list_delete(&pim_ifp
->upstream_switch_list
);
210 list_delete(&pim_ifp
->sec_addr_list
);
212 XFREE(MTYPE_PIM_INTERFACE
, pim_ifp
->boundary_oil_plist
);
214 while (!RB_EMPTY(pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
)) {
215 ch
= RB_ROOT(pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
);
217 pim_ifchannel_delete(ch
);
220 XFREE(MTYPE_PIM_INTERFACE
, pim_ifp
);
225 void pim_if_update_could_assert(struct interface
*ifp
)
227 struct pim_interface
*pim_ifp
;
228 struct pim_ifchannel
*ch
;
233 RB_FOREACH (ch
, pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
) {
234 pim_ifchannel_update_could_assert(ch
);
238 static void pim_if_update_my_assert_metric(struct interface
*ifp
)
240 struct pim_interface
*pim_ifp
;
241 struct pim_ifchannel
*ch
;
246 RB_FOREACH (ch
, pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
) {
247 pim_ifchannel_update_my_assert_metric(ch
);
251 static void pim_addr_change(struct interface
*ifp
)
253 struct pim_interface
*pim_ifp
;
258 pim_if_dr_election(ifp
); /* router's own DR Priority (addr) changes --
260 pim_if_update_join_desired(pim_ifp
); /* depends on DR */
261 pim_if_update_could_assert(ifp
); /* depends on DR */
262 pim_if_update_my_assert_metric(ifp
); /* depends on could_assert */
263 pim_if_update_assert_tracking_desired(
264 ifp
); /* depends on DR, join_desired */
267 RFC 4601: 4.3.1. Sending Hello Messages
269 1) Before an interface goes down or changes primary IP address, a
270 Hello message with a zero HoldTime should be sent immediately
271 (with the old IP address if the IP address changed).
272 -- FIXME See CAVEAT C13
274 2) After an interface has changed its IP address, it MUST send a
275 Hello message with its new IP address.
278 3) If an interface changes one of its secondary IP addresses, a
279 Hello message with an updated Address_List option and a non-zero
280 HoldTime should be sent immediately.
281 -- FIXME See TODO T31
283 pim_ifp
->pim_ifstat_hello_sent
= 0; /* reset hello counter */
284 if (pim_ifp
->pim_sock_fd
< 0)
286 pim_hello_restart_now(ifp
); /* send hello and restart timer */
289 static int detect_primary_address_change(struct interface
*ifp
,
290 int force_prim_as_any
,
293 struct pim_interface
*pim_ifp
= ifp
->info
;
294 struct in_addr new_prim_addr
;
297 if (force_prim_as_any
)
298 new_prim_addr
.s_addr
= INADDR_ANY
;
300 new_prim_addr
= pim_find_primary_addr(ifp
);
302 changed
= new_prim_addr
.s_addr
!= pim_ifp
->primary_address
.s_addr
;
304 if (PIM_DEBUG_ZEBRA
) {
305 char new_prim_str
[INET_ADDRSTRLEN
];
306 char old_prim_str
[INET_ADDRSTRLEN
];
307 pim_inet4_dump("<new?>", new_prim_addr
, new_prim_str
,
308 sizeof(new_prim_str
));
309 pim_inet4_dump("<old?>", pim_ifp
->primary_address
, old_prim_str
,
310 sizeof(old_prim_str
));
311 zlog_debug("%s: old=%s new=%s on interface %s: %s",
312 __PRETTY_FUNCTION__
, old_prim_str
, new_prim_str
,
313 ifp
->name
, changed
? "changed" : "unchanged");
317 pim_ifp
->primary_address
= new_prim_addr
;
323 static struct pim_secondary_addr
*
324 pim_sec_addr_find(struct pim_interface
*pim_ifp
, struct prefix
*addr
)
326 struct pim_secondary_addr
*sec_addr
;
327 struct listnode
*node
;
329 for (ALL_LIST_ELEMENTS_RO(pim_ifp
->sec_addr_list
, node
, sec_addr
)) {
330 if (prefix_cmp(&sec_addr
->addr
, addr
)) {
338 static void pim_sec_addr_del(struct pim_interface
*pim_ifp
,
339 struct pim_secondary_addr
*sec_addr
)
341 listnode_delete(pim_ifp
->sec_addr_list
, sec_addr
);
342 pim_sec_addr_free(sec_addr
);
345 static int pim_sec_addr_add(struct pim_interface
*pim_ifp
, struct prefix
*addr
)
348 struct pim_secondary_addr
*sec_addr
;
350 sec_addr
= pim_sec_addr_find(pim_ifp
, addr
);
352 sec_addr
->flags
&= ~PIM_SEC_ADDRF_STALE
;
356 sec_addr
= XCALLOC(MTYPE_PIM_SEC_ADDR
, sizeof(*sec_addr
));
359 sec_addr
->addr
= *addr
;
360 listnode_add_sort(pim_ifp
->sec_addr_list
, sec_addr
);
365 static int pim_sec_addr_del_all(struct pim_interface
*pim_ifp
)
369 if (!list_isempty(pim_ifp
->sec_addr_list
)) {
371 /* remove all nodes and free up the list itself */
372 list_delete_all_node(pim_ifp
->sec_addr_list
);
378 static int pim_sec_addr_update(struct interface
*ifp
)
380 struct pim_interface
*pim_ifp
= ifp
->info
;
381 struct connected
*ifc
;
382 struct listnode
*node
;
383 struct listnode
*nextnode
;
384 struct pim_secondary_addr
*sec_addr
;
387 for (ALL_LIST_ELEMENTS_RO(pim_ifp
->sec_addr_list
, node
, sec_addr
)) {
388 sec_addr
->flags
|= PIM_SEC_ADDRF_STALE
;
391 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
392 struct prefix
*p
= ifc
->address
;
394 if (PIM_INADDR_IS_ANY(p
->u
.prefix4
)) {
398 if (pim_ifp
->primary_address
.s_addr
== p
->u
.prefix4
.s_addr
) {
399 /* don't add the primary address into the secondary
404 if (pim_sec_addr_add(pim_ifp
, p
)) {
409 /* Drop stale entries */
410 for (ALL_LIST_ELEMENTS(pim_ifp
->sec_addr_list
, node
, nextnode
,
412 if (sec_addr
->flags
& PIM_SEC_ADDRF_STALE
) {
413 pim_sec_addr_del(pim_ifp
, sec_addr
);
421 static int detect_secondary_address_change(struct interface
*ifp
,
422 int force_prim_as_any
,
425 struct pim_interface
*pim_ifp
= ifp
->info
;
428 if (force_prim_as_any
) {
429 /* if primary address is being forced to zero just flush the
430 * secondary address list */
431 changed
= pim_sec_addr_del_all(pim_ifp
);
433 /* re-evaluate the secondary address list */
434 changed
= pim_sec_addr_update(ifp
);
440 static void detect_address_change(struct interface
*ifp
, int force_prim_as_any
,
444 struct pim_interface
*pim_ifp
;
450 if (detect_primary_address_change(ifp
, force_prim_as_any
, caller
)) {
454 if (detect_secondary_address_change(ifp
, force_prim_as_any
, caller
)) {
460 if (!PIM_IF_TEST_PIM(pim_ifp
->options
)) {
464 pim_addr_change(ifp
);
467 /* XXX: if we have unnumbered interfaces we need to run detect address
468 * address change on all of them when the lo address changes */
471 int pim_update_source_set(struct interface
*ifp
, struct in_addr source
)
473 struct pim_interface
*pim_ifp
= ifp
->info
;
476 return PIM_IFACE_NOT_FOUND
;
479 if (pim_ifp
->update_source
.s_addr
== source
.s_addr
) {
480 return PIM_UPDATE_SOURCE_DUP
;
483 pim_ifp
->update_source
= source
;
484 detect_address_change(ifp
, 0 /* force_prim_as_any */,
485 __PRETTY_FUNCTION__
);
490 void pim_if_addr_add(struct connected
*ifc
)
492 struct pim_interface
*pim_ifp
;
493 struct interface
*ifp
;
494 struct in_addr ifaddr
;
504 if (!if_is_operative(ifp
))
507 if (PIM_DEBUG_ZEBRA
) {
509 prefix2str(ifc
->address
, buf
, BUFSIZ
);
510 zlog_debug("%s: %s ifindex=%d connected IP address %s %s",
511 __PRETTY_FUNCTION__
, ifp
->name
, ifp
->ifindex
, buf
,
512 CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
)
517 ifaddr
= ifc
->address
->u
.prefix4
;
519 detect_address_change(ifp
, 0, __PRETTY_FUNCTION__
);
521 // if (ifc->address->family != AF_INET)
524 if (PIM_IF_TEST_IGMP(pim_ifp
->options
)) {
525 struct igmp_sock
*igmp
;
527 /* lookup IGMP socket */
528 igmp
= pim_igmp_sock_lookup_ifaddr(pim_ifp
->igmp_socket_list
,
531 /* if addr new, add IGMP socket */
532 if (ifc
->address
->family
== AF_INET
)
533 pim_igmp_sock_add(pim_ifp
->igmp_socket_list
,
535 } else if (igmp
->mtrace_only
) {
536 igmp_sock_delete(igmp
);
537 pim_igmp_sock_add(pim_ifp
->igmp_socket_list
, ifaddr
,
541 /* Replay Static IGMP groups */
542 if (pim_ifp
->igmp_join_list
) {
543 struct listnode
*node
;
544 struct listnode
*nextnode
;
545 struct igmp_join
*ij
;
548 for (ALL_LIST_ELEMENTS(pim_ifp
->igmp_join_list
, node
,
550 /* Close socket and reopen with Source and Group
553 join_fd
= igmp_join_sock(
554 ifp
->name
, ifp
->ifindex
, ij
->group_addr
,
557 char group_str
[INET_ADDRSTRLEN
];
558 char source_str
[INET_ADDRSTRLEN
];
559 pim_inet4_dump("<grp?>", ij
->group_addr
,
563 "<src?>", ij
->source_addr
,
564 source_str
, sizeof(source_str
));
566 "%s: igmp_join_sock() failure for IGMP group %s source %s on interface %s",
567 __PRETTY_FUNCTION__
, group_str
,
568 source_str
, ifp
->name
);
571 ij
->sock_fd
= join_fd
;
576 struct igmp_sock
*igmp
;
578 /* lookup IGMP socket */
579 igmp
= pim_igmp_sock_lookup_ifaddr(pim_ifp
->igmp_socket_list
,
581 if (ifc
->address
->family
== AF_INET
) {
583 igmp_sock_delete(igmp
);
584 /* if addr new, add IGMP socket */
585 pim_igmp_sock_add(pim_ifp
->igmp_socket_list
, ifaddr
,
588 } /* igmp mtrace only */
590 if (PIM_IF_TEST_PIM(pim_ifp
->options
)) {
592 if (PIM_INADDR_ISNOT_ANY(pim_ifp
->primary_address
)) {
594 /* Interface has a valid socket ? */
595 if (pim_ifp
->pim_sock_fd
< 0) {
596 if (pim_sock_add(ifp
)) {
598 "Failure creating PIM socket for interface %s",
602 struct pim_nexthop_cache
*pnc
= NULL
;
604 struct zclient
*zclient
= NULL
;
606 zclient
= pim_zebra_zclient_get();
607 /* RP config might come prior to (local RP's interface)
609 In this case, pnc would not have pim enabled
611 Once Interface is UP and pim info is available,
613 with RNH address to receive update and add the
614 interface as nexthop. */
615 memset(&rpf
, 0, sizeof(struct pim_rpf
));
616 rpf
.rpf_addr
.family
= AF_INET
;
617 rpf
.rpf_addr
.prefixlen
= IPV4_MAX_BITLEN
;
618 rpf
.rpf_addr
.u
.prefix4
= ifc
->address
->u
.prefix4
;
619 pnc
= pim_nexthop_cache_find(pim_ifp
->pim
, &rpf
);
621 pim_sendmsg_zebra_rnh(pim_ifp
->pim
, zclient
,
623 ZEBRA_NEXTHOP_REGISTER
);
628 PIM or IGMP is enabled on interface, and there is at least one
629 address assigned, then try to create a vif_index.
631 if (pim_ifp
->mroute_vif_index
< 0) {
632 pim_if_add_vif(ifp
, false, false /*vxlan_term*/);
634 pim_ifchannel_scan_forward_start(ifp
);
637 static void pim_if_addr_del_igmp(struct connected
*ifc
)
639 struct pim_interface
*pim_ifp
= ifc
->ifp
->info
;
640 struct igmp_sock
*igmp
;
641 struct in_addr ifaddr
;
643 if (ifc
->address
->family
!= AF_INET
) {
644 /* non-IPv4 address */
649 /* IGMP not enabled on interface */
653 ifaddr
= ifc
->address
->u
.prefix4
;
655 /* lookup IGMP socket */
656 igmp
= pim_igmp_sock_lookup_ifaddr(pim_ifp
->igmp_socket_list
, ifaddr
);
658 /* if addr found, del IGMP socket */
659 igmp_sock_delete(igmp
);
663 static void pim_if_addr_del_pim(struct connected
*ifc
)
665 struct pim_interface
*pim_ifp
= ifc
->ifp
->info
;
667 if (ifc
->address
->family
!= AF_INET
) {
668 /* non-IPv4 address */
673 /* PIM not enabled on interface */
677 if (PIM_INADDR_ISNOT_ANY(pim_ifp
->primary_address
)) {
678 /* Interface keeps a valid primary address */
682 if (pim_ifp
->pim_sock_fd
< 0) {
683 /* Interface does not hold a valid socket any longer */
688 pim_sock_delete() closes the socket, stops read and timer threads,
689 and kills all neighbors.
691 pim_sock_delete(ifc
->ifp
,
692 "last address has been removed from interface");
695 void pim_if_addr_del(struct connected
*ifc
, int force_prim_as_any
)
697 struct interface
*ifp
;
703 if (PIM_DEBUG_ZEBRA
) {
705 prefix2str(ifc
->address
, buf
, BUFSIZ
);
706 zlog_debug("%s: %s ifindex=%d disconnected IP address %s %s",
707 __PRETTY_FUNCTION__
, ifp
->name
, ifp
->ifindex
, buf
,
708 CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
)
713 detect_address_change(ifp
, force_prim_as_any
, __PRETTY_FUNCTION__
);
715 pim_if_addr_del_igmp(ifc
);
716 pim_if_addr_del_pim(ifc
);
719 void pim_if_addr_add_all(struct interface
*ifp
)
721 struct connected
*ifc
;
722 struct listnode
*node
;
723 struct listnode
*nextnode
;
726 struct pim_interface
*pim_ifp
= ifp
->info
;
729 /* PIM/IGMP enabled ? */
733 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nextnode
, ifc
)) {
734 struct prefix
*p
= ifc
->address
;
736 if (p
->family
!= AF_INET
)
740 pim_if_addr_add(ifc
);
743 if (!v4_addrs
&& v6_addrs
&& !if_is_loopback(ifp
)) {
744 if (PIM_IF_TEST_PIM(pim_ifp
->options
)) {
746 /* Interface has a valid primary address ? */
747 if (PIM_INADDR_ISNOT_ANY(pim_ifp
->primary_address
)) {
749 /* Interface has a valid socket ? */
750 if (pim_ifp
->pim_sock_fd
< 0) {
751 if (pim_sock_add(ifp
)) {
753 "Failure creating PIM socket for interface %s",
761 * PIM or IGMP is enabled on interface, and there is at least one
762 * address assigned, then try to create a vif_index.
764 if (pim_ifp
->mroute_vif_index
< 0) {
765 pim_if_add_vif(ifp
, false, false /*vxlan_term*/);
767 pim_ifchannel_scan_forward_start(ifp
);
769 pim_rp_setup(pim_ifp
->pim
);
770 pim_rp_check_on_if_add(pim_ifp
);
773 void pim_if_addr_del_all(struct interface
*ifp
)
775 struct connected
*ifc
;
776 struct listnode
*node
;
777 struct listnode
*nextnode
;
778 struct vrf
*vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
779 struct pim_instance
*pim
;
785 /* PIM/IGMP enabled ? */
789 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nextnode
, ifc
)) {
790 struct prefix
*p
= ifc
->address
;
792 if (p
->family
!= AF_INET
)
795 pim_if_addr_del(ifc
, 1 /* force_prim_as_any=true */);
799 pim_i_am_rp_re_evaluate(pim
);
802 void pim_if_addr_del_all_igmp(struct interface
*ifp
)
804 struct connected
*ifc
;
805 struct listnode
*node
;
806 struct listnode
*nextnode
;
808 /* PIM/IGMP enabled ? */
812 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nextnode
, ifc
)) {
813 struct prefix
*p
= ifc
->address
;
815 if (p
->family
!= AF_INET
)
818 pim_if_addr_del_igmp(ifc
);
822 void pim_if_addr_del_all_pim(struct interface
*ifp
)
824 struct connected
*ifc
;
825 struct listnode
*node
;
826 struct listnode
*nextnode
;
828 /* PIM/IGMP enabled ? */
832 for (ALL_LIST_ELEMENTS(ifp
->connected
, node
, nextnode
, ifc
)) {
833 struct prefix
*p
= ifc
->address
;
835 if (p
->family
!= AF_INET
)
838 pim_if_addr_del_pim(ifc
);
842 struct in_addr
pim_find_primary_addr(struct interface
*ifp
)
844 struct connected
*ifc
;
845 struct listnode
*node
;
846 struct in_addr addr
= {0};
849 struct pim_interface
*pim_ifp
= ifp
->info
;
850 struct vrf
*vrf
= vrf_lookup_by_id(ifp
->vrf_id
);
855 if (pim_ifp
&& PIM_INADDR_ISNOT_ANY(pim_ifp
->update_source
)) {
856 return pim_ifp
->update_source
;
859 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
860 struct prefix
*p
= ifc
->address
;
862 if (p
->family
!= AF_INET
) {
867 if (PIM_INADDR_IS_ANY(p
->u
.prefix4
)) {
869 "%s: null IPv4 address connected to interface %s",
870 __PRETTY_FUNCTION__
, ifp
->name
);
876 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_SECONDARY
))
883 * If we have no v4_addrs and v6 is configured
884 * We probably are using unnumbered
885 * So let's grab the loopbacks v4 address
886 * and use that as the primary address
888 if (!v4_addrs
&& v6_addrs
&& !if_is_loopback(ifp
)) {
889 struct interface
*lo_ifp
;
890 // DBS - Come back and check here
891 if (ifp
->vrf_id
== VRF_DEFAULT
)
892 lo_ifp
= if_lookup_by_name("lo", vrf
->vrf_id
);
894 lo_ifp
= if_lookup_by_name(vrf
->name
, vrf
->vrf_id
);
897 return pim_find_primary_addr(lo_ifp
);
900 addr
.s_addr
= PIM_NET_INADDR_ANY
;
905 static int pim_iface_next_vif_index(struct interface
*ifp
)
907 struct pim_interface
*pim_ifp
= ifp
->info
;
908 struct pim_instance
*pim
= pim_ifp
->pim
;
912 * The pimreg vif is always going to be in index 0
915 if (ifp
->ifindex
== PIM_OIF_PIM_REGISTER_VIF
)
918 for (i
= 1; i
< MAXVIFS
; i
++) {
919 if (pim
->iface_vif_index
[i
] == 0)
926 pim_if_add_vif() uses ifindex as vif_index
928 see also pim_if_find_vifindex_by_ifindex()
930 int pim_if_add_vif(struct interface
*ifp
, bool ispimreg
, bool is_vxlan_term
)
932 struct pim_interface
*pim_ifp
= ifp
->info
;
933 struct in_addr ifaddr
;
934 unsigned char flags
= 0;
938 if (pim_ifp
->mroute_vif_index
> 0) {
939 zlog_warn("%s: vif_index=%d > 0 on interface %s ifindex=%d",
940 __PRETTY_FUNCTION__
, pim_ifp
->mroute_vif_index
,
941 ifp
->name
, ifp
->ifindex
);
945 if (ifp
->ifindex
< 0) {
946 zlog_warn("%s: ifindex=%d < 1 on interface %s",
947 __PRETTY_FUNCTION__
, ifp
->ifindex
, ifp
->name
);
951 ifaddr
= pim_ifp
->primary_address
;
952 if (!ispimreg
&& !is_vxlan_term
&& PIM_INADDR_IS_ANY(ifaddr
)) {
954 "%s: could not get address for interface %s ifindex=%d",
955 __PRETTY_FUNCTION__
, ifp
->name
, ifp
->ifindex
);
959 pim_ifp
->mroute_vif_index
= pim_iface_next_vif_index(ifp
);
961 if (pim_ifp
->mroute_vif_index
>= MAXVIFS
) {
963 "%s: Attempting to configure more than MAXVIFS=%d on pim enabled interface %s",
964 __PRETTY_FUNCTION__
, MAXVIFS
, ifp
->name
);
968 if (ifp
->ifindex
== PIM_OIF_PIM_REGISTER_VIF
)
969 flags
= VIFF_REGISTER
;
970 #ifdef VIFF_USE_IFINDEX
972 flags
= VIFF_USE_IFINDEX
;
975 if (pim_mroute_add_vif(ifp
, ifaddr
, flags
)) {
976 /* pim_mroute_add_vif reported error */
980 pim_ifp
->pim
->iface_vif_index
[pim_ifp
->mroute_vif_index
] = 1;
982 /* if the device qualifies as pim_vxlan iif/oif update vxlan entries */
983 pim_vxlan_add_vif(ifp
);
988 int pim_if_del_vif(struct interface
*ifp
)
990 struct pim_interface
*pim_ifp
= ifp
->info
;
992 if (pim_ifp
->mroute_vif_index
< 1) {
993 zlog_warn("%s: vif_index=%d < 1 on interface %s ifindex=%d",
994 __PRETTY_FUNCTION__
, pim_ifp
->mroute_vif_index
,
995 ifp
->name
, ifp
->ifindex
);
999 /* if the device was a pim_vxlan iif/oif update vxlan mroute entries */
1000 pim_vxlan_del_vif(ifp
);
1002 pim_mroute_del_vif(ifp
);
1007 pim_ifp
->pim
->iface_vif_index
[pim_ifp
->mroute_vif_index
] = 0;
1009 pim_ifp
->mroute_vif_index
= -1;
1015 struct interface
*pim_if_find_by_vif_index(struct pim_instance
*pim
,
1016 ifindex_t vif_index
)
1018 struct interface
*ifp
;
1020 FOR_ALL_INTERFACES (pim
->vrf
, ifp
) {
1022 struct pim_interface
*pim_ifp
;
1023 pim_ifp
= ifp
->info
;
1025 if (vif_index
== pim_ifp
->mroute_vif_index
)
1034 pim_if_add_vif() uses ifindex as vif_index
1036 int pim_if_find_vifindex_by_ifindex(struct pim_instance
*pim
, ifindex_t ifindex
)
1038 struct pim_interface
*pim_ifp
;
1039 struct interface
*ifp
;
1041 ifp
= if_lookup_by_index(ifindex
, pim
->vrf_id
);
1042 if (!ifp
|| !ifp
->info
)
1044 pim_ifp
= ifp
->info
;
1046 return pim_ifp
->mroute_vif_index
;
1049 int pim_if_lan_delay_enabled(struct interface
*ifp
)
1051 struct pim_interface
*pim_ifp
;
1053 pim_ifp
= ifp
->info
;
1055 zassert(pim_ifp
->pim_number_of_nonlandelay_neighbors
>= 0);
1057 return pim_ifp
->pim_number_of_nonlandelay_neighbors
== 0;
1060 uint16_t pim_if_effective_propagation_delay_msec(struct interface
*ifp
)
1062 if (pim_if_lan_delay_enabled(ifp
)) {
1063 struct pim_interface
*pim_ifp
;
1064 pim_ifp
= ifp
->info
;
1065 return pim_ifp
->pim_neighbors_highest_propagation_delay_msec
;
1067 return PIM_DEFAULT_PROPAGATION_DELAY_MSEC
;
1071 uint16_t pim_if_effective_override_interval_msec(struct interface
*ifp
)
1073 if (pim_if_lan_delay_enabled(ifp
)) {
1074 struct pim_interface
*pim_ifp
;
1075 pim_ifp
= ifp
->info
;
1076 return pim_ifp
->pim_neighbors_highest_override_interval_msec
;
1078 return PIM_DEFAULT_OVERRIDE_INTERVAL_MSEC
;
1082 int pim_if_t_override_msec(struct interface
*ifp
)
1084 int effective_override_interval_msec
;
1085 int t_override_msec
;
1087 effective_override_interval_msec
=
1088 pim_if_effective_override_interval_msec(ifp
);
1090 t_override_msec
= random() % (effective_override_interval_msec
+ 1);
1092 return t_override_msec
;
1095 uint16_t pim_if_jp_override_interval_msec(struct interface
*ifp
)
1097 return pim_if_effective_propagation_delay_msec(ifp
)
1098 + pim_if_effective_override_interval_msec(ifp
);
1102 RFC 4601: 4.1.6. State Summarization Macros
1104 The function NBR( I, A ) uses information gathered through PIM Hello
1105 messages to map the IP address A of a directly connected PIM
1106 neighbor router on interface I to the primary IP address of the same
1107 router (Section 4.3.4). The primary IP address of a neighbor is the
1108 address that it uses as the source of its PIM Hello messages.
1110 struct pim_neighbor
*pim_if_find_neighbor(struct interface
*ifp
,
1111 struct in_addr addr
)
1113 struct listnode
*neighnode
;
1114 struct pim_neighbor
*neigh
;
1115 struct pim_interface
*pim_ifp
;
1120 pim_ifp
= ifp
->info
;
1122 zlog_warn("%s: multicast not enabled on interface %s",
1123 __PRETTY_FUNCTION__
, ifp
->name
);
1129 p
.prefixlen
= IPV4_MAX_PREFIXLEN
;
1131 for (ALL_LIST_ELEMENTS_RO(pim_ifp
->pim_neighbor_list
, neighnode
,
1134 /* primary address ? */
1135 if (neigh
->source_addr
.s_addr
== addr
.s_addr
)
1138 /* secondary address ? */
1139 if (pim_neighbor_find_secondary(neigh
, &p
))
1143 if (PIM_DEBUG_PIM_TRACE
) {
1144 char addr_str
[INET_ADDRSTRLEN
];
1145 pim_inet4_dump("<addr?>", addr
, addr_str
, sizeof(addr_str
));
1147 "%s: neighbor not found for address %s on interface %s",
1148 __PRETTY_FUNCTION__
, addr_str
, ifp
->name
);
1154 long pim_if_t_suppressed_msec(struct interface
*ifp
)
1156 struct pim_interface
*pim_ifp
;
1157 long t_suppressed_msec
;
1158 uint32_t ramount
= 0;
1160 pim_ifp
= ifp
->info
;
1163 /* join suppression disabled ? */
1164 if (PIM_IF_TEST_PIM_CAN_DISABLE_JOIN_SUPRESSION(pim_ifp
->options
))
1167 /* t_suppressed = t_periodic * rand(1.1, 1.4) */
1168 ramount
= 1100 + (random() % (1400 - 1100 + 1));
1169 t_suppressed_msec
= router
->t_periodic
* ramount
;
1171 return t_suppressed_msec
;
1174 static void igmp_join_free(struct igmp_join
*ij
)
1176 XFREE(MTYPE_PIM_IGMP_JOIN
, ij
);
1179 static struct igmp_join
*igmp_join_find(struct list
*join_list
,
1180 struct in_addr group_addr
,
1181 struct in_addr source_addr
)
1183 struct listnode
*node
;
1184 struct igmp_join
*ij
;
1188 for (ALL_LIST_ELEMENTS_RO(join_list
, node
, ij
)) {
1189 if ((group_addr
.s_addr
== ij
->group_addr
.s_addr
)
1190 && (source_addr
.s_addr
== ij
->source_addr
.s_addr
))
1197 static int igmp_join_sock(const char *ifname
, ifindex_t ifindex
,
1198 struct in_addr group_addr
, struct in_addr source_addr
)
1202 join_fd
= pim_socket_raw(IPPROTO_IGMP
);
1207 if (pim_igmp_join_source(join_fd
, ifindex
, group_addr
, source_addr
)) {
1208 char group_str
[INET_ADDRSTRLEN
];
1209 char source_str
[INET_ADDRSTRLEN
];
1210 pim_inet4_dump("<grp?>", group_addr
, group_str
,
1212 pim_inet4_dump("<src?>", source_addr
, source_str
,
1213 sizeof(source_str
));
1215 "%s: setsockopt(fd=%d) failure for IGMP group %s source %s ifindex %d on interface %s: errno=%d: %s",
1216 __PRETTY_FUNCTION__
, join_fd
, group_str
, source_str
,
1217 ifindex
, ifname
, errno
, safe_strerror(errno
));
1226 static struct igmp_join
*igmp_join_new(struct interface
*ifp
,
1227 struct in_addr group_addr
,
1228 struct in_addr source_addr
)
1230 struct pim_interface
*pim_ifp
;
1231 struct igmp_join
*ij
;
1234 pim_ifp
= ifp
->info
;
1237 join_fd
= igmp_join_sock(ifp
->name
, ifp
->ifindex
, group_addr
,
1240 char group_str
[INET_ADDRSTRLEN
];
1241 char source_str
[INET_ADDRSTRLEN
];
1243 pim_inet4_dump("<grp?>", group_addr
, group_str
,
1245 pim_inet4_dump("<src?>", source_addr
, source_str
,
1246 sizeof(source_str
));
1248 "%s: igmp_join_sock() failure for IGMP group %s source %s on interface %s",
1249 __PRETTY_FUNCTION__
, group_str
, source_str
, ifp
->name
);
1253 ij
= XCALLOC(MTYPE_PIM_IGMP_JOIN
, sizeof(*ij
));
1255 ij
->sock_fd
= join_fd
;
1256 ij
->group_addr
= group_addr
;
1257 ij
->source_addr
= source_addr
;
1258 ij
->sock_creation
= pim_time_monotonic_sec();
1260 listnode_add(pim_ifp
->igmp_join_list
, ij
);
1265 ferr_r
pim_if_igmp_join_add(struct interface
*ifp
, struct in_addr group_addr
,
1266 struct in_addr source_addr
)
1268 struct pim_interface
*pim_ifp
;
1269 struct igmp_join
*ij
;
1271 pim_ifp
= ifp
->info
;
1273 return ferr_cfg_invalid("multicast not enabled on interface %s",
1277 if (!pim_ifp
->igmp_join_list
) {
1278 pim_ifp
->igmp_join_list
= list_new();
1279 pim_ifp
->igmp_join_list
->del
= (void (*)(void *))igmp_join_free
;
1282 ij
= igmp_join_find(pim_ifp
->igmp_join_list
, group_addr
, source_addr
);
1284 /* This interface has already been configured to join this IGMP group
1290 ij
= igmp_join_new(ifp
, group_addr
, source_addr
);
1292 return ferr_cfg_invalid(
1293 "Failure to create new join data structure, see log file for more information");
1296 if (PIM_DEBUG_IGMP_EVENTS
) {
1297 char group_str
[INET_ADDRSTRLEN
];
1298 char source_str
[INET_ADDRSTRLEN
];
1299 pim_inet4_dump("<grp?>", group_addr
, group_str
,
1301 pim_inet4_dump("<src?>", source_addr
, source_str
,
1302 sizeof(source_str
));
1304 "%s: issued static igmp join for channel (S,G)=(%s,%s) on interface %s",
1305 __PRETTY_FUNCTION__
, source_str
, group_str
, ifp
->name
);
1312 int pim_if_igmp_join_del(struct interface
*ifp
, struct in_addr group_addr
,
1313 struct in_addr source_addr
)
1315 struct pim_interface
*pim_ifp
;
1316 struct igmp_join
*ij
;
1318 pim_ifp
= ifp
->info
;
1320 zlog_warn("%s: multicast not enabled on interface %s",
1321 __PRETTY_FUNCTION__
, ifp
->name
);
1325 if (!pim_ifp
->igmp_join_list
) {
1326 zlog_warn("%s: no IGMP join on interface %s",
1327 __PRETTY_FUNCTION__
, ifp
->name
);
1331 ij
= igmp_join_find(pim_ifp
->igmp_join_list
, group_addr
, source_addr
);
1333 char group_str
[INET_ADDRSTRLEN
];
1334 char source_str
[INET_ADDRSTRLEN
];
1335 pim_inet4_dump("<grp?>", group_addr
, group_str
,
1337 pim_inet4_dump("<src?>", source_addr
, source_str
,
1338 sizeof(source_str
));
1340 "%s: could not find IGMP group %s source %s on interface %s",
1341 __PRETTY_FUNCTION__
, group_str
, source_str
, ifp
->name
);
1345 if (close(ij
->sock_fd
)) {
1346 char group_str
[INET_ADDRSTRLEN
];
1347 char source_str
[INET_ADDRSTRLEN
];
1348 pim_inet4_dump("<grp?>", group_addr
, group_str
,
1350 pim_inet4_dump("<src?>", source_addr
, source_str
,
1351 sizeof(source_str
));
1353 "%s: failure closing sock_fd=%d for IGMP group %s source %s on interface %s: errno=%d: %s",
1354 __PRETTY_FUNCTION__
, ij
->sock_fd
, group_str
, source_str
,
1355 ifp
->name
, errno
, safe_strerror(errno
));
1358 listnode_delete(pim_ifp
->igmp_join_list
, ij
);
1360 if (listcount(pim_ifp
->igmp_join_list
) < 1) {
1361 list_delete(&pim_ifp
->igmp_join_list
);
1362 pim_ifp
->igmp_join_list
= 0;
1368 static void pim_if_igmp_join_del_all(struct interface
*ifp
)
1370 struct pim_interface
*pim_ifp
;
1371 struct listnode
*node
;
1372 struct listnode
*nextnode
;
1373 struct igmp_join
*ij
;
1375 pim_ifp
= ifp
->info
;
1377 zlog_warn("%s: multicast not enabled on interface %s",
1378 __PRETTY_FUNCTION__
, ifp
->name
);
1382 if (!pim_ifp
->igmp_join_list
)
1385 for (ALL_LIST_ELEMENTS(pim_ifp
->igmp_join_list
, node
, nextnode
, ij
))
1386 pim_if_igmp_join_del(ifp
, ij
->group_addr
, ij
->source_addr
);
1392 Transitions from "I am Assert Loser" State
1394 Current Winner's GenID Changes or NLT Expires
1396 The Neighbor Liveness Timer associated with the current winner
1397 expires or we receive a Hello message from the current winner
1398 reporting a different GenID from the one it previously reported.
1399 This indicates that the current winner's interface or router has
1400 gone down (and may have come back up), and so we must assume it no
1401 longer knows it was the winner.
1403 void pim_if_assert_on_neighbor_down(struct interface
*ifp
,
1404 struct in_addr neigh_addr
)
1406 struct pim_interface
*pim_ifp
;
1407 struct pim_ifchannel
*ch
;
1409 pim_ifp
= ifp
->info
;
1412 RB_FOREACH (ch
, pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
) {
1413 /* Is (S,G,I) assert loser ? */
1414 if (ch
->ifassert_state
!= PIM_IFASSERT_I_AM_LOSER
)
1416 /* Dead neighbor was winner ? */
1417 if (ch
->ifassert_winner
.s_addr
!= neigh_addr
.s_addr
)
1420 assert_action_a5(ch
);
1424 void pim_if_update_join_desired(struct pim_interface
*pim_ifp
)
1426 struct pim_ifchannel
*ch
;
1428 /* clear off flag from interface's upstreams */
1429 RB_FOREACH (ch
, pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
) {
1430 PIM_UPSTREAM_FLAG_UNSET_DR_JOIN_DESIRED_UPDATED(
1431 ch
->upstream
->flags
);
1434 /* scan per-interface (S,G,I) state on this I interface */
1435 RB_FOREACH (ch
, pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
) {
1436 struct pim_upstream
*up
= ch
->upstream
;
1438 if (PIM_UPSTREAM_FLAG_TEST_DR_JOIN_DESIRED_UPDATED(up
->flags
))
1441 /* update join_desired for the global (S,G) state */
1442 pim_upstream_update_join_desired(pim_ifp
->pim
, up
);
1443 PIM_UPSTREAM_FLAG_SET_DR_JOIN_DESIRED_UPDATED(up
->flags
);
1447 void pim_if_update_assert_tracking_desired(struct interface
*ifp
)
1449 struct pim_interface
*pim_ifp
;
1450 struct pim_ifchannel
*ch
;
1452 pim_ifp
= ifp
->info
;
1456 RB_FOREACH (ch
, pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
) {
1457 pim_ifchannel_update_assert_tracking_desired(ch
);
1462 * PIM wants to have an interface pointer for everything it does.
1463 * The pimreg is a special interface that we have that is not
1464 * quite an inteface but a VIF is created for it.
1466 void pim_if_create_pimreg(struct pim_instance
*pim
)
1468 char pimreg_name
[INTERFACE_NAMSIZ
];
1470 if (!pim
->regiface
) {
1471 if (pim
->vrf_id
== VRF_DEFAULT
)
1472 strlcpy(pimreg_name
, "pimreg", sizeof(pimreg_name
));
1474 snprintf(pimreg_name
, sizeof(pimreg_name
), "pimreg%u",
1475 pim
->vrf
->data
.l
.table_id
);
1477 pim
->regiface
= if_create(pimreg_name
, pim
->vrf_id
);
1478 pim
->regiface
->ifindex
= PIM_OIF_PIM_REGISTER_VIF
;
1480 pim_if_new(pim
->regiface
, false, false, true,
1481 false /*vxlan_term*/);
1485 int pim_if_connected_to_source(struct interface
*ifp
, struct in_addr src
)
1487 struct listnode
*cnode
;
1488 struct connected
*c
;
1496 p
.prefixlen
= IPV4_MAX_BITLEN
;
1498 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
1499 if ((c
->address
->family
== AF_INET
)
1500 && prefix_match(CONNECTED_PREFIX(c
), &p
)) {
1508 bool pim_if_is_vrf_device(struct interface
*ifp
)
1516 int pim_if_ifchannel_count(struct pim_interface
*pim_ifp
)
1518 struct pim_ifchannel
*ch
;
1521 RB_FOREACH (ch
, pim_ifchannel_rb
, &pim_ifp
->ifchannel_rb
) {