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 static VNET_DEFINE(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 TAILQ_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)
143 struct in_ifaddr *ia;
146 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
147 if (ifa->ifa_addr->sa_family != AF_INET)
149 ia = (struct in_ifaddr *)ifa;
150 if (ia->ia_addr.sin_addr.s_addr == in.s_addr) {
151 IF_ADDR_RUNLOCK(ifp);
155 IF_ADDR_RUNLOCK(ifp);
161 * Return a reference to the interface address which is different to
162 * the supplied one but with same IP address value.
164 static struct in_ifaddr *
165 in_localip_more(struct in_ifaddr *ia)
167 struct rm_priotracker in_ifa_tracker;
168 in_addr_t in = IA_SIN(ia)->sin_addr.s_addr;
169 struct in_ifaddr *it;
171 IN_IFADDR_RLOCK(&in_ifa_tracker);
172 LIST_FOREACH(it, INADDR_HASH(in), ia_hash) {
173 if (it != ia && IA_SIN(it)->sin_addr.s_addr == in) {
174 ifa_ref(&it->ia_ifa);
175 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
179 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
185 * Determine whether an IP address is in a reserved set of addresses
186 * that may not be forwarded, or whether datagrams to that destination
190 in_canforward(struct in_addr in)
192 u_long i = ntohl(in.s_addr);
195 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
198 net = i & IN_CLASSA_NET;
199 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
206 * Trim a mask in a sockaddr
209 in_socktrim(struct sockaddr_in *ap)
211 char *cplim = (char *) &ap->sin_addr;
212 char *cp = (char *) (&ap->sin_addr + 1);
215 while (--cp >= cplim)
217 (ap)->sin_len = cp - (char *) (ap) + 1;
223 * Generic internet control operations (ioctl's).
226 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
229 struct ifreq *ifr = (struct ifreq *)data;
230 struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
232 struct in_ifaddr *ia;
236 return (EADDRNOTAVAIL);
239 * Filter out 4 ioctls we implement directly. Forward the rest
240 * to specific functions and ifp->if_ioctl().
249 sx_xlock(&in_control_sx);
250 error = in_difaddr_ioctl(cmd, data, ifp, td);
251 sx_xunlock(&in_control_sx);
253 case OSIOCAIFADDR: /* 9.x compat */
255 sx_xlock(&in_control_sx);
256 error = in_aifaddr_ioctl(cmd, data, ifp, td);
257 sx_xunlock(&in_control_sx);
263 /* We no longer support that old commands. */
266 if (ifp->if_ioctl == NULL)
268 return ((*ifp->if_ioctl)(ifp, cmd, data));
271 if (addr->sin_addr.s_addr != INADDR_ANY &&
272 prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0)
273 return (EADDRNOTAVAIL);
276 * Find address for this interface, if it exists. If an
277 * address was specified, find that one instead of the
278 * first one on the interface, if possible.
281 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
282 if (ifa->ifa_addr->sa_family != AF_INET)
284 ia = (struct in_ifaddr *)ifa;
285 if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
289 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
290 if (ifa->ifa_addr->sa_family == AF_INET) {
291 ia = (struct in_ifaddr *)ifa;
292 if (prison_check_ip4(td->td_ucred,
293 &ia->ia_addr.sin_addr) == 0)
298 IF_ADDR_RUNLOCK(ifp);
299 return (EADDRNOTAVAIL);
309 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
313 *addr = ia->ia_broadaddr;
317 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
321 *addr = ia->ia_dstaddr;
325 *addr = ia->ia_sockmask;
329 IF_ADDR_RUNLOCK(ifp);
335 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
337 const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
338 const struct sockaddr_in *addr = &ifra->ifra_addr;
339 const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
340 const struct sockaddr_in *mask = &ifra->ifra_mask;
341 const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
342 const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
344 struct in_ifaddr *ia;
348 error = priv_check(td, PRIV_NET_ADDIFADDR);
353 * ifra_addr must be present and be of INET family.
354 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
356 if (addr->sin_len != sizeof(struct sockaddr_in) ||
357 addr->sin_family != AF_INET)
359 if (broadaddr->sin_len != 0 &&
360 (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
361 broadaddr->sin_family != AF_INET))
363 if (mask->sin_len != 0 &&
364 (mask->sin_len != sizeof(struct sockaddr_in) ||
365 mask->sin_family != AF_INET))
367 if ((ifp->if_flags & IFF_POINTOPOINT) &&
368 (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
369 dstaddr->sin_addr.s_addr == INADDR_ANY))
370 return (EDESTADDRREQ);
371 if (vhid > 0 && carp_attach_p == NULL)
372 return (EPROTONOSUPPORT);
375 * See whether address already exist.
380 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
381 struct in_ifaddr *it;
383 if (ifa->ifa_addr->sa_family != AF_INET)
386 it = (struct in_ifaddr *)ifa;
388 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
389 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0)
392 IF_ADDR_RUNLOCK(ifp);
395 (void )in_difaddr_ioctl(cmd, data, ifp, td);
397 ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
398 ia = (struct in_ifaddr *)ifa;
399 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
400 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
401 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
402 callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock,
403 CALLOUT_RETURNUNLOCKED);
407 if (mask->sin_len != 0) {
408 ia->ia_sockmask = *mask;
409 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
411 in_addr_t i = ntohl(addr->sin_addr.s_addr);
414 * Be compatible with network classes, if netmask isn't
415 * supplied, guess it based on classes.
418 ia->ia_subnetmask = IN_CLASSA_NET;
419 else if (IN_CLASSB(i))
420 ia->ia_subnetmask = IN_CLASSB_NET;
422 ia->ia_subnetmask = IN_CLASSC_NET;
423 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
425 ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
426 in_socktrim(&ia->ia_sockmask);
428 if (ifp->if_flags & IFF_BROADCAST) {
429 if (broadaddr->sin_len != 0) {
430 ia->ia_broadaddr = *broadaddr;
431 } else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
432 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
433 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
434 ia->ia_broadaddr.sin_family = AF_INET;
436 ia->ia_broadaddr.sin_addr.s_addr =
437 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
438 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
439 ia->ia_broadaddr.sin_family = AF_INET;
443 if (ifp->if_flags & IFF_POINTOPOINT)
444 ia->ia_dstaddr = *dstaddr;
446 /* XXXGL: rtinit() needs this strange assignment. */
447 if (ifp->if_flags & IFF_LOOPBACK)
448 ia->ia_dstaddr = ia->ia_addr;
451 error = (*carp_attach_p)(&ia->ia_ifa, vhid);
456 /* if_addrhead is already referenced by ifa_alloc() */
458 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
459 IF_ADDR_WUNLOCK(ifp);
461 ifa_ref(ifa); /* in_ifaddrhead */
463 TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
464 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
468 * Give the interface a chance to initialize
469 * if this is its first address,
470 * and to validate the address if necessary.
472 if (ifp->if_ioctl != NULL) {
473 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
479 * Add route for the network.
484 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
487 error = in_addprefix(ia, flags);
493 * Add a loopback route to self.
495 if (vhid == 0 && (ifp->if_flags & IFF_LOOPBACK) == 0 &&
496 ia->ia_addr.sin_addr.s_addr != INADDR_ANY &&
497 !((ifp->if_flags & IFF_POINTOPOINT) &&
498 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)) {
499 struct in_ifaddr *eia;
501 eia = in_localip_more(ia);
504 error = ifa_add_loopback_route((struct ifaddr *)ia,
505 (struct sockaddr *)&ia->ia_addr);
509 ifa_free(&eia->ia_ifa);
512 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
513 struct in_addr allhosts_addr;
514 struct in_ifinfo *ii;
516 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
517 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
519 error = in_joingroup(ifp, &allhosts_addr, NULL,
523 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
529 (void )in_scrubprefix(ia, LLE_STATIC);
532 if (ia->ia_ifa.ifa_carp)
533 (*carp_detach_p)(&ia->ia_ifa, false);
536 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
537 IF_ADDR_WUNLOCK(ifp);
538 ifa_free(&ia->ia_ifa); /* if_addrhead */
541 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
542 LIST_REMOVE(ia, ia_hash);
544 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
550 in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
552 const struct ifreq *ifr = (struct ifreq *)data;
553 const struct sockaddr_in *addr = (const struct sockaddr_in *)
556 struct in_ifaddr *ia;
557 bool deleteAny, iaIsLast;
561 error = priv_check(td, PRIV_NET_DELIFADDR);
566 if (addr->sin_len != sizeof(struct sockaddr_in) ||
567 addr->sin_family != AF_INET)
575 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
576 struct in_ifaddr *it;
578 if (ifa->ifa_addr->sa_family != AF_INET)
581 it = (struct in_ifaddr *)ifa;
582 if (deleteAny && ia == NULL && (td == NULL ||
583 prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0))
586 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
587 (td == NULL || prison_check_ip4(td->td_ucred,
588 &addr->sin_addr) == 0))
596 IF_ADDR_WUNLOCK(ifp);
597 return (EADDRNOTAVAIL);
600 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
601 IF_ADDR_WUNLOCK(ifp);
602 ifa_free(&ia->ia_ifa); /* if_addrhead */
605 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
606 LIST_REMOVE(ia, ia_hash);
610 * in_scrubprefix() kills the interface route.
612 in_scrubprefix(ia, LLE_STATIC);
615 * in_ifadown gets rid of all the rest of
616 * the routes. This is not quite the right
617 * thing to do, but at least if we are running
618 * a routing process they will come back.
620 in_ifadown(&ia->ia_ifa, 1);
622 if (ia->ia_ifa.ifa_carp)
623 (*carp_detach_p)(&ia->ia_ifa,
624 (cmd == SIOCDIFADDR) ? false : true);
627 * If this is the last IPv4 address configured on this
628 * interface, leave the all-hosts group.
629 * No state-change report need be transmitted.
631 if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
632 struct in_ifinfo *ii;
634 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
636 if (ii->ii_allhosts) {
637 (void)in_leavegroup_locked(ii->ii_allhosts, NULL);
638 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, ifp);
650 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
655 #define rtinitflags(x) \
656 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
660 * Check if we have a route for the given prefix already or add one accordingly.
663 in_addprefix(struct in_ifaddr *target, int flags)
665 struct rm_priotracker in_ifa_tracker;
666 struct in_ifaddr *ia;
667 struct in_addr prefix, mask, p, m;
670 if ((flags & RTF_HOST) != 0) {
671 prefix = target->ia_dstaddr.sin_addr;
674 prefix = target->ia_addr.sin_addr;
675 mask = target->ia_sockmask.sin_addr;
676 prefix.s_addr &= mask.s_addr;
679 IN_IFADDR_RLOCK(&in_ifa_tracker);
680 /* Look for an existing address with the same prefix, mask, and fib */
681 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
682 if (rtinitflags(ia)) {
683 p = ia->ia_dstaddr.sin_addr;
685 if (prefix.s_addr != p.s_addr)
688 p = ia->ia_addr.sin_addr;
689 m = ia->ia_sockmask.sin_addr;
690 p.s_addr &= m.s_addr;
692 if (prefix.s_addr != p.s_addr ||
693 mask.s_addr != m.s_addr)
696 if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
700 * If we got a matching prefix route inserted by other
701 * interface address, we are done here.
703 if (ia->ia_flags & IFA_ROUTE) {
705 if (ia->ia_addr.sin_addr.s_addr ==
706 target->ia_addr.sin_addr.s_addr) {
707 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
712 if (V_nosameprefix) {
713 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
718 fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS :
719 target->ia_ifp->if_fib;
720 rt_addrmsg(RTM_ADD, &target->ia_ifa, fibnum);
721 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
726 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
729 * No-one seem to have this prefix route, so we try to insert it.
731 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
733 target->ia_flags |= IFA_ROUTE;
738 * Removes either all lle entries for given @ia, or lle
739 * corresponding to @ia address.
742 in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags)
744 struct sockaddr_in addr, mask;
745 struct sockaddr *saddr, *smask;
748 saddr = (struct sockaddr *)&addr;
749 bzero(&addr, sizeof(addr));
750 addr.sin_len = sizeof(addr);
751 addr.sin_family = AF_INET;
752 smask = (struct sockaddr *)&mask;
753 bzero(&mask, sizeof(mask));
754 mask.sin_len = sizeof(mask);
755 mask.sin_family = AF_INET;
756 mask.sin_addr.s_addr = ia->ia_subnetmask;
762 * Remove all L2 entries matching given prefix.
763 * Convert address to host representation to avoid
764 * doing this on every callback. ia_subnetmask is already
765 * stored in host representation.
767 addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr);
768 lltable_prefix_free(AF_INET, saddr, smask, flags);
770 /* Remove interface address only */
771 addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr;
772 lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr);
777 * If there is no other address in the system that can serve a route to the
778 * same prefix, remove the route. Hand over the route to the new address
782 in_scrubprefix(struct in_ifaddr *target, u_int flags)
784 struct rm_priotracker in_ifa_tracker;
785 struct in_ifaddr *ia;
786 struct in_addr prefix, mask, p, m;
790 * Remove the loopback route to the interface address.
792 if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
793 !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
794 (flags & LLE_STATIC)) {
795 struct in_ifaddr *eia;
798 * XXXME: add fib-aware in_localip.
799 * We definitely don't want to switch between
800 * prefixes in different fibs.
802 eia = in_localip_more(target);
805 error = ifa_switch_loopback_route((struct ifaddr *)eia,
806 (struct sockaddr *)&target->ia_addr);
807 ifa_free(&eia->ia_ifa);
809 error = ifa_del_loopback_route((struct ifaddr *)target,
810 (struct sockaddr *)&target->ia_addr);
814 if (rtinitflags(target)) {
815 prefix = target->ia_dstaddr.sin_addr;
818 prefix = target->ia_addr.sin_addr;
819 mask = target->ia_sockmask.sin_addr;
820 prefix.s_addr &= mask.s_addr;
823 if ((target->ia_flags & IFA_ROUTE) == 0) {
826 fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS :
827 target->ia_ifp->if_fib;
828 rt_addrmsg(RTM_DELETE, &target->ia_ifa, fibnum);
831 * Removing address from !IFF_UP interface or
832 * prefix which exists on other interface (along with route).
833 * No entries should exist here except target addr.
834 * Given that, delete this entry only.
836 in_scrubprefixlle(target, 0, flags);
840 IN_IFADDR_RLOCK(&in_ifa_tracker);
841 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
842 if (rtinitflags(ia)) {
843 p = ia->ia_dstaddr.sin_addr;
845 if (prefix.s_addr != p.s_addr)
848 p = ia->ia_addr.sin_addr;
849 m = ia->ia_sockmask.sin_addr;
850 p.s_addr &= m.s_addr;
852 if (prefix.s_addr != p.s_addr ||
853 mask.s_addr != m.s_addr)
857 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
861 * If we got a matching prefix address, move IFA_ROUTE and
862 * the route itself to it. Make sure that routing daemons
865 if ((ia->ia_flags & IFA_ROUTE) == 0) {
866 ifa_ref(&ia->ia_ifa);
867 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
868 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
869 rtinitflags(target));
871 target->ia_flags &= ~IFA_ROUTE;
873 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
875 /* Scrub all entries IFF interface is different */
876 in_scrubprefixlle(target, target->ia_ifp != ia->ia_ifp,
878 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
879 rtinitflags(ia) | RTF_UP);
881 ia->ia_flags |= IFA_ROUTE;
883 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
885 ifa_free(&ia->ia_ifa);
889 IN_IFADDR_RUNLOCK(&in_ifa_tracker);
892 * remove all L2 entries on the given prefix
894 in_scrubprefixlle(target, 1, flags);
897 * As no-one seem to have this prefix, we can remove the route.
899 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
901 target->ia_flags &= ~IFA_ROUTE;
903 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
913 struct ifaddr *ifa, *nifa;
914 struct ifaliasreq ifr;
917 TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
918 /* Cannot lock here - lock recursion. */
919 /* IF_ADDR_RLOCK(ifp); */
920 TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
921 if (ifa->ifa_addr->sa_family != AF_INET)
925 * This is ugly but the only way for legacy IP to
926 * cleanly remove addresses and everything attached.
928 bzero(&ifr, sizeof(ifr));
929 ifr.ifra_addr = *ifa->ifa_addr;
930 if (ifa->ifa_dstaddr)
931 ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
932 (void)in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr,
935 /* IF_ADDR_RUNLOCK(ifp); */
937 igmp_domifdetach(ifp);
943 in_ifaddr_broadcast(struct in_addr in, struct in_ifaddr *ia)
946 return ((in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
948 * Check for old-style (host 0) broadcast, but
949 * taking into account that RFC 3021 obsoletes it.
951 (ia->ia_subnetmask != IN_RFC3021_MASK &&
952 ntohl(in.s_addr) == ia->ia_subnet)) &&
954 * Check for an all one subnetmask. These
955 * only exist when an interface gets a secondary
958 ia->ia_subnetmask != (u_long)0xffffffff);
962 * Return 1 if the address might be a local broadcast address.
965 in_broadcast(struct in_addr in, struct ifnet *ifp)
970 if (in.s_addr == INADDR_BROADCAST ||
971 in.s_addr == INADDR_ANY)
973 if ((ifp->if_flags & IFF_BROADCAST) == 0)
977 * Look through the list of addresses for a match
978 * with a broadcast address.
981 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
982 if (ifa->ifa_addr->sa_family == AF_INET &&
983 in_ifaddr_broadcast(in, (struct in_ifaddr *)ifa)) {
987 IF_ADDR_RUNLOCK(ifp);
992 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
995 in_ifdetach(struct ifnet *ifp)
998 in_pcbpurgeif0(&V_ripcbinfo, ifp);
999 in_pcbpurgeif0(&V_udbinfo, ifp);
1000 in_pcbpurgeif0(&V_ulitecbinfo, ifp);
1001 in_purgemaddrs(ifp);
1005 * Delete all IPv4 multicast address records, and associated link-layer
1006 * multicast address records, associated with ifp.
1007 * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1008 * XXX This should not race with ifma_protospec being set during
1009 * a new allocation, if it does, we have bigger problems.
1012 in_purgemaddrs(struct ifnet *ifp)
1014 LIST_HEAD(,in_multi) purgeinms;
1015 struct in_multi *inm, *tinm;
1016 struct ifmultiaddr *ifma;
1018 LIST_INIT(&purgeinms);
1022 * Extract list of in_multi associated with the detaching ifp
1023 * which the PF_INET layer is about to release.
1024 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1025 * by code further down.
1028 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1029 if (ifma->ifma_addr->sa_family != AF_INET ||
1030 ifma->ifma_protospec == NULL)
1033 KASSERT(ifma->ifma_protospec != NULL,
1034 ("%s: ifma_protospec is NULL", __func__));
1036 inm = (struct in_multi *)ifma->ifma_protospec;
1037 LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
1039 IF_ADDR_RUNLOCK(ifp);
1041 LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
1042 LIST_REMOVE(inm, inm_link);
1043 inm_release_locked(inm);
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(struct llentry *lle)
1065 LLE_LOCK_DESTROY(lle);
1066 LLE_REQ_DESTROY(lle);
1067 free(lle, M_LLTABLE);
1071 * Called by LLE_FREE_LOCKED when number of references
1075 in_lltable_destroy_lle(struct llentry *lle)
1079 in_lltable_destroy_lle_unlocked(lle);
1082 static struct llentry *
1083 in_lltable_new(struct in_addr addr4, u_int flags)
1085 struct in_llentry *lle;
1087 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
1088 if (lle == NULL) /* NB: caller generates msg */
1092 * For IPv4 this will trigger "arpresolve" to generate
1095 lle->base.la_expire = time_uptime; /* mark expired */
1096 lle->base.r_l3addr.addr4 = addr4;
1097 lle->base.lle_refcnt = 1;
1098 lle->base.lle_free = in_lltable_destroy_lle;
1099 LLE_LOCK_INIT(&lle->base);
1100 LLE_REQ_INIT(&lle->base);
1101 callout_init(&lle->base.lle_timer, 1);
1103 return (&lle->base);
1106 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1107 ((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
1110 in_lltable_match_prefix(const struct sockaddr *saddr,
1111 const struct sockaddr *smask, u_int flags, struct llentry *lle)
1113 struct in_addr addr, mask, lle_addr;
1115 addr = ((const struct sockaddr_in *)saddr)->sin_addr;
1116 mask = ((const struct sockaddr_in *)smask)->sin_addr;
1117 lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
1119 if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
1122 if (lle->la_flags & LLE_IFADDR) {
1125 * Delete LLE_IFADDR records IFF address & flag matches.
1126 * Note that addr is the interface address within prefix
1128 * Note also we should handle 'ifdown' cases without removing
1131 if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0)
1136 /* flags & LLE_STATIC means deleting both dynamic and static entries */
1137 if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
1144 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
1147 size_t pkts_dropped;
1149 LLE_WLOCK_ASSERT(lle);
1150 KASSERT(llt != NULL, ("lltable is NULL"));
1152 /* Unlink entry from table if not already */
1153 if ((lle->la_flags & LLE_LINKED) != 0) {
1155 IF_AFDATA_WLOCK_ASSERT(ifp);
1156 lltable_unlink_entry(llt, lle);
1160 if (callout_stop(&lle->lle_timer) > 0)
1163 /* Drop hold queue */
1164 pkts_dropped = llentry_free(lle);
1165 ARPSTAT_ADD(dropped, pkts_dropped);
1169 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1171 struct rt_addrinfo info;
1172 struct sockaddr_in rt_key, rt_mask;
1173 struct sockaddr rt_gateway;
1176 KASSERT(l3addr->sa_family == AF_INET,
1177 ("sin_family %d", l3addr->sa_family));
1179 bzero(&rt_key, sizeof(rt_key));
1180 rt_key.sin_len = sizeof(rt_key);
1181 bzero(&rt_mask, sizeof(rt_mask));
1182 rt_mask.sin_len = sizeof(rt_mask);
1183 bzero(&rt_gateway, sizeof(rt_gateway));
1184 rt_gateway.sa_len = sizeof(rt_gateway);
1186 bzero(&info, sizeof(info));
1187 info.rti_info[RTAX_DST] = (struct sockaddr *)&rt_key;
1188 info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&rt_mask;
1189 info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&rt_gateway;
1191 if (rib_lookup_info(ifp->if_fib, l3addr, NHR_REF, 0, &info) != 0)
1194 rt_flags = info.rti_flags;
1197 * If the gateway for an existing host route matches the target L3
1198 * address, which is a special route inserted by some implementation
1199 * such as MANET, and the interface is of the correct type, then
1200 * allow for ARP to proceed.
1202 if (rt_flags & RTF_GATEWAY) {
1203 if (!(rt_flags & RTF_HOST) || !info.rti_ifp ||
1204 info.rti_ifp->if_type != IFT_ETHER ||
1205 (info.rti_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1206 memcmp(rt_gateway.sa_data, l3addr->sa_data,
1207 sizeof(in_addr_t)) != 0) {
1208 rib_free_info(&info);
1212 rib_free_info(&info);
1215 * Make sure that at least the destination address is covered
1216 * by the route. This is for handling the case where 2 or more
1217 * interfaces have the same prefix. An incoming packet arrives
1218 * on one interface and the corresponding outgoing packet leaves
1219 * another interface.
1221 if (!(rt_flags & RTF_HOST) && info.rti_ifp != ifp) {
1222 const char *sa, *mask, *addr, *lim;
1223 const struct sockaddr_in *l3sin;
1225 mask = (const char *)&rt_mask;
1227 * Just being extra cautious to avoid some custom
1228 * code getting into trouble.
1230 if ((info.rti_addrs & RTA_NETMASK) == 0)
1233 sa = (const char *)&rt_key;
1234 addr = (const char *)l3addr;
1235 l3sin = (const struct sockaddr_in *)l3addr;
1236 lim = addr + l3sin->sin_len;
1238 for ( ; addr < lim; sa++, mask++, addr++) {
1239 if ((*sa ^ *addr) & *mask) {
1241 char addrbuf[INET_ADDRSTRLEN];
1243 log(LOG_INFO, "IPv4 address: \"%s\" "
1244 "is not on the network\n",
1245 inet_ntoa_r(l3sin->sin_addr, addrbuf));
1255 static inline uint32_t
1256 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
1259 return (IN_LLTBL_HASH(dst.s_addr, hsize));
1263 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
1266 return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
1270 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
1272 struct sockaddr_in *sin;
1274 sin = (struct sockaddr_in *)sa;
1275 bzero(sin, sizeof(*sin));
1276 sin->sin_family = AF_INET;
1277 sin->sin_len = sizeof(*sin);
1278 sin->sin_addr = lle->r_l3addr.addr4;
1281 static inline struct llentry *
1282 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
1284 struct llentry *lle;
1285 struct llentries *lleh;
1288 hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
1289 lleh = &llt->lle_head[hashidx];
1290 LIST_FOREACH(lle, lleh, lle_next) {
1291 if (lle->la_flags & LLE_DELETED)
1293 if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
1301 in_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
1304 lle->la_flags |= LLE_DELETED;
1305 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1307 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1312 static struct llentry *
1313 in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1315 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1316 struct ifnet *ifp = llt->llt_ifp;
1317 struct llentry *lle;
1318 char linkhdr[LLE_MAX_LINKHDR];
1322 KASSERT(l3addr->sa_family == AF_INET,
1323 ("sin_family %d", l3addr->sa_family));
1326 * A route that covers the given address must have
1327 * been installed 1st because we are doing a resolution,
1330 if (!(flags & LLE_IFADDR) &&
1331 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1334 lle = in_lltable_new(sin->sin_addr, flags);
1336 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1339 lle->la_flags = flags;
1340 if (flags & LLE_STATIC)
1341 lle->r_flags |= RLLE_VALID;
1342 if ((flags & LLE_IFADDR) == LLE_IFADDR) {
1343 linkhdrsize = LLE_MAX_LINKHDR;
1344 if (lltable_calc_llheader(ifp, AF_INET, IF_LLADDR(ifp),
1345 linkhdr, &linkhdrsize, &lladdr_off) != 0) {
1346 in_lltable_destroy_lle_unlocked(lle);
1349 lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
1351 lle->la_flags |= LLE_STATIC;
1352 lle->r_flags |= (RLLE_VALID | RLLE_IFADDR);
1359 * Return NULL if not found or marked for deletion.
1360 * If found return lle read locked.
1362 static struct llentry *
1363 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1365 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1366 struct llentry *lle;
1368 IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
1369 KASSERT(l3addr->sa_family == AF_INET,
1370 ("sin_family %d", l3addr->sa_family));
1371 lle = in_lltable_find_dst(llt, sin->sin_addr);
1376 KASSERT((flags & (LLE_UNLOCKED|LLE_EXCLUSIVE)) !=
1377 (LLE_UNLOCKED|LLE_EXCLUSIVE),("wrong lle request flags: 0x%X",
1380 if (flags & LLE_UNLOCKED)
1383 if (flags & LLE_EXCLUSIVE)
1392 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
1393 struct sysctl_req *wr)
1395 struct ifnet *ifp = llt->llt_ifp;
1398 struct rt_msghdr rtm;
1399 struct sockaddr_in sin;
1400 struct sockaddr_dl sdl;
1402 struct sockaddr_dl *sdl;
1405 bzero(&arpc, sizeof(arpc));
1406 /* skip deleted entries */
1407 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1409 /* Skip if jailed and not a valid IP of the prison. */
1410 lltable_fill_sa_entry(lle,(struct sockaddr *)&arpc.sin);
1411 if (prison_if(wr->td->td_ucred,
1412 (struct sockaddr *)&arpc.sin) != 0)
1415 * produce a msg made of:
1417 * struct sockaddr_in; (IPv4)
1418 * struct sockaddr_dl;
1420 arpc.rtm.rtm_msglen = sizeof(arpc);
1421 arpc.rtm.rtm_version = RTM_VERSION;
1422 arpc.rtm.rtm_type = RTM_GET;
1423 arpc.rtm.rtm_flags = RTF_UP;
1424 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1427 if (lle->la_flags & LLE_PUB)
1428 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1431 sdl->sdl_family = AF_LINK;
1432 sdl->sdl_len = sizeof(*sdl);
1433 sdl->sdl_index = ifp->if_index;
1434 sdl->sdl_type = ifp->if_type;
1435 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1436 sdl->sdl_alen = ifp->if_addrlen;
1437 bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1440 bzero(LLADDR(sdl), ifp->if_addrlen);
1443 arpc.rtm.rtm_rmx.rmx_expire =
1444 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1445 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1446 if (lle->la_flags & LLE_STATIC)
1447 arpc.rtm.rtm_flags |= RTF_STATIC;
1448 if (lle->la_flags & LLE_IFADDR)
1449 arpc.rtm.rtm_flags |= RTF_PINNED;
1450 arpc.rtm.rtm_index = ifp->if_index;
1451 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1456 static struct lltable *
1457 in_lltattach(struct ifnet *ifp)
1459 struct lltable *llt;
1461 llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
1462 llt->llt_af = AF_INET;
1465 llt->llt_lookup = in_lltable_lookup;
1466 llt->llt_alloc_entry = in_lltable_alloc;
1467 llt->llt_delete_entry = in_lltable_delete_entry;
1468 llt->llt_dump_entry = in_lltable_dump_entry;
1469 llt->llt_hash = in_lltable_hash;
1470 llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
1471 llt->llt_free_entry = in_lltable_free_entry;
1472 llt->llt_match_prefix = in_lltable_match_prefix;
1479 in_domifattach(struct ifnet *ifp)
1481 struct in_ifinfo *ii;
1483 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1485 ii->ii_llt = in_lltattach(ifp);
1486 ii->ii_igmp = igmp_domifattach(ifp);
1492 in_domifdetach(struct ifnet *ifp, void *aux)
1494 struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1496 igmp_domifdetach(ifp);
1497 lltable_free(ii->ii_llt);