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