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 */
46 connected_withdraw (struct connected
*ifc
)
51 /* Update interface address information to protocol daemon. */
52 if (CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
54 zebra_interface_address_delete_update (ifc
->ifp
, ifc
);
56 if (ifc
->address
->family
== AF_INET
)
57 if_subnet_delete (ifc
->ifp
, ifc
);
59 if (ifc
->address
->family
== AF_INET
)
60 connected_down_ipv4 (ifc
->ifp
, ifc
);
62 connected_down_ipv6 (ifc
->ifp
, ifc
);
64 UNSET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
67 /* The address is not in the kernel anymore, so clear the flag */
68 UNSET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
70 if (!CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
72 listnode_delete (ifc
->ifp
->connected
, ifc
);
78 connected_announce (struct interface
*ifp
, struct connected
*ifc
)
83 if (!if_is_loopback(ifp
) && ifc
->address
->family
== AF_INET
)
85 if (ifc
->address
->prefixlen
== 32)
86 SET_FLAG (ifc
->flags
, ZEBRA_IFA_UNNUMBERED
);
88 UNSET_FLAG (ifc
->flags
, ZEBRA_IFA_UNNUMBERED
);
91 listnode_add (ifp
->connected
, ifc
);
93 /* Update interface address information to protocol daemon. */
94 if (ifc
->address
->family
== AF_INET
)
95 if_subnet_add (ifp
, ifc
);
97 zebra_interface_address_add_update (ifp
, ifc
);
99 if (if_is_operative(ifp
))
101 if (ifc
->address
->family
== AF_INET
)
102 connected_up_ipv4 (ifp
, ifc
);
104 connected_up_ipv6 (ifp
, ifc
);
108 /* If same interface address is already exist... */
110 connected_check (struct interface
*ifp
, struct prefix
*p
)
112 struct connected
*ifc
;
113 struct listnode
*node
;
115 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
116 if (prefix_same (ifc
->address
, p
))
122 /* Check if two ifc's describe the same address in the same state */
124 connected_same (struct connected
*ifc1
, struct connected
*ifc2
)
126 if (ifc1
->ifp
!= ifc2
->ifp
)
129 if (ifc1
->destination
)
130 if (!ifc2
->destination
)
132 if (ifc2
->destination
)
133 if (!ifc1
->destination
)
136 if (ifc1
->destination
&& ifc2
->destination
)
137 if (!prefix_same (ifc1
->destination
, ifc2
->destination
))
140 if (ifc1
->flags
!= ifc2
->flags
)
143 if (ifc1
->conf
!= ifc2
->conf
)
149 /* Handle changes to addresses and send the neccesary announcements
152 connected_update(struct interface
*ifp
, struct connected
*ifc
)
154 struct connected
*current
;
156 /* Check same connected route. */
157 if ((current
= connected_check (ifp
, (struct prefix
*) ifc
->address
)))
159 if (CHECK_FLAG(current
->conf
, ZEBRA_IFC_CONFIGURED
))
160 SET_FLAG(ifc
->conf
, ZEBRA_IFC_CONFIGURED
);
162 /* Avoid spurious withdraws, this might be just the kernel 'reflecting'
163 * back an address we have already added.
165 if (connected_same (current
, ifc
))
168 connected_free (ifc
);
172 /* Clear the configured flag on the old ifc, so it will be freed by
173 * connected withdraw. */
174 UNSET_FLAG(current
->conf
, ZEBRA_IFC_CONFIGURED
);
175 connected_withdraw (current
); /* implicit withdraw - freebsd does this */
178 /* If the connected is new or has changed, announce it, if it is usable */
179 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
180 connected_announce(ifp
, ifc
);
183 /* Called from if_up(). */
185 connected_up_ipv4 (struct interface
*ifp
, struct connected
*ifc
)
189 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
192 PREFIX_COPY_IPV4((struct prefix_ipv4
*)&p
, CONNECTED_PREFIX(ifc
));
194 /* Apply mask to the network. */
197 /* In case of connected address is 0.0.0.0/0 we treat it tunnel
199 if (prefix_ipv4_any ((struct prefix_ipv4
*)&p
))
202 rib_add (AFI_IP
, SAFI_UNICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
203 0, 0, &p
, NULL
, NULL
, NULL
, ifp
->ifindex
,
204 RT_TABLE_MAIN
, ifp
->metric
, 0, 0);
206 rib_add (AFI_IP
, SAFI_MULTICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
207 0, 0, &p
, NULL
, NULL
, NULL
, ifp
->ifindex
,
208 RT_TABLE_MAIN
, ifp
->metric
, 0, 0);
210 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
211 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling RIB processing",
212 ifp
->vrf_id
, ifp
->name
);
213 rib_update (ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
215 /* Schedule LSP forwarding entries for processing, if appropriate. */
216 if (ifp
->vrf_id
== VRF_DEFAULT
)
218 if (IS_ZEBRA_DEBUG_MPLS
)
219 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
220 ifp
->vrf_id
, ifp
->name
);
221 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp
->vrf_id
));
225 /* Add connected IPv4 route to the interface. */
227 connected_add_ipv4 (struct interface
*ifp
, int flags
, struct in_addr
*addr
,
228 u_char prefixlen
, struct in_addr
*broad
,
231 struct prefix_ipv4
*p
;
232 struct connected
*ifc
;
234 if (ipv4_martian(addr
))
237 /* Make connected structure. */
238 ifc
= connected_new ();
241 /* If we get a notification from the kernel,
242 * we can safely assume the address is known to the kernel */
243 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
245 /* Allocate new connected address. */
246 p
= prefix_ipv4_new ();
249 p
->prefixlen
= prefixlen
;
250 ifc
->address
= (struct prefix
*) p
;
252 /* If there is broadcast or peer address. */
255 p
= prefix_ipv4_new ();
258 p
->prefixlen
= prefixlen
;
259 ifc
->destination
= (struct prefix
*) p
;
261 /* validate the destination address */
262 if (CONNECTED_PEER(ifc
))
264 if (IPV4_ADDR_SAME(addr
,broad
))
265 zlog_warn("warning: interface %s has same local and peer "
266 "address %s, routing protocols may malfunction",
267 ifp
->name
,inet_ntoa(*addr
));
271 if (broad
->s_addr
!= ipv4_broadcast_addr(addr
->s_addr
,prefixlen
))
273 char buf
[2][INET_ADDRSTRLEN
];
274 struct in_addr bcalc
;
275 bcalc
.s_addr
= ipv4_broadcast_addr(addr
->s_addr
,prefixlen
);
276 zlog_warn("warning: interface %s broadcast addr %s/%d != "
277 "calculated %s, routing protocols may malfunction",
279 inet_ntop (AF_INET
, broad
, buf
[0], sizeof(buf
[0])),
281 inet_ntop (AF_INET
, &bcalc
, buf
[1], sizeof(buf
[1])));
288 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
))
290 zlog_warn("warning: %s called for interface %s "
291 "with peer flag set, but no peer address supplied",
292 __func__
, ifp
->name
);
293 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
296 /* no broadcast or destination address was supplied */
297 if ((prefixlen
== IPV4_MAX_PREFIXLEN
) && if_is_pointopoint(ifp
))
298 zlog_warn("warning: PtP interface %s with addr %s/%d needs a "
299 "peer address",ifp
->name
,inet_ntoa(*addr
),prefixlen
);
302 /* Label of this address. */
304 ifc
->label
= XSTRDUP (MTYPE_CONNECTED_LABEL
, label
);
306 /* For all that I know an IPv4 address is always ready when we receive
307 * the notification. So it should be safe to set the REAL flag here. */
308 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
310 connected_update(ifp
, ifc
);
314 connected_down_ipv4 (struct interface
*ifp
, struct connected
*ifc
)
318 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
321 PREFIX_COPY_IPV4(&p
, CONNECTED_PREFIX(ifc
));
323 /* Apply mask to the network. */
326 /* In case of connected address is 0.0.0.0/0 we treat it tunnel
328 if (prefix_ipv4_any ((struct prefix_ipv4
*)&p
))
331 /* Same logic as for connected_up_ipv4(): push the changes into the head. */
332 rib_delete (AFI_IP
, SAFI_UNICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
333 0, 0, &p
, NULL
, NULL
, ifp
->ifindex
, 0);
335 rib_delete (AFI_IP
, SAFI_MULTICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
336 0, 0, &p
, NULL
, NULL
, ifp
->ifindex
, 0);
338 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
339 zlog_debug ("%u: IF %s IPv4 address down, scheduling RIB processing",
340 ifp
->vrf_id
, ifp
->name
);
342 rib_update (ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
344 /* Schedule LSP forwarding entries for processing, if appropriate. */
345 if (ifp
->vrf_id
== VRF_DEFAULT
)
347 if (IS_ZEBRA_DEBUG_MPLS
)
348 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
349 ifp
->vrf_id
, ifp
->name
);
350 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp
->vrf_id
));
354 /* Delete connected IPv4 route to the interface. */
356 connected_delete_ipv4 (struct interface
*ifp
, int flags
, struct in_addr
*addr
,
357 u_char prefixlen
, struct in_addr
*broad
)
359 struct prefix_ipv4 p
;
360 struct connected
*ifc
;
362 memset (&p
, 0, sizeof (struct prefix_ipv4
));
365 p
.prefixlen
= prefixlen
;
367 ifc
= connected_check (ifp
, (struct prefix
*) &p
);
371 connected_withdraw (ifc
);
373 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
374 zlog_debug ("%u: IF %s IPv4 address del, scheduling RIB processing",
375 ifp
->vrf_id
, ifp
->name
);
377 rib_update (ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
379 /* Schedule LSP forwarding entries for processing, if appropriate. */
380 if (ifp
->vrf_id
== VRF_DEFAULT
)
382 if (IS_ZEBRA_DEBUG_MPLS
)
383 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
384 ifp
->vrf_id
, ifp
->name
);
385 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp
->vrf_id
));
390 connected_up_ipv6 (struct interface
*ifp
, struct connected
*ifc
)
394 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
397 PREFIX_COPY_IPV6((struct prefix_ipv6
*)&p
, CONNECTED_PREFIX(ifc
));
399 /* Apply mask to the network. */
403 /* XXX: It is already done by rib_bogus_ipv6 within rib_add */
404 if (IN6_IS_ADDR_UNSPECIFIED (&p
.u
.prefix6
))
408 rib_add (AFI_IP6
, SAFI_UNICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
409 0, 0, &p
, NULL
, NULL
, NULL
, ifp
->ifindex
,
410 RT_TABLE_MAIN
, ifp
->metric
, 0, 0);
412 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
413 zlog_debug ("%u: IF %s IPv6 address down, scheduling RIB processing",
414 ifp
->vrf_id
, ifp
->name
);
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
)
421 if (IS_ZEBRA_DEBUG_MPLS
)
422 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
423 ifp
->vrf_id
, ifp
->name
);
424 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp
->vrf_id
));
428 /* Add connected IPv6 route to the interface. */
430 connected_add_ipv6 (struct interface
*ifp
, int flags
, struct in6_addr
*addr
,
431 u_char prefixlen
, struct in6_addr
*broad
,
434 struct prefix_ipv6
*p
;
435 struct connected
*ifc
;
437 if (ipv6_martian(addr
))
440 /* Make connected structure. */
441 ifc
= connected_new ();
444 /* If we get a notification from the kernel,
445 * we can safely assume the address is known to the kernel */
446 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
448 /* Allocate new connected address. */
449 p
= prefix_ipv6_new ();
450 p
->family
= AF_INET6
;
451 IPV6_ADDR_COPY (&p
->prefix
, addr
);
452 p
->prefixlen
= prefixlen
;
453 ifc
->address
= (struct prefix
*) p
;
455 /* If there is broadcast or peer address. */
458 if (IN6_IS_ADDR_UNSPECIFIED(broad
))
459 zlog_warn("warning: %s called for interface %s with unspecified "
460 "destination address; ignoring!", __func__
, ifp
->name
);
463 p
= prefix_ipv6_new ();
464 p
->family
= AF_INET6
;
465 IPV6_ADDR_COPY (&p
->prefix
, broad
);
466 p
->prefixlen
= prefixlen
;
467 ifc
->destination
= (struct prefix
*) p
;
470 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
) && !ifc
->destination
)
472 zlog_warn("warning: %s called for interface %s "
473 "with peer flag set, but no peer address supplied",
474 __func__
, ifp
->name
);
475 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
478 /* Label of this address. */
480 ifc
->label
= XSTRDUP (MTYPE_CONNECTED_LABEL
, label
);
482 /* On Linux, we only get here when DAD is complete, therefore we can set
485 * On BSD, there currently doesn't seem to be a way to check for completion of
486 * DAD, so we replicate the old behaviour and set ZEBRA_IFC_REAL, although DAD
487 * might still be running.
489 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
490 connected_update(ifp
, ifc
);
494 connected_down_ipv6 (struct interface
*ifp
, struct connected
*ifc
)
498 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
501 PREFIX_COPY_IPV6(&p
, CONNECTED_PREFIX(ifc
));
505 if (IN6_IS_ADDR_UNSPECIFIED (&p
.u
.prefix6
))
508 rib_delete (AFI_IP6
, SAFI_UNICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
509 0, 0, &p
, NULL
, NULL
, ifp
->ifindex
, 0);
511 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
512 zlog_debug ("%u: IF %s IPv6 address down, scheduling RIB processing",
513 ifp
->vrf_id
, ifp
->name
);
515 rib_update (ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
517 /* Schedule LSP forwarding entries for processing, if appropriate. */
518 if (ifp
->vrf_id
== VRF_DEFAULT
)
520 if (IS_ZEBRA_DEBUG_MPLS
)
521 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
522 ifp
->vrf_id
, ifp
->name
);
523 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp
->vrf_id
));
528 connected_delete_ipv6 (struct interface
*ifp
, struct in6_addr
*address
,
529 u_char prefixlen
, struct in6_addr
*broad
)
531 struct prefix_ipv6 p
;
532 struct connected
*ifc
;
534 memset (&p
, 0, sizeof (struct prefix_ipv6
));
536 memcpy (&p
.prefix
, address
, sizeof (struct in6_addr
));
537 p
.prefixlen
= prefixlen
;
539 ifc
= connected_check (ifp
, (struct prefix
*) &p
);
543 connected_withdraw (ifc
);
545 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
546 zlog_debug ("%u: IF %s IPv6 address del, scheduling RIB processing",
547 ifp
->vrf_id
, ifp
->name
);
549 rib_update (ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
551 /* Schedule LSP forwarding entries for processing, if appropriate. */
552 if (ifp
->vrf_id
== VRF_DEFAULT
)
554 if (IS_ZEBRA_DEBUG_MPLS
)
555 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
556 ifp
->vrf_id
, ifp
->name
);
557 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp
->vrf_id
));
562 connected_is_unnumbered (struct interface
*ifp
)
564 struct connected
*connected
;
565 struct listnode
*node
;
567 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, connected
))
569 if (CHECK_FLAG (connected
->conf
, ZEBRA_IFC_REAL
) &&
570 connected
->address
->family
== AF_INET
)
571 return CHECK_FLAG(connected
->flags
, ZEBRA_IFA_UNNUMBERED
);