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
32 #include "zebra_memory.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 #include "zebra/zebra_errors.h"
45 /* communicate the withdrawal of a connected address */
46 static void connected_withdraw(struct connected
*ifc
)
51 /* Update interface address information to protocol daemon. */
52 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
)) {
53 zebra_interface_address_delete_update(ifc
->ifp
, ifc
);
55 if (ifc
->address
->family
== AF_INET
)
56 if_subnet_delete(ifc
->ifp
, ifc
);
58 connected_down(ifc
->ifp
, ifc
);
60 UNSET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
63 /* The address is not in the kernel anymore, so clear the flag */
64 UNSET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
66 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_CONFIGURED
)) {
67 listnode_delete(ifc
->ifp
->connected
, ifc
);
72 static void connected_announce(struct interface
*ifp
, struct connected
*ifc
)
77 if (!if_is_loopback(ifp
) && ifc
->address
->family
== AF_INET
&&
78 !IS_ZEBRA_IF_VRF(ifp
)) {
79 if (ifc
->address
->prefixlen
== 32)
80 SET_FLAG(ifc
->flags
, ZEBRA_IFA_UNNUMBERED
);
82 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_UNNUMBERED
);
85 listnode_add(ifp
->connected
, ifc
);
87 /* Update interface address information to protocol daemon. */
88 if (ifc
->address
->family
== AF_INET
)
89 if_subnet_add(ifp
, ifc
);
91 zebra_interface_address_add_update(ifp
, ifc
);
93 if (if_is_operative(ifp
)) {
94 connected_up(ifp
, ifc
);
98 /* If same interface address is already exist... */
99 struct connected
*connected_check(struct interface
*ifp
,
100 union prefixconstptr pu
)
102 const struct prefix
*p
= pu
.p
;
103 struct connected
*ifc
;
104 struct listnode
*node
;
106 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
))
107 if (prefix_same(ifc
->address
, p
))
113 /* same, but with peer address */
114 struct connected
*connected_check_ptp(struct interface
*ifp
,
115 union prefixconstptr pu
,
116 union prefixconstptr du
)
118 const struct prefix
*p
= pu
.p
;
119 const struct prefix
*d
= du
.p
;
120 struct connected
*ifc
;
121 struct listnode
*node
;
123 /* ignore broadcast addresses */
124 if (p
->prefixlen
!= IPV4_MAX_PREFIXLEN
)
127 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
128 if (!prefix_same(ifc
->address
, p
))
130 if (!CONNECTED_PEER(ifc
) && !d
)
132 if (CONNECTED_PEER(ifc
) && d
133 && prefix_same(ifc
->destination
, d
))
140 /* Check if two ifc's describe the same address in the same state */
141 static int connected_same(struct connected
*ifc1
, struct connected
*ifc2
)
143 if (ifc1
->ifp
!= ifc2
->ifp
)
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
))
157 if (ifc1
->flags
!= ifc2
->flags
)
160 if (ifc1
->conf
!= ifc2
->conf
)
166 /* Handle changes to addresses and send the neccesary announcements
168 static void connected_update(struct interface
*ifp
, struct connected
*ifc
)
170 struct connected
*current
;
172 /* Check same connected route. */
173 current
= connected_check_ptp(ifp
, ifc
->address
, ifc
->destination
);
175 if (CHECK_FLAG(current
->conf
, ZEBRA_IFC_CONFIGURED
))
176 SET_FLAG(ifc
->conf
, ZEBRA_IFC_CONFIGURED
);
178 /* Avoid spurious withdraws, this might be just the kernel
180 * back an address we have already added.
182 if (connected_same(current
, ifc
)) {
188 /* Clear the configured flag on the old ifc, so it will be freed
190 * connected withdraw. */
191 UNSET_FLAG(current
->conf
, ZEBRA_IFC_CONFIGURED
);
193 current
); /* implicit withdraw - freebsd does this */
196 /* If the connected is new or has changed, announce it, if it is usable
198 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
199 connected_announce(ifp
, ifc
);
202 /* Called from if_up(). */
203 void connected_up(struct interface
*ifp
, struct connected
*ifc
)
207 struct nexthop nh
= {
208 .type
= NEXTHOP_TYPE_IFINDEX
,
209 .ifindex
= ifp
->ifindex
,
210 .vrf_id
= ifp
->vrf_id
,
212 struct zebra_vrf
*zvrf
;
215 zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
217 flog_err(EC_ZEBRA_VRF_NOT_FOUND
,
218 "%s: Received Up for interface but no associated zvrf: %d",
219 __PRETTY_FUNCTION__
, ifp
->vrf_id
);
222 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
225 PREFIX_COPY(&p
, CONNECTED_PREFIX(ifc
));
227 /* Apply mask to the network. */
230 afi
= family2afi(p
.family
);
235 * In case of connected address is 0.0.0.0/0 we treat it tunnel
238 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
243 /* XXX: It is already done by rib_bogus_ipv6 within rib_add */
244 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
249 flog_warn(EC_ZEBRA_CONNECTED_AFI_UNKNOWN
,
250 "Received unknown AFI: %s", afi2str(afi
));
255 metric
= (ifc
->metric
< (uint32_t)METRIC_MAX
) ?
256 ifc
->metric
: ifp
->metric
;
257 rib_add(afi
, SAFI_UNICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
258 0, 0, &p
, NULL
, &nh
, zvrf
->table_id
, metric
, 0, 0, 0);
260 rib_add(afi
, SAFI_MULTICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
261 0, 0, &p
, NULL
, &nh
, zvrf
->table_id
, metric
, 0, 0, 0);
263 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
264 char buf
[PREFIX_STRLEN
];
267 "%u: IF %s address %s add/up, scheduling RIB processing",
268 ifp
->vrf_id
, ifp
->name
,
269 prefix2str(&p
, buf
, sizeof(buf
)));
271 rib_update(zvrf
->vrf
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
273 /* Schedule LSP forwarding entries for processing, if appropriate. */
274 if (zvrf
->vrf
->vrf_id
== VRF_DEFAULT
) {
275 if (IS_ZEBRA_DEBUG_MPLS
) {
276 char buf
[PREFIX_STRLEN
];
279 "%u: IF %s IP %s address add/up, scheduling MPLS processing",
280 zvrf
->vrf
->vrf_id
, ifp
->name
,
281 prefix2str(&p
, buf
, sizeof(buf
)));
283 mpls_mark_lsps_for_processing(zvrf
, &p
);
287 /* Add connected IPv4 route to the interface. */
288 void connected_add_ipv4(struct interface
*ifp
, int flags
, struct in_addr
*addr
,
289 uint16_t prefixlen
, struct in_addr
*broad
,
290 const char *label
, uint32_t metric
)
292 struct prefix_ipv4
*p
;
293 struct connected
*ifc
;
295 if (ipv4_martian(addr
))
298 /* Make connected structure. */
299 ifc
= connected_new();
302 ifc
->metric
= metric
;
303 /* If we get a notification from the kernel,
304 * we can safely assume the address is known to the kernel */
305 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
307 /* Allocate new connected address. */
308 p
= prefix_ipv4_new();
311 p
->prefixlen
= CHECK_FLAG(flags
, ZEBRA_IFA_PEER
) ? IPV4_MAX_PREFIXLEN
313 ifc
->address
= (struct prefix
*)p
;
315 /* If there is broadcast or peer address. */
317 p
= prefix_ipv4_new();
320 p
->prefixlen
= prefixlen
;
321 ifc
->destination
= (struct prefix
*)p
;
323 /* validate the destination address */
324 if (CONNECTED_PEER(ifc
)) {
325 if (IPV4_ADDR_SAME(addr
, broad
))
327 EC_ZEBRA_IFACE_SAME_LOCAL_AS_PEER
,
328 "warning: interface %s has same local and peer "
329 "address %s, routing protocols may malfunction",
330 ifp
->name
, inet_ntoa(*addr
));
333 != ipv4_broadcast_addr(addr
->s_addr
, prefixlen
)) {
334 char buf
[2][INET_ADDRSTRLEN
];
335 struct in_addr bcalc
;
336 bcalc
.s_addr
= ipv4_broadcast_addr(addr
->s_addr
,
339 EC_ZEBRA_BCAST_ADDR_MISMATCH
,
340 "warning: interface %s broadcast addr %s/%d != "
341 "calculated %s, routing protocols may malfunction",
343 inet_ntop(AF_INET
, broad
, buf
[0],
346 inet_ntop(AF_INET
, &bcalc
, buf
[1],
352 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
354 "warning: %s called for interface %s "
355 "with peer flag set, but no peer address supplied",
356 __func__
, ifp
->name
);
357 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
360 /* no broadcast or destination address was supplied */
361 if ((prefixlen
== IPV4_MAX_PREFIXLEN
) && if_is_pointopoint(ifp
))
363 "warning: PtP interface %s with addr %s/%d needs a "
365 ifp
->name
, inet_ntoa(*addr
), prefixlen
);
368 /* Label of this address. */
370 ifc
->label
= XSTRDUP(MTYPE_CONNECTED_LABEL
, label
);
372 /* For all that I know an IPv4 address is always ready when we receive
373 * the notification. So it should be safe to set the REAL flag here. */
374 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
376 connected_update(ifp
, ifc
);
379 void connected_down(struct interface
*ifp
, struct connected
*ifc
)
383 struct nexthop nh
= {
384 .type
= NEXTHOP_TYPE_IFINDEX
,
385 .ifindex
= ifp
->ifindex
,
386 .vrf_id
= ifp
->vrf_id
,
388 struct zebra_vrf
*zvrf
;
390 zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
392 flog_err(EC_ZEBRA_VRF_NOT_FOUND
,
393 "%s: Received Up for interface but no associated zvrf: %d",
394 __PRETTY_FUNCTION__
, ifp
->vrf_id
);
398 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
401 PREFIX_COPY(&p
, CONNECTED_PREFIX(ifc
));
403 /* Apply mask to the network. */
406 afi
= family2afi(p
.family
);
411 * In case of connected address is 0.0.0.0/0 we treat it tunnel
414 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
418 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
422 zlog_info("Unknown AFI: %s", afi2str(afi
));
427 * Same logic as for connected_up(): push the changes into the
430 rib_delete(afi
, SAFI_UNICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
431 0, 0, &p
, NULL
, &nh
, zvrf
->table_id
, 0, 0, false);
433 rib_delete(afi
, SAFI_MULTICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
434 0, 0, &p
, NULL
, &nh
, zvrf
->table_id
, 0, 0, false);
436 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
437 char buf
[PREFIX_STRLEN
];
440 "%u: IF %s IP %s address down, scheduling RIB processing",
441 zvrf
->vrf
->vrf_id
, ifp
->name
,
442 prefix2str(&p
, buf
, sizeof(buf
)));
445 rib_update(zvrf
->vrf
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
447 /* Schedule LSP forwarding entries for processing, if appropriate. */
448 if (zvrf
->vrf
->vrf_id
== VRF_DEFAULT
) {
449 if (IS_ZEBRA_DEBUG_MPLS
) {
450 char buf
[PREFIX_STRLEN
];
453 "%u: IF %s IP %s address down, scheduling MPLS processing",
454 zvrf
->vrf
->vrf_id
, ifp
->name
,
455 prefix2str(&p
, buf
, sizeof(buf
)));
457 mpls_mark_lsps_for_processing(zvrf
, &p
);
461 static void connected_delete_helper(struct connected
*ifc
, struct prefix
*p
)
463 struct interface
*ifp
;
469 connected_withdraw(ifc
);
471 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
472 char buf
[PREFIX_STRLEN
];
475 "%u: IF %s IP %s address del, scheduling RIB processing",
476 ifp
->vrf_id
, ifp
->name
,
477 prefix2str(p
, buf
, sizeof(buf
)));
479 rib_update(ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
481 /* Schedule LSP forwarding entries for processing, if appropriate. */
482 if (ifp
->vrf_id
== VRF_DEFAULT
) {
483 if (IS_ZEBRA_DEBUG_MPLS
) {
484 char buf
[PREFIX_STRLEN
];
487 "%u: IF %s IP %s address delete, scheduling MPLS processing",
488 ifp
->vrf_id
, ifp
->name
,
489 prefix2str(p
, buf
, sizeof(buf
)));
491 mpls_mark_lsps_for_processing(vrf_info_lookup(ifp
->vrf_id
), p
);
495 /* Delete connected IPv4 route to the interface. */
496 void connected_delete_ipv4(struct interface
*ifp
, int flags
,
497 struct in_addr
*addr
, uint16_t prefixlen
,
498 struct in_addr
*broad
)
501 struct connected
*ifc
;
503 memset(&p
, 0, sizeof(struct prefix
));
506 p
.prefixlen
= CHECK_FLAG(flags
, ZEBRA_IFA_PEER
) ? IPV4_MAX_PREFIXLEN
510 memset(&d
, 0, sizeof(struct prefix
));
512 d
.u
.prefix4
= *broad
;
513 d
.prefixlen
= prefixlen
;
514 ifc
= connected_check_ptp(ifp
, &p
, &d
);
516 ifc
= connected_check_ptp(ifp
, &p
, NULL
);
518 connected_delete_helper(ifc
, &p
);
521 /* Add connected IPv6 route to the interface. */
522 void connected_add_ipv6(struct interface
*ifp
, int flags
, struct in6_addr
*addr
,
523 struct in6_addr
*broad
, uint16_t prefixlen
,
524 const char *label
, uint32_t metric
)
526 struct prefix_ipv6
*p
;
527 struct connected
*ifc
;
529 if (ipv6_martian(addr
))
532 /* Make connected structure. */
533 ifc
= connected_new();
536 ifc
->metric
= metric
;
537 /* If we get a notification from the kernel,
538 * we can safely assume the address is known to the kernel */
539 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
541 /* Allocate new connected address. */
542 p
= prefix_ipv6_new();
543 p
->family
= AF_INET6
;
544 IPV6_ADDR_COPY(&p
->prefix
, addr
);
545 p
->prefixlen
= prefixlen
;
546 ifc
->address
= (struct prefix
*)p
;
549 p
= prefix_ipv6_new();
550 p
->family
= AF_INET6
;
551 IPV6_ADDR_COPY(&p
->prefix
, broad
);
552 p
->prefixlen
= prefixlen
;
553 ifc
->destination
= (struct prefix
*)p
;
555 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
557 "warning: %s called for interface %s with peer flag set, but no peer address supplied",
558 __func__
, ifp
->name
);
559 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
563 /* Label of this address. */
565 ifc
->label
= XSTRDUP(MTYPE_CONNECTED_LABEL
, label
);
567 /* On Linux, we only get here when DAD is complete, therefore we can set
570 * On BSD, there currently doesn't seem to be a way to check for
572 * DAD, so we replicate the old behaviour and set ZEBRA_IFC_REAL,
574 * might still be running.
576 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
577 connected_update(ifp
, ifc
);
580 void connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
,
581 struct in6_addr
*broad
, uint16_t prefixlen
)
584 struct connected
*ifc
;
586 memset(&p
, 0, sizeof(struct prefix
));
588 memcpy(&p
.u
.prefix6
, address
, sizeof(struct in6_addr
));
589 p
.prefixlen
= prefixlen
;
592 memset(&d
, 0, sizeof(struct prefix
));
594 IPV6_ADDR_COPY(&d
.u
.prefix6
, broad
);
595 d
.prefixlen
= prefixlen
;
596 ifc
= connected_check_ptp(ifp
, &p
, &d
);
598 ifc
= connected_check_ptp(ifp
, &p
, NULL
);
600 connected_delete_helper(ifc
, &p
);
603 int connected_is_unnumbered(struct interface
*ifp
)
605 struct connected
*connected
;
606 struct listnode
*node
;
608 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
609 if (CHECK_FLAG(connected
->conf
, ZEBRA_IFC_REAL
)
610 && connected
->address
->family
== AF_INET
)
611 return CHECK_FLAG(connected
->flags
,
612 ZEBRA_IFA_UNNUMBERED
);