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 /* Interface address addition. */
461 zebra_interface_address_add_update (struct interface
*ifp
,
462 struct connected
*ifc
)
464 struct listnode
*node
, *nnode
;
465 struct zserv
*client
;
468 if (IS_ZEBRA_DEBUG_EVENT
)
470 char buf
[INET6_ADDRSTRLEN
];
473 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %s/%d on %s",
474 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
475 p
->prefixlen
, ifc
->ifp
->name
);
478 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
479 zlog_warn("WARNING: advertising address to clients that is not yet usable.");
481 router_id_add_address(ifc
);
483 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
484 if (client
->ifinfo
&& CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
486 client
->connected_rt_add_cnt
++;
487 zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD
, client
, ifp
, ifc
);
491 /* Interface address deletion. */
493 zebra_interface_address_delete_update (struct interface
*ifp
,
494 struct connected
*ifc
)
496 struct listnode
*node
, *nnode
;
497 struct zserv
*client
;
500 if (IS_ZEBRA_DEBUG_EVENT
)
502 char buf
[INET6_ADDRSTRLEN
];
505 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %s/%d on %s",
506 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
507 p
->prefixlen
, ifc
->ifp
->name
);
510 router_id_del_address(ifc
);
512 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
513 if (client
->ifinfo
&& CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
515 client
->connected_rt_del_cnt
++;
516 zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_DELETE
, client
, ifp
, ifc
);
521 zebra_add_import_table_entry (struct route_node
*rn
, struct rib
*rib
)
524 struct prefix_ipv4 p4
;
525 struct nexthop
*nhop
;
526 struct in_addr
*gate
;
528 if (rn
->p
.family
== AF_INET
)
531 p4
.prefixlen
= rn
->p
.prefixlen
;
532 p4
.prefix
= rn
->p
.u
.prefix4
;
534 if (rib
->nexthop_num
== 1)
537 if (nhop
->type
== NEXTHOP_TYPE_IFINDEX
)
540 gate
= &nhop
->gate
.ipv4
;
542 rib_add_ipv4(ZEBRA_ROUTE_TABLE
, rib
->table
, 0, &p4
,
543 gate
, &nhop
->src
.ipv4
,
544 nhop
->ifindex
, rib
->vrf_id
, zebrad
.rtm_table_default
,
546 zebra_import_table_distance
[AFI_IP
][rib
->table
],
549 else if (rib
->nexthop_num
> 1)
551 newrib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
552 newrib
->type
= ZEBRA_ROUTE_TABLE
;
553 newrib
->distance
= zebra_import_table_distance
[AFI_IP
][rib
->table
];
554 newrib
->flags
= rib
->flags
;
555 newrib
->metric
= rib
->metric
;
556 newrib
->table
= zebrad
.rtm_table_default
;
557 newrib
->nexthop_num
= 0;
558 newrib
->uptime
= time(NULL
);
559 newrib
->instance
= rib
->table
;
561 /* Assuming these routes are never recursive */
562 for (nhop
= rib
->nexthop
; nhop
; nhop
= nhop
->next
)
563 rib_copy_nexthops(newrib
, nhop
);
565 rib_add_ipv4_multipath(&p4
, newrib
, SAFI_UNICAST
);
568 /* DD: Add IPv6 code */
573 zebra_del_import_table_entry (struct route_node
*rn
, struct rib
*rib
)
575 struct prefix_ipv4 p4
;
577 if (rn
->p
.family
== AF_INET
)
580 p4
.prefixlen
= rn
->p
.prefixlen
;
581 p4
.prefix
= rn
->p
.u
.prefix4
;
583 rib_delete_ipv4(ZEBRA_ROUTE_TABLE
, rib
->table
, rib
->flags
, &p4
, NULL
,
584 0, rib
->vrf_id
, zebrad
.rtm_table_default
, SAFI_UNICAST
);
586 /* DD: Add IPv6 code */
591 /* Assuming no one calls this with the main routing table */
593 zebra_import_table (afi_t afi
, u_int32_t table_id
, u_int32_t distance
, int add
)
595 struct route_table
*table
;
597 struct route_node
*rn
;
599 if (!is_zebra_valid_kernel_table(table_id
) ||
600 ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
)))
606 table
= zebra_vrf_other_route_table(afi
, table_id
, VRF_DEFAULT
);
611 else if (IS_ZEBRA_DEBUG_RIB
)
613 zlog_debug ("%s routes from table %d",
614 add
? "Importing" : "Unimporting", table_id
);
619 SET_FLAG(zebra_import_table_used
[table_id
], afi
);
620 zebra_import_table_distance
[afi
][table_id
] = distance
;
624 UNSET_FLAG(zebra_import_table_used
[table_id
], (u_char
)afi
);
625 zebra_import_table_distance
[afi
][table_id
] = ZEBRA_TABLE_DISTANCE_DEFAULT
;
628 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
630 /* For each entry in the non-default routing table,
631 * add the entry in the main table
636 RNODE_FOREACH_RIB (rn
, rib
)
638 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
646 if (((afi
== AFI_IP
) && (rn
->p
.family
== AF_INET
)) ||
647 ((afi
== AFI_IP6
) && (rn
->p
.family
== AF_INET6
)))
650 zebra_add_import_table_entry (rn
, rib
);
652 zebra_del_import_table_entry (rn
, rib
);
659 zebra_import_table_config (struct vty
*vty
)
664 char afi_str
[AFI_MAX
][6] = {"", "ip", "ipv6"};
666 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
668 for (i
= 1; i
< ZEBRA_KERNEL_TABLE_MAX
; i
++)
670 if (is_zebra_import_table_enabled(afi
, i
))
672 if (zebra_import_table_distance
[afi
][i
] != ZEBRA_TABLE_DISTANCE_DEFAULT
)
674 vty_out(vty
, "%s import-table %d distance %d%s", afi_str
[afi
],
675 i
, zebra_import_table_distance
[afi
][i
], VTY_NEWLINE
);
679 vty_out(vty
, "%s import-table %d%s", afi_str
[afi
], i
,