]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * PIM for Quagga | |
3 | * Copyright (C) 2008 Everton da Silva Marques | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; see the file COPYING; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
19 | ||
20 | #include <zebra.h> | |
21 | #include "log.h" | |
22 | #include "privs.h" | |
23 | #include "if.h" | |
24 | #include "prefix.h" | |
25 | #include "vty.h" | |
26 | #include "plist.h" | |
27 | #include "sockopt.h" | |
28 | #include "lib_errors.h" | |
29 | ||
30 | #include "pimd.h" | |
31 | #include "pim_rpf.h" | |
32 | #include "pim_mroute.h" | |
33 | #include "pim_oil.h" | |
34 | #include "pim_str.h" | |
35 | #include "pim_time.h" | |
36 | #include "pim_iface.h" | |
37 | #include "pim_macro.h" | |
38 | #include "pim_rp.h" | |
39 | #include "pim_oil.h" | |
40 | #include "pim_register.h" | |
41 | #include "pim_ifchannel.h" | |
42 | #include "pim_zlookup.h" | |
43 | #include "pim_ssm.h" | |
44 | #include "pim_sock.h" | |
45 | #include "pim_vxlan.h" | |
46 | ||
47 | static void mroute_read_on(struct pim_instance *pim); | |
48 | ||
49 | static int pim_mroute_set(struct pim_instance *pim, int enable) | |
50 | { | |
51 | int err; | |
52 | int opt, data; | |
53 | socklen_t data_len = sizeof(data); | |
54 | long flags; | |
55 | ||
56 | /* | |
57 | * We need to create the VRF table for the pim mroute_socket | |
58 | */ | |
59 | if (pim->vrf_id != VRF_DEFAULT) { | |
60 | frr_with_privs(&pimd_privs) { | |
61 | ||
62 | data = pim->vrf->data.l.table_id; | |
63 | err = setsockopt(pim->mroute_socket, IPPROTO_IP, | |
64 | MRT_TABLE, | |
65 | &data, data_len); | |
66 | if (err) { | |
67 | zlog_warn( | |
68 | "%s %s: failure: setsockopt(fd=%d,IPPROTO_IP, MRT_TABLE=%d): errno=%d: %s", | |
69 | __FILE__, __PRETTY_FUNCTION__, | |
70 | pim->mroute_socket, data, errno, | |
71 | safe_strerror(errno)); | |
72 | return -1; | |
73 | } | |
74 | ||
75 | } | |
76 | } | |
77 | ||
78 | frr_with_privs(&pimd_privs) { | |
79 | opt = enable ? MRT_INIT : MRT_DONE; | |
80 | /* | |
81 | * *BSD *cares* about what value we pass down | |
82 | * here | |
83 | */ | |
84 | data = 1; | |
85 | err = setsockopt(pim->mroute_socket, IPPROTO_IP, | |
86 | opt, &data, data_len); | |
87 | if (err) { | |
88 | zlog_warn( | |
89 | "%s %s: failure: setsockopt(fd=%d,IPPROTO_IP,%s=%d): errno=%d: %s", | |
90 | __FILE__, __PRETTY_FUNCTION__, | |
91 | pim->mroute_socket, | |
92 | enable ? "MRT_INIT" : "MRT_DONE", data, errno, | |
93 | safe_strerror(errno)); | |
94 | return -1; | |
95 | } | |
96 | } | |
97 | ||
98 | #if defined(HAVE_IP_PKTINFO) | |
99 | if (enable) { | |
100 | /* Linux and Solaris IP_PKTINFO */ | |
101 | data = 1; | |
102 | if (setsockopt(pim->mroute_socket, IPPROTO_IP, IP_PKTINFO, | |
103 | &data, data_len)) { | |
104 | zlog_warn( | |
105 | "Could not set IP_PKTINFO on socket fd=%d: errno=%d: %s", | |
106 | pim->mroute_socket, errno, | |
107 | safe_strerror(errno)); | |
108 | } | |
109 | } | |
110 | #endif | |
111 | ||
112 | setsockopt_so_recvbuf(pim->mroute_socket, 1024 * 1024 * 8); | |
113 | ||
114 | flags = fcntl(pim->mroute_socket, F_GETFL, 0); | |
115 | if (flags < 0) { | |
116 | zlog_warn("Could not get flags on socket fd:%d %d %s", | |
117 | pim->mroute_socket, errno, safe_strerror(errno)); | |
118 | close(pim->mroute_socket); | |
119 | return -1; | |
120 | } | |
121 | if (fcntl(pim->mroute_socket, F_SETFL, flags | O_NONBLOCK)) { | |
122 | zlog_warn("Could not set O_NONBLOCK on socket fd:%d %d %s", | |
123 | pim->mroute_socket, errno, safe_strerror(errno)); | |
124 | close(pim->mroute_socket); | |
125 | return -1; | |
126 | } | |
127 | ||
128 | if (enable) { | |
129 | #if defined linux | |
130 | int upcalls = IGMPMSG_WRVIFWHOLE; | |
131 | opt = MRT_PIM; | |
132 | ||
133 | err = setsockopt(pim->mroute_socket, IPPROTO_IP, opt, &upcalls, | |
134 | sizeof(upcalls)); | |
135 | if (err) { | |
136 | zlog_warn( | |
137 | "Failure to register for VIFWHOLE and WRONGVIF upcalls %d %s", | |
138 | errno, safe_strerror(errno)); | |
139 | return -1; | |
140 | } | |
141 | #else | |
142 | zlog_warn( | |
143 | "PIM-SM will not work properly on this platform, until the ability to receive the WRVIFWHOLE upcall"); | |
144 | #endif | |
145 | } | |
146 | ||
147 | return 0; | |
148 | } | |
149 | ||
150 | static const char *const igmpmsgtype2str[IGMPMSG_WRVIFWHOLE + 1] = { | |
151 | "<unknown_upcall?>", "NOCACHE", "WRONGVIF", "WHOLEPKT", "WRVIFWHOLE"}; | |
152 | ||
153 | static int pim_mroute_msg_nocache(int fd, struct interface *ifp, | |
154 | const struct igmpmsg *msg) | |
155 | { | |
156 | struct pim_interface *pim_ifp = ifp->info; | |
157 | struct pim_upstream *up; | |
158 | struct pim_rpf *rpg; | |
159 | struct prefix_sg sg; | |
160 | ||
161 | rpg = pim_ifp ? RP(pim_ifp->pim, msg->im_dst) : NULL; | |
162 | /* | |
163 | * If the incoming interface is unknown OR | |
164 | * the Interface type is SSM we don't need to | |
165 | * do anything here | |
166 | */ | |
167 | if (!rpg || pim_rpf_addr_is_inaddr_none(rpg)) { | |
168 | if (PIM_DEBUG_MROUTE_DETAIL) | |
169 | zlog_debug( | |
170 | "%s: Interface is not configured correctly to handle incoming packet: Could be !pim_ifp, !SM, !RP", | |
171 | __PRETTY_FUNCTION__); | |
172 | ||
173 | return 0; | |
174 | } | |
175 | ||
176 | /* | |
177 | * If we've received a multicast packet that isn't connected to | |
178 | * us | |
179 | */ | |
180 | if (!pim_if_connected_to_source(ifp, msg->im_src)) { | |
181 | if (PIM_DEBUG_MROUTE_DETAIL) | |
182 | zlog_debug( | |
183 | "%s: Received incoming packet that doesn't originate on our seg", | |
184 | __PRETTY_FUNCTION__); | |
185 | return 0; | |
186 | } | |
187 | ||
188 | memset(&sg, 0, sizeof(struct prefix_sg)); | |
189 | sg.src = msg->im_src; | |
190 | sg.grp = msg->im_dst; | |
191 | ||
192 | if (!(PIM_I_am_DR(pim_ifp))) { | |
193 | if (PIM_DEBUG_MROUTE_DETAIL) | |
194 | zlog_debug("%s: Interface is not the DR blackholing incoming traffic for %s", | |
195 | __PRETTY_FUNCTION__, pim_str_sg_dump(&sg)); | |
196 | ||
197 | /* | |
198 | * We are not the DR, but we are still receiving packets | |
199 | * Let's blackhole those packets for the moment | |
200 | * As that they will be coming up to the cpu | |
201 | * and causing us to consider them. | |
202 | * | |
203 | * This *will* create a dangling channel_oil | |
204 | * that I see no way to get rid of. Just noting | |
205 | * this for future reference. | |
206 | */ | |
207 | up = pim_upstream_find_or_add( | |
208 | &sg, ifp, PIM_UPSTREAM_FLAG_MASK_SRC_NOCACHE, | |
209 | __PRETTY_FUNCTION__); | |
210 | pim_upstream_mroute_add(up->channel_oil, __PRETTY_FUNCTION__); | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
215 | up = pim_upstream_find_or_add(&sg, ifp, PIM_UPSTREAM_FLAG_MASK_FHR, | |
216 | __PRETTY_FUNCTION__); | |
217 | ||
218 | /* | |
219 | * I moved this debug till after the actual add because | |
220 | * I want to take advantage of the up->sg_str being filled in. | |
221 | */ | |
222 | if (PIM_DEBUG_MROUTE) { | |
223 | zlog_debug("%s: Adding a Route %s for WHOLEPKT consumption", | |
224 | __PRETTY_FUNCTION__, up->sg_str); | |
225 | } | |
226 | ||
227 | PIM_UPSTREAM_FLAG_SET_SRC_STREAM(up->flags); | |
228 | pim_upstream_keep_alive_timer_start(up, pim_ifp->pim->keep_alive_time); | |
229 | ||
230 | up->channel_oil->cc.pktcnt++; | |
231 | // resolve mfcc_parent prior to mroute_add in channel_add_oif | |
232 | if (up->rpf.source_nexthop.interface && | |
233 | up->channel_oil->oil.mfcc_parent >= MAXVIFS) { | |
234 | pim_upstream_mroute_iif_update(up->channel_oil, __func__); | |
235 | } | |
236 | pim_register_join(up); | |
237 | ||
238 | return 0; | |
239 | } | |
240 | ||
241 | static int pim_mroute_msg_wholepkt(int fd, struct interface *ifp, | |
242 | const char *buf) | |
243 | { | |
244 | struct pim_interface *pim_ifp; | |
245 | struct prefix_sg sg; | |
246 | struct pim_rpf *rpg; | |
247 | const struct ip *ip_hdr; | |
248 | struct pim_upstream *up; | |
249 | ||
250 | pim_ifp = ifp->info; | |
251 | ||
252 | ip_hdr = (const struct ip *)buf; | |
253 | ||
254 | memset(&sg, 0, sizeof(struct prefix_sg)); | |
255 | sg.src = ip_hdr->ip_src; | |
256 | sg.grp = ip_hdr->ip_dst; | |
257 | ||
258 | up = pim_upstream_find(pim_ifp->pim, &sg); | |
259 | if (!up) { | |
260 | struct prefix_sg star = sg; | |
261 | star.src.s_addr = INADDR_ANY; | |
262 | ||
263 | up = pim_upstream_find(pim_ifp->pim, &star); | |
264 | ||
265 | if (up && PIM_UPSTREAM_FLAG_TEST_SRC_IGMP(up->flags)) { | |
266 | up = pim_upstream_add(pim_ifp->pim, &sg, ifp, | |
267 | PIM_UPSTREAM_FLAG_MASK_SRC_LHR, | |
268 | __PRETTY_FUNCTION__, NULL); | |
269 | if (!up) { | |
270 | if (PIM_DEBUG_MROUTE) | |
271 | zlog_debug( | |
272 | "%s: Unable to create upstream information for %s", | |
273 | __PRETTY_FUNCTION__, | |
274 | pim_str_sg_dump(&sg)); | |
275 | return 0; | |
276 | } | |
277 | pim_upstream_keep_alive_timer_start( | |
278 | up, pim_ifp->pim->keep_alive_time); | |
279 | pim_upstream_inherited_olist(pim_ifp->pim, up); | |
280 | pim_upstream_update_join_desired(pim_ifp->pim, up); | |
281 | ||
282 | if (PIM_DEBUG_MROUTE) | |
283 | zlog_debug("%s: Creating %s upstream on LHR", | |
284 | __PRETTY_FUNCTION__, up->sg_str); | |
285 | return 0; | |
286 | } | |
287 | if (PIM_DEBUG_MROUTE_DETAIL) { | |
288 | zlog_debug( | |
289 | "%s: Unable to find upstream channel WHOLEPKT%s", | |
290 | __PRETTY_FUNCTION__, pim_str_sg_dump(&sg)); | |
291 | } | |
292 | return 0; | |
293 | } | |
294 | ||
295 | if (!up->rpf.source_nexthop.interface) { | |
296 | if (PIM_DEBUG_PIM_TRACE) | |
297 | zlog_debug("%s: up %s RPF is not present", | |
298 | __PRETTY_FUNCTION__, up->sg_str); | |
299 | return 0; | |
300 | } | |
301 | ||
302 | pim_ifp = up->rpf.source_nexthop.interface->info; | |
303 | ||
304 | rpg = pim_ifp ? RP(pim_ifp->pim, sg.grp) : NULL; | |
305 | ||
306 | if ((pim_rpf_addr_is_inaddr_none(rpg)) || (!pim_ifp) | |
307 | || (!(PIM_I_am_DR(pim_ifp)))) { | |
308 | if (PIM_DEBUG_MROUTE) { | |
309 | zlog_debug("%s: Failed Check send packet", | |
310 | __PRETTY_FUNCTION__); | |
311 | } | |
312 | return 0; | |
313 | } | |
314 | ||
315 | /* | |
316 | * If we've received a register suppress | |
317 | */ | |
318 | if (!up->t_rs_timer) { | |
319 | if (pim_is_grp_ssm(pim_ifp->pim, sg.grp)) { | |
320 | if (PIM_DEBUG_PIM_REG) | |
321 | zlog_debug( | |
322 | "%s register forward skipped as group is SSM", | |
323 | pim_str_sg_dump(&sg)); | |
324 | return 0; | |
325 | } | |
326 | ||
327 | if (!PIM_UPSTREAM_FLAG_TEST_FHR(up->flags)) { | |
328 | if (PIM_DEBUG_PIM_REG) | |
329 | zlog_debug( | |
330 | "%s register forward skipped, not FHR", | |
331 | up->sg_str); | |
332 | return 0; | |
333 | } | |
334 | ||
335 | pim_register_send((uint8_t *)buf + sizeof(struct ip), | |
336 | ntohs(ip_hdr->ip_len) - sizeof(struct ip), | |
337 | pim_ifp->primary_address, rpg, 0, up); | |
338 | } | |
339 | return 0; | |
340 | } | |
341 | ||
342 | static int pim_mroute_msg_wrongvif(int fd, struct interface *ifp, | |
343 | const struct igmpmsg *msg) | |
344 | { | |
345 | struct pim_ifchannel *ch; | |
346 | struct pim_interface *pim_ifp; | |
347 | struct prefix_sg sg; | |
348 | ||
349 | memset(&sg, 0, sizeof(struct prefix_sg)); | |
350 | sg.src = msg->im_src; | |
351 | sg.grp = msg->im_dst; | |
352 | ||
353 | /* | |
354 | Send Assert(S,G) on iif as response to WRONGVIF kernel upcall. | |
355 | ||
356 | RFC 4601 4.8.2. PIM-SSM-Only Routers | |
357 | ||
358 | iif is the incoming interface of the packet. | |
359 | if (iif is in inherited_olist(S,G)) { | |
360 | send Assert(S,G) on iif | |
361 | } | |
362 | */ | |
363 | ||
364 | if (!ifp) { | |
365 | if (PIM_DEBUG_MROUTE) | |
366 | zlog_debug( | |
367 | "%s: WRONGVIF (S,G)=%s could not find input interface for input_vif_index=%d", | |
368 | __PRETTY_FUNCTION__, pim_str_sg_dump(&sg), | |
369 | msg->im_vif); | |
370 | return -1; | |
371 | } | |
372 | ||
373 | pim_ifp = ifp->info; | |
374 | if (!pim_ifp) { | |
375 | if (PIM_DEBUG_MROUTE) | |
376 | zlog_debug( | |
377 | "%s: WRONGVIF (S,G)=%s multicast not enabled on interface %s", | |
378 | __PRETTY_FUNCTION__, pim_str_sg_dump(&sg), | |
379 | ifp->name); | |
380 | return -2; | |
381 | } | |
382 | ||
383 | ch = pim_ifchannel_find(ifp, &sg); | |
384 | if (!ch) { | |
385 | struct prefix_sg star_g = sg; | |
386 | if (PIM_DEBUG_MROUTE) | |
387 | zlog_debug( | |
388 | "%s: WRONGVIF (S,G)=%s could not find channel on interface %s", | |
389 | __PRETTY_FUNCTION__, pim_str_sg_dump(&sg), | |
390 | ifp->name); | |
391 | ||
392 | star_g.src.s_addr = INADDR_ANY; | |
393 | ch = pim_ifchannel_find(ifp, &star_g); | |
394 | if (!ch) { | |
395 | if (PIM_DEBUG_MROUTE) | |
396 | zlog_debug( | |
397 | "%s: WRONGVIF (*,G)=%s could not find channel on interface %s", | |
398 | __PRETTY_FUNCTION__, | |
399 | pim_str_sg_dump(&star_g), ifp->name); | |
400 | return -3; | |
401 | } | |
402 | } | |
403 | ||
404 | /* | |
405 | RFC 4601: 4.6.1. (S,G) Assert Message State Machine | |
406 | ||
407 | Transitions from NoInfo State | |
408 | ||
409 | An (S,G) data packet arrives on interface I, AND | |
410 | CouldAssert(S,G,I)==TRUE An (S,G) data packet arrived on an | |
411 | downstream interface that is in our (S,G) outgoing interface | |
412 | list. We optimistically assume that we will be the assert | |
413 | winner for this (S,G), and so we transition to the "I am Assert | |
414 | Winner" state and perform Actions A1 (below), which will | |
415 | initiate the assert negotiation for (S,G). | |
416 | */ | |
417 | ||
418 | if (ch->ifassert_state != PIM_IFASSERT_NOINFO) { | |
419 | if (PIM_DEBUG_MROUTE) { | |
420 | zlog_debug( | |
421 | "%s: WRONGVIF (S,G)=%s channel is not on Assert NoInfo state for interface %s", | |
422 | __PRETTY_FUNCTION__, ch->sg_str, ifp->name); | |
423 | } | |
424 | return -4; | |
425 | } | |
426 | ||
427 | if (!PIM_IF_FLAG_TEST_COULD_ASSERT(ch->flags)) { | |
428 | if (PIM_DEBUG_MROUTE) { | |
429 | zlog_debug( | |
430 | "%s: WRONGVIF (S,G)=%s interface %s is not downstream for channel", | |
431 | __PRETTY_FUNCTION__, ch->sg_str, ifp->name); | |
432 | } | |
433 | return -5; | |
434 | } | |
435 | ||
436 | if (assert_action_a1(ch)) { | |
437 | if (PIM_DEBUG_MROUTE) { | |
438 | zlog_debug( | |
439 | "%s: WRONGVIF (S,G)=%s assert_action_a1 failure on interface %s", | |
440 | __PRETTY_FUNCTION__, ch->sg_str, ifp->name); | |
441 | } | |
442 | return -6; | |
443 | } | |
444 | ||
445 | return 0; | |
446 | } | |
447 | ||
448 | static int pim_mroute_msg_wrvifwhole(int fd, struct interface *ifp, | |
449 | const char *buf) | |
450 | { | |
451 | const struct ip *ip_hdr = (const struct ip *)buf; | |
452 | struct pim_interface *pim_ifp; | |
453 | struct pim_ifchannel *ch; | |
454 | struct pim_upstream *up; | |
455 | struct prefix_sg star_g; | |
456 | struct prefix_sg sg; | |
457 | ||
458 | pim_ifp = ifp->info; | |
459 | ||
460 | memset(&sg, 0, sizeof(struct prefix_sg)); | |
461 | sg.src = ip_hdr->ip_src; | |
462 | sg.grp = ip_hdr->ip_dst; | |
463 | ||
464 | ch = pim_ifchannel_find(ifp, &sg); | |
465 | if (ch) { | |
466 | if (PIM_DEBUG_MROUTE) | |
467 | zlog_debug( | |
468 | "WRVIFWHOLE (S,G)=%s found ifchannel on interface %s", | |
469 | ch->sg_str, ifp->name); | |
470 | return -1; | |
471 | } | |
472 | ||
473 | star_g = sg; | |
474 | star_g.src.s_addr = INADDR_ANY; | |
475 | #if 0 | |
476 | ch = pim_ifchannel_find(ifp, &star_g); | |
477 | if (ch) | |
478 | { | |
479 | if (PIM_DEBUG_MROUTE) | |
480 | zlog_debug ("WRVIFWHOLE (*,G)=%s found ifchannel on interface %s", | |
481 | pim_str_sg_dump (&star_g), ifp->name); | |
482 | return -1; | |
483 | } | |
484 | #endif | |
485 | ||
486 | up = pim_upstream_find(pim_ifp->pim, &sg); | |
487 | if (up) { | |
488 | struct pim_upstream *parent; | |
489 | struct pim_nexthop source; | |
490 | struct pim_rpf *rpf = RP(pim_ifp->pim, sg.grp); | |
491 | if (!rpf || !rpf->source_nexthop.interface) | |
492 | return 0; | |
493 | ||
494 | /* | |
495 | * If we have received a WRVIFWHOLE and are at this | |
496 | * point, we could be receiving the packet on the *,G | |
497 | * tree, let's check and if so we can safely drop | |
498 | * it. | |
499 | */ | |
500 | parent = pim_upstream_find(pim_ifp->pim, &star_g); | |
501 | if (parent && parent->rpf.source_nexthop.interface == ifp) | |
502 | return 0; | |
503 | ||
504 | pim_ifp = rpf->source_nexthop.interface->info; | |
505 | ||
506 | memset(&source, 0, sizeof(source)); | |
507 | /* | |
508 | * If we are the fhr that means we are getting a callback during | |
509 | * the pimreg period, so I believe we can ignore this packet | |
510 | */ | |
511 | if (!PIM_UPSTREAM_FLAG_TEST_FHR(up->flags)) { | |
512 | // No if channel, but upstream we are at the RP. | |
513 | if (pim_nexthop_lookup(pim_ifp->pim, &source, | |
514 | up->upstream_register, 0)) { | |
515 | pim_register_stop_send(source.interface, &sg, | |
516 | pim_ifp->primary_address, | |
517 | up->upstream_register); | |
518 | up->sptbit = PIM_UPSTREAM_SPTBIT_TRUE; | |
519 | } | |
520 | ||
521 | pim_upstream_inherited_olist(pim_ifp->pim, up); | |
522 | if (!up->channel_oil->installed) | |
523 | pim_upstream_mroute_add(up->channel_oil, | |
524 | __PRETTY_FUNCTION__); | |
525 | } else { | |
526 | if (I_am_RP(pim_ifp->pim, up->sg.grp)) { | |
527 | if (pim_nexthop_lookup(pim_ifp->pim, &source, | |
528 | up->upstream_register, | |
529 | 0)) | |
530 | pim_register_stop_send( | |
531 | source.interface, &sg, | |
532 | pim_ifp->primary_address, | |
533 | up->upstream_register); | |
534 | up->sptbit = PIM_UPSTREAM_SPTBIT_TRUE; | |
535 | } | |
536 | pim_upstream_keep_alive_timer_start( | |
537 | up, pim_ifp->pim->keep_alive_time); | |
538 | pim_upstream_inherited_olist(pim_ifp->pim, up); | |
539 | pim_mroute_msg_wholepkt(fd, ifp, buf); | |
540 | } | |
541 | return 0; | |
542 | } | |
543 | ||
544 | pim_ifp = ifp->info; | |
545 | if (pim_if_connected_to_source(ifp, sg.src)) { | |
546 | up = pim_upstream_add(pim_ifp->pim, &sg, ifp, | |
547 | PIM_UPSTREAM_FLAG_MASK_FHR, | |
548 | __PRETTY_FUNCTION__, NULL); | |
549 | if (!up) { | |
550 | if (PIM_DEBUG_MROUTE) | |
551 | zlog_debug( | |
552 | "%s: WRONGVIF%s unable to create upstream on interface", | |
553 | pim_str_sg_dump(&sg), ifp->name); | |
554 | return -2; | |
555 | } | |
556 | PIM_UPSTREAM_FLAG_SET_SRC_STREAM(up->flags); | |
557 | pim_upstream_keep_alive_timer_start( | |
558 | up, pim_ifp->pim->keep_alive_time); | |
559 | up->channel_oil->cc.pktcnt++; | |
560 | pim_register_join(up); | |
561 | pim_upstream_inherited_olist(pim_ifp->pim, up); | |
562 | if (!up->channel_oil->installed) | |
563 | pim_upstream_mroute_add(up->channel_oil, __func__); | |
564 | ||
565 | // Send the packet to the RP | |
566 | pim_mroute_msg_wholepkt(fd, ifp, buf); | |
567 | } else { | |
568 | up = pim_upstream_add(pim_ifp->pim, &sg, ifp, | |
569 | PIM_UPSTREAM_FLAG_MASK_SRC_NOCACHE, | |
570 | __PRETTY_FUNCTION__, NULL); | |
571 | if (!up->channel_oil->installed) | |
572 | pim_upstream_mroute_add(up->channel_oil, | |
573 | __PRETTY_FUNCTION__); | |
574 | } | |
575 | ||
576 | return 0; | |
577 | } | |
578 | ||
579 | static int pim_mroute_msg(struct pim_instance *pim, const char *buf, | |
580 | int buf_size, ifindex_t ifindex) | |
581 | { | |
582 | struct interface *ifp; | |
583 | struct pim_interface *pim_ifp; | |
584 | const struct ip *ip_hdr; | |
585 | const struct igmpmsg *msg; | |
586 | char ip_src_str[INET_ADDRSTRLEN] = ""; | |
587 | char ip_dst_str[INET_ADDRSTRLEN] = ""; | |
588 | char src_str[INET_ADDRSTRLEN] = "<src?>"; | |
589 | char grp_str[INET_ADDRSTRLEN] = "<grp?>"; | |
590 | struct in_addr ifaddr; | |
591 | struct igmp_sock *igmp; | |
592 | ||
593 | if (buf_size < (int)sizeof(struct ip)) | |
594 | return 0; | |
595 | ||
596 | ip_hdr = (const struct ip *)buf; | |
597 | ||
598 | if (ip_hdr->ip_p == IPPROTO_IGMP) { | |
599 | ||
600 | /* We have the IP packet but we do not know which interface this | |
601 | * packet was | |
602 | * received on. Find the interface that is on the same subnet as | |
603 | * the source | |
604 | * of the IP packet. | |
605 | */ | |
606 | ifp = if_lookup_by_index(ifindex, pim->vrf_id); | |
607 | ||
608 | if (!ifp || !ifp->info) | |
609 | return 0; | |
610 | ||
611 | pim_ifp = ifp->info; | |
612 | ifaddr = pim_find_primary_addr(ifp); | |
613 | igmp = pim_igmp_sock_lookup_ifaddr(pim_ifp->igmp_socket_list, | |
614 | ifaddr); | |
615 | ||
616 | if (PIM_DEBUG_MROUTE) { | |
617 | pim_inet4_dump("<src?>", ip_hdr->ip_src, ip_src_str, | |
618 | sizeof(ip_src_str)); | |
619 | pim_inet4_dump("<dst?>", ip_hdr->ip_dst, ip_dst_str, | |
620 | sizeof(ip_dst_str)); | |
621 | ||
622 | zlog_debug( | |
623 | "%s(%s): igmp kernel upcall on %s(%p) for %s -> %s", | |
624 | __PRETTY_FUNCTION__, pim->vrf->name, ifp->name, | |
625 | igmp, ip_src_str, ip_dst_str); | |
626 | } | |
627 | if (igmp) | |
628 | pim_igmp_packet(igmp, (char *)buf, buf_size); | |
629 | ||
630 | } else if (ip_hdr->ip_p) { | |
631 | if (PIM_DEBUG_MROUTE_DETAIL) { | |
632 | pim_inet4_dump("<src?>", ip_hdr->ip_src, src_str, | |
633 | sizeof(src_str)); | |
634 | pim_inet4_dump("<grp?>", ip_hdr->ip_dst, grp_str, | |
635 | sizeof(grp_str)); | |
636 | zlog_debug( | |
637 | "%s: no kernel upcall proto=%d src: %s dst: %s msg_size=%d", | |
638 | __PRETTY_FUNCTION__, ip_hdr->ip_p, src_str, | |
639 | grp_str, buf_size); | |
640 | } | |
641 | ||
642 | } else { | |
643 | msg = (const struct igmpmsg *)buf; | |
644 | ||
645 | ifp = pim_if_find_by_vif_index(pim, msg->im_vif); | |
646 | ||
647 | if (!ifp) | |
648 | return 0; | |
649 | if (PIM_DEBUG_MROUTE) { | |
650 | pim_inet4_dump("<src?>", msg->im_src, src_str, | |
651 | sizeof(src_str)); | |
652 | pim_inet4_dump("<grp?>", msg->im_dst, grp_str, | |
653 | sizeof(grp_str)); | |
654 | zlog_debug( | |
655 | "%s: pim kernel upcall %s type=%d ip_p=%d from fd=%d for (S,G)=(%s,%s) on %s vifi=%d size=%d", | |
656 | __PRETTY_FUNCTION__, | |
657 | igmpmsgtype2str[msg->im_msgtype], | |
658 | msg->im_msgtype, ip_hdr->ip_p, | |
659 | pim->mroute_socket, src_str, grp_str, ifp->name, | |
660 | msg->im_vif, buf_size); | |
661 | } | |
662 | ||
663 | switch (msg->im_msgtype) { | |
664 | case IGMPMSG_WRONGVIF: | |
665 | return pim_mroute_msg_wrongvif(pim->mroute_socket, ifp, | |
666 | msg); | |
667 | break; | |
668 | case IGMPMSG_NOCACHE: | |
669 | return pim_mroute_msg_nocache(pim->mroute_socket, ifp, | |
670 | msg); | |
671 | break; | |
672 | case IGMPMSG_WHOLEPKT: | |
673 | return pim_mroute_msg_wholepkt(pim->mroute_socket, ifp, | |
674 | (const char *)msg); | |
675 | break; | |
676 | case IGMPMSG_WRVIFWHOLE: | |
677 | return pim_mroute_msg_wrvifwhole( | |
678 | pim->mroute_socket, ifp, (const char *)msg); | |
679 | break; | |
680 | default: | |
681 | break; | |
682 | } | |
683 | } | |
684 | ||
685 | return 0; | |
686 | } | |
687 | ||
688 | static int mroute_read(struct thread *t) | |
689 | { | |
690 | struct pim_instance *pim; | |
691 | static long long count; | |
692 | char buf[10000]; | |
693 | int result = 0; | |
694 | int cont = 1; | |
695 | int rd; | |
696 | ifindex_t ifindex; | |
697 | pim = THREAD_ARG(t); | |
698 | ||
699 | while (cont) { | |
700 | rd = pim_socket_recvfromto(pim->mroute_socket, (uint8_t *)buf, | |
701 | sizeof(buf), NULL, NULL, NULL, NULL, | |
702 | &ifindex); | |
703 | if (rd <= 0) { | |
704 | if (errno == EINTR) | |
705 | continue; | |
706 | if (errno == EWOULDBLOCK || errno == EAGAIN) | |
707 | break; | |
708 | ||
709 | zlog_warn("%s: failure reading rd=%d: fd=%d: errno=%d: %s", | |
710 | __PRETTY_FUNCTION__, rd, pim->mroute_socket, | |
711 | errno, safe_strerror(errno)); | |
712 | goto done; | |
713 | } | |
714 | ||
715 | result = pim_mroute_msg(pim, buf, rd, ifindex); | |
716 | ||
717 | count++; | |
718 | if (count % router->packet_process == 0) | |
719 | cont = 0; | |
720 | } | |
721 | /* Keep reading */ | |
722 | done: | |
723 | mroute_read_on(pim); | |
724 | ||
725 | return result; | |
726 | } | |
727 | ||
728 | static void mroute_read_on(struct pim_instance *pim) | |
729 | { | |
730 | thread_add_read(router->master, mroute_read, pim, pim->mroute_socket, | |
731 | &pim->thread); | |
732 | } | |
733 | ||
734 | static void mroute_read_off(struct pim_instance *pim) | |
735 | { | |
736 | THREAD_OFF(pim->thread); | |
737 | } | |
738 | ||
739 | int pim_mroute_socket_enable(struct pim_instance *pim) | |
740 | { | |
741 | int fd; | |
742 | ||
743 | frr_with_privs(&pimd_privs) { | |
744 | ||
745 | fd = socket(AF_INET, SOCK_RAW, IPPROTO_IGMP); | |
746 | ||
747 | if (fd < 0) { | |
748 | zlog_warn("Could not create mroute socket: errno=%d: %s", | |
749 | errno, | |
750 | safe_strerror(errno)); | |
751 | return -2; | |
752 | } | |
753 | ||
754 | #ifdef SO_BINDTODEVICE | |
755 | if (pim->vrf->vrf_id != VRF_DEFAULT | |
756 | && setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, | |
757 | pim->vrf->name, strlen(pim->vrf->name))) { | |
758 | zlog_warn("Could not setsockopt SO_BINDTODEVICE: %s", | |
759 | safe_strerror(errno)); | |
760 | close(fd); | |
761 | return -3; | |
762 | } | |
763 | #endif | |
764 | ||
765 | } | |
766 | ||
767 | pim->mroute_socket = fd; | |
768 | if (pim_mroute_set(pim, 1)) { | |
769 | zlog_warn( | |
770 | "Could not enable mroute on socket fd=%d: errno=%d: %s", | |
771 | fd, errno, safe_strerror(errno)); | |
772 | close(fd); | |
773 | pim->mroute_socket = -1; | |
774 | return -3; | |
775 | } | |
776 | ||
777 | pim->mroute_socket_creation = pim_time_monotonic_sec(); | |
778 | ||
779 | mroute_read_on(pim); | |
780 | ||
781 | return 0; | |
782 | } | |
783 | ||
784 | int pim_mroute_socket_disable(struct pim_instance *pim) | |
785 | { | |
786 | if (pim_mroute_set(pim, 0)) { | |
787 | zlog_warn( | |
788 | "Could not disable mroute on socket fd=%d: errno=%d: %s", | |
789 | pim->mroute_socket, errno, safe_strerror(errno)); | |
790 | return -2; | |
791 | } | |
792 | ||
793 | if (close(pim->mroute_socket)) { | |
794 | zlog_warn("Failure closing mroute socket: fd=%d errno=%d: %s", | |
795 | pim->mroute_socket, errno, safe_strerror(errno)); | |
796 | return -3; | |
797 | } | |
798 | ||
799 | mroute_read_off(pim); | |
800 | pim->mroute_socket = -1; | |
801 | ||
802 | return 0; | |
803 | } | |
804 | ||
805 | /* | |
806 | For each network interface (e.g., physical or a virtual tunnel) that | |
807 | would be used for multicast forwarding, a corresponding multicast | |
808 | interface must be added to the kernel. | |
809 | */ | |
810 | int pim_mroute_add_vif(struct interface *ifp, struct in_addr ifaddr, | |
811 | unsigned char flags) | |
812 | { | |
813 | struct pim_interface *pim_ifp = ifp->info; | |
814 | struct vifctl vc; | |
815 | int err; | |
816 | ||
817 | if (PIM_DEBUG_MROUTE) | |
818 | zlog_debug("%s: Add Vif %d (%s[%s])", __PRETTY_FUNCTION__, | |
819 | pim_ifp->mroute_vif_index, ifp->name, | |
820 | pim_ifp->pim->vrf->name); | |
821 | ||
822 | memset(&vc, 0, sizeof(vc)); | |
823 | vc.vifc_vifi = pim_ifp->mroute_vif_index; | |
824 | #ifdef VIFF_USE_IFINDEX | |
825 | vc.vifc_lcl_ifindex = ifp->ifindex; | |
826 | #else | |
827 | if (ifaddr.s_addr == INADDR_ANY) { | |
828 | zlog_warn( | |
829 | "%s: unnumbered interfaces are not supported on this platform", | |
830 | __PRETTY_FUNCTION__); | |
831 | return -1; | |
832 | } | |
833 | memcpy(&vc.vifc_lcl_addr, &ifaddr, sizeof(vc.vifc_lcl_addr)); | |
834 | #endif | |
835 | vc.vifc_flags = flags; | |
836 | vc.vifc_threshold = PIM_MROUTE_MIN_TTL; | |
837 | vc.vifc_rate_limit = 0; | |
838 | ||
839 | #ifdef PIM_DVMRP_TUNNEL | |
840 | if (vc.vifc_flags & VIFF_TUNNEL) { | |
841 | memcpy(&vc.vifc_rmt_addr, &vif_remote_addr, | |
842 | sizeof(vc.vifc_rmt_addr)); | |
843 | } | |
844 | #endif | |
845 | ||
846 | err = setsockopt(pim_ifp->pim->mroute_socket, IPPROTO_IP, MRT_ADD_VIF, | |
847 | (void *)&vc, sizeof(vc)); | |
848 | if (err) { | |
849 | char ifaddr_str[INET_ADDRSTRLEN]; | |
850 | ||
851 | pim_inet4_dump("<ifaddr?>", ifaddr, ifaddr_str, | |
852 | sizeof(ifaddr_str)); | |
853 | ||
854 | zlog_warn( | |
855 | "%s: failure: setsockopt(fd=%d,IPPROTO_IP,MRT_ADD_VIF,vif_index=%d,ifaddr=%s,flag=%d): errno=%d: %s", | |
856 | __PRETTY_FUNCTION__, pim_ifp->pim->mroute_socket, | |
857 | ifp->ifindex, ifaddr_str, flags, errno, | |
858 | safe_strerror(errno)); | |
859 | return -2; | |
860 | } | |
861 | ||
862 | return 0; | |
863 | } | |
864 | ||
865 | int pim_mroute_del_vif(struct interface *ifp) | |
866 | { | |
867 | struct pim_interface *pim_ifp = ifp->info; | |
868 | struct vifctl vc; | |
869 | int err; | |
870 | ||
871 | if (PIM_DEBUG_MROUTE) | |
872 | zlog_debug("%s: Del Vif %d (%s[%s])", __PRETTY_FUNCTION__, | |
873 | pim_ifp->mroute_vif_index, ifp->name, | |
874 | pim_ifp->pim->vrf->name); | |
875 | ||
876 | memset(&vc, 0, sizeof(vc)); | |
877 | vc.vifc_vifi = pim_ifp->mroute_vif_index; | |
878 | ||
879 | err = setsockopt(pim_ifp->pim->mroute_socket, IPPROTO_IP, MRT_DEL_VIF, | |
880 | (void *)&vc, sizeof(vc)); | |
881 | if (err) { | |
882 | zlog_warn( | |
883 | "%s %s: failure: setsockopt(fd=%d,IPPROTO_IP,MRT_DEL_VIF,vif_index=%d): errno=%d: %s", | |
884 | __FILE__, __PRETTY_FUNCTION__, | |
885 | pim_ifp->pim->mroute_socket, pim_ifp->mroute_vif_index, | |
886 | errno, safe_strerror(errno)); | |
887 | return -2; | |
888 | } | |
889 | ||
890 | return 0; | |
891 | } | |
892 | ||
893 | /* | |
894 | * Prevent creating MFC entry with OIF=IIF. | |
895 | * | |
896 | * This is a protection against implementation mistakes. | |
897 | * | |
898 | * PIM protocol implicitely ensures loopfree multicast topology. | |
899 | * | |
900 | * IGMP must be protected against adding looped MFC entries created | |
901 | * by both source and receiver attached to the same interface. See | |
902 | * TODO T22. | |
903 | * We shall allow igmp to create upstream when it is DR for the intf. | |
904 | * Assume RP reachable via non DR. | |
905 | */ | |
906 | bool pim_mroute_allow_iif_in_oil(struct channel_oil *c_oil, | |
907 | int oif_index) | |
908 | { | |
909 | #ifdef PIM_ENFORCE_LOOPFREE_MFC | |
910 | struct interface *ifp_out; | |
911 | struct pim_interface *pim_ifp; | |
912 | ||
913 | if (c_oil->up && | |
914 | PIM_UPSTREAM_FLAG_TEST_ALLOW_IIF_IN_OIL(c_oil->up->flags)) | |
915 | return true; | |
916 | ||
917 | ifp_out = pim_if_find_by_vif_index(c_oil->pim, oif_index); | |
918 | if (!ifp_out) | |
919 | return false; | |
920 | pim_ifp = ifp_out->info; | |
921 | if (!pim_ifp) | |
922 | return false; | |
923 | if ((c_oil->oif_flags[oif_index] & PIM_OIF_FLAG_PROTO_IGMP) && | |
924 | PIM_I_am_DR(pim_ifp)) | |
925 | return true; | |
926 | ||
927 | return false; | |
928 | #else | |
929 | return true; | |
930 | #endif | |
931 | } | |
932 | ||
933 | static inline void pim_mroute_copy(struct mfcctl *oil, | |
934 | struct channel_oil *c_oil) | |
935 | { | |
936 | int i; | |
937 | ||
938 | oil->mfcc_origin = c_oil->oil.mfcc_origin; | |
939 | oil->mfcc_mcastgrp = c_oil->oil.mfcc_mcastgrp; | |
940 | oil->mfcc_parent = c_oil->oil.mfcc_parent; | |
941 | ||
942 | for (i = 0; i < MAXVIFS; ++i) { | |
943 | if ((oil->mfcc_parent == i) && | |
944 | !pim_mroute_allow_iif_in_oil(c_oil, i)) { | |
945 | oil->mfcc_ttls[i] = 0; | |
946 | continue; | |
947 | } | |
948 | ||
949 | if (c_oil->oif_flags[i] & PIM_OIF_FLAG_MUTE) | |
950 | oil->mfcc_ttls[i] = 0; | |
951 | else | |
952 | oil->mfcc_ttls[i] = c_oil->oil.mfcc_ttls[i]; | |
953 | } | |
954 | } | |
955 | ||
956 | /* This function must not be called directly 0 | |
957 | * use pim_upstream_mroute_add or pim_static_mroute_add instead | |
958 | */ | |
959 | static int pim_mroute_add(struct channel_oil *c_oil, const char *name) | |
960 | { | |
961 | struct pim_instance *pim = c_oil->pim; | |
962 | struct mfcctl tmp_oil; | |
963 | int err; | |
964 | ||
965 | pim->mroute_add_last = pim_time_monotonic_sec(); | |
966 | ++pim->mroute_add_events; | |
967 | ||
968 | /* Copy the oil to a temporary structure to fixup (without need to | |
969 | * later restore) before sending the mroute add to the dataplane | |
970 | */ | |
971 | pim_mroute_copy(&tmp_oil, c_oil); | |
972 | ||
973 | /* The linux kernel *expects* the incoming | |
974 | * vif to be part of the outgoing list | |
975 | * in the case of a (*,G). | |
976 | */ | |
977 | if (c_oil->oil.mfcc_origin.s_addr == INADDR_ANY) { | |
978 | tmp_oil.mfcc_ttls[c_oil->oil.mfcc_parent] = 1; | |
979 | } | |
980 | ||
981 | /* | |
982 | * If we have an unresolved cache entry for the S,G | |
983 | * it is owned by the pimreg for the incoming IIF | |
984 | * So set pimreg as the IIF temporarily to cause | |
985 | * the packets to be forwarded. Then set it | |
986 | * to the correct IIF afterwords. | |
987 | */ | |
988 | if (!c_oil->installed && c_oil->oil.mfcc_origin.s_addr != INADDR_ANY | |
989 | && c_oil->oil.mfcc_parent != 0) { | |
990 | tmp_oil.mfcc_parent = 0; | |
991 | } | |
992 | err = setsockopt(pim->mroute_socket, IPPROTO_IP, MRT_ADD_MFC, | |
993 | &tmp_oil, sizeof(tmp_oil)); | |
994 | ||
995 | if (!err && !c_oil->installed | |
996 | && c_oil->oil.mfcc_origin.s_addr != INADDR_ANY | |
997 | && c_oil->oil.mfcc_parent != 0) { | |
998 | tmp_oil.mfcc_parent = c_oil->oil.mfcc_parent; | |
999 | err = setsockopt(pim->mroute_socket, IPPROTO_IP, MRT_ADD_MFC, | |
1000 | &tmp_oil, sizeof(tmp_oil)); | |
1001 | } | |
1002 | ||
1003 | if (err) { | |
1004 | zlog_warn( | |
1005 | "%s %s: failure: setsockopt(fd=%d,IPPROTO_IP,MRT_ADD_MFC): errno=%d: %s", | |
1006 | __FILE__, __PRETTY_FUNCTION__, pim->mroute_socket, | |
1007 | errno, safe_strerror(errno)); | |
1008 | return -2; | |
1009 | } | |
1010 | ||
1011 | if (PIM_DEBUG_MROUTE) { | |
1012 | char buf[1000]; | |
1013 | zlog_debug("%s(%s), vrf %s Added Route: %s", | |
1014 | __PRETTY_FUNCTION__, name, pim->vrf->name, | |
1015 | pim_channel_oil_dump(c_oil, buf, sizeof(buf))); | |
1016 | } | |
1017 | ||
1018 | c_oil->installed = 1; | |
1019 | c_oil->mroute_creation = pim_time_monotonic_sec(); | |
1020 | ||
1021 | return 0; | |
1022 | } | |
1023 | ||
1024 | static int pim_upstream_get_mroute_iif(struct channel_oil *c_oil, | |
1025 | const char *name) | |
1026 | { | |
1027 | vifi_t iif = MAXVIFS; | |
1028 | struct interface *ifp = NULL; | |
1029 | struct pim_interface *pim_ifp; | |
1030 | struct pim_upstream *up = c_oil->up; | |
1031 | ||
1032 | if (up) { | |
1033 | if (PIM_UPSTREAM_FLAG_TEST_USE_RPT(up->flags)) { | |
1034 | if (up->parent) | |
1035 | ifp = up->parent->rpf.source_nexthop.interface; | |
1036 | } else { | |
1037 | ifp = up->rpf.source_nexthop.interface; | |
1038 | } | |
1039 | if (ifp) { | |
1040 | pim_ifp = (struct pim_interface *)ifp->info; | |
1041 | if (pim_ifp) | |
1042 | iif = pim_ifp->mroute_vif_index; | |
1043 | } | |
1044 | } | |
1045 | return iif; | |
1046 | } | |
1047 | ||
1048 | static int pim_upstream_mroute_update(struct channel_oil *c_oil, | |
1049 | const char *name) | |
1050 | { | |
1051 | char buf[1000]; | |
1052 | ||
1053 | if (c_oil->oil.mfcc_parent >= MAXVIFS) { | |
1054 | /* the c_oil cannot be installed as a mroute yet */ | |
1055 | if (PIM_DEBUG_MROUTE) | |
1056 | zlog_debug( | |
1057 | "%s(%s) %s mroute not ready to be installed; %s", | |
1058 | __func__, name, | |
1059 | pim_channel_oil_dump(c_oil, buf, | |
1060 | sizeof(buf)), | |
1061 | c_oil->installed ? | |
1062 | "uninstall" : "skip"); | |
1063 | /* if already installed flush it out as we are going to stop | |
1064 | * updates to it leaving it in a stale state | |
1065 | */ | |
1066 | if (c_oil->installed) | |
1067 | pim_mroute_del(c_oil, name); | |
1068 | /* return success (skipped) */ | |
1069 | return 0; | |
1070 | } | |
1071 | ||
1072 | return pim_mroute_add(c_oil, name); | |
1073 | } | |
1074 | ||
1075 | /* IIF associated with SGrpt entries are re-evaluated when the parent | |
1076 | * (*,G) entries IIF changes | |
1077 | */ | |
1078 | static void pim_upstream_all_sources_iif_update(struct pim_upstream *up) | |
1079 | { | |
1080 | struct listnode *listnode; | |
1081 | struct pim_upstream *child; | |
1082 | ||
1083 | for (ALL_LIST_ELEMENTS_RO(up->sources, listnode, | |
1084 | child)) { | |
1085 | if (PIM_UPSTREAM_FLAG_TEST_USE_RPT(child->flags)) | |
1086 | pim_upstream_mroute_iif_update(child->channel_oil, | |
1087 | __func__); | |
1088 | } | |
1089 | } | |
1090 | ||
1091 | /* In the case of "PIM state machine" added mroutes an upstream entry | |
1092 | * must be present to decide on the SPT-forwarding vs. RPT-forwarding. | |
1093 | */ | |
1094 | int pim_upstream_mroute_add(struct channel_oil *c_oil, const char *name) | |
1095 | { | |
1096 | vifi_t iif; | |
1097 | ||
1098 | iif = pim_upstream_get_mroute_iif(c_oil, name); | |
1099 | ||
1100 | if (c_oil->oil.mfcc_parent != iif) { | |
1101 | c_oil->oil.mfcc_parent = iif; | |
1102 | if (c_oil->oil.mfcc_origin.s_addr == INADDR_ANY && | |
1103 | c_oil->up) | |
1104 | pim_upstream_all_sources_iif_update(c_oil->up); | |
1105 | } else { | |
1106 | c_oil->oil.mfcc_parent = iif; | |
1107 | } | |
1108 | ||
1109 | return pim_upstream_mroute_update(c_oil, name); | |
1110 | } | |
1111 | ||
1112 | /* Look for IIF changes and update the dateplane entry only if the IIF | |
1113 | * has changed. | |
1114 | */ | |
1115 | int pim_upstream_mroute_iif_update(struct channel_oil *c_oil, const char *name) | |
1116 | { | |
1117 | vifi_t iif; | |
1118 | char buf[1000]; | |
1119 | ||
1120 | iif = pim_upstream_get_mroute_iif(c_oil, name); | |
1121 | if (c_oil->oil.mfcc_parent == iif) { | |
1122 | /* no change */ | |
1123 | return 0; | |
1124 | } | |
1125 | c_oil->oil.mfcc_parent = iif; | |
1126 | ||
1127 | if (c_oil->oil.mfcc_origin.s_addr == INADDR_ANY && | |
1128 | c_oil->up) | |
1129 | pim_upstream_all_sources_iif_update(c_oil->up); | |
1130 | ||
1131 | if (PIM_DEBUG_MROUTE_DETAIL) | |
1132 | zlog_debug("%s(%s) %s mroute iif update %d", | |
1133 | __func__, name, | |
1134 | pim_channel_oil_dump(c_oil, buf, | |
1135 | sizeof(buf)), iif); | |
1136 | /* XXX: is this hack needed? */ | |
1137 | c_oil->oil_inherited_rescan = 1; | |
1138 | return pim_upstream_mroute_update(c_oil, name); | |
1139 | } | |
1140 | ||
1141 | int pim_static_mroute_add(struct channel_oil *c_oil, const char *name) | |
1142 | { | |
1143 | return pim_mroute_add(c_oil, name); | |
1144 | } | |
1145 | ||
1146 | void pim_static_mroute_iif_update(struct channel_oil *c_oil, | |
1147 | int input_vif_index, | |
1148 | const char *name) | |
1149 | { | |
1150 | if (c_oil->oil.mfcc_parent == input_vif_index) | |
1151 | return; | |
1152 | ||
1153 | c_oil->oil.mfcc_parent = input_vif_index; | |
1154 | if (input_vif_index == MAXVIFS) | |
1155 | pim_mroute_del(c_oil, name); | |
1156 | else | |
1157 | pim_static_mroute_add(c_oil, name); | |
1158 | } | |
1159 | ||
1160 | int pim_mroute_del(struct channel_oil *c_oil, const char *name) | |
1161 | { | |
1162 | struct pim_instance *pim = c_oil->pim; | |
1163 | int err; | |
1164 | ||
1165 | pim->mroute_del_last = pim_time_monotonic_sec(); | |
1166 | ++pim->mroute_del_events; | |
1167 | ||
1168 | if (!c_oil->installed) { | |
1169 | if (PIM_DEBUG_MROUTE) { | |
1170 | char buf[1000]; | |
1171 | zlog_debug( | |
1172 | "%s %s: vifi %d for route is %s not installed, do not need to send del req. ", | |
1173 | __FILE__, __PRETTY_FUNCTION__, | |
1174 | c_oil->oil.mfcc_parent, | |
1175 | pim_channel_oil_dump(c_oil, buf, sizeof(buf))); | |
1176 | } | |
1177 | return -2; | |
1178 | } | |
1179 | ||
1180 | err = setsockopt(pim->mroute_socket, IPPROTO_IP, MRT_DEL_MFC, | |
1181 | &c_oil->oil, sizeof(c_oil->oil)); | |
1182 | if (err) { | |
1183 | if (PIM_DEBUG_MROUTE) | |
1184 | zlog_warn( | |
1185 | "%s %s: failure: setsockopt(fd=%d,IPPROTO_IP,MRT_DEL_MFC): errno=%d: %s", | |
1186 | __FILE__, __PRETTY_FUNCTION__, | |
1187 | pim->mroute_socket, errno, | |
1188 | safe_strerror(errno)); | |
1189 | return -2; | |
1190 | } | |
1191 | ||
1192 | if (PIM_DEBUG_MROUTE) { | |
1193 | char buf[1000]; | |
1194 | zlog_debug("%s(%s), vrf %s Deleted Route: %s", | |
1195 | __PRETTY_FUNCTION__, name, pim->vrf->name, | |
1196 | pim_channel_oil_dump(c_oil, buf, sizeof(buf))); | |
1197 | } | |
1198 | ||
1199 | // Reset kernel installed flag | |
1200 | c_oil->installed = 0; | |
1201 | ||
1202 | return 0; | |
1203 | } | |
1204 | ||
1205 | void pim_mroute_update_counters(struct channel_oil *c_oil) | |
1206 | { | |
1207 | struct pim_instance *pim = c_oil->pim; | |
1208 | struct sioc_sg_req sgreq; | |
1209 | ||
1210 | c_oil->cc.oldpktcnt = c_oil->cc.pktcnt; | |
1211 | c_oil->cc.oldbytecnt = c_oil->cc.bytecnt; | |
1212 | c_oil->cc.oldwrong_if = c_oil->cc.wrong_if; | |
1213 | ||
1214 | if (!c_oil->installed) { | |
1215 | c_oil->cc.lastused = 100 * pim->keep_alive_time; | |
1216 | if (PIM_DEBUG_MROUTE) { | |
1217 | struct prefix_sg sg; | |
1218 | ||
1219 | sg.src = c_oil->oil.mfcc_origin; | |
1220 | sg.grp = c_oil->oil.mfcc_mcastgrp; | |
1221 | if (PIM_DEBUG_MROUTE) | |
1222 | zlog_debug( | |
1223 | "Channel%s is not installed no need to collect data from kernel", | |
1224 | pim_str_sg_dump(&sg)); | |
1225 | } | |
1226 | return; | |
1227 | } | |
1228 | ||
1229 | memset(&sgreq, 0, sizeof(sgreq)); | |
1230 | sgreq.src = c_oil->oil.mfcc_origin; | |
1231 | sgreq.grp = c_oil->oil.mfcc_mcastgrp; | |
1232 | ||
1233 | pim_zlookup_sg_statistics(c_oil); | |
1234 | if (ioctl(pim->mroute_socket, SIOCGETSGCNT, &sgreq)) { | |
1235 | struct prefix_sg sg; | |
1236 | ||
1237 | sg.src = c_oil->oil.mfcc_origin; | |
1238 | sg.grp = c_oil->oil.mfcc_mcastgrp; | |
1239 | ||
1240 | zlog_warn("ioctl(SIOCGETSGCNT=%lu) failure for (S,G)=%s: errno=%d: %s", | |
1241 | (unsigned long)SIOCGETSGCNT, pim_str_sg_dump(&sg), | |
1242 | errno, safe_strerror(errno)); | |
1243 | return; | |
1244 | } | |
1245 | ||
1246 | c_oil->cc.pktcnt = sgreq.pktcnt; | |
1247 | c_oil->cc.bytecnt = sgreq.bytecnt; | |
1248 | c_oil->cc.wrong_if = sgreq.wrong_if; | |
1249 | ||
1250 | return; | |
1251 | } |