blob: 53aa25432fa4eb76c865f8231ff4c0e46fa54e27 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Address linked list routine.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "prefix.h"
26#include "linklist.h"
27#include "if.h"
28#include "table.h"
29#include "rib.h"
30#include "table.h"
31#include "log.h"
paul0752ef02005-11-03 12:35:21 +000032#include "memory.h"
paul718e3742002-12-13 20:15:29 +000033
34#include "zebra/zserv.h"
35#include "zebra/redistribute.h"
hassoeef1fe12004-10-03 18:46:08 +000036#include "zebra/interface.h"
paula1ac18c2005-06-28 17:17:12 +000037#include "zebra/connected.h"
paul718e3742002-12-13 20:15:29 +000038
paulca162182005-09-12 16:58:52 +000039/* withdraw a connected address */
40static void
41connected_withdraw (struct connected *ifc)
42{
43 if (! ifc)
44 return;
45
46 /* Update interface address information to protocol daemon. */
47 if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
48 {
49 zebra_interface_address_delete_update (ifc->ifp, ifc);
50
51 if_subnet_delete (ifc->ifp, ifc);
52
53 if (ifc->address->family == AF_INET)
54 connected_down_ipv4 (ifc->ifp, ifc);
vincentaa2e32b2005-09-28 13:42:11 +000055#ifdef HAVE_IPV6
paulca162182005-09-12 16:58:52 +000056 else
57 connected_down_ipv6 (ifc->ifp, ifc);
vincentaa2e32b2005-09-28 13:42:11 +000058#endif
paulca162182005-09-12 16:58:52 +000059
60 UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
61 }
62
Andrew J. Schorr9c378512006-05-21 04:04:49 +000063 if (!CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
64 {
65 listnode_delete (ifc->ifp->connected, ifc);
66 connected_free (ifc);
67 }
paulca162182005-09-12 16:58:52 +000068}
69
70static void
71connected_announce (struct interface *ifp, struct connected *ifc)
72{
73 if (!ifc)
74 return;
75
76 listnode_add (ifp->connected, ifc);
77
78 /* Update interface address information to protocol daemon. */
79 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
80 {
81 if (ifc->address->family == AF_INET)
82 if_subnet_add (ifp, ifc);
83
84 SET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
85
86 zebra_interface_address_add_update (ifp, ifc);
87
88 if (if_is_up(ifp))
89 {
90 if (ifc->address->family == AF_INET)
91 connected_up_ipv4 (ifp, ifc);
vincentaa2e32b2005-09-28 13:42:11 +000092#ifdef HAVE_IPV6
paulca162182005-09-12 16:58:52 +000093 else
94 connected_up_ipv6 (ifp, ifc);
vincentaa2e32b2005-09-28 13:42:11 +000095#endif
paulca162182005-09-12 16:58:52 +000096 }
97 }
98}
99
paul718e3742002-12-13 20:15:29 +0000100/* If same interface address is already exist... */
101struct connected *
paulca162182005-09-12 16:58:52 +0000102connected_check (struct interface *ifp, struct prefix *p)
paul718e3742002-12-13 20:15:29 +0000103{
104 struct connected *ifc;
hasso52dc7ee2004-09-23 19:18:23 +0000105 struct listnode *node;
paul718e3742002-12-13 20:15:29 +0000106
paul1eb8ef22005-04-07 07:30:20 +0000107 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
108 if (prefix_same (ifc->address, p))
109 return ifc;
paul718e3742002-12-13 20:15:29 +0000110
paul718e3742002-12-13 20:15:29 +0000111 return NULL;
112}
113
Paul Jakma74ecdc92006-06-15 18:10:47 +0000114/* Check if two ifc's describe the same address */
115static int
116connected_same (struct connected *ifc1, struct connected *ifc2)
117{
118 if (ifc1->ifp != ifc2->ifp)
119 return 0;
120
121 if (ifc1->destination)
122 if (!ifc2->destination)
123 return 0;
124 if (ifc2->destination)
125 if (!ifc1->destination)
126 return 0;
127
128 if (ifc1->destination && ifc2->destination)
129 if (!prefix_same (ifc1->destination, ifc2->destination))
130 return 0;
131
132 if (ifc1->flags != ifc2->flags)
133 return 0;
134
135 return 1;
136}
137
138/* Handle implicit withdrawals of addresses, where a system ADDs an address
139 * to an interface which already has the same address configured.
140 *
141 * Returns the struct connected which must be announced to clients,
142 * or NULL if nothing to do.
143 */
144static struct connected *
145connected_implicit_withdraw (struct interface *ifp, struct connected *ifc)
146{
147 struct connected *current;
148
149 /* Check same connected route. */
150 if ((current = connected_check (ifp, (struct prefix *) ifc->address)))
151 {
152 if (CHECK_FLAG(current->conf, ZEBRA_IFC_CONFIGURED))
153 SET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);
154
155 /* Avoid spurious withdraws, this might be just the kernel 'reflecting'
156 * back an address we have already added.
157 */
David Young33b931e2007-04-16 05:54:02 +0000158 if (connected_same (current, ifc) && CHECK_FLAG(current->conf, ZEBRA_IFC_REAL))
Paul Jakma74ecdc92006-06-15 18:10:47 +0000159 {
160 /* nothing to do */
161 connected_free (ifc);
162 return NULL;
163 }
164
165 UNSET_FLAG(current->conf, ZEBRA_IFC_CONFIGURED);
166 connected_withdraw (current); /* implicit withdraw - freebsd does this */
167 }
168 return ifc;
169}
170
paul718e3742002-12-13 20:15:29 +0000171/* Called from if_up(). */
172void
173connected_up_ipv4 (struct interface *ifp, struct connected *ifc)
174{
175 struct prefix_ipv4 p;
paul718e3742002-12-13 20:15:29 +0000176
177 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
178 return;
179
Andrew J. Schorre4529632006-12-12 19:18:21 +0000180 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000181
182 /* Apply mask to the network. */
183 apply_mask_ipv4 (&p);
184
185 /* In case of connected address is 0.0.0.0/0 we treat it tunnel
186 address. */
187 if (prefix_ipv4_any (&p))
188 return;
189
Paul Jakma7514fb72007-05-02 16:05:35 +0000190 rib_add_ipv4 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, NULL, ifp->ifindex,
191 RT_TABLE_MAIN, ifp->metric, 0);
paul718e3742002-12-13 20:15:29 +0000192
193 rib_update ();
194}
195
196/* Add connected IPv4 route to the interface. */
197void
198connected_add_ipv4 (struct interface *ifp, int flags, struct in_addr *addr,
pauld06b2a62005-10-11 03:53:54 +0000199 u_char prefixlen, struct in_addr *broad,
200 const char *label)
paul718e3742002-12-13 20:15:29 +0000201{
202 struct prefix_ipv4 *p;
203 struct connected *ifc;
paul718e3742002-12-13 20:15:29 +0000204
205 /* Make connected structure. */
206 ifc = connected_new ();
207 ifc->ifp = ifp;
208 ifc->flags = flags;
209
210 /* Allocate new connected address. */
211 p = prefix_ipv4_new ();
212 p->family = AF_INET;
213 p->prefix = *addr;
214 p->prefixlen = prefixlen;
215 ifc->address = (struct prefix *) p;
paulca162182005-09-12 16:58:52 +0000216
Andrew J. Schorre4529632006-12-12 19:18:21 +0000217 /* If there is broadcast or peer address. */
paul718e3742002-12-13 20:15:29 +0000218 if (broad)
219 {
220 p = prefix_ipv4_new ();
221 p->family = AF_INET;
222 p->prefix = *broad;
Andrew J. Schorre4529632006-12-12 19:18:21 +0000223 p->prefixlen = prefixlen;
paul718e3742002-12-13 20:15:29 +0000224 ifc->destination = (struct prefix *) p;
hasso3fb9cd62004-10-19 19:44:43 +0000225
226 /* validate the destination address */
Andrew J. Schorre4529632006-12-12 19:18:21 +0000227 if (CONNECTED_PEER(ifc))
hasso3fb9cd62004-10-19 19:44:43 +0000228 {
229 if (IPV4_ADDR_SAME(addr,broad))
Andrew J. Schorre4529632006-12-12 19:18:21 +0000230 zlog_warn("warning: interface %s has same local and peer "
hasso3fb9cd62004-10-19 19:44:43 +0000231 "address %s, routing protocols may malfunction",
232 ifp->name,inet_ntoa(*addr));
hasso3fb9cd62004-10-19 19:44:43 +0000233 }
234 else
235 {
236 if (broad->s_addr != ipv4_broadcast_addr(addr->s_addr,prefixlen))
237 {
238 char buf[2][INET_ADDRSTRLEN];
239 struct in_addr bcalc;
240 bcalc.s_addr = ipv4_broadcast_addr(addr->s_addr,prefixlen);
241 zlog_warn("warning: interface %s broadcast addr %s/%d != "
242 "calculated %s, routing protocols may malfunction",
243 ifp->name,
244 inet_ntop (AF_INET, broad, buf[0], sizeof(buf[0])),
245 prefixlen,
246 inet_ntop (AF_INET, &bcalc, buf[1], sizeof(buf[1])));
247 }
248 }
249
paul718e3742002-12-13 20:15:29 +0000250 }
hasso3fb9cd62004-10-19 19:44:43 +0000251 else
Andrew J. Schorre4529632006-12-12 19:18:21 +0000252 {
253 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER))
254 {
255 zlog_warn("warning: %s called for interface %s "
256 "with peer flag set, but no peer address supplied",
257 __func__, ifp->name);
258 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
259 }
260
261 /* no broadcast or destination address was supplied */
262 if ((prefixlen == IPV4_MAX_PREFIXLEN) && if_is_pointopoint(ifp))
263 zlog_warn("warning: PtP interface %s with addr %s/%d needs a "
264 "peer address",ifp->name,inet_ntoa(*addr),prefixlen);
265 }
paul718e3742002-12-13 20:15:29 +0000266
267 /* Label of this address. */
268 if (label)
paul0752ef02005-11-03 12:35:21 +0000269 ifc->label = XSTRDUP (MTYPE_CONNECTED_LABEL, label);
paul718e3742002-12-13 20:15:29 +0000270
Paul Jakma74ecdc92006-06-15 18:10:47 +0000271 /* nothing to do? */
272 if ((ifc = connected_implicit_withdraw (ifp, ifc)) == NULL)
273 return;
paulca162182005-09-12 16:58:52 +0000274
275 connected_announce (ifp, ifc);
paul718e3742002-12-13 20:15:29 +0000276}
277
278void
279connected_down_ipv4 (struct interface *ifp, struct connected *ifc)
280{
281 struct prefix_ipv4 p;
paul718e3742002-12-13 20:15:29 +0000282
283 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
284 return;
285
Andrew J. Schorre4529632006-12-12 19:18:21 +0000286 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000287
288 /* Apply mask to the network. */
289 apply_mask_ipv4 (&p);
290
291 /* In case of connected address is 0.0.0.0/0 we treat it tunnel
292 address. */
293 if (prefix_ipv4_any (&p))
294 return;
295
296 rib_delete_ipv4 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0);
297
298 rib_update ();
299}
300
301/* Delete connected IPv4 route to the interface. */
302void
303connected_delete_ipv4 (struct interface *ifp, int flags, struct in_addr *addr,
paul0752ef02005-11-03 12:35:21 +0000304 u_char prefixlen, struct in_addr *broad)
paul718e3742002-12-13 20:15:29 +0000305{
306 struct prefix_ipv4 p;
307 struct connected *ifc;
308
309 memset (&p, 0, sizeof (struct prefix_ipv4));
310 p.family = AF_INET;
311 p.prefix = *addr;
312 p.prefixlen = prefixlen;
313
paulca162182005-09-12 16:58:52 +0000314 ifc = connected_check (ifp, (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000315 if (! ifc)
316 return;
paulca162182005-09-12 16:58:52 +0000317
318 connected_withdraw (ifc);
paul718e3742002-12-13 20:15:29 +0000319}
320
321#ifdef HAVE_IPV6
paul718e3742002-12-13 20:15:29 +0000322void
323connected_up_ipv6 (struct interface *ifp, struct connected *ifc)
324{
325 struct prefix_ipv6 p;
paul718e3742002-12-13 20:15:29 +0000326
327 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
328 return;
329
Andrew J. Schorre4529632006-12-12 19:18:21 +0000330 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000331
332 /* Apply mask to the network. */
333 apply_mask_ipv6 (&p);
334
hasso726f9b22003-05-25 21:04:54 +0000335#if ! defined (MUSICA) && ! defined (LINUX)
336 /* XXX: It is already done by rib_bogus_ipv6 within rib_add_ipv6 */
paul718e3742002-12-13 20:15:29 +0000337 if (IN6_IS_ADDR_UNSPECIFIED (&p.prefix))
338 return;
hasso726f9b22003-05-25 21:04:54 +0000339#endif
paul718e3742002-12-13 20:15:29 +0000340
paulc7133002006-01-17 17:56:18 +0000341 rib_add_ipv6 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0,
342 ifp->metric, 0);
paul718e3742002-12-13 20:15:29 +0000343
344 rib_update ();
345}
346
347/* Add connected IPv6 route to the interface. */
348void
Andrew J. Schorre4529632006-12-12 19:18:21 +0000349connected_add_ipv6 (struct interface *ifp, int flags, struct in6_addr *addr,
paul89368d92005-11-26 09:14:07 +0000350 u_char prefixlen, struct in6_addr *broad,
351 const char *label)
paul718e3742002-12-13 20:15:29 +0000352{
353 struct prefix_ipv6 *p;
354 struct connected *ifc;
paul718e3742002-12-13 20:15:29 +0000355
356 /* Make connected structure. */
357 ifc = connected_new ();
358 ifc->ifp = ifp;
Andrew J. Schorre4529632006-12-12 19:18:21 +0000359 ifc->flags = flags;
paul718e3742002-12-13 20:15:29 +0000360
361 /* Allocate new connected address. */
362 p = prefix_ipv6_new ();
363 p->family = AF_INET6;
364 IPV6_ADDR_COPY (&p->prefix, addr);
365 p->prefixlen = prefixlen;
366 ifc->address = (struct prefix *) p;
367
Andrew J. Schorre4529632006-12-12 19:18:21 +0000368 /* If there is broadcast or peer address. */
paul718e3742002-12-13 20:15:29 +0000369 if (broad)
370 {
Andrew J. Schorre4529632006-12-12 19:18:21 +0000371 if (IN6_IS_ADDR_UNSPECIFIED(broad))
372 zlog_warn("warning: %s called for interface %s with unspecified "
373 "destination address; ignoring!", __func__, ifp->name);
374 else
375 {
376 p = prefix_ipv6_new ();
377 p->family = AF_INET6;
378 IPV6_ADDR_COPY (&p->prefix, broad);
379 p->prefixlen = prefixlen;
380 ifc->destination = (struct prefix *) p;
381 }
382 }
383 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER) && !ifc->destination)
384 {
385 zlog_warn("warning: %s called for interface %s "
386 "with peer flag set, but no peer address supplied",
387 __func__, ifp->name);
388 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
paul718e3742002-12-13 20:15:29 +0000389 }
390
paul0752ef02005-11-03 12:35:21 +0000391 /* Label of this address. */
392 if (label)
393 ifc->label = XSTRDUP (MTYPE_CONNECTED_LABEL, label);
394
Paul Jakma74ecdc92006-06-15 18:10:47 +0000395 if ((ifc = connected_implicit_withdraw (ifp, ifc)) == NULL)
396 return;
paulca162182005-09-12 16:58:52 +0000397
398 connected_announce (ifp, ifc);
paul718e3742002-12-13 20:15:29 +0000399}
400
401void
402connected_down_ipv6 (struct interface *ifp, struct connected *ifc)
403{
404 struct prefix_ipv6 p;
paul718e3742002-12-13 20:15:29 +0000405
406 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
407 return;
408
Andrew J. Schorre4529632006-12-12 19:18:21 +0000409 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
paul718e3742002-12-13 20:15:29 +0000410
411 apply_mask_ipv6 (&p);
412
413 if (IN6_IS_ADDR_UNSPECIFIED (&p.prefix))
414 return;
415
416 rib_delete_ipv6 (ZEBRA_ROUTE_CONNECT, 0, &p, NULL, ifp->ifindex, 0);
417
418 rib_update ();
419}
420
421void
422connected_delete_ipv6 (struct interface *ifp, struct in6_addr *address,
hassofce954f2004-10-07 20:29:24 +0000423 u_char prefixlen, struct in6_addr *broad)
paul718e3742002-12-13 20:15:29 +0000424{
425 struct prefix_ipv6 p;
426 struct connected *ifc;
427
428 memset (&p, 0, sizeof (struct prefix_ipv6));
429 p.family = AF_INET6;
430 memcpy (&p.prefix, address, sizeof (struct in6_addr));
431 p.prefixlen = prefixlen;
432
paulca162182005-09-12 16:58:52 +0000433 ifc = connected_check (ifp, (struct prefix *) &p);
paul718e3742002-12-13 20:15:29 +0000434 if (! ifc)
435 return;
436
paulca162182005-09-12 16:58:52 +0000437 connected_withdraw (ifc);
paul718e3742002-12-13 20:15:29 +0000438}
439#endif /* HAVE_IPV6 */