]> git.proxmox.com Git - mirror_frr.git/blob - zebra/connected.c
*: make consistent & update GPLv2 file headers
[mirror_frr.git] / zebra / connected.c
1 /*
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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <zebra.h>
23
24 #include "prefix.h"
25 #include "linklist.h"
26 #include "if.h"
27 #include "table.h"
28 #include "rib.h"
29 #include "table.h"
30 #include "log.h"
31 #include "memory.h"
32 #include "zebra_memory.h"
33
34 #include "vty.h"
35 #include "zebra/debug.h"
36 #include "zebra/zserv.h"
37 #include "zebra/redistribute.h"
38 #include "zebra/interface.h"
39 #include "zebra/connected.h"
40 #include "zebra/rtadv.h"
41 #include "zebra/zebra_mpls.h"
42 #include "zebra/debug.h"
43
44 /* communicate the withdrawal of a connected address */
45 static void
46 connected_withdraw (struct connected *ifc)
47 {
48 if (! ifc)
49 return;
50
51 /* Update interface address information to protocol daemon. */
52 if (CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
53 {
54 zebra_interface_address_delete_update (ifc->ifp, ifc);
55
56 if (ifc->address->family == AF_INET)
57 if_subnet_delete (ifc->ifp, ifc);
58
59 if (ifc->address->family == AF_INET)
60 connected_down_ipv4 (ifc->ifp, ifc);
61 else
62 connected_down_ipv6 (ifc->ifp, ifc);
63
64 UNSET_FLAG (ifc->conf, ZEBRA_IFC_REAL);
65 }
66
67 /* The address is not in the kernel anymore, so clear the flag */
68 UNSET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
69
70 if (!CHECK_FLAG (ifc->conf, ZEBRA_IFC_CONFIGURED))
71 {
72 listnode_delete (ifc->ifp->connected, ifc);
73 connected_free (ifc);
74 }
75 }
76
77 static void
78 connected_announce (struct interface *ifp, struct connected *ifc)
79 {
80 if (!ifc)
81 return;
82
83 if (!if_is_loopback(ifp) && ifc->address->family == AF_INET)
84 {
85 if (ifc->address->prefixlen == 32)
86 SET_FLAG (ifc->flags, ZEBRA_IFA_UNNUMBERED);
87 else
88 UNSET_FLAG (ifc->flags, ZEBRA_IFA_UNNUMBERED);
89 }
90
91 listnode_add (ifp->connected, ifc);
92
93 /* Update interface address information to protocol daemon. */
94 if (ifc->address->family == AF_INET)
95 if_subnet_add (ifp, ifc);
96
97 zebra_interface_address_add_update (ifp, ifc);
98
99 if (if_is_operative(ifp))
100 {
101 if (ifc->address->family == AF_INET)
102 connected_up_ipv4 (ifp, ifc);
103 else
104 connected_up_ipv6 (ifp, ifc);
105 }
106 }
107
108 /* If same interface address is already exist... */
109 struct connected *
110 connected_check (struct interface *ifp, struct prefix *p)
111 {
112 struct connected *ifc;
113 struct listnode *node;
114
115 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, ifc))
116 if (prefix_same (ifc->address, p))
117 return ifc;
118
119 return NULL;
120 }
121
122 /* Check if two ifc's describe the same address in the same state */
123 static int
124 connected_same (struct connected *ifc1, struct connected *ifc2)
125 {
126 if (ifc1->ifp != ifc2->ifp)
127 return 0;
128
129 if (ifc1->destination)
130 if (!ifc2->destination)
131 return 0;
132 if (ifc2->destination)
133 if (!ifc1->destination)
134 return 0;
135
136 if (ifc1->destination && ifc2->destination)
137 if (!prefix_same (ifc1->destination, ifc2->destination))
138 return 0;
139
140 if (ifc1->flags != ifc2->flags)
141 return 0;
142
143 if (ifc1->conf != ifc2->conf)
144 return 0;
145
146 return 1;
147 }
148
149 /* Handle changes to addresses and send the neccesary announcements
150 * to clients. */
151 static void
152 connected_update(struct interface *ifp, struct connected *ifc)
153 {
154 struct connected *current;
155
156 /* Check same connected route. */
157 if ((current = connected_check (ifp, (struct prefix *) ifc->address)))
158 {
159 if (CHECK_FLAG(current->conf, ZEBRA_IFC_CONFIGURED))
160 SET_FLAG(ifc->conf, ZEBRA_IFC_CONFIGURED);
161
162 /* Avoid spurious withdraws, this might be just the kernel 'reflecting'
163 * back an address we have already added.
164 */
165 if (connected_same (current, ifc))
166 {
167 /* nothing to do */
168 connected_free (ifc);
169 return;
170 }
171
172 /* Clear the configured flag on the old ifc, so it will be freed by
173 * connected withdraw. */
174 UNSET_FLAG(current->conf, ZEBRA_IFC_CONFIGURED);
175 connected_withdraw (current); /* implicit withdraw - freebsd does this */
176 }
177
178 /* If the connected is new or has changed, announce it, if it is usable */
179 if (CHECK_FLAG(ifc->conf, ZEBRA_IFC_REAL))
180 connected_announce(ifp, ifc);
181 }
182
183 /* Called from if_up(). */
184 void
185 connected_up_ipv4 (struct interface *ifp, struct connected *ifc)
186 {
187 struct prefix p;
188
189 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
190 return;
191
192 PREFIX_COPY_IPV4((struct prefix_ipv4 *)&p, CONNECTED_PREFIX(ifc));
193
194 /* Apply mask to the network. */
195 apply_mask (&p);
196
197 /* In case of connected address is 0.0.0.0/0 we treat it tunnel
198 address. */
199 if (prefix_ipv4_any ((struct prefix_ipv4 *)&p))
200 return;
201
202 rib_add (AFI_IP, SAFI_UNICAST, ifp->vrf_id, ZEBRA_ROUTE_CONNECT,
203 0, 0, &p, NULL, NULL, NULL, ifp->ifindex,
204 RT_TABLE_MAIN, ifp->metric, 0, 0);
205
206 rib_add (AFI_IP, SAFI_MULTICAST, ifp->vrf_id, ZEBRA_ROUTE_CONNECT,
207 0, 0, &p, NULL, NULL, NULL, ifp->ifindex,
208 RT_TABLE_MAIN, ifp->metric, 0, 0);
209
210 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
211 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling RIB processing",
212 ifp->vrf_id, ifp->name);
213 rib_update (ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
214
215 /* Schedule LSP forwarding entries for processing, if appropriate. */
216 if (ifp->vrf_id == VRF_DEFAULT)
217 {
218 if (IS_ZEBRA_DEBUG_MPLS)
219 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
220 ifp->vrf_id, ifp->name);
221 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp->vrf_id));
222 }
223 }
224
225 /* Add connected IPv4 route to the interface. */
226 void
227 connected_add_ipv4 (struct interface *ifp, int flags, struct in_addr *addr,
228 u_char prefixlen, struct in_addr *broad,
229 const char *label)
230 {
231 struct prefix_ipv4 *p;
232 struct connected *ifc;
233
234 if (ipv4_martian(addr))
235 return;
236
237 /* Make connected structure. */
238 ifc = connected_new ();
239 ifc->ifp = ifp;
240 ifc->flags = flags;
241 /* If we get a notification from the kernel,
242 * we can safely assume the address is known to the kernel */
243 SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
244
245 /* Allocate new connected address. */
246 p = prefix_ipv4_new ();
247 p->family = AF_INET;
248 p->prefix = *addr;
249 p->prefixlen = prefixlen;
250 ifc->address = (struct prefix *) p;
251
252 /* If there is broadcast or peer address. */
253 if (broad)
254 {
255 p = prefix_ipv4_new ();
256 p->family = AF_INET;
257 p->prefix = *broad;
258 p->prefixlen = prefixlen;
259 ifc->destination = (struct prefix *) p;
260
261 /* validate the destination address */
262 if (CONNECTED_PEER(ifc))
263 {
264 if (IPV4_ADDR_SAME(addr,broad))
265 zlog_warn("warning: interface %s has same local and peer "
266 "address %s, routing protocols may malfunction",
267 ifp->name,inet_ntoa(*addr));
268 }
269 else
270 {
271 if (broad->s_addr != ipv4_broadcast_addr(addr->s_addr,prefixlen))
272 {
273 char buf[2][INET_ADDRSTRLEN];
274 struct in_addr bcalc;
275 bcalc.s_addr = ipv4_broadcast_addr(addr->s_addr,prefixlen);
276 zlog_warn("warning: interface %s broadcast addr %s/%d != "
277 "calculated %s, routing protocols may malfunction",
278 ifp->name,
279 inet_ntop (AF_INET, broad, buf[0], sizeof(buf[0])),
280 prefixlen,
281 inet_ntop (AF_INET, &bcalc, buf[1], sizeof(buf[1])));
282 }
283 }
284
285 }
286 else
287 {
288 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER))
289 {
290 zlog_warn("warning: %s called for interface %s "
291 "with peer flag set, but no peer address supplied",
292 __func__, ifp->name);
293 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
294 }
295
296 /* no broadcast or destination address was supplied */
297 if ((prefixlen == IPV4_MAX_PREFIXLEN) && if_is_pointopoint(ifp))
298 zlog_warn("warning: PtP interface %s with addr %s/%d needs a "
299 "peer address",ifp->name,inet_ntoa(*addr),prefixlen);
300 }
301
302 /* Label of this address. */
303 if (label)
304 ifc->label = XSTRDUP (MTYPE_CONNECTED_LABEL, label);
305
306 /* For all that I know an IPv4 address is always ready when we receive
307 * the notification. So it should be safe to set the REAL flag here. */
308 SET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
309
310 connected_update(ifp, ifc);
311 }
312
313 void
314 connected_down_ipv4 (struct interface *ifp, struct connected *ifc)
315 {
316 struct prefix p;
317
318 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
319 return;
320
321 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
322
323 /* Apply mask to the network. */
324 apply_mask (&p);
325
326 /* In case of connected address is 0.0.0.0/0 we treat it tunnel
327 address. */
328 if (prefix_ipv4_any ((struct prefix_ipv4 *)&p))
329 return;
330
331 /* Same logic as for connected_up_ipv4(): push the changes into the head. */
332 rib_delete (AFI_IP, SAFI_UNICAST, ifp->vrf_id, ZEBRA_ROUTE_CONNECT,
333 0, 0, &p, NULL, NULL, ifp->ifindex, 0);
334
335 rib_delete (AFI_IP, SAFI_MULTICAST, ifp->vrf_id, ZEBRA_ROUTE_CONNECT,
336 0, 0, &p, NULL, NULL, ifp->ifindex, 0);
337
338 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
339 zlog_debug ("%u: IF %s IPv4 address down, scheduling RIB processing",
340 ifp->vrf_id, ifp->name);
341
342 rib_update (ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
343
344 /* Schedule LSP forwarding entries for processing, if appropriate. */
345 if (ifp->vrf_id == VRF_DEFAULT)
346 {
347 if (IS_ZEBRA_DEBUG_MPLS)
348 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
349 ifp->vrf_id, ifp->name);
350 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp->vrf_id));
351 }
352 }
353
354 /* Delete connected IPv4 route to the interface. */
355 void
356 connected_delete_ipv4 (struct interface *ifp, int flags, struct in_addr *addr,
357 u_char prefixlen, struct in_addr *broad)
358 {
359 struct prefix_ipv4 p;
360 struct connected *ifc;
361
362 memset (&p, 0, sizeof (struct prefix_ipv4));
363 p.family = AF_INET;
364 p.prefix = *addr;
365 p.prefixlen = prefixlen;
366
367 ifc = connected_check (ifp, (struct prefix *) &p);
368 if (! ifc)
369 return;
370
371 connected_withdraw (ifc);
372
373 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
374 zlog_debug ("%u: IF %s IPv4 address del, scheduling RIB processing",
375 ifp->vrf_id, ifp->name);
376
377 rib_update (ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
378
379 /* Schedule LSP forwarding entries for processing, if appropriate. */
380 if (ifp->vrf_id == VRF_DEFAULT)
381 {
382 if (IS_ZEBRA_DEBUG_MPLS)
383 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
384 ifp->vrf_id, ifp->name);
385 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp->vrf_id));
386 }
387 }
388
389 void
390 connected_up_ipv6 (struct interface *ifp, struct connected *ifc)
391 {
392 struct prefix p;
393
394 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
395 return;
396
397 PREFIX_COPY_IPV6((struct prefix_ipv6 *)&p, CONNECTED_PREFIX(ifc));
398
399 /* Apply mask to the network. */
400 apply_mask (&p);
401
402 #ifndef LINUX
403 /* XXX: It is already done by rib_bogus_ipv6 within rib_add */
404 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
405 return;
406 #endif
407
408 rib_add (AFI_IP6, SAFI_UNICAST, ifp->vrf_id, ZEBRA_ROUTE_CONNECT,
409 0, 0, &p, NULL, NULL, NULL, ifp->ifindex,
410 RT_TABLE_MAIN, ifp->metric, 0, 0);
411
412 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
413 zlog_debug ("%u: IF %s IPv6 address down, scheduling RIB processing",
414 ifp->vrf_id, ifp->name);
415
416 rib_update (ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
417
418 /* Schedule LSP forwarding entries for processing, if appropriate. */
419 if (ifp->vrf_id == VRF_DEFAULT)
420 {
421 if (IS_ZEBRA_DEBUG_MPLS)
422 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
423 ifp->vrf_id, ifp->name);
424 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp->vrf_id));
425 }
426 }
427
428 /* Add connected IPv6 route to the interface. */
429 void
430 connected_add_ipv6 (struct interface *ifp, int flags, struct in6_addr *addr,
431 u_char prefixlen, struct in6_addr *broad,
432 const char *label)
433 {
434 struct prefix_ipv6 *p;
435 struct connected *ifc;
436
437 if (ipv6_martian(addr))
438 return;
439
440 /* Make connected structure. */
441 ifc = connected_new ();
442 ifc->ifp = ifp;
443 ifc->flags = flags;
444 /* If we get a notification from the kernel,
445 * we can safely assume the address is known to the kernel */
446 SET_FLAG(ifc->conf, ZEBRA_IFC_QUEUED);
447
448 /* Allocate new connected address. */
449 p = prefix_ipv6_new ();
450 p->family = AF_INET6;
451 IPV6_ADDR_COPY (&p->prefix, addr);
452 p->prefixlen = prefixlen;
453 ifc->address = (struct prefix *) p;
454
455 /* If there is broadcast or peer address. */
456 if (broad)
457 {
458 if (IN6_IS_ADDR_UNSPECIFIED(broad))
459 zlog_warn("warning: %s called for interface %s with unspecified "
460 "destination address; ignoring!", __func__, ifp->name);
461 else
462 {
463 p = prefix_ipv6_new ();
464 p->family = AF_INET6;
465 IPV6_ADDR_COPY (&p->prefix, broad);
466 p->prefixlen = prefixlen;
467 ifc->destination = (struct prefix *) p;
468 }
469 }
470 if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER) && !ifc->destination)
471 {
472 zlog_warn("warning: %s called for interface %s "
473 "with peer flag set, but no peer address supplied",
474 __func__, ifp->name);
475 UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER);
476 }
477
478 /* Label of this address. */
479 if (label)
480 ifc->label = XSTRDUP (MTYPE_CONNECTED_LABEL, label);
481
482 /* On Linux, we only get here when DAD is complete, therefore we can set
483 * ZEBRA_IFC_REAL.
484 *
485 * On BSD, there currently doesn't seem to be a way to check for completion of
486 * DAD, so we replicate the old behaviour and set ZEBRA_IFC_REAL, although DAD
487 * might still be running.
488 */
489 SET_FLAG(ifc->conf, ZEBRA_IFC_REAL);
490 connected_update(ifp, ifc);
491 }
492
493 void
494 connected_down_ipv6 (struct interface *ifp, struct connected *ifc)
495 {
496 struct prefix p;
497
498 if (! CHECK_FLAG (ifc->conf, ZEBRA_IFC_REAL))
499 return;
500
501 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
502
503 apply_mask (&p);
504
505 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
506 return;
507
508 rib_delete (AFI_IP6, SAFI_UNICAST, ifp->vrf_id, ZEBRA_ROUTE_CONNECT,
509 0, 0, &p, NULL, NULL, ifp->ifindex, 0);
510
511 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
512 zlog_debug ("%u: IF %s IPv6 address down, scheduling RIB processing",
513 ifp->vrf_id, ifp->name);
514
515 rib_update (ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
516
517 /* Schedule LSP forwarding entries for processing, if appropriate. */
518 if (ifp->vrf_id == VRF_DEFAULT)
519 {
520 if (IS_ZEBRA_DEBUG_MPLS)
521 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
522 ifp->vrf_id, ifp->name);
523 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp->vrf_id));
524 }
525 }
526
527 void
528 connected_delete_ipv6 (struct interface *ifp, struct in6_addr *address,
529 u_char prefixlen, struct in6_addr *broad)
530 {
531 struct prefix_ipv6 p;
532 struct connected *ifc;
533
534 memset (&p, 0, sizeof (struct prefix_ipv6));
535 p.family = AF_INET6;
536 memcpy (&p.prefix, address, sizeof (struct in6_addr));
537 p.prefixlen = prefixlen;
538
539 ifc = connected_check (ifp, (struct prefix *) &p);
540 if (! ifc)
541 return;
542
543 connected_withdraw (ifc);
544
545 if (IS_ZEBRA_DEBUG_RIB_DETAILED)
546 zlog_debug ("%u: IF %s IPv6 address del, scheduling RIB processing",
547 ifp->vrf_id, ifp->name);
548
549 rib_update (ifp->vrf_id, RIB_UPDATE_IF_CHANGE);
550
551 /* Schedule LSP forwarding entries for processing, if appropriate. */
552 if (ifp->vrf_id == VRF_DEFAULT)
553 {
554 if (IS_ZEBRA_DEBUG_MPLS)
555 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
556 ifp->vrf_id, ifp->name);
557 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp->vrf_id));
558 }
559 }
560
561 int
562 connected_is_unnumbered (struct interface *ifp)
563 {
564 struct connected *connected;
565 struct listnode *node;
566
567 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node, connected))
568 {
569 if (CHECK_FLAG (connected->conf, ZEBRA_IFC_REAL) &&
570 connected->address->family == AF_INET)
571 return CHECK_FLAG(connected->flags, ZEBRA_IFA_UNNUMBERED);
572 }
573 return 0;
574 }