2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (C) 2001 WIDE Project. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * @(#)in.c 8.4 (Berkeley) 1/9/95
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_mpath.h"
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>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
53 #include <net/if_var.h>
54 #include <net/if_arp.h>
55 #include <net/if_dl.h>
56 #include <net/if_llatbl.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
61 #include <netinet/if_ether.h>
62 #include <netinet/in.h>
63 #include <netinet/in_var.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/ip_var.h>
66 #include <netinet/ip_carp.h>
67 #include <netinet/igmp_var.h>
68 #include <netinet/udp.h>
69 #include <netinet/udp_var.h>
71 static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
72 static int in_difaddr_ioctl(caddr_t, struct ifnet *, struct thread *);
74 static void in_socktrim(struct sockaddr_in *);
75 static void in_purgemaddrs(struct ifnet *);
77 static VNET_DEFINE(int, nosameprefix);
78 #define V_nosameprefix VNET(nosameprefix)
79 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_RW,
80 &VNET_NAME(nosameprefix), 0,
81 "Refuse to create same prefixes on different interfaces");
83 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
84 #define V_ripcbinfo VNET(ripcbinfo)
86 static struct sx in_control_sx;
87 SX_SYSINIT(in_control_sx, &in_control_sx, "in_control");
90 * Return 1 if an internet address is for a ``local'' host
91 * (one to which we have a connection).
94 in_localaddr(struct in_addr in)
96 register u_long i = ntohl(in.s_addr);
97 register struct in_ifaddr *ia;
100 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
101 if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
111 * Return 1 if an internet address is for the local host and configured
112 * on one of its interfaces.
115 in_localip(struct in_addr in)
117 struct in_ifaddr *ia;
120 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
121 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
131 * Return a reference to the interface address which is different to
132 * the supplied one but with same IP address value.
134 static struct in_ifaddr *
135 in_localip_more(struct in_ifaddr *ia)
137 in_addr_t in = IA_SIN(ia)->sin_addr.s_addr;
138 struct in_ifaddr *it;
141 LIST_FOREACH(it, INADDR_HASH(in), ia_hash) {
142 if (it != ia && IA_SIN(it)->sin_addr.s_addr == in) {
143 ifa_ref(&it->ia_ifa);
154 * Determine whether an IP address is in a reserved set of addresses
155 * that may not be forwarded, or whether datagrams to that destination
159 in_canforward(struct in_addr in)
161 register u_long i = ntohl(in.s_addr);
164 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
167 net = i & IN_CLASSA_NET;
168 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
175 * Trim a mask in a sockaddr
178 in_socktrim(struct sockaddr_in *ap)
180 register char *cplim = (char *) &ap->sin_addr;
181 register char *cp = (char *) (&ap->sin_addr + 1);
184 while (--cp >= cplim)
186 (ap)->sin_len = cp - (char *) (ap) + 1;
192 * Generic internet control operations (ioctl's).
195 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
198 struct ifreq *ifr = (struct ifreq *)data;
199 struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
201 struct in_ifaddr *ia;
205 return (EADDRNOTAVAIL);
208 * Filter out 4 ioctls we implement directly. Forward the rest
209 * to specific functions and ifp->if_ioctl().
218 sx_xlock(&in_control_sx);
219 error = in_difaddr_ioctl(data, ifp, td);
220 sx_xunlock(&in_control_sx);
222 case OSIOCAIFADDR: /* 9.x compat */
224 sx_xlock(&in_control_sx);
225 error = in_aifaddr_ioctl(cmd, data, ifp, td);
226 sx_xunlock(&in_control_sx);
232 /* We no longer support that old commands. */
235 if (ifp->if_ioctl == NULL)
237 return ((*ifp->if_ioctl)(ifp, cmd, data));
240 if (addr->sin_addr.s_addr != INADDR_ANY &&
241 prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0)
242 return (EADDRNOTAVAIL);
245 * For SIOCGIFADDR, pick the first address. For the rest of
246 * ioctls, try to find specified address.
249 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
250 if (ifa->ifa_addr->sa_family != AF_INET)
252 ia = (struct in_ifaddr *)ifa;
253 if (cmd == SIOCGIFADDR || addr->sin_addr.s_addr == INADDR_ANY)
255 if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
260 IF_ADDR_RUNLOCK(ifp);
261 return (EADDRNOTAVAIL);
271 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
275 *addr = ia->ia_broadaddr;
279 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
283 *addr = ia->ia_dstaddr;
287 *addr = ia->ia_sockmask;
291 IF_ADDR_RUNLOCK(ifp);
297 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
299 const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
300 const struct sockaddr_in *addr = &ifra->ifra_addr;
301 const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
302 const struct sockaddr_in *mask = &ifra->ifra_mask;
303 const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
304 const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
306 struct in_ifaddr *ia;
310 error = priv_check(td, PRIV_NET_ADDIFADDR);
315 * ifra_addr must be present and be of INET family.
316 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
318 if (addr->sin_len != sizeof(struct sockaddr_in) ||
319 addr->sin_family != AF_INET)
321 if (broadaddr->sin_len != 0 &&
322 (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
323 broadaddr->sin_family != AF_INET))
325 if (mask->sin_len != 0 &&
326 (mask->sin_len != sizeof(struct sockaddr_in) ||
327 mask->sin_family != AF_INET))
329 if ((ifp->if_flags & IFF_POINTOPOINT) &&
330 (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
331 dstaddr->sin_addr.s_addr == INADDR_ANY))
332 return (EDESTADDRREQ);
333 if (vhid > 0 && carp_attach_p == NULL)
334 return (EPROTONOSUPPORT);
337 * See whether address already exist.
342 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
343 struct in_ifaddr *it;
345 if (ifa->ifa_addr->sa_family != AF_INET)
348 it = (struct in_ifaddr *)ifa;
350 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
351 prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0)
354 IF_ADDR_RUNLOCK(ifp);
357 (void )in_difaddr_ioctl(data, ifp, td);
359 ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
360 ia = (struct in_ifaddr *)ifa;
361 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
362 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
363 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
366 ia->ia_ifa.ifa_metric = ifp->if_metric;
368 if (mask->sin_len != 0) {
369 ia->ia_sockmask = *mask;
370 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
372 in_addr_t i = ntohl(addr->sin_addr.s_addr);
375 * Be compatible with network classes, if netmask isn't
376 * supplied, guess it based on classes.
379 ia->ia_subnetmask = IN_CLASSA_NET;
380 else if (IN_CLASSB(i))
381 ia->ia_subnetmask = IN_CLASSB_NET;
383 ia->ia_subnetmask = IN_CLASSC_NET;
384 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
386 ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
387 in_socktrim(&ia->ia_sockmask);
389 if (ifp->if_flags & IFF_BROADCAST) {
390 if (broadaddr->sin_len != 0) {
391 ia->ia_broadaddr = *broadaddr;
392 } else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
393 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
394 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
395 ia->ia_broadaddr.sin_family = AF_INET;
397 ia->ia_broadaddr.sin_addr.s_addr =
398 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
399 ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
400 ia->ia_broadaddr.sin_family = AF_INET;
404 if (ifp->if_flags & IFF_POINTOPOINT)
405 ia->ia_dstaddr = *dstaddr;
407 /* XXXGL: rtinit() needs this strange assignment. */
408 if (ifp->if_flags & IFF_LOOPBACK)
409 ia->ia_dstaddr = ia->ia_addr;
411 /* if_addrhead is already referenced by ifa_alloc() */
413 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
414 IF_ADDR_WUNLOCK(ifp);
416 ifa_ref(ifa); /* in_ifaddrhead */
418 TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
419 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
423 error = (*carp_attach_p)(&ia->ia_ifa, vhid);
428 * Give the interface a chance to initialize
429 * if this is its first address,
430 * and to validate the address if necessary.
432 if (ifp->if_ioctl != NULL)
433 error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
438 * Add route for the network.
443 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
446 error = in_addprefix(ia, flags);
452 * Add a loopback route to self.
454 if (vhid == 0 && (ifp->if_flags & IFF_LOOPBACK) == 0 &&
455 ia->ia_addr.sin_addr.s_addr != INADDR_ANY &&
456 !((ifp->if_flags & IFF_POINTOPOINT) &&
457 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)) {
458 struct in_ifaddr *eia;
460 eia = in_localip_more(ia);
463 error = ifa_add_loopback_route((struct ifaddr *)ia,
464 (struct sockaddr *)&ia->ia_addr);
468 ifa_free(&eia->ia_ifa);
471 if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
472 struct in_addr allhosts_addr;
473 struct in_ifinfo *ii;
475 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
476 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
478 error = in_joingroup(ifp, &allhosts_addr, NULL,
482 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
488 (void )in_scrubprefix(ia, LLE_STATIC);
491 if (ia->ia_ifa.ifa_carp)
492 (*carp_detach_p)(&ia->ia_ifa);
496 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
497 IF_ADDR_WUNLOCK(ifp);
498 ifa_free(&ia->ia_ifa); /* if_addrhead */
501 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
502 LIST_REMOVE(ia, ia_hash);
504 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
510 in_difaddr_ioctl(caddr_t data, struct ifnet *ifp, struct thread *td)
512 const struct ifreq *ifr = (struct ifreq *)data;
513 const struct sockaddr_in *addr = (const struct sockaddr_in *)
516 struct in_ifaddr *ia;
517 bool deleteAny, iaIsLast;
521 error = priv_check(td, PRIV_NET_DELIFADDR);
526 if (addr->sin_len != sizeof(struct sockaddr_in) ||
527 addr->sin_family != AF_INET)
535 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
536 struct in_ifaddr *it;
538 if (ifa->ifa_addr->sa_family != AF_INET)
541 it = (struct in_ifaddr *)ifa;
542 if (deleteAny && ia == NULL && (td == NULL ||
543 prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0))
546 if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
547 (td == NULL || prison_check_ip4(td->td_ucred,
548 &addr->sin_addr) == 0))
556 IF_ADDR_WUNLOCK(ifp);
557 return (EADDRNOTAVAIL);
560 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
561 IF_ADDR_WUNLOCK(ifp);
562 ifa_free(&ia->ia_ifa); /* if_addrhead */
565 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
566 LIST_REMOVE(ia, ia_hash);
570 * in_scrubprefix() kills the interface route.
572 in_scrubprefix(ia, LLE_STATIC);
575 * in_ifadown gets rid of all the rest of
576 * the routes. This is not quite the right
577 * thing to do, but at least if we are running
578 * a routing process they will come back.
580 in_ifadown(&ia->ia_ifa, 1);
582 if (ia->ia_ifa.ifa_carp)
583 (*carp_detach_p)(&ia->ia_ifa);
586 * If this is the last IPv4 address configured on this
587 * interface, leave the all-hosts group.
588 * No state-change report need be transmitted.
590 if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
591 struct in_ifinfo *ii;
593 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
595 if (ii->ii_allhosts) {
596 (void)in_leavegroup_locked(ii->ii_allhosts, NULL);
597 ii->ii_allhosts = NULL;
602 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
603 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
608 #define rtinitflags(x) \
609 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
613 * Check if we have a route for the given prefix already or add one accordingly.
616 in_addprefix(struct in_ifaddr *target, int flags)
618 struct in_ifaddr *ia;
619 struct in_addr prefix, mask, p, m;
622 if ((flags & RTF_HOST) != 0) {
623 prefix = target->ia_dstaddr.sin_addr;
626 prefix = target->ia_addr.sin_addr;
627 mask = target->ia_sockmask.sin_addr;
628 prefix.s_addr &= mask.s_addr;
632 /* Look for an existing address with the same prefix, mask, and fib */
633 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
634 if (rtinitflags(ia)) {
635 p = ia->ia_dstaddr.sin_addr;
637 if (prefix.s_addr != p.s_addr)
640 p = ia->ia_addr.sin_addr;
641 m = ia->ia_sockmask.sin_addr;
642 p.s_addr &= m.s_addr;
644 if (prefix.s_addr != p.s_addr ||
645 mask.s_addr != m.s_addr)
648 if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
652 * If we got a matching prefix route inserted by other
653 * interface address, we are done here.
655 if (ia->ia_flags & IFA_ROUTE) {
657 if (ia->ia_addr.sin_addr.s_addr ==
658 target->ia_addr.sin_addr.s_addr) {
664 if (V_nosameprefix) {
670 fibnum = rt_add_addr_allfibs ? RT_ALL_FIBS :
671 target->ia_ifp->if_fib;
672 rt_addrmsg(RTM_ADD, &target->ia_ifa, fibnum);
681 * No-one seem to have this prefix route, so we try to insert it.
683 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
685 target->ia_flags |= IFA_ROUTE;
690 * If there is no other address in the system that can serve a route to the
691 * same prefix, remove the route. Hand over the route to the new address
695 in_scrubprefix(struct in_ifaddr *target, u_int flags)
697 struct in_ifaddr *ia;
698 struct in_addr prefix, mask, p, m;
700 struct sockaddr_in prefix0, mask0;
703 * Remove the loopback route to the interface address.
705 if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
706 !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
707 (flags & LLE_STATIC)) {
708 struct in_ifaddr *eia;
710 eia = in_localip_more(target);
713 int fibnum = target->ia_ifp->if_fib;
715 error = ifa_switch_loopback_route((struct ifaddr *)eia,
716 (struct sockaddr *)&target->ia_addr, fibnum);
717 ifa_free(&eia->ia_ifa);
719 error = ifa_del_loopback_route((struct ifaddr *)target,
720 (struct sockaddr *)&target->ia_addr);
723 if (!(target->ia_ifp->if_flags & IFF_NOARP))
724 /* remove arp cache */
725 arp_ifscrub(target->ia_ifp,
726 IA_SIN(target)->sin_addr.s_addr);
729 if (rtinitflags(target)) {
730 prefix = target->ia_dstaddr.sin_addr;
733 prefix = target->ia_addr.sin_addr;
734 mask = target->ia_sockmask.sin_addr;
735 prefix.s_addr &= mask.s_addr;
738 if ((target->ia_flags & IFA_ROUTE) == 0) {
741 fibnum = rt_add_addr_allfibs ? RT_ALL_FIBS :
742 target->ia_ifp->if_fib;
743 rt_addrmsg(RTM_DELETE, &target->ia_ifa, fibnum);
748 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
749 if (rtinitflags(ia)) {
750 p = ia->ia_dstaddr.sin_addr;
752 if (prefix.s_addr != p.s_addr)
755 p = ia->ia_addr.sin_addr;
756 m = ia->ia_sockmask.sin_addr;
757 p.s_addr &= m.s_addr;
759 if (prefix.s_addr != p.s_addr ||
760 mask.s_addr != m.s_addr)
764 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
768 * If we got a matching prefix address, move IFA_ROUTE and
769 * the route itself to it. Make sure that routing daemons
772 if ((ia->ia_flags & IFA_ROUTE) == 0) {
773 ifa_ref(&ia->ia_ifa);
775 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
776 rtinitflags(target));
778 target->ia_flags &= ~IFA_ROUTE;
780 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
782 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
783 rtinitflags(ia) | RTF_UP);
785 ia->ia_flags |= IFA_ROUTE;
787 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
789 ifa_free(&ia->ia_ifa);
796 * remove all L2 entries on the given prefix
798 bzero(&prefix0, sizeof(prefix0));
799 prefix0.sin_len = sizeof(prefix0);
800 prefix0.sin_family = AF_INET;
801 prefix0.sin_addr.s_addr = target->ia_subnet;
802 bzero(&mask0, sizeof(mask0));
803 mask0.sin_len = sizeof(mask0);
804 mask0.sin_family = AF_INET;
805 mask0.sin_addr.s_addr = target->ia_subnetmask;
806 lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0,
807 (struct sockaddr *)&mask0, flags);
810 * As no-one seem to have this prefix, we can remove the route.
812 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
814 target->ia_flags &= ~IFA_ROUTE;
816 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
823 * Return 1 if the address might be a local broadcast address.
826 in_broadcast(struct in_addr in, struct ifnet *ifp)
828 register struct ifaddr *ifa;
831 if (in.s_addr == INADDR_BROADCAST ||
832 in.s_addr == INADDR_ANY)
834 if ((ifp->if_flags & IFF_BROADCAST) == 0)
836 t = ntohl(in.s_addr);
838 * Look through the list of addresses for a match
839 * with a broadcast address.
841 #define ia ((struct in_ifaddr *)ifa)
842 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
843 if (ifa->ifa_addr->sa_family == AF_INET &&
844 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
846 * Check for old-style (host 0) broadcast, but
847 * taking into account that RFC 3021 obsoletes it.
849 (ia->ia_subnetmask != IN_RFC3021_MASK &&
850 t == ia->ia_subnet)) &&
852 * Check for an all one subnetmask. These
853 * only exist when an interface gets a secondary
856 ia->ia_subnetmask != (u_long)0xffffffff)
863 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
866 in_ifdetach(struct ifnet *ifp)
869 in_pcbpurgeif0(&V_ripcbinfo, ifp);
870 in_pcbpurgeif0(&V_udbinfo, ifp);
871 in_pcbpurgeif0(&V_ulitecbinfo, ifp);
876 * Delete all IPv4 multicast address records, and associated link-layer
877 * multicast address records, associated with ifp.
878 * XXX It looks like domifdetach runs AFTER the link layer cleanup.
879 * XXX This should not race with ifma_protospec being set during
880 * a new allocation, if it does, we have bigger problems.
883 in_purgemaddrs(struct ifnet *ifp)
885 LIST_HEAD(,in_multi) purgeinms;
886 struct in_multi *inm, *tinm;
887 struct ifmultiaddr *ifma;
889 LIST_INIT(&purgeinms);
893 * Extract list of in_multi associated with the detaching ifp
894 * which the PF_INET layer is about to release.
895 * We need to do this as IF_ADDR_LOCK() may be re-acquired
896 * by code further down.
899 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
900 if (ifma->ifma_addr->sa_family != AF_INET ||
901 ifma->ifma_protospec == NULL)
904 KASSERT(ifma->ifma_protospec != NULL,
905 ("%s: ifma_protospec is NULL", __func__));
907 inm = (struct in_multi *)ifma->ifma_protospec;
908 LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
910 IF_ADDR_RUNLOCK(ifp);
912 LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
913 LIST_REMOVE(inm, inm_link);
914 inm_release_locked(inm);
923 struct sockaddr_in l3_addr4;
927 * Deletes an address from the address table.
928 * This function is called by the timer functions
929 * such as arptimer() and nd6_llinfo_timer(), and
930 * the caller does the locking.
933 in_lltable_free(struct lltable *llt, struct llentry *lle)
936 LLE_LOCK_DESTROY(lle);
937 free(lle, M_LLTABLE);
940 static struct llentry *
941 in_lltable_new(const struct sockaddr *l3addr, u_int flags)
943 struct in_llentry *lle;
945 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
946 if (lle == NULL) /* NB: caller generates msg */
950 * For IPv4 this will trigger "arpresolve" to generate
953 lle->base.la_expire = time_uptime; /* mark expired */
954 lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
955 lle->base.lle_refcnt = 1;
956 lle->base.lle_free = in_lltable_free;
957 LLE_LOCK_INIT(&lle->base);
958 callout_init_rw(&lle->base.la_timer, &lle->base.lle_lock,
959 CALLOUT_RETURNUNLOCKED);
964 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
965 (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
968 in_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix,
969 const struct sockaddr *mask, u_int flags)
971 const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
972 const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
973 struct llentry *lle, *next;
977 IF_AFDATA_WLOCK(llt->llt_ifp);
978 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
979 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
981 * (flags & LLE_STATIC) means deleting all entries
982 * including static ARP entries.
984 if (IN_ARE_MASKED_ADDR_EQUAL(satosin(L3_ADDR(lle)),
985 pfx, msk) && ((flags & LLE_STATIC) ||
986 !(lle->la_flags & LLE_STATIC))) {
988 if (callout_stop(&lle->la_timer))
990 pkts_dropped = llentry_free(lle);
991 ARPSTAT_ADD(dropped, pkts_dropped);
995 IF_AFDATA_WUNLOCK(llt->llt_ifp);
1000 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1004 KASSERT(l3addr->sa_family == AF_INET,
1005 ("sin_family %d", l3addr->sa_family));
1007 /* XXX rtalloc1_fib should take a const param */
1008 rt = rtalloc1_fib(__DECONST(struct sockaddr *, l3addr), 0, 0,
1015 * If the gateway for an existing host route matches the target L3
1016 * address, which is a special route inserted by some implementation
1017 * such as MANET, and the interface is of the correct type, then
1018 * allow for ARP to proceed.
1020 if (rt->rt_flags & RTF_GATEWAY) {
1021 if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp ||
1022 rt->rt_ifp->if_type != IFT_ETHER ||
1023 (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1024 memcmp(rt->rt_gateway->sa_data, l3addr->sa_data,
1025 sizeof(in_addr_t)) != 0) {
1032 * Make sure that at least the destination address is covered
1033 * by the route. This is for handling the case where 2 or more
1034 * interfaces have the same prefix. An incoming packet arrives
1035 * on one interface and the corresponding outgoing packet leaves
1036 * another interface.
1038 if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) {
1039 const char *sa, *mask, *addr, *lim;
1042 mask = (const char *)rt_mask(rt);
1044 * Just being extra cautious to avoid some custom
1045 * code getting into trouble.
1052 sa = (const char *)rt_key(rt);
1053 addr = (const char *)l3addr;
1054 len = ((const struct sockaddr_in *)l3addr)->sin_len;
1057 for ( ; addr < lim; sa++, mask++, addr++) {
1058 if ((*sa ^ *addr) & *mask) {
1060 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
1061 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
1074 * Return NULL if not found or marked for deletion.
1075 * If found return lle read locked.
1077 static struct llentry *
1078 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1080 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1081 struct ifnet *ifp = llt->llt_ifp;
1082 struct llentry *lle;
1083 struct llentries *lleh;
1086 IF_AFDATA_LOCK_ASSERT(ifp);
1087 KASSERT(l3addr->sa_family == AF_INET,
1088 ("sin_family %d", l3addr->sa_family));
1090 hashkey = sin->sin_addr.s_addr;
1091 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
1092 LIST_FOREACH(lle, lleh, lle_next) {
1093 struct sockaddr_in *sa2 = satosin(L3_ADDR(lle));
1094 if (lle->la_flags & LLE_DELETED)
1096 if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
1101 if (flags & LLE_DELETE)
1102 log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle);
1104 if (!(flags & LLE_CREATE))
1106 IF_AFDATA_WLOCK_ASSERT(ifp);
1108 * A route that covers the given address must have
1109 * been installed 1st because we are doing a resolution,
1112 if (!(flags & LLE_IFADDR) &&
1113 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1116 lle = in_lltable_new(l3addr, flags);
1118 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1121 lle->la_flags = flags & ~LLE_CREATE;
1122 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
1123 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
1124 lle->la_flags |= (LLE_VALID | LLE_STATIC);
1128 lle->lle_head = lleh;
1129 lle->la_flags |= LLE_LINKED;
1130 LIST_INSERT_HEAD(lleh, lle, lle_next);
1131 } else if (flags & LLE_DELETE) {
1132 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
1134 lle->la_flags |= LLE_DELETED;
1135 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1137 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1139 if ((lle->la_flags &
1140 (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
1148 if (LLE_IS_VALID(lle)) {
1149 if (flags & LLE_EXCLUSIVE)
1159 in_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
1161 #define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle))
1162 struct ifnet *ifp = llt->llt_ifp;
1163 struct llentry *lle;
1166 struct rt_msghdr rtm;
1167 struct sockaddr_in sin;
1168 struct sockaddr_dl sdl;
1172 LLTABLE_LOCK_ASSERT();
1175 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
1176 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
1177 struct sockaddr_dl *sdl;
1179 /* skip deleted entries */
1180 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1182 /* Skip if jailed and not a valid IP of the prison. */
1183 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
1186 * produce a msg made of:
1188 * struct sockaddr_in; (IPv4)
1189 * struct sockaddr_dl;
1191 bzero(&arpc, sizeof(arpc));
1192 arpc.rtm.rtm_msglen = sizeof(arpc);
1193 arpc.rtm.rtm_version = RTM_VERSION;
1194 arpc.rtm.rtm_type = RTM_GET;
1195 arpc.rtm.rtm_flags = RTF_UP;
1196 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1197 arpc.sin.sin_family = AF_INET;
1198 arpc.sin.sin_len = sizeof(arpc.sin);
1199 arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
1202 if (lle->la_flags & LLE_PUB)
1203 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1206 sdl->sdl_family = AF_LINK;
1207 sdl->sdl_len = sizeof(*sdl);
1208 sdl->sdl_index = ifp->if_index;
1209 sdl->sdl_type = ifp->if_type;
1210 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1211 sdl->sdl_alen = ifp->if_addrlen;
1212 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1215 bzero(LLADDR(sdl), ifp->if_addrlen);
1218 arpc.rtm.rtm_rmx.rmx_expire =
1219 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1220 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1221 if (lle->la_flags & LLE_STATIC)
1222 arpc.rtm.rtm_flags |= RTF_STATIC;
1223 arpc.rtm.rtm_index = ifp->if_index;
1224 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1234 in_domifattach(struct ifnet *ifp)
1236 struct in_ifinfo *ii;
1237 struct lltable *llt;
1239 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1241 llt = lltable_init(ifp, AF_INET);
1243 llt->llt_prefix_free = in_lltable_prefix_free;
1244 llt->llt_lookup = in_lltable_lookup;
1245 llt->llt_dump = in_lltable_dump;
1249 ii->ii_igmp = igmp_domifattach(ifp);
1255 in_domifdetach(struct ifnet *ifp, void *aux)
1257 struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1259 igmp_domifdetach(ifp);
1260 lltable_free(ii->ii_llt);