Home | History | Annotate | Line # | Download | only in netinet
in.c revision 1.160
      1 /*	$NetBSD: in.c,v 1.160 2015/08/31 09:21:55 ozaki-r Exp $	*/
      2 
      3 /*
      4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the project nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1998 The NetBSD Foundation, Inc.
     34  * All rights reserved.
     35  *
     36  * This code is derived from software contributed to The NetBSD Foundation
     37  * by Public Access Networks Corporation ("Panix").  It was developed under
     38  * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
     39  *
     40  * Redistribution and use in source and binary forms, with or without
     41  * modification, are permitted provided that the following conditions
     42  * are met:
     43  * 1. Redistributions of source code must retain the above copyright
     44  *    notice, this list of conditions and the following disclaimer.
     45  * 2. Redistributions in binary form must reproduce the above copyright
     46  *    notice, this list of conditions and the following disclaimer in the
     47  *    documentation and/or other materials provided with the distribution.
     48  *
     49  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     50  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     51  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     52  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     53  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     54  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     55  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     56  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     57  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     58  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     59  * POSSIBILITY OF SUCH DAMAGE.
     60  */
     61 
     62 /*
     63  * Copyright (c) 1982, 1986, 1991, 1993
     64  *	The Regents of the University of California.  All rights reserved.
     65  *
     66  * Redistribution and use in source and binary forms, with or without
     67  * modification, are permitted provided that the following conditions
     68  * are met:
     69  * 1. Redistributions of source code must retain the above copyright
     70  *    notice, this list of conditions and the following disclaimer.
     71  * 2. Redistributions in binary form must reproduce the above copyright
     72  *    notice, this list of conditions and the following disclaimer in the
     73  *    documentation and/or other materials provided with the distribution.
     74  * 3. Neither the name of the University nor the names of its contributors
     75  *    may be used to endorse or promote products derived from this software
     76  *    without specific prior written permission.
     77  *
     78  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     79  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     80  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     81  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     82  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     83  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     84  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     85  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     86  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     87  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     88  * SUCH DAMAGE.
     89  *
     90  *	@(#)in.c	8.4 (Berkeley) 1/9/95
     91  */
     92 
     93 #include <sys/cdefs.h>
     94 __KERNEL_RCSID(0, "$NetBSD: in.c,v 1.160 2015/08/31 09:21:55 ozaki-r Exp $");
     95 
     96 #include "arp.h"
     97 
     98 #ifdef _KERNEL_OPT
     99 #include "opt_inet.h"
    100 #include "opt_inet_conf.h"
    101 #include "opt_mrouting.h"
    102 #endif
    103 
    104 #include <sys/param.h>
    105 #include <sys/ioctl.h>
    106 #include <sys/errno.h>
    107 #include <sys/kernel.h>
    108 #include <sys/malloc.h>
    109 #include <sys/socket.h>
    110 #include <sys/socketvar.h>
    111 #include <sys/sysctl.h>
    112 #include <sys/systm.h>
    113 #include <sys/proc.h>
    114 #include <sys/syslog.h>
    115 #include <sys/kauth.h>
    116 #include <sys/kmem.h>
    117 
    118 #include <sys/cprng.h>
    119 
    120 #include <net/if.h>
    121 #include <net/route.h>
    122 #include <net/pfil.h>
    123 
    124 #include <net/if_arp.h>
    125 #include <net/if_ether.h>
    126 #include <net/if_types.h>
    127 #include <net/if_llatbl.h>
    128 #include <net/if_dl.h>
    129 
    130 #include <netinet/in_systm.h>
    131 #include <netinet/in.h>
    132 #include <netinet/in_var.h>
    133 #include <netinet/ip.h>
    134 #include <netinet/ip_var.h>
    135 #include <netinet/in_ifattach.h>
    136 #include <netinet/in_pcb.h>
    137 #include <netinet/in_selsrc.h>
    138 #include <netinet/if_inarp.h>
    139 #include <netinet/ip_mroute.h>
    140 #include <netinet/igmp_var.h>
    141 
    142 #ifdef IPSELSRC
    143 #include <netinet/in_selsrc.h>
    144 #endif
    145 
    146 static u_int	in_mask2len(struct in_addr *);
    147 static void	in_len2mask(struct in_addr *, u_int);
    148 static int	in_lifaddr_ioctl(struct socket *, u_long, void *,
    149 	struct ifnet *);
    150 
    151 static int	in_addprefix(struct in_ifaddr *, int);
    152 static int	in_scrubprefix(struct in_ifaddr *);
    153 static void	in_sysctl_init(struct sysctllog **);
    154 
    155 #ifndef SUBNETSARELOCAL
    156 #define	SUBNETSARELOCAL	1
    157 #endif
    158 
    159 #ifndef HOSTZEROBROADCAST
    160 #define HOSTZEROBROADCAST 1
    161 #endif
    162 
    163 /* Note: 61, 127, 251, 509, 1021, 2039 are good. */
    164 #ifndef IN_MULTI_HASH_SIZE
    165 #define IN_MULTI_HASH_SIZE	509
    166 #endif
    167 
    168 static int			subnetsarelocal = SUBNETSARELOCAL;
    169 static int			hostzeroisbroadcast = HOSTZEROBROADCAST;
    170 
    171 /*
    172  * This list is used to keep track of in_multi chains which belong to
    173  * deleted interface addresses.  We use in_ifaddr so that a chain head
    174  * won't be deallocated until all multicast address record are deleted.
    175  */
    176 
    177 LIST_HEAD(in_multihashhead, in_multi);		/* Type of the hash head */
    178 
    179 static struct pool		inmulti_pool;
    180 static u_int			in_multientries;
    181 static struct in_multihashhead *in_multihashtbl;
    182 static u_long			in_multihash;
    183 static krwlock_t		in_multilock;
    184 
    185 #define IN_MULTI_HASH(x, ifp) \
    186     (in_multihashtbl[(u_long)((x) ^ (ifp->if_index)) % IN_MULTI_HASH_SIZE])
    187 
    188 struct in_ifaddrhashhead *	in_ifaddrhashtbl;
    189 u_long				in_ifaddrhash;
    190 struct in_ifaddrhead		in_ifaddrhead;
    191 
    192 void
    193 in_init(void)
    194 {
    195 	pool_init(&inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl",
    196 	    NULL, IPL_SOFTNET);
    197 	TAILQ_INIT(&in_ifaddrhead);
    198 
    199 	in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
    200 	    &in_ifaddrhash);
    201 	in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
    202 	    &in_multihash);
    203 	rw_init(&in_multilock);
    204 
    205 	in_sysctl_init(NULL);
    206 }
    207 
    208 /*
    209  * Return 1 if an internet address is for a ``local'' host
    210  * (one to which we have a connection).  If subnetsarelocal
    211  * is true, this includes other subnets of the local net.
    212  * Otherwise, it includes only the directly-connected (sub)nets.
    213  */
    214 int
    215 in_localaddr(struct in_addr in)
    216 {
    217 	struct in_ifaddr *ia;
    218 
    219 	if (subnetsarelocal) {
    220 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list)
    221 			if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
    222 				return (1);
    223 	} else {
    224 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list)
    225 			if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
    226 				return (1);
    227 	}
    228 	return (0);
    229 }
    230 
    231 /*
    232  * Determine whether an IP address is in a reserved set of addresses
    233  * that may not be forwarded, or whether datagrams to that destination
    234  * may be forwarded.
    235  */
    236 int
    237 in_canforward(struct in_addr in)
    238 {
    239 	u_int32_t net;
    240 
    241 	if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
    242 		return (0);
    243 	if (IN_CLASSA(in.s_addr)) {
    244 		net = in.s_addr & IN_CLASSA_NET;
    245 		if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
    246 			return (0);
    247 	}
    248 	return (1);
    249 }
    250 
    251 /*
    252  * Trim a mask in a sockaddr
    253  */
    254 void
    255 in_socktrim(struct sockaddr_in *ap)
    256 {
    257 	char *cplim = (char *) &ap->sin_addr;
    258 	char *cp = (char *) (&ap->sin_addr + 1);
    259 
    260 	ap->sin_len = 0;
    261 	while (--cp >= cplim)
    262 		if (*cp) {
    263 			(ap)->sin_len = cp - (char *) (ap) + 1;
    264 			break;
    265 		}
    266 }
    267 
    268 /*
    269  *  Routine to take an Internet address and convert into a
    270  *  "dotted quad" representation for printing.
    271  */
    272 const char *
    273 in_fmtaddr(struct in_addr addr)
    274 {
    275 	static char buf[sizeof("123.456.789.123")];
    276 
    277 	addr.s_addr = ntohl(addr.s_addr);
    278 
    279 	snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
    280 		(addr.s_addr >> 24) & 0xFF,
    281 		(addr.s_addr >> 16) & 0xFF,
    282 		(addr.s_addr >>  8) & 0xFF,
    283 		(addr.s_addr >>  0) & 0xFF);
    284 	return buf;
    285 }
    286 
    287 /*
    288  * Maintain the "in_maxmtu" variable, which is the largest
    289  * mtu for non-local interfaces with AF_INET addresses assigned
    290  * to them that are up.
    291  */
    292 unsigned long in_maxmtu;
    293 
    294 void
    295 in_setmaxmtu(void)
    296 {
    297 	struct in_ifaddr *ia;
    298 	struct ifnet *ifp;
    299 	unsigned long maxmtu = 0;
    300 
    301 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
    302 		if ((ifp = ia->ia_ifp) == 0)
    303 			continue;
    304 		if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
    305 			continue;
    306 		if (ifp->if_mtu > maxmtu)
    307 			maxmtu = ifp->if_mtu;
    308 	}
    309 	if (maxmtu)
    310 		in_maxmtu = maxmtu;
    311 }
    312 
    313 static u_int
    314 in_mask2len(struct in_addr *mask)
    315 {
    316 	u_int x, y;
    317 	u_char *p;
    318 
    319 	p = (u_char *)mask;
    320 	for (x = 0; x < sizeof(*mask); x++) {
    321 		if (p[x] != 0xff)
    322 			break;
    323 	}
    324 	y = 0;
    325 	if (x < sizeof(*mask)) {
    326 		for (y = 0; y < NBBY; y++) {
    327 			if ((p[x] & (0x80 >> y)) == 0)
    328 				break;
    329 		}
    330 	}
    331 	return x * NBBY + y;
    332 }
    333 
    334 static void
    335 in_len2mask(struct in_addr *mask, u_int len)
    336 {
    337 	u_int i;
    338 	u_char *p;
    339 
    340 	p = (u_char *)mask;
    341 	memset(mask, 0, sizeof(*mask));
    342 	for (i = 0; i < len / NBBY; i++)
    343 		p[i] = 0xff;
    344 	if (len % NBBY)
    345 		p[i] = (0xff00 >> (len % NBBY)) & 0xff;
    346 }
    347 
    348 /*
    349  * Generic internet control operations (ioctl's).
    350  * Ifp is 0 if not an interface-specific ioctl.
    351  */
    352 /* ARGSUSED */
    353 int
    354 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
    355 {
    356 	struct ifreq *ifr = (struct ifreq *)data;
    357 	struct in_ifaddr *ia = NULL;
    358 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
    359 	struct sockaddr_in oldaddr;
    360 	int error, hostIsNew, maskIsNew;
    361 	int newifaddr = 0;
    362 
    363 	switch (cmd) {
    364 	case SIOCALIFADDR:
    365 	case SIOCDLIFADDR:
    366 	case SIOCGLIFADDR:
    367 		if (ifp == NULL)
    368 			return EINVAL;
    369 		return in_lifaddr_ioctl(so, cmd, data, ifp);
    370 	case SIOCGIFADDRPREF:
    371 	case SIOCSIFADDRPREF:
    372 		if (ifp == NULL)
    373 			return EINVAL;
    374 		return ifaddrpref_ioctl(so, cmd, data, ifp);
    375 	}
    376 
    377 	/*
    378 	 * Find address for this interface, if it exists.
    379 	 */
    380 	if (ifp != NULL)
    381 		IFP_TO_IA(ifp, ia);
    382 
    383 	hostIsNew = 1;		/* moved here to appease gcc */
    384 	switch (cmd) {
    385 	case SIOCAIFADDR:
    386 	case SIOCDIFADDR:
    387 	case SIOCGIFALIAS:
    388 	case SIOCGIFAFLAG_IN:
    389 		if (ifra->ifra_addr.sin_family == AF_INET)
    390 			LIST_FOREACH(ia,
    391 			    &IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr),
    392 			    ia_hash) {
    393 				if (ia->ia_ifp == ifp &&
    394 				    in_hosteq(ia->ia_addr.sin_addr,
    395 				    ifra->ifra_addr.sin_addr))
    396 					break;
    397 			}
    398 		if ((cmd == SIOCDIFADDR ||
    399 		    cmd == SIOCGIFALIAS ||
    400 		    cmd == SIOCGIFAFLAG_IN) &&
    401 		    ia == NULL)
    402 			return (EADDRNOTAVAIL);
    403 
    404 		if (cmd == SIOCDIFADDR &&
    405 		    ifra->ifra_addr.sin_family == AF_UNSPEC) {
    406 			ifra->ifra_addr.sin_family = AF_INET;
    407 		}
    408 		/* FALLTHROUGH */
    409 	case SIOCSIFADDR:
    410 		if (ia == NULL || ia->ia_addr.sin_family != AF_INET)
    411 			;
    412 		else if (ifra->ifra_addr.sin_len == 0) {
    413 			ifra->ifra_addr = ia->ia_addr;
    414 			hostIsNew = 0;
    415 		} else if (in_hosteq(ia->ia_addr.sin_addr,
    416 		           ifra->ifra_addr.sin_addr))
    417 			hostIsNew = 0;
    418 		/* FALLTHROUGH */
    419 	case SIOCSIFDSTADDR:
    420 		if (ifra->ifra_addr.sin_family != AF_INET)
    421 			return (EAFNOSUPPORT);
    422 		/* FALLTHROUGH */
    423 	case SIOCSIFNETMASK:
    424 		if (ifp == NULL)
    425 			panic("in_control");
    426 
    427 		if (cmd == SIOCGIFALIAS || cmd == SIOCGIFAFLAG_IN)
    428 			break;
    429 
    430 		if (ia == NULL &&
    431 		    (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR))
    432 			return (EADDRNOTAVAIL);
    433 
    434 		if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
    435 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
    436 		    NULL) != 0)
    437 			return (EPERM);
    438 
    439 		if (ia == NULL) {
    440 			ia = malloc(sizeof(*ia), M_IFADDR, M_WAITOK|M_ZERO);
    441 			if (ia == NULL)
    442 				return (ENOBUFS);
    443 			TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list);
    444 			ifaref(&ia->ia_ifa);
    445 			ifa_insert(ifp, &ia->ia_ifa);
    446 			ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    447 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    448 			ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
    449 #ifdef IPSELSRC
    450 			ia->ia_ifa.ifa_getifa = in_getifa;
    451 #else /* IPSELSRC */
    452 			ia->ia_ifa.ifa_getifa = NULL;
    453 #endif /* IPSELSRC */
    454 			ia->ia_sockmask.sin_len = 8;
    455 			ia->ia_sockmask.sin_family = AF_INET;
    456 			if (ifp->if_flags & IFF_BROADCAST) {
    457 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
    458 				ia->ia_broadaddr.sin_family = AF_INET;
    459 			}
    460 			ia->ia_ifp = ifp;
    461 			ia->ia_idsalt = cprng_fast32() % 65535;
    462 			LIST_INIT(&ia->ia_multiaddrs);
    463 			newifaddr = 1;
    464 		}
    465 		break;
    466 
    467 	case SIOCSIFBRDADDR:
    468 		if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
    469 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
    470 		    NULL) != 0)
    471 			return (EPERM);
    472 		/* FALLTHROUGH */
    473 
    474 	case SIOCGIFADDR:
    475 	case SIOCGIFNETMASK:
    476 	case SIOCGIFDSTADDR:
    477 	case SIOCGIFBRDADDR:
    478 		if (ia == NULL)
    479 			return (EADDRNOTAVAIL);
    480 		break;
    481 	}
    482 	error = 0;
    483 	switch (cmd) {
    484 
    485 	case SIOCGIFADDR:
    486 		ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr));
    487 		break;
    488 
    489 	case SIOCGIFBRDADDR:
    490 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
    491 			return (EINVAL);
    492 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr));
    493 		break;
    494 
    495 	case SIOCGIFDSTADDR:
    496 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    497 			return (EINVAL);
    498 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr));
    499 		break;
    500 
    501 	case SIOCGIFNETMASK:
    502 		/*
    503 		 * We keep the number of trailing zero bytes the sin_len field
    504 		 * of ia_sockmask, so we fix this before we pass it back to
    505 		 * userland.
    506 		 */
    507 		oldaddr = ia->ia_sockmask;
    508 		oldaddr.sin_len = sizeof(struct sockaddr_in);
    509 		ifreq_setaddr(cmd, ifr, (const void *)&oldaddr);
    510 		break;
    511 
    512 	case SIOCSIFDSTADDR:
    513 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
    514 			return (EINVAL);
    515 		oldaddr = ia->ia_dstaddr;
    516 		ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr));
    517 		if ((error = if_addr_init(ifp, &ia->ia_ifa, false)) != 0) {
    518 			ia->ia_dstaddr = oldaddr;
    519 			return error;
    520 		}
    521 		if (ia->ia_flags & IFA_ROUTE) {
    522 			ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
    523 			rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
    524 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    525 			rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP);
    526 		}
    527 		break;
    528 
    529 	case SIOCSIFBRDADDR:
    530 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
    531 			return EINVAL;
    532 		ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr));
    533 		break;
    534 
    535 	case SIOCSIFADDR:
    536 		error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)),
    537 		    1, hostIsNew);
    538 		if (error == 0) {
    539 			(void)pfil_run_hooks(if_pfil,
    540 			    (struct mbuf **)SIOCSIFADDR, ifp, PFIL_IFADDR);
    541 		}
    542 		break;
    543 
    544 	case SIOCSIFNETMASK:
    545 		in_ifscrub(ifp, ia);
    546 		ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr));
    547 		ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
    548 		error = in_ifinit(ifp, ia, NULL, 0, 0);
    549 		break;
    550 
    551 	case SIOCAIFADDR:
    552 		maskIsNew = 0;
    553 		if (ifra->ifra_mask.sin_len) {
    554 			/* Only scrub if we control the prefix route,
    555 			 * otherwise userland gets a bogus message */
    556 			if ((ia->ia_flags & IFA_ROUTE))
    557 				in_ifscrub(ifp, ia);
    558 			ia->ia_sockmask = ifra->ifra_mask;
    559 			ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
    560 			maskIsNew = 1;
    561 		}
    562 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
    563 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
    564 			/* Only scrub if we control the prefix route,
    565 			 * otherwise userland gets a bogus message */
    566 			if ((ia->ia_flags & IFA_ROUTE))
    567 				in_ifscrub(ifp, ia);
    568 			ia->ia_dstaddr = ifra->ifra_dstaddr;
    569 			maskIsNew  = 1; /* We lie; but the effect's the same */
    570 		}
    571 		if (ifra->ifra_addr.sin_family == AF_INET &&
    572 		    (hostIsNew || maskIsNew)) {
    573 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0,
    574 			    hostIsNew);
    575 		}
    576 		if ((ifp->if_flags & IFF_BROADCAST) &&
    577 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
    578 			ia->ia_broadaddr = ifra->ifra_broadaddr;
    579 		if (error == 0)
    580 			(void)pfil_run_hooks(if_pfil,
    581 			    (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR);
    582 		break;
    583 
    584 	case SIOCGIFALIAS:
    585 		ifra->ifra_mask = ia->ia_sockmask;
    586 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
    587 		    (ia->ia_dstaddr.sin_family == AF_INET))
    588 			ifra->ifra_dstaddr = ia->ia_dstaddr;
    589 		else if ((ifp->if_flags & IFF_BROADCAST) &&
    590 		    (ia->ia_broadaddr.sin_family == AF_INET))
    591 			ifra->ifra_broadaddr = ia->ia_broadaddr;
    592 		else
    593 			memset(&ifra->ifra_broadaddr, 0,
    594 			      sizeof(ifra->ifra_broadaddr));
    595 		break;
    596 
    597 	case SIOCGIFAFLAG_IN:
    598 		ifr->ifr_addrflags = ia->ia4_flags;
    599 		break;
    600 
    601 	case SIOCDIFADDR:
    602 		in_purgeaddr(&ia->ia_ifa);
    603 		(void)pfil_run_hooks(if_pfil, (struct mbuf **)SIOCDIFADDR,
    604 		    ifp, PFIL_IFADDR);
    605 		break;
    606 
    607 #ifdef MROUTING
    608 	case SIOCGETVIFCNT:
    609 	case SIOCGETSGCNT:
    610 		error = mrt_ioctl(so, cmd, data);
    611 		break;
    612 #endif /* MROUTING */
    613 
    614 	default:
    615 		return ENOTTY;
    616 	}
    617 
    618 	if (error != 0 && newifaddr) {
    619 		KASSERT(ia != NULL);
    620 		in_purgeaddr(&ia->ia_ifa);
    621 	}
    622 
    623 	return error;
    624 }
    625 
    626 /* Add ownaddr as loopback rtentry. */
    627 static void
    628 in_ifaddlocal(struct ifaddr *ifa)
    629 {
    630 	struct in_ifaddr *ia;
    631 
    632 	ia = (struct in_ifaddr *)ifa;
    633 	if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY ||
    634 	    (ia->ia_ifp->if_flags & IFF_POINTOPOINT &&
    635 	    in_hosteq(ia->ia_dstaddr.sin_addr, ia->ia_addr.sin_addr)))
    636 	{
    637 		rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
    638 		return;
    639 	}
    640 
    641 	rt_ifa_addlocal(ifa);
    642 }
    643 
    644 /* Rempve loopback entry of ownaddr */
    645 static void
    646 in_ifremlocal(struct ifaddr *ifa)
    647 {
    648 	struct in_ifaddr *ia, *p;
    649 	struct ifaddr *alt_ifa = NULL;
    650 	int ia_count = 0;
    651 
    652 	ia = (struct in_ifaddr *)ifa;
    653 	/* Delete the entry if exactly one ifaddr matches the
    654 	 * address, ifa->ifa_addr. */
    655 	TAILQ_FOREACH(p, &in_ifaddrhead, ia_list) {
    656 		if (!in_hosteq(p->ia_addr.sin_addr, ia->ia_addr.sin_addr))
    657 			continue;
    658 		if (p->ia_ifp != ia->ia_ifp)
    659 			alt_ifa = &p->ia_ifa;
    660 		if (++ia_count > 1 && alt_ifa != NULL)
    661 			break;
    662 	}
    663 
    664 	if (ia_count == 0)
    665 		return;
    666 
    667 	rt_ifa_remlocal(ifa, ia_count == 1 ? NULL : alt_ifa);
    668 }
    669 
    670 void
    671 in_purgeaddr(struct ifaddr *ifa)
    672 {
    673 	struct ifnet *ifp = ifa->ifa_ifp;
    674 	struct in_ifaddr *ia = (void *) ifa;
    675 
    676 	/* stop DAD processing */
    677 	if (ia->ia_dad_stop != NULL)
    678 		ia->ia_dad_stop(ifa);
    679 
    680 	in_ifscrub(ifp, ia);
    681 	in_ifremlocal(ifa);
    682 	LIST_REMOVE(ia, ia_hash);
    683 	ifa_remove(ifp, &ia->ia_ifa);
    684 	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list);
    685 	if (ia->ia_allhosts != NULL)
    686 		in_delmulti(ia->ia_allhosts);
    687 	ifafree(&ia->ia_ifa);
    688 	in_setmaxmtu();
    689 }
    690 
    691 void
    692 in_purgeif(struct ifnet *ifp)		/* MUST be called at splsoftnet() */
    693 {
    694 	if_purgeaddrs(ifp, AF_INET, in_purgeaddr);
    695 	igmp_purgeif(ifp);		/* manipulates pools */
    696 #ifdef MROUTING
    697 	ip_mrouter_detach(ifp);
    698 #endif
    699 }
    700 
    701 /*
    702  * SIOC[GAD]LIFADDR.
    703  *	SIOCGLIFADDR: get first address. (???)
    704  *	SIOCGLIFADDR with IFLR_PREFIX:
    705  *		get first address that matches the specified prefix.
    706  *	SIOCALIFADDR: add the specified address.
    707  *	SIOCALIFADDR with IFLR_PREFIX:
    708  *		EINVAL since we can't deduce hostid part of the address.
    709  *	SIOCDLIFADDR: delete the specified address.
    710  *	SIOCDLIFADDR with IFLR_PREFIX:
    711  *		delete the first address that matches the specified prefix.
    712  * return values:
    713  *	EINVAL on invalid parameters
    714  *	EADDRNOTAVAIL on prefix match failed/specified address not found
    715  *	other values may be returned from in_ioctl()
    716  */
    717 static int
    718 in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data,
    719     struct ifnet *ifp)
    720 {
    721 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
    722 	struct ifaddr *ifa;
    723 	struct sockaddr *sa;
    724 
    725 	/* sanity checks */
    726 	if (data == NULL || ifp == NULL) {
    727 		panic("invalid argument to in_lifaddr_ioctl");
    728 		/*NOTRECHED*/
    729 	}
    730 
    731 	switch (cmd) {
    732 	case SIOCGLIFADDR:
    733 		/* address must be specified on GET with IFLR_PREFIX */
    734 		if ((iflr->flags & IFLR_PREFIX) == 0)
    735 			break;
    736 		/*FALLTHROUGH*/
    737 	case SIOCALIFADDR:
    738 	case SIOCDLIFADDR:
    739 		/* address must be specified on ADD and DELETE */
    740 		sa = (struct sockaddr *)&iflr->addr;
    741 		if (sa->sa_family != AF_INET)
    742 			return EINVAL;
    743 		if (sa->sa_len != sizeof(struct sockaddr_in))
    744 			return EINVAL;
    745 		/* XXX need improvement */
    746 		sa = (struct sockaddr *)&iflr->dstaddr;
    747 		if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET)
    748 			return EINVAL;
    749 		if (sa->sa_len != 0 && sa->sa_len != sizeof(struct sockaddr_in))
    750 			return EINVAL;
    751 		break;
    752 	default: /*shouldn't happen*/
    753 #if 0
    754 		panic("invalid cmd to in_lifaddr_ioctl");
    755 		/*NOTREACHED*/
    756 #else
    757 		return EOPNOTSUPP;
    758 #endif
    759 	}
    760 	if (sizeof(struct in_addr) * NBBY < iflr->prefixlen)
    761 		return EINVAL;
    762 
    763 	switch (cmd) {
    764 	case SIOCALIFADDR:
    765 	    {
    766 		struct in_aliasreq ifra;
    767 
    768 		if (iflr->flags & IFLR_PREFIX)
    769 			return EINVAL;
    770 
    771 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
    772 		memset(&ifra, 0, sizeof(ifra));
    773 		memcpy(ifra.ifra_name, iflr->iflr_name,
    774 			sizeof(ifra.ifra_name));
    775 
    776 		memcpy(&ifra.ifra_addr, &iflr->addr,
    777 			((struct sockaddr *)&iflr->addr)->sa_len);
    778 
    779 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
    780 			memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr,
    781 				((struct sockaddr *)&iflr->dstaddr)->sa_len);
    782 		}
    783 
    784 		ifra.ifra_mask.sin_family = AF_INET;
    785 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
    786 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
    787 
    788 		return in_control(so, SIOCAIFADDR, &ifra, ifp);
    789 	    }
    790 	case SIOCGLIFADDR:
    791 	case SIOCDLIFADDR:
    792 	    {
    793 		struct in_ifaddr *ia;
    794 		struct in_addr mask, candidate, match;
    795 		struct sockaddr_in *sin;
    796 		int cmp;
    797 
    798 		memset(&mask, 0, sizeof(mask));
    799 		memset(&match, 0, sizeof(match));	/* XXX gcc */
    800 		if (iflr->flags & IFLR_PREFIX) {
    801 			/* lookup a prefix rather than address. */
    802 			in_len2mask(&mask, iflr->prefixlen);
    803 
    804 			sin = (struct sockaddr_in *)&iflr->addr;
    805 			match.s_addr = sin->sin_addr.s_addr;
    806 			match.s_addr &= mask.s_addr;
    807 
    808 			/* if you set extra bits, that's wrong */
    809 			if (match.s_addr != sin->sin_addr.s_addr)
    810 				return EINVAL;
    811 
    812 			cmp = 1;
    813 		} else {
    814 			if (cmd == SIOCGLIFADDR) {
    815 				/* on getting an address, take the 1st match */
    816 				cmp = 0;	/*XXX*/
    817 			} else {
    818 				/* on deleting an address, do exact match */
    819 				in_len2mask(&mask, 32);
    820 				sin = (struct sockaddr_in *)&iflr->addr;
    821 				match.s_addr = sin->sin_addr.s_addr;
    822 
    823 				cmp = 1;
    824 			}
    825 		}
    826 
    827 		IFADDR_FOREACH(ifa, ifp) {
    828 			if (ifa->ifa_addr->sa_family != AF_INET)
    829 				continue;
    830 			if (cmp == 0)
    831 				break;
    832 			candidate.s_addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
    833 			candidate.s_addr &= mask.s_addr;
    834 			if (candidate.s_addr == match.s_addr)
    835 				break;
    836 		}
    837 		if (ifa == NULL)
    838 			return EADDRNOTAVAIL;
    839 		ia = (struct in_ifaddr *)ifa;
    840 
    841 		if (cmd == SIOCGLIFADDR) {
    842 			/* fill in the if_laddrreq structure */
    843 			memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin_len);
    844 
    845 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
    846 				memcpy(&iflr->dstaddr, &ia->ia_dstaddr,
    847 					ia->ia_dstaddr.sin_len);
    848 			} else
    849 				memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr));
    850 
    851 			iflr->prefixlen =
    852 				in_mask2len(&ia->ia_sockmask.sin_addr);
    853 
    854 			iflr->flags = 0;	/*XXX*/
    855 
    856 			return 0;
    857 		} else {
    858 			struct in_aliasreq ifra;
    859 
    860 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
    861 			memset(&ifra, 0, sizeof(ifra));
    862 			memcpy(ifra.ifra_name, iflr->iflr_name,
    863 				sizeof(ifra.ifra_name));
    864 
    865 			memcpy(&ifra.ifra_addr, &ia->ia_addr,
    866 				ia->ia_addr.sin_len);
    867 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
    868 				memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr,
    869 					ia->ia_dstaddr.sin_len);
    870 			}
    871 			memcpy(&ifra.ifra_dstaddr, &ia->ia_sockmask,
    872 				ia->ia_sockmask.sin_len);
    873 
    874 			return in_control(so, SIOCDIFADDR, &ifra, ifp);
    875 		}
    876 	    }
    877 	}
    878 
    879 	return EOPNOTSUPP;	/*just for safety*/
    880 }
    881 
    882 /*
    883  * Delete any existing route for an interface.
    884  */
    885 void
    886 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
    887 {
    888 
    889 	in_scrubprefix(ia);
    890 }
    891 
    892 /*
    893  * Initialize an interface's internet address
    894  * and routing table entry.
    895  */
    896 int
    897 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
    898     const struct sockaddr_in *sin, int scrub, int hostIsNew)
    899 {
    900 	u_int32_t i;
    901 	struct sockaddr_in oldaddr;
    902 	int s = splnet(), flags = RTF_UP, error;
    903 
    904 	if (sin == NULL)
    905 		sin = &ia->ia_addr;
    906 
    907 	/*
    908 	 * Set up new addresses.
    909 	 */
    910 	oldaddr = ia->ia_addr;
    911 	if (ia->ia_addr.sin_family == AF_INET)
    912 		LIST_REMOVE(ia, ia_hash);
    913 	ia->ia_addr = *sin;
    914 	LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
    915 
    916 	/* Set IN_IFF flags early for if_addr_init() */
    917 	if (hostIsNew && if_do_dad(ifp) && !in_nullhost(ia->ia_addr.sin_addr)) {
    918 		if (ifp->if_link_state == LINK_STATE_DOWN)
    919 			ia->ia4_flags |= IN_IFF_DETACHED;
    920 		else
    921 			/* State the intent to try DAD if possible */
    922 			ia->ia4_flags |= IN_IFF_TRYTENTATIVE;
    923 	}
    924 
    925 	/*
    926 	 * Give the interface a chance to initialize
    927 	 * if this is its first address,
    928 	 * and to validate the address if necessary.
    929 	 */
    930 	if ((error = if_addr_init(ifp, &ia->ia_ifa, true)) != 0)
    931 		goto bad;
    932 	/* Now clear the try tentative flag, it's job is done. */
    933 	ia->ia4_flags &= ~IN_IFF_TRYTENTATIVE;
    934 	splx(s);
    935 
    936 	if (scrub) {
    937 		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
    938 		in_ifscrub(ifp, ia);
    939 		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    940 	}
    941 
    942 	/* Add the local route to the address */
    943 	in_ifaddlocal(&ia->ia_ifa);
    944 
    945 	i = ia->ia_addr.sin_addr.s_addr;
    946 	if (IN_CLASSA(i))
    947 		ia->ia_netmask = IN_CLASSA_NET;
    948 	else if (IN_CLASSB(i))
    949 		ia->ia_netmask = IN_CLASSB_NET;
    950 	else
    951 		ia->ia_netmask = IN_CLASSC_NET;
    952 	/*
    953 	 * The subnet mask usually includes at least the standard network part,
    954 	 * but may may be smaller in the case of supernetting.
    955 	 * If it is set, we believe it.
    956 	 */
    957 	if (ia->ia_subnetmask == 0) {
    958 		ia->ia_subnetmask = ia->ia_netmask;
    959 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
    960 	} else
    961 		ia->ia_netmask &= ia->ia_subnetmask;
    962 
    963 	ia->ia_net = i & ia->ia_netmask;
    964 	ia->ia_subnet = i & ia->ia_subnetmask;
    965 	in_socktrim(&ia->ia_sockmask);
    966 	/* re-calculate the "in_maxmtu" value */
    967 	in_setmaxmtu();
    968 	/*
    969 	 * Add route for the network.
    970 	 */
    971 	ia->ia_ifa.ifa_metric = ifp->if_metric;
    972 	if (ifp->if_flags & IFF_BROADCAST) {
    973 		ia->ia_broadaddr.sin_addr.s_addr =
    974 			ia->ia_subnet | ~ia->ia_subnetmask;
    975 		ia->ia_netbroadcast.s_addr =
    976 			ia->ia_net | ~ia->ia_netmask;
    977 	} else if (ifp->if_flags & IFF_LOOPBACK) {
    978 		ia->ia_dstaddr = ia->ia_addr;
    979 		flags |= RTF_HOST;
    980 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
    981 		if (ia->ia_dstaddr.sin_family != AF_INET)
    982 			return (0);
    983 		flags |= RTF_HOST;
    984 	}
    985 	error = in_addprefix(ia, flags);
    986 	/*
    987 	 * If the interface supports multicast, join the "all hosts"
    988 	 * multicast group on that interface.
    989 	 */
    990 	if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
    991 		struct in_addr addr;
    992 
    993 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
    994 		ia->ia_allhosts = in_addmulti(&addr, ifp);
    995 	}
    996 
    997 	if (hostIsNew && if_do_dad(ifp) &&
    998 	    !in_nullhost(ia->ia_addr.sin_addr) &&
    999 	    ia->ia4_flags & IN_IFF_TENTATIVE)
   1000 		ia->ia_dad_start((struct ifaddr *)ia);
   1001 
   1002 	return (error);
   1003 bad:
   1004 	splx(s);
   1005 	LIST_REMOVE(ia, ia_hash);
   1006 	ia->ia_addr = oldaddr;
   1007 	if (ia->ia_addr.sin_family == AF_INET)
   1008 		LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
   1009 		    ia, ia_hash);
   1010 	return (error);
   1011 }
   1012 
   1013 #define rtinitflags(x) \
   1014 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
   1015 	    ? RTF_HOST : 0)
   1016 
   1017 /*
   1018  * add a route to prefix ("connected route" in cisco terminology).
   1019  * does nothing if there's some interface address with the same prefix already.
   1020  */
   1021 static int
   1022 in_addprefix(struct in_ifaddr *target, int flags)
   1023 {
   1024 	struct in_ifaddr *ia;
   1025 	struct in_addr prefix, mask, p;
   1026 	int error;
   1027 
   1028 	if ((flags & RTF_HOST) != 0)
   1029 		prefix = target->ia_dstaddr.sin_addr;
   1030 	else {
   1031 		prefix = target->ia_addr.sin_addr;
   1032 		mask = target->ia_sockmask.sin_addr;
   1033 		prefix.s_addr &= mask.s_addr;
   1034 	}
   1035 
   1036 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
   1037 		if (rtinitflags(ia))
   1038 			p = ia->ia_dstaddr.sin_addr;
   1039 		else {
   1040 			p = ia->ia_addr.sin_addr;
   1041 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
   1042 		}
   1043 
   1044 		if (prefix.s_addr != p.s_addr)
   1045 			continue;
   1046 
   1047 		/*
   1048 		 * if we got a matching prefix route inserted by other
   1049 		 * interface address, we don't need to bother
   1050 		 *
   1051 		 * XXX RADIX_MPATH implications here? -dyoung
   1052 		 */
   1053 		if (ia->ia_flags & IFA_ROUTE)
   1054 			return 0;
   1055 	}
   1056 
   1057 	/*
   1058 	 * noone seem to have prefix route.  insert it.
   1059 	 */
   1060 	error = rtinit(&target->ia_ifa, RTM_ADD, flags);
   1061 	if (error == 0)
   1062 		target->ia_flags |= IFA_ROUTE;
   1063 	else if (error == EEXIST) {
   1064 		/*
   1065 		 * the fact the route already exists is not an error.
   1066 		 */
   1067 		error = 0;
   1068 	}
   1069 	return error;
   1070 }
   1071 
   1072 /*
   1073  * remove a route to prefix ("connected route" in cisco terminology).
   1074  * re-installs the route by using another interface address, if there's one
   1075  * with the same prefix (otherwise we lose the route mistakenly).
   1076  */
   1077 static int
   1078 in_scrubprefix(struct in_ifaddr *target)
   1079 {
   1080 	struct in_ifaddr *ia;
   1081 	struct in_addr prefix, mask, p;
   1082 	int error;
   1083 
   1084 	/* If we don't have IFA_ROUTE we should still inform userland */
   1085 	if ((target->ia_flags & IFA_ROUTE) == 0)
   1086 		return 0;
   1087 
   1088 	if (rtinitflags(target))
   1089 		prefix = target->ia_dstaddr.sin_addr;
   1090 	else {
   1091 		prefix = target->ia_addr.sin_addr;
   1092 		mask = target->ia_sockmask.sin_addr;
   1093 		prefix.s_addr &= mask.s_addr;
   1094 	}
   1095 
   1096 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
   1097 		if (rtinitflags(ia))
   1098 			p = ia->ia_dstaddr.sin_addr;
   1099 		else {
   1100 			p = ia->ia_addr.sin_addr;
   1101 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
   1102 		}
   1103 
   1104 		if (prefix.s_addr != p.s_addr)
   1105 			continue;
   1106 
   1107 		/*
   1108 		 * if we got a matching prefix route, move IFA_ROUTE to him
   1109 		 */
   1110 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
   1111 			rtinit(&target->ia_ifa, RTM_DELETE,
   1112 			    rtinitflags(target));
   1113 			target->ia_flags &= ~IFA_ROUTE;
   1114 
   1115 			error = rtinit(&ia->ia_ifa, RTM_ADD,
   1116 			    rtinitflags(ia) | RTF_UP);
   1117 			if (error == 0)
   1118 				ia->ia_flags |= IFA_ROUTE;
   1119 			return error;
   1120 		}
   1121 	}
   1122 
   1123 	/*
   1124 	 * noone seem to have prefix route.  remove it.
   1125 	 */
   1126 	rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
   1127 	target->ia_flags &= ~IFA_ROUTE;
   1128 	return 0;
   1129 }
   1130 
   1131 #undef rtinitflags
   1132 
   1133 /*
   1134  * Return 1 if the address might be a local broadcast address.
   1135  */
   1136 int
   1137 in_broadcast(struct in_addr in, struct ifnet *ifp)
   1138 {
   1139 	struct ifaddr *ifa;
   1140 
   1141 	if (in.s_addr == INADDR_BROADCAST ||
   1142 	    in_nullhost(in))
   1143 		return 1;
   1144 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
   1145 		return 0;
   1146 	/*
   1147 	 * Look through the list of addresses for a match
   1148 	 * with a broadcast address.
   1149 	 */
   1150 #define ia (ifatoia(ifa))
   1151 	IFADDR_FOREACH(ifa, ifp)
   1152 		if (ifa->ifa_addr->sa_family == AF_INET &&
   1153 		    !in_hosteq(in, ia->ia_addr.sin_addr) &&
   1154 		    (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
   1155 		     in_hosteq(in, ia->ia_netbroadcast) ||
   1156 		     (hostzeroisbroadcast &&
   1157 		      /*
   1158 		       * Check for old-style (host 0) broadcast.
   1159 		       */
   1160 		      (in.s_addr == ia->ia_subnet ||
   1161 		       in.s_addr == ia->ia_net))))
   1162 			return 1;
   1163 	return (0);
   1164 #undef ia
   1165 }
   1166 
   1167 /*
   1168  * perform DAD when interface becomes IFF_UP.
   1169  */
   1170 void
   1171 in_if_link_up(struct ifnet *ifp)
   1172 {
   1173 	struct ifaddr *ifa;
   1174 	struct in_ifaddr *ia;
   1175 
   1176 	/* Ensure it's sane to run DAD */
   1177 	if (ifp->if_link_state == LINK_STATE_DOWN)
   1178 		return;
   1179 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
   1180 		return;
   1181 
   1182 	IFADDR_FOREACH(ifa, ifp) {
   1183 		if (ifa->ifa_addr->sa_family != AF_INET)
   1184 			continue;
   1185 		ia = (struct in_ifaddr *)ifa;
   1186 
   1187 		/* If detached then mark as tentative */
   1188 		if (ia->ia4_flags & IN_IFF_DETACHED) {
   1189 			ia->ia4_flags &= ~IN_IFF_DETACHED;
   1190 			if (if_do_dad(ifp) && ia->ia_dad_start != NULL)
   1191 				ia->ia4_flags |= IN_IFF_TENTATIVE;
   1192 			else if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0)
   1193 				rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1194 		}
   1195 
   1196 		if (ia->ia4_flags & IN_IFF_TENTATIVE) {
   1197 			/* Clear the duplicated flag as we're starting DAD. */
   1198 			ia->ia4_flags &= ~IN_IFF_DUPLICATED;
   1199 			ia->ia_dad_start(ifa);
   1200 		}
   1201 	}
   1202 }
   1203 
   1204 void
   1205 in_if_up(struct ifnet *ifp)
   1206 {
   1207 
   1208 	/* interface may not support link state, so bring it up also */
   1209 	in_if_link_up(ifp);
   1210 }
   1211 
   1212 /*
   1213  * Mark all addresses as detached.
   1214  */
   1215 void
   1216 in_if_link_down(struct ifnet *ifp)
   1217 {
   1218 	struct ifaddr *ifa;
   1219 	struct in_ifaddr *ia;
   1220 
   1221 	IFADDR_FOREACH(ifa, ifp) {
   1222 		if (ifa->ifa_addr->sa_family != AF_INET)
   1223 			continue;
   1224 		ia = (struct in_ifaddr *)ifa;
   1225 
   1226 		/* Stop DAD processing */
   1227 		if (ia->ia_dad_stop != NULL)
   1228 			ia->ia_dad_stop(ifa);
   1229 
   1230 		/*
   1231 		 * Mark the address as detached.
   1232 		 */
   1233 		if (!(ia->ia4_flags & IN_IFF_DETACHED)) {
   1234 			ia->ia4_flags |= IN_IFF_DETACHED;
   1235 			ia->ia4_flags &=
   1236 			    ~(IN_IFF_TENTATIVE | IN_IFF_DUPLICATED);
   1237 			rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1238 		}
   1239 	}
   1240 }
   1241 
   1242 void
   1243 in_if_down(struct ifnet *ifp)
   1244 {
   1245 
   1246 	in_if_link_down(ifp);
   1247 }
   1248 
   1249 void
   1250 in_if_link_state_change(struct ifnet *ifp, int link_state)
   1251 {
   1252 
   1253 	switch (link_state) {
   1254 	case LINK_STATE_DOWN:
   1255 		in_if_link_down(ifp);
   1256 		break;
   1257 	case LINK_STATE_UP:
   1258 		in_if_link_up(ifp);
   1259 		break;
   1260 	}
   1261 }
   1262 
   1263 /*
   1264  * in_lookup_multi: look up the in_multi record for a given IP
   1265  * multicast address on a given interface.  If no matching record is
   1266  * found, return NULL.
   1267  */
   1268 struct in_multi *
   1269 in_lookup_multi(struct in_addr addr, ifnet_t *ifp)
   1270 {
   1271 	struct in_multi *inm;
   1272 
   1273 	KASSERT(rw_lock_held(&in_multilock));
   1274 
   1275 	LIST_FOREACH(inm, &IN_MULTI_HASH(addr.s_addr, ifp), inm_list) {
   1276 		if (in_hosteq(inm->inm_addr, addr) && inm->inm_ifp == ifp)
   1277 			break;
   1278 	}
   1279 	return inm;
   1280 }
   1281 
   1282 /*
   1283  * in_multi_group: check whether the address belongs to an IP multicast
   1284  * group we are joined on this interface.  Returns true or false.
   1285  */
   1286 bool
   1287 in_multi_group(struct in_addr addr, ifnet_t *ifp, int flags)
   1288 {
   1289 	bool ingroup;
   1290 
   1291 	if (__predict_true(flags & IP_IGMP_MCAST) == 0) {
   1292 		rw_enter(&in_multilock, RW_READER);
   1293 		ingroup = in_lookup_multi(addr, ifp) != NULL;
   1294 		rw_exit(&in_multilock);
   1295 	} else {
   1296 		/* XXX Recursive call from ip_output(). */
   1297 		KASSERT(rw_lock_held(&in_multilock));
   1298 		ingroup = in_lookup_multi(addr, ifp) != NULL;
   1299 	}
   1300 	return ingroup;
   1301 }
   1302 
   1303 /*
   1304  * Add an address to the list of IP multicast addresses for a given interface.
   1305  */
   1306 struct in_multi *
   1307 in_addmulti(struct in_addr *ap, ifnet_t *ifp)
   1308 {
   1309 	struct sockaddr_in sin;
   1310 	struct in_multi *inm;
   1311 
   1312 	/*
   1313 	 * See if address already in list.
   1314 	 */
   1315 	rw_enter(&in_multilock, RW_WRITER);
   1316 	inm = in_lookup_multi(*ap, ifp);
   1317 	if (inm != NULL) {
   1318 		/*
   1319 		 * Found it; just increment the reference count.
   1320 		 */
   1321 		inm->inm_refcount++;
   1322 		rw_exit(&in_multilock);
   1323 		return inm;
   1324 	}
   1325 
   1326 	/*
   1327 	 * New address; allocate a new multicast record.
   1328 	 */
   1329 	inm = pool_get(&inmulti_pool, PR_NOWAIT);
   1330 	if (inm == NULL) {
   1331 		rw_exit(&in_multilock);
   1332 		return NULL;
   1333 	}
   1334 	inm->inm_addr = *ap;
   1335 	inm->inm_ifp = ifp;
   1336 	inm->inm_refcount = 1;
   1337 
   1338 	/*
   1339 	 * Ask the network driver to update its multicast reception
   1340 	 * filter appropriately for the new address.
   1341 	 */
   1342 	sockaddr_in_init(&sin, ap, 0);
   1343 	if (if_mcast_op(ifp, SIOCADDMULTI, sintosa(&sin)) != 0) {
   1344 		rw_exit(&in_multilock);
   1345 		pool_put(&inmulti_pool, inm);
   1346 		return NULL;
   1347 	}
   1348 
   1349 	/*
   1350 	 * Let IGMP know that we have joined a new IP multicast group.
   1351 	 */
   1352 	if (igmp_joingroup(inm) != 0) {
   1353 		rw_exit(&in_multilock);
   1354 		pool_put(&inmulti_pool, inm);
   1355 		return NULL;
   1356 	}
   1357 	LIST_INSERT_HEAD(
   1358 	    &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp),
   1359 	    inm, inm_list);
   1360 	in_multientries++;
   1361 	rw_exit(&in_multilock);
   1362 
   1363 	return inm;
   1364 }
   1365 
   1366 /*
   1367  * Delete a multicast address record.
   1368  */
   1369 void
   1370 in_delmulti(struct in_multi *inm)
   1371 {
   1372 	struct sockaddr_in sin;
   1373 
   1374 	rw_enter(&in_multilock, RW_WRITER);
   1375 	if (--inm->inm_refcount > 0) {
   1376 		rw_exit(&in_multilock);
   1377 		return;
   1378 	}
   1379 
   1380 	/*
   1381 	 * No remaining claims to this record; let IGMP know that
   1382 	 * we are leaving the multicast group.
   1383 	 */
   1384 	igmp_leavegroup(inm);
   1385 
   1386 	/*
   1387 	 * Notify the network driver to update its multicast reception
   1388 	 * filter.
   1389 	 */
   1390 	sockaddr_in_init(&sin, &inm->inm_addr, 0);
   1391 	if_mcast_op(inm->inm_ifp, SIOCDELMULTI, sintosa(&sin));
   1392 
   1393 	/*
   1394 	 * Unlink from list.
   1395 	 */
   1396 	LIST_REMOVE(inm, inm_list);
   1397 	in_multientries--;
   1398 	rw_exit(&in_multilock);
   1399 
   1400 	pool_put(&inmulti_pool, inm);
   1401 }
   1402 
   1403 /*
   1404  * in_next_multi: step through all of the in_multi records, one at a time.
   1405  * The current position is remembered in "step", which the caller must
   1406  * provide.  in_first_multi(), below, must be called to initialize "step"
   1407  * and get the first record.  Both macros return a NULL "inm" when there
   1408  * are no remaining records.
   1409  */
   1410 struct in_multi *
   1411 in_next_multi(struct in_multistep *step)
   1412 {
   1413 	struct in_multi *inm;
   1414 
   1415 	KASSERT(rw_lock_held(&in_multilock));
   1416 
   1417 	while (step->i_inm == NULL && step->i_n < IN_MULTI_HASH_SIZE) {
   1418 		step->i_inm = LIST_FIRST(&in_multihashtbl[++step->i_n]);
   1419 	}
   1420 	if ((inm = step->i_inm) != NULL) {
   1421 		step->i_inm = LIST_NEXT(inm, inm_list);
   1422 	}
   1423 	return inm;
   1424 }
   1425 
   1426 struct in_multi *
   1427 in_first_multi(struct in_multistep *step)
   1428 {
   1429 	KASSERT(rw_lock_held(&in_multilock));
   1430 
   1431 	step->i_n = 0;
   1432 	step->i_inm = LIST_FIRST(&in_multihashtbl[0]);
   1433 	return in_next_multi(step);
   1434 }
   1435 
   1436 void
   1437 in_multi_lock(int op)
   1438 {
   1439 	rw_enter(&in_multilock, op);
   1440 }
   1441 
   1442 void
   1443 in_multi_unlock(void)
   1444 {
   1445 	rw_exit(&in_multilock);
   1446 }
   1447 
   1448 int
   1449 in_multi_lock_held(void)
   1450 {
   1451 	return rw_lock_held(&in_multilock);
   1452 }
   1453 
   1454 struct sockaddr_in *
   1455 in_selectsrc(struct sockaddr_in *sin, struct route *ro,
   1456     int soopts, struct ip_moptions *mopts, int *errorp)
   1457 {
   1458 	struct rtentry *rt = NULL;
   1459 	struct in_ifaddr *ia = NULL;
   1460 
   1461 	/*
   1462          * If route is known or can be allocated now, take the
   1463          * source address from the interface.  Otherwise, punt.
   1464 	 */
   1465 	if ((soopts & SO_DONTROUTE) != 0)
   1466 		rtcache_free(ro);
   1467 	else {
   1468 		union {
   1469 			struct sockaddr		dst;
   1470 			struct sockaddr_in	dst4;
   1471 		} u;
   1472 
   1473 		sockaddr_in_init(&u.dst4, &sin->sin_addr, 0);
   1474 		rt = rtcache_lookup(ro, &u.dst);
   1475 	}
   1476 	/*
   1477 	 * If we found a route, use the address
   1478 	 * corresponding to the outgoing interface
   1479 	 * unless it is the loopback (in case a route
   1480 	 * to our address on another net goes to loopback).
   1481 	 *
   1482 	 * XXX Is this still true?  Do we care?
   1483 	 */
   1484 	if (rt != NULL && (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
   1485 		ia = ifatoia(rt->rt_ifa);
   1486 	if (ia == NULL) {
   1487 		u_int16_t fport = sin->sin_port;
   1488 
   1489 		sin->sin_port = 0;
   1490 		ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
   1491 		sin->sin_port = fport;
   1492 		if (ia == NULL) {
   1493 			/* Find 1st non-loopback AF_INET address */
   1494 			TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
   1495 				if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
   1496 					break;
   1497 			}
   1498 		}
   1499 		if (ia == NULL) {
   1500 			*errorp = EADDRNOTAVAIL;
   1501 			return NULL;
   1502 		}
   1503 	}
   1504 	/*
   1505 	 * If the destination address is multicast and an outgoing
   1506 	 * interface has been set as a multicast option, use the
   1507 	 * address of that interface as our source address.
   1508 	 */
   1509 	if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
   1510 		struct ip_moptions *imo;
   1511 		struct ifnet *ifp;
   1512 
   1513 		imo = mopts;
   1514 		if (imo->imo_multicast_ifp != NULL) {
   1515 			ifp = imo->imo_multicast_ifp;
   1516 			IFP_TO_IA(ifp, ia);		/* XXX */
   1517 			if (ia == 0 || ia->ia4_flags & IN_IFF_NOTREADY) {
   1518 				*errorp = EADDRNOTAVAIL;
   1519 				return NULL;
   1520 			}
   1521 		}
   1522 	}
   1523 	if (ia->ia_ifa.ifa_getifa != NULL) {
   1524 		ia = ifatoia((*ia->ia_ifa.ifa_getifa)(&ia->ia_ifa,
   1525 		                                      sintosa(sin)));
   1526 		if (ia == NULL) {
   1527 			*errorp = EADDRNOTAVAIL;
   1528 			return NULL;
   1529 		}
   1530 	}
   1531 #ifdef GETIFA_DEBUG
   1532 	else
   1533 		printf("%s: missing ifa_getifa\n", __func__);
   1534 #endif
   1535 	return satosin(&ia->ia_addr);
   1536 }
   1537 
   1538 struct in_llentry {
   1539 	struct llentry		base;
   1540 };
   1541 
   1542 #define	IN_LLTBL_DEFAULT_HSIZE	32
   1543 #define	IN_LLTBL_HASH(k, h) \
   1544 	(((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
   1545 
   1546 /*
   1547  * Do actual deallocation of @lle.
   1548  * Called by LLE_FREE_LOCKED when number of references
   1549  * drops to zero.
   1550  */
   1551 static void
   1552 in_lltable_destroy_lle(struct llentry *lle)
   1553 {
   1554 
   1555 	LLE_WUNLOCK(lle);
   1556 	LLE_LOCK_DESTROY(lle);
   1557 	kmem_intr_free(lle, sizeof(*lle));
   1558 }
   1559 
   1560 static struct llentry *
   1561 in_lltable_new(struct in_addr addr4, u_int flags)
   1562 {
   1563 	struct in_llentry *lle;
   1564 
   1565 	lle = kmem_intr_zalloc(sizeof(*lle), KM_NOSLEEP);
   1566 	if (lle == NULL)		/* NB: caller generates msg */
   1567 		return NULL;
   1568 
   1569 	/*
   1570 	 * For IPv4 this will trigger "arpresolve" to generate
   1571 	 * an ARP request.
   1572 	 */
   1573 	lle->base.la_expire = time_uptime; /* mark expired */
   1574 	lle->base.r_l3addr.addr4 = addr4;
   1575 	lle->base.lle_refcnt = 1;
   1576 	lle->base.lle_free = in_lltable_destroy_lle;
   1577 	LLE_LOCK_INIT(&lle->base);
   1578 	callout_init(&lle->base.la_timer, CALLOUT_MPSAFE);
   1579 
   1580 	return (&lle->base);
   1581 }
   1582 
   1583 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m)	(			\
   1584 	    (((ntohl((d).s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
   1585 
   1586 static int
   1587 in_lltable_match_prefix(const struct sockaddr *prefix,
   1588     const struct sockaddr *mask, u_int flags, struct llentry *lle)
   1589 {
   1590 	const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
   1591 	const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
   1592 
   1593 	/*
   1594 	 * (flags & LLE_STATIC) means deleting all entries
   1595 	 * including static ARP entries.
   1596 	 */
   1597 	if (IN_ARE_MASKED_ADDR_EQUAL(lle->r_l3addr.addr4, pfx, msk) &&
   1598 	    ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC)))
   1599 		return (1);
   1600 
   1601 	return (0);
   1602 }
   1603 
   1604 static void
   1605 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
   1606 {
   1607 	struct ifnet *ifp __diagused;
   1608 	size_t pkts_dropped;
   1609 
   1610 	LLE_WLOCK_ASSERT(lle);
   1611 	KASSERT(llt != NULL);
   1612 
   1613 	/* Unlink entry from table if not already */
   1614 	if ((lle->la_flags & LLE_LINKED) != 0) {
   1615 		ifp = llt->llt_ifp;
   1616 		IF_AFDATA_WLOCK_ASSERT(ifp);
   1617 		lltable_unlink_entry(llt, lle);
   1618 	}
   1619 
   1620 	/* cancel timer */
   1621 	if (callout_stop(&lle->lle_timer))
   1622 		LLE_REMREF(lle);
   1623 
   1624 	/* Drop hold queue */
   1625 	pkts_dropped = llentry_free(lle);
   1626 	arp_stat_add(ARP_STAT_DFRDROPPED, (uint64_t)pkts_dropped);
   1627 }
   1628 
   1629 static int
   1630 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
   1631 {
   1632 	struct rtentry *rt;
   1633 	int error = EINVAL;
   1634 
   1635 	KASSERTMSG(l3addr->sa_family == AF_INET,
   1636 	    "sin_family %d", l3addr->sa_family);
   1637 
   1638 	rt = rtalloc1(l3addr, 0);
   1639 	if (rt == NULL)
   1640 		return error;
   1641 
   1642 	/*
   1643 	 * If the gateway for an existing host route matches the target L3
   1644 	 * address, which is a special route inserted by some implementation
   1645 	 * such as MANET, and the interface is of the correct type, then
   1646 	 * allow for ARP to proceed.
   1647 	 */
   1648 	if (rt->rt_flags & RTF_GATEWAY) {
   1649 		if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp ||
   1650 		    rt->rt_ifp->if_type != IFT_ETHER ||
   1651 #ifdef __FreeBSD__
   1652 		    (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
   1653 #else
   1654 		    (rt->rt_ifp->if_flags & IFF_NOARP) != 0 ||
   1655 #endif
   1656 		    memcmp(rt->rt_gateway->sa_data, l3addr->sa_data,
   1657 		    sizeof(in_addr_t)) != 0) {
   1658 			goto error;
   1659 		}
   1660 	}
   1661 
   1662 	/*
   1663 	 * Make sure that at least the destination address is covered
   1664 	 * by the route. This is for handling the case where 2 or more
   1665 	 * interfaces have the same prefix. An incoming packet arrives
   1666 	 * on one interface and the corresponding outgoing packet leaves
   1667 	 * another interface.
   1668 	 */
   1669 	if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) {
   1670 		const char *sa, *mask, *addr, *lim;
   1671 		int len;
   1672 
   1673 		mask = (const char *)rt_mask(rt);
   1674 		/*
   1675 		 * Just being extra cautious to avoid some custom
   1676 		 * code getting into trouble.
   1677 		 */
   1678 		if (mask == NULL)
   1679 			goto error;
   1680 
   1681 		sa = (const char *)rt_getkey(rt);
   1682 		addr = (const char *)l3addr;
   1683 		len = ((const struct sockaddr_in *)l3addr)->sin_len;
   1684 		lim = addr + len;
   1685 
   1686 		for ( ; addr < lim; sa++, mask++, addr++) {
   1687 			if ((*sa ^ *addr) & *mask) {
   1688 #ifdef DIAGNOSTIC
   1689 				log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
   1690 				    inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
   1691 #endif
   1692 				goto error;
   1693 			}
   1694 		}
   1695 	}
   1696 
   1697 	error = 0;
   1698 error:
   1699 	return error;
   1700 }
   1701 
   1702 static inline uint32_t
   1703 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
   1704 {
   1705 
   1706 	return (IN_LLTBL_HASH(dst.s_addr, hsize));
   1707 }
   1708 
   1709 static uint32_t
   1710 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
   1711 {
   1712 
   1713 	return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
   1714 }
   1715 
   1716 static void
   1717 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
   1718 {
   1719 	struct sockaddr_in *sin;
   1720 
   1721 	sin = (struct sockaddr_in *)sa;
   1722 	memset(sin, 0, sizeof(*sin));
   1723 	sin->sin_family = AF_INET;
   1724 	sin->sin_len = sizeof(*sin);
   1725 	sin->sin_addr = lle->r_l3addr.addr4;
   1726 }
   1727 
   1728 static inline struct llentry *
   1729 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
   1730 {
   1731 	struct llentry *lle;
   1732 	struct llentries *lleh;
   1733 	u_int hashidx;
   1734 
   1735 	hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
   1736 	lleh = &llt->lle_head[hashidx];
   1737 	LIST_FOREACH(lle, lleh, lle_next) {
   1738 		if (lle->la_flags & LLE_DELETED)
   1739 			continue;
   1740 		if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
   1741 			break;
   1742 	}
   1743 
   1744 	return (lle);
   1745 }
   1746 
   1747 static int
   1748 in_lltable_delete(struct lltable *llt, u_int flags,
   1749     const struct sockaddr *l3addr)
   1750 {
   1751 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
   1752 	struct ifnet *ifp __diagused = llt->llt_ifp;
   1753 	struct llentry *lle;
   1754 
   1755 	IF_AFDATA_WLOCK_ASSERT(ifp);
   1756 	KASSERTMSG(l3addr->sa_family == AF_INET,
   1757 	    "sin_family %d", l3addr->sa_family);
   1758 
   1759 	lle = in_lltable_find_dst(llt, sin->sin_addr);
   1760 	if (lle == NULL) {
   1761 #ifdef DIAGNOSTIC
   1762 		log(LOG_INFO, "interface address is missing from cache = %p  in delete\n", lle);
   1763 #endif
   1764 		return (ENOENT);
   1765 	}
   1766 
   1767 	if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
   1768 		LLE_WLOCK(lle);
   1769 		lle->la_flags |= LLE_DELETED;
   1770 #ifdef DIAGNOSTIC
   1771 		log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
   1772 #endif
   1773 		if ((lle->la_flags & (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
   1774 			llentry_free(lle);
   1775 		else
   1776 			LLE_WUNLOCK(lle);
   1777 	}
   1778 
   1779 	return (0);
   1780 }
   1781 
   1782 static struct llentry *
   1783 in_lltable_create(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
   1784 {
   1785 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
   1786 	struct ifnet *ifp = llt->llt_ifp;
   1787 	struct llentry *lle;
   1788 
   1789 	IF_AFDATA_WLOCK_ASSERT(ifp);
   1790 	KASSERTMSG(l3addr->sa_family == AF_INET,
   1791 	    "sin_family %d", l3addr->sa_family);
   1792 
   1793 	lle = in_lltable_find_dst(llt, sin->sin_addr);
   1794 
   1795 	if (lle != NULL) {
   1796 		LLE_WLOCK(lle);
   1797 		return (lle);
   1798 	}
   1799 
   1800 	/* no existing record, we need to create new one */
   1801 
   1802 	/*
   1803 	 * A route that covers the given address must have
   1804 	 * been installed 1st because we are doing a resolution,
   1805 	 * verify this.
   1806 	 */
   1807 	if (!(flags & LLE_IFADDR) &&
   1808 	    in_lltable_rtcheck(ifp, flags, l3addr) != 0)
   1809 		return (NULL);
   1810 
   1811 	lle = in_lltable_new(sin->sin_addr, flags);
   1812 	if (lle == NULL) {
   1813 		log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
   1814 		return (NULL);
   1815 	}
   1816 	lle->la_flags = flags;
   1817 	if ((flags & LLE_IFADDR) == LLE_IFADDR) {
   1818 		memcpy(&lle->ll_addr, CLLADDR(ifp->if_sadl), ifp->if_addrlen);
   1819 		lle->la_flags |= (LLE_VALID | LLE_STATIC);
   1820 	}
   1821 
   1822 	lltable_link_entry(llt, lle);
   1823 	LLE_WLOCK(lle);
   1824 
   1825 	return (lle);
   1826 }
   1827 
   1828 /*
   1829  * Return NULL if not found or marked for deletion.
   1830  * If found return lle read locked.
   1831  */
   1832 static struct llentry *
   1833 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
   1834 {
   1835 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
   1836 	struct llentry *lle;
   1837 
   1838 	IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
   1839 	KASSERTMSG(l3addr->sa_family == AF_INET,
   1840 	    "sin_family %d", l3addr->sa_family);
   1841 
   1842 	lle = in_lltable_find_dst(llt, sin->sin_addr);
   1843 
   1844 	if (lle == NULL)
   1845 		return NULL;
   1846 
   1847 	if (flags & LLE_EXCLUSIVE)
   1848 		LLE_WLOCK(lle);
   1849 	else
   1850 		LLE_RLOCK(lle);
   1851 
   1852 	return lle;
   1853 }
   1854 
   1855 static void
   1856 in_sysctl_init(struct sysctllog **clog)
   1857 {
   1858 	sysctl_createv(clog, 0, NULL, NULL,
   1859 		       CTLFLAG_PERMANENT,
   1860 		       CTLTYPE_NODE, "inet",
   1861 		       SYSCTL_DESCR("PF_INET related settings"),
   1862 		       NULL, 0, NULL, 0,
   1863 		       CTL_NET, PF_INET, CTL_EOL);
   1864 	sysctl_createv(clog, 0, NULL, NULL,
   1865 		       CTLFLAG_PERMANENT,
   1866 		       CTLTYPE_NODE, "ip",
   1867 		       SYSCTL_DESCR("IPv4 related settings"),
   1868 		       NULL, 0, NULL, 0,
   1869 		       CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
   1870 
   1871 	sysctl_createv(clog, 0, NULL, NULL,
   1872 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1873 		       CTLTYPE_INT, "subnetsarelocal",
   1874 		       SYSCTL_DESCR("Whether logical subnets are considered "
   1875 				    "local"),
   1876 		       NULL, 0, &subnetsarelocal, 0,
   1877 		       CTL_NET, PF_INET, IPPROTO_IP,
   1878 		       IPCTL_SUBNETSARELOCAL, CTL_EOL);
   1879 	sysctl_createv(clog, 0, NULL, NULL,
   1880 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   1881 		       CTLTYPE_INT, "hostzerobroadcast",
   1882 		       SYSCTL_DESCR("All zeroes address is broadcast address"),
   1883 		       NULL, 0, &hostzeroisbroadcast, 0,
   1884 		       CTL_NET, PF_INET, IPPROTO_IP,
   1885 		       IPCTL_HOSTZEROBROADCAST, CTL_EOL);
   1886 }
   1887 
   1888 static struct lltable *
   1889 in_lltattach(struct ifnet *ifp)
   1890 {
   1891 	struct lltable *llt;
   1892 
   1893 	llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
   1894 	llt->llt_af = AF_INET;
   1895 	llt->llt_ifp = ifp;
   1896 
   1897 	llt->llt_lookup = in_lltable_lookup;
   1898 	llt->llt_create = in_lltable_create;
   1899 	llt->llt_delete = in_lltable_delete;
   1900 #if 0
   1901 	llt->llt_dump_entry = in_lltable_dump_entry;
   1902 #endif
   1903 	llt->llt_hash = in_lltable_hash;
   1904 	llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
   1905 	llt->llt_free_entry = in_lltable_free_entry;
   1906 	llt->llt_match_prefix = in_lltable_match_prefix;
   1907 	lltable_link(llt);
   1908 
   1909 	return (llt);
   1910 }
   1911 
   1912 void *
   1913 in_domifattach(struct ifnet *ifp)
   1914 {
   1915 	struct in_ifinfo *ii;
   1916 
   1917 	ii = kmem_zalloc(sizeof(struct in_ifinfo), KM_SLEEP);
   1918 	KASSERT(ii != NULL);
   1919 
   1920 	ii->ii_llt = in_lltattach(ifp);
   1921 
   1922 #ifdef IPSELSRC
   1923 	ii->ii_selsrc = in_selsrc_domifattach(ifp);
   1924 	KASSERT(ii->ii_selsrc != NULL);
   1925 #endif
   1926 
   1927 	return ii;
   1928 }
   1929 
   1930 void
   1931 in_domifdetach(struct ifnet *ifp, void *aux)
   1932 {
   1933 	struct in_ifinfo *ii = aux;
   1934 
   1935 #ifdef IPSELSRC
   1936 	in_selsrc_domifdetach(ifp, ii->ii_selsrc);
   1937 #endif
   1938 	lltable_free(ii->ii_llt);
   1939 	kmem_free(ii, sizeof(struct in_ifinfo));
   1940 }
   1941