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
)) {
180 connected_free(&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
)
202 struct prefix p
= {0};
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
);
214 EC_ZEBRA_VRF_NOT_FOUND
,
215 "%s: Received Up for interface but no associated zvrf: %d",
216 __func__
, ifp
->vrf_id
);
219 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
222 PREFIX_COPY(&p
, CONNECTED_PREFIX(ifc
));
224 /* Apply mask to the network. */
227 afi
= family2afi(p
.family
);
232 * In case of connected address is 0.0.0.0/0 we treat it tunnel
235 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
240 /* XXX: It is already done by rib_bogus_ipv6 within rib_add */
241 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
246 flog_warn(EC_ZEBRA_CONNECTED_AFI_UNKNOWN
,
247 "Received unknown AFI: %s", afi2str(afi
));
252 metric
= (ifc
->metric
< (uint32_t)METRIC_MAX
) ?
253 ifc
->metric
: ifp
->metric
;
254 rib_add(afi
, SAFI_UNICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
255 0, 0, &p
, NULL
, &nh
, 0, zvrf
->table_id
, metric
, 0, 0, 0);
257 rib_add(afi
, SAFI_MULTICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
258 0, 0, &p
, NULL
, &nh
, 0, zvrf
->table_id
, metric
, 0, 0, 0);
260 /* Schedule LSP forwarding entries for processing, if appropriate. */
261 if (zvrf
->vrf
->vrf_id
== VRF_DEFAULT
) {
262 if (IS_ZEBRA_DEBUG_MPLS
) {
263 char buf
[PREFIX_STRLEN
];
266 "%u: IF %s IP %s address add/up, scheduling MPLS processing",
267 zvrf
->vrf
->vrf_id
, ifp
->name
,
268 prefix2str(&p
, buf
, sizeof(buf
)));
270 mpls_mark_lsps_for_processing(zvrf
, &p
);
274 /* Add connected IPv4 route to the interface. */
275 void connected_add_ipv4(struct interface
*ifp
, int flags
, struct in_addr
*addr
,
276 uint16_t prefixlen
, struct in_addr
*dest
,
277 const char *label
, uint32_t metric
)
279 struct prefix_ipv4
*p
;
280 struct connected
*ifc
;
282 if (ipv4_martian(addr
))
285 /* Make connected structure. */
286 ifc
= connected_new();
289 ifc
->metric
= metric
;
290 /* If we get a notification from the kernel,
291 * we can safely assume the address is known to the kernel */
292 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
294 /* Allocate new connected address. */
295 p
= prefix_ipv4_new();
298 p
->prefixlen
= CHECK_FLAG(flags
, ZEBRA_IFA_PEER
) ? IPV4_MAX_PREFIXLEN
300 ifc
->address
= (struct prefix
*)p
;
302 /* If there is a peer address. */
303 if (CONNECTED_PEER(ifc
)) {
304 /* validate the destination address */
306 p
= prefix_ipv4_new();
309 p
->prefixlen
= prefixlen
;
310 ifc
->destination
= (struct prefix
*)p
;
312 if (IPV4_ADDR_SAME(addr
, dest
))
314 EC_ZEBRA_IFACE_SAME_LOCAL_AS_PEER
,
315 "warning: interface %s has same local and peer address %s, routing protocols may malfunction",
316 ifp
->name
, inet_ntoa(*addr
));
319 "warning: %s called for interface %s with peer flag set, but no peer address supplied",
320 __func__
, ifp
->name
);
321 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
325 /* no destination address was supplied */
326 if (!dest
&& (prefixlen
== IPV4_MAX_PREFIXLEN
)
327 && if_is_pointopoint(ifp
))
329 "warning: PtP interface %s with addr %s/%d needs a peer address",
330 ifp
->name
, inet_ntoa(*addr
), prefixlen
);
332 /* Label of this address. */
334 ifc
->label
= XSTRDUP(MTYPE_CONNECTED_LABEL
, label
);
336 /* For all that I know an IPv4 address is always ready when we receive
337 * the notification. So it should be safe to set the REAL flag here. */
338 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
340 connected_update(ifp
, ifc
);
343 void connected_down(struct interface
*ifp
, struct connected
*ifc
)
347 struct nexthop nh
= {
348 .type
= NEXTHOP_TYPE_IFINDEX
,
349 .ifindex
= ifp
->ifindex
,
350 .vrf_id
= ifp
->vrf_id
,
352 struct zebra_vrf
*zvrf
;
354 zvrf
= zebra_vrf_lookup_by_id(ifp
->vrf_id
);
357 EC_ZEBRA_VRF_NOT_FOUND
,
358 "%s: Received Up for interface but no associated zvrf: %d",
359 __func__
, ifp
->vrf_id
);
363 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
366 PREFIX_COPY(&p
, CONNECTED_PREFIX(ifc
));
368 /* Apply mask to the network. */
371 afi
= family2afi(p
.family
);
376 * In case of connected address is 0.0.0.0/0 we treat it tunnel
379 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
383 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
387 zlog_warn("Unknown AFI: %s", afi2str(afi
));
392 * Same logic as for connected_up(): push the changes into the
395 rib_delete(afi
, SAFI_UNICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0,
396 0, &p
, NULL
, &nh
, 0, zvrf
->table_id
, 0, 0, false);
398 rib_delete(afi
, SAFI_MULTICAST
, zvrf
->vrf
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
399 0, 0, &p
, NULL
, &nh
, 0, zvrf
->table_id
, 0, 0, false);
401 /* Schedule LSP forwarding entries for processing, if appropriate. */
402 if (zvrf
->vrf
->vrf_id
== VRF_DEFAULT
) {
403 if (IS_ZEBRA_DEBUG_MPLS
) {
404 char buf
[PREFIX_STRLEN
];
407 "%u: IF %s IP %s address down, scheduling MPLS processing",
408 zvrf
->vrf
->vrf_id
, ifp
->name
,
409 prefix2str(&p
, buf
, sizeof(buf
)));
411 mpls_mark_lsps_for_processing(zvrf
, &p
);
415 static void connected_delete_helper(struct connected
*ifc
, struct prefix
*p
)
417 struct interface
*ifp
;
423 connected_withdraw(ifc
);
425 /* Schedule LSP forwarding entries for processing, if appropriate. */
426 if (ifp
->vrf_id
== VRF_DEFAULT
) {
427 if (IS_ZEBRA_DEBUG_MPLS
) {
428 char buf
[PREFIX_STRLEN
];
431 "%u: IF %s IP %s address delete, scheduling MPLS processing",
432 ifp
->vrf_id
, ifp
->name
,
433 prefix2str(p
, buf
, sizeof(buf
)));
435 mpls_mark_lsps_for_processing(vrf_info_lookup(ifp
->vrf_id
), p
);
439 /* Delete connected IPv4 route to the interface. */
440 void connected_delete_ipv4(struct interface
*ifp
, int flags
,
441 struct in_addr
*addr
, uint16_t prefixlen
,
442 struct in_addr
*dest
)
445 struct connected
*ifc
;
447 memset(&p
, 0, sizeof(struct prefix
));
450 p
.prefixlen
= CHECK_FLAG(flags
, ZEBRA_IFA_PEER
) ? IPV4_MAX_PREFIXLEN
454 memset(&d
, 0, sizeof(struct prefix
));
457 d
.prefixlen
= prefixlen
;
458 ifc
= connected_check_ptp(ifp
, &p
, &d
);
460 ifc
= connected_check_ptp(ifp
, &p
, NULL
);
462 connected_delete_helper(ifc
, &p
);
465 /* Add connected IPv6 route to the interface. */
466 void connected_add_ipv6(struct interface
*ifp
, int flags
, struct in6_addr
*addr
,
467 struct in6_addr
*dest
, uint16_t prefixlen
,
468 const char *label
, uint32_t metric
)
470 struct prefix_ipv6
*p
;
471 struct connected
*ifc
;
473 if (ipv6_martian(addr
))
476 /* Make connected structure. */
477 ifc
= connected_new();
480 ifc
->metric
= metric
;
481 /* If we get a notification from the kernel,
482 * we can safely assume the address is known to the kernel */
483 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
485 /* Allocate new connected address. */
486 p
= prefix_ipv6_new();
487 p
->family
= AF_INET6
;
488 IPV6_ADDR_COPY(&p
->prefix
, addr
);
489 p
->prefixlen
= prefixlen
;
490 ifc
->address
= (struct prefix
*)p
;
492 /* Add global ipv6 address to the RA prefix list */
493 if (!IN6_IS_ADDR_LINKLOCAL(&p
->prefix
))
494 rtadv_add_prefix(ifp
->info
, p
);
497 p
= prefix_ipv6_new();
498 p
->family
= AF_INET6
;
499 IPV6_ADDR_COPY(&p
->prefix
, dest
);
500 p
->prefixlen
= prefixlen
;
501 ifc
->destination
= (struct prefix
*)p
;
503 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
505 "warning: %s called for interface %s with peer flag set, but no peer address supplied",
506 __func__
, ifp
->name
);
507 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
511 /* Label of this address. */
513 ifc
->label
= XSTRDUP(MTYPE_CONNECTED_LABEL
, label
);
515 /* On Linux, we only get here when DAD is complete, therefore we can set
518 * On BSD, there currently doesn't seem to be a way to check for
520 * DAD, so we replicate the old behaviour and set ZEBRA_IFC_REAL,
522 * might still be running.
524 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
525 connected_update(ifp
, ifc
);
528 void connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
,
529 struct in6_addr
*dest
, uint16_t prefixlen
)
532 struct connected
*ifc
;
534 memset(&p
, 0, sizeof(struct prefix
));
536 memcpy(&p
.u
.prefix6
, address
, sizeof(struct in6_addr
));
537 p
.prefixlen
= prefixlen
;
539 /* Delete global ipv6 address from RA prefix list */
540 if (!IN6_IS_ADDR_LINKLOCAL(&p
.u
.prefix6
))
541 rtadv_delete_prefix(ifp
->info
, &p
);
544 memset(&d
, 0, sizeof(struct prefix
));
546 IPV6_ADDR_COPY(&d
.u
.prefix6
, dest
);
547 d
.prefixlen
= prefixlen
;
548 ifc
= connected_check_ptp(ifp
, &p
, &d
);
550 ifc
= connected_check_ptp(ifp
, &p
, NULL
);
552 connected_delete_helper(ifc
, &p
);
555 int connected_is_unnumbered(struct interface
*ifp
)
557 struct connected
*connected
;
558 struct listnode
*node
;
560 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
561 if (CHECK_FLAG(connected
->conf
, ZEBRA_IFC_REAL
)
562 && connected
->address
->family
== AF_INET
)
563 return CHECK_FLAG(connected
->flags
,
564 ZEBRA_IFA_UNNUMBERED
);