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
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
33 #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
);
63 connected_down_ipv6 (ifc
->ifp
, ifc
);
66 UNSET_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
);
69 /* The address is not in the kernel anymore, so clear the flag */
70 UNSET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
72 if (!CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_CONFIGURED
))
74 listnode_delete (ifc
->ifp
->connected
, ifc
);
80 connected_announce (struct interface
*ifp
, struct connected
*ifc
)
85 if (!if_is_loopback(ifp
) && ifc
->address
->family
== AF_INET
)
87 if (ifc
->address
->prefixlen
== 32)
88 SET_FLAG (ifc
->flags
, ZEBRA_IFA_UNNUMBERED
);
90 UNSET_FLAG (ifc
->flags
, ZEBRA_IFA_UNNUMBERED
);
93 listnode_add (ifp
->connected
, ifc
);
95 /* Update interface address information to protocol daemon. */
96 if (ifc
->address
->family
== AF_INET
)
97 if_subnet_add (ifp
, ifc
);
99 zebra_interface_address_add_update (ifp
, ifc
);
101 if (if_is_operative(ifp
))
103 if (ifc
->address
->family
== AF_INET
)
104 connected_up_ipv4 (ifp
, ifc
);
107 connected_up_ipv6 (ifp
, ifc
);
112 /* If same interface address is already exist... */
114 connected_check (struct interface
*ifp
, struct prefix
*p
)
116 struct connected
*ifc
;
117 struct listnode
*node
;
119 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, ifc
))
120 if (prefix_same (ifc
->address
, p
))
126 /* Check if two ifc's describe the same address in the same state */
128 connected_same (struct connected
*ifc1
, struct connected
*ifc2
)
130 if (ifc1
->ifp
!= ifc2
->ifp
)
133 if (ifc1
->destination
)
134 if (!ifc2
->destination
)
136 if (ifc2
->destination
)
137 if (!ifc1
->destination
)
140 if (ifc1
->destination
&& ifc2
->destination
)
141 if (!prefix_same (ifc1
->destination
, ifc2
->destination
))
144 if (ifc1
->flags
!= ifc2
->flags
)
147 if (ifc1
->conf
!= ifc2
->conf
)
153 /* Handle changes to addresses and send the neccesary announcements
156 connected_update(struct interface
*ifp
, struct connected
*ifc
)
158 struct connected
*current
;
160 /* Check same connected route. */
161 if ((current
= connected_check (ifp
, (struct prefix
*) ifc
->address
)))
163 if (CHECK_FLAG(current
->conf
, ZEBRA_IFC_CONFIGURED
))
164 SET_FLAG(ifc
->conf
, ZEBRA_IFC_CONFIGURED
);
166 /* Avoid spurious withdraws, this might be just the kernel 'reflecting'
167 * back an address we have already added.
169 if (connected_same (current
, ifc
))
172 connected_free (ifc
);
176 /* Clear the configured flag on the old ifc, so it will be freed by
177 * connected withdraw. */
178 UNSET_FLAG(current
->conf
, ZEBRA_IFC_CONFIGURED
);
179 connected_withdraw (current
); /* implicit withdraw - freebsd does this */
182 /* If the connected is new or has changed, announce it, if it is usable */
183 if (CHECK_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
))
184 connected_announce(ifp
, ifc
);
187 /* Called from if_up(). */
189 connected_up_ipv4 (struct interface
*ifp
, struct connected
*ifc
)
193 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
196 PREFIX_COPY_IPV4((struct prefix_ipv4
*)&p
, CONNECTED_PREFIX(ifc
));
198 /* Apply mask to the network. */
201 /* In case of connected address is 0.0.0.0/0 we treat it tunnel
203 if (prefix_ipv4_any ((struct prefix_ipv4
*)&p
))
206 rib_add (AFI_IP
, SAFI_UNICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
207 0, 0, &p
, NULL
, NULL
, ifp
->ifindex
,
208 RT_TABLE_MAIN
, ifp
->metric
, 0, 0);
210 rib_add (AFI_IP
, SAFI_MULTICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
211 0, 0, &p
, NULL
, NULL
, ifp
->ifindex
,
212 RT_TABLE_MAIN
, ifp
->metric
, 0, 0);
214 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
215 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling RIB processing",
216 ifp
->vrf_id
, ifp
->name
);
217 rib_update (ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
219 /* Schedule LSP forwarding entries for processing, if appropriate. */
220 if (ifp
->vrf_id
== VRF_DEFAULT
)
222 if (IS_ZEBRA_DEBUG_MPLS
)
223 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
224 ifp
->vrf_id
, ifp
->name
);
225 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp
->vrf_id
));
229 /* Add connected IPv4 route to the interface. */
231 connected_add_ipv4 (struct interface
*ifp
, int flags
, struct in_addr
*addr
,
232 u_char prefixlen
, struct in_addr
*broad
,
235 struct prefix_ipv4
*p
;
236 struct connected
*ifc
;
238 if (ipv4_martian(addr
))
241 /* Make connected structure. */
242 ifc
= connected_new ();
245 /* If we get a notification from the kernel,
246 * we can safely assume the address is known to the kernel */
247 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
249 /* Allocate new connected address. */
250 p
= prefix_ipv4_new ();
253 p
->prefixlen
= prefixlen
;
254 ifc
->address
= (struct prefix
*) p
;
256 /* If there is broadcast or peer address. */
259 p
= prefix_ipv4_new ();
262 p
->prefixlen
= prefixlen
;
263 ifc
->destination
= (struct prefix
*) p
;
265 /* validate the destination address */
266 if (CONNECTED_PEER(ifc
))
268 if (IPV4_ADDR_SAME(addr
,broad
))
269 zlog_warn("warning: interface %s has same local and peer "
270 "address %s, routing protocols may malfunction",
271 ifp
->name
,inet_ntoa(*addr
));
275 if (broad
->s_addr
!= ipv4_broadcast_addr(addr
->s_addr
,prefixlen
))
277 char buf
[2][INET_ADDRSTRLEN
];
278 struct in_addr bcalc
;
279 bcalc
.s_addr
= ipv4_broadcast_addr(addr
->s_addr
,prefixlen
);
280 zlog_warn("warning: interface %s broadcast addr %s/%d != "
281 "calculated %s, routing protocols may malfunction",
283 inet_ntop (AF_INET
, broad
, buf
[0], sizeof(buf
[0])),
285 inet_ntop (AF_INET
, &bcalc
, buf
[1], sizeof(buf
[1])));
292 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
))
294 zlog_warn("warning: %s called for interface %s "
295 "with peer flag set, but no peer address supplied",
296 __func__
, ifp
->name
);
297 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
300 /* no broadcast or destination address was supplied */
301 if ((prefixlen
== IPV4_MAX_PREFIXLEN
) && if_is_pointopoint(ifp
))
302 zlog_warn("warning: PtP interface %s with addr %s/%d needs a "
303 "peer address",ifp
->name
,inet_ntoa(*addr
),prefixlen
);
306 /* Label of this address. */
308 ifc
->label
= XSTRDUP (MTYPE_CONNECTED_LABEL
, label
);
310 /* For all that I know an IPv4 address is always ready when we receive
311 * the notification. So it should be safe to set the REAL flag here. */
312 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
314 connected_update(ifp
, ifc
);
318 connected_down_ipv4 (struct interface
*ifp
, struct connected
*ifc
)
322 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
325 PREFIX_COPY_IPV4(&p
, CONNECTED_PREFIX(ifc
));
327 /* Apply mask to the network. */
330 /* In case of connected address is 0.0.0.0/0 we treat it tunnel
332 if (prefix_ipv4_any ((struct prefix_ipv4
*)&p
))
335 /* Same logic as for connected_up_ipv4(): push the changes into the head. */
336 rib_delete (AFI_IP
, SAFI_UNICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
337 0, 0, &p
, NULL
, ifp
->ifindex
, 0);
339 rib_delete (AFI_IP
, SAFI_MULTICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
340 0, 0, &p
, NULL
, ifp
->ifindex
, 0);
342 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
343 zlog_debug ("%u: IF %s IPv4 address down, scheduling RIB processing",
344 ifp
->vrf_id
, ifp
->name
);
346 rib_update (ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
348 /* Schedule LSP forwarding entries for processing, if appropriate. */
349 if (ifp
->vrf_id
== VRF_DEFAULT
)
351 if (IS_ZEBRA_DEBUG_MPLS
)
352 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
353 ifp
->vrf_id
, ifp
->name
);
354 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp
->vrf_id
));
358 /* Delete connected IPv4 route to the interface. */
360 connected_delete_ipv4 (struct interface
*ifp
, int flags
, struct in_addr
*addr
,
361 u_char prefixlen
, struct in_addr
*broad
)
363 struct prefix_ipv4 p
;
364 struct connected
*ifc
;
366 memset (&p
, 0, sizeof (struct prefix_ipv4
));
369 p
.prefixlen
= prefixlen
;
371 ifc
= connected_check (ifp
, (struct prefix
*) &p
);
375 connected_withdraw (ifc
);
377 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
378 zlog_debug ("%u: IF %s IPv4 address del, scheduling RIB processing",
379 ifp
->vrf_id
, ifp
->name
);
381 rib_update (ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
383 /* Schedule LSP forwarding entries for processing, if appropriate. */
384 if (ifp
->vrf_id
== VRF_DEFAULT
)
386 if (IS_ZEBRA_DEBUG_MPLS
)
387 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
388 ifp
->vrf_id
, ifp
->name
);
389 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp
->vrf_id
));
394 connected_up_ipv6 (struct interface
*ifp
, struct connected
*ifc
)
398 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
401 PREFIX_COPY_IPV6((struct prefix_ipv6
*)&p
, CONNECTED_PREFIX(ifc
));
403 /* Apply mask to the network. */
407 /* XXX: It is already done by rib_bogus_ipv6 within rib_add */
408 if (IN6_IS_ADDR_UNSPECIFIED (&p
.u
.prefix6
))
412 rib_add (AFI_IP6
, SAFI_UNICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
413 0, 0, &p
, NULL
, NULL
, ifp
->ifindex
,
414 RT_TABLE_MAIN
, ifp
->metric
, 0, 0);
416 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
417 zlog_debug ("%u: IF %s IPv6 address down, scheduling RIB processing",
418 ifp
->vrf_id
, ifp
->name
);
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
)
425 if (IS_ZEBRA_DEBUG_MPLS
)
426 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
427 ifp
->vrf_id
, ifp
->name
);
428 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp
->vrf_id
));
432 /* Add connected IPv6 route to the interface. */
434 connected_add_ipv6 (struct interface
*ifp
, int flags
, struct in6_addr
*addr
,
435 u_char prefixlen
, struct in6_addr
*broad
,
438 struct prefix_ipv6
*p
;
439 struct connected
*ifc
;
441 if (ipv6_martian(addr
))
444 /* Make connected structure. */
445 ifc
= connected_new ();
448 /* If we get a notification from the kernel,
449 * we can safely assume the address is known to the kernel */
450 SET_FLAG(ifc
->conf
, ZEBRA_IFC_QUEUED
);
452 /* Allocate new connected address. */
453 p
= prefix_ipv6_new ();
454 p
->family
= AF_INET6
;
455 IPV6_ADDR_COPY (&p
->prefix
, addr
);
456 p
->prefixlen
= prefixlen
;
457 ifc
->address
= (struct prefix
*) p
;
459 /* If there is broadcast or peer address. */
462 if (IN6_IS_ADDR_UNSPECIFIED(broad
))
463 zlog_warn("warning: %s called for interface %s with unspecified "
464 "destination address; ignoring!", __func__
, ifp
->name
);
467 p
= prefix_ipv6_new ();
468 p
->family
= AF_INET6
;
469 IPV6_ADDR_COPY (&p
->prefix
, broad
);
470 p
->prefixlen
= prefixlen
;
471 ifc
->destination
= (struct prefix
*) p
;
474 if (CHECK_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
) && !ifc
->destination
)
476 zlog_warn("warning: %s called for interface %s "
477 "with peer flag set, but no peer address supplied",
478 __func__
, ifp
->name
);
479 UNSET_FLAG(ifc
->flags
, ZEBRA_IFA_PEER
);
482 /* Label of this address. */
484 ifc
->label
= XSTRDUP (MTYPE_CONNECTED_LABEL
, label
);
486 /* On Linux, we only get here when DAD is complete, therefore we can set
489 * On BSD, there currently doesn't seem to be a way to check for completion of
490 * DAD, so we replicate the old behaviour and set ZEBRA_IFC_REAL, although DAD
491 * might still be running.
493 SET_FLAG(ifc
->conf
, ZEBRA_IFC_REAL
);
494 connected_update(ifp
, ifc
);
498 connected_down_ipv6 (struct interface
*ifp
, struct connected
*ifc
)
502 if (! CHECK_FLAG (ifc
->conf
, ZEBRA_IFC_REAL
))
505 PREFIX_COPY_IPV6(&p
, CONNECTED_PREFIX(ifc
));
509 if (IN6_IS_ADDR_UNSPECIFIED (&p
.u
.prefix6
))
512 rib_delete (AFI_IP6
, SAFI_UNICAST
, ifp
->vrf_id
, ZEBRA_ROUTE_CONNECT
,
513 0, 0, &p
, NULL
, ifp
->ifindex
, 0);
515 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
516 zlog_debug ("%u: IF %s IPv6 address down, scheduling RIB processing",
517 ifp
->vrf_id
, ifp
->name
);
519 rib_update (ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
521 /* Schedule LSP forwarding entries for processing, if appropriate. */
522 if (ifp
->vrf_id
== VRF_DEFAULT
)
524 if (IS_ZEBRA_DEBUG_MPLS
)
525 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
526 ifp
->vrf_id
, ifp
->name
);
527 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp
->vrf_id
));
532 connected_delete_ipv6 (struct interface
*ifp
, struct in6_addr
*address
,
533 u_char prefixlen
, struct in6_addr
*broad
)
535 struct prefix_ipv6 p
;
536 struct connected
*ifc
;
538 memset (&p
, 0, sizeof (struct prefix_ipv6
));
540 memcpy (&p
.prefix
, address
, sizeof (struct in6_addr
));
541 p
.prefixlen
= prefixlen
;
543 ifc
= connected_check (ifp
, (struct prefix
*) &p
);
547 connected_withdraw (ifc
);
549 if (IS_ZEBRA_DEBUG_RIB_DETAILED
)
550 zlog_debug ("%u: IF %s IPv6 address del, scheduling RIB processing",
551 ifp
->vrf_id
, ifp
->name
);
553 rib_update (ifp
->vrf_id
, RIB_UPDATE_IF_CHANGE
);
555 /* Schedule LSP forwarding entries for processing, if appropriate. */
556 if (ifp
->vrf_id
== VRF_DEFAULT
)
558 if (IS_ZEBRA_DEBUG_MPLS
)
559 zlog_debug ("%u: IF %s IPv4 address add/up, scheduling MPLS processing",
560 ifp
->vrf_id
, ifp
->name
);
561 mpls_mark_lsps_for_processing (vrf_info_lookup(ifp
->vrf_id
));
566 connected_is_unnumbered (struct interface
*ifp
)
568 struct connected
*connected
;
569 struct listnode
*node
;
571 for (ALL_LIST_ELEMENTS_RO (ifp
->connected
, node
, connected
))
573 if (CHECK_FLAG (connected
->conf
, ZEBRA_IFC_REAL
) &&
574 connected
->address
->family
== AF_INET
)
575 return CHECK_FLAG(connected
->flags
, ZEBRA_IFA_UNNUMBERED
);