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_types.h>
63 #include <net/route.h>
64 #include <net/route/nhop.h>
65 #include <net/route/route_ctl.h>
68 #include <netinet/if_ether.h>
69 #include <netinet/in.h>
70 #include <netinet/in_fib.h>
71 #include <netinet/in_var.h>
72 #include <netinet/in_pcb.h>
73 #include <netinet/ip_var.h>
74 #include <netinet/ip_carp.h>
75 #include <netinet/igmp_var.h>
76 #include <netinet/udp.h>
77 #include <netinet/udp_var.h>
79 static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
80 static int in_difaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
81 static int in_gifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
83 static void in_socktrim(struct sockaddr_in *);
84 static void in_purgemaddrs(struct ifnet *);
86 static bool ia_need_loopback_route(const struct in_ifaddr *);
88 VNET_DEFINE_STATIC(int, nosameprefix);
89 #define V_nosameprefix VNET(nosameprefix)
90 SYSCTL_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_VNET | CTLFLAG_RW,
91 &VNET_NAME(nosameprefix), 0,
92 "Refuse to create same prefixes on different interfaces");
94 VNET_DEFINE_STATIC(bool, broadcast_lowest);
95 #define V_broadcast_lowest VNET(broadcast_lowest)
96 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, broadcast_lowest, CTLFLAG_VNET | CTLFLAG_RW,
97 &VNET_NAME(broadcast_lowest), 0,
98 "Treat lowest address on a subnet (host 0) as broadcast");
100 VNET_DEFINE(bool, ip_allow_net240) = false;
101 #define V_ip_allow_net240 VNET(ip_allow_net240)
102 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, allow_net240,
103 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_allow_net240), 0,
104 "Allow use of Experimental addresses, aka Class E (240/4)");
105 /* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-240 */
107 VNET_DEFINE(bool, ip_allow_net0) = false;
108 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, allow_net0,
109 CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_allow_net0), 0,
110 "Allow use of addresses in network 0/8");
111 /* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-0 */
113 VNET_DEFINE(uint32_t, in_loopback_mask) = IN_LOOPBACK_MASK_DFLT;
114 #define V_in_loopback_mask VNET(in_loopback_mask)
115 static int sysctl_loopback_prefixlen(SYSCTL_HANDLER_ARGS);
116 SYSCTL_PROC(_net_inet_ip, OID_AUTO, loopback_prefixlen,
117 CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
118 NULL, 0, sysctl_loopback_prefixlen, "I",
119 "Prefix length of address space reserved for loopback");
120 /* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-127 */
122 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
123 #define V_ripcbinfo VNET(ripcbinfo)
125 static struct sx in_control_sx;
126 SX_SYSINIT(in_control_sx, &in_control_sx, "in_control");
129 * Return 1 if an internet address is for a ``local'' host
130 * (one to which we have a connection).
133 in_localaddr(struct in_addr in)
135 u_long i = ntohl(in.s_addr);
136 struct in_ifaddr *ia;
140 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
141 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
149 * Return 1 if an internet address is for the local host and configured
150 * on one of its interfaces.
153 in_localip(struct in_addr in)
155 struct in_ifaddr *ia;
159 CK_LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash)
160 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
167 * Like in_localip(), but FIB-aware.
170 in_localip_fib(struct in_addr in, uint16_t fib)
172 struct in_ifaddr *ia;
176 CK_LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash)
177 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr &&
178 ia->ia_ifa.ifa_ifp->if_fib == fib)
185 * Return 1 if an internet address is configured on an interface.
188 in_ifhasaddr(struct ifnet *ifp, struct in_addr in)
191 struct in_ifaddr *ia;
195 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
196 if (ifa->ifa_addr->sa_family != AF_INET)
198 ia = (struct in_ifaddr *)ifa;
199 if (ia->ia_addr.sin_addr.s_addr == in.s_addr)
207 * Return a reference to the interface address which is different to
208 * the supplied one but with same IP address value.
210 static struct in_ifaddr *
211 in_localip_more(struct in_ifaddr *original_ia)
213 struct epoch_tracker et;
214 in_addr_t original_addr = IA_SIN(original_ia)->sin_addr.s_addr;
215 uint32_t original_fib = original_ia->ia_ifa.ifa_ifp->if_fib;
216 struct in_ifaddr *ia;
219 CK_LIST_FOREACH(ia, INADDR_HASH(original_addr), ia_hash) {
220 in_addr_t addr = IA_SIN(ia)->sin_addr.s_addr;
221 uint32_t fib = ia->ia_ifa.ifa_ifp->if_fib;
222 if (!V_rt_add_addr_allfibs && (original_fib != fib))
224 if ((original_ia != ia) && (original_addr == addr)) {
225 ifa_ref(&ia->ia_ifa);
236 * Tries to find first IPv4 address in the provided fib.
237 * Prefers non-loopback addresses and return loopback IFF
238 * @loopback_ok is set.
240 * Returns ifa or NULL.
243 in_findlocal(uint32_t fibnum, bool loopback_ok)
245 struct in_ifaddr *ia = NULL, *ia_lo = NULL;
249 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
250 uint32_t ia_fib = ia->ia_ifa.ifa_ifp->if_fib;
251 if (!V_rt_add_addr_allfibs && (fibnum != ia_fib))
254 if (!IN_LOOPBACK(ntohl(IA_SIN(ia)->sin_addr.s_addr)))
267 * Determine whether an IP address is in a reserved set of addresses
268 * that may not be forwarded, or whether datagrams to that destination
272 in_canforward(struct in_addr in)
274 u_long i = ntohl(in.s_addr);
276 if (IN_MULTICAST(i) || IN_LINKLOCAL(i) || IN_LOOPBACK(i))
278 if (IN_EXPERIMENTAL(i) && !V_ip_allow_net240)
280 if (IN_ZERONET(i) && !V_ip_allow_net0)
286 * Sysctl to manage prefix of reserved loopback network; translate
287 * to/from mask. The mask is always contiguous high-order 1 bits
288 * followed by all 0 bits.
291 sysctl_loopback_prefixlen(SYSCTL_HANDLER_ARGS)
295 /* ffs is 1-based; compensate. */
296 preflen = 33 - ffs(V_in_loopback_mask);
297 error = sysctl_handle_int(oidp, &preflen, 0, req);
298 if (error || !req->newptr)
300 if (preflen < 8 || preflen > 31)
302 V_in_loopback_mask = 0xffffffff << (32 - preflen);
307 * Trim a mask in a sockaddr
310 in_socktrim(struct sockaddr_in *ap)
312 char *cplim = (char *) &ap->sin_addr;
313 char *cp = (char *) (&ap->sin_addr + 1);
316 while (--cp >= cplim)
318 (ap)->sin_len = cp - (char *) (ap) + 1;
324 * Generic internet control operations (ioctl's).
327 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp,
330 struct ifreq *ifr = (struct ifreq *)data;
331 struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
332 struct epoch_tracker et;
334 struct in_ifaddr *ia;
338 return (EADDRNOTAVAIL);
341 * Filter out 4 ioctls we implement directly. Forward the rest
342 * to specific functions and ifp->if_ioctl().
351 sx_xlock(&in_control_sx);
352 error = in_gifaddr_ioctl(cmd, data, ifp, td);
353 sx_xunlock(&in_control_sx);
356 sx_xlock(&in_control_sx);
357 error = in_difaddr_ioctl(cmd, data, ifp, td);
358 sx_xunlock(&in_control_sx);
360 case OSIOCAIFADDR: /* 9.x compat */
362 sx_xlock(&in_control_sx);
363 error = in_aifaddr_ioctl(cmd, data, ifp, td);
364 sx_xunlock(&in_control_sx);
370 /* We no longer support that old commands. */
373 if (ifp->if_ioctl == NULL)
375 return ((*ifp->if_ioctl)(ifp, cmd, data));
378 if (addr->sin_addr.s_addr != INADDR_ANY &&
379 prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0)
380 return (EADDRNOTAVAIL);
383 * Find address for this interface, if it exists. If an
384 * address was specified, find that one instead of the
385 * first one on the interface, if possible.
388 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
389 if (ifa->ifa_addr->sa_family != AF_INET)
391 ia = (struct in_ifaddr *)ifa;
392 if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
396 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
397 if (ifa->ifa_addr->sa_family == AF_INET) {
398 ia = (struct in_ifaddr *)ifa;
399 if (prison_check_ip4(td->td_ucred,
400 &ia->ia_addr.sin_addr) == 0)
406 return (EADDRNOTAVAIL);
416 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
420 *addr = ia->ia_broadaddr;
424 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
428 *addr = ia->ia_dstaddr;
432 *addr = ia->ia_sockmask;
442 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
444 const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
445 const struct sockaddr_in *addr = &ifra->ifra_addr;
446 const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
447 const struct sockaddr_in *mask = &ifra->ifra_mask;
448 const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
449 const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
450 struct epoch_tracker et;
452 struct in_ifaddr *ia;
456 error = priv_check(td, PRIV_NET_ADDIFADDR);
461 * ifra_addr must be present and be of INET family.
462 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
464 if (addr->sin_len != sizeof(struct sockaddr_in) ||
465 addr->sin_family != AF_INET)
467 if (broadaddr->sin_len != 0 &&
468 (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
469 broadaddr->sin_family != AF_INET))
471 if (mask->sin_len != 0 &&
472 (mask->sin_len != sizeof(struct sockaddr_in) ||
473 mask->sin_family != AF_INET))
475 if ((ifp->if_flags & IFF_POINTOPOINT) &&
476 (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
477 dstaddr->sin_addr.s_addr == INADDR_ANY))
478 return (EDESTADDRREQ);
479 if (vhid != 0 && carp_attach_p == NULL)
480 return (EPROTONOSUPPORT);
483 * See whether address already exist.
488 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
489 struct in_ifaddr *it;
491 if (ifa->ifa_addr->sa_family != AF_INET)
494 it = (struct in_ifaddr *)ifa;
495 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
496 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0)
504 (void )in_difaddr_ioctl(cmd, data, ifp, td);
506 ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
507 ia = (struct in_ifaddr *)ifa;
508 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
509 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
510 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
511 callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock,
512 CALLOUT_RETURNUNLOCKED);
516 if (mask->sin_len != 0) {
517 ia->ia_sockmask = *mask;
518 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
520 in_addr_t i = ntohl(addr->sin_addr.s_addr);
523 * If netmask isn't supplied, use historical default.
524 * This is deprecated for interfaces other than loopback
525 * or point-to-point; warn in other cases. In the future
526 * we should return an error rather than warning.
528 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0)
529 printf("%s: set address: WARNING: network mask "
530 "should be specified; using historical default\n",
533 ia->ia_subnetmask = IN_CLASSA_NET;
534 else if (IN_CLASSB(i))
535 ia->ia_subnetmask = IN_CLASSB_NET;
537 ia->ia_subnetmask = IN_CLASSC_NET;
538 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
540 ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
541 in_socktrim(&ia->ia_sockmask);
543 if (ifp->if_flags & IFF_BROADCAST) {
544 if (broadaddr->sin_len != 0) {
545 ia->ia_broadaddr = *broadaddr;
546 } else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
547 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
548 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
549 ia->ia_broadaddr.sin_family = AF_INET;
551 ia->ia_broadaddr.sin_addr.s_addr =
552 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
553 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
554 ia->ia_broadaddr.sin_family = AF_INET;
558 if (ifp->if_flags & IFF_POINTOPOINT)
559 ia->ia_dstaddr = *dstaddr;
562 error = (*carp_attach_p)(&ia->ia_ifa, vhid);
567 /* if_addrhead is already referenced by ifa_alloc() */
569 CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
570 IF_ADDR_WUNLOCK(ifp);
572 ifa_ref(ifa); /* in_ifaddrhead */
573 sx_assert(&in_control_sx, SA_XLOCKED);
574 CK_STAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
575 CK_LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia,
579 * Give the interface a chance to initialize
580 * if this is its first address,
581 * and to validate the address if necessary.
583 if (ifp->if_ioctl != NULL) {
584 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
590 * Add route for the network.
593 error = in_addprefix(ia);
599 * Add a loopback route to self.
601 if (vhid == 0 && ia_need_loopback_route(ia)) {
602 struct in_ifaddr *eia;
604 eia = in_localip_more(ia);
607 error = ifa_add_loopback_route((struct ifaddr *)ia,
608 (struct sockaddr *)&ia->ia_addr);
612 ifa_free(&eia->ia_ifa);
615 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
616 struct in_addr allhosts_addr;
617 struct in_ifinfo *ii;
619 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
620 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
622 error = in_joingroup(ifp, &allhosts_addr, NULL,
627 * Note: we don't need extra reference for ifa, since we called
628 * with sx lock held, and ifaddr can not be deleted in concurrent
631 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, ifa, IFADDR_EVENT_ADD);
637 (void )in_scrubprefix(ia, LLE_STATIC);
640 if (ia->ia_ifa.ifa_carp)
641 (*carp_detach_p)(&ia->ia_ifa, false);
644 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
645 IF_ADDR_WUNLOCK(ifp);
646 ifa_free(&ia->ia_ifa); /* if_addrhead */
648 sx_assert(&in_control_sx, SA_XLOCKED);
649 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
650 CK_LIST_REMOVE(ia, ia_hash);
651 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
657 in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
659 const struct ifreq *ifr = (struct ifreq *)data;
660 const struct sockaddr_in *addr = (const struct sockaddr_in *)
663 struct in_ifaddr *ia;
664 bool deleteAny, iaIsLast;
668 error = priv_check(td, PRIV_NET_DELIFADDR);
673 if (addr->sin_len != sizeof(struct sockaddr_in) ||
674 addr->sin_family != AF_INET)
682 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
683 struct in_ifaddr *it;
685 if (ifa->ifa_addr->sa_family != AF_INET)
688 it = (struct in_ifaddr *)ifa;
689 if (deleteAny && ia == NULL && (td == NULL ||
690 prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0))
693 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
694 (td == NULL || prison_check_ip4(td->td_ucred,
695 &addr->sin_addr) == 0))
703 IF_ADDR_WUNLOCK(ifp);
704 return (EADDRNOTAVAIL);
707 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
708 IF_ADDR_WUNLOCK(ifp);
709 ifa_free(&ia->ia_ifa); /* if_addrhead */
711 sx_assert(&in_control_sx, SA_XLOCKED);
712 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
713 CK_LIST_REMOVE(ia, ia_hash);
716 * in_scrubprefix() kills the interface route.
718 in_scrubprefix(ia, LLE_STATIC);
721 * in_ifadown gets rid of all the rest of
722 * the routes. This is not quite the right
723 * thing to do, but at least if we are running
724 * a routing process they will come back.
726 in_ifadown(&ia->ia_ifa, 1);
728 if (ia->ia_ifa.ifa_carp)
729 (*carp_detach_p)(&ia->ia_ifa, cmd == SIOCAIFADDR);
732 * If this is the last IPv4 address configured on this
733 * interface, leave the all-hosts group.
734 * No state-change report need be transmitted.
736 if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
737 struct in_ifinfo *ii;
739 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
740 if (ii->ii_allhosts) {
741 (void)in_leavegroup(ii->ii_allhosts, NULL);
742 ii->ii_allhosts = NULL;
747 if (callout_stop(&ia->ia_garp_timer) == 1) {
748 ifa_free(&ia->ia_ifa);
750 IF_ADDR_WUNLOCK(ifp);
752 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
754 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
760 in_gifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
762 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
763 const struct sockaddr_in *addr = &ifra->ifra_addr;
764 struct epoch_tracker et;
766 struct in_ifaddr *ia;
769 * ifra_addr must be present and be of INET family.
771 if (addr->sin_len != sizeof(struct sockaddr_in) ||
772 addr->sin_family != AF_INET)
776 * See whether address exist.
780 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
781 struct in_ifaddr *it;
783 if (ifa->ifa_addr->sa_family != AF_INET)
786 it = (struct in_ifaddr *)ifa;
787 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
788 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0) {
795 return (EADDRNOTAVAIL);
798 ifra->ifra_mask = ia->ia_sockmask;
799 if ((ifp->if_flags & IFF_POINTOPOINT) &&
800 ia->ia_dstaddr.sin_family == AF_INET)
801 ifra->ifra_dstaddr = ia->ia_dstaddr;
802 else if ((ifp->if_flags & IFF_BROADCAST) &&
803 ia->ia_broadaddr.sin_family == AF_INET)
804 ifra->ifra_broadaddr = ia->ia_broadaddr;
806 memset(&ifra->ifra_broadaddr, 0,
807 sizeof(ifra->ifra_broadaddr));
814 in_match_ifaddr(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
817 if (nh->nh_ifa == (struct ifaddr *)arg)
824 in_handle_prefix_route(uint32_t fibnum, int cmd, struct sockaddr *dst,
825 int plen, struct ifaddr *ifa, struct ifnet *ifp)
831 if (cmd == RTM_DELETE) {
832 error = rib_del_kernel_px(fibnum, dst, plen, in_match_ifaddr, ifa,
835 struct nhop_object *nh = nhop_alloc(fibnum, dst->sa_family);
836 struct route_nhop_data rnd = { .rnd_weight = RT_DEFAULT_WEIGHT };
840 nhop_set_direct_gw(nh, ifa->ifa_ifp);
841 nhop_set_transmit_ifp(nh, ifp);
842 nhop_set_src(nh, ifa);
843 nhop_set_pinned(nh, true);
844 nhop_set_pxtype_flag(nh, plen == 32 ? NHF_HOST : 0);
845 rnd.rnd_nhop = nhop_get_nhop(nh, &error);
848 int op_flags = RTM_F_CREATE | RTM_F_REPLACE | RTM_F_FORCE;
849 error = rib_add_kernel_px(fibnum, dst, plen, &rnd, op_flags);
855 * Routing table interaction with interface addresses.
857 * In general, two types of routes needs to be installed:
858 * a) "interface" or "prefix" route, telling user that the addresses
859 * behind the ifa prefix are reached directly.
860 * b) "loopback" route installed for the ifa address, telling user that
861 * the address belongs to local system.
863 * Handling for (a) and (b) differs in multi-fib aspects, hence they
864 * are implemented in different functions below.
866 * The cases above may intersect - /32 interface aliases results in
867 * the same prefix produced by (a) and (b). This blurs the definition
868 * of the "loopback" route and complicate interactions. The interaction
869 * table is defined below. The case numbers are used in the multiple
870 * functions below to refer to the particular test case.
872 * There can be multiple options:
873 * 1) Adding address with prefix on non-p2p/non-loopback interface.
874 * Example: 192.0.2.1/24. Action:
875 * * add "prefix" route towards 192.0.2.0/24 via @ia interface,
876 * using @ia as an address source.
877 * * add "loopback" route towards 192.0.2.1 via V_loif, saving
878 * @ia ifp in the gateway and using @ia as an address source.
880 * 2) Adding address with /32 mask to non-p2p/non-loopback interface.
881 * Example: 192.0.2.2/32. Action:
882 * * add "prefix" host route via V_loif, using @ia as an address source.
884 * 3) Adding address with or without prefix to p2p interface.
885 * Example: 10.0.0.1/24->10.0.0.2. Action:
886 * * add "prefix" host route towards 10.0.0.2 via this interface, using @ia
887 * as an address source. Note: no sense in installing full /24 as the interface
889 * * add "loopback" route towards 10.0.9.1 via V_loif, saving
890 * @ia ifp in the gateway and using @ia as an address source.
892 * 4) Adding address with or without prefix to loopback interface.
893 * Example: 192.0.2.1/24. Action:
894 * * add "prefix" host route via @ia interface, using @ia as an address source.
895 * Note: Skip installing /24 prefix as it would introduce TTL loop
896 * for the traffic destined to these addresses.
900 * Checks if @ia needs to install loopback route to @ia address via
901 * ifa_maintain_loopback_route().
903 * Return true on success.
906 ia_need_loopback_route(const struct in_ifaddr *ia)
908 struct ifnet *ifp = ia->ia_ifp;
910 /* Case 4: Skip loopback interfaces */
911 if ((ifp->if_flags & IFF_LOOPBACK) ||
912 (ia->ia_addr.sin_addr.s_addr == INADDR_ANY))
915 /* Clash avoidance: Skip p2p interfaces with both addresses are equal */
916 if ((ifp->if_flags & IFF_POINTOPOINT) &&
917 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
920 /* Case 2: skip /32 prefixes */
921 if (!(ifp->if_flags & IFF_POINTOPOINT) &&
922 (ia->ia_sockmask.sin_addr.s_addr == INADDR_BROADCAST))
929 * Calculate "prefix" route corresponding to @ia.
932 ia_getrtprefix(const struct in_ifaddr *ia, struct in_addr *prefix, struct in_addr *mask)
935 if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) {
936 /* Case 3: return host route for dstaddr */
937 *prefix = ia->ia_dstaddr.sin_addr;
938 mask->s_addr = INADDR_BROADCAST;
939 } else if (ia->ia_ifp->if_flags & IFF_LOOPBACK) {
940 /* Case 4: return host route for ifaddr */
941 *prefix = ia->ia_addr.sin_addr;
942 mask->s_addr = INADDR_BROADCAST;
944 /* Cases 1,2: return actual ia prefix */
945 *prefix = ia->ia_addr.sin_addr;
946 *mask = ia->ia_sockmask.sin_addr;
947 prefix->s_addr &= mask->s_addr;
952 * Adds or delete interface "prefix" route corresponding to @ifa.
953 * Returns 0 on success or errno.
956 in_handle_ifaddr_route(int cmd, struct in_ifaddr *ia)
958 struct ifaddr *ifa = &ia->ia_ifa;
959 struct in_addr daddr, maddr;
960 struct epoch_tracker et;
963 ia_getrtprefix(ia, &daddr, &maddr);
965 int plen = bitcount32(maddr.s_addr);
967 struct sockaddr_in dst = {
968 .sin_family = AF_INET,
969 .sin_len = sizeof(struct sockaddr_in),
970 .sin_addr.s_addr = daddr.s_addr & maddr.s_addr,
973 struct ifnet *ifp = ia->ia_ifp;
975 if ((maddr.s_addr == INADDR_BROADCAST) &&
976 (!(ia->ia_ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))) {
977 /* Case 2: host route on broadcast interface */
981 uint32_t fibnum = ifa->ifa_ifp->if_fib;
983 error = in_handle_prefix_route(fibnum, cmd, (struct sockaddr *)&dst,
991 * Check if we have a route for the given prefix already.
994 in_hasrtprefix(struct in_ifaddr *target)
996 struct epoch_tracker et;
997 struct in_ifaddr *ia;
998 struct in_addr prefix, mask, p, m;
1001 ia_getrtprefix(target, &prefix, &mask);
1003 /* Look for an existing address with the same prefix, mask, and fib */
1004 NET_EPOCH_ENTER(et);
1005 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1006 ia_getrtprefix(ia, &p, &m);
1008 if (prefix.s_addr != p.s_addr ||
1009 mask.s_addr != m.s_addr)
1012 if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
1016 * If we got a matching prefix route inserted by other
1017 * interface address, we are done here.
1019 if (ia->ia_flags & IFA_ROUTE) {
1030 in_addprefix(struct in_ifaddr *target)
1034 if (in_hasrtprefix(target)) {
1038 rt_addrmsg(RTM_ADD, &target->ia_ifa,
1039 target->ia_ifp->if_fib);
1045 * No-one seem to have this prefix route, so we try to insert it.
1047 rt_addrmsg(RTM_ADD, &target->ia_ifa, target->ia_ifp->if_fib);
1048 error = in_handle_ifaddr_route(RTM_ADD, target);
1050 target->ia_flags |= IFA_ROUTE;
1055 * Removes either all lle entries for given @ia, or lle
1056 * corresponding to @ia address.
1059 in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags)
1061 struct sockaddr_in addr, mask;
1062 struct sockaddr *saddr, *smask;
1065 saddr = (struct sockaddr *)&addr;
1066 bzero(&addr, sizeof(addr));
1067 addr.sin_len = sizeof(addr);
1068 addr.sin_family = AF_INET;
1069 smask = (struct sockaddr *)&mask;
1070 bzero(&mask, sizeof(mask));
1071 mask.sin_len = sizeof(mask);
1072 mask.sin_family = AF_INET;
1073 mask.sin_addr.s_addr = ia->ia_subnetmask;
1078 * Remove all L2 entries matching given prefix.
1079 * Convert address to host representation to avoid
1080 * doing this on every callback. ia_subnetmask is already
1081 * stored in host representation.
1083 addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr);
1084 lltable_prefix_free(AF_INET, saddr, smask, flags);
1086 /* Remove interface address only */
1087 addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr;
1088 lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr);
1093 * If there is no other address in the system that can serve a route to the
1094 * same prefix, remove the route. Hand over the route to the new address
1098 in_scrubprefix(struct in_ifaddr *target, u_int flags)
1100 struct epoch_tracker et;
1101 struct in_ifaddr *ia;
1102 struct in_addr prefix, mask, p, m;
1106 * Remove the loopback route to the interface address.
1108 if (ia_need_loopback_route(target) && (flags & LLE_STATIC)) {
1109 struct in_ifaddr *eia;
1111 eia = in_localip_more(target);
1114 error = ifa_switch_loopback_route((struct ifaddr *)eia,
1115 (struct sockaddr *)&target->ia_addr);
1116 ifa_free(&eia->ia_ifa);
1118 error = ifa_del_loopback_route((struct ifaddr *)target,
1119 (struct sockaddr *)&target->ia_addr);
1123 ia_getrtprefix(target, &prefix, &mask);
1125 if ((target->ia_flags & IFA_ROUTE) == 0) {
1126 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1129 * Removing address from !IFF_UP interface or
1130 * prefix which exists on other interface (along with route).
1131 * No entries should exist here except target addr.
1132 * Given that, delete this entry only.
1134 in_scrubprefixlle(target, 0, flags);
1138 NET_EPOCH_ENTER(et);
1139 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1140 ia_getrtprefix(ia, &p, &m);
1142 if (prefix.s_addr != p.s_addr ||
1143 mask.s_addr != m.s_addr)
1146 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1150 * If we got a matching prefix address, move IFA_ROUTE and
1151 * the route itself to it. Make sure that routing daemons
1154 if ((ia->ia_flags & IFA_ROUTE) == 0) {
1155 ifa_ref(&ia->ia_ifa);
1157 error = in_handle_ifaddr_route(RTM_DELETE, target);
1159 target->ia_flags &= ~IFA_ROUTE;
1161 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
1163 /* Scrub all entries IFF interface is different */
1164 in_scrubprefixlle(target, target->ia_ifp != ia->ia_ifp,
1166 error = in_handle_ifaddr_route(RTM_ADD, ia);
1168 ia->ia_flags |= IFA_ROUTE;
1170 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
1172 ifa_free(&ia->ia_ifa);
1179 * remove all L2 entries on the given prefix
1181 in_scrubprefixlle(target, 1, flags);
1184 * As no-one seem to have this prefix, we can remove the route.
1186 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1187 error = in_handle_ifaddr_route(RTM_DELETE, target);
1189 target->ia_flags &= ~IFA_ROUTE;
1191 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
1196 in_ifscrub_all(void)
1199 struct ifaddr *ifa, *nifa;
1200 struct ifaliasreq ifr;
1203 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1204 /* Cannot lock here - lock recursion. */
1205 /* NET_EPOCH_ENTER(et); */
1206 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1207 if (ifa->ifa_addr->sa_family != AF_INET)
1211 * This is ugly but the only way for legacy IP to
1212 * cleanly remove addresses and everything attached.
1214 bzero(&ifr, sizeof(ifr));
1215 ifr.ifra_addr = *ifa->ifa_addr;
1216 if (ifa->ifa_dstaddr)
1217 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1218 (void)in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr,
1221 /* NET_EPOCH_EXIT(et); */
1222 in_purgemaddrs(ifp);
1223 igmp_domifdetach(ifp);
1229 in_ifaddr_broadcast(struct in_addr in, struct in_ifaddr *ia)
1232 return ((in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1234 * Optionally check for old-style (host 0) broadcast, but
1235 * taking into account that RFC 3021 obsoletes it.
1237 (V_broadcast_lowest && ia->ia_subnetmask != IN_RFC3021_MASK &&
1238 ntohl(in.s_addr) == ia->ia_subnet)) &&
1240 * Check for an all one subnetmask. These
1241 * only exist when an interface gets a secondary
1244 ia->ia_subnetmask != (u_long)0xffffffff);
1248 * Return 1 if the address might be a local broadcast address.
1251 in_broadcast(struct in_addr in, struct ifnet *ifp)
1258 if (in.s_addr == INADDR_BROADCAST ||
1259 in.s_addr == INADDR_ANY)
1261 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1265 * Look through the list of addresses for a match
1266 * with a broadcast address.
1268 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1269 if (ifa->ifa_addr->sa_family == AF_INET &&
1270 in_ifaddr_broadcast(in, (struct in_ifaddr *)ifa)) {
1278 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1281 in_ifdetach(struct ifnet *ifp)
1284 in_pcbpurgeif0(&V_ripcbinfo, ifp);
1285 in_pcbpurgeif0(&V_udbinfo, ifp);
1286 in_pcbpurgeif0(&V_ulitecbinfo, ifp);
1287 in_purgemaddrs(ifp);
1291 * Make sure all multicast deletions invoking if_ioctl() are
1292 * completed before returning. Else we risk accessing a freed
1293 * ifnet structure pointer.
1295 inm_release_wait(NULL);
1299 in_ifnet_event(void *arg __unused, struct ifnet *ifp, int event)
1301 struct epoch_tracker et;
1303 struct in_ifaddr *ia;
1306 NET_EPOCH_ENTER(et);
1308 case IFNET_EVENT_DOWN:
1309 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1310 if (ifa->ifa_addr->sa_family != AF_INET)
1312 ia = (struct in_ifaddr *)ifa;
1313 if ((ia->ia_flags & IFA_ROUTE) == 0)
1317 * in_scrubprefix() kills the interface route.
1319 in_scrubprefix(ia, 0);
1321 * in_ifadown gets rid of all the rest of the
1322 * routes. This is not quite the right thing
1323 * to do, but at least if we are running a
1324 * routing process they will come back.
1331 case IFNET_EVENT_UP:
1332 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1333 if (ifa->ifa_addr->sa_family != AF_INET)
1335 ia = (struct in_ifaddr *)ifa;
1336 if (ia->ia_flags & IFA_ROUTE)
1339 error = ifa_del_loopback_route(ifa, ifa->ifa_addr);
1340 rt_addrmsg(RTM_ADD, ifa, ifa->ifa_ifp->if_fib);
1341 error = in_handle_ifaddr_route(RTM_ADD, ia);
1343 ia->ia_flags |= IFA_ROUTE;
1344 error = ifa_add_loopback_route(ifa, ifa->ifa_addr);
1351 EVENTHANDLER_DEFINE(ifnet_event, in_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
1354 * Delete all IPv4 multicast address records, and associated link-layer
1355 * multicast address records, associated with ifp.
1356 * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1357 * XXX This should not race with ifma_protospec being set during
1358 * a new allocation, if it does, we have bigger problems.
1361 in_purgemaddrs(struct ifnet *ifp)
1363 struct in_multi_head purgeinms;
1364 struct in_multi *inm;
1365 struct ifmultiaddr *ifma, *next;
1367 SLIST_INIT(&purgeinms);
1368 IN_MULTI_LIST_LOCK();
1371 * Extract list of in_multi associated with the detaching ifp
1372 * which the PF_INET layer is about to release.
1373 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1374 * by code further down.
1378 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) {
1379 if (ifma->ifma_addr->sa_family != AF_INET ||
1380 ifma->ifma_protospec == NULL)
1382 inm = (struct in_multi *)ifma->ifma_protospec;
1383 inm_rele_locked(&purgeinms, inm);
1384 if (__predict_false(ifma_restart)) {
1385 ifma_restart = true;
1389 IF_ADDR_WUNLOCK(ifp);
1391 inm_release_list_deferred(&purgeinms);
1393 IN_MULTI_LIST_UNLOCK();
1397 struct llentry base;
1400 #define IN_LLTBL_DEFAULT_HSIZE 32
1401 #define IN_LLTBL_HASH(k, h) \
1402 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
1405 * Do actual deallocation of @lle.
1408 in_lltable_destroy_lle_unlocked(epoch_context_t ctx)
1410 struct llentry *lle;
1412 lle = __containerof(ctx, struct llentry, lle_epoch_ctx);
1413 LLE_LOCK_DESTROY(lle);
1414 LLE_REQ_DESTROY(lle);
1415 free(lle, M_LLTABLE);
1419 * Called by LLE_FREE_LOCKED when number of references
1423 in_lltable_destroy_lle(struct llentry *lle)
1427 NET_EPOCH_CALL(in_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
1430 static struct llentry *
1431 in_lltable_new(struct in_addr addr4, u_int flags)
1433 struct in_llentry *lle;
1435 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
1436 if (lle == NULL) /* NB: caller generates msg */
1440 * For IPv4 this will trigger "arpresolve" to generate
1443 lle->base.la_expire = time_uptime; /* mark expired */
1444 lle->base.r_l3addr.addr4 = addr4;
1445 lle->base.lle_refcnt = 1;
1446 lle->base.lle_free = in_lltable_destroy_lle;
1447 LLE_LOCK_INIT(&lle->base);
1448 LLE_REQ_INIT(&lle->base);
1449 callout_init(&lle->base.lle_timer, 1);
1451 return (&lle->base);
1454 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1455 ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
1458 in_lltable_match_prefix(const struct sockaddr *saddr,
1459 const struct sockaddr *smask, u_int flags, struct llentry *lle)
1461 struct in_addr addr, mask, lle_addr;
1463 addr = ((const struct sockaddr_in *)saddr)->sin_addr;
1464 mask = ((const struct sockaddr_in *)smask)->sin_addr;
1465 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
1467 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
1470 if (lle->la_flags & LLE_IFADDR) {
1472 * Delete LLE_IFADDR records IFF address & flag matches.
1473 * Note that addr is the interface address within prefix
1475 * Note also we should handle 'ifdown' cases without removing
1478 if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0)
1483 /* flags & LLE_STATIC means deleting both dynamic and static entries */
1484 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
1491 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
1493 size_t pkts_dropped;
1495 LLE_WLOCK_ASSERT(lle);
1496 KASSERT(llt != NULL, ("lltable is NULL"));
1498 /* Unlink entry from table if not already */
1499 if ((lle->la_flags & LLE_LINKED) != 0) {
1500 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
1501 lltable_unlink_entry(llt, lle);
1504 /* Drop hold queue */
1505 pkts_dropped = llentry_free(lle);
1506 ARPSTAT_ADD(dropped, pkts_dropped);
1510 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1512 struct nhop_object *nh;
1513 struct in_addr addr;
1515 KASSERT(l3addr->sa_family == AF_INET,
1516 ("sin_family %d", l3addr->sa_family));
1518 addr = ((const struct sockaddr_in *)l3addr)->sin_addr;
1520 nh = fib4_lookup(ifp->if_fib, addr, 0, NHR_NONE, 0);
1525 * If the gateway for an existing host route matches the target L3
1526 * address, which is a special route inserted by some implementation
1527 * such as MANET, and the interface is of the correct type, then
1528 * allow for ARP to proceed.
1530 if (nh->nh_flags & NHF_GATEWAY) {
1531 if (!(nh->nh_flags & NHF_HOST) || nh->nh_ifp->if_type != IFT_ETHER ||
1532 (nh->nh_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1533 memcmp(nh->gw_sa.sa_data, l3addr->sa_data,
1534 sizeof(in_addr_t)) != 0) {
1540 * Make sure that at least the destination address is covered
1541 * by the route. This is for handling the case where 2 or more
1542 * interfaces have the same prefix. An incoming packet arrives
1543 * on one interface and the corresponding outgoing packet leaves
1544 * another interface.
1546 if ((nh->nh_ifp != ifp) && (nh->nh_flags & NHF_HOST) == 0) {
1547 struct in_ifaddr *ia = (struct in_ifaddr *)ifaof_ifpforaddr(l3addr, ifp);
1548 struct in_addr dst_addr, mask_addr;
1554 * ifaof_ifpforaddr() returns _best matching_ IFA.
1555 * It is possible that ifa prefix does not cover our address.
1556 * Explicitly verify and fail if that's the case.
1558 dst_addr = IA_SIN(ia)->sin_addr;
1559 mask_addr.s_addr = htonl(ia->ia_subnetmask);
1561 if (!IN_ARE_MASKED_ADDR_EQUAL(dst_addr, addr, mask_addr))
1568 static inline uint32_t
1569 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
1572 return (IN_LLTBL_HASH(dst.s_addr, hsize));
1576 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
1579 return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
1583 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
1585 struct sockaddr_in *sin;
1587 sin = (struct sockaddr_in *)sa;
1588 bzero(sin, sizeof(*sin));
1589 sin->sin_family = AF_INET;
1590 sin->sin_len = sizeof(*sin);
1591 sin->sin_addr = lle->r_l3addr.addr4;
1594 static inline struct llentry *
1595 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
1597 struct llentry *lle;
1598 struct llentries *lleh;
1601 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
1602 lleh = &llt->lle_head[hashidx];
1603 CK_LIST_FOREACH(lle, lleh, lle_next) {
1604 if (lle->la_flags & LLE_DELETED)
1606 if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
1614 in_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
1617 lle->la_flags |= LLE_DELETED;
1618 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1620 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1625 static struct llentry *
1626 in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1628 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1629 struct ifnet *ifp = llt->llt_ifp;
1630 struct llentry *lle;
1631 char linkhdr[LLE_MAX_LINKHDR];
1635 KASSERT(l3addr->sa_family == AF_INET,
1636 ("sin_family %d", l3addr->sa_family));
1639 * A route that covers the given address must have
1640 * been installed 1st because we are doing a resolution,
1643 if (!(flags & LLE_IFADDR) &&
1644 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1647 lle = in_lltable_new(sin->sin_addr, flags);
1649 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1652 lle->la_flags = flags;
1653 if (flags & LLE_STATIC)
1654 lle->r_flags |= RLLE_VALID;
1655 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
1656 linkhdrsize = LLE_MAX_LINKHDR;
1657 if (lltable_calc_llheader(ifp, AF_INET, IF_LLADDR(ifp),
1658 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
1659 in_lltable_free_entry(llt, lle);
1662 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
1664 lle->la_flags |= LLE_STATIC;
1665 lle->r_flags |= (RLLE_VALID | RLLE_IFADDR);
1672 * Return NULL if not found or marked for deletion.
1673 * If found return lle read locked.
1675 static struct llentry *
1676 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1678 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1679 struct llentry *lle;
1681 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
1682 KASSERT(l3addr->sa_family == AF_INET,
1683 ("sin_family %d", l3addr->sa_family));
1684 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) !=
1685 (LLE_UNLOCKED | LLE_EXCLUSIVE),
1686 ("wrong lle request flags: %#x", flags));
1688 lle = in_lltable_find_dst(llt, sin->sin_addr);
1691 if (flags & LLE_UNLOCKED)
1694 if (flags & LLE_EXCLUSIVE)
1700 * If the afdata lock is not held, the LLE may have been unlinked while
1701 * we were blocked on the LLE lock. Check for this case.
1703 if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) {
1704 if (flags & LLE_EXCLUSIVE)
1714 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
1715 struct sysctl_req *wr)
1717 struct ifnet *ifp = llt->llt_ifp;
1720 struct rt_msghdr rtm;
1721 struct sockaddr_in sin;
1722 struct sockaddr_dl sdl;
1724 struct sockaddr_dl *sdl;
1727 bzero(&arpc, sizeof(arpc));
1728 /* skip deleted entries */
1729 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1731 /* Skip if jailed and not a valid IP of the prison. */
1732 lltable_fill_sa_entry(lle,(struct sockaddr *)&arpc.sin);
1733 if (prison_if(wr->td->td_ucred, (struct sockaddr *)&arpc.sin) != 0)
1736 * produce a msg made of:
1738 * struct sockaddr_in; (IPv4)
1739 * struct sockaddr_dl;
1741 arpc.rtm.rtm_msglen = sizeof(arpc);
1742 arpc.rtm.rtm_version = RTM_VERSION;
1743 arpc.rtm.rtm_type = RTM_GET;
1744 arpc.rtm.rtm_flags = RTF_UP;
1745 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1748 if (lle->la_flags & LLE_PUB)
1749 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1752 sdl->sdl_family = AF_LINK;
1753 sdl->sdl_len = sizeof(*sdl);
1754 sdl->sdl_index = ifp->if_index;
1755 sdl->sdl_type = ifp->if_type;
1756 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1757 sdl->sdl_alen = ifp->if_addrlen;
1758 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1761 bzero(LLADDR(sdl), ifp->if_addrlen);
1764 arpc.rtm.rtm_rmx.rmx_expire =
1765 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1766 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1767 if (lle->la_flags & LLE_STATIC)
1768 arpc.rtm.rtm_flags |= RTF_STATIC;
1769 if (lle->la_flags & LLE_IFADDR)
1770 arpc.rtm.rtm_flags |= RTF_PINNED;
1771 arpc.rtm.rtm_index = ifp->if_index;
1772 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1778 in_lltable_post_resolved(struct lltable *llt, struct llentry *lle)
1780 struct ifnet *ifp = llt->llt_ifp;
1782 /* gratuitous ARP */
1783 if ((lle->la_flags & LLE_PUB) != 0)
1784 arprequest(ifp, &lle->r_l3addr.addr4, &lle->r_l3addr.addr4,
1788 static struct lltable *
1789 in_lltattach(struct ifnet *ifp)
1791 struct lltable *llt;
1793 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
1794 llt->llt_af = AF_INET;
1797 llt->llt_lookup = in_lltable_lookup;
1798 llt->llt_alloc_entry = in_lltable_alloc;
1799 llt->llt_delete_entry = in_lltable_delete_entry;
1800 llt->llt_dump_entry = in_lltable_dump_entry;
1801 llt->llt_hash = in_lltable_hash;
1802 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
1803 llt->llt_free_entry = in_lltable_free_entry;
1804 llt->llt_match_prefix = in_lltable_match_prefix;
1805 llt->llt_mark_used = llentry_mark_used;
1806 llt->llt_post_resolved = in_lltable_post_resolved;
1813 in_lltable_get(struct ifnet *ifp)
1815 struct lltable *llt = NULL;
1817 void *afdata_ptr = ifp->if_afdata[AF_INET];
1818 if (afdata_ptr != NULL)
1819 llt = ((struct in_ifinfo *)afdata_ptr)->ii_llt;
1824 in_domifattach(struct ifnet *ifp)
1826 struct in_ifinfo *ii;
1828 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1830 ii->ii_llt = in_lltattach(ifp);
1831 ii->ii_igmp = igmp_domifattach(ifp);
1837 in_domifdetach(struct ifnet *ifp, void *aux)
1839 struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1841 igmp_domifdetach(ifp);
1842 lltable_free(ii->ii_llt);