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"
41 #include "pim_zebra.h"
45 #include "pim_vxlan.h"
48 struct thread
*send_test_packet_timer
= NULL
;
50 void pim_register_join(struct pim_upstream
*up
)
52 struct pim_instance
*pim
= up
->channel_oil
->pim
;
54 if (pim_is_grp_ssm(pim
, up
->sg
.grp
)) {
55 if (PIM_DEBUG_PIM_EVENTS
)
56 zlog_debug("%s register setup skipped as group is SSM",
61 pim_channel_add_oif(up
->channel_oil
, pim
->regiface
,
62 PIM_OIF_FLAG_PROTO_PIM
, __func__
);
63 up
->reg_state
= PIM_REG_JOIN
;
64 pim_vxlan_update_sg_reg_state(pim
, up
, true);
67 void pim_register_stop_send(struct interface
*ifp
, pim_sgaddr
*sg
, pim_addr src
,
70 struct pim_interface
*pinfo
;
71 unsigned char buffer
[10000];
72 unsigned int b1length
= 0;
76 if (PIM_DEBUG_PIM_REG
) {
77 zlog_debug("Sending Register stop for %pSG to %pPA on %s", sg
,
78 &originator
, ifp
->name
);
81 memset(buffer
, 0, 10000);
82 b1
= (uint8_t *)buffer
+ PIM_MSG_REGISTER_STOP_LEN
;
84 length
= pim_encode_addr_group(b1
, AFI_IP
, 0, 0, sg
->grp
);
88 length
= pim_encode_addr_ucast(b1
, sg
->src
);
91 pim_msg_build_header(src
, originator
, buffer
,
92 b1length
+ PIM_MSG_REGISTER_STOP_LEN
,
93 PIM_MSG_TYPE_REG_STOP
, false);
95 pinfo
= (struct pim_interface
*)ifp
->info
;
97 if (PIM_DEBUG_PIM_TRACE
)
98 zlog_debug("%s: No pinfo!", __func__
);
101 if (pim_msg_send(pinfo
->pim_sock_fd
, src
, originator
, buffer
,
102 b1length
+ PIM_MSG_REGISTER_STOP_LEN
, ifp
)) {
103 if (PIM_DEBUG_PIM_TRACE
) {
105 "%s: could not send PIM register stop message on interface %s",
106 __func__
, ifp
->name
);
110 if (!pinfo
->pim_passive_enable
)
111 ++pinfo
->pim_ifstat_reg_stop_send
;
114 static void pim_reg_stop_upstream(struct pim_instance
*pim
,
115 struct pim_upstream
*up
)
117 switch (up
->reg_state
) {
122 up
->reg_state
= PIM_REG_PRUNE
;
123 pim_channel_del_oif(up
->channel_oil
, pim
->regiface
,
124 PIM_OIF_FLAG_PROTO_PIM
, __func__
);
125 pim_upstream_start_register_stop_timer(up
, 0);
126 pim_vxlan_update_sg_reg_state(pim
, up
, false);
128 case PIM_REG_JOIN_PENDING
:
129 up
->reg_state
= PIM_REG_PRUNE
;
130 pim_upstream_start_register_stop_timer(up
, 0);
135 int pim_register_stop_recv(struct interface
*ifp
, uint8_t *buf
, int buf_size
)
137 struct pim_interface
*pim_ifp
= ifp
->info
;
138 struct pim_instance
*pim
= pim_ifp
->pim
;
139 struct pim_upstream
*up
= NULL
;
142 struct listnode
*up_node
;
143 struct pim_upstream
*child
;
144 bool wrong_af
= false;
145 bool handling_star
= false;
148 if (pim_ifp
->pim_passive_enable
) {
149 if (PIM_DEBUG_PIM_PACKETS
)
151 "skip receiving PIM message on passive interface %s",
156 ++pim_ifp
->pim_ifstat_reg_stop_recv
;
158 memset(&sg
, 0, sizeof(sg
));
159 l
= pim_parse_addr_group(&sg
, buf
, buf_size
);
162 pim_parse_addr_ucast(&sg
.src
, buf
, buf_size
, &wrong_af
);
165 zlog_err("invalid AF in Register-Stop on %s", ifp
->name
);
170 if (PIM_DEBUG_PIM_REG
)
171 zlog_debug("Received Register stop for %pSG", &sg
);
173 rp
= RP(pim_ifp
->pim
, sg
.grp
);
175 /* As per RFC 7761, Section 4.9.4:
176 * A special wildcard value consisting of an address field of
177 * all zeros can be used to indicate any source.
179 if ((pim_addr_cmp(sg
.src
, rp
->rpf_addr
) == 0) ||
180 pim_addr_is_any(sg
.src
)) {
181 handling_star
= true;
182 sg
.src
= PIMADDR_ANY
;
188 * Handling Register-Stop(*,G) Messages at the DR:
189 * A Register-Stop(*,G) should be treated as a
190 * Register-Stop(S,G) for all (S,G) Register state
191 * machines that are not in the NoInfo state.
193 up
= pim_upstream_find(pim
, &sg
);
196 * If the upstream find actually found a particular
197 * S,G then we *know* that the following for loop
198 * is not going to execute and this is ok
200 for (ALL_LIST_ELEMENTS_RO(up
->sources
, up_node
, child
)) {
201 if (PIM_DEBUG_PIM_REG
)
202 zlog_debug("Executing Reg stop for %s",
205 pim_reg_stop_upstream(pim
, child
);
208 if (PIM_DEBUG_PIM_REG
)
209 zlog_debug("Executing Reg stop for %s", up
->sg_str
);
210 pim_reg_stop_upstream(pim
, up
);
215 * Unfortunately pim was unable to find a *,G
216 * but pim may still actually have individual
217 * S,G's that need to be processed. In that
218 * case pim must do the expensive walk to find
221 frr_each (rb_pim_upstream
, &pim
->upstream_head
, up
) {
222 if (pim_addr_cmp(up
->sg
.grp
, sg
.grp
) == 0) {
223 if (PIM_DEBUG_PIM_REG
)
224 zlog_debug("Executing Reg stop for %s",
226 pim_reg_stop_upstream(pim
, up
);
235 struct in6_addr
pim_register_get_unicast_v6_addr(struct pim_interface
*p_ifp
)
237 struct listnode
*node
;
238 struct listnode
*nextnode
;
239 struct pim_secondary_addr
*sec_addr
;
240 struct pim_interface
*pim_ifp
;
241 struct interface
*ifp
;
242 struct pim_instance
*pim
= p_ifp
->pim
;
244 /* Trying to get the unicast address from the RPF interface first */
245 for (ALL_LIST_ELEMENTS(p_ifp
->sec_addr_list
, node
, nextnode
,
247 if (!is_ipv6_global_unicast(&sec_addr
->addr
.u
.prefix6
))
250 return sec_addr
->addr
.u
.prefix6
;
253 /* Loop through all the pim interface and try to return a global
254 * unicast ipv6 address
256 FOR_ALL_INTERFACES (pim
->vrf
, ifp
) {
262 for (ALL_LIST_ELEMENTS(pim_ifp
->sec_addr_list
, node
, nextnode
,
264 if (!is_ipv6_global_unicast(&sec_addr
->addr
.u
.prefix6
))
267 return sec_addr
->addr
.u
.prefix6
;
271 zlog_warn("No global address found for use to send register message");
276 void pim_register_send(const uint8_t *buf
, int buf_size
, pim_addr src
,
277 struct pim_rpf
*rpg
, int null_register
,
278 struct pim_upstream
*up
)
280 unsigned char buffer
[10000];
282 struct pim_interface
*pinfo
;
283 struct interface
*ifp
;
285 if (PIM_DEBUG_PIM_REG
) {
286 zlog_debug("Sending %s %sRegister Packet to %pPA", up
->sg_str
,
287 null_register
? "NULL " : "", &rpg
->rpf_addr
);
290 ifp
= rpg
->source_nexthop
.interface
;
292 if (PIM_DEBUG_PIM_REG
)
293 zlog_debug("%s: No interface to transmit register on",
297 pinfo
= (struct pim_interface
*)ifp
->info
;
299 if (PIM_DEBUG_PIM_REG
)
301 "%s: Interface: %s not configured for pim to transmit on!",
302 __func__
, ifp
->name
);
306 if (PIM_DEBUG_PIM_REG
) {
307 zlog_debug("%s: Sending %s %sRegister Packet to %pPA on %s",
308 __func__
, up
->sg_str
, null_register
? "NULL " : "",
309 &rpg
->rpf_addr
, ifp
->name
);
312 memset(buffer
, 0, 10000);
313 b1
= buffer
+ PIM_MSG_HEADER_LEN
;
314 *b1
|= null_register
<< 6;
315 b1
= buffer
+ PIM_MSG_REGISTER_LEN
;
317 memcpy(b1
, (const unsigned char *)buf
, buf_size
);
320 /* While sending Register message to RP, we cannot use link-local
321 * address therefore using unicast ipv6 address here, choosing it
322 * from the RPF Interface
324 src
= pim_register_get_unicast_v6_addr(pinfo
);
326 pim_msg_build_header(src
, rpg
->rpf_addr
, buffer
,
327 buf_size
+ PIM_MSG_REGISTER_LEN
,
328 PIM_MSG_TYPE_REGISTER
, false);
330 if (!pinfo
->pim_passive_enable
)
331 ++pinfo
->pim_ifstat_reg_send
;
333 if (pim_msg_send(pinfo
->pim
->reg_sock
, src
, rpg
->rpf_addr
, buffer
,
334 buf_size
+ PIM_MSG_REGISTER_LEN
, ifp
)) {
335 if (PIM_DEBUG_PIM_TRACE
) {
337 "%s: could not send PIM register message on interface %s",
338 __func__
, ifp
->name
);
345 void pim_null_register_send(struct pim_upstream
*up
)
348 struct pim_interface
*pim_ifp
;
352 pim_ifp
= up
->rpf
.source_nexthop
.interface
->info
;
354 if (PIM_DEBUG_PIM_TRACE
)
356 "%s: Cannot send null-register for %s no valid iif",
357 __func__
, up
->sg_str
);
361 rpg
= RP(pim_ifp
->pim
, up
->sg
.grp
);
363 if (PIM_DEBUG_PIM_TRACE
)
365 "%s: Cannot send null-register for %s no RPF to the RP",
366 __func__
, up
->sg_str
);
370 memset(&ip_hdr
, 0, sizeof(ip_hdr
));
371 ip_hdr
.ip_p
= PIM_IP_PROTO_PIM
;
374 ip_hdr
.ip_src
= up
->sg
.src
;
375 ip_hdr
.ip_dst
= up
->sg
.grp
;
376 ip_hdr
.ip_len
= htons(20);
378 /* checksum is broken */
379 src
= pim_ifp
->primary_address
;
380 if (PIM_UPSTREAM_FLAG_TEST_SRC_VXLAN_ORIG(up
->flags
)) {
381 if (!pim_vxlan_get_register_src(pim_ifp
->pim
, up
, &src
)) {
382 if (PIM_DEBUG_PIM_TRACE
)
384 "%s: Cannot send null-register for %s vxlan-aa PIP unavailable",
385 __func__
, up
->sg_str
);
389 pim_register_send((uint8_t *)&ip_hdr
, sizeof(struct ip
), src
, rpg
, 1,
393 void pim_null_register_send(struct pim_upstream
*up
)
395 struct ip6_hdr ip6_hdr
;
396 struct pim_msg_header pim_msg_header
;
397 struct pim_interface
*pim_ifp
;
400 unsigned char buffer
[sizeof(ip6_hdr
) + sizeof(pim_msg_header
)];
403 pim_ifp
= up
->rpf
.source_nexthop
.interface
->info
;
405 if (PIM_DEBUG_PIM_TRACE
)
407 "Cannot send null-register for %s no valid iif",
412 rpg
= RP(pim_ifp
->pim
, up
->sg
.grp
);
414 if (PIM_DEBUG_PIM_TRACE
)
416 "Cannot send null-register for %s no RPF to the RP",
421 memset(&ip6_hdr
, 0, sizeof(ip6_hdr
));
422 ip6_hdr
.ip6_nxt
= PIM_IP_PROTO_PIM
;
423 ip6_hdr
.ip6_plen
= PIM_MSG_HEADER_LEN
;
424 ip6_hdr
.ip6_vfc
= 6 << 4;
425 ip6_hdr
.ip6_hlim
= MAXTTL
;
426 ip6_hdr
.ip6_src
= up
->sg
.src
;
427 ip6_hdr
.ip6_dst
= up
->sg
.grp
;
429 memset(buffer
, 0, (sizeof(ip6_hdr
) + sizeof(pim_msg_header
)));
430 memcpy(buffer
, &ip6_hdr
, sizeof(ip6_hdr
));
432 pim_msg_header
.ver
= 0;
433 pim_msg_header
.type
= 0;
434 pim_msg_header
.reserved
= 0;
436 pim_msg_header
.checksum
= 0;
440 ph
.ulpl
= htonl(PIM_MSG_HEADER_LEN
);
441 ph
.next_hdr
= IPPROTO_PIM
;
442 pim_msg_header
.checksum
=
443 in_cksum_with_ph6(&ph
, &pim_msg_header
, PIM_MSG_HEADER_LEN
);
445 memcpy(buffer
+ sizeof(ip6_hdr
), &pim_msg_header
, PIM_MSG_HEADER_LEN
);
448 src
= pim_ifp
->primary_address
;
449 pim_register_send((uint8_t *)buffer
,
450 sizeof(ip6_hdr
) + PIM_MSG_HEADER_LEN
, src
, rpg
, 1,
456 * 4.4.2 Receiving Register Messages at the RP
458 * When an RP receives a Register message, the course of action is
459 * decided according to the following pseudocode:
461 * packet_arrives_on_rp_tunnel( pkt ) {
462 * if( outer.dst is not one of my addresses ) {
463 * drop the packet silently.
464 * # Note: this may be a spoofing attempt
466 * if( I_am_RP(G) AND outer.dst == RP(G) ) {
467 * sentRegisterStop = false;
468 * if ( register.borderbit == true ) {
469 * if ( PMBR(S,G) == unknown ) {
470 * PMBR(S,G) = outer.src
471 * } else if ( outer.src != PMBR(S,G) ) {
472 * send Register-Stop(S,G) to outer.src
473 * drop the packet silently.
476 * if ( SPTbit(S,G) OR
477 * ( SwitchToSptDesired(S,G) AND
478 * ( inherited_olist(S,G) == NULL ))) {
479 * send Register-Stop(S,G) to outer.src
480 * sentRegisterStop = true;
482 * if ( SPTbit(S,G) OR SwitchToSptDesired(S,G) ) {
483 * if ( sentRegisterStop == true ) {
484 * set KeepaliveTimer(S,G) to RP_Keepalive_Period;
486 * set KeepaliveTimer(S,G) to Keepalive_Period;
489 * if( !SPTbit(S,G) AND ! pkt.NullRegisterBit ) {
490 * decapsulate and forward the inner packet to
491 * inherited_olist(S,G,rpt) # Note (+)
494 * send Register-Stop(S,G) to outer.src
499 int pim_register_recv(struct interface
*ifp
, pim_addr dest_addr
,
500 pim_addr src_addr
, uint8_t *tlv_buf
, int tlv_buf_size
)
502 int sentRegisterStop
= 0;
507 struct pim_interface
*pim_ifp
= ifp
->info
;
508 struct pim_instance
*pim
= pim_ifp
->pim
;
511 if (pim_ifp
->pim_passive_enable
) {
512 if (PIM_DEBUG_PIM_PACKETS
)
514 "skip receiving PIM message on passive interface %s",
519 #define PIM_MSG_REGISTER_BIT_RESERVED_LEN 4
520 ip_hdr
= (tlv_buf
+ PIM_MSG_REGISTER_BIT_RESERVED_LEN
);
522 if (!if_address_is_local(&dest_addr
, PIM_AF
, pim
->vrf
->vrf_id
)) {
523 if (PIM_DEBUG_PIM_REG
)
525 "%s: Received Register message for destination address: %pPA that I do not own",
526 __func__
, &dest_addr
);
530 ++pim_ifp
->pim_ifstat_reg_recv
;
533 * Please note this is not drawn to get the correct bit/data size
535 * The entirety of the REGISTER packet looks like this:
536 * -------------------------------------------------------------
537 * | Ver | Type | Reserved | Checksum |
538 * |-----------------------------------------------------------|
540 * |-----------------------------------------------------------|
543 * | Packet |--------------------------------------------------|
548 * tlv_buf when received from the caller points at the B bit
549 * We need to know the inner source and dest
551 bits
= (uint32_t *)tlv_buf
;
554 * tlv_buf points to the start of the |B|N|... Reserved
555 * Line above. So we need to add 4 bytes to get to the
556 * start of the actual Encapsulated data.
558 memset(&sg
, 0, sizeof(sg
));
559 sg
= pim_sgaddr_from_iphdr(ip_hdr
);
563 * According to RFC section 4.9.3, If Dummy PIM Header is included
564 * in NULL Register as a payload there would be two PIM headers.
565 * The inner PIM Header's checksum field should also be validated
566 * in addition to the outer PIM Header's checksum. Validation of
567 * inner PIM header checksum is done here.
569 if ((*bits
& PIM_REGISTER_NR_BIT
) &&
570 ((tlv_buf_size
- PIM_MSG_REGISTER_BIT_RESERVED_LEN
) >
571 (int)sizeof(struct ip6_hdr
))) {
572 uint16_t computed_checksum
;
573 uint16_t received_checksum
;
575 struct pim_msg_header
*header
;
577 header
= (struct pim_msg_header
579 PIM_MSG_REGISTER_BIT_RESERVED_LEN
+
580 sizeof(struct ip6_hdr
));
583 ph
.ulpl
= htonl(PIM_MSG_HEADER_LEN
);
584 ph
.next_hdr
= IPPROTO_PIM
;
586 received_checksum
= header
->checksum
;
588 header
->checksum
= 0;
589 computed_checksum
= in_cksum_with_ph6(
590 &ph
, header
, htonl(PIM_MSG_HEADER_LEN
));
592 if (computed_checksum
!= received_checksum
) {
593 if (PIM_DEBUG_PIM_PACKETS
)
595 "Ignoring Null Register message%pSG from %pPA due to bad checksum in Encapsulated dummy PIM header",
601 i_am_rp
= I_am_RP(pim
, sg
.grp
);
603 if (PIM_DEBUG_PIM_REG
)
605 "Received Register message%pSG from %pPA on %s, rp: %d",
606 &sg
, &src_addr
, ifp
->name
, i_am_rp
);
608 if (pim_is_grp_ssm(pim_ifp
->pim
, sg
.grp
)) {
609 if (pim_addr_is_any(sg
.src
)) {
611 "%s: Received Register message for Group(%pPA) is now in SSM, dropping the packet",
613 /* Drop Packet Silently */
618 rp_addr
= (RP(pim
, sg
.grp
))->rpf_addr
;
619 if (i_am_rp
&& (!pim_addr_cmp(dest_addr
, rp_addr
))) {
620 sentRegisterStop
= 0;
622 if (pim
->register_plist
) {
623 struct prefix_list
*plist
;
626 plist
= prefix_list_lookup(PIM_AFI
,
627 pim
->register_plist
);
629 pim_addr_to_prefix(&src
, sg
.src
);
631 if (prefix_list_apply(plist
, &src
) == PREFIX_DENY
) {
632 pim_register_stop_send(ifp
, &sg
, dest_addr
,
634 if (PIM_DEBUG_PIM_PACKETS
)
636 "%s: Sending register-stop to %pPA for %pSG due to prefix-list denial, dropping packet",
637 __func__
, &src_addr
, &sg
);
643 if (*bits
& PIM_REGISTER_BORDER_BIT
) {
644 if (PIM_DEBUG_PIM_PACKETS
)
646 "%s: Received Register message with Border bit set, ignoring",
649 /* Drop Packet Silently */
653 struct pim_upstream
*upstream
= pim_upstream_find(pim
, &sg
);
655 * If we don't have a place to send ignore the packet
658 upstream
= pim_upstream_add(
660 PIM_UPSTREAM_FLAG_MASK_SRC_STREAM
, __func__
,
663 zlog_warn("Failure to create upstream state");
667 upstream
->upstream_register
= src_addr
;
670 * If the FHR has set a very very fast register timer
671 * there exists a possibility that the incoming NULL
673 * is happening before we set the spt bit. If so
674 * Do a quick check to update the counters and
675 * then set the spt bit as appropriate
677 if (upstream
->sptbit
!= PIM_UPSTREAM_SPTBIT_TRUE
) {
678 pim_mroute_update_counters(
679 upstream
->channel_oil
);
681 * Have we seen packets?
683 if (upstream
->channel_oil
->cc
.oldpktcnt
684 < upstream
->channel_oil
->cc
.pktcnt
)
685 pim_upstream_set_sptbit(
687 upstream
->rpf
.source_nexthop
692 if ((upstream
->sptbit
== PIM_UPSTREAM_SPTBIT_TRUE
)
693 || ((SwitchToSptDesiredOnRp(pim
, &sg
))
694 && pim_upstream_inherited_olist(pim
, upstream
) == 0)) {
695 pim_register_stop_send(ifp
, &sg
, dest_addr
, src_addr
);
696 sentRegisterStop
= 1;
698 if (PIM_DEBUG_PIM_REG
)
699 zlog_debug("(%s) sptbit: %d", upstream
->sg_str
,
702 if ((upstream
->sptbit
== PIM_UPSTREAM_SPTBIT_TRUE
)
703 || (SwitchToSptDesiredOnRp(pim
, &sg
))) {
704 if (sentRegisterStop
) {
705 pim_upstream_keep_alive_timer_start(
706 upstream
, pim
->rp_keep_alive_time
);
708 pim_upstream_keep_alive_timer_start(
709 upstream
, pim
->keep_alive_time
);
713 if (!(upstream
->sptbit
== PIM_UPSTREAM_SPTBIT_TRUE
)
714 && !(*bits
& PIM_REGISTER_NR_BIT
)) {
715 // decapsulate and forward the iner packet to
716 // inherited_olist(S,G,rpt)
717 // This is taken care of by the kernel for us
719 pim_upstream_msdp_reg_timer_start(upstream
);
721 if (PIM_DEBUG_PIM_REG
) {
723 zlog_debug("Received Register packet for %pSG, Rejecting packet because I am not the RP configured for group",
726 zlog_debug("Received Register packet for %pSG, Rejecting packet because the dst ip address is not the actual RP",
729 pim_register_stop_send(ifp
, &sg
, dest_addr
, src_addr
);
736 * This routine scan all upstream and update register state and remove pimreg
737 * when couldreg becomes false.
739 void pim_reg_del_on_couldreg_fail(struct interface
*ifp
)
741 struct pim_interface
*pim_ifp
= ifp
->info
;
742 struct pim_instance
*pim
;
743 struct pim_upstream
*up
;
750 frr_each (rb_pim_upstream
, &pim
->upstream_head
, up
) {
751 if (ifp
!= up
->rpf
.source_nexthop
.interface
)
754 if (!pim_upstream_could_register(up
)
755 && (up
->reg_state
!= PIM_REG_NOINFO
)) {
756 pim_channel_del_oif(up
->channel_oil
, pim
->regiface
,
757 PIM_OIF_FLAG_PROTO_PIM
, __func__
);
758 THREAD_OFF(up
->t_rs_timer
);
759 up
->reg_state
= PIM_REG_NOINFO
;