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