]> git.proxmox.com Git - mirror_frr.git/blob - zebra/redistribute.c
Merge pull request #13403 from anlancs/fix/zebra-missing-vrf-flag
[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 }
552
553 /* Interface address deletion. */
554 void zebra_interface_address_delete_update(struct interface *ifp,
555 struct connected *ifc)
556 {
557 struct listnode *node, *nnode;
558 struct zserv *client;
559
560 if (IS_ZEBRA_DEBUG_EVENT)
561 zlog_debug(
562 "MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %pFX on %s vrf %s(%u)",
563 ifc->address, ifp->name, ifp->vrf->name,
564 ifp->vrf->vrf_id);
565
566 zebra_vxlan_add_del_gw_macip(ifp, ifc->address, 0);
567
568 router_id_del_address(ifc);
569
570 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
571 /* Do not send unsolicited messages to synchronous clients. */
572 if (client->synchronous)
573 continue;
574
575 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL)) {
576 client->connected_rt_del_cnt++;
577 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_DELETE,
578 client, ifp, ifc);
579 }
580 }
581 }
582
583 /* Interface VRF change. May need to delete from clients not interested in
584 * the new VRF. Note that this function is invoked *prior* to the VRF change.
585 */
586 void zebra_interface_vrf_update_del(struct interface *ifp, vrf_id_t new_vrf_id)
587 {
588 struct listnode *node, *nnode;
589 struct zserv *client;
590
591 if (IS_ZEBRA_DEBUG_EVENT)
592 zlog_debug(
593 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/DEL %s VRF Id %u -> %u",
594 ifp->name, ifp->vrf->vrf_id, new_vrf_id);
595
596 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
597 /* Do not send unsolicited messages to synchronous clients. */
598 if (client->synchronous)
599 continue;
600
601 /* Need to delete if the client is not interested in the new
602 * VRF. */
603 zsend_interface_update(ZEBRA_INTERFACE_DOWN, client, ifp);
604 client->ifdel_cnt++;
605 zsend_interface_delete(client, ifp);
606 zsend_interface_vrf_update(client, ifp, new_vrf_id);
607 }
608 }
609
610 /* Interface VRF change. This function is invoked *post* VRF change and sends an
611 * add to clients who are interested in the new VRF but not in the old VRF.
612 */
613 void zebra_interface_vrf_update_add(struct interface *ifp, vrf_id_t old_vrf_id)
614 {
615 struct listnode *node, *nnode;
616 struct zserv *client;
617
618 if (IS_ZEBRA_DEBUG_EVENT)
619 zlog_debug(
620 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/ADD %s VRF Id %u -> %u",
621 ifp->name, old_vrf_id, ifp->vrf->vrf_id);
622
623 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
624 /* Do not send unsolicited messages to synchronous clients. */
625 if (client->synchronous)
626 continue;
627
628 /* Need to add if the client is interested in the new VRF. */
629 client->ifadd_cnt++;
630 zsend_interface_add(client, ifp);
631 zsend_interface_addresses(client, ifp);
632 }
633 }
634
635 int zebra_add_import_table_entry(struct zebra_vrf *zvrf, struct route_node *rn,
636 struct route_entry *re, const char *rmap_name)
637 {
638 struct route_entry *newre;
639 struct route_entry *same;
640 struct prefix p;
641 struct nexthop_group *ng;
642 route_map_result_t ret = RMAP_PERMITMATCH;
643 afi_t afi;
644
645 afi = family2afi(rn->p.family);
646 if (rmap_name)
647 ret = zebra_import_table_route_map_check(
648 afi, re->type, re->instance, &rn->p,
649 re->nhe->nhg.nexthop,
650 zvrf->vrf->vrf_id, re->tag, rmap_name);
651
652 if (ret != RMAP_PERMITMATCH) {
653 UNSET_FLAG(re->flags, ZEBRA_FLAG_SELECTED);
654 zebra_del_import_table_entry(zvrf, rn, re);
655 return 0;
656 }
657
658 prefix_copy(&p, &rn->p);
659
660 RNODE_FOREACH_RE (rn, same) {
661 if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED))
662 continue;
663
664 if (same->type == re->type && same->instance == re->instance
665 && same->table == re->table
666 && same->type != ZEBRA_ROUTE_CONNECT)
667 break;
668 }
669
670 if (same) {
671 UNSET_FLAG(same->flags, ZEBRA_FLAG_SELECTED);
672 zebra_del_import_table_entry(zvrf, rn, same);
673 }
674
675 newre = zebra_rib_route_entry_new(
676 0, ZEBRA_ROUTE_TABLE, re->table, re->flags, re->nhe_id,
677 zvrf->table_id, re->metric, re->mtu,
678 zebra_import_table_distance[afi][re->table], re->tag);
679
680 ng = nexthop_group_new();
681 copy_nexthops(&ng->nexthop, re->nhe->nhg.nexthop, NULL);
682
683 rib_add_multipath(afi, SAFI_UNICAST, &p, NULL, newre, ng, false);
684
685 return 0;
686 }
687
688 int zebra_del_import_table_entry(struct zebra_vrf *zvrf, struct route_node *rn,
689 struct route_entry *re)
690 {
691 struct prefix p;
692 afi_t afi;
693
694 afi = family2afi(rn->p.family);
695 prefix_copy(&p, &rn->p);
696
697 rib_delete(afi, SAFI_UNICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_TABLE,
698 re->table, re->flags, &p, NULL, re->nhe->nhg.nexthop,
699 re->nhe_id, zvrf->table_id, re->metric, re->distance,
700 false);
701
702 return 0;
703 }
704
705 /* Assuming no one calls this with the main routing table */
706 int zebra_import_table(afi_t afi, vrf_id_t vrf_id, uint32_t table_id,
707 uint32_t distance, const char *rmap_name, int add)
708 {
709 struct route_table *table;
710 struct route_entry *re;
711 struct route_node *rn;
712 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(vrf_id);
713
714 if (!is_zebra_valid_kernel_table(table_id)
715 || (table_id == RT_TABLE_MAIN))
716 return -1;
717
718 if (afi >= AFI_MAX)
719 return -1;
720
721 table = zebra_vrf_get_table_with_table_id(afi, SAFI_UNICAST, vrf_id,
722 table_id);
723 if (table == NULL) {
724 return 0;
725 } else if (IS_ZEBRA_DEBUG_RIB) {
726 zlog_debug("%s routes from table %d",
727 add ? "Importing" : "Unimporting", table_id);
728 }
729
730 if (add) {
731 if (rmap_name)
732 zebra_add_import_table_route_map(afi, rmap_name,
733 table_id);
734 else {
735 rmap_name =
736 zebra_get_import_table_route_map(afi, table_id);
737 if (rmap_name) {
738 zebra_del_import_table_route_map(afi, table_id);
739 rmap_name = NULL;
740 }
741 }
742
743 zebra_import_table_used[afi][table_id] = 1;
744 zebra_import_table_distance[afi][table_id] = distance;
745 } else {
746 zebra_import_table_used[afi][table_id] = 0;
747 zebra_import_table_distance[afi][table_id] =
748 ZEBRA_TABLE_DISTANCE_DEFAULT;
749
750 rmap_name = zebra_get_import_table_route_map(afi, table_id);
751 if (rmap_name) {
752 zebra_del_import_table_route_map(afi, table_id);
753 rmap_name = NULL;
754 }
755 }
756
757 for (rn = route_top(table); rn; rn = route_next(rn)) {
758 /* For each entry in the non-default routing table,
759 * add the entry in the main table
760 */
761 if (!rn->info)
762 continue;
763
764 RNODE_FOREACH_RE (rn, re) {
765 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
766 continue;
767 break;
768 }
769
770 if (!re)
771 continue;
772
773 if (((afi == AFI_IP) && (rn->p.family == AF_INET))
774 || ((afi == AFI_IP6) && (rn->p.family == AF_INET6))) {
775 if (add)
776 zebra_add_import_table_entry(zvrf, rn, re,
777 rmap_name);
778 else
779 zebra_del_import_table_entry(zvrf, rn, re);
780 }
781 }
782 return 0;
783 }
784
785 int zebra_import_table_config(struct vty *vty, vrf_id_t vrf_id)
786 {
787 int i;
788 afi_t afi;
789 int write = 0;
790 char afi_str[AFI_MAX][10] = {"", "ip", "ipv6", "ethernet"};
791 const char *rmap_name;
792
793 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
794 for (i = 1; i < ZEBRA_KERNEL_TABLE_MAX; i++) {
795 if (!is_zebra_import_table_enabled(afi, vrf_id, i))
796 continue;
797
798 if (zebra_import_table_distance[afi][i]
799 != ZEBRA_TABLE_DISTANCE_DEFAULT) {
800 vty_out(vty, "%s import-table %d distance %d",
801 afi_str[afi], i,
802 zebra_import_table_distance[afi][i]);
803 } else {
804 vty_out(vty, "%s import-table %d", afi_str[afi],
805 i);
806 }
807
808 rmap_name = zebra_get_import_table_route_map(afi, i);
809 if (rmap_name)
810 vty_out(vty, " route-map %s", rmap_name);
811
812 vty_out(vty, "\n");
813 write = 1;
814 }
815 }
816
817 return write;
818 }
819
820 static void zebra_import_table_rm_update_vrf_afi(struct zebra_vrf *zvrf,
821 afi_t afi, int table_id,
822 const char *rmap)
823 {
824 struct route_table *table;
825 struct route_entry *re;
826 struct route_node *rn;
827 const char *rmap_name;
828
829 rmap_name = zebra_get_import_table_route_map(afi, table_id);
830 if ((!rmap_name) || (strcmp(rmap_name, rmap) != 0))
831 return;
832
833 table = zebra_vrf_get_table_with_table_id(afi, SAFI_UNICAST,
834 zvrf->vrf->vrf_id, table_id);
835 if (!table) {
836 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
837 zlog_debug("%s: Table id=%d not found", __func__,
838 table_id);
839 return;
840 }
841
842 for (rn = route_top(table); rn; rn = route_next(rn)) {
843 /*
844 * For each entry in the non-default routing table,
845 * add the entry in the main table
846 */
847 if (!rn->info)
848 continue;
849
850 RNODE_FOREACH_RE (rn, re) {
851 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
852 continue;
853 break;
854 }
855
856 if (!re)
857 continue;
858
859 if (((afi == AFI_IP) && (rn->p.family == AF_INET))
860 || ((afi == AFI_IP6) && (rn->p.family == AF_INET6)))
861 zebra_add_import_table_entry(zvrf, rn, re, rmap_name);
862 }
863
864 return;
865 }
866
867 static void zebra_import_table_rm_update_vrf(struct zebra_vrf *zvrf,
868 const char *rmap)
869 {
870 afi_t afi;
871 int i;
872
873 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
874 for (i = 1; i < ZEBRA_KERNEL_TABLE_MAX; i++) {
875 if (!is_zebra_import_table_enabled(
876 afi, zvrf->vrf->vrf_id, i))
877 continue;
878
879 zebra_import_table_rm_update_vrf_afi(zvrf, afi, i,
880 rmap);
881 }
882 }
883 }
884
885 void zebra_import_table_rm_update(const char *rmap)
886 {
887 struct vrf *vrf;
888 struct zebra_vrf *zvrf;
889
890 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
891 zvrf = vrf->info;
892
893 if (!zvrf)
894 continue;
895
896 zebra_import_table_rm_update_vrf(zvrf, rmap);
897 }
898 }
899
900 /* Interface parameters update */
901 void zebra_interface_parameters_update(struct interface *ifp)
902 {
903 struct listnode *node, *nnode;
904 struct zserv *client;
905
906 if (IS_ZEBRA_DEBUG_EVENT)
907 zlog_debug("MESSAGE: ZEBRA_INTERFACE_LINK_PARAMS %s vrf %s(%u)",
908 ifp->name, ifp->vrf->name, ifp->vrf->vrf_id);
909
910 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
911 /* Do not send unsolicited messages to synchronous clients. */
912 if (client->synchronous)
913 continue;
914
915 zsend_interface_link_params(client, ifp);
916 }
917 }