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