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
34 #include "zebra/rib.h"
35 #include "zebra/zserv.h"
36 #include "zebra/redistribute.h"
37 #include "zebra/debug.h"
38 #include "zebra/router-id.h"
40 #define ZEBRA_PTM_SUPPORT
43 /* master zebra server structure */
44 extern struct zebra_t zebrad
;
46 /* array holding redistribute info about table redistribution */
47 /* bit AFI is set if that AFI is redistributing routes from this table */
48 static u_char zebra_import_table_used
[ZEBRA_KERNEL_TABLE_MAX
];
49 static u_int32_t zebra_import_table_distance
[AFI_MAX
][ZEBRA_KERNEL_TABLE_MAX
];
52 is_zebra_import_table_enabled(afi_t afi
, u_int32_t table_id
)
54 if (is_zebra_valid_kernel_table(table_id
))
56 if (CHECK_FLAG(zebra_import_table_used
[table_id
], (u_char
)afi
))
66 is_default (struct prefix
*p
)
68 if (p
->family
== AF_INET
)
69 if (p
->u
.prefix4
.s_addr
== 0 && p
->prefixlen
== 0)
72 #if 0 /* IPv6 default separation is now pending until protocol daemon
74 if (p
->family
== AF_INET6
)
75 if (IN6_IS_ADDR_UNSPECIFIED (&p
->u
.prefix6
) && p
->prefixlen
== 0)
78 #endif /* HAVE_IPV6 */
83 zebra_redistribute_default (struct zserv
*client
)
86 struct route_table
*table
;
87 struct route_node
*rn
;
90 struct prefix_ipv6 p6
;
91 #endif /* HAVE_IPV6 */
94 /* Lookup default route. */
95 memset (&p
, 0, sizeof (struct prefix_ipv4
));
99 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
102 rn
= route_node_lookup (table
, (struct prefix
*)&p
);
105 RNODE_FOREACH_RIB (rn
, newrib
)
106 if (CHECK_FLAG (newrib
->flags
, ZEBRA_FLAG_SELECTED
)
107 && newrib
->distance
!= DISTANCE_INFINITY
)
108 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV4_ADD
, client
, &rn
->p
, newrib
);
109 route_unlock_node (rn
);
114 /* Lookup default route. */
115 memset (&p6
, 0, sizeof (struct prefix_ipv6
));
116 p6
.family
= AF_INET6
;
119 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
122 rn
= route_node_lookup (table
, (struct prefix
*)&p6
);
125 RNODE_FOREACH_RIB (rn
, newrib
)
126 if (CHECK_FLAG (newrib
->flags
, ZEBRA_FLAG_SELECTED
)
127 && newrib
->distance
!= DISTANCE_INFINITY
)
128 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV6_ADD
, client
, &rn
->p
, newrib
);
129 route_unlock_node (rn
);
132 #endif /* HAVE_IPV6 */
135 /* Redistribute routes. */
137 zebra_redistribute (struct zserv
*client
, int type
, u_short instance
)
140 struct route_table
*table
;
141 struct route_node
*rn
;
143 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
145 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
146 RNODE_FOREACH_RIB (rn
, newrib
)
147 if (CHECK_FLAG (newrib
->flags
, ZEBRA_FLAG_SELECTED
)
148 && newrib
->type
== type
149 && newrib
->instance
== instance
150 && newrib
->distance
!= DISTANCE_INFINITY
151 && zebra_check_addr (&rn
->p
))
153 client
->redist_v4_add_cnt
++;
154 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV4_ADD
, client
, &rn
->p
, newrib
);
158 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
160 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
161 RNODE_FOREACH_RIB (rn
, newrib
)
162 if (CHECK_FLAG (newrib
->flags
, ZEBRA_FLAG_SELECTED
)
163 && newrib
->type
== type
164 && newrib
->instance
== instance
165 && newrib
->distance
!= DISTANCE_INFINITY
166 && zebra_check_addr (&rn
->p
))
168 client
->redist_v6_add_cnt
++;
169 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV6_ADD
, client
, &rn
->p
, newrib
);
171 #endif /* HAVE_IPV6 */
175 redistribute_add (struct prefix
*p
, struct rib
*rib
)
177 struct listnode
*node
, *nnode
;
178 struct zserv
*client
;
180 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
184 if ((p
->family
== AF_INET
) &&
185 (client
->redist_default
||
186 redist_check_instance(&client
->redist
[AFI_IP
][rib
->type
],
189 client
->redist_v4_add_cnt
++;
190 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV4_ADD
, client
,
194 if ((p
->family
== AF_INET6
) &&
195 (client
->redist_default
||
196 redist_check_instance(&client
->redist
[AFI_IP6
][rib
->type
],
199 client
->redist_v6_add_cnt
++;
200 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV6_ADD
, client
,
203 #endif /* HAVE_IPV6 */
207 if ((p
->family
== AF_INET
) &&
208 redist_check_instance(&client
->redist
[AFI_IP
][rib
->type
],
211 client
->redist_v4_add_cnt
++;
212 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV4_ADD
, client
,
216 if ((p
->family
== AF_INET6
) &&
217 redist_check_instance(&client
->redist
[AFI_IP6
][rib
->type
],
220 client
->redist_v6_add_cnt
++;
221 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV6_ADD
, client
,
224 #endif /* HAVE_IPV6 */
230 redistribute_delete (struct prefix
*p
, struct rib
*rib
)
232 struct listnode
*node
, *nnode
;
233 struct zserv
*client
;
235 /* Add DISTANCE_INFINITY check. */
236 if (rib
->distance
== DISTANCE_INFINITY
)
239 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
243 if ((p
->family
== AF_INET
) &&
244 (client
->redist_default
||
245 redist_check_instance(&client
->redist
[AFI_IP
][rib
->type
],
247 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV4_DEL
, client
, p
,
250 if ((p
->family
== AF_INET6
) &&
251 (client
->redist_default
||
252 redist_check_instance(&client
->redist
[AFI_IP6
][rib
->type
],
254 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV6_DEL
, client
, p
,
256 #endif /* HAVE_IPV6 */
260 if ((p
->family
== AF_INET
) &&
261 redist_check_instance(&client
->redist
[AFI_IP
][rib
->type
],
263 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV4_DEL
, client
, p
,
266 if ((p
->family
== AF_INET6
) &&
267 redist_check_instance(&client
->redist
[AFI_IP6
][rib
->type
],
269 zsend_redistribute_route (ZEBRA_REDISTRIBUTE_IPV6_DEL
, client
, p
,
271 #endif /* HAVE_IPV6 */
277 zebra_redistribute_add (int command
, struct zserv
*client
, int length
)
283 afi
= stream_getc (client
->ibuf
);
284 type
= stream_getc (client
->ibuf
);
285 instance
= stream_getw (client
->ibuf
);
287 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
)
290 if (!redist_check_instance(&client
->redist
[afi
][type
], instance
))
292 redist_add_instance(&client
->redist
[afi
][type
], instance
);
293 zebra_redistribute (client
, type
, instance
);
298 zebra_redistribute_delete (int command
, struct zserv
*client
, int length
)
304 afi
= stream_getc (client
->ibuf
);
305 type
= stream_getc (client
->ibuf
);
306 instance
= stream_getw (client
->ibuf
);
308 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
)
311 if (redist_check_instance(&client
->redist
[afi
][type
], instance
))
313 redist_del_instance(&client
->redist
[afi
][type
], instance
);
314 //Pending: why no reaction here?
319 zebra_redistribute_default_add (int command
, struct zserv
*client
, int length
)
321 client
->redist_default
= 1;
322 zebra_redistribute_default (client
);
326 zebra_redistribute_default_delete (int command
, struct zserv
*client
,
329 client
->redist_default
= 0;;
332 /* Interface up information. */
334 zebra_interface_up_update (struct interface
*ifp
)
336 struct listnode
*node
, *nnode
;
337 struct zserv
*client
;
339 if (IS_ZEBRA_DEBUG_EVENT
)
340 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_UP %s", ifp
->name
);
342 if (ifp
->ptm_status
|| !ifp
->ptm_enable
) {
343 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
345 zsend_interface_update (ZEBRA_INTERFACE_UP
, client
, ifp
);
350 /* Interface down information. */
352 zebra_interface_down_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_DOWN %s", ifp
->name
);
360 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
362 zsend_interface_update (ZEBRA_INTERFACE_DOWN
, client
, ifp
);
366 /* Interface information update. */
368 zebra_interface_add_update (struct interface
*ifp
)
370 struct listnode
*node
, *nnode
;
371 struct zserv
*client
;
373 if (IS_ZEBRA_DEBUG_EVENT
)
374 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADD %s", ifp
->name
);
376 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
380 zsend_interface_add (client
, ifp
);
385 zebra_interface_delete_update (struct interface
*ifp
)
387 struct listnode
*node
, *nnode
;
388 struct zserv
*client
;
390 if (IS_ZEBRA_DEBUG_EVENT
)
391 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_DELETE %s", ifp
->name
);
393 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
397 zsend_interface_delete (client
, ifp
);
401 /* Interface address addition. */
403 zebra_interface_address_add_update (struct interface
*ifp
,
404 struct connected
*ifc
)
406 struct listnode
*node
, *nnode
;
407 struct zserv
*client
;
410 if (IS_ZEBRA_DEBUG_EVENT
)
412 char buf
[INET6_ADDRSTRLEN
];
415 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %s/%d on %s",
416 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
417 p
->prefixlen
, ifc
->ifp
->name
);
420 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
421 zlog_warn("WARNING: advertising address to clients that is not yet usable.");
423 router_id_add_address(ifc
);
425 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
426 if (client
->ifinfo
&& CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
428 client
->connected_rt_add_cnt
++;
429 zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD
, client
, ifp
, ifc
);
433 /* Interface address deletion. */
435 zebra_interface_address_delete_update (struct interface
*ifp
,
436 struct connected
*ifc
)
438 struct listnode
*node
, *nnode
;
439 struct zserv
*client
;
442 if (IS_ZEBRA_DEBUG_EVENT
)
444 char buf
[INET6_ADDRSTRLEN
];
447 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %s/%d on %s",
448 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
449 p
->prefixlen
, ifc
->ifp
->name
);
452 router_id_del_address(ifc
);
454 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
455 if (client
->ifinfo
&& CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
457 client
->connected_rt_del_cnt
++;
458 zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_DELETE
, client
, ifp
, ifc
);
463 zebra_add_import_table_entry (struct route_node
*rn
, struct rib
*rib
)
466 struct prefix_ipv4 p4
;
467 struct nexthop
*nhop
;
468 struct in_addr
*gate
;
470 if (rn
->p
.family
== AF_INET
)
473 p4
.prefixlen
= rn
->p
.prefixlen
;
474 p4
.prefix
= rn
->p
.u
.prefix4
;
476 if (rib
->nexthop_num
== 1)
479 if ((nhop
->type
== NEXTHOP_TYPE_IFINDEX
) ||
480 (nhop
->type
== NEXTHOP_TYPE_IFNAME
))
483 gate
= &nhop
->gate
.ipv4
;
485 rib_add_ipv4(ZEBRA_ROUTE_TABLE
, rib
->table
, 0, &p4
,
486 gate
, &nhop
->src
.ipv4
,
487 nhop
->ifindex
, zebrad
.rtm_table_default
,
489 zebra_import_table_distance
[AFI_IP
][rib
->table
],
492 else if (rib
->nexthop_num
> 1)
494 newrib
= XCALLOC (MTYPE_RIB
, sizeof (struct rib
));
495 newrib
->type
= ZEBRA_ROUTE_TABLE
;
496 newrib
->distance
= zebra_import_table_distance
[AFI_IP
][rib
->table
];
497 newrib
->flags
= rib
->flags
;
498 newrib
->metric
= rib
->metric
;
499 newrib
->table
= zebrad
.rtm_table_default
;
500 newrib
->nexthop_num
= 0;
501 newrib
->uptime
= time(NULL
);
502 newrib
->instance
= rib
->table
;
504 /* Assuming these routes are never recursive */
505 for (nhop
= rib
->nexthop
; nhop
; nhop
= nhop
->next
)
506 copy_nexthops(newrib
, nhop
);
508 rib_add_ipv4_multipath(&p4
, newrib
, SAFI_UNICAST
);
511 /* DD: Add IPv6 code */
516 zebra_del_import_table_entry (struct route_node
*rn
, struct rib
*rib
)
518 struct prefix_ipv4 p4
;
520 if (rn
->p
.family
== AF_INET
)
523 p4
.prefixlen
= rn
->p
.prefixlen
;
524 p4
.prefix
= rn
->p
.u
.prefix4
;
526 rib_delete_ipv4(ZEBRA_ROUTE_TABLE
, rib
->table
, rib
->flags
, &p4
, NULL
,
527 0, zebrad
.rtm_table_default
, SAFI_UNICAST
);
529 /* DD: Add IPv6 code */
534 /* Assuming no one calls this with the main routing table */
536 zebra_import_table (afi_t afi
, u_int32_t table_id
, u_int32_t distance
, int add
)
538 struct route_table
*table
;
540 struct route_node
*rn
;
542 if (!is_zebra_valid_kernel_table(table_id
) ||
543 ((table_id
== RT_TABLE_MAIN
) || (table_id
== zebrad
.rtm_table_default
)))
549 table
= vrf_other_route_table(afi
, table_id
, 0);
554 else if (IS_ZEBRA_DEBUG_RIB
)
556 zlog_debug ("%s routes from table %d",
557 add
? "Importing" : "Unimporting", table_id
);
562 SET_FLAG(zebra_import_table_used
[table_id
], afi
);
563 zebra_import_table_distance
[afi
][table_id
] = distance
;
567 UNSET_FLAG(zebra_import_table_used
[table_id
], (u_char
)afi
);
568 zebra_import_table_distance
[afi
][table_id
] = ZEBRA_TABLE_DISTANCE_DEFAULT
;
571 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
573 /* For each entry in the non-default routing table,
574 * add the entry in the main table
579 RNODE_FOREACH_RIB (rn
, rib
)
581 if (CHECK_FLAG (rib
->status
, RIB_ENTRY_REMOVED
))
589 if (((afi
== AFI_IP
) && (rn
->p
.family
== AF_INET
)) ||
590 ((afi
== AFI_IP6
) && (rn
->p
.family
== AF_INET6
)))
593 zebra_add_import_table_entry (rn
, rib
);
595 zebra_del_import_table_entry (rn
, rib
);
602 zebra_import_table_config (struct vty
*vty
)
607 char afi_str
[AFI_MAX
][6] = {"", "ip", "ipv6"};
609 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
611 for (i
= 1; i
< ZEBRA_KERNEL_TABLE_MAX
; i
++)
613 if (is_zebra_import_table_enabled(afi
, i
))
615 if (zebra_import_table_distance
[afi
][i
] != ZEBRA_TABLE_DISTANCE_DEFAULT
)
617 vty_out(vty
, "%s import-table %d distance %d%s", afi_str
[afi
],
618 i
, zebra_import_table_distance
[afi
][i
], VTY_NEWLINE
);
622 vty_out(vty
, "%s import-table %d%s", afi_str
[afi
], i
,