]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_zebra.c
*: auto-convert to SPDX License IDs
[mirror_frr.git] / pimd / pim_zebra.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * PIM for Quagga
4 * Copyright (C) 2008 Everton da Silva Marques
5 */
6
7 #include <zebra.h>
8
9 #include "if.h"
10 #include "log.h"
11 #include "prefix.h"
12 #include "zclient.h"
13 #include "stream.h"
14 #include "network.h"
15 #include "vty.h"
16 #include "plist.h"
17 #include "lib/bfd.h"
18
19 #include "pimd.h"
20 #include "pim_pim.h"
21 #include "pim_zebra.h"
22 #include "pim_iface.h"
23 #include "pim_str.h"
24 #include "pim_oil.h"
25 #include "pim_rpf.h"
26 #include "pim_time.h"
27 #include "pim_join.h"
28 #include "pim_zlookup.h"
29 #include "pim_ifchannel.h"
30 #include "pim_rp.h"
31 #include "pim_igmpv3.h"
32 #include "pim_jp_agg.h"
33 #include "pim_nht.h"
34 #include "pim_ssm.h"
35 #include "pim_vxlan.h"
36 #include "pim_mlag.h"
37
38 #undef PIM_DEBUG_IFADDR_DUMP
39 #define PIM_DEBUG_IFADDR_DUMP
40
41 struct zclient *zclient;
42
43
44 /* Router-id update message from zebra. */
45 static int pim_router_id_update_zebra(ZAPI_CALLBACK_ARGS)
46 {
47 struct prefix router_id;
48
49 zebra_router_id_update_read(zclient->ibuf, &router_id);
50
51 return 0;
52 }
53
54 static int pim_zebra_interface_vrf_update(ZAPI_CALLBACK_ARGS)
55 {
56 struct interface *ifp;
57 vrf_id_t new_vrf_id;
58
59 ifp = zebra_interface_vrf_update_read(zclient->ibuf, vrf_id,
60 &new_vrf_id);
61 if (!ifp)
62 return 0;
63
64 if (PIM_DEBUG_ZEBRA)
65 zlog_debug("%s: %s updating from %u to %u", __func__, ifp->name,
66 vrf_id, new_vrf_id);
67
68 if_update_to_new_vrf(ifp, new_vrf_id);
69
70 return 0;
71 }
72
73 #ifdef PIM_DEBUG_IFADDR_DUMP
74 static void dump_if_address(struct interface *ifp)
75 {
76 struct connected *ifc;
77 struct listnode *node;
78
79 zlog_debug("%s %s: interface %s addresses:", __FILE__, __func__,
80 ifp->name);
81
82 for (ALL_LIST_ELEMENTS_RO(ifp->connected, node, ifc)) {
83 struct prefix *p = ifc->address;
84
85 if (p->family != AF_INET)
86 continue;
87
88 zlog_debug("%s %s: interface %s address %pI4 %s", __FILE__,
89 __func__, ifp->name, &p->u.prefix4,
90 CHECK_FLAG(ifc->flags, ZEBRA_IFA_SECONDARY)
91 ? "secondary"
92 : "primary");
93 }
94 }
95 #endif
96
97 static int pim_zebra_if_address_add(ZAPI_CALLBACK_ARGS)
98 {
99 struct connected *c;
100 struct prefix *p;
101 struct pim_interface *pim_ifp;
102
103 /*
104 zebra api notifies address adds/dels events by using the same call
105 interface_add_read below, see comments in lib/zclient.c
106
107 zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_ADD, ...)
108 will add address to interface list by calling
109 connected_add_by_prefix()
110 */
111 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
112 if (!c)
113 return 0;
114
115 pim_ifp = c->ifp->info;
116 p = c->address;
117
118 if (PIM_DEBUG_ZEBRA) {
119 zlog_debug("%s: %s(%u) connected IP address %pFX flags %u %s",
120 __func__, c->ifp->name, vrf_id, p, c->flags,
121 CHECK_FLAG(c->flags, ZEBRA_IFA_SECONDARY)
122 ? "secondary"
123 : "primary");
124
125 #ifdef PIM_DEBUG_IFADDR_DUMP
126 dump_if_address(c->ifp);
127 #endif
128 }
129
130 #if PIM_IPV == 4
131 if (p->family != PIM_AF)
132 SET_FLAG(c->flags, ZEBRA_IFA_SECONDARY);
133 else if (!CHECK_FLAG(c->flags, ZEBRA_IFA_SECONDARY)) {
134 /* trying to add primary address? */
135 pim_addr primary_addr = pim_find_primary_addr(c->ifp);
136 pim_addr addr = pim_addr_from_prefix(p);
137
138 if (pim_addr_cmp(primary_addr, addr)) {
139 if (PIM_DEBUG_ZEBRA)
140 zlog_warn(
141 "%s: %s : forcing secondary flag on %pFX",
142 __func__, c->ifp->name, p);
143 SET_FLAG(c->flags, ZEBRA_IFA_SECONDARY);
144 }
145 }
146 #else /* PIM_IPV != 4 */
147 if (p->family != PIM_AF)
148 return 0;
149 #endif
150
151 pim_if_addr_add(c);
152 if (pim_ifp) {
153 struct pim_instance *pim;
154
155 pim = pim_get_pim_instance(vrf_id);
156 if (!pim) {
157 if (PIM_DEBUG_ZEBRA)
158 zlog_debug("%s: Unable to find pim instance",
159 __func__);
160 return 0;
161 }
162
163 pim_ifp->pim = pim;
164
165 pim_rp_check_on_if_add(pim_ifp);
166 }
167
168 if (if_is_loopback(c->ifp)) {
169 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
170 struct interface *ifp;
171
172 FOR_ALL_INTERFACES (vrf, ifp) {
173 if (!if_is_loopback(ifp) && if_is_operative(ifp))
174 pim_if_addr_add_all(ifp);
175 }
176 }
177 return 0;
178 }
179
180 static int pim_zebra_if_address_del(ZAPI_CALLBACK_ARGS)
181 {
182 struct connected *c;
183 struct prefix *p;
184 struct vrf *vrf = vrf_lookup_by_id(vrf_id);
185
186 if (!vrf)
187 return 0;
188
189 /*
190 zebra api notifies address adds/dels events by using the same call
191 interface_add_read below, see comments in lib/zclient.c
192
193 zebra_interface_address_read(ZEBRA_INTERFACE_ADDRESS_DELETE, ...)
194 will remove address from interface list by calling
195 connected_delete_by_prefix()
196 */
197 c = zebra_interface_address_read(cmd, zclient->ibuf, vrf_id);
198 if (!c)
199 return 0;
200
201 p = c->address;
202
203 if (PIM_DEBUG_ZEBRA) {
204 zlog_debug(
205 "%s: %s(%u) disconnected IP address %pFX flags %u %s",
206 __func__, c->ifp->name, vrf_id, p, c->flags,
207 CHECK_FLAG(c->flags, ZEBRA_IFA_SECONDARY)
208 ? "secondary"
209 : "primary");
210 #ifdef PIM_DEBUG_IFADDR_DUMP
211 dump_if_address(c->ifp);
212 #endif
213 }
214
215 if (p->family == PIM_AF) {
216 struct pim_instance *pim;
217
218 pim = vrf->info;
219 pim_if_addr_del(c, 0);
220 pim_rp_setup(pim);
221 pim_i_am_rp_re_evaluate(pim);
222 }
223
224 connected_free(&c);
225 return 0;
226 }
227
228 void pim_zebra_update_all_interfaces(struct pim_instance *pim)
229 {
230 struct interface *ifp;
231
232 FOR_ALL_INTERFACES (pim->vrf, ifp) {
233 struct pim_interface *pim_ifp = ifp->info;
234 struct pim_iface_upstream_switch *us;
235 struct listnode *node;
236
237 if (!pim_ifp)
238 continue;
239
240 for (ALL_LIST_ELEMENTS_RO(pim_ifp->upstream_switch_list, node,
241 us)) {
242 struct pim_rpf rpf;
243
244 rpf.source_nexthop.interface = ifp;
245 rpf.rpf_addr = us->address;
246 pim_joinprune_send(&rpf, us->us);
247 pim_jp_agg_clear_group(us->us);
248 }
249 }
250 }
251
252 void pim_zebra_upstream_rpf_changed(struct pim_instance *pim,
253 struct pim_upstream *up,
254 struct pim_rpf *old)
255 {
256 if (old->source_nexthop.interface) {
257 struct pim_neighbor *nbr;
258
259 nbr = pim_neighbor_find(old->source_nexthop.interface,
260 old->rpf_addr);
261 if (nbr)
262 pim_jp_agg_remove_group(nbr->upstream_jp_agg, up, nbr);
263
264 /*
265 * We have detected a case where we might need
266 * to rescan the inherited o_list so do it.
267 */
268 if (up->channel_oil->oil_inherited_rescan) {
269 pim_upstream_inherited_olist_decide(pim, up);
270 up->channel_oil->oil_inherited_rescan = 0;
271 }
272
273 if (up->join_state == PIM_UPSTREAM_JOINED) {
274 /*
275 * If we come up real fast we can be here
276 * where the mroute has not been installed
277 * so install it.
278 */
279 if (!up->channel_oil->installed)
280 pim_upstream_mroute_add(up->channel_oil,
281 __func__);
282
283 /*
284 * RFC 4601: 4.5.7. Sending (S,G)
285 * Join/Prune Messages
286 *
287 * Transitions from Joined State
288 *
289 * RPF'(S,G) changes not due to an Assert
290 *
291 * The upstream (S,G) state machine remains
292 * in Joined state. Send Join(S,G) to the new
293 * upstream neighbor, which is the new value
294 * of RPF'(S,G). Send Prune(S,G) to the old
295 * upstream neighbor, which is the old value
296 * of RPF'(S,G). Set the Join Timer (JT) to
297 * expire after t_periodic seconds.
298 */
299 pim_jp_agg_switch_interface(old, &up->rpf, up);
300
301 pim_upstream_join_timer_restart(up, old);
302 } /* up->join_state == PIM_UPSTREAM_JOINED */
303 }
304
305 else {
306 /*
307 * We have detected a case where we might need
308 * to rescan the inherited o_list so do it.
309 */
310 if (up->channel_oil->oil_inherited_rescan) {
311 pim_upstream_inherited_olist_decide(pim, up);
312 up->channel_oil->oil_inherited_rescan = 0;
313 }
314
315 if (up->join_state == PIM_UPSTREAM_JOINED)
316 pim_jp_agg_switch_interface(old, &up->rpf, up);
317
318 if (!up->channel_oil->installed)
319 pim_upstream_mroute_add(up->channel_oil, __func__);
320 }
321
322 /* FIXME can join_desired actually be changed by pim_rpf_update()
323 * returning PIM_RPF_CHANGED ?
324 */
325 pim_upstream_update_join_desired(pim, up);
326 }
327
328 __attribute__((unused))
329 static int pim_zebra_vxlan_sg_proc(ZAPI_CALLBACK_ARGS)
330 {
331 struct stream *s;
332 struct pim_instance *pim;
333 pim_sgaddr sg;
334 size_t prefixlen;
335
336 pim = pim_get_pim_instance(vrf_id);
337 if (!pim)
338 return 0;
339
340 s = zclient->ibuf;
341
342 prefixlen = stream_getl(s);
343 stream_get(&sg.src, s, prefixlen);
344 stream_get(&sg.grp, s, prefixlen);
345
346 if (PIM_DEBUG_ZEBRA)
347 zlog_debug("%u:recv SG %s %pSG", vrf_id,
348 (cmd == ZEBRA_VXLAN_SG_ADD) ? "add" : "del", &sg);
349
350 if (cmd == ZEBRA_VXLAN_SG_ADD)
351 pim_vxlan_sg_add(pim, &sg);
352 else
353 pim_vxlan_sg_del(pim, &sg);
354
355 return 0;
356 }
357
358 __attribute__((unused))
359 static void pim_zebra_vxlan_replay(void)
360 {
361 struct stream *s = NULL;
362
363 /* Check socket. */
364 if (!zclient || zclient->sock < 0)
365 return;
366
367 s = zclient->obuf;
368 stream_reset(s);
369
370 zclient_create_header(s, ZEBRA_VXLAN_SG_REPLAY, VRF_DEFAULT);
371 stream_putw_at(s, 0, stream_get_endp(s));
372
373 zclient_send_message(zclient);
374 }
375
376 void pim_scan_oil(struct pim_instance *pim)
377 {
378 struct channel_oil *c_oil;
379
380 pim->scan_oil_last = pim_time_monotonic_sec();
381 ++pim->scan_oil_events;
382
383 frr_each (rb_pim_oil, &pim->channel_oil_head, c_oil)
384 pim_upstream_mroute_iif_update(c_oil, __func__);
385 }
386
387 static void on_rpf_cache_refresh(struct thread *t)
388 {
389 struct pim_instance *pim = THREAD_ARG(t);
390
391 /* update kernel multicast forwarding cache (MFC) */
392 pim_scan_oil(pim);
393
394 pim->rpf_cache_refresh_last = pim_time_monotonic_sec();
395 ++pim->rpf_cache_refresh_events;
396
397 // It is called as part of pim_neighbor_add
398 // pim_rp_setup ();
399 }
400
401 void sched_rpf_cache_refresh(struct pim_instance *pim)
402 {
403 ++pim->rpf_cache_refresh_requests;
404
405 pim_rpf_set_refresh_time(pim);
406
407 if (pim->rpf_cache_refresher) {
408 /* Refresh timer is already running */
409 return;
410 }
411
412 /* Start refresh timer */
413
414 if (PIM_DEBUG_ZEBRA) {
415 zlog_debug("%s: triggering %ld msec timer", __func__,
416 router->rpf_cache_refresh_delay_msec);
417 }
418
419 thread_add_timer_msec(router->master, on_rpf_cache_refresh, pim,
420 router->rpf_cache_refresh_delay_msec,
421 &pim->rpf_cache_refresher);
422 }
423
424 static void pim_zebra_connected(struct zclient *zclient)
425 {
426 #if PIM_IPV == 4
427 /* Send the client registration */
428 bfd_client_sendmsg(zclient, ZEBRA_BFD_CLIENT_REGISTER, router->vrf_id);
429 #endif
430
431 zclient_send_reg_requests(zclient, router->vrf_id);
432
433 #if PIM_IPV == 4
434 /* request for VxLAN BUM group addresses */
435 pim_zebra_vxlan_replay();
436 #endif
437 }
438
439 static void pim_zebra_capabilities(struct zclient_capabilities *cap)
440 {
441 router->mlag_role = cap->role;
442 router->multipath = cap->ecmp;
443 }
444
445 static zclient_handler *const pim_handlers[] = {
446 [ZEBRA_INTERFACE_ADDRESS_ADD] = pim_zebra_if_address_add,
447 [ZEBRA_INTERFACE_ADDRESS_DELETE] = pim_zebra_if_address_del,
448
449 [ZEBRA_NEXTHOP_UPDATE] = pim_parse_nexthop_update,
450 [ZEBRA_ROUTER_ID_UPDATE] = pim_router_id_update_zebra,
451 [ZEBRA_INTERFACE_VRF_UPDATE] = pim_zebra_interface_vrf_update,
452
453 #if PIM_IPV == 4
454 [ZEBRA_VXLAN_SG_ADD] = pim_zebra_vxlan_sg_proc,
455 [ZEBRA_VXLAN_SG_DEL] = pim_zebra_vxlan_sg_proc,
456
457 [ZEBRA_MLAG_PROCESS_UP] = pim_zebra_mlag_process_up,
458 [ZEBRA_MLAG_PROCESS_DOWN] = pim_zebra_mlag_process_down,
459 [ZEBRA_MLAG_FORWARD_MSG] = pim_zebra_mlag_handle_msg,
460 #endif
461 };
462
463 void pim_zebra_init(void)
464 {
465 /* Socket for receiving updates from Zebra daemon */
466 zclient = zclient_new(router->master, &zclient_options_default,
467 pim_handlers, array_size(pim_handlers));
468
469 zclient->zebra_capabilities = pim_zebra_capabilities;
470 zclient->zebra_connected = pim_zebra_connected;
471
472 zclient_init(zclient, ZEBRA_ROUTE_PIM, 0, &pimd_privs);
473 if (PIM_DEBUG_PIM_TRACE) {
474 zlog_notice("%s: zclient socket initialized", __func__);
475 }
476
477 zclient_lookup_new();
478 }
479
480 void pim_forward_start(struct pim_ifchannel *ch)
481 {
482 struct pim_upstream *up = ch->upstream;
483 uint32_t mask = 0;
484
485 if (PIM_DEBUG_PIM_TRACE)
486 zlog_debug("%s: (S,G)=%pSG oif=%s (%pPA)", __func__, &ch->sg,
487 ch->interface->name, &up->upstream_addr);
488
489 if (PIM_IF_FLAG_TEST_PROTO_IGMP(ch->flags))
490 mask = PIM_OIF_FLAG_PROTO_GM;
491
492 if (PIM_IF_FLAG_TEST_PROTO_PIM(ch->flags))
493 mask |= PIM_OIF_FLAG_PROTO_PIM;
494
495 pim_channel_add_oif(up->channel_oil, ch->interface,
496 mask, __func__);
497 }
498
499 void pim_forward_stop(struct pim_ifchannel *ch)
500 {
501 struct pim_upstream *up = ch->upstream;
502
503 if (PIM_DEBUG_PIM_TRACE) {
504 zlog_debug("%s: (S,G)=%s oif=%s installed: %d",
505 __func__, ch->sg_str, ch->interface->name,
506 up->channel_oil->installed);
507 }
508
509 /*
510 * If a channel is being removed, check to see if we still need
511 * to inherit the interface. If so make sure it is added in
512 */
513 if (pim_upstream_evaluate_join_desired_interface(up, ch, ch->parent))
514 pim_channel_add_oif(up->channel_oil, ch->interface,
515 PIM_OIF_FLAG_PROTO_PIM, __func__);
516 else
517 pim_channel_del_oif(up->channel_oil, ch->interface,
518 PIM_OIF_FLAG_PROTO_PIM, __func__);
519 }
520
521 void pim_zebra_zclient_update(struct vty *vty)
522 {
523 vty_out(vty, "Zclient update socket: ");
524
525 if (zclient) {
526 vty_out(vty, "%d failures=%d\n", zclient->sock, zclient->fail);
527 } else {
528 vty_out(vty, "<null zclient>\n");
529 }
530 }
531
532 struct zclient *pim_zebra_zclient_get(void)
533 {
534 if (zclient)
535 return zclient;
536 else
537 return NULL;
538 }
539
540 void pim_zebra_interface_set_master(struct interface *vrf,
541 struct interface *ifp)
542 {
543 zclient_interface_set_master(zclient, vrf, ifp);
544 }