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 !IS_ZEBRA_IF_VRF(ifp
)) {
78 if (ifc
->address
->prefixlen
== 32)
79 SET_FLAG(ifc
->flags
, ZEBRA_IFA_UNNUMBERED
);
81 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_UNNUMBERED
);
84 listnode_add(ifp
->connected
, ifc
);
86 /* Update interface address information to protocol daemon. */
87 if (ifc
->address
->family
== AF_INET
)
88 if_subnet_add(ifp
, ifc
);
90 zebra_interface_address_add_update(ifp
, ifc
);
92 if (if_is_operative(ifp
)) {
93 connected_up(ifp
, ifc
);
97 /* If same interface address is already exist... */
98 struct connected
*connected_check(struct interface
*ifp
,
99 union prefixconstptr pu
)
101 const struct prefix
*p
= pu
.p
;
102 struct connected
*ifc
;
103 struct listnode
*node
;
105 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
))
106 if (prefix_same(ifc
->address
, p
))
112 /* same, but with peer address */
113 struct connected
*connected_check_ptp(struct interface
*ifp
,
114 union prefixconstptr pu
,
115 union prefixconstptr du
)
117 const struct prefix
*p
= pu
.p
;
118 const struct prefix
*d
= du
.p
;
119 struct connected
*ifc
;
120 struct listnode
*node
;
122 /* ignore broadcast addresses */
123 if (p
->prefixlen
!= IPV4_MAX_PREFIXLEN
)
126 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, ifc
)) {
127 if (!prefix_same(ifc
->address
, p
))
129 if (!CONNECTED_PEER(ifc
) && !d
)
131 if (CONNECTED_PEER(ifc
) && d
132 && prefix_same(ifc
->destination
, d
))
139 /* Check if two ifc's describe the same address in the same state */
140 static int connected_same(struct connected
*ifc1
, struct connected
*ifc2
)
142 if (ifc1
->ifp
!= ifc2
->ifp
)
145 if (ifc1
->destination
)
146 if (!ifc2
->destination
)
148 if (ifc2
->destination
)
149 if (!ifc1
->destination
)
152 if (ifc1
->destination
&& ifc2
->destination
)
153 if (!prefix_same(ifc1
->destination
, ifc2
->destination
))
156 if (ifc1
->flags
!= ifc2
->flags
)
159 if (ifc1
->conf
!= ifc2
->conf
)
165 /* Handle changes to addresses and send the neccesary announcements
167 static void connected_update(struct interface
*ifp
, struct connected
*ifc
)
169 struct connected
*current
;
171 /* Check same connected route. */
172 current
= connected_check_ptp(ifp
, ifc
->address
, ifc
->destination
);
174 if (CHECK_FLAG(current
->conf
, ZEBRA_IFC_CONFIGURED
))
175 SET_FLAG(ifc
->conf
, ZEBRA_IFC_CONFIGURED
);
177 /* Avoid spurious withdraws, this might be just the kernel
179 * back an address we have already added.
181 if (connected_same(current
, ifc
)) {
187 /* Clear the configured flag on the old ifc, so it will be freed
189 * connected withdraw. */
190 UNSET_FLAG(current
->conf
, ZEBRA_IFC_CONFIGURED
);
192 current
); /* implicit withdraw - freebsd does this */
195 /* If the connected is new or has changed, announce it, if it is usable
197 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
198 connected_announce(ifp
, ifc
);
201 /* Called from if_up(). */
202 void connected_up(struct interface
*ifp
, struct connected
*ifc
)
206 struct nexthop nh
= {
207 .type
= NEXTHOP_TYPE_IFINDEX
,
208 .ifindex
= ifp
->ifindex
,
209 .vrf_id
= ifp
->vrf_id
,
212 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
215 PREFIX_COPY(&p
, CONNECTED_PREFIX(ifc
));
217 /* Apply mask to the network. */
220 afi
= family2afi(p
.family
);
225 * In case of connected address is 0.0.0.0/0 we treat it tunnel
228 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
233 /* XXX: It is already done by rib_bogus_ipv6 within rib_add */
234 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
239 zlog_warn("Received unknown AFI: %s", afi2str(afi
));
244 rib_add(afi
, SAFI_UNICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0, 0, &p
,
245 NULL
, &nh
, RT_TABLE_MAIN
, ifp
->metric
, 0, 0, 0);
247 rib_add(afi
, SAFI_MULTICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0, 0, &p
,
248 NULL
, &nh
, RT_TABLE_MAIN
, ifp
->metric
, 0, 0, 0);
250 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
251 char buf
[PREFIX_STRLEN
];
254 "%u: IF %s address %s add/up, scheduling RIB processing",
255 ifp
->vrf_id
, ifp
->name
,
256 prefix2str(&p
, buf
, sizeof(buf
)));
258 rib_update(ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
260 /* Schedule LSP forwarding entries for processing, if appropriate. */
261 if (ifp
->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 ifp
->vrf_id
, ifp
->name
,
268 prefix2str(&p
, buf
, sizeof(buf
)));
270 mpls_mark_lsps_for_processing(vrf_info_lookup(ifp
->vrf_id
));
274 /* Add connected IPv4 route to the interface. */
275 void connected_add_ipv4(struct interface
*ifp
, int flags
, struct in_addr
*addr
,
276 uint8_t prefixlen
, struct in_addr
*broad
,
279 struct prefix_ipv4
*p
;
280 struct connected
*ifc
;
282 if (ipv4_martian(addr
))
285 /* Make connected structure. */
286 ifc
= connected_new();
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 broadcast or peer address. */
303 p
= prefix_ipv4_new();
306 p
->prefixlen
= prefixlen
;
307 ifc
->destination
= (struct prefix
*)p
;
309 /* validate the destination address */
310 if (CONNECTED_PEER(ifc
)) {
311 if (IPV4_ADDR_SAME(addr
, broad
))
313 "warning: interface %s has same local and peer "
314 "address %s, routing protocols may malfunction",
315 ifp
->name
, inet_ntoa(*addr
));
318 != ipv4_broadcast_addr(addr
->s_addr
, prefixlen
)) {
319 char buf
[2][INET_ADDRSTRLEN
];
320 struct in_addr bcalc
;
321 bcalc
.s_addr
= ipv4_broadcast_addr(addr
->s_addr
,
324 "warning: interface %s broadcast addr %s/%d != "
325 "calculated %s, routing protocols may malfunction",
327 inet_ntop(AF_INET
, broad
, buf
[0],
330 inet_ntop(AF_INET
, &bcalc
, buf
[1],
336 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
338 "warning: %s called for interface %s "
339 "with peer flag set, but no peer address supplied",
340 __func__
, ifp
->name
);
341 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
344 /* no broadcast or destination address was supplied */
345 if ((prefixlen
== IPV4_MAX_PREFIXLEN
) && if_is_pointopoint(ifp
))
347 "warning: PtP interface %s with addr %s/%d needs a "
349 ifp
->name
, inet_ntoa(*addr
), prefixlen
);
352 /* Label of this address. */
354 ifc
->label
= XSTRDUP(MTYPE_CONNECTED_LABEL
, label
);
356 /* For all that I know an IPv4 address is always ready when we receive
357 * the notification. So it should be safe to set the REAL flag here. */
358 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
360 connected_update(ifp
, ifc
);
363 void connected_down(struct interface
*ifp
, struct connected
*ifc
)
367 struct nexthop nh
= {
368 .type
= NEXTHOP_TYPE_IFINDEX
,
369 .ifindex
= ifp
->ifindex
,
370 .vrf_id
= ifp
->vrf_id
,
373 if (!CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
376 PREFIX_COPY(&p
, CONNECTED_PREFIX(ifc
));
378 /* Apply mask to the network. */
381 afi
= family2afi(p
.family
);
386 * In case of connected address is 0.0.0.0/0 we treat it tunnel
389 if (prefix_ipv4_any((struct prefix_ipv4
*)&p
))
393 if (IN6_IS_ADDR_UNSPECIFIED(&p
.u
.prefix6
))
397 zlog_info("Unknown AFI: %s", afi2str(afi
));
402 * Same logic as for connected_up(): push the changes into the
405 rib_delete(afi
, SAFI_UNICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0, 0,
406 &p
, NULL
, &nh
, 0, 0, false);
408 rib_delete(afi
, SAFI_MULTICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
, 0, 0,
409 &p
, NULL
, &nh
, 0, 0, false);
411 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
412 char buf
[PREFIX_STRLEN
];
415 "%u: IF %s IP %s address down, scheduling RIB processing",
416 ifp
->vrf_id
, ifp
->name
,
417 prefix2str(&p
, buf
, sizeof(buf
)));
420 rib_update(ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
422 /* Schedule LSP forwarding entries for processing, if appropriate. */
423 if (ifp
->vrf_id
== VRF_DEFAULT
) {
424 if (IS_ZEBRA_DEBUG_MPLS
) {
425 char buf
[PREFIX_STRLEN
];
428 "%u: IF %s IP %s address down, scheduling MPLS processing",
429 ifp
->vrf_id
, ifp
->name
,
430 prefix2str(&p
, buf
, sizeof(buf
)));
432 mpls_mark_lsps_for_processing(vrf_info_lookup(ifp
->vrf_id
));
436 static void connected_delete_helper(struct connected
*ifc
, struct prefix
*p
)
438 struct interface
*ifp
;
444 connected_withdraw(ifc
);
446 if (IS_ZEBRA_DEBUG_RIB_DETAILED
) {
447 char buf
[PREFIX_STRLEN
];
450 "%u: IF %s IP %s address del, scheduling RIB processing",
451 ifp
->vrf_id
, ifp
->name
,
452 prefix2str(p
, buf
, sizeof(buf
)));
454 rib_update(ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
456 /* Schedule LSP forwarding entries for processing, if appropriate. */
457 if (ifp
->vrf_id
== VRF_DEFAULT
) {
458 if (IS_ZEBRA_DEBUG_MPLS
) {
459 char buf
[PREFIX_STRLEN
];
462 "%u: IF %s IP %s address delete, scheduling MPLS processing",
463 ifp
->vrf_id
, ifp
->name
,
464 prefix2str(p
, buf
, sizeof(buf
)));
466 mpls_mark_lsps_for_processing(vrf_info_lookup(ifp
->vrf_id
));
470 /* Delete connected IPv4 route to the interface. */
471 void connected_delete_ipv4(struct interface
*ifp
, int flags
,
472 struct in_addr
*addr
, uint8_t prefixlen
,
473 struct in_addr
*broad
)
476 struct connected
*ifc
;
478 memset(&p
, 0, sizeof(struct prefix
));
481 p
.prefixlen
= CHECK_FLAG(flags
, ZEBRA_IFA_PEER
) ? IPV4_MAX_PREFIXLEN
485 memset(&d
, 0, sizeof(struct prefix
));
487 d
.u
.prefix4
= *broad
;
488 d
.prefixlen
= prefixlen
;
489 ifc
= connected_check_ptp(ifp
, &p
, &d
);
491 ifc
= connected_check_ptp(ifp
, &p
, NULL
);
493 connected_delete_helper(ifc
, &p
);
496 /* Add connected IPv6 route to the interface. */
497 void connected_add_ipv6(struct interface
*ifp
, int flags
, struct in6_addr
*addr
,
498 struct in6_addr
*broad
, uint8_t prefixlen
,
501 struct prefix_ipv6
*p
;
502 struct connected
*ifc
;
504 if (ipv6_martian(addr
))
507 /* Make connected structure. */
508 ifc
= connected_new();
511 /* If we get a notification from the kernel,
512 * we can safely assume the address is known to the kernel */
513 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
515 /* Allocate new connected address. */
516 p
= prefix_ipv6_new();
517 p
->family
= AF_INET6
;
518 IPV6_ADDR_COPY(&p
->prefix
, addr
);
519 p
->prefixlen
= prefixlen
;
520 ifc
->address
= (struct prefix
*)p
;
523 p
= prefix_ipv6_new();
524 p
->family
= AF_INET6
;
525 IPV6_ADDR_COPY(&p
->prefix
, broad
);
526 p
->prefixlen
= prefixlen
;
527 ifc
->destination
= (struct prefix
*)p
;
529 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
)) {
530 zlog_warn("warning: %s called for interface %s with peer flag set, but no peer address supplied",
531 __func__
, ifp
->name
);
532 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
536 /* Label of this address. */
538 ifc
->label
= XSTRDUP(MTYPE_CONNECTED_LABEL
, label
);
540 /* On Linux, we only get here when DAD is complete, therefore we can set
543 * On BSD, there currently doesn't seem to be a way to check for
545 * DAD, so we replicate the old behaviour and set ZEBRA_IFC_REAL,
547 * might still be running.
549 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
550 connected_update(ifp
, ifc
);
553 void connected_delete_ipv6(struct interface
*ifp
, struct in6_addr
*address
,
554 struct in6_addr
*broad
, uint8_t prefixlen
)
557 struct connected
*ifc
;
559 memset(&p
, 0, sizeof(struct prefix
));
561 memcpy(&p
.u
.prefix6
, address
, sizeof(struct in6_addr
));
562 p
.prefixlen
= prefixlen
;
565 memset(&d
, 0, sizeof(struct prefix
));
567 IPV6_ADDR_COPY(&d
.u
.prefix
, broad
);
568 d
.prefixlen
= prefixlen
;
569 ifc
= connected_check_ptp(ifp
, &p
, &d
);
571 ifc
= connected_check_ptp(ifp
, &p
, NULL
);
573 connected_delete_helper(ifc
, &p
);
576 int connected_is_unnumbered(struct interface
*ifp
)
578 struct connected
*connected
;
579 struct listnode
*node
;
581 for (ALL_LIST_ELEMENTS_RO(ifp
->connected
, node
, connected
)) {
582 if (CHECK_FLAG(connected
->conf
, ZEBRA_IFC_REAL
)
583 && connected
->address
->family
== AF_INET
)
584 return CHECK_FLAG(connected
->flags
,
585 ZEBRA_IFA_UNNUMBERED
);