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 ret
= zebra_import_table_route_map_check(
495 AFI_IP
, re
->type
, &rn
->p
, re
->nexthop
, re
->vrf_id
,
498 if (ret
!= RMAP_MATCH
) {
499 zebra_del_import_table_entry(rn
, re
);
503 if (rn
->p
.family
== AF_INET
) {
505 p
.prefixlen
= rn
->p
.prefixlen
;
506 p
.u
.prefix4
= rn
->p
.u
.prefix4
;
508 RNODE_FOREACH_RE(rn
, same
)
510 if (CHECK_FLAG(same
->status
,
511 ROUTE_ENTRY_REMOVED
))
514 if (same
->type
== re
->type
515 && same
->instance
== re
->instance
516 && same
->table
== re
->table
517 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
522 zebra_del_import_table_entry(rn
, same
);
524 if (re
->nexthop_num
== 1) {
525 rib_add(AFI_IP
, SAFI_UNICAST
, re
->vrf_id
,
526 ZEBRA_ROUTE_TABLE
, re
->table
, 0, &p
,
528 zebrad
.rtm_table_default
, re
->metric
,
530 zebra_import_table_distance
[AFI_IP
]
532 } else if (re
->nexthop_num
> 1) {
533 newre
= XCALLOC(MTYPE_RE
,
534 sizeof(struct route_entry
));
535 newre
->type
= ZEBRA_ROUTE_TABLE
;
537 zebra_import_table_distance
[AFI_IP
][re
->table
];
538 newre
->flags
= re
->flags
;
539 newre
->metric
= re
->metric
;
540 newre
->mtu
= re
->mtu
;
541 newre
->table
= zebrad
.rtm_table_default
;
542 newre
->nexthop_num
= 0;
543 newre
->uptime
= time(NULL
);
544 newre
->instance
= re
->table
;
545 route_entry_copy_nexthops(newre
, re
->nexthop
);
547 rib_add_multipath(AFI_IP
, SAFI_UNICAST
, &p
,
551 /* DD: Add IPv6 code */
555 int zebra_del_import_table_entry(struct route_node
*rn
, struct route_entry
*re
)
559 if (rn
->p
.family
== AF_INET
) {
561 p
.prefixlen
= rn
->p
.prefixlen
;
562 p
.u
.prefix4
= rn
->p
.u
.prefix4
;
564 rib_delete(AFI_IP
, SAFI_UNICAST
, re
->vrf_id
, ZEBRA_ROUTE_TABLE
,
565 re
->table
, re
->flags
, &p
, NULL
, NULL
,
566 zebrad
.rtm_table_default
, re
->metric
);
568 /* DD: Add IPv6 code */
573 /* Assuming no one calls this with the main routing table */
574 int zebra_import_table(afi_t afi
, u_int32_t table_id
, u_int32_t distance
,
575 const char *rmap_name
, int add
)
577 struct route_table
*table
;
578 struct route_entry
*re
;
579 struct route_node
*rn
;
581 if (!is_zebra_valid_kernel_table(table_id
)
582 || ((table_id
== RT_TABLE_MAIN
)
583 || (table_id
== zebrad
.rtm_table_default
)))
589 table
= zebra_vrf_other_route_table(afi
, table_id
, VRF_DEFAULT
);
592 } else if (IS_ZEBRA_DEBUG_RIB
) {
593 zlog_debug("%s routes from table %d",
594 add
? "Importing" : "Unimporting", table_id
);
599 zebra_add_import_table_route_map(afi
, rmap_name
,
603 zebra_get_import_table_route_map(afi
, table_id
);
605 zebra_del_import_table_route_map(afi
, table_id
);
608 zebra_import_table_used
[afi
][table_id
] = 1;
609 zebra_import_table_distance
[afi
][table_id
] = distance
;
611 zebra_import_table_used
[afi
][table_id
] = 0;
612 zebra_import_table_distance
[afi
][table_id
] =
613 ZEBRA_TABLE_DISTANCE_DEFAULT
;
615 rmap_name
= zebra_get_import_table_route_map(afi
, table_id
);
617 zebra_del_import_table_route_map(afi
, table_id
);
620 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
621 /* For each entry in the non-default routing table,
622 * add the entry in the main table
627 RNODE_FOREACH_RE(rn
, re
)
629 if (CHECK_FLAG(re
->status
, ROUTE_ENTRY_REMOVED
))
637 if (((afi
== AFI_IP
) && (rn
->p
.family
== AF_INET
))
638 || ((afi
== AFI_IP6
) && (rn
->p
.family
== AF_INET6
))) {
640 zebra_add_import_table_entry(rn
, re
, rmap_name
);
642 zebra_del_import_table_entry(rn
, re
);
648 int zebra_import_table_config(struct vty
*vty
)
653 char afi_str
[AFI_MAX
][10] = {"", "ip", "ipv6", "ethernet"};
654 const char *rmap_name
;
656 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
657 for (i
= 1; i
< ZEBRA_KERNEL_TABLE_MAX
; i
++) {
658 if (!is_zebra_import_table_enabled(afi
, i
))
661 if (zebra_import_table_distance
[afi
][i
]
662 != ZEBRA_TABLE_DISTANCE_DEFAULT
) {
664 "%s import-table %d distance %d",
666 zebra_import_table_distance
[afi
][i
]);
668 vty_out(vty
, "%s import-table %d",
672 rmap_name
= zebra_get_import_table_route_map(afi
, i
);
674 vty_out(vty
, " route-map %s",
685 void zebra_import_table_rm_update()
689 struct route_table
*table
;
690 struct route_entry
*re
;
691 struct route_node
*rn
;
692 const char *rmap_name
;
694 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++) {
695 for (i
= 1; i
< ZEBRA_KERNEL_TABLE_MAX
; i
++) {
696 if (!is_zebra_import_table_enabled(afi
, i
))
699 rmap_name
= zebra_get_import_table_route_map(afi
, i
);
703 table
= zebra_vrf_other_route_table(afi
,
706 for (rn
= route_top(table
); rn
;
707 rn
= route_next(rn
)) {
708 /* For each entry in the non-default
710 * add the entry in the main table
715 RNODE_FOREACH_RE(rn
, re
)
719 ROUTE_ENTRY_REMOVED
))
728 && (rn
->p
.family
== AF_INET
))
730 && (rn
->p
.family
== AF_INET6
)))
731 zebra_add_import_table_entry(
740 /* Interface parameters update */
741 void zebra_interface_parameters_update(struct interface
*ifp
)
743 struct listnode
*node
, *nnode
;
744 struct zserv
*client
;
746 if (IS_ZEBRA_DEBUG_EVENT
)
747 zlog_debug("MESSAGE: ZEBRA_INTERFACE_LINK_PARAMS %s",
750 for (ALL_LIST_ELEMENTS(zebrad
.client_list
, node
, nnode
, client
))
752 zsend_interface_link_params(client
, ifp
);