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