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/systm.h>
40 #include <sys/sockio.h>
41 #include <sys/malloc.h>
43 #include <sys/socket.h>
45 #include <sys/kernel.h>
47 #include <sys/sysctl.h>
48 #include <sys/syslog.h>
51 #include <net/if_var.h>
52 #include <net/if_arp.h>
53 #include <net/if_dl.h>
54 #include <net/if_llatbl.h>
55 #include <net/if_types.h>
56 #include <net/route.h>
59 #include <netinet/if_ether.h>
60 #include <netinet/in.h>
61 #include <netinet/in_var.h>
62 #include <netinet/in_pcb.h>
63 #include <netinet/ip_var.h>
64 #include <netinet/ip_carp.h>
65 #include <netinet/igmp_var.h>
66 #include <netinet/udp.h>
67 #include <netinet/udp_var.h>
69 static int in_mask2len(struct in_addr *);
70 static void in_len2mask(struct in_addr *, int);
71 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
72 struct ifnet *, struct thread *);
74 static void in_socktrim(struct sockaddr_in *);
75 static int in_ifinit(struct ifnet *, struct in_ifaddr *,
76 struct sockaddr_in *, int, int);
77 static void in_purgemaddrs(struct ifnet *);
79 static VNET_DEFINE(int, nosameprefix);
80 #define V_nosameprefix VNET(nosameprefix)
81 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_RW,
82 &VNET_NAME(nosameprefix), 0,
83 "Refuse to create same prefixes on different interfaces");
85 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
86 #define V_ripcbinfo VNET(ripcbinfo)
89 * Return 1 if an internet address is for a ``local'' host
90 * (one to which we have a connection).
93 in_localaddr(struct in_addr in)
95 register u_long i = ntohl(in.s_addr);
96 register struct in_ifaddr *ia;
99 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
100 if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
110 * Return 1 if an internet address is for the local host and configured
111 * on one of its interfaces.
114 in_localip(struct in_addr in)
116 struct in_ifaddr *ia;
119 LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
120 if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
130 * Determine whether an IP address is in a reserved set of addresses
131 * that may not be forwarded, or whether datagrams to that destination
135 in_canforward(struct in_addr in)
137 register u_long i = ntohl(in.s_addr);
140 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
143 net = i & IN_CLASSA_NET;
144 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
151 * Trim a mask in a sockaddr
154 in_socktrim(struct sockaddr_in *ap)
156 register char *cplim = (char *) &ap->sin_addr;
157 register char *cp = (char *) (&ap->sin_addr + 1);
160 while (--cp >= cplim)
162 (ap)->sin_len = cp - (char *) (ap) + 1;
169 struct in_addr *mask;
175 for (x = 0; x < sizeof(*mask); x++) {
180 if (x < sizeof(*mask)) {
181 for (y = 0; y < 8; y++) {
182 if ((p[x] & (0x80 >> y)) == 0)
190 in_len2mask(struct in_addr *mask, int len)
196 bzero(mask, sizeof(*mask));
197 for (i = 0; i < len / 8; i++)
200 p[i] = (0xff00 >> (len % 8)) & 0xff;
204 * Generic internet control operations (ioctl's).
206 * ifp is NULL if not an interface-specific ioctl.
210 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
213 register struct ifreq *ifr = (struct ifreq *)data;
214 register struct in_ifaddr *ia, *iap;
215 register struct ifaddr *ifa;
216 struct in_addr allhosts_addr;
218 struct in_ifinfo *ii;
219 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
220 int error, hostIsNew, iaIsNew, maskIsNew;
225 * Pre-10.x compat: OSIOCAIFADDR passes a shorter
226 * struct in_aliasreq, without ifra_vhid.
228 if (cmd == OSIOCAIFADDR)
234 allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
237 * Filter out ioctls we implement directly; forward the rest on to
238 * in_lifaddr_ioctl() and ifp->if_ioctl().
249 * ifra_addr must be present and be of INET family.
250 * ifra_broadaddr and ifra_mask are optional.
252 if (ifra->ifra_addr.sin_len != sizeof(struct sockaddr_in) ||
253 ifra->ifra_addr.sin_family != AF_INET)
255 if (ifra->ifra_broadaddr.sin_len != 0 &&
256 (ifra->ifra_broadaddr.sin_len !=
257 sizeof(struct sockaddr_in) ||
258 ifra->ifra_broadaddr.sin_family != AF_INET))
262 * ifconfig(8) in pre-10.x doesn't set sin_family for the
263 * mask. The code is disabled for the 10.x timeline, to
264 * make SIOCAIFADDR compatible with 9.x ifconfig(8).
265 * The code should be enabled in 11.x
267 if (ifra->ifra_mask.sin_len != 0 &&
268 (ifra->ifra_mask.sin_len != sizeof(struct sockaddr_in) ||
269 ifra->ifra_mask.sin_family != AF_INET))
277 /* We no longer support that old commands. */
282 error = priv_check(td, PRIV_NET_ADDIFADDR);
288 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
292 error = priv_check(td, PRIV_NET_DELIFADDR);
298 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
303 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
306 if (ifp == NULL || ifp->if_ioctl == NULL)
308 return ((*ifp->if_ioctl)(ifp, cmd, data));
312 return (EADDRNOTAVAIL);
315 * Security checks before we get involved in any work.
320 error = priv_check(td, PRIV_NET_ADDIFADDR);
328 error = priv_check(td, PRIV_NET_DELIFADDR);
336 * Find address for this interface, if it exists.
338 * If an alias address was specified, find that one instead of the
339 * first one on the interface, if possible.
341 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
343 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) {
344 if (iap->ia_ifp == ifp &&
345 iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
346 if (td == NULL || prison_check_ip4(td->td_ucred,
353 ifa_ref(&ia->ia_ifa);
357 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
359 if (iap->ia_addr.sin_family == AF_INET) {
361 prison_check_ip4(td->td_ucred,
362 &iap->ia_addr.sin_addr) != 0)
369 ifa_ref(&ia->ia_ifa);
370 IF_ADDR_RUNLOCK(ifp);
379 if (ifra->ifra_addr.sin_family == AF_INET) {
380 struct in_ifaddr *oia;
383 for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
384 if (ia->ia_ifp == ifp &&
385 ia->ia_addr.sin_addr.s_addr ==
386 ifra->ifra_addr.sin_addr.s_addr)
389 if (ia != NULL && ia != oia)
390 ifa_ref(&ia->ia_ifa);
391 if (oia != NULL && ia != oia)
392 ifa_free(&oia->ia_ifa);
394 if ((ifp->if_flags & IFF_POINTOPOINT)
395 && (cmd == SIOCAIFADDR)
396 && (ifra->ifra_dstaddr.sin_addr.s_addr
398 error = EDESTADDRREQ;
402 if (cmd == SIOCDIFADDR && ia == NULL) {
403 error = EADDRNOTAVAIL;
407 ia = (struct in_ifaddr *)
408 malloc(sizeof *ia, M_IFADDR, M_NOWAIT |
417 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
418 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
419 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
421 ia->ia_sockmask.sin_len = 8;
422 ia->ia_sockmask.sin_family = AF_INET;
423 if (ifp->if_flags & IFF_BROADCAST) {
424 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
425 ia->ia_broadaddr.sin_family = AF_INET;
429 ifa_ref(ifa); /* if_addrhead */
431 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
432 IF_ADDR_WUNLOCK(ifp);
433 ifa_ref(ifa); /* in_ifaddrhead */
435 TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
446 error = EADDRNOTAVAIL;
453 * Most paths in this switch return directly or via out. Only paths
454 * that remove the address break in order to hit common removal code.
458 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
462 if ((ifp->if_flags & IFF_BROADCAST) == 0) {
466 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
470 if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
474 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
478 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
485 if (ifra->ifra_addr.sin_addr.s_addr ==
486 ia->ia_addr.sin_addr.s_addr)
488 if (ifra->ifra_mask.sin_len) {
491 * Need to scrub the prefix here in case
492 * the issued command is SIOCAIFADDR with
493 * the same address, but with a different
494 * prefix length. And if the prefix length
495 * is the same as before, then the call is
496 * un-necessarily executed here.
498 in_ifscrub(ifp, ia, LLE_STATIC);
499 ia->ia_sockmask = ifra->ifra_mask;
500 ia->ia_sockmask.sin_family = AF_INET;
502 ntohl(ia->ia_sockmask.sin_addr.s_addr);
505 if ((ifp->if_flags & IFF_POINTOPOINT) &&
506 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
507 in_ifscrub(ifp, ia, LLE_STATIC);
508 ia->ia_dstaddr = ifra->ifra_dstaddr;
509 maskIsNew = 1; /* We lie; but the effect's the same */
511 if (hostIsNew || maskIsNew)
512 error = in_ifinit(ifp, ia, &ifra->ifra_addr, maskIsNew,
513 (ocmd == cmd ? ifra->ifra_vhid : 0));
514 if (error != 0 && iaIsNew)
517 if ((ifp->if_flags & IFF_BROADCAST) &&
518 ifra->ifra_broadaddr.sin_len)
519 ia->ia_broadaddr = ifra->ifra_broadaddr;
521 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
523 (ifp->if_flags & IFF_MULTICAST) != 0) {
524 error = in_joingroup(ifp, &allhosts_addr,
525 NULL, &ii->ii_allhosts);
527 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
533 * in_ifscrub kills the interface route.
535 in_ifscrub(ifp, ia, LLE_STATIC);
538 * in_ifadown gets rid of all the rest of
539 * the routes. This is not quite the right
540 * thing to do, but at least if we are running
541 * a routing process they will come back.
543 in_ifadown(&ia->ia_ifa, 1);
544 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
549 panic("in_control: unsupported ioctl");
552 if (ia->ia_ifa.ifa_carp)
553 (*carp_detach_p)(&ia->ia_ifa);
556 /* Re-check that ia is still part of the list. */
557 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
558 if (ifa == &ia->ia_ifa)
563 * If we lost the race with another thread, there is no need to
564 * try it again for the next loop as there is no other exit
565 * path between here and out.
567 IF_ADDR_WUNLOCK(ifp);
568 error = EADDRNOTAVAIL;
571 TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
572 IF_ADDR_WUNLOCK(ifp);
573 ifa_free(&ia->ia_ifa); /* if_addrhead */
576 TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
578 LIST_REMOVE(ia, ia_hash);
581 * If this is the last IPv4 address configured on this
582 * interface, leave the all-hosts group.
583 * No state-change report need be transmitted.
587 ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
589 if (ii->ii_allhosts) {
590 (void)in_leavegroup_locked(ii->ii_allhosts, NULL);
591 ii->ii_allhosts = NULL;
595 ifa_free(&iap->ia_ifa);
597 ifa_free(&ia->ia_ifa); /* in_ifaddrhead */
600 ifa_free(&ia->ia_ifa);
606 * SIOCGLIFADDR: get first address. (?!?)
607 * SIOCGLIFADDR with IFLR_PREFIX:
608 * get first address that matches the specified prefix.
609 * SIOCALIFADDR: add the specified address.
610 * SIOCALIFADDR with IFLR_PREFIX:
611 * EINVAL since we can't deduce hostid part of the address.
612 * SIOCDLIFADDR: delete the specified address.
613 * SIOCDLIFADDR with IFLR_PREFIX:
614 * delete the first address that matches the specified prefix.
616 * EINVAL on invalid parameters
617 * EADDRNOTAVAIL on prefix match failed/specified address not found
618 * other values may be returned from in_ioctl()
621 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
622 struct ifnet *ifp, struct thread *td)
624 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
628 if (data == NULL || ifp == NULL) {
629 panic("invalid argument to in_lifaddr_ioctl");
635 /* address must be specified on GET with IFLR_PREFIX */
636 if ((iflr->flags & IFLR_PREFIX) == 0)
641 /* address must be specified on ADD and DELETE */
642 if (iflr->addr.ss_family != AF_INET)
644 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
646 /* XXX need improvement */
647 if (iflr->dstaddr.ss_family
648 && iflr->dstaddr.ss_family != AF_INET)
650 if (iflr->dstaddr.ss_family
651 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
654 default: /*shouldn't happen*/
657 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
663 struct in_aliasreq ifra;
665 if (iflr->flags & IFLR_PREFIX)
668 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
669 bzero(&ifra, sizeof(ifra));
670 bcopy(iflr->iflr_name, ifra.ifra_name,
671 sizeof(ifra.ifra_name));
673 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
675 if (iflr->dstaddr.ss_family) { /*XXX*/
676 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
677 iflr->dstaddr.ss_len);
680 ifra.ifra_mask.sin_family = AF_INET;
681 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
682 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
684 return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td));
689 struct in_ifaddr *ia;
690 struct in_addr mask, candidate, match;
691 struct sockaddr_in *sin;
693 bzero(&mask, sizeof(mask));
694 bzero(&match, sizeof(match));
695 if (iflr->flags & IFLR_PREFIX) {
696 /* lookup a prefix rather than address. */
697 in_len2mask(&mask, iflr->prefixlen);
699 sin = (struct sockaddr_in *)&iflr->addr;
700 match.s_addr = sin->sin_addr.s_addr;
701 match.s_addr &= mask.s_addr;
703 /* if you set extra bits, that's wrong */
704 if (match.s_addr != sin->sin_addr.s_addr)
708 /* on getting an address, take the 1st match */
709 /* on deleting an address, do exact match */
710 if (cmd != SIOCGLIFADDR) {
711 in_len2mask(&mask, 32);
712 sin = (struct sockaddr_in *)&iflr->addr;
713 match.s_addr = sin->sin_addr.s_addr;
718 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
719 if (ifa->ifa_addr->sa_family != AF_INET)
721 if (match.s_addr == 0)
723 sin = (struct sockaddr_in *)&ifa->ifa_addr;
724 candidate.s_addr = sin->sin_addr.s_addr;
725 candidate.s_addr &= mask.s_addr;
726 if (candidate.s_addr == match.s_addr)
731 IF_ADDR_RUNLOCK(ifp);
733 return (EADDRNOTAVAIL);
734 ia = (struct in_ifaddr *)ifa;
736 if (cmd == SIOCGLIFADDR) {
737 /* fill in the if_laddrreq structure */
738 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
740 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
741 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
742 ia->ia_dstaddr.sin_len);
744 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
747 in_mask2len(&ia->ia_sockmask.sin_addr);
749 iflr->flags = 0; /*XXX*/
754 struct in_aliasreq ifra;
756 /* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
757 bzero(&ifra, sizeof(ifra));
758 bcopy(iflr->iflr_name, ifra.ifra_name,
759 sizeof(ifra.ifra_name));
761 bcopy(&ia->ia_addr, &ifra.ifra_addr,
762 ia->ia_addr.sin_len);
763 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
764 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
765 ia->ia_dstaddr.sin_len);
767 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
768 ia->ia_sockmask.sin_len);
771 return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
777 return (EOPNOTSUPP); /*just for safety*/
781 * Delete any existing route for an interface.
784 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia, u_int flags)
787 in_scrubprefix(ia, flags);
791 * Initialize an interface's internet address
792 * and routing table entry.
795 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
796 int masksupplied, int vhid)
798 register u_long i = ntohl(sin->sin_addr.s_addr);
799 int flags, error = 0;
802 if (ia->ia_addr.sin_family == AF_INET)
803 LIST_REMOVE(ia, ia_hash);
805 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
810 if (carp_attach_p != NULL)
811 error = (*carp_attach_p)(&ia->ia_ifa, vhid);
813 error = EPROTONOSUPPORT;
819 * Give the interface a chance to initialize
820 * if this is its first address,
821 * and to validate the address if necessary.
823 if (ifp->if_ioctl != NULL &&
824 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)) != 0)
825 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
829 * Be compatible with network classes, if netmask isn't supplied,
830 * guess it based on classes.
834 ia->ia_subnetmask = IN_CLASSA_NET;
835 else if (IN_CLASSB(i))
836 ia->ia_subnetmask = IN_CLASSB_NET;
838 ia->ia_subnetmask = IN_CLASSC_NET;
839 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
841 ia->ia_subnet = i & ia->ia_subnetmask;
842 in_socktrim(&ia->ia_sockmask);
845 * Add route for the network.
848 ia->ia_ifa.ifa_metric = ifp->if_metric;
849 if (ifp->if_flags & IFF_BROADCAST) {
850 if (ia->ia_subnetmask == IN_RFC3021_MASK)
851 ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
853 ia->ia_broadaddr.sin_addr.s_addr =
854 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
855 } else if (ifp->if_flags & IFF_LOOPBACK) {
856 ia->ia_dstaddr = ia->ia_addr;
858 } else if (ifp->if_flags & IFF_POINTOPOINT) {
859 if (ia->ia_dstaddr.sin_family != AF_INET)
863 if (!vhid && (error = in_addprefix(ia, flags)) != 0)
866 if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY)
869 if (ifp->if_flags & IFF_POINTOPOINT &&
870 ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
874 * add a loopback route to self
876 if (V_useloopback && !vhid && !(ifp->if_flags & IFF_LOOPBACK)) {
879 bzero(&ia_ro, sizeof(ia_ro));
880 *((struct sockaddr_in *)(&ia_ro.ro_dst)) = ia->ia_addr;
881 rtalloc_ign_fib(&ia_ro, 0, RT_DEFAULT_FIB);
882 if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
883 (ia_ro.ro_rt->rt_ifp == V_loif)) {
884 RT_LOCK(ia_ro.ro_rt);
885 RT_ADDREF(ia_ro.ro_rt);
886 RTFREE_LOCKED(ia_ro.ro_rt);
888 error = ifa_add_loopback_route((struct ifaddr *)ia,
889 (struct sockaddr *)&ia->ia_addr);
891 ia->ia_flags |= IFA_RTSELF;
892 if (ia_ro.ro_rt != NULL)
899 #define rtinitflags(x) \
900 ((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
904 * Check if we have a route for the given prefix already or add one accordingly.
907 in_addprefix(struct in_ifaddr *target, int flags)
909 struct in_ifaddr *ia;
910 struct in_addr prefix, mask, p, m;
913 if ((flags & RTF_HOST) != 0) {
914 prefix = target->ia_dstaddr.sin_addr;
917 prefix = target->ia_addr.sin_addr;
918 mask = target->ia_sockmask.sin_addr;
919 prefix.s_addr &= mask.s_addr;
923 /* Look for an existing address with the same prefix, mask, and fib */
924 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
925 if (rtinitflags(ia)) {
926 p = ia->ia_dstaddr.sin_addr;
928 if (prefix.s_addr != p.s_addr)
931 p = ia->ia_addr.sin_addr;
932 m = ia->ia_sockmask.sin_addr;
933 p.s_addr &= m.s_addr;
935 if (prefix.s_addr != p.s_addr ||
936 mask.s_addr != m.s_addr)
939 if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
943 * If we got a matching prefix route inserted by other
944 * interface address, we are done here.
946 if (ia->ia_flags & IFA_ROUTE) {
948 if (ia->ia_addr.sin_addr.s_addr ==
949 target->ia_addr.sin_addr.s_addr) {
955 if (V_nosameprefix) {
961 fibnum = rt_add_addr_allfibs ? RT_ALL_FIBS :
962 target->ia_ifp->if_fib;
963 rt_addrmsg(RTM_ADD, &target->ia_ifa, fibnum);
972 * No-one seem to have this prefix route, so we try to insert it.
974 error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
976 target->ia_flags |= IFA_ROUTE;
981 * If there is no other address in the system that can serve a route to the
982 * same prefix, remove the route. Hand over the route to the new address
986 in_scrubprefix(struct in_ifaddr *target, u_int flags)
988 struct in_ifaddr *ia;
989 struct in_addr prefix, mask, p, m;
991 struct sockaddr_in prefix0, mask0;
994 * Remove the loopback route to the interface address.
995 * The "useloopback" setting is not consulted because if the
996 * user configures an interface address, turns off this
997 * setting, and then tries to delete that interface address,
998 * checking the current setting of "useloopback" would leave
999 * that interface address loopback route untouched, which
1000 * would be wrong. Therefore the interface address loopback route
1001 * deletion is unconditional.
1003 if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
1004 !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
1005 (target->ia_flags & IFA_RTSELF)) {
1010 bzero(&ia_ro, sizeof(ia_ro));
1011 *((struct sockaddr_in *)(&ia_ro.ro_dst)) = target->ia_addr;
1012 fib = target->ia_ifa.ifa_ifp->if_fib;
1013 rtalloc_ign_fib(&ia_ro, 0, fib);
1014 if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
1015 (ia_ro.ro_rt->rt_ifp == V_loif)) {
1016 RT_LOCK(ia_ro.ro_rt);
1017 if (ia_ro.ro_rt->rt_refcnt <= 1)
1019 else if (flags & LLE_STATIC) {
1020 RT_REMREF(ia_ro.ro_rt);
1021 target->ia_flags &= ~IFA_RTSELF;
1023 RTFREE_LOCKED(ia_ro.ro_rt);
1025 if (freeit && (flags & LLE_STATIC)) {
1026 error = ifa_del_loopback_route((struct ifaddr *)target,
1027 (struct sockaddr *)&target->ia_addr);
1029 target->ia_flags &= ~IFA_RTSELF;
1031 if ((flags & LLE_STATIC) &&
1032 !(target->ia_ifp->if_flags & IFF_NOARP))
1033 /* remove arp cache */
1034 arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr);
1037 if (rtinitflags(target)) {
1038 prefix = target->ia_dstaddr.sin_addr;
1041 prefix = target->ia_addr.sin_addr;
1042 mask = target->ia_sockmask.sin_addr;
1043 prefix.s_addr &= mask.s_addr;
1046 if ((target->ia_flags & IFA_ROUTE) == 0) {
1049 fibnum = rt_add_addr_allfibs ? RT_ALL_FIBS :
1050 target->ia_ifp->if_fib;
1051 rt_addrmsg(RTM_DELETE, &target->ia_ifa, fibnum);
1056 TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1057 if (rtinitflags(ia)) {
1058 p = ia->ia_dstaddr.sin_addr;
1060 if (prefix.s_addr != p.s_addr)
1063 p = ia->ia_addr.sin_addr;
1064 m = ia->ia_sockmask.sin_addr;
1065 p.s_addr &= m.s_addr;
1067 if (prefix.s_addr != p.s_addr ||
1068 mask.s_addr != m.s_addr)
1072 if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1076 * If we got a matching prefix address, move IFA_ROUTE and
1077 * the route itself to it. Make sure that routing daemons
1080 if ((ia->ia_flags & IFA_ROUTE) == 0) {
1081 ifa_ref(&ia->ia_ifa);
1082 IN_IFADDR_RUNLOCK();
1083 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
1084 rtinitflags(target));
1086 target->ia_flags &= ~IFA_ROUTE;
1088 log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
1090 error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
1091 rtinitflags(ia) | RTF_UP);
1093 ia->ia_flags |= IFA_ROUTE;
1095 log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
1097 ifa_free(&ia->ia_ifa);
1101 IN_IFADDR_RUNLOCK();
1104 * remove all L2 entries on the given prefix
1106 bzero(&prefix0, sizeof(prefix0));
1107 prefix0.sin_len = sizeof(prefix0);
1108 prefix0.sin_family = AF_INET;
1109 prefix0.sin_addr.s_addr = target->ia_subnet;
1110 bzero(&mask0, sizeof(mask0));
1111 mask0.sin_len = sizeof(mask0);
1112 mask0.sin_family = AF_INET;
1113 mask0.sin_addr.s_addr = target->ia_subnetmask;
1114 lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0,
1115 (struct sockaddr *)&mask0, flags);
1118 * As no-one seem to have this prefix, we can remove the route.
1120 error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
1122 target->ia_flags &= ~IFA_ROUTE;
1124 log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
1131 * Return 1 if the address might be a local broadcast address.
1134 in_broadcast(struct in_addr in, struct ifnet *ifp)
1136 register struct ifaddr *ifa;
1139 if (in.s_addr == INADDR_BROADCAST ||
1140 in.s_addr == INADDR_ANY)
1142 if ((ifp->if_flags & IFF_BROADCAST) == 0)
1144 t = ntohl(in.s_addr);
1146 * Look through the list of addresses for a match
1147 * with a broadcast address.
1149 #define ia ((struct in_ifaddr *)ifa)
1150 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1151 if (ifa->ifa_addr->sa_family == AF_INET &&
1152 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1154 * Check for old-style (host 0) broadcast, but
1155 * taking into account that RFC 3021 obsoletes it.
1157 (ia->ia_subnetmask != IN_RFC3021_MASK &&
1158 t == ia->ia_subnet)) &&
1160 * Check for an all one subnetmask. These
1161 * only exist when an interface gets a secondary
1164 ia->ia_subnetmask != (u_long)0xffffffff)
1171 * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1174 in_ifdetach(struct ifnet *ifp)
1177 in_pcbpurgeif0(&V_ripcbinfo, ifp);
1178 in_pcbpurgeif0(&V_udbinfo, ifp);
1179 in_pcbpurgeif0(&V_ulitecbinfo, ifp);
1180 in_purgemaddrs(ifp);
1184 * Delete all IPv4 multicast address records, and associated link-layer
1185 * multicast address records, associated with ifp.
1186 * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1187 * XXX This should not race with ifma_protospec being set during
1188 * a new allocation, if it does, we have bigger problems.
1191 in_purgemaddrs(struct ifnet *ifp)
1193 LIST_HEAD(,in_multi) purgeinms;
1194 struct in_multi *inm, *tinm;
1195 struct ifmultiaddr *ifma;
1197 LIST_INIT(&purgeinms);
1201 * Extract list of in_multi associated with the detaching ifp
1202 * which the PF_INET layer is about to release.
1203 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1204 * by code further down.
1207 TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1208 if (ifma->ifma_addr->sa_family != AF_INET ||
1209 ifma->ifma_protospec == NULL)
1212 KASSERT(ifma->ifma_protospec != NULL,
1213 ("%s: ifma_protospec is NULL", __func__));
1215 inm = (struct in_multi *)ifma->ifma_protospec;
1216 LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
1218 IF_ADDR_RUNLOCK(ifp);
1220 LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
1221 LIST_REMOVE(inm, inm_link);
1222 inm_release_locked(inm);
1230 struct llentry base;
1231 struct sockaddr_in l3_addr4;
1235 * Deletes an address from the address table.
1236 * This function is called by the timer functions
1237 * such as arptimer() and nd6_llinfo_timer(), and
1238 * the caller does the locking.
1241 in_lltable_free(struct lltable *llt, struct llentry *lle)
1244 LLE_LOCK_DESTROY(lle);
1245 free(lle, M_LLTABLE);
1248 static struct llentry *
1249 in_lltable_new(const struct sockaddr *l3addr, u_int flags)
1251 struct in_llentry *lle;
1253 lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
1254 if (lle == NULL) /* NB: caller generates msg */
1258 * For IPv4 this will trigger "arpresolve" to generate
1261 lle->base.la_expire = time_uptime; /* mark expired */
1262 lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
1263 lle->base.lle_refcnt = 1;
1264 lle->base.lle_free = in_lltable_free;
1265 LLE_LOCK_INIT(&lle->base);
1266 callout_init(&lle->base.la_timer, 1);
1268 return (&lle->base);
1271 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m) ( \
1272 (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
1275 in_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix,
1276 const struct sockaddr *mask, u_int flags)
1278 const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
1279 const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
1280 struct llentry *lle, *next;
1282 size_t pkts_dropped;
1284 IF_AFDATA_WLOCK(llt->llt_ifp);
1285 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
1286 LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
1288 * (flags & LLE_STATIC) means deleting all entries
1289 * including static ARP entries.
1291 if (IN_ARE_MASKED_ADDR_EQUAL(satosin(L3_ADDR(lle)),
1292 pfx, msk) && ((flags & LLE_STATIC) ||
1293 !(lle->la_flags & LLE_STATIC))) {
1295 if (callout_stop(&lle->la_timer))
1297 pkts_dropped = llentry_free(lle);
1298 ARPSTAT_ADD(dropped, pkts_dropped);
1302 IF_AFDATA_WUNLOCK(llt->llt_ifp);
1307 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1311 KASSERT(l3addr->sa_family == AF_INET,
1312 ("sin_family %d", l3addr->sa_family));
1314 /* XXX rtalloc1_fib should take a const param */
1315 rt = rtalloc1_fib(__DECONST(struct sockaddr *, l3addr), 0, 0,
1322 * If the gateway for an existing host route matches the target L3
1323 * address, which is a special route inserted by some implementation
1324 * such as MANET, and the interface is of the correct type, then
1325 * allow for ARP to proceed.
1327 if (rt->rt_flags & RTF_GATEWAY) {
1328 if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp ||
1329 rt->rt_ifp->if_type != IFT_ETHER ||
1330 (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1331 memcmp(rt->rt_gateway->sa_data, l3addr->sa_data,
1332 sizeof(in_addr_t)) != 0) {
1339 * Make sure that at least the destination address is covered
1340 * by the route. This is for handling the case where 2 or more
1341 * interfaces have the same prefix. An incoming packet arrives
1342 * on one interface and the corresponding outgoing packet leaves
1343 * another interface.
1345 if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) {
1346 const char *sa, *mask, *addr, *lim;
1349 mask = (const char *)rt_mask(rt);
1351 * Just being extra cautious to avoid some custom
1352 * code getting into trouble.
1359 sa = (const char *)rt_key(rt);
1360 addr = (const char *)l3addr;
1361 len = ((const struct sockaddr_in *)l3addr)->sin_len;
1364 for ( ; addr < lim; sa++, mask++, addr++) {
1365 if ((*sa ^ *addr) & *mask) {
1367 log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
1368 inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
1381 * Return NULL if not found or marked for deletion.
1382 * If found return lle read locked.
1384 static struct llentry *
1385 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1387 const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1388 struct ifnet *ifp = llt->llt_ifp;
1389 struct llentry *lle;
1390 struct llentries *lleh;
1393 IF_AFDATA_LOCK_ASSERT(ifp);
1394 KASSERT(l3addr->sa_family == AF_INET,
1395 ("sin_family %d", l3addr->sa_family));
1397 hashkey = sin->sin_addr.s_addr;
1398 lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
1399 LIST_FOREACH(lle, lleh, lle_next) {
1400 struct sockaddr_in *sa2 = satosin(L3_ADDR(lle));
1401 if (lle->la_flags & LLE_DELETED)
1403 if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
1408 if (flags & LLE_DELETE)
1409 log(LOG_INFO, "interface address is missing from cache = %p in delete\n", lle);
1411 if (!(flags & LLE_CREATE))
1413 IF_AFDATA_WLOCK_ASSERT(ifp);
1415 * A route that covers the given address must have
1416 * been installed 1st because we are doing a resolution,
1419 if (!(flags & LLE_IFADDR) &&
1420 in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1423 lle = in_lltable_new(l3addr, flags);
1425 log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1428 lle->la_flags = flags & ~LLE_CREATE;
1429 if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
1430 bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
1431 lle->la_flags |= (LLE_VALID | LLE_STATIC);
1435 lle->lle_head = lleh;
1436 lle->la_flags |= LLE_LINKED;
1437 LIST_INSERT_HEAD(lleh, lle, lle_next);
1438 } else if (flags & LLE_DELETE) {
1439 if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
1441 lle->la_flags |= LLE_DELETED;
1442 EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1444 log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1446 if ((lle->la_flags &
1447 (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
1455 if (LLE_IS_VALID(lle)) {
1456 if (flags & LLE_EXCLUSIVE)
1466 in_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
1468 #define SIN(lle) ((struct sockaddr_in *) L3_ADDR(lle))
1469 struct ifnet *ifp = llt->llt_ifp;
1470 struct llentry *lle;
1473 struct rt_msghdr rtm;
1474 struct sockaddr_in sin;
1475 struct sockaddr_dl sdl;
1479 LLTABLE_LOCK_ASSERT();
1482 for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
1483 LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
1484 struct sockaddr_dl *sdl;
1486 /* skip deleted entries */
1487 if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1489 /* Skip if jailed and not a valid IP of the prison. */
1490 if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
1493 * produce a msg made of:
1495 * struct sockaddr_in; (IPv4)
1496 * struct sockaddr_dl;
1498 bzero(&arpc, sizeof(arpc));
1499 arpc.rtm.rtm_msglen = sizeof(arpc);
1500 arpc.rtm.rtm_version = RTM_VERSION;
1501 arpc.rtm.rtm_type = RTM_GET;
1502 arpc.rtm.rtm_flags = RTF_UP;
1503 arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1504 arpc.sin.sin_family = AF_INET;
1505 arpc.sin.sin_len = sizeof(arpc.sin);
1506 arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
1509 if (lle->la_flags & LLE_PUB)
1510 arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1513 sdl->sdl_family = AF_LINK;
1514 sdl->sdl_len = sizeof(*sdl);
1515 sdl->sdl_index = ifp->if_index;
1516 sdl->sdl_type = ifp->if_type;
1517 if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1518 sdl->sdl_alen = ifp->if_addrlen;
1519 bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1522 bzero(LLADDR(sdl), ifp->if_addrlen);
1525 arpc.rtm.rtm_rmx.rmx_expire =
1526 lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1527 arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1528 if (lle->la_flags & LLE_STATIC)
1529 arpc.rtm.rtm_flags |= RTF_STATIC;
1530 arpc.rtm.rtm_index = ifp->if_index;
1531 error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1541 in_domifattach(struct ifnet *ifp)
1543 struct in_ifinfo *ii;
1544 struct lltable *llt;
1546 ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1548 llt = lltable_init(ifp, AF_INET);
1550 llt->llt_prefix_free = in_lltable_prefix_free;
1551 llt->llt_lookup = in_lltable_lookup;
1552 llt->llt_dump = in_lltable_dump;
1556 ii->ii_igmp = igmp_domifattach(ifp);
1562 in_domifdetach(struct ifnet *ifp, void *aux)
1564 struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1566 igmp_domifdetach(ifp);
1567 lltable_free(ii->ii_llt);