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 #include <sys/param.h>
39 #include <sys/eventhandler.h>
40 #include <sys/systm.h>
41 #include <sys/sockio.h>
42 #include <sys/malloc.h>
44 #include <sys/socket.h>
46 #include <sys/kernel.h>
49 #include <sys/rmlock.h>
50 #include <sys/sysctl.h>
51 #include <sys/syslog.h>
55 #include <net/if_var.h>
56 #include <net/if_arp.h>
57 #include <net/if_dl.h>
58 #include <net/if_llatbl.h>
59 #include <net/if_types.h>
60 #include <net/route.h>
61 #include <net/route/nhop.h>
62 #include <net/route/route_ctl.h>
65 #include <netinet/if_ether.h>
66 #include <netinet/in.h>
67 #include <netinet/in_fib.h>
68 #include <netinet/in_var.h>
69 #include <netinet/in_pcb.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/ip_carp.h>
72 #include <netinet/igmp_var.h>
73 #include <netinet/udp.h>
74 #include <netinet/udp_var.h>
76 static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
77 static int in_difaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
78 static int in_gifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
80 static void in_socktrim(struct sockaddr_in *);
81 static void in_purgemaddrs(struct ifnet *);
83 static bool ia_need_loopback_route(const struct in_ifaddr *);
85 VNET_DEFINE_STATIC(int, nosameprefix);
86 #define V_nosameprefix VNET(nosameprefix)
87 SYSCTL_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_VNET | CTLFLAG_RW,
88 &VNET_NAME(nosameprefix), 0,
89 "Refuse to create same prefixes on different interfaces");
91 VNET_DEFINE_STATIC(bool, broadcast_lowest);
92 #define V_broadcast_lowest VNET(broadcast_lowest)
93 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, broadcast_lowest, CTLFLAG_VNET | CTLFLAG_RW,
94 &VNET_NAME(broadcast_lowest), 0,
95 "Treat lowest address on a subnet (host 0) as broadcast");
97 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
98 #define V_ripcbinfo VNET(ripcbinfo)
100 static struct sx in_control_sx;
101 SX_SYSINIT(in_control_sx, &in_control_sx, "in_control");
104 * Return 1 if an internet address is for a ``local'' host
105 * (one to which we have a connection).
108 in_localaddr(struct in_addr in)
110 struct rm_priotracker in_ifa_tracker;
111 u_long i = ntohl(in.s_addr);
112 struct in_ifaddr *ia;
114 IN_IFADDR_RLOCK(&in_ifa_tracker);
115 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
116 if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
117 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
121 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
126 * Return 1 if an internet address is for the local host and configured
127 * on one of its interfaces.
130 in_localip(struct in_addr in)
132 struct rm_priotracker in_ifa_tracker;
133 struct in_ifaddr *ia;
135 IN_IFADDR_RLOCK(&in_ifa_tracker);
136 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
137 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
138 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
142 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
147 * Return 1 if an internet address is configured on an interface.
150 in_ifhasaddr(struct ifnet *ifp, struct in_addr in)
153 struct in_ifaddr *ia;
157 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
158 if (ifa->ifa_addr->sa_family != AF_INET)
160 ia = (struct in_ifaddr *)ifa;
161 if (ia->ia_addr.sin_addr.s_addr == in.s_addr)
169 * Return a reference to the interface address which is different to
170 * the supplied one but with same IP address value.
172 static struct in_ifaddr *
173 in_localip_more(struct in_ifaddr *original_ia)
175 struct rm_priotracker in_ifa_tracker;
176 in_addr_t original_addr = IA_SIN(original_ia)->sin_addr.s_addr;
177 uint32_t original_fib = original_ia->ia_ifa.ifa_ifp->if_fib;
178 struct in_ifaddr *ia;
180 IN_IFADDR_RLOCK(&in_ifa_tracker);
181 LIST_FOREACH(ia, INADDR_HASH(original_addr), ia_hash) {
182 in_addr_t addr = IA_SIN(ia)->sin_addr.s_addr;
183 uint32_t fib = ia->ia_ifa.ifa_ifp->if_fib;
184 if (!V_rt_add_addr_allfibs && (original_fib != fib))
186 if ((original_ia != ia) && (original_addr == addr)) {
187 ifa_ref(&ia->ia_ifa);
188 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
192 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
198 * Tries to find first IPv4 address in the provided fib.
199 * Prefers non-loopback addresses and return loopback IFF
200 * @loopback_ok is set.
202 * Returns ifa or NULL.
205 in_findlocal(uint32_t fibnum, bool loopback_ok)
207 struct rm_priotracker in_ifa_tracker;
208 struct in_ifaddr *ia = NULL, *ia_lo = NULL;
212 IN_IFADDR_RLOCK(&in_ifa_tracker);
213 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
214 uint32_t ia_fib = ia->ia_ifa.ifa_ifp->if_fib;
215 if (!V_rt_add_addr_allfibs && (fibnum != ia_fib))
218 if (!IN_LOOPBACK(ntohl(IA_SIN(ia)->sin_addr.s_addr)))
223 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
232 * Determine whether an IP address is in a reserved set of addresses
233 * that may not be forwarded, or whether datagrams to that destination
237 in_canforward(struct in_addr in)
239 u_long i = ntohl(in.s_addr);
241 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i) ||
242 IN_ZERONET(i) || IN_LOOPBACK(i))
248 * Trim a mask in a sockaddr
251 in_socktrim(struct sockaddr_in *ap)
253 char *cplim = (char *) &ap->sin_addr;
254 char *cp = (char *) (&ap->sin_addr + 1);
257 while (--cp >= cplim)
259 (ap)->sin_len = cp - (char *) (ap) + 1;
265 * Generic internet control operations (ioctl's).
268 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
271 struct ifreq *ifr = (struct ifreq *)data;
272 struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
273 struct epoch_tracker et;
275 struct in_ifaddr *ia;
279 return (EADDRNOTAVAIL);
282 * Filter out 4 ioctls we implement directly. Forward the rest
283 * to specific functions and ifp->if_ioctl().
292 sx_xlock(&in_control_sx);
293 error = in_gifaddr_ioctl(cmd, data, ifp, td);
294 sx_xunlock(&in_control_sx);
297 sx_xlock(&in_control_sx);
298 error = in_difaddr_ioctl(cmd, data, ifp, td);
299 sx_xunlock(&in_control_sx);
301 case OSIOCAIFADDR: /* 9.x compat */
303 sx_xlock(&in_control_sx);
304 error = in_aifaddr_ioctl(cmd, data, ifp, td);
305 sx_xunlock(&in_control_sx);
311 /* We no longer support that old commands. */
314 if (ifp->if_ioctl == NULL)
316 return ((*ifp->if_ioctl)(ifp, cmd, data));
319 if (addr->sin_addr.s_addr != INADDR_ANY &&
320 prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0)
321 return (EADDRNOTAVAIL);
324 * Find address for this interface, if it exists. If an
325 * address was specified, find that one instead of the
326 * first one on the interface, if possible.
329 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
330 if (ifa->ifa_addr->sa_family != AF_INET)
332 ia = (struct in_ifaddr *)ifa;
333 if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
337 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
338 if (ifa->ifa_addr->sa_family == AF_INET) {
339 ia = (struct in_ifaddr *)ifa;
340 if (prison_check_ip4(td->td_ucred,
341 &ia->ia_addr.sin_addr) == 0)
347 return (EADDRNOTAVAIL);
357 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
361 *addr = ia->ia_broadaddr;
365 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
369 *addr = ia->ia_dstaddr;
373 *addr = ia->ia_sockmask;
383 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
385 const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
386 const struct sockaddr_in *addr = &ifra->ifra_addr;
387 const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
388 const struct sockaddr_in *mask = &ifra->ifra_mask;
389 const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
390 const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
391 struct epoch_tracker et;
393 struct in_ifaddr *ia;
397 error = priv_check(td, PRIV_NET_ADDIFADDR);
402 * ifra_addr must be present and be of INET family.
403 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
405 if (addr->sin_len != sizeof(struct sockaddr_in) ||
406 addr->sin_family != AF_INET)
408 if (broadaddr->sin_len != 0 &&
409 (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
410 broadaddr->sin_family != AF_INET))
412 if (mask->sin_len != 0 &&
413 (mask->sin_len != sizeof(struct sockaddr_in) ||
414 mask->sin_family != AF_INET))
416 if ((ifp->if_flags & IFF_POINTOPOINT) &&
417 (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
418 dstaddr->sin_addr.s_addr == INADDR_ANY))
419 return (EDESTADDRREQ);
420 if (vhid != 0 && carp_attach_p == NULL)
421 return (EPROTONOSUPPORT);
424 * See whether address already exist.
429 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
430 struct in_ifaddr *it;
432 if (ifa->ifa_addr->sa_family != AF_INET)
435 it = (struct in_ifaddr *)ifa;
436 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
437 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0)
445 (void )in_difaddr_ioctl(cmd, data, ifp, td);
447 ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
448 ia = (struct in_ifaddr *)ifa;
449 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
450 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
451 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
452 callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock,
453 CALLOUT_RETURNUNLOCKED);
457 if (mask->sin_len != 0) {
458 ia->ia_sockmask = *mask;
459 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
461 in_addr_t i = ntohl(addr->sin_addr.s_addr);
464 * Be compatible with network classes, if netmask isn't
465 * supplied, guess it based on classes.
468 ia->ia_subnetmask = IN_CLASSA_NET;
469 else if (IN_CLASSB(i))
470 ia->ia_subnetmask = IN_CLASSB_NET;
472 ia->ia_subnetmask = IN_CLASSC_NET;
473 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
475 ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
476 in_socktrim(&ia->ia_sockmask);
478 if (ifp->if_flags & IFF_BROADCAST) {
479 if (broadaddr->sin_len != 0) {
480 ia->ia_broadaddr = *broadaddr;
481 } else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
482 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
483 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
484 ia->ia_broadaddr.sin_family = AF_INET;
486 ia->ia_broadaddr.sin_addr.s_addr =
487 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
488 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
489 ia->ia_broadaddr.sin_family = AF_INET;
493 if (ifp->if_flags & IFF_POINTOPOINT)
494 ia->ia_dstaddr = *dstaddr;
497 error = (*carp_attach_p)(&ia->ia_ifa, vhid);
502 /* if_addrhead is already referenced by ifa_alloc() */
504 CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
505 IF_ADDR_WUNLOCK(ifp);
507 ifa_ref(ifa); /* in_ifaddrhead */
509 CK_STAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
510 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
514 * Give the interface a chance to initialize
515 * if this is its first address,
516 * and to validate the address if necessary.
518 if (ifp->if_ioctl != NULL) {
519 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
525 * Add route for the network.
528 error = in_addprefix(ia);
534 * Add a loopback route to self.
536 if (vhid == 0 && ia_need_loopback_route(ia)) {
537 struct in_ifaddr *eia;
539 eia = in_localip_more(ia);
542 error = ifa_add_loopback_route((struct ifaddr *)ia,
543 (struct sockaddr *)&ia->ia_addr);
547 ifa_free(&eia->ia_ifa);
550 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
551 struct in_addr allhosts_addr;
552 struct in_ifinfo *ii;
554 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
555 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
557 error = in_joingroup(ifp, &allhosts_addr, NULL,
562 * Note: we don't need extra reference for ifa, since we called
563 * with sx lock held, and ifaddr can not be deleted in concurrent
566 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, ifa, IFADDR_EVENT_ADD);
572 (void )in_scrubprefix(ia, LLE_STATIC);
575 if (ia->ia_ifa.ifa_carp)
576 (*carp_detach_p)(&ia->ia_ifa, false);
579 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
580 IF_ADDR_WUNLOCK(ifp);
581 ifa_free(&ia->ia_ifa); /* if_addrhead */
584 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
585 LIST_REMOVE(ia, ia_hash);
587 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
593 in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
595 const struct ifreq *ifr = (struct ifreq *)data;
596 const struct sockaddr_in *addr = (const struct sockaddr_in *)
599 struct in_ifaddr *ia;
600 bool deleteAny, iaIsLast;
604 error = priv_check(td, PRIV_NET_DELIFADDR);
609 if (addr->sin_len != sizeof(struct sockaddr_in) ||
610 addr->sin_family != AF_INET)
618 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
619 struct in_ifaddr *it;
621 if (ifa->ifa_addr->sa_family != AF_INET)
624 it = (struct in_ifaddr *)ifa;
625 if (deleteAny && ia == NULL && (td == NULL ||
626 prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0))
629 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
630 (td == NULL || prison_check_ip4(td->td_ucred,
631 &addr->sin_addr) == 0))
639 IF_ADDR_WUNLOCK(ifp);
640 return (EADDRNOTAVAIL);
643 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
644 IF_ADDR_WUNLOCK(ifp);
645 ifa_free(&ia->ia_ifa); /* if_addrhead */
648 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
649 LIST_REMOVE(ia, ia_hash);
653 * in_scrubprefix() kills the interface route.
655 in_scrubprefix(ia, LLE_STATIC);
658 * in_ifadown gets rid of all the rest of
659 * the routes. This is not quite the right
660 * thing to do, but at least if we are running
661 * a routing process they will come back.
663 in_ifadown(&ia->ia_ifa, 1);
665 if (ia->ia_ifa.ifa_carp)
666 (*carp_detach_p)(&ia->ia_ifa, cmd == SIOCAIFADDR);
669 * If this is the last IPv4 address configured on this
670 * interface, leave the all-hosts group.
671 * No state-change report need be transmitted.
673 if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
674 struct in_ifinfo *ii;
676 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
677 if (ii->ii_allhosts) {
678 (void)in_leavegroup(ii->ii_allhosts, NULL);
679 ii->ii_allhosts = NULL;
684 if (callout_stop(&ia->ia_garp_timer) == 1) {
685 ifa_free(&ia->ia_ifa);
687 IF_ADDR_WUNLOCK(ifp);
689 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
691 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
697 in_gifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
699 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
700 const struct sockaddr_in *addr = &ifra->ifra_addr;
701 struct epoch_tracker et;
703 struct in_ifaddr *ia;
706 * ifra_addr must be present and be of INET family.
708 if (addr->sin_len != sizeof(struct sockaddr_in) ||
709 addr->sin_family != AF_INET)
713 * See whether address exist.
717 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
718 struct in_ifaddr *it;
720 if (ifa->ifa_addr->sa_family != AF_INET)
723 it = (struct in_ifaddr *)ifa;
724 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
725 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0) {
732 return (EADDRNOTAVAIL);
735 ifra->ifra_mask = ia->ia_sockmask;
736 if ((ifp->if_flags & IFF_POINTOPOINT) &&
737 ia->ia_dstaddr.sin_family == AF_INET)
738 ifra->ifra_dstaddr = ia->ia_dstaddr;
739 else if ((ifp->if_flags & IFF_BROADCAST) &&
740 ia->ia_broadaddr.sin_family == AF_INET)
741 ifra->ifra_broadaddr = ia->ia_broadaddr;
743 memset(&ifra->ifra_broadaddr, 0,
744 sizeof(ifra->ifra_broadaddr));
751 in_match_ifaddr(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
754 if (nh->nh_ifa == (struct ifaddr *)arg)
761 in_handle_prefix_route(uint32_t fibnum, int cmd,
762 struct sockaddr_in *dst, struct sockaddr_in *netmask, struct ifaddr *ifa,
768 /* Prepare gateway */
769 struct sockaddr_dl_short sdl = {
770 .sdl_family = AF_LINK,
771 .sdl_len = sizeof(struct sockaddr_dl_short),
772 .sdl_type = ifa->ifa_ifp->if_type,
773 .sdl_index = ifa->ifa_ifp->if_index,
776 struct rt_addrinfo info = {
779 .rti_flags = RTF_PINNED | ((netmask != NULL) ? 0 : RTF_HOST),
781 [RTAX_DST] = (struct sockaddr *)dst,
782 [RTAX_NETMASK] = (struct sockaddr *)netmask,
783 [RTAX_GATEWAY] = (struct sockaddr *)&sdl,
785 /* Ensure we delete the prefix IFF prefix ifa matches */
786 .rti_filter = in_match_ifaddr,
787 .rti_filterdata = ifa,
790 return (rib_handle_ifaddr_info(fibnum, cmd, &info));
794 * Routing table interaction with interface addresses.
796 * In general, two types of routes needs to be installed:
797 * a) "interface" or "prefix" route, telling user that the addresses
798 * behind the ifa prefix are reached directly.
799 * b) "loopback" route installed for the ifa address, telling user that
800 * the address belongs to local system.
802 * Handling for (a) and (b) differs in multi-fib aspects, hence they
803 * are implemented in different functions below.
805 * The cases above may intersect - /32 interface aliases results in
806 * the same prefix produced by (a) and (b). This blurs the definition
807 * of the "loopback" route and complicate interactions. The interaction
808 * table is defined below. The case numbers are used in the multiple
809 * functions below to refer to the particular test case.
811 * There can be multiple options:
812 * 1) Adding address with prefix on non-p2p/non-loopback interface.
813 * Example: 192.0.2.1/24. Action:
814 * * add "prefix" route towards 192.0.2.0/24 via @ia interface,
815 * using @ia as an address source.
816 * * add "loopback" route towards 192.0.2.1 via V_loif, saving
817 * @ia ifp in the gateway and using @ia as an address source.
819 * 2) Adding address with /32 mask to non-p2p/non-loopback interface.
820 * Example: 192.0.2.2/32. Action:
821 * * add "prefix" host route via V_loif, using @ia as an address source.
823 * 3) Adding address with or without prefix to p2p interface.
824 * Example: 10.0.0.1/24->10.0.0.2. Action:
825 * * add "prefix" host route towards 10.0.0.2 via this interface, using @ia
826 * as an address source. Note: no sense in installing full /24 as the interface
828 * * add "loopback" route towards 10.0.9.1 via V_loif, saving
829 * @ia ifp in the gateway and using @ia as an address source.
831 * 4) Adding address with or without prefix to loopback interface.
832 * Example: 192.0.2.1/24. Action:
833 * * add "prefix" host route via @ia interface, using @ia as an address source.
834 * Note: Skip installing /24 prefix as it would introduce TTL loop
835 * for the traffic destined to these addresses.
839 * Checks if @ia needs to install loopback route to @ia address via
840 * ifa_maintain_loopback_route().
842 * Return true on success.
845 ia_need_loopback_route(const struct in_ifaddr *ia)
847 struct ifnet *ifp = ia->ia_ifp;
849 /* Case 4: Skip loopback interfaces */
850 if ((ifp->if_flags & IFF_LOOPBACK) ||
851 (ia->ia_addr.sin_addr.s_addr == INADDR_ANY))
854 /* Clash avoidance: Skip p2p interfaces with both addresses are equal */
855 if ((ifp->if_flags & IFF_POINTOPOINT) &&
856 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
859 /* Case 2: skip /32 prefixes */
860 if (!(ifp->if_flags & IFF_POINTOPOINT) &&
861 (ia->ia_sockmask.sin_addr.s_addr == INADDR_BROADCAST))
868 * Calculate "prefix" route corresponding to @ia.
871 ia_getrtprefix(const struct in_ifaddr *ia, struct in_addr *prefix, struct in_addr *mask)
874 if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) {
875 /* Case 3: return host route for dstaddr */
876 *prefix = ia->ia_dstaddr.sin_addr;
877 mask->s_addr = INADDR_BROADCAST;
878 } else if (ia->ia_ifp->if_flags & IFF_LOOPBACK) {
879 /* Case 4: return host route for ifaddr */
880 *prefix = ia->ia_addr.sin_addr;
881 mask->s_addr = INADDR_BROADCAST;
883 /* Cases 1,2: return actual ia prefix */
884 *prefix = ia->ia_addr.sin_addr;
885 *mask = ia->ia_sockmask.sin_addr;
886 prefix->s_addr &= mask->s_addr;
891 * Adds or delete interface "prefix" route corresponding to @ifa.
892 * Returns 0 on success or errno.
895 in_handle_ifaddr_route(int cmd, struct in_ifaddr *ia)
897 struct ifaddr *ifa = &ia->ia_ifa;
898 struct in_addr daddr, maddr;
899 struct sockaddr_in *pmask;
900 struct epoch_tracker et;
903 ia_getrtprefix(ia, &daddr, &maddr);
905 struct sockaddr_in mask = {
906 .sin_family = AF_INET,
907 .sin_len = sizeof(struct sockaddr_in),
911 pmask = (maddr.s_addr != INADDR_BROADCAST) ? &mask : NULL;
913 struct sockaddr_in dst = {
914 .sin_family = AF_INET,
915 .sin_len = sizeof(struct sockaddr_in),
916 .sin_addr.s_addr = daddr.s_addr & maddr.s_addr,
919 struct ifnet *ifp = ia->ia_ifp;
921 if ((maddr.s_addr == INADDR_BROADCAST) &&
922 (!(ia->ia_ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))) {
923 /* Case 2: host route on broadcast interface */
927 uint32_t fibnum = ifa->ifa_ifp->if_fib;
929 error = in_handle_prefix_route(fibnum, cmd, &dst, pmask, ifa, ifp);
936 * Check if we have a route for the given prefix already.
939 in_hasrtprefix(struct in_ifaddr *target)
941 struct rm_priotracker in_ifa_tracker;
942 struct in_ifaddr *ia;
943 struct in_addr prefix, mask, p, m;
946 ia_getrtprefix(target, &prefix, &mask);
948 IN_IFADDR_RLOCK(&in_ifa_tracker);
949 /* Look for an existing address with the same prefix, mask, and fib */
950 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
951 ia_getrtprefix(ia, &p, &m);
953 if (prefix.s_addr != p.s_addr ||
954 mask.s_addr != m.s_addr)
957 if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
961 * If we got a matching prefix route inserted by other
962 * interface address, we are done here.
964 if (ia->ia_flags & IFA_ROUTE) {
969 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
975 in_addprefix(struct in_ifaddr *target)
979 if (in_hasrtprefix(target)) {
983 rt_addrmsg(RTM_ADD, &target->ia_ifa,
984 target->ia_ifp->if_fib);
990 * No-one seem to have this prefix route, so we try to insert it.
992 rt_addrmsg(RTM_ADD, &target->ia_ifa, target->ia_ifp->if_fib);
993 error = in_handle_ifaddr_route(RTM_ADD, target);
995 target->ia_flags |= IFA_ROUTE;
1000 * Removes either all lle entries for given @ia, or lle
1001 * corresponding to @ia address.
1004 in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags)
1006 struct sockaddr_in addr, mask;
1007 struct sockaddr *saddr, *smask;
1010 saddr = (struct sockaddr *)&addr;
1011 bzero(&addr, sizeof(addr));
1012 addr.sin_len = sizeof(addr);
1013 addr.sin_family = AF_INET;
1014 smask = (struct sockaddr *)&mask;
1015 bzero(&mask, sizeof(mask));
1016 mask.sin_len = sizeof(mask);
1017 mask.sin_family = AF_INET;
1018 mask.sin_addr.s_addr = ia->ia_subnetmask;
1023 * Remove all L2 entries matching given prefix.
1024 * Convert address to host representation to avoid
1025 * doing this on every callback. ia_subnetmask is already
1026 * stored in host representation.
1028 addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr);
1029 lltable_prefix_free(AF_INET, saddr, smask, flags);
1031 /* Remove interface address only */
1032 addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr;
1033 lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr);
1038 * If there is no other address in the system that can serve a route to the
1039 * same prefix, remove the route. Hand over the route to the new address
1043 in_scrubprefix(struct in_ifaddr *target, u_int flags)
1045 struct rm_priotracker in_ifa_tracker;
1046 struct in_ifaddr *ia;
1047 struct in_addr prefix, mask, p, m;
1051 * Remove the loopback route to the interface address.
1053 if (ia_need_loopback_route(target) && (flags & LLE_STATIC)) {
1054 struct in_ifaddr *eia;
1056 eia = in_localip_more(target);
1059 error = ifa_switch_loopback_route((struct ifaddr *)eia,
1060 (struct sockaddr *)&target->ia_addr);
1061 ifa_free(&eia->ia_ifa);
1063 error = ifa_del_loopback_route((struct ifaddr *)target,
1064 (struct sockaddr *)&target->ia_addr);
1068 ia_getrtprefix(target, &prefix, &mask);
1070 if ((target->ia_flags & IFA_ROUTE) == 0) {
1071 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1074 * Removing address from !IFF_UP interface or
1075 * prefix which exists on other interface (along with route).
1076 * No entries should exist here except target addr.
1077 * Given that, delete this entry only.
1079 in_scrubprefixlle(target, 0, flags);
1083 IN_IFADDR_RLOCK(&in_ifa_tracker);
1084 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1085 ia_getrtprefix(ia, &p, &m);
1087 if (prefix.s_addr != p.s_addr ||
1088 mask.s_addr != m.s_addr)
1091 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1095 * If we got a matching prefix address, move IFA_ROUTE and
1096 * the route itself to it. Make sure that routing daemons
1099 if ((ia->ia_flags & IFA_ROUTE) == 0) {
1100 ifa_ref(&ia->ia_ifa);
1101 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1102 error = in_handle_ifaddr_route(RTM_DELETE, target);
1104 target->ia_flags &= ~IFA_ROUTE;
1106 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
1108 /* Scrub all entries IFF interface is different */
1109 in_scrubprefixlle(target, target->ia_ifp != ia->ia_ifp,
1111 error = in_handle_ifaddr_route(RTM_ADD, ia);
1113 ia->ia_flags |= IFA_ROUTE;
1115 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
1117 ifa_free(&ia->ia_ifa);
1121 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1124 * remove all L2 entries on the given prefix
1126 in_scrubprefixlle(target, 1, flags);
1129 * As no-one seem to have this prefix, we can remove the route.
1131 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1132 error = in_handle_ifaddr_route(RTM_DELETE, target);
1134 target->ia_flags &= ~IFA_ROUTE;
1136 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
1141 in_ifscrub_all(void)
1144 struct ifaddr *ifa, *nifa;
1145 struct ifaliasreq ifr;
1148 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1149 /* Cannot lock here - lock recursion. */
1150 /* NET_EPOCH_ENTER(et); */
1151 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1152 if (ifa->ifa_addr->sa_family != AF_INET)
1156 * This is ugly but the only way for legacy IP to
1157 * cleanly remove addresses and everything attached.
1159 bzero(&ifr, sizeof(ifr));
1160 ifr.ifra_addr = *ifa->ifa_addr;
1161 if (ifa->ifa_dstaddr)
1162 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1163 (void)in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr,
1166 /* NET_EPOCH_EXIT(et); */
1167 in_purgemaddrs(ifp);
1168 igmp_domifdetach(ifp);
1174 in_ifaddr_broadcast(struct in_addr in, struct in_ifaddr *ia)
1177 return ((in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1179 * Optionally check for old-style (host 0) broadcast, but
1180 * taking into account that RFC 3021 obsoletes it.
1182 (V_broadcast_lowest && ia->ia_subnetmask != IN_RFC3021_MASK &&
1183 ntohl(in.s_addr) == ia->ia_subnet)) &&
1185 * Check for an all one subnetmask. These
1186 * only exist when an interface gets a secondary
1189 ia->ia_subnetmask != (u_long)0xffffffff);
1193 * Return 1 if the address might be a local broadcast address.
1196 in_broadcast(struct in_addr in, struct ifnet *ifp)
1203 if (in.s_addr == INADDR_BROADCAST ||
1204 in.s_addr == INADDR_ANY)
1206 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1210 * Look through the list of addresses for a match
1211 * with a broadcast address.
1213 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1214 if (ifa->ifa_addr->sa_family == AF_INET &&
1215 in_ifaddr_broadcast(in, (struct in_ifaddr *)ifa)) {
1223 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1226 in_ifdetach(struct ifnet *ifp)
1229 in_pcbpurgeif0(&V_ripcbinfo, ifp);
1230 in_pcbpurgeif0(&V_udbinfo, ifp);
1231 in_pcbpurgeif0(&V_ulitecbinfo, ifp);
1232 in_purgemaddrs(ifp);
1236 * Make sure all multicast deletions invoking if_ioctl() are
1237 * completed before returning. Else we risk accessing a freed
1238 * ifnet structure pointer.
1240 inm_release_wait(NULL);
1244 * Delete all IPv4 multicast address records, and associated link-layer
1245 * multicast address records, associated with ifp.
1246 * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1247 * XXX This should not race with ifma_protospec being set during
1248 * a new allocation, if it does, we have bigger problems.
1251 in_purgemaddrs(struct ifnet *ifp)
1253 struct in_multi_head purgeinms;
1254 struct in_multi *inm;
1255 struct ifmultiaddr *ifma, *next;
1257 SLIST_INIT(&purgeinms);
1258 IN_MULTI_LIST_LOCK();
1261 * Extract list of in_multi associated with the detaching ifp
1262 * which the PF_INET layer is about to release.
1263 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1264 * by code further down.
1268 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) {
1269 if (ifma->ifma_addr->sa_family != AF_INET ||
1270 ifma->ifma_protospec == NULL)
1272 inm = (struct in_multi *)ifma->ifma_protospec;
1273 inm_rele_locked(&purgeinms, inm);
1274 if (__predict_false(ifma_restart)) {
1275 ifma_restart = true;
1279 IF_ADDR_WUNLOCK(ifp);
1281 inm_release_list_deferred(&purgeinms);
1283 IN_MULTI_LIST_UNLOCK();
1287 struct llentry base;
1290 #define IN_LLTBL_DEFAULT_HSIZE 32
1291 #define IN_LLTBL_HASH(k, h) \
1292 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
1295 * Do actual deallocation of @lle.
1298 in_lltable_destroy_lle_unlocked(epoch_context_t ctx)
1300 struct llentry *lle;
1302 lle = __containerof(ctx, struct llentry, lle_epoch_ctx);
1303 LLE_LOCK_DESTROY(lle);
1304 LLE_REQ_DESTROY(lle);
1305 free(lle, M_LLTABLE);
1309 * Called by LLE_FREE_LOCKED when number of references
1313 in_lltable_destroy_lle(struct llentry *lle)
1317 NET_EPOCH_CALL(in_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
1320 static struct llentry *
1321 in_lltable_new(struct in_addr addr4, u_int flags)
1323 struct in_llentry *lle;
1325 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
1326 if (lle == NULL) /* NB: caller generates msg */
1330 * For IPv4 this will trigger "arpresolve" to generate
1333 lle->base.la_expire = time_uptime; /* mark expired */
1334 lle->base.r_l3addr.addr4 = addr4;
1335 lle->base.lle_refcnt = 1;
1336 lle->base.lle_free = in_lltable_destroy_lle;
1337 LLE_LOCK_INIT(&lle->base);
1338 LLE_REQ_INIT(&lle->base);
1339 callout_init(&lle->base.lle_timer, 1);
1341 return (&lle->base);
1344 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1345 ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
1348 in_lltable_match_prefix(const struct sockaddr *saddr,
1349 const struct sockaddr *smask, u_int flags, struct llentry *lle)
1351 struct in_addr addr, mask, lle_addr;
1353 addr = ((const struct sockaddr_in *)saddr)->sin_addr;
1354 mask = ((const struct sockaddr_in *)smask)->sin_addr;
1355 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
1357 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
1360 if (lle->la_flags & LLE_IFADDR) {
1362 * Delete LLE_IFADDR records IFF address & flag matches.
1363 * Note that addr is the interface address within prefix
1365 * Note also we should handle 'ifdown' cases without removing
1368 if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0)
1373 /* flags & LLE_STATIC means deleting both dynamic and static entries */
1374 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
1381 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
1383 size_t pkts_dropped;
1385 LLE_WLOCK_ASSERT(lle);
1386 KASSERT(llt != NULL, ("lltable is NULL"));
1388 /* Unlink entry from table if not already */
1389 if ((lle->la_flags & LLE_LINKED) != 0) {
1390 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
1391 lltable_unlink_entry(llt, lle);
1394 /* Drop hold queue */
1395 pkts_dropped = llentry_free(lle);
1396 ARPSTAT_ADD(dropped, pkts_dropped);
1400 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1402 struct nhop_object *nh;
1403 struct in_addr addr;
1405 KASSERT(l3addr->sa_family == AF_INET,
1406 ("sin_family %d", l3addr->sa_family));
1408 addr = ((const struct sockaddr_in *)l3addr)->sin_addr;
1410 nh = fib4_lookup(ifp->if_fib, addr, 0, NHR_NONE, 0);
1415 * If the gateway for an existing host route matches the target L3
1416 * address, which is a special route inserted by some implementation
1417 * such as MANET, and the interface is of the correct type, then
1418 * allow for ARP to proceed.
1420 if (nh->nh_flags & NHF_GATEWAY) {
1421 if (!(nh->nh_flags & NHF_HOST) || nh->nh_ifp->if_type != IFT_ETHER ||
1422 (nh->nh_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1423 memcmp(nh->gw_sa.sa_data, l3addr->sa_data,
1424 sizeof(in_addr_t)) != 0) {
1430 * Make sure that at least the destination address is covered
1431 * by the route. This is for handling the case where 2 or more
1432 * interfaces have the same prefix. An incoming packet arrives
1433 * on one interface and the corresponding outgoing packet leaves
1434 * another interface.
1436 if ((nh->nh_ifp != ifp) && (nh->nh_flags & NHF_HOST) == 0) {
1437 struct in_ifaddr *ia = (struct in_ifaddr *)ifaof_ifpforaddr(l3addr, ifp);
1438 struct in_addr dst_addr, mask_addr;
1444 * ifaof_ifpforaddr() returns _best matching_ IFA.
1445 * It is possible that ifa prefix does not cover our address.
1446 * Explicitly verify and fail if that's the case.
1448 dst_addr = IA_SIN(ia)->sin_addr;
1449 mask_addr.s_addr = htonl(ia->ia_subnetmask);
1451 if (!IN_ARE_MASKED_ADDR_EQUAL(dst_addr, addr, mask_addr))
1458 static inline uint32_t
1459 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
1462 return (IN_LLTBL_HASH(dst.s_addr, hsize));
1466 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
1469 return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
1473 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
1475 struct sockaddr_in *sin;
1477 sin = (struct sockaddr_in *)sa;
1478 bzero(sin, sizeof(*sin));
1479 sin->sin_family = AF_INET;
1480 sin->sin_len = sizeof(*sin);
1481 sin->sin_addr = lle->r_l3addr.addr4;
1484 static inline struct llentry *
1485 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
1487 struct llentry *lle;
1488 struct llentries *lleh;
1491 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
1492 lleh = &llt->lle_head[hashidx];
1493 CK_LIST_FOREACH(lle, lleh, lle_next) {
1494 if (lle->la_flags & LLE_DELETED)
1496 if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
1504 in_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
1507 lle->la_flags |= LLE_DELETED;
1508 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1510 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1515 static struct llentry *
1516 in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1518 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1519 struct ifnet *ifp = llt->llt_ifp;
1520 struct llentry *lle;
1521 char linkhdr[LLE_MAX_LINKHDR];
1525 KASSERT(l3addr->sa_family == AF_INET,
1526 ("sin_family %d", l3addr->sa_family));
1529 * A route that covers the given address must have
1530 * been installed 1st because we are doing a resolution,
1533 if (!(flags & LLE_IFADDR) &&
1534 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1537 lle = in_lltable_new(sin->sin_addr, flags);
1539 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1542 lle->la_flags = flags;
1543 if (flags & LLE_STATIC)
1544 lle->r_flags |= RLLE_VALID;
1545 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
1546 linkhdrsize = LLE_MAX_LINKHDR;
1547 if (lltable_calc_llheader(ifp, AF_INET, IF_LLADDR(ifp),
1548 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
1549 NET_EPOCH_CALL(in_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
1552 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
1554 lle->la_flags |= LLE_STATIC;
1555 lle->r_flags |= (RLLE_VALID | RLLE_IFADDR);
1562 * Return NULL if not found or marked for deletion.
1563 * If found return lle read locked.
1565 static struct llentry *
1566 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1568 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1569 struct llentry *lle;
1571 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
1572 KASSERT(l3addr->sa_family == AF_INET,
1573 ("sin_family %d", l3addr->sa_family));
1574 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) !=
1575 (LLE_UNLOCKED | LLE_EXCLUSIVE),
1576 ("wrong lle request flags: %#x", flags));
1578 lle = in_lltable_find_dst(llt, sin->sin_addr);
1581 if (flags & LLE_UNLOCKED)
1584 if (flags & LLE_EXCLUSIVE)
1590 * If the afdata lock is not held, the LLE may have been unlinked while
1591 * we were blocked on the LLE lock. Check for this case.
1593 if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) {
1594 if (flags & LLE_EXCLUSIVE)
1604 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
1605 struct sysctl_req *wr)
1607 struct ifnet *ifp = llt->llt_ifp;
1610 struct rt_msghdr rtm;
1611 struct sockaddr_in sin;
1612 struct sockaddr_dl sdl;
1614 struct sockaddr_dl *sdl;
1617 bzero(&arpc, sizeof(arpc));
1618 /* skip deleted entries */
1619 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1621 /* Skip if jailed and not a valid IP of the prison. */
1622 lltable_fill_sa_entry(lle,(struct sockaddr *)&arpc.sin);
1623 if (prison_if(wr->td->td_ucred, (struct sockaddr *)&arpc.sin) != 0)
1626 * produce a msg made of:
1628 * struct sockaddr_in; (IPv4)
1629 * struct sockaddr_dl;
1631 arpc.rtm.rtm_msglen = sizeof(arpc);
1632 arpc.rtm.rtm_version = RTM_VERSION;
1633 arpc.rtm.rtm_type = RTM_GET;
1634 arpc.rtm.rtm_flags = RTF_UP;
1635 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1638 if (lle->la_flags & LLE_PUB)
1639 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1642 sdl->sdl_family = AF_LINK;
1643 sdl->sdl_len = sizeof(*sdl);
1644 sdl->sdl_index = ifp->if_index;
1645 sdl->sdl_type = ifp->if_type;
1646 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1647 sdl->sdl_alen = ifp->if_addrlen;
1648 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1651 bzero(LLADDR(sdl), ifp->if_addrlen);
1654 arpc.rtm.rtm_rmx.rmx_expire =
1655 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1656 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1657 if (lle->la_flags & LLE_STATIC)
1658 arpc.rtm.rtm_flags |= RTF_STATIC;
1659 if (lle->la_flags & LLE_IFADDR)
1660 arpc.rtm.rtm_flags |= RTF_PINNED;
1661 arpc.rtm.rtm_index = ifp->if_index;
1662 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1667 static struct lltable *
1668 in_lltattach(struct ifnet *ifp)
1670 struct lltable *llt;
1672 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
1673 llt->llt_af = AF_INET;
1676 llt->llt_lookup = in_lltable_lookup;
1677 llt->llt_alloc_entry = in_lltable_alloc;
1678 llt->llt_delete_entry = in_lltable_delete_entry;
1679 llt->llt_dump_entry = in_lltable_dump_entry;
1680 llt->llt_hash = in_lltable_hash;
1681 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
1682 llt->llt_free_entry = in_lltable_free_entry;
1683 llt->llt_match_prefix = in_lltable_match_prefix;
1684 llt->llt_mark_used = llentry_mark_used;
1691 in_domifattach(struct ifnet *ifp)
1693 struct in_ifinfo *ii;
1695 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1697 ii->ii_llt = in_lltattach(ifp);
1698 ii->ii_igmp = igmp_domifattach(ifp);
1704 in_domifdetach(struct ifnet *ifp, void *aux)
1706 struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1708 igmp_domifdetach(ifp);
1709 lltable_free(ii->ii_llt);