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