]> git.proxmox.com Git - mirror_frr.git/blob - zebra/redistribute.c
Merge pull request #4809 from martonksz/master
[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_EVENT)
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 struct route_entry *re, struct route_entry *prev_re)
153 {
154 struct listnode *node, *nnode;
155 struct zserv *client;
156 int send_redistribute;
157 int afi;
158 char buf[PREFIX_STRLEN];
159
160 if (IS_ZEBRA_DEBUG_RIB) {
161 zlog_debug(
162 "%u:%s: Redist update re %p (%s), old %p (%s)",
163 re->vrf_id, prefix2str(p, buf, sizeof(buf)),
164 re, zebra_route_string(re->type), prev_re,
165 prev_re ? zebra_route_string(prev_re->type) : "None");
166 }
167
168 afi = family2afi(p->family);
169 if (!afi) {
170 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
171 "%s: Unknown AFI/SAFI prefix received\n",
172 __FUNCTION__);
173 return;
174 }
175 if (!zebra_check_addr(p)) {
176 if (IS_ZEBRA_DEBUG_RIB)
177 zlog_debug("Redist update filter prefix %s",
178 prefix2str(p, buf, sizeof(buf)));
179 return;
180 }
181
182
183 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
184 send_redistribute = 0;
185
186 if (is_default_prefix(p)
187 && vrf_bitmap_check(client->redist_default[afi],
188 re->vrf_id))
189 send_redistribute = 1;
190 else if (vrf_bitmap_check(client->redist[afi][ZEBRA_ROUTE_ALL],
191 re->vrf_id))
192 send_redistribute = 1;
193 else if (re->instance
194 && redist_check_instance(
195 &client->mi_redist[afi][re->type],
196 re->instance))
197 send_redistribute = 1;
198 else if (vrf_bitmap_check(client->redist[afi][re->type],
199 re->vrf_id))
200 send_redistribute = 1;
201
202 if (send_redistribute) {
203 if (IS_ZEBRA_DEBUG_EVENT) {
204 zlog_debug(
205 "%s: client %s %s(%u), type=%d, distance=%d, metric=%d",
206 __func__,
207 zebra_route_string(client->proto),
208 prefix2str(p, buf, sizeof(buf)),
209 re->vrf_id, re->type,
210 re->distance, re->metric);
211 }
212 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_ADD,
213 client, p, src_p, re);
214 } else if (prev_re
215 && ((re->instance
216 && redist_check_instance(
217 &client->mi_redist[afi]
218 [prev_re->type],
219 re->instance))
220 || vrf_bitmap_check(
221 client->redist[afi][prev_re->type],
222 re->vrf_id))) {
223 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_DEL,
224 client, p, src_p, prev_re);
225 }
226 }
227 }
228
229 void redistribute_delete(const struct prefix *p, const struct prefix *src_p,
230 struct route_entry *re)
231 {
232 struct listnode *node, *nnode;
233 struct zserv *client;
234 char buf[INET6_ADDRSTRLEN];
235 int afi;
236
237 if (IS_ZEBRA_DEBUG_RIB) {
238 inet_ntop(p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN);
239 zlog_debug("%u:%s/%d: Redist delete re %p (%s)",
240 re->vrf_id, buf, p->prefixlen, re,
241 zebra_route_string(re->type));
242 }
243
244 /* Add DISTANCE_INFINITY check. */
245 if (re->distance == DISTANCE_INFINITY)
246 return;
247
248 afi = family2afi(p->family);
249 if (!afi) {
250 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
251 "%s: Unknown AFI/SAFI prefix received\n",
252 __FUNCTION__);
253 return;
254 }
255
256 if (!zebra_check_addr(p)) {
257 if (IS_ZEBRA_DEBUG_RIB)
258 zlog_debug("Redist delete filter prefix %s",
259 prefix2str(p, buf, sizeof(buf)));
260 return;
261 }
262
263 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
264 if ((is_default_prefix(p)
265 && vrf_bitmap_check(client->redist_default[afi],
266 re->vrf_id))
267 || vrf_bitmap_check(client->redist[afi][ZEBRA_ROUTE_ALL],
268 re->vrf_id)
269 || (re->instance
270 && redist_check_instance(
271 &client->mi_redist[afi][re->type],
272 re->instance))
273 || vrf_bitmap_check(client->redist[afi][re->type],
274 re->vrf_id)) {
275 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_DEL,
276 client, p, src_p, re);
277 }
278 }
279 }
280
281 void zebra_redistribute_add(ZAPI_HANDLER_ARGS)
282 {
283 afi_t afi = 0;
284 int type = 0;
285 unsigned short instance;
286
287 STREAM_GETC(msg, afi);
288 STREAM_GETC(msg, type);
289 STREAM_GETW(msg, instance);
290
291 if (IS_ZEBRA_DEBUG_EVENT)
292 zlog_debug(
293 "%s: client proto %s afi=%d, wants %s, vrf %u, instance=%d",
294 __func__, zebra_route_string(client->proto), afi,
295 zebra_route_string(type), zvrf_id(zvrf), instance);
296
297 if (afi == 0 || afi >= AFI_MAX) {
298 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
299 "%s: Specified afi %d does not exist",
300 __PRETTY_FUNCTION__, afi);
301 return;
302 }
303
304 if (type == 0 || type >= ZEBRA_ROUTE_MAX) {
305 zlog_debug("%s: Specified Route Type %d does not exist",
306 __PRETTY_FUNCTION__, type);
307 return;
308 }
309
310 if (instance) {
311 if (!redist_check_instance(&client->mi_redist[afi][type],
312 instance)) {
313 redist_add_instance(&client->mi_redist[afi][type],
314 instance);
315 zebra_redistribute(client, type, instance,
316 zvrf_id(zvrf), afi);
317 }
318 } else {
319 if (!vrf_bitmap_check(client->redist[afi][type],
320 zvrf_id(zvrf))) {
321 if (IS_ZEBRA_DEBUG_EVENT)
322 zlog_debug("%s: setting vrf %u redist bitmap",
323 __func__, zvrf_id(zvrf));
324 vrf_bitmap_set(client->redist[afi][type],
325 zvrf_id(zvrf));
326 zebra_redistribute(client, type, 0, zvrf_id(zvrf), afi);
327 }
328 }
329
330 stream_failure:
331 return;
332 }
333
334 void zebra_redistribute_delete(ZAPI_HANDLER_ARGS)
335 {
336 afi_t afi = 0;
337 int type = 0;
338 unsigned short instance;
339
340 STREAM_GETC(msg, afi);
341 STREAM_GETC(msg, type);
342 STREAM_GETW(msg, instance);
343
344 if (afi == 0 || afi >= AFI_MAX) {
345 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
346 "%s: Specified afi %d does not exist",
347 __PRETTY_FUNCTION__, afi);
348 return;
349 }
350
351 if (type == 0 || type >= ZEBRA_ROUTE_MAX) {
352 zlog_debug("%s: Specified Route Type %d does not exist",
353 __PRETTY_FUNCTION__, type);
354 return;
355 }
356
357 /*
358 * NOTE: no need to withdraw the previously advertised routes. The
359 * clients
360 * themselves should keep track of the received routes from zebra and
361 * withdraw them when necessary.
362 */
363 if (instance)
364 redist_del_instance(&client->mi_redist[afi][type], instance);
365 else
366 vrf_bitmap_unset(client->redist[afi][type], zvrf_id(zvrf));
367
368 stream_failure:
369 return;
370 }
371
372 void zebra_redistribute_default_add(ZAPI_HANDLER_ARGS)
373 {
374 afi_t afi = 0;
375
376 STREAM_GETC(msg, afi);
377
378 if (afi == 0 || afi >= AFI_MAX) {
379 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
380 "%s: Specified afi %u does not exist",
381 __PRETTY_FUNCTION__, afi);
382 return;
383 }
384
385 vrf_bitmap_set(client->redist_default[afi], zvrf_id(zvrf));
386 zebra_redistribute_default(client, zvrf_id(zvrf));
387
388 stream_failure:
389 return;
390 }
391
392 void zebra_redistribute_default_delete(ZAPI_HANDLER_ARGS)
393 {
394 afi_t afi = 0;
395
396 STREAM_GETC(msg, afi);
397
398 if (afi == 0 || afi >= AFI_MAX) {
399 flog_warn(EC_ZEBRA_REDISTRIBUTE_UNKNOWN_AF,
400 "%s: Specified afi %u does not exist",
401 __PRETTY_FUNCTION__, afi);
402 return;
403 }
404
405 vrf_bitmap_unset(client->redist_default[afi], zvrf_id(zvrf));
406
407 stream_failure:
408 return;
409 }
410
411 /* Interface up information. */
412 void zebra_interface_up_update(struct interface *ifp)
413 {
414 struct listnode *node, *nnode;
415 struct zserv *client;
416
417 if (IS_ZEBRA_DEBUG_EVENT)
418 zlog_debug("MESSAGE: ZEBRA_INTERFACE_UP %s(%u)",
419 ifp->name, ifp->vrf_id);
420
421 if (ifp->ptm_status || !ifp->ptm_enable) {
422 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode,
423 client)) {
424 zsend_interface_update(ZEBRA_INTERFACE_UP,
425 client, ifp);
426 zsend_interface_link_params(client, ifp);
427 }
428 }
429 }
430
431 /* Interface down information. */
432 void zebra_interface_down_update(struct interface *ifp)
433 {
434 struct listnode *node, *nnode;
435 struct zserv *client;
436
437 if (IS_ZEBRA_DEBUG_EVENT)
438 zlog_debug("MESSAGE: ZEBRA_INTERFACE_DOWN %s(%u)",
439 ifp->name, ifp->vrf_id);
440
441 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
442 zsend_interface_update(ZEBRA_INTERFACE_DOWN, client, ifp);
443 }
444 }
445
446 /* Interface information update. */
447 void zebra_interface_add_update(struct interface *ifp)
448 {
449 struct listnode *node, *nnode;
450 struct zserv *client;
451
452 if (IS_ZEBRA_DEBUG_EVENT)
453 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADD %s(%u)", ifp->name,
454 ifp->vrf_id);
455
456 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
457 client->ifadd_cnt++;
458 zsend_interface_add(client, ifp);
459 zsend_interface_link_params(client, ifp);
460 }
461 }
462
463 void zebra_interface_delete_update(struct interface *ifp)
464 {
465 struct listnode *node, *nnode;
466 struct zserv *client;
467
468 if (IS_ZEBRA_DEBUG_EVENT)
469 zlog_debug("MESSAGE: ZEBRA_INTERFACE_DELETE %s(%u)",
470 ifp->name, ifp->vrf_id);
471
472 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
473 client->ifdel_cnt++;
474 zsend_interface_delete(client, ifp);
475 }
476 }
477
478 /* Interface address addition. */
479 void zebra_interface_address_add_update(struct interface *ifp,
480 struct connected *ifc)
481 {
482 struct listnode *node, *nnode;
483 struct zserv *client;
484 struct prefix *p;
485
486 if (IS_ZEBRA_DEBUG_EVENT) {
487 char buf[PREFIX_STRLEN];
488
489 p = ifc->address;
490 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %s on %s(%u)",
491 prefix2str(p, buf, sizeof(buf)), ifp->name,
492 ifp->vrf_id);
493 }
494
495 if (!CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
496 flog_warn(
497 EC_ZEBRA_ADVERTISING_UNUSABLE_ADDR,
498 "WARNING: advertising address to clients that is not yet usable.");
499
500 zebra_vxlan_add_del_gw_macip(ifp, ifc->address, 1);
501
502 router_id_add_address(ifc);
503
504 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client))
505 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL)) {
506 client->connected_rt_add_cnt++;
507 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD,
508 client, ifp, ifc);
509 }
510 }
511
512 /* Interface address deletion. */
513 void zebra_interface_address_delete_update(struct interface *ifp,
514 struct connected *ifc)
515 {
516 struct listnode *node, *nnode;
517 struct zserv *client;
518 struct prefix *p;
519
520 if (IS_ZEBRA_DEBUG_EVENT) {
521 char buf[PREFIX_STRLEN];
522
523 p = ifc->address;
524 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %s on %s(%u)",
525 prefix2str(p, buf, sizeof(buf)),
526 ifp->name, ifp->vrf_id);
527 }
528
529 zebra_vxlan_add_del_gw_macip(ifp, ifc->address, 0);
530
531 router_id_del_address(ifc);
532
533 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client))
534 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL)) {
535 client->connected_rt_del_cnt++;
536 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_DELETE,
537 client, ifp, ifc);
538 }
539 }
540
541 /* Interface VRF change. May need to delete from clients not interested in
542 * the new VRF. Note that this function is invoked *prior* to the VRF change.
543 */
544 void zebra_interface_vrf_update_del(struct interface *ifp, vrf_id_t new_vrf_id)
545 {
546 struct listnode *node, *nnode;
547 struct zserv *client;
548
549 if (IS_ZEBRA_DEBUG_EVENT)
550 zlog_debug(
551 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/DEL %s VRF Id %u -> %u",
552 ifp->name, ifp->vrf_id, new_vrf_id);
553
554 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
555 /* Need to delete if the client is not interested in the new
556 * VRF. */
557 zsend_interface_update(ZEBRA_INTERFACE_DOWN, client, ifp);
558 client->ifdel_cnt++;
559 zsend_interface_delete(client, ifp);
560 zsend_interface_vrf_update(client, ifp, new_vrf_id);
561 }
562 }
563
564 /* Interface VRF change. This function is invoked *post* VRF change and sends an
565 * add to clients who are interested in the new VRF but not in the old VRF.
566 */
567 void zebra_interface_vrf_update_add(struct interface *ifp, vrf_id_t old_vrf_id)
568 {
569 struct listnode *node, *nnode;
570 struct zserv *client;
571
572 if (IS_ZEBRA_DEBUG_EVENT)
573 zlog_debug(
574 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/ADD %s VRF Id %u -> %u",
575 ifp->name, old_vrf_id, ifp->vrf_id);
576
577 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client)) {
578 /* Need to add if the client is interested in the new VRF. */
579 client->ifadd_cnt++;
580 zsend_interface_add(client, ifp);
581 zsend_interface_addresses(client, ifp);
582 }
583 }
584
585 int zebra_add_import_table_entry(struct zebra_vrf *zvrf, struct route_node *rn,
586 struct route_entry *re, const char *rmap_name)
587 {
588 struct route_entry *newre;
589 struct route_entry *same;
590 struct prefix p;
591 route_map_result_t ret = RMAP_PERMITMATCH;
592 afi_t afi;
593
594 afi = family2afi(rn->p.family);
595 if (rmap_name)
596 ret = zebra_import_table_route_map_check(
597 afi, re->type, re->instance, &rn->p, re->ng.nexthop,
598 zvrf->vrf->vrf_id, re->tag, rmap_name);
599
600 if (ret != RMAP_PERMITMATCH) {
601 UNSET_FLAG(re->flags, ZEBRA_FLAG_SELECTED);
602 zebra_del_import_table_entry(zvrf, rn, re);
603 return 0;
604 }
605
606 prefix_copy(&p, &rn->p);
607
608 RNODE_FOREACH_RE (rn, same) {
609 if (CHECK_FLAG(same->status, ROUTE_ENTRY_REMOVED))
610 continue;
611
612 if (same->type == re->type && same->instance == re->instance
613 && same->table == re->table
614 && same->type != ZEBRA_ROUTE_CONNECT)
615 break;
616 }
617
618 if (same) {
619 UNSET_FLAG(same->flags, ZEBRA_FLAG_SELECTED);
620 zebra_del_import_table_entry(zvrf, rn, same);
621 }
622
623 newre = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
624 newre->type = ZEBRA_ROUTE_TABLE;
625 newre->distance = zebra_import_table_distance[afi][re->table];
626 newre->flags = re->flags;
627 newre->metric = re->metric;
628 newre->mtu = re->mtu;
629 newre->table = zvrf->table_id;
630 newre->nexthop_num = 0;
631 newre->uptime = monotime(NULL);
632 newre->instance = re->table;
633 route_entry_copy_nexthops(newre, re->ng.nexthop);
634
635 rib_add_multipath(afi, SAFI_UNICAST, &p, NULL, newre);
636
637 return 0;
638 }
639
640 int zebra_del_import_table_entry(struct zebra_vrf *zvrf, struct route_node *rn,
641 struct route_entry *re)
642 {
643 struct prefix p;
644 afi_t afi;
645
646 afi = family2afi(rn->p.family);
647 prefix_copy(&p, &rn->p);
648
649 rib_delete(afi, SAFI_UNICAST, zvrf->vrf->vrf_id, ZEBRA_ROUTE_TABLE,
650 re->table, re->flags, &p, NULL, re->ng.nexthop,
651 zvrf->table_id, re->metric, re->distance, false);
652
653 return 0;
654 }
655
656 /* Assuming no one calls this with the main routing table */
657 int zebra_import_table(afi_t afi, vrf_id_t vrf_id, uint32_t table_id,
658 uint32_t distance, const char *rmap_name, int add)
659 {
660 struct route_table *table;
661 struct route_entry *re;
662 struct route_node *rn;
663 struct zebra_vrf *zvrf = zebra_vrf_lookup_by_id(vrf_id);
664
665 if (!is_zebra_valid_kernel_table(table_id)
666 || (table_id == RT_TABLE_MAIN))
667 return (-1);
668
669 if (afi >= AFI_MAX)
670 return (-1);
671
672 table = zebra_vrf_table_with_table_id(afi, SAFI_UNICAST, vrf_id,
673 table_id);
674 if (table == NULL) {
675 return 0;
676 } else if (IS_ZEBRA_DEBUG_RIB) {
677 zlog_debug("%s routes from table %d",
678 add ? "Importing" : "Unimporting", table_id);
679 }
680
681 if (add) {
682 if (rmap_name)
683 zebra_add_import_table_route_map(afi, rmap_name,
684 table_id);
685 else {
686 rmap_name =
687 zebra_get_import_table_route_map(afi, table_id);
688 if (rmap_name) {
689 zebra_del_import_table_route_map(afi, table_id);
690 rmap_name = NULL;
691 }
692 }
693
694 zebra_import_table_used[afi][table_id] = 1;
695 zebra_import_table_distance[afi][table_id] = distance;
696 } else {
697 zebra_import_table_used[afi][table_id] = 0;
698 zebra_import_table_distance[afi][table_id] =
699 ZEBRA_TABLE_DISTANCE_DEFAULT;
700
701 rmap_name = zebra_get_import_table_route_map(afi, table_id);
702 if (rmap_name) {
703 zebra_del_import_table_route_map(afi, table_id);
704 rmap_name = NULL;
705 }
706 }
707
708 for (rn = route_top(table); rn; rn = route_next(rn)) {
709 /* For each entry in the non-default routing table,
710 * add the entry in the main table
711 */
712 if (!rn->info)
713 continue;
714
715 RNODE_FOREACH_RE (rn, re) {
716 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
717 continue;
718 break;
719 }
720
721 if (!re)
722 continue;
723
724 if (((afi == AFI_IP) && (rn->p.family == AF_INET))
725 || ((afi == AFI_IP6) && (rn->p.family == AF_INET6))) {
726 if (add)
727 zebra_add_import_table_entry(zvrf, rn, re,
728 rmap_name);
729 else
730 zebra_del_import_table_entry(zvrf, rn, re);
731 }
732 }
733 return 0;
734 }
735
736 int zebra_import_table_config(struct vty *vty, vrf_id_t vrf_id)
737 {
738 int i;
739 afi_t afi;
740 int write = 0;
741 char afi_str[AFI_MAX][10] = {"", "ip", "ipv6", "ethernet"};
742 const char *rmap_name;
743
744 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
745 for (i = 1; i < ZEBRA_KERNEL_TABLE_MAX; i++) {
746 if (!is_zebra_import_table_enabled(afi, vrf_id, i))
747 continue;
748
749 if (zebra_import_table_distance[afi][i]
750 != ZEBRA_TABLE_DISTANCE_DEFAULT) {
751 vty_out(vty, "%s import-table %d distance %d",
752 afi_str[afi], i,
753 zebra_import_table_distance[afi][i]);
754 } else {
755 vty_out(vty, "%s import-table %d", afi_str[afi],
756 i);
757 }
758
759 rmap_name = zebra_get_import_table_route_map(afi, i);
760 if (rmap_name)
761 vty_out(vty, " route-map %s", rmap_name);
762
763 vty_out(vty, "\n");
764 write = 1;
765 }
766 }
767
768 return write;
769 }
770
771 static void zebra_import_table_rm_update_vrf_afi(struct zebra_vrf *zvrf,
772 afi_t afi, int table_id,
773 const char *rmap)
774 {
775 struct route_table *table;
776 struct route_entry *re;
777 struct route_node *rn;
778 const char *rmap_name;
779
780 rmap_name = zebra_get_import_table_route_map(afi, table_id);
781 if ((!rmap_name) || (strcmp(rmap_name, rmap) != 0))
782 return;
783
784 table = zebra_vrf_table_with_table_id(afi, SAFI_UNICAST,
785 zvrf->vrf->vrf_id, table_id);
786 if (!table) {
787 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
788 zlog_debug("%s: Table id=%d not found", __func__,
789 table_id);
790 return;
791 }
792
793 for (rn = route_top(table); rn; rn = route_next(rn)) {
794 /*
795 * For each entry in the non-default routing table,
796 * add the entry in the main table
797 */
798 if (!rn->info)
799 continue;
800
801 RNODE_FOREACH_RE (rn, re) {
802 if (CHECK_FLAG(re->status, ROUTE_ENTRY_REMOVED))
803 continue;
804 break;
805 }
806
807 if (!re)
808 continue;
809
810 if (((afi == AFI_IP) && (rn->p.family == AF_INET))
811 || ((afi == AFI_IP6) && (rn->p.family == AF_INET6)))
812 zebra_add_import_table_entry(zvrf, rn, re, rmap_name);
813 }
814
815 return;
816 }
817
818 static void zebra_import_table_rm_update_vrf(struct zebra_vrf *zvrf,
819 const char *rmap)
820 {
821 afi_t afi;
822 int i;
823
824 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
825 for (i = 1; i < ZEBRA_KERNEL_TABLE_MAX; i++) {
826 if (!is_zebra_import_table_enabled(
827 afi, zvrf->vrf->vrf_id, i))
828 continue;
829
830 zebra_import_table_rm_update_vrf_afi(zvrf, afi, i,
831 rmap);
832 }
833 }
834 }
835
836 void zebra_import_table_rm_update(const char *rmap)
837 {
838 struct vrf *vrf;
839 struct zebra_vrf *zvrf;
840
841 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name) {
842 zvrf = vrf->info;
843
844 if (!zvrf)
845 continue;
846
847 zebra_import_table_rm_update_vrf(zvrf, rmap);
848 }
849 }
850
851 /* Interface parameters update */
852 void zebra_interface_parameters_update(struct interface *ifp)
853 {
854 struct listnode *node, *nnode;
855 struct zserv *client;
856
857 if (IS_ZEBRA_DEBUG_EVENT)
858 zlog_debug("MESSAGE: ZEBRA_INTERFACE_LINK_PARAMS %s(%u)",
859 ifp->name, ifp->vrf_id);
860
861 for (ALL_LIST_ELEMENTS(zrouter.client_list, node, nnode, client))
862 zsend_interface_link_params(client, ifp);
863 }