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 "opt_mpath.h"
40 #include <sys/param.h>
41 #include <sys/eventhandler.h>
42 #include <sys/systm.h>
43 #include <sys/sockio.h>
44 #include <sys/malloc.h>
46 #include <sys/socket.h>
48 #include <sys/kernel.h>
51 #include <sys/rmlock.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
57 #include <net/if_var.h>
58 #include <net/if_arp.h>
59 #include <net/if_dl.h>
60 #include <net/if_llatbl.h>
61 #include <net/if_types.h>
62 #include <net/route.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 *);
78 static void in_socktrim(struct sockaddr_in *);
79 static void in_purgemaddrs(struct ifnet *);
81 VNET_DEFINE_STATIC(int, nosameprefix);
82 #define V_nosameprefix VNET(nosameprefix)
83 SYSCTL_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_VNET | CTLFLAG_RW,
84 &VNET_NAME(nosameprefix), 0,
85 "Refuse to create same prefixes on different interfaces");
87 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
88 #define V_ripcbinfo VNET(ripcbinfo)
90 static struct sx in_control_sx;
91 SX_SYSINIT(in_control_sx, &in_control_sx, "in_control");
94 * Return 1 if an internet address is for a ``local'' host
95 * (one to which we have a connection).
98 in_localaddr(struct in_addr in)
100 struct rm_priotracker in_ifa_tracker;
101 u_long i = ntohl(in.s_addr);
102 struct in_ifaddr *ia;
104 IN_IFADDR_RLOCK(&in_ifa_tracker);
105 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
106 if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
107 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
111 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
116 * Return 1 if an internet address is for the local host and configured
117 * on one of its interfaces.
120 in_localip(struct in_addr in)
122 struct rm_priotracker in_ifa_tracker;
123 struct in_ifaddr *ia;
125 IN_IFADDR_RLOCK(&in_ifa_tracker);
126 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
127 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
128 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
132 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
137 * Return 1 if an internet address is configured on an interface.
140 in_ifhasaddr(struct ifnet *ifp, struct in_addr in)
142 struct epoch_tracker et;
144 struct in_ifaddr *ia;
147 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
148 if (ifa->ifa_addr->sa_family != AF_INET)
150 ia = (struct in_ifaddr *)ifa;
151 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_difaddr_ioctl(cmd, data, ifp, td);
248 sx_xunlock(&in_control_sx);
250 case OSIOCAIFADDR: /* 9.x compat */
252 sx_xlock(&in_control_sx);
253 error = in_aifaddr_ioctl(cmd, data, ifp, td);
254 sx_xunlock(&in_control_sx);
260 /* We no longer support that old commands. */
263 if (ifp->if_ioctl == NULL)
265 return ((*ifp->if_ioctl)(ifp, cmd, data));
268 if (addr->sin_addr.s_addr != INADDR_ANY &&
269 prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0)
270 return (EADDRNOTAVAIL);
273 * Find address for this interface, if it exists. If an
274 * address was specified, find that one instead of the
275 * first one on the interface, if possible.
278 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
279 if (ifa->ifa_addr->sa_family != AF_INET)
281 ia = (struct in_ifaddr *)ifa;
282 if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
286 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
287 if (ifa->ifa_addr->sa_family == AF_INET) {
288 ia = (struct in_ifaddr *)ifa;
289 if (prison_check_ip4(td->td_ucred,
290 &ia->ia_addr.sin_addr) == 0)
296 return (EADDRNOTAVAIL);
306 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
310 *addr = ia->ia_broadaddr;
314 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
318 *addr = ia->ia_dstaddr;
322 *addr = ia->ia_sockmask;
332 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
334 const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
335 const struct sockaddr_in *addr = &ifra->ifra_addr;
336 const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
337 const struct sockaddr_in *mask = &ifra->ifra_mask;
338 const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
339 const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
340 struct epoch_tracker et;
342 struct in_ifaddr *ia;
346 error = priv_check(td, PRIV_NET_ADDIFADDR);
351 * ifra_addr must be present and be of INET family.
352 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
354 if (addr->sin_len != sizeof(struct sockaddr_in) ||
355 addr->sin_family != AF_INET)
357 if (broadaddr->sin_len != 0 &&
358 (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
359 broadaddr->sin_family != AF_INET))
361 if (mask->sin_len != 0 &&
362 (mask->sin_len != sizeof(struct sockaddr_in) ||
363 mask->sin_family != AF_INET))
365 if ((ifp->if_flags & IFF_POINTOPOINT) &&
366 (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
367 dstaddr->sin_addr.s_addr == INADDR_ANY))
368 return (EDESTADDRREQ);
369 if (vhid > 0 && carp_attach_p == NULL)
370 return (EPROTONOSUPPORT);
373 * See whether address already exist.
378 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
379 struct in_ifaddr *it;
381 if (ifa->ifa_addr->sa_family != AF_INET)
384 it = (struct in_ifaddr *)ifa;
386 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
387 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0)
393 (void )in_difaddr_ioctl(cmd, data, ifp, td);
395 ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
396 ia = (struct in_ifaddr *)ifa;
397 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
398 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
399 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
400 callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock,
401 CALLOUT_RETURNUNLOCKED);
405 if (mask->sin_len != 0) {
406 ia->ia_sockmask = *mask;
407 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
409 in_addr_t i = ntohl(addr->sin_addr.s_addr);
412 * Be compatible with network classes, if netmask isn't
413 * supplied, guess it based on classes.
416 ia->ia_subnetmask = IN_CLASSA_NET;
417 else if (IN_CLASSB(i))
418 ia->ia_subnetmask = IN_CLASSB_NET;
420 ia->ia_subnetmask = IN_CLASSC_NET;
421 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
423 ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
424 in_socktrim(&ia->ia_sockmask);
426 if (ifp->if_flags & IFF_BROADCAST) {
427 if (broadaddr->sin_len != 0) {
428 ia->ia_broadaddr = *broadaddr;
429 } else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
430 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
431 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
432 ia->ia_broadaddr.sin_family = AF_INET;
434 ia->ia_broadaddr.sin_addr.s_addr =
435 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
436 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
437 ia->ia_broadaddr.sin_family = AF_INET;
441 if (ifp->if_flags & IFF_POINTOPOINT)
442 ia->ia_dstaddr = *dstaddr;
444 /* XXXGL: rtinit() needs this strange assignment. */
445 if (ifp->if_flags & IFF_LOOPBACK)
446 ia->ia_dstaddr = ia->ia_addr;
449 error = (*carp_attach_p)(&ia->ia_ifa, vhid);
454 /* if_addrhead is already referenced by ifa_alloc() */
456 CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
457 IF_ADDR_WUNLOCK(ifp);
459 ifa_ref(ifa); /* in_ifaddrhead */
461 CK_STAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
462 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
466 * Give the interface a chance to initialize
467 * if this is its first address,
468 * and to validate the address if necessary.
470 if (ifp->if_ioctl != NULL) {
471 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
477 * Add route for the network.
482 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
485 error = in_addprefix(ia, flags);
491 * Add a loopback route to self.
493 if (vhid == 0 && (ifp->if_flags & IFF_LOOPBACK) == 0 &&
494 ia->ia_addr.sin_addr.s_addr != INADDR_ANY &&
495 !((ifp->if_flags & IFF_POINTOPOINT) &&
496 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)) {
497 struct in_ifaddr *eia;
499 eia = in_localip_more(ia);
502 error = ifa_add_loopback_route((struct ifaddr *)ia,
503 (struct sockaddr *)&ia->ia_addr);
507 ifa_free(&eia->ia_ifa);
510 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
511 struct in_addr allhosts_addr;
512 struct in_ifinfo *ii;
514 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
515 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
517 error = in_joingroup(ifp, &allhosts_addr, NULL,
522 * Note: we don't need extra reference for ifa, since we called
523 * with sx lock held, and ifaddr can not be deleted in concurrent
526 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, ifa, IFADDR_EVENT_ADD);
532 (void )in_scrubprefix(ia, LLE_STATIC);
535 if (ia->ia_ifa.ifa_carp)
536 (*carp_detach_p)(&ia->ia_ifa, false);
539 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
540 IF_ADDR_WUNLOCK(ifp);
541 ifa_free(&ia->ia_ifa); /* if_addrhead */
544 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
545 LIST_REMOVE(ia, ia_hash);
547 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
553 in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
555 const struct ifreq *ifr = (struct ifreq *)data;
556 const struct sockaddr_in *addr = (const struct sockaddr_in *)
559 struct in_ifaddr *ia;
560 bool deleteAny, iaIsLast;
564 error = priv_check(td, PRIV_NET_DELIFADDR);
569 if (addr->sin_len != sizeof(struct sockaddr_in) ||
570 addr->sin_family != AF_INET)
578 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
579 struct in_ifaddr *it;
581 if (ifa->ifa_addr->sa_family != AF_INET)
584 it = (struct in_ifaddr *)ifa;
585 if (deleteAny && ia == NULL && (td == NULL ||
586 prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0))
589 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
590 (td == NULL || prison_check_ip4(td->td_ucred,
591 &addr->sin_addr) == 0))
599 IF_ADDR_WUNLOCK(ifp);
600 return (EADDRNOTAVAIL);
603 CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
604 IF_ADDR_WUNLOCK(ifp);
605 ifa_free(&ia->ia_ifa); /* if_addrhead */
608 CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
609 LIST_REMOVE(ia, ia_hash);
613 * in_scrubprefix() kills the interface route.
615 in_scrubprefix(ia, LLE_STATIC);
618 * in_ifadown gets rid of all the rest of
619 * the routes. This is not quite the right
620 * thing to do, but at least if we are running
621 * a routing process they will come back.
623 in_ifadown(&ia->ia_ifa, 1);
625 if (ia->ia_ifa.ifa_carp)
626 (*carp_detach_p)(&ia->ia_ifa, cmd == SIOCAIFADDR);
629 * If this is the last IPv4 address configured on this
630 * interface, leave the all-hosts group.
631 * No state-change report need be transmitted.
633 if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
634 struct in_ifinfo *ii;
636 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
637 if (ii->ii_allhosts) {
638 (void)in_leavegroup(ii->ii_allhosts, NULL);
639 ii->ii_allhosts = NULL;
644 if (callout_stop(&ia->ia_garp_timer) == 1) {
645 ifa_free(&ia->ia_ifa);
647 IF_ADDR_WUNLOCK(ifp);
649 EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
651 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
656 #define rtinitflags(x) \
657 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
661 * Check if we have a route for the given prefix already or add one accordingly.
664 in_addprefix(struct in_ifaddr *target, int flags)
666 struct rm_priotracker in_ifa_tracker;
667 struct in_ifaddr *ia;
668 struct in_addr prefix, mask, p, m;
671 if ((flags & RTF_HOST) != 0) {
672 prefix = target->ia_dstaddr.sin_addr;
675 prefix = target->ia_addr.sin_addr;
676 mask = target->ia_sockmask.sin_addr;
677 prefix.s_addr &= mask.s_addr;
680 IN_IFADDR_RLOCK(&in_ifa_tracker);
681 /* Look for an existing address with the same prefix, mask, and fib */
682 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
683 if (rtinitflags(ia)) {
684 p = ia->ia_dstaddr.sin_addr;
686 if (prefix.s_addr != p.s_addr)
689 p = ia->ia_addr.sin_addr;
690 m = ia->ia_sockmask.sin_addr;
691 p.s_addr &= m.s_addr;
693 if (prefix.s_addr != p.s_addr ||
694 mask.s_addr != m.s_addr)
697 if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
701 * If we got a matching prefix route inserted by other
702 * interface address, we are done here.
704 if (ia->ia_flags & IFA_ROUTE) {
706 if (ia->ia_addr.sin_addr.s_addr ==
707 target->ia_addr.sin_addr.s_addr) {
708 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
713 if (V_nosameprefix) {
714 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
719 fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS :
720 target->ia_ifp->if_fib;
721 rt_addrmsg(RTM_ADD, &target->ia_ifa, fibnum);
722 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
727 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
730 * No-one seem to have this prefix route, so we try to insert it.
732 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
734 target->ia_flags |= IFA_ROUTE;
739 * Removes either all lle entries for given @ia, or lle
740 * corresponding to @ia address.
743 in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags)
745 struct sockaddr_in addr, mask;
746 struct sockaddr *saddr, *smask;
749 saddr = (struct sockaddr *)&addr;
750 bzero(&addr, sizeof(addr));
751 addr.sin_len = sizeof(addr);
752 addr.sin_family = AF_INET;
753 smask = (struct sockaddr *)&mask;
754 bzero(&mask, sizeof(mask));
755 mask.sin_len = sizeof(mask);
756 mask.sin_family = AF_INET;
757 mask.sin_addr.s_addr = ia->ia_subnetmask;
763 * Remove all L2 entries matching given prefix.
764 * Convert address to host representation to avoid
765 * doing this on every callback. ia_subnetmask is already
766 * stored in host representation.
768 addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr);
769 lltable_prefix_free(AF_INET, saddr, smask, flags);
771 /* Remove interface address only */
772 addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr;
773 lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr);
778 * If there is no other address in the system that can serve a route to the
779 * same prefix, remove the route. Hand over the route to the new address
783 in_scrubprefix(struct in_ifaddr *target, u_int flags)
785 struct rm_priotracker in_ifa_tracker;
786 struct in_ifaddr *ia;
787 struct in_addr prefix, mask, p, m;
791 * Remove the loopback route to the interface address.
793 if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
794 !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
795 (flags & LLE_STATIC)) {
796 struct in_ifaddr *eia;
799 * XXXME: add fib-aware in_localip.
800 * We definitely don't want to switch between
801 * prefixes in different fibs.
803 eia = in_localip_more(target);
806 error = ifa_switch_loopback_route((struct ifaddr *)eia,
807 (struct sockaddr *)&target->ia_addr);
808 ifa_free(&eia->ia_ifa);
810 error = ifa_del_loopback_route((struct ifaddr *)target,
811 (struct sockaddr *)&target->ia_addr);
815 if (rtinitflags(target)) {
816 prefix = target->ia_dstaddr.sin_addr;
819 prefix = target->ia_addr.sin_addr;
820 mask = target->ia_sockmask.sin_addr;
821 prefix.s_addr &= mask.s_addr;
824 if ((target->ia_flags & IFA_ROUTE) == 0) {
827 fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS :
828 target->ia_ifp->if_fib;
829 rt_addrmsg(RTM_DELETE, &target->ia_ifa, fibnum);
832 * Removing address from !IFF_UP interface or
833 * prefix which exists on other interface (along with route).
834 * No entries should exist here except target addr.
835 * Given that, delete this entry only.
837 in_scrubprefixlle(target, 0, flags);
841 IN_IFADDR_RLOCK(&in_ifa_tracker);
842 CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
843 if (rtinitflags(ia)) {
844 p = ia->ia_dstaddr.sin_addr;
846 if (prefix.s_addr != p.s_addr)
849 p = ia->ia_addr.sin_addr;
850 m = ia->ia_sockmask.sin_addr;
851 p.s_addr &= m.s_addr;
853 if (prefix.s_addr != p.s_addr ||
854 mask.s_addr != m.s_addr)
858 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
862 * If we got a matching prefix address, move IFA_ROUTE and
863 * the route itself to it. Make sure that routing daemons
866 if ((ia->ia_flags & IFA_ROUTE) == 0) {
867 ifa_ref(&ia->ia_ifa);
868 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
869 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
870 rtinitflags(target));
872 target->ia_flags &= ~IFA_ROUTE;
874 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
876 /* Scrub all entries IFF interface is different */
877 in_scrubprefixlle(target, target->ia_ifp != ia->ia_ifp,
879 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
880 rtinitflags(ia) | RTF_UP);
882 ia->ia_flags |= IFA_ROUTE;
884 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
886 ifa_free(&ia->ia_ifa);
890 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
893 * remove all L2 entries on the given prefix
895 in_scrubprefixlle(target, 1, flags);
898 * As no-one seem to have this prefix, we can remove the route.
900 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
902 target->ia_flags &= ~IFA_ROUTE;
904 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
914 struct ifaddr *ifa, *nifa;
915 struct ifaliasreq ifr;
918 CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
919 /* Cannot lock here - lock recursion. */
920 /* NET_EPOCH_ENTER(et); */
921 CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
922 if (ifa->ifa_addr->sa_family != AF_INET)
926 * This is ugly but the only way for legacy IP to
927 * cleanly remove addresses and everything attached.
929 bzero(&ifr, sizeof(ifr));
930 ifr.ifra_addr = *ifa->ifa_addr;
931 if (ifa->ifa_dstaddr)
932 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
933 (void)in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr,
936 /* NET_EPOCH_EXIT(et); */
938 igmp_domifdetach(ifp);
944 in_ifaddr_broadcast(struct in_addr in, struct in_ifaddr *ia)
947 return ((in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
949 * Check for old-style (host 0) broadcast, but
950 * taking into account that RFC 3021 obsoletes it.
952 (ia->ia_subnetmask != IN_RFC3021_MASK &&
953 ntohl(in.s_addr) == ia->ia_subnet)) &&
955 * Check for an all one subnetmask. These
956 * only exist when an interface gets a secondary
959 ia->ia_subnetmask != (u_long)0xffffffff);
963 * Return 1 if the address might be a local broadcast address.
966 in_broadcast(struct in_addr in, struct ifnet *ifp)
968 struct epoch_tracker et;
972 if (in.s_addr == INADDR_BROADCAST ||
973 in.s_addr == INADDR_ANY)
975 if ((ifp->if_flags & IFF_BROADCAST) == 0)
979 * Look through the list of addresses for a match
980 * with a broadcast address.
983 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
984 if (ifa->ifa_addr->sa_family == AF_INET &&
985 in_ifaddr_broadcast(in, (struct in_ifaddr *)ifa)) {
994 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
997 in_ifdetach(struct ifnet *ifp)
1000 in_pcbpurgeif0(&V_ripcbinfo, ifp);
1001 in_pcbpurgeif0(&V_udbinfo, ifp);
1002 in_pcbpurgeif0(&V_ulitecbinfo, ifp);
1003 in_purgemaddrs(ifp);
1008 * Delete all IPv4 multicast address records, and associated link-layer
1009 * multicast address records, associated with ifp.
1010 * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1011 * XXX This should not race with ifma_protospec being set during
1012 * a new allocation, if it does, we have bigger problems.
1015 in_purgemaddrs(struct ifnet *ifp)
1017 struct in_multi_head purgeinms;
1018 struct in_multi *inm;
1019 struct ifmultiaddr *ifma, *next;
1021 SLIST_INIT(&purgeinms);
1022 IN_MULTI_LIST_LOCK();
1025 * Extract list of in_multi associated with the detaching ifp
1026 * which the PF_INET layer is about to release.
1027 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1028 * by code further down.
1032 CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) {
1033 if (ifma->ifma_addr->sa_family != AF_INET ||
1034 ifma->ifma_protospec == NULL)
1036 inm = (struct in_multi *)ifma->ifma_protospec;
1037 inm_rele_locked(&purgeinms, inm);
1038 if (__predict_false(ifma_restart)) {
1039 ifma_restart = true;
1043 IF_ADDR_WUNLOCK(ifp);
1045 inm_release_list_deferred(&purgeinms);
1047 IN_MULTI_LIST_UNLOCK();
1051 struct llentry base;
1054 #define IN_LLTBL_DEFAULT_HSIZE 32
1055 #define IN_LLTBL_HASH(k, h) \
1056 (((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
1059 * Do actual deallocation of @lle.
1062 in_lltable_destroy_lle_unlocked(epoch_context_t ctx)
1064 struct llentry *lle;
1066 lle = __containerof(ctx, struct llentry, lle_epoch_ctx);
1067 LLE_LOCK_DESTROY(lle);
1068 LLE_REQ_DESTROY(lle);
1069 free(lle, M_LLTABLE);
1073 * Called by the datapath to indicate that
1074 * the entry was used.
1077 in_lltable_mark_used(struct llentry *lle)
1081 lle->r_skip_req = 0;
1082 LLE_REQ_UNLOCK(lle);
1086 * Called by LLE_FREE_LOCKED when number of references
1090 in_lltable_destroy_lle(struct llentry *lle)
1094 epoch_call(net_epoch_preempt, &lle->lle_epoch_ctx, in_lltable_destroy_lle_unlocked);
1097 static struct llentry *
1098 in_lltable_new(struct in_addr addr4, u_int flags)
1100 struct in_llentry *lle;
1102 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
1103 if (lle == NULL) /* NB: caller generates msg */
1107 * For IPv4 this will trigger "arpresolve" to generate
1110 lle->base.la_expire = time_uptime; /* mark expired */
1111 lle->base.r_l3addr.addr4 = addr4;
1112 lle->base.lle_refcnt = 1;
1113 lle->base.lle_free = in_lltable_destroy_lle;
1114 LLE_LOCK_INIT(&lle->base);
1115 LLE_REQ_INIT(&lle->base);
1116 callout_init(&lle->base.lle_timer, 1);
1118 return (&lle->base);
1121 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1122 ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
1125 in_lltable_match_prefix(const struct sockaddr *saddr,
1126 const struct sockaddr *smask, u_int flags, struct llentry *lle)
1128 struct in_addr addr, mask, lle_addr;
1130 addr = ((const struct sockaddr_in *)saddr)->sin_addr;
1131 mask = ((const struct sockaddr_in *)smask)->sin_addr;
1132 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
1134 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
1137 if (lle->la_flags & LLE_IFADDR) {
1140 * Delete LLE_IFADDR records IFF address & flag matches.
1141 * Note that addr is the interface address within prefix
1143 * Note also we should handle 'ifdown' cases without removing
1146 if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0)
1151 /* flags & LLE_STATIC means deleting both dynamic and static entries */
1152 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
1159 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
1161 size_t pkts_dropped;
1163 LLE_WLOCK_ASSERT(lle);
1164 KASSERT(llt != NULL, ("lltable is NULL"));
1166 /* Unlink entry from table if not already */
1167 if ((lle->la_flags & LLE_LINKED) != 0) {
1168 IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
1169 lltable_unlink_entry(llt, lle);
1172 /* Drop hold queue */
1173 pkts_dropped = llentry_free(lle);
1174 ARPSTAT_ADD(dropped, pkts_dropped);
1178 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1180 struct rt_addrinfo info;
1181 struct sockaddr_in rt_key, rt_mask;
1182 struct sockaddr rt_gateway;
1185 KASSERT(l3addr->sa_family == AF_INET,
1186 ("sin_family %d", l3addr->sa_family));
1188 bzero(&rt_key, sizeof(rt_key));
1189 rt_key.sin_len = sizeof(rt_key);
1190 bzero(&rt_mask, sizeof(rt_mask));
1191 rt_mask.sin_len = sizeof(rt_mask);
1192 bzero(&rt_gateway, sizeof(rt_gateway));
1193 rt_gateway.sa_len = sizeof(rt_gateway);
1195 bzero(&info, sizeof(info));
1196 info.rti_info[RTAX_DST] = (struct sockaddr *)&rt_key;
1197 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&rt_mask;
1198 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&rt_gateway;
1200 if (rib_lookup_info(ifp->if_fib, l3addr, NHR_REF, 0, &info) != 0)
1203 rt_flags = info.rti_flags;
1206 * If the gateway for an existing host route matches the target L3
1207 * address, which is a special route inserted by some implementation
1208 * such as MANET, and the interface is of the correct type, then
1209 * allow for ARP to proceed.
1211 if (rt_flags & RTF_GATEWAY) {
1212 if (!(rt_flags & RTF_HOST) || !info.rti_ifp ||
1213 info.rti_ifp->if_type != IFT_ETHER ||
1214 (info.rti_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1215 memcmp(rt_gateway.sa_data, l3addr->sa_data,
1216 sizeof(in_addr_t)) != 0) {
1217 rib_free_info(&info);
1221 rib_free_info(&info);
1224 * Make sure that at least the destination address is covered
1225 * by the route. This is for handling the case where 2 or more
1226 * interfaces have the same prefix. An incoming packet arrives
1227 * on one interface and the corresponding outgoing packet leaves
1228 * another interface.
1230 if (!(rt_flags & RTF_HOST) && info.rti_ifp != ifp) {
1231 const char *sa, *mask, *addr, *lim;
1232 const struct sockaddr_in *l3sin;
1234 mask = (const char *)&rt_mask;
1236 * Just being extra cautious to avoid some custom
1237 * code getting into trouble.
1239 if ((info.rti_addrs & RTA_NETMASK) == 0)
1242 sa = (const char *)&rt_key;
1243 addr = (const char *)l3addr;
1244 l3sin = (const struct sockaddr_in *)l3addr;
1245 lim = addr + l3sin->sin_len;
1247 for ( ; addr < lim; sa++, mask++, addr++) {
1248 if ((*sa ^ *addr) & *mask) {
1250 char addrbuf[INET_ADDRSTRLEN];
1252 log(LOG_INFO, "IPv4 address: \"%s\" "
1253 "is not on the network\n",
1254 inet_ntoa_r(l3sin->sin_addr, addrbuf));
1264 static inline uint32_t
1265 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
1268 return (IN_LLTBL_HASH(dst.s_addr, hsize));
1272 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
1275 return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
1279 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
1281 struct sockaddr_in *sin;
1283 sin = (struct sockaddr_in *)sa;
1284 bzero(sin, sizeof(*sin));
1285 sin->sin_family = AF_INET;
1286 sin->sin_len = sizeof(*sin);
1287 sin->sin_addr = lle->r_l3addr.addr4;
1290 static inline struct llentry *
1291 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
1293 struct llentry *lle;
1294 struct llentries *lleh;
1297 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
1298 lleh = &llt->lle_head[hashidx];
1299 CK_LIST_FOREACH(lle, lleh, lle_next) {
1300 if (lle->la_flags & LLE_DELETED)
1302 if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
1310 in_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
1313 lle->la_flags |= LLE_DELETED;
1314 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1316 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1321 static struct llentry *
1322 in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1324 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1325 struct ifnet *ifp = llt->llt_ifp;
1326 struct llentry *lle;
1327 char linkhdr[LLE_MAX_LINKHDR];
1331 KASSERT(l3addr->sa_family == AF_INET,
1332 ("sin_family %d", l3addr->sa_family));
1335 * A route that covers the given address must have
1336 * been installed 1st because we are doing a resolution,
1339 if (!(flags & LLE_IFADDR) &&
1340 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1343 lle = in_lltable_new(sin->sin_addr, flags);
1345 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1348 lle->la_flags = flags;
1349 if (flags & LLE_STATIC)
1350 lle->r_flags |= RLLE_VALID;
1351 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
1352 linkhdrsize = LLE_MAX_LINKHDR;
1353 if (lltable_calc_llheader(ifp, AF_INET, IF_LLADDR(ifp),
1354 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
1355 epoch_call(net_epoch_preempt, &lle->lle_epoch_ctx, in_lltable_destroy_lle_unlocked);
1358 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
1360 lle->la_flags |= LLE_STATIC;
1361 lle->r_flags |= (RLLE_VALID | RLLE_IFADDR);
1368 * Return NULL if not found or marked for deletion.
1369 * If found return lle read locked.
1371 static struct llentry *
1372 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1374 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1375 struct llentry *lle;
1377 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
1378 KASSERT(l3addr->sa_family == AF_INET,
1379 ("sin_family %d", l3addr->sa_family));
1380 KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) !=
1381 (LLE_UNLOCKED | LLE_EXCLUSIVE),
1382 ("wrong lle request flags: %#x", flags));
1384 lle = in_lltable_find_dst(llt, sin->sin_addr);
1387 if (flags & LLE_UNLOCKED)
1390 if (flags & LLE_EXCLUSIVE)
1396 * If the afdata lock is not held, the LLE may have been unlinked while
1397 * we were blocked on the LLE lock. Check for this case.
1399 if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) {
1400 if (flags & LLE_EXCLUSIVE)
1410 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
1411 struct sysctl_req *wr)
1413 struct ifnet *ifp = llt->llt_ifp;
1416 struct rt_msghdr rtm;
1417 struct sockaddr_in sin;
1418 struct sockaddr_dl sdl;
1420 struct sockaddr_dl *sdl;
1423 bzero(&arpc, sizeof(arpc));
1424 /* skip deleted entries */
1425 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1427 /* Skip if jailed and not a valid IP of the prison. */
1428 lltable_fill_sa_entry(lle,(struct sockaddr *)&arpc.sin);
1429 if (prison_if(wr->td->td_ucred, (struct sockaddr *)&arpc.sin) != 0)
1432 * produce a msg made of:
1434 * struct sockaddr_in; (IPv4)
1435 * struct sockaddr_dl;
1437 arpc.rtm.rtm_msglen = sizeof(arpc);
1438 arpc.rtm.rtm_version = RTM_VERSION;
1439 arpc.rtm.rtm_type = RTM_GET;
1440 arpc.rtm.rtm_flags = RTF_UP;
1441 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1444 if (lle->la_flags & LLE_PUB)
1445 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1448 sdl->sdl_family = AF_LINK;
1449 sdl->sdl_len = sizeof(*sdl);
1450 sdl->sdl_index = ifp->if_index;
1451 sdl->sdl_type = ifp->if_type;
1452 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1453 sdl->sdl_alen = ifp->if_addrlen;
1454 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1457 bzero(LLADDR(sdl), ifp->if_addrlen);
1460 arpc.rtm.rtm_rmx.rmx_expire =
1461 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1462 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1463 if (lle->la_flags & LLE_STATIC)
1464 arpc.rtm.rtm_flags |= RTF_STATIC;
1465 if (lle->la_flags & LLE_IFADDR)
1466 arpc.rtm.rtm_flags |= RTF_PINNED;
1467 arpc.rtm.rtm_index = ifp->if_index;
1468 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1473 static struct lltable *
1474 in_lltattach(struct ifnet *ifp)
1476 struct lltable *llt;
1478 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
1479 llt->llt_af = AF_INET;
1482 llt->llt_lookup = in_lltable_lookup;
1483 llt->llt_alloc_entry = in_lltable_alloc;
1484 llt->llt_delete_entry = in_lltable_delete_entry;
1485 llt->llt_dump_entry = in_lltable_dump_entry;
1486 llt->llt_hash = in_lltable_hash;
1487 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
1488 llt->llt_free_entry = in_lltable_free_entry;
1489 llt->llt_match_prefix = in_lltable_match_prefix;
1490 llt->llt_mark_used = in_lltable_mark_used;
1497 in_domifattach(struct ifnet *ifp)
1499 struct in_ifinfo *ii;
1501 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1503 ii->ii_llt = in_lltattach(ifp);
1504 ii->ii_igmp = igmp_domifattach(ifp);
1510 in_domifdetach(struct ifnet *ifp, void *aux)
1512 struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1514 igmp_domifdetach(ifp);
1515 lltable_free(ii->ii_llt);