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 int is_default(struct prefix
*p
)
68 if (p
->family
== AF_INET
)
69 if (p
->u
.prefix4
.s_addr
== 0 && p
->prefixlen
== 0)
71 #if 0 /* IPv6 default separation is now pending until protocol daemon \
73 if (p
->family
== AF_INET6
)
74 if (IN6_IS_ADDR_UNSPECIFIED (&p
->u
.prefix6
) && p
->prefixlen
== 0)
80 static void zebra_redistribute_default(struct zserv
*client
, vrf_id_t vrf_id
)
84 struct route_table
*table
;
85 struct route_node
*rn
;
86 struct route_entry
*newre
;
88 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++) {
90 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, vrf_id
);
94 /* Lookup default route. */
95 memset(&p
, 0, sizeof(p
));
96 p
.family
= afi2family(afi
);
97 rn
= route_node_lookup(table
, &p
);
101 RNODE_FOREACH_RE(rn
, newre
)
102 if (CHECK_FLAG(newre
->flags
, ZEBRA_FLAG_SELECTED
)
103 && newre
->distance
!= DISTANCE_INFINITY
)
104 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_ADD
,
105 client
, &rn
->p
, NULL
, newre
);
107 route_unlock_node(rn
);
111 /* Redistribute routes. */
112 static void zebra_redistribute(struct zserv
*client
, int type
, u_short instance
,
113 vrf_id_t vrf_id
, int afi
)
115 struct route_entry
*newre
;
116 struct route_table
*table
;
117 struct route_node
*rn
;
119 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, vrf_id
);
123 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
124 RNODE_FOREACH_RE(rn
, newre
)
126 struct prefix
*dst_p
, *src_p
;
127 srcdest_rnode_prefixes(rn
, &dst_p
, &src_p
);
129 if (IS_ZEBRA_DEBUG_EVENT
)
131 "%s: checking: selected=%d, type=%d, distance=%d, "
132 "zebra_check_addr=%d",
134 CHECK_FLAG(newre
->flags
,
135 ZEBRA_FLAG_SELECTED
),
136 newre
->type
, newre
->distance
,
137 zebra_check_addr(dst_p
));
139 if (!CHECK_FLAG(newre
->flags
, ZEBRA_FLAG_SELECTED
))
141 if ((type
!= ZEBRA_ROUTE_ALL
142 && (newre
->type
!= type
143 || newre
->instance
!= instance
)))
145 if (newre
->distance
== DISTANCE_INFINITY
)
147 if (!zebra_check_addr(dst_p
))
150 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_ADD
,
151 client
, dst_p
, src_p
, newre
);
155 /* Either advertise a route for redistribution to registered clients or */
156 /* withdraw redistribution if add cannot be done for client */
157 void redistribute_update(struct prefix
*p
, struct prefix
*src_p
,
158 struct route_entry
*re
, struct route_entry
*prev_re
)
160 struct listnode
*node
, *nnode
;
161 struct zserv
*client
;
162 int send_redistribute
;
164 char buf
[INET6_ADDRSTRLEN
];
166 if (IS_ZEBRA_DEBUG_RIB
) {
167 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
169 "%u:%s/%d: Redist update re %p (type %d), old %p (type %d)",
170 re
->vrf_id
, buf
, p
->prefixlen
, re
, re
->type
, prev_re
,
171 prev_re
? prev_re
->type
: -1);
174 afi
= family2afi(p
->family
);
176 zlog_warn("%s: Unknown AFI/SAFI prefix received\n",
181 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
182 send_redistribute
= 0;
185 && vrf_bitmap_check(client
->redist_default
, re
->vrf_id
))
186 send_redistribute
= 1;
187 else if (vrf_bitmap_check(client
->redist
[afi
][ZEBRA_ROUTE_ALL
],
189 send_redistribute
= 1;
190 else if (re
->instance
191 && redist_check_instance(
192 &client
->mi_redist
[afi
][re
->type
],
194 send_redistribute
= 1;
195 else if (vrf_bitmap_check(client
->redist
[afi
][re
->type
],
197 send_redistribute
= 1;
199 if (send_redistribute
) {
200 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_ADD
,
201 client
, p
, src_p
, re
);
204 && redist_check_instance(
205 &client
->mi_redist
[afi
]
209 client
->redist
[afi
][prev_re
->type
],
211 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_DEL
,
212 client
, p
, src_p
, prev_re
);
217 void redistribute_delete(struct prefix
*p
, struct prefix
*src_p
,
218 struct route_entry
*re
)
220 struct listnode
*node
, *nnode
;
221 struct zserv
*client
;
222 char buf
[INET6_ADDRSTRLEN
];
225 if (IS_ZEBRA_DEBUG_RIB
) {
226 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
227 zlog_debug("%u:%s/%d: Redist delete re %p (type %d)",
228 re
->vrf_id
, buf
, p
->prefixlen
, re
, re
->type
);
231 /* Add DISTANCE_INFINITY check. */
232 if (re
->distance
== DISTANCE_INFINITY
)
235 afi
= family2afi(p
->family
);
237 zlog_warn("%s: Unknown AFI/SAFI prefix received\n",
242 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
244 && vrf_bitmap_check(client
->redist_default
, re
->vrf_id
))
245 || vrf_bitmap_check(client
->redist
[afi
][ZEBRA_ROUTE_ALL
],
248 && redist_check_instance(
249 &client
->mi_redist
[afi
][re
->type
],
251 || vrf_bitmap_check(client
->redist
[afi
][re
->type
],
253 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_DEL
,
254 client
, p
, src_p
, re
);
259 void zebra_redistribute_add(int command
, struct zserv
*client
, int length
,
260 struct zebra_vrf
*zvrf
)
266 afi
= stream_getc(client
->ibuf
);
267 type
= stream_getc(client
->ibuf
);
268 instance
= stream_getw(client
->ibuf
);
270 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
)
274 if (!redist_check_instance(&client
->mi_redist
[afi
][type
],
276 redist_add_instance(&client
->mi_redist
[afi
][type
],
278 zebra_redistribute(client
, type
, instance
,
282 if (!vrf_bitmap_check(client
->redist
[afi
][type
],
284 vrf_bitmap_set(client
->redist
[afi
][type
],
286 zebra_redistribute(client
, type
, 0, zvrf_id(zvrf
), afi
);
291 void zebra_redistribute_delete(int command
, struct zserv
*client
, int length
,
292 struct zebra_vrf
*zvrf
)
298 afi
= stream_getc(client
->ibuf
);
299 type
= stream_getc(client
->ibuf
);
300 instance
= stream_getw(client
->ibuf
);
302 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
)
306 * NOTE: no need to withdraw the previously advertised routes. The
308 * themselves should keep track of the received routes from zebra and
309 * withdraw them when necessary.
312 redist_del_instance(&client
->mi_redist
[afi
][type
], instance
);
314 vrf_bitmap_unset(client
->redist
[afi
][type
], zvrf_id(zvrf
));
317 void zebra_redistribute_default_add(int command
, struct zserv
*client
,
318 int length
, struct zebra_vrf
*zvrf
)
320 vrf_bitmap_set(client
->redist_default
, zvrf_id(zvrf
));
321 zebra_redistribute_default(client
, zvrf_id(zvrf
));
324 void zebra_redistribute_default_delete(int command
, struct zserv
*client
,
325 int length
, struct zebra_vrf
*zvrf
)
327 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
330 /* Interface up information. */
331 void zebra_interface_up_update(struct interface
*ifp
)
333 struct listnode
*node
, *nnode
;
334 struct zserv
*client
;
336 if (IS_ZEBRA_DEBUG_EVENT
)
337 zlog_debug("MESSAGE: ZEBRA_INTERFACE_UP %s", ifp
->name
);
339 if (ifp
->ptm_status
|| !ifp
->ptm_enable
) {
340 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
341 if (client
->ifinfo
) {
342 zsend_interface_update(ZEBRA_INTERFACE_UP
,
344 zsend_interface_link_params(client
, ifp
);
349 /* Interface down information. */
350 void zebra_interface_down_update(struct interface
*ifp
)
352 struct listnode
*node
, *nnode
;
353 struct zserv
*client
;
355 if (IS_ZEBRA_DEBUG_EVENT
)
356 zlog_debug("MESSAGE: ZEBRA_INTERFACE_DOWN %s", ifp
->name
);
358 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
359 zsend_interface_update(ZEBRA_INTERFACE_DOWN
, client
, ifp
);
363 /* Interface information update. */
364 void zebra_interface_add_update(struct interface
*ifp
)
366 struct listnode
*node
, *nnode
;
367 struct zserv
*client
;
369 if (IS_ZEBRA_DEBUG_EVENT
)
370 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADD %s[%d]", ifp
->name
,
373 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
374 if (client
->ifinfo
) {
376 zsend_interface_add(client
, ifp
);
377 zsend_interface_link_params(client
, ifp
);
381 void zebra_interface_delete_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_DELETE %s", ifp
->name
);
389 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
391 zsend_interface_delete(client
, ifp
);
395 /* Interface address addition. */
396 void zebra_interface_address_add_update(struct interface
*ifp
,
397 struct connected
*ifc
)
399 struct listnode
*node
, *nnode
;
400 struct zserv
*client
;
403 if (IS_ZEBRA_DEBUG_EVENT
) {
404 char buf
[PREFIX_STRLEN
];
407 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %s on %s",
408 prefix2str(p
, buf
, sizeof(buf
)), ifc
->ifp
->name
);
411 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
413 "WARNING: advertising address to clients that is not yet usable.");
415 zebra_vxlan_add_del_gw_macip(ifp
, ifc
->address
, 1);
417 router_id_add_address(ifc
);
419 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
420 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
)) {
421 client
->connected_rt_add_cnt
++;
422 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
,
427 /* Interface address deletion. */
428 void zebra_interface_address_delete_update(struct interface
*ifp
,
429 struct connected
*ifc
)
431 struct listnode
*node
, *nnode
;
432 struct zserv
*client
;
435 if (IS_ZEBRA_DEBUG_EVENT
) {
436 char buf
[PREFIX_STRLEN
];
439 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %s on %s",
440 prefix2str(p
, buf
, sizeof(buf
)), ifc
->ifp
->name
);
443 zebra_vxlan_add_del_gw_macip(ifp
, ifc
->address
, 0);
445 router_id_del_address(ifc
);
447 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
448 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
)) {
449 client
->connected_rt_del_cnt
++;
450 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_DELETE
,
455 /* Interface VRF change. May need to delete from clients not interested in
456 * the new VRF. Note that this function is invoked *prior* to the VRF change.
458 void zebra_interface_vrf_update_del(struct interface
*ifp
, vrf_id_t new_vrf_id
)
460 struct listnode
*node
, *nnode
;
461 struct zserv
*client
;
463 if (IS_ZEBRA_DEBUG_EVENT
)
465 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/DEL %s VRF Id %u -> %u",
466 ifp
->name
, ifp
->vrf_id
, new_vrf_id
);
468 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
469 /* Need to delete if the client is not interested in the new
471 zsend_interface_update(ZEBRA_INTERFACE_DOWN
, client
, ifp
);
473 zsend_interface_delete(client
, ifp
);
474 zsend_interface_vrf_update(client
, ifp
, new_vrf_id
);
478 /* Interface VRF change. This function is invoked *post* VRF change and sends an
479 * add to clients who are interested in the new VRF but not in the old VRF.
481 void zebra_interface_vrf_update_add(struct interface
*ifp
, vrf_id_t old_vrf_id
)
483 struct listnode
*node
, *nnode
;
484 struct zserv
*client
;
486 if (IS_ZEBRA_DEBUG_EVENT
)
488 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/ADD %s VRF Id %u -> %u",
489 ifp
->name
, old_vrf_id
, ifp
->vrf_id
);
491 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
492 /* Need to add if the client is interested in the new VRF. */
494 zsend_interface_add(client
, ifp
);
495 zsend_interface_addresses(client
, ifp
);
499 int zebra_add_import_table_entry(struct route_node
*rn
, struct route_entry
*re
,
500 const char *rmap_name
)
502 struct route_entry
*newre
;
503 struct route_entry
*same
;
505 route_map_result_t ret
= RMAP_MATCH
;
508 ret
= zebra_import_table_route_map_check(
509 AFI_IP
, re
->type
, &rn
->p
, re
->nexthop
, re
->vrf_id
,
512 if (ret
== RMAP_MATCH
) {
513 if (rn
->p
.family
== AF_INET
) {
515 p
.prefixlen
= rn
->p
.prefixlen
;
516 p
.u
.prefix4
= rn
->p
.u
.prefix4
;
518 RNODE_FOREACH_RE(rn
, same
)
520 if (CHECK_FLAG(same
->status
,
521 ROUTE_ENTRY_REMOVED
))
524 if (same
->type
== re
->type
525 && same
->instance
== re
->instance
526 && same
->table
== re
->table
527 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
532 zebra_del_import_table_entry(rn
, same
);
535 if (re
->nexthop_num
== 1) {
536 rib_add(AFI_IP
, SAFI_UNICAST
, re
->vrf_id
,
537 ZEBRA_ROUTE_TABLE
, re
->table
, 0, &p
,
539 zebrad
.rtm_table_default
, re
->metric
,
541 zebra_import_table_distance
[AFI_IP
]
543 } else if (re
->nexthop_num
> 1) {
544 newre
= XCALLOC(MTYPE_RE
,
545 sizeof(struct route_entry
));
546 newre
->type
= ZEBRA_ROUTE_TABLE
;
548 zebra_import_table_distance
[AFI_IP
]
550 newre
->flags
= re
->flags
;
551 newre
->metric
= re
->metric
;
552 newre
->mtu
= re
->mtu
;
553 newre
->table
= zebrad
.rtm_table_default
;
554 newre
->nexthop_num
= 0;
555 newre
->uptime
= time(NULL
);
556 newre
->instance
= re
->table
;
557 route_entry_copy_nexthops(newre
, re
->nexthop
);
559 rib_add_multipath(AFI_IP
, SAFI_UNICAST
, &p
,
564 zebra_del_import_table_entry(rn
, re
);
566 /* DD: Add IPv6 code */
570 int zebra_del_import_table_entry(struct route_node
*rn
, struct route_entry
*re
)
574 if (rn
->p
.family
== AF_INET
) {
576 p
.prefixlen
= rn
->p
.prefixlen
;
577 p
.u
.prefix4
= rn
->p
.u
.prefix4
;
579 rib_delete(AFI_IP
, SAFI_UNICAST
, re
->vrf_id
, ZEBRA_ROUTE_TABLE
,
580 re
->table
, re
->flags
, &p
, NULL
, NULL
,
581 zebrad
.rtm_table_default
, re
->metric
);
583 /* DD: Add IPv6 code */
588 /* Assuming no one calls this with the main routing table */
589 int zebra_import_table(afi_t afi
, u_int32_t table_id
, u_int32_t distance
,
590 const char *rmap_name
, int add
)
592 struct route_table
*table
;
593 struct route_entry
*re
;
594 struct route_node
*rn
;
596 if (!is_zebra_valid_kernel_table(table_id
)
597 || ((table_id
== RT_TABLE_MAIN
)
598 || (table_id
== zebrad
.rtm_table_default
)))
604 table
= zebra_vrf_other_route_table(afi
, table_id
, VRF_DEFAULT
);
607 } else if (IS_ZEBRA_DEBUG_RIB
) {
608 zlog_debug("%s routes from table %d",
609 add
? "Importing" : "Unimporting", table_id
);
614 zebra_add_import_table_route_map(afi
, rmap_name
,
618 zebra_get_import_table_route_map(afi
, table_id
);
620 zebra_del_import_table_route_map(afi
, table_id
);
623 zebra_import_table_used
[afi
][table_id
] = 1;
624 zebra_import_table_distance
[afi
][table_id
] = distance
;
626 zebra_import_table_used
[afi
][table_id
] = 0;
627 zebra_import_table_distance
[afi
][table_id
] =
628 ZEBRA_TABLE_DISTANCE_DEFAULT
;
630 rmap_name
= zebra_get_import_table_route_map(afi
, table_id
);
632 zebra_del_import_table_route_map(afi
, table_id
);
635 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
636 /* For each entry in the non-default routing table,
637 * add the entry in the main table
642 RNODE_FOREACH_RE(rn
, re
)
644 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
652 if (((afi
== AFI_IP
) && (rn
->p
.family
== AF_INET
))
653 || ((afi
== AFI_IP6
) && (rn
->p
.family
== AF_INET6
))) {
655 zebra_add_import_table_entry(rn
, re
, rmap_name
);
657 zebra_del_import_table_entry(rn
, re
);
663 int zebra_import_table_config(struct vty
*vty
)
668 char afi_str
[AFI_MAX
][10] = {"", "ip", "ipv6", "ethernet"};
669 const char *rmap_name
;
671 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
672 for (i
= 1; i
< ZEBRA_KERNEL_TABLE_MAX
; i
++) {
673 if (is_zebra_import_table_enabled(afi
, i
)) {
674 if (zebra_import_table_distance
[afi
][i
]
675 != ZEBRA_TABLE_DISTANCE_DEFAULT
) {
677 "%s import-table %d distance %d",
679 zebra_import_table_distance
[afi
]
682 vty_out(vty
, "%s import-table %d",
686 rmap_name
= zebra_get_import_table_route_map(
689 vty_out(vty
, " route-map %s",
701 void zebra_import_table_rm_update()
705 struct route_table
*table
;
706 struct route_entry
*re
;
707 struct route_node
*rn
;
708 const char *rmap_name
;
710 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
711 for (i
= 1; i
< ZEBRA_KERNEL_TABLE_MAX
; i
++) {
712 if (is_zebra_import_table_enabled(afi
, i
)) {
713 rmap_name
= zebra_get_import_table_route_map(
718 table
= zebra_vrf_other_route_table(
719 afi
, i
, VRF_DEFAULT
);
720 for (rn
= route_top(table
); rn
;
721 rn
= route_next(rn
)) {
722 /* For each entry in the non-default
724 * add the entry in the main table
729 RNODE_FOREACH_RE(rn
, re
)
733 ROUTE_ENTRY_REMOVED
))
742 && (rn
->p
.family
== AF_INET
))
744 && (rn
->p
.family
== AF_INET6
)))
745 zebra_add_import_table_entry(
755 /* Interface parameters update */
756 void zebra_interface_parameters_update(struct interface
*ifp
)
758 struct listnode
*node
, *nnode
;
759 struct zserv
*client
;
761 if (IS_ZEBRA_DEBUG_EVENT
)
762 zlog_debug("MESSAGE: ZEBRA_INTERFACE_LINK_PARAMS %s",
765 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
767 zsend_interface_link_params(client
, ifp
);