xref: /netbsd-src/sys/netinet/in.c (revision f89f6560d453f5e37386cc7938c072d2f528b9fa)
1 /*	$NetBSD: in.c,v 1.151 2015/02/26 12:58:36 roy 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.151 2015/02/26 12:58:36 roy 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 *);
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 {
345 	struct ifreq *ifr = (struct ifreq *)data;
346 	struct in_ifaddr *ia = NULL;
347 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
348 	struct sockaddr_in oldaddr;
349 	int error, hostIsNew, maskIsNew;
350 	int newifaddr = 0;
351 
352 	switch (cmd) {
353 	case SIOCALIFADDR:
354 	case SIOCDLIFADDR:
355 	case SIOCGLIFADDR:
356 		if (ifp == NULL)
357 			return EINVAL;
358 		return in_lifaddr_ioctl(so, cmd, data, ifp);
359 	case SIOCGIFADDRPREF:
360 	case SIOCSIFADDRPREF:
361 		if (ifp == NULL)
362 			return EINVAL;
363 		return ifaddrpref_ioctl(so, cmd, data, ifp);
364 	}
365 
366 	/*
367 	 * Find address for this interface, if it exists.
368 	 */
369 	if (ifp != NULL)
370 		IFP_TO_IA(ifp, ia);
371 
372 	switch (cmd) {
373 	case SIOCAIFADDR:
374 	case SIOCDIFADDR:
375 	case SIOCGIFALIAS:
376 		if (ifra->ifra_addr.sin_family == AF_INET)
377 			LIST_FOREACH(ia,
378 			    &IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr),
379 			    ia_hash) {
380 				if (ia->ia_ifp == ifp &&
381 				    in_hosteq(ia->ia_addr.sin_addr,
382 				    ifra->ifra_addr.sin_addr))
383 					break;
384 			}
385 		if ((cmd == SIOCDIFADDR || cmd == SIOCGIFALIAS) && ia == NULL)
386 			return (EADDRNOTAVAIL);
387 
388 		if (cmd == SIOCDIFADDR &&
389 		    ifra->ifra_addr.sin_family == AF_UNSPEC) {
390 			ifra->ifra_addr.sin_family = AF_INET;
391 		}
392 		/* FALLTHROUGH */
393 	case SIOCSIFADDR:
394 	case SIOCSIFDSTADDR:
395 		if (ifra->ifra_addr.sin_family != AF_INET)
396 			return (EAFNOSUPPORT);
397 		/* FALLTHROUGH */
398 	case SIOCSIFNETMASK:
399 		if (ifp == NULL)
400 			panic("in_control");
401 
402 		if (cmd == SIOCGIFALIAS)
403 			break;
404 
405 		if (ia == NULL &&
406 		    (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR))
407 			return (EADDRNOTAVAIL);
408 
409 		if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
410 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
411 		    NULL) != 0)
412 			return (EPERM);
413 
414 		if (ia == NULL) {
415 			ia = malloc(sizeof(*ia), M_IFADDR, M_WAITOK|M_ZERO);
416 			if (ia == NULL)
417 				return (ENOBUFS);
418 			TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list);
419 			ifaref(&ia->ia_ifa);
420 			ifa_insert(ifp, &ia->ia_ifa);
421 			ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
422 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
423 			ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
424 #ifdef IPSELSRC
425 			ia->ia_ifa.ifa_getifa = in_getifa;
426 #else /* IPSELSRC */
427 			ia->ia_ifa.ifa_getifa = NULL;
428 #endif /* IPSELSRC */
429 			ia->ia_sockmask.sin_len = 8;
430 			ia->ia_sockmask.sin_family = AF_INET;
431 			if (ifp->if_flags & IFF_BROADCAST) {
432 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
433 				ia->ia_broadaddr.sin_family = AF_INET;
434 			}
435 			ia->ia_ifp = ifp;
436 			ia->ia_idsalt = cprng_fast32() % 65535;
437 			LIST_INIT(&ia->ia_multiaddrs);
438 			newifaddr = 1;
439 		}
440 		break;
441 
442 	case SIOCSIFBRDADDR:
443 		if (kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_INTERFACE,
444 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
445 		    NULL) != 0)
446 			return (EPERM);
447 		/* FALLTHROUGH */
448 
449 	case SIOCGIFADDR:
450 	case SIOCGIFNETMASK:
451 	case SIOCGIFDSTADDR:
452 	case SIOCGIFBRDADDR:
453 		if (ia == NULL)
454 			return (EADDRNOTAVAIL);
455 		break;
456 	}
457 	error = 0;
458 	switch (cmd) {
459 
460 	case SIOCGIFADDR:
461 		ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr));
462 		break;
463 
464 	case SIOCGIFBRDADDR:
465 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
466 			return (EINVAL);
467 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr));
468 		break;
469 
470 	case SIOCGIFDSTADDR:
471 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
472 			return (EINVAL);
473 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr));
474 		break;
475 
476 	case SIOCGIFNETMASK:
477 		/*
478 		 * We keep the number of trailing zero bytes the sin_len field
479 		 * of ia_sockmask, so we fix this before we pass it back to
480 		 * userland.
481 		 */
482 		oldaddr = ia->ia_sockmask;
483 		oldaddr.sin_len = sizeof(struct sockaddr_in);
484 		ifreq_setaddr(cmd, ifr, (const void *)&oldaddr);
485 		break;
486 
487 	case SIOCSIFDSTADDR:
488 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
489 			return (EINVAL);
490 		oldaddr = ia->ia_dstaddr;
491 		ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr));
492 		if ((error = if_addr_init(ifp, &ia->ia_ifa, false)) != 0) {
493 			ia->ia_dstaddr = oldaddr;
494 			return error;
495 		}
496 		if (ia->ia_flags & IFA_ROUTE) {
497 			ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
498 			rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
499 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
500 			rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP);
501 		}
502 		break;
503 
504 	case SIOCSIFBRDADDR:
505 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
506 			return EINVAL;
507 		ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr));
508 		break;
509 
510 	case SIOCSIFADDR:
511 		error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)),
512 		    1);
513 		if (error == 0) {
514 			(void)pfil_run_hooks(if_pfil,
515 			    (struct mbuf **)SIOCSIFADDR, ifp, PFIL_IFADDR);
516 		}
517 		break;
518 
519 	case SIOCSIFNETMASK:
520 		in_ifscrub(ifp, ia);
521 		ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr));
522 		ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
523 		error = in_ifinit(ifp, ia, NULL, 0);
524 		break;
525 
526 	case SIOCAIFADDR:
527 		maskIsNew = 0;
528 		hostIsNew = 1;
529 		if (ia->ia_addr.sin_family != AF_INET)
530 			;
531 		else if (ifra->ifra_addr.sin_len == 0) {
532 			ifra->ifra_addr = ia->ia_addr;
533 			hostIsNew = 0;
534 		} else if (in_hosteq(ia->ia_addr.sin_addr,
535 		           ifra->ifra_addr.sin_addr))
536 			hostIsNew = 0;
537 		if (ifra->ifra_mask.sin_len) {
538 			/* Only scrub if we control the prefix route,
539 			 * otherwise userland gets a bogus message */
540 			if ((ia->ia_flags & IFA_ROUTE))
541 				in_ifscrub(ifp, ia);
542 			ia->ia_sockmask = ifra->ifra_mask;
543 			ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
544 			maskIsNew = 1;
545 		}
546 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
547 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
548 			/* Only scrub if we control the prefix route,
549 			 * otherwise userland gets a bogus message */
550 			if ((ia->ia_flags & IFA_ROUTE))
551 				in_ifscrub(ifp, ia);
552 			ia->ia_dstaddr = ifra->ifra_dstaddr;
553 			maskIsNew  = 1; /* We lie; but the effect's the same */
554 		}
555 		if (ifra->ifra_addr.sin_family == AF_INET &&
556 		    (hostIsNew || maskIsNew)) {
557 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
558 		}
559 		if ((ifp->if_flags & IFF_BROADCAST) &&
560 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
561 			ia->ia_broadaddr = ifra->ifra_broadaddr;
562 		if (error == 0)
563 			(void)pfil_run_hooks(if_pfil,
564 			    (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR);
565 		break;
566 
567 	case SIOCGIFALIAS:
568 		ifra->ifra_mask = ia->ia_sockmask;
569 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
570 		    (ia->ia_dstaddr.sin_family == AF_INET))
571 			ifra->ifra_dstaddr = ia->ia_dstaddr;
572 		else if ((ifp->if_flags & IFF_BROADCAST) &&
573 		    (ia->ia_broadaddr.sin_family == AF_INET))
574 			ifra->ifra_broadaddr = ia->ia_broadaddr;
575 		else
576 			memset(&ifra->ifra_broadaddr, 0,
577 			      sizeof(ifra->ifra_broadaddr));
578 		break;
579 
580 	case SIOCDIFADDR:
581 		in_purgeaddr(&ia->ia_ifa);
582 		(void)pfil_run_hooks(if_pfil, (struct mbuf **)SIOCDIFADDR,
583 		    ifp, PFIL_IFADDR);
584 		break;
585 
586 #ifdef MROUTING
587 	case SIOCGETVIFCNT:
588 	case SIOCGETSGCNT:
589 		error = mrt_ioctl(so, cmd, data);
590 		break;
591 #endif /* MROUTING */
592 
593 	default:
594 		return ENOTTY;
595 	}
596 
597 	if (error != 0 && newifaddr) {
598 		KASSERT(ia != NULL);
599 		in_purgeaddr(&ia->ia_ifa);
600 	}
601 
602 	return error;
603 }
604 
605 /* Add ownaddr as loopback rtentry. */
606 static void
607 in_ifaddlocal(struct ifaddr *ifa)
608 {
609 	struct in_ifaddr *ia;
610 
611 	ia = (struct in_ifaddr *)ifa;
612 	if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY ||
613 	    (ia->ia_ifp->if_flags & IFF_POINTOPOINT &&
614 	    in_hosteq(ia->ia_dstaddr.sin_addr, ia->ia_addr.sin_addr)))
615 	{
616 		rt_newaddrmsg(RTM_NEWADDR, ifa, 0, NULL);
617 		return;
618 	}
619 
620 	rt_ifa_addlocal(ifa);
621 }
622 
623 /* Rempve loopback entry of ownaddr */
624 static void
625 in_ifremlocal(struct ifaddr *ifa)
626 {
627 	struct in_ifaddr *ia, *p;
628 	struct ifaddr *alt_ifa = NULL;
629 	int ia_count = 0;
630 
631 	ia = (struct in_ifaddr *)ifa;
632 	/* Delete the entry if exactly one ifaddr matches the
633 	 * address, ifa->ifa_addr. */
634 	TAILQ_FOREACH(p, &in_ifaddrhead, ia_list) {
635 		if (!in_hosteq(p->ia_addr.sin_addr, ia->ia_addr.sin_addr))
636 			continue;
637 		if (p->ia_ifp != ia->ia_ifp)
638 			alt_ifa = &p->ia_ifa;
639 		if (++ia_count > 1 && alt_ifa != NULL)
640 			break;
641 	}
642 
643 	if (ia_count == 0)
644 		return;
645 
646 	rt_ifa_remlocal(ifa, ia_count == 1 ? NULL : alt_ifa);
647 }
648 
649 void
650 in_purgeaddr(struct ifaddr *ifa)
651 {
652 	struct ifnet *ifp = ifa->ifa_ifp;
653 	struct in_ifaddr *ia = (void *) ifa;
654 
655 	in_ifscrub(ifp, ia);
656 	in_ifremlocal(ifa);
657 	LIST_REMOVE(ia, ia_hash);
658 	ifa_remove(ifp, &ia->ia_ifa);
659 	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list);
660 	if (ia->ia_allhosts != NULL)
661 		in_delmulti(ia->ia_allhosts);
662 	ifafree(&ia->ia_ifa);
663 	in_setmaxmtu();
664 }
665 
666 void
667 in_purgeif(struct ifnet *ifp)		/* MUST be called at splsoftnet() */
668 {
669 	if_purgeaddrs(ifp, AF_INET, in_purgeaddr);
670 	igmp_purgeif(ifp);		/* manipulates pools */
671 #ifdef MROUTING
672 	ip_mrouter_detach(ifp);
673 #endif
674 }
675 
676 /*
677  * SIOC[GAD]LIFADDR.
678  *	SIOCGLIFADDR: get first address. (???)
679  *	SIOCGLIFADDR with IFLR_PREFIX:
680  *		get first address that matches the specified prefix.
681  *	SIOCALIFADDR: add the specified address.
682  *	SIOCALIFADDR with IFLR_PREFIX:
683  *		EINVAL since we can't deduce hostid part of the address.
684  *	SIOCDLIFADDR: delete the specified address.
685  *	SIOCDLIFADDR with IFLR_PREFIX:
686  *		delete the first address that matches the specified prefix.
687  * return values:
688  *	EINVAL on invalid parameters
689  *	EADDRNOTAVAIL on prefix match failed/specified address not found
690  *	other values may be returned from in_ioctl()
691  */
692 static int
693 in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data,
694     struct ifnet *ifp)
695 {
696 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
697 	struct ifaddr *ifa;
698 	struct sockaddr *sa;
699 
700 	/* sanity checks */
701 	if (data == NULL || ifp == NULL) {
702 		panic("invalid argument to in_lifaddr_ioctl");
703 		/*NOTRECHED*/
704 	}
705 
706 	switch (cmd) {
707 	case SIOCGLIFADDR:
708 		/* address must be specified on GET with IFLR_PREFIX */
709 		if ((iflr->flags & IFLR_PREFIX) == 0)
710 			break;
711 		/*FALLTHROUGH*/
712 	case SIOCALIFADDR:
713 	case SIOCDLIFADDR:
714 		/* address must be specified on ADD and DELETE */
715 		sa = (struct sockaddr *)&iflr->addr;
716 		if (sa->sa_family != AF_INET)
717 			return EINVAL;
718 		if (sa->sa_len != sizeof(struct sockaddr_in))
719 			return EINVAL;
720 		/* XXX need improvement */
721 		sa = (struct sockaddr *)&iflr->dstaddr;
722 		if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET)
723 			return EINVAL;
724 		if (sa->sa_len != 0 && sa->sa_len != sizeof(struct sockaddr_in))
725 			return EINVAL;
726 		break;
727 	default: /*shouldn't happen*/
728 #if 0
729 		panic("invalid cmd to in_lifaddr_ioctl");
730 		/*NOTREACHED*/
731 #else
732 		return EOPNOTSUPP;
733 #endif
734 	}
735 	if (sizeof(struct in_addr) * NBBY < iflr->prefixlen)
736 		return EINVAL;
737 
738 	switch (cmd) {
739 	case SIOCALIFADDR:
740 	    {
741 		struct in_aliasreq ifra;
742 
743 		if (iflr->flags & IFLR_PREFIX)
744 			return EINVAL;
745 
746 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
747 		memset(&ifra, 0, sizeof(ifra));
748 		memcpy(ifra.ifra_name, iflr->iflr_name,
749 			sizeof(ifra.ifra_name));
750 
751 		memcpy(&ifra.ifra_addr, &iflr->addr,
752 			((struct sockaddr *)&iflr->addr)->sa_len);
753 
754 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
755 			memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr,
756 				((struct sockaddr *)&iflr->dstaddr)->sa_len);
757 		}
758 
759 		ifra.ifra_mask.sin_family = AF_INET;
760 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
761 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
762 
763 		return in_control(so, SIOCAIFADDR, &ifra, ifp);
764 	    }
765 	case SIOCGLIFADDR:
766 	case SIOCDLIFADDR:
767 	    {
768 		struct in_ifaddr *ia;
769 		struct in_addr mask, candidate, match;
770 		struct sockaddr_in *sin;
771 		int cmp;
772 
773 		memset(&mask, 0, sizeof(mask));
774 		memset(&match, 0, sizeof(match));	/* XXX gcc */
775 		if (iflr->flags & IFLR_PREFIX) {
776 			/* lookup a prefix rather than address. */
777 			in_len2mask(&mask, iflr->prefixlen);
778 
779 			sin = (struct sockaddr_in *)&iflr->addr;
780 			match.s_addr = sin->sin_addr.s_addr;
781 			match.s_addr &= mask.s_addr;
782 
783 			/* if you set extra bits, that's wrong */
784 			if (match.s_addr != sin->sin_addr.s_addr)
785 				return EINVAL;
786 
787 			cmp = 1;
788 		} else {
789 			if (cmd == SIOCGLIFADDR) {
790 				/* on getting an address, take the 1st match */
791 				cmp = 0;	/*XXX*/
792 			} else {
793 				/* on deleting an address, do exact match */
794 				in_len2mask(&mask, 32);
795 				sin = (struct sockaddr_in *)&iflr->addr;
796 				match.s_addr = sin->sin_addr.s_addr;
797 
798 				cmp = 1;
799 			}
800 		}
801 
802 		IFADDR_FOREACH(ifa, ifp) {
803 			if (ifa->ifa_addr->sa_family != AF_INET)
804 				continue;
805 			if (cmp == 0)
806 				break;
807 			candidate.s_addr = ((struct sockaddr_in *)ifa->ifa_addr)->sin_addr.s_addr;
808 			candidate.s_addr &= mask.s_addr;
809 			if (candidate.s_addr == match.s_addr)
810 				break;
811 		}
812 		if (ifa == NULL)
813 			return EADDRNOTAVAIL;
814 		ia = (struct in_ifaddr *)ifa;
815 
816 		if (cmd == SIOCGLIFADDR) {
817 			/* fill in the if_laddrreq structure */
818 			memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin_len);
819 
820 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
821 				memcpy(&iflr->dstaddr, &ia->ia_dstaddr,
822 					ia->ia_dstaddr.sin_len);
823 			} else
824 				memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr));
825 
826 			iflr->prefixlen =
827 				in_mask2len(&ia->ia_sockmask.sin_addr);
828 
829 			iflr->flags = 0;	/*XXX*/
830 
831 			return 0;
832 		} else {
833 			struct in_aliasreq ifra;
834 
835 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
836 			memset(&ifra, 0, sizeof(ifra));
837 			memcpy(ifra.ifra_name, iflr->iflr_name,
838 				sizeof(ifra.ifra_name));
839 
840 			memcpy(&ifra.ifra_addr, &ia->ia_addr,
841 				ia->ia_addr.sin_len);
842 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
843 				memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr,
844 					ia->ia_dstaddr.sin_len);
845 			}
846 			memcpy(&ifra.ifra_dstaddr, &ia->ia_sockmask,
847 				ia->ia_sockmask.sin_len);
848 
849 			return in_control(so, SIOCDIFADDR, &ifra, ifp);
850 		}
851 	    }
852 	}
853 
854 	return EOPNOTSUPP;	/*just for safety*/
855 }
856 
857 /*
858  * Delete any existing route for an interface.
859  */
860 void
861 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
862 {
863 
864 	in_scrubprefix(ia);
865 }
866 
867 /*
868  * Initialize an interface's internet address
869  * and routing table entry.
870  */
871 int
872 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
873     const struct sockaddr_in *sin, int scrub)
874 {
875 	u_int32_t i;
876 	struct sockaddr_in oldaddr;
877 	int s = splnet(), flags = RTF_UP, error;
878 
879 	if (sin == NULL)
880 		sin = &ia->ia_addr;
881 
882 	/*
883 	 * Set up new addresses.
884 	 */
885 	oldaddr = ia->ia_addr;
886 	if (ia->ia_addr.sin_family == AF_INET)
887 		LIST_REMOVE(ia, ia_hash);
888 	ia->ia_addr = *sin;
889 	LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
890 
891 	/*
892 	 * Give the interface a chance to initialize
893 	 * if this is its first address,
894 	 * and to validate the address if necessary.
895 	 */
896 	if ((error = if_addr_init(ifp, &ia->ia_ifa, true)) != 0)
897 		goto bad;
898 	splx(s);
899 	if (scrub) {
900 		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
901 		in_ifscrub(ifp, ia);
902 		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
903 	}
904 
905 	/* Add the local route to the address */
906 	in_ifaddlocal(&ia->ia_ifa);
907 
908 	i = ia->ia_addr.sin_addr.s_addr;
909 	if (IN_CLASSA(i))
910 		ia->ia_netmask = IN_CLASSA_NET;
911 	else if (IN_CLASSB(i))
912 		ia->ia_netmask = IN_CLASSB_NET;
913 	else
914 		ia->ia_netmask = IN_CLASSC_NET;
915 	/*
916 	 * The subnet mask usually includes at least the standard network part,
917 	 * but may may be smaller in the case of supernetting.
918 	 * If it is set, we believe it.
919 	 */
920 	if (ia->ia_subnetmask == 0) {
921 		ia->ia_subnetmask = ia->ia_netmask;
922 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
923 	} else
924 		ia->ia_netmask &= ia->ia_subnetmask;
925 
926 	ia->ia_net = i & ia->ia_netmask;
927 	ia->ia_subnet = i & ia->ia_subnetmask;
928 	in_socktrim(&ia->ia_sockmask);
929 	/* re-calculate the "in_maxmtu" value */
930 	in_setmaxmtu();
931 	/*
932 	 * Add route for the network.
933 	 */
934 	ia->ia_ifa.ifa_metric = ifp->if_metric;
935 	if (ifp->if_flags & IFF_BROADCAST) {
936 		ia->ia_broadaddr.sin_addr.s_addr =
937 			ia->ia_subnet | ~ia->ia_subnetmask;
938 		ia->ia_netbroadcast.s_addr =
939 			ia->ia_net | ~ia->ia_netmask;
940 	} else if (ifp->if_flags & IFF_LOOPBACK) {
941 		ia->ia_dstaddr = ia->ia_addr;
942 		flags |= RTF_HOST;
943 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
944 		if (ia->ia_dstaddr.sin_family != AF_INET)
945 			return (0);
946 		flags |= RTF_HOST;
947 	}
948 	error = in_addprefix(ia, flags);
949 	/*
950 	 * If the interface supports multicast, join the "all hosts"
951 	 * multicast group on that interface.
952 	 */
953 	if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
954 		struct in_addr addr;
955 
956 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
957 		ia->ia_allhosts = in_addmulti(&addr, ifp);
958 	}
959 	return (error);
960 bad:
961 	splx(s);
962 	LIST_REMOVE(ia, ia_hash);
963 	ia->ia_addr = oldaddr;
964 	if (ia->ia_addr.sin_family == AF_INET)
965 		LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
966 		    ia, ia_hash);
967 	return (error);
968 }
969 
970 #define rtinitflags(x) \
971 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
972 	    ? RTF_HOST : 0)
973 
974 /*
975  * add a route to prefix ("connected route" in cisco terminology).
976  * does nothing if there's some interface address with the same prefix already.
977  */
978 static int
979 in_addprefix(struct in_ifaddr *target, int flags)
980 {
981 	struct in_ifaddr *ia;
982 	struct in_addr prefix, mask, p;
983 	int error;
984 
985 	if ((flags & RTF_HOST) != 0)
986 		prefix = target->ia_dstaddr.sin_addr;
987 	else {
988 		prefix = target->ia_addr.sin_addr;
989 		mask = target->ia_sockmask.sin_addr;
990 		prefix.s_addr &= mask.s_addr;
991 	}
992 
993 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
994 		if (rtinitflags(ia))
995 			p = ia->ia_dstaddr.sin_addr;
996 		else {
997 			p = ia->ia_addr.sin_addr;
998 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
999 		}
1000 
1001 		if (prefix.s_addr != p.s_addr)
1002 			continue;
1003 
1004 		/*
1005 		 * if we got a matching prefix route inserted by other
1006 		 * interface address, we don't need to bother
1007 		 *
1008 		 * XXX RADIX_MPATH implications here? -dyoung
1009 		 */
1010 		if (ia->ia_flags & IFA_ROUTE)
1011 			return 0;
1012 	}
1013 
1014 	/*
1015 	 * noone seem to have prefix route.  insert it.
1016 	 */
1017 	error = rtinit(&target->ia_ifa, RTM_ADD, flags);
1018 	if (error == 0)
1019 		target->ia_flags |= IFA_ROUTE;
1020 	else if (error == EEXIST) {
1021 		/*
1022 		 * the fact the route already exists is not an error.
1023 		 */
1024 		error = 0;
1025 	}
1026 	return error;
1027 }
1028 
1029 /*
1030  * remove a route to prefix ("connected route" in cisco terminology).
1031  * re-installs the route by using another interface address, if there's one
1032  * with the same prefix (otherwise we lose the route mistakenly).
1033  */
1034 static int
1035 in_scrubprefix(struct in_ifaddr *target)
1036 {
1037 	struct in_ifaddr *ia;
1038 	struct in_addr prefix, mask, p;
1039 	int error;
1040 
1041 	/* If we don't have IFA_ROUTE we should still inform userland */
1042 	if ((target->ia_flags & IFA_ROUTE) == 0)
1043 		return 0;
1044 
1045 	if (rtinitflags(target))
1046 		prefix = target->ia_dstaddr.sin_addr;
1047 	else {
1048 		prefix = target->ia_addr.sin_addr;
1049 		mask = target->ia_sockmask.sin_addr;
1050 		prefix.s_addr &= mask.s_addr;
1051 	}
1052 
1053 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
1054 		if (rtinitflags(ia))
1055 			p = ia->ia_dstaddr.sin_addr;
1056 		else {
1057 			p = ia->ia_addr.sin_addr;
1058 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1059 		}
1060 
1061 		if (prefix.s_addr != p.s_addr)
1062 			continue;
1063 
1064 		/*
1065 		 * if we got a matching prefix route, move IFA_ROUTE to him
1066 		 */
1067 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
1068 			rtinit(&target->ia_ifa, RTM_DELETE,
1069 			    rtinitflags(target));
1070 			target->ia_flags &= ~IFA_ROUTE;
1071 
1072 			error = rtinit(&ia->ia_ifa, RTM_ADD,
1073 			    rtinitflags(ia) | RTF_UP);
1074 			if (error == 0)
1075 				ia->ia_flags |= IFA_ROUTE;
1076 			return error;
1077 		}
1078 	}
1079 
1080 	/*
1081 	 * noone seem to have prefix route.  remove it.
1082 	 */
1083 	rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1084 	target->ia_flags &= ~IFA_ROUTE;
1085 	return 0;
1086 }
1087 
1088 #undef rtinitflags
1089 
1090 /*
1091  * Return 1 if the address might be a local broadcast address.
1092  */
1093 int
1094 in_broadcast(struct in_addr in, struct ifnet *ifp)
1095 {
1096 	struct ifaddr *ifa;
1097 
1098 	if (in.s_addr == INADDR_BROADCAST ||
1099 	    in_nullhost(in))
1100 		return 1;
1101 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
1102 		return 0;
1103 	/*
1104 	 * Look through the list of addresses for a match
1105 	 * with a broadcast address.
1106 	 */
1107 #define ia (ifatoia(ifa))
1108 	IFADDR_FOREACH(ifa, ifp)
1109 		if (ifa->ifa_addr->sa_family == AF_INET &&
1110 		    !in_hosteq(in, ia->ia_addr.sin_addr) &&
1111 		    (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
1112 		     in_hosteq(in, ia->ia_netbroadcast) ||
1113 		     (hostzeroisbroadcast &&
1114 		      /*
1115 		       * Check for old-style (host 0) broadcast.
1116 		       */
1117 		      (in.s_addr == ia->ia_subnet ||
1118 		       in.s_addr == ia->ia_net))))
1119 			return 1;
1120 	return (0);
1121 #undef ia
1122 }
1123 
1124 /*
1125  * in_lookup_multi: look up the in_multi record for a given IP
1126  * multicast address on a given interface.  If no matching record is
1127  * found, return NULL.
1128  */
1129 struct in_multi *
1130 in_lookup_multi(struct in_addr addr, ifnet_t *ifp)
1131 {
1132 	struct in_multi *inm;
1133 
1134 	KASSERT(rw_lock_held(&in_multilock));
1135 
1136 	LIST_FOREACH(inm, &IN_MULTI_HASH(addr.s_addr, ifp), inm_list) {
1137 		if (in_hosteq(inm->inm_addr, addr) && inm->inm_ifp == ifp)
1138 			break;
1139 	}
1140 	return inm;
1141 }
1142 
1143 /*
1144  * in_multi_group: check whether the address belongs to an IP multicast
1145  * group we are joined on this interface.  Returns true or false.
1146  */
1147 bool
1148 in_multi_group(struct in_addr addr, ifnet_t *ifp, int flags)
1149 {
1150 	bool ingroup;
1151 
1152 	if (__predict_true(flags & IP_IGMP_MCAST) == 0) {
1153 		rw_enter(&in_multilock, RW_READER);
1154 		ingroup = in_lookup_multi(addr, ifp) != NULL;
1155 		rw_exit(&in_multilock);
1156 	} else {
1157 		/* XXX Recursive call from ip_output(). */
1158 		KASSERT(rw_lock_held(&in_multilock));
1159 		ingroup = in_lookup_multi(addr, ifp) != NULL;
1160 	}
1161 	return ingroup;
1162 }
1163 
1164 /*
1165  * Add an address to the list of IP multicast addresses for a given interface.
1166  */
1167 struct in_multi *
1168 in_addmulti(struct in_addr *ap, ifnet_t *ifp)
1169 {
1170 	struct sockaddr_in sin;
1171 	struct in_multi *inm;
1172 
1173 	/*
1174 	 * See if address already in list.
1175 	 */
1176 	rw_enter(&in_multilock, RW_WRITER);
1177 	inm = in_lookup_multi(*ap, ifp);
1178 	if (inm != NULL) {
1179 		/*
1180 		 * Found it; just increment the reference count.
1181 		 */
1182 		inm->inm_refcount++;
1183 		rw_exit(&in_multilock);
1184 		return inm;
1185 	}
1186 
1187 	/*
1188 	 * New address; allocate a new multicast record.
1189 	 */
1190 	inm = pool_get(&inmulti_pool, PR_NOWAIT);
1191 	if (inm == NULL) {
1192 		rw_exit(&in_multilock);
1193 		return NULL;
1194 	}
1195 	inm->inm_addr = *ap;
1196 	inm->inm_ifp = ifp;
1197 	inm->inm_refcount = 1;
1198 
1199 	/*
1200 	 * Ask the network driver to update its multicast reception
1201 	 * filter appropriately for the new address.
1202 	 */
1203 	sockaddr_in_init(&sin, ap, 0);
1204 	if (if_mcast_op(ifp, SIOCADDMULTI, sintosa(&sin)) != 0) {
1205 		rw_exit(&in_multilock);
1206 		pool_put(&inmulti_pool, inm);
1207 		return NULL;
1208 	}
1209 
1210 	/*
1211 	 * Let IGMP know that we have joined a new IP multicast group.
1212 	 */
1213 	if (igmp_joingroup(inm) != 0) {
1214 		rw_exit(&in_multilock);
1215 		pool_put(&inmulti_pool, inm);
1216 		return NULL;
1217 	}
1218 	LIST_INSERT_HEAD(
1219 	    &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp),
1220 	    inm, inm_list);
1221 	in_multientries++;
1222 	rw_exit(&in_multilock);
1223 
1224 	return inm;
1225 }
1226 
1227 /*
1228  * Delete a multicast address record.
1229  */
1230 void
1231 in_delmulti(struct in_multi *inm)
1232 {
1233 	struct sockaddr_in sin;
1234 
1235 	rw_enter(&in_multilock, RW_WRITER);
1236 	if (--inm->inm_refcount > 0) {
1237 		rw_exit(&in_multilock);
1238 		return;
1239 	}
1240 
1241 	/*
1242 	 * No remaining claims to this record; let IGMP know that
1243 	 * we are leaving the multicast group.
1244 	 */
1245 	igmp_leavegroup(inm);
1246 
1247 	/*
1248 	 * Notify the network driver to update its multicast reception
1249 	 * filter.
1250 	 */
1251 	sockaddr_in_init(&sin, &inm->inm_addr, 0);
1252 	if_mcast_op(inm->inm_ifp, SIOCDELMULTI, sintosa(&sin));
1253 
1254 	/*
1255 	 * Unlink from list.
1256 	 */
1257 	LIST_REMOVE(inm, inm_list);
1258 	in_multientries--;
1259 	rw_exit(&in_multilock);
1260 
1261 	pool_put(&inmulti_pool, inm);
1262 }
1263 
1264 /*
1265  * in_next_multi: step through all of the in_multi records, one at a time.
1266  * The current position is remembered in "step", which the caller must
1267  * provide.  in_first_multi(), below, must be called to initialize "step"
1268  * and get the first record.  Both macros return a NULL "inm" when there
1269  * are no remaining records.
1270  */
1271 struct in_multi *
1272 in_next_multi(struct in_multistep *step)
1273 {
1274 	struct in_multi *inm;
1275 
1276 	KASSERT(rw_lock_held(&in_multilock));
1277 
1278 	while (step->i_inm == NULL && step->i_n < IN_MULTI_HASH_SIZE) {
1279 		step->i_inm = LIST_FIRST(&in_multihashtbl[++step->i_n]);
1280 	}
1281 	if ((inm = step->i_inm) != NULL) {
1282 		step->i_inm = LIST_NEXT(inm, inm_list);
1283 	}
1284 	return inm;
1285 }
1286 
1287 struct in_multi *
1288 in_first_multi(struct in_multistep *step)
1289 {
1290 	KASSERT(rw_lock_held(&in_multilock));
1291 
1292 	step->i_n = 0;
1293 	step->i_inm = LIST_FIRST(&in_multihashtbl[0]);
1294 	return in_next_multi(step);
1295 }
1296 
1297 void
1298 in_multi_lock(int op)
1299 {
1300 	rw_enter(&in_multilock, op);
1301 }
1302 
1303 void
1304 in_multi_unlock(void)
1305 {
1306 	rw_exit(&in_multilock);
1307 }
1308 
1309 int
1310 in_multi_lock_held(void)
1311 {
1312 	return rw_lock_held(&in_multilock);
1313 }
1314 
1315 struct sockaddr_in *
1316 in_selectsrc(struct sockaddr_in *sin, struct route *ro,
1317     int soopts, struct ip_moptions *mopts, int *errorp)
1318 {
1319 	struct rtentry *rt = NULL;
1320 	struct in_ifaddr *ia = NULL;
1321 
1322 	/*
1323          * If route is known or can be allocated now, take the
1324          * source address from the interface.  Otherwise, punt.
1325 	 */
1326 	if ((soopts & SO_DONTROUTE) != 0)
1327 		rtcache_free(ro);
1328 	else {
1329 		union {
1330 			struct sockaddr		dst;
1331 			struct sockaddr_in	dst4;
1332 		} u;
1333 
1334 		sockaddr_in_init(&u.dst4, &sin->sin_addr, 0);
1335 		rt = rtcache_lookup(ro, &u.dst);
1336 	}
1337 	/*
1338 	 * If we found a route, use the address
1339 	 * corresponding to the outgoing interface
1340 	 * unless it is the loopback (in case a route
1341 	 * to our address on another net goes to loopback).
1342 	 *
1343 	 * XXX Is this still true?  Do we care?
1344 	 */
1345 	if (rt != NULL && (rt->rt_ifp->if_flags & IFF_LOOPBACK) == 0)
1346 		ia = ifatoia(rt->rt_ifa);
1347 	if (ia == NULL) {
1348 		u_int16_t fport = sin->sin_port;
1349 
1350 		sin->sin_port = 0;
1351 		ia = ifatoia(ifa_ifwithladdr(sintosa(sin)));
1352 		sin->sin_port = fport;
1353 		if (ia == NULL) {
1354 			/* Find 1st non-loopback AF_INET address */
1355 			TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
1356 				if (!(ia->ia_ifp->if_flags & IFF_LOOPBACK))
1357 					break;
1358 			}
1359 		}
1360 		if (ia == NULL) {
1361 			*errorp = EADDRNOTAVAIL;
1362 			return NULL;
1363 		}
1364 	}
1365 	/*
1366 	 * If the destination address is multicast and an outgoing
1367 	 * interface has been set as a multicast option, use the
1368 	 * address of that interface as our source address.
1369 	 */
1370 	if (IN_MULTICAST(sin->sin_addr.s_addr) && mopts != NULL) {
1371 		struct ip_moptions *imo;
1372 		struct ifnet *ifp;
1373 
1374 		imo = mopts;
1375 		if (imo->imo_multicast_ifp != NULL) {
1376 			ifp = imo->imo_multicast_ifp;
1377 			IFP_TO_IA(ifp, ia);		/* XXX */
1378 			if (ia == 0) {
1379 				*errorp = EADDRNOTAVAIL;
1380 				return NULL;
1381 			}
1382 		}
1383 	}
1384 	if (ia->ia_ifa.ifa_getifa != NULL) {
1385 		ia = ifatoia((*ia->ia_ifa.ifa_getifa)(&ia->ia_ifa,
1386 		                                      sintosa(sin)));
1387 	}
1388 #ifdef GETIFA_DEBUG
1389 	else
1390 		printf("%s: missing ifa_getifa\n", __func__);
1391 #endif
1392 	return satosin(&ia->ia_addr);
1393 }
1394 
1395 static void
1396 in_sysctl_init(struct sysctllog **clog)
1397 {
1398 	sysctl_createv(clog, 0, NULL, NULL,
1399 		       CTLFLAG_PERMANENT,
1400 		       CTLTYPE_NODE, "inet",
1401 		       SYSCTL_DESCR("PF_INET related settings"),
1402 		       NULL, 0, NULL, 0,
1403 		       CTL_NET, PF_INET, CTL_EOL);
1404 	sysctl_createv(clog, 0, NULL, NULL,
1405 		       CTLFLAG_PERMANENT,
1406 		       CTLTYPE_NODE, "ip",
1407 		       SYSCTL_DESCR("IPv4 related settings"),
1408 		       NULL, 0, NULL, 0,
1409 		       CTL_NET, PF_INET, IPPROTO_IP, CTL_EOL);
1410 
1411 	sysctl_createv(clog, 0, NULL, NULL,
1412 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1413 		       CTLTYPE_INT, "subnetsarelocal",
1414 		       SYSCTL_DESCR("Whether logical subnets are considered "
1415 				    "local"),
1416 		       NULL, 0, &subnetsarelocal, 0,
1417 		       CTL_NET, PF_INET, IPPROTO_IP,
1418 		       IPCTL_SUBNETSARELOCAL, CTL_EOL);
1419 	sysctl_createv(clog, 0, NULL, NULL,
1420 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
1421 		       CTLTYPE_INT, "hostzerobroadcast",
1422 		       SYSCTL_DESCR("All zeroes address is broadcast address"),
1423 		       NULL, 0, &hostzeroisbroadcast, 0,
1424 		       CTL_NET, PF_INET, IPPROTO_IP,
1425 		       IPCTL_HOSTZEROBROADCAST, CTL_EOL);
1426 }
1427