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$");
40 #define IN_HISTORICAL_NETS /* include class masks */
42 #include <sys/param.h>
43 #include <sys/eventhandler.h>
44 #include <sys/systm.h>
45 #include <sys/sockio.h>
46 #include <sys/malloc.h>
48 #include <sys/socket.h>
50 #include <sys/kernel.h>
53 #include <sys/sysctl.h>
54 #include <sys/syslog.h>
58 #include <net/if_var.h>
59 #include <net/if_arp.h>
60 #include <net/if_dl.h>
61 #include <net/if_llatbl.h>
62 #include <net/if_private.h>
63 #include <net/if_types.h>
64 #include <net/route.h>
65 #include <net/route/nhop.h>
66 #include <net/route/route_ctl.h>
69 #include <netinet/if_ether.h>
70 #include <netinet/in.h>
71 #include <netinet/in_fib.h>
72 #include <netinet/in_var.h>
73 #include <netinet/in_pcb.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/ip_carp.h>
76 #include <netinet/igmp_var.h>
77 #include <netinet/udp.h>
78 #include <netinet/udp_var.h>
80 static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *);
81 static int in_difaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *);
82 static int in_gifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *);
84 static void in_socktrim(struct sockaddr_in *);
85 static void in_purgemaddrs(struct ifnet *);
87 static bool ia_need_loopback_route(const struct in_ifaddr *);
89 VNET_DEFINE_STATIC(int, nosameprefix);
90 #define V_nosameprefix VNET(nosameprefix)
91 SYSCTL_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_VNET | CTLFLAG_RW,
92 &VNET_NAME(nosameprefix), 0,
93 "Refuse to create same prefixes on different interfaces");
95 VNET_DEFINE_STATIC(bool, broadcast_lowest);
96 #define V_broadcast_lowest VNET(broadcast_lowest)
97 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, broadcast_lowest, CTLFLAG_VNET | CTLFLAG_RW,
98 &VNET_NAME(broadcast_lowest), 0,
99 "Treat lowest address on a subnet (host 0) as broadcast");
101 VNET_DEFINE(bool, ip_allow_net240) = false;
102 #define V_ip_allow_net240 VNET(ip_allow_net240)
103 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, allow_net240,
104 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_allow_net240), 0,
105 "Allow use of Experimental addresses, aka Class E (240/4)");
106 /* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-240 */
108 VNET_DEFINE(bool, ip_allow_net0) = false;
109 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, allow_net0,
110 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_allow_net0), 0,
111 "Allow use of addresses in network 0/8");
112 /* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-0 */
114 VNET_DEFINE(uint32_t, in_loopback_mask) = IN_LOOPBACK_MASK_DFLT;
115 #define V_in_loopback_mask VNET(in_loopback_mask)
116 static int sysctl_loopback_prefixlen(SYSCTL_HANDLER_ARGS);
117 SYSCTL_PROC(_net_inet_ip, OID_AUTO, loopback_prefixlen,
118 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
119 NULL, 0, sysctl_loopback_prefixlen, "I",
120 "Prefix length of address space reserved for loopback");
121 /* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-127 */
123 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
124 #define V_ripcbinfo VNET(ripcbinfo)
126 static struct sx in_control_sx;
127 SX_SYSINIT(in_control_sx, &in_control_sx, "in_control");
130 * Return 1 if an internet address is for a ``local'' host
131 * (one to which we have a connection).
134 in_localaddr(struct in_addr in)
136 u_long i = ntohl(in.s_addr);
137 struct in_ifaddr *ia;
141 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
142 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
150 * Return 1 if an internet address is for the local host and configured
151 * on one of its interfaces.
154 in_localip(struct in_addr in)
156 struct in_ifaddr *ia;
160 CK_LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash)
161 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
168 * Like in_localip(), but FIB-aware.
171 in_localip_fib(struct in_addr in, uint16_t fib)
173 struct in_ifaddr *ia;
177 CK_LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash)
178 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr &&
179 ia->ia_ifa.ifa_ifp->if_fib == fib)
186 * Return 1 if an internet address is configured on an interface.
189 in_ifhasaddr(struct ifnet *ifp, struct in_addr in)
192 struct in_ifaddr *ia;
196 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
197 if (ifa->ifa_addr->sa_family != AF_INET)
199 ia = (struct in_ifaddr *)ifa;
200 if (ia->ia_addr.sin_addr.s_addr == in.s_addr)
208 * Return a reference to the interface address which is different to
209 * the supplied one but with same IP address value.
211 static struct in_ifaddr *
212 in_localip_more(struct in_ifaddr *original_ia)
214 struct epoch_tracker et;
215 in_addr_t original_addr = IA_SIN(original_ia)->sin_addr.s_addr;
216 uint32_t original_fib = original_ia->ia_ifa.ifa_ifp->if_fib;
217 struct in_ifaddr *ia;
220 CK_LIST_FOREACH(ia, INADDR_HASH(original_addr), ia_hash) {
221 in_addr_t addr = IA_SIN(ia)->sin_addr.s_addr;
222 uint32_t fib = ia->ia_ifa.ifa_ifp->if_fib;
223 if (!V_rt_add_addr_allfibs && (original_fib != fib))
225 if ((original_ia != ia) && (original_addr == addr)) {
226 ifa_ref(&ia->ia_ifa);
237 * Tries to find first IPv4 address in the provided fib.
238 * Prefers non-loopback addresses and return loopback IFF
239 * @loopback_ok is set.
241 * Returns ifa or NULL.
244 in_findlocal(uint32_t fibnum, bool loopback_ok)
246 struct in_ifaddr *ia = NULL, *ia_lo = NULL;
250 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
251 uint32_t ia_fib = ia->ia_ifa.ifa_ifp->if_fib;
252 if (!V_rt_add_addr_allfibs && (fibnum != ia_fib))
255 if (!IN_LOOPBACK(ntohl(IA_SIN(ia)->sin_addr.s_addr)))
268 * Determine whether an IP address is in a reserved set of addresses
269 * that may not be forwarded, or whether datagrams to that destination
273 in_canforward(struct in_addr in)
275 u_long i = ntohl(in.s_addr);
277 if (IN_MULTICAST(i) || IN_LINKLOCAL(i) || IN_LOOPBACK(i))
279 if (IN_EXPERIMENTAL(i) && !V_ip_allow_net240)
281 if (IN_ZERONET(i) && !V_ip_allow_net0)
287 * Sysctl to manage prefix of reserved loopback network; translate
288 * to/from mask. The mask is always contiguous high-order 1 bits
289 * followed by all 0 bits.
292 sysctl_loopback_prefixlen(SYSCTL_HANDLER_ARGS)
296 /* ffs is 1-based; compensate. */
297 preflen = 33 - ffs(V_in_loopback_mask);
298 error = sysctl_handle_int(oidp, &preflen, 0, req);
299 if (error || !req->newptr)
301 if (preflen < 8 || preflen > 31)
303 V_in_loopback_mask = 0xffffffff << (32 - preflen);
308 * Trim a mask in a sockaddr
311 in_socktrim(struct sockaddr_in *ap)
313 char *cplim = (char *) &ap->sin_addr;
314 char *cp = (char *) (&ap->sin_addr + 1);
317 while (--cp >= cplim)
319 (ap)->sin_len = cp - (char *) (ap) + 1;
325 * Generic internet control operations (ioctl's).
328 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp,
331 struct ifreq *ifr = (struct ifreq *)data;
332 struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
333 struct epoch_tracker et;
335 struct in_ifaddr *ia;
339 return (EADDRNOTAVAIL);
341 struct ucred *cred = (td != NULL) ? td->td_ucred : NULL;
344 * Filter out 4 ioctls we implement directly. Forward the rest
345 * to specific functions and ifp->if_ioctl().
354 sx_xlock(&in_control_sx);
355 error = in_gifaddr_ioctl(cmd, data, ifp, cred);
356 sx_xunlock(&in_control_sx);
359 sx_xlock(&in_control_sx);
360 error = in_difaddr_ioctl(cmd, data, ifp, cred);
361 sx_xunlock(&in_control_sx);
363 case OSIOCAIFADDR: /* 9.x compat */
365 sx_xlock(&in_control_sx);
366 error = in_aifaddr_ioctl(cmd, data, ifp, cred);
367 sx_xunlock(&in_control_sx);
373 /* We no longer support that old commands. */
376 if (ifp->if_ioctl == NULL)
378 return ((*ifp->if_ioctl)(ifp, cmd, data));
381 if (addr->sin_addr.s_addr != INADDR_ANY &&
382 prison_check_ip4(cred, &addr->sin_addr) != 0)
383 return (EADDRNOTAVAIL);
386 * Find address for this interface, if it exists. If an
387 * address was specified, find that one instead of the
388 * first one on the interface, if possible.
391 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
392 if (ifa->ifa_addr->sa_family != AF_INET)
394 ia = (struct in_ifaddr *)ifa;
395 if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
399 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
400 if (ifa->ifa_addr->sa_family == AF_INET) {
401 ia = (struct in_ifaddr *)ifa;
402 if (prison_check_ip4(cred,
403 &ia->ia_addr.sin_addr) == 0)
409 return (EADDRNOTAVAIL);
419 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
423 *addr = ia->ia_broadaddr;
427 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
431 *addr = ia->ia_dstaddr;
435 *addr = ia->ia_sockmask;
445 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred)
447 const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
448 const struct sockaddr_in *addr = &ifra->ifra_addr;
449 const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
450 const struct sockaddr_in *mask = &ifra->ifra_mask;
451 const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
452 const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
453 struct epoch_tracker et;
455 struct in_ifaddr *ia;
459 error = priv_check_cred(cred, PRIV_NET_ADDIFADDR);
464 * ifra_addr must be present and be of INET family.
465 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
467 if (addr->sin_len != sizeof(struct sockaddr_in) ||
468 addr->sin_family != AF_INET)
470 if (broadaddr->sin_len != 0 &&
471 (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
472 broadaddr->sin_family != AF_INET))
474 if (mask->sin_len != 0 &&
475 (mask->sin_len != sizeof(struct sockaddr_in) ||
476 mask->sin_family != AF_INET))
478 if ((ifp->if_flags & IFF_POINTOPOINT) &&
479 (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
480 dstaddr->sin_addr.s_addr == INADDR_ANY))
481 return (EDESTADDRREQ);
482 if (vhid != 0 && carp_attach_p == NULL)
483 return (EPROTONOSUPPORT);
486 * See whether address already exist.
491 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
492 struct in_ifaddr *it;
494 if (ifa->ifa_addr->sa_family != AF_INET)
497 it = (struct in_ifaddr *)ifa;
498 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
499 prison_check_ip4(cred, &addr->sin_addr) == 0)
507 (void )in_difaddr_ioctl(cmd, data, ifp, cred);
509 ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
510 ia = (struct in_ifaddr *)ifa;
511 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
512 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
513 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
514 callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock,
515 CALLOUT_RETURNUNLOCKED);
519 if (mask->sin_len != 0) {
520 ia->ia_sockmask = *mask;
521 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
523 in_addr_t i = ntohl(addr->sin_addr.s_addr);
526 * If netmask isn't supplied, use historical default.
527 * This is deprecated for interfaces other than loopback
528 * or point-to-point; warn in other cases. In the future
529 * we should return an error rather than warning.
531 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0)
532 printf("%s: set address: WARNING: network mask "
533 "should be specified; using historical default\n",
536 ia->ia_subnetmask = IN_CLASSA_NET;
537 else if (IN_CLASSB(i))
538 ia->ia_subnetmask = IN_CLASSB_NET;
540 ia->ia_subnetmask = IN_CLASSC_NET;
541 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
543 ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
544 in_socktrim(&ia->ia_sockmask);
546 if (ifp->if_flags & IFF_BROADCAST) {
547 if (broadaddr->sin_len != 0) {
548 ia->ia_broadaddr = *broadaddr;
549 } else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
550 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
551 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
552 ia->ia_broadaddr.sin_family = AF_INET;
554 ia->ia_broadaddr.sin_addr.s_addr =
555 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
556 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
557 ia->ia_broadaddr.sin_family = AF_INET;
561 if (ifp->if_flags & IFF_POINTOPOINT)
562 ia->ia_dstaddr = *dstaddr;
565 error = (*carp_attach_p)(&ia->ia_ifa, vhid);
570 /* if_addrhead is already referenced by ifa_alloc() */
572 CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
573 IF_ADDR_WUNLOCK(ifp);
575 ifa_ref(ifa); /* in_ifaddrhead */
576 sx_assert(&in_control_sx, SA_XLOCKED);
577 CK_STAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
578 CK_LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia,
582 * Give the interface a chance to initialize
583 * if this is its first address,
584 * and to validate the address if necessary.
586 if (ifp->if_ioctl != NULL) {
587 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
593 * Add route for the network.
596 error = in_addprefix(ia);
602 * Add a loopback route to self.
604 if (vhid == 0 && ia_need_loopback_route(ia)) {
605 struct in_ifaddr *eia;
607 eia = in_localip_more(ia);
610 error = ifa_add_loopback_route((struct ifaddr *)ia,
611 (struct sockaddr *)&ia->ia_addr);
615 ifa_free(&eia->ia_ifa);
618 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
619 struct in_addr allhosts_addr;
620 struct in_ifinfo *ii;
622 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
623 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
625 error = in_joingroup(ifp, &allhosts_addr, NULL,
630 * Note: we don't need extra reference for ifa, since we called
631 * with sx lock held, and ifaddr can not be deleted in concurrent
634 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, ifa, IFADDR_EVENT_ADD);
640 (void )in_scrubprefix(ia, LLE_STATIC);
643 if (ia->ia_ifa.ifa_carp)
644 (*carp_detach_p)(&ia->ia_ifa, false);
647 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
648 IF_ADDR_WUNLOCK(ifp);
649 ifa_free(&ia->ia_ifa); /* if_addrhead */
651 sx_assert(&in_control_sx, SA_XLOCKED);
652 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
653 CK_LIST_REMOVE(ia, ia_hash);
654 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
660 in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred)
662 const struct ifreq *ifr = (struct ifreq *)data;
663 const struct sockaddr_in *addr = (const struct sockaddr_in *)
666 struct in_ifaddr *ia;
667 bool deleteAny, iaIsLast;
671 error = priv_check_cred(cred, PRIV_NET_DELIFADDR);
676 if (addr->sin_len != sizeof(struct sockaddr_in) ||
677 addr->sin_family != AF_INET)
685 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
686 struct in_ifaddr *it;
688 if (ifa->ifa_addr->sa_family != AF_INET)
691 it = (struct in_ifaddr *)ifa;
692 if (deleteAny && ia == NULL && (cred == NULL ||
693 prison_check_ip4(cred, &it->ia_addr.sin_addr) == 0))
696 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
697 (cred == NULL || prison_check_ip4(cred,
698 &addr->sin_addr) == 0))
706 IF_ADDR_WUNLOCK(ifp);
707 return (EADDRNOTAVAIL);
710 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
711 IF_ADDR_WUNLOCK(ifp);
712 ifa_free(&ia->ia_ifa); /* if_addrhead */
714 sx_assert(&in_control_sx, SA_XLOCKED);
715 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
716 CK_LIST_REMOVE(ia, ia_hash);
719 * in_scrubprefix() kills the interface route.
721 in_scrubprefix(ia, LLE_STATIC);
724 * in_ifadown gets rid of all the rest of
725 * the routes. This is not quite the right
726 * thing to do, but at least if we are running
727 * a routing process they will come back.
729 in_ifadown(&ia->ia_ifa, 1);
731 if (ia->ia_ifa.ifa_carp)
732 (*carp_detach_p)(&ia->ia_ifa, cmd == SIOCAIFADDR);
735 * If this is the last IPv4 address configured on this
736 * interface, leave the all-hosts group.
737 * No state-change report need be transmitted.
739 if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
740 struct in_ifinfo *ii;
742 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
743 if (ii->ii_allhosts) {
744 (void)in_leavegroup(ii->ii_allhosts, NULL);
745 ii->ii_allhosts = NULL;
750 if (callout_stop(&ia->ia_garp_timer) == 1) {
751 ifa_free(&ia->ia_ifa);
753 IF_ADDR_WUNLOCK(ifp);
755 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
757 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
763 in_gifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred)
765 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
766 const struct sockaddr_in *addr = &ifra->ifra_addr;
767 struct epoch_tracker et;
769 struct in_ifaddr *ia;
772 * ifra_addr must be present and be of INET family.
774 if (addr->sin_len != sizeof(struct sockaddr_in) ||
775 addr->sin_family != AF_INET)
779 * See whether address exist.
783 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
784 struct in_ifaddr *it;
786 if (ifa->ifa_addr->sa_family != AF_INET)
789 it = (struct in_ifaddr *)ifa;
790 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
791 prison_check_ip4(cred, &addr->sin_addr) == 0) {
798 return (EADDRNOTAVAIL);
801 ifra->ifra_mask = ia->ia_sockmask;
802 if ((ifp->if_flags & IFF_POINTOPOINT) &&
803 ia->ia_dstaddr.sin_family == AF_INET)
804 ifra->ifra_dstaddr = ia->ia_dstaddr;
805 else if ((ifp->if_flags & IFF_BROADCAST) &&
806 ia->ia_broadaddr.sin_family == AF_INET)
807 ifra->ifra_broadaddr = ia->ia_broadaddr;
809 memset(&ifra->ifra_broadaddr, 0,
810 sizeof(ifra->ifra_broadaddr));
817 in_match_ifaddr(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
820 if (nh->nh_ifa == (struct ifaddr *)arg)
827 in_handle_prefix_route(uint32_t fibnum, int cmd,
828 struct sockaddr_in *dst, struct sockaddr_in *netmask, struct ifaddr *ifa,
834 /* Prepare gateway */
835 struct sockaddr_dl_short sdl = {
836 .sdl_family = AF_LINK,
837 .sdl_len = sizeof(struct sockaddr_dl_short),
838 .sdl_type = ifa->ifa_ifp->if_type,
839 .sdl_index = ifa->ifa_ifp->if_index,
842 struct rt_addrinfo info = {
845 .rti_flags = RTF_PINNED | ((netmask != NULL) ? 0 : RTF_HOST),
847 [RTAX_DST] = (struct sockaddr *)dst,
848 [RTAX_NETMASK] = (struct sockaddr *)netmask,
849 [RTAX_GATEWAY] = (struct sockaddr *)&sdl,
851 /* Ensure we delete the prefix IFF prefix ifa matches */
852 .rti_filter = in_match_ifaddr,
853 .rti_filterdata = ifa,
856 return (rib_handle_ifaddr_info(fibnum, cmd, &info));
860 * Routing table interaction with interface addresses.
862 * In general, two types of routes needs to be installed:
863 * a) "interface" or "prefix" route, telling user that the addresses
864 * behind the ifa prefix are reached directly.
865 * b) "loopback" route installed for the ifa address, telling user that
866 * the address belongs to local system.
868 * Handling for (a) and (b) differs in multi-fib aspects, hence they
869 * are implemented in different functions below.
871 * The cases above may intersect - /32 interface aliases results in
872 * the same prefix produced by (a) and (b). This blurs the definition
873 * of the "loopback" route and complicate interactions. The interaction
874 * table is defined below. The case numbers are used in the multiple
875 * functions below to refer to the particular test case.
877 * There can be multiple options:
878 * 1) Adding address with prefix on non-p2p/non-loopback interface.
879 * Example: 192.0.2.1/24. Action:
880 * * add "prefix" route towards 192.0.2.0/24 via @ia interface,
881 * using @ia as an address source.
882 * * add "loopback" route towards 192.0.2.1 via V_loif, saving
883 * @ia ifp in the gateway and using @ia as an address source.
885 * 2) Adding address with /32 mask to non-p2p/non-loopback interface.
886 * Example: 192.0.2.2/32. Action:
887 * * add "prefix" host route via V_loif, using @ia as an address source.
889 * 3) Adding address with or without prefix to p2p interface.
890 * Example: 10.0.0.1/24->10.0.0.2. Action:
891 * * add "prefix" host route towards 10.0.0.2 via this interface, using @ia
892 * as an address source. Note: no sense in installing full /24 as the interface
894 * * add "loopback" route towards 10.0.9.1 via V_loif, saving
895 * @ia ifp in the gateway and using @ia as an address source.
897 * 4) Adding address with or without prefix to loopback interface.
898 * Example: 192.0.2.1/24. Action:
899 * * add "prefix" host route via @ia interface, using @ia as an address source.
900 * Note: Skip installing /24 prefix as it would introduce TTL loop
901 * for the traffic destined to these addresses.
905 * Checks if @ia needs to install loopback route to @ia address via
906 * ifa_maintain_loopback_route().
908 * Return true on success.
911 ia_need_loopback_route(const struct in_ifaddr *ia)
913 struct ifnet *ifp = ia->ia_ifp;
915 /* Case 4: Skip loopback interfaces */
916 if ((ifp->if_flags & IFF_LOOPBACK) ||
917 (ia->ia_addr.sin_addr.s_addr == INADDR_ANY))
920 /* Clash avoidance: Skip p2p interfaces with both addresses are equal */
921 if ((ifp->if_flags & IFF_POINTOPOINT) &&
922 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
925 /* Case 2: skip /32 prefixes */
926 if (!(ifp->if_flags & IFF_POINTOPOINT) &&
927 (ia->ia_sockmask.sin_addr.s_addr == INADDR_BROADCAST))
934 * Calculate "prefix" route corresponding to @ia.
937 ia_getrtprefix(const struct in_ifaddr *ia, struct in_addr *prefix, struct in_addr *mask)
940 if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) {
941 /* Case 3: return host route for dstaddr */
942 *prefix = ia->ia_dstaddr.sin_addr;
943 mask->s_addr = INADDR_BROADCAST;
944 } else if (ia->ia_ifp->if_flags & IFF_LOOPBACK) {
945 /* Case 4: return host route for ifaddr */
946 *prefix = ia->ia_addr.sin_addr;
947 mask->s_addr = INADDR_BROADCAST;
949 /* Cases 1,2: return actual ia prefix */
950 *prefix = ia->ia_addr.sin_addr;
951 *mask = ia->ia_sockmask.sin_addr;
952 prefix->s_addr &= mask->s_addr;
957 * Adds or delete interface "prefix" route corresponding to @ifa.
958 * Returns 0 on success or errno.
961 in_handle_ifaddr_route(int cmd, struct in_ifaddr *ia)
963 struct ifaddr *ifa = &ia->ia_ifa;
964 struct in_addr daddr, maddr;
965 struct sockaddr_in *pmask;
966 struct epoch_tracker et;
969 ia_getrtprefix(ia, &daddr, &maddr);
971 struct sockaddr_in mask = {
972 .sin_family = AF_INET,
973 .sin_len = sizeof(struct sockaddr_in),
977 pmask = (maddr.s_addr != INADDR_BROADCAST) ? &mask : NULL;
979 struct sockaddr_in dst = {
980 .sin_family = AF_INET,
981 .sin_len = sizeof(struct sockaddr_in),
982 .sin_addr.s_addr = daddr.s_addr & maddr.s_addr,
985 struct ifnet *ifp = ia->ia_ifp;
987 if ((maddr.s_addr == INADDR_BROADCAST) &&
988 (!(ia->ia_ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))) {
989 /* Case 2: host route on broadcast interface */
993 uint32_t fibnum = ifa->ifa_ifp->if_fib;
995 error = in_handle_prefix_route(fibnum, cmd, &dst, pmask, ifa, ifp);
1002 * Check if we have a route for the given prefix already.
1005 in_hasrtprefix(struct in_ifaddr *target)
1007 struct epoch_tracker et;
1008 struct in_ifaddr *ia;
1009 struct in_addr prefix, mask, p, m;
1010 bool result = false;
1012 ia_getrtprefix(target, &prefix, &mask);
1014 /* Look for an existing address with the same prefix, mask, and fib */
1015 NET_EPOCH_ENTER(et);
1016 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1017 ia_getrtprefix(ia, &p, &m);
1019 if (prefix.s_addr != p.s_addr ||
1020 mask.s_addr != m.s_addr)
1023 if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
1027 * If we got a matching prefix route inserted by other
1028 * interface address, we are done here.
1030 if (ia->ia_flags & IFA_ROUTE) {
1041 in_addprefix(struct in_ifaddr *target)
1045 if (in_hasrtprefix(target)) {
1049 rt_addrmsg(RTM_ADD, &target->ia_ifa,
1050 target->ia_ifp->if_fib);
1056 * No-one seem to have this prefix route, so we try to insert it.
1058 rt_addrmsg(RTM_ADD, &target->ia_ifa, target->ia_ifp->if_fib);
1059 error = in_handle_ifaddr_route(RTM_ADD, target);
1061 target->ia_flags |= IFA_ROUTE;
1066 * Removes either all lle entries for given @ia, or lle
1067 * corresponding to @ia address.
1070 in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags)
1072 struct sockaddr_in addr, mask;
1073 struct sockaddr *saddr, *smask;
1076 saddr = (struct sockaddr *)&addr;
1077 bzero(&addr, sizeof(addr));
1078 addr.sin_len = sizeof(addr);
1079 addr.sin_family = AF_INET;
1080 smask = (struct sockaddr *)&mask;
1081 bzero(&mask, sizeof(mask));
1082 mask.sin_len = sizeof(mask);
1083 mask.sin_family = AF_INET;
1084 mask.sin_addr.s_addr = ia->ia_subnetmask;
1089 * Remove all L2 entries matching given prefix.
1090 * Convert address to host representation to avoid
1091 * doing this on every callback. ia_subnetmask is already
1092 * stored in host representation.
1094 addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr);
1095 lltable_prefix_free(AF_INET, saddr, smask, flags);
1097 /* Remove interface address only */
1098 addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr;
1099 lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr);
1104 * If there is no other address in the system that can serve a route to the
1105 * same prefix, remove the route. Hand over the route to the new address
1109 in_scrubprefix(struct in_ifaddr *target, u_int flags)
1111 struct epoch_tracker et;
1112 struct in_ifaddr *ia;
1113 struct in_addr prefix, mask, p, m;
1117 * Remove the loopback route to the interface address.
1119 if (ia_need_loopback_route(target) && (flags & LLE_STATIC)) {
1120 struct in_ifaddr *eia;
1122 eia = in_localip_more(target);
1125 error = ifa_switch_loopback_route((struct ifaddr *)eia,
1126 (struct sockaddr *)&target->ia_addr);
1127 ifa_free(&eia->ia_ifa);
1129 error = ifa_del_loopback_route((struct ifaddr *)target,
1130 (struct sockaddr *)&target->ia_addr);
1134 ia_getrtprefix(target, &prefix, &mask);
1136 if ((target->ia_flags & IFA_ROUTE) == 0) {
1137 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1140 * Removing address from !IFF_UP interface or
1141 * prefix which exists on other interface (along with route).
1142 * No entries should exist here except target addr.
1143 * Given that, delete this entry only.
1145 in_scrubprefixlle(target, 0, flags);
1149 NET_EPOCH_ENTER(et);
1150 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1151 ia_getrtprefix(ia, &p, &m);
1153 if (prefix.s_addr != p.s_addr ||
1154 mask.s_addr != m.s_addr)
1157 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1161 * If we got a matching prefix address, move IFA_ROUTE and
1162 * the route itself to it. Make sure that routing daemons
1165 if ((ia->ia_flags & IFA_ROUTE) == 0) {
1166 ifa_ref(&ia->ia_ifa);
1168 error = in_handle_ifaddr_route(RTM_DELETE, target);
1170 target->ia_flags &= ~IFA_ROUTE;
1172 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
1174 /* Scrub all entries IFF interface is different */
1175 in_scrubprefixlle(target, target->ia_ifp != ia->ia_ifp,
1177 error = in_handle_ifaddr_route(RTM_ADD, ia);
1179 ia->ia_flags |= IFA_ROUTE;
1181 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
1183 ifa_free(&ia->ia_ifa);
1190 * remove all L2 entries on the given prefix
1192 in_scrubprefixlle(target, 1, flags);
1195 * As no-one seem to have this prefix, we can remove the route.
1197 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1198 error = in_handle_ifaddr_route(RTM_DELETE, target);
1200 target->ia_flags &= ~IFA_ROUTE;
1202 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
1207 in_ifscrub_all(void)
1210 struct ifaddr *ifa, *nifa;
1211 struct ifaliasreq ifr;
1214 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1215 /* Cannot lock here - lock recursion. */
1216 /* NET_EPOCH_ENTER(et); */
1217 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1218 if (ifa->ifa_addr->sa_family != AF_INET)
1222 * This is ugly but the only way for legacy IP to
1223 * cleanly remove addresses and everything attached.
1225 bzero(&ifr, sizeof(ifr));
1226 ifr.ifra_addr = *ifa->ifa_addr;
1227 if (ifa->ifa_dstaddr)
1228 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1229 (void)in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr,
1232 /* NET_EPOCH_EXIT(et); */
1233 in_purgemaddrs(ifp);
1234 igmp_domifdetach(ifp);
1240 in_ifaddr_broadcast(struct in_addr in, struct in_ifaddr *ia)
1243 return ((in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1245 * Optionally check for old-style (host 0) broadcast, but
1246 * taking into account that RFC 3021 obsoletes it.
1248 (V_broadcast_lowest && ia->ia_subnetmask != IN_RFC3021_MASK &&
1249 ntohl(in.s_addr) == ia->ia_subnet)) &&
1251 * Check for an all one subnetmask. These
1252 * only exist when an interface gets a secondary
1255 ia->ia_subnetmask != (u_long)0xffffffff);
1259 * Return 1 if the address might be a local broadcast address.
1262 in_broadcast(struct in_addr in, struct ifnet *ifp)
1269 if (in.s_addr == INADDR_BROADCAST ||
1270 in.s_addr == INADDR_ANY)
1272 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1276 * Look through the list of addresses for a match
1277 * with a broadcast address.
1279 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1280 if (ifa->ifa_addr->sa_family == AF_INET &&
1281 in_ifaddr_broadcast(in, (struct in_ifaddr *)ifa)) {
1289 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1292 in_ifdetach(struct ifnet *ifp)
1295 in_pcbpurgeif0(&V_ripcbinfo, ifp);
1296 in_pcbpurgeif0(&V_udbinfo, ifp);
1297 in_pcbpurgeif0(&V_ulitecbinfo, ifp);
1298 in_purgemaddrs(ifp);
1302 * Make sure all multicast deletions invoking if_ioctl() are
1303 * completed before returning. Else we risk accessing a freed
1304 * ifnet structure pointer.
1306 inm_release_wait(NULL);
1310 in_ifnet_event(void *arg __unused, struct ifnet *ifp, int event)
1312 struct epoch_tracker et;
1314 struct in_ifaddr *ia;
1317 NET_EPOCH_ENTER(et);
1319 case IFNET_EVENT_DOWN:
1320 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1321 if (ifa->ifa_addr->sa_family != AF_INET)
1323 ia = (struct in_ifaddr *)ifa;
1324 if ((ia->ia_flags & IFA_ROUTE) == 0)
1328 * in_scrubprefix() kills the interface route.
1330 in_scrubprefix(ia, 0);
1332 * in_ifadown gets rid of all the rest of the
1333 * routes. This is not quite the right thing
1334 * to do, but at least if we are running a
1335 * routing process they will come back.
1342 case IFNET_EVENT_UP:
1343 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1344 if (ifa->ifa_addr->sa_family != AF_INET)
1346 ia = (struct in_ifaddr *)ifa;
1347 if (ia->ia_flags & IFA_ROUTE)
1350 error = ifa_del_loopback_route(ifa, ifa->ifa_addr);
1351 rt_addrmsg(RTM_ADD, ifa, ifa->ifa_ifp->if_fib);
1352 error = in_handle_ifaddr_route(RTM_ADD, ia);
1354 ia->ia_flags |= IFA_ROUTE;
1355 error = ifa_add_loopback_route(ifa, ifa->ifa_addr);
1362 EVENTHANDLER_DEFINE(ifnet_event, in_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
1365 * Delete all IPv4 multicast address records, and associated link-layer
1366 * multicast address records, associated with ifp.
1367 * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1368 * XXX This should not race with ifma_protospec being set during
1369 * a new allocation, if it does, we have bigger problems.
1372 in_purgemaddrs(struct ifnet *ifp)
1374 struct epoch_tracker et;
1375 struct in_multi_head purgeinms;
1376 struct in_multi *inm;
1377 struct ifmultiaddr *ifma;
1379 SLIST_INIT(&purgeinms);
1380 IN_MULTI_LIST_LOCK();
1383 * Extract list of in_multi associated with the detaching ifp
1384 * which the PF_INET layer is about to release.
1385 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1386 * by code further down.
1389 NET_EPOCH_ENTER(et);
1390 CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1391 inm = inm_ifmultiaddr_get_inm(ifma);
1394 inm_rele_locked(&purgeinms, inm);
1397 IF_ADDR_WUNLOCK(ifp);
1399 inm_release_list_deferred(&purgeinms);
1401 IN_MULTI_LIST_UNLOCK();
1405 struct llentry base;
1408 #define IN_LLTBL_DEFAULT_HSIZE 32
1409 #define IN_LLTBL_HASH(k, h) \
1410 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
1413 * Do actual deallocation of @lle.
1416 in_lltable_destroy_lle_unlocked(epoch_context_t ctx)
1418 struct llentry *lle;
1420 lle = __containerof(ctx, struct llentry, lle_epoch_ctx);
1421 LLE_LOCK_DESTROY(lle);
1422 LLE_REQ_DESTROY(lle);
1423 free(lle, M_LLTABLE);
1427 * Called by LLE_FREE_LOCKED when number of references
1431 in_lltable_destroy_lle(struct llentry *lle)
1435 NET_EPOCH_CALL(in_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
1438 static struct llentry *
1439 in_lltable_new(struct in_addr addr4, u_int flags)
1441 struct in_llentry *lle;
1443 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
1444 if (lle == NULL) /* NB: caller generates msg */
1448 * For IPv4 this will trigger "arpresolve" to generate
1451 lle->base.la_expire = time_uptime; /* mark expired */
1452 lle->base.r_l3addr.addr4 = addr4;
1453 lle->base.lle_refcnt = 1;
1454 lle->base.lle_free = in_lltable_destroy_lle;
1455 LLE_LOCK_INIT(&lle->base);
1456 LLE_REQ_INIT(&lle->base);
1457 callout_init(&lle->base.lle_timer, 1);
1459 return (&lle->base);
1462 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1463 ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
1466 in_lltable_match_prefix(const struct sockaddr *saddr,
1467 const struct sockaddr *smask, u_int flags, struct llentry *lle)
1469 struct in_addr addr, mask, lle_addr;
1471 addr = ((const struct sockaddr_in *)saddr)->sin_addr;
1472 mask = ((const struct sockaddr_in *)smask)->sin_addr;
1473 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
1475 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
1478 if (lle->la_flags & LLE_IFADDR) {
1480 * Delete LLE_IFADDR records IFF address & flag matches.
1481 * Note that addr is the interface address within prefix
1483 * Note also we should handle 'ifdown' cases without removing
1486 if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0)
1491 /* flags & LLE_STATIC means deleting both dynamic and static entries */
1492 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
1499 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
1501 size_t pkts_dropped;
1503 LLE_WLOCK_ASSERT(lle);
1504 KASSERT(llt != NULL, ("lltable is NULL"));
1506 /* Unlink entry from table if not already */
1507 if ((lle->la_flags & LLE_LINKED) != 0) {
1508 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
1509 lltable_unlink_entry(llt, lle);
1512 /* Drop hold queue */
1513 pkts_dropped = llentry_free(lle);
1514 ARPSTAT_ADD(dropped, pkts_dropped);
1518 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1520 struct nhop_object *nh;
1521 struct in_addr addr;
1523 KASSERT(l3addr->sa_family == AF_INET,
1524 ("sin_family %d", l3addr->sa_family));
1526 addr = ((const struct sockaddr_in *)l3addr)->sin_addr;
1528 nh = fib4_lookup(ifp->if_fib, addr, 0, NHR_NONE, 0);
1533 * If the gateway for an existing host route matches the target L3
1534 * address, which is a special route inserted by some implementation
1535 * such as MANET, and the interface is of the correct type, then
1536 * allow for ARP to proceed.
1538 if (nh->nh_flags & NHF_GATEWAY) {
1539 if (!(nh->nh_flags & NHF_HOST) || nh->nh_ifp->if_type != IFT_ETHER ||
1540 (nh->nh_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1541 memcmp(nh->gw_sa.sa_data, l3addr->sa_data,
1542 sizeof(in_addr_t)) != 0) {
1548 * Make sure that at least the destination address is covered
1549 * by the route. This is for handling the case where 2 or more
1550 * interfaces have the same prefix. An incoming packet arrives
1551 * on one interface and the corresponding outgoing packet leaves
1552 * another interface.
1554 if ((nh->nh_ifp != ifp) && (nh->nh_flags & NHF_HOST) == 0) {
1555 struct in_ifaddr *ia = (struct in_ifaddr *)ifaof_ifpforaddr(l3addr, ifp);
1556 struct in_addr dst_addr, mask_addr;
1562 * ifaof_ifpforaddr() returns _best matching_ IFA.
1563 * It is possible that ifa prefix does not cover our address.
1564 * Explicitly verify and fail if that's the case.
1566 dst_addr = IA_SIN(ia)->sin_addr;
1567 mask_addr.s_addr = htonl(ia->ia_subnetmask);
1569 if (!IN_ARE_MASKED_ADDR_EQUAL(dst_addr, addr, mask_addr))
1576 static inline uint32_t
1577 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
1580 return (IN_LLTBL_HASH(dst.s_addr, hsize));
1584 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
1587 return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
1591 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
1593 struct sockaddr_in *sin;
1595 sin = (struct sockaddr_in *)sa;
1596 bzero(sin, sizeof(*sin));
1597 sin->sin_family = AF_INET;
1598 sin->sin_len = sizeof(*sin);
1599 sin->sin_addr = lle->r_l3addr.addr4;
1602 static inline struct llentry *
1603 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
1605 struct llentry *lle;
1606 struct llentries *lleh;
1609 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
1610 lleh = &llt->lle_head[hashidx];
1611 CK_LIST_FOREACH(lle, lleh, lle_next) {
1612 if (lle->la_flags & LLE_DELETED)
1614 if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
1622 in_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
1625 lle->la_flags |= LLE_DELETED;
1626 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1628 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1633 static struct llentry *
1634 in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1636 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1637 struct ifnet *ifp = llt->llt_ifp;
1638 struct llentry *lle;
1639 char linkhdr[LLE_MAX_LINKHDR];
1643 KASSERT(l3addr->sa_family == AF_INET,
1644 ("sin_family %d", l3addr->sa_family));
1647 * A route that covers the given address must have
1648 * been installed 1st because we are doing a resolution,
1651 if (!(flags & LLE_IFADDR) &&
1652 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1655 lle = in_lltable_new(sin->sin_addr, flags);
1657 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1660 lle->la_flags = flags;
1661 if (flags & LLE_STATIC)
1662 lle->r_flags |= RLLE_VALID;
1663 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
1664 linkhdrsize = LLE_MAX_LINKHDR;
1665 if (lltable_calc_llheader(ifp, AF_INET, IF_LLADDR(ifp),
1666 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
1667 in_lltable_free_entry(llt, lle);
1670 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
1672 lle->la_flags |= LLE_STATIC;
1673 lle->r_flags |= (RLLE_VALID | RLLE_IFADDR);
1680 * Return NULL if not found or marked for deletion.
1681 * If found return lle read locked.
1683 static struct llentry *
1684 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1686 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1687 struct llentry *lle;
1689 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
1690 KASSERT(l3addr->sa_family == AF_INET,
1691 ("sin_family %d", l3addr->sa_family));
1692 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) !=
1693 (LLE_UNLOCKED | LLE_EXCLUSIVE),
1694 ("wrong lle request flags: %#x", flags));
1696 lle = in_lltable_find_dst(llt, sin->sin_addr);
1699 if (flags & LLE_UNLOCKED)
1702 if (flags & LLE_EXCLUSIVE)
1708 * If the afdata lock is not held, the LLE may have been unlinked while
1709 * we were blocked on the LLE lock. Check for this case.
1711 if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) {
1712 if (flags & LLE_EXCLUSIVE)
1722 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
1723 struct sysctl_req *wr)
1725 struct ifnet *ifp = llt->llt_ifp;
1728 struct rt_msghdr rtm;
1729 struct sockaddr_in sin;
1730 struct sockaddr_dl sdl;
1732 struct sockaddr_dl *sdl;
1735 bzero(&arpc, sizeof(arpc));
1736 /* skip deleted entries */
1737 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1739 /* Skip if jailed and not a valid IP of the prison. */
1740 lltable_fill_sa_entry(lle,(struct sockaddr *)&arpc.sin);
1741 if (prison_if(wr->td->td_ucred, (struct sockaddr *)&arpc.sin) != 0)
1744 * produce a msg made of:
1746 * struct sockaddr_in; (IPv4)
1747 * struct sockaddr_dl;
1749 arpc.rtm.rtm_msglen = sizeof(arpc);
1750 arpc.rtm.rtm_version = RTM_VERSION;
1751 arpc.rtm.rtm_type = RTM_GET;
1752 arpc.rtm.rtm_flags = RTF_UP;
1753 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1756 if (lle->la_flags & LLE_PUB)
1757 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1760 sdl->sdl_family = AF_LINK;
1761 sdl->sdl_len = sizeof(*sdl);
1762 sdl->sdl_index = ifp->if_index;
1763 sdl->sdl_type = ifp->if_type;
1764 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1765 sdl->sdl_alen = ifp->if_addrlen;
1766 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1769 bzero(LLADDR(sdl), ifp->if_addrlen);
1772 arpc.rtm.rtm_rmx.rmx_expire =
1773 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1774 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1775 if (lle->la_flags & LLE_STATIC)
1776 arpc.rtm.rtm_flags |= RTF_STATIC;
1777 if (lle->la_flags & LLE_IFADDR)
1778 arpc.rtm.rtm_flags |= RTF_PINNED;
1779 arpc.rtm.rtm_index = ifp->if_index;
1780 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1786 in_lltable_post_resolved(struct lltable *llt, struct llentry *lle)
1788 struct ifnet *ifp = llt->llt_ifp;
1790 /* gratuitous ARP */
1791 if ((lle->la_flags & LLE_PUB) != 0)
1792 arprequest(ifp, &lle->r_l3addr.addr4, &lle->r_l3addr.addr4,
1796 static struct lltable *
1797 in_lltattach(struct ifnet *ifp)
1799 struct lltable *llt;
1801 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
1802 llt->llt_af = AF_INET;
1805 llt->llt_lookup = in_lltable_lookup;
1806 llt->llt_alloc_entry = in_lltable_alloc;
1807 llt->llt_delete_entry = in_lltable_delete_entry;
1808 llt->llt_dump_entry = in_lltable_dump_entry;
1809 llt->llt_hash = in_lltable_hash;
1810 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
1811 llt->llt_free_entry = in_lltable_free_entry;
1812 llt->llt_match_prefix = in_lltable_match_prefix;
1813 llt->llt_mark_used = llentry_mark_used;
1814 llt->llt_post_resolved = in_lltable_post_resolved;
1821 in_lltable_get(struct ifnet *ifp)
1823 struct lltable *llt = NULL;
1825 void *afdata_ptr = ifp->if_afdata[AF_INET];
1826 if (afdata_ptr != NULL)
1827 llt = ((struct in_ifinfo *)afdata_ptr)->ii_llt;
1832 in_domifattach(struct ifnet *ifp)
1834 struct in_ifinfo *ii;
1836 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1838 ii->ii_llt = in_lltattach(ifp);
1839 ii->ii_igmp = igmp_domifattach(ifp);
1845 in_domifdetach(struct ifnet *ifp, void *aux)
1847 struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1849 igmp_domifdetach(ifp);
1850 lltable_free(ii->ii_llt);