3 * Copyright (C) 2008 Everton da Silva Marques
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.
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.
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
28 #include "lib_errors.h"
32 #include "pim_mroute.h"
36 #include "pim_iface.h"
37 #include "pim_macro.h"
40 #include "pim_register.h"
41 #include "pim_ifchannel.h"
42 #include "pim_zlookup.h"
45 #include "pim_vxlan.h"
47 static void mroute_read_on(struct pim_instance
*pim
);
49 static int pim_mroute_set(struct pim_instance
*pim
, int enable
)
53 socklen_t data_len
= sizeof(data
);
57 * We need to create the VRF table for the pim mroute_socket
59 if (pim
->vrf_id
!= VRF_DEFAULT
) {
60 frr_with_privs(&pimd_privs
) {
62 data
= pim
->vrf
->data
.l
.table_id
;
63 err
= setsockopt(pim
->mroute_socket
, IPPROTO_IP
,
68 "%s %s: failure: setsockopt(fd=%d,IPPROTO_IP, MRT_TABLE=%d): errno=%d: %s",
69 __FILE__
, __PRETTY_FUNCTION__
,
70 pim
->mroute_socket
, data
, errno
,
71 safe_strerror(errno
));
78 frr_with_privs(&pimd_privs
) {
79 opt
= enable
? MRT_INIT
: MRT_DONE
;
81 * *BSD *cares* about what value we pass down
85 err
= setsockopt(pim
->mroute_socket
, IPPROTO_IP
,
86 opt
, &data
, data_len
);
89 "%s %s: failure: setsockopt(fd=%d,IPPROTO_IP,%s=%d): errno=%d: %s",
90 __FILE__
, __PRETTY_FUNCTION__
,
92 enable
? "MRT_INIT" : "MRT_DONE", data
, errno
,
93 safe_strerror(errno
));
98 #if defined(HAVE_IP_PKTINFO)
100 /* Linux and Solaris IP_PKTINFO */
102 if (setsockopt(pim
->mroute_socket
, IPPROTO_IP
, IP_PKTINFO
,
105 "Could not set IP_PKTINFO on socket fd=%d: errno=%d: %s",
106 pim
->mroute_socket
, errno
,
107 safe_strerror(errno
));
112 setsockopt_so_recvbuf(pim
->mroute_socket
, 1024 * 1024 * 8);
114 flags
= fcntl(pim
->mroute_socket
, F_GETFL
, 0);
116 zlog_warn("Could not get flags on socket fd:%d %d %s",
117 pim
->mroute_socket
, errno
, safe_strerror(errno
));
118 close(pim
->mroute_socket
);
121 if (fcntl(pim
->mroute_socket
, F_SETFL
, flags
| O_NONBLOCK
)) {
122 zlog_warn("Could not set O_NONBLOCK on socket fd:%d %d %s",
123 pim
->mroute_socket
, errno
, safe_strerror(errno
));
124 close(pim
->mroute_socket
);
130 int upcalls
= IGMPMSG_WRVIFWHOLE
;
133 err
= setsockopt(pim
->mroute_socket
, IPPROTO_IP
, opt
, &upcalls
,
137 "Failure to register for VIFWHOLE and WRONGVIF upcalls %d %s",
138 errno
, safe_strerror(errno
));
143 "PIM-SM will not work properly on this platform, until the ability to receive the WRVIFWHOLE upcall");
150 static const char *const igmpmsgtype2str
[IGMPMSG_WRVIFWHOLE
+ 1] = {
151 "<unknown_upcall?>", "NOCACHE", "WRONGVIF", "WHOLEPKT", "WRVIFWHOLE"};
153 static int pim_mroute_msg_nocache(int fd
, struct interface
*ifp
,
154 const struct igmpmsg
*msg
)
156 struct pim_interface
*pim_ifp
= ifp
->info
;
157 struct pim_upstream
*up
;
161 rpg
= pim_ifp
? RP(pim_ifp
->pim
, msg
->im_dst
) : NULL
;
163 * If the incoming interface is unknown OR
164 * the Interface type is SSM we don't need to
167 if (!rpg
|| pim_rpf_addr_is_inaddr_none(rpg
)) {
168 if (PIM_DEBUG_MROUTE_DETAIL
)
170 "%s: Interface is not configured correctly to handle incoming packet: Could be !pim_ifp, !SM, !RP",
171 __PRETTY_FUNCTION__
);
177 * If we've received a multicast packet that isn't connected to
180 if (!pim_if_connected_to_source(ifp
, msg
->im_src
)) {
181 if (PIM_DEBUG_MROUTE_DETAIL
)
183 "%s: Received incoming packet that doesn't originate on our seg",
184 __PRETTY_FUNCTION__
);
188 memset(&sg
, 0, sizeof(struct prefix_sg
));
189 sg
.src
= msg
->im_src
;
190 sg
.grp
= msg
->im_dst
;
192 if (!(PIM_I_am_DR(pim_ifp
))) {
193 if (PIM_DEBUG_MROUTE_DETAIL
)
194 zlog_debug("%s: Interface is not the DR blackholing incoming traffic for %s",
195 __PRETTY_FUNCTION__
, pim_str_sg_dump(&sg
));
198 * We are not the DR, but we are still receiving packets
199 * Let's blackhole those packets for the moment
200 * As that they will be coming up to the cpu
201 * and causing us to consider them.
203 * This *will* create a dangling channel_oil
204 * that I see no way to get rid of. Just noting
205 * this for future reference.
207 up
= pim_upstream_find_or_add(
208 &sg
, ifp
, PIM_UPSTREAM_FLAG_MASK_SRC_NOCACHE
,
209 __PRETTY_FUNCTION__
);
210 pim_upstream_mroute_add(up
->channel_oil
, __PRETTY_FUNCTION__
);
215 up
= pim_upstream_find_or_add(&sg
, ifp
, PIM_UPSTREAM_FLAG_MASK_FHR
,
216 __PRETTY_FUNCTION__
);
219 * I moved this debug till after the actual add because
220 * I want to take advantage of the up->sg_str being filled in.
222 if (PIM_DEBUG_MROUTE
) {
223 zlog_debug("%s: Adding a Route %s for WHOLEPKT consumption",
224 __PRETTY_FUNCTION__
, up
->sg_str
);
227 PIM_UPSTREAM_FLAG_SET_SRC_STREAM(up
->flags
);
228 pim_upstream_keep_alive_timer_start(up
, pim_ifp
->pim
->keep_alive_time
);
230 up
->channel_oil
->cc
.pktcnt
++;
231 // resolve mfcc_parent prior to mroute_add in channel_add_oif
232 if (up
->rpf
.source_nexthop
.interface
&&
233 up
->channel_oil
->oil
.mfcc_parent
>= MAXVIFS
) {
234 pim_upstream_mroute_iif_update(up
->channel_oil
, __func__
);
236 pim_register_join(up
);
241 static int pim_mroute_msg_wholepkt(int fd
, struct interface
*ifp
,
244 struct pim_interface
*pim_ifp
;
247 const struct ip
*ip_hdr
;
248 struct pim_upstream
*up
;
252 ip_hdr
= (const struct ip
*)buf
;
254 memset(&sg
, 0, sizeof(struct prefix_sg
));
255 sg
.src
= ip_hdr
->ip_src
;
256 sg
.grp
= ip_hdr
->ip_dst
;
258 up
= pim_upstream_find(pim_ifp
->pim
, &sg
);
260 struct prefix_sg star
= sg
;
261 star
.src
.s_addr
= INADDR_ANY
;
263 up
= pim_upstream_find(pim_ifp
->pim
, &star
);
265 if (up
&& PIM_UPSTREAM_FLAG_TEST_SRC_IGMP(up
->flags
)) {
266 up
= pim_upstream_add(pim_ifp
->pim
, &sg
, ifp
,
267 PIM_UPSTREAM_FLAG_MASK_SRC_LHR
,
268 __PRETTY_FUNCTION__
, NULL
);
270 if (PIM_DEBUG_MROUTE
)
272 "%s: Unable to create upstream information for %s",
274 pim_str_sg_dump(&sg
));
277 pim_upstream_keep_alive_timer_start(
278 up
, pim_ifp
->pim
->keep_alive_time
);
279 pim_upstream_inherited_olist(pim_ifp
->pim
, up
);
280 pim_upstream_update_join_desired(pim_ifp
->pim
, up
);
282 if (PIM_DEBUG_MROUTE
)
283 zlog_debug("%s: Creating %s upstream on LHR",
284 __PRETTY_FUNCTION__
, up
->sg_str
);
287 if (PIM_DEBUG_MROUTE_DETAIL
) {
289 "%s: Unable to find upstream channel WHOLEPKT%s",
290 __PRETTY_FUNCTION__
, pim_str_sg_dump(&sg
));
295 if (!up
->rpf
.source_nexthop
.interface
) {
296 if (PIM_DEBUG_PIM_TRACE
)
297 zlog_debug("%s: up %s RPF is not present",
298 __PRETTY_FUNCTION__
, up
->sg_str
);
302 pim_ifp
= up
->rpf
.source_nexthop
.interface
->info
;
304 rpg
= pim_ifp
? RP(pim_ifp
->pim
, sg
.grp
) : NULL
;
306 if ((pim_rpf_addr_is_inaddr_none(rpg
)) || (!pim_ifp
)
307 || (!(PIM_I_am_DR(pim_ifp
)))) {
308 if (PIM_DEBUG_MROUTE
) {
309 zlog_debug("%s: Failed Check send packet",
310 __PRETTY_FUNCTION__
);
316 * If we've received a register suppress
318 if (!up
->t_rs_timer
) {
319 if (pim_is_grp_ssm(pim_ifp
->pim
, sg
.grp
)) {
320 if (PIM_DEBUG_PIM_REG
)
322 "%s register forward skipped as group is SSM",
323 pim_str_sg_dump(&sg
));
327 if (!PIM_UPSTREAM_FLAG_TEST_FHR(up
->flags
)) {
328 if (PIM_DEBUG_PIM_REG
)
330 "%s register forward skipped, not FHR",
335 pim_register_send((uint8_t *)buf
+ sizeof(struct ip
),
336 ntohs(ip_hdr
->ip_len
) - sizeof(struct ip
),
337 pim_ifp
->primary_address
, rpg
, 0, up
);
342 static int pim_mroute_msg_wrongvif(int fd
, struct interface
*ifp
,
343 const struct igmpmsg
*msg
)
345 struct pim_ifchannel
*ch
;
346 struct pim_interface
*pim_ifp
;
349 memset(&sg
, 0, sizeof(struct prefix_sg
));
350 sg
.src
= msg
->im_src
;
351 sg
.grp
= msg
->im_dst
;
354 Send Assert(S,G) on iif as response to WRONGVIF kernel upcall.
356 RFC 4601 4.8.2. PIM-SSM-Only Routers
358 iif is the incoming interface of the packet.
359 if (iif is in inherited_olist(S,G)) {
360 send Assert(S,G) on iif
365 if (PIM_DEBUG_MROUTE
)
367 "%s: WRONGVIF (S,G)=%s could not find input interface for input_vif_index=%d",
368 __PRETTY_FUNCTION__
, pim_str_sg_dump(&sg
),
375 if (PIM_DEBUG_MROUTE
)
377 "%s: WRONGVIF (S,G)=%s multicast not enabled on interface %s",
378 __PRETTY_FUNCTION__
, pim_str_sg_dump(&sg
),
383 ch
= pim_ifchannel_find(ifp
, &sg
);
385 struct prefix_sg star_g
= sg
;
386 if (PIM_DEBUG_MROUTE
)
388 "%s: WRONGVIF (S,G)=%s could not find channel on interface %s",
389 __PRETTY_FUNCTION__
, pim_str_sg_dump(&sg
),
392 star_g
.src
.s_addr
= INADDR_ANY
;
393 ch
= pim_ifchannel_find(ifp
, &star_g
);
395 if (PIM_DEBUG_MROUTE
)
397 "%s: WRONGVIF (*,G)=%s could not find channel on interface %s",
399 pim_str_sg_dump(&star_g
), ifp
->name
);
405 RFC 4601: 4.6.1. (S,G) Assert Message State Machine
407 Transitions from NoInfo State
409 An (S,G) data packet arrives on interface I, AND
410 CouldAssert(S,G,I)==TRUE An (S,G) data packet arrived on an
411 downstream interface that is in our (S,G) outgoing interface
412 list. We optimistically assume that we will be the assert
413 winner for this (S,G), and so we transition to the "I am Assert
414 Winner" state and perform Actions A1 (below), which will
415 initiate the assert negotiation for (S,G).
418 if (ch
->ifassert_state
!= PIM_IFASSERT_NOINFO
) {
419 if (PIM_DEBUG_MROUTE
) {
421 "%s: WRONGVIF (S,G)=%s channel is not on Assert NoInfo state for interface %s",
422 __PRETTY_FUNCTION__
, ch
->sg_str
, ifp
->name
);
427 if (!PIM_IF_FLAG_TEST_COULD_ASSERT(ch
->flags
)) {
428 if (PIM_DEBUG_MROUTE
) {
430 "%s: WRONGVIF (S,G)=%s interface %s is not downstream for channel",
431 __PRETTY_FUNCTION__
, ch
->sg_str
, ifp
->name
);
436 if (assert_action_a1(ch
)) {
437 if (PIM_DEBUG_MROUTE
) {
439 "%s: WRONGVIF (S,G)=%s assert_action_a1 failure on interface %s",
440 __PRETTY_FUNCTION__
, ch
->sg_str
, ifp
->name
);
448 static int pim_mroute_msg_wrvifwhole(int fd
, struct interface
*ifp
,
451 const struct ip
*ip_hdr
= (const struct ip
*)buf
;
452 struct pim_interface
*pim_ifp
;
453 struct pim_ifchannel
*ch
;
454 struct pim_upstream
*up
;
455 struct prefix_sg star_g
;
460 memset(&sg
, 0, sizeof(struct prefix_sg
));
461 sg
.src
= ip_hdr
->ip_src
;
462 sg
.grp
= ip_hdr
->ip_dst
;
464 ch
= pim_ifchannel_find(ifp
, &sg
);
466 if (PIM_DEBUG_MROUTE
)
468 "WRVIFWHOLE (S,G)=%s found ifchannel on interface %s",
469 ch
->sg_str
, ifp
->name
);
474 star_g
.src
.s_addr
= INADDR_ANY
;
476 ch
= pim_ifchannel_find(ifp
, &star_g
);
479 if (PIM_DEBUG_MROUTE
)
480 zlog_debug ("WRVIFWHOLE (*,G)=%s found ifchannel on interface %s",
481 pim_str_sg_dump (&star_g
), ifp
->name
);
486 up
= pim_upstream_find(pim_ifp
->pim
, &sg
);
488 struct pim_upstream
*parent
;
489 struct pim_nexthop source
;
490 struct pim_rpf
*rpf
= RP(pim_ifp
->pim
, sg
.grp
);
491 if (!rpf
|| !rpf
->source_nexthop
.interface
)
495 * If we have received a WRVIFWHOLE and are at this
496 * point, we could be receiving the packet on the *,G
497 * tree, let's check and if so we can safely drop
500 parent
= pim_upstream_find(pim_ifp
->pim
, &star_g
);
501 if (parent
&& parent
->rpf
.source_nexthop
.interface
== ifp
)
504 pim_ifp
= rpf
->source_nexthop
.interface
->info
;
506 memset(&source
, 0, sizeof(source
));
508 * If we are the fhr that means we are getting a callback during
509 * the pimreg period, so I believe we can ignore this packet
511 if (!PIM_UPSTREAM_FLAG_TEST_FHR(up
->flags
)) {
512 // No if channel, but upstream we are at the RP.
513 if (pim_nexthop_lookup(pim_ifp
->pim
, &source
,
514 up
->upstream_register
, 0)) {
515 pim_register_stop_send(source
.interface
, &sg
,
516 pim_ifp
->primary_address
,
517 up
->upstream_register
);
518 up
->sptbit
= PIM_UPSTREAM_SPTBIT_TRUE
;
521 pim_upstream_inherited_olist(pim_ifp
->pim
, up
);
522 if (!up
->channel_oil
->installed
)
523 pim_upstream_mroute_add(up
->channel_oil
,
524 __PRETTY_FUNCTION__
);
526 if (I_am_RP(pim_ifp
->pim
, up
->sg
.grp
)) {
527 if (pim_nexthop_lookup(pim_ifp
->pim
, &source
,
528 up
->upstream_register
,
530 pim_register_stop_send(
531 source
.interface
, &sg
,
532 pim_ifp
->primary_address
,
533 up
->upstream_register
);
534 up
->sptbit
= PIM_UPSTREAM_SPTBIT_TRUE
;
536 pim_upstream_keep_alive_timer_start(
537 up
, pim_ifp
->pim
->keep_alive_time
);
538 pim_upstream_inherited_olist(pim_ifp
->pim
, up
);
539 pim_mroute_msg_wholepkt(fd
, ifp
, buf
);
545 if (pim_if_connected_to_source(ifp
, sg
.src
)) {
546 up
= pim_upstream_add(pim_ifp
->pim
, &sg
, ifp
,
547 PIM_UPSTREAM_FLAG_MASK_FHR
,
548 __PRETTY_FUNCTION__
, NULL
);
550 if (PIM_DEBUG_MROUTE
)
552 "%s: WRONGVIF%s unable to create upstream on interface",
553 pim_str_sg_dump(&sg
), ifp
->name
);
556 PIM_UPSTREAM_FLAG_SET_SRC_STREAM(up
->flags
);
557 pim_upstream_keep_alive_timer_start(
558 up
, pim_ifp
->pim
->keep_alive_time
);
559 up
->channel_oil
->cc
.pktcnt
++;
560 pim_register_join(up
);
561 pim_upstream_inherited_olist(pim_ifp
->pim
, up
);
562 if (!up
->channel_oil
->installed
)
563 pim_upstream_mroute_add(up
->channel_oil
, __func__
);
565 // Send the packet to the RP
566 pim_mroute_msg_wholepkt(fd
, ifp
, buf
);
568 up
= pim_upstream_add(pim_ifp
->pim
, &sg
, ifp
,
569 PIM_UPSTREAM_FLAG_MASK_SRC_NOCACHE
,
570 __PRETTY_FUNCTION__
, NULL
);
571 if (!up
->channel_oil
->installed
)
572 pim_upstream_mroute_add(up
->channel_oil
,
573 __PRETTY_FUNCTION__
);
579 static int pim_mroute_msg(struct pim_instance
*pim
, const char *buf
,
580 int buf_size
, ifindex_t ifindex
)
582 struct interface
*ifp
;
583 struct pim_interface
*pim_ifp
;
584 const struct ip
*ip_hdr
;
585 const struct igmpmsg
*msg
;
586 char ip_src_str
[INET_ADDRSTRLEN
] = "";
587 char ip_dst_str
[INET_ADDRSTRLEN
] = "";
588 char src_str
[INET_ADDRSTRLEN
] = "<src?>";
589 char grp_str
[INET_ADDRSTRLEN
] = "<grp?>";
590 struct in_addr ifaddr
;
591 struct igmp_sock
*igmp
;
593 ip_hdr
= (const struct ip
*)buf
;
595 if (ip_hdr
->ip_p
== IPPROTO_IGMP
) {
597 /* We have the IP packet but we do not know which interface this
599 * received on. Find the interface that is on the same subnet as
603 ifp
= if_lookup_by_index(ifindex
, pim
->vrf_id
);
605 if (!ifp
|| !ifp
->info
)
609 ifaddr
= pim_find_primary_addr(ifp
);
610 igmp
= pim_igmp_sock_lookup_ifaddr(pim_ifp
->igmp_socket_list
,
613 if (PIM_DEBUG_MROUTE
) {
614 pim_inet4_dump("<src?>", ip_hdr
->ip_src
, ip_src_str
,
616 pim_inet4_dump("<dst?>", ip_hdr
->ip_dst
, ip_dst_str
,
620 "%s(%s): igmp kernel upcall on %s(%p) for %s -> %s",
621 __PRETTY_FUNCTION__
, pim
->vrf
->name
, ifp
->name
,
622 igmp
, ip_src_str
, ip_dst_str
);
625 pim_igmp_packet(igmp
, (char *)buf
, buf_size
);
627 } else if (ip_hdr
->ip_p
) {
628 if (PIM_DEBUG_MROUTE_DETAIL
) {
629 pim_inet4_dump("<src?>", ip_hdr
->ip_src
, src_str
,
631 pim_inet4_dump("<grp?>", ip_hdr
->ip_dst
, grp_str
,
634 "%s: no kernel upcall proto=%d src: %s dst: %s msg_size=%d",
635 __PRETTY_FUNCTION__
, ip_hdr
->ip_p
, src_str
,
640 msg
= (const struct igmpmsg
*)buf
;
642 ifp
= pim_if_find_by_vif_index(pim
, msg
->im_vif
);
646 if (PIM_DEBUG_MROUTE
) {
647 pim_inet4_dump("<src?>", msg
->im_src
, src_str
,
649 pim_inet4_dump("<grp?>", msg
->im_dst
, grp_str
,
652 "%s: pim kernel upcall %s type=%d ip_p=%d from fd=%d for (S,G)=(%s,%s) on %s vifi=%d size=%d",
654 igmpmsgtype2str
[msg
->im_msgtype
],
655 msg
->im_msgtype
, ip_hdr
->ip_p
,
656 pim
->mroute_socket
, src_str
, grp_str
, ifp
->name
,
657 msg
->im_vif
, buf_size
);
660 switch (msg
->im_msgtype
) {
661 case IGMPMSG_WRONGVIF
:
662 return pim_mroute_msg_wrongvif(pim
->mroute_socket
, ifp
,
665 case IGMPMSG_NOCACHE
:
666 return pim_mroute_msg_nocache(pim
->mroute_socket
, ifp
,
669 case IGMPMSG_WHOLEPKT
:
670 return pim_mroute_msg_wholepkt(pim
->mroute_socket
, ifp
,
673 case IGMPMSG_WRVIFWHOLE
:
674 return pim_mroute_msg_wrvifwhole(
675 pim
->mroute_socket
, ifp
, (const char *)msg
);
685 static int mroute_read(struct thread
*t
)
687 struct pim_instance
*pim
;
688 static long long count
;
697 rd
= pim_socket_recvfromto(pim
->mroute_socket
, (uint8_t *)buf
,
698 sizeof(buf
), NULL
, NULL
, NULL
, NULL
,
703 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
706 zlog_warn("%s: failure reading rd=%d: fd=%d: errno=%d: %s",
707 __PRETTY_FUNCTION__
, rd
, pim
->mroute_socket
,
708 errno
, safe_strerror(errno
));
712 result
= pim_mroute_msg(pim
, buf
, rd
, ifindex
);
715 if (count
% router
->packet_process
== 0)
725 static void mroute_read_on(struct pim_instance
*pim
)
727 thread_add_read(router
->master
, mroute_read
, pim
, pim
->mroute_socket
,
731 static void mroute_read_off(struct pim_instance
*pim
)
733 THREAD_OFF(pim
->thread
);
736 int pim_mroute_socket_enable(struct pim_instance
*pim
)
740 frr_with_privs(&pimd_privs
) {
742 fd
= socket(AF_INET
, SOCK_RAW
, IPPROTO_IGMP
);
745 zlog_warn("Could not create mroute socket: errno=%d: %s",
747 safe_strerror(errno
));
751 #ifdef SO_BINDTODEVICE
752 if (pim
->vrf
->vrf_id
!= VRF_DEFAULT
753 && setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
,
754 pim
->vrf
->name
, strlen(pim
->vrf
->name
))) {
755 zlog_warn("Could not setsockopt SO_BINDTODEVICE: %s",
756 safe_strerror(errno
));
764 pim
->mroute_socket
= fd
;
765 if (pim_mroute_set(pim
, 1)) {
767 "Could not enable mroute on socket fd=%d: errno=%d: %s",
768 fd
, errno
, safe_strerror(errno
));
770 pim
->mroute_socket
= -1;
774 pim
->mroute_socket_creation
= pim_time_monotonic_sec();
781 int pim_mroute_socket_disable(struct pim_instance
*pim
)
783 if (pim_mroute_set(pim
, 0)) {
785 "Could not disable mroute on socket fd=%d: errno=%d: %s",
786 pim
->mroute_socket
, errno
, safe_strerror(errno
));
790 if (close(pim
->mroute_socket
)) {
791 zlog_warn("Failure closing mroute socket: fd=%d errno=%d: %s",
792 pim
->mroute_socket
, errno
, safe_strerror(errno
));
796 mroute_read_off(pim
);
797 pim
->mroute_socket
= -1;
803 For each network interface (e.g., physical or a virtual tunnel) that
804 would be used for multicast forwarding, a corresponding multicast
805 interface must be added to the kernel.
807 int pim_mroute_add_vif(struct interface
*ifp
, struct in_addr ifaddr
,
810 struct pim_interface
*pim_ifp
= ifp
->info
;
814 if (PIM_DEBUG_MROUTE
)
815 zlog_debug("%s: Add Vif %d (%s[%s])", __PRETTY_FUNCTION__
,
816 pim_ifp
->mroute_vif_index
, ifp
->name
,
817 pim_ifp
->pim
->vrf
->name
);
819 memset(&vc
, 0, sizeof(vc
));
820 vc
.vifc_vifi
= pim_ifp
->mroute_vif_index
;
821 #ifdef VIFF_USE_IFINDEX
822 vc
.vifc_lcl_ifindex
= ifp
->ifindex
;
824 if (ifaddr
.s_addr
== INADDR_ANY
) {
826 "%s: unnumbered interfaces are not supported on this platform",
827 __PRETTY_FUNCTION__
);
830 memcpy(&vc
.vifc_lcl_addr
, &ifaddr
, sizeof(vc
.vifc_lcl_addr
));
832 vc
.vifc_flags
= flags
;
833 vc
.vifc_threshold
= PIM_MROUTE_MIN_TTL
;
834 vc
.vifc_rate_limit
= 0;
836 #ifdef PIM_DVMRP_TUNNEL
837 if (vc
.vifc_flags
& VIFF_TUNNEL
) {
838 memcpy(&vc
.vifc_rmt_addr
, &vif_remote_addr
,
839 sizeof(vc
.vifc_rmt_addr
));
843 err
= setsockopt(pim_ifp
->pim
->mroute_socket
, IPPROTO_IP
, MRT_ADD_VIF
,
844 (void *)&vc
, sizeof(vc
));
846 char ifaddr_str
[INET_ADDRSTRLEN
];
848 pim_inet4_dump("<ifaddr?>", ifaddr
, ifaddr_str
,
852 "%s: failure: setsockopt(fd=%d,IPPROTO_IP,MRT_ADD_VIF,vif_index=%d,ifaddr=%s,flag=%d): errno=%d: %s",
853 __PRETTY_FUNCTION__
, pim_ifp
->pim
->mroute_socket
,
854 ifp
->ifindex
, ifaddr_str
, flags
, errno
,
855 safe_strerror(errno
));
862 int pim_mroute_del_vif(struct interface
*ifp
)
864 struct pim_interface
*pim_ifp
= ifp
->info
;
868 if (PIM_DEBUG_MROUTE
)
869 zlog_debug("%s: Del Vif %d (%s[%s])", __PRETTY_FUNCTION__
,
870 pim_ifp
->mroute_vif_index
, ifp
->name
,
871 pim_ifp
->pim
->vrf
->name
);
873 memset(&vc
, 0, sizeof(vc
));
874 vc
.vifc_vifi
= pim_ifp
->mroute_vif_index
;
876 err
= setsockopt(pim_ifp
->pim
->mroute_socket
, IPPROTO_IP
, MRT_DEL_VIF
,
877 (void *)&vc
, sizeof(vc
));
880 "%s %s: failure: setsockopt(fd=%d,IPPROTO_IP,MRT_DEL_VIF,vif_index=%d): errno=%d: %s",
881 __FILE__
, __PRETTY_FUNCTION__
,
882 pim_ifp
->pim
->mroute_socket
, pim_ifp
->mroute_vif_index
,
883 errno
, safe_strerror(errno
));
891 * Prevent creating MFC entry with OIF=IIF.
893 * This is a protection against implementation mistakes.
895 * PIM protocol implicitely ensures loopfree multicast topology.
897 * IGMP must be protected against adding looped MFC entries created
898 * by both source and receiver attached to the same interface. See
900 * We shall allow igmp to create upstream when it is DR for the intf.
901 * Assume RP reachable via non DR.
903 bool pim_mroute_allow_iif_in_oil(struct channel_oil
*c_oil
,
906 #ifdef PIM_ENFORCE_LOOPFREE_MFC
907 struct interface
*ifp_out
;
908 struct pim_interface
*pim_ifp
;
911 PIM_UPSTREAM_FLAG_TEST_ALLOW_IIF_IN_OIL(c_oil
->up
->flags
))
914 ifp_out
= pim_if_find_by_vif_index(c_oil
->pim
, oif_index
);
917 pim_ifp
= ifp_out
->info
;
920 if ((c_oil
->oif_flags
[oif_index
] & PIM_OIF_FLAG_PROTO_IGMP
) &&
921 PIM_I_am_DR(pim_ifp
))
930 static inline void pim_mroute_copy(struct mfcctl
*oil
,
931 struct channel_oil
*c_oil
)
935 oil
->mfcc_origin
= c_oil
->oil
.mfcc_origin
;
936 oil
->mfcc_mcastgrp
= c_oil
->oil
.mfcc_mcastgrp
;
937 oil
->mfcc_parent
= c_oil
->oil
.mfcc_parent
;
939 for (i
= 0; i
< MAXVIFS
; ++i
) {
940 if ((oil
->mfcc_parent
== i
) &&
941 !pim_mroute_allow_iif_in_oil(c_oil
, i
)) {
942 oil
->mfcc_ttls
[i
] = 0;
946 if (c_oil
->oif_flags
[i
] & PIM_OIF_FLAG_MUTE
)
947 oil
->mfcc_ttls
[i
] = 0;
949 oil
->mfcc_ttls
[i
] = c_oil
->oil
.mfcc_ttls
[i
];
953 /* This function must not be called directly 0
954 * use pim_upstream_mroute_add or pim_static_mroute_add instead
956 static int pim_mroute_add(struct channel_oil
*c_oil
, const char *name
)
958 struct pim_instance
*pim
= c_oil
->pim
;
959 struct mfcctl tmp_oil
;
962 pim
->mroute_add_last
= pim_time_monotonic_sec();
963 ++pim
->mroute_add_events
;
965 /* Copy the oil to a temporary structure to fixup (without need to
966 * later restore) before sending the mroute add to the dataplane
968 pim_mroute_copy(&tmp_oil
, c_oil
);
970 /* The linux kernel *expects* the incoming
971 * vif to be part of the outgoing list
972 * in the case of a (*,G).
974 if (c_oil
->oil
.mfcc_origin
.s_addr
== INADDR_ANY
) {
975 tmp_oil
.mfcc_ttls
[c_oil
->oil
.mfcc_parent
] = 1;
979 * If we have an unresolved cache entry for the S,G
980 * it is owned by the pimreg for the incoming IIF
981 * So set pimreg as the IIF temporarily to cause
982 * the packets to be forwarded. Then set it
983 * to the correct IIF afterwords.
985 if (!c_oil
->installed
&& c_oil
->oil
.mfcc_origin
.s_addr
!= INADDR_ANY
986 && c_oil
->oil
.mfcc_parent
!= 0) {
987 tmp_oil
.mfcc_parent
= 0;
989 err
= setsockopt(pim
->mroute_socket
, IPPROTO_IP
, MRT_ADD_MFC
,
990 &tmp_oil
, sizeof(tmp_oil
));
992 if (!err
&& !c_oil
->installed
993 && c_oil
->oil
.mfcc_origin
.s_addr
!= INADDR_ANY
994 && c_oil
->oil
.mfcc_parent
!= 0) {
995 tmp_oil
.mfcc_parent
= c_oil
->oil
.mfcc_parent
;
996 err
= setsockopt(pim
->mroute_socket
, IPPROTO_IP
, MRT_ADD_MFC
,
997 &tmp_oil
, sizeof(tmp_oil
));
1002 "%s %s: failure: setsockopt(fd=%d,IPPROTO_IP,MRT_ADD_MFC): errno=%d: %s",
1003 __FILE__
, __PRETTY_FUNCTION__
, pim
->mroute_socket
,
1004 errno
, safe_strerror(errno
));
1008 if (PIM_DEBUG_MROUTE
) {
1010 zlog_debug("%s(%s), vrf %s Added Route: %s",
1011 __PRETTY_FUNCTION__
, name
, pim
->vrf
->name
,
1012 pim_channel_oil_dump(c_oil
, buf
, sizeof(buf
)));
1015 c_oil
->installed
= 1;
1016 c_oil
->mroute_creation
= pim_time_monotonic_sec();
1021 static int pim_upstream_get_mroute_iif(struct channel_oil
*c_oil
,
1024 vifi_t iif
= MAXVIFS
;
1025 struct interface
*ifp
= NULL
;
1026 struct pim_interface
*pim_ifp
;
1027 struct pim_upstream
*up
= c_oil
->up
;
1030 if (PIM_UPSTREAM_FLAG_TEST_USE_RPT(up
->flags
)) {
1032 ifp
= up
->parent
->rpf
.source_nexthop
.interface
;
1034 ifp
= up
->rpf
.source_nexthop
.interface
;
1037 pim_ifp
= (struct pim_interface
*)ifp
->info
;
1039 iif
= pim_ifp
->mroute_vif_index
;
1045 static int pim_upstream_mroute_update(struct channel_oil
*c_oil
,
1050 if (c_oil
->oil
.mfcc_parent
>= MAXVIFS
) {
1051 /* the c_oil cannot be installed as a mroute yet */
1052 if (PIM_DEBUG_MROUTE
)
1054 "%s(%s) %s mroute not ready to be installed; %s",
1056 pim_channel_oil_dump(c_oil
, buf
,
1059 "uninstall" : "skip");
1060 /* if already installed flush it out as we are going to stop
1061 * updates to it leaving it in a stale state
1063 if (c_oil
->installed
)
1064 pim_mroute_del(c_oil
, name
);
1065 /* return success (skipped) */
1069 return pim_mroute_add(c_oil
, name
);
1072 /* IIF associated with SGrpt entries are re-evaluated when the parent
1073 * (*,G) entries IIF changes
1075 static void pim_upstream_all_sources_iif_update(struct pim_upstream
*up
)
1077 struct listnode
*listnode
;
1078 struct pim_upstream
*child
;
1080 for (ALL_LIST_ELEMENTS_RO(up
->sources
, listnode
,
1082 if (PIM_UPSTREAM_FLAG_TEST_USE_RPT(child
->flags
))
1083 pim_upstream_mroute_iif_update(child
->channel_oil
,
1088 /* In the case of "PIM state machine" added mroutes an upstream entry
1089 * must be present to decide on the SPT-forwarding vs. RPT-forwarding.
1091 int pim_upstream_mroute_add(struct channel_oil
*c_oil
, const char *name
)
1095 iif
= pim_upstream_get_mroute_iif(c_oil
, name
);
1097 if (c_oil
->oil
.mfcc_parent
!= iif
) {
1098 c_oil
->oil
.mfcc_parent
= iif
;
1099 if (c_oil
->oil
.mfcc_origin
.s_addr
== INADDR_ANY
&&
1101 pim_upstream_all_sources_iif_update(c_oil
->up
);
1103 c_oil
->oil
.mfcc_parent
= iif
;
1106 return pim_upstream_mroute_update(c_oil
, name
);
1109 /* Look for IIF changes and update the dateplane entry only if the IIF
1112 int pim_upstream_mroute_iif_update(struct channel_oil
*c_oil
, const char *name
)
1117 iif
= pim_upstream_get_mroute_iif(c_oil
, name
);
1118 if (c_oil
->oil
.mfcc_parent
== iif
) {
1122 c_oil
->oil
.mfcc_parent
= iif
;
1124 if (c_oil
->oil
.mfcc_origin
.s_addr
== INADDR_ANY
&&
1126 pim_upstream_all_sources_iif_update(c_oil
->up
);
1128 if (PIM_DEBUG_MROUTE_DETAIL
)
1129 zlog_debug("%s(%s) %s mroute iif update %d",
1131 pim_channel_oil_dump(c_oil
, buf
,
1133 /* XXX: is this hack needed? */
1134 c_oil
->oil_inherited_rescan
= 1;
1135 return pim_upstream_mroute_update(c_oil
, name
);
1138 int pim_static_mroute_add(struct channel_oil
*c_oil
, const char *name
)
1140 return pim_mroute_add(c_oil
, name
);
1143 void pim_static_mroute_iif_update(struct channel_oil
*c_oil
,
1144 int input_vif_index
,
1147 if (c_oil
->oil
.mfcc_parent
== input_vif_index
)
1150 c_oil
->oil
.mfcc_parent
= input_vif_index
;
1151 if (input_vif_index
== MAXVIFS
)
1152 pim_mroute_del(c_oil
, name
);
1154 pim_static_mroute_add(c_oil
, name
);
1157 int pim_mroute_del(struct channel_oil
*c_oil
, const char *name
)
1159 struct pim_instance
*pim
= c_oil
->pim
;
1162 pim
->mroute_del_last
= pim_time_monotonic_sec();
1163 ++pim
->mroute_del_events
;
1165 if (!c_oil
->installed
) {
1166 if (PIM_DEBUG_MROUTE
) {
1169 "%s %s: vifi %d for route is %s not installed, do not need to send del req. ",
1170 __FILE__
, __PRETTY_FUNCTION__
,
1171 c_oil
->oil
.mfcc_parent
,
1172 pim_channel_oil_dump(c_oil
, buf
, sizeof(buf
)));
1177 err
= setsockopt(pim
->mroute_socket
, IPPROTO_IP
, MRT_DEL_MFC
,
1178 &c_oil
->oil
, sizeof(c_oil
->oil
));
1180 if (PIM_DEBUG_MROUTE
)
1182 "%s %s: failure: setsockopt(fd=%d,IPPROTO_IP,MRT_DEL_MFC): errno=%d: %s",
1183 __FILE__
, __PRETTY_FUNCTION__
,
1184 pim
->mroute_socket
, errno
,
1185 safe_strerror(errno
));
1189 if (PIM_DEBUG_MROUTE
) {
1191 zlog_debug("%s(%s), vrf %s Deleted Route: %s",
1192 __PRETTY_FUNCTION__
, name
, pim
->vrf
->name
,
1193 pim_channel_oil_dump(c_oil
, buf
, sizeof(buf
)));
1196 // Reset kernel installed flag
1197 c_oil
->installed
= 0;
1202 void pim_mroute_update_counters(struct channel_oil
*c_oil
)
1204 struct pim_instance
*pim
= c_oil
->pim
;
1205 struct sioc_sg_req sgreq
;
1207 c_oil
->cc
.oldpktcnt
= c_oil
->cc
.pktcnt
;
1208 c_oil
->cc
.oldbytecnt
= c_oil
->cc
.bytecnt
;
1209 c_oil
->cc
.oldwrong_if
= c_oil
->cc
.wrong_if
;
1211 if (!c_oil
->installed
) {
1212 c_oil
->cc
.lastused
= 100 * pim
->keep_alive_time
;
1213 if (PIM_DEBUG_MROUTE
) {
1214 struct prefix_sg sg
;
1216 sg
.src
= c_oil
->oil
.mfcc_origin
;
1217 sg
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
1218 if (PIM_DEBUG_MROUTE
)
1220 "Channel%s is not installed no need to collect data from kernel",
1221 pim_str_sg_dump(&sg
));
1226 memset(&sgreq
, 0, sizeof(sgreq
));
1227 sgreq
.src
= c_oil
->oil
.mfcc_origin
;
1228 sgreq
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
1230 pim_zlookup_sg_statistics(c_oil
);
1231 if (ioctl(pim
->mroute_socket
, SIOCGETSGCNT
, &sgreq
)) {
1232 struct prefix_sg sg
;
1234 sg
.src
= c_oil
->oil
.mfcc_origin
;
1235 sg
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
1237 zlog_warn("ioctl(SIOCGETSGCNT=%lu) failure for (S,G)=%s: errno=%d: %s",
1238 (unsigned long)SIOCGETSGCNT
, pim_str_sg_dump(&sg
),
1239 errno
, safe_strerror(errno
));
1243 c_oil
->cc
.pktcnt
= sgreq
.pktcnt
;
1244 c_oil
->cc
.bytecnt
= sgreq
.bytecnt
;
1245 c_oil
->cc
.wrong_if
= sgreq
.wrong_if
;