xref: /netbsd-src/sys/netinet/in.c (revision 2c6fc41c810f5088457889d00eba558e8bc74d9e)
1 /*	$NetBSD: in.c,v 1.146 2014/05/29 23:02:48 rmind 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.146 2014/05/29 23:02:48 rmind Exp $");
95 
96 #include "opt_inet.h"
97 #include "opt_inet_conf.h"
98 #include "opt_mrouting.h"
99 
100 #include <sys/param.h>
101 #include <sys/ioctl.h>
102 #include <sys/errno.h>
103 #include <sys/malloc.h>
104 #include <sys/socket.h>
105 #include <sys/socketvar.h>
106 #include <sys/sysctl.h>
107 #include <sys/systm.h>
108 #include <sys/proc.h>
109 #include <sys/syslog.h>
110 #include <sys/kauth.h>
111 
112 #include <sys/cprng.h>
113 
114 #include <net/if.h>
115 #include <net/route.h>
116 #include <net/pfil.h>
117 
118 #include <net/if_ether.h>
119 
120 #include <netinet/in_systm.h>
121 #include <netinet/in.h>
122 #include <netinet/in_var.h>
123 #include <netinet/ip.h>
124 #include <netinet/ip_var.h>
125 #include <netinet/in_ifattach.h>
126 #include <netinet/in_pcb.h>
127 #include <netinet/if_inarp.h>
128 #include <netinet/ip_mroute.h>
129 #include <netinet/igmp_var.h>
130 
131 #ifdef IPSELSRC
132 #include <netinet/in_selsrc.h>
133 #endif
134 
135 static u_int	in_mask2len(struct in_addr *);
136 static void	in_len2mask(struct in_addr *, u_int);
137 static int	in_lifaddr_ioctl(struct socket *, u_long, void *,
138 	struct ifnet *, struct lwp *);
139 
140 static int	in_addprefix(struct in_ifaddr *, int);
141 static int	in_scrubprefix(struct in_ifaddr *);
142 static void	in_sysctl_init(struct sysctllog **);
143 
144 #ifndef SUBNETSARELOCAL
145 #define	SUBNETSARELOCAL	1
146 #endif
147 
148 #ifndef HOSTZEROBROADCAST
149 #define HOSTZEROBROADCAST 1
150 #endif
151 
152 /* Note: 61, 127, 251, 509, 1021, 2039 are good. */
153 #ifndef IN_MULTI_HASH_SIZE
154 #define IN_MULTI_HASH_SIZE	509
155 #endif
156 
157 static int			subnetsarelocal = SUBNETSARELOCAL;
158 static int			hostzeroisbroadcast = HOSTZEROBROADCAST;
159 
160 /*
161  * This list is used to keep track of in_multi chains which belong to
162  * deleted interface addresses.  We use in_ifaddr so that a chain head
163  * won't be deallocated until all multicast address record are deleted.
164  */
165 
166 LIST_HEAD(in_multihashhead, in_multi);		/* Type of the hash head */
167 
168 static struct pool		inmulti_pool;
169 static u_int			in_multientries;
170 static struct in_multihashhead *in_multihashtbl;
171 static u_long			in_multihash;
172 static krwlock_t		in_multilock;
173 
174 #define IN_MULTI_HASH(x, ifp) \
175     (in_multihashtbl[(u_long)((x) ^ (ifp->if_index)) % IN_MULTI_HASH_SIZE])
176 
177 struct in_ifaddrhashhead *	in_ifaddrhashtbl;
178 u_long				in_ifaddrhash;
179 struct in_ifaddrhead		in_ifaddrhead;
180 
181 void
182 in_init(void)
183 {
184 	pool_init(&inmulti_pool, sizeof(struct in_multi), 0, 0, 0, "inmltpl",
185 	    NULL, IPL_SOFTNET);
186 	TAILQ_INIT(&in_ifaddrhead);
187 
188 	in_ifaddrhashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
189 	    &in_ifaddrhash);
190 	in_multihashtbl = hashinit(IN_IFADDR_HASH_SIZE, HASH_LIST, true,
191 	    &in_multihash);
192 	rw_init(&in_multilock);
193 
194 	in_sysctl_init(NULL);
195 }
196 
197 /*
198  * Return 1 if an internet address is for a ``local'' host
199  * (one to which we have a connection).  If subnetsarelocal
200  * is true, this includes other subnets of the local net.
201  * Otherwise, it includes only the directly-connected (sub)nets.
202  */
203 int
204 in_localaddr(struct in_addr in)
205 {
206 	struct in_ifaddr *ia;
207 
208 	if (subnetsarelocal) {
209 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list)
210 			if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
211 				return (1);
212 	} else {
213 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list)
214 			if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
215 				return (1);
216 	}
217 	return (0);
218 }
219 
220 /*
221  * Determine whether an IP address is in a reserved set of addresses
222  * that may not be forwarded, or whether datagrams to that destination
223  * may be forwarded.
224  */
225 int
226 in_canforward(struct in_addr in)
227 {
228 	u_int32_t net;
229 
230 	if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
231 		return (0);
232 	if (IN_CLASSA(in.s_addr)) {
233 		net = in.s_addr & IN_CLASSA_NET;
234 		if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
235 			return (0);
236 	}
237 	return (1);
238 }
239 
240 /*
241  * Trim a mask in a sockaddr
242  */
243 void
244 in_socktrim(struct sockaddr_in *ap)
245 {
246 	char *cplim = (char *) &ap->sin_addr;
247 	char *cp = (char *) (&ap->sin_addr + 1);
248 
249 	ap->sin_len = 0;
250 	while (--cp >= cplim)
251 		if (*cp) {
252 			(ap)->sin_len = cp - (char *) (ap) + 1;
253 			break;
254 		}
255 }
256 
257 /*
258  *  Routine to take an Internet address and convert into a
259  *  "dotted quad" representation for printing.
260  */
261 const char *
262 in_fmtaddr(struct in_addr addr)
263 {
264 	static char buf[sizeof("123.456.789.123")];
265 
266 	addr.s_addr = ntohl(addr.s_addr);
267 
268 	snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
269 		(addr.s_addr >> 24) & 0xFF,
270 		(addr.s_addr >> 16) & 0xFF,
271 		(addr.s_addr >>  8) & 0xFF,
272 		(addr.s_addr >>  0) & 0xFF);
273 	return buf;
274 }
275 
276 /*
277  * Maintain the "in_maxmtu" variable, which is the largest
278  * mtu for non-local interfaces with AF_INET addresses assigned
279  * to them that are up.
280  */
281 unsigned long in_maxmtu;
282 
283 void
284 in_setmaxmtu(void)
285 {
286 	struct in_ifaddr *ia;
287 	struct ifnet *ifp;
288 	unsigned long maxmtu = 0;
289 
290 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
291 		if ((ifp = ia->ia_ifp) == 0)
292 			continue;
293 		if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
294 			continue;
295 		if (ifp->if_mtu > maxmtu)
296 			maxmtu = ifp->if_mtu;
297 	}
298 	if (maxmtu)
299 		in_maxmtu = maxmtu;
300 }
301 
302 static u_int
303 in_mask2len(struct in_addr *mask)
304 {
305 	u_int x, y;
306 	u_char *p;
307 
308 	p = (u_char *)mask;
309 	for (x = 0; x < sizeof(*mask); x++) {
310 		if (p[x] != 0xff)
311 			break;
312 	}
313 	y = 0;
314 	if (x < sizeof(*mask)) {
315 		for (y = 0; y < NBBY; y++) {
316 			if ((p[x] & (0x80 >> y)) == 0)
317 				break;
318 		}
319 	}
320 	return x * NBBY + y;
321 }
322 
323 static void
324 in_len2mask(struct in_addr *mask, u_int len)
325 {
326 	u_int i;
327 	u_char *p;
328 
329 	p = (u_char *)mask;
330 	memset(mask, 0, sizeof(*mask));
331 	for (i = 0; i < len / NBBY; i++)
332 		p[i] = 0xff;
333 	if (len % NBBY)
334 		p[i] = (0xff00 >> (len % NBBY)) & 0xff;
335 }
336 
337 /*
338  * Generic internet control operations (ioctl's).
339  * Ifp is 0 if not an interface-specific ioctl.
340  */
341 /* ARGSUSED */
342 int
343 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp,
344     struct lwp *l)
345 {
346 	struct ifreq *ifr = (struct ifreq *)data;
347 	struct in_ifaddr *ia = NULL;
348 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
349 	struct sockaddr_in oldaddr;
350 	int error, hostIsNew, maskIsNew;
351 	int newifaddr = 0;
352 
353 	switch (cmd) {
354 	case SIOCALIFADDR:
355 	case SIOCDLIFADDR:
356 	case SIOCGLIFADDR:
357 		if (ifp == NULL)
358 			return EINVAL;
359 		return in_lifaddr_ioctl(so, cmd, data, ifp, l);
360 	case SIOCGIFADDRPREF:
361 	case SIOCSIFADDRPREF:
362 		if (ifp == NULL)
363 			return EINVAL;
364 		return ifaddrpref_ioctl(so, cmd, data, ifp, l);
365 	}
366 
367 	/*
368 	 * Find address for this interface, if it exists.
369 	 */
370 	if (ifp != NULL)
371 		IFP_TO_IA(ifp, ia);
372 
373 	switch (cmd) {
374 	case SIOCAIFADDR:
375 	case SIOCDIFADDR:
376 	case SIOCGIFALIAS:
377 		if (ifra->ifra_addr.sin_family == AF_INET)
378 			LIST_FOREACH(ia,
379 			    &IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr),
380 			    ia_hash) {
381 				if (ia->ia_ifp == ifp &&
382 				    in_hosteq(ia->ia_addr.sin_addr,
383 				    ifra->ifra_addr.sin_addr))
384 					break;
385 			}
386 		if ((cmd == SIOCDIFADDR || cmd == SIOCGIFALIAS) && ia == NULL)
387 			return (EADDRNOTAVAIL);
388 
389 		if (cmd == SIOCDIFADDR &&
390 		    ifra->ifra_addr.sin_family == AF_UNSPEC) {
391 			ifra->ifra_addr.sin_family = AF_INET;
392 		}
393 		/* FALLTHROUGH */
394 	case SIOCSIFADDR:
395 	case SIOCSIFDSTADDR:
396 		if (ifra->ifra_addr.sin_family != AF_INET)
397 			return (EAFNOSUPPORT);
398 		/* FALLTHROUGH */
399 	case SIOCSIFNETMASK:
400 		if (ifp == NULL)
401 			panic("in_control");
402 
403 		if (cmd == SIOCGIFALIAS)
404 			break;
405 
406 		if (ia == NULL &&
407 		    (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR))
408 			return (EADDRNOTAVAIL);
409 
410 		if (l == NULL)
411 			return (EPERM);
412 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
413 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
414 		    NULL) != 0)
415 			return (EPERM);
416 
417 		if (ia == NULL) {
418 			ia = malloc(sizeof(*ia), M_IFADDR, M_WAITOK|M_ZERO);
419 			if (ia == NULL)
420 				return (ENOBUFS);
421 			TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list);
422 			IFAREF(&ia->ia_ifa);
423 			ifa_insert(ifp, &ia->ia_ifa);
424 			ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
425 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
426 			ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
427 #ifdef IPSELSRC
428 			ia->ia_ifa.ifa_getifa = in_getifa;
429 #else /* IPSELSRC */
430 			ia->ia_ifa.ifa_getifa = NULL;
431 #endif /* IPSELSRC */
432 			ia->ia_sockmask.sin_len = 8;
433 			if (ifp->if_flags & IFF_BROADCAST) {
434 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
435 				ia->ia_broadaddr.sin_family = AF_INET;
436 			}
437 			ia->ia_ifp = ifp;
438 			ia->ia_idsalt = cprng_fast32() % 65535;
439 			LIST_INIT(&ia->ia_multiaddrs);
440 			newifaddr = 1;
441 		}
442 		break;
443 
444 	case SIOCSIFBRDADDR:
445 		if (l == NULL)
446 			return (EPERM);
447 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
448 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
449 		    NULL) != 0)
450 			return (EPERM);
451 		/* FALLTHROUGH */
452 
453 	case SIOCGIFADDR:
454 	case SIOCGIFNETMASK:
455 	case SIOCGIFDSTADDR:
456 	case SIOCGIFBRDADDR:
457 		if (ia == NULL)
458 			return (EADDRNOTAVAIL);
459 		break;
460 	}
461 	error = 0;
462 	switch (cmd) {
463 
464 	case SIOCGIFADDR:
465 		ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr));
466 		break;
467 
468 	case SIOCGIFBRDADDR:
469 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
470 			return (EINVAL);
471 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr));
472 		break;
473 
474 	case SIOCGIFDSTADDR:
475 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
476 			return (EINVAL);
477 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr));
478 		break;
479 
480 	case SIOCGIFNETMASK:
481 		ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_sockmask));
482 		break;
483 
484 	case SIOCSIFDSTADDR:
485 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
486 			return (EINVAL);
487 		oldaddr = ia->ia_dstaddr;
488 		ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr));
489 		if ((error = if_addr_init(ifp, &ia->ia_ifa, false)) != 0) {
490 			ia->ia_dstaddr = oldaddr;
491 			return error;
492 		}
493 		if (ia->ia_flags & IFA_ROUTE) {
494 			ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
495 			rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
496 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
497 			rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP);
498 		}
499 		break;
500 
501 	case SIOCSIFBRDADDR:
502 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
503 			return EINVAL;
504 		ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr));
505 		break;
506 
507 	case SIOCSIFADDR:
508 		error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)),
509 		    1);
510 		if (error == 0) {
511 			(void)pfil_run_hooks(if_pfil,
512 			    (struct mbuf **)SIOCSIFADDR, ifp, PFIL_IFADDR);
513 		}
514 		break;
515 
516 	case SIOCSIFNETMASK:
517 		in_ifscrub(ifp, ia);
518 		ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr));
519 		ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
520 		error = in_ifinit(ifp, ia, NULL, 0);
521 		break;
522 
523 	case SIOCAIFADDR:
524 		maskIsNew = 0;
525 		hostIsNew = 1;
526 		if (ia->ia_addr.sin_family != AF_INET)
527 			;
528 		else if (ifra->ifra_addr.sin_len == 0) {
529 			ifra->ifra_addr = ia->ia_addr;
530 			hostIsNew = 0;
531 		} else if (in_hosteq(ia->ia_addr.sin_addr,
532 		           ifra->ifra_addr.sin_addr))
533 			hostIsNew = 0;
534 		if (ifra->ifra_mask.sin_len) {
535 			/* Only scrub if we control the prefix route,
536 			 * otherwise userland gets a bogus message */
537 			if ((ia->ia_flags & IFA_ROUTE))
538 				in_ifscrub(ifp, ia);
539 			ia->ia_sockmask = ifra->ifra_mask;
540 			ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
541 			maskIsNew = 1;
542 		}
543 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
544 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
545 			/* Only scrub if we control the prefix route,
546 			 * otherwise userland gets a bogus message */
547 			if ((ia->ia_flags & IFA_ROUTE))
548 				in_ifscrub(ifp, ia);
549 			ia->ia_dstaddr = ifra->ifra_dstaddr;
550 			maskIsNew  = 1; /* We lie; but the effect's the same */
551 		}
552 		if (ifra->ifra_addr.sin_family == AF_INET &&
553 		    (hostIsNew || maskIsNew)) {
554 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
555 		}
556 		if ((ifp->if_flags & IFF_BROADCAST) &&
557 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
558 			ia->ia_broadaddr = ifra->ifra_broadaddr;
559 		if (error == 0)
560 			(void)pfil_run_hooks(if_pfil,
561 			    (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR);
562 		break;
563 
564 	case SIOCGIFALIAS:
565 		ifra->ifra_mask = ia->ia_sockmask;
566 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
567 		    (ia->ia_dstaddr.sin_family == AF_INET))
568 			ifra->ifra_dstaddr = ia->ia_dstaddr;
569 		else if ((ifp->if_flags & IFF_BROADCAST) &&
570 		    (ia->ia_broadaddr.sin_family == AF_INET))
571 			ifra->ifra_broadaddr = ia->ia_broadaddr;
572 		else
573 			memset(&ifra->ifra_broadaddr, 0,
574 			      sizeof(ifra->ifra_broadaddr));
575 		break;
576 
577 	case SIOCDIFADDR:
578 		in_purgeaddr(&ia->ia_ifa);
579 		(void)pfil_run_hooks(if_pfil, (struct mbuf **)SIOCDIFADDR,
580 		    ifp, PFIL_IFADDR);
581 		break;
582 
583 #ifdef MROUTING
584 	case SIOCGETVIFCNT:
585 	case SIOCGETSGCNT:
586 		error = mrt_ioctl(so, cmd, data);
587 		break;
588 #endif /* MROUTING */
589 
590 	default:
591 		return ENOTTY;
592 	}
593 
594 	if (error != 0 && newifaddr) {
595 		KASSERT(ia != NULL);
596 		in_purgeaddr(&ia->ia_ifa);
597 	}
598 
599 	return error;
600 }
601 
602 void
603 in_purgeaddr(struct ifaddr *ifa)
604 {
605 	struct ifnet *ifp = ifa->ifa_ifp;
606 	struct in_ifaddr *ia = (void *) ifa;
607 
608 	in_ifscrub(ifp, ia);
609 	LIST_REMOVE(ia, ia_hash);
610 	ifa_remove(ifp, &ia->ia_ifa);
611 	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list);
612 	if (ia->ia_allhosts != NULL)
613 		in_delmulti(ia->ia_allhosts);
614 	IFAFREE(&ia->ia_ifa);
615 	in_setmaxmtu();
616 }
617 
618 void
619 in_purgeif(struct ifnet *ifp)		/* MUST be called at splsoftnet() */
620 {
621 	if_purgeaddrs(ifp, AF_INET, in_purgeaddr);
622 	igmp_purgeif(ifp);		/* manipulates pools */
623 #ifdef MROUTING
624 	ip_mrouter_detach(ifp);
625 #endif
626 }
627 
628 /*
629  * SIOC[GAD]LIFADDR.
630  *	SIOCGLIFADDR: get first address. (???)
631  *	SIOCGLIFADDR with IFLR_PREFIX:
632  *		get first address that matches the specified prefix.
633  *	SIOCALIFADDR: add the specified address.
634  *	SIOCALIFADDR with IFLR_PREFIX:
635  *		EINVAL since we can't deduce hostid part of the address.
636  *	SIOCDLIFADDR: delete the specified address.
637  *	SIOCDLIFADDR with IFLR_PREFIX:
638  *		delete the first address that matches the specified prefix.
639  * return values:
640  *	EINVAL on invalid parameters
641  *	EADDRNOTAVAIL on prefix match failed/specified address not found
642  *	other values may be returned from in_ioctl()
643  */
644 static int
645 in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data,
646     struct ifnet *ifp, struct lwp *l)
647 {
648 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
649 	struct ifaddr *ifa;
650 	struct sockaddr *sa;
651 
652 	/* sanity checks */
653 	if (data == NULL || ifp == NULL) {
654 		panic("invalid argument to in_lifaddr_ioctl");
655 		/*NOTRECHED*/
656 	}
657 
658 	switch (cmd) {
659 	case SIOCGLIFADDR:
660 		/* address must be specified on GET with IFLR_PREFIX */
661 		if ((iflr->flags & IFLR_PREFIX) == 0)
662 			break;
663 		/*FALLTHROUGH*/
664 	case SIOCALIFADDR:
665 	case SIOCDLIFADDR:
666 		/* address must be specified on ADD and DELETE */
667 		sa = (struct sockaddr *)&iflr->addr;
668 		if (sa->sa_family != AF_INET)
669 			return EINVAL;
670 		if (sa->sa_len != sizeof(struct sockaddr_in))
671 			return EINVAL;
672 		/* XXX need improvement */
673 		sa = (struct sockaddr *)&iflr->dstaddr;
674 		if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET)
675 			return EINVAL;
676 		if (sa->sa_len != 0 && sa->sa_len != sizeof(struct sockaddr_in))
677 			return EINVAL;
678 		break;
679 	default: /*shouldn't happen*/
680 #if 0
681 		panic("invalid cmd to in_lifaddr_ioctl");
682 		/*NOTREACHED*/
683 #else
684 		return EOPNOTSUPP;
685 #endif
686 	}
687 	if (sizeof(struct in_addr) * NBBY < iflr->prefixlen)
688 		return EINVAL;
689 
690 	switch (cmd) {
691 	case SIOCALIFADDR:
692 	    {
693 		struct in_aliasreq ifra;
694 
695 		if (iflr->flags & IFLR_PREFIX)
696 			return EINVAL;
697 
698 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
699 		memset(&ifra, 0, sizeof(ifra));
700 		memcpy(ifra.ifra_name, iflr->iflr_name,
701 			sizeof(ifra.ifra_name));
702 
703 		memcpy(&ifra.ifra_addr, &iflr->addr,
704 			((struct sockaddr *)&iflr->addr)->sa_len);
705 
706 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
707 			memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr,
708 				((struct sockaddr *)&iflr->dstaddr)->sa_len);
709 		}
710 
711 		ifra.ifra_mask.sin_family = AF_INET;
712 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
713 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
714 
715 		return in_control(so, SIOCAIFADDR, (void *)&ifra, ifp, l);
716 	    }
717 	case SIOCGLIFADDR:
718 	case SIOCDLIFADDR:
719 	    {
720 		struct in_ifaddr *ia;
721 		struct in_addr mask, candidate, match;
722 		struct sockaddr_in *sin;
723 		int cmp;
724 
725 		memset(&mask, 0, sizeof(mask));
726 		memset(&match, 0, sizeof(match));	/* XXX gcc */
727 		if (iflr->flags & IFLR_PREFIX) {
728 			/* lookup a prefix rather than address. */
729 			in_len2mask(&mask, iflr->prefixlen);
730 
731 			sin = (struct sockaddr_in *)&iflr->addr;
732 			match.s_addr = sin->sin_addr.s_addr;
733 			match.s_addr &= mask.s_addr;
734 
735 			/* if you set extra bits, that's wrong */
736 			if (match.s_addr != sin->sin_addr.s_addr)
737 				return EINVAL;
738 
739 			cmp = 1;
740 		} else {
741 			if (cmd == SIOCGLIFADDR) {
742 				/* on getting an address, take the 1st match */
743 				cmp = 0;	/*XXX*/
744 			} else {
745 				/* on deleting an address, do exact match */
746 				in_len2mask(&mask, 32);
747 				sin = (struct sockaddr_in *)&iflr->addr;
748 				match.s_addr = sin->sin_addr.s_addr;
749 
750 				cmp = 1;
751 			}
752 		}
753 
754 		IFADDR_FOREACH(ifa, ifp) {
755 			if (ifa->ifa_addr->sa_family != AF_INET)
756 				continue;
757 			if (cmp == 0)
758 				break;
759 			candidate.s_addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
760 			candidate.s_addr &= mask.s_addr;
761 			if (candidate.s_addr == match.s_addr)
762 				break;
763 		}
764 		if (ifa == NULL)
765 			return EADDRNOTAVAIL;
766 		ia = (struct in_ifaddr *)ifa;
767 
768 		if (cmd == SIOCGLIFADDR) {
769 			/* fill in the if_laddrreq structure */
770 			memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin_len);
771 
772 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
773 				memcpy(&iflr->dstaddr, &ia->ia_dstaddr,
774 					ia->ia_dstaddr.sin_len);
775 			} else
776 				memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr));
777 
778 			iflr->prefixlen =
779 				in_mask2len(&ia->ia_sockmask.sin_addr);
780 
781 			iflr->flags = 0;	/*XXX*/
782 
783 			return 0;
784 		} else {
785 			struct in_aliasreq ifra;
786 
787 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
788 			memset(&ifra, 0, sizeof(ifra));
789 			memcpy(ifra.ifra_name, iflr->iflr_name,
790 				sizeof(ifra.ifra_name));
791 
792 			memcpy(&ifra.ifra_addr, &ia->ia_addr,
793 				ia->ia_addr.sin_len);
794 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
795 				memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr,
796 					ia->ia_dstaddr.sin_len);
797 			}
798 			memcpy(&ifra.ifra_dstaddr, &ia->ia_sockmask,
799 				ia->ia_sockmask.sin_len);
800 
801 			return in_control(so, SIOCDIFADDR, (void *)&ifra,
802 				ifp, l);
803 		}
804 	    }
805 	}
806 
807 	return EOPNOTSUPP;	/*just for safety*/
808 }
809 
810 /*
811  * Delete any existing route for an interface.
812  */
813 void
814 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
815 {
816 
817 	in_scrubprefix(ia);
818 }
819 
820 /*
821  * Initialize an interface's internet address
822  * and routing table entry.
823  */
824 int
825 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
826     const struct sockaddr_in *sin, int scrub)
827 {
828 	u_int32_t i;
829 	struct sockaddr_in oldaddr;
830 	int s = splnet(), flags = RTF_UP, error;
831 
832 	if (sin == NULL)
833 		sin = &ia->ia_addr;
834 
835 	/*
836 	 * Set up new addresses.
837 	 */
838 	oldaddr = ia->ia_addr;
839 	if (ia->ia_addr.sin_family == AF_INET)
840 		LIST_REMOVE(ia, ia_hash);
841 	ia->ia_addr = *sin;
842 	LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
843 
844 	/*
845 	 * Give the interface a chance to initialize
846 	 * if this is its first address,
847 	 * and to validate the address if necessary.
848 	 */
849 	if ((error = if_addr_init(ifp, &ia->ia_ifa, true)) != 0)
850 		goto bad;
851 	splx(s);
852 	if (scrub) {
853 		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
854 		in_ifscrub(ifp, ia);
855 		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
856 	}
857 
858 	i = ia->ia_addr.sin_addr.s_addr;
859 	if (IN_CLASSA(i))
860 		ia->ia_netmask = IN_CLASSA_NET;
861 	else if (IN_CLASSB(i))
862 		ia->ia_netmask = IN_CLASSB_NET;
863 	else
864 		ia->ia_netmask = IN_CLASSC_NET;
865 	/*
866 	 * The subnet mask usually includes at least the standard network part,
867 	 * but may may be smaller in the case of supernetting.
868 	 * If it is set, we believe it.
869 	 */
870 	if (ia->ia_subnetmask == 0) {
871 		ia->ia_subnetmask = ia->ia_netmask;
872 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
873 	} else
874 		ia->ia_netmask &= ia->ia_subnetmask;
875 
876 	ia->ia_net = i & ia->ia_netmask;
877 	ia->ia_subnet = i & ia->ia_subnetmask;
878 	in_socktrim(&ia->ia_sockmask);
879 	/* re-calculate the "in_maxmtu" value */
880 	in_setmaxmtu();
881 	/*
882 	 * Add route for the network.
883 	 */
884 	ia->ia_ifa.ifa_metric = ifp->if_metric;
885 	if (ifp->if_flags & IFF_BROADCAST) {
886 		ia->ia_broadaddr.sin_addr.s_addr =
887 			ia->ia_subnet | ~ia->ia_subnetmask;
888 		ia->ia_netbroadcast.s_addr =
889 			ia->ia_net | ~ia->ia_netmask;
890 	} else if (ifp->if_flags & IFF_LOOPBACK) {
891 		ia->ia_dstaddr = ia->ia_addr;
892 		flags |= RTF_HOST;
893 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
894 		if (ia->ia_dstaddr.sin_family != AF_INET)
895 			return (0);
896 		flags |= RTF_HOST;
897 	}
898 	error = in_addprefix(ia, flags);
899 	/*
900 	 * If the interface supports multicast, join the "all hosts"
901 	 * multicast group on that interface.
902 	 */
903 	if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
904 		struct in_addr addr;
905 
906 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
907 		ia->ia_allhosts = in_addmulti(&addr, ifp);
908 	}
909 	return (error);
910 bad:
911 	splx(s);
912 	LIST_REMOVE(ia, ia_hash);
913 	ia->ia_addr = oldaddr;
914 	if (ia->ia_addr.sin_family == AF_INET)
915 		LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
916 		    ia, ia_hash);
917 	return (error);
918 }
919 
920 #define rtinitflags(x) \
921 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
922 	    ? RTF_HOST : 0)
923 
924 /*
925  * add a route to prefix ("connected route" in cisco terminology).
926  * does nothing if there's some interface address with the same prefix already.
927  */
928 static int
929 in_addprefix(struct in_ifaddr *target, int flags)
930 {
931 	struct in_ifaddr *ia;
932 	struct in_addr prefix, mask, p;
933 	int error;
934 
935 	if ((flags & RTF_HOST) != 0)
936 		prefix = target->ia_dstaddr.sin_addr;
937 	else {
938 		prefix = target->ia_addr.sin_addr;
939 		mask = target->ia_sockmask.sin_addr;
940 		prefix.s_addr &= mask.s_addr;
941 	}
942 
943 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
944 		if (rtinitflags(ia))
945 			p = ia->ia_dstaddr.sin_addr;
946 		else {
947 			p = ia->ia_addr.sin_addr;
948 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
949 		}
950 
951 		if (prefix.s_addr != p.s_addr)
952 			continue;
953 
954 		/*
955 		 * if we got a matching prefix route inserted by other
956 		 * interface address, we don't need to bother
957 		 *
958 		 * XXX RADIX_MPATH implications here? -dyoung
959 		 *
960 		 * But we should still notify userland of the new address
961 		 */
962 		if (ia->ia_flags & IFA_ROUTE) {
963 			rt_newaddrmsg(RTM_NEWADDR, &target->ia_ifa, 0, NULL);
964 			return 0;
965 		}
966 	}
967 
968 	/*
969 	 * noone seem to have prefix route.  insert it.
970 	 */
971 	error = rtinit(&target->ia_ifa, RTM_ADD, flags);
972 	if (error == 0)
973 		target->ia_flags |= IFA_ROUTE;
974 	else if (error == EEXIST) {
975 		/*
976 		 * the fact the route already exists is not an error.
977 		 */
978 		error = 0;
979 	}
980 	return error;
981 }
982 
983 /*
984  * remove a route to prefix ("connected route" in cisco terminology).
985  * re-installs the route by using another interface address, if there's one
986  * with the same prefix (otherwise we lose the route mistakenly).
987  */
988 static int
989 in_scrubprefix(struct in_ifaddr *target)
990 {
991 	struct in_ifaddr *ia;
992 	struct in_addr prefix, mask, p;
993 	int error;
994 
995 	/* If we don't have IFA_ROUTE we should still inform userland */
996 	if ((target->ia_flags & IFA_ROUTE) == 0) {
997 		rt_newaddrmsg(RTM_DELADDR, &target->ia_ifa, 0, NULL);
998 		return 0;
999 	}
1000 
1001 	if (rtinitflags(target))
1002 		prefix = target->ia_dstaddr.sin_addr;
1003 	else {
1004 		prefix = target->ia_addr.sin_addr;
1005 		mask = target->ia_sockmask.sin_addr;
1006 		prefix.s_addr &= mask.s_addr;
1007 	}
1008 
1009 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
1010 		if (rtinitflags(ia))
1011 			p = ia->ia_dstaddr.sin_addr;
1012 		else {
1013 			p = ia->ia_addr.sin_addr;
1014 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1015 		}
1016 
1017 		if (prefix.s_addr != p.s_addr)
1018 			continue;
1019 
1020 		/*
1021 		 * if we got a matching prefix route, move IFA_ROUTE to him
1022 		 */
1023 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
1024 			rtinit(&target->ia_ifa, RTM_DELETE,
1025 			    rtinitflags(target));
1026 			target->ia_flags &= ~IFA_ROUTE;
1027 
1028 			error = rtinit(&ia->ia_ifa, RTM_ADD,
1029 			    rtinitflags(ia) | RTF_UP);
1030 			if (error == 0)
1031 				ia->ia_flags |= IFA_ROUTE;
1032 			return error;
1033 		}
1034 	}
1035 
1036 	/*
1037 	 * noone seem to have prefix route.  remove it.
1038 	 */
1039 	rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1040 	target->ia_flags &= ~IFA_ROUTE;
1041 	return 0;
1042 }
1043 
1044 #undef rtinitflags
1045 
1046 /*
1047  * Return 1 if the address might be a local broadcast address.
1048  */
1049 int
1050 in_broadcast(struct in_addr in, struct ifnet *ifp)
1051 {
1052 	struct ifaddr *ifa;
1053 
1054 	if (in.s_addr == INADDR_BROADCAST ||
1055 	    in_nullhost(in))
1056 		return 1;
1057 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
1058 		return 0;
1059 	/*
1060 	 * Look through the list of addresses for a match
1061 	 * with a broadcast address.
1062 	 */
1063 #define ia (ifatoia(ifa))
1064 	IFADDR_FOREACH(ifa, ifp)
1065 		if (ifa->ifa_addr->sa_family == AF_INET &&
1066 		    !in_hosteq(in, ia->ia_addr.sin_addr) &&
1067 		    (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
1068 		     in_hosteq(in, ia->ia_netbroadcast) ||
1069 		     (hostzeroisbroadcast &&
1070 		      /*
1071 		       * Check for old-style (host 0) broadcast.
1072 		       */
1073 		      (in.s_addr == ia->ia_subnet ||
1074 		       in.s_addr == ia->ia_net))))
1075 			return 1;
1076 	return (0);
1077 #undef ia
1078 }
1079 
1080 /*
1081  * in_lookup_multi: look up the in_multi record for a given IP
1082  * multicast address on a given interface.  If no matching record is
1083  * found, return NULL.
1084  */
1085 struct in_multi *
1086 in_lookup_multi(struct in_addr addr, ifnet_t *ifp)
1087 {
1088 	struct in_multi *inm;
1089 
1090 	KASSERT(rw_lock_held(&in_multilock));
1091 
1092 	LIST_FOREACH(inm, &IN_MULTI_HASH(addr.s_addr, ifp), inm_list) {
1093 		if (in_hosteq(inm->inm_addr, addr) && inm->inm_ifp == ifp)
1094 			break;
1095 	}
1096 	return inm;
1097 }
1098 
1099 /*
1100  * in_multi_group: check whether the address belongs to an IP multicast
1101  * group we are joined on this interface.  Returns true or false.
1102  */
1103 bool
1104 in_multi_group(struct in_addr addr, ifnet_t *ifp, int flags)
1105 {
1106 	bool ingroup;
1107 
1108 	if (__predict_true(flags & IP_IGMP_MCAST) == 0) {
1109 		rw_enter(&in_multilock, RW_READER);
1110 		ingroup = in_lookup_multi(addr, ifp) != NULL;
1111 		rw_exit(&in_multilock);
1112 	} else {
1113 		/* XXX Recursive call from ip_output(). */
1114 		KASSERT(rw_lock_held(&in_multilock));
1115 		ingroup = in_lookup_multi(addr, ifp) != NULL;
1116 	}
1117 	return ingroup;
1118 }
1119 
1120 /*
1121  * Add an address to the list of IP multicast addresses for a given interface.
1122  */
1123 struct in_multi *
1124 in_addmulti(struct in_addr *ap, ifnet_t *ifp)
1125 {
1126 	struct sockaddr_in sin;
1127 	struct in_multi *inm;
1128 
1129 	/*
1130 	 * See if address already in list.
1131 	 */
1132 	rw_enter(&in_multilock, RW_WRITER);
1133 	inm = in_lookup_multi(*ap, ifp);
1134 	if (inm != NULL) {
1135 		/*
1136 		 * Found it; just increment the reference count.
1137 		 */
1138 		inm->inm_refcount++;
1139 		rw_exit(&in_multilock);
1140 		return inm;
1141 	}
1142 
1143 	/*
1144 	 * New address; allocate a new multicast record.
1145 	 */
1146 	inm = pool_get(&inmulti_pool, PR_NOWAIT);
1147 	if (inm == NULL) {
1148 		rw_exit(&in_multilock);
1149 		return NULL;
1150 	}
1151 	inm->inm_addr = *ap;
1152 	inm->inm_ifp = ifp;
1153 	inm->inm_refcount = 1;
1154 
1155 	/*
1156 	 * Ask the network driver to update its multicast reception
1157 	 * filter appropriately for the new address.
1158 	 */
1159 	sockaddr_in_init(&sin, ap, 0);
1160 	if (if_mcast_op(ifp, SIOCADDMULTI, sintosa(&sin)) != 0) {
1161 		rw_exit(&in_multilock);
1162 		pool_put(&inmulti_pool, inm);
1163 		return NULL;
1164 	}
1165 
1166 	/*
1167 	 * Let IGMP know that we have joined a new IP multicast group.
1168 	 */
1169 	if (igmp_joingroup(inm) != 0) {
1170 		rw_exit(&in_multilock);
1171 		pool_put(&inmulti_pool, inm);
1172 		return NULL;
1173 	}
1174 	LIST_INSERT_HEAD(
1175 	    &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp),
1176 	    inm, inm_list);
1177 	in_multientries++;
1178 	rw_exit(&in_multilock);
1179 
1180 	return inm;
1181 }
1182 
1183 /*
1184  * Delete a multicast address record.
1185  */
1186 void
1187 in_delmulti(struct in_multi *inm)
1188 {
1189 	struct sockaddr_in sin;
1190 
1191 	rw_enter(&in_multilock, RW_WRITER);
1192 	if (--inm->inm_refcount > 0) {
1193 		rw_exit(&in_multilock);
1194 		return;
1195 	}
1196 
1197 	/*
1198 	 * No remaining claims to this record; let IGMP know that
1199 	 * we are leaving the multicast group.
1200 	 */
1201 	igmp_leavegroup(inm);
1202 
1203 	/*
1204 	 * Notify the network driver to update its multicast reception
1205 	 * filter.
1206 	 */
1207 	sockaddr_in_init(&sin, &inm->inm_addr, 0);
1208 	if_mcast_op(inm->inm_ifp, SIOCDELMULTI, sintosa(&sin));
1209 
1210 	/*
1211 	 * Unlink from list.
1212 	 */
1213 	LIST_REMOVE(inm, inm_list);
1214 	in_multientries--;
1215 	rw_exit(&in_multilock);
1216 
1217 	pool_put(&inmulti_pool, inm);
1218 }
1219 
1220 /*
1221  * in_next_multi: step through all of the in_multi records, one at a time.
1222  * The current position is remembered in "step", which the caller must
1223  * provide.  in_first_multi(), below, must be called to initialize "step"
1224  * and get the first record.  Both macros return a NULL "inm" when there
1225  * are no remaining records.
1226  */
1227 struct in_multi *
1228 in_next_multi(struct in_multistep *step)
1229 {
1230 	struct in_multi *inm;
1231 
1232 	KASSERT(rw_lock_held(&in_multilock));
1233 
1234 	while (step->i_inm == NULL && step->i_n < IN_MULTI_HASH_SIZE) {
1235 		step->i_inm = LIST_FIRST(&in_multihashtbl[++step->i_n]);
1236 	}
1237 	if ((inm = step->i_inm) != NULL) {
1238 		step->i_inm = LIST_NEXT(inm, inm_list);
1239 	}
1240 	return inm;
1241 }
1242 
1243 struct in_multi *
1244 in_first_multi(struct in_multistep *step)
1245 {
1246 	KASSERT(rw_lock_held(&in_multilock));
1247 
1248 	step->i_n = 0;
1249 	step->i_inm = LIST_FIRST(&in_multihashtbl[0]);
1250 	return in_next_multi(step);
1251 }
1252 
1253 void
1254 in_multi_lock(int op)
1255 {
1256 	rw_enter(&in_multilock, op);
1257 }
1258 
1259 void
1260 in_multi_unlock(void)
1261 {
1262 	rw_exit(&in_multilock);
1263 }
1264 
1265 int
1266 in_multi_lock_held(void)
1267 {
1268 	return rw_lock_held(&in_multilock);
1269 }
1270 
1271 struct sockaddr_in *
1272 in_selectsrc(struct sockaddr_in *sin, struct route *ro,
1273     int soopts, struct ip_moptions *mopts, int *errorp)
1274 {
1275 	struct rtentry *rt = NULL;
1276 	struct in_ifaddr *ia = NULL;
1277 
1278 	/*
1279          * If route is known or can be allocated now, take the
1280          * source address from the interface.  Otherwise, punt.
1281 	 */
1282 	if ((soopts & SO_DONTROUTE) != 0)
1283 		rtcache_free(ro);
1284 	else {
1285 		union {
1286 			struct sockaddr		dst;
1287 			struct sockaddr_in	dst4;
1288 		} u;
1289 
1290 		sockaddr_in_init(&u.dst4, &sin->sin_addr, 0);
1291 		rt = rtcache_lookup(ro, &u.dst);
1292 	}
1293 	/*
1294 	 * If we found a route, use the address
1295 	 * corresponding to the outgoing interface
1296 	 * unless it is the loopback (in case a route
1297 	 * to our address on another net goes to loopback).
1298 	 *
1299 	 * XXX Is this still true?  Do we care?
1300 	 */
1301 	if (rt != NULL && (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
1302 		ia = ifatoia(rt->rt_ifa);
1303 	if (ia == NULL) {
1304 		u_int16_t fport = sin->sin_port;
1305 
1306 		sin->sin_port = 0;
1307 		ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
1308 		sin->sin_port = fport;
1309 		if (ia == NULL) {
1310 			/* Find 1st non-loopback AF_INET address */
1311 			TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
1312 				if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
1313 					break;
1314 			}
1315 		}
1316 		if (ia == NULL) {
1317 			*errorp = EADDRNOTAVAIL;
1318 			return NULL;
1319 		}
1320 	}
1321 	/*
1322 	 * If the destination address is multicast and an outgoing
1323 	 * interface has been set as a multicast option, use the
1324 	 * address of that interface as our source address.
1325 	 */
1326 	if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
1327 		struct ip_moptions *imo;
1328 		struct ifnet *ifp;
1329 
1330 		imo = mopts;
1331 		if (imo->imo_multicast_ifp != NULL) {
1332 			ifp = imo->imo_multicast_ifp;
1333 			IFP_TO_IA(ifp, ia);		/* XXX */
1334 			if (ia == 0) {
1335 				*errorp = EADDRNOTAVAIL;
1336 				return NULL;
1337 			}
1338 		}
1339 	}
1340 	if (ia->ia_ifa.ifa_getifa != NULL) {
1341 		ia = ifatoia((*ia->ia_ifa.ifa_getifa)(&ia->ia_ifa,
1342 		                                      sintosa(sin)));
1343 	}
1344 #ifdef GETIFA_DEBUG
1345 	else
1346 		printf("%s: missing ifa_getifa\n", __func__);
1347 #endif
1348 	return satosin(&ia->ia_addr);
1349 }
1350 
1351 static void
1352 in_sysctl_init(struct sysctllog **clog)
1353 {
1354 	sysctl_createv(clog, 0, NULL, NULL,
1355 		       CTLFLAG_PERMANENT,
1356 		       CTLTYPE_NODE, "inet",
1357 		       SYSCTL_DESCR("PF_INET related settings"),
1358 		       NULL, 0, NULL, 0,
1359 		       CTL_NET, PF_INET, CTL_EOL);
1360 	sysctl_createv(clog, 0, NULL, NULL,
1361 		       CTLFLAG_PERMANENT,
1362 		       CTLTYPE_NODE, "ip",
1363 		       SYSCTL_DESCR("IPv4 related settings"),
1364 		       NULL, 0, NULL, 0,
1365 		       CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
1366 
1367 	sysctl_createv(clog, 0, NULL, NULL,
1368 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1369 		       CTLTYPE_INT, "subnetsarelocal",
1370 		       SYSCTL_DESCR("Whether logical subnets are considered "
1371 				    "local"),
1372 		       NULL, 0, &subnetsarelocal, 0,
1373 		       CTL_NET, PF_INET, IPPROTO_IP,
1374 		       IPCTL_SUBNETSARELOCAL, CTL_EOL);
1375 	sysctl_createv(clog, 0, NULL, NULL,
1376 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1377 		       CTLTYPE_INT, "hostzerobroadcast",
1378 		       SYSCTL_DESCR("All zeroes address is broadcast address"),
1379 		       NULL, 0, &hostzeroisbroadcast, 0,
1380 		       CTL_NET, PF_INET, IPPROTO_IP,
1381 		       IPCTL_HOSTZEROBROADCAST, CTL_EOL);
1382 }
1383