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 /* master zebra server structure */
41 extern struct zebra_t zebrad
;
44 zebra_check_addr (struct prefix
*p
)
46 if (p
->family
== AF_INET
)
50 addr
= p
->u
.prefix4
.s_addr
;
53 if (IPV4_NET127 (addr
)
55 || IPV4_LINKLOCAL(addr
))
59 if (p
->family
== AF_INET6
)
61 if (IN6_IS_ADDR_LOOPBACK (&p
->u
.prefix6
))
63 if (IN6_IS_ADDR_LINKLOCAL(&p
->u
.prefix6
))
66 #endif /* HAVE_IPV6 */
71 is_default (struct prefix
*p
)
73 if (p
->family
== AF_INET
)
74 if (p
->u
.prefix4
.s_addr
== 0 && p
->prefixlen
== 0)
77 #if 0 /* IPv6 default separation is now pending until protocol daemon
79 if (p
->family
== AF_INET6
)
80 if (IN6_IS_ADDR_UNSPECIFIED (&p
->u
.prefix6
) && p
->prefixlen
== 0)
83 #endif /* HAVE_IPV6 */
88 zebra_redistribute_default (struct zserv
*client
)
91 struct route_table
*table
;
92 struct route_node
*rn
;
95 struct prefix_ipv6 p6
;
96 #endif /* HAVE_IPV6 */
99 /* Lookup default route. */
100 memset (&p
, 0, sizeof (struct prefix_ipv4
));
104 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
107 rn
= route_node_lookup (table
, (struct prefix
*)&p
);
110 RNODE_FOREACH_RIB (rn
, newrib
)
111 if (CHECK_FLAG (newrib
->flags
, ZEBRA_FLAG_SELECTED
)
112 && newrib
->distance
!= DISTANCE_INFINITY
)
113 zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD
, client
, &rn
->p
, newrib
);
114 route_unlock_node (rn
);
119 /* Lookup default route. */
120 memset (&p6
, 0, sizeof (struct prefix_ipv6
));
121 p6
.family
= AF_INET6
;
124 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
127 rn
= route_node_lookup (table
, (struct prefix
*)&p6
);
130 RNODE_FOREACH_RIB (rn
, newrib
)
131 if (CHECK_FLAG (newrib
->flags
, ZEBRA_FLAG_SELECTED
)
132 && newrib
->distance
!= DISTANCE_INFINITY
)
133 zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD
, client
, &rn
->p
, newrib
);
134 route_unlock_node (rn
);
137 #endif /* HAVE_IPV6 */
140 /* Redistribute routes. */
142 zebra_redistribute (struct zserv
*client
, int type
)
145 struct route_table
*table
;
146 struct route_node
*rn
;
148 table
= vrf_table (AFI_IP
, SAFI_UNICAST
, 0);
150 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
151 RNODE_FOREACH_RIB (rn
, newrib
)
152 if (CHECK_FLAG (newrib
->flags
, ZEBRA_FLAG_SELECTED
)
153 && newrib
->type
== type
154 && newrib
->distance
!= DISTANCE_INFINITY
155 && zebra_check_addr (&rn
->p
))
156 zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD
, client
, &rn
->p
, newrib
);
159 table
= vrf_table (AFI_IP6
, SAFI_UNICAST
, 0);
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
->distance
!= DISTANCE_INFINITY
166 && zebra_check_addr (&rn
->p
))
167 zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD
, client
, &rn
->p
, newrib
);
168 #endif /* HAVE_IPV6 */
172 redistribute_add (struct prefix
*p
, struct rib
*rib
)
174 struct listnode
*node
, *nnode
;
175 struct zserv
*client
;
177 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
181 if (client
->redist_default
|| client
->redist
[rib
->type
])
183 if (p
->family
== AF_INET
)
184 zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD
, client
, p
, rib
);
186 if (p
->family
== AF_INET6
)
187 zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD
, client
, p
, rib
);
188 #endif /* HAVE_IPV6 */
191 else if (client
->redist
[rib
->type
])
193 if (p
->family
== AF_INET
)
194 zsend_route_multipath (ZEBRA_IPV4_ROUTE_ADD
, client
, p
, rib
);
196 if (p
->family
== AF_INET6
)
197 zsend_route_multipath (ZEBRA_IPV6_ROUTE_ADD
, client
, p
, rib
);
198 #endif /* HAVE_IPV6 */
204 redistribute_delete (struct prefix
*p
, struct rib
*rib
)
206 struct listnode
*node
, *nnode
;
207 struct zserv
*client
;
209 /* Add DISTANCE_INFINITY check. */
210 if (rib
->distance
== DISTANCE_INFINITY
)
213 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
217 if (client
->redist_default
|| client
->redist
[rib
->type
])
219 if (p
->family
== AF_INET
)
220 zsend_route_multipath (ZEBRA_IPV4_ROUTE_DELETE
, client
, p
,
223 if (p
->family
== AF_INET6
)
224 zsend_route_multipath (ZEBRA_IPV6_ROUTE_DELETE
, client
, p
,
226 #endif /* HAVE_IPV6 */
229 else if (client
->redist
[rib
->type
])
231 if (p
->family
== AF_INET
)
232 zsend_route_multipath (ZEBRA_IPV4_ROUTE_DELETE
, client
, p
, rib
);
234 if (p
->family
== AF_INET6
)
235 zsend_route_multipath (ZEBRA_IPV6_ROUTE_DELETE
, client
, p
, rib
);
236 #endif /* HAVE_IPV6 */
242 zebra_redistribute_add (int command
, struct zserv
*client
, int length
)
246 type
= stream_getc (client
->ibuf
);
248 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
)
251 if (! client
->redist
[type
])
253 client
->redist
[type
] = 1;
254 zebra_redistribute (client
, type
);
259 zebra_redistribute_delete (int command
, struct zserv
*client
, int length
)
263 type
= stream_getc (client
->ibuf
);
265 if (type
== 0 || type
>= ZEBRA_ROUTE_MAX
)
268 client
->redist
[type
] = 0;
272 zebra_redistribute_default_add (int command
, struct zserv
*client
, int length
)
274 client
->redist_default
= 1;
275 zebra_redistribute_default (client
);
279 zebra_redistribute_default_delete (int command
, struct zserv
*client
,
282 client
->redist_default
= 0;;
285 /* Interface up information. */
287 zebra_interface_up_update (struct interface
*ifp
)
289 struct listnode
*node
, *nnode
;
290 struct zserv
*client
;
292 if (IS_ZEBRA_DEBUG_EVENT
)
293 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_UP %s", ifp
->name
);
295 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
296 zsend_interface_update (ZEBRA_INTERFACE_UP
, client
, ifp
);
299 /* Interface down information. */
301 zebra_interface_down_update (struct interface
*ifp
)
303 struct listnode
*node
, *nnode
;
304 struct zserv
*client
;
306 if (IS_ZEBRA_DEBUG_EVENT
)
307 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_DOWN %s", ifp
->name
);
309 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
310 zsend_interface_update (ZEBRA_INTERFACE_DOWN
, client
, ifp
);
313 /* Interface information update. */
315 zebra_interface_add_update (struct interface
*ifp
)
317 struct listnode
*node
, *nnode
;
318 struct zserv
*client
;
320 if (IS_ZEBRA_DEBUG_EVENT
)
321 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADD %s", ifp
->name
);
323 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
325 zsend_interface_add (client
, ifp
);
329 zebra_interface_delete_update (struct interface
*ifp
)
331 struct listnode
*node
, *nnode
;
332 struct zserv
*client
;
334 if (IS_ZEBRA_DEBUG_EVENT
)
335 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_DELETE %s", ifp
->name
);
337 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
339 zsend_interface_delete (client
, ifp
);
342 /* Interface address addition. */
344 zebra_interface_address_add_update (struct interface
*ifp
,
345 struct connected
*ifc
)
347 struct listnode
*node
, *nnode
;
348 struct zserv
*client
;
351 if (IS_ZEBRA_DEBUG_EVENT
)
353 char buf
[INET6_ADDRSTRLEN
];
356 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_ADD %s/%d on %s",
357 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
358 p
->prefixlen
, ifc
->ifp
->name
);
361 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
362 zlog_warn("WARNING: advertising address to clients that is not yet usable.");
364 router_id_add_address(ifc
);
366 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
367 if (client
->ifinfo
&& CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
368 zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD
, client
, ifp
, ifc
);
371 /* Interface address deletion. */
373 zebra_interface_address_delete_update (struct interface
*ifp
,
374 struct connected
*ifc
)
376 struct listnode
*node
, *nnode
;
377 struct zserv
*client
;
380 if (IS_ZEBRA_DEBUG_EVENT
)
382 char buf
[INET6_ADDRSTRLEN
];
385 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_ADDRESS_DELETE %s/%d on %s",
386 inet_ntop (p
->family
, &p
->u
.prefix
, buf
, INET6_ADDRSTRLEN
),
387 p
->prefixlen
, ifc
->ifp
->name
);
390 router_id_del_address(ifc
);
392 for (ALL_LIST_ELEMENTS (zebrad
.client_list
, node
, nnode
, client
))
393 if (client
->ifinfo
&& CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
394 zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_DELETE
, client
, ifp
, ifc
);