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"
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
== 32)
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 /* ignore broadcast addresses */
122 if (p
->prefixlen
!= IPV4_MAX_PREFIXLEN
)
125 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
126 if (!prefix_same(ifc
->address
, p
))
128 if (!CONNECTED_PEER(ifc
) && !d
)
130 if (CONNECTED_PEER(ifc
) && d
131 && prefix_same(ifc
->destination
, d
))
138 /* Check if two ifc's describe the same address in the same state */
139 static int connected_same(struct connected
*ifc1
, struct connected
*ifc2
)
141 if (ifc1
->ifp
!= ifc2
->ifp
)
144 if (ifc1
->destination
)
145 if (!ifc2
->destination
)
147 if (ifc2
->destination
)
148 if (!ifc1
->destination
)
151 if (ifc1
->destination
&& ifc2
->destination
)
152 if (!prefix_same(ifc1
->destination
, ifc2
->destination
))
155 if (ifc1
->flags
!= ifc2
->flags
)
158 if (ifc1
->conf
!= ifc2
->conf
)
164 /* Handle changes to addresses and send the neccesary announcements
166 static void connected_update(struct interface
*ifp
, struct connected
*ifc
)
168 struct connected
*current
;
170 /* Check same connected route. */
171 current
= connected_check_ptp(ifp
, ifc
->address
, ifc
->destination
);
173 if (CHECK_FLAG(current
->conf
, ZEBRA_IFC_CONFIGURED
))
174 SET_FLAG(ifc
->conf
, ZEBRA_IFC_CONFIGURED
);
176 /* Avoid spurious withdraws, this might be just the kernel
178 * back an address we have already added.
180 if (connected_same(current
, ifc
)) {
186 /* Clear the configured flag on the old ifc, so it will be freed
188 * connected withdraw. */
189 UNSET_FLAG(current
->conf
, ZEBRA_IFC_CONFIGURED
);
191 current
); /* implicit withdraw - freebsd does this */
194 /* If the connected is new or has changed, announce it, if it is usable
196 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
197 connected_announce(ifp
, ifc
);
200 /* Called from if_up(). */
201 void connected_up(struct interface
*ifp
, struct connected
*ifc
)
205 struct nexthop nh
= {
206 .type
= NEXTHOP_TYPE_IFINDEX
,
207 .ifindex
= ifp
->ifindex
,
208 .vrf_id
= ifp
->vrf_id
,
211 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
214 PREFIX_COPY(&p
, CONNECTED_PREFIX(ifc
));
216 /* Apply mask to the network. */
219 afi
= family2afi(p
.family
);
224 * In case of connected address is 0.0.0.0/0 we treat it tunnel
227 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
232 /* XXX: It is already done by rib_bogus_ipv6 within rib_add */
233 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
238 zlog_warn("Received unknown AFI: %s", afi2str(afi
));
243 rib_add(afi
, SAFI_UNICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0, 0, &p
,
244 NULL
, &nh
, RT_TABLE_MAIN
, ifp
->metric
, 0, 0, 0);
246 rib_add(afi
, SAFI_MULTICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0, 0, &p
,
247 NULL
, &nh
, RT_TABLE_MAIN
, ifp
->metric
, 0, 0, 0);
249 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
250 char buf
[PREFIX_STRLEN
];
252 zlog_debug("%u: IF %s address %s add/up, scheduling RIB processing",
253 ifp
->vrf_id
, ifp
->name
,
254 prefix2str(&p
, buf
, sizeof(buf
)));
256 rib_update(ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
258 /* Schedule LSP forwarding entries for processing, if appropriate. */
259 if (ifp
->vrf_id
== VRF_DEFAULT
) {
260 if (IS_ZEBRA_DEBUG_MPLS
) {
261 char buf
[PREFIX_STRLEN
];
263 zlog_debug("%u: IF %s IP %s address add/up, scheduling MPLS processing",
264 ifp
->vrf_id
, ifp
->name
,
265 prefix2str(&p
, buf
, sizeof(buf
)));
267 mpls_mark_lsps_for_processing(vrf_info_lookup(ifp
->vrf_id
));
271 /* Add connected IPv4 route to the interface. */
272 void connected_add_ipv4(struct interface
*ifp
, int flags
, struct in_addr
*addr
,
273 u_char prefixlen
, struct in_addr
*broad
,
276 struct prefix_ipv4
*p
;
277 struct connected
*ifc
;
279 if (ipv4_martian(addr
))
282 /* Make connected structure. */
283 ifc
= connected_new();
286 /* If we get a notification from the kernel,
287 * we can safely assume the address is known to the kernel */
288 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
290 /* Allocate new connected address. */
291 p
= prefix_ipv4_new();
294 p
->prefixlen
= CHECK_FLAG(flags
, ZEBRA_IFA_PEER
) ? IPV4_MAX_PREFIXLEN
296 ifc
->address
= (struct prefix
*)p
;
298 /* If there is broadcast or peer address. */
300 p
= prefix_ipv4_new();
303 p
->prefixlen
= prefixlen
;
304 ifc
->destination
= (struct prefix
*)p
;
306 /* validate the destination address */
307 if (CONNECTED_PEER(ifc
)) {
308 if (IPV4_ADDR_SAME(addr
, broad
))
310 "warning: interface %s has same local and peer "
311 "address %s, routing protocols may malfunction",
312 ifp
->name
, inet_ntoa(*addr
));
315 != ipv4_broadcast_addr(addr
->s_addr
, prefixlen
)) {
316 char buf
[2][INET_ADDRSTRLEN
];
317 struct in_addr bcalc
;
318 bcalc
.s_addr
= ipv4_broadcast_addr(addr
->s_addr
,
321 "warning: interface %s broadcast addr %s/%d != "
322 "calculated %s, routing protocols may malfunction",
324 inet_ntop(AF_INET
, broad
, buf
[0],
327 inet_ntop(AF_INET
, &bcalc
, buf
[1],
333 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
335 "warning: %s called for interface %s "
336 "with peer flag set, but no peer address supplied",
337 __func__
, ifp
->name
);
338 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
341 /* no broadcast or destination address was supplied */
342 if ((prefixlen
== IPV4_MAX_PREFIXLEN
) && if_is_pointopoint(ifp
))
344 "warning: PtP interface %s with addr %s/%d needs a "
346 ifp
->name
, inet_ntoa(*addr
), prefixlen
);
349 /* Label of this address. */
351 ifc
->label
= XSTRDUP(MTYPE_CONNECTED_LABEL
, label
);
353 /* For all that I know an IPv4 address is always ready when we receive
354 * the notification. So it should be safe to set the REAL flag here. */
355 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
357 connected_update(ifp
, ifc
);
360 void connected_down(struct interface
*ifp
, struct connected
*ifc
)
364 struct nexthop nh
= {
365 .type
= NEXTHOP_TYPE_IFINDEX
,
366 .ifindex
= ifp
->ifindex
,
367 .vrf_id
= ifp
->vrf_id
,
370 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
373 PREFIX_COPY(&p
, CONNECTED_PREFIX(ifc
));
375 /* Apply mask to the network. */
378 afi
= family2afi(p
.family
);
383 * In case of connected address is 0.0.0.0/0 we treat it tunnel
386 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
390 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
394 zlog_info("Unknown AFI: %s", afi2str(afi
));
399 * Same logic as for connected_up(): push the changes into the
402 rib_delete(afi
, SAFI_UNICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0, 0,
403 &p
, NULL
, &nh
, 0, 0, false, NULL
);
405 rib_delete(afi
, SAFI_MULTICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0,
406 0, &p
, NULL
, &nh
, 0, 0, false, NULL
);
408 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
409 char buf
[PREFIX_STRLEN
];
411 zlog_debug("%u: IF %s IP %s address down, scheduling RIB processing",
412 ifp
->vrf_id
, ifp
->name
,
413 prefix2str(&p
, buf
, sizeof(buf
)));
416 rib_update(ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
418 /* Schedule LSP forwarding entries for processing, if appropriate. */
419 if (ifp
->vrf_id
== VRF_DEFAULT
) {
420 if (IS_ZEBRA_DEBUG_MPLS
) {
421 char buf
[PREFIX_STRLEN
];
423 zlog_debug("%u: IF %s IP %s address down, scheduling MPLS processing",
424 ifp
->vrf_id
, ifp
->name
,
425 prefix2str(&p
, buf
, sizeof(buf
)));
427 mpls_mark_lsps_for_processing(vrf_info_lookup(ifp
->vrf_id
));
431 static void connected_delete_helper(struct connected
*ifc
, struct prefix
*p
)
433 struct interface
*ifp
;
439 connected_withdraw(ifc
);
441 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
442 char buf
[PREFIX_STRLEN
];
444 zlog_debug("%u: IF %s IP %s address del, scheduling RIB processing",
445 ifp
->vrf_id
, ifp
->name
,
446 prefix2str(p
, buf
, sizeof(buf
)));
448 rib_update(ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
450 /* Schedule LSP forwarding entries for processing, if appropriate. */
451 if (ifp
->vrf_id
== VRF_DEFAULT
) {
452 if (IS_ZEBRA_DEBUG_MPLS
) {
453 char buf
[PREFIX_STRLEN
];
455 zlog_debug("%u: IF %s IP %s address delete, scheduling MPLS processing",
456 ifp
->vrf_id
, ifp
->name
,
457 prefix2str(p
, buf
, sizeof(buf
)));
459 mpls_mark_lsps_for_processing(vrf_info_lookup(ifp
->vrf_id
));
463 /* Delete connected IPv4 route to the interface. */
464 void connected_delete_ipv4(struct interface
*ifp
, int flags
,
465 struct in_addr
*addr
, u_char prefixlen
,
466 struct in_addr
*broad
)
469 struct connected
*ifc
;
471 memset(&p
, 0, sizeof(struct prefix
));
474 p
.prefixlen
= CHECK_FLAG(flags
, ZEBRA_IFA_PEER
) ? IPV4_MAX_PREFIXLEN
478 memset(&d
, 0, sizeof(struct prefix
));
480 d
.u
.prefix4
= *broad
;
481 d
.prefixlen
= prefixlen
;
482 ifc
= connected_check_ptp(ifp
, &p
, &d
);
484 ifc
= connected_check_ptp(ifp
, &p
, NULL
);
486 connected_delete_helper(ifc
, &p
);
489 /* Add connected IPv6 route to the interface. */
490 void connected_add_ipv6(struct interface
*ifp
, int flags
, struct in6_addr
*addr
,
491 u_char prefixlen
, const char *label
)
493 struct prefix_ipv6
*p
;
494 struct connected
*ifc
;
496 if (ipv6_martian(addr
))
499 /* Make connected structure. */
500 ifc
= connected_new();
503 /* If we get a notification from the kernel,
504 * we can safely assume the address is known to the kernel */
505 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
507 /* Allocate new connected address. */
508 p
= prefix_ipv6_new();
509 p
->family
= AF_INET6
;
510 IPV6_ADDR_COPY(&p
->prefix
, addr
);
511 p
->prefixlen
= prefixlen
;
512 ifc
->address
= (struct prefix
*)p
;
514 /* Label of this address. */
516 ifc
->label
= XSTRDUP(MTYPE_CONNECTED_LABEL
, label
);
518 /* On Linux, we only get here when DAD is complete, therefore we can set
521 * On BSD, there currently doesn't seem to be a way to check for
523 * DAD, so we replicate the old behaviour and set ZEBRA_IFC_REAL,
525 * might still be running.
527 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
528 connected_update(ifp
, ifc
);
531 void connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
,
535 struct connected
*ifc
;
537 memset(&p
, 0, sizeof(struct prefix
));
539 memcpy(&p
.u
.prefix6
, address
, sizeof(struct in6_addr
));
540 p
.prefixlen
= prefixlen
;
542 ifc
= connected_check(ifp
, &p
);
544 connected_delete_helper(ifc
, &p
);
547 int connected_is_unnumbered(struct interface
*ifp
)
549 struct connected
*connected
;
550 struct listnode
*node
;
552 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
553 if (CHECK_FLAG(connected
->conf
, ZEBRA_IFC_REAL
)
554 && connected
->address
->family
== AF_INET
)
555 return CHECK_FLAG(connected
->flags
,
556 ZEBRA_IFA_UNNUMBERED
);