]> git.proxmox.com Git - mirror_frr.git/blob - zebra/redistribute.c
Merge pull request #8639 from idryzhov/isis-new-bfd-lib
[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 (IS_ZEBRA_DEBUG_EVENT)
341 zlog_debug("%s: setting vrf %s(%u) redist bitmap",
342 __func__, VRF_LOGNAME(zvrf->vrf),
343 zvrf_id(zvrf));
344 vrf_bitmap_set(client->redist[afi][type], zvrf_id(zvrf));
345 zebra_redistribute(client, type, 0, zvrf_id(zvrf), afi);
346 }
347
348 stream_failure:
349 return;
350 }
351
352 void zebra_redistribute_delete(ZAPI_HANDLER_ARGS)
353 {
354 afi_t afi = 0;
355 int type = 0;
356 unsigned short instance;
357
358 STREAM_GETC(msg, afi);
359 STREAM_GETC(msg, type);
360 STREAM_GETW(msg, instance);
361
362 if (IS_ZEBRA_DEBUG_EVENT)
363 zlog_debug(
364 "%s: client proto %s afi=%d, no longer wants %s, vrf %s(%u), instance=%d",
365 __func__, zebra_route_string(client->proto), afi,
366 zebra_route_string(type), VRF_LOGNAME(zvrf->vrf),
367 zvrf_id(zvrf), instance);
368
369
370 if (afi == 0 || afi >= AFI_MAX) {
371 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
372 "%s: Specified afi %d does not exist", __func__, afi);
373 return;
374 }
375
376 if (type == 0 || type >= ZEBRA_ROUTE_MAX) {
377 zlog_debug("%s: Specified Route Type %d does not exist",
378 __func__, type);
379 return;
380 }
381
382 /*
383 * NOTE: no need to withdraw the previously advertised routes. The
384 * clients
385 * themselves should keep track of the received routes from zebra and
386 * withdraw them when necessary.
387 */
388 if (instance)
389 redist_del_instance(&client->mi_redist[afi][type], instance);
390 else
391 vrf_bitmap_unset(client->redist[afi][type], zvrf_id(zvrf));
392
393 stream_failure:
394 return;
395 }
396
397 void zebra_redistribute_default_add(ZAPI_HANDLER_ARGS)
398 {
399 afi_t afi = 0;
400
401 STREAM_GETC(msg, afi);
402
403 if (afi == 0 || afi >= AFI_MAX) {
404 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
405 "%s: Specified afi %u does not exist", __func__, afi);
406 return;
407 }
408
409 vrf_bitmap_set(client->redist_default[afi], zvrf_id(zvrf));
410 zebra_redistribute_default(client, zvrf_id(zvrf));
411
412 stream_failure:
413 return;
414 }
415
416 void zebra_redistribute_default_delete(ZAPI_HANDLER_ARGS)
417 {
418 afi_t afi = 0;
419
420 STREAM_GETC(msg, afi);
421
422 if (afi == 0 || afi >= AFI_MAX) {
423 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
424 "%s: Specified afi %u does not exist", __func__, afi);
425 return;
426 }
427
428 vrf_bitmap_unset(client->redist_default[afi], zvrf_id(zvrf));
429
430 stream_failure:
431 return;
432 }
433
434 /* Interface up information. */
435 void zebra_interface_up_update(struct interface *ifp)
436 {
437 struct listnode *node, *nnode;
438 struct zserv *client;
439
440 if (IS_ZEBRA_DEBUG_EVENT)
441 zlog_debug("MESSAGE: ZEBRA_INTERFACE_UP %s(%u)",
442 ifp->name, ifp->vrf_id);
443
444 if (ifp->ptm_status || !ifp->ptm_enable) {
445 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode,
446 client)) {
447 /* Do not send unsolicited messages to synchronous
448 * clients.
449 */
450 if (client->synchronous)
451 continue;
452
453 zsend_interface_update(ZEBRA_INTERFACE_UP,
454 client, ifp);
455 zsend_interface_link_params(client, ifp);
456 }
457 }
458 }
459
460 /* Interface down information. */
461 void zebra_interface_down_update(struct interface *ifp)
462 {
463 struct listnode *node, *nnode;
464 struct zserv *client;
465
466 if (IS_ZEBRA_DEBUG_EVENT)
467 zlog_debug("MESSAGE: ZEBRA_INTERFACE_DOWN %s(%u)",
468 ifp->name, ifp->vrf_id);
469
470 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
471 /* Do not send unsolicited messages to synchronous clients. */
472 if (client->synchronous)
473 continue;
474
475 zsend_interface_update(ZEBRA_INTERFACE_DOWN, client, ifp);
476 }
477 }
478
479 /* Interface information update. */
480 void zebra_interface_add_update(struct interface *ifp)
481 {
482 struct listnode *node, *nnode;
483 struct zserv *client;
484
485 if (IS_ZEBRA_DEBUG_EVENT)
486 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADD %s(%u)", ifp->name,
487 ifp->vrf_id);
488
489 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
490 /* Do not send unsolicited messages to synchronous clients. */
491 if (client->synchronous)
492 continue;
493
494 client->ifadd_cnt++;
495 zsend_interface_add(client, ifp);
496 zsend_interface_link_params(client, ifp);
497 }
498 }
499
500 void zebra_interface_delete_update(struct interface *ifp)
501 {
502 struct listnode *node, *nnode;
503 struct zserv *client;
504
505 if (IS_ZEBRA_DEBUG_EVENT)
506 zlog_debug("MESSAGE: ZEBRA_INTERFACE_DELETE %s(%u)",
507 ifp->name, ifp->vrf_id);
508
509 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
510 /* Do not send unsolicited messages to synchronous clients. */
511 if (client->synchronous)
512 continue;
513
514 client->ifdel_cnt++;
515 zsend_interface_delete(client, ifp);
516 }
517 }
518
519 /* Interface address addition. */
520 void zebra_interface_address_add_update(struct interface *ifp,
521 struct connected *ifc)
522 {
523 struct listnode *node, *nnode;
524 struct zserv *client;
525 struct prefix *p;
526
527 if (IS_ZEBRA_DEBUG_EVENT) {
528 p = ifc->address;
529 zlog_debug(
530 "MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %pFX on %s(%u)",
531 p, ifp->name, ifp->vrf_id);
532 }
533
534 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
535 flog_warn(
536 EC_ZEBRA_ADVERTISING_UNUSABLE_ADDR,
537 "advertising address to clients that is not yet usable.");
538
539 zebra_vxlan_add_del_gw_macip(ifp, ifc->address, 1);
540
541 router_id_add_address(ifc);
542
543 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
544 /* Do not send unsolicited messages to synchronous clients. */
545 if (client->synchronous)
546 continue;
547
548 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL)) {
549 client->connected_rt_add_cnt++;
550 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD,
551 client, ifp, ifc);
552 }
553 }
554 }
555
556 /* Interface address deletion. */
557 void zebra_interface_address_delete_update(struct interface *ifp,
558 struct connected *ifc)
559 {
560 struct listnode *node, *nnode;
561 struct zserv *client;
562 struct prefix *p;
563
564 if (IS_ZEBRA_DEBUG_EVENT) {
565 p = ifc->address;
566 zlog_debug(
567 "MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %pFX on %s(%u)",
568 p, ifp->name, ifp->vrf_id);
569 }
570
571 zebra_vxlan_add_del_gw_macip(ifp, ifc->address, 0);
572
573 router_id_del_address(ifc);
574
575 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
576 /* Do not send unsolicited messages to synchronous clients. */
577 if (client->synchronous)
578 continue;
579
580 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL)) {
581 client->connected_rt_del_cnt++;
582 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_DELETE,
583 client, ifp, ifc);
584 }
585 }
586 }
587
588 /* Interface VRF change. May need to delete from clients not interested in
589 * the new VRF. Note that this function is invoked *prior* to the VRF change.
590 */
591 void zebra_interface_vrf_update_del(struct interface *ifp, vrf_id_t new_vrf_id)
592 {
593 struct listnode *node, *nnode;
594 struct zserv *client;
595
596 if (IS_ZEBRA_DEBUG_EVENT)
597 zlog_debug(
598 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/DEL %s VRF Id %u -> %u",
599 ifp->name, ifp->vrf_id, new_vrf_id);
600
601 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
602 /* Do not send unsolicited messages to synchronous clients. */
603 if (client->synchronous)
604 continue;
605
606 /* Need to delete if the client is not interested in the new
607 * VRF. */
608 zsend_interface_update(ZEBRA_INTERFACE_DOWN, client, ifp);
609 client->ifdel_cnt++;
610 zsend_interface_delete(client, ifp);
611 zsend_interface_vrf_update(client, ifp, new_vrf_id);
612 }
613 }
614
615 /* Interface VRF change. This function is invoked *post* VRF change and sends an
616 * add to clients who are interested in the new VRF but not in the old VRF.
617 */
618 void zebra_interface_vrf_update_add(struct interface *ifp, vrf_id_t old_vrf_id)
619 {
620 struct listnode *node, *nnode;
621 struct zserv *client;
622
623 if (IS_ZEBRA_DEBUG_EVENT)
624 zlog_debug(
625 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/ADD %s VRF Id %u -> %u",
626 ifp->name, old_vrf_id, ifp->vrf_id);
627
628 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
629 /* Do not send unsolicited messages to synchronous clients. */
630 if (client->synchronous)
631 continue;
632
633 /* Need to add if the client is interested in the new VRF. */
634 client->ifadd_cnt++;
635 zsend_interface_add(client, ifp);
636 zsend_interface_addresses(client, ifp);
637 }
638 }
639
640 int zebra_add_import_table_entry(struct zebra_vrf *zvrf, struct route_node *rn,
641 struct route_entry *re, const char *rmap_name)
642 {
643 struct route_entry *newre;
644 struct route_entry *same;
645 struct prefix p;
646 struct nexthop_group *ng;
647 route_map_result_t ret = RMAP_PERMITMATCH;
648 afi_t afi;
649
650 afi = family2afi(rn->p.family);
651 if (rmap_name)
652 ret = zebra_import_table_route_map_check(
653 afi, re->type, re->instance, &rn->p,
654 re->nhe->nhg.nexthop,
655 zvrf->vrf->vrf_id, re->tag, rmap_name);
656
657 if (ret != RMAP_PERMITMATCH) {
658 UNSET_FLAG(re->flags, ZEBRA_FLAG_SELECTED);
659 zebra_del_import_table_entry(zvrf, rn, re);
660 return 0;
661 }
662
663 prefix_copy(&p, &rn->p);
664
665 RNODE_FOREACH_RE (rn, same) {
666 if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED))
667 continue;
668
669 if (same->type == re->type && same->instance == re->instance
670 && same->table == re->table
671 && same->type != ZEBRA_ROUTE_CONNECT)
672 break;
673 }
674
675 if (same) {
676 UNSET_FLAG(same->flags, ZEBRA_FLAG_SELECTED);
677 zebra_del_import_table_entry(zvrf, rn, same);
678 }
679
680 newre = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
681 newre->type = ZEBRA_ROUTE_TABLE;
682 newre->distance = zebra_import_table_distance[afi][re->table];
683 newre->flags = re->flags;
684 newre->metric = re->metric;
685 newre->mtu = re->mtu;
686 newre->table = zvrf->table_id;
687 newre->uptime = monotime(NULL);
688 newre->instance = re->table;
689
690 ng = nexthop_group_new();
691 copy_nexthops(&ng->nexthop, re->nhe->nhg.nexthop, NULL);
692
693 rib_add_multipath(afi, SAFI_UNICAST, &p, NULL, newre, ng);
694
695 return 0;
696 }
697
698 int zebra_del_import_table_entry(struct zebra_vrf *zvrf, struct route_node *rn,
699 struct route_entry *re)
700 {
701 struct prefix p;
702 afi_t afi;
703
704 afi = family2afi(rn->p.family);
705 prefix_copy(&p, &rn->p);
706
707 rib_delete(afi, SAFI_UNICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_TABLE,
708 re->table, re->flags, &p, NULL, re->nhe->nhg.nexthop,
709 re->nhe_id, zvrf->table_id, re->metric, re->distance,
710 false);
711
712 return 0;
713 }
714
715 /* Assuming no one calls this with the main routing table */
716 int zebra_import_table(afi_t afi, vrf_id_t vrf_id, uint32_t table_id,
717 uint32_t distance, const char *rmap_name, int add)
718 {
719 struct route_table *table;
720 struct route_entry *re;
721 struct route_node *rn;
722 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(vrf_id);
723
724 if (!is_zebra_valid_kernel_table(table_id)
725 || (table_id == RT_TABLE_MAIN))
726 return -1;
727
728 if (afi >= AFI_MAX)
729 return -1;
730
731 table = zebra_vrf_get_table_with_table_id(afi, SAFI_UNICAST, vrf_id,
732 table_id);
733 if (table == NULL) {
734 return 0;
735 } else if (IS_ZEBRA_DEBUG_RIB) {
736 zlog_debug("%s routes from table %d",
737 add ? "Importing" : "Unimporting", table_id);
738 }
739
740 if (add) {
741 if (rmap_name)
742 zebra_add_import_table_route_map(afi, rmap_name,
743 table_id);
744 else {
745 rmap_name =
746 zebra_get_import_table_route_map(afi, table_id);
747 if (rmap_name) {
748 zebra_del_import_table_route_map(afi, table_id);
749 rmap_name = NULL;
750 }
751 }
752
753 zebra_import_table_used[afi][table_id] = 1;
754 zebra_import_table_distance[afi][table_id] = distance;
755 } else {
756 zebra_import_table_used[afi][table_id] = 0;
757 zebra_import_table_distance[afi][table_id] =
758 ZEBRA_TABLE_DISTANCE_DEFAULT;
759
760 rmap_name = zebra_get_import_table_route_map(afi, table_id);
761 if (rmap_name) {
762 zebra_del_import_table_route_map(afi, table_id);
763 rmap_name = NULL;
764 }
765 }
766
767 for (rn = route_top(table); rn; rn = route_next(rn)) {
768 /* For each entry in the non-default routing table,
769 * add the entry in the main table
770 */
771 if (!rn->info)
772 continue;
773
774 RNODE_FOREACH_RE (rn, re) {
775 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
776 continue;
777 break;
778 }
779
780 if (!re)
781 continue;
782
783 if (((afi == AFI_IP) && (rn->p.family == AF_INET))
784 || ((afi == AFI_IP6) && (rn->p.family == AF_INET6))) {
785 if (add)
786 zebra_add_import_table_entry(zvrf, rn, re,
787 rmap_name);
788 else
789 zebra_del_import_table_entry(zvrf, rn, re);
790 }
791 }
792 return 0;
793 }
794
795 int zebra_import_table_config(struct vty *vty, vrf_id_t vrf_id)
796 {
797 int i;
798 afi_t afi;
799 int write = 0;
800 char afi_str[AFI_MAX][10] = {"", "ip", "ipv6", "ethernet"};
801 const char *rmap_name;
802
803 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
804 for (i = 1; i < ZEBRA_KERNEL_TABLE_MAX; i++) {
805 if (!is_zebra_import_table_enabled(afi, vrf_id, i))
806 continue;
807
808 if (zebra_import_table_distance[afi][i]
809 != ZEBRA_TABLE_DISTANCE_DEFAULT) {
810 vty_out(vty, "%s import-table %d distance %d",
811 afi_str[afi], i,
812 zebra_import_table_distance[afi][i]);
813 } else {
814 vty_out(vty, "%s import-table %d", afi_str[afi],
815 i);
816 }
817
818 rmap_name = zebra_get_import_table_route_map(afi, i);
819 if (rmap_name)
820 vty_out(vty, " route-map %s", rmap_name);
821
822 vty_out(vty, "\n");
823 write = 1;
824 }
825 }
826
827 return write;
828 }
829
830 static void zebra_import_table_rm_update_vrf_afi(struct zebra_vrf *zvrf,
831 afi_t afi, int table_id,
832 const char *rmap)
833 {
834 struct route_table *table;
835 struct route_entry *re;
836 struct route_node *rn;
837 const char *rmap_name;
838
839 rmap_name = zebra_get_import_table_route_map(afi, table_id);
840 if ((!rmap_name) || (strcmp(rmap_name, rmap) != 0))
841 return;
842
843 table = zebra_vrf_get_table_with_table_id(afi, SAFI_UNICAST,
844 zvrf->vrf->vrf_id, table_id);
845 if (!table) {
846 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
847 zlog_debug("%s: Table id=%d not found", __func__,
848 table_id);
849 return;
850 }
851
852 for (rn = route_top(table); rn; rn = route_next(rn)) {
853 /*
854 * For each entry in the non-default routing table,
855 * add the entry in the main table
856 */
857 if (!rn->info)
858 continue;
859
860 RNODE_FOREACH_RE (rn, re) {
861 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
862 continue;
863 break;
864 }
865
866 if (!re)
867 continue;
868
869 if (((afi == AFI_IP) && (rn->p.family == AF_INET))
870 || ((afi == AFI_IP6) && (rn->p.family == AF_INET6)))
871 zebra_add_import_table_entry(zvrf, rn, re, rmap_name);
872 }
873
874 return;
875 }
876
877 static void zebra_import_table_rm_update_vrf(struct zebra_vrf *zvrf,
878 const char *rmap)
879 {
880 afi_t afi;
881 int i;
882
883 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
884 for (i = 1; i < ZEBRA_KERNEL_TABLE_MAX; i++) {
885 if (!is_zebra_import_table_enabled(
886 afi, zvrf->vrf->vrf_id, i))
887 continue;
888
889 zebra_import_table_rm_update_vrf_afi(zvrf, afi, i,
890 rmap);
891 }
892 }
893 }
894
895 void zebra_import_table_rm_update(const char *rmap)
896 {
897 struct vrf *vrf;
898 struct zebra_vrf *zvrf;
899
900 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
901 zvrf = vrf->info;
902
903 if (!zvrf)
904 continue;
905
906 zebra_import_table_rm_update_vrf(zvrf, rmap);
907 }
908 }
909
910 /* Interface parameters update */
911 void zebra_interface_parameters_update(struct interface *ifp)
912 {
913 struct listnode *node, *nnode;
914 struct zserv *client;
915
916 if (IS_ZEBRA_DEBUG_EVENT)
917 zlog_debug("MESSAGE: ZEBRA_INTERFACE_LINK_PARAMS %s(%u)",
918 ifp->name, ifp->vrf_id);
919
920 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
921 /* Do not send unsolicited messages to synchronous clients. */
922 if (client->synchronous)
923 continue;
924
925 zsend_interface_link_params(client, ifp);
926 }
927 }