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