]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_register.c
Merge remote-tracking branch 'origin/stable/3.0'
[mirror_frr.git] / pimd / pim_register.c
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"
27 #include "prefix.h"
28 #include "vty.h"
29 #include "plist.h"
30
31 #include "pimd.h"
32 #include "pim_mroute.h"
33 #include "pim_iface.h"
34 #include "pim_msg.h"
35 #include "pim_pim.h"
36 #include "pim_str.h"
37 #include "pim_rp.h"
38 #include "pim_register.h"
39 #include "pim_upstream.h"
40 #include "pim_br.h"
41 #include "pim_rpf.h"
42 #include "pim_oil.h"
43 #include "pim_zebra.h"
44 #include "pim_join.h"
45 #include "pim_util.h"
46 #include "pim_ssm.h"
47
48 struct thread *send_test_packet_timer = NULL;
49
50 void
51 pim_register_join (struct pim_upstream *up)
52 {
53 if (pim_is_grp_ssm (up->sg.grp))
54 {
55 if (PIM_DEBUG_PIM_EVENTS)
56 zlog_debug ("%s register setup skipped as group is SSM", up->sg_str);
57 return;
58 }
59
60 pim_channel_add_oif (up->channel_oil, pim_regiface, PIM_OIF_FLAG_PROTO_PIM);
61 up->reg_state = PIM_REG_JOIN;
62 }
63
64 void
65 pim_register_stop_send (struct interface *ifp, struct prefix_sg *sg,
66 struct in_addr src, struct in_addr originator)
67 {
68 struct pim_interface *pinfo;
69 unsigned char buffer[10000];
70 unsigned int b1length = 0;
71 unsigned int length;
72 uint8_t *b1;
73 struct prefix p;
74
75 if (PIM_DEBUG_PIM_REG)
76 {
77 zlog_debug ("Sending Register stop for %s to %s on %s",
78 pim_str_sg_dump (sg), inet_ntoa(originator), ifp->name);
79 }
80
81 memset (buffer, 0, 10000);
82 b1 = (uint8_t *)buffer + PIM_MSG_REGISTER_STOP_LEN;
83
84 length = pim_encode_addr_group (b1, AFI_IP, 0, 0, sg->grp);
85 b1length += length;
86 b1 += length;
87
88 p.family = AF_INET;
89 p.u.prefix4 = sg->src;
90 p.prefixlen = 32;
91 length = pim_encode_addr_ucast (b1, &p);
92 b1length += length;
93
94 pim_msg_build_header (buffer, b1length + PIM_MSG_REGISTER_STOP_LEN, PIM_MSG_TYPE_REG_STOP);
95
96 pinfo = (struct pim_interface *)ifp->info;
97 if (!pinfo)
98 {
99 if (PIM_DEBUG_PIM_TRACE)
100 zlog_debug ("%s: No pinfo!\n", __PRETTY_FUNCTION__);
101 return;
102 }
103 if (pim_msg_send (pinfo->pim_sock_fd, src, originator,
104 buffer, b1length + PIM_MSG_REGISTER_STOP_LEN,
105 ifp->name))
106 {
107 if (PIM_DEBUG_PIM_TRACE)
108 {
109 zlog_debug ("%s: could not send PIM register stop message on interface %s",
110 __PRETTY_FUNCTION__, ifp->name);
111 }
112 }
113 }
114
115 int
116 pim_register_stop_recv (uint8_t *buf, int buf_size)
117 {
118 struct pim_upstream *upstream = NULL;
119 struct prefix source;
120 struct prefix_sg sg;
121 int l;
122
123 memset (&sg, 0, sizeof (struct prefix_sg));
124 l = pim_parse_addr_group (&sg, buf, buf_size);
125 buf += l;
126 buf_size -= l;
127 pim_parse_addr_ucast (&source, buf, buf_size);
128 sg.src = source.u.prefix4;
129
130 upstream = pim_upstream_find (&sg);
131 if (!upstream)
132 {
133 return 0;
134 }
135
136 if (PIM_DEBUG_PIM_REG)
137 zlog_debug ("Received Register stop for %s",
138 upstream->sg_str);
139
140 switch (upstream->reg_state)
141 {
142 case PIM_REG_NOINFO:
143 case PIM_REG_PRUNE:
144 return 0;
145 break;
146 case PIM_REG_JOIN:
147 upstream->reg_state = PIM_REG_PRUNE;
148 pim_channel_del_oif (upstream->channel_oil, pim_regiface, PIM_OIF_FLAG_PROTO_PIM);
149 pim_upstream_start_register_stop_timer (upstream, 0);
150 break;
151 case PIM_REG_JOIN_PENDING:
152 upstream->reg_state = PIM_REG_PRUNE;
153 pim_upstream_start_register_stop_timer (upstream, 0);
154 return 0;
155 break;
156 }
157
158 return 0;
159 }
160
161 void
162 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)
163 {
164 unsigned char buffer[10000];
165 unsigned char *b1;
166 struct pim_interface *pinfo;
167 struct interface *ifp;
168
169 if (PIM_DEBUG_PIM_REG)
170 {
171 zlog_debug ("Sending %s %sRegister Packet to %s",
172 up->sg_str, null_register ? "NULL " : "",
173 inet_ntoa (rpg->rpf_addr.u.prefix4));
174 }
175
176 ifp = rpg->source_nexthop.interface;
177 if (!ifp)
178 {
179 if (PIM_DEBUG_PIM_REG)
180 zlog_debug ("%s: No interface to transmit register on", __PRETTY_FUNCTION__);
181 return;
182 }
183 pinfo = (struct pim_interface *)ifp->info;
184 if (!pinfo) {
185 if (PIM_DEBUG_PIM_REG)
186 zlog_debug("%s: Interface: %s not configured for pim to trasmit on!\n", __PRETTY_FUNCTION__, ifp->name);
187 return;
188 }
189
190 if (PIM_DEBUG_PIM_REG)
191 {
192 char rp_str[INET_ADDRSTRLEN];
193 strncpy (rp_str, inet_ntoa (rpg->rpf_addr.u.prefix4), INET_ADDRSTRLEN-1);
194 zlog_debug ("%s: Sending %s %sRegister Packet to %s on %s",
195 __PRETTY_FUNCTION__, up->sg_str,
196 null_register ? "NULL " : "", rp_str, ifp->name);
197 }
198
199 memset(buffer, 0, 10000);
200 b1 = buffer + PIM_MSG_HEADER_LEN;
201 *b1 |= null_register << 6;
202 b1 = buffer + PIM_MSG_REGISTER_LEN;
203
204 memcpy(b1, (const unsigned char *)buf, buf_size);
205
206 pim_msg_build_header(buffer, buf_size + PIM_MSG_REGISTER_LEN, PIM_MSG_TYPE_REGISTER);
207
208 if (pim_msg_send(pinfo->pim_sock_fd,
209 src,
210 rpg->rpf_addr.u.prefix4,
211 buffer,
212 buf_size + PIM_MSG_REGISTER_LEN,
213 ifp->name)) {
214 if (PIM_DEBUG_PIM_TRACE) {
215 zlog_debug("%s: could not send PIM register message on interface %s",
216 __PRETTY_FUNCTION__, ifp->name);
217 }
218 return;
219 }
220 }
221
222 /*
223 * 4.4.2 Receiving Register Messages at the RP
224 *
225 * When an RP receives a Register message, the course of action is
226 * decided according to the following pseudocode:
227 *
228 * packet_arrives_on_rp_tunnel( pkt ) {
229 * if( outer.dst is not one of my addresses ) {
230 * drop the packet silently.
231 * # Note: this may be a spoofing attempt
232 * }
233 * if( I_am_RP(G) AND outer.dst == RP(G) ) {
234 * sentRegisterStop = FALSE;
235 * if ( register.borderbit == TRUE ) {
236 * if ( PMBR(S,G) == unknown ) {
237 * PMBR(S,G) = outer.src
238 * } else if ( outer.src != PMBR(S,G) ) {
239 * send Register-Stop(S,G) to outer.src
240 * drop the packet silently.
241 * }
242 * }
243 * if ( SPTbit(S,G) OR
244 * ( SwitchToSptDesired(S,G) AND
245 * ( inherited_olist(S,G) == NULL ))) {
246 * send Register-Stop(S,G) to outer.src
247 * sentRegisterStop = TRUE;
248 * }
249 * if ( SPTbit(S,G) OR SwitchToSptDesired(S,G) ) {
250 * if ( sentRegisterStop == TRUE ) {
251 * set KeepaliveTimer(S,G) to RP_Keepalive_Period;
252 * } else {
253 * set KeepaliveTimer(S,G) to Keepalive_Period;
254 * }
255 * }
256 * if( !SPTbit(S,G) AND ! pkt.NullRegisterBit ) {
257 * decapsulate and forward the inner packet to
258 * inherited_olist(S,G,rpt) # Note (+)
259 * }
260 * } else {
261 * send Register-Stop(S,G) to outer.src
262 * # Note (*)
263 * }
264 * }
265 */
266 int
267 pim_register_recv (struct interface *ifp,
268 struct in_addr dest_addr,
269 struct in_addr src_addr,
270 uint8_t *tlv_buf, int tlv_buf_size)
271 {
272 int sentRegisterStop = 0;
273 struct ip *ip_hdr;
274 struct prefix_sg sg;
275 uint32_t *bits;
276 int i_am_rp = 0;
277
278 #define PIM_MSG_REGISTER_BIT_RESERVED_LEN 4
279 ip_hdr = (struct ip *)(tlv_buf + PIM_MSG_REGISTER_BIT_RESERVED_LEN);
280
281 if (!pim_rp_check_is_my_ip_address (ip_hdr->ip_dst, dest_addr)) {
282 if (PIM_DEBUG_PIM_REG) {
283 char dest[INET_ADDRSTRLEN];
284
285 pim_inet4_dump ("<dst?>", dest_addr, dest, sizeof(dest));
286 zlog_debug ("%s: Received Register message for %s that I do not own", __func__,
287 dest);
288 }
289 return 0;
290 }
291
292 /*
293 * Please note this is not drawn to get the correct bit/data size
294 *
295 * The entirety of the REGISTER packet looks like this:
296 * -------------------------------------------------------------
297 * | Ver | Type | Reserved | Checksum |
298 * |-----------------------------------------------------------|
299 * |B|N| Reserved 2 |
300 * |-----------------------------------------------------------|
301 * | Encap | IP HDR |
302 * | Mcast | |
303 * | Packet |--------------------------------------------------|
304 * | | Mcast Data |
305 * | | |
306 * ...
307 *
308 * tlv_buf when received from the caller points at the B bit
309 * We need to know the inner source and dest
310 */
311 bits = (uint32_t *)tlv_buf;
312
313 /*
314 * tlv_buf points to the start of the |B|N|... Reserved
315 * Line above. So we need to add 4 bytes to get to the
316 * start of the actual Encapsulated data.
317 */
318 memset (&sg, 0, sizeof (struct prefix_sg));
319 sg.src = ip_hdr->ip_src;
320 sg.grp = ip_hdr->ip_dst;
321
322 i_am_rp = I_am_RP (sg.grp);
323
324 if (PIM_DEBUG_PIM_REG)
325 {
326 char src_str[INET_ADDRSTRLEN];
327
328 pim_inet4_dump ("<src?>", src_addr, src_str, sizeof (src_str));
329 zlog_debug ("Received Register message(%s) from %s on %s, rp: %d",
330 pim_str_sg_dump (&sg), src_str, ifp->name, i_am_rp);
331 }
332
333 if (i_am_rp && (dest_addr.s_addr == ((RP (sg.grp))->rpf_addr.u.prefix4.s_addr))) {
334 sentRegisterStop = 0;
335
336 if (*bits & PIM_REGISTER_BORDER_BIT) {
337 struct in_addr pimbr = pim_br_get_pmbr (&sg);
338 if (PIM_DEBUG_PIM_PACKETS)
339 zlog_debug("%s: Received Register message with Border bit set", __func__);
340
341 if (pimbr.s_addr == pim_br_unknown.s_addr)
342 pim_br_set_pmbr(&sg, src_addr);
343 else if (src_addr.s_addr != pimbr.s_addr) {
344 pim_register_stop_send (ifp, &sg, dest_addr, src_addr);
345 if (PIM_DEBUG_PIM_PACKETS)
346 zlog_debug("%s: Sending register-Stop to %s and dropping mr. packet",
347 __func__, "Sender");
348 /* Drop Packet Silently */
349 return 1;
350 }
351 }
352
353 struct pim_upstream *upstream = pim_upstream_find (&sg);
354 /*
355 * If we don't have a place to send ignore the packet
356 */
357 if (!upstream)
358 {
359 upstream = pim_upstream_add (&sg, ifp,
360 PIM_UPSTREAM_FLAG_MASK_SRC_STREAM,
361 __PRETTY_FUNCTION__);
362 if (!upstream)
363 {
364 zlog_warn ("Failure to create upstream state");
365 return 1;
366 }
367
368 upstream->upstream_register = src_addr;
369 }
370
371 if ((upstream->sptbit == PIM_UPSTREAM_SPTBIT_TRUE) ||
372 ((SwitchToSptDesired(&sg)) &&
373 pim_upstream_inherited_olist (upstream) == 0)) {
374 //pim_scan_individual_oil (upstream->channel_oil);
375 pim_register_stop_send (ifp, &sg, dest_addr, src_addr);
376 sentRegisterStop = 1;
377 } else {
378 if (PIM_DEBUG_PIM_REG)
379 zlog_debug ("(%s) sptbit: %d", upstream->sg_str, upstream->sptbit);
380 }
381 if ((upstream->sptbit == PIM_UPSTREAM_SPTBIT_TRUE) ||
382 (SwitchToSptDesired(&sg))) {
383 if (sentRegisterStop) {
384 pim_upstream_keep_alive_timer_start (upstream, qpim_rp_keep_alive_time);
385 } else {
386 pim_upstream_keep_alive_timer_start (upstream, qpim_keep_alive_time);
387 }
388 }
389
390 if (!(upstream->sptbit == PIM_UPSTREAM_SPTBIT_TRUE) &&
391 !(*bits & PIM_REGISTER_NR_BIT))
392 {
393 //decapsulate and forward the iner packet to
394 //inherited_olist(S,G,rpt)
395 // This is taken care of by the kernel for us
396 }
397 pim_upstream_msdp_reg_timer_start(upstream);
398 } else {
399 if (PIM_DEBUG_PIM_REG)
400 {
401 if (!i_am_rp)
402 zlog_debug ("Received Register packet for %s, Rejecting packet because I am not the RP configured for group",
403 pim_str_sg_dump (&sg));
404 else
405 zlog_debug ("Received Register packet for %s, Rejecting packet because the dst ip address is not the actual RP",
406 pim_str_sg_dump (&sg));
407 }
408 pim_register_stop_send (ifp, &sg, dest_addr, src_addr);
409 }
410
411 return 1;
412 }