Home | History | Annotate | Line # | Download | only in netinet
in.c revision 1.199
      1 /*	$NetBSD: in.c,v 1.199 2017/03/17 17:26:20 roy 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.199 2017/03/17 17:26:20 roy 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 #include "opt_net_mpsafe.h"
    103 #endif
    104 
    105 #include <sys/param.h>
    106 #include <sys/ioctl.h>
    107 #include <sys/errno.h>
    108 #include <sys/kernel.h>
    109 #include <sys/malloc.h>
    110 #include <sys/socket.h>
    111 #include <sys/socketvar.h>
    112 #include <sys/sysctl.h>
    113 #include <sys/systm.h>
    114 #include <sys/proc.h>
    115 #include <sys/syslog.h>
    116 #include <sys/kauth.h>
    117 #include <sys/kmem.h>
    118 
    119 #include <sys/cprng.h>
    120 
    121 #include <net/if.h>
    122 #include <net/route.h>
    123 #include <net/pfil.h>
    124 
    125 #include <net/if_arp.h>
    126 #include <net/if_ether.h>
    127 #include <net/if_types.h>
    128 #include <net/if_llatbl.h>
    129 #include <net/if_dl.h>
    130 
    131 #include <netinet/in_systm.h>
    132 #include <netinet/in.h>
    133 #include <netinet/in_var.h>
    134 #include <netinet/ip.h>
    135 #include <netinet/ip_var.h>
    136 #include <netinet/in_ifattach.h>
    137 #include <netinet/in_pcb.h>
    138 #include <netinet/in_selsrc.h>
    139 #include <netinet/if_inarp.h>
    140 #include <netinet/ip_mroute.h>
    141 #include <netinet/igmp_var.h>
    142 
    143 #ifdef IPSELSRC
    144 #include <netinet/in_selsrc.h>
    145 #endif
    146 
    147 static u_int	in_mask2len(struct in_addr *);
    148 static void	in_len2mask(struct in_addr *, u_int);
    149 static int	in_lifaddr_ioctl(struct socket *, u_long, void *,
    150 	struct ifnet *);
    151 
    152 static void	in_addrhash_insert_locked(struct in_ifaddr *);
    153 static void	in_addrhash_remove_locked(struct in_ifaddr *);
    154 
    155 static int	in_addprefix(struct in_ifaddr *, int);
    156 static void	in_scrubaddr(struct in_ifaddr *);
    157 static int	in_scrubprefix(struct in_ifaddr *);
    158 static void	in_sysctl_init(struct sysctllog **);
    159 
    160 #ifndef SUBNETSARELOCAL
    161 #define	SUBNETSARELOCAL	1
    162 #endif
    163 
    164 #ifndef HOSTZEROBROADCAST
    165 #define HOSTZEROBROADCAST 0
    166 #endif
    167 
    168 /* Note: 61, 127, 251, 509, 1021, 2039 are good. */
    169 #ifndef IN_MULTI_HASH_SIZE
    170 #define IN_MULTI_HASH_SIZE	509
    171 #endif
    172 
    173 static int			subnetsarelocal = SUBNETSARELOCAL;
    174 static int			hostzeroisbroadcast = HOSTZEROBROADCAST;
    175 
    176 /*
    177  * This list is used to keep track of in_multi chains which belong to
    178  * deleted interface addresses.  We use in_ifaddr so that a chain head
    179  * won't be deallocated until all multicast address record are deleted.
    180  */
    181 
    182 LIST_HEAD(in_multihashhead, in_multi);		/* Type of the hash head */
    183 
    184 static struct pool		inmulti_pool;
    185 static u_int			in_multientries;
    186 static struct in_multihashhead *in_multihashtbl;
    187 static u_long			in_multihash;
    188 static krwlock_t		in_multilock;
    189 
    190 #define IN_MULTI_HASH(x, ifp) \
    191     (in_multihashtbl[(u_long)((x) ^ (ifp->if_index)) % IN_MULTI_HASH_SIZE])
    192 
    193 /* XXX DEPRECATED. Keep them to avoid breaking kvm(3) users. */
    194 struct in_ifaddrhashhead *	in_ifaddrhashtbl;
    195 u_long				in_ifaddrhash;
    196 struct in_ifaddrhead		in_ifaddrhead;
    197 static kmutex_t			in_ifaddr_lock;
    198 
    199 pserialize_t			in_ifaddrhash_psz;
    200 struct pslist_head *		in_ifaddrhashtbl_pslist;
    201 u_long				in_ifaddrhash_pslist;
    202 struct pslist_head		in_ifaddrhead_pslist;
    203 
    204 void
    205 in_init(void)
    206 {
    207 	pool_init(&inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl",
    208 	    NULL, IPL_SOFTNET);
    209 	TAILQ_INIT(&in_ifaddrhead);
    210 	PSLIST_INIT(&in_ifaddrhead_pslist);
    211 
    212 	in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
    213 	    &in_ifaddrhash);
    214 
    215 	in_ifaddrhash_psz = pserialize_create();
    216 	in_ifaddrhashtbl_pslist = hashinit(IN_IFADDR_HASH_SIZE, HASH_PSLIST,
    217 	    true, &in_ifaddrhash_pslist);
    218 	mutex_init(&in_ifaddr_lock, MUTEX_DEFAULT, IPL_NONE);
    219 
    220 	in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
    221 	    &in_multihash);
    222 	rw_init(&in_multilock);
    223 
    224 	in_sysctl_init(NULL);
    225 }
    226 
    227 /*
    228  * Return 1 if an internet address is for a ``local'' host
    229  * (one to which we have a connection).  If subnetsarelocal
    230  * is true, this includes other subnets of the local net.
    231  * Otherwise, it includes only the directly-connected (sub)nets.
    232  */
    233 int
    234 in_localaddr(struct in_addr in)
    235 {
    236 	struct in_ifaddr *ia;
    237 	int localaddr = 0;
    238 	int s = pserialize_read_enter();
    239 
    240 	if (subnetsarelocal) {
    241 		IN_ADDRLIST_READER_FOREACH(ia) {
    242 			if ((in.s_addr & ia->ia_netmask) == ia->ia_net) {
    243 				localaddr = 1;
    244 				break;
    245 			}
    246 		}
    247 	} else {
    248 		IN_ADDRLIST_READER_FOREACH(ia) {
    249 			if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet) {
    250 				localaddr = 1;
    251 				break;
    252 			}
    253 		}
    254 	}
    255 	pserialize_read_exit(s);
    256 
    257 	return localaddr;
    258 }
    259 
    260 /*
    261  * Determine whether an IP address is in a reserved set of addresses
    262  * that may not be forwarded, or whether datagrams to that destination
    263  * may be forwarded.
    264  */
    265 int
    266 in_canforward(struct in_addr in)
    267 {
    268 	u_int32_t net;
    269 
    270 	if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
    271 		return (0);
    272 	if (IN_CLASSA(in.s_addr)) {
    273 		net = in.s_addr & IN_CLASSA_NET;
    274 		if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
    275 			return (0);
    276 	}
    277 	return (1);
    278 }
    279 
    280 /*
    281  * Trim a mask in a sockaddr
    282  */
    283 void
    284 in_socktrim(struct sockaddr_in *ap)
    285 {
    286 	char *cplim = (char *) &ap->sin_addr;
    287 	char *cp = (char *) (&ap->sin_addr + 1);
    288 
    289 	ap->sin_len = 0;
    290 	while (--cp >= cplim)
    291 		if (*cp) {
    292 			(ap)->sin_len = cp - (char *) (ap) + 1;
    293 			break;
    294 		}
    295 }
    296 
    297 /*
    298  *  Routine to take an Internet address and convert into a
    299  *  "dotted quad" representation for printing.
    300  *  Caller has to make sure that buf is at least INET_ADDRSTRLEN long.
    301  */
    302 const char *
    303 in_fmtaddr(char *buf, struct in_addr addr)
    304 {
    305 	addr.s_addr = ntohl(addr.s_addr);
    306 
    307 	snprintf(buf, INET_ADDRSTRLEN, "%d.%d.%d.%d",
    308 	    (addr.s_addr >> 24) & 0xFF,
    309 	    (addr.s_addr >> 16) & 0xFF,
    310 	    (addr.s_addr >>  8) & 0xFF,
    311 	    (addr.s_addr >>  0) & 0xFF);
    312 	return buf;
    313 }
    314 
    315 /*
    316  * Maintain the "in_maxmtu" variable, which is the largest
    317  * mtu for non-local interfaces with AF_INET addresses assigned
    318  * to them that are up.
    319  */
    320 unsigned long in_maxmtu;
    321 
    322 void
    323 in_setmaxmtu(void)
    324 {
    325 	struct in_ifaddr *ia;
    326 	struct ifnet *ifp;
    327 	unsigned long maxmtu = 0;
    328 	int s = pserialize_read_enter();
    329 
    330 	IN_ADDRLIST_READER_FOREACH(ia) {
    331 		if ((ifp = ia->ia_ifp) == 0)
    332 			continue;
    333 		if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
    334 			continue;
    335 		if (ifp->if_mtu > maxmtu)
    336 			maxmtu = ifp->if_mtu;
    337 	}
    338 	if (maxmtu)
    339 		in_maxmtu = maxmtu;
    340 	pserialize_read_exit(s);
    341 }
    342 
    343 static u_int
    344 in_mask2len(struct in_addr *mask)
    345 {
    346 	u_int x, y;
    347 	u_char *p;
    348 
    349 	p = (u_char *)mask;
    350 	for (x = 0; x < sizeof(*mask); x++) {
    351 		if (p[x] != 0xff)
    352 			break;
    353 	}
    354 	y = 0;
    355 	if (x < sizeof(*mask)) {
    356 		for (y = 0; y < NBBY; y++) {
    357 			if ((p[x] & (0x80 >> y)) == 0)
    358 				break;
    359 		}
    360 	}
    361 	return x * NBBY + y;
    362 }
    363 
    364 static void
    365 in_len2mask(struct in_addr *mask, u_int len)
    366 {
    367 	u_int i;
    368 	u_char *p;
    369 
    370 	p = (u_char *)mask;
    371 	memset(mask, 0, sizeof(*mask));
    372 	for (i = 0; i < len / NBBY; i++)
    373 		p[i] = 0xff;
    374 	if (len % NBBY)
    375 		p[i] = (0xff00 >> (len % NBBY)) & 0xff;
    376 }
    377 
    378 /*
    379  * Generic internet control operations (ioctl's).
    380  * Ifp is 0 if not an interface-specific ioctl.
    381  */
    382 /* ARGSUSED */
    383 static int
    384 in_control0(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
    385 {
    386 	struct ifreq *ifr = (struct ifreq *)data;
    387 	struct in_ifaddr *ia = NULL;
    388 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
    389 	struct sockaddr_in oldaddr, *new_dstaddr;
    390 	int error, hostIsNew, maskIsNew;
    391 	int newifaddr = 0;
    392 	bool run_hook = false;
    393 	bool need_reinsert = false;
    394 	struct psref psref;
    395 	int bound;
    396 
    397 	switch (cmd) {
    398 	case SIOCALIFADDR:
    399 	case SIOCDLIFADDR:
    400 	case SIOCGLIFADDR:
    401 		if (ifp == NULL)
    402 			return EINVAL;
    403 		return in_lifaddr_ioctl(so, cmd, data, ifp);
    404 	case SIOCGIFADDRPREF:
    405 	case SIOCSIFADDRPREF:
    406 		if (ifp == NULL)
    407 			return EINVAL;
    408 		return ifaddrpref_ioctl(so, cmd, data, ifp);
    409 	}
    410 
    411 	bound = curlwp_bind();
    412 	/*
    413 	 * Find address for this interface, if it exists.
    414 	 */
    415 	if (ifp != NULL)
    416 		ia = in_get_ia_from_ifp_psref(ifp, &psref);
    417 
    418 	hostIsNew = 1;		/* moved here to appease gcc */
    419 	switch (cmd) {
    420 	case SIOCAIFADDR:
    421 	case SIOCDIFADDR:
    422 	case SIOCGIFALIAS:
    423 	case SIOCGIFAFLAG_IN:
    424 		if (ifra->ifra_addr.sin_family == AF_INET) {
    425 			int s;
    426 
    427 			if (ia != NULL)
    428 				ia4_release(ia, &psref);
    429 			s = pserialize_read_enter();
    430 			IN_ADDRHASH_READER_FOREACH(ia,
    431 			    ifra->ifra_addr.sin_addr.s_addr) {
    432 				if (ia->ia_ifp == ifp &&
    433 				    in_hosteq(ia->ia_addr.sin_addr,
    434 				    ifra->ifra_addr.sin_addr))
    435 					break;
    436 			}
    437 			if (ia != NULL)
    438 				ia4_acquire(ia, &psref);
    439 			pserialize_read_exit(s);
    440 		}
    441 		if ((cmd == SIOCDIFADDR ||
    442 		    cmd == SIOCGIFALIAS ||
    443 		    cmd == SIOCGIFAFLAG_IN) &&
    444 		    ia == NULL) {
    445 			error = EADDRNOTAVAIL;
    446 			goto out;
    447 		}
    448 
    449 		if (cmd == SIOCDIFADDR &&
    450 		    ifra->ifra_addr.sin_family == AF_UNSPEC) {
    451 			ifra->ifra_addr.sin_family = AF_INET;
    452 		}
    453 		/* FALLTHROUGH */
    454 	case SIOCSIFADDR:
    455 		if (ia == NULL || ia->ia_addr.sin_family != AF_INET)
    456 			;
    457 		else if (ifra->ifra_addr.sin_len == 0) {
    458 			ifra->ifra_addr = ia->ia_addr;
    459 			hostIsNew = 0;
    460 		} else if (in_hosteq(ia->ia_addr.sin_addr,
    461 		           ifra->ifra_addr.sin_addr))
    462 			hostIsNew = 0;
    463 		/* FALLTHROUGH */
    464 	case SIOCSIFDSTADDR:
    465 		if (ifra->ifra_addr.sin_family != AF_INET) {
    466 			error = EAFNOSUPPORT;
    467 			goto out;
    468 		}
    469 		/* FALLTHROUGH */
    470 	case SIOCSIFNETMASK:
    471 		if (ifp == NULL)
    472 			panic("in_control");
    473 
    474 		if (cmd == SIOCGIFALIAS || cmd == SIOCGIFAFLAG_IN)
    475 			break;
    476 
    477 		if (ia == NULL &&
    478 		    (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR)) {
    479 			error = EADDRNOTAVAIL;
    480 			goto out;
    481 		}
    482 
    483 		if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
    484 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
    485 		    NULL) != 0) {
    486 			error = EPERM;
    487 			goto out;
    488 		}
    489 
    490 		if (ia == NULL) {
    491 			ia = malloc(sizeof(*ia), M_IFADDR, M_WAITOK|M_ZERO);
    492 			if (ia == NULL) {
    493 				error = ENOBUFS;
    494 				goto out;
    495 			}
    496 			ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
    497 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    498 			ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
    499 #ifdef IPSELSRC
    500 			ia->ia_ifa.ifa_getifa = in_getifa;
    501 #else /* IPSELSRC */
    502 			ia->ia_ifa.ifa_getifa = NULL;
    503 #endif /* IPSELSRC */
    504 			ia->ia_sockmask.sin_len = 8;
    505 			ia->ia_sockmask.sin_family = AF_INET;
    506 			if (ifp->if_flags & IFF_BROADCAST) {
    507 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
    508 				ia->ia_broadaddr.sin_family = AF_INET;
    509 			}
    510 			ia->ia_ifp = ifp;
    511 			ia->ia_idsalt = cprng_fast32() % 65535;
    512 			LIST_INIT(&ia->ia_multiaddrs);
    513 			IN_ADDRHASH_ENTRY_INIT(ia);
    514 			IN_ADDRLIST_ENTRY_INIT(ia);
    515 			ifa_psref_init(&ia->ia_ifa);
    516 
    517 			newifaddr = 1;
    518 		}
    519 		break;
    520 
    521 	case SIOCSIFBRDADDR:
    522 		if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
    523 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
    524 		    NULL) != 0) {
    525 			error = EPERM;
    526 			goto out;
    527 		}
    528 		/* FALLTHROUGH */
    529 
    530 	case SIOCGIFADDR:
    531 	case SIOCGIFNETMASK:
    532 	case SIOCGIFDSTADDR:
    533 	case SIOCGIFBRDADDR:
    534 		if (ia == NULL) {
    535 			error = EADDRNOTAVAIL;
    536 			goto out;
    537 		}
    538 		break;
    539 	}
    540 	error = 0;
    541 	switch (cmd) {
    542 
    543 	case SIOCGIFADDR:
    544 		ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr));
    545 		break;
    546 
    547 	case SIOCGIFBRDADDR:
    548 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
    549 			error = EINVAL;
    550 			goto out;
    551 		}
    552 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr));
    553 		break;
    554 
    555 	case SIOCGIFDSTADDR:
    556 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
    557 			error = EINVAL;
    558 			goto out;
    559 		}
    560 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr));
    561 		break;
    562 
    563 	case SIOCGIFNETMASK:
    564 		/*
    565 		 * We keep the number of trailing zero bytes the sin_len field
    566 		 * of ia_sockmask, so we fix this before we pass it back to
    567 		 * userland.
    568 		 */
    569 		oldaddr = ia->ia_sockmask;
    570 		oldaddr.sin_len = sizeof(struct sockaddr_in);
    571 		ifreq_setaddr(cmd, ifr, (const void *)&oldaddr);
    572 		break;
    573 
    574 	case SIOCSIFDSTADDR:
    575 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
    576 			error = EINVAL;
    577 			goto out;
    578 		}
    579 		oldaddr = ia->ia_dstaddr;
    580 		ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr));
    581 		if ((error = if_addr_init(ifp, &ia->ia_ifa, false)) != 0) {
    582 			ia->ia_dstaddr = oldaddr;
    583 			goto out;
    584 		}
    585 		if (ia->ia_flags & IFA_ROUTE) {
    586 			ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
    587 			rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
    588 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
    589 			rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP);
    590 		}
    591 		break;
    592 
    593 	case SIOCSIFBRDADDR:
    594 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
    595 			error = EINVAL;
    596 			goto out;
    597 		}
    598 		ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr));
    599 		break;
    600 
    601 	case SIOCSIFADDR:
    602 		if (!newifaddr) {
    603 			in_addrhash_remove(ia);
    604 			need_reinsert = true;
    605 		}
    606 		error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)),
    607 		    NULL, 1);
    608 
    609 		run_hook = true;
    610 		break;
    611 
    612 	case SIOCSIFNETMASK:
    613 		in_scrubprefix(ia);
    614 		ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr));
    615 		ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
    616 		if (!newifaddr) {
    617 			in_addrhash_remove(ia);
    618 			need_reinsert = true;
    619 		}
    620 		error = in_ifinit(ifp, ia, NULL, NULL, 0);
    621 		break;
    622 
    623 	case SIOCAIFADDR:
    624 		maskIsNew = 0;
    625 		if (ifra->ifra_mask.sin_len) {
    626 			in_scrubprefix(ia);
    627 			ia->ia_sockmask = ifra->ifra_mask;
    628 			ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
    629 			maskIsNew = 1;
    630 		}
    631 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
    632 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
    633 			new_dstaddr = &ifra->ifra_dstaddr;
    634 			maskIsNew  = 1; /* We lie; but the effect's the same */
    635 		} else
    636 			new_dstaddr = NULL;
    637 		if (ifra->ifra_addr.sin_family == AF_INET &&
    638 		    (hostIsNew || maskIsNew)) {
    639 			if (!newifaddr) {
    640 				in_addrhash_remove(ia);
    641 				need_reinsert = true;
    642 			}
    643 			error = in_ifinit(ifp, ia, &ifra->ifra_addr,
    644 			    new_dstaddr, 0);
    645 		}
    646 		if ((ifp->if_flags & IFF_BROADCAST) &&
    647 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
    648 			ia->ia_broadaddr = ifra->ifra_broadaddr;
    649 		run_hook = true;
    650 		break;
    651 
    652 	case SIOCGIFALIAS:
    653 		ifra->ifra_mask = ia->ia_sockmask;
    654 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
    655 		    (ia->ia_dstaddr.sin_family == AF_INET))
    656 			ifra->ifra_dstaddr = ia->ia_dstaddr;
    657 		else if ((ifp->if_flags & IFF_BROADCAST) &&
    658 		    (ia->ia_broadaddr.sin_family == AF_INET))
    659 			ifra->ifra_broadaddr = ia->ia_broadaddr;
    660 		else
    661 			memset(&ifra->ifra_broadaddr, 0,
    662 			      sizeof(ifra->ifra_broadaddr));
    663 		break;
    664 
    665 	case SIOCGIFAFLAG_IN:
    666 		ifr->ifr_addrflags = ia->ia4_flags;
    667 		break;
    668 
    669 	case SIOCDIFADDR:
    670 		ia4_release(ia, &psref);
    671 		ifaref(&ia->ia_ifa);
    672 		in_purgeaddr(&ia->ia_ifa);
    673 		pfil_run_addrhooks(if_pfil, cmd, &ia->ia_ifa);
    674 		ifafree(&ia->ia_ifa);
    675 		ia = NULL;
    676 		break;
    677 
    678 #ifdef MROUTING
    679 	case SIOCGETVIFCNT:
    680 	case SIOCGETSGCNT:
    681 		error = mrt_ioctl(so, cmd, data);
    682 		break;
    683 #endif /* MROUTING */
    684 
    685 	default:
    686 		error = ENOTTY;
    687 		goto out;
    688 	}
    689 
    690 	/*
    691 	 * XXX insert regardless of error to make in_purgeaddr below work.
    692 	 * Need to improve.
    693 	 */
    694 	if (newifaddr) {
    695 		ifaref(&ia->ia_ifa);
    696 		ifa_insert(ifp, &ia->ia_ifa);
    697 
    698 		mutex_enter(&in_ifaddr_lock);
    699 		TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list);
    700 		IN_ADDRLIST_WRITER_INSERT_TAIL(ia);
    701 		in_addrhash_insert_locked(ia);
    702 		mutex_exit(&in_ifaddr_lock);
    703 	} else if (need_reinsert) {
    704 		in_addrhash_insert(ia);
    705 	}
    706 
    707 	if (error == 0) {
    708 		if (run_hook)
    709 			pfil_run_addrhooks(if_pfil, cmd, &ia->ia_ifa);
    710 	} else if (newifaddr) {
    711 		KASSERT(ia != NULL);
    712 		in_purgeaddr(&ia->ia_ifa);
    713 		ia = NULL;
    714 	}
    715 
    716 out:
    717 	if (!newifaddr && ia != NULL)
    718 		ia4_release(ia, &psref);
    719 	curlwp_bindx(bound);
    720 	return error;
    721 }
    722 
    723 int
    724 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp)
    725 {
    726 	int error;
    727 
    728 #ifndef NET_MPSAFE
    729 	mutex_enter(softnet_lock);
    730 #endif
    731 	error = in_control0(so, cmd, data, ifp);
    732 #ifndef NET_MPSAFE
    733 	mutex_exit(softnet_lock);
    734 #endif
    735 
    736 	return error;
    737 }
    738 
    739 /* Add ownaddr as loopback rtentry. */
    740 static void
    741 in_ifaddlocal(struct ifaddr *ifa)
    742 {
    743 	struct in_ifaddr *ia;
    744 
    745 	ia = (struct in_ifaddr *)ifa;
    746 	if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY ||
    747 	    (ia->ia_ifp->if_flags & IFF_POINTOPOINT &&
    748 	    in_hosteq(ia->ia_dstaddr.sin_addr, ia->ia_addr.sin_addr)))
    749 	{
    750 		rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
    751 		return;
    752 	}
    753 
    754 	rt_ifa_addlocal(ifa);
    755 }
    756 
    757 /* Remove loopback entry of ownaddr */
    758 static void
    759 in_ifremlocal(struct ifaddr *ifa)
    760 {
    761 	struct in_ifaddr *ia, *p;
    762 	struct ifaddr *alt_ifa = NULL;
    763 	int ia_count = 0;
    764 	int s;
    765 	struct psref psref;
    766 	int bound = curlwp_bind();
    767 
    768 	ia = (struct in_ifaddr *)ifa;
    769 	/* Delete the entry if exactly one ifaddr matches the
    770 	 * address, ifa->ifa_addr. */
    771 	s = pserialize_read_enter();
    772 	IN_ADDRLIST_READER_FOREACH(p) {
    773 		if (!in_hosteq(p->ia_addr.sin_addr, ia->ia_addr.sin_addr))
    774 			continue;
    775 		if (p->ia_ifp != ia->ia_ifp)
    776 			alt_ifa = &p->ia_ifa;
    777 		if (++ia_count > 1 && alt_ifa != NULL)
    778 			break;
    779 	}
    780 	if (alt_ifa != NULL && ia_count > 1)
    781 		ifa_acquire(alt_ifa, &psref);
    782 	pserialize_read_exit(s);
    783 
    784 	if (ia_count == 0)
    785 		goto out;
    786 
    787 	rt_ifa_remlocal(ifa, ia_count == 1 ? NULL : alt_ifa);
    788 	if (alt_ifa != NULL && ia_count > 1)
    789 		ifa_release(alt_ifa, &psref);
    790 out:
    791 	curlwp_bindx(bound);
    792 }
    793 
    794 static void
    795 in_scrubaddr(struct in_ifaddr *ia)
    796 {
    797 
    798 	/* stop DAD processing */
    799 	if (ia->ia_dad_stop != NULL)
    800 		ia->ia_dad_stop(&ia->ia_ifa);
    801 
    802 	in_scrubprefix(ia);
    803 	in_ifremlocal(&ia->ia_ifa);
    804 
    805 	mutex_enter(&in_ifaddr_lock);
    806 	if (ia->ia_allhosts != NULL) {
    807 		in_delmulti(ia->ia_allhosts);
    808 		ia->ia_allhosts = NULL;
    809 	}
    810 	mutex_exit(&in_ifaddr_lock);
    811 }
    812 
    813 /*
    814  * Depends on it isn't called in concurrent. It should be guaranteed
    815  * by ifa->ifa_ifp's ioctl lock. The possible callers are in_control
    816  * and if_purgeaddrs; the former is called iva ifa->ifa_ifp's ioctl
    817  * and the latter is called via ifa->ifa_ifp's if_detach. The functions
    818  * never be executed in concurrent.
    819  */
    820 void
    821 in_purgeaddr(struct ifaddr *ifa)
    822 {
    823 	struct in_ifaddr *ia = (void *) ifa;
    824 	struct ifnet *ifp = ifa->ifa_ifp;
    825 
    826 	KASSERT(!ifa_held(ifa));
    827 
    828 	ifa->ifa_flags |= IFA_DESTROYING;
    829 	in_scrubaddr(ia);
    830 
    831 	mutex_enter(&in_ifaddr_lock);
    832 	in_addrhash_remove_locked(ia);
    833 	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list);
    834 	IN_ADDRLIST_WRITER_REMOVE(ia);
    835 	ifa_remove(ifp, &ia->ia_ifa);
    836 #ifdef NET_MPSAFE
    837 	pserialize_perform(in_ifaddrhash_psz);
    838 #endif
    839 	mutex_exit(&in_ifaddr_lock);
    840 	IN_ADDRHASH_ENTRY_DESTROY(ia);
    841 	IN_ADDRLIST_ENTRY_DESTROY(ia);
    842 	ifafree(&ia->ia_ifa);
    843 	in_setmaxmtu();
    844 }
    845 
    846 static void
    847 in_addrhash_insert_locked(struct in_ifaddr *ia)
    848 {
    849 
    850 	KASSERT(mutex_owned(&in_ifaddr_lock));
    851 
    852 	LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia,
    853 	    ia_hash);
    854 	IN_ADDRHASH_ENTRY_INIT(ia);
    855 	IN_ADDRHASH_WRITER_INSERT_HEAD(ia);
    856 }
    857 
    858 void
    859 in_addrhash_insert(struct in_ifaddr *ia)
    860 {
    861 
    862 	mutex_enter(&in_ifaddr_lock);
    863 	in_addrhash_insert_locked(ia);
    864 	mutex_exit(&in_ifaddr_lock);
    865 }
    866 
    867 static void
    868 in_addrhash_remove_locked(struct in_ifaddr *ia)
    869 {
    870 
    871 	KASSERT(mutex_owned(&in_ifaddr_lock));
    872 
    873 	LIST_REMOVE(ia, ia_hash);
    874 	IN_ADDRHASH_WRITER_REMOVE(ia);
    875 }
    876 
    877 void
    878 in_addrhash_remove(struct in_ifaddr *ia)
    879 {
    880 
    881 	mutex_enter(&in_ifaddr_lock);
    882 	in_addrhash_remove_locked(ia);
    883 #ifdef NET_MPSAFE
    884 	pserialize_perform(in_ifaddrhash_psz);
    885 #endif
    886 	mutex_exit(&in_ifaddr_lock);
    887 	IN_ADDRHASH_ENTRY_DESTROY(ia);
    888 }
    889 
    890 void
    891 in_purgeif(struct ifnet *ifp)		/* MUST be called at splsoftnet() */
    892 {
    893 	if_purgeaddrs(ifp, AF_INET, in_purgeaddr);
    894 	igmp_purgeif(ifp);		/* manipulates pools */
    895 #ifdef MROUTING
    896 	ip_mrouter_detach(ifp);
    897 #endif
    898 }
    899 
    900 /*
    901  * SIOC[GAD]LIFADDR.
    902  *	SIOCGLIFADDR: get first address. (???)
    903  *	SIOCGLIFADDR with IFLR_PREFIX:
    904  *		get first address that matches the specified prefix.
    905  *	SIOCALIFADDR: add the specified address.
    906  *	SIOCALIFADDR with IFLR_PREFIX:
    907  *		EINVAL since we can't deduce hostid part of the address.
    908  *	SIOCDLIFADDR: delete the specified address.
    909  *	SIOCDLIFADDR with IFLR_PREFIX:
    910  *		delete the first address that matches the specified prefix.
    911  * return values:
    912  *	EINVAL on invalid parameters
    913  *	EADDRNOTAVAIL on prefix match failed/specified address not found
    914  *	other values may be returned from in_ioctl()
    915  */
    916 static int
    917 in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data,
    918     struct ifnet *ifp)
    919 {
    920 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
    921 	struct ifaddr *ifa;
    922 	struct sockaddr *sa;
    923 
    924 	/* sanity checks */
    925 	if (data == NULL || ifp == NULL) {
    926 		panic("invalid argument to in_lifaddr_ioctl");
    927 		/*NOTRECHED*/
    928 	}
    929 
    930 	switch (cmd) {
    931 	case SIOCGLIFADDR:
    932 		/* address must be specified on GET with IFLR_PREFIX */
    933 		if ((iflr->flags & IFLR_PREFIX) == 0)
    934 			break;
    935 		/*FALLTHROUGH*/
    936 	case SIOCALIFADDR:
    937 	case SIOCDLIFADDR:
    938 		/* address must be specified on ADD and DELETE */
    939 		sa = (struct sockaddr *)&iflr->addr;
    940 		if (sa->sa_family != AF_INET)
    941 			return EINVAL;
    942 		if (sa->sa_len != sizeof(struct sockaddr_in))
    943 			return EINVAL;
    944 		/* XXX need improvement */
    945 		sa = (struct sockaddr *)&iflr->dstaddr;
    946 		if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET)
    947 			return EINVAL;
    948 		if (sa->sa_len != 0 && sa->sa_len != sizeof(struct sockaddr_in))
    949 			return EINVAL;
    950 		break;
    951 	default: /*shouldn't happen*/
    952 #if 0
    953 		panic("invalid cmd to in_lifaddr_ioctl");
    954 		/*NOTREACHED*/
    955 #else
    956 		return EOPNOTSUPP;
    957 #endif
    958 	}
    959 	if (sizeof(struct in_addr) * NBBY < iflr->prefixlen)
    960 		return EINVAL;
    961 
    962 	switch (cmd) {
    963 	case SIOCALIFADDR:
    964 	    {
    965 		struct in_aliasreq ifra;
    966 
    967 		if (iflr->flags & IFLR_PREFIX)
    968 			return EINVAL;
    969 
    970 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
    971 		memset(&ifra, 0, sizeof(ifra));
    972 		memcpy(ifra.ifra_name, iflr->iflr_name,
    973 			sizeof(ifra.ifra_name));
    974 
    975 		memcpy(&ifra.ifra_addr, &iflr->addr,
    976 			((struct sockaddr *)&iflr->addr)->sa_len);
    977 
    978 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
    979 			memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr,
    980 				((struct sockaddr *)&iflr->dstaddr)->sa_len);
    981 		}
    982 
    983 		ifra.ifra_mask.sin_family = AF_INET;
    984 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
    985 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
    986 
    987 		return in_control(so, SIOCAIFADDR, &ifra, ifp);
    988 	    }
    989 	case SIOCGLIFADDR:
    990 	case SIOCDLIFADDR:
    991 	    {
    992 		struct in_ifaddr *ia;
    993 		struct in_addr mask, candidate, match;
    994 		struct sockaddr_in *sin;
    995 		int cmp, s;
    996 
    997 		memset(&mask, 0, sizeof(mask));
    998 		memset(&match, 0, sizeof(match));	/* XXX gcc */
    999 		if (iflr->flags & IFLR_PREFIX) {
   1000 			/* lookup a prefix rather than address. */
   1001 			in_len2mask(&mask, iflr->prefixlen);
   1002 
   1003 			sin = (struct sockaddr_in *)&iflr->addr;
   1004 			match.s_addr = sin->sin_addr.s_addr;
   1005 			match.s_addr &= mask.s_addr;
   1006 
   1007 			/* if you set extra bits, that's wrong */
   1008 			if (match.s_addr != sin->sin_addr.s_addr)
   1009 				return EINVAL;
   1010 
   1011 			cmp = 1;
   1012 		} else {
   1013 			if (cmd == SIOCGLIFADDR) {
   1014 				/* on getting an address, take the 1st match */
   1015 				cmp = 0;	/*XXX*/
   1016 			} else {
   1017 				/* on deleting an address, do exact match */
   1018 				in_len2mask(&mask, 32);
   1019 				sin = (struct sockaddr_in *)&iflr->addr;
   1020 				match.s_addr = sin->sin_addr.s_addr;
   1021 
   1022 				cmp = 1;
   1023 			}
   1024 		}
   1025 
   1026 		s = pserialize_read_enter();
   1027 		IFADDR_READER_FOREACH(ifa, ifp) {
   1028 			if (ifa->ifa_addr->sa_family != AF_INET)
   1029 				continue;
   1030 			if (cmp == 0)
   1031 				break;
   1032 			candidate.s_addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
   1033 			candidate.s_addr &= mask.s_addr;
   1034 			if (candidate.s_addr == match.s_addr)
   1035 				break;
   1036 		}
   1037 		if (ifa == NULL) {
   1038 			pserialize_read_exit(s);
   1039 			return EADDRNOTAVAIL;
   1040 		}
   1041 		ia = (struct in_ifaddr *)ifa;
   1042 
   1043 		if (cmd == SIOCGLIFADDR) {
   1044 			/* fill in the if_laddrreq structure */
   1045 			memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin_len);
   1046 
   1047 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
   1048 				memcpy(&iflr->dstaddr, &ia->ia_dstaddr,
   1049 					ia->ia_dstaddr.sin_len);
   1050 			} else
   1051 				memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr));
   1052 
   1053 			iflr->prefixlen =
   1054 				in_mask2len(&ia->ia_sockmask.sin_addr);
   1055 
   1056 			iflr->flags = 0;	/*XXX*/
   1057 			pserialize_read_exit(s);
   1058 
   1059 			return 0;
   1060 		} else {
   1061 			struct in_aliasreq ifra;
   1062 
   1063 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
   1064 			memset(&ifra, 0, sizeof(ifra));
   1065 			memcpy(ifra.ifra_name, iflr->iflr_name,
   1066 				sizeof(ifra.ifra_name));
   1067 
   1068 			memcpy(&ifra.ifra_addr, &ia->ia_addr,
   1069 				ia->ia_addr.sin_len);
   1070 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
   1071 				memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr,
   1072 					ia->ia_dstaddr.sin_len);
   1073 			}
   1074 			memcpy(&ifra.ifra_dstaddr, &ia->ia_sockmask,
   1075 				ia->ia_sockmask.sin_len);
   1076 			pserialize_read_exit(s);
   1077 
   1078 			return in_control(so, SIOCDIFADDR, &ifra, ifp);
   1079 		}
   1080 	    }
   1081 	}
   1082 
   1083 	return EOPNOTSUPP;	/*just for safety*/
   1084 }
   1085 
   1086 /*
   1087  * Initialize an interface's internet address
   1088  * and routing table entry.
   1089  */
   1090 int
   1091 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
   1092     const struct sockaddr_in *sin, const struct sockaddr_in *dst, int scrub)
   1093 {
   1094 	u_int32_t i;
   1095 	struct sockaddr_in oldaddr, olddst;
   1096 	int s, oldflags, flags = RTF_UP, error, hostIsNew;
   1097 
   1098 	if (sin == NULL)
   1099 		sin = &ia->ia_addr;
   1100 	if (dst == NULL)
   1101 		dst = &ia->ia_dstaddr;
   1102 
   1103 	/*
   1104 	 * Set up new addresses.
   1105 	 */
   1106 	oldaddr = ia->ia_addr;
   1107 	olddst = ia->ia_dstaddr;
   1108 	oldflags = ia->ia4_flags;
   1109 	ia->ia_addr = *sin;
   1110 	ia->ia_dstaddr = *dst;
   1111 	hostIsNew = oldaddr.sin_family != AF_INET ||
   1112 	    !in_hosteq(ia->ia_addr.sin_addr, oldaddr.sin_addr);
   1113 	if (!scrub)
   1114 		scrub = oldaddr.sin_family != ia->ia_dstaddr.sin_family ||
   1115 		    !in_hosteq(ia->ia_dstaddr.sin_addr, olddst.sin_addr);
   1116 
   1117 	/*
   1118 	 * Configure address flags.
   1119 	 * We need to do this early because they maybe adjusted
   1120 	 * by if_addr_init depending on the address.
   1121 	 */
   1122 	if (ia->ia4_flags & IN_IFF_DUPLICATED) {
   1123 		ia->ia4_flags &= ~IN_IFF_DUPLICATED;
   1124 		hostIsNew = 1;
   1125 	}
   1126 	if (ifp->if_link_state == LINK_STATE_DOWN) {
   1127 		ia->ia4_flags |= IN_IFF_DETACHED;
   1128 		ia->ia4_flags &= ~IN_IFF_TENTATIVE;
   1129 	} else if (hostIsNew && if_do_dad(ifp))
   1130 		ia->ia4_flags |= IN_IFF_TRYTENTATIVE;
   1131 
   1132 	/*
   1133 	 * Give the interface a chance to initialize
   1134 	 * if this is its first address,
   1135 	 * and to validate the address if necessary.
   1136 	 */
   1137 	s = splsoftnet();
   1138 	error = if_addr_init(ifp, &ia->ia_ifa, true);
   1139 	splx(s);
   1140 	/* Now clear the try tentative flag, it's job is done. */
   1141 	ia->ia4_flags &= ~IN_IFF_TRYTENTATIVE;
   1142 	if (error != 0) {
   1143 		ia->ia_addr = oldaddr;
   1144 		ia->ia_dstaddr = olddst;
   1145 		ia->ia4_flags = oldflags;
   1146 		return error;
   1147 	}
   1148 
   1149 	if (scrub || hostIsNew) {
   1150 		int newflags = ia->ia4_flags;
   1151 
   1152 		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
   1153 		ia->ia_ifa.ifa_dstaddr = sintosa(&olddst);
   1154 		ia->ia4_flags = oldflags;
   1155 		if (hostIsNew)
   1156 			in_scrubaddr(ia);
   1157 		else if (scrub)
   1158 			in_scrubprefix(ia);
   1159 		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
   1160 		ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
   1161 		ia->ia4_flags = newflags;
   1162 	}
   1163 
   1164 	i = ia->ia_addr.sin_addr.s_addr;
   1165 	if (ifp->if_flags & IFF_POINTOPOINT)
   1166 		ia->ia_netmask = INADDR_BROADCAST;	/* default to /32 */
   1167 	else if (IN_CLASSA(i))
   1168 		ia->ia_netmask = IN_CLASSA_NET;
   1169 	else if (IN_CLASSB(i))
   1170 		ia->ia_netmask = IN_CLASSB_NET;
   1171 	else
   1172 		ia->ia_netmask = IN_CLASSC_NET;
   1173 	/*
   1174 	 * The subnet mask usually includes at least the standard network part,
   1175 	 * but may may be smaller in the case of supernetting.
   1176 	 * If it is set, we believe it.
   1177 	 */
   1178 	if (ia->ia_subnetmask == 0) {
   1179 		ia->ia_subnetmask = ia->ia_netmask;
   1180 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
   1181 	} else
   1182 		ia->ia_netmask &= ia->ia_subnetmask;
   1183 
   1184 	ia->ia_net = i & ia->ia_netmask;
   1185 	ia->ia_subnet = i & ia->ia_subnetmask;
   1186 	in_socktrim(&ia->ia_sockmask);
   1187 
   1188 	/* re-calculate the "in_maxmtu" value */
   1189 	in_setmaxmtu();
   1190 
   1191 	ia->ia_ifa.ifa_metric = ifp->if_metric;
   1192 	if (ifp->if_flags & IFF_BROADCAST) {
   1193 		ia->ia_broadaddr.sin_addr.s_addr =
   1194 			ia->ia_subnet | ~ia->ia_subnetmask;
   1195 		ia->ia_netbroadcast.s_addr =
   1196 			ia->ia_net | ~ia->ia_netmask;
   1197 	} else if (ifp->if_flags & IFF_LOOPBACK) {
   1198 		ia->ia_dstaddr = ia->ia_addr;
   1199 		flags |= RTF_HOST;
   1200 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
   1201 		if (ia->ia_dstaddr.sin_family != AF_INET)
   1202 			return (0);
   1203 		flags |= RTF_HOST;
   1204 	}
   1205 
   1206 	/* Add the local route to the address */
   1207 	in_ifaddlocal(&ia->ia_ifa);
   1208 
   1209 	/* Add the prefix route for the address */
   1210 	error = in_addprefix(ia, flags);
   1211 
   1212 	/*
   1213 	 * If the interface supports multicast, join the "all hosts"
   1214 	 * multicast group on that interface.
   1215 	 */
   1216 	mutex_enter(&in_ifaddr_lock);
   1217 	if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
   1218 		struct in_addr addr;
   1219 
   1220 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
   1221 		ia->ia_allhosts = in_addmulti(&addr, ifp);
   1222 	}
   1223 	mutex_exit(&in_ifaddr_lock);
   1224 
   1225 	if (hostIsNew &&
   1226 	    ia->ia4_flags & IN_IFF_TENTATIVE &&
   1227 	    if_do_dad(ifp))
   1228 		ia->ia_dad_start((struct ifaddr *)ia);
   1229 
   1230 	return error;
   1231 }
   1232 
   1233 #define rtinitflags(x) \
   1234 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
   1235 	    ? RTF_HOST : 0)
   1236 
   1237 /*
   1238  * add a route to prefix ("connected route" in cisco terminology).
   1239  * does nothing if there's some interface address with the same prefix already.
   1240  */
   1241 static int
   1242 in_addprefix(struct in_ifaddr *target, int flags)
   1243 {
   1244 	struct in_ifaddr *ia;
   1245 	struct in_addr prefix, mask, p;
   1246 	int error;
   1247 	int s;
   1248 
   1249 	if ((flags & RTF_HOST) != 0)
   1250 		prefix = target->ia_dstaddr.sin_addr;
   1251 	else {
   1252 		prefix = target->ia_addr.sin_addr;
   1253 		mask = target->ia_sockmask.sin_addr;
   1254 		prefix.s_addr &= mask.s_addr;
   1255 	}
   1256 
   1257 	s = pserialize_read_enter();
   1258 	IN_ADDRLIST_READER_FOREACH(ia) {
   1259 		if (rtinitflags(ia))
   1260 			p = ia->ia_dstaddr.sin_addr;
   1261 		else {
   1262 			p = ia->ia_addr.sin_addr;
   1263 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
   1264 		}
   1265 
   1266 		if (prefix.s_addr != p.s_addr)
   1267 			continue;
   1268 
   1269 		/*
   1270 		 * if we got a matching prefix route inserted by other
   1271 		 * interface address, we don't need to bother
   1272 		 *
   1273 		 * XXX RADIX_MPATH implications here? -dyoung
   1274 		 */
   1275 		if (ia->ia_flags & IFA_ROUTE) {
   1276 			pserialize_read_exit(s);
   1277 			return 0;
   1278 		}
   1279 	}
   1280 	pserialize_read_exit(s);
   1281 
   1282 	/*
   1283 	 * noone seem to have prefix route.  insert it.
   1284 	 */
   1285 	error = rtinit(&target->ia_ifa, RTM_ADD, flags);
   1286 	if (error == 0)
   1287 		target->ia_flags |= IFA_ROUTE;
   1288 	else if (error == EEXIST) {
   1289 		/*
   1290 		 * the fact the route already exists is not an error.
   1291 		 */
   1292 		error = 0;
   1293 	}
   1294 	return error;
   1295 }
   1296 
   1297 /*
   1298  * remove a route to prefix ("connected route" in cisco terminology).
   1299  * re-installs the route by using another interface address, if there's one
   1300  * with the same prefix (otherwise we lose the route mistakenly).
   1301  */
   1302 static int
   1303 in_scrubprefix(struct in_ifaddr *target)
   1304 {
   1305 	struct in_ifaddr *ia;
   1306 	struct in_addr prefix, mask, p;
   1307 	int error;
   1308 	int s;
   1309 
   1310 	/* If we don't have IFA_ROUTE we have nothing to do */
   1311 	if ((target->ia_flags & IFA_ROUTE) == 0)
   1312 		return 0;
   1313 
   1314 	if (rtinitflags(target))
   1315 		prefix = target->ia_dstaddr.sin_addr;
   1316 	else {
   1317 		prefix = target->ia_addr.sin_addr;
   1318 		mask = target->ia_sockmask.sin_addr;
   1319 		prefix.s_addr &= mask.s_addr;
   1320 	}
   1321 
   1322 	s = pserialize_read_enter();
   1323 	IN_ADDRLIST_READER_FOREACH(ia) {
   1324 		if (rtinitflags(ia))
   1325 			p = ia->ia_dstaddr.sin_addr;
   1326 		else {
   1327 			p = ia->ia_addr.sin_addr;
   1328 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
   1329 		}
   1330 
   1331 		if (prefix.s_addr != p.s_addr)
   1332 			continue;
   1333 
   1334 		/*
   1335 		 * if we got a matching prefix route, move IFA_ROUTE to him
   1336 		 */
   1337 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
   1338 			struct psref psref;
   1339 			int bound = curlwp_bind();
   1340 
   1341 			ia4_acquire(ia, &psref);
   1342 			pserialize_read_exit(s);
   1343 
   1344 			rtinit(&target->ia_ifa, RTM_DELETE,
   1345 			    rtinitflags(target));
   1346 			target->ia_flags &= ~IFA_ROUTE;
   1347 
   1348 			error = rtinit(&ia->ia_ifa, RTM_ADD,
   1349 			    rtinitflags(ia) | RTF_UP);
   1350 			if (error == 0)
   1351 				ia->ia_flags |= IFA_ROUTE;
   1352 
   1353 			ia4_release(ia, &psref);
   1354 			curlwp_bindx(bound);
   1355 
   1356 			return error;
   1357 		}
   1358 	}
   1359 	pserialize_read_exit(s);
   1360 
   1361 	/*
   1362 	 * noone seem to have prefix route.  remove it.
   1363 	 */
   1364 	rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
   1365 	target->ia_flags &= ~IFA_ROUTE;
   1366 	return 0;
   1367 }
   1368 
   1369 #undef rtinitflags
   1370 
   1371 /*
   1372  * Return 1 if the address might be a local broadcast address.
   1373  */
   1374 int
   1375 in_broadcast(struct in_addr in, struct ifnet *ifp)
   1376 {
   1377 	struct ifaddr *ifa;
   1378 	int s;
   1379 
   1380 	KASSERT(ifp != NULL);
   1381 
   1382 	if (in.s_addr == INADDR_BROADCAST ||
   1383 	    in_nullhost(in))
   1384 		return 1;
   1385 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
   1386 		return 0;
   1387 	/*
   1388 	 * Look through the list of addresses for a match
   1389 	 * with a broadcast address.
   1390 	 */
   1391 #define ia (ifatoia(ifa))
   1392 	s = pserialize_read_enter();
   1393 	IFADDR_READER_FOREACH(ifa, ifp) {
   1394 		if (ifa->ifa_addr->sa_family == AF_INET &&
   1395 		    !in_hosteq(in, ia->ia_addr.sin_addr) &&
   1396 		    (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
   1397 		     in_hosteq(in, ia->ia_netbroadcast) ||
   1398 		     (hostzeroisbroadcast &&
   1399 		      /*
   1400 		       * Check for old-style (host 0) broadcast.
   1401 		       */
   1402 		      (in.s_addr == ia->ia_subnet ||
   1403 		       in.s_addr == ia->ia_net)))) {
   1404 			pserialize_read_exit(s);
   1405 			return 1;
   1406 		}
   1407 	}
   1408 	pserialize_read_exit(s);
   1409 	return (0);
   1410 #undef ia
   1411 }
   1412 
   1413 /*
   1414  * perform DAD when interface becomes IFF_UP.
   1415  */
   1416 void
   1417 in_if_link_up(struct ifnet *ifp)
   1418 {
   1419 	struct ifaddr *ifa;
   1420 	struct in_ifaddr *ia;
   1421 	int s, bound;
   1422 
   1423 	/* Ensure it's sane to run DAD */
   1424 	if (ifp->if_link_state == LINK_STATE_DOWN)
   1425 		return;
   1426 	if ((ifp->if_flags & (IFF_UP|IFF_RUNNING)) != (IFF_UP|IFF_RUNNING))
   1427 		return;
   1428 
   1429 	bound = curlwp_bind();
   1430 	s = pserialize_read_enter();
   1431 	IFADDR_READER_FOREACH(ifa, ifp) {
   1432 		struct psref psref;
   1433 
   1434 		if (ifa->ifa_addr->sa_family != AF_INET)
   1435 			continue;
   1436 		ifa_acquire(ifa, &psref);
   1437 		pserialize_read_exit(s);
   1438 
   1439 		ia = (struct in_ifaddr *)ifa;
   1440 
   1441 		/* If detached then mark as tentative */
   1442 		if (ia->ia4_flags & IN_IFF_DETACHED) {
   1443 			ia->ia4_flags &= ~IN_IFF_DETACHED;
   1444 			if (if_do_dad(ifp) && ia->ia_dad_start != NULL)
   1445 				ia->ia4_flags |= IN_IFF_TENTATIVE;
   1446 			else if ((ia->ia4_flags & IN_IFF_TENTATIVE) == 0)
   1447 				rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1448 		}
   1449 
   1450 		if (ia->ia4_flags & IN_IFF_TENTATIVE) {
   1451 			/* Clear the duplicated flag as we're starting DAD. */
   1452 			ia->ia4_flags &= ~IN_IFF_DUPLICATED;
   1453 			ia->ia_dad_start(ifa);
   1454 		}
   1455 
   1456 		s = pserialize_read_enter();
   1457 		ifa_release(ifa, &psref);
   1458 	}
   1459 	pserialize_read_exit(s);
   1460 	curlwp_bindx(bound);
   1461 }
   1462 
   1463 void
   1464 in_if_up(struct ifnet *ifp)
   1465 {
   1466 
   1467 	/* interface may not support link state, so bring it up also */
   1468 	in_if_link_up(ifp);
   1469 }
   1470 
   1471 /*
   1472  * Mark all addresses as detached.
   1473  */
   1474 void
   1475 in_if_link_down(struct ifnet *ifp)
   1476 {
   1477 	struct ifaddr *ifa;
   1478 	struct in_ifaddr *ia;
   1479 	int s, bound;
   1480 
   1481 	bound = curlwp_bind();
   1482 	s = pserialize_read_enter();
   1483 	IFADDR_READER_FOREACH(ifa, ifp) {
   1484 		struct psref psref;
   1485 
   1486 		if (ifa->ifa_addr->sa_family != AF_INET)
   1487 			continue;
   1488 		ifa_acquire(ifa, &psref);
   1489 		pserialize_read_exit(s);
   1490 
   1491 		ia = (struct in_ifaddr *)ifa;
   1492 
   1493 		/* Stop DAD processing */
   1494 		if (ia->ia_dad_stop != NULL)
   1495 			ia->ia_dad_stop(ifa);
   1496 
   1497 		/*
   1498 		 * Mark the address as detached.
   1499 		 */
   1500 		if (!(ia->ia4_flags & IN_IFF_DETACHED)) {
   1501 			ia->ia4_flags |= IN_IFF_DETACHED;
   1502 			ia->ia4_flags &=
   1503 			    ~(IN_IFF_TENTATIVE | IN_IFF_DUPLICATED);
   1504 			rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
   1505 		}
   1506 
   1507 		s = pserialize_read_enter();
   1508 		ifa_release(ifa, &psref);
   1509 	}
   1510 	pserialize_read_exit(s);
   1511 	curlwp_bindx(bound);
   1512 }
   1513 
   1514 void
   1515 in_if_down(struct ifnet *ifp)
   1516 {
   1517 
   1518 	in_if_link_down(ifp);
   1519 }
   1520 
   1521 void
   1522 in_if_link_state_change(struct ifnet *ifp, int link_state)
   1523 {
   1524 
   1525 	switch (link_state) {
   1526 	case LINK_STATE_DOWN:
   1527 		in_if_link_down(ifp);
   1528 		break;
   1529 	case LINK_STATE_UP:
   1530 		in_if_link_up(ifp);
   1531 		break;
   1532 	}
   1533 }
   1534 
   1535 /*
   1536  * in_lookup_multi: look up the in_multi record for a given IP
   1537  * multicast address on a given interface.  If no matching record is
   1538  * found, return NULL.
   1539  */
   1540 struct in_multi *
   1541 in_lookup_multi(struct in_addr addr, ifnet_t *ifp)
   1542 {
   1543 	struct in_multi *inm;
   1544 
   1545 	KASSERT(rw_lock_held(&in_multilock));
   1546 
   1547 	LIST_FOREACH(inm, &IN_MULTI_HASH(addr.s_addr, ifp), inm_list) {
   1548 		if (in_hosteq(inm->inm_addr, addr) && inm->inm_ifp == ifp)
   1549 			break;
   1550 	}
   1551 	return inm;
   1552 }
   1553 
   1554 /*
   1555  * in_multi_group: check whether the address belongs to an IP multicast
   1556  * group we are joined on this interface.  Returns true or false.
   1557  */
   1558 bool
   1559 in_multi_group(struct in_addr addr, ifnet_t *ifp, int flags)
   1560 {
   1561 	bool ingroup;
   1562 
   1563 	if (__predict_true(flags & IP_IGMP_MCAST) == 0) {
   1564 		rw_enter(&in_multilock, RW_READER);
   1565 		ingroup = in_lookup_multi(addr, ifp) != NULL;
   1566 		rw_exit(&in_multilock);
   1567 	} else {
   1568 		/* XXX Recursive call from ip_output(). */
   1569 		KASSERT(rw_lock_held(&in_multilock));
   1570 		ingroup = in_lookup_multi(addr, ifp) != NULL;
   1571 	}
   1572 	return ingroup;
   1573 }
   1574 
   1575 /*
   1576  * Add an address to the list of IP multicast addresses for a given interface.
   1577  */
   1578 struct in_multi *
   1579 in_addmulti(struct in_addr *ap, ifnet_t *ifp)
   1580 {
   1581 	struct sockaddr_in sin;
   1582 	struct in_multi *inm;
   1583 
   1584 	/*
   1585 	 * See if address already in list.
   1586 	 */
   1587 	rw_enter(&in_multilock, RW_WRITER);
   1588 	inm = in_lookup_multi(*ap, ifp);
   1589 	if (inm != NULL) {
   1590 		/*
   1591 		 * Found it; just increment the reference count.
   1592 		 */
   1593 		inm->inm_refcount++;
   1594 		rw_exit(&in_multilock);
   1595 		return inm;
   1596 	}
   1597 
   1598 	/*
   1599 	 * New address; allocate a new multicast record.
   1600 	 */
   1601 	inm = pool_get(&inmulti_pool, PR_NOWAIT);
   1602 	if (inm == NULL) {
   1603 		rw_exit(&in_multilock);
   1604 		return NULL;
   1605 	}
   1606 	inm->inm_addr = *ap;
   1607 	inm->inm_ifp = ifp;
   1608 	inm->inm_refcount = 1;
   1609 
   1610 	/*
   1611 	 * Ask the network driver to update its multicast reception
   1612 	 * filter appropriately for the new address.
   1613 	 */
   1614 	sockaddr_in_init(&sin, ap, 0);
   1615 	if (if_mcast_op(ifp, SIOCADDMULTI, sintosa(&sin)) != 0) {
   1616 		rw_exit(&in_multilock);
   1617 		pool_put(&inmulti_pool, inm);
   1618 		return NULL;
   1619 	}
   1620 
   1621 	/*
   1622 	 * Let IGMP know that we have joined a new IP multicast group.
   1623 	 */
   1624 	if (igmp_joingroup(inm) != 0) {
   1625 		rw_exit(&in_multilock);
   1626 		pool_put(&inmulti_pool, inm);
   1627 		return NULL;
   1628 	}
   1629 	LIST_INSERT_HEAD(
   1630 	    &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp),
   1631 	    inm, inm_list);
   1632 	in_multientries++;
   1633 	rw_exit(&in_multilock);
   1634 
   1635 	return inm;
   1636 }
   1637 
   1638 /*
   1639  * Delete a multicast address record.
   1640  */
   1641 void
   1642 in_delmulti(struct in_multi *inm)
   1643 {
   1644 	struct sockaddr_in sin;
   1645 
   1646 	rw_enter(&in_multilock, RW_WRITER);
   1647 	if (--inm->inm_refcount > 0) {
   1648 		rw_exit(&in_multilock);
   1649 		return;
   1650 	}
   1651 
   1652 	/*
   1653 	 * No remaining claims to this record; let IGMP know that
   1654 	 * we are leaving the multicast group.
   1655 	 */
   1656 	igmp_leavegroup(inm);
   1657 
   1658 	/*
   1659 	 * Notify the network driver to update its multicast reception
   1660 	 * filter.
   1661 	 */
   1662 	sockaddr_in_init(&sin, &inm->inm_addr, 0);
   1663 	if_mcast_op(inm->inm_ifp, SIOCDELMULTI, sintosa(&sin));
   1664 
   1665 	/*
   1666 	 * Unlink from list.
   1667 	 */
   1668 	LIST_REMOVE(inm, inm_list);
   1669 	in_multientries--;
   1670 	rw_exit(&in_multilock);
   1671 
   1672 	pool_put(&inmulti_pool, inm);
   1673 }
   1674 
   1675 /*
   1676  * in_next_multi: step through all of the in_multi records, one at a time.
   1677  * The current position is remembered in "step", which the caller must
   1678  * provide.  in_first_multi(), below, must be called to initialize "step"
   1679  * and get the first record.  Both macros return a NULL "inm" when there
   1680  * are no remaining records.
   1681  */
   1682 struct in_multi *
   1683 in_next_multi(struct in_multistep *step)
   1684 {
   1685 	struct in_multi *inm;
   1686 
   1687 	KASSERT(rw_lock_held(&in_multilock));
   1688 
   1689 	while (step->i_inm == NULL && step->i_n < IN_MULTI_HASH_SIZE) {
   1690 		step->i_inm = LIST_FIRST(&in_multihashtbl[++step->i_n]);
   1691 	}
   1692 	if ((inm = step->i_inm) != NULL) {
   1693 		step->i_inm = LIST_NEXT(inm, inm_list);
   1694 	}
   1695 	return inm;
   1696 }
   1697 
   1698 struct in_multi *
   1699 in_first_multi(struct in_multistep *step)
   1700 {
   1701 	KASSERT(rw_lock_held(&in_multilock));
   1702 
   1703 	step->i_n = 0;
   1704 	step->i_inm = LIST_FIRST(&in_multihashtbl[0]);
   1705 	return in_next_multi(step);
   1706 }
   1707 
   1708 void
   1709 in_multi_lock(int op)
   1710 {
   1711 	rw_enter(&in_multilock, op);
   1712 }
   1713 
   1714 void
   1715 in_multi_unlock(void)
   1716 {
   1717 	rw_exit(&in_multilock);
   1718 }
   1719 
   1720 int
   1721 in_multi_lock_held(void)
   1722 {
   1723 	return rw_lock_held(&in_multilock);
   1724 }
   1725 
   1726 struct in_ifaddr *
   1727 in_selectsrc(struct sockaddr_in *sin, struct route *ro,
   1728     int soopts, struct ip_moptions *mopts, int *errorp, struct psref *psref)
   1729 {
   1730 	struct rtentry *rt = NULL;
   1731 	struct in_ifaddr *ia = NULL;
   1732 
   1733 	KASSERT(ISSET(curlwp->l_pflag, LP_BOUND));
   1734 	/*
   1735          * If route is known or can be allocated now, take the
   1736          * source address from the interface.  Otherwise, punt.
   1737 	 */
   1738 	if ((soopts & SO_DONTROUTE) != 0)
   1739 		rtcache_free(ro);
   1740 	else {
   1741 		union {
   1742 			struct sockaddr		dst;
   1743 			struct sockaddr_in	dst4;
   1744 		} u;
   1745 
   1746 		sockaddr_in_init(&u.dst4, &sin->sin_addr, 0);
   1747 		rt = rtcache_lookup(ro, &u.dst);
   1748 	}
   1749 	/*
   1750 	 * If we found a route, use the address
   1751 	 * corresponding to the outgoing interface
   1752 	 * unless it is the loopback (in case a route
   1753 	 * to our address on another net goes to loopback).
   1754 	 *
   1755 	 * XXX Is this still true?  Do we care?
   1756 	 */
   1757 	if (rt != NULL && (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0) {
   1758 		int s;
   1759 		struct ifaddr *ifa;
   1760 		/*
   1761 		 * Just in case. May not need to do this workaround.
   1762 		 * Revisit when working on rtentry MP-ification.
   1763 		 */
   1764 		s = pserialize_read_enter();
   1765 		IFADDR_READER_FOREACH(ifa, rt->rt_ifp) {
   1766 			if (ifa == rt->rt_ifa)
   1767 				break;
   1768 		}
   1769 		if (ifa != NULL)
   1770 			ifa_acquire(ifa, psref);
   1771 		pserialize_read_exit(s);
   1772 
   1773 		ia = ifatoia(ifa);
   1774 	}
   1775 	if (ia == NULL) {
   1776 		u_int16_t fport = sin->sin_port;
   1777 		struct ifaddr *ifa;
   1778 		int s;
   1779 
   1780 		sin->sin_port = 0;
   1781 		ifa = ifa_ifwithladdr_psref(sintosa(sin), psref);
   1782 		sin->sin_port = fport;
   1783 		if (ifa == NULL) {
   1784 			/* Find 1st non-loopback AF_INET address */
   1785 			s = pserialize_read_enter();
   1786 			IN_ADDRLIST_READER_FOREACH(ia) {
   1787 				if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
   1788 					break;
   1789 			}
   1790 			if (ia != NULL)
   1791 				ia4_acquire(ia, psref);
   1792 			pserialize_read_exit(s);
   1793 		} else {
   1794 			/* ia is already referenced by psref */
   1795 			ia = ifatoia(ifa);
   1796 		}
   1797 		if (ia == NULL) {
   1798 			*errorp = EADDRNOTAVAIL;
   1799 			goto out;
   1800 		}
   1801 	}
   1802 	/*
   1803 	 * If the destination address is multicast and an outgoing
   1804 	 * interface has been set as a multicast option, use the
   1805 	 * address of that interface as our source address.
   1806 	 */
   1807 	if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
   1808 		struct ip_moptions *imo;
   1809 
   1810 		imo = mopts;
   1811 		if (imo->imo_multicast_if_index != 0) {
   1812 			struct ifnet *ifp;
   1813 			int s;
   1814 
   1815 			if (ia != NULL)
   1816 				ia4_release(ia, psref);
   1817 			s = pserialize_read_enter();
   1818 			ifp = if_byindex(imo->imo_multicast_if_index);
   1819 			if (ifp != NULL) {
   1820 				/* XXX */
   1821 				ia = in_get_ia_from_ifp_psref(ifp, psref);
   1822 			} else
   1823 				ia = NULL;
   1824 			if (ia == NULL || ia->ia4_flags & IN_IFF_NOTREADY) {
   1825 				pserialize_read_exit(s);
   1826 				if (ia != NULL)
   1827 					ia4_release(ia, psref);
   1828 				*errorp = EADDRNOTAVAIL;
   1829 				ia = NULL;
   1830 				goto out;
   1831 			}
   1832 			pserialize_read_exit(s);
   1833 		}
   1834 	}
   1835 	if (ia->ia_ifa.ifa_getifa != NULL) {
   1836 		ia = ifatoia((*ia->ia_ifa.ifa_getifa)(&ia->ia_ifa,
   1837 		                                      sintosa(sin)));
   1838 		if (ia == NULL) {
   1839 			*errorp = EADDRNOTAVAIL;
   1840 			goto out;
   1841 		}
   1842 		/* FIXME NOMPSAFE */
   1843 		ia4_acquire(ia, psref);
   1844 	}
   1845 #ifdef GETIFA_DEBUG
   1846 	else
   1847 		printf("%s: missing ifa_getifa\n", __func__);
   1848 #endif
   1849 out:
   1850 	rtcache_unref(rt, ro);
   1851 	return ia;
   1852 }
   1853 
   1854 #if NARP > 0
   1855 
   1856 struct in_llentry {
   1857 	struct llentry		base;
   1858 };
   1859 
   1860 #define	IN_LLTBL_DEFAULT_HSIZE	32
   1861 #define	IN_LLTBL_HASH(k, h) \
   1862 	(((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
   1863 
   1864 /*
   1865  * Do actual deallocation of @lle.
   1866  * Called by LLE_FREE_LOCKED when number of references
   1867  * drops to zero.
   1868  */
   1869 static void
   1870 in_lltable_destroy_lle(struct llentry *lle)
   1871 {
   1872 
   1873 	LLE_WUNLOCK(lle);
   1874 	LLE_LOCK_DESTROY(lle);
   1875 	kmem_intr_free(lle, sizeof(*lle));
   1876 }
   1877 
   1878 static struct llentry *
   1879 in_lltable_new(struct in_addr addr4, u_int flags)
   1880 {
   1881 	struct in_llentry *lle;
   1882 
   1883 	lle = kmem_intr_zalloc(sizeof(*lle), KM_NOSLEEP);
   1884 	if (lle == NULL)		/* NB: caller generates msg */
   1885 		return NULL;
   1886 
   1887 	/*
   1888 	 * For IPv4 this will trigger "arpresolve" to generate
   1889 	 * an ARP request.
   1890 	 */
   1891 	lle->base.la_expire = time_uptime; /* mark expired */
   1892 	lle->base.r_l3addr.addr4 = addr4;
   1893 	lle->base.lle_refcnt = 1;
   1894 	lle->base.lle_free = in_lltable_destroy_lle;
   1895 	LLE_LOCK_INIT(&lle->base);
   1896 	callout_init(&lle->base.la_timer, CALLOUT_MPSAFE);
   1897 
   1898 	return (&lle->base);
   1899 }
   1900 
   1901 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m)	(			\
   1902 	    (((ntohl((d).s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
   1903 
   1904 static int
   1905 in_lltable_match_prefix(const struct sockaddr *prefix,
   1906     const struct sockaddr *mask, u_int flags, struct llentry *lle)
   1907 {
   1908 	const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
   1909 	const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
   1910 
   1911 	/*
   1912 	 * (flags & LLE_STATIC) means deleting all entries
   1913 	 * including static ARP entries.
   1914 	 */
   1915 	if (IN_ARE_MASKED_ADDR_EQUAL(lle->r_l3addr.addr4, pfx, msk) &&
   1916 	    ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC)))
   1917 		return (1);
   1918 
   1919 	return (0);
   1920 }
   1921 
   1922 static void
   1923 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
   1924 {
   1925 	struct ifnet *ifp __diagused;
   1926 	size_t pkts_dropped;
   1927 
   1928 	LLE_WLOCK_ASSERT(lle);
   1929 	KASSERT(llt != NULL);
   1930 
   1931 	/* Unlink entry from table if not already */
   1932 	if ((lle->la_flags & LLE_LINKED) != 0) {
   1933 		ifp = llt->llt_ifp;
   1934 		IF_AFDATA_WLOCK_ASSERT(ifp);
   1935 		lltable_unlink_entry(llt, lle);
   1936 	}
   1937 
   1938 	/* cancel timer */
   1939 	if (callout_halt(&lle->lle_timer, &lle->lle_lock))
   1940 		LLE_REMREF(lle);
   1941 
   1942 	/* Drop hold queue */
   1943 	pkts_dropped = llentry_free(lle);
   1944 	arp_stat_add(ARP_STAT_DFRDROPPED, (uint64_t)pkts_dropped);
   1945 }
   1946 
   1947 static int
   1948 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
   1949 {
   1950 	struct rtentry *rt;
   1951 	int error = EINVAL;
   1952 
   1953 	KASSERTMSG(l3addr->sa_family == AF_INET,
   1954 	    "sin_family %d", l3addr->sa_family);
   1955 
   1956 	rt = rtalloc1(l3addr, 0);
   1957 	if (rt == NULL)
   1958 		return error;
   1959 
   1960 	/*
   1961 	 * If the gateway for an existing host route matches the target L3
   1962 	 * address, which is a special route inserted by some implementation
   1963 	 * such as MANET, and the interface is of the correct type, then
   1964 	 * allow for ARP to proceed.
   1965 	 */
   1966 	if (rt->rt_flags & RTF_GATEWAY) {
   1967 		if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp ||
   1968 		    rt->rt_ifp->if_type != IFT_ETHER ||
   1969 #ifdef __FreeBSD__
   1970 		    (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
   1971 #else
   1972 		    (rt->rt_ifp->if_flags & IFF_NOARP) != 0 ||
   1973 #endif
   1974 		    memcmp(rt->rt_gateway->sa_data, l3addr->sa_data,
   1975 		    sizeof(in_addr_t)) != 0) {
   1976 			goto error;
   1977 		}
   1978 	}
   1979 
   1980 	/*
   1981 	 * Make sure that at least the destination address is covered
   1982 	 * by the route. This is for handling the case where 2 or more
   1983 	 * interfaces have the same prefix. An incoming packet arrives
   1984 	 * on one interface and the corresponding outgoing packet leaves
   1985 	 * another interface.
   1986 	 */
   1987 	if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) {
   1988 		const char *sa, *mask, *addr, *lim;
   1989 		int len;
   1990 
   1991 		mask = (const char *)rt_mask(rt);
   1992 		/*
   1993 		 * Just being extra cautious to avoid some custom
   1994 		 * code getting into trouble.
   1995 		 */
   1996 		if (mask == NULL)
   1997 			goto error;
   1998 
   1999 		sa = (const char *)rt_getkey(rt);
   2000 		addr = (const char *)l3addr;
   2001 		len = ((const struct sockaddr_in *)l3addr)->sin_len;
   2002 		lim = addr + len;
   2003 
   2004 		for ( ; addr < lim; sa++, mask++, addr++) {
   2005 			if ((*sa ^ *addr) & *mask) {
   2006 #ifdef DIAGNOSTIC
   2007 				log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
   2008 				    inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
   2009 #endif
   2010 				goto error;
   2011 			}
   2012 		}
   2013 	}
   2014 
   2015 	error = 0;
   2016 error:
   2017 	rt_unref(rt);
   2018 	return error;
   2019 }
   2020 
   2021 static inline uint32_t
   2022 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
   2023 {
   2024 
   2025 	return (IN_LLTBL_HASH(dst.s_addr, hsize));
   2026 }
   2027 
   2028 static uint32_t
   2029 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
   2030 {
   2031 
   2032 	return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
   2033 }
   2034 
   2035 static void
   2036 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
   2037 {
   2038 	struct sockaddr_in *sin;
   2039 
   2040 	sin = (struct sockaddr_in *)sa;
   2041 	memset(sin, 0, sizeof(*sin));
   2042 	sin->sin_family = AF_INET;
   2043 	sin->sin_len = sizeof(*sin);
   2044 	sin->sin_addr = lle->r_l3addr.addr4;
   2045 }
   2046 
   2047 static inline struct llentry *
   2048 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
   2049 {
   2050 	struct llentry *lle;
   2051 	struct llentries *lleh;
   2052 	u_int hashidx;
   2053 
   2054 	hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
   2055 	lleh = &llt->lle_head[hashidx];
   2056 	LIST_FOREACH(lle, lleh, lle_next) {
   2057 		if (lle->la_flags & LLE_DELETED)
   2058 			continue;
   2059 		if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
   2060 			break;
   2061 	}
   2062 
   2063 	return (lle);
   2064 }
   2065 
   2066 static int
   2067 in_lltable_delete(struct lltable *llt, u_int flags,
   2068     const struct sockaddr *l3addr)
   2069 {
   2070 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
   2071 	struct ifnet *ifp __diagused = llt->llt_ifp;
   2072 	struct llentry *lle;
   2073 
   2074 	IF_AFDATA_WLOCK_ASSERT(ifp);
   2075 	KASSERTMSG(l3addr->sa_family == AF_INET,
   2076 	    "sin_family %d", l3addr->sa_family);
   2077 
   2078 	lle = in_lltable_find_dst(llt, sin->sin_addr);
   2079 	if (lle == NULL) {
   2080 #ifdef DIAGNOSTIC
   2081 		log(LOG_INFO, "interface address is missing from cache = %p  in delete\n", lle);
   2082 #endif
   2083 		return (ENOENT);
   2084 	}
   2085 
   2086 	LLE_WLOCK(lle);
   2087 	lle->la_flags |= LLE_DELETED;
   2088 #ifdef DIAGNOSTIC
   2089 	log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
   2090 #endif
   2091 	if ((lle->la_flags & (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
   2092 		llentry_free(lle);
   2093 	else
   2094 		LLE_WUNLOCK(lle);
   2095 
   2096 	return (0);
   2097 }
   2098 
   2099 static struct llentry *
   2100 in_lltable_create(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
   2101 {
   2102 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
   2103 	struct ifnet *ifp = llt->llt_ifp;
   2104 	struct llentry *lle;
   2105 
   2106 	IF_AFDATA_WLOCK_ASSERT(ifp);
   2107 	KASSERTMSG(l3addr->sa_family == AF_INET,
   2108 	    "sin_family %d", l3addr->sa_family);
   2109 
   2110 	lle = in_lltable_find_dst(llt, sin->sin_addr);
   2111 
   2112 	if (lle != NULL) {
   2113 		LLE_WLOCK(lle);
   2114 		return (lle);
   2115 	}
   2116 
   2117 	/* no existing record, we need to create new one */
   2118 
   2119 	/*
   2120 	 * A route that covers the given address must have
   2121 	 * been installed 1st because we are doing a resolution,
   2122 	 * verify this.
   2123 	 */
   2124 	if (!(flags & LLE_IFADDR) &&
   2125 	    in_lltable_rtcheck(ifp, flags, l3addr) != 0)
   2126 		return (NULL);
   2127 
   2128 	lle = in_lltable_new(sin->sin_addr, flags);
   2129 	if (lle == NULL) {
   2130 		log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
   2131 		return (NULL);
   2132 	}
   2133 	lle->la_flags = flags;
   2134 	if ((flags & LLE_IFADDR) == LLE_IFADDR) {
   2135 		memcpy(&lle->ll_addr, CLLADDR(ifp->if_sadl), ifp->if_addrlen);
   2136 		lle->la_flags |= (LLE_VALID | LLE_STATIC);
   2137 	}
   2138 
   2139 	lltable_link_entry(llt, lle);
   2140 	LLE_WLOCK(lle);
   2141 
   2142 	return (lle);
   2143 }
   2144 
   2145 /*
   2146  * Return NULL if not found or marked for deletion.
   2147  * If found return lle read locked.
   2148  */
   2149 static struct llentry *
   2150 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
   2151 {
   2152 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
   2153 	struct llentry *lle;
   2154 
   2155 	IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
   2156 	KASSERTMSG(l3addr->sa_family == AF_INET,
   2157 	    "sin_family %d", l3addr->sa_family);
   2158 
   2159 	lle = in_lltable_find_dst(llt, sin->sin_addr);
   2160 
   2161 	if (lle == NULL)
   2162 		return NULL;
   2163 
   2164 	if (flags & LLE_EXCLUSIVE)
   2165 		LLE_WLOCK(lle);
   2166 	else
   2167 		LLE_RLOCK(lle);
   2168 
   2169 	return lle;
   2170 }
   2171 
   2172 static int
   2173 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
   2174     struct rt_walkarg *w)
   2175 {
   2176 	struct sockaddr_in sin;
   2177 
   2178 	LLTABLE_LOCK_ASSERT();
   2179 
   2180 	/* skip deleted entries */
   2181 	if (lle->la_flags & LLE_DELETED)
   2182 		return 0;
   2183 
   2184 	sockaddr_in_init(&sin, &lle->r_l3addr.addr4, 0);
   2185 
   2186 	return lltable_dump_entry(llt, lle, w, sintosa(&sin));
   2187 }
   2188 
   2189 #endif /* NARP > 0 */
   2190 
   2191 static int
   2192 in_multicast_sysctl(SYSCTLFN_ARGS)
   2193 {
   2194 	struct ifnet *ifp;
   2195 	struct ifaddr *ifa;
   2196 	struct in_ifaddr *ifa4;
   2197 	struct in_multi *inm;
   2198 	uint32_t tmp;
   2199 	int error;
   2200 	size_t written;
   2201 	struct psref psref;
   2202 	int bound;
   2203 
   2204 	if (namelen != 1)
   2205 		return EINVAL;
   2206 
   2207 	bound = curlwp_bind();
   2208 	ifp = if_get_byindex(name[0], &psref);
   2209 	if (ifp == NULL) {
   2210 		curlwp_bindx(bound);
   2211 		return ENODEV;
   2212 	}
   2213 
   2214 	if (oldp == NULL) {
   2215 		*oldlenp = 0;
   2216 		IFADDR_FOREACH(ifa, ifp) {
   2217 			if (ifa->ifa_addr->sa_family != AF_INET)
   2218 				continue;
   2219 			ifa4 = (void *)ifa;
   2220 			LIST_FOREACH(inm, &ifa4->ia_multiaddrs, inm_list) {
   2221 				*oldlenp += 2 * sizeof(struct in_addr) +
   2222 				    sizeof(uint32_t);
   2223 			}
   2224 		}
   2225 		if_put(ifp, &psref);
   2226 		curlwp_bindx(bound);
   2227 		return 0;
   2228 	}
   2229 
   2230 	error = 0;
   2231 	written = 0;
   2232 	IFADDR_FOREACH(ifa, ifp) {
   2233 		if (ifa->ifa_addr->sa_family != AF_INET)
   2234 			continue;
   2235 		ifa4 = (void *)ifa;
   2236 		LIST_FOREACH(inm, &ifa4->ia_multiaddrs, inm_list) {
   2237 			if (written + 2 * sizeof(struct in_addr) +
   2238 			    sizeof(uint32_t) > *oldlenp)
   2239 				goto done;
   2240 			error = sysctl_copyout(l, &ifa4->ia_addr.sin_addr,
   2241 			    oldp, sizeof(struct in_addr));
   2242 			if (error)
   2243 				goto done;
   2244 			oldp = (char *)oldp + sizeof(struct in_addr);
   2245 			written += sizeof(struct in_addr);
   2246 			error = sysctl_copyout(l, &inm->inm_addr,
   2247 			    oldp, sizeof(struct in_addr));
   2248 			if (error)
   2249 				goto done;
   2250 			oldp = (char *)oldp + sizeof(struct in_addr);
   2251 			written += sizeof(struct in_addr);
   2252 			tmp = inm->inm_refcount;
   2253 			error = sysctl_copyout(l, &tmp, oldp, sizeof(tmp));
   2254 			if (error)
   2255 				goto done;
   2256 			oldp = (char *)oldp + sizeof(tmp);
   2257 			written += sizeof(tmp);
   2258 		}
   2259 	}
   2260 done:
   2261 	if_put(ifp, &psref);
   2262 	curlwp_bindx(bound);
   2263 	*oldlenp = written;
   2264 	return error;
   2265 }
   2266 
   2267 static void
   2268 in_sysctl_init(struct sysctllog **clog)
   2269 {
   2270 	sysctl_createv(clog, 0, NULL, NULL,
   2271 		       CTLFLAG_PERMANENT,
   2272 		       CTLTYPE_NODE, "inet",
   2273 		       SYSCTL_DESCR("PF_INET related settings"),
   2274 		       NULL, 0, NULL, 0,
   2275 		       CTL_NET, PF_INET, CTL_EOL);
   2276 	sysctl_createv(clog, 0, NULL, NULL,
   2277 		       CTLFLAG_PERMANENT,
   2278 		       CTLTYPE_NODE, "multicast",
   2279 		       SYSCTL_DESCR("Multicast information"),
   2280 		       in_multicast_sysctl, 0, NULL, 0,
   2281 		       CTL_NET, PF_INET, CTL_CREATE, CTL_EOL);
   2282 	sysctl_createv(clog, 0, NULL, NULL,
   2283 		       CTLFLAG_PERMANENT,
   2284 		       CTLTYPE_NODE, "ip",
   2285 		       SYSCTL_DESCR("IPv4 related settings"),
   2286 		       NULL, 0, NULL, 0,
   2287 		       CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
   2288 
   2289 	sysctl_createv(clog, 0, NULL, NULL,
   2290 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2291 		       CTLTYPE_INT, "subnetsarelocal",
   2292 		       SYSCTL_DESCR("Whether logical subnets are considered "
   2293 				    "local"),
   2294 		       NULL, 0, &subnetsarelocal, 0,
   2295 		       CTL_NET, PF_INET, IPPROTO_IP,
   2296 		       IPCTL_SUBNETSARELOCAL, CTL_EOL);
   2297 	sysctl_createv(clog, 0, NULL, NULL,
   2298 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
   2299 		       CTLTYPE_INT, "hostzerobroadcast",
   2300 		       SYSCTL_DESCR("All zeroes address is broadcast address"),
   2301 		       NULL, 0, &hostzeroisbroadcast, 0,
   2302 		       CTL_NET, PF_INET, IPPROTO_IP,
   2303 		       IPCTL_HOSTZEROBROADCAST, CTL_EOL);
   2304 }
   2305 
   2306 #if NARP > 0
   2307 
   2308 static struct lltable *
   2309 in_lltattach(struct ifnet *ifp)
   2310 {
   2311 	struct lltable *llt;
   2312 
   2313 	llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
   2314 	llt->llt_af = AF_INET;
   2315 	llt->llt_ifp = ifp;
   2316 
   2317 	llt->llt_lookup = in_lltable_lookup;
   2318 	llt->llt_create = in_lltable_create;
   2319 	llt->llt_delete = in_lltable_delete;
   2320 	llt->llt_dump_entry = in_lltable_dump_entry;
   2321 	llt->llt_hash = in_lltable_hash;
   2322 	llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
   2323 	llt->llt_free_entry = in_lltable_free_entry;
   2324 	llt->llt_match_prefix = in_lltable_match_prefix;
   2325 	lltable_link(llt);
   2326 
   2327 	return (llt);
   2328 }
   2329 
   2330 #endif /* NARP > 0 */
   2331 
   2332 void *
   2333 in_domifattach(struct ifnet *ifp)
   2334 {
   2335 	struct in_ifinfo *ii;
   2336 
   2337 	ii = kmem_zalloc(sizeof(struct in_ifinfo), KM_SLEEP);
   2338 	KASSERT(ii != NULL);
   2339 
   2340 #if NARP > 0
   2341 	ii->ii_llt = in_lltattach(ifp);
   2342 #endif
   2343 
   2344 #ifdef IPSELSRC
   2345 	ii->ii_selsrc = in_selsrc_domifattach(ifp);
   2346 	KASSERT(ii->ii_selsrc != NULL);
   2347 #endif
   2348 
   2349 	return ii;
   2350 }
   2351 
   2352 void
   2353 in_domifdetach(struct ifnet *ifp, void *aux)
   2354 {
   2355 	struct in_ifinfo *ii = aux;
   2356 
   2357 #ifdef IPSELSRC
   2358 	in_selsrc_domifdetach(ifp, ii->ii_selsrc);
   2359 #endif
   2360 #if NARP > 0
   2361 	lltable_free(ii->ii_llt);
   2362 #endif
   2363 	kmem_free(ii, sizeof(struct in_ifinfo));
   2364 }
   2365