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(ZEBRA_REDISTRIBUTE_ROUTE_ADD
,
91 client
, &rn
->p
, NULL
, newre
);
93 route_unlock_node(rn
);
97 /* Redistribute routes. */
98 static void zebra_redistribute(struct zserv
*client
, int type
, u_short instance
,
99 vrf_id_t vrf_id
, int afi
)
101 struct route_entry
*newre
;
102 struct route_table
*table
;
103 struct route_node
*rn
;
105 table
= zebra_vrf_table(afi
, SAFI_UNICAST
, vrf_id
);
109 for (rn
= route_top(table
); rn
; rn
= srcdest_route_next(rn
))
110 RNODE_FOREACH_RE(rn
, newre
)
112 struct prefix
*dst_p
, *src_p
;
113 srcdest_rnode_prefixes(rn
, &dst_p
, &src_p
);
115 if (IS_ZEBRA_DEBUG_EVENT
)
117 "%s: checking: selected=%d, type=%d, distance=%d, "
118 "zebra_check_addr=%d",
120 CHECK_FLAG(newre
->flags
,
121 ZEBRA_FLAG_SELECTED
),
122 newre
->type
, newre
->distance
,
123 zebra_check_addr(dst_p
));
125 if (!CHECK_FLAG(newre
->flags
, ZEBRA_FLAG_SELECTED
))
127 if ((type
!= ZEBRA_ROUTE_ALL
128 && (newre
->type
!= type
129 || newre
->instance
!= instance
)))
131 if (newre
->distance
== DISTANCE_INFINITY
)
133 if (!zebra_check_addr(dst_p
))
136 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_ADD
,
137 client
, dst_p
, src_p
, newre
);
141 /* Either advertise a route for redistribution to registered clients or */
142 /* withdraw redistribution if add cannot be done for client */
143 void redistribute_update(struct prefix
*p
, struct prefix
*src_p
,
144 struct route_entry
*re
, struct route_entry
*prev_re
)
146 struct listnode
*node
, *nnode
;
147 struct zserv
*client
;
148 int send_redistribute
;
150 char buf
[INET6_ADDRSTRLEN
];
152 if (IS_ZEBRA_DEBUG_RIB
) {
153 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
155 "%u:%s/%d: Redist update re %p (type %d), old %p (type %d)",
156 re
->vrf_id
, buf
, p
->prefixlen
, re
, re
->type
, prev_re
,
157 prev_re
? prev_re
->type
: -1);
160 afi
= family2afi(p
->family
);
162 zlog_warn("%s: Unknown AFI/SAFI prefix received\n",
167 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
168 send_redistribute
= 0;
170 if (is_default_prefix(p
)
171 && vrf_bitmap_check(client
->redist_default
, re
->vrf_id
))
172 send_redistribute
= 1;
173 else if (vrf_bitmap_check(client
->redist
[afi
][ZEBRA_ROUTE_ALL
],
175 send_redistribute
= 1;
176 else if (re
->instance
177 && redist_check_instance(
178 &client
->mi_redist
[afi
][re
->type
],
180 send_redistribute
= 1;
181 else if (vrf_bitmap_check(client
->redist
[afi
][re
->type
],
183 send_redistribute
= 1;
185 if (send_redistribute
) {
186 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_ADD
,
187 client
, p
, src_p
, re
);
190 && redist_check_instance(
191 &client
->mi_redist
[afi
]
195 client
->redist
[afi
][prev_re
->type
],
197 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_DEL
,
198 client
, p
, src_p
, prev_re
);
203 void redistribute_delete(struct prefix
*p
, struct prefix
*src_p
,
204 struct route_entry
*re
)
206 struct listnode
*node
, *nnode
;
207 struct zserv
*client
;
208 char buf
[INET6_ADDRSTRLEN
];
211 if (IS_ZEBRA_DEBUG_RIB
) {
212 inet_ntop(p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
213 zlog_debug("%u:%s/%d: Redist delete re %p (type %d)",
214 re
->vrf_id
, buf
, p
->prefixlen
, re
, re
->type
);
217 /* Add DISTANCE_INFINITY check. */
218 if (re
->distance
== DISTANCE_INFINITY
)
221 afi
= family2afi(p
->family
);
223 zlog_warn("%s: Unknown AFI/SAFI prefix received\n",
228 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
229 if ((is_default_prefix(p
)
230 && vrf_bitmap_check(client
->redist_default
, re
->vrf_id
))
231 || vrf_bitmap_check(client
->redist
[afi
][ZEBRA_ROUTE_ALL
],
234 && redist_check_instance(
235 &client
->mi_redist
[afi
][re
->type
],
237 || vrf_bitmap_check(client
->redist
[afi
][re
->type
],
239 zsend_redistribute_route(ZEBRA_REDISTRIBUTE_ROUTE_DEL
,
240 client
, p
, src_p
, re
);
245 void zebra_redistribute_add(int command
, struct zserv
*client
, int length
,
246 struct zebra_vrf
*zvrf
)
252 afi
= stream_getc(client
->ibuf
);
253 type
= stream_getc(client
->ibuf
);
254 instance
= stream_getw(client
->ibuf
);
256 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
)
260 if (!redist_check_instance(&client
->mi_redist
[afi
][type
],
262 redist_add_instance(&client
->mi_redist
[afi
][type
],
264 zebra_redistribute(client
, type
, instance
,
268 if (!vrf_bitmap_check(client
->redist
[afi
][type
],
270 vrf_bitmap_set(client
->redist
[afi
][type
],
272 zebra_redistribute(client
, type
, 0, zvrf_id(zvrf
), afi
);
277 void zebra_redistribute_delete(int command
, struct zserv
*client
, int length
,
278 struct zebra_vrf
*zvrf
)
284 afi
= stream_getc(client
->ibuf
);
285 type
= stream_getc(client
->ibuf
);
286 instance
= stream_getw(client
->ibuf
);
288 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
)
292 * NOTE: no need to withdraw the previously advertised routes. The
294 * themselves should keep track of the received routes from zebra and
295 * withdraw them when necessary.
298 redist_del_instance(&client
->mi_redist
[afi
][type
], instance
);
300 vrf_bitmap_unset(client
->redist
[afi
][type
], zvrf_id(zvrf
));
303 void zebra_redistribute_default_add(int command
, struct zserv
*client
,
304 int length
, struct zebra_vrf
*zvrf
)
306 vrf_bitmap_set(client
->redist_default
, zvrf_id(zvrf
));
307 zebra_redistribute_default(client
, zvrf_id(zvrf
));
310 void zebra_redistribute_default_delete(int command
, struct zserv
*client
,
311 int length
, struct zebra_vrf
*zvrf
)
313 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
316 /* Interface up information. */
317 void zebra_interface_up_update(struct interface
*ifp
)
319 struct listnode
*node
, *nnode
;
320 struct zserv
*client
;
322 if (IS_ZEBRA_DEBUG_EVENT
)
323 zlog_debug("MESSAGE: ZEBRA_INTERFACE_UP %s", ifp
->name
);
325 if (ifp
->ptm_status
|| !ifp
->ptm_enable
) {
326 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
327 if (client
->ifinfo
) {
328 zsend_interface_update(ZEBRA_INTERFACE_UP
,
330 zsend_interface_link_params(client
, ifp
);
335 /* Interface down information. */
336 void zebra_interface_down_update(struct interface
*ifp
)
338 struct listnode
*node
, *nnode
;
339 struct zserv
*client
;
341 if (IS_ZEBRA_DEBUG_EVENT
)
342 zlog_debug("MESSAGE: ZEBRA_INTERFACE_DOWN %s", ifp
->name
);
344 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
345 zsend_interface_update(ZEBRA_INTERFACE_DOWN
, client
, ifp
);
349 /* Interface information update. */
350 void zebra_interface_add_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_ADD %s[%d]", ifp
->name
,
359 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
360 if (client
->ifinfo
) {
362 zsend_interface_add(client
, ifp
);
363 zsend_interface_link_params(client
, ifp
);
367 void zebra_interface_delete_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_DELETE %s", ifp
->name
);
375 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
377 zsend_interface_delete(client
, ifp
);
381 /* Interface address addition. */
382 void zebra_interface_address_add_update(struct interface
*ifp
,
383 struct connected
*ifc
)
385 struct listnode
*node
, *nnode
;
386 struct zserv
*client
;
389 if (IS_ZEBRA_DEBUG_EVENT
) {
390 char buf
[PREFIX_STRLEN
];
393 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %s on %s",
394 prefix2str(p
, buf
, sizeof(buf
)), ifc
->ifp
->name
);
397 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
399 "WARNING: advertising address to clients that is not yet usable.");
401 zebra_vxlan_add_del_gw_macip(ifp
, ifc
->address
, 1);
403 router_id_add_address(ifc
);
405 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
406 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
)) {
407 client
->connected_rt_add_cnt
++;
408 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
,
413 /* Interface address deletion. */
414 void zebra_interface_address_delete_update(struct interface
*ifp
,
415 struct connected
*ifc
)
417 struct listnode
*node
, *nnode
;
418 struct zserv
*client
;
421 if (IS_ZEBRA_DEBUG_EVENT
) {
422 char buf
[PREFIX_STRLEN
];
425 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %s on %s",
426 prefix2str(p
, buf
, sizeof(buf
)), ifc
->ifp
->name
);
429 zebra_vxlan_add_del_gw_macip(ifp
, ifc
->address
, 0);
431 router_id_del_address(ifc
);
433 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
434 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
)) {
435 client
->connected_rt_del_cnt
++;
436 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_DELETE
,
441 /* Interface VRF change. May need to delete from clients not interested in
442 * the new VRF. Note that this function is invoked *prior* to the VRF change.
444 void zebra_interface_vrf_update_del(struct interface
*ifp
, vrf_id_t new_vrf_id
)
446 struct listnode
*node
, *nnode
;
447 struct zserv
*client
;
449 if (IS_ZEBRA_DEBUG_EVENT
)
451 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/DEL %s VRF Id %u -> %u",
452 ifp
->name
, ifp
->vrf_id
, new_vrf_id
);
454 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
455 /* Need to delete if the client is not interested in the new
457 zsend_interface_update(ZEBRA_INTERFACE_DOWN
, client
, ifp
);
459 zsend_interface_delete(client
, ifp
);
460 zsend_interface_vrf_update(client
, ifp
, new_vrf_id
);
464 /* Interface VRF change. This function is invoked *post* VRF change and sends an
465 * add to clients who are interested in the new VRF but not in the old VRF.
467 void zebra_interface_vrf_update_add(struct interface
*ifp
, vrf_id_t old_vrf_id
)
469 struct listnode
*node
, *nnode
;
470 struct zserv
*client
;
472 if (IS_ZEBRA_DEBUG_EVENT
)
474 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/ADD %s VRF Id %u -> %u",
475 ifp
->name
, old_vrf_id
, ifp
->vrf_id
);
477 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
478 /* Need to add if the client is interested in the new VRF. */
480 zsend_interface_add(client
, ifp
);
481 zsend_interface_addresses(client
, ifp
);
485 int zebra_add_import_table_entry(struct route_node
*rn
, struct route_entry
*re
,
486 const char *rmap_name
)
488 struct route_entry
*newre
;
489 struct route_entry
*same
;
491 route_map_result_t ret
= RMAP_MATCH
;
494 afi
= family2afi(rn
->p
.family
);
496 ret
= zebra_import_table_route_map_check(
497 afi
, re
->type
, &rn
->p
, re
->nexthop
, re
->vrf_id
,
500 if (ret
!= RMAP_MATCH
) {
501 zebra_del_import_table_entry(rn
, re
);
505 prefix_copy(&p
, &rn
->p
);
507 RNODE_FOREACH_RE(rn
, same
)
509 if (CHECK_FLAG(same
->status
,
510 ROUTE_ENTRY_REMOVED
))
513 if (same
->type
== re
->type
514 && same
->instance
== re
->instance
515 && same
->table
== re
->table
516 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
521 zebra_del_import_table_entry(rn
, same
);
523 if (re
->nexthop_num
== 1) {
524 rib_add(afi
, SAFI_UNICAST
, re
->vrf_id
,
525 ZEBRA_ROUTE_TABLE
, re
->table
, 0, &p
,
527 zebrad
.rtm_table_default
, re
->metric
,
529 zebra_import_table_distance
[afi
]
531 } else if (re
->nexthop_num
> 1) {
532 newre
= XCALLOC(MTYPE_RE
,
533 sizeof(struct route_entry
));
534 newre
->type
= ZEBRA_ROUTE_TABLE
;
536 zebra_import_table_distance
[afi
][re
->table
];
537 newre
->flags
= re
->flags
;
538 newre
->metric
= re
->metric
;
539 newre
->mtu
= re
->mtu
;
540 newre
->table
= zebrad
.rtm_table_default
;
541 newre
->nexthop_num
= 0;
542 newre
->uptime
= time(NULL
);
543 newre
->instance
= re
->table
;
544 route_entry_copy_nexthops(newre
, re
->nexthop
);
546 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
552 int zebra_del_import_table_entry(struct route_node
*rn
, struct route_entry
*re
)
557 afi
= family2afi(rn
->p
.family
);
558 prefix_copy(&p
, &rn
->p
);
560 rib_delete(afi
, SAFI_UNICAST
, re
->vrf_id
, ZEBRA_ROUTE_TABLE
,
561 re
->table
, re
->flags
, &p
, NULL
, NULL
,
562 zebrad
.rtm_table_default
, re
->metric
);
567 /* Assuming no one calls this with the main routing table */
568 int zebra_import_table(afi_t afi
, u_int32_t table_id
, u_int32_t distance
,
569 const char *rmap_name
, int add
)
571 struct route_table
*table
;
572 struct route_entry
*re
;
573 struct route_node
*rn
;
575 if (!is_zebra_valid_kernel_table(table_id
)
576 || ((table_id
== RT_TABLE_MAIN
)
577 || (table_id
== zebrad
.rtm_table_default
)))
583 table
= zebra_vrf_other_route_table(afi
, table_id
, VRF_DEFAULT
);
586 } else if (IS_ZEBRA_DEBUG_RIB
) {
587 zlog_debug("%s routes from table %d",
588 add
? "Importing" : "Unimporting", table_id
);
593 zebra_add_import_table_route_map(afi
, rmap_name
,
597 zebra_get_import_table_route_map(afi
, table_id
);
599 zebra_del_import_table_route_map(afi
, table_id
);
602 zebra_import_table_used
[afi
][table_id
] = 1;
603 zebra_import_table_distance
[afi
][table_id
] = distance
;
605 zebra_import_table_used
[afi
][table_id
] = 0;
606 zebra_import_table_distance
[afi
][table_id
] =
607 ZEBRA_TABLE_DISTANCE_DEFAULT
;
609 rmap_name
= zebra_get_import_table_route_map(afi
, table_id
);
611 zebra_del_import_table_route_map(afi
, table_id
);
614 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
615 /* For each entry in the non-default routing table,
616 * add the entry in the main table
621 RNODE_FOREACH_RE(rn
, re
)
623 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
631 if (((afi
== AFI_IP
) && (rn
->p
.family
== AF_INET
))
632 || ((afi
== AFI_IP6
) && (rn
->p
.family
== AF_INET6
))) {
634 zebra_add_import_table_entry(rn
, re
, rmap_name
);
636 zebra_del_import_table_entry(rn
, re
);
642 int zebra_import_table_config(struct vty
*vty
)
647 char afi_str
[AFI_MAX
][10] = {"", "ip", "ipv6", "ethernet"};
648 const char *rmap_name
;
650 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
651 for (i
= 1; i
< ZEBRA_KERNEL_TABLE_MAX
; i
++) {
652 if (!is_zebra_import_table_enabled(afi
, i
))
655 if (zebra_import_table_distance
[afi
][i
]
656 != ZEBRA_TABLE_DISTANCE_DEFAULT
) {
658 "%s import-table %d distance %d",
660 zebra_import_table_distance
[afi
][i
]);
662 vty_out(vty
, "%s import-table %d",
666 rmap_name
= zebra_get_import_table_route_map(afi
, i
);
668 vty_out(vty
, " route-map %s",
679 void zebra_import_table_rm_update()
683 struct route_table
*table
;
684 struct route_entry
*re
;
685 struct route_node
*rn
;
686 const char *rmap_name
;
688 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
689 for (i
= 1; i
< ZEBRA_KERNEL_TABLE_MAX
; i
++) {
690 if (!is_zebra_import_table_enabled(afi
, i
))
693 rmap_name
= zebra_get_import_table_route_map(afi
, i
);
697 table
= zebra_vrf_other_route_table(afi
,
700 for (rn
= route_top(table
); rn
;
701 rn
= route_next(rn
)) {
702 /* For each entry in the non-default
704 * add the entry in the main table
709 RNODE_FOREACH_RE(rn
, re
)
713 ROUTE_ENTRY_REMOVED
))
722 && (rn
->p
.family
== AF_INET
))
724 && (rn
->p
.family
== AF_INET6
)))
725 zebra_add_import_table_entry(
734 /* Interface parameters update */
735 void zebra_interface_parameters_update(struct interface
*ifp
)
737 struct listnode
*node
, *nnode
;
738 struct zserv
*client
;
740 if (IS_ZEBRA_DEBUG_EVENT
)
741 zlog_debug("MESSAGE: ZEBRA_INTERFACE_LINK_PARAMS %s",
744 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
746 zsend_interface_link_params(client
, ifp
);