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(int command
, struct zserv
*client
, int length
,
248 struct zebra_vrf
*zvrf
)
254 STREAM_GETC(client
->ibuf
, afi
);
255 STREAM_GETC(client
->ibuf
, type
);
256 STREAM_GETW(client
->ibuf
, instance
);
258 if (IS_ZEBRA_DEBUG_EVENT
)
260 "%s: client proto %s afi=%d, wants %s, vrf %d, instance=%d",
261 __func__
, zebra_route_string(client
->proto
), afi
,
262 zebra_route_string(type
), zvrf_id(zvrf
), instance
);
264 if (afi
== 0 || afi
> AFI_MAX
) {
265 zlog_warn("%s: Specified afi %d does not exist",
266 __PRETTY_FUNCTION__
, afi
);
270 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
) {
271 zlog_warn("%s: Specified Route Type %d does not exist",
272 __PRETTY_FUNCTION__
, type
);
277 if (!redist_check_instance(&client
->mi_redist
[afi
][type
],
279 redist_add_instance(&client
->mi_redist
[afi
][type
],
281 zebra_redistribute(client
, type
, instance
,
285 if (!vrf_bitmap_check(client
->redist
[afi
][type
],
287 if (IS_ZEBRA_DEBUG_EVENT
)
288 zlog_debug("%s: setting vrf %d redist bitmap",
289 __func__
, zvrf_id(zvrf
));
290 vrf_bitmap_set(client
->redist
[afi
][type
],
292 zebra_redistribute(client
, type
, 0, zvrf_id(zvrf
), afi
);
300 void zebra_redistribute_delete(int command
, struct zserv
*client
, int length
,
301 struct zebra_vrf
*zvrf
)
307 STREAM_GETC(client
->ibuf
, afi
);
308 STREAM_GETC(client
->ibuf
, type
);
309 STREAM_GETW(client
->ibuf
, instance
);
311 if (afi
== 0 || afi
> AFI_MAX
) {
312 zlog_warn("%s: Specified afi %d does not exist",
313 __PRETTY_FUNCTION__
, afi
);
317 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
) {
318 zlog_warn("%s: Specified Route Type %d does not exist",
319 __PRETTY_FUNCTION__
, type
);
324 * NOTE: no need to withdraw the previously advertised routes. The
326 * themselves should keep track of the received routes from zebra and
327 * withdraw them when necessary.
330 redist_del_instance(&client
->mi_redist
[afi
][type
], instance
);
332 vrf_bitmap_unset(client
->redist
[afi
][type
], zvrf_id(zvrf
));
338 void zebra_redistribute_default_add(int command
, struct zserv
*client
,
339 int length
, struct zebra_vrf
*zvrf
)
341 vrf_bitmap_set(client
->redist_default
, zvrf_id(zvrf
));
342 zebra_redistribute_default(client
, zvrf_id(zvrf
));
345 void zebra_redistribute_default_delete(int command
, struct zserv
*client
,
346 int length
, struct zebra_vrf
*zvrf
)
348 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
351 /* Interface up information. */
352 void zebra_interface_up_update(struct interface
*ifp
)
354 struct listnode
*node
, *nnode
;
355 struct zserv
*client
;
357 if (IS_ZEBRA_DEBUG_EVENT
)
358 zlog_debug("MESSAGE: ZEBRA_INTERFACE_UP %s", ifp
->name
);
360 if (ifp
->ptm_status
|| !ifp
->ptm_enable
) {
361 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
362 if (client
->ifinfo
) {
363 zsend_interface_update(ZEBRA_INTERFACE_UP
,
365 zsend_interface_link_params(client
, ifp
);
370 /* Interface down information. */
371 void zebra_interface_down_update(struct interface
*ifp
)
373 struct listnode
*node
, *nnode
;
374 struct zserv
*client
;
376 if (IS_ZEBRA_DEBUG_EVENT
)
377 zlog_debug("MESSAGE: ZEBRA_INTERFACE_DOWN %s", ifp
->name
);
379 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
380 zsend_interface_update(ZEBRA_INTERFACE_DOWN
, client
, ifp
);
384 /* Interface information update. */
385 void zebra_interface_add_update(struct interface
*ifp
)
387 struct listnode
*node
, *nnode
;
388 struct zserv
*client
;
390 if (IS_ZEBRA_DEBUG_EVENT
)
391 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADD %s[%d]", ifp
->name
,
394 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
395 if (client
->ifinfo
) {
397 zsend_interface_add(client
, ifp
);
398 zsend_interface_link_params(client
, ifp
);
402 void zebra_interface_delete_update(struct interface
*ifp
)
404 struct listnode
*node
, *nnode
;
405 struct zserv
*client
;
407 if (IS_ZEBRA_DEBUG_EVENT
)
408 zlog_debug("MESSAGE: ZEBRA_INTERFACE_DELETE %s", ifp
->name
);
410 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
412 zsend_interface_delete(client
, ifp
);
416 /* Interface address addition. */
417 void zebra_interface_address_add_update(struct interface
*ifp
,
418 struct connected
*ifc
)
420 struct listnode
*node
, *nnode
;
421 struct zserv
*client
;
424 if (IS_ZEBRA_DEBUG_EVENT
) {
425 char buf
[PREFIX_STRLEN
];
428 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %s on %s",
429 prefix2str(p
, buf
, sizeof(buf
)), ifc
->ifp
->name
);
432 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
434 "WARNING: advertising address to clients that is not yet usable.");
436 zebra_vxlan_add_del_gw_macip(ifp
, ifc
->address
, 1);
438 router_id_add_address(ifc
);
440 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
441 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
)) {
442 client
->connected_rt_add_cnt
++;
443 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
,
448 /* Interface address deletion. */
449 void zebra_interface_address_delete_update(struct interface
*ifp
,
450 struct connected
*ifc
)
452 struct listnode
*node
, *nnode
;
453 struct zserv
*client
;
456 if (IS_ZEBRA_DEBUG_EVENT
) {
457 char buf
[PREFIX_STRLEN
];
460 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %s on %s",
461 prefix2str(p
, buf
, sizeof(buf
)), ifc
->ifp
->name
);
464 zebra_vxlan_add_del_gw_macip(ifp
, ifc
->address
, 0);
466 router_id_del_address(ifc
);
468 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
469 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
)) {
470 client
->connected_rt_del_cnt
++;
471 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_DELETE
,
476 /* Interface VRF change. May need to delete from clients not interested in
477 * the new VRF. Note that this function is invoked *prior* to the VRF change.
479 void zebra_interface_vrf_update_del(struct interface
*ifp
, vrf_id_t new_vrf_id
)
481 struct listnode
*node
, *nnode
;
482 struct zserv
*client
;
484 if (IS_ZEBRA_DEBUG_EVENT
)
486 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/DEL %s VRF Id %u -> %u",
487 ifp
->name
, ifp
->vrf_id
, new_vrf_id
);
489 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
490 /* Need to delete if the client is not interested in the new
492 zsend_interface_update(ZEBRA_INTERFACE_DOWN
, client
, ifp
);
494 zsend_interface_delete(client
, ifp
);
495 zsend_interface_vrf_update(client
, ifp
, new_vrf_id
);
499 /* Interface VRF change. This function is invoked *post* VRF change and sends an
500 * add to clients who are interested in the new VRF but not in the old VRF.
502 void zebra_interface_vrf_update_add(struct interface
*ifp
, vrf_id_t old_vrf_id
)
504 struct listnode
*node
, *nnode
;
505 struct zserv
*client
;
507 if (IS_ZEBRA_DEBUG_EVENT
)
509 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/ADD %s VRF Id %u -> %u",
510 ifp
->name
, old_vrf_id
, ifp
->vrf_id
);
512 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
513 /* Need to add if the client is interested in the new VRF. */
515 zsend_interface_add(client
, ifp
);
516 zsend_interface_addresses(client
, ifp
);
520 int zebra_add_import_table_entry(struct route_node
*rn
, struct route_entry
*re
,
521 const char *rmap_name
)
523 struct route_entry
*newre
;
524 struct route_entry
*same
;
526 route_map_result_t ret
= RMAP_MATCH
;
529 afi
= family2afi(rn
->p
.family
);
531 ret
= zebra_import_table_route_map_check(
532 afi
, re
->type
, &rn
->p
, re
->ng
.nexthop
, re
->vrf_id
,
535 if (ret
!= RMAP_MATCH
) {
536 zebra_del_import_table_entry(rn
, re
);
540 prefix_copy(&p
, &rn
->p
);
542 RNODE_FOREACH_RE (rn
, same
) {
543 if (CHECK_FLAG(same
->status
, ROUTE_ENTRY_REMOVED
))
546 if (same
->type
== re
->type
&& same
->instance
== re
->instance
547 && same
->table
== re
->table
548 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
553 zebra_del_import_table_entry(rn
, same
);
555 newre
= XCALLOC(MTYPE_RE
, sizeof(struct route_entry
));
556 newre
->type
= ZEBRA_ROUTE_TABLE
;
557 newre
->distance
= zebra_import_table_distance
[afi
][re
->table
];
558 newre
->flags
= re
->flags
;
559 newre
->metric
= re
->metric
;
560 newre
->mtu
= re
->mtu
;
561 newre
->table
= zebrad
.rtm_table_default
;
562 newre
->nexthop_num
= 0;
563 newre
->uptime
= time(NULL
);
564 newre
->instance
= re
->table
;
565 route_entry_copy_nexthops(newre
, re
->ng
.nexthop
);
567 rib_add_multipath(afi
, SAFI_UNICAST
, &p
, NULL
, newre
);
572 int zebra_del_import_table_entry(struct route_node
*rn
, struct route_entry
*re
)
577 afi
= family2afi(rn
->p
.family
);
578 prefix_copy(&p
, &rn
->p
);
580 rib_delete(afi
, SAFI_UNICAST
, re
->vrf_id
, ZEBRA_ROUTE_TABLE
, re
->table
,
581 re
->flags
, &p
, NULL
, re
->ng
.nexthop
,
582 zebrad
.rtm_table_default
, re
->metric
, false, NULL
);
587 /* Assuming no one calls this with the main routing table */
588 int zebra_import_table(afi_t afi
, u_int32_t table_id
, u_int32_t distance
,
589 const char *rmap_name
, int add
)
591 struct route_table
*table
;
592 struct route_entry
*re
;
593 struct route_node
*rn
;
595 if (!is_zebra_valid_kernel_table(table_id
)
596 || ((table_id
== RT_TABLE_MAIN
)
597 || (table_id
== zebrad
.rtm_table_default
)))
603 table
= zebra_vrf_other_route_table(afi
, table_id
, VRF_DEFAULT
);
606 } else if (IS_ZEBRA_DEBUG_RIB
) {
607 zlog_debug("%s routes from table %d",
608 add
? "Importing" : "Unimporting", table_id
);
613 zebra_add_import_table_route_map(afi
, rmap_name
,
617 zebra_get_import_table_route_map(afi
, table_id
);
619 zebra_del_import_table_route_map(afi
, table_id
);
622 zebra_import_table_used
[afi
][table_id
] = 1;
623 zebra_import_table_distance
[afi
][table_id
] = distance
;
625 zebra_import_table_used
[afi
][table_id
] = 0;
626 zebra_import_table_distance
[afi
][table_id
] =
627 ZEBRA_TABLE_DISTANCE_DEFAULT
;
629 rmap_name
= zebra_get_import_table_route_map(afi
, table_id
);
631 zebra_del_import_table_route_map(afi
, table_id
);
634 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
635 /* For each entry in the non-default routing table,
636 * add the entry in the main table
641 RNODE_FOREACH_RE (rn
, re
) {
642 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
650 if (((afi
== AFI_IP
) && (rn
->p
.family
== AF_INET
))
651 || ((afi
== AFI_IP6
) && (rn
->p
.family
== AF_INET6
))) {
653 zebra_add_import_table_entry(rn
, re
, rmap_name
);
655 zebra_del_import_table_entry(rn
, re
);
661 int zebra_import_table_config(struct vty
*vty
)
666 char afi_str
[AFI_MAX
][10] = {"", "ip", "ipv6", "ethernet"};
667 const char *rmap_name
;
669 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
670 for (i
= 1; i
< ZEBRA_KERNEL_TABLE_MAX
; i
++) {
671 if (!is_zebra_import_table_enabled(afi
, i
))
674 if (zebra_import_table_distance
[afi
][i
]
675 != ZEBRA_TABLE_DISTANCE_DEFAULT
) {
676 vty_out(vty
, "%s import-table %d distance %d",
678 zebra_import_table_distance
[afi
][i
]);
680 vty_out(vty
, "%s import-table %d", afi_str
[afi
],
684 rmap_name
= zebra_get_import_table_route_map(afi
, i
);
686 vty_out(vty
, " route-map %s", rmap_name
);
696 void zebra_import_table_rm_update()
700 struct route_table
*table
;
701 struct route_entry
*re
;
702 struct route_node
*rn
;
703 const char *rmap_name
;
705 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
706 for (i
= 1; i
< ZEBRA_KERNEL_TABLE_MAX
; i
++) {
707 if (!is_zebra_import_table_enabled(afi
, i
))
710 rmap_name
= zebra_get_import_table_route_map(afi
, i
);
714 table
= zebra_vrf_other_route_table(afi
, i
,
716 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
717 /* For each entry in the non-default
719 * add the entry in the main table
724 RNODE_FOREACH_RE (rn
, re
) {
725 if (CHECK_FLAG(re
->status
,
726 ROUTE_ENTRY_REMOVED
))
735 && (rn
->p
.family
== AF_INET
))
737 && (rn
->p
.family
== AF_INET6
)))
738 zebra_add_import_table_entry(rn
, re
,
747 /* Interface parameters update */
748 void zebra_interface_parameters_update(struct interface
*ifp
)
750 struct listnode
*node
, *nnode
;
751 struct zserv
*client
;
753 if (IS_ZEBRA_DEBUG_EVENT
)
754 zlog_debug("MESSAGE: ZEBRA_INTERFACE_LINK_PARAMS %s",
757 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
759 zsend_interface_link_params(client
, ifp
);