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