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_elevate_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_elevate_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 *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 struct channel_oil
*c_oil
;
195 if (PIM_DEBUG_MROUTE_DETAIL
)
196 zlog_debug("%s: Interface is not the DR blackholing incoming traffic for %s",
197 __PRETTY_FUNCTION__
, pim_str_sg_dump(&sg
));
200 * We are not the DR, but we are still receiving packets
201 * Let's blackhole those packets for the moment
202 * As that they will be coming up to the cpu
203 * and causing us to consider them.
205 c_oil
= pim_channel_oil_add(pim_ifp
->pim
, &sg
,
206 pim_ifp
->mroute_vif_index
,
207 __PRETTY_FUNCTION__
);
208 pim_mroute_add(c_oil
, __PRETTY_FUNCTION__
);
213 up
= pim_upstream_find_or_add(&sg
, ifp
, PIM_UPSTREAM_FLAG_MASK_FHR
,
214 __PRETTY_FUNCTION__
);
216 if (PIM_DEBUG_MROUTE
) {
218 "%s: Failure to add upstream information for %s",
219 __PRETTY_FUNCTION__
, pim_str_sg_dump(&sg
));
225 * I moved this debug till after the actual add because
226 * I want to take advantage of the up->sg_str being filled in.
228 if (PIM_DEBUG_MROUTE
) {
229 zlog_debug("%s: Adding a Route %s for WHOLEPKT consumption",
230 __PRETTY_FUNCTION__
, up
->sg_str
);
233 PIM_UPSTREAM_FLAG_SET_SRC_STREAM(up
->flags
);
234 pim_upstream_keep_alive_timer_start(up
, pim_ifp
->pim
->keep_alive_time
);
236 up
->channel_oil
->cc
.pktcnt
++;
237 PIM_UPSTREAM_FLAG_SET_FHR(up
->flags
);
238 // resolve mfcc_parent prior to mroute_add in channel_add_oif
239 if (up
->rpf
.source_nexthop
.interface
&&
240 up
->channel_oil
->oil
.mfcc_parent
>= MAXVIFS
) {
242 vif_index
= pim_if_find_vifindex_by_ifindex(
244 up
->rpf
.source_nexthop
.interface
->ifindex
);
245 up
->channel_oil
->oil
.mfcc_parent
= vif_index
;
247 pim_register_join(up
);
252 static int pim_mroute_msg_wholepkt(int fd
, struct interface
*ifp
,
255 struct pim_interface
*pim_ifp
;
258 const struct ip
*ip_hdr
;
259 struct pim_upstream
*up
;
263 ip_hdr
= (const struct ip
*)buf
;
265 memset(&sg
, 0, sizeof(struct prefix_sg
));
266 sg
.src
= ip_hdr
->ip_src
;
267 sg
.grp
= ip_hdr
->ip_dst
;
269 up
= pim_upstream_find(pim_ifp
->pim
, &sg
);
271 struct prefix_sg star
= sg
;
272 star
.src
.s_addr
= INADDR_ANY
;
274 up
= pim_upstream_find(pim_ifp
->pim
, &star
);
276 if (up
&& PIM_UPSTREAM_FLAG_TEST_SRC_IGMP(up
->flags
)) {
277 up
= pim_upstream_add(pim_ifp
->pim
, &sg
, ifp
,
278 PIM_UPSTREAM_FLAG_MASK_SRC_LHR
,
279 __PRETTY_FUNCTION__
, NULL
);
281 if (PIM_DEBUG_MROUTE
)
283 "%s: Unable to create upstream information for %s",
285 pim_str_sg_dump(&sg
));
288 pim_upstream_keep_alive_timer_start(
289 up
, pim_ifp
->pim
->keep_alive_time
);
290 pim_upstream_inherited_olist(pim_ifp
->pim
, up
);
291 pim_upstream_switch(pim_ifp
->pim
, up
,
292 PIM_UPSTREAM_JOINED
);
294 if (PIM_DEBUG_MROUTE
)
295 zlog_debug("%s: Creating %s upstream on LHR",
296 __PRETTY_FUNCTION__
, up
->sg_str
);
299 if (PIM_DEBUG_MROUTE_DETAIL
) {
301 "%s: Unable to find upstream channel WHOLEPKT%s",
302 __PRETTY_FUNCTION__
, pim_str_sg_dump(&sg
));
307 if (!up
->rpf
.source_nexthop
.interface
) {
309 zlog_debug("%s: up %s RPF is not present",
310 __PRETTY_FUNCTION__
, up
->sg_str
);
314 pim_ifp
= up
->rpf
.source_nexthop
.interface
->info
;
316 rpg
= pim_ifp
? RP(pim_ifp
->pim
, sg
.grp
) : NULL
;
318 if ((pim_rpf_addr_is_inaddr_none(rpg
)) || (!pim_ifp
)
319 || (!(PIM_I_am_DR(pim_ifp
)))) {
320 if (PIM_DEBUG_MROUTE
) {
321 zlog_debug("%s: Failed Check send packet",
322 __PRETTY_FUNCTION__
);
328 * If we've received a register suppress
330 if (!up
->t_rs_timer
) {
331 if (pim_is_grp_ssm(pim_ifp
->pim
, sg
.grp
)) {
332 if (PIM_DEBUG_PIM_REG
)
334 "%s register forward skipped as group is SSM",
335 pim_str_sg_dump(&sg
));
338 pim_register_send((uint8_t *)buf
+ sizeof(struct ip
),
339 ntohs(ip_hdr
->ip_len
) - sizeof(struct ip
),
340 pim_ifp
->primary_address
, rpg
, 0, up
);
345 static int pim_mroute_msg_wrongvif(int fd
, struct interface
*ifp
,
346 const struct igmpmsg
*msg
)
348 struct pim_ifchannel
*ch
;
349 struct pim_interface
*pim_ifp
;
352 memset(&sg
, 0, sizeof(struct prefix_sg
));
353 sg
.src
= msg
->im_src
;
354 sg
.grp
= msg
->im_dst
;
357 Send Assert(S,G) on iif as response to WRONGVIF kernel upcall.
359 RFC 4601 4.8.2. PIM-SSM-Only Routers
361 iif is the incoming interface of the packet.
362 if (iif is in inherited_olist(S,G)) {
363 send Assert(S,G) on iif
368 if (PIM_DEBUG_MROUTE
)
370 "%s: WRONGVIF (S,G)=%s could not find input interface for input_vif_index=%d",
371 __PRETTY_FUNCTION__
, pim_str_sg_dump(&sg
),
378 if (PIM_DEBUG_MROUTE
)
380 "%s: WRONGVIF (S,G)=%s multicast not enabled on interface %s",
381 __PRETTY_FUNCTION__
, pim_str_sg_dump(&sg
),
386 ch
= pim_ifchannel_find(ifp
, &sg
);
388 struct prefix_sg star_g
= sg
;
389 if (PIM_DEBUG_MROUTE
)
391 "%s: WRONGVIF (S,G)=%s could not find channel on interface %s",
392 __PRETTY_FUNCTION__
, pim_str_sg_dump(&sg
),
395 star_g
.src
.s_addr
= INADDR_ANY
;
396 ch
= pim_ifchannel_find(ifp
, &star_g
);
398 if (PIM_DEBUG_MROUTE
)
400 "%s: WRONGVIF (*,G)=%s could not find channel on interface %s",
402 pim_str_sg_dump(&star_g
), ifp
->name
);
408 RFC 4601: 4.6.1. (S,G) Assert Message State Machine
410 Transitions from NoInfo State
412 An (S,G) data packet arrives on interface I, AND
413 CouldAssert(S,G,I)==TRUE An (S,G) data packet arrived on an
414 downstream interface that is in our (S,G) outgoing interface
415 list. We optimistically assume that we will be the assert
416 winner for this (S,G), and so we transition to the "I am Assert
417 Winner" state and perform Actions A1 (below), which will
418 initiate the assert negotiation for (S,G).
421 if (ch
->ifassert_state
!= PIM_IFASSERT_NOINFO
) {
422 if (PIM_DEBUG_MROUTE
) {
424 "%s: WRONGVIF (S,G)=%s channel is not on Assert NoInfo state for interface %s",
425 __PRETTY_FUNCTION__
, ch
->sg_str
, ifp
->name
);
430 if (!PIM_IF_FLAG_TEST_COULD_ASSERT(ch
->flags
)) {
431 if (PIM_DEBUG_MROUTE
) {
433 "%s: WRONGVIF (S,G)=%s interface %s is not downstream for channel",
434 __PRETTY_FUNCTION__
, ch
->sg_str
, ifp
->name
);
439 if (assert_action_a1(ch
)) {
440 if (PIM_DEBUG_MROUTE
) {
442 "%s: WRONGVIF (S,G)=%s assert_action_a1 failure on interface %s",
443 __PRETTY_FUNCTION__
, ch
->sg_str
, ifp
->name
);
451 static int pim_mroute_msg_wrvifwhole(int fd
, struct interface
*ifp
,
454 const struct ip
*ip_hdr
= (const struct ip
*)buf
;
455 struct pim_interface
*pim_ifp
;
456 struct pim_ifchannel
*ch
;
457 struct pim_upstream
*up
;
458 struct prefix_sg star_g
;
460 struct channel_oil
*oil
;
464 memset(&sg
, 0, sizeof(struct prefix_sg
));
465 sg
.src
= ip_hdr
->ip_src
;
466 sg
.grp
= ip_hdr
->ip_dst
;
468 ch
= pim_ifchannel_find(ifp
, &sg
);
470 if (PIM_DEBUG_MROUTE
)
472 "WRVIFWHOLE (S,G)=%s found ifchannel on interface %s",
473 ch
->sg_str
, ifp
->name
);
478 star_g
.src
.s_addr
= INADDR_ANY
;
480 ch
= pim_ifchannel_find(ifp
, &star_g
);
483 if (PIM_DEBUG_MROUTE
)
484 zlog_debug ("WRVIFWHOLE (*,G)=%s found ifchannel on interface %s",
485 pim_str_sg_dump (&star_g
), ifp
->name
);
490 up
= pim_upstream_find(pim_ifp
->pim
, &sg
);
492 struct pim_upstream
*parent
;
493 struct pim_nexthop source
;
494 struct pim_rpf
*rpf
= RP(pim_ifp
->pim
, sg
.grp
);
495 if (!rpf
|| !rpf
->source_nexthop
.interface
)
499 * If we have received a WRVIFWHOLE and are at this
500 * point, we could be receiving the packet on the *,G
501 * tree, let's check and if so we can safely drop
504 parent
= pim_upstream_find(pim_ifp
->pim
, &star_g
);
505 if (parent
&& parent
->rpf
.source_nexthop
.interface
== ifp
)
508 pim_ifp
= rpf
->source_nexthop
.interface
->info
;
510 memset(&source
, 0, sizeof(source
));
512 * If we are the fhr that means we are getting a callback during
513 * the pimreg period, so I believe we can ignore this packet
515 if (!PIM_UPSTREAM_FLAG_TEST_FHR(up
->flags
)) {
516 // No if channel, but upstream we are at the RP.
517 if (pim_nexthop_lookup(pim_ifp
->pim
, &source
,
518 up
->upstream_register
, 0)) {
519 pim_register_stop_send(source
.interface
, &sg
,
520 pim_ifp
->primary_address
,
521 up
->upstream_register
);
522 up
->sptbit
= PIM_UPSTREAM_SPTBIT_TRUE
;
524 if (!up
->channel_oil
)
525 up
->channel_oil
= pim_channel_oil_add(
527 pim_ifp
->mroute_vif_index
,
528 __PRETTY_FUNCTION__
);
529 pim_upstream_inherited_olist(pim_ifp
->pim
, up
);
530 if (!up
->channel_oil
->installed
)
531 pim_mroute_add(up
->channel_oil
,
532 __PRETTY_FUNCTION__
);
534 if (I_am_RP(pim_ifp
->pim
, up
->sg
.grp
)) {
535 if (pim_nexthop_lookup(pim_ifp
->pim
, &source
,
536 up
->upstream_register
,
538 pim_register_stop_send(
539 source
.interface
, &sg
,
540 pim_ifp
->primary_address
,
541 up
->upstream_register
);
542 up
->sptbit
= PIM_UPSTREAM_SPTBIT_TRUE
;
544 pim_upstream_keep_alive_timer_start(
545 up
, pim_ifp
->pim
->keep_alive_time
);
546 pim_upstream_inherited_olist(pim_ifp
->pim
, up
);
547 pim_mroute_msg_wholepkt(fd
, ifp
, buf
);
553 oil
= pim_channel_oil_add(pim_ifp
->pim
, &sg
, pim_ifp
->mroute_vif_index
,
554 __PRETTY_FUNCTION__
);
556 pim_mroute_add(oil
, __PRETTY_FUNCTION__
);
557 if (pim_if_connected_to_source(ifp
, sg
.src
)) {
558 up
= pim_upstream_add(pim_ifp
->pim
, &sg
, ifp
,
559 PIM_UPSTREAM_FLAG_MASK_FHR
,
560 __PRETTY_FUNCTION__
, NULL
);
562 if (PIM_DEBUG_MROUTE
)
564 "%s: WRONGVIF%s unable to create upstream on interface",
565 pim_str_sg_dump(&sg
), ifp
->name
);
568 PIM_UPSTREAM_FLAG_SET_SRC_STREAM(up
->flags
);
569 pim_upstream_keep_alive_timer_start(
570 up
, pim_ifp
->pim
->keep_alive_time
);
571 up
->channel_oil
= oil
;
572 up
->channel_oil
->cc
.pktcnt
++;
573 pim_register_join(up
);
574 pim_upstream_inherited_olist(pim_ifp
->pim
, up
);
576 // Send the packet to the RP
577 pim_mroute_msg_wholepkt(fd
, ifp
, buf
);
583 static int pim_mroute_msg(struct pim_instance
*pim
, const char *buf
,
584 int buf_size
, ifindex_t ifindex
)
586 struct interface
*ifp
;
587 struct pim_interface
*pim_ifp
;
588 const struct ip
*ip_hdr
;
589 const struct igmpmsg
*msg
;
590 char ip_src_str
[INET_ADDRSTRLEN
] = "";
591 char ip_dst_str
[INET_ADDRSTRLEN
] = "";
592 char src_str
[INET_ADDRSTRLEN
] = "<src?>";
593 char grp_str
[INET_ADDRSTRLEN
] = "<grp?>";
594 struct in_addr ifaddr
;
595 struct igmp_sock
*igmp
;
597 ip_hdr
= (const struct ip
*)buf
;
599 if (ip_hdr
->ip_p
== IPPROTO_IGMP
) {
601 /* We have the IP packet but we do not know which interface this
603 * received on. Find the interface that is on the same subnet as
607 ifp
= if_lookup_by_index(ifindex
, pim
->vrf_id
);
609 if (!ifp
|| !ifp
->info
)
613 ifaddr
= pim_find_primary_addr(ifp
);
614 igmp
= pim_igmp_sock_lookup_ifaddr(pim_ifp
->igmp_socket_list
,
617 if (PIM_DEBUG_MROUTE
) {
618 pim_inet4_dump("<src?>", ip_hdr
->ip_src
, ip_src_str
,
620 pim_inet4_dump("<dst?>", ip_hdr
->ip_dst
, ip_dst_str
,
624 "%s(%s): igmp kernel upcall on %s(%p) for %s -> %s",
625 __PRETTY_FUNCTION__
, pim
->vrf
->name
, ifp
->name
,
626 igmp
, ip_src_str
, ip_dst_str
);
629 pim_igmp_packet(igmp
, (char *)buf
, buf_size
);
631 } else if (ip_hdr
->ip_p
) {
632 if (PIM_DEBUG_MROUTE_DETAIL
) {
633 pim_inet4_dump("<src?>", ip_hdr
->ip_src
, src_str
,
635 pim_inet4_dump("<grp?>", ip_hdr
->ip_dst
, grp_str
,
638 "%s: no kernel upcall proto=%d src: %s dst: %s msg_size=%d",
639 __PRETTY_FUNCTION__
, ip_hdr
->ip_p
, src_str
,
644 msg
= (const struct igmpmsg
*)buf
;
646 ifp
= pim_if_find_by_vif_index(pim
, msg
->im_vif
);
650 if (PIM_DEBUG_MROUTE
) {
651 pim_inet4_dump("<src?>", msg
->im_src
, src_str
,
653 pim_inet4_dump("<grp?>", msg
->im_dst
, grp_str
,
656 "%s: pim kernel upcall %s type=%d ip_p=%d from fd=%d for (S,G)=(%s,%s) on %s vifi=%d size=%d",
658 igmpmsgtype2str
[msg
->im_msgtype
],
659 msg
->im_msgtype
, ip_hdr
->ip_p
,
660 pim
->mroute_socket
, src_str
, grp_str
, ifp
->name
,
661 msg
->im_vif
, buf_size
);
664 switch (msg
->im_msgtype
) {
665 case IGMPMSG_WRONGVIF
:
666 return pim_mroute_msg_wrongvif(pim
->mroute_socket
, ifp
,
669 case IGMPMSG_NOCACHE
:
670 return pim_mroute_msg_nocache(pim
->mroute_socket
, ifp
,
673 case IGMPMSG_WHOLEPKT
:
674 return pim_mroute_msg_wholepkt(pim
->mroute_socket
, ifp
,
677 case IGMPMSG_WRVIFWHOLE
:
678 return pim_mroute_msg_wrvifwhole(
679 pim
->mroute_socket
, ifp
, (const char *)msg
);
689 static int mroute_read(struct thread
*t
)
691 struct pim_instance
*pim
;
692 static long long count
;
701 rd
= pim_socket_recvfromto(pim
->mroute_socket
, (uint8_t *)buf
,
702 sizeof(buf
), NULL
, NULL
, NULL
, NULL
,
707 if (errno
== EWOULDBLOCK
|| errno
== EAGAIN
)
710 if (PIM_DEBUG_MROUTE
)
712 "%s: failure reading rd=%d: fd=%d: errno=%d: %s",
713 __PRETTY_FUNCTION__
, rd
,
714 pim
->mroute_socket
, errno
,
715 safe_strerror(errno
));
719 result
= pim_mroute_msg(pim
, buf
, rd
, ifindex
);
722 if (count
% router
->packet_process
== 0)
732 static void mroute_read_on(struct pim_instance
*pim
)
734 thread_add_read(router
->master
, mroute_read
, pim
, pim
->mroute_socket
,
738 static void mroute_read_off(struct pim_instance
*pim
)
740 THREAD_OFF(pim
->thread
);
743 int pim_mroute_socket_enable(struct pim_instance
*pim
)
747 frr_elevate_privs(&pimd_privs
) {
749 fd
= socket(AF_INET
, SOCK_RAW
, IPPROTO_IGMP
);
752 zlog_warn("Could not create mroute socket: errno=%d: %s",
754 safe_strerror(errno
));
758 #ifdef SO_BINDTODEVICE
759 if (pim
->vrf
->vrf_id
!= VRF_DEFAULT
760 && setsockopt(fd
, SOL_SOCKET
, SO_BINDTODEVICE
,
761 pim
->vrf
->name
, strlen(pim
->vrf
->name
))) {
762 zlog_warn("Could not setsockopt SO_BINDTODEVICE: %s",
763 safe_strerror(errno
));
771 pim
->mroute_socket
= fd
;
772 if (pim_mroute_set(pim
, 1)) {
774 "Could not enable mroute on socket fd=%d: errno=%d: %s",
775 fd
, errno
, safe_strerror(errno
));
777 pim
->mroute_socket
= -1;
781 pim
->mroute_socket_creation
= pim_time_monotonic_sec();
788 int pim_mroute_socket_disable(struct pim_instance
*pim
)
790 if (pim_mroute_set(pim
, 0)) {
792 "Could not disable mroute on socket fd=%d: errno=%d: %s",
793 pim
->mroute_socket
, errno
, safe_strerror(errno
));
797 if (close(pim
->mroute_socket
)) {
798 zlog_warn("Failure closing mroute socket: fd=%d errno=%d: %s",
799 pim
->mroute_socket
, errno
, safe_strerror(errno
));
803 mroute_read_off(pim
);
804 pim
->mroute_socket
= -1;
810 For each network interface (e.g., physical or a virtual tunnel) that
811 would be used for multicast forwarding, a corresponding multicast
812 interface must be added to the kernel.
814 int pim_mroute_add_vif(struct interface
*ifp
, struct in_addr ifaddr
,
817 struct pim_interface
*pim_ifp
= ifp
->info
;
821 if (PIM_DEBUG_MROUTE
)
822 zlog_debug("%s: Add Vif %d (%s[%s])", __PRETTY_FUNCTION__
,
823 pim_ifp
->mroute_vif_index
, ifp
->name
,
824 pim_ifp
->pim
->vrf
->name
);
826 memset(&vc
, 0, sizeof(vc
));
827 vc
.vifc_vifi
= pim_ifp
->mroute_vif_index
;
828 #ifdef VIFF_USE_IFINDEX
829 vc
.vifc_lcl_ifindex
= ifp
->ifindex
;
831 if (ifaddr
.s_addr
== INADDR_ANY
) {
833 "%s: unnumbered interfaces are not supported on this platform",
834 __PRETTY_FUNCTION__
);
837 memcpy(&vc
.vifc_lcl_addr
, &ifaddr
, sizeof(vc
.vifc_lcl_addr
));
839 vc
.vifc_flags
= flags
;
840 vc
.vifc_threshold
= PIM_MROUTE_MIN_TTL
;
841 vc
.vifc_rate_limit
= 0;
843 #ifdef PIM_DVMRP_TUNNEL
844 if (vc
.vifc_flags
& VIFF_TUNNEL
) {
845 memcpy(&vc
.vifc_rmt_addr
, &vif_remote_addr
,
846 sizeof(vc
.vifc_rmt_addr
));
850 err
= setsockopt(pim_ifp
->pim
->mroute_socket
, IPPROTO_IP
, MRT_ADD_VIF
,
851 (void *)&vc
, sizeof(vc
));
853 char ifaddr_str
[INET_ADDRSTRLEN
];
855 pim_inet4_dump("<ifaddr?>", ifaddr
, ifaddr_str
,
859 "%s: failure: setsockopt(fd=%d,IPPROTO_IP,MRT_ADD_VIF,vif_index=%d,ifaddr=%s,flag=%d): errno=%d: %s",
860 __PRETTY_FUNCTION__
, pim_ifp
->pim
->mroute_socket
,
861 ifp
->ifindex
, ifaddr_str
, flags
, errno
,
862 safe_strerror(errno
));
869 int pim_mroute_del_vif(struct interface
*ifp
)
871 struct pim_interface
*pim_ifp
= ifp
->info
;
875 if (PIM_DEBUG_MROUTE
)
876 zlog_debug("%s: Del Vif %d (%s[%s])", __PRETTY_FUNCTION__
,
877 pim_ifp
->mroute_vif_index
, ifp
->name
,
878 pim_ifp
->pim
->vrf
->name
);
880 memset(&vc
, 0, sizeof(vc
));
881 vc
.vifc_vifi
= pim_ifp
->mroute_vif_index
;
883 err
= setsockopt(pim_ifp
->pim
->mroute_socket
, IPPROTO_IP
, MRT_DEL_VIF
,
884 (void *)&vc
, sizeof(vc
));
887 "%s %s: failure: setsockopt(fd=%d,IPPROTO_IP,MRT_DEL_VIF,vif_index=%d): errno=%d: %s",
888 __FILE__
, __PRETTY_FUNCTION__
,
889 pim_ifp
->pim
->mroute_socket
, pim_ifp
->mroute_vif_index
,
890 errno
, safe_strerror(errno
));
897 int pim_mroute_add(struct channel_oil
*c_oil
, const char *name
)
899 struct pim_instance
*pim
= c_oil
->pim
;
902 int orig_iif_vif
= 0;
903 struct pim_interface
*pim_reg_ifp
= NULL
;
904 int orig_pimreg_ttl
= 0;
905 bool pimreg_ttl_reset
= false;
906 struct pim_interface
*vxlan_ifp
= NULL
;
907 int orig_term_ttl
= 0;
908 bool orig_term_ttl_reset
= false;
910 pim
->mroute_add_last
= pim_time_monotonic_sec();
911 ++pim
->mroute_add_events
;
913 /* Do not install route if incoming interface is undefined. */
914 if (c_oil
->oil
.mfcc_parent
>= MAXVIFS
) {
915 if (PIM_DEBUG_MROUTE
) {
918 "%s(%s) %s Attempting to add vifi that is invalid to mroute table",
919 __PRETTY_FUNCTION__
, name
,
920 pim_channel_oil_dump(c_oil
, buf
, sizeof(buf
)));
925 /* The linux kernel *expects* the incoming
926 * vif to be part of the outgoing list
927 * in the case of a (*,G).
929 if (c_oil
->oil
.mfcc_origin
.s_addr
== INADDR_ANY
) {
930 orig
= c_oil
->oil
.mfcc_ttls
[c_oil
->oil
.mfcc_parent
];
931 c_oil
->oil
.mfcc_ttls
[c_oil
->oil
.mfcc_parent
] = 1;
935 /* suppress pimreg in the OIL if the mroute is not supposed to
936 * trigger register encapsulated data
938 if (PIM_UPSTREAM_FLAG_TEST_NO_PIMREG_DATA(c_oil
->up
->flags
)) {
939 pim_reg_ifp
= pim
->regiface
->info
;
941 c_oil
->oil
.mfcc_ttls
[pim_reg_ifp
->mroute_vif_index
];
942 c_oil
->oil
.mfcc_ttls
[pim_reg_ifp
->mroute_vif_index
] = 0;
943 /* remember to flip it back after MFC programming */
944 pimreg_ttl_reset
= true;
947 vxlan_ifp
= pim_vxlan_get_term_ifp(pim
);
948 /* 1. vxlan termination device must never be added to the
949 * origination mroute (and that can actually happen because
950 * of XG inheritance from the termination mroute) otherwise
951 * traffic will end up looping.
952 * 2. vxlan termination device should be removed from the non-DF
953 * to prevent duplicates to the overlay rxer
956 (PIM_UPSTREAM_FLAG_TEST_SRC_VXLAN_ORIG(c_oil
->up
->flags
) ||
957 PIM_UPSTREAM_FLAG_TEST_MLAG_NON_DF(c_oil
->up
->flags
))) {
958 orig_term_ttl_reset
= true;
960 c_oil
->oil
.mfcc_ttls
[vxlan_ifp
->mroute_vif_index
];
961 c_oil
->oil
.mfcc_ttls
[vxlan_ifp
->mroute_vif_index
] = 0;
966 * If we have an unresolved cache entry for the S,G
967 * it is owned by the pimreg for the incoming IIF
968 * So set pimreg as the IIF temporarily to cause
969 * the packets to be forwarded. Then set it
970 * to the correct IIF afterwords.
972 if (!c_oil
->installed
&& c_oil
->oil
.mfcc_origin
.s_addr
!= INADDR_ANY
973 && c_oil
->oil
.mfcc_parent
!= 0) {
974 orig_iif_vif
= c_oil
->oil
.mfcc_parent
;
975 c_oil
->oil
.mfcc_parent
= 0;
977 err
= setsockopt(pim
->mroute_socket
, IPPROTO_IP
, MRT_ADD_MFC
,
978 &c_oil
->oil
, sizeof(c_oil
->oil
));
980 if (!err
&& !c_oil
->installed
981 && c_oil
->oil
.mfcc_origin
.s_addr
!= INADDR_ANY
982 && orig_iif_vif
!= 0) {
983 c_oil
->oil
.mfcc_parent
= orig_iif_vif
;
984 err
= setsockopt(pim
->mroute_socket
, IPPROTO_IP
, MRT_ADD_MFC
,
985 &c_oil
->oil
, sizeof(c_oil
->oil
));
988 if (c_oil
->oil
.mfcc_origin
.s_addr
== INADDR_ANY
)
989 c_oil
->oil
.mfcc_ttls
[c_oil
->oil
.mfcc_parent
] = orig
;
991 if (pimreg_ttl_reset
) {
993 c_oil
->oil
.mfcc_ttls
[pim_reg_ifp
->mroute_vif_index
] =
997 if (orig_term_ttl_reset
)
998 c_oil
->oil
.mfcc_ttls
[vxlan_ifp
->mroute_vif_index
] =
1003 "%s %s: failure: setsockopt(fd=%d,IPPROTO_IP,MRT_ADD_MFC): errno=%d: %s",
1004 __FILE__
, __PRETTY_FUNCTION__
, pim
->mroute_socket
,
1005 errno
, safe_strerror(errno
));
1009 if (PIM_DEBUG_MROUTE
) {
1011 zlog_debug("%s(%s), vrf %s Added Route: %s",
1012 __PRETTY_FUNCTION__
, name
, pim
->vrf
->name
,
1013 pim_channel_oil_dump(c_oil
, buf
, sizeof(buf
)));
1016 c_oil
->installed
= 1;
1017 c_oil
->mroute_creation
= pim_time_monotonic_sec();
1022 int pim_mroute_del(struct channel_oil
*c_oil
, const char *name
)
1024 struct pim_instance
*pim
= c_oil
->pim
;
1027 pim
->mroute_del_last
= pim_time_monotonic_sec();
1028 ++pim
->mroute_del_events
;
1030 if (!c_oil
->installed
) {
1031 if (PIM_DEBUG_MROUTE
) {
1034 "%s %s: vifi %d for route is %s not installed, do not need to send del req. ",
1035 __FILE__
, __PRETTY_FUNCTION__
,
1036 c_oil
->oil
.mfcc_parent
,
1037 pim_channel_oil_dump(c_oil
, buf
, sizeof(buf
)));
1042 err
= setsockopt(pim
->mroute_socket
, IPPROTO_IP
, MRT_DEL_MFC
,
1043 &c_oil
->oil
, sizeof(c_oil
->oil
));
1045 if (PIM_DEBUG_MROUTE
)
1047 "%s %s: failure: setsockopt(fd=%d,IPPROTO_IP,MRT_DEL_MFC): errno=%d: %s",
1048 __FILE__
, __PRETTY_FUNCTION__
,
1049 pim
->mroute_socket
, errno
,
1050 safe_strerror(errno
));
1054 if (PIM_DEBUG_MROUTE
) {
1056 zlog_debug("%s(%s), vrf %s Deleted Route: %s",
1057 __PRETTY_FUNCTION__
, name
, pim
->vrf
->name
,
1058 pim_channel_oil_dump(c_oil
, buf
, sizeof(buf
)));
1061 // Reset kernel installed flag
1062 c_oil
->installed
= 0;
1067 void pim_mroute_update_counters(struct channel_oil
*c_oil
)
1069 struct pim_instance
*pim
= c_oil
->pim
;
1070 struct sioc_sg_req sgreq
;
1072 c_oil
->cc
.oldpktcnt
= c_oil
->cc
.pktcnt
;
1073 c_oil
->cc
.oldbytecnt
= c_oil
->cc
.bytecnt
;
1074 c_oil
->cc
.oldwrong_if
= c_oil
->cc
.wrong_if
;
1076 if (!c_oil
->installed
) {
1077 c_oil
->cc
.lastused
= 100 * pim
->keep_alive_time
;
1078 if (PIM_DEBUG_MROUTE
) {
1079 struct prefix_sg sg
;
1081 sg
.src
= c_oil
->oil
.mfcc_origin
;
1082 sg
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
1083 if (PIM_DEBUG_MROUTE
)
1085 "Channel%s is not installed no need to collect data from kernel",
1086 pim_str_sg_dump(&sg
));
1091 memset(&sgreq
, 0, sizeof(sgreq
));
1092 sgreq
.src
= c_oil
->oil
.mfcc_origin
;
1093 sgreq
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
1095 pim_zlookup_sg_statistics(c_oil
);
1096 if (ioctl(pim
->mroute_socket
, SIOCGETSGCNT
, &sgreq
)) {
1097 if (PIM_DEBUG_MROUTE
) {
1098 struct prefix_sg sg
;
1100 sg
.src
= c_oil
->oil
.mfcc_origin
;
1101 sg
.grp
= c_oil
->oil
.mfcc_mcastgrp
;
1104 "ioctl(SIOCGETSGCNT=%lu) failure for (S,G)=%s: errno=%d: %s",
1105 (unsigned long)SIOCGETSGCNT
,
1106 pim_str_sg_dump(&sg
), errno
,
1107 safe_strerror(errno
));
1112 c_oil
->cc
.pktcnt
= sgreq
.pktcnt
;
1113 c_oil
->cc
.bytecnt
= sgreq
.bytecnt
;
1114 c_oil
->cc
.wrong_if
= sgreq
.wrong_if
;