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 if (pim_addr_cmp(sg
.src
, rpf_addr
) == 0) {
179 handling_star
= true;
180 sg
.src
= PIMADDR_ANY
;
186 * Handling Register-Stop(*,G) Messages at the DR:
187 * A Register-Stop(*,G) should be treated as a
188 * Register-Stop(S,G) for all (S,G) Register state
189 * machines that are not in the NoInfo state.
191 up
= pim_upstream_find(pim
, &sg
);
194 * If the upstream find actually found a particular
195 * S,G then we *know* that the following for loop
196 * is not going to execute and this is ok
198 for (ALL_LIST_ELEMENTS_RO(up
->sources
, up_node
, child
)) {
199 if (PIM_DEBUG_PIM_REG
)
200 zlog_debug("Executing Reg stop for %s",
203 pim_reg_stop_upstream(pim
, child
);
206 if (PIM_DEBUG_PIM_REG
)
207 zlog_debug("Executing Reg stop for %s", up
->sg_str
);
208 pim_reg_stop_upstream(pim
, up
);
213 * Unfortunately pim was unable to find a *,G
214 * but pim may still actually have individual
215 * S,G's that need to be processed. In that
216 * case pim must do the expensive walk to find
219 frr_each (rb_pim_upstream
, &pim
->upstream_head
, up
) {
220 if (pim_addr_cmp(up
->sg
.grp
, sg
.grp
) == 0) {
221 if (PIM_DEBUG_PIM_REG
)
222 zlog_debug("Executing Reg stop for %s",
224 pim_reg_stop_upstream(pim
, up
);
232 void pim_register_send(const uint8_t *buf
, int buf_size
, pim_addr src
,
233 struct pim_rpf
*rpg
, int null_register
,
234 struct pim_upstream
*up
)
236 unsigned char buffer
[10000];
238 struct pim_interface
*pinfo
;
239 struct interface
*ifp
;
240 pim_addr dst
= pim_addr_from_prefix(&rpg
->rpf_addr
);
242 if (PIM_DEBUG_PIM_REG
) {
243 zlog_debug("Sending %s %sRegister Packet to %pPA", up
->sg_str
,
244 null_register
? "NULL " : "", &dst
);
247 ifp
= rpg
->source_nexthop
.interface
;
249 if (PIM_DEBUG_PIM_REG
)
250 zlog_debug("%s: No interface to transmit register on",
254 pinfo
= (struct pim_interface
*)ifp
->info
;
256 if (PIM_DEBUG_PIM_REG
)
258 "%s: Interface: %s not configured for pim to transmit on!",
259 __func__
, ifp
->name
);
263 if (PIM_DEBUG_PIM_REG
) {
264 zlog_debug("%s: Sending %s %sRegister Packet to %pPA on %s",
265 __func__
, up
->sg_str
, null_register
? "NULL " : "",
269 memset(buffer
, 0, 10000);
270 b1
= buffer
+ PIM_MSG_HEADER_LEN
;
271 *b1
|= null_register
<< 6;
272 b1
= buffer
+ PIM_MSG_REGISTER_LEN
;
274 memcpy(b1
, (const unsigned char *)buf
, buf_size
);
276 pim_msg_build_header(src
, dst
, buffer
, buf_size
+ PIM_MSG_REGISTER_LEN
,
277 PIM_MSG_TYPE_REGISTER
, false);
279 if (!pinfo
->pim_passive_enable
)
280 ++pinfo
->pim_ifstat_reg_send
;
282 if (pim_msg_send(pinfo
->pim_sock_fd
, src
, dst
, buffer
,
283 buf_size
+ PIM_MSG_REGISTER_LEN
, ifp
)) {
284 if (PIM_DEBUG_PIM_TRACE
) {
286 "%s: could not send PIM register message on interface %s",
287 __func__
, ifp
->name
);
294 void pim_null_register_send(struct pim_upstream
*up
)
297 struct pim_interface
*pim_ifp
;
301 pim_ifp
= up
->rpf
.source_nexthop
.interface
->info
;
303 if (PIM_DEBUG_PIM_TRACE
)
305 "%s: Cannot send null-register for %s no valid iif",
306 __func__
, up
->sg_str
);
310 rpg
= RP(pim_ifp
->pim
, up
->sg
.grp
);
312 if (PIM_DEBUG_PIM_TRACE
)
314 "%s: Cannot send null-register for %s no RPF to the RP",
315 __func__
, up
->sg_str
);
319 memset(&ip_hdr
, 0, sizeof(ip_hdr
));
320 ip_hdr
.ip_p
= PIM_IP_PROTO_PIM
;
323 ip_hdr
.ip_src
= up
->sg
.src
;
324 ip_hdr
.ip_dst
= up
->sg
.grp
;
325 ip_hdr
.ip_len
= htons(20);
327 /* checksum is broken */
328 src
= pim_ifp
->primary_address
;
329 if (PIM_UPSTREAM_FLAG_TEST_SRC_VXLAN_ORIG(up
->flags
)) {
330 if (!pim_vxlan_get_register_src(pim_ifp
->pim
, up
, &src
)) {
331 if (PIM_DEBUG_PIM_TRACE
)
333 "%s: Cannot send null-register for %s vxlan-aa PIP unavailable",
334 __func__
, up
->sg_str
);
338 pim_register_send((uint8_t *)&ip_hdr
, sizeof(struct ip
), src
, rpg
, 1,
342 void pim_null_register_send(struct pim_upstream
*up
)
344 struct ip6_hdr ip6_hdr
;
345 struct pim_msg_header pim_msg_header
;
346 struct pim_interface
*pim_ifp
;
349 unsigned char buffer
[sizeof(ip6_hdr
) + sizeof(pim_msg_header
)];
352 pim_ifp
= up
->rpf
.source_nexthop
.interface
->info
;
354 if (PIM_DEBUG_PIM_TRACE
)
356 "Cannot send null-register for %s no valid iif",
361 rpg
= RP(pim_ifp
->pim
, up
->sg
.grp
);
363 if (PIM_DEBUG_PIM_TRACE
)
365 "Cannot send null-register for %s no RPF to the RP",
370 memset(&ip6_hdr
, 0, sizeof(ip6_hdr
));
371 ip6_hdr
.ip6_nxt
= PIM_IP_PROTO_PIM
;
372 ip6_hdr
.ip6_plen
= PIM_MSG_HEADER_LEN
;
373 ip6_hdr
.ip6_vfc
= 6 << 4;
374 ip6_hdr
.ip6_hlim
= MAXTTL
;
375 ip6_hdr
.ip6_src
= up
->sg
.src
;
376 ip6_hdr
.ip6_dst
= up
->sg
.grp
;
378 memset(buffer
, 0, (sizeof(ip6_hdr
) + sizeof(pim_msg_header
)));
379 memcpy(buffer
, &ip6_hdr
, sizeof(ip6_hdr
));
381 pim_msg_header
.ver
= 0;
382 pim_msg_header
.type
= 0;
383 pim_msg_header
.reserved
= 0;
385 pim_msg_header
.checksum
= 0;
389 ph
.ulpl
= htonl(PIM_MSG_HEADER_LEN
);
390 ph
.next_hdr
= IPPROTO_PIM
;
391 pim_msg_header
.checksum
=
392 in_cksum_with_ph6(&ph
, &pim_msg_header
, PIM_MSG_HEADER_LEN
);
394 memcpy(buffer
+ sizeof(ip6_hdr
), &pim_msg_header
, PIM_MSG_HEADER_LEN
);
397 src
= pim_ifp
->primary_address
;
398 pim_register_send((uint8_t *)buffer
,
399 sizeof(ip6_hdr
) + PIM_MSG_HEADER_LEN
, src
, rpg
, 1,
405 * 4.4.2 Receiving Register Messages at the RP
407 * When an RP receives a Register message, the course of action is
408 * decided according to the following pseudocode:
410 * packet_arrives_on_rp_tunnel( pkt ) {
411 * if( outer.dst is not one of my addresses ) {
412 * drop the packet silently.
413 * # Note: this may be a spoofing attempt
415 * if( I_am_RP(G) AND outer.dst == RP(G) ) {
416 * sentRegisterStop = false;
417 * if ( register.borderbit == true ) {
418 * if ( PMBR(S,G) == unknown ) {
419 * PMBR(S,G) = outer.src
420 * } else if ( outer.src != PMBR(S,G) ) {
421 * send Register-Stop(S,G) to outer.src
422 * drop the packet silently.
425 * if ( SPTbit(S,G) OR
426 * ( SwitchToSptDesired(S,G) AND
427 * ( inherited_olist(S,G) == NULL ))) {
428 * send Register-Stop(S,G) to outer.src
429 * sentRegisterStop = true;
431 * if ( SPTbit(S,G) OR SwitchToSptDesired(S,G) ) {
432 * if ( sentRegisterStop == true ) {
433 * set KeepaliveTimer(S,G) to RP_Keepalive_Period;
435 * set KeepaliveTimer(S,G) to Keepalive_Period;
438 * if( !SPTbit(S,G) AND ! pkt.NullRegisterBit ) {
439 * decapsulate and forward the inner packet to
440 * inherited_olist(S,G,rpt) # Note (+)
443 * send Register-Stop(S,G) to outer.src
448 int pim_register_recv(struct interface
*ifp
, pim_addr dest_addr
,
449 pim_addr src_addr
, uint8_t *tlv_buf
, int tlv_buf_size
)
451 int sentRegisterStop
= 0;
456 struct pim_interface
*pim_ifp
= ifp
->info
;
457 struct pim_instance
*pim
= pim_ifp
->pim
;
460 if (pim_ifp
->pim_passive_enable
) {
461 if (PIM_DEBUG_PIM_PACKETS
)
463 "skip receiving PIM message on passive interface %s",
468 #define PIM_MSG_REGISTER_BIT_RESERVED_LEN 4
469 ip_hdr
= (tlv_buf
+ PIM_MSG_REGISTER_BIT_RESERVED_LEN
);
471 if (!if_address_is_local(&dest_addr
, PIM_AF
, pim
->vrf
->vrf_id
)) {
472 if (PIM_DEBUG_PIM_REG
)
474 "%s: Received Register message for destination address: %pPA that I do not own",
475 __func__
, &dest_addr
);
479 ++pim_ifp
->pim_ifstat_reg_recv
;
482 * Please note this is not drawn to get the correct bit/data size
484 * The entirety of the REGISTER packet looks like this:
485 * -------------------------------------------------------------
486 * | Ver | Type | Reserved | Checksum |
487 * |-----------------------------------------------------------|
489 * |-----------------------------------------------------------|
492 * | Packet |--------------------------------------------------|
497 * tlv_buf when received from the caller points at the B bit
498 * We need to know the inner source and dest
500 bits
= (uint32_t *)tlv_buf
;
503 * tlv_buf points to the start of the |B|N|... Reserved
504 * Line above. So we need to add 4 bytes to get to the
505 * start of the actual Encapsulated data.
507 memset(&sg
, 0, sizeof(sg
));
508 sg
= pim_sgaddr_from_iphdr(ip_hdr
);
512 * According to RFC section 4.9.3, If Dummy PIM Header is included
513 * in NULL Register as a payload there would be two PIM headers.
514 * The inner PIM Header's checksum field should also be validated
515 * in addition to the outer PIM Header's checksum. Validation of
516 * inner PIM header checksum is done here.
518 if ((*bits
& PIM_REGISTER_NR_BIT
) &&
519 ((tlv_buf_size
- PIM_MSG_REGISTER_BIT_RESERVED_LEN
) >
520 (int)sizeof(struct ip6_hdr
))) {
521 uint16_t computed_checksum
;
522 uint16_t received_checksum
;
524 struct pim_msg_header
*header
;
526 header
= (struct pim_msg_header
528 PIM_MSG_REGISTER_BIT_RESERVED_LEN
+
529 sizeof(struct ip6_hdr
));
532 ph
.ulpl
= htonl(PIM_MSG_HEADER_LEN
);
533 ph
.next_hdr
= IPPROTO_PIM
;
535 received_checksum
= header
->checksum
;
537 header
->checksum
= 0;
538 computed_checksum
= in_cksum_with_ph6(
539 &ph
, header
, htonl(PIM_MSG_HEADER_LEN
));
541 if (computed_checksum
!= received_checksum
) {
542 if (PIM_DEBUG_PIM_PACKETS
)
544 "Ignoring Null Register message%pSG from %pPA due to bad checksum in Encapsulated dummy PIM header",
550 i_am_rp
= I_am_RP(pim
, sg
.grp
);
552 if (PIM_DEBUG_PIM_REG
)
554 "Received Register message%pSG from %pPA on %s, rp: %d",
555 &sg
, &src_addr
, ifp
->name
, i_am_rp
);
557 if (pim_is_grp_ssm(pim_ifp
->pim
, sg
.grp
)) {
558 if (pim_addr_is_any(sg
.src
)) {
560 "%s: Received Register message for Group(%pPA) is now in SSM, dropping the packet",
562 /* Drop Packet Silently */
567 rp_addr
= pim_addr_from_prefix(&(RP(pim
, sg
.grp
))->rpf_addr
);
568 if (i_am_rp
&& (!pim_addr_cmp(dest_addr
, rp_addr
))) {
569 sentRegisterStop
= 0;
571 if (pim
->register_plist
) {
572 struct prefix_list
*plist
;
575 plist
= prefix_list_lookup(PIM_AFI
,
576 pim
->register_plist
);
578 pim_addr_to_prefix(&src
, sg
.src
);
580 if (prefix_list_apply(plist
, &src
) == PREFIX_DENY
) {
581 pim_register_stop_send(ifp
, &sg
, dest_addr
,
583 if (PIM_DEBUG_PIM_PACKETS
)
585 "%s: Sending register-stop to %pPA for %pSG due to prefix-list denial, dropping packet",
586 __func__
, &src_addr
, &sg
);
592 if (*bits
& PIM_REGISTER_BORDER_BIT
) {
593 pim_addr pimbr
= pim_br_get_pmbr(&sg
);
594 if (PIM_DEBUG_PIM_PACKETS
)
596 "%s: Received Register message with Border bit set",
599 if (pim_addr_is_any(pimbr
))
600 pim_br_set_pmbr(&sg
, src_addr
);
601 else if (pim_addr_cmp(src_addr
, pimbr
)) {
602 pim_register_stop_send(ifp
, &sg
, dest_addr
,
604 if (PIM_DEBUG_PIM_PACKETS
)
606 "%s: Sending register-Stop to %s and dropping mr. packet",
608 /* Drop Packet Silently */
613 struct pim_upstream
*upstream
= pim_upstream_find(pim
, &sg
);
615 * If we don't have a place to send ignore the packet
618 upstream
= pim_upstream_add(
620 PIM_UPSTREAM_FLAG_MASK_SRC_STREAM
, __func__
,
623 zlog_warn("Failure to create upstream state");
627 upstream
->upstream_register
= src_addr
;
630 * If the FHR has set a very very fast register timer
631 * there exists a possibility that the incoming NULL
633 * is happening before we set the spt bit. If so
634 * Do a quick check to update the counters and
635 * then set the spt bit as appropriate
637 if (upstream
->sptbit
!= PIM_UPSTREAM_SPTBIT_TRUE
) {
638 pim_mroute_update_counters(
639 upstream
->channel_oil
);
641 * Have we seen packets?
643 if (upstream
->channel_oil
->cc
.oldpktcnt
644 < upstream
->channel_oil
->cc
.pktcnt
)
645 pim_upstream_set_sptbit(
647 upstream
->rpf
.source_nexthop
652 if ((upstream
->sptbit
== PIM_UPSTREAM_SPTBIT_TRUE
)
653 || ((SwitchToSptDesiredOnRp(pim
, &sg
))
654 && pim_upstream_inherited_olist(pim
, upstream
) == 0)) {
655 pim_register_stop_send(ifp
, &sg
, dest_addr
, src_addr
);
656 sentRegisterStop
= 1;
658 if (PIM_DEBUG_PIM_REG
)
659 zlog_debug("(%s) sptbit: %d", upstream
->sg_str
,
662 if ((upstream
->sptbit
== PIM_UPSTREAM_SPTBIT_TRUE
)
663 || (SwitchToSptDesiredOnRp(pim
, &sg
))) {
664 if (sentRegisterStop
) {
665 pim_upstream_keep_alive_timer_start(
666 upstream
, pim
->rp_keep_alive_time
);
668 pim_upstream_keep_alive_timer_start(
669 upstream
, pim
->keep_alive_time
);
673 if (!(upstream
->sptbit
== PIM_UPSTREAM_SPTBIT_TRUE
)
674 && !(*bits
& PIM_REGISTER_NR_BIT
)) {
675 // decapsulate and forward the iner packet to
676 // inherited_olist(S,G,rpt)
677 // This is taken care of by the kernel for us
679 pim_upstream_msdp_reg_timer_start(upstream
);
681 if (PIM_DEBUG_PIM_REG
) {
683 zlog_debug("Received Register packet for %pSG, Rejecting packet because I am not the RP configured for group",
686 zlog_debug("Received Register packet for %pSG, Rejecting packet because the dst ip address is not the actual RP",
689 pim_register_stop_send(ifp
, &sg
, dest_addr
, src_addr
);
696 * This routine scan all upstream and update register state and remove pimreg
697 * when couldreg becomes false.
699 void pim_reg_del_on_couldreg_fail(struct interface
*ifp
)
701 struct pim_interface
*pim_ifp
= ifp
->info
;
702 struct pim_instance
*pim
;
703 struct pim_upstream
*up
;
710 frr_each (rb_pim_upstream
, &pim
->upstream_head
, up
) {
711 if (ifp
!= up
->rpf
.source_nexthop
.interface
)
714 if (!pim_upstream_could_register(up
)
715 && (up
->reg_state
!= PIM_REG_NOINFO
)) {
716 pim_channel_del_oif(up
->channel_oil
, pim
->regiface
,
717 PIM_OIF_FLAG_PROTO_PIM
, __func__
);
718 THREAD_OFF(up
->t_rs_timer
);
719 up
->reg_state
= PIM_REG_NOINFO
;