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
, *zvrf_iter
;
391 uint32_t count_ipv4
= 0;
392 struct listnode
*cnode
;
394 struct route_table
*table
;
395 struct route_node
*rn
;
396 struct route_entry
*re
, *next
;
399 zvrf
= ifp
->vrf
->info
;
402 EC_ZEBRA_VRF_NOT_FOUND
,
403 "%s: Received Down for interface but no associated zvrf: %s(%d)",
404 __func__
, ifp
->vrf
->name
, ifp
->vrf
->vrf_id
);
408 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
411 /* Skip if we've already done this; this can happen if we have a
412 * config change that takes an interface down, then we receive kernel
413 * notifications about the downed interface and its addresses.
415 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_DOWN
)) {
416 if (IS_ZEBRA_DEBUG_RIB
)
417 zlog_debug("%s: ifc %p, %pFX already DOWN",
418 __func__
, ifc
, ifc
->address
);
422 prefix_copy(&p
, CONNECTED_PREFIX(ifc
));
424 /* Apply mask to the network. */
427 afi
= family2afi(p
.family
);
432 * In case of connected address is 0.0.0.0/0 we treat it tunnel
435 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
439 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
443 zlog_warn("Unknown AFI: %s", afi2str(afi
));
447 /* Mark the address as 'down' */
448 SET_FLAG(ifc
->conf
, ZEBRA_IFC_DOWN
);
451 * It's possible to have X number of addresses
452 * on a interface that all resolve to the same
453 * network and mask. Find them and just
454 * allow the deletion when are removing the last
457 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, cnode
, c
)) {
460 prefix_copy(&cp
, CONNECTED_PREFIX(c
));
463 if (CHECK_FLAG(c
->conf
, ZEBRA_IFC_DOWN
))
466 if (prefix_same(&p
, &cp
))
469 if (cp
.family
== AF_INET
)
474 * Same logic as for connected_up(): push the changes into the
477 rib_delete(afi
, SAFI_UNICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0,
478 0, &p
, NULL
, &nh
, 0, zvrf
->table_id
, 0, 0, false);
480 rib_delete(afi
, SAFI_MULTICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
481 0, 0, &p
, NULL
, &nh
, 0, zvrf
->table_id
, 0, 0, false);
483 /* When the last IPv4 address of an interface is deleted, Linux removes
484 * all routes using this interface without any Netlink advertisement.
485 * The removed routes include those that only have this particular
486 * interface as a nexthop. Among those, remove the kernel one from the
487 * FRR RIB and reinstall the other that have been added from FRR.
489 if (afi
== AFI_IP
&& count_ipv4
== 0 && if_is_operative(ifp
)) {
490 RB_FOREACH (vrf
, vrf_id_head
, &vrfs_by_id
) {
491 zvrf_iter
= vrf
->info
;
496 table
= zvrf_iter
->table
[AFI_IP
][SAFI_UNICAST
];
500 for (rn
= route_top(table
); rn
;
501 rn
= srcdest_route_next(rn
)) {
502 RNODE_FOREACH_RE_SAFE (rn
, re
, next
) {
503 if (CHECK_FLAG(re
->status
,
504 ROUTE_ENTRY_REMOVED
))
506 if (re
->nhe
->ifp
!= ifp
)
508 if (re
->type
== ZEBRA_ROUTE_KERNEL
)
511 zvrf_iter
->vrf
->vrf_id
,
512 re
->type
, 0, re
->flags
,
513 &rn
->p
, NULL
, &nh
, 0,
516 re
->distance
, false);
518 ZEBRA_ROUTE_CONNECT
) {
520 ROUTE_ENTRY_CHANGED
);
523 ROUTE_ENTRY_INSTALLED
);
524 rib_add(afi
, SAFI_UNICAST
,
525 zvrf_iter
->vrf
->vrf_id
,
526 re
->type
, 0, 0, &rn
->p
,
527 NULL
, &nh
, re
->nhe_id
,
530 re
->distance
, 0, false);
537 /* Schedule LSP forwarding entries for processing, if appropriate. */
538 if (zvrf
->vrf
->vrf_id
== VRF_DEFAULT
) {
539 if (IS_ZEBRA_DEBUG_MPLS
)
541 "%u: IF %s IP %pFX address down, scheduling MPLS processing",
542 zvrf
->vrf
->vrf_id
, ifp
->name
, &p
);
543 mpls_mark_lsps_for_processing(zvrf
, &p
);
547 static void connected_delete_helper(struct connected
*ifc
, struct prefix
*p
)
549 struct interface
*ifp
;
555 connected_withdraw(ifc
);
557 /* Schedule LSP forwarding entries for processing, if appropriate. */
558 if (ifp
->vrf
->vrf_id
== VRF_DEFAULT
) {
559 if (IS_ZEBRA_DEBUG_MPLS
)
561 "%u: IF %s IP %pFX address delete, scheduling MPLS processing",
562 ifp
->vrf
->vrf_id
, ifp
->name
, p
);
563 mpls_mark_lsps_for_processing(ifp
->vrf
->info
, p
);
567 /* Delete connected IPv4 route to the interface. */
568 void connected_delete_ipv4(struct interface
*ifp
, int flags
,
569 const struct in_addr
*addr
, uint16_t prefixlen
,
570 const struct in_addr
*dest
)
573 struct connected
*ifc
;
575 memset(&p
, 0, sizeof(p
));
579 CHECK_FLAG(flags
, ZEBRA_IFA_PEER
) ? IPV4_MAX_BITLEN
: prefixlen
;
582 memset(&d
, 0, sizeof(d
));
585 d
.prefixlen
= prefixlen
;
586 ifc
= connected_check_ptp(ifp
, &p
, &d
);
588 ifc
= connected_check_ptp(ifp
, &p
, NULL
);
590 connected_delete_helper(ifc
, &p
);
593 /* Add connected IPv6 route to the interface. */
594 void connected_add_ipv6(struct interface
*ifp
, int flags
,
595 const struct in6_addr
*addr
,
596 const struct in6_addr
*dest
, uint16_t prefixlen
,
597 const char *label
, uint32_t metric
)
599 struct prefix_ipv6
*p
;
600 struct connected
*ifc
;
602 if (ipv6_martian(addr
))
605 /* Make connected structure. */
606 ifc
= connected_new();
609 ifc
->metric
= metric
;
610 /* If we get a notification from the kernel,
611 * we can safely assume the address is known to the kernel */
612 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
613 if (!if_is_operative(ifp
))
614 SET_FLAG(ifc
->conf
, ZEBRA_IFC_DOWN
);
616 /* Allocate new connected address. */
617 p
= prefix_ipv6_new();
618 p
->family
= AF_INET6
;
619 IPV6_ADDR_COPY(&p
->prefix
, addr
);
620 p
->prefixlen
= prefixlen
;
621 ifc
->address
= (struct prefix
*)p
;
623 /* Add global ipv6 address to the RA prefix list */
624 if (!IN6_IS_ADDR_LINKLOCAL(&p
->prefix
))
625 rtadv_add_prefix(ifp
->info
, p
);
628 p
= prefix_ipv6_new();
629 p
->family
= AF_INET6
;
630 IPV6_ADDR_COPY(&p
->prefix
, dest
);
631 p
->prefixlen
= prefixlen
;
632 ifc
->destination
= (struct prefix
*)p
;
634 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
636 "%s called for interface %s with peer flag set, but no peer address supplied",
637 __func__
, ifp
->name
);
638 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
642 /* Label of this address. */
644 ifc
->label
= XSTRDUP(MTYPE_CONNECTED_LABEL
, label
);
646 /* On Linux, we only get here when DAD is complete, therefore we can set
649 * On BSD, there currently doesn't seem to be a way to check for
651 * DAD, so we replicate the old behaviour and set ZEBRA_IFC_REAL,
653 * might still be running.
655 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
656 connected_update(ifp
, ifc
);
659 void connected_delete_ipv6(struct interface
*ifp
,
660 const struct in6_addr
*address
,
661 const struct in6_addr
*dest
, uint16_t prefixlen
)
664 struct connected
*ifc
;
666 memset(&p
, 0, sizeof(p
));
668 memcpy(&p
.u
.prefix6
, address
, sizeof(struct in6_addr
));
669 p
.prefixlen
= prefixlen
;
671 /* Delete global ipv6 address from RA prefix list */
672 if (!IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
))
673 rtadv_delete_prefix(ifp
->info
, &p
);
676 memset(&d
, 0, sizeof(d
));
678 IPV6_ADDR_COPY(&d
.u
.prefix6
, dest
);
679 d
.prefixlen
= prefixlen
;
680 ifc
= connected_check_ptp(ifp
, &p
, &d
);
682 ifc
= connected_check_ptp(ifp
, &p
, NULL
);
684 connected_delete_helper(ifc
, &p
);
687 int connected_is_unnumbered(struct interface
*ifp
)
689 struct connected
*connected
;
690 struct listnode
*node
;
692 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
693 if (CHECK_FLAG(connected
->conf
, ZEBRA_IFC_REAL
)
694 && connected
->address
->family
== AF_INET
)
695 return CHECK_FLAG(connected
->flags
,
696 ZEBRA_IFA_UNNUMBERED
);