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
17 * along with this program; see the file COPYING; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
32 #include "pim_mroute.h"
33 #include "pim_iface.h"
38 #include "pim_register.h"
39 #include "pim_upstream.h"
43 #include "pim_zebra.h"
47 struct thread
*send_test_packet_timer
= NULL
;
50 pim_register_stop_send (struct interface
*ifp
, struct prefix_sg
*sg
,
51 struct in_addr src
, struct in_addr originator
)
53 struct pim_interface
*pinfo
;
54 unsigned char buffer
[10000];
55 unsigned int b1length
= 0;
60 if (PIM_DEBUG_PIM_REG
)
62 zlog_debug ("Sending Register stop for %s to %s on %s",
63 pim_str_sg_dump (sg
), inet_ntoa(originator
), ifp
->name
);
66 memset (buffer
, 0, 10000);
67 b1
= (uint8_t *)buffer
+ PIM_MSG_REGISTER_STOP_LEN
;
69 length
= pim_encode_addr_group (b1
, AFI_IP
, 0, 0, sg
->grp
);
74 p
.u
.prefix4
= sg
->src
;
76 length
= pim_encode_addr_ucast (b1
, &p
);
79 pim_msg_build_header (buffer
, b1length
+ PIM_MSG_REGISTER_STOP_LEN
, PIM_MSG_TYPE_REG_STOP
);
81 pinfo
= (struct pim_interface
*)ifp
->info
;
84 if (PIM_DEBUG_PIM_TRACE
)
85 zlog_debug ("%s: No pinfo!\n", __PRETTY_FUNCTION__
);
88 if (pim_msg_send (pinfo
->pim_sock_fd
, src
, originator
,
89 buffer
, b1length
+ PIM_MSG_REGISTER_STOP_LEN
,
92 if (PIM_DEBUG_PIM_TRACE
)
94 zlog_debug ("%s: could not send PIM register stop message on interface %s",
95 __PRETTY_FUNCTION__
, ifp
->name
);
101 pim_register_stop_recv (uint8_t *buf
, int buf_size
)
103 struct pim_upstream
*upstream
= NULL
;
104 struct prefix source
;
108 memset (&sg
, 0, sizeof (struct prefix_sg
));
109 l
= pim_parse_addr_group (&sg
, buf
, buf_size
);
112 pim_parse_addr_ucast (&source
, buf
, buf_size
);
113 sg
.src
= source
.u
.prefix4
;
115 upstream
= pim_upstream_find (&sg
);
121 if (PIM_DEBUG_PIM_REG
)
122 zlog_debug ("Received Register stop for %s",
125 switch (upstream
->join_state
)
127 case PIM_UPSTREAM_NOTJOINED
:
128 case PIM_UPSTREAM_PRUNE
:
131 case PIM_UPSTREAM_JOINED
:
132 upstream
->join_state
= PIM_UPSTREAM_PRUNE
;
133 pim_channel_del_oif (upstream
->channel_oil
, pim_regiface
, PIM_OIF_FLAG_PROTO_PIM
);
134 pim_upstream_start_register_stop_timer (upstream
, 0);
135 case PIM_UPSTREAM_JOIN_PENDING
:
136 upstream
->join_state
= PIM_UPSTREAM_PRUNE
;
137 pim_upstream_start_register_stop_timer (upstream
, 0);
146 pim_register_send (const uint8_t *buf
, int buf_size
, struct in_addr src
, struct pim_rpf
*rpg
, int null_register
, struct pim_upstream
*up
)
148 unsigned char buffer
[10000];
150 struct pim_interface
*pinfo
;
151 struct interface
*ifp
;
153 if (PIM_DEBUG_PIM_REG
)
155 char rp_str
[INET_ADDRSTRLEN
];
156 strcpy (rp_str
, inet_ntoa (rpg
->rpf_addr
.u
.prefix4
));
157 zlog_debug ("Sending %s %sRegister Packet to %s",
158 up
->sg_str
, null_register
? "NULL " : "", rp_str
);
161 ifp
= rpg
->source_nexthop
.interface
;
164 if (PIM_DEBUG_PIM_REG
)
165 zlog_debug ("%s: No interface to transmit register on", __PRETTY_FUNCTION__
);
168 pinfo
= (struct pim_interface
*)ifp
->info
;
170 if (PIM_DEBUG_PIM_REG
)
171 zlog_debug("%s: Interface: %s not configured for pim to trasmit on!\n", __PRETTY_FUNCTION__
, ifp
->name
);
175 memset(buffer
, 0, 10000);
176 b1
= buffer
+ PIM_MSG_HEADER_LEN
;
177 *b1
|= null_register
<< 6;
178 b1
= buffer
+ PIM_MSG_REGISTER_LEN
;
180 memcpy(b1
, (const unsigned char *)buf
, buf_size
);
182 pim_msg_build_header(buffer
, buf_size
+ PIM_MSG_REGISTER_LEN
, PIM_MSG_TYPE_REGISTER
);
184 if (pim_msg_send(pinfo
->pim_sock_fd
,
186 rpg
->rpf_addr
.u
.prefix4
,
188 buf_size
+ PIM_MSG_REGISTER_LEN
,
190 if (PIM_DEBUG_PIM_TRACE
) {
191 zlog_debug("%s: could not send PIM register message on interface %s",
192 __PRETTY_FUNCTION__
, ifp
->name
);
199 * 4.4.2 Receiving Register Messages at the RP
201 * When an RP receives a Register message, the course of action is
202 * decided according to the following pseudocode:
204 * packet_arrives_on_rp_tunnel( pkt ) {
205 * if( outer.dst is not one of my addresses ) {
206 * drop the packet silently.
207 * # Note: this may be a spoofing attempt
209 * if( I_am_RP(G) AND outer.dst == RP(G) ) {
210 * sentRegisterStop = FALSE;
211 * if ( register.borderbit == TRUE ) {
212 * if ( PMBR(S,G) == unknown ) {
213 * PMBR(S,G) = outer.src
214 * } else if ( outer.src != PMBR(S,G) ) {
215 * send Register-Stop(S,G) to outer.src
216 * drop the packet silently.
219 * if ( SPTbit(S,G) OR
220 * ( SwitchToSptDesired(S,G) AND
221 * ( inherited_olist(S,G) == NULL ))) {
222 * send Register-Stop(S,G) to outer.src
223 * sentRegisterStop = TRUE;
225 * if ( SPTbit(S,G) OR SwitchToSptDesired(S,G) ) {
226 * if ( sentRegisterStop == TRUE ) {
227 * set KeepaliveTimer(S,G) to RP_Keepalive_Period;
229 * set KeepaliveTimer(S,G) to Keepalive_Period;
232 * if( !SPTbit(S,G) AND ! pkt.NullRegisterBit ) {
233 * decapsulate and forward the inner packet to
234 * inherited_olist(S,G,rpt) # Note (+)
237 * send Register-Stop(S,G) to outer.src
243 pim_register_recv (struct interface
*ifp
,
244 struct in_addr dest_addr
,
245 struct in_addr src_addr
,
246 uint8_t *tlv_buf
, int tlv_buf_size
)
248 int sentRegisterStop
= 0;
254 #define PIM_MSG_REGISTER_BIT_RESERVED_LEN 4
255 ip_hdr
= (struct ip
*)(tlv_buf
+ PIM_MSG_REGISTER_BIT_RESERVED_LEN
);
257 if (!pim_rp_check_is_my_ip_address (ip_hdr
->ip_dst
, dest_addr
)) {
258 if (PIM_DEBUG_PIM_REG
) {
259 char dest
[INET_ADDRSTRLEN
];
261 pim_inet4_dump ("<dst?>", dest_addr
, dest
, sizeof(dest
));
262 zlog_debug ("%s: Received Register message for %s that I do not own", __func__
,
269 * Please note this is not drawn to get the correct bit/data size
271 * The entirety of the REGISTER packet looks like this:
272 * -------------------------------------------------------------
273 * | Ver | Type | Reserved | Checksum |
274 * |-----------------------------------------------------------|
276 * |-----------------------------------------------------------|
279 * | Packet |--------------------------------------------------|
284 * tlv_buf when received from the caller points at the B bit
285 * We need to know the inner source and dest
287 bits
= (uint32_t *)tlv_buf
;
290 * tlv_buf points to the start of the |B|N|... Reserved
291 * Line above. So we need to add 4 bytes to get to the
292 * start of the actual Encapsulated data.
294 memset (&sg
, 0, sizeof (struct prefix_sg
));
295 sg
.src
= ip_hdr
->ip_src
;
296 sg
.grp
= ip_hdr
->ip_dst
;
298 i_am_rp
= I_am_RP (sg
.grp
);
300 if (PIM_DEBUG_PIM_REG
)
302 char src_str
[INET_ADDRSTRLEN
];
304 pim_inet4_dump ("<src?>", src_addr
, src_str
, sizeof (src_str
));
305 zlog_debug ("Received Register message(%s) from %s on %s, rp: %d",
306 pim_str_sg_dump (&sg
), src_str
, ifp
->name
, i_am_rp
);
309 if (i_am_rp
&& (dest_addr
.s_addr
== ((RP (sg
.grp
))->rpf_addr
.u
.prefix4
.s_addr
))) {
310 sentRegisterStop
= 0;
312 if (*bits
& PIM_REGISTER_BORDER_BIT
) {
313 struct in_addr pimbr
= pim_br_get_pmbr (&sg
);
314 if (PIM_DEBUG_PIM_PACKETS
)
315 zlog_debug("%s: Received Register message with Border bit set", __func__
);
317 if (pimbr
.s_addr
== pim_br_unknown
.s_addr
)
318 pim_br_set_pmbr(&sg
, src_addr
);
319 else if (src_addr
.s_addr
!= pimbr
.s_addr
) {
320 pim_register_stop_send (ifp
, &sg
, dest_addr
, src_addr
);
321 if (PIM_DEBUG_PIM_PACKETS
)
322 zlog_debug("%s: Sending register-Stop to %s and dropping mr. packet",
324 /* Drop Packet Silently */
329 struct pim_upstream
*upstream
= pim_upstream_find (&sg
);
331 * If we don't have a place to send ignore the packet
335 upstream
= pim_upstream_add (&sg
, ifp
,
336 PIM_UPSTREAM_FLAG_MASK_SRC_STREAM
,
337 __PRETTY_FUNCTION__
);
340 zlog_warn ("Failure to create upstream state");
343 PIM_UPSTREAM_FLAG_SET_SRC_STREAM(upstream
->flags
);
345 upstream
->upstream_register
= src_addr
;
346 pim_rp_set_upstream_addr (&upstream
->upstream_addr
, sg
.src
, sg
.grp
);
347 if (pim_nexthop_lookup (&upstream
->rpf
.source_nexthop
,
348 upstream
->upstream_addr
, 1) != 0)
350 if (PIM_DEBUG_PIM_REG
)
352 zlog_debug ("Received Register(%s), for which I have no path back", upstream
->sg_str
);
354 PIM_UPSTREAM_FLAG_UNSET_SRC_STREAM(upstream
->flags
);
355 pim_upstream_del (upstream
, __PRETTY_FUNCTION__
);
358 upstream
->sg
.src
= sg
.src
;
359 upstream
->rpf
.rpf_addr
= upstream
->rpf
.source_nexthop
.mrib_nexthop_addr
;
361 upstream
->join_state
= PIM_UPSTREAM_PRUNE
;
365 if ((upstream
->sptbit
== PIM_UPSTREAM_SPTBIT_TRUE
) ||
366 ((SwitchToSptDesired(&sg
)) &&
367 pim_upstream_inherited_olist (upstream
) == 0)) {
368 //pim_scan_individual_oil (upstream->channel_oil);
369 pim_register_stop_send (ifp
, &sg
, dest_addr
, src_addr
);
370 sentRegisterStop
= 1;
372 if (PIM_DEBUG_PIM_REG
)
373 zlog_debug ("(%s) sptbit: %d", upstream
->sg_str
, upstream
->sptbit
);
375 if ((upstream
->sptbit
== PIM_UPSTREAM_SPTBIT_TRUE
) ||
376 (SwitchToSptDesired(&sg
))) {
377 if (sentRegisterStop
) {
378 pim_upstream_keep_alive_timer_start (upstream
, qpim_rp_keep_alive_time
);
380 pim_upstream_keep_alive_timer_start (upstream
, qpim_keep_alive_time
);
384 if (!(upstream
->sptbit
== PIM_UPSTREAM_SPTBIT_TRUE
) &&
385 !(*bits
& PIM_REGISTER_NR_BIT
))
387 //decapsulate and forward the iner packet to
388 //inherited_olist(S,G,rpt)
389 // This is taken care of by the kernel for us
391 pim_upstream_msdp_reg_timer_start(upstream
);
393 if (PIM_DEBUG_PIM_REG
)
396 zlog_debug ("Received Register packet for %s, Rejecting packet because I am not the RP configured for group",
397 pim_str_sg_dump (&sg
));
399 zlog_debug ("Received Register packet for %s, Rejecting packet because the dst ip address is not the actual RP",
400 pim_str_sg_dump (&sg
));
402 pim_register_stop_send (ifp
, &sg
, dest_addr
, src_addr
);