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