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 *original_ia)
168 struct rm_priotracker in_ifa_tracker;
169 in_addr_t original_addr = IA_SIN(original_ia)->sin_addr.s_addr;
170 uint32_t original_fib = original_ia->ia_ifa.ifa_ifp->if_fib;
171 struct in_ifaddr *ia;
173 IN_IFADDR_RLOCK(&in_ifa_tracker);
174 LIST_FOREACH(ia, INADDR_HASH(original_addr), ia_hash) {
175 in_addr_t addr = IA_SIN(ia)->sin_addr.s_addr;
176 uint32_t fib = ia->ia_ifa.ifa_ifp->if_fib;
177 if (!V_rt_add_addr_allfibs && (original_fib != fib))
179 if ((original_ia != ia) && (original_addr == addr)) {
180 ifa_ref(&ia->ia_ifa);
181 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
185 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
191 * Determine whether an IP address is in a reserved set of addresses
192 * that may not be forwarded, or whether datagrams to that destination
196 in_canforward(struct in_addr in)
198 u_long i = ntohl(in.s_addr);
200 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i) ||
201 IN_ZERONET(i) || IN_LOOPBACK(i))
207 * Trim a mask in a sockaddr
210 in_socktrim(struct sockaddr_in *ap)
212 char *cplim = (char *) &ap->sin_addr;
213 char *cp = (char *) (&ap->sin_addr + 1);
216 while (--cp >= cplim)
218 (ap)->sin_len = cp - (char *) (ap) + 1;
224 * Generic internet control operations (ioctl's).
227 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
230 struct ifreq *ifr = (struct ifreq *)data;
231 struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
232 struct epoch_tracker et;
234 struct in_ifaddr *ia;
238 return (EADDRNOTAVAIL);
241 * Filter out 4 ioctls we implement directly. Forward the rest
242 * to specific functions and ifp->if_ioctl().
251 sx_xlock(&in_control_sx);
252 error = in_gifaddr_ioctl(cmd, data, ifp, td);
253 sx_xunlock(&in_control_sx);
256 sx_xlock(&in_control_sx);
257 error = in_difaddr_ioctl(cmd, data, ifp, td);
258 sx_xunlock(&in_control_sx);
260 case OSIOCAIFADDR: /* 9.x compat */
262 sx_xlock(&in_control_sx);
263 error = in_aifaddr_ioctl(cmd, data, ifp, td);
264 sx_xunlock(&in_control_sx);
270 /* We no longer support that old commands. */
273 if (ifp->if_ioctl == NULL)
275 return ((*ifp->if_ioctl)(ifp, cmd, data));
278 if (addr->sin_addr.s_addr != INADDR_ANY &&
279 prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0)
280 return (EADDRNOTAVAIL);
283 * Find address for this interface, if it exists. If an
284 * address was specified, find that one instead of the
285 * first one on the interface, if possible.
288 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
289 if (ifa->ifa_addr->sa_family != AF_INET)
291 ia = (struct in_ifaddr *)ifa;
292 if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
296 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
297 if (ifa->ifa_addr->sa_family == AF_INET) {
298 ia = (struct in_ifaddr *)ifa;
299 if (prison_check_ip4(td->td_ucred,
300 &ia->ia_addr.sin_addr) == 0)
306 return (EADDRNOTAVAIL);
316 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
320 *addr = ia->ia_broadaddr;
324 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
328 *addr = ia->ia_dstaddr;
332 *addr = ia->ia_sockmask;
342 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
344 const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
345 const struct sockaddr_in *addr = &ifra->ifra_addr;
346 const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
347 const struct sockaddr_in *mask = &ifra->ifra_mask;
348 const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
349 const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
350 struct epoch_tracker et;
352 struct in_ifaddr *ia;
356 error = priv_check(td, PRIV_NET_ADDIFADDR);
361 * ifra_addr must be present and be of INET family.
362 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
364 if (addr->sin_len != sizeof(struct sockaddr_in) ||
365 addr->sin_family != AF_INET)
367 if (broadaddr->sin_len != 0 &&
368 (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
369 broadaddr->sin_family != AF_INET))
371 if (mask->sin_len != 0 &&
372 (mask->sin_len != sizeof(struct sockaddr_in) ||
373 mask->sin_family != AF_INET))
375 if ((ifp->if_flags & IFF_POINTOPOINT) &&
376 (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
377 dstaddr->sin_addr.s_addr == INADDR_ANY))
378 return (EDESTADDRREQ);
379 if (vhid != 0 && carp_attach_p == NULL)
380 return (EPROTONOSUPPORT);
383 * See whether address already exist.
388 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
389 struct in_ifaddr *it;
391 if (ifa->ifa_addr->sa_family != AF_INET)
394 it = (struct in_ifaddr *)ifa;
395 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
396 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0)
404 (void )in_difaddr_ioctl(cmd, data, ifp, td);
406 ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
407 ia = (struct in_ifaddr *)ifa;
408 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
409 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
410 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
411 callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock,
412 CALLOUT_RETURNUNLOCKED);
416 if (mask->sin_len != 0) {
417 ia->ia_sockmask = *mask;
418 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
420 in_addr_t i = ntohl(addr->sin_addr.s_addr);
423 * Be compatible with network classes, if netmask isn't
424 * supplied, guess it based on classes.
427 ia->ia_subnetmask = IN_CLASSA_NET;
428 else if (IN_CLASSB(i))
429 ia->ia_subnetmask = IN_CLASSB_NET;
431 ia->ia_subnetmask = IN_CLASSC_NET;
432 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
434 ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
435 in_socktrim(&ia->ia_sockmask);
437 if (ifp->if_flags & IFF_BROADCAST) {
438 if (broadaddr->sin_len != 0) {
439 ia->ia_broadaddr = *broadaddr;
440 } else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
441 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
442 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
443 ia->ia_broadaddr.sin_family = AF_INET;
445 ia->ia_broadaddr.sin_addr.s_addr =
446 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
447 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
448 ia->ia_broadaddr.sin_family = AF_INET;
452 if (ifp->if_flags & IFF_POINTOPOINT)
453 ia->ia_dstaddr = *dstaddr;
456 error = (*carp_attach_p)(&ia->ia_ifa, vhid);
461 /* if_addrhead is already referenced by ifa_alloc() */
463 CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
464 IF_ADDR_WUNLOCK(ifp);
466 ifa_ref(ifa); /* in_ifaddrhead */
468 CK_STAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
469 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
473 * Give the interface a chance to initialize
474 * if this is its first address,
475 * and to validate the address if necessary.
477 if (ifp->if_ioctl != NULL) {
478 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
484 * Add route for the network.
487 error = in_addprefix(ia);
493 * Add a loopback route to self.
495 if (vhid == 0 && ia_need_loopback_route(ia)) {
496 struct in_ifaddr *eia;
498 eia = in_localip_more(ia);
501 error = ifa_add_loopback_route((struct ifaddr *)ia,
502 (struct sockaddr *)&ia->ia_addr);
506 ifa_free(&eia->ia_ifa);
509 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
510 struct in_addr allhosts_addr;
511 struct in_ifinfo *ii;
513 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
514 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
516 error = in_joingroup(ifp, &allhosts_addr, NULL,
521 * Note: we don't need extra reference for ifa, since we called
522 * with sx lock held, and ifaddr can not be deleted in concurrent
525 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, ifa, IFADDR_EVENT_ADD);
531 (void )in_scrubprefix(ia, LLE_STATIC);
534 if (ia->ia_ifa.ifa_carp)
535 (*carp_detach_p)(&ia->ia_ifa, false);
538 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
539 IF_ADDR_WUNLOCK(ifp);
540 ifa_free(&ia->ia_ifa); /* if_addrhead */
543 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
544 LIST_REMOVE(ia, ia_hash);
546 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
552 in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
554 const struct ifreq *ifr = (struct ifreq *)data;
555 const struct sockaddr_in *addr = (const struct sockaddr_in *)
558 struct in_ifaddr *ia;
559 bool deleteAny, iaIsLast;
563 error = priv_check(td, PRIV_NET_DELIFADDR);
568 if (addr->sin_len != sizeof(struct sockaddr_in) ||
569 addr->sin_family != AF_INET)
577 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
578 struct in_ifaddr *it;
580 if (ifa->ifa_addr->sa_family != AF_INET)
583 it = (struct in_ifaddr *)ifa;
584 if (deleteAny && ia == NULL && (td == NULL ||
585 prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0))
588 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
589 (td == NULL || prison_check_ip4(td->td_ucred,
590 &addr->sin_addr) == 0))
598 IF_ADDR_WUNLOCK(ifp);
599 return (EADDRNOTAVAIL);
602 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
603 IF_ADDR_WUNLOCK(ifp);
604 ifa_free(&ia->ia_ifa); /* if_addrhead */
607 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
608 LIST_REMOVE(ia, ia_hash);
612 * in_scrubprefix() kills the interface route.
614 in_scrubprefix(ia, LLE_STATIC);
617 * in_ifadown gets rid of all the rest of
618 * the routes. This is not quite the right
619 * thing to do, but at least if we are running
620 * a routing process they will come back.
622 in_ifadown(&ia->ia_ifa, 1);
624 if (ia->ia_ifa.ifa_carp)
625 (*carp_detach_p)(&ia->ia_ifa, cmd == SIOCAIFADDR);
628 * If this is the last IPv4 address configured on this
629 * interface, leave the all-hosts group.
630 * No state-change report need be transmitted.
632 if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
633 struct in_ifinfo *ii;
635 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
636 if (ii->ii_allhosts) {
637 (void)in_leavegroup(ii->ii_allhosts, NULL);
638 ii->ii_allhosts = NULL;
643 if (callout_stop(&ia->ia_garp_timer) == 1) {
644 ifa_free(&ia->ia_ifa);
646 IF_ADDR_WUNLOCK(ifp);
648 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
650 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
656 in_gifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
658 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
659 const struct sockaddr_in *addr = &ifra->ifra_addr;
660 struct epoch_tracker et;
662 struct in_ifaddr *ia;
665 * ifra_addr must be present and be of INET family.
667 if (addr->sin_len != sizeof(struct sockaddr_in) ||
668 addr->sin_family != AF_INET)
672 * See whether address exist.
676 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
677 struct in_ifaddr *it;
679 if (ifa->ifa_addr->sa_family != AF_INET)
682 it = (struct in_ifaddr *)ifa;
683 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
684 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0) {
691 return (EADDRNOTAVAIL);
694 ifra->ifra_mask = ia->ia_sockmask;
695 if ((ifp->if_flags & IFF_POINTOPOINT) &&
696 ia->ia_dstaddr.sin_family == AF_INET)
697 ifra->ifra_dstaddr = ia->ia_dstaddr;
698 else if ((ifp->if_flags & IFF_BROADCAST) &&
699 ia->ia_broadaddr.sin_family == AF_INET)
700 ifra->ifra_broadaddr = ia->ia_broadaddr;
702 memset(&ifra->ifra_broadaddr, 0,
703 sizeof(ifra->ifra_broadaddr));
710 in_match_ifaddr(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
713 if (nh->nh_ifa == (struct ifaddr *)arg)
720 in_handle_prefix_route(uint32_t fibnum, int cmd,
721 struct sockaddr_in *dst, struct sockaddr_in *netmask, struct ifaddr *ifa,
727 /* Prepare gateway */
728 struct sockaddr_dl_short sdl = {
729 .sdl_family = AF_LINK,
730 .sdl_len = sizeof(struct sockaddr_dl_short),
731 .sdl_type = ifa->ifa_ifp->if_type,
732 .sdl_index = ifa->ifa_ifp->if_index,
735 struct rt_addrinfo info = {
738 .rti_flags = RTF_PINNED | ((netmask != NULL) ? 0 : RTF_HOST),
740 [RTAX_DST] = (struct sockaddr *)dst,
741 [RTAX_NETMASK] = (struct sockaddr *)netmask,
742 [RTAX_GATEWAY] = (struct sockaddr *)&sdl,
744 /* Ensure we delete the prefix IFF prefix ifa matches */
745 .rti_filter = in_match_ifaddr,
746 .rti_filterdata = ifa,
749 return (rib_handle_ifaddr_info(fibnum, cmd, &info));
753 * Routing table interaction with interface addresses.
755 * In general, two types of routes needs to be installed:
756 * a) "interface" or "prefix" route, telling user that the addresses
757 * behind the ifa prefix are reached directly.
758 * b) "loopback" route installed for the ifa address, telling user that
759 * the address belongs to local system.
761 * Handling for (a) and (b) differs in multi-fib aspects, hence they
762 * are implemented in different functions below.
764 * The cases above may intersect - /32 interface aliases results in
765 * the same prefix produced by (a) and (b). This blurs the definition
766 * of the "loopback" route and complicate interactions. The interaction
767 * table is defined below. The case numbers are used in the multiple
768 * functions below to refer to the particular test case.
770 * There can be multiple options:
771 * 1) Adding address with prefix on non-p2p/non-loopback interface.
772 * Example: 192.0.2.1/24. Action:
773 * * add "prefix" route towards 192.0.2.0/24 via @ia interface,
774 * using @ia as an address source.
775 * * add "loopback" route towards 192.0.2.1 via V_loif, saving
776 * @ia ifp in the gateway and using @ia as an address source.
778 * 2) Adding address with /32 mask to non-p2p/non-loopback interface.
779 * Example: 192.0.2.2/32. Action:
780 * * add "prefix" host route via V_loif, using @ia as an address source.
782 * 3) Adding address with or without prefix to p2p interface.
783 * Example: 10.0.0.1/24->10.0.0.2. Action:
784 * * add "prefix" host route towards 10.0.0.2 via this interface, using @ia
785 * as an address source. Note: no sense in installing full /24 as the interface
787 * * add "loopback" route towards 10.0.9.1 via V_loif, saving
788 * @ia ifp in the gateway and using @ia as an address source.
790 * 4) Adding address with or without prefix to loopback interface.
791 * Example: 192.0.2.1/24. Action:
792 * * add "prefix" host route via @ia interface, using @ia as an address source.
793 * Note: Skip installing /24 prefix as it would introduce TTL loop
794 * for the traffic destined to these addresses.
798 * Checks if @ia needs to install loopback route to @ia address via
799 * ifa_maintain_loopback_route().
801 * Return true on success.
804 ia_need_loopback_route(const struct in_ifaddr *ia)
806 struct ifnet *ifp = ia->ia_ifp;
808 /* Case 4: Skip loopback interfaces */
809 if ((ifp->if_flags & IFF_LOOPBACK) ||
810 (ia->ia_addr.sin_addr.s_addr == INADDR_ANY))
813 /* Clash avoidance: Skip p2p interfaces with both addresses are equal */
814 if ((ifp->if_flags & IFF_POINTOPOINT) &&
815 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
818 /* Case 2: skip /32 prefixes */
819 if (!(ifp->if_flags & IFF_POINTOPOINT) &&
820 (ia->ia_sockmask.sin_addr.s_addr == INADDR_BROADCAST))
827 * Calculate "prefix" route corresponding to @ia.
830 ia_getrtprefix(const struct in_ifaddr *ia, struct in_addr *prefix, struct in_addr *mask)
833 if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) {
834 /* Case 3: return host route for dstaddr */
835 *prefix = ia->ia_dstaddr.sin_addr;
836 mask->s_addr = INADDR_BROADCAST;
837 } else if (ia->ia_ifp->if_flags & IFF_LOOPBACK) {
838 /* Case 4: return host route for ifaddr */
839 *prefix = ia->ia_addr.sin_addr;
840 mask->s_addr = INADDR_BROADCAST;
842 /* Cases 1,2: return actual ia prefix */
843 *prefix = ia->ia_addr.sin_addr;
844 *mask = ia->ia_sockmask.sin_addr;
845 prefix->s_addr &= mask->s_addr;
850 * Adds or delete interface "prefix" route corresponding to @ifa.
851 * Returns 0 on success or errno.
854 in_handle_ifaddr_route(int cmd, struct in_ifaddr *ia)
856 struct ifaddr *ifa = &ia->ia_ifa;
857 struct in_addr daddr, maddr;
858 struct sockaddr_in *pmask;
859 struct epoch_tracker et;
862 ia_getrtprefix(ia, &daddr, &maddr);
864 struct sockaddr_in mask = {
865 .sin_family = AF_INET,
866 .sin_len = sizeof(struct sockaddr_in),
870 pmask = (maddr.s_addr != INADDR_BROADCAST) ? &mask : NULL;
872 struct sockaddr_in dst = {
873 .sin_family = AF_INET,
874 .sin_len = sizeof(struct sockaddr_in),
875 .sin_addr.s_addr = daddr.s_addr & maddr.s_addr,
878 struct ifnet *ifp = ia->ia_ifp;
880 if ((maddr.s_addr == INADDR_BROADCAST) &&
881 (!(ia->ia_ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))) {
882 /* Case 2: host route on broadcast interface */
886 uint32_t fibnum = ifa->ifa_ifp->if_fib;
888 error = in_handle_prefix_route(fibnum, cmd, &dst, pmask, ifa, ifp);
895 * Check if we have a route for the given prefix already.
898 in_hasrtprefix(struct in_ifaddr *target)
900 struct rm_priotracker in_ifa_tracker;
901 struct in_ifaddr *ia;
902 struct in_addr prefix, mask, p, m;
905 ia_getrtprefix(target, &prefix, &mask);
907 IN_IFADDR_RLOCK(&in_ifa_tracker);
908 /* Look for an existing address with the same prefix, mask, and fib */
909 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
910 ia_getrtprefix(ia, &p, &m);
912 if (prefix.s_addr != p.s_addr ||
913 mask.s_addr != m.s_addr)
916 if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
920 * If we got a matching prefix route inserted by other
921 * interface address, we are done here.
923 if (ia->ia_flags & IFA_ROUTE) {
928 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
934 in_addprefix(struct in_ifaddr *target)
938 if (in_hasrtprefix(target)) {
942 rt_addrmsg(RTM_ADD, &target->ia_ifa,
943 target->ia_ifp->if_fib);
949 * No-one seem to have this prefix route, so we try to insert it.
951 rt_addrmsg(RTM_ADD, &target->ia_ifa, target->ia_ifp->if_fib);
952 error = in_handle_ifaddr_route(RTM_ADD, target);
954 target->ia_flags |= IFA_ROUTE;
959 * Removes either all lle entries for given @ia, or lle
960 * corresponding to @ia address.
963 in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags)
965 struct sockaddr_in addr, mask;
966 struct sockaddr *saddr, *smask;
969 saddr = (struct sockaddr *)&addr;
970 bzero(&addr, sizeof(addr));
971 addr.sin_len = sizeof(addr);
972 addr.sin_family = AF_INET;
973 smask = (struct sockaddr *)&mask;
974 bzero(&mask, sizeof(mask));
975 mask.sin_len = sizeof(mask);
976 mask.sin_family = AF_INET;
977 mask.sin_addr.s_addr = ia->ia_subnetmask;
982 * Remove all L2 entries matching given prefix.
983 * Convert address to host representation to avoid
984 * doing this on every callback. ia_subnetmask is already
985 * stored in host representation.
987 addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr);
988 lltable_prefix_free(AF_INET, saddr, smask, flags);
990 /* Remove interface address only */
991 addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr;
992 lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr);
997 * If there is no other address in the system that can serve a route to the
998 * same prefix, remove the route. Hand over the route to the new address
1002 in_scrubprefix(struct in_ifaddr *target, u_int flags)
1004 struct rm_priotracker in_ifa_tracker;
1005 struct in_ifaddr *ia;
1006 struct in_addr prefix, mask, p, m;
1010 * Remove the loopback route to the interface address.
1012 if (ia_need_loopback_route(target) && (flags & LLE_STATIC)) {
1013 struct in_ifaddr *eia;
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 LLE_FREE_LOCKED when number of references
1272 in_lltable_destroy_lle(struct llentry *lle)
1276 NET_EPOCH_CALL(in_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
1279 static struct llentry *
1280 in_lltable_new(struct in_addr addr4, u_int flags)
1282 struct in_llentry *lle;
1284 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
1285 if (lle == NULL) /* NB: caller generates msg */
1289 * For IPv4 this will trigger "arpresolve" to generate
1292 lle->base.la_expire = time_uptime; /* mark expired */
1293 lle->base.r_l3addr.addr4 = addr4;
1294 lle->base.lle_refcnt = 1;
1295 lle->base.lle_free = in_lltable_destroy_lle;
1296 LLE_LOCK_INIT(&lle->base);
1297 LLE_REQ_INIT(&lle->base);
1298 callout_init(&lle->base.lle_timer, 1);
1300 return (&lle->base);
1303 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1304 ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
1307 in_lltable_match_prefix(const struct sockaddr *saddr,
1308 const struct sockaddr *smask, u_int flags, struct llentry *lle)
1310 struct in_addr addr, mask, lle_addr;
1312 addr = ((const struct sockaddr_in *)saddr)->sin_addr;
1313 mask = ((const struct sockaddr_in *)smask)->sin_addr;
1314 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
1316 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
1319 if (lle->la_flags & LLE_IFADDR) {
1321 * Delete LLE_IFADDR records IFF address & flag matches.
1322 * Note that addr is the interface address within prefix
1324 * Note also we should handle 'ifdown' cases without removing
1327 if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0)
1332 /* flags & LLE_STATIC means deleting both dynamic and static entries */
1333 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
1340 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
1342 size_t pkts_dropped;
1344 LLE_WLOCK_ASSERT(lle);
1345 KASSERT(llt != NULL, ("lltable is NULL"));
1347 /* Unlink entry from table if not already */
1348 if ((lle->la_flags & LLE_LINKED) != 0) {
1349 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
1350 lltable_unlink_entry(llt, lle);
1353 /* Drop hold queue */
1354 pkts_dropped = llentry_free(lle);
1355 ARPSTAT_ADD(dropped, pkts_dropped);
1359 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1361 struct rt_addrinfo info;
1362 struct sockaddr_in rt_key, rt_mask;
1363 struct sockaddr rt_gateway;
1366 KASSERT(l3addr->sa_family == AF_INET,
1367 ("sin_family %d", l3addr->sa_family));
1369 bzero(&rt_key, sizeof(rt_key));
1370 rt_key.sin_len = sizeof(rt_key);
1371 bzero(&rt_mask, sizeof(rt_mask));
1372 rt_mask.sin_len = sizeof(rt_mask);
1373 bzero(&rt_gateway, sizeof(rt_gateway));
1374 rt_gateway.sa_len = sizeof(rt_gateway);
1376 bzero(&info, sizeof(info));
1377 info.rti_info[RTAX_DST] = (struct sockaddr *)&rt_key;
1378 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&rt_mask;
1379 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&rt_gateway;
1381 if (rib_lookup_info(ifp->if_fib, l3addr, NHR_REF, 0, &info) != 0)
1384 rt_flags = info.rti_flags;
1387 * If the gateway for an existing host route matches the target L3
1388 * address, which is a special route inserted by some implementation
1389 * such as MANET, and the interface is of the correct type, then
1390 * allow for ARP to proceed.
1392 if (rt_flags & RTF_GATEWAY) {
1393 if (!(rt_flags & RTF_HOST) || !info.rti_ifp ||
1394 info.rti_ifp->if_type != IFT_ETHER ||
1395 (info.rti_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1396 memcmp(rt_gateway.sa_data, l3addr->sa_data,
1397 sizeof(in_addr_t)) != 0) {
1398 rib_free_info(&info);
1402 rib_free_info(&info);
1405 * Make sure that at least the destination address is covered
1406 * by the route. This is for handling the case where 2 or more
1407 * interfaces have the same prefix. An incoming packet arrives
1408 * on one interface and the corresponding outgoing packet leaves
1409 * another interface.
1411 if (!(rt_flags & RTF_HOST) && info.rti_ifp != ifp) {
1412 const char *sa, *mask, *addr, *lim;
1413 const struct sockaddr_in *l3sin;
1415 mask = (const char *)&rt_mask;
1417 * Just being extra cautious to avoid some custom
1418 * code getting into trouble.
1420 if ((info.rti_addrs & RTA_NETMASK) == 0)
1423 sa = (const char *)&rt_key;
1424 addr = (const char *)l3addr;
1425 l3sin = (const struct sockaddr_in *)l3addr;
1426 lim = addr + l3sin->sin_len;
1428 for ( ; addr < lim; sa++, mask++, addr++) {
1429 if ((*sa ^ *addr) & *mask) {
1431 char addrbuf[INET_ADDRSTRLEN];
1433 log(LOG_INFO, "IPv4 address: \"%s\" "
1434 "is not on the network\n",
1435 inet_ntoa_r(l3sin->sin_addr, addrbuf));
1445 static inline uint32_t
1446 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
1449 return (IN_LLTBL_HASH(dst.s_addr, hsize));
1453 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
1456 return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
1460 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
1462 struct sockaddr_in *sin;
1464 sin = (struct sockaddr_in *)sa;
1465 bzero(sin, sizeof(*sin));
1466 sin->sin_family = AF_INET;
1467 sin->sin_len = sizeof(*sin);
1468 sin->sin_addr = lle->r_l3addr.addr4;
1471 static inline struct llentry *
1472 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
1474 struct llentry *lle;
1475 struct llentries *lleh;
1478 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
1479 lleh = &llt->lle_head[hashidx];
1480 CK_LIST_FOREACH(lle, lleh, lle_next) {
1481 if (lle->la_flags & LLE_DELETED)
1483 if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
1491 in_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
1494 lle->la_flags |= LLE_DELETED;
1495 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1497 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1502 static struct llentry *
1503 in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1505 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1506 struct ifnet *ifp = llt->llt_ifp;
1507 struct llentry *lle;
1508 char linkhdr[LLE_MAX_LINKHDR];
1512 KASSERT(l3addr->sa_family == AF_INET,
1513 ("sin_family %d", l3addr->sa_family));
1516 * A route that covers the given address must have
1517 * been installed 1st because we are doing a resolution,
1520 if (!(flags & LLE_IFADDR) &&
1521 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1524 lle = in_lltable_new(sin->sin_addr, flags);
1526 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1529 lle->la_flags = flags;
1530 if (flags & LLE_STATIC)
1531 lle->r_flags |= RLLE_VALID;
1532 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
1533 linkhdrsize = LLE_MAX_LINKHDR;
1534 if (lltable_calc_llheader(ifp, AF_INET, IF_LLADDR(ifp),
1535 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
1536 NET_EPOCH_CALL(in_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
1539 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
1541 lle->la_flags |= LLE_STATIC;
1542 lle->r_flags |= (RLLE_VALID | RLLE_IFADDR);
1549 * Return NULL if not found or marked for deletion.
1550 * If found return lle read locked.
1552 static struct llentry *
1553 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1555 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1556 struct llentry *lle;
1558 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
1559 KASSERT(l3addr->sa_family == AF_INET,
1560 ("sin_family %d", l3addr->sa_family));
1561 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) !=
1562 (LLE_UNLOCKED | LLE_EXCLUSIVE),
1563 ("wrong lle request flags: %#x", flags));
1565 lle = in_lltable_find_dst(llt, sin->sin_addr);
1568 if (flags & LLE_UNLOCKED)
1571 if (flags & LLE_EXCLUSIVE)
1577 * If the afdata lock is not held, the LLE may have been unlinked while
1578 * we were blocked on the LLE lock. Check for this case.
1580 if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) {
1581 if (flags & LLE_EXCLUSIVE)
1591 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
1592 struct sysctl_req *wr)
1594 struct ifnet *ifp = llt->llt_ifp;
1597 struct rt_msghdr rtm;
1598 struct sockaddr_in sin;
1599 struct sockaddr_dl sdl;
1601 struct sockaddr_dl *sdl;
1604 bzero(&arpc, sizeof(arpc));
1605 /* skip deleted entries */
1606 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1608 /* Skip if jailed and not a valid IP of the prison. */
1609 lltable_fill_sa_entry(lle,(struct sockaddr *)&arpc.sin);
1610 if (prison_if(wr->td->td_ucred, (struct sockaddr *)&arpc.sin) != 0)
1613 * produce a msg made of:
1615 * struct sockaddr_in; (IPv4)
1616 * struct sockaddr_dl;
1618 arpc.rtm.rtm_msglen = sizeof(arpc);
1619 arpc.rtm.rtm_version = RTM_VERSION;
1620 arpc.rtm.rtm_type = RTM_GET;
1621 arpc.rtm.rtm_flags = RTF_UP;
1622 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1625 if (lle->la_flags & LLE_PUB)
1626 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1629 sdl->sdl_family = AF_LINK;
1630 sdl->sdl_len = sizeof(*sdl);
1631 sdl->sdl_index = ifp->if_index;
1632 sdl->sdl_type = ifp->if_type;
1633 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1634 sdl->sdl_alen = ifp->if_addrlen;
1635 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1638 bzero(LLADDR(sdl), ifp->if_addrlen);
1641 arpc.rtm.rtm_rmx.rmx_expire =
1642 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1643 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1644 if (lle->la_flags & LLE_STATIC)
1645 arpc.rtm.rtm_flags |= RTF_STATIC;
1646 if (lle->la_flags & LLE_IFADDR)
1647 arpc.rtm.rtm_flags |= RTF_PINNED;
1648 arpc.rtm.rtm_index = ifp->if_index;
1649 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1654 static struct lltable *
1655 in_lltattach(struct ifnet *ifp)
1657 struct lltable *llt;
1659 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
1660 llt->llt_af = AF_INET;
1663 llt->llt_lookup = in_lltable_lookup;
1664 llt->llt_alloc_entry = in_lltable_alloc;
1665 llt->llt_delete_entry = in_lltable_delete_entry;
1666 llt->llt_dump_entry = in_lltable_dump_entry;
1667 llt->llt_hash = in_lltable_hash;
1668 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
1669 llt->llt_free_entry = in_lltable_free_entry;
1670 llt->llt_match_prefix = in_lltable_match_prefix;
1671 llt->llt_mark_used = llentry_mark_used;
1678 in_domifattach(struct ifnet *ifp)
1680 struct in_ifinfo *ii;
1682 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1684 ii->ii_llt = in_lltattach(ifp);
1685 ii->ii_igmp = igmp_domifattach(ifp);
1691 in_domifdetach(struct ifnet *ifp, void *aux)
1693 struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1695 igmp_domifdetach(ifp);
1696 lltable_free(ii->ii_llt);