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