]> git.proxmox.com Git - mirror_frr.git/blob - zebra/redistribute.c
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / zebra / redistribute.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Redistribution Handler
3 * Copyright (C) 1998 Kunihiro Ishiguro
4 */
5
6 #include <zebra.h>
7
8 #include "vector.h"
9 #include "vty.h"
10 #include "command.h"
11 #include "prefix.h"
12 #include "table.h"
13 #include "stream.h"
14 #include "zclient.h"
15 #include "linklist.h"
16 #include "log.h"
17 #include "vrf.h"
18 #include "srcdest_table.h"
19
20 #include "zebra/rib.h"
21 #include "zebra/zebra_router.h"
22 #include "zebra/zebra_ns.h"
23 #include "zebra/zebra_vrf.h"
24 #include "zebra/zebra_routemap.h"
25 #include "zebra/redistribute.h"
26 #include "zebra/debug.h"
27 #include "zebra/router-id.h"
28 #include "zebra/zapi_msg.h"
29 #include "zebra/zebra_vxlan.h"
30 #include "zebra/zebra_errors.h"
31
32 #define ZEBRA_PTM_SUPPORT
33
34 /* array holding redistribute info about table redistribution */
35 /* bit AFI is set if that AFI is redistributing routes from this table */
36 static int zebra_import_table_used[AFI_MAX][ZEBRA_KERNEL_TABLE_MAX];
37 static uint32_t zebra_import_table_distance[AFI_MAX][ZEBRA_KERNEL_TABLE_MAX];
38
39 int is_zebra_import_table_enabled(afi_t afi, vrf_id_t vrf_id, uint32_t table_id)
40 {
41 /*
42 * Make sure that what we are called with actualy makes sense
43 */
44 if (afi == AFI_MAX)
45 return 0;
46
47 if (is_zebra_valid_kernel_table(table_id) &&
48 table_id < ZEBRA_KERNEL_TABLE_MAX)
49 return zebra_import_table_used[afi][table_id];
50 return 0;
51 }
52
53 static void zebra_redistribute_default(struct zserv *client, vrf_id_t vrf_id)
54 {
55 int afi;
56 struct prefix p;
57 struct route_table *table;
58 struct route_node *rn;
59 struct route_entry *newre;
60
61 for (afi = AFI_IP; afi <= AFI_IP6; afi++) {
62
63 if (!vrf_bitmap_check(client->redist_default[afi], vrf_id))
64 continue;
65
66 /* Lookup table. */
67 table = zebra_vrf_table(afi, SAFI_UNICAST, vrf_id);
68 if (!table)
69 continue;
70
71 /* Lookup default route. */
72 memset(&p, 0, sizeof(p));
73 p.family = afi2family(afi);
74 rn = route_node_lookup(table, &p);
75 if (!rn)
76 continue;
77
78 RNODE_FOREACH_RE (rn, newre) {
79 if (CHECK_FLAG(newre->flags, ZEBRA_FLAG_SELECTED))
80 zsend_redistribute_route(
81 ZEBRA_REDISTRIBUTE_ROUTE_ADD, client,
82 rn, newre);
83 }
84
85 route_unlock_node(rn);
86 }
87 }
88
89 /* Redistribute routes. */
90 static void zebra_redistribute(struct zserv *client, int type,
91 unsigned short instance, vrf_id_t vrf_id,
92 int afi)
93 {
94 struct route_entry *newre;
95 struct route_table *table;
96 struct route_node *rn;
97
98 table = zebra_vrf_table(afi, SAFI_UNICAST, vrf_id);
99 if (!table)
100 return;
101
102 for (rn = route_top(table); rn; rn = srcdest_route_next(rn))
103 RNODE_FOREACH_RE (rn, newre) {
104 if (IS_ZEBRA_DEBUG_RIB)
105 zlog_debug(
106 "%s: client %s %pRN(%u:%u) checking: selected=%d, type=%s, instance=%u, distance=%d, metric=%d zebra_check_addr=%d",
107 __func__,
108 zebra_route_string(client->proto), rn,
109 vrf_id, newre->instance,
110 !!CHECK_FLAG(newre->flags,
111 ZEBRA_FLAG_SELECTED),
112 zebra_route_string(newre->type),
113 newre->instance,
114 newre->distance,
115 newre->metric,
116 zebra_check_addr(&rn->p));
117
118 if (!CHECK_FLAG(newre->flags, ZEBRA_FLAG_SELECTED))
119 continue;
120 if ((type != ZEBRA_ROUTE_ALL
121 && (newre->type != type
122 || newre->instance != instance)))
123 continue;
124 if (!zebra_check_addr(&rn->p))
125 continue;
126
127 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_ADD,
128 client, rn, newre);
129 }
130 }
131
132 /*
133 * Function to check if prefix is candidate for
134 * redistribute.
135 */
136 static bool zebra_redistribute_check(const struct route_node *rn,
137 const struct route_entry *re,
138 struct zserv *client)
139 {
140 struct zebra_vrf *zvrf;
141 afi_t afi;
142
143 /* Process only if there is valid re */
144 if (!re)
145 return false;
146
147 afi = family2afi(rn->p.family);
148 zvrf = zebra_vrf_lookup_by_id(re->vrf_id);
149 if (re->vrf_id == VRF_DEFAULT && zvrf->table_id != re->table)
150 return false;
151
152 /* If default route and redistributed */
153 if (is_default_prefix(&rn->p) &&
154 vrf_bitmap_check(client->redist_default[afi], re->vrf_id))
155 return true;
156
157 /* If redistribute in enabled for zebra route all */
158 if (vrf_bitmap_check(client->redist[afi][ZEBRA_ROUTE_ALL], re->vrf_id))
159 return true;
160
161 /*
162 * If multi-instance then check for route
163 * redistribution for given instance.
164 */
165 if (re->instance) {
166 if (redist_check_instance(&client->mi_redist[afi][re->type],
167 re->instance))
168 return true;
169 else
170 return false;
171 }
172
173 /* If redistribution is enabled for give route type. */
174 if (vrf_bitmap_check(client->redist[afi][re->type], re->vrf_id))
175 return true;
176
177 return false;
178 }
179
180 /* Either advertise a route for redistribution to registered clients or */
181 /* withdraw redistribution if add cannot be done for client */
182 void redistribute_update(const struct route_node *rn,
183 const struct route_entry *re,
184 const struct route_entry *prev_re)
185 {
186 struct listnode *node, *nnode;
187 struct zserv *client;
188
189 if (IS_ZEBRA_DEBUG_RIB)
190 zlog_debug(
191 "(%u:%u):%pRN(%u): Redist update re %p (%s), old %p (%s)",
192 re->vrf_id, re->table, rn, re->instance, re,
193 zebra_route_string(re->type), prev_re,
194 prev_re ? zebra_route_string(prev_re->type) : "None");
195
196 if (!zebra_check_addr(&rn->p)) {
197 if (IS_ZEBRA_DEBUG_RIB)
198 zlog_debug("Redist update filter prefix %pRN", rn);
199 return;
200 }
201
202
203 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
204 if (zebra_redistribute_check(rn, re, client)) {
205 if (IS_ZEBRA_DEBUG_RIB) {
206 zlog_debug(
207 "%s: client %s %pRN(%u:%u), type=%d, distance=%d, metric=%d",
208 __func__,
209 zebra_route_string(client->proto), rn,
210 re->vrf_id, re->table, re->type,
211 re->distance, re->metric);
212 }
213 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_ADD,
214 client, rn, re);
215 } else if (zebra_redistribute_check(rn, prev_re, client))
216 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_DEL,
217 client, rn, prev_re);
218 }
219 }
220
221 /*
222 * During a route delete, where 'new_re' is NULL, redist a delete to all
223 * clients registered for the type of 'old_re'.
224 * During a route update, redist a delete to any clients who will not see
225 * an update when the new route is installed. There are cases when a client
226 * may have seen a redist for 'old_re', but will not see
227 * the redist for 'new_re'.
228 */
229 void redistribute_delete(const struct route_node *rn,
230 const struct route_entry *old_re,
231 const struct route_entry *new_re)
232 {
233 struct listnode *node, *nnode;
234 struct zserv *client;
235 vrf_id_t vrfid;
236
237 if (old_re)
238 vrfid = old_re->vrf_id;
239 else if (new_re)
240 vrfid = new_re->vrf_id;
241 else
242 return;
243
244 if (IS_ZEBRA_DEBUG_RIB) {
245 uint8_t old_inst, new_inst;
246 uint32_t table = 0;
247
248 old_inst = new_inst = 0;
249
250 if (old_re) {
251 old_inst = old_re->instance;
252 table = old_re->table;
253 }
254 if (new_re) {
255 new_inst = new_re->instance;
256 table = new_re->table;
257 }
258
259 zlog_debug(
260 "%u:%u%pRN: Redist del: re %p (%u:%s), new re %p (%u:%s)",
261 vrfid, table, rn, old_re, old_inst,
262 old_re ? zebra_route_string(old_re->type) : "None",
263 new_re, new_inst,
264 new_re ? zebra_route_string(new_re->type) : "None");
265 }
266
267 /* Skip invalid (e.g. linklocal) prefix */
268 if (!zebra_check_addr(&rn->p)) {
269 if (IS_ZEBRA_DEBUG_RIB) {
270 zlog_debug(
271 "%u:%pRN: Redist del old: skipping invalid prefix",
272 vrfid, rn);
273 }
274 return;
275 }
276
277 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
278 /* Do not send unsolicited messages to synchronous clients. */
279 if (client->synchronous)
280 continue;
281 /*
282 * Skip this client if it will receive an update for the
283 * 'new' re
284 */
285 if (zebra_redistribute_check(rn, new_re, client))
286 continue;
287
288 /* Send a delete for the 'old' re to any subscribed client. */
289 if (zebra_redistribute_check(rn, old_re, client))
290 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_DEL,
291 client, rn, old_re);
292 }
293 }
294
295
296 void zebra_redistribute_add(ZAPI_HANDLER_ARGS)
297 {
298 afi_t afi = 0;
299 int type = 0;
300 unsigned short instance;
301
302 STREAM_GETC(msg, afi);
303 STREAM_GETC(msg, type);
304 STREAM_GETW(msg, instance);
305
306 if (IS_ZEBRA_DEBUG_EVENT)
307 zlog_debug(
308 "%s: client proto %s afi=%d, wants %s, vrf %s(%u), instance=%d",
309 __func__, zebra_route_string(client->proto), afi,
310 zebra_route_string(type), VRF_LOGNAME(zvrf->vrf),
311 zvrf_id(zvrf), instance);
312
313 if (afi == 0 || afi >= AFI_MAX) {
314 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
315 "%s: Specified afi %d does not exist", __func__, afi);
316 return;
317 }
318
319 if (type == 0 || type >= ZEBRA_ROUTE_MAX) {
320 zlog_debug("%s: Specified Route Type %d does not exist",
321 __func__, type);
322 return;
323 }
324
325 if (instance) {
326 if (!redist_check_instance(&client->mi_redist[afi][type],
327 instance)) {
328 redist_add_instance(&client->mi_redist[afi][type],
329 instance);
330 zebra_redistribute(client, type, instance,
331 zvrf_id(zvrf), afi);
332 }
333 } else {
334 if (!vrf_bitmap_check(client->redist[afi][type],
335 zvrf_id(zvrf))) {
336 if (IS_ZEBRA_DEBUG_EVENT)
337 zlog_debug(
338 "%s: setting vrf %s(%u) redist bitmap",
339 __func__, VRF_LOGNAME(zvrf->vrf),
340 zvrf_id(zvrf));
341 vrf_bitmap_set(client->redist[afi][type],
342 zvrf_id(zvrf));
343 zebra_redistribute(client, type, 0, zvrf_id(zvrf), afi);
344 }
345 }
346
347 stream_failure:
348 return;
349 }
350
351 void zebra_redistribute_delete(ZAPI_HANDLER_ARGS)
352 {
353 afi_t afi = 0;
354 int type = 0;
355 unsigned short instance;
356
357 STREAM_GETC(msg, afi);
358 STREAM_GETC(msg, type);
359 STREAM_GETW(msg, instance);
360
361 if (IS_ZEBRA_DEBUG_EVENT)
362 zlog_debug(
363 "%s: client proto %s afi=%d, no longer wants %s, vrf %s(%u), instance=%d",
364 __func__, zebra_route_string(client->proto), afi,
365 zebra_route_string(type), VRF_LOGNAME(zvrf->vrf),
366 zvrf_id(zvrf), instance);
367
368
369 if (afi == 0 || afi >= AFI_MAX) {
370 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
371 "%s: Specified afi %d does not exist", __func__, afi);
372 return;
373 }
374
375 if (type == 0 || type >= ZEBRA_ROUTE_MAX) {
376 zlog_debug("%s: Specified Route Type %d does not exist",
377 __func__, type);
378 return;
379 }
380
381 /*
382 * NOTE: no need to withdraw the previously advertised routes. The
383 * clients
384 * themselves should keep track of the received routes from zebra and
385 * withdraw them when necessary.
386 */
387 if (instance)
388 redist_del_instance(&client->mi_redist[afi][type], instance);
389 else
390 vrf_bitmap_unset(client->redist[afi][type], zvrf_id(zvrf));
391
392 stream_failure:
393 return;
394 }
395
396 void zebra_redistribute_default_add(ZAPI_HANDLER_ARGS)
397 {
398 afi_t afi = 0;
399
400 STREAM_GETC(msg, afi);
401
402 if (afi == 0 || afi >= AFI_MAX) {
403 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
404 "%s: Specified afi %u does not exist", __func__, afi);
405 return;
406 }
407
408 vrf_bitmap_set(client->redist_default[afi], zvrf_id(zvrf));
409 zebra_redistribute_default(client, zvrf_id(zvrf));
410
411 stream_failure:
412 return;
413 }
414
415 void zebra_redistribute_default_delete(ZAPI_HANDLER_ARGS)
416 {
417 afi_t afi = 0;
418
419 STREAM_GETC(msg, afi);
420
421 if (afi == 0 || afi >= AFI_MAX) {
422 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
423 "%s: Specified afi %u does not exist", __func__, afi);
424 return;
425 }
426
427 vrf_bitmap_unset(client->redist_default[afi], zvrf_id(zvrf));
428
429 stream_failure:
430 return;
431 }
432
433 /* Interface up information. */
434 void zebra_interface_up_update(struct interface *ifp)
435 {
436 struct listnode *node, *nnode;
437 struct zserv *client;
438
439 if (IS_ZEBRA_DEBUG_EVENT)
440 zlog_debug("MESSAGE: ZEBRA_INTERFACE_UP %s vrf %s(%u)",
441 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id);
442
443 if (ifp->ptm_status || !ifp->ptm_enable) {
444 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode,
445 client)) {
446 /* Do not send unsolicited messages to synchronous
447 * clients.
448 */
449 if (client->synchronous)
450 continue;
451
452 zsend_interface_update(ZEBRA_INTERFACE_UP,
453 client, ifp);
454 zsend_interface_link_params(client, ifp);
455 }
456 }
457 }
458
459 /* Interface down information. */
460 void zebra_interface_down_update(struct interface *ifp)
461 {
462 struct listnode *node, *nnode;
463 struct zserv *client;
464
465 if (IS_ZEBRA_DEBUG_EVENT)
466 zlog_debug("MESSAGE: ZEBRA_INTERFACE_DOWN %s vrf %s(%u)",
467 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id);
468
469 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
470 /* Do not send unsolicited messages to synchronous clients. */
471 if (client->synchronous)
472 continue;
473
474 zsend_interface_update(ZEBRA_INTERFACE_DOWN, client, ifp);
475 }
476 }
477
478 /* Interface information update. */
479 void zebra_interface_add_update(struct interface *ifp)
480 {
481 struct listnode *node, *nnode;
482 struct zserv *client;
483
484 if (IS_ZEBRA_DEBUG_EVENT)
485 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADD %s vrf %s(%u)",
486 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id);
487
488 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
489 /* Do not send unsolicited messages to synchronous clients. */
490 if (client->synchronous)
491 continue;
492
493 client->ifadd_cnt++;
494 zsend_interface_add(client, ifp);
495 zsend_interface_link_params(client, ifp);
496 }
497 }
498
499 void zebra_interface_delete_update(struct interface *ifp)
500 {
501 struct listnode *node, *nnode;
502 struct zserv *client;
503
504 if (IS_ZEBRA_DEBUG_EVENT)
505 zlog_debug("MESSAGE: ZEBRA_INTERFACE_DELETE %s vrf %s(%u)",
506 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id);
507
508 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
509 /* Do not send unsolicited messages to synchronous clients. */
510 if (client->synchronous)
511 continue;
512
513 client->ifdel_cnt++;
514 zsend_interface_delete(client, ifp);
515 }
516 }
517
518 /* Interface address addition. */
519 void zebra_interface_address_add_update(struct interface *ifp,
520 struct connected *ifc)
521 {
522 struct listnode *node, *nnode;
523 struct zserv *client;
524
525 if (IS_ZEBRA_DEBUG_EVENT)
526 zlog_debug(
527 "MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %pFX on %s vrf %s(%u)",
528 ifc->address, ifp->name, ifp->vrf->name,
529 ifp->vrf->vrf_id);
530
531 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
532 flog_warn(
533 EC_ZEBRA_ADVERTISING_UNUSABLE_ADDR,
534 "advertising address to clients that is not yet usable.");
535
536 zebra_vxlan_add_del_gw_macip(ifp, ifc->address, 1);
537
538 router_id_add_address(ifc);
539
540 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
541 /* Do not send unsolicited messages to synchronous clients. */
542 if (client->synchronous)
543 continue;
544
545 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL)) {
546 client->connected_rt_add_cnt++;
547 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD,
548 client, ifp, ifc);
549 }
550 }
551 /* interface associated NHGs may have been deleted,
552 * re-sync zebra -> dplane NHGs
553 */
554 zebra_interface_nhg_reinstall(ifp);
555 }
556
557 /* Interface address deletion. */
558 void zebra_interface_address_delete_update(struct interface *ifp,
559 struct connected *ifc)
560 {
561 struct listnode *node, *nnode;
562 struct zserv *client;
563
564 if (IS_ZEBRA_DEBUG_EVENT)
565 zlog_debug(
566 "MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %pFX on %s vrf %s(%u)",
567 ifc->address, ifp->name, ifp->vrf->name,
568 ifp->vrf->vrf_id);
569
570 zebra_vxlan_add_del_gw_macip(ifp, ifc->address, 0);
571
572 router_id_del_address(ifc);
573
574 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
575 /* Do not send unsolicited messages to synchronous clients. */
576 if (client->synchronous)
577 continue;
578
579 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL)) {
580 client->connected_rt_del_cnt++;
581 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_DELETE,
582 client, ifp, ifc);
583 }
584 }
585 }
586
587 /* Interface VRF change. May need to delete from clients not interested in
588 * the new VRF. Note that this function is invoked *prior* to the VRF change.
589 */
590 void zebra_interface_vrf_update_del(struct interface *ifp, vrf_id_t new_vrf_id)
591 {
592 struct listnode *node, *nnode;
593 struct zserv *client;
594
595 if (IS_ZEBRA_DEBUG_EVENT)
596 zlog_debug(
597 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/DEL %s VRF Id %u -> %u",
598 ifp->name, ifp->vrf->vrf_id, new_vrf_id);
599
600 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
601 /* Do not send unsolicited messages to synchronous clients. */
602 if (client->synchronous)
603 continue;
604
605 /* Need to delete if the client is not interested in the new
606 * VRF. */
607 zsend_interface_update(ZEBRA_INTERFACE_DOWN, client, ifp);
608 client->ifdel_cnt++;
609 zsend_interface_delete(client, ifp);
610 zsend_interface_vrf_update(client, ifp, new_vrf_id);
611 }
612 }
613
614 /* Interface VRF change. This function is invoked *post* VRF change and sends an
615 * add to clients who are interested in the new VRF but not in the old VRF.
616 */
617 void zebra_interface_vrf_update_add(struct interface *ifp, vrf_id_t old_vrf_id)
618 {
619 struct listnode *node, *nnode;
620 struct zserv *client;
621
622 if (IS_ZEBRA_DEBUG_EVENT)
623 zlog_debug(
624 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/ADD %s VRF Id %u -> %u",
625 ifp->name, old_vrf_id, ifp->vrf->vrf_id);
626
627 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
628 /* Do not send unsolicited messages to synchronous clients. */
629 if (client->synchronous)
630 continue;
631
632 /* Need to add if the client is interested in the new VRF. */
633 client->ifadd_cnt++;
634 zsend_interface_add(client, ifp);
635 zsend_interface_addresses(client, ifp);
636 }
637 }
638
639 int zebra_add_import_table_entry(struct zebra_vrf *zvrf, struct route_node *rn,
640 struct route_entry *re, const char *rmap_name)
641 {
642 struct route_entry *newre;
643 struct route_entry *same;
644 struct prefix p;
645 struct nexthop_group *ng;
646 route_map_result_t ret = RMAP_PERMITMATCH;
647 afi_t afi;
648
649 afi = family2afi(rn->p.family);
650 if (rmap_name)
651 ret = zebra_import_table_route_map_check(
652 afi, re->type, re->instance, &rn->p,
653 re->nhe->nhg.nexthop,
654 zvrf->vrf->vrf_id, re->tag, rmap_name);
655
656 if (ret != RMAP_PERMITMATCH) {
657 UNSET_FLAG(re->flags, ZEBRA_FLAG_SELECTED);
658 zebra_del_import_table_entry(zvrf, rn, re);
659 return 0;
660 }
661
662 prefix_copy(&p, &rn->p);
663
664 RNODE_FOREACH_RE (rn, same) {
665 if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED))
666 continue;
667
668 if (same->type == re->type && same->instance == re->instance
669 && same->table == re->table
670 && same->type != ZEBRA_ROUTE_CONNECT)
671 break;
672 }
673
674 if (same) {
675 UNSET_FLAG(same->flags, ZEBRA_FLAG_SELECTED);
676 zebra_del_import_table_entry(zvrf, rn, same);
677 }
678
679 newre = zebra_rib_route_entry_new(
680 0, ZEBRA_ROUTE_TABLE, re->table, re->flags, re->nhe_id,
681 zvrf->table_id, re->metric, re->mtu,
682 zebra_import_table_distance[afi][re->table], re->tag);
683
684 ng = nexthop_group_new();
685 copy_nexthops(&ng->nexthop, re->nhe->nhg.nexthop, NULL);
686
687 rib_add_multipath(afi, SAFI_UNICAST, &p, NULL, newre, ng, false);
688
689 return 0;
690 }
691
692 int zebra_del_import_table_entry(struct zebra_vrf *zvrf, struct route_node *rn,
693 struct route_entry *re)
694 {
695 struct prefix p;
696 afi_t afi;
697
698 afi = family2afi(rn->p.family);
699 prefix_copy(&p, &rn->p);
700
701 rib_delete(afi, SAFI_UNICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_TABLE,
702 re->table, re->flags, &p, NULL, re->nhe->nhg.nexthop,
703 re->nhe_id, zvrf->table_id, re->metric, re->distance,
704 false);
705
706 return 0;
707 }
708
709 /* Assuming no one calls this with the main routing table */
710 int zebra_import_table(afi_t afi, vrf_id_t vrf_id, uint32_t table_id,
711 uint32_t distance, const char *rmap_name, int add)
712 {
713 struct route_table *table;
714 struct route_entry *re;
715 struct route_node *rn;
716 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(vrf_id);
717
718 if (!is_zebra_valid_kernel_table(table_id)
719 || (table_id == RT_TABLE_MAIN))
720 return -1;
721
722 if (afi >= AFI_MAX)
723 return -1;
724
725 table = zebra_vrf_get_table_with_table_id(afi, SAFI_UNICAST, vrf_id,
726 table_id);
727 if (table == NULL) {
728 return 0;
729 } else if (IS_ZEBRA_DEBUG_RIB) {
730 zlog_debug("%s routes from table %d",
731 add ? "Importing" : "Unimporting", table_id);
732 }
733
734 if (add) {
735 if (rmap_name)
736 zebra_add_import_table_route_map(afi, rmap_name,
737 table_id);
738 else {
739 rmap_name =
740 zebra_get_import_table_route_map(afi, table_id);
741 if (rmap_name) {
742 zebra_del_import_table_route_map(afi, table_id);
743 rmap_name = NULL;
744 }
745 }
746
747 zebra_import_table_used[afi][table_id] = 1;
748 zebra_import_table_distance[afi][table_id] = distance;
749 } else {
750 zebra_import_table_used[afi][table_id] = 0;
751 zebra_import_table_distance[afi][table_id] =
752 ZEBRA_TABLE_DISTANCE_DEFAULT;
753
754 rmap_name = zebra_get_import_table_route_map(afi, table_id);
755 if (rmap_name) {
756 zebra_del_import_table_route_map(afi, table_id);
757 rmap_name = NULL;
758 }
759 }
760
761 for (rn = route_top(table); rn; rn = route_next(rn)) {
762 /* For each entry in the non-default routing table,
763 * add the entry in the main table
764 */
765 if (!rn->info)
766 continue;
767
768 RNODE_FOREACH_RE (rn, re) {
769 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
770 continue;
771 break;
772 }
773
774 if (!re)
775 continue;
776
777 if (((afi == AFI_IP) && (rn->p.family == AF_INET))
778 || ((afi == AFI_IP6) && (rn->p.family == AF_INET6))) {
779 if (add)
780 zebra_add_import_table_entry(zvrf, rn, re,
781 rmap_name);
782 else
783 zebra_del_import_table_entry(zvrf, rn, re);
784 }
785 }
786 return 0;
787 }
788
789 int zebra_import_table_config(struct vty *vty, vrf_id_t vrf_id)
790 {
791 int i;
792 afi_t afi;
793 int write = 0;
794 char afi_str[AFI_MAX][10] = {"", "ip", "ipv6", "ethernet"};
795 const char *rmap_name;
796
797 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
798 for (i = 1; i < ZEBRA_KERNEL_TABLE_MAX; i++) {
799 if (!is_zebra_import_table_enabled(afi, vrf_id, i))
800 continue;
801
802 if (zebra_import_table_distance[afi][i]
803 != ZEBRA_TABLE_DISTANCE_DEFAULT) {
804 vty_out(vty, "%s import-table %d distance %d",
805 afi_str[afi], i,
806 zebra_import_table_distance[afi][i]);
807 } else {
808 vty_out(vty, "%s import-table %d", afi_str[afi],
809 i);
810 }
811
812 rmap_name = zebra_get_import_table_route_map(afi, i);
813 if (rmap_name)
814 vty_out(vty, " route-map %s", rmap_name);
815
816 vty_out(vty, "\n");
817 write = 1;
818 }
819 }
820
821 return write;
822 }
823
824 static void zebra_import_table_rm_update_vrf_afi(struct zebra_vrf *zvrf,
825 afi_t afi, int table_id,
826 const char *rmap)
827 {
828 struct route_table *table;
829 struct route_entry *re;
830 struct route_node *rn;
831 const char *rmap_name;
832
833 rmap_name = zebra_get_import_table_route_map(afi, table_id);
834 if ((!rmap_name) || (strcmp(rmap_name, rmap) != 0))
835 return;
836
837 table = zebra_vrf_get_table_with_table_id(afi, SAFI_UNICAST,
838 zvrf->vrf->vrf_id, table_id);
839 if (!table) {
840 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
841 zlog_debug("%s: Table id=%d not found", __func__,
842 table_id);
843 return;
844 }
845
846 for (rn = route_top(table); rn; rn = route_next(rn)) {
847 /*
848 * For each entry in the non-default routing table,
849 * add the entry in the main table
850 */
851 if (!rn->info)
852 continue;
853
854 RNODE_FOREACH_RE (rn, re) {
855 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
856 continue;
857 break;
858 }
859
860 if (!re)
861 continue;
862
863 if (((afi == AFI_IP) && (rn->p.family == AF_INET))
864 || ((afi == AFI_IP6) && (rn->p.family == AF_INET6)))
865 zebra_add_import_table_entry(zvrf, rn, re, rmap_name);
866 }
867
868 return;
869 }
870
871 static void zebra_import_table_rm_update_vrf(struct zebra_vrf *zvrf,
872 const char *rmap)
873 {
874 afi_t afi;
875 int i;
876
877 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
878 for (i = 1; i < ZEBRA_KERNEL_TABLE_MAX; i++) {
879 if (!is_zebra_import_table_enabled(
880 afi, zvrf->vrf->vrf_id, i))
881 continue;
882
883 zebra_import_table_rm_update_vrf_afi(zvrf, afi, i,
884 rmap);
885 }
886 }
887 }
888
889 void zebra_import_table_rm_update(const char *rmap)
890 {
891 struct vrf *vrf;
892 struct zebra_vrf *zvrf;
893
894 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
895 zvrf = vrf->info;
896
897 if (!zvrf)
898 continue;
899
900 zebra_import_table_rm_update_vrf(zvrf, rmap);
901 }
902 }
903
904 /* Interface parameters update */
905 void zebra_interface_parameters_update(struct interface *ifp)
906 {
907 struct listnode *node, *nnode;
908 struct zserv *client;
909
910 if (IS_ZEBRA_DEBUG_EVENT)
911 zlog_debug("MESSAGE: ZEBRA_INTERFACE_LINK_PARAMS %s vrf %s(%u)",
912 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id);
913
914 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
915 /* Do not send unsolicited messages to synchronous clients. */
916 if (client->synchronous)
917 continue;
918
919 zsend_interface_link_params(client, ifp);
920 }
921 }