2 * Address linked list routine.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
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
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.
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
34 #include "zebra/debug.h"
35 #include "zebra/zserv.h"
36 #include "zebra/redistribute.h"
37 #include "zebra/interface.h"
38 #include "zebra/connected.h"
39 #include "zebra/rtadv.h"
40 #include "zebra/zebra_mpls.h"
41 #include "zebra/zebra_errors.h"
42 #include "zebra/zebra_router.h"
44 /* communicate the withdrawal of a connected address */
45 static void connected_withdraw(struct connected
*ifc
)
50 /* Update interface address information to protocol daemon. */
51 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
)) {
52 zebra_interface_address_delete_update(ifc
->ifp
, ifc
);
54 if (ifc
->address
->family
== AF_INET
)
55 if_subnet_delete(ifc
->ifp
, ifc
);
57 connected_down(ifc
->ifp
, ifc
);
59 UNSET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
62 /* The address is not in the kernel anymore, so clear the flag */
63 UNSET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
65 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_CONFIGURED
)) {
66 listnode_delete(ifc
->ifp
->connected
, ifc
);
71 static void connected_announce(struct interface
*ifp
, struct connected
*ifc
)
76 if (!if_is_loopback(ifp
) && ifc
->address
->family
== AF_INET
) {
77 if (ifc
->address
->prefixlen
== IPV4_MAX_BITLEN
)
78 SET_FLAG(ifc
->flags
, ZEBRA_IFA_UNNUMBERED
);
80 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_UNNUMBERED
);
83 listnode_add(ifp
->connected
, ifc
);
85 /* Update interface address information to protocol daemon. */
86 if (ifc
->address
->family
== AF_INET
)
87 if_subnet_add(ifp
, ifc
);
89 zebra_interface_address_add_update(ifp
, ifc
);
91 if (if_is_operative(ifp
)) {
92 connected_up(ifp
, ifc
);
96 /* If same interface address is already exist... */
97 struct connected
*connected_check(struct interface
*ifp
,
98 union prefixconstptr pu
)
100 const struct prefix
*p
= pu
.p
;
101 struct connected
*ifc
;
102 struct listnode
*node
;
104 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
))
105 if (prefix_same(ifc
->address
, p
))
111 /* same, but with peer address */
112 struct connected
*connected_check_ptp(struct interface
*ifp
,
113 union prefixconstptr pu
,
114 union prefixconstptr du
)
116 const struct prefix
*p
= pu
.p
;
117 const struct prefix
*d
= du
.p
;
118 struct connected
*ifc
;
119 struct listnode
*node
;
121 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
122 if (!prefix_same(ifc
->address
, p
))
124 if (!CONNECTED_PEER(ifc
) && !d
)
126 if (CONNECTED_PEER(ifc
) && d
127 && prefix_same(ifc
->destination
, d
))
134 /* Check if two ifc's describe the same address in the same state */
135 static int connected_same(struct connected
*ifc1
, struct connected
*ifc2
)
137 if (ifc1
->ifp
!= ifc2
->ifp
)
140 if (ifc1
->flags
!= ifc2
->flags
)
143 if (ifc1
->conf
!= ifc2
->conf
)
146 if (ifc1
->destination
)
147 if (!ifc2
->destination
)
149 if (ifc2
->destination
)
150 if (!ifc1
->destination
)
153 if (ifc1
->destination
&& ifc2
->destination
)
154 if (!prefix_same(ifc1
->destination
, ifc2
->destination
))
160 /* Handle changes to addresses and send the neccesary announcements
162 static void connected_update(struct interface
*ifp
, struct connected
*ifc
)
164 struct connected
*current
;
166 /* Check same connected route. */
167 current
= connected_check_ptp(ifp
, ifc
->address
, ifc
->destination
);
169 if (CHECK_FLAG(current
->conf
, ZEBRA_IFC_CONFIGURED
))
170 SET_FLAG(ifc
->conf
, ZEBRA_IFC_CONFIGURED
);
172 /* Avoid spurious withdraws, this might be just the kernel
174 * back an address we have already added.
176 if (connected_same(current
, ifc
)) {
178 connected_free(&ifc
);
182 /* Clear the configured flag on the old ifc, so it will be freed
184 * connected withdraw. */
185 UNSET_FLAG(current
->conf
, ZEBRA_IFC_CONFIGURED
);
187 current
); /* implicit withdraw - freebsd does this */
190 /* If the connected is new or has changed, announce it, if it is usable
192 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
193 connected_announce(ifp
, ifc
);
196 /* Called from if_up(). */
197 void connected_up(struct interface
*ifp
, struct connected
*ifc
)
201 struct nexthop nh
= {
202 .type
= NEXTHOP_TYPE_IFINDEX
,
203 .ifindex
= ifp
->ifindex
,
204 .vrf_id
= ifp
->vrf
->vrf_id
,
206 struct zebra_vrf
*zvrf
;
210 struct listnode
*cnode
;
213 zvrf
= ifp
->vrf
->info
;
216 EC_ZEBRA_VRF_NOT_FOUND
,
217 "%s: Received Up for interface but no associated zvrf: %s(%d)",
218 __func__
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
);
221 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
224 /* Ensure 'down' flag is cleared */
225 UNSET_FLAG(ifc
->conf
, ZEBRA_IFC_DOWN
);
227 prefix_copy(&p
, CONNECTED_PREFIX(ifc
));
229 /* Apply mask to the network. */
232 afi
= family2afi(p
.family
);
237 * In case of connected address is 0.0.0.0/0 we treat it tunnel
240 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
245 /* XXX: It is already done by rib_bogus_ipv6 within rib_add */
246 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
251 flog_warn(EC_ZEBRA_CONNECTED_AFI_UNKNOWN
,
252 "Received unknown AFI: %s", afi2str(afi
));
257 metric
= (ifc
->metric
< (uint32_t)METRIC_MAX
) ?
258 ifc
->metric
: ifp
->metric
;
261 * Since we are hand creating the connected routes
262 * in our main routing table, *if* we are working
263 * in an offloaded environment then we need to
264 * pretend like the route is offloaded so everything
267 if (zrouter
.asic_offloaded
)
268 flags
|= ZEBRA_FLAG_OFFLOADED
;
271 * It's possible to add the same network and mask
272 * to an interface over and over. This would
273 * result in an equivalent number of connected
274 * routes. Just add one connected route in
275 * for all the addresses on an interface that
276 * resolve to the same network and mask
278 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
281 prefix_copy(&cp
, CONNECTED_PREFIX(c
));
284 if (prefix_same(&cp
, &p
) &&
285 !CHECK_FLAG(c
->conf
, ZEBRA_IFC_DOWN
))
292 rib_add(afi
, SAFI_UNICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0,
293 flags
, &p
, NULL
, &nh
, 0, zvrf
->table_id
, metric
, 0, 0, 0,
296 rib_add(afi
, SAFI_MULTICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0,
297 flags
, &p
, NULL
, &nh
, 0, zvrf
->table_id
, metric
, 0, 0, 0,
300 /* Schedule LSP forwarding entries for processing, if appropriate. */
301 if (zvrf
->vrf
->vrf_id
== VRF_DEFAULT
) {
302 if (IS_ZEBRA_DEBUG_MPLS
)
304 "%u: IF %s IP %pFX address add/up, scheduling MPLS processing",
305 zvrf
->vrf
->vrf_id
, ifp
->name
, &p
);
306 mpls_mark_lsps_for_processing(zvrf
, &p
);
310 /* Add connected IPv4 route to the interface. */
311 void connected_add_ipv4(struct interface
*ifp
, int flags
,
312 const struct in_addr
*addr
, uint16_t prefixlen
,
313 const struct in_addr
*dest
, const char *label
,
316 struct prefix_ipv4
*p
;
317 struct connected
*ifc
;
319 if (ipv4_martian(addr
))
322 /* Make connected structure. */
323 ifc
= connected_new();
326 ifc
->metric
= metric
;
327 /* If we get a notification from the kernel,
328 * we can safely assume the address is known to the kernel */
329 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
330 if (!if_is_operative(ifp
))
331 SET_FLAG(ifc
->conf
, ZEBRA_IFC_DOWN
);
333 /* Allocate new connected address. */
334 p
= prefix_ipv4_new();
338 CHECK_FLAG(flags
, ZEBRA_IFA_PEER
) ? IPV4_MAX_BITLEN
: prefixlen
;
339 ifc
->address
= (struct prefix
*)p
;
341 /* If there is a peer address. */
342 if (CONNECTED_PEER(ifc
)) {
343 /* validate the destination address */
345 p
= prefix_ipv4_new();
348 p
->prefixlen
= prefixlen
;
349 ifc
->destination
= (struct prefix
*)p
;
351 if (IPV4_ADDR_SAME(addr
, dest
))
353 EC_ZEBRA_IFACE_SAME_LOCAL_AS_PEER
,
354 "interface %s has same local and peer address %pI4, routing protocols may malfunction",
358 "%s called for interface %s with peer flag set, but no peer address supplied",
359 __func__
, ifp
->name
);
360 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
364 /* no destination address was supplied */
365 if (!dest
&& (prefixlen
== IPV4_MAX_BITLEN
) && if_is_pointopoint(ifp
))
367 "PtP interface %s with addr %pI4/%d needs a peer address",
368 ifp
->name
, addr
, prefixlen
);
370 /* Label of this address. */
372 ifc
->label
= XSTRDUP(MTYPE_CONNECTED_LABEL
, label
);
374 /* For all that I know an IPv4 address is always ready when we receive
375 * the notification. So it should be safe to set the REAL flag here. */
376 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
378 connected_update(ifp
, ifc
);
381 void connected_down(struct interface
*ifp
, struct connected
*ifc
)
385 struct nexthop nh
= {
386 .type
= NEXTHOP_TYPE_IFINDEX
,
387 .ifindex
= ifp
->ifindex
,
388 .vrf_id
= ifp
->vrf
->vrf_id
,
390 struct zebra_vrf
*zvrf
;
392 struct listnode
*cnode
;
395 zvrf
= ifp
->vrf
->info
;
398 EC_ZEBRA_VRF_NOT_FOUND
,
399 "%s: Received Down for interface but no associated zvrf: %s(%d)",
400 __func__
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
);
404 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
407 /* Skip if we've already done this; this can happen if we have a
408 * config change that takes an interface down, then we receive kernel
409 * notifications about the downed interface and its addresses.
411 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_DOWN
)) {
412 if (IS_ZEBRA_DEBUG_RIB
)
413 zlog_debug("%s: ifc %p, %pFX already DOWN",
414 __func__
, ifc
, ifc
->address
);
418 prefix_copy(&p
, CONNECTED_PREFIX(ifc
));
420 /* Apply mask to the network. */
423 afi
= family2afi(p
.family
);
428 * In case of connected address is 0.0.0.0/0 we treat it tunnel
431 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
435 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
439 zlog_warn("Unknown AFI: %s", afi2str(afi
));
443 /* Mark the address as 'down' */
444 SET_FLAG(ifc
->conf
, ZEBRA_IFC_DOWN
);
447 * It's possible to have X number of addresses
448 * on a interface that all resolve to the same
449 * network and mask. Find them and just
450 * allow the deletion when are removing the last
453 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
456 prefix_copy(&cp
, CONNECTED_PREFIX(c
));
459 if (prefix_same(&p
, &cp
) &&
460 !CHECK_FLAG(c
->conf
, ZEBRA_IFC_DOWN
))
468 * Same logic as for connected_up(): push the changes into the
471 rib_delete(afi
, SAFI_UNICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0,
472 0, &p
, NULL
, &nh
, 0, zvrf
->table_id
, 0, 0, false);
474 rib_delete(afi
, SAFI_MULTICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
475 0, 0, &p
, NULL
, &nh
, 0, zvrf
->table_id
, 0, 0, false);
477 /* Schedule LSP forwarding entries for processing, if appropriate. */
478 if (zvrf
->vrf
->vrf_id
== VRF_DEFAULT
) {
479 if (IS_ZEBRA_DEBUG_MPLS
)
481 "%u: IF %s IP %pFX address down, scheduling MPLS processing",
482 zvrf
->vrf
->vrf_id
, ifp
->name
, &p
);
483 mpls_mark_lsps_for_processing(zvrf
, &p
);
487 static void connected_delete_helper(struct connected
*ifc
, struct prefix
*p
)
489 struct interface
*ifp
;
495 connected_withdraw(ifc
);
497 /* Schedule LSP forwarding entries for processing, if appropriate. */
498 if (ifp
->vrf
->vrf_id
== VRF_DEFAULT
) {
499 if (IS_ZEBRA_DEBUG_MPLS
)
501 "%u: IF %s IP %pFX address delete, scheduling MPLS processing",
502 ifp
->vrf
->vrf_id
, ifp
->name
, p
);
503 mpls_mark_lsps_for_processing(ifp
->vrf
->info
, p
);
507 /* Delete connected IPv4 route to the interface. */
508 void connected_delete_ipv4(struct interface
*ifp
, int flags
,
509 const struct in_addr
*addr
, uint16_t prefixlen
,
510 const struct in_addr
*dest
)
513 struct connected
*ifc
;
515 memset(&p
, 0, sizeof(p
));
519 CHECK_FLAG(flags
, ZEBRA_IFA_PEER
) ? IPV4_MAX_BITLEN
: prefixlen
;
522 memset(&d
, 0, sizeof(d
));
525 d
.prefixlen
= prefixlen
;
526 ifc
= connected_check_ptp(ifp
, &p
, &d
);
528 ifc
= connected_check_ptp(ifp
, &p
, NULL
);
530 connected_delete_helper(ifc
, &p
);
533 /* Add connected IPv6 route to the interface. */
534 void connected_add_ipv6(struct interface
*ifp
, int flags
,
535 const struct in6_addr
*addr
,
536 const struct in6_addr
*dest
, uint16_t prefixlen
,
537 const char *label
, uint32_t metric
)
539 struct prefix_ipv6
*p
;
540 struct connected
*ifc
;
542 if (ipv6_martian(addr
))
545 /* Make connected structure. */
546 ifc
= connected_new();
549 ifc
->metric
= metric
;
550 /* If we get a notification from the kernel,
551 * we can safely assume the address is known to the kernel */
552 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
553 if (!if_is_operative(ifp
))
554 SET_FLAG(ifc
->conf
, ZEBRA_IFC_DOWN
);
556 /* Allocate new connected address. */
557 p
= prefix_ipv6_new();
558 p
->family
= AF_INET6
;
559 IPV6_ADDR_COPY(&p
->prefix
, addr
);
560 p
->prefixlen
= prefixlen
;
561 ifc
->address
= (struct prefix
*)p
;
563 /* Add global ipv6 address to the RA prefix list */
564 if (!IN6_IS_ADDR_LINKLOCAL(&p
->prefix
))
565 rtadv_add_prefix(ifp
->info
, p
);
568 p
= prefix_ipv6_new();
569 p
->family
= AF_INET6
;
570 IPV6_ADDR_COPY(&p
->prefix
, dest
);
571 p
->prefixlen
= prefixlen
;
572 ifc
->destination
= (struct prefix
*)p
;
574 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
576 "%s called for interface %s with peer flag set, but no peer address supplied",
577 __func__
, ifp
->name
);
578 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
582 /* Label of this address. */
584 ifc
->label
= XSTRDUP(MTYPE_CONNECTED_LABEL
, label
);
586 /* On Linux, we only get here when DAD is complete, therefore we can set
589 * On BSD, there currently doesn't seem to be a way to check for
591 * DAD, so we replicate the old behaviour and set ZEBRA_IFC_REAL,
593 * might still be running.
595 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
596 connected_update(ifp
, ifc
);
599 void connected_delete_ipv6(struct interface
*ifp
,
600 const struct in6_addr
*address
,
601 const struct in6_addr
*dest
, uint16_t prefixlen
)
604 struct connected
*ifc
;
606 memset(&p
, 0, sizeof(p
));
608 memcpy(&p
.u
.prefix6
, address
, sizeof(struct in6_addr
));
609 p
.prefixlen
= prefixlen
;
611 /* Delete global ipv6 address from RA prefix list */
612 if (!IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
))
613 rtadv_delete_prefix(ifp
->info
, &p
);
616 memset(&d
, 0, sizeof(d
));
618 IPV6_ADDR_COPY(&d
.u
.prefix6
, dest
);
619 d
.prefixlen
= prefixlen
;
620 ifc
= connected_check_ptp(ifp
, &p
, &d
);
622 ifc
= connected_check_ptp(ifp
, &p
, NULL
);
624 connected_delete_helper(ifc
, &p
);
627 int connected_is_unnumbered(struct interface
*ifp
)
629 struct connected
*connected
;
630 struct listnode
*node
;
632 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
633 if (CHECK_FLAG(connected
->conf
, ZEBRA_IFC_REAL
)
634 && connected
->address
->family
== AF_INET
)
635 return CHECK_FLAG(connected
->flags
,
636 ZEBRA_IFA_UNNUMBERED
);