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"
45 #define ZEBRA_PTM_SUPPORT
47 /* array holding redistribute info about table redistribution */
48 /* bit AFI is set if that AFI is redistributing routes from this table */
49 static int zebra_import_table_used
[AFI_MAX
][ZEBRA_KERNEL_TABLE_MAX
];
50 static u_int32_t zebra_import_table_distance
[AFI_MAX
][ZEBRA_KERNEL_TABLE_MAX
];
53 is_zebra_import_table_enabled(afi_t afi
, u_int32_t table_id
)
55 if (is_zebra_valid_kernel_table(table_id
))
56 return zebra_import_table_used
[afi
][table_id
];
61 is_default (struct prefix
*p
)
63 if (p
->family
== AF_INET
)
64 if (p
->u
.prefix4
.s_addr
== 0 && p
->prefixlen
== 0)
66 #if 0 /* IPv6 default separation is now pending until protocol daemon
68 if (p
->family
== AF_INET6
)
69 if (IN6_IS_ADDR_UNSPECIFIED (&p
->u
.prefix6
) && p
->prefixlen
== 0)
76 zebra_redistribute_default (struct zserv
*client
, vrf_id_t vrf_id
)
80 struct route_table
*table
;
81 struct route_node
*rn
;
82 struct route_entry
*newre
;
84 for (afi
= AFI_IP
; afi
<= AFI_IP6
; afi
++)
87 table
= zebra_vrf_table (afi
, SAFI_UNICAST
, vrf_id
);
91 /* Lookup default route. */
92 memset (&p
, 0, sizeof (p
));
93 p
.family
= afi2family (afi
);
94 rn
= route_node_lookup (table
, &p
);
98 RNODE_FOREACH_RE (rn
, newre
)
99 if (CHECK_FLAG (newre
->flags
, ZEBRA_FLAG_SELECTED
)
100 && newre
->distance
!= DISTANCE_INFINITY
)
101 zsend_redistribute_route (1, client
, &rn
->p
, NULL
, newre
);
103 route_unlock_node (rn
);
107 /* Redistribute routes. */
109 zebra_redistribute (struct zserv
*client
, int type
, u_short instance
, vrf_id_t vrf_id
, int afi
)
111 struct route_entry
*newre
;
112 struct route_table
*table
;
113 struct route_node
*rn
;
115 table
= zebra_vrf_table (afi
, SAFI_UNICAST
, vrf_id
);
119 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
120 RNODE_FOREACH_RE (rn
, newre
)
122 struct prefix
*dst_p
, *src_p
;
123 srcdest_rnode_prefixes(rn
, &dst_p
, &src_p
);
125 if (IS_ZEBRA_DEBUG_EVENT
)
126 zlog_debug("%s: checking: selected=%d, type=%d, distance=%d, "
127 "zebra_check_addr=%d", __func__
,
128 CHECK_FLAG (newre
->flags
, ZEBRA_FLAG_SELECTED
),
129 newre
->type
, newre
->distance
,
130 zebra_check_addr (dst_p
));
132 if (! CHECK_FLAG (newre
->flags
, ZEBRA_FLAG_SELECTED
))
134 if ((type
!= ZEBRA_ROUTE_ALL
&&
135 (newre
->type
!= type
|| newre
->instance
!= instance
)))
137 if (newre
->distance
== DISTANCE_INFINITY
)
139 if (! zebra_check_addr (dst_p
))
142 zsend_redistribute_route (1, client
, dst_p
, src_p
, newre
);
146 /* Either advertise a route for redistribution to registered clients or */
147 /* withdraw redistribution if add cannot be done for client */
149 redistribute_update (struct prefix
*p
, struct prefix
*src_p
,
150 struct route_entry
*re
, struct route_entry
*prev_re
)
152 struct listnode
*node
, *nnode
;
153 struct zserv
*client
;
154 int send_redistribute
;
156 char buf
[INET6_ADDRSTRLEN
];
158 if (IS_ZEBRA_DEBUG_RIB
)
160 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
161 zlog_debug ("%u:%s/%d: Redist update re %p (type %d), old %p (type %d)",
162 re
->vrf_id
, buf
, p
->prefixlen
, re
, re
->type
,
163 prev_re
, prev_re
? prev_re
->type
: -1);
166 afi
= family2afi(p
->family
);
169 zlog_warn("%s: Unknown AFI/SAFI prefix received\n", __FUNCTION__
);
173 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
175 send_redistribute
= 0;
177 if (is_default (p
) && vrf_bitmap_check (client
->redist_default
, re
->vrf_id
))
178 send_redistribute
= 1;
179 else if (vrf_bitmap_check (client
->redist
[afi
][ZEBRA_ROUTE_ALL
], re
->vrf_id
))
180 send_redistribute
= 1;
181 else if (re
->instance
&& redist_check_instance (&client
->mi_redist
[afi
][re
->type
],
183 send_redistribute
= 1;
184 else if (vrf_bitmap_check (client
->redist
[afi
][re
->type
], re
->vrf_id
))
185 send_redistribute
= 1;
187 if (send_redistribute
)
189 zsend_redistribute_route (1, client
, p
, src_p
, re
);
193 redist_check_instance(&client
->mi_redist
[afi
][prev_re
->type
],
195 vrf_bitmap_check (client
->redist
[afi
][prev_re
->type
], re
->vrf_id
)))
197 zsend_redistribute_route (0, client
, p
, src_p
, prev_re
);
203 redistribute_delete (struct prefix
*p
, struct prefix
*src_p
, struct route_entry
*re
)
205 struct listnode
*node
, *nnode
;
206 struct zserv
*client
;
207 char buf
[INET6_ADDRSTRLEN
];
210 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
);
224 zlog_warn("%s: Unknown AFI/SAFI prefix received\n", __FUNCTION__
);
228 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
230 if ((is_default (p
) &&
231 vrf_bitmap_check (client
->redist_default
, re
->vrf_id
)) ||
232 vrf_bitmap_check (client
->redist
[afi
][ZEBRA_ROUTE_ALL
], re
->vrf_id
) ||
234 redist_check_instance(&client
->mi_redist
[afi
][re
->type
],
236 vrf_bitmap_check (client
->redist
[afi
][re
->type
], re
->vrf_id
))
238 zsend_redistribute_route (0, client
, p
, src_p
, re
);
244 zebra_redistribute_add (int command
, struct zserv
*client
, int length
,
245 struct zebra_vrf
*zvrf
)
251 afi
= stream_getc (client
->ibuf
);
252 type
= stream_getc (client
->ibuf
);
253 instance
= stream_getw (client
->ibuf
);
255 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
)
260 if (! redist_check_instance (&client
->mi_redist
[afi
][type
], instance
))
262 redist_add_instance (&client
->mi_redist
[afi
][type
], instance
);
263 zebra_redistribute (client
, type
, instance
, zvrf_id (zvrf
), afi
);
266 if (! vrf_bitmap_check (client
->redist
[afi
][type
], zvrf_id (zvrf
)))
268 vrf_bitmap_set (client
->redist
[afi
][type
], zvrf_id (zvrf
));
269 zebra_redistribute (client
, type
, 0, zvrf_id (zvrf
), afi
);
275 zebra_redistribute_delete (int command
, struct zserv
*client
, int length
,
276 struct zebra_vrf
*zvrf
)
282 afi
= stream_getc (client
->ibuf
);
283 type
= stream_getc (client
->ibuf
);
284 instance
= stream_getw (client
->ibuf
);
286 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
)
290 * NOTE: no need to withdraw the previously advertised routes. The clients
291 * themselves should keep track of the received routes from zebra and
292 * withdraw them when necessary.
295 redist_del_instance (&client
->mi_redist
[afi
][type
], instance
);
297 vrf_bitmap_unset (client
->redist
[afi
][type
], zvrf_id (zvrf
));
301 zebra_redistribute_default_add (int command
, struct zserv
*client
, int length
,
302 struct zebra_vrf
*zvrf
)
304 vrf_bitmap_set (client
->redist_default
, zvrf_id (zvrf
));
305 zebra_redistribute_default (client
, zvrf_id (zvrf
));
309 zebra_redistribute_default_delete (int command
, struct zserv
*client
,
310 int length
, struct zebra_vrf
*zvrf
)
312 vrf_bitmap_unset (client
->redist_default
, zvrf_id (zvrf
));
315 /* Interface up information. */
317 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
))
329 zsend_interface_update (ZEBRA_INTERFACE_UP
, client
, ifp
);
330 zsend_interface_link_params (client
, ifp
);
335 /* Interface down information. */
337 zebra_interface_down_update (struct interface
*ifp
)
339 struct listnode
*node
, *nnode
;
340 struct zserv
*client
;
342 if (IS_ZEBRA_DEBUG_EVENT
)
343 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_DOWN %s", ifp
->name
);
345 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
347 zsend_interface_update (ZEBRA_INTERFACE_DOWN
, client
, ifp
);
351 /* Interface information update. */
353 zebra_interface_add_update (struct interface
*ifp
)
355 struct listnode
*node
, *nnode
;
356 struct zserv
*client
;
358 if (IS_ZEBRA_DEBUG_EVENT
)
359 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADD %s[%d]", ifp
->name
, ifp
->vrf_id
);
361 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
365 zsend_interface_add (client
, ifp
);
366 zsend_interface_link_params (client
, ifp
);
371 zebra_interface_delete_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_DELETE %s", ifp
->name
);
379 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
382 zsend_interface_delete (client
, ifp
);
386 /* Interface address addition. */
388 zebra_interface_address_add_update (struct interface
*ifp
,
389 struct connected
*ifc
)
391 struct listnode
*node
, *nnode
;
392 struct zserv
*client
;
395 if (IS_ZEBRA_DEBUG_EVENT
)
397 char buf
[PREFIX_STRLEN
];
400 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %s on %s",
401 prefix2str (p
, buf
, sizeof(buf
)),
405 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
406 zlog_warn("WARNING: advertising address to clients that is not yet usable.");
408 router_id_add_address(ifc
);
410 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
411 if (CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
413 client
->connected_rt_add_cnt
++;
414 zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD
, client
, ifp
, ifc
);
418 /* Interface address deletion. */
420 zebra_interface_address_delete_update (struct interface
*ifp
,
421 struct connected
*ifc
)
423 struct listnode
*node
, *nnode
;
424 struct zserv
*client
;
427 if (IS_ZEBRA_DEBUG_EVENT
)
429 char buf
[PREFIX_STRLEN
];
432 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %s on %s",
433 prefix2str (p
, buf
, sizeof(buf
)),
437 router_id_del_address(ifc
);
439 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
440 if (CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
442 client
->connected_rt_del_cnt
++;
443 zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_DELETE
, client
, ifp
, ifc
);
447 /* Interface VRF change. May need to delete from clients not interested in
448 * the new VRF. Note that this function is invoked *prior* to the VRF change.
451 zebra_interface_vrf_update_del (struct interface
*ifp
, vrf_id_t new_vrf_id
)
453 struct listnode
*node
, *nnode
;
454 struct zserv
*client
;
456 if (IS_ZEBRA_DEBUG_EVENT
)
457 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/DEL %s VRF Id %u -> %u",
458 ifp
->name
, ifp
->vrf_id
, new_vrf_id
);
460 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
462 /* Need to delete if the client is not interested in the new VRF. */
463 zsend_interface_update (ZEBRA_INTERFACE_DOWN
, client
, ifp
);
465 zsend_interface_delete (client
, ifp
);
466 zsend_interface_vrf_update (client
, ifp
, new_vrf_id
);
470 /* Interface VRF change. This function is invoked *post* VRF change and sends an
471 * add to clients who are interested in the new VRF but not in the old VRF.
474 zebra_interface_vrf_update_add (struct interface
*ifp
, vrf_id_t old_vrf_id
)
476 struct listnode
*node
, *nnode
;
477 struct zserv
*client
;
479 if (IS_ZEBRA_DEBUG_EVENT
)
480 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_VRF_UPDATE/ADD %s VRF Id %u -> %u",
481 ifp
->name
, old_vrf_id
, ifp
->vrf_id
);
483 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
485 /* Need to add if the client is interested in the new VRF. */
487 zsend_interface_add (client
, ifp
);
488 zsend_interface_addresses (client
, ifp
);
493 zebra_add_import_table_entry (struct route_node
*rn
, struct route_entry
*re
, const char *rmap_name
)
495 struct route_entry
*newre
;
496 struct route_entry
*same
;
498 struct nexthop
*nhop
;
500 route_map_result_t ret
= RMAP_MATCH
;
503 ret
= zebra_import_table_route_map_check (AFI_IP
, re
->type
, &rn
->p
, re
->nexthop
, re
->vrf_id
,
506 if (ret
== RMAP_MATCH
)
508 if (rn
->p
.family
== AF_INET
)
511 p
.prefixlen
= rn
->p
.prefixlen
;
512 p
.u
.prefix4
= rn
->p
.u
.prefix4
;
514 RNODE_FOREACH_RE (rn
, same
)
516 if (CHECK_FLAG (same
->status
, ROUTE_ENTRY_REMOVED
))
519 if (same
->type
== re
->type
&& same
->instance
== re
->instance
520 && same
->table
== re
->table
521 && same
->type
!= ZEBRA_ROUTE_CONNECT
)
526 zebra_del_import_table_entry (rn
, same
);
529 if (re
->nexthop_num
== 1)
532 if (nhop
->type
== NEXTHOP_TYPE_IFINDEX
)
535 gate
= (union g_addr
*)&nhop
->gate
.ipv4
;
537 rib_add (AFI_IP
, SAFI_UNICAST
, re
->vrf_id
, ZEBRA_ROUTE_TABLE
,
538 re
->table
, 0, &p
, NULL
, gate
, (union g_addr
*)&nhop
->src
.ipv4
,
539 nhop
->ifindex
, zebrad
.rtm_table_default
,
541 zebra_import_table_distance
[AFI_IP
][re
->table
]);
543 else if (re
->nexthop_num
> 1)
545 newre
= XCALLOC (MTYPE_RE
, sizeof (struct route_entry
));
546 newre
->type
= ZEBRA_ROUTE_TABLE
;
547 newre
->distance
= zebra_import_table_distance
[AFI_IP
][re
->table
];
548 newre
->flags
= re
->flags
;
549 newre
->metric
= re
->metric
;
550 newre
->mtu
= re
->mtu
;
551 newre
->table
= zebrad
.rtm_table_default
;
552 newre
->nexthop_num
= 0;
553 newre
->uptime
= time(NULL
);
554 newre
->instance
= re
->table
;
556 /* Assuming these routes are never recursive */
557 for (nhop
= re
->nexthop
; nhop
; nhop
= nhop
->next
)
558 route_entry_copy_nexthops(newre
, nhop
);
560 rib_add_multipath(AFI_IP
, SAFI_UNICAST
, &p
, NULL
, newre
);
566 zebra_del_import_table_entry (rn
, re
);
568 /* DD: Add IPv6 code */
573 zebra_del_import_table_entry (struct route_node
*rn
, struct route_entry
*re
)
577 if (rn
->p
.family
== AF_INET
)
580 p
.prefixlen
= rn
->p
.prefixlen
;
581 p
.u
.prefix4
= rn
->p
.u
.prefix4
;
583 rib_delete (AFI_IP
, SAFI_UNICAST
, re
->vrf_id
, ZEBRA_ROUTE_TABLE
,
584 re
->table
, re
->flags
, &p
, NULL
, NULL
,
585 0, zebrad
.rtm_table_default
);
587 /* DD: Add IPv6 code */
592 /* Assuming no one calls this with the main routing table */
594 zebra_import_table (afi_t afi
, u_int32_t table_id
, u_int32_t distance
, const char *rmap_name
, int add
)
596 struct route_table
*table
;
597 struct route_entry
*re
;
598 struct route_node
*rn
;
600 if (!is_zebra_valid_kernel_table(table_id
) ||
601 ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
)))
607 table
= zebra_vrf_other_route_table(afi
, table_id
, VRF_DEFAULT
);
612 else if (IS_ZEBRA_DEBUG_RIB
)
614 zlog_debug ("%s routes from table %d",
615 add
? "Importing" : "Unimporting", table_id
);
621 zebra_add_import_table_route_map (afi
, rmap_name
, table_id
);
624 rmap_name
= zebra_get_import_table_route_map (afi
, table_id
);
626 zebra_del_import_table_route_map (afi
, table_id
);
629 zebra_import_table_used
[afi
][table_id
] = 1;
630 zebra_import_table_distance
[afi
][table_id
] = distance
;
634 zebra_import_table_used
[afi
][table_id
] = 0;
635 zebra_import_table_distance
[afi
][table_id
] = ZEBRA_TABLE_DISTANCE_DEFAULT
;
637 rmap_name
= zebra_get_import_table_route_map (afi
, table_id
);
639 zebra_del_import_table_route_map (afi
, table_id
);
642 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
644 /* For each entry in the non-default routing table,
645 * add the entry in the main table
650 RNODE_FOREACH_RE (rn
, re
)
652 if (CHECK_FLAG (re
->status
, ROUTE_ENTRY_REMOVED
))
660 if (((afi
== AFI_IP
) && (rn
->p
.family
== AF_INET
)) ||
661 ((afi
== AFI_IP6
) && (rn
->p
.family
== AF_INET6
)))
664 zebra_add_import_table_entry (rn
, re
, rmap_name
);
666 zebra_del_import_table_entry (rn
, re
);
673 zebra_import_table_config (struct vty
*vty
)
678 char afi_str
[AFI_MAX
][10] = {"", "ip", "ipv6", "ethernet"};
679 const char *rmap_name
;
681 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
683 for (i
= 1; i
< ZEBRA_KERNEL_TABLE_MAX
; i
++)
685 if (is_zebra_import_table_enabled(afi
, i
))
687 if (zebra_import_table_distance
[afi
][i
] != ZEBRA_TABLE_DISTANCE_DEFAULT
)
689 vty_out(vty
, "%s import-table %d distance %d", afi_str
[afi
],
690 i
, zebra_import_table_distance
[afi
][i
]);
694 vty_out(vty
, "%s import-table %d", afi_str
[afi
], i
);
697 rmap_name
= zebra_get_import_table_route_map (afi
, i
);
699 vty_out(vty
, " route-map %s", rmap_name
);
701 vty_out(vty
, "%s", VTY_NEWLINE
);
711 zebra_import_table_rm_update ()
715 struct route_table
*table
;
716 struct route_entry
*re
;
717 struct route_node
*rn
;
718 const char *rmap_name
;
720 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
722 for (i
= 1; i
< ZEBRA_KERNEL_TABLE_MAX
; i
++)
724 if (is_zebra_import_table_enabled(afi
, i
))
726 rmap_name
= zebra_get_import_table_route_map (afi
, i
);
730 table
= zebra_vrf_other_route_table(afi
, i
, VRF_DEFAULT
);
731 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
733 /* For each entry in the non-default routing table,
734 * add the entry in the main table
739 RNODE_FOREACH_RE (rn
, re
)
741 if (CHECK_FLAG (re
->status
, ROUTE_ENTRY_REMOVED
))
749 if (((afi
== AFI_IP
) && (rn
->p
.family
== AF_INET
)) ||
750 ((afi
== AFI_IP6
) && (rn
->p
.family
== AF_INET6
)))
751 zebra_add_import_table_entry (rn
, re
, rmap_name
);
760 /* Interface parameters update */
762 zebra_interface_parameters_update (struct interface
*ifp
)
764 struct listnode
*node
, *nnode
;
765 struct zserv
*client
;
767 if (IS_ZEBRA_DEBUG_EVENT
)
768 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_LINK_PARAMS %s", ifp
->name
);
770 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
772 zsend_interface_link_params (client
, ifp
);