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
, .ifindex
= ifp
->ifindex
,
209 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
212 PREFIX_COPY(&p
, CONNECTED_PREFIX(ifc
));
214 /* Apply mask to the network. */
217 afi
= family2afi(p
.family
);
222 * In case of connected address is 0.0.0.0/0 we treat it tunnel
225 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
230 /* XXX: It is already done by rib_bogus_ipv6 within rib_add */
231 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
236 zlog_warn("Received unknown AFI: %s", afi2str(afi
));
241 rib_add(afi
, SAFI_UNICAST
, ifp
->vrf_id
, ifp
->vrf_id
,
242 ZEBRA_ROUTE_CONNECT
, 0, 0,
243 &p
, NULL
, &nh
, RT_TABLE_MAIN
, ifp
->metric
, 0, 0, 0);
245 rib_add(afi
, SAFI_MULTICAST
, ifp
->vrf_id
, ifp
->vrf_id
,
246 ZEBRA_ROUTE_CONNECT
, 0, 0,
247 &p
, 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
, .ifindex
= ifp
->ifindex
,
368 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
371 PREFIX_COPY(&p
, CONNECTED_PREFIX(ifc
));
373 /* Apply mask to the network. */
376 afi
= family2afi(p
.family
);
381 * In case of connected address is 0.0.0.0/0 we treat it tunnel
384 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
388 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
392 zlog_info("Unknown AFI: %s", afi2str(afi
));
397 * Same logic as for connected_up(): push the changes into the
400 rib_delete(afi
, SAFI_UNICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0, 0,
401 &p
, NULL
, &nh
, 0, 0, false, NULL
);
403 rib_delete(afi
, SAFI_MULTICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0,
404 0, &p
, NULL
, &nh
, 0, 0, false, NULL
);
406 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
407 char buf
[PREFIX_STRLEN
];
409 zlog_debug("%u: IF %s IP %s address down, scheduling RIB processing",
410 ifp
->vrf_id
, ifp
->name
,
411 prefix2str(&p
, buf
, sizeof(buf
)));
414 rib_update(ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
416 /* Schedule LSP forwarding entries for processing, if appropriate. */
417 if (ifp
->vrf_id
== VRF_DEFAULT
) {
418 if (IS_ZEBRA_DEBUG_MPLS
) {
419 char buf
[PREFIX_STRLEN
];
421 zlog_debug("%u: IF %s IP %s address down, scheduling MPLS processing",
422 ifp
->vrf_id
, ifp
->name
,
423 prefix2str(&p
, buf
, sizeof(buf
)));
425 mpls_mark_lsps_for_processing(vrf_info_lookup(ifp
->vrf_id
));
429 static void connected_delete_helper(struct connected
*ifc
, struct prefix
*p
)
431 struct interface
*ifp
;
437 connected_withdraw(ifc
);
439 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
440 char buf
[PREFIX_STRLEN
];
442 zlog_debug("%u: IF %s IP %s address del, scheduling RIB processing",
443 ifp
->vrf_id
, ifp
->name
,
444 prefix2str(p
, buf
, sizeof(buf
)));
446 rib_update(ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
448 /* Schedule LSP forwarding entries for processing, if appropriate. */
449 if (ifp
->vrf_id
== VRF_DEFAULT
) {
450 if (IS_ZEBRA_DEBUG_MPLS
) {
451 char buf
[PREFIX_STRLEN
];
453 zlog_debug("%u: IF %s IP %s address delete, scheduling MPLS processing",
454 ifp
->vrf_id
, ifp
->name
,
455 prefix2str(p
, buf
, sizeof(buf
)));
457 mpls_mark_lsps_for_processing(vrf_info_lookup(ifp
->vrf_id
));
461 /* Delete connected IPv4 route to the interface. */
462 void connected_delete_ipv4(struct interface
*ifp
, int flags
,
463 struct in_addr
*addr
, u_char prefixlen
,
464 struct in_addr
*broad
)
467 struct connected
*ifc
;
469 memset(&p
, 0, sizeof(struct prefix
));
472 p
.prefixlen
= CHECK_FLAG(flags
, ZEBRA_IFA_PEER
) ? IPV4_MAX_PREFIXLEN
476 memset(&d
, 0, sizeof(struct prefix
));
478 d
.u
.prefix4
= *broad
;
479 d
.prefixlen
= prefixlen
;
480 ifc
= connected_check_ptp(ifp
, &p
, &d
);
482 ifc
= connected_check_ptp(ifp
, &p
, NULL
);
484 connected_delete_helper(ifc
, &p
);
487 /* Add connected IPv6 route to the interface. */
488 void connected_add_ipv6(struct interface
*ifp
, int flags
, struct in6_addr
*addr
,
489 u_char prefixlen
, const char *label
)
491 struct prefix_ipv6
*p
;
492 struct connected
*ifc
;
494 if (ipv6_martian(addr
))
497 /* Make connected structure. */
498 ifc
= connected_new();
501 /* If we get a notification from the kernel,
502 * we can safely assume the address is known to the kernel */
503 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
505 /* Allocate new connected address. */
506 p
= prefix_ipv6_new();
507 p
->family
= AF_INET6
;
508 IPV6_ADDR_COPY(&p
->prefix
, addr
);
509 p
->prefixlen
= prefixlen
;
510 ifc
->address
= (struct prefix
*)p
;
512 /* Label of this address. */
514 ifc
->label
= XSTRDUP(MTYPE_CONNECTED_LABEL
, label
);
516 /* On Linux, we only get here when DAD is complete, therefore we can set
519 * On BSD, there currently doesn't seem to be a way to check for
521 * DAD, so we replicate the old behaviour and set ZEBRA_IFC_REAL,
523 * might still be running.
525 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
526 connected_update(ifp
, ifc
);
529 void connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
,
533 struct connected
*ifc
;
535 memset(&p
, 0, sizeof(struct prefix
));
537 memcpy(&p
.u
.prefix6
, address
, sizeof(struct in6_addr
));
538 p
.prefixlen
= prefixlen
;
540 ifc
= connected_check(ifp
, &p
);
542 connected_delete_helper(ifc
, &p
);
545 int connected_is_unnumbered(struct interface
*ifp
)
547 struct connected
*connected
;
548 struct listnode
*node
;
550 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
551 if (CHECK_FLAG(connected
->conf
, ZEBRA_IFC_REAL
)
552 && connected
->address
->family
== AF_INET
)
553 return CHECK_FLAG(connected
->flags
,
554 ZEBRA_IFA_UNNUMBERED
);