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
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
35 #include "zebra/rib.h"
36 #include "zebra/zserv.h"
37 #include "zebra/redistribute.h"
38 #include "zebra/debug.h"
39 #include "zebra/router-id.h"
41 #define ZEBRA_PTM_SUPPORT
44 /* master zebra server structure */
45 extern struct zebra_t zebrad
;
47 /* array holding redistribute info about table redistribution */
48 /* bit AFI is set if that AFI is redistributing routes from this table */
49 static u_char zebra_import_table_used
[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
))
57 if (CHECK_FLAG(zebra_import_table_used
[table_id
], (u_char
)afi
))
67 is_default (struct prefix
*p
)
69 if (p
->family
== AF_INET
)
70 if (p
->u
.prefix4
.s_addr
== 0 && p
->prefixlen
== 0)
73 #if 0 /* IPv6 default separation is now pending until protocol daemon
75 if (p
->family
== AF_INET6
)
76 if (IN6_IS_ADDR_UNSPECIFIED (&p
->u
.prefix6
) && p
->prefixlen
== 0)
79 #endif /* HAVE_IPV6 */
84 zebra_redistribute_default (struct zserv
*client
, vrf_id_t vrf_id
)
87 struct route_table
*table
;
88 struct route_node
*rn
;
91 struct prefix_ipv6 p6
;
92 #endif /* HAVE_IPV6 */
95 /* Lookup default route. */
96 memset (&p
, 0, sizeof (struct prefix_ipv4
));
100 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
103 rn
= route_node_lookup (table
, (struct prefix
*)&p
);
106 RNODE_FOREACH_RIB (rn
, newrib
)
107 if (CHECK_FLAG (newrib
->flags
, ZEBRA_FLAG_SELECTED
)
108 && newrib
->distance
!= DISTANCE_INFINITY
)
109 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV4_ADD
, client
, &rn
->p
, newrib
);
110 route_unlock_node (rn
);
115 /* Lookup default route. */
116 memset (&p6
, 0, sizeof (struct prefix_ipv6
));
117 p6
.family
= AF_INET6
;
120 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
123 rn
= route_node_lookup (table
, (struct prefix
*)&p6
);
126 RNODE_FOREACH_RIB (rn
, newrib
)
127 if (CHECK_FLAG (newrib
->flags
, ZEBRA_FLAG_SELECTED
)
128 && newrib
->distance
!= DISTANCE_INFINITY
)
129 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV6_ADD
, client
, &rn
->p
, newrib
);
130 route_unlock_node (rn
);
133 #endif /* HAVE_IPV6 */
136 /* Redistribute routes. */
138 zebra_redistribute (struct zserv
*client
, int type
, u_short instance
, vrf_id_t vrf_id
)
141 struct route_table
*table
;
142 struct route_node
*rn
;
144 table
= zebra_vrf_table (AFI_IP
, SAFI_UNICAST
, vrf_id
);
146 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
147 RNODE_FOREACH_RIB (rn
, newrib
)
148 if (CHECK_FLAG (newrib
->flags
, ZEBRA_FLAG_SELECTED
)
149 && newrib
->type
== type
150 && newrib
->instance
== instance
151 && newrib
->distance
!= DISTANCE_INFINITY
152 && zebra_check_addr (&rn
->p
))
154 client
->redist_v4_add_cnt
++;
155 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV4_ADD
, client
, &rn
->p
, newrib
);
159 table
= zebra_vrf_table (AFI_IP6
, SAFI_UNICAST
, vrf_id
);
161 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
162 RNODE_FOREACH_RIB (rn
, newrib
)
163 if (CHECK_FLAG (newrib
->flags
, ZEBRA_FLAG_SELECTED
)
164 && newrib
->type
== type
165 && newrib
->instance
== instance
166 && newrib
->distance
!= DISTANCE_INFINITY
167 && zebra_check_addr (&rn
->p
))
169 client
->redist_v6_add_cnt
++;
170 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV6_ADD
, client
, &rn
->p
, newrib
);
172 #endif /* HAVE_IPV6 */
175 /* Either advertise a route for redistribution to registered clients or */
176 /* withdraw redistribution if add cannot be done for client */
178 redistribute_update (struct prefix
*p
, struct rib
*rib
, struct rib
*prev_rib
)
180 struct listnode
*node
, *nnode
;
181 struct zserv
*client
;
182 int send_redistribute
;
184 char buf
[INET6_ADDRSTRLEN
];
186 if (IS_ZEBRA_DEBUG_RIB
)
188 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
189 zlog_debug ("%u:%s/%d: Redist update rib %p (type %d), old %p (type %d)",
190 rib
->vrf_id
, buf
, p
->prefixlen
, rib
, rib
->type
,
191 prev_rib
, prev_rib
? prev_rib
->type
: -1);
194 afi
= family2afi(p
->family
);
197 zlog_warn("%s: Unknown AFI/SAFI prefix received\n", __FUNCTION__
);
201 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
203 send_redistribute
= 0;
205 if (is_default(p
) && client
->redist_default
)
206 send_redistribute
= 1;
208 if (rib
->instance
&& redist_check_instance(&client
->mi_redist
[afi
][rib
->type
],
210 send_redistribute
= 1;
212 if ((is_default (p
) &&
213 vrf_bitmap_check (client
->redist_default
, rib
->vrf_id
))
214 || vrf_bitmap_check (client
->redist
[afi
][rib
->type
], rib
->vrf_id
))
215 send_redistribute
= 1;
217 if (send_redistribute
)
222 client
->redist_v4_add_cnt
++;
223 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV4_ADD
, client
,
227 client
->redist_v6_add_cnt
++;
228 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV6_ADD
, client
,
232 zlog_warn("%s: Unknown AFI/SAFI prefix received\n", __FUNCTION__
);
238 redist_check_instance(&client
->mi_redist
[afi
][prev_rib
->type
],
240 vrf_bitmap_check (client
->redist
[afi
][prev_rib
->type
], rib
->vrf_id
)))
245 client
->redist_v4_del_cnt
++;
246 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV4_DEL
, client
, p
,
250 client
->redist_v6_del_cnt
++;
251 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV6_DEL
, client
, p
,
262 redistribute_delete (struct prefix
*p
, struct rib
*rib
)
264 struct listnode
*node
, *nnode
;
265 struct zserv
*client
;
266 char buf
[INET6_ADDRSTRLEN
];
268 if (IS_ZEBRA_DEBUG_RIB
)
270 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
);
271 zlog_debug ("%u:%s/%d: Redist delete rib %p (type %d)",
272 rib
->vrf_id
, buf
, p
->prefixlen
, rib
, rib
->type
);
275 /* Add DISTANCE_INFINITY check. */
276 if (rib
->distance
== DISTANCE_INFINITY
)
279 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
283 if ((p
->family
== AF_INET
) &&
284 (vrf_bitmap_check (client
->redist_default
, rib
->vrf_id
) ||
286 redist_check_instance(&client
->mi_redist
[AFI_IP
][rib
->type
],
288 vrf_bitmap_check (client
->redist
[AFI_IP
][rib
->type
], rib
->vrf_id
)))
289 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV4_DEL
, client
, p
,
292 if ((p
->family
== AF_INET6
) &&
293 (vrf_bitmap_check (client
->redist_default
, rib
->vrf_id
) ||
295 redist_check_instance(&client
->mi_redist
[AFI_IP6
][rib
->type
],
297 vrf_bitmap_check (client
->redist
[AFI_IP6
][rib
->type
], rib
->vrf_id
)))
298 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV6_DEL
, client
, p
,
300 #endif /* HAVE_IPV6 */
304 if ((p
->family
== AF_INET
) &&
306 redist_check_instance(&client
->mi_redist
[AFI_IP
][rib
->type
],
308 vrf_bitmap_check (client
->redist
[AFI_IP
][rib
->type
], rib
->vrf_id
)))
309 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV4_DEL
, client
, p
,
312 if ((p
->family
== AF_INET6
) &&
314 redist_check_instance(&client
->mi_redist
[AFI_IP6
][rib
->type
],
316 vrf_bitmap_check (client
->redist
[AFI_IP6
][rib
->type
], rib
->vrf_id
)))
317 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV6_DEL
, client
, p
,
319 #endif /* HAVE_IPV6 */
325 zebra_redistribute_add (int command
, struct zserv
*client
, int length
,
332 afi
= stream_getc (client
->ibuf
);
333 type
= stream_getc (client
->ibuf
);
334 instance
= stream_getw (client
->ibuf
);
336 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
)
339 if (instance
&& !redist_check_instance(&client
->mi_redist
[afi
][type
], instance
))
341 redist_add_instance(&client
->mi_redist
[afi
][type
], instance
);
342 zebra_redistribute (client
, type
, instance
, vrf_id
);
345 if (! vrf_bitmap_check (client
->redist
[afi
][type
], vrf_id
))
347 vrf_bitmap_set (client
->redist
[afi
][type
], vrf_id
);
348 zebra_redistribute (client
, type
, 0, vrf_id
);
353 zebra_redistribute_delete (int command
, struct zserv
*client
, int length
,
360 afi
= stream_getc (client
->ibuf
);
361 type
= stream_getc (client
->ibuf
);
362 instance
= stream_getw (client
->ibuf
);
364 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
)
367 if (instance
&& redist_check_instance(&client
->mi_redist
[afi
][type
], instance
))
369 redist_del_instance(&client
->mi_redist
[afi
][type
], instance
);
370 //Pending: why no reaction here?
372 vrf_bitmap_unset (client
->redist
[afi
][type
], vrf_id
);
376 zebra_redistribute_default_add (int command
, struct zserv
*client
, int length
,
379 vrf_bitmap_set (client
->redist_default
, vrf_id
);
380 zebra_redistribute_default (client
, vrf_id
);
384 zebra_redistribute_default_delete (int command
, struct zserv
*client
,
385 int length
, vrf_id_t vrf_id
)
387 vrf_bitmap_unset (client
->redist_default
, vrf_id
);
390 /* Interface up information. */
392 zebra_interface_up_update (struct interface
*ifp
)
394 struct listnode
*node
, *nnode
;
395 struct zserv
*client
;
397 if (IS_ZEBRA_DEBUG_EVENT
)
398 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_UP %s", ifp
->name
);
400 if (ifp
->ptm_status
|| !ifp
->ptm_enable
) {
401 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
403 zsend_interface_update (ZEBRA_INTERFACE_UP
, client
, ifp
);
408 /* Interface down information. */
410 zebra_interface_down_update (struct interface
*ifp
)
412 struct listnode
*node
, *nnode
;
413 struct zserv
*client
;
415 if (IS_ZEBRA_DEBUG_EVENT
)
416 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_DOWN %s", ifp
->name
);
418 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
420 zsend_interface_update (ZEBRA_INTERFACE_DOWN
, client
, ifp
);
424 /* Interface information update. */
426 zebra_interface_add_update (struct interface
*ifp
)
428 struct listnode
*node
, *nnode
;
429 struct zserv
*client
;
431 if (IS_ZEBRA_DEBUG_EVENT
)
432 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADD %s", ifp
->name
);
434 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
438 zsend_interface_add (client
, ifp
);
443 zebra_interface_delete_update (struct interface
*ifp
)
445 struct listnode
*node
, *nnode
;
446 struct zserv
*client
;
448 if (IS_ZEBRA_DEBUG_EVENT
)
449 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_DELETE %s", ifp
->name
);
451 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
455 zsend_interface_delete (client
, ifp
);
459 /* VRF information update. */
461 zebra_vrf_add_update (struct vrf
*vrfp
)
463 struct listnode
*node
, *nnode
;
464 struct zserv
*client
;
466 if (IS_ZEBRA_DEBUG_EVENT
)
467 zlog_debug ("MESSAGE: ZEBRA_VRF_ADD %s", vrfp
->name
);
469 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
470 zsend_vrf_add (client
, vrfp
);
474 zebra_vrf_delete_update (struct vrf
*vrfp
)
476 struct listnode
*node
, *nnode
;
477 struct zserv
*client
;
479 if (IS_ZEBRA_DEBUG_EVENT
)
480 zlog_debug ("MESSAGE: ZEBRA_VRF_DELETE %s", vrfp
->name
);
482 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
483 zsend_vrf_delete (client
, vrfp
);
487 zebra_vrf_update_all (struct zserv
*client
)
492 for (iter
= vrf_first (); iter
!= VRF_ITER_INVALID
; iter
= vrf_next (iter
))
494 if ((vrf
= vrf_iter2vrf (iter
)) && vrf
->vrf_id
)
495 zsend_vrf_add (client
, vrf
);
500 /* Interface address addition. */
502 zebra_interface_address_add_update (struct interface
*ifp
,
503 struct connected
*ifc
)
505 struct listnode
*node
, *nnode
;
506 struct zserv
*client
;
509 if (IS_ZEBRA_DEBUG_EVENT
)
511 char buf
[INET6_ADDRSTRLEN
];
514 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %s/%d on %s",
515 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
516 p
->prefixlen
, ifc
->ifp
->name
);
519 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
520 zlog_warn("WARNING: advertising address to clients that is not yet usable.");
522 router_id_add_address(ifc
);
524 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
525 if (client
->ifinfo
&& CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
527 client
->connected_rt_add_cnt
++;
528 zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD
, client
, ifp
, ifc
);
532 /* Interface address deletion. */
534 zebra_interface_address_delete_update (struct interface
*ifp
,
535 struct connected
*ifc
)
537 struct listnode
*node
, *nnode
;
538 struct zserv
*client
;
541 if (IS_ZEBRA_DEBUG_EVENT
)
543 char buf
[INET6_ADDRSTRLEN
];
546 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %s/%d on %s",
547 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
548 p
->prefixlen
, ifc
->ifp
->name
);
551 router_id_del_address(ifc
);
553 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
554 if (client
->ifinfo
&& CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
556 client
->connected_rt_del_cnt
++;
557 zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_DELETE
, client
, ifp
, ifc
);
562 zebra_add_import_table_entry (struct route_node
*rn
, struct rib
*rib
)
565 struct prefix_ipv4 p4
;
566 struct nexthop
*nhop
;
567 struct in_addr
*gate
;
569 if (rn
->p
.family
== AF_INET
)
572 p4
.prefixlen
= rn
->p
.prefixlen
;
573 p4
.prefix
= rn
->p
.u
.prefix4
;
575 if (rib
->nexthop_num
== 1)
578 if (nhop
->type
== NEXTHOP_TYPE_IFINDEX
)
581 gate
= &nhop
->gate
.ipv4
;
583 rib_add_ipv4(ZEBRA_ROUTE_TABLE
, rib
->table
, 0, &p4
,
584 gate
, &nhop
->src
.ipv4
,
585 nhop
->ifindex
, rib
->vrf_id
, zebrad
.rtm_table_default
,
587 zebra_import_table_distance
[AFI_IP
][rib
->table
],
590 else if (rib
->nexthop_num
> 1)
592 newrib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
593 newrib
->type
= ZEBRA_ROUTE_TABLE
;
594 newrib
->distance
= zebra_import_table_distance
[AFI_IP
][rib
->table
];
595 newrib
->flags
= rib
->flags
;
596 newrib
->metric
= rib
->metric
;
597 newrib
->table
= zebrad
.rtm_table_default
;
598 newrib
->nexthop_num
= 0;
599 newrib
->uptime
= time(NULL
);
600 newrib
->instance
= rib
->table
;
602 /* Assuming these routes are never recursive */
603 for (nhop
= rib
->nexthop
; nhop
; nhop
= nhop
->next
)
604 rib_copy_nexthops(newrib
, nhop
);
606 rib_add_ipv4_multipath(&p4
, newrib
, SAFI_UNICAST
);
609 /* DD: Add IPv6 code */
614 zebra_del_import_table_entry (struct route_node
*rn
, struct rib
*rib
)
616 struct prefix_ipv4 p4
;
618 if (rn
->p
.family
== AF_INET
)
621 p4
.prefixlen
= rn
->p
.prefixlen
;
622 p4
.prefix
= rn
->p
.u
.prefix4
;
624 rib_delete_ipv4(ZEBRA_ROUTE_TABLE
, rib
->table
, rib
->flags
, &p4
, NULL
,
625 0, rib
->vrf_id
, zebrad
.rtm_table_default
, SAFI_UNICAST
);
627 /* DD: Add IPv6 code */
632 /* Assuming no one calls this with the main routing table */
634 zebra_import_table (afi_t afi
, u_int32_t table_id
, u_int32_t distance
, int add
)
636 struct route_table
*table
;
638 struct route_node
*rn
;
640 if (!is_zebra_valid_kernel_table(table_id
) ||
641 ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
)))
647 table
= zebra_vrf_other_route_table(afi
, table_id
, VRF_DEFAULT
);
652 else if (IS_ZEBRA_DEBUG_RIB
)
654 zlog_debug ("%s routes from table %d",
655 add
? "Importing" : "Unimporting", table_id
);
660 SET_FLAG(zebra_import_table_used
[table_id
], afi
);
661 zebra_import_table_distance
[afi
][table_id
] = distance
;
665 UNSET_FLAG(zebra_import_table_used
[table_id
], (u_char
)afi
);
666 zebra_import_table_distance
[afi
][table_id
] = ZEBRA_TABLE_DISTANCE_DEFAULT
;
669 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
671 /* For each entry in the non-default routing table,
672 * add the entry in the main table
677 RNODE_FOREACH_RIB (rn
, rib
)
679 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
687 if (((afi
== AFI_IP
) && (rn
->p
.family
== AF_INET
)) ||
688 ((afi
== AFI_IP6
) && (rn
->p
.family
== AF_INET6
)))
691 zebra_add_import_table_entry (rn
, rib
);
693 zebra_del_import_table_entry (rn
, rib
);
700 zebra_import_table_config (struct vty
*vty
)
705 char afi_str
[AFI_MAX
][6] = {"", "ip", "ipv6"};
707 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
709 for (i
= 1; i
< ZEBRA_KERNEL_TABLE_MAX
; i
++)
711 if (is_zebra_import_table_enabled(afi
, i
))
713 if (zebra_import_table_distance
[afi
][i
] != ZEBRA_TABLE_DISTANCE_DEFAULT
)
715 vty_out(vty
, "%s import-table %d distance %d%s", afi_str
[afi
],
716 i
, zebra_import_table_distance
[afi
][i
], VTY_NEWLINE
);
720 vty_out(vty
, "%s import-table %d%s", afi_str
[afi
], i
,