]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_zebra.c
Merge pull request #3042 from donaldsharp/pim_startup
[mirror_frr.git] / pimd / pim_zebra.c
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
22 #include "zebra/rib.h"
23
24 #include "if.h"
25 #include "log.h"
26 #include "prefix.h"
27 #include "zclient.h"
28 #include "stream.h"
29 #include "network.h"
30 #include "vty.h"
31 #include "plist.h"
32 #include "lib/bfd.h"
33
34 #include "pimd.h"
35 #include "pim_pim.h"
36 #include "pim_zebra.h"
37 #include "pim_iface.h"
38 #include "pim_str.h"
39 #include "pim_oil.h"
40 #include "pim_rpf.h"
41 #include "pim_time.h"
42 #include "pim_join.h"
43 #include "pim_zlookup.h"
44 #include "pim_ifchannel.h"
45 #include "pim_rp.h"
46 #include "pim_igmpv3.h"
47 #include "pim_jp_agg.h"
48 #include "pim_nht.h"
49 #include "pim_ssm.h"
50
51 #undef PIM_DEBUG_IFADDR_DUMP
52 #define PIM_DEBUG_IFADDR_DUMP
53
54 static struct zclient *zclient = NULL;
55
56
57 /* Router-id update message from zebra. */
58 static int pim_router_id_update_zebra(int command, struct zclient *zclient,
59 zebra_size_t length, vrf_id_t vrf_id)
60 {
61 struct prefix router_id;
62
63 zebra_router_id_update_read(zclient->ibuf, &router_id);
64
65 return 0;
66 }
67
68 static int pim_zebra_if_add(int command, struct zclient *zclient,
69 zebra_size_t length, vrf_id_t vrf_id)
70 {
71 struct interface *ifp;
72 struct pim_instance *pim;
73
74 /*
75 zebra api adds/dels interfaces using the same call
76 interface_add_read below, see comments in lib/zclient.c
77 */
78 ifp = zebra_interface_add_read(zclient->ibuf, vrf_id);
79 if (!ifp)
80 return 0;
81
82 pim = pim_get_pim_instance(vrf_id);
83 if (PIM_DEBUG_ZEBRA) {
84 zlog_debug(
85 "%s: %s index %d(%u) flags %ld metric %d mtu %d operative %d",
86 __PRETTY_FUNCTION__, ifp->name, ifp->ifindex, vrf_id,
87 (long)ifp->flags, ifp->metric, ifp->mtu,
88 if_is_operative(ifp));
89 }
90
91 if (if_is_operative(ifp)) {
92 struct pim_interface *pim_ifp;
93
94 pim_ifp = ifp->info;
95 /*
96 * If we have a pim_ifp already and this is an if_add
97 * that means that we probably have a vrf move event
98 * If that is the case, set the proper vrfness.
99 */
100 if (pim_ifp)
101 pim_ifp->pim = pim;
102 pim_if_addr_add_all(ifp);
103 }
104
105 /*
106 * If we are a vrf device that is up, open up the pim_socket for
107 * listening
108 * to incoming pim messages irrelevant if the user has configured us
109 * for pim or not.
110 */
111 if (pim_if_is_vrf_device(ifp)) {
112 struct pim_interface *pim_ifp;
113
114 if (!ifp->info) {
115 pim_ifp = pim_if_new(ifp, false, false, false);
116 ifp->info = pim_ifp;
117 }
118
119 pim_sock_add(ifp);
120 }
121
122 return 0;
123 }
124
125 static int pim_zebra_if_del(int command, struct zclient *zclient,
126 zebra_size_t length, vrf_id_t vrf_id)
127 {
128 struct interface *ifp;
129
130 /*
131 zebra api adds/dels interfaces using the same call
132 interface_add_read below, see comments in lib/zclient.c
133
134 comments in lib/zclient.c seem to indicate that calling
135 zebra_interface_add_read is the correct call, but that
136 results in an attemted out of bounds read which causes
137 pimd to assert. Other clients use zebra_interface_state_read
138 and it appears to work just fine.
139 */
140 ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
141 if (!ifp)
142 return 0;
143
144 if (PIM_DEBUG_ZEBRA) {
145 zlog_debug(
146 "%s: %s index %d(%u) flags %ld metric %d mtu %d operative %d",
147 __PRETTY_FUNCTION__, ifp->name, ifp->ifindex, vrf_id,
148 (long)ifp->flags, ifp->metric, ifp->mtu,
149 if_is_operative(ifp));
150 }
151
152 if (!if_is_operative(ifp))
153 pim_if_addr_del_all(ifp);
154
155 return 0;
156 }
157
158 static int pim_zebra_if_state_up(int command, struct zclient *zclient,
159 zebra_size_t length, vrf_id_t vrf_id)
160 {
161 struct pim_instance *pim;
162 struct interface *ifp;
163 uint32_t table_id;
164
165 /*
166 zebra api notifies interface up/down events by using the same call
167 zebra_interface_state_read below, see comments in lib/zclient.c
168 */
169 ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
170 if (!ifp)
171 return 0;
172
173 if (PIM_DEBUG_ZEBRA) {
174 zlog_debug(
175 "%s: %s index %d(%u) flags %ld metric %d mtu %d operative %d",
176 __PRETTY_FUNCTION__, ifp->name, ifp->ifindex, vrf_id,
177 (long)ifp->flags, ifp->metric, ifp->mtu,
178 if_is_operative(ifp));
179 }
180
181 pim = pim_get_pim_instance(vrf_id);
182 if (if_is_operative(ifp)) {
183 struct pim_interface *pim_ifp;
184
185 pim_ifp = ifp->info;
186 /*
187 * If we have a pim_ifp already and this is an if_add
188 * that means that we probably have a vrf move event
189 * If that is the case, set the proper vrfness.
190 */
191 if (pim_ifp)
192 pim_ifp->pim = pim;
193
194 /*
195 pim_if_addr_add_all() suffices for bringing up both IGMP and
196 PIM
197 */
198 pim_if_addr_add_all(ifp);
199 }
200
201 /*
202 * If we have a pimreg device callback and it's for a specific
203 * table set the master appropriately
204 */
205 if (sscanf(ifp->name, "pimreg%" SCNu32, &table_id) == 1) {
206 struct vrf *vrf;
207 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
208 if ((table_id == vrf->data.l.table_id)
209 && (ifp->vrf_id != vrf->vrf_id)) {
210 struct interface *master = if_lookup_by_name(
211 vrf->name, vrf->vrf_id);
212
213 if (!master) {
214 zlog_debug(
215 "%s: Unable to find Master interface for %s",
216 __PRETTY_FUNCTION__, vrf->name);
217 return 0;
218 }
219 zclient_interface_set_master(zclient, master,
220 ifp);
221 }
222 }
223 }
224 return 0;
225 }
226
227 static int pim_zebra_if_state_down(int command, struct zclient *zclient,
228 zebra_size_t length, vrf_id_t vrf_id)
229 {
230 struct interface *ifp;
231
232 /*
233 zebra api notifies interface up/down events by using the same call
234 zebra_interface_state_read below, see comments in lib/zclient.c
235 */
236 ifp = zebra_interface_state_read(zclient->ibuf, vrf_id);
237 if (!ifp)
238 return 0;
239
240 if (PIM_DEBUG_ZEBRA) {
241 zlog_debug(
242 "%s: %s index %d(%u) flags %ld metric %d mtu %d operative %d",
243 __PRETTY_FUNCTION__, ifp->name, ifp->ifindex, vrf_id,
244 (long)ifp->flags, ifp->metric, ifp->mtu,
245 if_is_operative(ifp));
246 }
247
248 if (!if_is_operative(ifp)) {
249 pim_ifchannel_delete_all(ifp);
250 /*
251 pim_if_addr_del_all() suffices for shutting down IGMP,
252 but not for shutting down PIM
253 */
254 pim_if_addr_del_all(ifp);
255
256 /*
257 pim_sock_delete() closes the socket, stops read and timer
258 threads,
259 and kills all neighbors.
260 */
261 if (ifp->info) {
262 pim_sock_delete(ifp, "link down");
263 }
264 }
265
266 if (ifp->info)
267 pim_if_del_vif(ifp);
268
269 return 0;
270 }
271
272 #ifdef PIM_DEBUG_IFADDR_DUMP
273 static void dump_if_address(struct interface *ifp)
274 {
275 struct connected *ifc;
276 struct listnode *node;
277
278 zlog_debug("%s %s: interface %s addresses:", __FILE__,
279 __PRETTY_FUNCTION__, ifp->name);
280
281 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
282 struct prefix *p = ifc->address;
283
284 if (p->family != AF_INET)
285 continue;
286
287 zlog_debug("%s %s: interface %s address %s %s", __FILE__,
288 __PRETTY_FUNCTION__, ifp->name,
289 inet_ntoa(p->u.prefix4),
290 CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY)
291 ? "secondary"
292 : "primary");
293 }
294 }
295 #endif
296
297 static int pim_zebra_if_address_add(int command, struct zclient *zclient,
298 zebra_size_t length, vrf_id_t vrf_id)
299 {
300 struct connected *c;
301 struct prefix *p;
302 struct pim_interface *pim_ifp;
303 struct pim_instance *pim;
304
305 /*
306 zebra api notifies address adds/dels events by using the same call
307 interface_add_read below, see comments in lib/zclient.c
308
309 zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD, ...)
310 will add address to interface list by calling
311 connected_add_by_prefix()
312 */
313 c = zebra_interface_address_read(command, zclient->ibuf, vrf_id);
314 if (!c)
315 return 0;
316
317 pim_ifp = c->ifp->info;
318 p = c->address;
319
320 if (PIM_DEBUG_ZEBRA) {
321 char buf[BUFSIZ];
322 prefix2str(p, buf, BUFSIZ);
323 zlog_debug("%s: %s(%u) connected IP address %s flags %u %s",
324 __PRETTY_FUNCTION__, c->ifp->name, vrf_id, buf,
325 c->flags,
326 CHECK_FLAG(c->flags, ZEBRA_IFA_SECONDARY)
327 ? "secondary"
328 : "primary");
329
330 #ifdef PIM_DEBUG_IFADDR_DUMP
331 dump_if_address(c->ifp);
332 #endif
333 }
334
335 if (!CHECK_FLAG(c->flags, ZEBRA_IFA_SECONDARY)) {
336 /* trying to add primary address */
337
338 struct in_addr primary_addr = pim_find_primary_addr(c->ifp);
339 if (p->family != AF_INET
340 || primary_addr.s_addr != p->u.prefix4.s_addr) {
341 if (PIM_DEBUG_ZEBRA) {
342 /* but we had a primary address already */
343
344 char buf[BUFSIZ];
345
346 prefix2str(p, buf, BUFSIZ);
347
348 zlog_warn(
349 "%s: %s : forcing secondary flag on %s",
350 __PRETTY_FUNCTION__, c->ifp->name, buf);
351 }
352 SET_FLAG(c->flags, ZEBRA_IFA_SECONDARY);
353 }
354 }
355
356 pim_if_addr_add(c);
357 if (pim_ifp) {
358 pim = pim_get_pim_instance(vrf_id);
359 pim_ifp->pim = pim;
360
361 pim_rp_check_on_if_add(pim_ifp);
362 }
363
364 if (if_is_loopback(c->ifp)) {
365 struct vrf *vrf = vrf_lookup_by_id(VRF_DEFAULT);
366 struct interface *ifp;
367
368 FOR_ALL_INTERFACES (vrf, ifp) {
369 if (!if_is_loopback(ifp) && if_is_operative(ifp))
370 pim_if_addr_add_all(ifp);
371 }
372 }
373
374 return 0;
375 }
376
377 static int pim_zebra_if_address_del(int command, struct zclient *client,
378 zebra_size_t length, vrf_id_t vrf_id)
379 {
380 struct connected *c;
381 struct prefix *p;
382 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
383 struct pim_instance *pim;
384
385 if (!vrf)
386 return 0;
387 pim = vrf->info;
388
389 /*
390 zebra api notifies address adds/dels events by using the same call
391 interface_add_read below, see comments in lib/zclient.c
392
393 zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE, ...)
394 will remove address from interface list by calling
395 connected_delete_by_prefix()
396 */
397 c = zebra_interface_address_read(command, client->ibuf, vrf_id);
398 if (!c)
399 return 0;
400
401 p = c->address;
402 if (p->family == AF_INET) {
403 if (PIM_DEBUG_ZEBRA) {
404 char buf[BUFSIZ];
405 prefix2str(p, buf, BUFSIZ);
406 zlog_debug(
407 "%s: %s(%u) disconnected IP address %s flags %u %s",
408 __PRETTY_FUNCTION__, c->ifp->name, vrf_id, buf,
409 c->flags,
410 CHECK_FLAG(c->flags, ZEBRA_IFA_SECONDARY)
411 ? "secondary"
412 : "primary");
413
414 #ifdef PIM_DEBUG_IFADDR_DUMP
415 dump_if_address(c->ifp);
416 #endif
417 }
418
419 pim_if_addr_del(c, 0);
420 pim_rp_setup(pim);
421 pim_i_am_rp_re_evaluate(pim);
422 }
423
424 connected_free(c);
425 return 0;
426 }
427
428 void pim_zebra_update_all_interfaces(struct pim_instance *pim)
429 {
430 struct interface *ifp;
431
432 FOR_ALL_INTERFACES (pim->vrf, ifp) {
433 struct pim_interface *pim_ifp = ifp->info;
434 struct pim_iface_upstream_switch *us;
435 struct listnode *node;
436
437 if (!pim_ifp)
438 continue;
439
440 for (ALL_LIST_ELEMENTS_RO(pim_ifp->upstream_switch_list, node,
441 us)) {
442 struct pim_rpf rpf;
443
444 rpf.source_nexthop.interface = ifp;
445 rpf.rpf_addr.u.prefix4 = us->address;
446 pim_joinprune_send(&rpf, us->us);
447 pim_jp_agg_clear_group(us->us);
448 }
449 }
450 }
451
452 void pim_zebra_upstream_rpf_changed(struct pim_instance *pim,
453 struct pim_upstream *up,
454 struct pim_rpf *old)
455 {
456 struct pim_neighbor *nbr;
457
458 nbr = pim_neighbor_find(old->source_nexthop.interface,
459 old->rpf_addr.u.prefix4);
460 if (nbr)
461 pim_jp_agg_remove_group(nbr->upstream_jp_agg, up);
462
463 /*
464 * We have detected a case where we might need
465 * to rescan the inherited o_list so do it.
466 */
467 if (up->channel_oil->oil_inherited_rescan) {
468 pim_upstream_inherited_olist_decide(pim, up);
469 up->channel_oil->oil_inherited_rescan = 0;
470 }
471
472 if (up->join_state == PIM_UPSTREAM_JOINED) {
473 /*
474 * If we come up real fast we can be here
475 * where the mroute has not been installed
476 * so install it.
477 */
478 if (!up->channel_oil->installed)
479 pim_mroute_add(up->channel_oil, __PRETTY_FUNCTION__);
480
481 /*
482 * RFC 4601: 4.5.7. Sending (S,G)
483 * Join/Prune Messages
484 *
485 * Transitions from Joined State
486 *
487 * RPF'(S,G) changes not due to an Assert
488 *
489 * The upstream (S,G) state machine remains
490 * in Joined state. Send Join(S,G) to the new
491 * upstream neighbor, which is the new value
492 * of RPF'(S,G). Send Prune(S,G) to the old
493 * upstream neighbor, which is the old value
494 * of RPF'(S,G). Set the Join Timer (JT) to
495 * expire after t_periodic seconds.
496 */
497 pim_jp_agg_switch_interface(old, &up->rpf, up);
498
499 pim_upstream_join_timer_restart(up, old);
500 } /* up->join_state == PIM_UPSTREAM_JOINED */
501
502 /* FIXME can join_desired actually be changed by
503 pim_rpf_update()
504 returning PIM_RPF_CHANGED ? */
505 pim_upstream_update_join_desired(pim, up);
506 }
507
508 static void scan_upstream_rpf_cache(struct pim_instance *pim)
509 {
510 struct listnode *up_node;
511 struct listnode *up_nextnode;
512 struct pim_upstream *up;
513
514 for (ALL_LIST_ELEMENTS(pim->upstream_list, up_node, up_nextnode, up)) {
515 enum pim_rpf_result rpf_result;
516 struct pim_rpf old;
517 struct prefix nht_p;
518
519 nht_p.family = AF_INET;
520 nht_p.prefixlen = IPV4_MAX_BITLEN;
521 nht_p.u.prefix4.s_addr = up->upstream_addr.s_addr;
522 pim_resolve_upstream_nh(pim, &nht_p);
523
524 old.source_nexthop.interface = up->rpf.source_nexthop.interface;
525 old.source_nexthop.nbr = up->rpf.source_nexthop.nbr;
526 rpf_result = pim_rpf_update(pim, up, &old, 0);
527
528 if (rpf_result == PIM_RPF_FAILURE)
529 continue;
530
531 if (rpf_result == PIM_RPF_CHANGED)
532 pim_zebra_upstream_rpf_changed(pim, up, &old);
533
534 } /* for (qpim_upstream_list) */
535
536 pim_zebra_update_all_interfaces(pim);
537 }
538
539 void pim_scan_individual_oil(struct channel_oil *c_oil, int in_vif_index)
540 {
541 struct in_addr vif_source;
542 int input_iface_vif_index;
543 int old_vif_index;
544
545 if (!pim_rp_set_upstream_addr(c_oil->pim, &vif_source,
546 c_oil->oil.mfcc_origin,
547 c_oil->oil.mfcc_mcastgrp))
548 return;
549
550 if (in_vif_index)
551 input_iface_vif_index = in_vif_index;
552 else {
553 struct prefix src, grp;
554
555 src.family = AF_INET;
556 src.prefixlen = IPV4_MAX_BITLEN;
557 src.u.prefix4 = vif_source;
558 grp.family = AF_INET;
559 grp.prefixlen = IPV4_MAX_BITLEN;
560 grp.u.prefix4 = c_oil->oil.mfcc_mcastgrp;
561
562 if (PIM_DEBUG_ZEBRA) {
563 char source_str[INET_ADDRSTRLEN];
564 char group_str[INET_ADDRSTRLEN];
565 pim_inet4_dump("<source?>", c_oil->oil.mfcc_origin,
566 source_str, sizeof(source_str));
567 pim_inet4_dump("<group?>", c_oil->oil.mfcc_mcastgrp,
568 group_str, sizeof(group_str));
569 zlog_debug(
570 "%s: channel_oil (%s,%s) upstream info is not present.",
571 __PRETTY_FUNCTION__, source_str, group_str);
572 }
573 input_iface_vif_index = pim_ecmp_fib_lookup_if_vif_index(
574 c_oil->pim, &src, &grp);
575 }
576
577 if (input_iface_vif_index < 1) {
578 if (PIM_DEBUG_ZEBRA) {
579 char source_str[INET_ADDRSTRLEN];
580 char group_str[INET_ADDRSTRLEN];
581 pim_inet4_dump("<source?>", c_oil->oil.mfcc_origin,
582 source_str, sizeof(source_str));
583 pim_inet4_dump("<group?>", c_oil->oil.mfcc_mcastgrp,
584 group_str, sizeof(group_str));
585 zlog_debug(
586 "%s %s: could not find input interface(%d) for (S,G)=(%s,%s)",
587 __FILE__, __PRETTY_FUNCTION__,
588 c_oil->oil.mfcc_parent, source_str, group_str);
589 }
590 pim_mroute_del(c_oil, __PRETTY_FUNCTION__);
591 return;
592 }
593
594 if (input_iface_vif_index == c_oil->oil.mfcc_parent) {
595 if (!c_oil->installed)
596 pim_mroute_add(c_oil, __PRETTY_FUNCTION__);
597
598 /* RPF unchanged */
599 return;
600 }
601
602 if (PIM_DEBUG_ZEBRA) {
603 struct interface *old_iif = pim_if_find_by_vif_index(
604 c_oil->pim, c_oil->oil.mfcc_parent);
605 struct interface *new_iif = pim_if_find_by_vif_index(
606 c_oil->pim, input_iface_vif_index);
607 char source_str[INET_ADDRSTRLEN];
608 char group_str[INET_ADDRSTRLEN];
609 pim_inet4_dump("<source?>", c_oil->oil.mfcc_origin, source_str,
610 sizeof(source_str));
611 pim_inet4_dump("<group?>", c_oil->oil.mfcc_mcastgrp, group_str,
612 sizeof(group_str));
613 zlog_debug(
614 "%s %s: (S,G)=(%s,%s) input interface changed from %s vif_index=%d to %s vif_index=%d",
615 __FILE__, __PRETTY_FUNCTION__, source_str, group_str,
616 (old_iif) ? old_iif->name : "<old_iif?>",
617 c_oil->oil.mfcc_parent,
618 (new_iif) ? new_iif->name : "<new_iif?>",
619 input_iface_vif_index);
620 }
621
622 /* new iif loops to existing oif ? */
623 if (c_oil->oil.mfcc_ttls[input_iface_vif_index]) {
624 struct interface *new_iif = pim_if_find_by_vif_index(
625 c_oil->pim, input_iface_vif_index);
626
627 if (PIM_DEBUG_ZEBRA) {
628 char source_str[INET_ADDRSTRLEN];
629 char group_str[INET_ADDRSTRLEN];
630 pim_inet4_dump("<source?>", c_oil->oil.mfcc_origin,
631 source_str, sizeof(source_str));
632 pim_inet4_dump("<group?>", c_oil->oil.mfcc_mcastgrp,
633 group_str, sizeof(group_str));
634 zlog_debug(
635 "%s %s: (S,G)=(%s,%s) new iif loops to existing oif: %s vif_index=%d",
636 __FILE__, __PRETTY_FUNCTION__, source_str,
637 group_str,
638 (new_iif) ? new_iif->name : "<new_iif?>",
639 input_iface_vif_index);
640 }
641 }
642
643 /* update iif vif_index */
644 old_vif_index = c_oil->oil.mfcc_parent;
645 c_oil->oil.mfcc_parent = input_iface_vif_index;
646
647 /* update kernel multicast forwarding cache (MFC) */
648 if (pim_mroute_add(c_oil, __PRETTY_FUNCTION__)) {
649 if (PIM_DEBUG_MROUTE) {
650 /* just log warning */
651 struct interface *old_iif = pim_if_find_by_vif_index(
652 c_oil->pim, old_vif_index);
653 struct interface *new_iif = pim_if_find_by_vif_index(
654 c_oil->pim, input_iface_vif_index);
655 char source_str[INET_ADDRSTRLEN];
656 char group_str[INET_ADDRSTRLEN];
657 pim_inet4_dump("<source?>", c_oil->oil.mfcc_origin,
658 source_str, sizeof(source_str));
659 pim_inet4_dump("<group?>", c_oil->oil.mfcc_mcastgrp,
660 group_str, sizeof(group_str));
661 zlog_debug(
662 "%s %s: (S,G)=(%s,%s) failure updating input interface from %s vif_index=%d to %s vif_index=%d",
663 __FILE__, __PRETTY_FUNCTION__, source_str,
664 group_str,
665 old_iif ? old_iif->name : "<old_iif?>",
666 c_oil->oil.mfcc_parent,
667 new_iif ? new_iif->name : "<new_iif?>",
668 input_iface_vif_index);
669 }
670 }
671 }
672
673 void pim_scan_oil(struct pim_instance *pim)
674 {
675 struct listnode *node;
676 struct listnode *nextnode;
677 struct channel_oil *c_oil;
678 ifindex_t ifindex;
679 int vif_index = 0;
680
681 pim->scan_oil_last = pim_time_monotonic_sec();
682 ++pim->scan_oil_events;
683
684 for (ALL_LIST_ELEMENTS(pim->channel_oil_list, node, nextnode, c_oil)) {
685 if (c_oil->up && c_oil->up->rpf.source_nexthop.interface) {
686 ifindex = c_oil->up->rpf.source_nexthop
687 .interface->ifindex;
688 vif_index =
689 pim_if_find_vifindex_by_ifindex(pim, ifindex);
690 /* Pass Current selected NH vif index to mroute
691 * download */
692 if (vif_index)
693 pim_scan_individual_oil(c_oil, vif_index);
694 } else
695 pim_scan_individual_oil(c_oil, 0);
696 }
697 }
698
699 static int on_rpf_cache_refresh(struct thread *t)
700 {
701 struct pim_instance *pim = THREAD_ARG(t);
702
703 /* update PIM protocol state */
704 scan_upstream_rpf_cache(pim);
705
706 /* update kernel multicast forwarding cache (MFC) */
707 pim_scan_oil(pim);
708
709 pim->rpf_cache_refresh_last = pim_time_monotonic_sec();
710 ++pim->rpf_cache_refresh_events;
711
712 // It is called as part of pim_neighbor_add
713 // pim_rp_setup ();
714 return 0;
715 }
716
717 void sched_rpf_cache_refresh(struct pim_instance *pim)
718 {
719 ++pim->rpf_cache_refresh_requests;
720
721 pim_rpf_set_refresh_time(pim);
722
723 if (pim->rpf_cache_refresher) {
724 /* Refresh timer is already running */
725 return;
726 }
727
728 /* Start refresh timer */
729
730 if (PIM_DEBUG_ZEBRA) {
731 zlog_debug("%s: triggering %ld msec timer", __PRETTY_FUNCTION__,
732 qpim_rpf_cache_refresh_delay_msec);
733 }
734
735 thread_add_timer_msec(master, on_rpf_cache_refresh, pim,
736 qpim_rpf_cache_refresh_delay_msec,
737 &pim->rpf_cache_refresher);
738 }
739
740 static void pim_zebra_connected(struct zclient *zclient)
741 {
742 /* Send the client registration */
743 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER);
744
745 zclient_send_reg_requests(zclient, pimg->vrf_id);
746 }
747
748 void pim_zebra_init(void)
749 {
750 /* Socket for receiving updates from Zebra daemon */
751 zclient = zclient_new_notify(master, &zclient_options_default);
752
753 zclient->zebra_connected = pim_zebra_connected;
754 zclient->router_id_update = pim_router_id_update_zebra;
755 zclient->interface_add = pim_zebra_if_add;
756 zclient->interface_delete = pim_zebra_if_del;
757 zclient->interface_up = pim_zebra_if_state_up;
758 zclient->interface_down = pim_zebra_if_state_down;
759 zclient->interface_address_add = pim_zebra_if_address_add;
760 zclient->interface_address_delete = pim_zebra_if_address_del;
761 zclient->nexthop_update = pim_parse_nexthop_update;
762
763 zclient_init(zclient, ZEBRA_ROUTE_PIM, 0, &pimd_privs);
764 if (PIM_DEBUG_PIM_TRACE) {
765 zlog_notice("%s: zclient socket initialized",
766 __PRETTY_FUNCTION__);
767 }
768
769 zclient_lookup_new();
770 }
771
772 void igmp_anysource_forward_start(struct pim_instance *pim,
773 struct igmp_group *group)
774 {
775 struct igmp_source *source;
776 struct in_addr src_addr = {.s_addr = 0};
777 /* Any source (*,G) is forwarded only if mode is EXCLUDE {empty} */
778 zassert(group->group_filtermode_isexcl);
779 zassert(listcount(group->group_source_list) < 1);
780
781 source = source_new(group, src_addr);
782 if (!source) {
783 zlog_warn("%s: Failure to create * source",
784 __PRETTY_FUNCTION__);
785 return;
786 }
787
788 igmp_source_forward_start(pim, source);
789 }
790
791 void igmp_anysource_forward_stop(struct igmp_group *group)
792 {
793 struct igmp_source *source;
794 struct in_addr star = {.s_addr = 0};
795
796 source = igmp_find_source_by_addr(group, star);
797 if (source)
798 igmp_source_forward_stop(source);
799 }
800
801 static void igmp_source_forward_reevaluate_one(struct pim_instance *pim,
802 struct igmp_source *source)
803 {
804 struct prefix_sg sg;
805 struct igmp_group *group = source->source_group;
806 struct pim_ifchannel *ch;
807
808 if ((source->source_addr.s_addr != INADDR_ANY)
809 || !IGMP_SOURCE_TEST_FORWARDING(source->source_flags))
810 return;
811
812 memset(&sg, 0, sizeof(struct prefix_sg));
813 sg.src = source->source_addr;
814 sg.grp = group->group_addr;
815
816 ch = pim_ifchannel_find(group->group_igmp_sock->interface, &sg);
817 if (pim_is_grp_ssm(pim, group->group_addr)) {
818 /* If SSM group withdraw local membership */
819 if (ch
820 && (ch->local_ifmembership == PIM_IFMEMBERSHIP_INCLUDE)) {
821 if (PIM_DEBUG_PIM_EVENTS)
822 zlog_debug(
823 "local membership del for %s as G is now SSM",
824 pim_str_sg_dump(&sg));
825 pim_ifchannel_local_membership_del(
826 group->group_igmp_sock->interface, &sg);
827 }
828 } else {
829 /* If ASM group add local membership */
830 if (!ch
831 || (ch->local_ifmembership == PIM_IFMEMBERSHIP_NOINFO)) {
832 if (PIM_DEBUG_PIM_EVENTS)
833 zlog_debug(
834 "local membership add for %s as G is now ASM",
835 pim_str_sg_dump(&sg));
836 pim_ifchannel_local_membership_add(
837 group->group_igmp_sock->interface, &sg);
838 }
839 }
840 }
841
842 void igmp_source_forward_reevaluate_all(struct pim_instance *pim)
843 {
844 struct interface *ifp;
845
846 FOR_ALL_INTERFACES (pim->vrf, ifp) {
847 struct pim_interface *pim_ifp = ifp->info;
848 struct listnode *sock_node;
849 struct igmp_sock *igmp;
850
851 if (!pim_ifp)
852 continue;
853
854 /* scan igmp sockets */
855 for (ALL_LIST_ELEMENTS_RO(pim_ifp->igmp_socket_list, sock_node,
856 igmp)) {
857 struct listnode *grpnode;
858 struct igmp_group *grp;
859
860 /* scan igmp groups */
861 for (ALL_LIST_ELEMENTS_RO(igmp->igmp_group_list,
862 grpnode, grp)) {
863 struct listnode *srcnode;
864 struct igmp_source *src;
865
866 /* scan group sources */
867 for (ALL_LIST_ELEMENTS_RO(
868 grp->group_source_list, srcnode,
869 src)) {
870 igmp_source_forward_reevaluate_one(pim,
871 src);
872 } /* scan group sources */
873 } /* scan igmp groups */
874 } /* scan igmp sockets */
875 } /* scan interfaces */
876 }
877
878 void igmp_source_forward_start(struct pim_instance *pim,
879 struct igmp_source *source)
880 {
881 struct pim_interface *pim_oif;
882 struct igmp_group *group;
883 struct prefix_sg sg;
884 int result;
885 int input_iface_vif_index = 0;
886
887 memset(&sg, 0, sizeof(struct prefix_sg));
888 sg.src = source->source_addr;
889 sg.grp = source->source_group->group_addr;
890
891 if (PIM_DEBUG_IGMP_TRACE) {
892 zlog_debug(
893 "%s: (S,G)=%s igmp_sock=%d oif=%s fwd=%d",
894 __PRETTY_FUNCTION__, pim_str_sg_dump(&sg),
895 source->source_group->group_igmp_sock->fd,
896 source->source_group->group_igmp_sock->interface->name,
897 IGMP_SOURCE_TEST_FORWARDING(source->source_flags));
898 }
899
900 /* Prevent IGMP interface from installing multicast route multiple
901 times */
902 if (IGMP_SOURCE_TEST_FORWARDING(source->source_flags)) {
903 return;
904 }
905
906 group = source->source_group;
907 pim_oif = group->group_igmp_sock->interface->info;
908 if (!pim_oif) {
909 if (PIM_DEBUG_IGMP_TRACE) {
910 zlog_debug(
911 "%s: multicast not enabled on oif=%s ?",
912 __PRETTY_FUNCTION__,
913 source->source_group->group_igmp_sock
914 ->interface->name);
915 }
916 return;
917 }
918
919 if (!source->source_channel_oil) {
920 struct in_addr vif_source;
921 struct prefix nht_p, src, grp;
922 struct pim_nexthop_cache out_pnc;
923 struct pim_nexthop nexthop;
924 struct pim_upstream *up = NULL;
925
926 if (!pim_rp_set_upstream_addr(pim, &vif_source,
927 source->source_addr, sg.grp))
928 return;
929
930 /* Register addr with Zebra NHT */
931 nht_p.family = AF_INET;
932 nht_p.prefixlen = IPV4_MAX_BITLEN;
933 nht_p.u.prefix4 = vif_source;
934 memset(&out_pnc, 0, sizeof(struct pim_nexthop_cache));
935
936 src.family = AF_INET;
937 src.prefixlen = IPV4_MAX_BITLEN;
938 src.u.prefix4 = vif_source; // RP or Src address
939 grp.family = AF_INET;
940 grp.prefixlen = IPV4_MAX_BITLEN;
941 grp.u.prefix4 = sg.grp;
942
943 if (pim_find_or_track_nexthop(pim, &nht_p, NULL, NULL,
944 &out_pnc)) {
945 if (out_pnc.nexthop_num) {
946 up = pim_upstream_find(pim, &sg);
947 memset(&nexthop, 0, sizeof(nexthop));
948 if (up)
949 memcpy(&nexthop,
950 &up->rpf.source_nexthop,
951 sizeof(struct pim_nexthop));
952 pim_ecmp_nexthop_search(pim, &out_pnc, &nexthop,
953 &src, &grp, 0);
954 if (nexthop.interface)
955 input_iface_vif_index =
956 pim_if_find_vifindex_by_ifindex(
957 pim,
958 nexthop.interface->ifindex);
959 } else {
960 if (PIM_DEBUG_ZEBRA) {
961 char buf1[INET_ADDRSTRLEN];
962 char buf2[INET_ADDRSTRLEN];
963 pim_inet4_dump("<source?>",
964 nht_p.u.prefix4, buf1,
965 sizeof(buf1));
966 pim_inet4_dump("<source?>",
967 grp.u.prefix4, buf2,
968 sizeof(buf2));
969 zlog_debug(
970 "%s: NHT Nexthop not found for addr %s grp %s",
971 __PRETTY_FUNCTION__, buf1,
972 buf2);
973 }
974 }
975 } else
976 input_iface_vif_index =
977 pim_ecmp_fib_lookup_if_vif_index(pim, &src,
978 &grp);
979
980 if (PIM_DEBUG_ZEBRA) {
981 char buf2[INET_ADDRSTRLEN];
982 pim_inet4_dump("<source?>", vif_source, buf2,
983 sizeof(buf2));
984 zlog_debug("%s: NHT %s vif_source %s vif_index:%d ",
985 __PRETTY_FUNCTION__, pim_str_sg_dump(&sg),
986 buf2, input_iface_vif_index);
987 }
988
989 if (input_iface_vif_index < 1) {
990 if (PIM_DEBUG_IGMP_TRACE) {
991 char source_str[INET_ADDRSTRLEN];
992 pim_inet4_dump("<source?>", source->source_addr,
993 source_str, sizeof(source_str));
994 zlog_debug(
995 "%s %s: could not find input interface for source %s",
996 __FILE__, __PRETTY_FUNCTION__,
997 source_str);
998 }
999 return;
1000 }
1001
1002 /*
1003 Protect IGMP against adding looped MFC entries created by both
1004 source and receiver attached to the same interface. See TODO
1005 T22.
1006 */
1007 if (input_iface_vif_index == pim_oif->mroute_vif_index) {
1008 /* ignore request for looped MFC entry */
1009 if (PIM_DEBUG_IGMP_TRACE) {
1010 zlog_debug(
1011 "%s: ignoring request for looped MFC entry (S,G)=%s: igmp_sock=%d oif=%s vif_index=%d",
1012 __PRETTY_FUNCTION__,
1013 pim_str_sg_dump(&sg),
1014 source->source_group->group_igmp_sock
1015 ->fd,
1016 source->source_group->group_igmp_sock
1017 ->interface->name,
1018 input_iface_vif_index);
1019 }
1020 return;
1021 }
1022
1023 source->source_channel_oil =
1024 pim_channel_oil_add(pim, &sg, input_iface_vif_index);
1025 if (!source->source_channel_oil) {
1026 if (PIM_DEBUG_IGMP_TRACE) {
1027 zlog_debug(
1028 "%s %s: could not create OIL for channel (S,G)=%s",
1029 __FILE__, __PRETTY_FUNCTION__,
1030 pim_str_sg_dump(&sg));
1031 }
1032 return;
1033 }
1034 }
1035
1036 result = pim_channel_add_oif(source->source_channel_oil,
1037 group->group_igmp_sock->interface,
1038 PIM_OIF_FLAG_PROTO_IGMP);
1039 if (result) {
1040 if (PIM_DEBUG_MROUTE) {
1041 zlog_warn("%s: add_oif() failed with return=%d",
1042 __func__, result);
1043 }
1044 return;
1045 }
1046
1047 if (!(PIM_I_am_DR(pim_oif)))
1048 return;
1049
1050 /*
1051 Feed IGMPv3-gathered local membership information into PIM
1052 per-interface (S,G) state.
1053 */
1054 if (!pim_ifchannel_local_membership_add(
1055 group->group_igmp_sock->interface, &sg)) {
1056 if (PIM_DEBUG_MROUTE)
1057 zlog_warn("%s: Failure to add local membership for %s",
1058 __PRETTY_FUNCTION__, pim_str_sg_dump(&sg));
1059 return;
1060 }
1061
1062 IGMP_SOURCE_DO_FORWARDING(source->source_flags);
1063 }
1064
1065 /*
1066 igmp_source_forward_stop: stop fowarding, but keep the source
1067 igmp_source_delete: stop fowarding, and delete the source
1068 */
1069 void igmp_source_forward_stop(struct igmp_source *source)
1070 {
1071 struct igmp_group *group;
1072 struct prefix_sg sg;
1073 int result;
1074
1075 memset(&sg, 0, sizeof(struct prefix_sg));
1076 sg.src = source->source_addr;
1077 sg.grp = source->source_group->group_addr;
1078
1079 if (PIM_DEBUG_IGMP_TRACE) {
1080 zlog_debug(
1081 "%s: (S,G)=%s igmp_sock=%d oif=%s fwd=%d",
1082 __PRETTY_FUNCTION__, pim_str_sg_dump(&sg),
1083 source->source_group->group_igmp_sock->fd,
1084 source->source_group->group_igmp_sock->interface->name,
1085 IGMP_SOURCE_TEST_FORWARDING(source->source_flags));
1086 }
1087
1088 /* Prevent IGMP interface from removing multicast route multiple
1089 times */
1090 if (!IGMP_SOURCE_TEST_FORWARDING(source->source_flags)) {
1091 return;
1092 }
1093
1094 group = source->source_group;
1095
1096 /*
1097 It appears that in certain circumstances that
1098 igmp_source_forward_stop is called when IGMP forwarding
1099 was not enabled in oif_flags for this outgoing interface.
1100 Possibly because of multiple calls. When that happens, we
1101 enter the below if statement and this function returns early
1102 which in turn triggers the calling function to assert.
1103 Making the call to pim_channel_del_oif and ignoring the return code
1104 fixes the issue without ill effect, similar to
1105 pim_forward_stop below.
1106 */
1107 result = pim_channel_del_oif(source->source_channel_oil,
1108 group->group_igmp_sock->interface,
1109 PIM_OIF_FLAG_PROTO_IGMP);
1110 if (result) {
1111 if (PIM_DEBUG_IGMP_TRACE)
1112 zlog_debug(
1113 "%s: pim_channel_del_oif() failed with return=%d",
1114 __func__, result);
1115 return;
1116 }
1117
1118 /*
1119 Feed IGMPv3-gathered local membership information into PIM
1120 per-interface (S,G) state.
1121 */
1122 pim_ifchannel_local_membership_del(group->group_igmp_sock->interface,
1123 &sg);
1124
1125 IGMP_SOURCE_DONT_FORWARDING(source->source_flags);
1126 }
1127
1128 void pim_forward_start(struct pim_ifchannel *ch)
1129 {
1130 struct pim_upstream *up = ch->upstream;
1131 uint32_t mask = PIM_OIF_FLAG_PROTO_PIM;
1132 int input_iface_vif_index = 0;
1133 struct pim_instance *pim;
1134 struct pim_interface *pim_ifp;
1135
1136 pim_ifp = ch->interface->info;
1137 pim = pim_ifp->pim;
1138
1139 if (PIM_DEBUG_PIM_TRACE) {
1140 char source_str[INET_ADDRSTRLEN];
1141 char group_str[INET_ADDRSTRLEN];
1142 char upstream_str[INET_ADDRSTRLEN];
1143
1144 pim_inet4_dump("<source?>", ch->sg.src, source_str,
1145 sizeof(source_str));
1146 pim_inet4_dump("<group?>", ch->sg.grp, group_str,
1147 sizeof(group_str));
1148 pim_inet4_dump("<upstream?>", up->upstream_addr, upstream_str,
1149 sizeof(upstream_str));
1150 zlog_debug("%s: (S,G)=(%s,%s) oif=%s (%s)", __PRETTY_FUNCTION__,
1151 source_str, group_str, ch->interface->name,
1152 upstream_str);
1153 }
1154
1155 /* Resolve IIF for upstream as mroute_del sets mfcc_parent to MAXVIFS,
1156 as part of mroute_del called by pim_forward_stop.
1157 */
1158 if (!up->channel_oil
1159 || (up->channel_oil
1160 && up->channel_oil->oil.mfcc_parent >= MAXVIFS)) {
1161 struct prefix nht_p, src, grp;
1162 struct pim_nexthop_cache out_pnc;
1163
1164 /* Register addr with Zebra NHT */
1165 nht_p.family = AF_INET;
1166 nht_p.prefixlen = IPV4_MAX_BITLEN;
1167 nht_p.u.prefix4 = up->upstream_addr;
1168 grp.family = AF_INET;
1169 grp.prefixlen = IPV4_MAX_BITLEN;
1170 grp.u.prefix4 = up->sg.grp;
1171 memset(&out_pnc, 0, sizeof(struct pim_nexthop_cache));
1172
1173 if (pim_find_or_track_nexthop(pim, &nht_p, NULL, NULL,
1174 &out_pnc)) {
1175 if (out_pnc.nexthop_num) {
1176 src.family = AF_INET;
1177 src.prefixlen = IPV4_MAX_BITLEN;
1178 src.u.prefix4 =
1179 up->upstream_addr; // RP or Src address
1180 grp.family = AF_INET;
1181 grp.prefixlen = IPV4_MAX_BITLEN;
1182 grp.u.prefix4 = up->sg.grp;
1183 // Compute PIM RPF using Cached nexthop
1184 if (pim_ecmp_nexthop_search(
1185 pim, &out_pnc,
1186 &up->rpf.source_nexthop, &src, &grp,
1187 0))
1188 input_iface_vif_index =
1189 pim_if_find_vifindex_by_ifindex(
1190 pim,
1191 up->rpf.source_nexthop
1192 .interface->ifindex);
1193 else {
1194 if (PIM_DEBUG_TRACE)
1195 zlog_debug(
1196 "%s: Nexthop selection failed for %s ",
1197 __PRETTY_FUNCTION__,
1198 up->sg_str);
1199 }
1200 } else {
1201 if (PIM_DEBUG_ZEBRA) {
1202 char buf1[INET_ADDRSTRLEN];
1203 char buf2[INET_ADDRSTRLEN];
1204 pim_inet4_dump("<source?>",
1205 nht_p.u.prefix4, buf1,
1206 sizeof(buf1));
1207 pim_inet4_dump("<source?>",
1208 grp.u.prefix4, buf2,
1209 sizeof(buf2));
1210 zlog_debug(
1211 "%s: NHT pnc is NULL for addr %s grp %s",
1212 __PRETTY_FUNCTION__, buf1,
1213 buf2);
1214 }
1215 }
1216 } else
1217 input_iface_vif_index =
1218 pim_ecmp_fib_lookup_if_vif_index(pim, &src,
1219 &grp);
1220
1221 if (input_iface_vif_index < 1) {
1222 if (PIM_DEBUG_PIM_TRACE) {
1223 char source_str[INET_ADDRSTRLEN];
1224 pim_inet4_dump("<source?>", up->sg.src,
1225 source_str, sizeof(source_str));
1226 zlog_debug(
1227 "%s %s: could not find input interface for source %s",
1228 __FILE__, __PRETTY_FUNCTION__,
1229 source_str);
1230 }
1231 return;
1232 }
1233 if (PIM_DEBUG_TRACE) {
1234 struct interface *in_intf = pim_if_find_by_vif_index(
1235 pim, input_iface_vif_index);
1236 zlog_debug(
1237 "%s: Update channel_oil IIF %s VIFI %d entry %s ",
1238 __PRETTY_FUNCTION__,
1239 in_intf ? in_intf->name : "NIL",
1240 input_iface_vif_index, up->sg_str);
1241 }
1242 up->channel_oil = pim_channel_oil_add(pim, &up->sg,
1243 input_iface_vif_index);
1244 if (!up->channel_oil) {
1245 if (PIM_DEBUG_PIM_TRACE)
1246 zlog_debug(
1247 "%s %s: could not create OIL for channel (S,G)=%s",
1248 __FILE__, __PRETTY_FUNCTION__,
1249 up->sg_str);
1250 return;
1251 }
1252 }
1253
1254 if (up->flags & PIM_UPSTREAM_FLAG_MASK_SRC_IGMP)
1255 mask = PIM_OIF_FLAG_PROTO_IGMP;
1256
1257 pim_channel_add_oif(up->channel_oil, ch->interface, mask);
1258 }
1259
1260 void pim_forward_stop(struct pim_ifchannel *ch, bool install_it)
1261 {
1262 struct pim_upstream *up = ch->upstream;
1263
1264 if (PIM_DEBUG_PIM_TRACE) {
1265 zlog_debug("%s: (S,G)=%s oif=%s install_it: %d installed: %d",
1266 __PRETTY_FUNCTION__, ch->sg_str, ch->interface->name,
1267 install_it, up->channel_oil->installed);
1268 }
1269
1270 pim_channel_del_oif(up->channel_oil, ch->interface,
1271 PIM_OIF_FLAG_PROTO_PIM);
1272
1273 if (install_it && !up->channel_oil->installed)
1274 pim_mroute_add(up->channel_oil, __PRETTY_FUNCTION__);
1275 }
1276
1277 void pim_zebra_zclient_update(struct vty *vty)
1278 {
1279 vty_out(vty, "Zclient update socket: ");
1280
1281 if (zclient) {
1282 vty_out(vty, "%d failures=%d\n", zclient->sock, zclient->fail);
1283 } else {
1284 vty_out(vty, "<null zclient>\n");
1285 }
1286 }
1287
1288 struct zclient *pim_zebra_zclient_get(void)
1289 {
1290 if (zclient)
1291 return zclient;
1292 else
1293 return NULL;
1294 }