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
,
213 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
216 PREFIX_COPY(&p
, CONNECTED_PREFIX(ifc
));
218 /* Apply mask to the network. */
221 afi
= family2afi(p
.family
);
226 * In case of connected address is 0.0.0.0/0 we treat it tunnel
229 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
234 /* XXX: It is already done by rib_bogus_ipv6 within rib_add */
235 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
240 flog_warn(EC_ZEBRA_CONNECTED_AFI_UNKNOWN
,
241 "Received unknown AFI: %s", afi2str(afi
));
246 rib_add(afi
, SAFI_UNICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0, 0, &p
,
247 NULL
, &nh
, RT_TABLE_MAIN
, ifp
->metric
, 0, 0, 0);
249 rib_add(afi
, SAFI_MULTICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0, 0, &p
,
250 NULL
, &nh
, RT_TABLE_MAIN
, ifp
->metric
, 0, 0, 0);
252 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
253 char buf
[PREFIX_STRLEN
];
256 "%u: IF %s address %s add/up, scheduling RIB processing",
257 ifp
->vrf_id
, ifp
->name
,
258 prefix2str(&p
, buf
, sizeof(buf
)));
260 rib_update(ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
262 /* Schedule LSP forwarding entries for processing, if appropriate. */
263 if (ifp
->vrf_id
== VRF_DEFAULT
) {
264 if (IS_ZEBRA_DEBUG_MPLS
) {
265 char buf
[PREFIX_STRLEN
];
268 "%u: IF %s IP %s address add/up, scheduling MPLS processing",
269 ifp
->vrf_id
, ifp
->name
,
270 prefix2str(&p
, buf
, sizeof(buf
)));
272 mpls_mark_lsps_for_processing(vrf_info_lookup(ifp
->vrf_id
));
276 /* Add connected IPv4 route to the interface. */
277 void connected_add_ipv4(struct interface
*ifp
, int flags
, struct in_addr
*addr
,
278 uint8_t prefixlen
, struct in_addr
*broad
,
281 struct prefix_ipv4
*p
;
282 struct connected
*ifc
;
284 if (ipv4_martian(addr
))
287 /* Make connected structure. */
288 ifc
= connected_new();
291 /* If we get a notification from the kernel,
292 * we can safely assume the address is known to the kernel */
293 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
295 /* Allocate new connected address. */
296 p
= prefix_ipv4_new();
299 p
->prefixlen
= CHECK_FLAG(flags
, ZEBRA_IFA_PEER
) ? IPV4_MAX_PREFIXLEN
301 ifc
->address
= (struct prefix
*)p
;
303 /* If there is broadcast or peer address. */
305 p
= prefix_ipv4_new();
308 p
->prefixlen
= prefixlen
;
309 ifc
->destination
= (struct prefix
*)p
;
311 /* validate the destination address */
312 if (CONNECTED_PEER(ifc
)) {
313 if (IPV4_ADDR_SAME(addr
, broad
))
315 EC_ZEBRA_IFACE_SAME_LOCAL_AS_PEER
,
316 "warning: interface %s has same local and peer "
317 "address %s, routing protocols may malfunction",
318 ifp
->name
, inet_ntoa(*addr
));
321 != ipv4_broadcast_addr(addr
->s_addr
, prefixlen
)) {
322 char buf
[2][INET_ADDRSTRLEN
];
323 struct in_addr bcalc
;
324 bcalc
.s_addr
= ipv4_broadcast_addr(addr
->s_addr
,
327 EC_ZEBRA_BCAST_ADDR_MISMATCH
,
328 "warning: interface %s broadcast addr %s/%d != "
329 "calculated %s, routing protocols may malfunction",
331 inet_ntop(AF_INET
, broad
, buf
[0],
334 inet_ntop(AF_INET
, &bcalc
, buf
[1],
340 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
342 "warning: %s called for interface %s "
343 "with peer flag set, but no peer address supplied",
344 __func__
, ifp
->name
);
345 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
348 /* no broadcast or destination address was supplied */
349 if ((prefixlen
== IPV4_MAX_PREFIXLEN
) && if_is_pointopoint(ifp
))
351 "warning: PtP interface %s with addr %s/%d needs a "
353 ifp
->name
, inet_ntoa(*addr
), prefixlen
);
356 /* Label of this address. */
358 ifc
->label
= XSTRDUP(MTYPE_CONNECTED_LABEL
, label
);
360 /* For all that I know an IPv4 address is always ready when we receive
361 * the notification. So it should be safe to set the REAL flag here. */
362 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
364 connected_update(ifp
, ifc
);
367 void connected_down(struct interface
*ifp
, struct connected
*ifc
)
371 struct nexthop nh
= {
372 .type
= NEXTHOP_TYPE_IFINDEX
,
373 .ifindex
= ifp
->ifindex
,
374 .vrf_id
= ifp
->vrf_id
,
377 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
380 PREFIX_COPY(&p
, CONNECTED_PREFIX(ifc
));
382 /* Apply mask to the network. */
385 afi
= family2afi(p
.family
);
390 * In case of connected address is 0.0.0.0/0 we treat it tunnel
393 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
397 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
401 zlog_info("Unknown AFI: %s", afi2str(afi
));
406 * Same logic as for connected_up(): push the changes into the
409 rib_delete(afi
, SAFI_UNICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0, 0,
410 &p
, NULL
, &nh
, 0, 0, 0, false);
412 rib_delete(afi
, SAFI_MULTICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0, 0,
413 &p
, NULL
, &nh
, 0, 0, 0, false);
415 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
416 char buf
[PREFIX_STRLEN
];
419 "%u: IF %s IP %s address down, scheduling RIB processing",
420 ifp
->vrf_id
, ifp
->name
,
421 prefix2str(&p
, buf
, sizeof(buf
)));
424 rib_update(ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
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 down, 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
));
440 static void connected_delete_helper(struct connected
*ifc
, struct prefix
*p
)
442 struct interface
*ifp
;
448 connected_withdraw(ifc
);
450 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
451 char buf
[PREFIX_STRLEN
];
454 "%u: IF %s IP %s address del, scheduling RIB processing",
455 ifp
->vrf_id
, ifp
->name
,
456 prefix2str(p
, buf
, sizeof(buf
)));
458 rib_update(ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
460 /* Schedule LSP forwarding entries for processing, if appropriate. */
461 if (ifp
->vrf_id
== VRF_DEFAULT
) {
462 if (IS_ZEBRA_DEBUG_MPLS
) {
463 char buf
[PREFIX_STRLEN
];
466 "%u: IF %s IP %s address delete, scheduling MPLS processing",
467 ifp
->vrf_id
, ifp
->name
,
468 prefix2str(p
, buf
, sizeof(buf
)));
470 mpls_mark_lsps_for_processing(vrf_info_lookup(ifp
->vrf_id
));
474 /* Delete connected IPv4 route to the interface. */
475 void connected_delete_ipv4(struct interface
*ifp
, int flags
,
476 struct in_addr
*addr
, uint8_t prefixlen
,
477 struct in_addr
*broad
)
480 struct connected
*ifc
;
482 memset(&p
, 0, sizeof(struct prefix
));
485 p
.prefixlen
= CHECK_FLAG(flags
, ZEBRA_IFA_PEER
) ? IPV4_MAX_PREFIXLEN
489 memset(&d
, 0, sizeof(struct prefix
));
491 d
.u
.prefix4
= *broad
;
492 d
.prefixlen
= prefixlen
;
493 ifc
= connected_check_ptp(ifp
, &p
, &d
);
495 ifc
= connected_check_ptp(ifp
, &p
, NULL
);
497 connected_delete_helper(ifc
, &p
);
500 /* Add connected IPv6 route to the interface. */
501 void connected_add_ipv6(struct interface
*ifp
, int flags
, struct in6_addr
*addr
,
502 struct in6_addr
*broad
, uint8_t prefixlen
,
505 struct prefix_ipv6
*p
;
506 struct connected
*ifc
;
508 if (ipv6_martian(addr
))
511 /* Make connected structure. */
512 ifc
= connected_new();
515 /* If we get a notification from the kernel,
516 * we can safely assume the address is known to the kernel */
517 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
519 /* Allocate new connected address. */
520 p
= prefix_ipv6_new();
521 p
->family
= AF_INET6
;
522 IPV6_ADDR_COPY(&p
->prefix
, addr
);
523 p
->prefixlen
= prefixlen
;
524 ifc
->address
= (struct prefix
*)p
;
527 p
= prefix_ipv6_new();
528 p
->family
= AF_INET6
;
529 IPV6_ADDR_COPY(&p
->prefix
, broad
);
530 p
->prefixlen
= prefixlen
;
531 ifc
->destination
= (struct prefix
*)p
;
533 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
535 "warning: %s called for interface %s with peer flag set, but no peer address supplied",
536 __func__
, ifp
->name
);
537 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
541 /* Label of this address. */
543 ifc
->label
= XSTRDUP(MTYPE_CONNECTED_LABEL
, label
);
545 /* On Linux, we only get here when DAD is complete, therefore we can set
548 * On BSD, there currently doesn't seem to be a way to check for
550 * DAD, so we replicate the old behaviour and set ZEBRA_IFC_REAL,
552 * might still be running.
554 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
555 connected_update(ifp
, ifc
);
558 void connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
,
559 struct in6_addr
*broad
, uint8_t prefixlen
)
562 struct connected
*ifc
;
564 memset(&p
, 0, sizeof(struct prefix
));
566 memcpy(&p
.u
.prefix6
, address
, sizeof(struct in6_addr
));
567 p
.prefixlen
= prefixlen
;
570 memset(&d
, 0, sizeof(struct prefix
));
572 IPV6_ADDR_COPY(&d
.u
.prefix6
, broad
);
573 d
.prefixlen
= prefixlen
;
574 ifc
= connected_check_ptp(ifp
, &p
, &d
);
576 ifc
= connected_check_ptp(ifp
, &p
, NULL
);
578 connected_delete_helper(ifc
, &p
);
581 int connected_is_unnumbered(struct interface
*ifp
)
583 struct connected
*connected
;
584 struct listnode
*node
;
586 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
587 if (CHECK_FLAG(connected
->conf
, ZEBRA_IFC_REAL
)
588 && connected
->address
->family
== AF_INET
)
589 return CHECK_FLAG(connected
->flags
,
590 ZEBRA_IFA_UNNUMBERED
);