2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (C) 2001 WIDE Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)in.c 8.4 (Berkeley) 1/9/95
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #define IN_HISTORICAL_NETS /* include class masks */
40 #include <sys/param.h>
41 #include <sys/eventhandler.h>
42 #include <sys/systm.h>
43 #include <sys/sockio.h>
44 #include <sys/malloc.h>
46 #include <sys/socket.h>
48 #include <sys/kernel.h>
51 #include <sys/sysctl.h>
52 #include <sys/syslog.h>
56 #include <net/if_var.h>
57 #include <net/if_arp.h>
58 #include <net/if_dl.h>
59 #include <net/if_llatbl.h>
60 #include <net/if_types.h>
61 #include <net/route.h>
62 #include <net/route/nhop.h>
63 #include <net/route/route_ctl.h>
66 #include <netinet/if_ether.h>
67 #include <netinet/in.h>
68 #include <netinet/in_fib.h>
69 #include <netinet/in_var.h>
70 #include <netinet/in_pcb.h>
71 #include <netinet/ip_var.h>
72 #include <netinet/ip_carp.h>
73 #include <netinet/igmp_var.h>
74 #include <netinet/udp.h>
75 #include <netinet/udp_var.h>
77 static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
78 static int in_difaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
79 static int in_gifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
81 static void in_socktrim(struct sockaddr_in *);
82 static void in_purgemaddrs(struct ifnet *);
84 static bool ia_need_loopback_route(const struct in_ifaddr *);
86 VNET_DEFINE_STATIC(int, nosameprefix);
87 #define V_nosameprefix VNET(nosameprefix)
88 SYSCTL_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_VNET | CTLFLAG_RW,
89 &VNET_NAME(nosameprefix), 0,
90 "Refuse to create same prefixes on different interfaces");
92 VNET_DEFINE_STATIC(bool, broadcast_lowest);
93 #define V_broadcast_lowest VNET(broadcast_lowest)
94 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, broadcast_lowest, CTLFLAG_VNET | CTLFLAG_RW,
95 &VNET_NAME(broadcast_lowest), 0,
96 "Treat lowest address on a subnet (host 0) as broadcast");
98 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
99 #define V_ripcbinfo VNET(ripcbinfo)
101 static struct sx in_control_sx;
102 SX_SYSINIT(in_control_sx, &in_control_sx, "in_control");
105 * Return 1 if an internet address is for a ``local'' host
106 * (one to which we have a connection).
109 in_localaddr(struct in_addr in)
111 u_long i = ntohl(in.s_addr);
112 struct in_ifaddr *ia;
116 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
117 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
125 * Return 1 if an internet address is for the local host and configured
126 * on one of its interfaces.
129 in_localip(struct in_addr in)
131 struct in_ifaddr *ia;
135 CK_LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash)
136 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
143 * Like in_localip(), but FIB-aware.
146 in_localip_fib(struct in_addr in, uint16_t fib)
148 struct in_ifaddr *ia;
152 CK_LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash)
153 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr &&
154 ia->ia_ifa.ifa_ifp->if_fib == fib)
161 * Return 1 if an internet address is configured on an interface.
164 in_ifhasaddr(struct ifnet *ifp, struct in_addr in)
167 struct in_ifaddr *ia;
171 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
172 if (ifa->ifa_addr->sa_family != AF_INET)
174 ia = (struct in_ifaddr *)ifa;
175 if (ia->ia_addr.sin_addr.s_addr == in.s_addr)
183 * Return a reference to the interface address which is different to
184 * the supplied one but with same IP address value.
186 static struct in_ifaddr *
187 in_localip_more(struct in_ifaddr *original_ia)
189 struct epoch_tracker et;
190 in_addr_t original_addr = IA_SIN(original_ia)->sin_addr.s_addr;
191 uint32_t original_fib = original_ia->ia_ifa.ifa_ifp->if_fib;
192 struct in_ifaddr *ia;
195 CK_LIST_FOREACH(ia, INADDR_HASH(original_addr), ia_hash) {
196 in_addr_t addr = IA_SIN(ia)->sin_addr.s_addr;
197 uint32_t fib = ia->ia_ifa.ifa_ifp->if_fib;
198 if (!V_rt_add_addr_allfibs && (original_fib != fib))
200 if ((original_ia != ia) && (original_addr == addr)) {
201 ifa_ref(&ia->ia_ifa);
212 * Tries to find first IPv4 address in the provided fib.
213 * Prefers non-loopback addresses and return loopback IFF
214 * @loopback_ok is set.
216 * Returns ifa or NULL.
219 in_findlocal(uint32_t fibnum, bool loopback_ok)
221 struct in_ifaddr *ia = NULL, *ia_lo = NULL;
225 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
226 uint32_t ia_fib = ia->ia_ifa.ifa_ifp->if_fib;
227 if (!V_rt_add_addr_allfibs && (fibnum != ia_fib))
230 if (!IN_LOOPBACK(ntohl(IA_SIN(ia)->sin_addr.s_addr)))
243 * Determine whether an IP address is in a reserved set of addresses
244 * that may not be forwarded, or whether datagrams to that destination
248 in_canforward(struct in_addr in)
250 u_long i = ntohl(in.s_addr);
252 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i) ||
253 IN_ZERONET(i) || IN_LOOPBACK(i))
259 * Trim a mask in a sockaddr
262 in_socktrim(struct sockaddr_in *ap)
264 char *cplim = (char *) &ap->sin_addr;
265 char *cp = (char *) (&ap->sin_addr + 1);
268 while (--cp >= cplim)
270 (ap)->sin_len = cp - (char *) (ap) + 1;
276 * Generic internet control operations (ioctl's).
279 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
282 struct ifreq *ifr = (struct ifreq *)data;
283 struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
284 struct epoch_tracker et;
286 struct in_ifaddr *ia;
290 return (EADDRNOTAVAIL);
293 * Filter out 4 ioctls we implement directly. Forward the rest
294 * to specific functions and ifp->if_ioctl().
303 sx_xlock(&in_control_sx);
304 error = in_gifaddr_ioctl(cmd, data, ifp, td);
305 sx_xunlock(&in_control_sx);
308 sx_xlock(&in_control_sx);
309 error = in_difaddr_ioctl(cmd, data, ifp, td);
310 sx_xunlock(&in_control_sx);
312 case OSIOCAIFADDR: /* 9.x compat */
314 sx_xlock(&in_control_sx);
315 error = in_aifaddr_ioctl(cmd, data, ifp, td);
316 sx_xunlock(&in_control_sx);
322 /* We no longer support that old commands. */
325 if (ifp->if_ioctl == NULL)
327 return ((*ifp->if_ioctl)(ifp, cmd, data));
330 if (addr->sin_addr.s_addr != INADDR_ANY &&
331 prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0)
332 return (EADDRNOTAVAIL);
335 * Find address for this interface, if it exists. If an
336 * address was specified, find that one instead of the
337 * first one on the interface, if possible.
340 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
341 if (ifa->ifa_addr->sa_family != AF_INET)
343 ia = (struct in_ifaddr *)ifa;
344 if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
348 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
349 if (ifa->ifa_addr->sa_family == AF_INET) {
350 ia = (struct in_ifaddr *)ifa;
351 if (prison_check_ip4(td->td_ucred,
352 &ia->ia_addr.sin_addr) == 0)
358 return (EADDRNOTAVAIL);
368 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
372 *addr = ia->ia_broadaddr;
376 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
380 *addr = ia->ia_dstaddr;
384 *addr = ia->ia_sockmask;
394 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
396 const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
397 const struct sockaddr_in *addr = &ifra->ifra_addr;
398 const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
399 const struct sockaddr_in *mask = &ifra->ifra_mask;
400 const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
401 const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
402 struct epoch_tracker et;
404 struct in_ifaddr *ia;
408 error = priv_check(td, PRIV_NET_ADDIFADDR);
413 * ifra_addr must be present and be of INET family.
414 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
416 if (addr->sin_len != sizeof(struct sockaddr_in) ||
417 addr->sin_family != AF_INET)
419 if (broadaddr->sin_len != 0 &&
420 (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
421 broadaddr->sin_family != AF_INET))
423 if (mask->sin_len != 0 &&
424 (mask->sin_len != sizeof(struct sockaddr_in) ||
425 mask->sin_family != AF_INET))
427 if ((ifp->if_flags & IFF_POINTOPOINT) &&
428 (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
429 dstaddr->sin_addr.s_addr == INADDR_ANY))
430 return (EDESTADDRREQ);
431 if (vhid != 0 && carp_attach_p == NULL)
432 return (EPROTONOSUPPORT);
435 * See whether address already exist.
440 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
441 struct in_ifaddr *it;
443 if (ifa->ifa_addr->sa_family != AF_INET)
446 it = (struct in_ifaddr *)ifa;
447 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
448 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0)
456 (void )in_difaddr_ioctl(cmd, data, ifp, td);
458 ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
459 ia = (struct in_ifaddr *)ifa;
460 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
461 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
462 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
463 callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock,
464 CALLOUT_RETURNUNLOCKED);
468 if (mask->sin_len != 0) {
469 ia->ia_sockmask = *mask;
470 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
472 in_addr_t i = ntohl(addr->sin_addr.s_addr);
475 * If netmask isn't supplied, use historical default.
476 * This is deprecated for interfaces other than loopback
477 * or point-to-point; warn in other cases. In the future
478 * we should return an error rather than warning.
480 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0)
481 printf("%s: set address: WARNING: network mask "
482 "should be specified; using historical default\n",
485 ia->ia_subnetmask = IN_CLASSA_NET;
486 else if (IN_CLASSB(i))
487 ia->ia_subnetmask = IN_CLASSB_NET;
489 ia->ia_subnetmask = IN_CLASSC_NET;
490 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
492 ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
493 in_socktrim(&ia->ia_sockmask);
495 if (ifp->if_flags & IFF_BROADCAST) {
496 if (broadaddr->sin_len != 0) {
497 ia->ia_broadaddr = *broadaddr;
498 } else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
499 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
500 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
501 ia->ia_broadaddr.sin_family = AF_INET;
503 ia->ia_broadaddr.sin_addr.s_addr =
504 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
505 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
506 ia->ia_broadaddr.sin_family = AF_INET;
510 if (ifp->if_flags & IFF_POINTOPOINT)
511 ia->ia_dstaddr = *dstaddr;
514 error = (*carp_attach_p)(&ia->ia_ifa, vhid);
519 /* if_addrhead is already referenced by ifa_alloc() */
521 CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
522 IF_ADDR_WUNLOCK(ifp);
524 ifa_ref(ifa); /* in_ifaddrhead */
525 sx_assert(&in_control_sx, SA_XLOCKED);
526 CK_STAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
527 CK_LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia,
531 * Give the interface a chance to initialize
532 * if this is its first address,
533 * and to validate the address if necessary.
535 if (ifp->if_ioctl != NULL) {
536 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
542 * Add route for the network.
545 error = in_addprefix(ia);
551 * Add a loopback route to self.
553 if (vhid == 0 && ia_need_loopback_route(ia)) {
554 struct in_ifaddr *eia;
556 eia = in_localip_more(ia);
559 error = ifa_add_loopback_route((struct ifaddr *)ia,
560 (struct sockaddr *)&ia->ia_addr);
564 ifa_free(&eia->ia_ifa);
567 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
568 struct in_addr allhosts_addr;
569 struct in_ifinfo *ii;
571 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
572 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
574 error = in_joingroup(ifp, &allhosts_addr, NULL,
579 * Note: we don't need extra reference for ifa, since we called
580 * with sx lock held, and ifaddr can not be deleted in concurrent
583 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, ifa, IFADDR_EVENT_ADD);
589 (void )in_scrubprefix(ia, LLE_STATIC);
592 if (ia->ia_ifa.ifa_carp)
593 (*carp_detach_p)(&ia->ia_ifa, false);
596 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
597 IF_ADDR_WUNLOCK(ifp);
598 ifa_free(&ia->ia_ifa); /* if_addrhead */
600 sx_assert(&in_control_sx, SA_XLOCKED);
601 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
602 CK_LIST_REMOVE(ia, ia_hash);
603 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
609 in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
611 const struct ifreq *ifr = (struct ifreq *)data;
612 const struct sockaddr_in *addr = (const struct sockaddr_in *)
615 struct in_ifaddr *ia;
616 bool deleteAny, iaIsLast;
620 error = priv_check(td, PRIV_NET_DELIFADDR);
625 if (addr->sin_len != sizeof(struct sockaddr_in) ||
626 addr->sin_family != AF_INET)
634 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
635 struct in_ifaddr *it;
637 if (ifa->ifa_addr->sa_family != AF_INET)
640 it = (struct in_ifaddr *)ifa;
641 if (deleteAny && ia == NULL && (td == NULL ||
642 prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0))
645 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
646 (td == NULL || prison_check_ip4(td->td_ucred,
647 &addr->sin_addr) == 0))
655 IF_ADDR_WUNLOCK(ifp);
656 return (EADDRNOTAVAIL);
659 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
660 IF_ADDR_WUNLOCK(ifp);
661 ifa_free(&ia->ia_ifa); /* if_addrhead */
663 sx_assert(&in_control_sx, SA_XLOCKED);
664 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
665 CK_LIST_REMOVE(ia, ia_hash);
668 * in_scrubprefix() kills the interface route.
670 in_scrubprefix(ia, LLE_STATIC);
673 * in_ifadown gets rid of all the rest of
674 * the routes. This is not quite the right
675 * thing to do, but at least if we are running
676 * a routing process they will come back.
678 in_ifadown(&ia->ia_ifa, 1);
680 if (ia->ia_ifa.ifa_carp)
681 (*carp_detach_p)(&ia->ia_ifa, cmd == SIOCAIFADDR);
684 * If this is the last IPv4 address configured on this
685 * interface, leave the all-hosts group.
686 * No state-change report need be transmitted.
688 if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
689 struct in_ifinfo *ii;
691 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
692 if (ii->ii_allhosts) {
693 (void)in_leavegroup(ii->ii_allhosts, NULL);
694 ii->ii_allhosts = NULL;
699 if (callout_stop(&ia->ia_garp_timer) == 1) {
700 ifa_free(&ia->ia_ifa);
702 IF_ADDR_WUNLOCK(ifp);
704 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
706 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
712 in_gifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
714 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
715 const struct sockaddr_in *addr = &ifra->ifra_addr;
716 struct epoch_tracker et;
718 struct in_ifaddr *ia;
721 * ifra_addr must be present and be of INET family.
723 if (addr->sin_len != sizeof(struct sockaddr_in) ||
724 addr->sin_family != AF_INET)
728 * See whether address exist.
732 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
733 struct in_ifaddr *it;
735 if (ifa->ifa_addr->sa_family != AF_INET)
738 it = (struct in_ifaddr *)ifa;
739 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
740 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0) {
747 return (EADDRNOTAVAIL);
750 ifra->ifra_mask = ia->ia_sockmask;
751 if ((ifp->if_flags & IFF_POINTOPOINT) &&
752 ia->ia_dstaddr.sin_family == AF_INET)
753 ifra->ifra_dstaddr = ia->ia_dstaddr;
754 else if ((ifp->if_flags & IFF_BROADCAST) &&
755 ia->ia_broadaddr.sin_family == AF_INET)
756 ifra->ifra_broadaddr = ia->ia_broadaddr;
758 memset(&ifra->ifra_broadaddr, 0,
759 sizeof(ifra->ifra_broadaddr));
766 in_match_ifaddr(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
769 if (nh->nh_ifa == (struct ifaddr *)arg)
776 in_handle_prefix_route(uint32_t fibnum, int cmd,
777 struct sockaddr_in *dst, struct sockaddr_in *netmask, struct ifaddr *ifa,
783 /* Prepare gateway */
784 struct sockaddr_dl_short sdl = {
785 .sdl_family = AF_LINK,
786 .sdl_len = sizeof(struct sockaddr_dl_short),
787 .sdl_type = ifa->ifa_ifp->if_type,
788 .sdl_index = ifa->ifa_ifp->if_index,
791 struct rt_addrinfo info = {
794 .rti_flags = RTF_PINNED | ((netmask != NULL) ? 0 : RTF_HOST),
796 [RTAX_DST] = (struct sockaddr *)dst,
797 [RTAX_NETMASK] = (struct sockaddr *)netmask,
798 [RTAX_GATEWAY] = (struct sockaddr *)&sdl,
800 /* Ensure we delete the prefix IFF prefix ifa matches */
801 .rti_filter = in_match_ifaddr,
802 .rti_filterdata = ifa,
805 return (rib_handle_ifaddr_info(fibnum, cmd, &info));
809 * Routing table interaction with interface addresses.
811 * In general, two types of routes needs to be installed:
812 * a) "interface" or "prefix" route, telling user that the addresses
813 * behind the ifa prefix are reached directly.
814 * b) "loopback" route installed for the ifa address, telling user that
815 * the address belongs to local system.
817 * Handling for (a) and (b) differs in multi-fib aspects, hence they
818 * are implemented in different functions below.
820 * The cases above may intersect - /32 interface aliases results in
821 * the same prefix produced by (a) and (b). This blurs the definition
822 * of the "loopback" route and complicate interactions. The interaction
823 * table is defined below. The case numbers are used in the multiple
824 * functions below to refer to the particular test case.
826 * There can be multiple options:
827 * 1) Adding address with prefix on non-p2p/non-loopback interface.
828 * Example: 192.0.2.1/24. Action:
829 * * add "prefix" route towards 192.0.2.0/24 via @ia interface,
830 * using @ia as an address source.
831 * * add "loopback" route towards 192.0.2.1 via V_loif, saving
832 * @ia ifp in the gateway and using @ia as an address source.
834 * 2) Adding address with /32 mask to non-p2p/non-loopback interface.
835 * Example: 192.0.2.2/32. Action:
836 * * add "prefix" host route via V_loif, using @ia as an address source.
838 * 3) Adding address with or without prefix to p2p interface.
839 * Example: 10.0.0.1/24->10.0.0.2. Action:
840 * * add "prefix" host route towards 10.0.0.2 via this interface, using @ia
841 * as an address source. Note: no sense in installing full /24 as the interface
843 * * add "loopback" route towards 10.0.9.1 via V_loif, saving
844 * @ia ifp in the gateway and using @ia as an address source.
846 * 4) Adding address with or without prefix to loopback interface.
847 * Example: 192.0.2.1/24. Action:
848 * * add "prefix" host route via @ia interface, using @ia as an address source.
849 * Note: Skip installing /24 prefix as it would introduce TTL loop
850 * for the traffic destined to these addresses.
854 * Checks if @ia needs to install loopback route to @ia address via
855 * ifa_maintain_loopback_route().
857 * Return true on success.
860 ia_need_loopback_route(const struct in_ifaddr *ia)
862 struct ifnet *ifp = ia->ia_ifp;
864 /* Case 4: Skip loopback interfaces */
865 if ((ifp->if_flags & IFF_LOOPBACK) ||
866 (ia->ia_addr.sin_addr.s_addr == INADDR_ANY))
869 /* Clash avoidance: Skip p2p interfaces with both addresses are equal */
870 if ((ifp->if_flags & IFF_POINTOPOINT) &&
871 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
874 /* Case 2: skip /32 prefixes */
875 if (!(ifp->if_flags & IFF_POINTOPOINT) &&
876 (ia->ia_sockmask.sin_addr.s_addr == INADDR_BROADCAST))
883 * Calculate "prefix" route corresponding to @ia.
886 ia_getrtprefix(const struct in_ifaddr *ia, struct in_addr *prefix, struct in_addr *mask)
889 if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) {
890 /* Case 3: return host route for dstaddr */
891 *prefix = ia->ia_dstaddr.sin_addr;
892 mask->s_addr = INADDR_BROADCAST;
893 } else if (ia->ia_ifp->if_flags & IFF_LOOPBACK) {
894 /* Case 4: return host route for ifaddr */
895 *prefix = ia->ia_addr.sin_addr;
896 mask->s_addr = INADDR_BROADCAST;
898 /* Cases 1,2: return actual ia prefix */
899 *prefix = ia->ia_addr.sin_addr;
900 *mask = ia->ia_sockmask.sin_addr;
901 prefix->s_addr &= mask->s_addr;
906 * Adds or delete interface "prefix" route corresponding to @ifa.
907 * Returns 0 on success or errno.
910 in_handle_ifaddr_route(int cmd, struct in_ifaddr *ia)
912 struct ifaddr *ifa = &ia->ia_ifa;
913 struct in_addr daddr, maddr;
914 struct sockaddr_in *pmask;
915 struct epoch_tracker et;
918 ia_getrtprefix(ia, &daddr, &maddr);
920 struct sockaddr_in mask = {
921 .sin_family = AF_INET,
922 .sin_len = sizeof(struct sockaddr_in),
926 pmask = (maddr.s_addr != INADDR_BROADCAST) ? &mask : NULL;
928 struct sockaddr_in dst = {
929 .sin_family = AF_INET,
930 .sin_len = sizeof(struct sockaddr_in),
931 .sin_addr.s_addr = daddr.s_addr & maddr.s_addr,
934 struct ifnet *ifp = ia->ia_ifp;
936 if ((maddr.s_addr == INADDR_BROADCAST) &&
937 (!(ia->ia_ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))) {
938 /* Case 2: host route on broadcast interface */
942 uint32_t fibnum = ifa->ifa_ifp->if_fib;
944 error = in_handle_prefix_route(fibnum, cmd, &dst, pmask, ifa, ifp);
951 * Check if we have a route for the given prefix already.
954 in_hasrtprefix(struct in_ifaddr *target)
956 struct epoch_tracker et;
957 struct in_ifaddr *ia;
958 struct in_addr prefix, mask, p, m;
961 ia_getrtprefix(target, &prefix, &mask);
963 /* Look for an existing address with the same prefix, mask, and fib */
965 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
966 ia_getrtprefix(ia, &p, &m);
968 if (prefix.s_addr != p.s_addr ||
969 mask.s_addr != m.s_addr)
972 if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
976 * If we got a matching prefix route inserted by other
977 * interface address, we are done here.
979 if (ia->ia_flags & IFA_ROUTE) {
990 in_addprefix(struct in_ifaddr *target)
994 if (in_hasrtprefix(target)) {
998 rt_addrmsg(RTM_ADD, &target->ia_ifa,
999 target->ia_ifp->if_fib);
1005 * No-one seem to have this prefix route, so we try to insert it.
1007 rt_addrmsg(RTM_ADD, &target->ia_ifa, target->ia_ifp->if_fib);
1008 error = in_handle_ifaddr_route(RTM_ADD, target);
1010 target->ia_flags |= IFA_ROUTE;
1015 * Removes either all lle entries for given @ia, or lle
1016 * corresponding to @ia address.
1019 in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags)
1021 struct sockaddr_in addr, mask;
1022 struct sockaddr *saddr, *smask;
1025 saddr = (struct sockaddr *)&addr;
1026 bzero(&addr, sizeof(addr));
1027 addr.sin_len = sizeof(addr);
1028 addr.sin_family = AF_INET;
1029 smask = (struct sockaddr *)&mask;
1030 bzero(&mask, sizeof(mask));
1031 mask.sin_len = sizeof(mask);
1032 mask.sin_family = AF_INET;
1033 mask.sin_addr.s_addr = ia->ia_subnetmask;
1038 * Remove all L2 entries matching given prefix.
1039 * Convert address to host representation to avoid
1040 * doing this on every callback. ia_subnetmask is already
1041 * stored in host representation.
1043 addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr);
1044 lltable_prefix_free(AF_INET, saddr, smask, flags);
1046 /* Remove interface address only */
1047 addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr;
1048 lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr);
1053 * If there is no other address in the system that can serve a route to the
1054 * same prefix, remove the route. Hand over the route to the new address
1058 in_scrubprefix(struct in_ifaddr *target, u_int flags)
1060 struct epoch_tracker et;
1061 struct in_ifaddr *ia;
1062 struct in_addr prefix, mask, p, m;
1066 * Remove the loopback route to the interface address.
1068 if (ia_need_loopback_route(target) && (flags & LLE_STATIC)) {
1069 struct in_ifaddr *eia;
1071 eia = in_localip_more(target);
1074 error = ifa_switch_loopback_route((struct ifaddr *)eia,
1075 (struct sockaddr *)&target->ia_addr);
1076 ifa_free(&eia->ia_ifa);
1078 error = ifa_del_loopback_route((struct ifaddr *)target,
1079 (struct sockaddr *)&target->ia_addr);
1083 ia_getrtprefix(target, &prefix, &mask);
1085 if ((target->ia_flags & IFA_ROUTE) == 0) {
1086 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1089 * Removing address from !IFF_UP interface or
1090 * prefix which exists on other interface (along with route).
1091 * No entries should exist here except target addr.
1092 * Given that, delete this entry only.
1094 in_scrubprefixlle(target, 0, flags);
1098 NET_EPOCH_ENTER(et);
1099 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1100 ia_getrtprefix(ia, &p, &m);
1102 if (prefix.s_addr != p.s_addr ||
1103 mask.s_addr != m.s_addr)
1106 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1110 * If we got a matching prefix address, move IFA_ROUTE and
1111 * the route itself to it. Make sure that routing daemons
1114 if ((ia->ia_flags & IFA_ROUTE) == 0) {
1115 ifa_ref(&ia->ia_ifa);
1117 error = in_handle_ifaddr_route(RTM_DELETE, target);
1119 target->ia_flags &= ~IFA_ROUTE;
1121 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
1123 /* Scrub all entries IFF interface is different */
1124 in_scrubprefixlle(target, target->ia_ifp != ia->ia_ifp,
1126 error = in_handle_ifaddr_route(RTM_ADD, ia);
1128 ia->ia_flags |= IFA_ROUTE;
1130 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
1132 ifa_free(&ia->ia_ifa);
1139 * remove all L2 entries on the given prefix
1141 in_scrubprefixlle(target, 1, flags);
1144 * As no-one seem to have this prefix, we can remove the route.
1146 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1147 error = in_handle_ifaddr_route(RTM_DELETE, target);
1149 target->ia_flags &= ~IFA_ROUTE;
1151 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
1156 in_ifscrub_all(void)
1159 struct ifaddr *ifa, *nifa;
1160 struct ifaliasreq ifr;
1163 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1164 /* Cannot lock here - lock recursion. */
1165 /* NET_EPOCH_ENTER(et); */
1166 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1167 if (ifa->ifa_addr->sa_family != AF_INET)
1171 * This is ugly but the only way for legacy IP to
1172 * cleanly remove addresses and everything attached.
1174 bzero(&ifr, sizeof(ifr));
1175 ifr.ifra_addr = *ifa->ifa_addr;
1176 if (ifa->ifa_dstaddr)
1177 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1178 (void)in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr,
1181 /* NET_EPOCH_EXIT(et); */
1182 in_purgemaddrs(ifp);
1183 igmp_domifdetach(ifp);
1189 in_ifaddr_broadcast(struct in_addr in, struct in_ifaddr *ia)
1192 return ((in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1194 * Optionally check for old-style (host 0) broadcast, but
1195 * taking into account that RFC 3021 obsoletes it.
1197 (V_broadcast_lowest && ia->ia_subnetmask != IN_RFC3021_MASK &&
1198 ntohl(in.s_addr) == ia->ia_subnet)) &&
1200 * Check for an all one subnetmask. These
1201 * only exist when an interface gets a secondary
1204 ia->ia_subnetmask != (u_long)0xffffffff);
1208 * Return 1 if the address might be a local broadcast address.
1211 in_broadcast(struct in_addr in, struct ifnet *ifp)
1218 if (in.s_addr == INADDR_BROADCAST ||
1219 in.s_addr == INADDR_ANY)
1221 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1225 * Look through the list of addresses for a match
1226 * with a broadcast address.
1228 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1229 if (ifa->ifa_addr->sa_family == AF_INET &&
1230 in_ifaddr_broadcast(in, (struct in_ifaddr *)ifa)) {
1238 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1241 in_ifdetach(struct ifnet *ifp)
1244 in_pcbpurgeif0(&V_ripcbinfo, ifp);
1245 in_pcbpurgeif0(&V_udbinfo, ifp);
1246 in_pcbpurgeif0(&V_ulitecbinfo, ifp);
1247 in_purgemaddrs(ifp);
1251 * Make sure all multicast deletions invoking if_ioctl() are
1252 * completed before returning. Else we risk accessing a freed
1253 * ifnet structure pointer.
1255 inm_release_wait(NULL);
1259 * Delete all IPv4 multicast address records, and associated link-layer
1260 * multicast address records, associated with ifp.
1261 * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1262 * XXX This should not race with ifma_protospec being set during
1263 * a new allocation, if it does, we have bigger problems.
1266 in_purgemaddrs(struct ifnet *ifp)
1268 struct in_multi_head purgeinms;
1269 struct in_multi *inm;
1270 struct ifmultiaddr *ifma, *next;
1272 SLIST_INIT(&purgeinms);
1273 IN_MULTI_LIST_LOCK();
1276 * Extract list of in_multi associated with the detaching ifp
1277 * which the PF_INET layer is about to release.
1278 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1279 * by code further down.
1283 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) {
1284 if (ifma->ifma_addr->sa_family != AF_INET ||
1285 ifma->ifma_protospec == NULL)
1287 inm = (struct in_multi *)ifma->ifma_protospec;
1288 inm_rele_locked(&purgeinms, inm);
1289 if (__predict_false(ifma_restart)) {
1290 ifma_restart = true;
1294 IF_ADDR_WUNLOCK(ifp);
1296 inm_release_list_deferred(&purgeinms);
1298 IN_MULTI_LIST_UNLOCK();
1302 struct llentry base;
1305 #define IN_LLTBL_DEFAULT_HSIZE 32
1306 #define IN_LLTBL_HASH(k, h) \
1307 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
1310 * Do actual deallocation of @lle.
1313 in_lltable_destroy_lle_unlocked(epoch_context_t ctx)
1315 struct llentry *lle;
1317 lle = __containerof(ctx, struct llentry, lle_epoch_ctx);
1318 LLE_LOCK_DESTROY(lle);
1319 LLE_REQ_DESTROY(lle);
1320 free(lle, M_LLTABLE);
1324 * Called by LLE_FREE_LOCKED when number of references
1328 in_lltable_destroy_lle(struct llentry *lle)
1332 NET_EPOCH_CALL(in_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
1335 static struct llentry *
1336 in_lltable_new(struct in_addr addr4, u_int flags)
1338 struct in_llentry *lle;
1340 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
1341 if (lle == NULL) /* NB: caller generates msg */
1345 * For IPv4 this will trigger "arpresolve" to generate
1348 lle->base.la_expire = time_uptime; /* mark expired */
1349 lle->base.r_l3addr.addr4 = addr4;
1350 lle->base.lle_refcnt = 1;
1351 lle->base.lle_free = in_lltable_destroy_lle;
1352 LLE_LOCK_INIT(&lle->base);
1353 LLE_REQ_INIT(&lle->base);
1354 callout_init(&lle->base.lle_timer, 1);
1356 return (&lle->base);
1359 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1360 ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
1363 in_lltable_match_prefix(const struct sockaddr *saddr,
1364 const struct sockaddr *smask, u_int flags, struct llentry *lle)
1366 struct in_addr addr, mask, lle_addr;
1368 addr = ((const struct sockaddr_in *)saddr)->sin_addr;
1369 mask = ((const struct sockaddr_in *)smask)->sin_addr;
1370 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
1372 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
1375 if (lle->la_flags & LLE_IFADDR) {
1377 * Delete LLE_IFADDR records IFF address & flag matches.
1378 * Note that addr is the interface address within prefix
1380 * Note also we should handle 'ifdown' cases without removing
1383 if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0)
1388 /* flags & LLE_STATIC means deleting both dynamic and static entries */
1389 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
1396 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
1398 size_t pkts_dropped;
1400 LLE_WLOCK_ASSERT(lle);
1401 KASSERT(llt != NULL, ("lltable is NULL"));
1403 /* Unlink entry from table if not already */
1404 if ((lle->la_flags & LLE_LINKED) != 0) {
1405 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
1406 lltable_unlink_entry(llt, lle);
1409 /* Drop hold queue */
1410 pkts_dropped = llentry_free(lle);
1411 ARPSTAT_ADD(dropped, pkts_dropped);
1415 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1417 struct nhop_object *nh;
1418 struct in_addr addr;
1420 KASSERT(l3addr->sa_family == AF_INET,
1421 ("sin_family %d", l3addr->sa_family));
1423 addr = ((const struct sockaddr_in *)l3addr)->sin_addr;
1425 nh = fib4_lookup(ifp->if_fib, addr, 0, NHR_NONE, 0);
1430 * If the gateway for an existing host route matches the target L3
1431 * address, which is a special route inserted by some implementation
1432 * such as MANET, and the interface is of the correct type, then
1433 * allow for ARP to proceed.
1435 if (nh->nh_flags & NHF_GATEWAY) {
1436 if (!(nh->nh_flags & NHF_HOST) || nh->nh_ifp->if_type != IFT_ETHER ||
1437 (nh->nh_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1438 memcmp(nh->gw_sa.sa_data, l3addr->sa_data,
1439 sizeof(in_addr_t)) != 0) {
1445 * Make sure that at least the destination address is covered
1446 * by the route. This is for handling the case where 2 or more
1447 * interfaces have the same prefix. An incoming packet arrives
1448 * on one interface and the corresponding outgoing packet leaves
1449 * another interface.
1451 if ((nh->nh_ifp != ifp) && (nh->nh_flags & NHF_HOST) == 0) {
1452 struct in_ifaddr *ia = (struct in_ifaddr *)ifaof_ifpforaddr(l3addr, ifp);
1453 struct in_addr dst_addr, mask_addr;
1459 * ifaof_ifpforaddr() returns _best matching_ IFA.
1460 * It is possible that ifa prefix does not cover our address.
1461 * Explicitly verify and fail if that's the case.
1463 dst_addr = IA_SIN(ia)->sin_addr;
1464 mask_addr.s_addr = htonl(ia->ia_subnetmask);
1466 if (!IN_ARE_MASKED_ADDR_EQUAL(dst_addr, addr, mask_addr))
1473 static inline uint32_t
1474 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
1477 return (IN_LLTBL_HASH(dst.s_addr, hsize));
1481 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
1484 return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
1488 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
1490 struct sockaddr_in *sin;
1492 sin = (struct sockaddr_in *)sa;
1493 bzero(sin, sizeof(*sin));
1494 sin->sin_family = AF_INET;
1495 sin->sin_len = sizeof(*sin);
1496 sin->sin_addr = lle->r_l3addr.addr4;
1499 static inline struct llentry *
1500 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
1502 struct llentry *lle;
1503 struct llentries *lleh;
1506 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
1507 lleh = &llt->lle_head[hashidx];
1508 CK_LIST_FOREACH(lle, lleh, lle_next) {
1509 if (lle->la_flags & LLE_DELETED)
1511 if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
1519 in_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
1522 lle->la_flags |= LLE_DELETED;
1523 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1525 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1530 static struct llentry *
1531 in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1533 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1534 struct ifnet *ifp = llt->llt_ifp;
1535 struct llentry *lle;
1536 char linkhdr[LLE_MAX_LINKHDR];
1540 KASSERT(l3addr->sa_family == AF_INET,
1541 ("sin_family %d", l3addr->sa_family));
1544 * A route that covers the given address must have
1545 * been installed 1st because we are doing a resolution,
1548 if (!(flags & LLE_IFADDR) &&
1549 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1552 lle = in_lltable_new(sin->sin_addr, flags);
1554 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1557 lle->la_flags = flags;
1558 if (flags & LLE_STATIC)
1559 lle->r_flags |= RLLE_VALID;
1560 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
1561 linkhdrsize = LLE_MAX_LINKHDR;
1562 if (lltable_calc_llheader(ifp, AF_INET, IF_LLADDR(ifp),
1563 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
1564 NET_EPOCH_CALL(in_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
1567 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
1569 lle->la_flags |= LLE_STATIC;
1570 lle->r_flags |= (RLLE_VALID | RLLE_IFADDR);
1577 * Return NULL if not found or marked for deletion.
1578 * If found return lle read locked.
1580 static struct llentry *
1581 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1583 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1584 struct llentry *lle;
1586 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
1587 KASSERT(l3addr->sa_family == AF_INET,
1588 ("sin_family %d", l3addr->sa_family));
1589 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) !=
1590 (LLE_UNLOCKED | LLE_EXCLUSIVE),
1591 ("wrong lle request flags: %#x", flags));
1593 lle = in_lltable_find_dst(llt, sin->sin_addr);
1596 if (flags & LLE_UNLOCKED)
1599 if (flags & LLE_EXCLUSIVE)
1605 * If the afdata lock is not held, the LLE may have been unlinked while
1606 * we were blocked on the LLE lock. Check for this case.
1608 if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) {
1609 if (flags & LLE_EXCLUSIVE)
1619 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
1620 struct sysctl_req *wr)
1622 struct ifnet *ifp = llt->llt_ifp;
1625 struct rt_msghdr rtm;
1626 struct sockaddr_in sin;
1627 struct sockaddr_dl sdl;
1629 struct sockaddr_dl *sdl;
1632 bzero(&arpc, sizeof(arpc));
1633 /* skip deleted entries */
1634 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1636 /* Skip if jailed and not a valid IP of the prison. */
1637 lltable_fill_sa_entry(lle,(struct sockaddr *)&arpc.sin);
1638 if (prison_if(wr->td->td_ucred, (struct sockaddr *)&arpc.sin) != 0)
1641 * produce a msg made of:
1643 * struct sockaddr_in; (IPv4)
1644 * struct sockaddr_dl;
1646 arpc.rtm.rtm_msglen = sizeof(arpc);
1647 arpc.rtm.rtm_version = RTM_VERSION;
1648 arpc.rtm.rtm_type = RTM_GET;
1649 arpc.rtm.rtm_flags = RTF_UP;
1650 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1653 if (lle->la_flags & LLE_PUB)
1654 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1657 sdl->sdl_family = AF_LINK;
1658 sdl->sdl_len = sizeof(*sdl);
1659 sdl->sdl_index = ifp->if_index;
1660 sdl->sdl_type = ifp->if_type;
1661 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1662 sdl->sdl_alen = ifp->if_addrlen;
1663 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1666 bzero(LLADDR(sdl), ifp->if_addrlen);
1669 arpc.rtm.rtm_rmx.rmx_expire =
1670 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1671 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1672 if (lle->la_flags & LLE_STATIC)
1673 arpc.rtm.rtm_flags |= RTF_STATIC;
1674 if (lle->la_flags & LLE_IFADDR)
1675 arpc.rtm.rtm_flags |= RTF_PINNED;
1676 arpc.rtm.rtm_index = ifp->if_index;
1677 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1682 static struct lltable *
1683 in_lltattach(struct ifnet *ifp)
1685 struct lltable *llt;
1687 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
1688 llt->llt_af = AF_INET;
1691 llt->llt_lookup = in_lltable_lookup;
1692 llt->llt_alloc_entry = in_lltable_alloc;
1693 llt->llt_delete_entry = in_lltable_delete_entry;
1694 llt->llt_dump_entry = in_lltable_dump_entry;
1695 llt->llt_hash = in_lltable_hash;
1696 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
1697 llt->llt_free_entry = in_lltable_free_entry;
1698 llt->llt_match_prefix = in_lltable_match_prefix;
1699 llt->llt_mark_used = llentry_mark_used;
1706 in_domifattach(struct ifnet *ifp)
1708 struct in_ifinfo *ii;
1710 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1712 ii->ii_llt = in_lltattach(ifp);
1713 ii->ii_igmp = igmp_domifattach(ifp);
1719 in_domifdetach(struct ifnet *ifp, void *aux)
1721 struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1723 igmp_domifdetach(ifp);
1724 lltable_free(ii->ii_llt);