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
,
92 client
, &rn
->p
, NULL
, newre
);
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
)
114 struct prefix
*dst_p
, *src_p
;
115 srcdest_rnode_prefixes(rn
, &dst_p
, &src_p
);
117 if (IS_ZEBRA_DEBUG_EVENT
)
119 "%s: checking: selected=%d, type=%d, distance=%d, "
120 "zebra_check_addr=%d",
122 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 afi
= stream_getc(client
->ibuf
);
255 type
= stream_getc(client
->ibuf
);
256 instance
= stream_getw(client
->ibuf
);
258 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
)
262 if (!redist_check_instance(&client
->mi_redist
[afi
][type
],
264 redist_add_instance(&client
->mi_redist
[afi
][type
],
266 zebra_redistribute(client
, type
, instance
,
270 if (!vrf_bitmap_check(client
->redist
[afi
][type
],
272 vrf_bitmap_set(client
->redist
[afi
][type
],
274 zebra_redistribute(client
, type
, 0, zvrf_id(zvrf
), afi
);
279 void zebra_redistribute_delete(int command
, struct zserv
*client
, int length
,
280 struct zebra_vrf
*zvrf
)
286 afi
= stream_getc(client
->ibuf
);
287 type
= stream_getc(client
->ibuf
);
288 instance
= stream_getw(client
->ibuf
);
290 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
)
294 * NOTE: no need to withdraw the previously advertised routes. The
296 * themselves should keep track of the received routes from zebra and
297 * withdraw them when necessary.
300 redist_del_instance(&client
->mi_redist
[afi
][type
], instance
);
302 vrf_bitmap_unset(client
->redist
[afi
][type
], zvrf_id(zvrf
));
305 void zebra_redistribute_default_add(int command
, struct zserv
*client
,
306 int length
, struct zebra_vrf
*zvrf
)
308 vrf_bitmap_set(client
->redist_default
, zvrf_id(zvrf
));
309 zebra_redistribute_default(client
, zvrf_id(zvrf
));
312 void zebra_redistribute_default_delete(int command
, struct zserv
*client
,
313 int length
, struct zebra_vrf
*zvrf
)
315 vrf_bitmap_unset(client
->redist_default
, zvrf_id(zvrf
));
318 /* Interface up information. */
319 void zebra_interface_up_update(struct interface
*ifp
)
321 struct listnode
*node
, *nnode
;
322 struct zserv
*client
;
324 if (IS_ZEBRA_DEBUG_EVENT
)
325 zlog_debug("MESSAGE: ZEBRA_INTERFACE_UP %s", ifp
->name
);
327 if (ifp
->ptm_status
|| !ifp
->ptm_enable
) {
328 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
329 if (client
->ifinfo
) {
330 zsend_interface_update(ZEBRA_INTERFACE_UP
,
332 zsend_interface_link_params(client
, ifp
);
337 /* Interface down information. */
338 void zebra_interface_down_update(struct interface
*ifp
)
340 struct listnode
*node
, *nnode
;
341 struct zserv
*client
;
343 if (IS_ZEBRA_DEBUG_EVENT
)
344 zlog_debug("MESSAGE: ZEBRA_INTERFACE_DOWN %s", ifp
->name
);
346 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
347 zsend_interface_update(ZEBRA_INTERFACE_DOWN
, client
, ifp
);
351 /* Interface information update. */
352 void zebra_interface_add_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_ADD %s[%d]", ifp
->name
,
361 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
362 if (client
->ifinfo
) {
364 zsend_interface_add(client
, ifp
);
365 zsend_interface_link_params(client
, ifp
);
369 void zebra_interface_delete_update(struct interface
*ifp
)
371 struct listnode
*node
, *nnode
;
372 struct zserv
*client
;
374 if (IS_ZEBRA_DEBUG_EVENT
)
375 zlog_debug("MESSAGE: ZEBRA_INTERFACE_DELETE %s", ifp
->name
);
377 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
379 zsend_interface_delete(client
, ifp
);
383 /* Interface address addition. */
384 void zebra_interface_address_add_update(struct interface
*ifp
,
385 struct connected
*ifc
)
387 struct listnode
*node
, *nnode
;
388 struct zserv
*client
;
391 if (IS_ZEBRA_DEBUG_EVENT
) {
392 char buf
[PREFIX_STRLEN
];
395 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %s on %s",
396 prefix2str(p
, buf
, sizeof(buf
)), ifc
->ifp
->name
);
399 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
401 "WARNING: advertising address to clients that is not yet usable.");
403 zebra_vxlan_add_del_gw_macip(ifp
, ifc
->address
, 1);
405 router_id_add_address(ifc
);
407 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
408 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
)) {
409 client
->connected_rt_add_cnt
++;
410 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_ADD
,
415 /* Interface address deletion. */
416 void zebra_interface_address_delete_update(struct interface
*ifp
,
417 struct connected
*ifc
)
419 struct listnode
*node
, *nnode
;
420 struct zserv
*client
;
423 if (IS_ZEBRA_DEBUG_EVENT
) {
424 char buf
[PREFIX_STRLEN
];
427 zlog_debug("MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %s on %s",
428 prefix2str(p
, buf
, sizeof(buf
)), ifc
->ifp
->name
);
431 zebra_vxlan_add_del_gw_macip(ifp
, ifc
->address
, 0);
433 router_id_del_address(ifc
);
435 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
436 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
)) {
437 client
->connected_rt_del_cnt
++;
438 zsend_interface_address(ZEBRA_INTERFACE_ADDRESS_DELETE
,
443 /* Interface VRF change. May need to delete from clients not interested in
444 * the new VRF. Note that this function is invoked *prior* to the VRF change.
446 void zebra_interface_vrf_update_del(struct interface
*ifp
, vrf_id_t new_vrf_id
)
448 struct listnode
*node
, *nnode
;
449 struct zserv
*client
;
451 if (IS_ZEBRA_DEBUG_EVENT
)
453 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/DEL %s VRF Id %u -> %u",
454 ifp
->name
, ifp
->vrf_id
, new_vrf_id
);
456 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
457 /* Need to delete if the client is not interested in the new
459 zsend_interface_update(ZEBRA_INTERFACE_DOWN
, client
, ifp
);
461 zsend_interface_delete(client
, ifp
);
462 zsend_interface_vrf_update(client
, ifp
, new_vrf_id
);
466 /* Interface VRF change. This function is invoked *post* VRF change and sends an
467 * add to clients who are interested in the new VRF but not in the old VRF.
469 void zebra_interface_vrf_update_add(struct interface
*ifp
, vrf_id_t old_vrf_id
)
471 struct listnode
*node
, *nnode
;
472 struct zserv
*client
;
474 if (IS_ZEBRA_DEBUG_EVENT
)
476 "MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/ADD %s VRF Id %u -> %u",
477 ifp
->name
, old_vrf_id
, ifp
->vrf_id
);
479 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
)) {
480 /* Need to add if the client is interested in the new VRF. */
482 zsend_interface_add(client
, ifp
);
483 zsend_interface_addresses(client
, ifp
);
487 int zebra_add_import_table_entry(struct route_node
*rn
, struct route_entry
*re
,
488 const char *rmap_name
)
490 struct route_entry
*newre
;
491 struct route_entry
*same
;
493 route_map_result_t ret
= RMAP_MATCH
;
496 afi
= family2afi(rn
->p
.family
);
498 ret
= zebra_import_table_route_map_check(
499 afi
, re
->type
, &rn
->p
, re
->nexthop
, re
->vrf_id
,
502 if (ret
!= RMAP_MATCH
) {
503 zebra_del_import_table_entry(rn
, re
);
507 prefix_copy(&p
, &rn
->p
);
509 RNODE_FOREACH_RE(rn
, same
)
511 if (CHECK_FLAG(same
->status
,
512 ROUTE_ENTRY_REMOVED
))
515 if (same
->type
== re
->type
516 && same
->instance
== re
->instance
517 && same
->table
== re
->table
518 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
523 zebra_del_import_table_entry(rn
, same
);
525 if (re
->nexthop_num
== 1) {
526 rib_add(afi
, SAFI_UNICAST
, re
->vrf_id
,
527 ZEBRA_ROUTE_TABLE
, re
->table
, 0, &p
,
529 zebrad
.rtm_table_default
, re
->metric
,
531 zebra_import_table_distance
[afi
]
533 } else if (re
->nexthop_num
> 1) {
534 newre
= XCALLOC(MTYPE_RE
,
535 sizeof(struct route_entry
));
536 newre
->type
= ZEBRA_ROUTE_TABLE
;
538 zebra_import_table_distance
[afi
][re
->table
];
539 newre
->flags
= re
->flags
;
540 newre
->metric
= re
->metric
;
541 newre
->mtu
= re
->mtu
;
542 newre
->table
= zebrad
.rtm_table_default
;
543 newre
->nexthop_num
= 0;
544 newre
->uptime
= time(NULL
);
545 newre
->instance
= re
->table
;
546 route_entry_copy_nexthops(newre
, re
->nexthop
);
548 rib_add_multipath(afi
, SAFI_UNICAST
, &p
,
554 int zebra_del_import_table_entry(struct route_node
*rn
, struct route_entry
*re
)
559 afi
= family2afi(rn
->p
.family
);
560 prefix_copy(&p
, &rn
->p
);
562 rib_delete(afi
, SAFI_UNICAST
, re
->vrf_id
, ZEBRA_ROUTE_TABLE
,
563 re
->table
, re
->flags
, &p
, NULL
, NULL
,
564 zebrad
.rtm_table_default
, re
->metric
);
569 /* Assuming no one calls this with the main routing table */
570 int zebra_import_table(afi_t afi
, u_int32_t table_id
, u_int32_t distance
,
571 const char *rmap_name
, int add
)
573 struct route_table
*table
;
574 struct route_entry
*re
;
575 struct route_node
*rn
;
577 if (!is_zebra_valid_kernel_table(table_id
)
578 || ((table_id
== RT_TABLE_MAIN
)
579 || (table_id
== zebrad
.rtm_table_default
)))
585 table
= zebra_vrf_other_route_table(afi
, table_id
, VRF_DEFAULT
);
588 } else if (IS_ZEBRA_DEBUG_RIB
) {
589 zlog_debug("%s routes from table %d",
590 add
? "Importing" : "Unimporting", table_id
);
595 zebra_add_import_table_route_map(afi
, rmap_name
,
599 zebra_get_import_table_route_map(afi
, table_id
);
601 zebra_del_import_table_route_map(afi
, table_id
);
604 zebra_import_table_used
[afi
][table_id
] = 1;
605 zebra_import_table_distance
[afi
][table_id
] = distance
;
607 zebra_import_table_used
[afi
][table_id
] = 0;
608 zebra_import_table_distance
[afi
][table_id
] =
609 ZEBRA_TABLE_DISTANCE_DEFAULT
;
611 rmap_name
= zebra_get_import_table_route_map(afi
, table_id
);
613 zebra_del_import_table_route_map(afi
, table_id
);
616 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
617 /* For each entry in the non-default routing table,
618 * add the entry in the main table
623 RNODE_FOREACH_RE(rn
, re
)
625 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
633 if (((afi
== AFI_IP
) && (rn
->p
.family
== AF_INET
))
634 || ((afi
== AFI_IP6
) && (rn
->p
.family
== AF_INET6
))) {
636 zebra_add_import_table_entry(rn
, re
, rmap_name
);
638 zebra_del_import_table_entry(rn
, re
);
644 int zebra_import_table_config(struct vty
*vty
)
649 char afi_str
[AFI_MAX
][10] = {"", "ip", "ipv6", "ethernet"};
650 const char *rmap_name
;
652 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
653 for (i
= 1; i
< ZEBRA_KERNEL_TABLE_MAX
; i
++) {
654 if (!is_zebra_import_table_enabled(afi
, i
))
657 if (zebra_import_table_distance
[afi
][i
]
658 != ZEBRA_TABLE_DISTANCE_DEFAULT
) {
660 "%s import-table %d distance %d",
662 zebra_import_table_distance
[afi
][i
]);
664 vty_out(vty
, "%s import-table %d",
668 rmap_name
= zebra_get_import_table_route_map(afi
, i
);
670 vty_out(vty
, " route-map %s",
681 void zebra_import_table_rm_update()
685 struct route_table
*table
;
686 struct route_entry
*re
;
687 struct route_node
*rn
;
688 const char *rmap_name
;
690 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
691 for (i
= 1; i
< ZEBRA_KERNEL_TABLE_MAX
; i
++) {
692 if (!is_zebra_import_table_enabled(afi
, i
))
695 rmap_name
= zebra_get_import_table_route_map(afi
, i
);
699 table
= zebra_vrf_other_route_table(afi
,
702 for (rn
= route_top(table
); rn
;
703 rn
= route_next(rn
)) {
704 /* For each entry in the non-default
706 * add the entry in the main table
711 RNODE_FOREACH_RE(rn
, re
)
715 ROUTE_ENTRY_REMOVED
))
724 && (rn
->p
.family
== AF_INET
))
726 && (rn
->p
.family
== AF_INET6
)))
727 zebra_add_import_table_entry(
736 /* Interface parameters update */
737 void zebra_interface_parameters_update(struct interface
*ifp
)
739 struct listnode
*node
, *nnode
;
740 struct zserv
*client
;
742 if (IS_ZEBRA_DEBUG_EVENT
)
743 zlog_debug("MESSAGE: ZEBRA_INTERFACE_LINK_PARAMS %s",
746 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
748 zsend_interface_link_params(client
, ifp
);