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/sysctl.h>
50 #include <sys/syslog.h>
54 #include <net/if_var.h>
55 #include <net/if_arp.h>
56 #include <net/if_dl.h>
57 #include <net/if_llatbl.h>
58 #include <net/if_types.h>
59 #include <net/route.h>
60 #include <net/route/nhop.h>
61 #include <net/route/route_ctl.h>
64 #include <netinet/if_ether.h>
65 #include <netinet/in.h>
66 #include <netinet/in_fib.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_DEFINE_STATIC(bool, broadcast_lowest);
91 #define V_broadcast_lowest VNET(broadcast_lowest)
92 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, broadcast_lowest, CTLFLAG_VNET | CTLFLAG_RW,
93 &VNET_NAME(broadcast_lowest), 0,
94 "Treat lowest address on a subnet (host 0) as broadcast");
96 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
97 #define V_ripcbinfo VNET(ripcbinfo)
99 static struct sx in_control_sx;
100 SX_SYSINIT(in_control_sx, &in_control_sx, "in_control");
103 * Return 1 if an internet address is for a ``local'' host
104 * (one to which we have a connection).
107 in_localaddr(struct in_addr in)
109 u_long i = ntohl(in.s_addr);
110 struct in_ifaddr *ia;
114 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
115 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
123 * Return 1 if an internet address is for the local host and configured
124 * on one of its interfaces.
127 in_localip(struct in_addr in)
129 struct in_ifaddr *ia;
133 CK_LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash)
134 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
141 * Return 1 if an internet address is configured on an interface.
144 in_ifhasaddr(struct ifnet *ifp, struct in_addr in)
147 struct in_ifaddr *ia;
151 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
152 if (ifa->ifa_addr->sa_family != AF_INET)
154 ia = (struct in_ifaddr *)ifa;
155 if (ia->ia_addr.sin_addr.s_addr == in.s_addr)
163 * Return a reference to the interface address which is different to
164 * the supplied one but with same IP address value.
166 static struct in_ifaddr *
167 in_localip_more(struct in_ifaddr *original_ia)
169 struct epoch_tracker et;
170 in_addr_t original_addr = IA_SIN(original_ia)->sin_addr.s_addr;
171 uint32_t original_fib = original_ia->ia_ifa.ifa_ifp->if_fib;
172 struct in_ifaddr *ia;
175 CK_LIST_FOREACH(ia, INADDR_HASH(original_addr), ia_hash) {
176 in_addr_t addr = IA_SIN(ia)->sin_addr.s_addr;
177 uint32_t fib = ia->ia_ifa.ifa_ifp->if_fib;
178 if (!V_rt_add_addr_allfibs && (original_fib != fib))
180 if ((original_ia != ia) && (original_addr == addr)) {
181 ifa_ref(&ia->ia_ifa);
192 * Tries to find first IPv4 address in the provided fib.
193 * Prefers non-loopback addresses and return loopback IFF
194 * @loopback_ok is set.
196 * Returns ifa or NULL.
199 in_findlocal(uint32_t fibnum, bool loopback_ok)
201 struct in_ifaddr *ia = NULL, *ia_lo = NULL;
205 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
206 uint32_t ia_fib = ia->ia_ifa.ifa_ifp->if_fib;
207 if (!V_rt_add_addr_allfibs && (fibnum != ia_fib))
210 if (!IN_LOOPBACK(ntohl(IA_SIN(ia)->sin_addr.s_addr)))
223 * Determine whether an IP address is in a reserved set of addresses
224 * that may not be forwarded, or whether datagrams to that destination
228 in_canforward(struct in_addr in)
230 u_long i = ntohl(in.s_addr);
232 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i) ||
233 IN_ZERONET(i) || IN_LOOPBACK(i))
239 * Trim a mask in a sockaddr
242 in_socktrim(struct sockaddr_in *ap)
244 char *cplim = (char *) &ap->sin_addr;
245 char *cp = (char *) (&ap->sin_addr + 1);
248 while (--cp >= cplim)
250 (ap)->sin_len = cp - (char *) (ap) + 1;
256 * Generic internet control operations (ioctl's).
259 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
262 struct ifreq *ifr = (struct ifreq *)data;
263 struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
264 struct epoch_tracker et;
266 struct in_ifaddr *ia;
270 return (EADDRNOTAVAIL);
273 * Filter out 4 ioctls we implement directly. Forward the rest
274 * to specific functions and ifp->if_ioctl().
283 sx_xlock(&in_control_sx);
284 error = in_gifaddr_ioctl(cmd, data, ifp, td);
285 sx_xunlock(&in_control_sx);
288 sx_xlock(&in_control_sx);
289 error = in_difaddr_ioctl(cmd, data, ifp, td);
290 sx_xunlock(&in_control_sx);
292 case OSIOCAIFADDR: /* 9.x compat */
294 sx_xlock(&in_control_sx);
295 error = in_aifaddr_ioctl(cmd, data, ifp, td);
296 sx_xunlock(&in_control_sx);
302 /* We no longer support that old commands. */
305 if (ifp->if_ioctl == NULL)
307 return ((*ifp->if_ioctl)(ifp, cmd, data));
310 if (addr->sin_addr.s_addr != INADDR_ANY &&
311 prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0)
312 return (EADDRNOTAVAIL);
315 * Find address for this interface, if it exists. If an
316 * address was specified, find that one instead of the
317 * first one on the interface, if possible.
320 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
321 if (ifa->ifa_addr->sa_family != AF_INET)
323 ia = (struct in_ifaddr *)ifa;
324 if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
328 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
329 if (ifa->ifa_addr->sa_family == AF_INET) {
330 ia = (struct in_ifaddr *)ifa;
331 if (prison_check_ip4(td->td_ucred,
332 &ia->ia_addr.sin_addr) == 0)
338 return (EADDRNOTAVAIL);
348 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
352 *addr = ia->ia_broadaddr;
356 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
360 *addr = ia->ia_dstaddr;
364 *addr = ia->ia_sockmask;
374 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
376 const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
377 const struct sockaddr_in *addr = &ifra->ifra_addr;
378 const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
379 const struct sockaddr_in *mask = &ifra->ifra_mask;
380 const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
381 const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
382 struct epoch_tracker et;
384 struct in_ifaddr *ia;
388 error = priv_check(td, PRIV_NET_ADDIFADDR);
393 * ifra_addr must be present and be of INET family.
394 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
396 if (addr->sin_len != sizeof(struct sockaddr_in) ||
397 addr->sin_family != AF_INET)
399 if (broadaddr->sin_len != 0 &&
400 (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
401 broadaddr->sin_family != AF_INET))
403 if (mask->sin_len != 0 &&
404 (mask->sin_len != sizeof(struct sockaddr_in) ||
405 mask->sin_family != AF_INET))
407 if ((ifp->if_flags & IFF_POINTOPOINT) &&
408 (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
409 dstaddr->sin_addr.s_addr == INADDR_ANY))
410 return (EDESTADDRREQ);
411 if (vhid != 0 && carp_attach_p == NULL)
412 return (EPROTONOSUPPORT);
415 * See whether address already exist.
420 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
421 struct in_ifaddr *it;
423 if (ifa->ifa_addr->sa_family != AF_INET)
426 it = (struct in_ifaddr *)ifa;
427 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
428 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0)
436 (void )in_difaddr_ioctl(cmd, data, ifp, td);
438 ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
439 ia = (struct in_ifaddr *)ifa;
440 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
441 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
442 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
443 callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock,
444 CALLOUT_RETURNUNLOCKED);
448 if (mask->sin_len != 0) {
449 ia->ia_sockmask = *mask;
450 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
453 * If netmask isn't supplied, use default for now.
454 * This is deprecated for interfaces other than loopback
455 * or point-to-point; warn in other cases. In the future
456 * we should return an error rather than warning.
458 if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0)
459 printf("%s: set address: WARNING: network mask"
460 " should be specified; using default mask\n",
462 ia->ia_subnetmask = IN_NETMASK_DEFAULT;
463 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
465 ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
466 in_socktrim(&ia->ia_sockmask);
468 if (ifp->if_flags & IFF_BROADCAST) {
469 if (broadaddr->sin_len != 0) {
470 ia->ia_broadaddr = *broadaddr;
471 } else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
472 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
473 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
474 ia->ia_broadaddr.sin_family = AF_INET;
476 ia->ia_broadaddr.sin_addr.s_addr =
477 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
478 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
479 ia->ia_broadaddr.sin_family = AF_INET;
483 if (ifp->if_flags & IFF_POINTOPOINT)
484 ia->ia_dstaddr = *dstaddr;
487 error = (*carp_attach_p)(&ia->ia_ifa, vhid);
492 /* if_addrhead is already referenced by ifa_alloc() */
494 CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
495 IF_ADDR_WUNLOCK(ifp);
497 ifa_ref(ifa); /* in_ifaddrhead */
498 sx_assert(&in_control_sx, SA_XLOCKED);
499 CK_STAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
500 CK_LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia,
504 * Give the interface a chance to initialize
505 * if this is its first address,
506 * and to validate the address if necessary.
508 if (ifp->if_ioctl != NULL) {
509 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
515 * Add route for the network.
518 error = in_addprefix(ia);
524 * Add a loopback route to self.
526 if (vhid == 0 && ia_need_loopback_route(ia)) {
527 struct in_ifaddr *eia;
529 eia = in_localip_more(ia);
532 error = ifa_add_loopback_route((struct ifaddr *)ia,
533 (struct sockaddr *)&ia->ia_addr);
537 ifa_free(&eia->ia_ifa);
540 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
541 struct in_addr allhosts_addr;
542 struct in_ifinfo *ii;
544 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
545 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
547 error = in_joingroup(ifp, &allhosts_addr, NULL,
552 * Note: we don't need extra reference for ifa, since we called
553 * with sx lock held, and ifaddr can not be deleted in concurrent
556 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, ifa, IFADDR_EVENT_ADD);
562 (void )in_scrubprefix(ia, LLE_STATIC);
565 if (ia->ia_ifa.ifa_carp)
566 (*carp_detach_p)(&ia->ia_ifa, false);
569 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
570 IF_ADDR_WUNLOCK(ifp);
571 ifa_free(&ia->ia_ifa); /* if_addrhead */
573 sx_assert(&in_control_sx, SA_XLOCKED);
574 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
575 CK_LIST_REMOVE(ia, ia_hash);
576 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
582 in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
584 const struct ifreq *ifr = (struct ifreq *)data;
585 const struct sockaddr_in *addr = (const struct sockaddr_in *)
588 struct in_ifaddr *ia;
589 bool deleteAny, iaIsLast;
593 error = priv_check(td, PRIV_NET_DELIFADDR);
598 if (addr->sin_len != sizeof(struct sockaddr_in) ||
599 addr->sin_family != AF_INET)
607 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
608 struct in_ifaddr *it;
610 if (ifa->ifa_addr->sa_family != AF_INET)
613 it = (struct in_ifaddr *)ifa;
614 if (deleteAny && ia == NULL && (td == NULL ||
615 prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0))
618 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
619 (td == NULL || prison_check_ip4(td->td_ucred,
620 &addr->sin_addr) == 0))
628 IF_ADDR_WUNLOCK(ifp);
629 return (EADDRNOTAVAIL);
632 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
633 IF_ADDR_WUNLOCK(ifp);
634 ifa_free(&ia->ia_ifa); /* if_addrhead */
636 sx_assert(&in_control_sx, SA_XLOCKED);
637 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
638 CK_LIST_REMOVE(ia, ia_hash);
641 * in_scrubprefix() kills the interface route.
643 in_scrubprefix(ia, LLE_STATIC);
646 * in_ifadown gets rid of all the rest of
647 * the routes. This is not quite the right
648 * thing to do, but at least if we are running
649 * a routing process they will come back.
651 in_ifadown(&ia->ia_ifa, 1);
653 if (ia->ia_ifa.ifa_carp)
654 (*carp_detach_p)(&ia->ia_ifa, cmd == SIOCAIFADDR);
657 * If this is the last IPv4 address configured on this
658 * interface, leave the all-hosts group.
659 * No state-change report need be transmitted.
661 if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
662 struct in_ifinfo *ii;
664 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
665 if (ii->ii_allhosts) {
666 (void)in_leavegroup(ii->ii_allhosts, NULL);
667 ii->ii_allhosts = NULL;
672 if (callout_stop(&ia->ia_garp_timer) == 1) {
673 ifa_free(&ia->ia_ifa);
675 IF_ADDR_WUNLOCK(ifp);
677 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
679 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
685 in_gifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
687 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
688 const struct sockaddr_in *addr = &ifra->ifra_addr;
689 struct epoch_tracker et;
691 struct in_ifaddr *ia;
694 * ifra_addr must be present and be of INET family.
696 if (addr->sin_len != sizeof(struct sockaddr_in) ||
697 addr->sin_family != AF_INET)
701 * See whether address exist.
705 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
706 struct in_ifaddr *it;
708 if (ifa->ifa_addr->sa_family != AF_INET)
711 it = (struct in_ifaddr *)ifa;
712 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
713 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0) {
720 return (EADDRNOTAVAIL);
723 ifra->ifra_mask = ia->ia_sockmask;
724 if ((ifp->if_flags & IFF_POINTOPOINT) &&
725 ia->ia_dstaddr.sin_family == AF_INET)
726 ifra->ifra_dstaddr = ia->ia_dstaddr;
727 else if ((ifp->if_flags & IFF_BROADCAST) &&
728 ia->ia_broadaddr.sin_family == AF_INET)
729 ifra->ifra_broadaddr = ia->ia_broadaddr;
731 memset(&ifra->ifra_broadaddr, 0,
732 sizeof(ifra->ifra_broadaddr));
739 in_match_ifaddr(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
742 if (nh->nh_ifa == (struct ifaddr *)arg)
749 in_handle_prefix_route(uint32_t fibnum, int cmd,
750 struct sockaddr_in *dst, struct sockaddr_in *netmask, struct ifaddr *ifa,
756 /* Prepare gateway */
757 struct sockaddr_dl_short sdl = {
758 .sdl_family = AF_LINK,
759 .sdl_len = sizeof(struct sockaddr_dl_short),
760 .sdl_type = ifa->ifa_ifp->if_type,
761 .sdl_index = ifa->ifa_ifp->if_index,
764 struct rt_addrinfo info = {
767 .rti_flags = RTF_PINNED | ((netmask != NULL) ? 0 : RTF_HOST),
769 [RTAX_DST] = (struct sockaddr *)dst,
770 [RTAX_NETMASK] = (struct sockaddr *)netmask,
771 [RTAX_GATEWAY] = (struct sockaddr *)&sdl,
773 /* Ensure we delete the prefix IFF prefix ifa matches */
774 .rti_filter = in_match_ifaddr,
775 .rti_filterdata = ifa,
778 return (rib_handle_ifaddr_info(fibnum, cmd, &info));
782 * Routing table interaction with interface addresses.
784 * In general, two types of routes needs to be installed:
785 * a) "interface" or "prefix" route, telling user that the addresses
786 * behind the ifa prefix are reached directly.
787 * b) "loopback" route installed for the ifa address, telling user that
788 * the address belongs to local system.
790 * Handling for (a) and (b) differs in multi-fib aspects, hence they
791 * are implemented in different functions below.
793 * The cases above may intersect - /32 interface aliases results in
794 * the same prefix produced by (a) and (b). This blurs the definition
795 * of the "loopback" route and complicate interactions. The interaction
796 * table is defined below. The case numbers are used in the multiple
797 * functions below to refer to the particular test case.
799 * There can be multiple options:
800 * 1) Adding address with prefix on non-p2p/non-loopback interface.
801 * Example: 192.0.2.1/24. Action:
802 * * add "prefix" route towards 192.0.2.0/24 via @ia interface,
803 * using @ia as an address source.
804 * * add "loopback" route towards 192.0.2.1 via V_loif, saving
805 * @ia ifp in the gateway and using @ia as an address source.
807 * 2) Adding address with /32 mask to non-p2p/non-loopback interface.
808 * Example: 192.0.2.2/32. Action:
809 * * add "prefix" host route via V_loif, using @ia as an address source.
811 * 3) Adding address with or without prefix to p2p interface.
812 * Example: 10.0.0.1/24->10.0.0.2. Action:
813 * * add "prefix" host route towards 10.0.0.2 via this interface, using @ia
814 * as an address source. Note: no sense in installing full /24 as the interface
816 * * add "loopback" route towards 10.0.9.1 via V_loif, saving
817 * @ia ifp in the gateway and using @ia as an address source.
819 * 4) Adding address with or without prefix to loopback interface.
820 * Example: 192.0.2.1/24. Action:
821 * * add "prefix" host route via @ia interface, using @ia as an address source.
822 * Note: Skip installing /24 prefix as it would introduce TTL loop
823 * for the traffic destined to these addresses.
827 * Checks if @ia needs to install loopback route to @ia address via
828 * ifa_maintain_loopback_route().
830 * Return true on success.
833 ia_need_loopback_route(const struct in_ifaddr *ia)
835 struct ifnet *ifp = ia->ia_ifp;
837 /* Case 4: Skip loopback interfaces */
838 if ((ifp->if_flags & IFF_LOOPBACK) ||
839 (ia->ia_addr.sin_addr.s_addr == INADDR_ANY))
842 /* Clash avoidance: Skip p2p interfaces with both addresses are equal */
843 if ((ifp->if_flags & IFF_POINTOPOINT) &&
844 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
847 /* Case 2: skip /32 prefixes */
848 if (!(ifp->if_flags & IFF_POINTOPOINT) &&
849 (ia->ia_sockmask.sin_addr.s_addr == INADDR_BROADCAST))
856 * Calculate "prefix" route corresponding to @ia.
859 ia_getrtprefix(const struct in_ifaddr *ia, struct in_addr *prefix, struct in_addr *mask)
862 if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) {
863 /* Case 3: return host route for dstaddr */
864 *prefix = ia->ia_dstaddr.sin_addr;
865 mask->s_addr = INADDR_BROADCAST;
866 } else if (ia->ia_ifp->if_flags & IFF_LOOPBACK) {
867 /* Case 4: return host route for ifaddr */
868 *prefix = ia->ia_addr.sin_addr;
869 mask->s_addr = INADDR_BROADCAST;
871 /* Cases 1,2: return actual ia prefix */
872 *prefix = ia->ia_addr.sin_addr;
873 *mask = ia->ia_sockmask.sin_addr;
874 prefix->s_addr &= mask->s_addr;
879 * Adds or delete interface "prefix" route corresponding to @ifa.
880 * Returns 0 on success or errno.
883 in_handle_ifaddr_route(int cmd, struct in_ifaddr *ia)
885 struct ifaddr *ifa = &ia->ia_ifa;
886 struct in_addr daddr, maddr;
887 struct sockaddr_in *pmask;
888 struct epoch_tracker et;
891 ia_getrtprefix(ia, &daddr, &maddr);
893 struct sockaddr_in mask = {
894 .sin_family = AF_INET,
895 .sin_len = sizeof(struct sockaddr_in),
899 pmask = (maddr.s_addr != INADDR_BROADCAST) ? &mask : NULL;
901 struct sockaddr_in dst = {
902 .sin_family = AF_INET,
903 .sin_len = sizeof(struct sockaddr_in),
904 .sin_addr.s_addr = daddr.s_addr & maddr.s_addr,
907 struct ifnet *ifp = ia->ia_ifp;
909 if ((maddr.s_addr == INADDR_BROADCAST) &&
910 (!(ia->ia_ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))) {
911 /* Case 2: host route on broadcast interface */
915 uint32_t fibnum = ifa->ifa_ifp->if_fib;
917 error = in_handle_prefix_route(fibnum, cmd, &dst, pmask, ifa, ifp);
924 * Check if we have a route for the given prefix already.
927 in_hasrtprefix(struct in_ifaddr *target)
929 struct epoch_tracker et;
930 struct in_ifaddr *ia;
931 struct in_addr prefix, mask, p, m;
934 ia_getrtprefix(target, &prefix, &mask);
936 /* Look for an existing address with the same prefix, mask, and fib */
938 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
939 ia_getrtprefix(ia, &p, &m);
941 if (prefix.s_addr != p.s_addr ||
942 mask.s_addr != m.s_addr)
945 if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
949 * If we got a matching prefix route inserted by other
950 * interface address, we are done here.
952 if (ia->ia_flags & IFA_ROUTE) {
963 in_addprefix(struct in_ifaddr *target)
967 if (in_hasrtprefix(target)) {
971 rt_addrmsg(RTM_ADD, &target->ia_ifa,
972 target->ia_ifp->if_fib);
978 * No-one seem to have this prefix route, so we try to insert it.
980 rt_addrmsg(RTM_ADD, &target->ia_ifa, target->ia_ifp->if_fib);
981 error = in_handle_ifaddr_route(RTM_ADD, target);
983 target->ia_flags |= IFA_ROUTE;
988 * Removes either all lle entries for given @ia, or lle
989 * corresponding to @ia address.
992 in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags)
994 struct sockaddr_in addr, mask;
995 struct sockaddr *saddr, *smask;
998 saddr = (struct sockaddr *)&addr;
999 bzero(&addr, sizeof(addr));
1000 addr.sin_len = sizeof(addr);
1001 addr.sin_family = AF_INET;
1002 smask = (struct sockaddr *)&mask;
1003 bzero(&mask, sizeof(mask));
1004 mask.sin_len = sizeof(mask);
1005 mask.sin_family = AF_INET;
1006 mask.sin_addr.s_addr = ia->ia_subnetmask;
1011 * Remove all L2 entries matching given prefix.
1012 * Convert address to host representation to avoid
1013 * doing this on every callback. ia_subnetmask is already
1014 * stored in host representation.
1016 addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr);
1017 lltable_prefix_free(AF_INET, saddr, smask, flags);
1019 /* Remove interface address only */
1020 addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr;
1021 lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr);
1026 * If there is no other address in the system that can serve a route to the
1027 * same prefix, remove the route. Hand over the route to the new address
1031 in_scrubprefix(struct in_ifaddr *target, u_int flags)
1033 struct epoch_tracker et;
1034 struct in_ifaddr *ia;
1035 struct in_addr prefix, mask, p, m;
1039 * Remove the loopback route to the interface address.
1041 if (ia_need_loopback_route(target) && (flags & LLE_STATIC)) {
1042 struct in_ifaddr *eia;
1044 eia = in_localip_more(target);
1047 error = ifa_switch_loopback_route((struct ifaddr *)eia,
1048 (struct sockaddr *)&target->ia_addr);
1049 ifa_free(&eia->ia_ifa);
1051 error = ifa_del_loopback_route((struct ifaddr *)target,
1052 (struct sockaddr *)&target->ia_addr);
1056 ia_getrtprefix(target, &prefix, &mask);
1058 if ((target->ia_flags & IFA_ROUTE) == 0) {
1059 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1062 * Removing address from !IFF_UP interface or
1063 * prefix which exists on other interface (along with route).
1064 * No entries should exist here except target addr.
1065 * Given that, delete this entry only.
1067 in_scrubprefixlle(target, 0, flags);
1071 NET_EPOCH_ENTER(et);
1072 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1073 ia_getrtprefix(ia, &p, &m);
1075 if (prefix.s_addr != p.s_addr ||
1076 mask.s_addr != m.s_addr)
1079 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1083 * If we got a matching prefix address, move IFA_ROUTE and
1084 * the route itself to it. Make sure that routing daemons
1087 if ((ia->ia_flags & IFA_ROUTE) == 0) {
1088 ifa_ref(&ia->ia_ifa);
1090 error = in_handle_ifaddr_route(RTM_DELETE, target);
1092 target->ia_flags &= ~IFA_ROUTE;
1094 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
1096 /* Scrub all entries IFF interface is different */
1097 in_scrubprefixlle(target, target->ia_ifp != ia->ia_ifp,
1099 error = in_handle_ifaddr_route(RTM_ADD, ia);
1101 ia->ia_flags |= IFA_ROUTE;
1103 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
1105 ifa_free(&ia->ia_ifa);
1112 * remove all L2 entries on the given prefix
1114 in_scrubprefixlle(target, 1, flags);
1117 * As no-one seem to have this prefix, we can remove the route.
1119 rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1120 error = in_handle_ifaddr_route(RTM_DELETE, target);
1122 target->ia_flags &= ~IFA_ROUTE;
1124 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
1129 in_ifscrub_all(void)
1132 struct ifaddr *ifa, *nifa;
1133 struct ifaliasreq ifr;
1136 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1137 /* Cannot lock here - lock recursion. */
1138 /* NET_EPOCH_ENTER(et); */
1139 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1140 if (ifa->ifa_addr->sa_family != AF_INET)
1144 * This is ugly but the only way for legacy IP to
1145 * cleanly remove addresses and everything attached.
1147 bzero(&ifr, sizeof(ifr));
1148 ifr.ifra_addr = *ifa->ifa_addr;
1149 if (ifa->ifa_dstaddr)
1150 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1151 (void)in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr,
1154 /* NET_EPOCH_EXIT(et); */
1155 in_purgemaddrs(ifp);
1156 igmp_domifdetach(ifp);
1162 in_ifaddr_broadcast(struct in_addr in, struct in_ifaddr *ia)
1165 return ((in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1167 * Optionally check for old-style (host 0) broadcast, but
1168 * taking into account that RFC 3021 obsoletes it.
1170 (V_broadcast_lowest && ia->ia_subnetmask != IN_RFC3021_MASK &&
1171 ntohl(in.s_addr) == ia->ia_subnet)) &&
1173 * Check for an all one subnetmask. These
1174 * only exist when an interface gets a secondary
1177 ia->ia_subnetmask != (u_long)0xffffffff);
1181 * Return 1 if the address might be a local broadcast address.
1184 in_broadcast(struct in_addr in, struct ifnet *ifp)
1191 if (in.s_addr == INADDR_BROADCAST ||
1192 in.s_addr == INADDR_ANY)
1194 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1198 * Look through the list of addresses for a match
1199 * with a broadcast address.
1201 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1202 if (ifa->ifa_addr->sa_family == AF_INET &&
1203 in_ifaddr_broadcast(in, (struct in_ifaddr *)ifa)) {
1211 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1214 in_ifdetach(struct ifnet *ifp)
1217 in_pcbpurgeif0(&V_ripcbinfo, ifp);
1218 in_pcbpurgeif0(&V_udbinfo, ifp);
1219 in_pcbpurgeif0(&V_ulitecbinfo, ifp);
1220 in_purgemaddrs(ifp);
1224 * Make sure all multicast deletions invoking if_ioctl() are
1225 * completed before returning. Else we risk accessing a freed
1226 * ifnet structure pointer.
1228 inm_release_wait(NULL);
1232 * Delete all IPv4 multicast address records, and associated link-layer
1233 * multicast address records, associated with ifp.
1234 * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1235 * XXX This should not race with ifma_protospec being set during
1236 * a new allocation, if it does, we have bigger problems.
1239 in_purgemaddrs(struct ifnet *ifp)
1241 struct in_multi_head purgeinms;
1242 struct in_multi *inm;
1243 struct ifmultiaddr *ifma, *next;
1245 SLIST_INIT(&purgeinms);
1246 IN_MULTI_LIST_LOCK();
1249 * Extract list of in_multi associated with the detaching ifp
1250 * which the PF_INET layer is about to release.
1251 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1252 * by code further down.
1256 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) {
1257 if (ifma->ifma_addr->sa_family != AF_INET ||
1258 ifma->ifma_protospec == NULL)
1260 inm = (struct in_multi *)ifma->ifma_protospec;
1261 inm_rele_locked(&purgeinms, inm);
1262 if (__predict_false(ifma_restart)) {
1263 ifma_restart = true;
1267 IF_ADDR_WUNLOCK(ifp);
1269 inm_release_list_deferred(&purgeinms);
1271 IN_MULTI_LIST_UNLOCK();
1275 struct llentry base;
1278 #define IN_LLTBL_DEFAULT_HSIZE 32
1279 #define IN_LLTBL_HASH(k, h) \
1280 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
1283 * Do actual deallocation of @lle.
1286 in_lltable_destroy_lle_unlocked(epoch_context_t ctx)
1288 struct llentry *lle;
1290 lle = __containerof(ctx, struct llentry, lle_epoch_ctx);
1291 LLE_LOCK_DESTROY(lle);
1292 LLE_REQ_DESTROY(lle);
1293 free(lle, M_LLTABLE);
1297 * Called by LLE_FREE_LOCKED when number of references
1301 in_lltable_destroy_lle(struct llentry *lle)
1305 NET_EPOCH_CALL(in_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
1308 static struct llentry *
1309 in_lltable_new(struct in_addr addr4, u_int flags)
1311 struct in_llentry *lle;
1313 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
1314 if (lle == NULL) /* NB: caller generates msg */
1318 * For IPv4 this will trigger "arpresolve" to generate
1321 lle->base.la_expire = time_uptime; /* mark expired */
1322 lle->base.r_l3addr.addr4 = addr4;
1323 lle->base.lle_refcnt = 1;
1324 lle->base.lle_free = in_lltable_destroy_lle;
1325 LLE_LOCK_INIT(&lle->base);
1326 LLE_REQ_INIT(&lle->base);
1327 callout_init(&lle->base.lle_timer, 1);
1329 return (&lle->base);
1332 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1333 ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
1336 in_lltable_match_prefix(const struct sockaddr *saddr,
1337 const struct sockaddr *smask, u_int flags, struct llentry *lle)
1339 struct in_addr addr, mask, lle_addr;
1341 addr = ((const struct sockaddr_in *)saddr)->sin_addr;
1342 mask = ((const struct sockaddr_in *)smask)->sin_addr;
1343 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
1345 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
1348 if (lle->la_flags & LLE_IFADDR) {
1350 * Delete LLE_IFADDR records IFF address & flag matches.
1351 * Note that addr is the interface address within prefix
1353 * Note also we should handle 'ifdown' cases without removing
1356 if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0)
1361 /* flags & LLE_STATIC means deleting both dynamic and static entries */
1362 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
1369 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
1371 size_t pkts_dropped;
1373 LLE_WLOCK_ASSERT(lle);
1374 KASSERT(llt != NULL, ("lltable is NULL"));
1376 /* Unlink entry from table if not already */
1377 if ((lle->la_flags & LLE_LINKED) != 0) {
1378 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
1379 lltable_unlink_entry(llt, lle);
1382 /* Drop hold queue */
1383 pkts_dropped = llentry_free(lle);
1384 ARPSTAT_ADD(dropped, pkts_dropped);
1388 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1390 struct nhop_object *nh;
1391 struct in_addr addr;
1393 KASSERT(l3addr->sa_family == AF_INET,
1394 ("sin_family %d", l3addr->sa_family));
1396 addr = ((const struct sockaddr_in *)l3addr)->sin_addr;
1398 nh = fib4_lookup(ifp->if_fib, addr, 0, NHR_NONE, 0);
1403 * If the gateway for an existing host route matches the target L3
1404 * address, which is a special route inserted by some implementation
1405 * such as MANET, and the interface is of the correct type, then
1406 * allow for ARP to proceed.
1408 if (nh->nh_flags & NHF_GATEWAY) {
1409 if (!(nh->nh_flags & NHF_HOST) || nh->nh_ifp->if_type != IFT_ETHER ||
1410 (nh->nh_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1411 memcmp(nh->gw_sa.sa_data, l3addr->sa_data,
1412 sizeof(in_addr_t)) != 0) {
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 ((nh->nh_ifp != ifp) && (nh->nh_flags & NHF_HOST) == 0) {
1425 struct in_ifaddr *ia = (struct in_ifaddr *)ifaof_ifpforaddr(l3addr, ifp);
1426 struct in_addr dst_addr, mask_addr;
1432 * ifaof_ifpforaddr() returns _best matching_ IFA.
1433 * It is possible that ifa prefix does not cover our address.
1434 * Explicitly verify and fail if that's the case.
1436 dst_addr = IA_SIN(ia)->sin_addr;
1437 mask_addr.s_addr = htonl(ia->ia_subnetmask);
1439 if (!IN_ARE_MASKED_ADDR_EQUAL(dst_addr, addr, mask_addr))
1446 static inline uint32_t
1447 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
1450 return (IN_LLTBL_HASH(dst.s_addr, hsize));
1454 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
1457 return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
1461 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
1463 struct sockaddr_in *sin;
1465 sin = (struct sockaddr_in *)sa;
1466 bzero(sin, sizeof(*sin));
1467 sin->sin_family = AF_INET;
1468 sin->sin_len = sizeof(*sin);
1469 sin->sin_addr = lle->r_l3addr.addr4;
1472 static inline struct llentry *
1473 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
1475 struct llentry *lle;
1476 struct llentries *lleh;
1479 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
1480 lleh = &llt->lle_head[hashidx];
1481 CK_LIST_FOREACH(lle, lleh, lle_next) {
1482 if (lle->la_flags & LLE_DELETED)
1484 if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
1492 in_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
1495 lle->la_flags |= LLE_DELETED;
1496 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1498 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1503 static struct llentry *
1504 in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1506 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1507 struct ifnet *ifp = llt->llt_ifp;
1508 struct llentry *lle;
1509 char linkhdr[LLE_MAX_LINKHDR];
1513 KASSERT(l3addr->sa_family == AF_INET,
1514 ("sin_family %d", l3addr->sa_family));
1517 * A route that covers the given address must have
1518 * been installed 1st because we are doing a resolution,
1521 if (!(flags & LLE_IFADDR) &&
1522 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1525 lle = in_lltable_new(sin->sin_addr, flags);
1527 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1530 lle->la_flags = flags;
1531 if (flags & LLE_STATIC)
1532 lle->r_flags |= RLLE_VALID;
1533 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
1534 linkhdrsize = LLE_MAX_LINKHDR;
1535 if (lltable_calc_llheader(ifp, AF_INET, IF_LLADDR(ifp),
1536 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
1537 NET_EPOCH_CALL(in_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
1540 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
1542 lle->la_flags |= LLE_STATIC;
1543 lle->r_flags |= (RLLE_VALID | RLLE_IFADDR);
1550 * Return NULL if not found or marked for deletion.
1551 * If found return lle read locked.
1553 static struct llentry *
1554 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1556 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1557 struct llentry *lle;
1559 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
1560 KASSERT(l3addr->sa_family == AF_INET,
1561 ("sin_family %d", l3addr->sa_family));
1562 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) !=
1563 (LLE_UNLOCKED | LLE_EXCLUSIVE),
1564 ("wrong lle request flags: %#x", flags));
1566 lle = in_lltable_find_dst(llt, sin->sin_addr);
1569 if (flags & LLE_UNLOCKED)
1572 if (flags & LLE_EXCLUSIVE)
1578 * If the afdata lock is not held, the LLE may have been unlinked while
1579 * we were blocked on the LLE lock. Check for this case.
1581 if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) {
1582 if (flags & LLE_EXCLUSIVE)
1592 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
1593 struct sysctl_req *wr)
1595 struct ifnet *ifp = llt->llt_ifp;
1598 struct rt_msghdr rtm;
1599 struct sockaddr_in sin;
1600 struct sockaddr_dl sdl;
1602 struct sockaddr_dl *sdl;
1605 bzero(&arpc, sizeof(arpc));
1606 /* skip deleted entries */
1607 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1609 /* Skip if jailed and not a valid IP of the prison. */
1610 lltable_fill_sa_entry(lle,(struct sockaddr *)&arpc.sin);
1611 if (prison_if(wr->td->td_ucred, (struct sockaddr *)&arpc.sin) != 0)
1614 * produce a msg made of:
1616 * struct sockaddr_in; (IPv4)
1617 * struct sockaddr_dl;
1619 arpc.rtm.rtm_msglen = sizeof(arpc);
1620 arpc.rtm.rtm_version = RTM_VERSION;
1621 arpc.rtm.rtm_type = RTM_GET;
1622 arpc.rtm.rtm_flags = RTF_UP;
1623 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1626 if (lle->la_flags & LLE_PUB)
1627 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1630 sdl->sdl_family = AF_LINK;
1631 sdl->sdl_len = sizeof(*sdl);
1632 sdl->sdl_index = ifp->if_index;
1633 sdl->sdl_type = ifp->if_type;
1634 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1635 sdl->sdl_alen = ifp->if_addrlen;
1636 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1639 bzero(LLADDR(sdl), ifp->if_addrlen);
1642 arpc.rtm.rtm_rmx.rmx_expire =
1643 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1644 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1645 if (lle->la_flags & LLE_STATIC)
1646 arpc.rtm.rtm_flags |= RTF_STATIC;
1647 if (lle->la_flags & LLE_IFADDR)
1648 arpc.rtm.rtm_flags |= RTF_PINNED;
1649 arpc.rtm.rtm_index = ifp->if_index;
1650 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1655 static struct lltable *
1656 in_lltattach(struct ifnet *ifp)
1658 struct lltable *llt;
1660 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
1661 llt->llt_af = AF_INET;
1664 llt->llt_lookup = in_lltable_lookup;
1665 llt->llt_alloc_entry = in_lltable_alloc;
1666 llt->llt_delete_entry = in_lltable_delete_entry;
1667 llt->llt_dump_entry = in_lltable_dump_entry;
1668 llt->llt_hash = in_lltable_hash;
1669 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
1670 llt->llt_free_entry = in_lltable_free_entry;
1671 llt->llt_match_prefix = in_lltable_match_prefix;
1672 llt->llt_mark_used = llentry_mark_used;
1679 in_domifattach(struct ifnet *ifp)
1681 struct in_ifinfo *ii;
1683 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1685 ii->ii_llt = in_lltattach(ifp);
1686 ii->ii_igmp = igmp_domifattach(ifp);
1692 in_domifdetach(struct ifnet *ifp, void *aux)
1694 struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1696 igmp_domifdetach(ifp);
1697 lltable_free(ii->ii_llt);