3 * Copyright (C) 2015 Cumulus Networks, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include "pim_mroute.h"
32 #include "pim_iface.h"
37 #include "pim_register.h"
38 #include "pim_upstream.h"
42 #include "pim_zebra.h"
46 #include "pim_vxlan.h"
49 struct thread
*send_test_packet_timer
= NULL
;
51 void pim_register_join(struct pim_upstream
*up
)
53 struct pim_instance
*pim
= up
->channel_oil
->pim
;
55 if (pim_is_grp_ssm(pim
, up
->sg
.grp
)) {
56 if (PIM_DEBUG_PIM_EVENTS
)
57 zlog_debug("%s register setup skipped as group is SSM",
62 pim_channel_add_oif(up
->channel_oil
, pim
->regiface
,
63 PIM_OIF_FLAG_PROTO_PIM
, __func__
);
64 up
->reg_state
= PIM_REG_JOIN
;
65 pim_vxlan_update_sg_reg_state(pim
, up
, true);
68 void pim_register_stop_send(struct interface
*ifp
, pim_sgaddr
*sg
, pim_addr src
,
71 struct pim_interface
*pinfo
;
72 unsigned char buffer
[10000];
73 unsigned int b1length
= 0;
77 if (PIM_DEBUG_PIM_REG
) {
78 zlog_debug("Sending Register stop for %pSG to %pPA on %s", sg
,
79 &originator
, ifp
->name
);
82 memset(buffer
, 0, 10000);
83 b1
= (uint8_t *)buffer
+ PIM_MSG_REGISTER_STOP_LEN
;
85 length
= pim_encode_addr_group(b1
, AFI_IP
, 0, 0, sg
->grp
);
89 length
= pim_encode_addr_ucast(b1
, sg
->src
);
92 pim_msg_build_header(src
, originator
, buffer
,
93 b1length
+ PIM_MSG_REGISTER_STOP_LEN
,
94 PIM_MSG_TYPE_REG_STOP
, false);
96 pinfo
= (struct pim_interface
*)ifp
->info
;
98 if (PIM_DEBUG_PIM_TRACE
)
99 zlog_debug("%s: No pinfo!", __func__
);
102 if (pim_msg_send(pinfo
->pim_sock_fd
, src
, originator
, buffer
,
103 b1length
+ PIM_MSG_REGISTER_STOP_LEN
, ifp
)) {
104 if (PIM_DEBUG_PIM_TRACE
) {
106 "%s: could not send PIM register stop message on interface %s",
107 __func__
, ifp
->name
);
111 if (!pinfo
->pim_passive_enable
)
112 ++pinfo
->pim_ifstat_reg_stop_send
;
115 static void pim_reg_stop_upstream(struct pim_instance
*pim
,
116 struct pim_upstream
*up
)
118 switch (up
->reg_state
) {
123 up
->reg_state
= PIM_REG_PRUNE
;
124 pim_channel_del_oif(up
->channel_oil
, pim
->regiface
,
125 PIM_OIF_FLAG_PROTO_PIM
, __func__
);
126 pim_upstream_start_register_stop_timer(up
, 0);
127 pim_vxlan_update_sg_reg_state(pim
, up
, false);
129 case PIM_REG_JOIN_PENDING
:
130 up
->reg_state
= PIM_REG_PRUNE
;
131 pim_upstream_start_register_stop_timer(up
, 0);
136 int pim_register_stop_recv(struct interface
*ifp
, uint8_t *buf
, int buf_size
)
138 struct pim_interface
*pim_ifp
= ifp
->info
;
139 struct pim_instance
*pim
= pim_ifp
->pim
;
140 struct pim_upstream
*up
= NULL
;
144 struct listnode
*up_node
;
145 struct pim_upstream
*child
;
146 bool wrong_af
= false;
147 bool handling_star
= false;
150 if (pim_ifp
->pim_passive_enable
) {
151 if (PIM_DEBUG_PIM_PACKETS
)
153 "skip receiving PIM message on passive interface %s",
158 ++pim_ifp
->pim_ifstat_reg_stop_recv
;
160 memset(&sg
, 0, sizeof(sg
));
161 l
= pim_parse_addr_group(&sg
, buf
, buf_size
);
164 pim_parse_addr_ucast(&sg
.src
, buf
, buf_size
, &wrong_af
);
167 zlog_err("invalid AF in Register-Stop on %s", ifp
->name
);
172 if (PIM_DEBUG_PIM_REG
)
173 zlog_debug("Received Register stop for %pSG", &sg
);
175 rp
= RP(pim_ifp
->pim
, sg
.grp
);
177 rpf_addr
= pim_addr_from_prefix(&rp
->rpf_addr
);
178 /* As per RFC 7761, Section 4.9.4:
179 * A special wildcard value consisting of an address field of
180 * all zeros can be used to indicate any source.
182 if ((pim_addr_cmp(sg
.src
, rpf_addr
) == 0) ||
183 pim_addr_is_any(sg
.src
)) {
184 handling_star
= true;
185 sg
.src
= PIMADDR_ANY
;
191 * Handling Register-Stop(*,G) Messages at the DR:
192 * A Register-Stop(*,G) should be treated as a
193 * Register-Stop(S,G) for all (S,G) Register state
194 * machines that are not in the NoInfo state.
196 up
= pim_upstream_find(pim
, &sg
);
199 * If the upstream find actually found a particular
200 * S,G then we *know* that the following for loop
201 * is not going to execute and this is ok
203 for (ALL_LIST_ELEMENTS_RO(up
->sources
, up_node
, child
)) {
204 if (PIM_DEBUG_PIM_REG
)
205 zlog_debug("Executing Reg stop for %s",
208 pim_reg_stop_upstream(pim
, child
);
211 if (PIM_DEBUG_PIM_REG
)
212 zlog_debug("Executing Reg stop for %s", up
->sg_str
);
213 pim_reg_stop_upstream(pim
, up
);
218 * Unfortunately pim was unable to find a *,G
219 * but pim may still actually have individual
220 * S,G's that need to be processed. In that
221 * case pim must do the expensive walk to find
224 frr_each (rb_pim_upstream
, &pim
->upstream_head
, up
) {
225 if (pim_addr_cmp(up
->sg
.grp
, sg
.grp
) == 0) {
226 if (PIM_DEBUG_PIM_REG
)
227 zlog_debug("Executing Reg stop for %s",
229 pim_reg_stop_upstream(pim
, up
);
238 struct in6_addr
pim_register_get_unicast_v6_addr(struct pim_interface
*p_ifp
)
240 struct listnode
*node
;
241 struct listnode
*nextnode
;
242 struct pim_secondary_addr
*sec_addr
;
243 struct pim_interface
*pim_ifp
;
244 struct interface
*ifp
;
245 struct pim_instance
*pim
= p_ifp
->pim
;
247 /* Trying to get the unicast address from the RPF interface first */
248 for (ALL_LIST_ELEMENTS(p_ifp
->sec_addr_list
, node
, nextnode
,
250 if (!is_ipv6_global_unicast(&sec_addr
->addr
.u
.prefix6
))
253 return sec_addr
->addr
.u
.prefix6
;
256 /* Loop through all the pim interface and try to return a global
257 * unicast ipv6 address
259 FOR_ALL_INTERFACES (pim
->vrf
, ifp
) {
265 for (ALL_LIST_ELEMENTS(pim_ifp
->sec_addr_list
, node
, nextnode
,
267 if (!is_ipv6_global_unicast(&sec_addr
->addr
.u
.prefix6
))
270 return sec_addr
->addr
.u
.prefix6
;
274 zlog_warn("No global address found for use to send register message");
279 void pim_register_send(const uint8_t *buf
, int buf_size
, pim_addr src
,
280 struct pim_rpf
*rpg
, int null_register
,
281 struct pim_upstream
*up
)
283 unsigned char buffer
[10000];
285 struct pim_interface
*pinfo
;
286 struct interface
*ifp
;
287 pim_addr dst
= pim_addr_from_prefix(&rpg
->rpf_addr
);
289 if (PIM_DEBUG_PIM_REG
) {
290 zlog_debug("Sending %s %sRegister Packet to %pPA", up
->sg_str
,
291 null_register
? "NULL " : "", &dst
);
294 ifp
= rpg
->source_nexthop
.interface
;
296 if (PIM_DEBUG_PIM_REG
)
297 zlog_debug("%s: No interface to transmit register on",
301 pinfo
= (struct pim_interface
*)ifp
->info
;
303 if (PIM_DEBUG_PIM_REG
)
305 "%s: Interface: %s not configured for pim to transmit on!",
306 __func__
, ifp
->name
);
310 if (PIM_DEBUG_PIM_REG
) {
311 zlog_debug("%s: Sending %s %sRegister Packet to %pPA on %s",
312 __func__
, up
->sg_str
, null_register
? "NULL " : "",
316 memset(buffer
, 0, 10000);
317 b1
= buffer
+ PIM_MSG_HEADER_LEN
;
318 *b1
|= null_register
<< 6;
319 b1
= buffer
+ PIM_MSG_REGISTER_LEN
;
321 memcpy(b1
, (const unsigned char *)buf
, buf_size
);
324 /* While sending Register message to RP, we cannot use link-local
325 * address therefore using unicast ipv6 address here, choosing it
326 * from the RPF Interface
328 src
= pim_register_get_unicast_v6_addr(pinfo
);
330 pim_msg_build_header(src
, dst
, buffer
, buf_size
+ PIM_MSG_REGISTER_LEN
,
331 PIM_MSG_TYPE_REGISTER
, false);
333 if (!pinfo
->pim_passive_enable
)
334 ++pinfo
->pim_ifstat_reg_send
;
336 if (pim_msg_send(pinfo
->pim_sock_fd
, src
, dst
, buffer
,
337 buf_size
+ PIM_MSG_REGISTER_LEN
, ifp
)) {
338 if (PIM_DEBUG_PIM_TRACE
) {
340 "%s: could not send PIM register message on interface %s",
341 __func__
, ifp
->name
);
348 void pim_null_register_send(struct pim_upstream
*up
)
351 struct pim_interface
*pim_ifp
;
355 pim_ifp
= up
->rpf
.source_nexthop
.interface
->info
;
357 if (PIM_DEBUG_PIM_TRACE
)
359 "%s: Cannot send null-register for %s no valid iif",
360 __func__
, up
->sg_str
);
364 rpg
= RP(pim_ifp
->pim
, up
->sg
.grp
);
366 if (PIM_DEBUG_PIM_TRACE
)
368 "%s: Cannot send null-register for %s no RPF to the RP",
369 __func__
, up
->sg_str
);
373 memset(&ip_hdr
, 0, sizeof(ip_hdr
));
374 ip_hdr
.ip_p
= PIM_IP_PROTO_PIM
;
377 ip_hdr
.ip_src
= up
->sg
.src
;
378 ip_hdr
.ip_dst
= up
->sg
.grp
;
379 ip_hdr
.ip_len
= htons(20);
381 /* checksum is broken */
382 src
= pim_ifp
->primary_address
;
383 if (PIM_UPSTREAM_FLAG_TEST_SRC_VXLAN_ORIG(up
->flags
)) {
384 if (!pim_vxlan_get_register_src(pim_ifp
->pim
, up
, &src
)) {
385 if (PIM_DEBUG_PIM_TRACE
)
387 "%s: Cannot send null-register for %s vxlan-aa PIP unavailable",
388 __func__
, up
->sg_str
);
392 pim_register_send((uint8_t *)&ip_hdr
, sizeof(struct ip
), src
, rpg
, 1,
396 void pim_null_register_send(struct pim_upstream
*up
)
398 struct ip6_hdr ip6_hdr
;
399 struct pim_msg_header pim_msg_header
;
400 struct pim_interface
*pim_ifp
;
403 unsigned char buffer
[sizeof(ip6_hdr
) + sizeof(pim_msg_header
)];
406 pim_ifp
= up
->rpf
.source_nexthop
.interface
->info
;
408 if (PIM_DEBUG_PIM_TRACE
)
410 "Cannot send null-register for %s no valid iif",
415 rpg
= RP(pim_ifp
->pim
, up
->sg
.grp
);
417 if (PIM_DEBUG_PIM_TRACE
)
419 "Cannot send null-register for %s no RPF to the RP",
424 memset(&ip6_hdr
, 0, sizeof(ip6_hdr
));
425 ip6_hdr
.ip6_nxt
= PIM_IP_PROTO_PIM
;
426 ip6_hdr
.ip6_plen
= PIM_MSG_HEADER_LEN
;
427 ip6_hdr
.ip6_vfc
= 6 << 4;
428 ip6_hdr
.ip6_hlim
= MAXTTL
;
429 ip6_hdr
.ip6_src
= up
->sg
.src
;
430 ip6_hdr
.ip6_dst
= up
->sg
.grp
;
432 memset(buffer
, 0, (sizeof(ip6_hdr
) + sizeof(pim_msg_header
)));
433 memcpy(buffer
, &ip6_hdr
, sizeof(ip6_hdr
));
435 pim_msg_header
.ver
= 0;
436 pim_msg_header
.type
= 0;
437 pim_msg_header
.reserved
= 0;
439 pim_msg_header
.checksum
= 0;
443 ph
.ulpl
= htonl(PIM_MSG_HEADER_LEN
);
444 ph
.next_hdr
= IPPROTO_PIM
;
445 pim_msg_header
.checksum
=
446 in_cksum_with_ph6(&ph
, &pim_msg_header
, PIM_MSG_HEADER_LEN
);
448 memcpy(buffer
+ sizeof(ip6_hdr
), &pim_msg_header
, PIM_MSG_HEADER_LEN
);
451 src
= pim_ifp
->primary_address
;
452 pim_register_send((uint8_t *)buffer
,
453 sizeof(ip6_hdr
) + PIM_MSG_HEADER_LEN
, src
, rpg
, 1,
459 * 4.4.2 Receiving Register Messages at the RP
461 * When an RP receives a Register message, the course of action is
462 * decided according to the following pseudocode:
464 * packet_arrives_on_rp_tunnel( pkt ) {
465 * if( outer.dst is not one of my addresses ) {
466 * drop the packet silently.
467 * # Note: this may be a spoofing attempt
469 * if( I_am_RP(G) AND outer.dst == RP(G) ) {
470 * sentRegisterStop = false;
471 * if ( register.borderbit == true ) {
472 * if ( PMBR(S,G) == unknown ) {
473 * PMBR(S,G) = outer.src
474 * } else if ( outer.src != PMBR(S,G) ) {
475 * send Register-Stop(S,G) to outer.src
476 * drop the packet silently.
479 * if ( SPTbit(S,G) OR
480 * ( SwitchToSptDesired(S,G) AND
481 * ( inherited_olist(S,G) == NULL ))) {
482 * send Register-Stop(S,G) to outer.src
483 * sentRegisterStop = true;
485 * if ( SPTbit(S,G) OR SwitchToSptDesired(S,G) ) {
486 * if ( sentRegisterStop == true ) {
487 * set KeepaliveTimer(S,G) to RP_Keepalive_Period;
489 * set KeepaliveTimer(S,G) to Keepalive_Period;
492 * if( !SPTbit(S,G) AND ! pkt.NullRegisterBit ) {
493 * decapsulate and forward the inner packet to
494 * inherited_olist(S,G,rpt) # Note (+)
497 * send Register-Stop(S,G) to outer.src
502 int pim_register_recv(struct interface
*ifp
, pim_addr dest_addr
,
503 pim_addr src_addr
, uint8_t *tlv_buf
, int tlv_buf_size
)
505 int sentRegisterStop
= 0;
510 struct pim_interface
*pim_ifp
= ifp
->info
;
511 struct pim_instance
*pim
= pim_ifp
->pim
;
514 if (pim_ifp
->pim_passive_enable
) {
515 if (PIM_DEBUG_PIM_PACKETS
)
517 "skip receiving PIM message on passive interface %s",
522 #define PIM_MSG_REGISTER_BIT_RESERVED_LEN 4
523 ip_hdr
= (tlv_buf
+ PIM_MSG_REGISTER_BIT_RESERVED_LEN
);
525 if (!if_address_is_local(&dest_addr
, PIM_AF
, pim
->vrf
->vrf_id
)) {
526 if (PIM_DEBUG_PIM_REG
)
528 "%s: Received Register message for destination address: %pPA that I do not own",
529 __func__
, &dest_addr
);
533 ++pim_ifp
->pim_ifstat_reg_recv
;
536 * Please note this is not drawn to get the correct bit/data size
538 * The entirety of the REGISTER packet looks like this:
539 * -------------------------------------------------------------
540 * | Ver | Type | Reserved | Checksum |
541 * |-----------------------------------------------------------|
543 * |-----------------------------------------------------------|
546 * | Packet |--------------------------------------------------|
551 * tlv_buf when received from the caller points at the B bit
552 * We need to know the inner source and dest
554 bits
= (uint32_t *)tlv_buf
;
557 * tlv_buf points to the start of the |B|N|... Reserved
558 * Line above. So we need to add 4 bytes to get to the
559 * start of the actual Encapsulated data.
561 memset(&sg
, 0, sizeof(sg
));
562 sg
= pim_sgaddr_from_iphdr(ip_hdr
);
566 * According to RFC section 4.9.3, If Dummy PIM Header is included
567 * in NULL Register as a payload there would be two PIM headers.
568 * The inner PIM Header's checksum field should also be validated
569 * in addition to the outer PIM Header's checksum. Validation of
570 * inner PIM header checksum is done here.
572 if ((*bits
& PIM_REGISTER_NR_BIT
) &&
573 ((tlv_buf_size
- PIM_MSG_REGISTER_BIT_RESERVED_LEN
) >
574 (int)sizeof(struct ip6_hdr
))) {
575 uint16_t computed_checksum
;
576 uint16_t received_checksum
;
578 struct pim_msg_header
*header
;
580 header
= (struct pim_msg_header
582 PIM_MSG_REGISTER_BIT_RESERVED_LEN
+
583 sizeof(struct ip6_hdr
));
586 ph
.ulpl
= htonl(PIM_MSG_HEADER_LEN
);
587 ph
.next_hdr
= IPPROTO_PIM
;
589 received_checksum
= header
->checksum
;
591 header
->checksum
= 0;
592 computed_checksum
= in_cksum_with_ph6(
593 &ph
, header
, htonl(PIM_MSG_HEADER_LEN
));
595 if (computed_checksum
!= received_checksum
) {
596 if (PIM_DEBUG_PIM_PACKETS
)
598 "Ignoring Null Register message%pSG from %pPA due to bad checksum in Encapsulated dummy PIM header",
604 i_am_rp
= I_am_RP(pim
, sg
.grp
);
606 if (PIM_DEBUG_PIM_REG
)
608 "Received Register message%pSG from %pPA on %s, rp: %d",
609 &sg
, &src_addr
, ifp
->name
, i_am_rp
);
611 if (pim_is_grp_ssm(pim_ifp
->pim
, sg
.grp
)) {
612 if (pim_addr_is_any(sg
.src
)) {
614 "%s: Received Register message for Group(%pPA) is now in SSM, dropping the packet",
616 /* Drop Packet Silently */
621 rp_addr
= pim_addr_from_prefix(&(RP(pim
, sg
.grp
))->rpf_addr
);
622 if (i_am_rp
&& (!pim_addr_cmp(dest_addr
, rp_addr
))) {
623 sentRegisterStop
= 0;
625 if (pim
->register_plist
) {
626 struct prefix_list
*plist
;
629 plist
= prefix_list_lookup(PIM_AFI
,
630 pim
->register_plist
);
632 pim_addr_to_prefix(&src
, sg
.src
);
634 if (prefix_list_apply(plist
, &src
) == PREFIX_DENY
) {
635 pim_register_stop_send(ifp
, &sg
, dest_addr
,
637 if (PIM_DEBUG_PIM_PACKETS
)
639 "%s: Sending register-stop to %pPA for %pSG due to prefix-list denial, dropping packet",
640 __func__
, &src_addr
, &sg
);
646 if (*bits
& PIM_REGISTER_BORDER_BIT
) {
647 pim_addr pimbr
= pim_br_get_pmbr(&sg
);
648 if (PIM_DEBUG_PIM_PACKETS
)
650 "%s: Received Register message with Border bit set",
653 if (pim_addr_is_any(pimbr
))
654 pim_br_set_pmbr(&sg
, src_addr
);
655 else if (pim_addr_cmp(src_addr
, pimbr
)) {
656 pim_register_stop_send(ifp
, &sg
, dest_addr
,
658 if (PIM_DEBUG_PIM_PACKETS
)
660 "%s: Sending register-Stop to %s and dropping mr. packet",
662 /* Drop Packet Silently */
667 struct pim_upstream
*upstream
= pim_upstream_find(pim
, &sg
);
669 * If we don't have a place to send ignore the packet
672 upstream
= pim_upstream_add(
674 PIM_UPSTREAM_FLAG_MASK_SRC_STREAM
, __func__
,
677 zlog_warn("Failure to create upstream state");
681 upstream
->upstream_register
= src_addr
;
684 * If the FHR has set a very very fast register timer
685 * there exists a possibility that the incoming NULL
687 * is happening before we set the spt bit. If so
688 * Do a quick check to update the counters and
689 * then set the spt bit as appropriate
691 if (upstream
->sptbit
!= PIM_UPSTREAM_SPTBIT_TRUE
) {
692 pim_mroute_update_counters(
693 upstream
->channel_oil
);
695 * Have we seen packets?
697 if (upstream
->channel_oil
->cc
.oldpktcnt
698 < upstream
->channel_oil
->cc
.pktcnt
)
699 pim_upstream_set_sptbit(
701 upstream
->rpf
.source_nexthop
706 if ((upstream
->sptbit
== PIM_UPSTREAM_SPTBIT_TRUE
)
707 || ((SwitchToSptDesiredOnRp(pim
, &sg
))
708 && pim_upstream_inherited_olist(pim
, upstream
) == 0)) {
709 pim_register_stop_send(ifp
, &sg
, dest_addr
, src_addr
);
710 sentRegisterStop
= 1;
712 if (PIM_DEBUG_PIM_REG
)
713 zlog_debug("(%s) sptbit: %d", upstream
->sg_str
,
716 if ((upstream
->sptbit
== PIM_UPSTREAM_SPTBIT_TRUE
)
717 || (SwitchToSptDesiredOnRp(pim
, &sg
))) {
718 if (sentRegisterStop
) {
719 pim_upstream_keep_alive_timer_start(
720 upstream
, pim
->rp_keep_alive_time
);
722 pim_upstream_keep_alive_timer_start(
723 upstream
, pim
->keep_alive_time
);
727 if (!(upstream
->sptbit
== PIM_UPSTREAM_SPTBIT_TRUE
)
728 && !(*bits
& PIM_REGISTER_NR_BIT
)) {
729 // decapsulate and forward the iner packet to
730 // inherited_olist(S,G,rpt)
731 // This is taken care of by the kernel for us
733 pim_upstream_msdp_reg_timer_start(upstream
);
735 if (PIM_DEBUG_PIM_REG
) {
737 zlog_debug("Received Register packet for %pSG, Rejecting packet because I am not the RP configured for group",
740 zlog_debug("Received Register packet for %pSG, Rejecting packet because the dst ip address is not the actual RP",
743 pim_register_stop_send(ifp
, &sg
, dest_addr
, src_addr
);
750 * This routine scan all upstream and update register state and remove pimreg
751 * when couldreg becomes false.
753 void pim_reg_del_on_couldreg_fail(struct interface
*ifp
)
755 struct pim_interface
*pim_ifp
= ifp
->info
;
756 struct pim_instance
*pim
;
757 struct pim_upstream
*up
;
764 frr_each (rb_pim_upstream
, &pim
->upstream_head
, up
) {
765 if (ifp
!= up
->rpf
.source_nexthop
.interface
)
768 if (!pim_upstream_could_register(up
)
769 && (up
->reg_state
!= PIM_REG_NOINFO
)) {
770 pim_channel_del_oif(up
->channel_oil
, pim
->regiface
,
771 PIM_OIF_FLAG_PROTO_PIM
, __func__
);
772 THREAD_OFF(up
->t_rs_timer
);
773 up
->reg_state
= PIM_REG_NOINFO
;