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