1 /* Redistribution Handler
2 * Copyright (C) 1998 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
33 #include "srcdest_table.h"
35 #include "zebra/rib.h"
36 #include "zebra/zserv.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/zebra_memory.h"
44 #include "zebra/zebra_vxlan.h"
46 #define ZEBRA_PTM_SUPPORT
48 /* array holding redistribute info about table redistribution */
49 /* bit AFI is set if that AFI is redistributing routes from this table */
50 static int zebra_import_table_used
[AFI_MAX
][ZEBRA_KERNEL_TABLE_MAX
];
51 static u_int32_t zebra_import_table_distance
[AFI_MAX
][ZEBRA_KERNEL_TABLE_MAX
];
53 int is_zebra_import_table_enabled(afi_t afi
, u_int32_t table_id
)
56 * Make sure that what we are called with actualy makes sense
61 if (is_zebra_valid_kernel_table(table_id
))
62 return zebra_import_table_used
[afi
][table_id
];
66 static void zebra_redistribute_default(struct zserv
*client
, vrf_id_t vrf_id
)
70 struct route_table
*table
;
71 struct route_node
*rn
;
72 struct route_entry
*newre
;
74 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
76 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, vrf_id
);
80 /* Lookup default route. */
81 memset(&p
, 0, sizeof(p
));
82 p
.family
= afi2family(afi
);
83 rn
= route_node_lookup(table
, &p
);
87 RNODE_FOREACH_RE (rn
, newre
) {
88 if (CHECK_FLAG(newre
->flags
, ZEBRA_FLAG_SELECTED
)
89 && newre
->distance
!= DISTANCE_INFINITY
)
90 zsend_redistribute_route(
91 ZEBRA_REDISTRIBUTE_ROUTE_ADD
, client
,
95 route_unlock_node(rn
);
99 /* Redistribute routes. */
100 static void zebra_redistribute(struct zserv
*client
, int type
, u_short instance
,
101 vrf_id_t vrf_id
, int afi
)
103 struct route_entry
*newre
;
104 struct route_table
*table
;
105 struct route_node
*rn
;
107 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, vrf_id
);
111 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
112 RNODE_FOREACH_RE (rn
, newre
) {
113 struct prefix
*dst_p
, *src_p
;
114 srcdest_rnode_prefixes(rn
, &dst_p
, &src_p
);
116 if (IS_ZEBRA_DEBUG_EVENT
)
118 "%s: client %s vrf %d checking: selected=%d, type=%d, distance=%d, "
119 "zebra_check_addr=%d",
121 zebra_route_string(client
->proto
),
122 vrf_id
, CHECK_FLAG(newre
->flags
,
123 ZEBRA_FLAG_SELECTED
),
124 newre
->type
, newre
->distance
,
125 zebra_check_addr(dst_p
));
127 if (!CHECK_FLAG(newre
->flags
, ZEBRA_FLAG_SELECTED
))
129 if ((type
!= ZEBRA_ROUTE_ALL
130 && (newre
->type
!= type
131 || newre
->instance
!= instance
)))
133 if (newre
->distance
== DISTANCE_INFINITY
)
135 if (!zebra_check_addr(dst_p
))
138 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_ADD
,
139 client
, dst_p
, src_p
, newre
);
143 /* Either advertise a route for redistribution to registered clients or */
144 /* withdraw redistribution if add cannot be done for client */
145 void redistribute_update(struct prefix
*p
, struct prefix
*src_p
,
146 struct route_entry
*re
, struct route_entry
*prev_re
)
148 struct listnode
*node
, *nnode
;
149 struct zserv
*client
;
150 int send_redistribute
;
152 char buf
[INET6_ADDRSTRLEN
];
154 if (IS_ZEBRA_DEBUG_RIB
) {
155 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
157 "%u:%s/%d: Redist update re %p (type %d), old %p (type %d)",
158 re
->vrf_id
, buf
, p
->prefixlen
, re
, re
->type
, prev_re
,
159 prev_re
? prev_re
->type
: -1);
162 afi
= family2afi(p
->family
);
164 zlog_warn("%s: Unknown AFI/SAFI prefix received\n",
169 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
170 send_redistribute
= 0;
172 if (is_default_prefix(p
)
173 && vrf_bitmap_check(client
->redist_default
, re
->vrf_id
))
174 send_redistribute
= 1;
175 else if (vrf_bitmap_check(client
->redist
[afi
][ZEBRA_ROUTE_ALL
],
177 send_redistribute
= 1;
178 else if (re
->instance
179 && redist_check_instance(
180 &client
->mi_redist
[afi
][re
->type
],
182 send_redistribute
= 1;
183 else if (vrf_bitmap_check(client
->redist
[afi
][re
->type
],
185 send_redistribute
= 1;
187 if (send_redistribute
) {
188 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_ADD
,
189 client
, p
, src_p
, re
);
192 && redist_check_instance(
193 &client
->mi_redist
[afi
]
197 client
->redist
[afi
][prev_re
->type
],
199 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_DEL
,
200 client
, p
, src_p
, prev_re
);
205 void redistribute_delete(struct prefix
*p
, struct prefix
*src_p
,
206 struct route_entry
*re
)
208 struct listnode
*node
, *nnode
;
209 struct zserv
*client
;
210 char buf
[INET6_ADDRSTRLEN
];
213 if (IS_ZEBRA_DEBUG_RIB
) {
214 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
215 zlog_debug("%u:%s/%d: Redist delete re %p (type %d)",
216 re
->vrf_id
, buf
, p
->prefixlen
, re
, re
->type
);
219 /* Add DISTANCE_INFINITY check. */
220 if (re
->distance
== DISTANCE_INFINITY
)
223 afi
= family2afi(p
->family
);
225 zlog_warn("%s: Unknown AFI/SAFI prefix received\n",
230 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
231 if ((is_default_prefix(p
)
232 && vrf_bitmap_check(client
->redist_default
, re
->vrf_id
))
233 || vrf_bitmap_check(client
->redist
[afi
][ZEBRA_ROUTE_ALL
],
236 && redist_check_instance(
237 &client
->mi_redist
[afi
][re
->type
],
239 || vrf_bitmap_check(client
->redist
[afi
][re
->type
],
241 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_DEL
,
242 client
, p
, src_p
, re
);
247 void zebra_redistribute_add(ZAPI_HANDLER_ARGS
)
253 STREAM_GETC(msg
, afi
);
254 STREAM_GETC(msg
, type
);
255 STREAM_GETW(msg
, instance
);
257 if (IS_ZEBRA_DEBUG_EVENT
)
259 "%s: client proto %s afi=%d, wants %s, vrf %d, instance=%d",
260 __func__
, zebra_route_string(client
->proto
), afi
,
261 zebra_route_string(type
), zvrf_id(zvrf
), instance
);
263 if (afi
== 0 || afi
> AFI_MAX
) {
264 zlog_warn("%s: Specified afi %d does not exist",
265 __PRETTY_FUNCTION__
, afi
);
269 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
) {
270 zlog_warn("%s: Specified Route Type %d does not exist",
271 __PRETTY_FUNCTION__
, type
);
276 if (!redist_check_instance(&client
->mi_redist
[afi
][type
],
278 redist_add_instance(&client
->mi_redist
[afi
][type
],
280 zebra_redistribute(client
, type
, instance
,
284 if (!vrf_bitmap_check(client
->redist
[afi
][type
],
286 if (IS_ZEBRA_DEBUG_EVENT
)
287 zlog_debug("%s: setting vrf %d redist bitmap",
288 __func__
, zvrf_id(zvrf
));
289 vrf_bitmap_set(client
->redist
[afi
][type
],
291 zebra_redistribute(client
, type
, 0, zvrf_id(zvrf
), afi
);
299 void zebra_redistribute_delete(ZAPI_HANDLER_ARGS
)
305 STREAM_GETC(msg
, afi
);
306 STREAM_GETC(msg
, type
);
307 STREAM_GETW(msg
, instance
);
309 if (afi
== 0 || afi
> AFI_MAX
) {
310 zlog_warn("%s: Specified afi %d does not exist",
311 __PRETTY_FUNCTION__
, afi
);
315 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
) {
316 zlog_warn("%s: Specified Route Type %d does not exist",
317 __PRETTY_FUNCTION__
, type
);
322 * NOTE: no need to withdraw the previously advertised routes. The
324 * themselves should keep track of the received routes from zebra and
325 * withdraw them when necessary.
328 redist_del_instance(&client
->mi_redist
[afi
][type
], instance
);
330 vrf_bitmap_unset(client
->redist
[afi
][type
], zvrf_id(zvrf
));
336 void zebra_redistribute_default_add(ZAPI_HANDLER_ARGS
)
338 vrf_bitmap_set(client
->redist_default
, zvrf_id(zvrf
));
339 zebra_redistribute_default(client
, zvrf_id(zvrf
));
342 void zebra_redistribute_default_delete(ZAPI_HANDLER_ARGS
)
344 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
347 /* Interface up information. */
348 void zebra_interface_up_update(struct interface
*ifp
)
350 struct listnode
*node
, *nnode
;
351 struct zserv
*client
;
353 if (IS_ZEBRA_DEBUG_EVENT
)
354 zlog_debug("MESSAGE: ZEBRA_INTERFACE_UP %s", ifp
->name
);
356 if (ifp
->ptm_status
|| !ifp
->ptm_enable
) {
357 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
358 if (client
->ifinfo
) {
359 zsend_interface_update(ZEBRA_INTERFACE_UP
,
361 zsend_interface_link_params(client
, ifp
);
366 /* Interface down information. */
367 void zebra_interface_down_update(struct interface
*ifp
)
369 struct listnode
*node
, *nnode
;
370 struct zserv
*client
;
372 if (IS_ZEBRA_DEBUG_EVENT
)
373 zlog_debug("MESSAGE: ZEBRA_INTERFACE_DOWN %s", ifp
->name
);
375 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
376 zsend_interface_update(ZEBRA_INTERFACE_DOWN
, client
, ifp
);
380 /* Interface information update. */
381 void zebra_interface_add_update(struct interface
*ifp
)
383 struct listnode
*node
, *nnode
;
384 struct zserv
*client
;
386 if (IS_ZEBRA_DEBUG_EVENT
)
387 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADD %s[%d]", ifp
->name
,
390 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
391 if (client
->ifinfo
) {
393 zsend_interface_add(client
, ifp
);
394 zsend_interface_link_params(client
, ifp
);
398 void zebra_interface_delete_update(struct interface
*ifp
)
400 struct listnode
*node
, *nnode
;
401 struct zserv
*client
;
403 if (IS_ZEBRA_DEBUG_EVENT
)
404 zlog_debug("MESSAGE: ZEBRA_INTERFACE_DELETE %s", ifp
->name
);
406 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
408 zsend_interface_delete(client
, ifp
);
412 /* Interface address addition. */
413 void zebra_interface_address_add_update(struct interface
*ifp
,
414 struct connected
*ifc
)
416 struct listnode
*node
, *nnode
;
417 struct zserv
*client
;
420 if (IS_ZEBRA_DEBUG_EVENT
) {
421 char buf
[PREFIX_STRLEN
];
424 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %s on %s",
425 prefix2str(p
, buf
, sizeof(buf
)), ifc
->ifp
->name
);
428 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
430 "WARNING: advertising address to clients that is not yet usable.");
432 zebra_vxlan_add_del_gw_macip(ifp
, ifc
->address
, 1);
434 router_id_add_address(ifc
);
436 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
437 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
)) {
438 client
->connected_rt_add_cnt
++;
439 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
,
444 /* Interface address deletion. */
445 void zebra_interface_address_delete_update(struct interface
*ifp
,
446 struct connected
*ifc
)
448 struct listnode
*node
, *nnode
;
449 struct zserv
*client
;
452 if (IS_ZEBRA_DEBUG_EVENT
) {
453 char buf
[PREFIX_STRLEN
];
456 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %s on %s",
457 prefix2str(p
, buf
, sizeof(buf
)), ifc
->ifp
->name
);
460 zebra_vxlan_add_del_gw_macip(ifp
, ifc
->address
, 0);
462 router_id_del_address(ifc
);
464 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
465 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
)) {
466 client
->connected_rt_del_cnt
++;
467 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_DELETE
,
472 /* Interface VRF change. May need to delete from clients not interested in
473 * the new VRF. Note that this function is invoked *prior* to the VRF change.
475 void zebra_interface_vrf_update_del(struct interface
*ifp
, vrf_id_t new_vrf_id
)
477 struct listnode
*node
, *nnode
;
478 struct zserv
*client
;
480 if (IS_ZEBRA_DEBUG_EVENT
)
482 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/DEL %s VRF Id %u -> %u",
483 ifp
->name
, ifp
->vrf_id
, new_vrf_id
);
485 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
486 /* Need to delete if the client is not interested in the new
488 zsend_interface_update(ZEBRA_INTERFACE_DOWN
, client
, ifp
);
490 zsend_interface_delete(client
, ifp
);
491 zsend_interface_vrf_update(client
, ifp
, new_vrf_id
);
495 /* Interface VRF change. This function is invoked *post* VRF change and sends an
496 * add to clients who are interested in the new VRF but not in the old VRF.
498 void zebra_interface_vrf_update_add(struct interface
*ifp
, vrf_id_t old_vrf_id
)
500 struct listnode
*node
, *nnode
;
501 struct zserv
*client
;
503 if (IS_ZEBRA_DEBUG_EVENT
)
505 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/ADD %s VRF Id %u -> %u",
506 ifp
->name
, old_vrf_id
, ifp
->vrf_id
);
508 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
509 /* Need to add if the client is interested in the new VRF. */
511 zsend_interface_add(client
, ifp
);
512 zsend_interface_addresses(client
, ifp
);
516 int zebra_add_import_table_entry(struct route_node
*rn
, struct route_entry
*re
,
517 const char *rmap_name
)
519 struct route_entry
*newre
;
520 struct route_entry
*same
;
522 route_map_result_t ret
= RMAP_MATCH
;
525 afi
= family2afi(rn
->p
.family
);
527 ret
= zebra_import_table_route_map_check(
528 afi
, re
->type
, &rn
->p
, re
->ng
.nexthop
, re
->vrf_id
,
531 if (ret
!= RMAP_MATCH
) {
532 zebra_del_import_table_entry(rn
, re
);
536 prefix_copy(&p
, &rn
->p
);
538 RNODE_FOREACH_RE (rn
, same
) {
539 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
542 if (same
->type
== re
->type
&& same
->instance
== re
->instance
543 && same
->table
== re
->table
544 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
549 zebra_del_import_table_entry(rn
, same
);
551 newre
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
552 newre
->type
= ZEBRA_ROUTE_TABLE
;
553 newre
->distance
= zebra_import_table_distance
[afi
][re
->table
];
554 newre
->flags
= re
->flags
;
555 newre
->metric
= re
->metric
;
556 newre
->mtu
= re
->mtu
;
557 newre
->table
= zebrad
.rtm_table_default
;
558 newre
->nexthop_num
= 0;
559 newre
->uptime
= time(NULL
);
560 newre
->instance
= re
->table
;
561 route_entry_copy_nexthops(newre
, re
->ng
.nexthop
);
563 rib_add_multipath(afi
, SAFI_UNICAST
, &p
, NULL
, newre
);
568 int zebra_del_import_table_entry(struct route_node
*rn
, struct route_entry
*re
)
573 afi
= family2afi(rn
->p
.family
);
574 prefix_copy(&p
, &rn
->p
);
576 rib_delete(afi
, SAFI_UNICAST
, re
->vrf_id
, ZEBRA_ROUTE_TABLE
, re
->table
,
577 re
->flags
, &p
, NULL
, re
->ng
.nexthop
,
578 zebrad
.rtm_table_default
, re
->metric
, false, NULL
);
583 /* Assuming no one calls this with the main routing table */
584 int zebra_import_table(afi_t afi
, u_int32_t table_id
, u_int32_t distance
,
585 const char *rmap_name
, int add
)
587 struct route_table
*table
;
588 struct route_entry
*re
;
589 struct route_node
*rn
;
591 if (!is_zebra_valid_kernel_table(table_id
)
592 || ((table_id
== RT_TABLE_MAIN
)
593 || (table_id
== zebrad
.rtm_table_default
)))
599 table
= zebra_vrf_other_route_table(afi
, table_id
, VRF_DEFAULT
);
602 } else if (IS_ZEBRA_DEBUG_RIB
) {
603 zlog_debug("%s routes from table %d",
604 add
? "Importing" : "Unimporting", table_id
);
609 zebra_add_import_table_route_map(afi
, rmap_name
,
613 zebra_get_import_table_route_map(afi
, table_id
);
615 zebra_del_import_table_route_map(afi
, table_id
);
618 zebra_import_table_used
[afi
][table_id
] = 1;
619 zebra_import_table_distance
[afi
][table_id
] = distance
;
621 zebra_import_table_used
[afi
][table_id
] = 0;
622 zebra_import_table_distance
[afi
][table_id
] =
623 ZEBRA_TABLE_DISTANCE_DEFAULT
;
625 rmap_name
= zebra_get_import_table_route_map(afi
, table_id
);
627 zebra_del_import_table_route_map(afi
, table_id
);
630 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
631 /* For each entry in the non-default routing table,
632 * add the entry in the main table
637 RNODE_FOREACH_RE (rn
, re
) {
638 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
646 if (((afi
== AFI_IP
) && (rn
->p
.family
== AF_INET
))
647 || ((afi
== AFI_IP6
) && (rn
->p
.family
== AF_INET6
))) {
649 zebra_add_import_table_entry(rn
, re
, rmap_name
);
651 zebra_del_import_table_entry(rn
, re
);
657 int zebra_import_table_config(struct vty
*vty
)
662 char afi_str
[AFI_MAX
][10] = {"", "ip", "ipv6", "ethernet"};
663 const char *rmap_name
;
665 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
666 for (i
= 1; i
< ZEBRA_KERNEL_TABLE_MAX
; i
++) {
667 if (!is_zebra_import_table_enabled(afi
, i
))
670 if (zebra_import_table_distance
[afi
][i
]
671 != ZEBRA_TABLE_DISTANCE_DEFAULT
) {
672 vty_out(vty
, "%s import-table %d distance %d",
674 zebra_import_table_distance
[afi
][i
]);
676 vty_out(vty
, "%s import-table %d", afi_str
[afi
],
680 rmap_name
= zebra_get_import_table_route_map(afi
, i
);
682 vty_out(vty
, " route-map %s", rmap_name
);
692 void zebra_import_table_rm_update()
696 struct route_table
*table
;
697 struct route_entry
*re
;
698 struct route_node
*rn
;
699 const char *rmap_name
;
701 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
702 for (i
= 1; i
< ZEBRA_KERNEL_TABLE_MAX
; i
++) {
703 if (!is_zebra_import_table_enabled(afi
, i
))
706 rmap_name
= zebra_get_import_table_route_map(afi
, i
);
710 table
= zebra_vrf_other_route_table(afi
, i
,
712 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
713 /* For each entry in the non-default
715 * add the entry in the main table
720 RNODE_FOREACH_RE (rn
, re
) {
721 if (CHECK_FLAG(re
->status
,
722 ROUTE_ENTRY_REMOVED
))
731 && (rn
->p
.family
== AF_INET
))
733 && (rn
->p
.family
== AF_INET6
)))
734 zebra_add_import_table_entry(rn
, re
,
743 /* Interface parameters update */
744 void zebra_interface_parameters_update(struct interface
*ifp
)
746 struct listnode
*node
, *nnode
;
747 struct zserv
*client
;
749 if (IS_ZEBRA_DEBUG_EVENT
)
750 zlog_debug("MESSAGE: ZEBRA_INTERFACE_LINK_PARAMS %s",
753 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
755 zsend_interface_link_params(client
, ifp
);