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