]> git.proxmox.com Git - mirror_frr.git/blame - pimd/pim_register.c
pimd: Separate the register and upstream join states on the FHR
[mirror_frr.git] / pimd / pim_register.c
CommitLineData
01d68c9b
DS
1/*
2 * PIM for Quagga
3 * Copyright (C) 2015 Cumulus Networks, Inc.
4 * Donald Sharp
5 *
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.
10 *
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.
15 *
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,
19 * MA 02110-1301 USA
20 */
21
22#include <zebra.h>
23
24#include "log.h"
25#include "if.h"
26#include "thread.h"
13c2408c 27#include "prefix.h"
dfe43e25
DW
28#include "vty.h"
29#include "plist.h"
01d68c9b
DS
30
31#include "pimd.h"
998af219
DS
32#include "pim_mroute.h"
33#include "pim_iface.h"
34#include "pim_msg.h"
35#include "pim_pim.h"
01d68c9b
DS
36#include "pim_str.h"
37#include "pim_rp.h"
38#include "pim_register.h"
80c0d168 39#include "pim_upstream.h"
01d68c9b 40#include "pim_br.h"
2ca35b3d
DS
41#include "pim_rpf.h"
42#include "pim_oil.h"
43#include "pim_zebra.h"
44#include "pim_join.h"
13c2408c 45#include "pim_util.h"
01d68c9b
DS
46
47struct thread *send_test_packet_timer = NULL;
48
8e38a2cf 49void
4ed0af70 50pim_register_stop_send (struct interface *ifp, struct prefix_sg *sg,
4df01a4e 51 struct in_addr src, struct in_addr originator)
01d68c9b 52{
13c2408c 53 struct pim_interface *pinfo;
19e55095 54 unsigned char buffer[10000];
13c2408c
DS
55 unsigned int b1length = 0;
56 unsigned int length;
57 uint8_t *b1;
58 struct prefix p;
59
9add3b88
DS
60 if (PIM_DEBUG_PIM_REG)
61 {
62 zlog_debug ("Sending Register stop for %s to %s on %s",
63 pim_str_sg_dump (sg), inet_ntoa(originator), ifp->name);
64 }
65
19e55095 66 memset (buffer, 0, 10000);
13c2408c
DS
67 b1 = (uint8_t *)buffer + PIM_MSG_REGISTER_STOP_LEN;
68
4ed0af70 69 length = pim_encode_addr_group (b1, AFI_IP, 0, 0, sg->grp);
13c2408c
DS
70 b1length += length;
71 b1 += length;
72
73 p.family = AF_INET;
4ed0af70 74 p.u.prefix4 = sg->src;
13c2408c
DS
75 p.prefixlen = 32;
76 length = pim_encode_addr_ucast (b1, &p);
77 b1length += length;
78
79 pim_msg_build_header (buffer, b1length + PIM_MSG_REGISTER_STOP_LEN, PIM_MSG_TYPE_REG_STOP);
80
81 pinfo = (struct pim_interface *)ifp->info;
82 if (!pinfo)
83 {
84 if (PIM_DEBUG_PIM_TRACE)
85 zlog_debug ("%s: No pinfo!\n", __PRETTY_FUNCTION__);
86 return;
87 }
4df01a4e 88 if (pim_msg_send (pinfo->pim_sock_fd, src, originator,
13c2408c
DS
89 buffer, b1length + PIM_MSG_REGISTER_STOP_LEN,
90 ifp->name))
91 {
92 if (PIM_DEBUG_PIM_TRACE)
93 {
94 zlog_debug ("%s: could not send PIM register stop message on interface %s",
95 __PRETTY_FUNCTION__, ifp->name);
96 }
97 }
01d68c9b
DS
98}
99
4dffc880 100int
56638739 101pim_register_stop_recv (uint8_t *buf, int buf_size)
4dffc880 102{
56638739
DS
103 struct pim_upstream *upstream = NULL;
104 struct prefix source;
4ed0af70 105 struct prefix_sg sg;
56638739
DS
106 int l;
107
53e39e14 108 memset (&sg, 0, sizeof (struct prefix_sg));
bce0e645 109 l = pim_parse_addr_group (&sg, buf, buf_size);
56638739
DS
110 buf += l;
111 buf_size -= l;
a878ba45 112 pim_parse_addr_ucast (&source, buf, buf_size);
4ed0af70 113 sg.src = source.u.prefix4;
9add3b88 114
5074a423 115 upstream = pim_upstream_find (&sg);
56638739
DS
116 if (!upstream)
117 {
118 return 0;
119 }
120
8bfb8b67
DS
121 if (PIM_DEBUG_PIM_REG)
122 zlog_debug ("Received Register stop for %s",
123 upstream->sg_str);
124
e0e127b0 125 switch (upstream->reg_state)
56638739
DS
126 {
127 case PIM_UPSTREAM_NOTJOINED:
128 case PIM_UPSTREAM_PRUNE:
129 return 0;
130 break;
131 case PIM_UPSTREAM_JOINED:
e0e127b0 132 upstream->reg_state = PIM_UPSTREAM_PRUNE;
bb027ee8
DS
133 pim_channel_del_oif (upstream->channel_oil, pim_regiface, PIM_OIF_FLAG_PROTO_PIM);
134 pim_upstream_start_register_stop_timer (upstream, 0);
35661ffa 135 break;
56638739 136 case PIM_UPSTREAM_JOIN_PENDING:
e0e127b0 137 upstream->reg_state = PIM_UPSTREAM_PRUNE;
56638739 138 pim_upstream_start_register_stop_timer (upstream, 0);
56638739
DS
139 return 0;
140 break;
141 }
142
4dffc880
DS
143 return 0;
144}
145
998af219 146void
0e3b3d5d 147pim_register_send (const uint8_t *buf, int buf_size, struct in_addr src, struct pim_rpf *rpg, int null_register, struct pim_upstream *up)
998af219 148{
19e55095 149 unsigned char buffer[10000];
998af219
DS
150 unsigned char *b1;
151 struct pim_interface *pinfo;
152 struct interface *ifp;
998af219 153
9add3b88
DS
154 if (PIM_DEBUG_PIM_REG)
155 {
0e3b3d5d 156 zlog_debug ("Sending %s %sRegister Packet to %s",
35661ffa
DS
157 up->sg_str, null_register ? "NULL " : "",
158 inet_ntoa (rpg->rpf_addr.u.prefix4));
9add3b88
DS
159 }
160
998af219 161 ifp = rpg->source_nexthop.interface;
63b8f7a3
DS
162 if (!ifp)
163 {
164 if (PIM_DEBUG_PIM_REG)
165 zlog_debug ("%s: No interface to transmit register on", __PRETTY_FUNCTION__);
166 return;
167 }
998af219
DS
168 pinfo = (struct pim_interface *)ifp->info;
169 if (!pinfo) {
63b8f7a3
DS
170 if (PIM_DEBUG_PIM_REG)
171 zlog_debug("%s: Interface: %s not configured for pim to trasmit on!\n", __PRETTY_FUNCTION__, ifp->name);
998af219
DS
172 return;
173 }
174
19e55095 175 memset(buffer, 0, 10000);
b100a4f5 176 b1 = buffer + PIM_MSG_HEADER_LEN;
dc686f82 177 *b1 |= null_register << 6;
998af219
DS
178 b1 = buffer + PIM_MSG_REGISTER_LEN;
179
b100a4f5 180 memcpy(b1, (const unsigned char *)buf, buf_size);
998af219 181
b100a4f5 182 pim_msg_build_header(buffer, buf_size + PIM_MSG_REGISTER_LEN, PIM_MSG_TYPE_REGISTER);
998af219
DS
183
184 if (pim_msg_send(pinfo->pim_sock_fd,
4df01a4e 185 src,
63c59d0c 186 rpg->rpf_addr.u.prefix4,
998af219 187 buffer,
b100a4f5 188 buf_size + PIM_MSG_REGISTER_LEN,
998af219
DS
189 ifp->name)) {
190 if (PIM_DEBUG_PIM_TRACE) {
191 zlog_debug("%s: could not send PIM register message on interface %s",
192 __PRETTY_FUNCTION__, ifp->name);
193 }
194 return;
195 }
196}
197
01d68c9b
DS
198/*
199 * 4.4.2 Receiving Register Messages at the RP
200 *
201 * When an RP receives a Register message, the course of action is
202 * decided according to the following pseudocode:
203 *
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
208 * }
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.
217 * }
218 * }
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;
224 * }
225 * if ( SPTbit(S,G) OR SwitchToSptDesired(S,G) ) {
226 * if ( sentRegisterStop == TRUE ) {
227 * set KeepaliveTimer(S,G) to RP_Keepalive_Period;
228 * } else {
229 * set KeepaliveTimer(S,G) to Keepalive_Period;
230 * }
231 * }
232 * if( !SPTbit(S,G) AND ! pkt.NullRegisterBit ) {
233 * decapsulate and forward the inner packet to
234 * inherited_olist(S,G,rpt) # Note (+)
235 * }
236 * } else {
237 * send Register-Stop(S,G) to outer.src
238 * # Note (*)
239 * }
240 * }
241 */
242int
243pim_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)
247{
77e390e5
DS
248 int sentRegisterStop = 0;
249 struct ip *ip_hdr;
4ed0af70 250 struct prefix_sg sg;
77e390e5 251 uint32_t *bits;
a6dc7de6 252 int i_am_rp = 0;
01d68c9b 253
75a26779
DS
254#define PIM_MSG_REGISTER_BIT_RESERVED_LEN 4
255 ip_hdr = (struct ip *)(tlv_buf + PIM_MSG_REGISTER_BIT_RESERVED_LEN);
256
257 if (!pim_rp_check_is_my_ip_address (ip_hdr->ip_dst, dest_addr)) {
d41b415c 258 if (PIM_DEBUG_PIM_REG) {
eaa54bdb 259 char dest[INET_ADDRSTRLEN];
01d68c9b
DS
260
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__,
263 dest);
264 }
265 return 0;
266 }
267
77e390e5
DS
268 /*
269 * Please note this is not drawn to get the correct bit/data size
270 *
271 * The entirety of the REGISTER packet looks like this:
272 * -------------------------------------------------------------
273 * | Ver | Type | Reserved | Checksum |
274 * |-----------------------------------------------------------|
275 * |B|N| Reserved 2 |
276 * |-----------------------------------------------------------|
277 * | Encap | IP HDR |
278 * | Mcast | |
279 * | Packet |--------------------------------------------------|
280 * | | Mcast Data |
281 * | | |
282 * ...
283 *
284 * tlv_buf when received from the caller points at the B bit
285 * We need to know the inner source and dest
286 */
287 bits = (uint32_t *)tlv_buf;
83d8ff00
DS
288
289 /*
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.
293 */
4ed0af70
DS
294 memset (&sg, 0, sizeof (struct prefix_sg));
295 sg.src = ip_hdr->ip_src;
296 sg.grp = ip_hdr->ip_dst;
77e390e5 297
a6dc7de6 298 i_am_rp = I_am_RP (sg.grp);
5d84a3bc
DS
299
300 if (PIM_DEBUG_PIM_REG)
301 {
302 char src_str[INET_ADDRSTRLEN];
303
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);
307 }
308
a6dc7de6 309 if (i_am_rp && (dest_addr.s_addr == ((RP (sg.grp))->rpf_addr.u.prefix4.s_addr))) {
77e390e5 310 sentRegisterStop = 0;
01d68c9b 311
9eda95bd 312 if (*bits & PIM_REGISTER_BORDER_BIT) {
65e1fcd7 313 struct in_addr pimbr = pim_br_get_pmbr (&sg);
01d68c9b
DS
314 if (PIM_DEBUG_PIM_PACKETS)
315 zlog_debug("%s: Received Register message with Border bit set", __func__);
316
317 if (pimbr.s_addr == pim_br_unknown.s_addr)
65e1fcd7 318 pim_br_set_pmbr(&sg, src_addr);
80c0d168 319 else if (src_addr.s_addr != pimbr.s_addr) {
4df01a4e 320 pim_register_stop_send (ifp, &sg, dest_addr, src_addr);
01d68c9b
DS
321 if (PIM_DEBUG_PIM_PACKETS)
322 zlog_debug("%s: Sending register-Stop to %s and dropping mr. packet",
323 __func__, "Sender");
77e390e5
DS
324 /* Drop Packet Silently */
325 return 1;
01d68c9b
DS
326 }
327 }
77e390e5 328
5074a423 329 struct pim_upstream *upstream = pim_upstream_find (&sg);
77e390e5
DS
330 /*
331 * If we don't have a place to send ignore the packet
332 */
333 if (!upstream)
9fd309db 334 {
e5905a3b
DS
335 upstream = pim_upstream_add (&sg, ifp,
336 PIM_UPSTREAM_FLAG_MASK_SRC_STREAM,
337 __PRETTY_FUNCTION__);
4e85d209
DS
338 if (!upstream)
339 {
63b8f7a3 340 zlog_warn ("Failure to create upstream state");
4e85d209
DS
341 return 1;
342 }
1bf16443 343 PIM_UPSTREAM_FLAG_SET_SRC_STREAM(upstream->flags);
5ce79466 344
8e38a2cf 345 upstream->upstream_register = src_addr;
36d6bd7d 346 pim_rp_set_upstream_addr (&upstream->upstream_addr, sg.src, sg.grp);
e5009aed
DS
347 if (pim_nexthop_lookup (&upstream->rpf.source_nexthop,
348 upstream->upstream_addr, 1) != 0)
349 {
350 if (PIM_DEBUG_PIM_REG)
351 {
8bfb8b67 352 zlog_debug ("Received Register(%s), for which I have no path back", upstream->sg_str);
e5009aed 353 }
1bf16443 354 PIM_UPSTREAM_FLAG_UNSET_SRC_STREAM(upstream->flags);
e5009aed
DS
355 pim_upstream_del (upstream, __PRETTY_FUNCTION__);
356 return 1;
357 }
4ed0af70 358 upstream->sg.src = sg.src;
216bb84f
DS
359 upstream->rpf.rpf_addr = upstream->rpf.source_nexthop.mrib_nexthop_addr;
360
8e38a2cf 361 upstream->join_state = PIM_UPSTREAM_PRUNE;
219e0013 362
9fd309db
DS
363 }
364
77e390e5 365 if ((upstream->sptbit == PIM_UPSTREAM_SPTBIT_TRUE) ||
ad410c6c 366 ((SwitchToSptDesired(&sg)) &&
4fdc8f36 367 pim_upstream_inherited_olist (upstream) == 0)) {
216bb84f 368 //pim_scan_individual_oil (upstream->channel_oil);
4df01a4e 369 pim_register_stop_send (ifp, &sg, dest_addr, src_addr);
77e390e5 370 sentRegisterStop = 1;
5d84a3bc
DS
371 } else {
372 if (PIM_DEBUG_PIM_REG)
8bfb8b67 373 zlog_debug ("(%s) sptbit: %d", upstream->sg_str, upstream->sptbit);
77e390e5 374 }
77e390e5 375 if ((upstream->sptbit == PIM_UPSTREAM_SPTBIT_TRUE) ||
ad410c6c 376 (SwitchToSptDesired(&sg))) {
80c0d168 377 if (sentRegisterStop) {
01408ede 378 pim_upstream_keep_alive_timer_start (upstream, qpim_rp_keep_alive_time);
77e390e5 379 } else {
4304f95c 380 pim_upstream_keep_alive_timer_start (upstream, qpim_keep_alive_time);
77e390e5
DS
381 }
382 }
383
384 if (!(upstream->sptbit == PIM_UPSTREAM_SPTBIT_TRUE) &&
2ca35b3d
DS
385 !(*bits & PIM_REGISTER_NR_BIT))
386 {
2ca35b3d
DS
387 //decapsulate and forward the iner packet to
388 //inherited_olist(S,G,rpt)
0365f56b 389 // This is taken care of by the kernel for us
2ca35b3d 390 }
1bf16443 391 pim_upstream_msdp_reg_timer_start(upstream);
77e390e5 392 } else {
a6dc7de6
DS
393 if (PIM_DEBUG_PIM_REG)
394 {
395 if (!i_am_rp)
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));
398 else
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));
401 }
4df01a4e 402 pim_register_stop_send (ifp, &sg, dest_addr, src_addr);
77e390e5 403 }
01d68c9b
DS
404
405 return 1;
406}