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