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 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
124 if (!prefix_same(ifc
->address
, p
))
126 if (!CONNECTED_PEER(ifc
) && !d
)
128 if (CONNECTED_PEER(ifc
) && d
129 && prefix_same(ifc
->destination
, d
))
136 /* Check if two ifc's describe the same address in the same state */
137 static int connected_same(struct connected
*ifc1
, struct connected
*ifc2
)
139 if (ifc1
->ifp
!= ifc2
->ifp
)
142 if (ifc1
->flags
!= ifc2
->flags
)
145 if (ifc1
->conf
!= ifc2
->conf
)
148 if (ifc1
->destination
)
149 if (!ifc2
->destination
)
151 if (ifc2
->destination
)
152 if (!ifc1
->destination
)
155 if (ifc1
->destination
&& ifc2
->destination
)
156 if (!prefix_same(ifc1
->destination
, ifc2
->destination
))
162 /* Handle changes to addresses and send the neccesary announcements
164 static void connected_update(struct interface
*ifp
, struct connected
*ifc
)
166 struct connected
*current
;
168 /* Check same connected route. */
169 current
= connected_check_ptp(ifp
, ifc
->address
, ifc
->destination
);
171 if (CHECK_FLAG(current
->conf
, ZEBRA_IFC_CONFIGURED
))
172 SET_FLAG(ifc
->conf
, ZEBRA_IFC_CONFIGURED
);
174 /* Avoid spurious withdraws, this might be just the kernel
176 * back an address we have already added.
178 if (connected_same(current
, ifc
)) {
184 /* Clear the configured flag on the old ifc, so it will be freed
186 * connected withdraw. */
187 UNSET_FLAG(current
->conf
, ZEBRA_IFC_CONFIGURED
);
189 current
); /* implicit withdraw - freebsd does this */
192 /* If the connected is new or has changed, announce it, if it is usable
194 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
195 connected_announce(ifp
, ifc
);
198 /* Called from if_up(). */
199 void connected_up(struct interface
*ifp
, struct connected
*ifc
)
203 struct nexthop nh
= {
204 .type
= NEXTHOP_TYPE_IFINDEX
,
205 .ifindex
= ifp
->ifindex
,
206 .vrf_id
= ifp
->vrf_id
,
208 struct zebra_vrf
*zvrf
;
211 zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
213 flog_err(EC_ZEBRA_VRF_NOT_FOUND
,
214 "%s: Received Up for interface but no associated zvrf: %d",
215 __PRETTY_FUNCTION__
, ifp
->vrf_id
);
218 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
221 PREFIX_COPY(&p
, CONNECTED_PREFIX(ifc
));
223 /* Apply mask to the network. */
226 afi
= family2afi(p
.family
);
231 * In case of connected address is 0.0.0.0/0 we treat it tunnel
234 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
239 /* XXX: It is already done by rib_bogus_ipv6 within rib_add */
240 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
245 flog_warn(EC_ZEBRA_CONNECTED_AFI_UNKNOWN
,
246 "Received unknown AFI: %s", afi2str(afi
));
251 metric
= (ifc
->metric
< (uint32_t)METRIC_MAX
) ?
252 ifc
->metric
: ifp
->metric
;
253 rib_add(afi
, SAFI_UNICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
254 0, 0, &p
, NULL
, &nh
, 0, zvrf
->table_id
, metric
, 0, 0, 0);
256 rib_add(afi
, SAFI_MULTICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
257 0, 0, &p
, NULL
, &nh
, 0, zvrf
->table_id
, metric
, 0, 0, 0);
259 /* Schedule LSP forwarding entries for processing, if appropriate. */
260 if (zvrf
->vrf
->vrf_id
== VRF_DEFAULT
) {
261 if (IS_ZEBRA_DEBUG_MPLS
) {
262 char buf
[PREFIX_STRLEN
];
265 "%u: IF %s IP %s address add/up, scheduling MPLS processing",
266 zvrf
->vrf
->vrf_id
, ifp
->name
,
267 prefix2str(&p
, buf
, sizeof(buf
)));
269 mpls_mark_lsps_for_processing(zvrf
, &p
);
273 /* Add connected IPv4 route to the interface. */
274 void connected_add_ipv4(struct interface
*ifp
, int flags
, struct in_addr
*addr
,
275 uint16_t prefixlen
, struct in_addr
*dest
,
276 const char *label
, uint32_t metric
)
278 struct prefix_ipv4
*p
;
279 struct connected
*ifc
;
281 if (ipv4_martian(addr
))
284 /* Make connected structure. */
285 ifc
= connected_new();
288 ifc
->metric
= metric
;
289 /* If we get a notification from the kernel,
290 * we can safely assume the address is known to the kernel */
291 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
293 /* Allocate new connected address. */
294 p
= prefix_ipv4_new();
297 p
->prefixlen
= CHECK_FLAG(flags
, ZEBRA_IFA_PEER
) ? IPV4_MAX_PREFIXLEN
299 ifc
->address
= (struct prefix
*)p
;
301 /* If there is a peer address. */
302 if (CONNECTED_PEER(ifc
)) {
303 /* validate the destination address */
305 p
= prefix_ipv4_new();
308 p
->prefixlen
= prefixlen
;
309 ifc
->destination
= (struct prefix
*)p
;
311 if (IPV4_ADDR_SAME(addr
, dest
))
313 EC_ZEBRA_IFACE_SAME_LOCAL_AS_PEER
,
314 "warning: interface %s has same local and peer "
315 "address %s, routing protocols may malfunction",
316 ifp
->name
, inet_ntoa(*addr
));
319 "warning: %s called for interface %s "
320 "with peer flag set, but no peer address supplied",
321 __func__
, ifp
->name
);
322 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
326 /* no destination address was supplied */
327 if (!dest
&& (prefixlen
== IPV4_MAX_PREFIXLEN
)
328 && if_is_pointopoint(ifp
))
330 "warning: PtP interface %s with addr %s/%d needs a "
332 ifp
->name
, inet_ntoa(*addr
), prefixlen
);
334 /* Label of this address. */
336 ifc
->label
= XSTRDUP(MTYPE_CONNECTED_LABEL
, label
);
338 /* For all that I know an IPv4 address is always ready when we receive
339 * the notification. So it should be safe to set the REAL flag here. */
340 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
342 connected_update(ifp
, ifc
);
345 void connected_down(struct interface
*ifp
, struct connected
*ifc
)
349 struct nexthop nh
= {
350 .type
= NEXTHOP_TYPE_IFINDEX
,
351 .ifindex
= ifp
->ifindex
,
352 .vrf_id
= ifp
->vrf_id
,
354 struct zebra_vrf
*zvrf
;
356 zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
358 flog_err(EC_ZEBRA_VRF_NOT_FOUND
,
359 "%s: Received Up for interface but no associated zvrf: %d",
360 __PRETTY_FUNCTION__
, ifp
->vrf_id
);
364 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
367 PREFIX_COPY(&p
, CONNECTED_PREFIX(ifc
));
369 /* Apply mask to the network. */
372 afi
= family2afi(p
.family
);
377 * In case of connected address is 0.0.0.0/0 we treat it tunnel
380 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
384 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
388 zlog_warn("Unknown AFI: %s", afi2str(afi
));
393 * Same logic as for connected_up(): push the changes into the
396 rib_delete(afi
, SAFI_UNICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0,
397 0, &p
, NULL
, &nh
, 0, zvrf
->table_id
, 0, 0, false);
399 rib_delete(afi
, SAFI_MULTICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
400 0, 0, &p
, NULL
, &nh
, 0, zvrf
->table_id
, 0, 0, false);
402 /* Schedule LSP forwarding entries for processing, if appropriate. */
403 if (zvrf
->vrf
->vrf_id
== VRF_DEFAULT
) {
404 if (IS_ZEBRA_DEBUG_MPLS
) {
405 char buf
[PREFIX_STRLEN
];
408 "%u: IF %s IP %s address down, scheduling MPLS processing",
409 zvrf
->vrf
->vrf_id
, ifp
->name
,
410 prefix2str(&p
, buf
, sizeof(buf
)));
412 mpls_mark_lsps_for_processing(zvrf
, &p
);
416 static void connected_delete_helper(struct connected
*ifc
, struct prefix
*p
)
418 struct interface
*ifp
;
424 connected_withdraw(ifc
);
426 /* Schedule LSP forwarding entries for processing, if appropriate. */
427 if (ifp
->vrf_id
== VRF_DEFAULT
) {
428 if (IS_ZEBRA_DEBUG_MPLS
) {
429 char buf
[PREFIX_STRLEN
];
432 "%u: IF %s IP %s address delete, scheduling MPLS processing",
433 ifp
->vrf_id
, ifp
->name
,
434 prefix2str(p
, buf
, sizeof(buf
)));
436 mpls_mark_lsps_for_processing(vrf_info_lookup(ifp
->vrf_id
), p
);
440 /* Delete connected IPv4 route to the interface. */
441 void connected_delete_ipv4(struct interface
*ifp
, int flags
,
442 struct in_addr
*addr
, uint16_t prefixlen
,
443 struct in_addr
*dest
)
446 struct connected
*ifc
;
448 memset(&p
, 0, sizeof(struct prefix
));
451 p
.prefixlen
= CHECK_FLAG(flags
, ZEBRA_IFA_PEER
) ? IPV4_MAX_PREFIXLEN
455 memset(&d
, 0, sizeof(struct prefix
));
458 d
.prefixlen
= prefixlen
;
459 ifc
= connected_check_ptp(ifp
, &p
, &d
);
461 ifc
= connected_check_ptp(ifp
, &p
, NULL
);
463 connected_delete_helper(ifc
, &p
);
466 /* Add connected IPv6 route to the interface. */
467 void connected_add_ipv6(struct interface
*ifp
, int flags
, struct in6_addr
*addr
,
468 struct in6_addr
*dest
, uint16_t prefixlen
,
469 const char *label
, uint32_t metric
)
471 struct prefix_ipv6
*p
;
472 struct connected
*ifc
;
474 if (ipv6_martian(addr
))
477 /* Make connected structure. */
478 ifc
= connected_new();
481 ifc
->metric
= metric
;
482 /* If we get a notification from the kernel,
483 * we can safely assume the address is known to the kernel */
484 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
486 /* Allocate new connected address. */
487 p
= prefix_ipv6_new();
488 p
->family
= AF_INET6
;
489 IPV6_ADDR_COPY(&p
->prefix
, addr
);
490 p
->prefixlen
= prefixlen
;
491 ifc
->address
= (struct prefix
*)p
;
494 p
= prefix_ipv6_new();
495 p
->family
= AF_INET6
;
496 IPV6_ADDR_COPY(&p
->prefix
, dest
);
497 p
->prefixlen
= prefixlen
;
498 ifc
->destination
= (struct prefix
*)p
;
500 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
502 "warning: %s called for interface %s with peer flag set, but no peer address supplied",
503 __func__
, ifp
->name
);
504 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
508 /* Label of this address. */
510 ifc
->label
= XSTRDUP(MTYPE_CONNECTED_LABEL
, label
);
512 /* On Linux, we only get here when DAD is complete, therefore we can set
515 * On BSD, there currently doesn't seem to be a way to check for
517 * DAD, so we replicate the old behaviour and set ZEBRA_IFC_REAL,
519 * might still be running.
521 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
522 connected_update(ifp
, ifc
);
525 void connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
,
526 struct in6_addr
*dest
, uint16_t prefixlen
)
529 struct connected
*ifc
;
531 memset(&p
, 0, sizeof(struct prefix
));
533 memcpy(&p
.u
.prefix6
, address
, sizeof(struct in6_addr
));
534 p
.prefixlen
= prefixlen
;
537 memset(&d
, 0, sizeof(struct prefix
));
539 IPV6_ADDR_COPY(&d
.u
.prefix6
, dest
);
540 d
.prefixlen
= prefixlen
;
541 ifc
= connected_check_ptp(ifp
, &p
, &d
);
543 ifc
= connected_check_ptp(ifp
, &p
, NULL
);
545 connected_delete_helper(ifc
, &p
);
548 int connected_is_unnumbered(struct interface
*ifp
)
550 struct connected
*connected
;
551 struct listnode
*node
;
553 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
554 if (CHECK_FLAG(connected
->conf
, ZEBRA_IFC_REAL
)
555 && connected
->address
->family
== AF_INET
)
556 return CHECK_FLAG(connected
->flags
,
557 ZEBRA_IFA_UNNUMBERED
);