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_var.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/ip_var.h>
70 #include <netinet/ip_carp.h>
71 #include <netinet/igmp_var.h>
72 #include <netinet/udp.h>
73 #include <netinet/udp_var.h>
75 static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
76 static int in_difaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
77 static int in_gifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
79 static void in_socktrim(struct sockaddr_in *);
80 static void in_purgemaddrs(struct ifnet *);
82 static bool ia_need_loopback_route(const struct in_ifaddr *);
84 VNET_DEFINE_STATIC(int, nosameprefix);
85 #define V_nosameprefix VNET(nosameprefix)
86 SYSCTL_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_VNET | CTLFLAG_RW,
87 &VNET_NAME(nosameprefix), 0,
88 "Refuse to create same prefixes on different interfaces");
90 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
91 #define V_ripcbinfo VNET(ripcbinfo)
93 static struct sx in_control_sx;
94 SX_SYSINIT(in_control_sx, &in_control_sx, "in_control");
97 * Return 1 if an internet address is for a ``local'' host
98 * (one to which we have a connection).
101 in_localaddr(struct in_addr in)
103 struct rm_priotracker in_ifa_tracker;
104 u_long i = ntohl(in.s_addr);
105 struct in_ifaddr *ia;
107 IN_IFADDR_RLOCK(&in_ifa_tracker);
108 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
109 if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
110 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
114 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
119 * Return 1 if an internet address is for the local host and configured
120 * on one of its interfaces.
123 in_localip(struct in_addr in)
125 struct rm_priotracker in_ifa_tracker;
126 struct in_ifaddr *ia;
128 IN_IFADDR_RLOCK(&in_ifa_tracker);
129 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
130 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
131 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
135 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
140 * Return 1 if an internet address is configured on an interface.
143 in_ifhasaddr(struct ifnet *ifp, struct in_addr in)
146 struct in_ifaddr *ia;
150 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
151 if (ifa->ifa_addr->sa_family != AF_INET)
153 ia = (struct in_ifaddr *)ifa;
154 if (ia->ia_addr.sin_addr.s_addr == in.s_addr)
162 * Return a reference to the interface address which is different to
163 * the supplied one but with same IP address value.
165 static struct in_ifaddr *
166 in_localip_more(struct in_ifaddr *ia)
168 struct rm_priotracker in_ifa_tracker;
169 in_addr_t in = IA_SIN(ia)->sin_addr.s_addr;
170 struct in_ifaddr *it;
172 IN_IFADDR_RLOCK(&in_ifa_tracker);
173 LIST_FOREACH(it, INADDR_HASH(in), ia_hash) {
174 if (it != ia && IA_SIN(it)->sin_addr.s_addr == in) {
175 ifa_ref(&it->ia_ifa);
176 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
180 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
186 * Determine whether an IP address is in a reserved set of addresses
187 * that may not be forwarded, or whether datagrams to that destination
191 in_canforward(struct in_addr in)
193 u_long i = ntohl(in.s_addr);
195 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i) ||
196 IN_ZERONET(i) || IN_LOOPBACK(i))
202 * Trim a mask in a sockaddr
205 in_socktrim(struct sockaddr_in *ap)
207 char *cplim = (char *) &ap->sin_addr;
208 char *cp = (char *) (&ap->sin_addr + 1);
211 while (--cp >= cplim)
213 (ap)->sin_len = cp - (char *) (ap) + 1;
219 * Generic internet control operations (ioctl's).
222 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
225 struct ifreq *ifr = (struct ifreq *)data;
226 struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
227 struct epoch_tracker et;
229 struct in_ifaddr *ia;
233 return (EADDRNOTAVAIL);
236 * Filter out 4 ioctls we implement directly. Forward the rest
237 * to specific functions and ifp->if_ioctl().
246 sx_xlock(&in_control_sx);
247 error = in_gifaddr_ioctl(cmd, data, ifp, td);
248 sx_xunlock(&in_control_sx);
251 sx_xlock(&in_control_sx);
252 error = in_difaddr_ioctl(cmd, data, ifp, td);
253 sx_xunlock(&in_control_sx);
255 case OSIOCAIFADDR: /* 9.x compat */
257 sx_xlock(&in_control_sx);
258 error = in_aifaddr_ioctl(cmd, data, ifp, td);
259 sx_xunlock(&in_control_sx);
265 /* We no longer support that old commands. */
268 if (ifp->if_ioctl == NULL)
270 return ((*ifp->if_ioctl)(ifp, cmd, data));
273 if (addr->sin_addr.s_addr != INADDR_ANY &&
274 prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0)
275 return (EADDRNOTAVAIL);
278 * Find address for this interface, if it exists. If an
279 * address was specified, find that one instead of the
280 * first one on the interface, if possible.
283 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
284 if (ifa->ifa_addr->sa_family != AF_INET)
286 ia = (struct in_ifaddr *)ifa;
287 if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
291 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
292 if (ifa->ifa_addr->sa_family == AF_INET) {
293 ia = (struct in_ifaddr *)ifa;
294 if (prison_check_ip4(td->td_ucred,
295 &ia->ia_addr.sin_addr) == 0)
301 return (EADDRNOTAVAIL);
311 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
315 *addr = ia->ia_broadaddr;
319 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
323 *addr = ia->ia_dstaddr;
327 *addr = ia->ia_sockmask;
337 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
339 const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
340 const struct sockaddr_in *addr = &ifra->ifra_addr;
341 const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
342 const struct sockaddr_in *mask = &ifra->ifra_mask;
343 const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
344 const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
345 struct epoch_tracker et;
347 struct in_ifaddr *ia;
351 error = priv_check(td, PRIV_NET_ADDIFADDR);
356 * ifra_addr must be present and be of INET family.
357 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
359 if (addr->sin_len != sizeof(struct sockaddr_in) ||
360 addr->sin_family != AF_INET)
362 if (broadaddr->sin_len != 0 &&
363 (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
364 broadaddr->sin_family != AF_INET))
366 if (mask->sin_len != 0 &&
367 (mask->sin_len != sizeof(struct sockaddr_in) ||
368 mask->sin_family != AF_INET))
370 if ((ifp->if_flags & IFF_POINTOPOINT) &&
371 (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
372 dstaddr->sin_addr.s_addr == INADDR_ANY))
373 return (EDESTADDRREQ);
374 if (vhid > 0 && carp_attach_p == NULL)
375 return (EPROTONOSUPPORT);
378 * See whether address already exist.
383 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
384 struct in_ifaddr *it;
386 if (ifa->ifa_addr->sa_family != AF_INET)
389 it = (struct in_ifaddr *)ifa;
390 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
391 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0)
399 (void )in_difaddr_ioctl(cmd, data, ifp, td);
401 ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
402 ia = (struct in_ifaddr *)ifa;
403 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
404 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
405 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
406 callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock,
407 CALLOUT_RETURNUNLOCKED);
411 if (mask->sin_len != 0) {
412 ia->ia_sockmask = *mask;
413 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
415 in_addr_t i = ntohl(addr->sin_addr.s_addr);
418 * Be compatible with network classes, if netmask isn't
419 * supplied, guess it based on classes.
422 ia->ia_subnetmask = IN_CLASSA_NET;
423 else if (IN_CLASSB(i))
424 ia->ia_subnetmask = IN_CLASSB_NET;
426 ia->ia_subnetmask = IN_CLASSC_NET;
427 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
429 ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
430 in_socktrim(&ia->ia_sockmask);
432 if (ifp->if_flags & IFF_BROADCAST) {
433 if (broadaddr->sin_len != 0) {
434 ia->ia_broadaddr = *broadaddr;
435 } else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
436 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
437 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
438 ia->ia_broadaddr.sin_family = AF_INET;
440 ia->ia_broadaddr.sin_addr.s_addr =
441 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
442 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
443 ia->ia_broadaddr.sin_family = AF_INET;
447 if (ifp->if_flags & IFF_POINTOPOINT)
448 ia->ia_dstaddr = *dstaddr;
451 error = (*carp_attach_p)(&ia->ia_ifa, vhid);
456 /* if_addrhead is already referenced by ifa_alloc() */
458 CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
459 IF_ADDR_WUNLOCK(ifp);
461 ifa_ref(ifa); /* in_ifaddrhead */
463 CK_STAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
464 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
468 * Give the interface a chance to initialize
469 * if this is its first address,
470 * and to validate the address if necessary.
472 if (ifp->if_ioctl != NULL) {
473 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
479 * Add route for the network.
482 error = in_addprefix(ia);
488 * Add a loopback route to self.
490 if (vhid == 0 && ia_need_loopback_route(ia)) {
491 struct in_ifaddr *eia;
493 eia = in_localip_more(ia);
496 error = ifa_add_loopback_route((struct ifaddr *)ia,
497 (struct sockaddr *)&ia->ia_addr);
501 ifa_free(&eia->ia_ifa);
504 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
505 struct in_addr allhosts_addr;
506 struct in_ifinfo *ii;
508 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
509 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
511 error = in_joingroup(ifp, &allhosts_addr, NULL,
516 * Note: we don't need extra reference for ifa, since we called
517 * with sx lock held, and ifaddr can not be deleted in concurrent
520 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, ifa, IFADDR_EVENT_ADD);
526 (void )in_scrubprefix(ia, LLE_STATIC);
529 if (ia->ia_ifa.ifa_carp)
530 (*carp_detach_p)(&ia->ia_ifa, false);
533 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
534 IF_ADDR_WUNLOCK(ifp);
535 ifa_free(&ia->ia_ifa); /* if_addrhead */
538 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
539 LIST_REMOVE(ia, ia_hash);
541 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
547 in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
549 const struct ifreq *ifr = (struct ifreq *)data;
550 const struct sockaddr_in *addr = (const struct sockaddr_in *)
553 struct in_ifaddr *ia;
554 bool deleteAny, iaIsLast;
558 error = priv_check(td, PRIV_NET_DELIFADDR);
563 if (addr->sin_len != sizeof(struct sockaddr_in) ||
564 addr->sin_family != AF_INET)
572 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
573 struct in_ifaddr *it;
575 if (ifa->ifa_addr->sa_family != AF_INET)
578 it = (struct in_ifaddr *)ifa;
579 if (deleteAny && ia == NULL && (td == NULL ||
580 prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0))
583 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
584 (td == NULL || prison_check_ip4(td->td_ucred,
585 &addr->sin_addr) == 0))
593 IF_ADDR_WUNLOCK(ifp);
594 return (EADDRNOTAVAIL);
597 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
598 IF_ADDR_WUNLOCK(ifp);
599 ifa_free(&ia->ia_ifa); /* if_addrhead */
602 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
603 LIST_REMOVE(ia, ia_hash);
607 * in_scrubprefix() kills the interface route.
609 in_scrubprefix(ia, LLE_STATIC);
612 * in_ifadown gets rid of all the rest of
613 * the routes. This is not quite the right
614 * thing to do, but at least if we are running
615 * a routing process they will come back.
617 in_ifadown(&ia->ia_ifa, 1);
619 if (ia->ia_ifa.ifa_carp)
620 (*carp_detach_p)(&ia->ia_ifa, cmd == SIOCAIFADDR);
623 * If this is the last IPv4 address configured on this
624 * interface, leave the all-hosts group.
625 * No state-change report need be transmitted.
627 if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
628 struct in_ifinfo *ii;
630 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
631 if (ii->ii_allhosts) {
632 (void)in_leavegroup(ii->ii_allhosts, NULL);
633 ii->ii_allhosts = NULL;
638 if (callout_stop(&ia->ia_garp_timer) == 1) {
639 ifa_free(&ia->ia_ifa);
641 IF_ADDR_WUNLOCK(ifp);
643 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
645 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
651 in_gifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
653 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
654 const struct sockaddr_in *addr = &ifra->ifra_addr;
655 struct epoch_tracker et;
657 struct in_ifaddr *ia;
660 * ifra_addr must be present and be of INET family.
662 if (addr->sin_len != sizeof(struct sockaddr_in) ||
663 addr->sin_family != AF_INET)
667 * See whether address exist.
671 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
672 struct in_ifaddr *it;
674 if (ifa->ifa_addr->sa_family != AF_INET)
677 it = (struct in_ifaddr *)ifa;
678 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
679 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0) {
686 return (EADDRNOTAVAIL);
689 ifra->ifra_mask = ia->ia_sockmask;
690 if ((ifp->if_flags & IFF_POINTOPOINT) &&
691 ia->ia_dstaddr.sin_family == AF_INET)
692 ifra->ifra_dstaddr = ia->ia_dstaddr;
693 else if ((ifp->if_flags & IFF_BROADCAST) &&
694 ia->ia_broadaddr.sin_family == AF_INET)
695 ifra->ifra_broadaddr = ia->ia_broadaddr;
697 memset(&ifra->ifra_broadaddr, 0,
698 sizeof(ifra->ifra_broadaddr));
705 in_match_ifaddr(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
708 if (nh->nh_ifa == (struct ifaddr *)arg)
715 in_handle_prefix_route(uint32_t fibnum, int cmd,
716 struct sockaddr_in *dst, struct sockaddr_in *netmask, struct ifaddr *ifa,
722 /* Prepare gateway */
723 struct sockaddr_dl_short sdl = {
724 .sdl_family = AF_LINK,
725 .sdl_len = sizeof(struct sockaddr_dl_short),
726 .sdl_type = ifa->ifa_ifp->if_type,
727 .sdl_index = ifa->ifa_ifp->if_index,
730 struct rt_addrinfo info = {
733 .rti_flags = RTF_PINNED | ((netmask != NULL) ? 0 : RTF_HOST),
735 [RTAX_DST] = (struct sockaddr *)dst,
736 [RTAX_NETMASK] = (struct sockaddr *)netmask,
737 [RTAX_GATEWAY] = (struct sockaddr *)&sdl,
739 /* Ensure we delete the prefix IFF prefix ifa matches */
740 .rti_filter = in_match_ifaddr,
741 .rti_filterdata = ifa,
744 return (rib_handle_ifaddr_info(fibnum, cmd, &info));
748 * Routing table interaction with interface addresses.
750 * In general, two types of routes needs to be installed:
751 * a) "interface" or "prefix" route, telling user that the addresses
752 * behind the ifa prefix are reached directly.
753 * b) "loopback" route installed for the ifa address, telling user that
754 * the address belongs to local system.
756 * Handling for (a) and (b) differs in multi-fib aspects, hence they
757 * are implemented in different functions below.
759 * The cases above may intersect - /32 interface aliases results in
760 * the same prefix produced by (a) and (b). This blurs the definition
761 * of the "loopback" route and complicate interactions. The interaction
762 * table is defined below. The case numbers are used in the multiple
763 * functions below to refer to the particular test case.
765 * There can be multiple options:
766 * 1) Adding address with prefix on non-p2p/non-loopback interface.
767 * Example: 192.0.2.1/24. Action:
768 * * add "prefix" route towards 192.0.2.0/24 via @ia interface,
769 * using @ia as an address source.
770 * * add "loopback" route towards 192.0.2.1 via V_loif, saving
771 * @ia ifp in the gateway and using @ia as an address source.
773 * 2) Adding address with /32 mask to non-p2p/non-loopback interface.
774 * Example: 192.0.2.2/32. Action:
775 * * add "prefix" host route via V_loif, using @ia as an address source.
777 * 3) Adding address with or without prefix to p2p interface.
778 * Example: 10.0.0.1/24->10.0.0.2. Action:
779 * * add "prefix" host route towards 10.0.0.2 via this interface, using @ia
780 * as an address source. Note: no sense in installing full /24 as the interface
782 * * add "loopback" route towards 10.0.9.1 via V_loif, saving
783 * @ia ifp in the gateway and using @ia as an address source.
785 * 4) Adding address with or without prefix to loopback interface.
786 * Example: 192.0.2.1/24. Action:
787 * * add "prefix" host route via @ia interface, using @ia as an address source.
788 * Note: Skip installing /24 prefix as it would introduce TTL loop
789 * for the traffic destined to these addresses.
793 * Checks if @ia needs to install loopback route to @ia address via
794 * ifa_maintain_loopback_route().
796 * Return true on success.
799 ia_need_loopback_route(const struct in_ifaddr *ia)
801 struct ifnet *ifp = ia->ia_ifp;
803 /* Case 4: Skip loopback interfaces */
804 if ((ifp->if_flags & IFF_LOOPBACK) ||
805 (ia->ia_addr.sin_addr.s_addr == INADDR_ANY))
808 /* Clash avoidance: Skip p2p interfaces with both addresses are equal */
809 if ((ifp->if_flags & IFF_POINTOPOINT) &&
810 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
813 /* Case 2: skip /32 prefixes */
814 if (!(ifp->if_flags & IFF_POINTOPOINT) &&
815 (ia->ia_sockmask.sin_addr.s_addr == INADDR_BROADCAST))
822 * Calculate "prefix" route corresponding to @ia.
825 ia_getrtprefix(const struct in_ifaddr *ia, struct in_addr *prefix, struct in_addr *mask)
828 if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) {
829 /* Case 3: return host route for dstaddr */
830 *prefix = ia->ia_dstaddr.sin_addr;
831 mask->s_addr = INADDR_BROADCAST;
832 } else if (ia->ia_ifp->if_flags & IFF_LOOPBACK) {
833 /* Case 4: return host route for ifaddr */
834 *prefix = ia->ia_addr.sin_addr;
835 mask->s_addr = INADDR_BROADCAST;
837 /* Cases 1,2: return actual ia prefix */
838 *prefix = ia->ia_addr.sin_addr;
839 *mask = ia->ia_sockmask.sin_addr;
840 prefix->s_addr &= mask->s_addr;
845 * Adds or delete interface "prefix" route corresponding to @ifa.
846 * Returns 0 on success or errno.
849 in_handle_ifaddr_route(int cmd, struct in_ifaddr *ia)
851 struct ifaddr *ifa = &ia->ia_ifa;
852 struct in_addr daddr, maddr;
853 struct sockaddr_in *pmask;
854 struct epoch_tracker et;
857 ia_getrtprefix(ia, &daddr, &maddr);
859 struct sockaddr_in mask = {
860 .sin_family = AF_INET,
861 .sin_len = sizeof(struct sockaddr_in),
865 pmask = (maddr.s_addr != INADDR_BROADCAST) ? &mask : NULL;
867 struct sockaddr_in dst = {
868 .sin_family = AF_INET,
869 .sin_len = sizeof(struct sockaddr_in),
870 .sin_addr.s_addr = daddr.s_addr & maddr.s_addr,
873 struct ifnet *ifp = ia->ia_ifp;
875 if ((maddr.s_addr == INADDR_BROADCAST) &&
876 (!(ia->ia_ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))) {
877 /* Case 2: host route on broadcast interface */
881 uint32_t fibnum = ifa->ifa_ifp->if_fib;
883 error = in_handle_prefix_route(fibnum, cmd, &dst, pmask, ifa, ifp);
890 * Check if we have a route for the given prefix already.
893 in_hasrtprefix(struct in_ifaddr *target)
895 struct rm_priotracker in_ifa_tracker;
896 struct in_ifaddr *ia;
897 struct in_addr prefix, mask, p, m;
900 ia_getrtprefix(target, &prefix, &mask);
902 IN_IFADDR_RLOCK(&in_ifa_tracker);
903 /* Look for an existing address with the same prefix, mask, and fib */
904 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
905 ia_getrtprefix(ia, &p, &m);
907 if (prefix.s_addr != p.s_addr ||
908 mask.s_addr != m.s_addr)
911 if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
915 * If we got a matching prefix route inserted by other
916 * interface address, we are done here.
918 if (ia->ia_flags & IFA_ROUTE) {
923 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
929 in_addprefix(struct in_ifaddr *target)
933 if (in_hasrtprefix(target)) {
937 rt_addrmsg(RTM_ADD, &target->ia_ifa,
938 target->ia_ifp->if_fib);
944 * No-one seem to have this prefix route, so we try to insert it.
946 rt_addrmsg(RTM_ADD, &target->ia_ifa, target->ia_ifp->if_fib);
947 error = in_handle_ifaddr_route(RTM_ADD, target);
949 target->ia_flags |= IFA_ROUTE;
954 * Removes either all lle entries for given @ia, or lle
955 * corresponding to @ia address.
958 in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags)
960 struct sockaddr_in addr, mask;
961 struct sockaddr *saddr, *smask;
964 saddr = (struct sockaddr *)&addr;
965 bzero(&addr, sizeof(addr));
966 addr.sin_len = sizeof(addr);
967 addr.sin_family = AF_INET;
968 smask = (struct sockaddr *)&mask;
969 bzero(&mask, sizeof(mask));
970 mask.sin_len = sizeof(mask);
971 mask.sin_family = AF_INET;
972 mask.sin_addr.s_addr = ia->ia_subnetmask;
977 * Remove all L2 entries matching given prefix.
978 * Convert address to host representation to avoid
979 * doing this on every callback. ia_subnetmask is already
980 * stored in host representation.
982 addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr);
983 lltable_prefix_free(AF_INET, saddr, smask, flags);
985 /* Remove interface address only */
986 addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr;
987 lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr);
992 * If there is no other address in the system that can serve a route to the
993 * same prefix, remove the route. Hand over the route to the new address
997 in_scrubprefix(struct in_ifaddr *target, u_int flags)
999 struct rm_priotracker in_ifa_tracker;
1000 struct in_ifaddr *ia;
1001 struct in_addr prefix, mask, p, m;
1005 * Remove the loopback route to the interface address.
1007 if (ia_need_loopback_route(target) && (flags & LLE_STATIC)) {
1008 struct in_ifaddr *eia;
1011 * XXXME: add fib-aware in_localip.
1012 * We definitely don't want to switch between
1013 * prefixes in different fibs.
1015 eia = in_localip_more(target);
1018 error = ifa_switch_loopback_route((struct ifaddr *)eia,
1019 (struct sockaddr *)&target->ia_addr);
1020 ifa_free(&eia->ia_ifa);
1022 error = ifa_del_loopback_route((struct ifaddr *)target,
1023 (struct sockaddr *)&target->ia_addr);
1027 ia_getrtprefix(target, &prefix, &mask);
1029 if ((target->ia_flags & IFA_ROUTE) == 0) {
1030 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1033 * Removing address from !IFF_UP interface or
1034 * prefix which exists on other interface (along with route).
1035 * No entries should exist here except target addr.
1036 * Given that, delete this entry only.
1038 in_scrubprefixlle(target, 0, flags);
1042 IN_IFADDR_RLOCK(&in_ifa_tracker);
1043 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1044 ia_getrtprefix(ia, &p, &m);
1046 if (prefix.s_addr != p.s_addr ||
1047 mask.s_addr != m.s_addr)
1050 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1054 * If we got a matching prefix address, move IFA_ROUTE and
1055 * the route itself to it. Make sure that routing daemons
1058 if ((ia->ia_flags & IFA_ROUTE) == 0) {
1059 ifa_ref(&ia->ia_ifa);
1060 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1061 error = in_handle_ifaddr_route(RTM_DELETE, target);
1063 target->ia_flags &= ~IFA_ROUTE;
1065 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
1067 /* Scrub all entries IFF interface is different */
1068 in_scrubprefixlle(target, target->ia_ifp != ia->ia_ifp,
1070 error = in_handle_ifaddr_route(RTM_ADD, ia);
1072 ia->ia_flags |= IFA_ROUTE;
1074 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
1076 ifa_free(&ia->ia_ifa);
1080 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
1083 * remove all L2 entries on the given prefix
1085 in_scrubprefixlle(target, 1, flags);
1088 * As no-one seem to have this prefix, we can remove the route.
1090 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1091 error = in_handle_ifaddr_route(RTM_DELETE, target);
1093 target->ia_flags &= ~IFA_ROUTE;
1095 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
1100 in_ifscrub_all(void)
1103 struct ifaddr *ifa, *nifa;
1104 struct ifaliasreq ifr;
1107 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1108 /* Cannot lock here - lock recursion. */
1109 /* NET_EPOCH_ENTER(et); */
1110 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1111 if (ifa->ifa_addr->sa_family != AF_INET)
1115 * This is ugly but the only way for legacy IP to
1116 * cleanly remove addresses and everything attached.
1118 bzero(&ifr, sizeof(ifr));
1119 ifr.ifra_addr = *ifa->ifa_addr;
1120 if (ifa->ifa_dstaddr)
1121 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1122 (void)in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr,
1125 /* NET_EPOCH_EXIT(et); */
1126 in_purgemaddrs(ifp);
1127 igmp_domifdetach(ifp);
1133 in_ifaddr_broadcast(struct in_addr in, struct in_ifaddr *ia)
1136 return ((in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1138 * Check for old-style (host 0) broadcast, but
1139 * taking into account that RFC 3021 obsoletes it.
1141 (ia->ia_subnetmask != IN_RFC3021_MASK &&
1142 ntohl(in.s_addr) == ia->ia_subnet)) &&
1144 * Check for an all one subnetmask. These
1145 * only exist when an interface gets a secondary
1148 ia->ia_subnetmask != (u_long)0xffffffff);
1152 * Return 1 if the address might be a local broadcast address.
1155 in_broadcast(struct in_addr in, struct ifnet *ifp)
1162 if (in.s_addr == INADDR_BROADCAST ||
1163 in.s_addr == INADDR_ANY)
1165 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1169 * Look through the list of addresses for a match
1170 * with a broadcast address.
1172 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1173 if (ifa->ifa_addr->sa_family == AF_INET &&
1174 in_ifaddr_broadcast(in, (struct in_ifaddr *)ifa)) {
1182 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1185 in_ifdetach(struct ifnet *ifp)
1188 in_pcbpurgeif0(&V_ripcbinfo, ifp);
1189 in_pcbpurgeif0(&V_udbinfo, ifp);
1190 in_pcbpurgeif0(&V_ulitecbinfo, ifp);
1191 in_purgemaddrs(ifp);
1195 * Make sure all multicast deletions invoking if_ioctl() are
1196 * completed before returning. Else we risk accessing a freed
1197 * ifnet structure pointer.
1199 inm_release_wait(NULL);
1203 * Delete all IPv4 multicast address records, and associated link-layer
1204 * multicast address records, associated with ifp.
1205 * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1206 * XXX This should not race with ifma_protospec being set during
1207 * a new allocation, if it does, we have bigger problems.
1210 in_purgemaddrs(struct ifnet *ifp)
1212 struct in_multi_head purgeinms;
1213 struct in_multi *inm;
1214 struct ifmultiaddr *ifma, *next;
1216 SLIST_INIT(&purgeinms);
1217 IN_MULTI_LIST_LOCK();
1220 * Extract list of in_multi associated with the detaching ifp
1221 * which the PF_INET layer is about to release.
1222 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1223 * by code further down.
1227 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) {
1228 if (ifma->ifma_addr->sa_family != AF_INET ||
1229 ifma->ifma_protospec == NULL)
1231 inm = (struct in_multi *)ifma->ifma_protospec;
1232 inm_rele_locked(&purgeinms, inm);
1233 if (__predict_false(ifma_restart)) {
1234 ifma_restart = true;
1238 IF_ADDR_WUNLOCK(ifp);
1240 inm_release_list_deferred(&purgeinms);
1242 IN_MULTI_LIST_UNLOCK();
1246 struct llentry base;
1249 #define IN_LLTBL_DEFAULT_HSIZE 32
1250 #define IN_LLTBL_HASH(k, h) \
1251 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
1254 * Do actual deallocation of @lle.
1257 in_lltable_destroy_lle_unlocked(epoch_context_t ctx)
1259 struct llentry *lle;
1261 lle = __containerof(ctx, struct llentry, lle_epoch_ctx);
1262 LLE_LOCK_DESTROY(lle);
1263 LLE_REQ_DESTROY(lle);
1264 free(lle, M_LLTABLE);
1268 * Called by the datapath to indicate that
1269 * the entry was used.
1272 in_lltable_mark_used(struct llentry *lle)
1276 lle->r_skip_req = 0;
1277 LLE_REQ_UNLOCK(lle);
1281 * Called by LLE_FREE_LOCKED when number of references
1285 in_lltable_destroy_lle(struct llentry *lle)
1289 NET_EPOCH_CALL(in_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
1292 static struct llentry *
1293 in_lltable_new(struct in_addr addr4, u_int flags)
1295 struct in_llentry *lle;
1297 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
1298 if (lle == NULL) /* NB: caller generates msg */
1302 * For IPv4 this will trigger "arpresolve" to generate
1305 lle->base.la_expire = time_uptime; /* mark expired */
1306 lle->base.r_l3addr.addr4 = addr4;
1307 lle->base.lle_refcnt = 1;
1308 lle->base.lle_free = in_lltable_destroy_lle;
1309 LLE_LOCK_INIT(&lle->base);
1310 LLE_REQ_INIT(&lle->base);
1311 callout_init(&lle->base.lle_timer, 1);
1313 return (&lle->base);
1316 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1317 ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
1320 in_lltable_match_prefix(const struct sockaddr *saddr,
1321 const struct sockaddr *smask, u_int flags, struct llentry *lle)
1323 struct in_addr addr, mask, lle_addr;
1325 addr = ((const struct sockaddr_in *)saddr)->sin_addr;
1326 mask = ((const struct sockaddr_in *)smask)->sin_addr;
1327 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
1329 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
1332 if (lle->la_flags & LLE_IFADDR) {
1334 * Delete LLE_IFADDR records IFF address & flag matches.
1335 * Note that addr is the interface address within prefix
1337 * Note also we should handle 'ifdown' cases without removing
1340 if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0)
1345 /* flags & LLE_STATIC means deleting both dynamic and static entries */
1346 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
1353 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
1355 size_t pkts_dropped;
1357 LLE_WLOCK_ASSERT(lle);
1358 KASSERT(llt != NULL, ("lltable is NULL"));
1360 /* Unlink entry from table if not already */
1361 if ((lle->la_flags & LLE_LINKED) != 0) {
1362 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
1363 lltable_unlink_entry(llt, lle);
1366 /* Drop hold queue */
1367 pkts_dropped = llentry_free(lle);
1368 ARPSTAT_ADD(dropped, pkts_dropped);
1372 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1374 struct rt_addrinfo info;
1375 struct sockaddr_in rt_key, rt_mask;
1376 struct sockaddr rt_gateway;
1379 KASSERT(l3addr->sa_family == AF_INET,
1380 ("sin_family %d", l3addr->sa_family));
1382 bzero(&rt_key, sizeof(rt_key));
1383 rt_key.sin_len = sizeof(rt_key);
1384 bzero(&rt_mask, sizeof(rt_mask));
1385 rt_mask.sin_len = sizeof(rt_mask);
1386 bzero(&rt_gateway, sizeof(rt_gateway));
1387 rt_gateway.sa_len = sizeof(rt_gateway);
1389 bzero(&info, sizeof(info));
1390 info.rti_info[RTAX_DST] = (struct sockaddr *)&rt_key;
1391 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&rt_mask;
1392 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&rt_gateway;
1394 if (rib_lookup_info(ifp->if_fib, l3addr, NHR_REF, 0, &info) != 0)
1397 rt_flags = info.rti_flags;
1400 * If the gateway for an existing host route matches the target L3
1401 * address, which is a special route inserted by some implementation
1402 * such as MANET, and the interface is of the correct type, then
1403 * allow for ARP to proceed.
1405 if (rt_flags & RTF_GATEWAY) {
1406 if (!(rt_flags & RTF_HOST) || !info.rti_ifp ||
1407 info.rti_ifp->if_type != IFT_ETHER ||
1408 (info.rti_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1409 memcmp(rt_gateway.sa_data, l3addr->sa_data,
1410 sizeof(in_addr_t)) != 0) {
1411 rib_free_info(&info);
1415 rib_free_info(&info);
1418 * Make sure that at least the destination address is covered
1419 * by the route. This is for handling the case where 2 or more
1420 * interfaces have the same prefix. An incoming packet arrives
1421 * on one interface and the corresponding outgoing packet leaves
1422 * another interface.
1424 if (!(rt_flags & RTF_HOST) && info.rti_ifp != ifp) {
1425 const char *sa, *mask, *addr, *lim;
1426 const struct sockaddr_in *l3sin;
1428 mask = (const char *)&rt_mask;
1430 * Just being extra cautious to avoid some custom
1431 * code getting into trouble.
1433 if ((info.rti_addrs & RTA_NETMASK) == 0)
1436 sa = (const char *)&rt_key;
1437 addr = (const char *)l3addr;
1438 l3sin = (const struct sockaddr_in *)l3addr;
1439 lim = addr + l3sin->sin_len;
1441 for ( ; addr < lim; sa++, mask++, addr++) {
1442 if ((*sa ^ *addr) & *mask) {
1444 char addrbuf[INET_ADDRSTRLEN];
1446 log(LOG_INFO, "IPv4 address: \"%s\" "
1447 "is not on the network\n",
1448 inet_ntoa_r(l3sin->sin_addr, addrbuf));
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 = in_lltable_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);