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