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 ifa_ref(ifa); /* if_addrhead */
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);
501 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
502 LIST_REMOVE(ia, ia_hash);
504 ifa_free(&ia->ia_ifa);
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);
568 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
571 * in_scrubprefix() kills the interface route.
573 in_scrubprefix(ia, LLE_STATIC);
576 * in_ifadown gets rid of all the rest of
577 * the routes. This is not quite the right
578 * thing to do, but at least if we are running
579 * a routing process they will come back.
581 in_ifadown(&ia->ia_ifa, 1);
583 if (ia->ia_ifa.ifa_carp)
584 (*carp_detach_p)(&ia->ia_ifa);
587 * If this is the last IPv4 address configured on this
588 * interface, leave the all-hosts group.
589 * No state-change report need be transmitted.
591 if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
592 struct in_ifinfo *ii;
594 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
596 if (ii->ii_allhosts) {
597 (void)in_leavegroup_locked(ii->ii_allhosts, NULL);
598 ii->ii_allhosts = NULL;
603 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
608 #define rtinitflags(x) \
609 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
613 * Generate a routing message when inserting or deleting
614 * an interface address alias.
616 static void in_addralias_rtmsg(int cmd, struct in_addr *prefix,
617 struct in_ifaddr *target)
620 struct sockaddr_in *pfx_addr;
621 struct rtentry msg_rt;
624 * This is a bit questionable because there is no
625 * additional route entry added/deleted for an address
626 * alias. Therefore this route report is inaccurate.
628 bzero(&pfx_ro, sizeof(pfx_ro));
629 pfx_addr = (struct sockaddr_in *)(&pfx_ro.ro_dst);
630 pfx_addr->sin_len = sizeof(*pfx_addr);
631 pfx_addr->sin_family = AF_INET;
632 pfx_addr->sin_addr = *prefix;
633 rtalloc_ign_fib(&pfx_ro, 0, 0);
634 if (pfx_ro.ro_rt != NULL) {
635 msg_rt = *pfx_ro.ro_rt;
638 * Point the gateway to the new interface
639 * address as if a new prefix route entry has
640 * been added through the new address alias.
641 * All other parts of the rtentry is accurate,
642 * e.g., rt_key, rt_mask, rt_ifp etc.
644 msg_rt.rt_gateway = (struct sockaddr *)&target->ia_addr;
645 rt_newaddrmsg(cmd, (struct ifaddr *)target, 0, &msg_rt);
646 RTFREE(pfx_ro.ro_rt);
652 * Check if we have a route for the given prefix already or add one accordingly.
655 in_addprefix(struct in_ifaddr *target, int flags)
657 struct in_ifaddr *ia;
658 struct in_addr prefix, mask, p, m;
661 if ((flags & RTF_HOST) != 0) {
662 prefix = target->ia_dstaddr.sin_addr;
665 prefix = target->ia_addr.sin_addr;
666 mask = target->ia_sockmask.sin_addr;
667 prefix.s_addr &= mask.s_addr;
671 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
672 if (rtinitflags(ia)) {
673 p = ia->ia_dstaddr.sin_addr;
675 if (prefix.s_addr != p.s_addr)
678 p = ia->ia_addr.sin_addr;
679 m = ia->ia_sockmask.sin_addr;
680 p.s_addr &= m.s_addr;
682 if (prefix.s_addr != p.s_addr ||
683 mask.s_addr != m.s_addr)
688 * If we got a matching prefix route inserted by other
689 * interface address, we are done here.
691 if (ia->ia_flags & IFA_ROUTE) {
693 if (ia->ia_addr.sin_addr.s_addr ==
694 target->ia_addr.sin_addr.s_addr) {
700 if (V_nosameprefix) {
704 in_addralias_rtmsg(RTM_ADD, &prefix, target);
713 * No-one seem to have this prefix route, so we try to insert it.
715 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
717 target->ia_flags |= IFA_ROUTE;
722 * If there is no other address in the system that can serve a route to the
723 * same prefix, remove the route. Hand over the route to the new address
727 in_scrubprefix(struct in_ifaddr *target, u_int flags)
729 struct in_ifaddr *ia;
730 struct in_addr prefix, mask, p, m;
732 struct sockaddr_in prefix0, mask0;
735 * Remove the loopback route to the interface address.
737 if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
738 !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
739 (flags & LLE_STATIC)) {
740 struct in_ifaddr *eia;
742 eia = in_localip_more(target);
745 error = ifa_switch_loopback_route((struct ifaddr *)eia,
746 (struct sockaddr *)&target->ia_addr);
747 ifa_free(&eia->ia_ifa);
749 error = ifa_del_loopback_route((struct ifaddr *)target,
750 (struct sockaddr *)&target->ia_addr);
753 if (!(target->ia_ifp->if_flags & IFF_NOARP))
754 /* remove arp cache */
755 arp_ifscrub(target->ia_ifp,
756 IA_SIN(target)->sin_addr.s_addr);
759 if (rtinitflags(target)) {
760 prefix = target->ia_dstaddr.sin_addr;
763 prefix = target->ia_addr.sin_addr;
764 mask = target->ia_sockmask.sin_addr;
765 prefix.s_addr &= mask.s_addr;
768 if ((target->ia_flags & IFA_ROUTE) == 0) {
769 in_addralias_rtmsg(RTM_DELETE, &prefix, target);
774 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
775 if (rtinitflags(ia)) {
776 p = ia->ia_dstaddr.sin_addr;
778 if (prefix.s_addr != p.s_addr)
781 p = ia->ia_addr.sin_addr;
782 m = ia->ia_sockmask.sin_addr;
783 p.s_addr &= m.s_addr;
785 if (prefix.s_addr != p.s_addr ||
786 mask.s_addr != m.s_addr)
790 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
794 * If we got a matching prefix address, move IFA_ROUTE and
795 * the route itself to it. Make sure that routing daemons
798 if ((ia->ia_flags & IFA_ROUTE) == 0) {
799 ifa_ref(&ia->ia_ifa);
801 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
802 rtinitflags(target));
804 target->ia_flags &= ~IFA_ROUTE;
806 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
808 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
809 rtinitflags(ia) | RTF_UP);
811 ia->ia_flags |= IFA_ROUTE;
813 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
815 ifa_free(&ia->ia_ifa);
822 * remove all L2 entries on the given prefix
824 bzero(&prefix0, sizeof(prefix0));
825 prefix0.sin_len = sizeof(prefix0);
826 prefix0.sin_family = AF_INET;
827 prefix0.sin_addr.s_addr = target->ia_subnet;
828 bzero(&mask0, sizeof(mask0));
829 mask0.sin_len = sizeof(mask0);
830 mask0.sin_family = AF_INET;
831 mask0.sin_addr.s_addr = target->ia_subnetmask;
832 lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0,
833 (struct sockaddr *)&mask0, flags);
836 * As no-one seem to have this prefix, we can remove the route.
838 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
840 target->ia_flags &= ~IFA_ROUTE;
842 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
849 * Return 1 if the address might be a local broadcast address.
852 in_broadcast(struct in_addr in, struct ifnet *ifp)
854 register struct ifaddr *ifa;
857 if (in.s_addr == INADDR_BROADCAST ||
858 in.s_addr == INADDR_ANY)
860 if ((ifp->if_flags & IFF_BROADCAST) == 0)
862 t = ntohl(in.s_addr);
864 * Look through the list of addresses for a match
865 * with a broadcast address.
867 #define ia ((struct in_ifaddr *)ifa)
868 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
869 if (ifa->ifa_addr->sa_family == AF_INET &&
870 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
872 * Check for old-style (host 0) broadcast, but
873 * taking into account that RFC 3021 obsoletes it.
875 (ia->ia_subnetmask != IN_RFC3021_MASK &&
876 t == ia->ia_subnet)) &&
878 * Check for an all one subnetmask. These
879 * only exist when an interface gets a secondary
882 ia->ia_subnetmask != (u_long)0xffffffff)
889 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
892 in_ifdetach(struct ifnet *ifp)
895 in_pcbpurgeif0(&V_ripcbinfo, ifp);
896 in_pcbpurgeif0(&V_udbinfo, ifp);
901 * Delete all IPv4 multicast address records, and associated link-layer
902 * multicast address records, associated with ifp.
903 * XXX It looks like domifdetach runs AFTER the link layer cleanup.
904 * XXX This should not race with ifma_protospec being set during
905 * a new allocation, if it does, we have bigger problems.
908 in_purgemaddrs(struct ifnet *ifp)
910 LIST_HEAD(,in_multi) purgeinms;
911 struct in_multi *inm, *tinm;
912 struct ifmultiaddr *ifma;
914 LIST_INIT(&purgeinms);
918 * Extract list of in_multi associated with the detaching ifp
919 * which the PF_INET layer is about to release.
920 * We need to do this as IF_ADDR_LOCK() may be re-acquired
921 * by code further down.
924 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
925 if (ifma->ifma_addr->sa_family != AF_INET ||
926 ifma->ifma_protospec == NULL)
929 KASSERT(ifma->ifma_protospec != NULL,
930 ("%s: ifma_protospec is NULL", __func__));
932 inm = (struct in_multi *)ifma->ifma_protospec;
933 LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
935 IF_ADDR_RUNLOCK(ifp);
937 LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
938 LIST_REMOVE(inm, inm_link);
939 inm_release_locked(inm);
948 struct sockaddr_in l3_addr4;
952 * Deletes an address from the address table.
953 * This function is called by the timer functions
954 * such as arptimer() and nd6_llinfo_timer(), and
955 * the caller does the locking.
958 in_lltable_free(struct lltable *llt, struct llentry *lle)
961 LLE_LOCK_DESTROY(lle);
962 free(lle, M_LLTABLE);
965 static struct llentry *
966 in_lltable_new(const struct sockaddr *l3addr, u_int flags)
968 struct in_llentry *lle;
970 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
971 if (lle == NULL) /* NB: caller generates msg */
975 * For IPv4 this will trigger "arpresolve" to generate
978 lle->base.la_expire = time_uptime; /* mark expired */
979 lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
980 lle->base.lle_refcnt = 1;
981 lle->base.lle_free = in_lltable_free;
982 LLE_LOCK_INIT(&lle->base);
983 callout_init_rw(&lle->base.la_timer, &lle->base.lle_lock,
984 CALLOUT_RETURNUNLOCKED);
989 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
990 (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
993 in_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix,
994 const struct sockaddr *mask, u_int flags)
996 const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
997 const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
998 struct llentry *lle, *next;
1000 size_t pkts_dropped;
1002 IF_AFDATA_WLOCK(llt->llt_ifp);
1003 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
1004 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
1006 * (flags & LLE_STATIC) means deleting all entries
1007 * including static ARP entries.
1009 if (IN_ARE_MASKED_ADDR_EQUAL(satosin(L3_ADDR(lle)),
1010 pfx, msk) && ((flags & LLE_STATIC) ||
1011 !(lle->la_flags & LLE_STATIC))) {
1013 if (callout_stop(&lle->la_timer))
1015 pkts_dropped = llentry_free(lle);
1016 ARPSTAT_ADD(dropped, pkts_dropped);
1020 IF_AFDATA_WUNLOCK(llt->llt_ifp);
1025 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1029 KASSERT(l3addr->sa_family == AF_INET,
1030 ("sin_family %d", l3addr->sa_family));
1032 /* XXX rtalloc1 should take a const param */
1033 rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
1039 * If the gateway for an existing host route matches the target L3
1040 * address, which is a special route inserted by some implementation
1041 * such as MANET, and the interface is of the correct type, then
1042 * allow for ARP to proceed.
1044 if (rt->rt_flags & RTF_GATEWAY) {
1045 if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp ||
1046 rt->rt_ifp->if_type != IFT_ETHER ||
1047 (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1048 memcmp(rt->rt_gateway->sa_data, l3addr->sa_data,
1049 sizeof(in_addr_t)) != 0) {
1056 * Make sure that at least the destination address is covered
1057 * by the route. This is for handling the case where 2 or more
1058 * interfaces have the same prefix. An incoming packet arrives
1059 * on one interface and the corresponding outgoing packet leaves
1060 * another interface.
1062 if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) {
1063 const char *sa, *mask, *addr, *lim;
1066 mask = (const char *)rt_mask(rt);
1068 * Just being extra cautious to avoid some custom
1069 * code getting into trouble.
1076 sa = (const char *)rt_key(rt);
1077 addr = (const char *)l3addr;
1078 len = ((const struct sockaddr_in *)l3addr)->sin_len;
1081 for ( ; addr < lim; sa++, mask++, addr++) {
1082 if ((*sa ^ *addr) & *mask) {
1084 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
1085 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
1098 * Return NULL if not found or marked for deletion.
1099 * If found return lle read locked.
1101 static struct llentry *
1102 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1104 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1105 struct ifnet *ifp = llt->llt_ifp;
1106 struct llentry *lle;
1107 struct llentries *lleh;
1110 IF_AFDATA_LOCK_ASSERT(ifp);
1111 KASSERT(l3addr->sa_family == AF_INET,
1112 ("sin_family %d", l3addr->sa_family));
1114 hashkey = sin->sin_addr.s_addr;
1115 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
1116 LIST_FOREACH(lle, lleh, lle_next) {
1117 struct sockaddr_in *sa2 = satosin(L3_ADDR(lle));
1118 if (lle->la_flags & LLE_DELETED)
1120 if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
1125 if (flags & LLE_DELETE)
1126 log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle);
1128 if (!(flags & LLE_CREATE))
1130 IF_AFDATA_WLOCK_ASSERT(ifp);
1132 * A route that covers the given address must have
1133 * been installed 1st because we are doing a resolution,
1136 if (!(flags & LLE_IFADDR) &&
1137 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1140 lle = in_lltable_new(l3addr, flags);
1142 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1145 lle->la_flags = flags & ~LLE_CREATE;
1146 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
1147 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
1148 lle->la_flags |= (LLE_VALID | LLE_STATIC);
1152 lle->lle_head = lleh;
1153 lle->la_flags |= LLE_LINKED;
1154 LIST_INSERT_HEAD(lleh, lle, lle_next);
1155 } else if (flags & LLE_DELETE) {
1156 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
1158 lle->la_flags |= LLE_DELETED;
1159 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1161 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1163 if ((lle->la_flags &
1164 (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
1172 if (LLE_IS_VALID(lle)) {
1173 if (flags & LLE_EXCLUSIVE)
1183 in_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
1185 #define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle))
1186 struct ifnet *ifp = llt->llt_ifp;
1187 struct llentry *lle;
1190 struct rt_msghdr rtm;
1191 struct sockaddr_in sin;
1192 struct sockaddr_dl sdl;
1196 LLTABLE_LOCK_ASSERT();
1199 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
1200 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
1201 struct sockaddr_dl *sdl;
1203 /* skip deleted entries */
1204 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1206 /* Skip if jailed and not a valid IP of the prison. */
1207 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
1210 * produce a msg made of:
1212 * struct sockaddr_in; (IPv4)
1213 * struct sockaddr_dl;
1215 bzero(&arpc, sizeof(arpc));
1216 arpc.rtm.rtm_msglen = sizeof(arpc);
1217 arpc.rtm.rtm_version = RTM_VERSION;
1218 arpc.rtm.rtm_type = RTM_GET;
1219 arpc.rtm.rtm_flags = RTF_UP;
1220 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1221 arpc.sin.sin_family = AF_INET;
1222 arpc.sin.sin_len = sizeof(arpc.sin);
1223 arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
1226 if (lle->la_flags & LLE_PUB)
1227 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1230 sdl->sdl_family = AF_LINK;
1231 sdl->sdl_len = sizeof(*sdl);
1232 sdl->sdl_index = ifp->if_index;
1233 sdl->sdl_type = ifp->if_type;
1234 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1235 sdl->sdl_alen = ifp->if_addrlen;
1236 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1239 bzero(LLADDR(sdl), ifp->if_addrlen);
1242 arpc.rtm.rtm_rmx.rmx_expire =
1243 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1244 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1245 if (lle->la_flags & LLE_STATIC)
1246 arpc.rtm.rtm_flags |= RTF_STATIC;
1247 arpc.rtm.rtm_index = ifp->if_index;
1248 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1258 in_domifattach(struct ifnet *ifp)
1260 struct in_ifinfo *ii;
1261 struct lltable *llt;
1263 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1265 llt = lltable_init(ifp, AF_INET);
1267 llt->llt_prefix_free = in_lltable_prefix_free;
1268 llt->llt_lookup = in_lltable_lookup;
1269 llt->llt_dump = in_lltable_dump;
1273 ii->ii_igmp = igmp_domifattach(ifp);
1279 in_domifdetach(struct ifnet *ifp, void *aux)
1281 struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1283 igmp_domifdetach(ifp);
1284 lltable_free(ii->ii_llt);