xref: /netbsd-src/sys/netinet/in.c (revision a7e090f70e491979434963c9a27df4020fe0a18b)
1 /*	$NetBSD: in.c,v 1.137 2010/03/12 13:33:19 oki 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.137 2010/03/12 13:33:19 oki Exp $");
95 
96 #include "opt_inet.h"
97 #include "opt_inet_conf.h"
98 #include "opt_mrouting.h"
99 #include "opt_pfil_hooks.h"
100 
101 #include <sys/param.h>
102 #include <sys/ioctl.h>
103 #include <sys/errno.h>
104 #include <sys/malloc.h>
105 #include <sys/socket.h>
106 #include <sys/socketvar.h>
107 #include <sys/sysctl.h>
108 #include <sys/systm.h>
109 #include <sys/proc.h>
110 #include <sys/syslog.h>
111 #include <sys/kauth.h>
112 
113 #include <net/if.h>
114 #include <net/route.h>
115 
116 #include <net/if_ether.h>
117 
118 #include <netinet/in_systm.h>
119 #include <netinet/in.h>
120 #include <netinet/in_var.h>
121 #include <netinet/ip.h>
122 #include <netinet/ip_var.h>
123 #include <netinet/in_ifattach.h>
124 #include <netinet/in_pcb.h>
125 #include <netinet/if_inarp.h>
126 #include <netinet/ip_mroute.h>
127 #include <netinet/igmp_var.h>
128 
129 #ifdef IPSELSRC
130 #include <netinet/in_selsrc.h>
131 #endif
132 
133 #ifdef PFIL_HOOKS
134 #include <net/pfil.h>
135 #endif
136 
137 static u_int in_mask2len(struct in_addr *);
138 static void in_len2mask(struct in_addr *, u_int);
139 static int in_lifaddr_ioctl(struct socket *, u_long, void *,
140 	struct ifnet *, struct lwp *);
141 
142 static int in_addprefix(struct in_ifaddr *, int);
143 static int in_scrubprefix(struct in_ifaddr *);
144 
145 #ifndef SUBNETSARELOCAL
146 #define	SUBNETSARELOCAL	1
147 #endif
148 
149 #ifndef HOSTZEROBROADCAST
150 #define HOSTZEROBROADCAST 1
151 #endif
152 
153 int subnetsarelocal = SUBNETSARELOCAL;
154 int hostzeroisbroadcast = HOSTZEROBROADCAST;
155 
156 /*
157  * This list is used to keep track of in_multi chains which belong to
158  * deleted interface addresses.  We use in_ifaddr so that a chain head
159  * won't be deallocated until all multicast address record are deleted.
160  */
161 static TAILQ_HEAD(, in_ifaddr) in_mk = TAILQ_HEAD_INITIALIZER(in_mk);
162 
163 /*
164  * Return 1 if an internet address is for a ``local'' host
165  * (one to which we have a connection).  If subnetsarelocal
166  * is true, this includes other subnets of the local net.
167  * Otherwise, it includes only the directly-connected (sub)nets.
168  */
169 int
170 in_localaddr(struct in_addr in)
171 {
172 	struct in_ifaddr *ia;
173 
174 	if (subnetsarelocal) {
175 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list)
176 			if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
177 				return (1);
178 	} else {
179 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list)
180 			if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
181 				return (1);
182 	}
183 	return (0);
184 }
185 
186 /*
187  * Determine whether an IP address is in a reserved set of addresses
188  * that may not be forwarded, or whether datagrams to that destination
189  * may be forwarded.
190  */
191 int
192 in_canforward(struct in_addr in)
193 {
194 	u_int32_t net;
195 
196 	if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
197 		return (0);
198 	if (IN_CLASSA(in.s_addr)) {
199 		net = in.s_addr & IN_CLASSA_NET;
200 		if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
201 			return (0);
202 	}
203 	return (1);
204 }
205 
206 /*
207  * Trim a mask in a sockaddr
208  */
209 void
210 in_socktrim(struct sockaddr_in *ap)
211 {
212 	char *cplim = (char *) &ap->sin_addr;
213 	char *cp = (char *) (&ap->sin_addr + 1);
214 
215 	ap->sin_len = 0;
216 	while (--cp >= cplim)
217 		if (*cp) {
218 			(ap)->sin_len = cp - (char *) (ap) + 1;
219 			break;
220 		}
221 }
222 
223 /*
224  *  Routine to take an Internet address and convert into a
225  *  "dotted quad" representation for printing.
226  */
227 const char *
228 in_fmtaddr(struct in_addr addr)
229 {
230 	static char buf[sizeof("123.456.789.123")];
231 
232 	addr.s_addr = ntohl(addr.s_addr);
233 
234 	snprintf(buf, sizeof(buf), "%d.%d.%d.%d",
235 		(addr.s_addr >> 24) & 0xFF,
236 		(addr.s_addr >> 16) & 0xFF,
237 		(addr.s_addr >>  8) & 0xFF,
238 		(addr.s_addr >>  0) & 0xFF);
239 	return buf;
240 }
241 
242 /*
243  * Maintain the "in_maxmtu" variable, which is the largest
244  * mtu for non-local interfaces with AF_INET addresses assigned
245  * to them that are up.
246  */
247 unsigned long in_maxmtu;
248 
249 void
250 in_setmaxmtu(void)
251 {
252 	struct in_ifaddr *ia;
253 	struct ifnet *ifp;
254 	unsigned long maxmtu = 0;
255 
256 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
257 		if ((ifp = ia->ia_ifp) == 0)
258 			continue;
259 		if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
260 			continue;
261 		if (ifp->if_mtu > maxmtu)
262 			maxmtu = ifp->if_mtu;
263 	}
264 	if (maxmtu)
265 		in_maxmtu = maxmtu;
266 }
267 
268 static u_int
269 in_mask2len(struct in_addr *mask)
270 {
271 	u_int x, y;
272 	u_char *p;
273 
274 	p = (u_char *)mask;
275 	for (x = 0; x < sizeof(*mask); x++) {
276 		if (p[x] != 0xff)
277 			break;
278 	}
279 	y = 0;
280 	if (x < sizeof(*mask)) {
281 		for (y = 0; y < NBBY; y++) {
282 			if ((p[x] & (0x80 >> y)) == 0)
283 				break;
284 		}
285 	}
286 	return x * NBBY + y;
287 }
288 
289 static void
290 in_len2mask(struct in_addr *mask, u_int len)
291 {
292 	u_int i;
293 	u_char *p;
294 
295 	p = (u_char *)mask;
296 	memset(mask, 0, sizeof(*mask));
297 	for (i = 0; i < len / NBBY; i++)
298 		p[i] = 0xff;
299 	if (len % NBBY)
300 		p[i] = (0xff00 >> (len % NBBY)) & 0xff;
301 }
302 
303 /*
304  * Generic internet control operations (ioctl's).
305  * Ifp is 0 if not an interface-specific ioctl.
306  */
307 /* ARGSUSED */
308 int
309 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp,
310     struct lwp *l)
311 {
312 	struct ifreq *ifr = (struct ifreq *)data;
313 	struct in_ifaddr *ia = NULL;
314 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
315 	struct sockaddr_in oldaddr;
316 	int error, hostIsNew, maskIsNew;
317 	int newifaddr = 0;
318 
319 	switch (cmd) {
320 	case SIOCALIFADDR:
321 	case SIOCDLIFADDR:
322 	case SIOCGLIFADDR:
323 		if (ifp == NULL)
324 			return EINVAL;
325 		return in_lifaddr_ioctl(so, cmd, data, ifp, l);
326 	case SIOCGIFADDRPREF:
327 	case SIOCSIFADDRPREF:
328 		if (ifp == NULL)
329 			return EINVAL;
330 		return ifaddrpref_ioctl(so, cmd, data, ifp, l);
331 	}
332 
333 	/*
334 	 * Find address for this interface, if it exists.
335 	 */
336 	if (ifp != NULL)
337 		IFP_TO_IA(ifp, ia);
338 
339 	switch (cmd) {
340 	case SIOCAIFADDR:
341 	case SIOCDIFADDR:
342 	case SIOCGIFALIAS:
343 		if (ifra->ifra_addr.sin_family == AF_INET)
344 			LIST_FOREACH(ia,
345 			    &IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr),
346 			    ia_hash) {
347 				if (ia->ia_ifp == ifp &&
348 				    in_hosteq(ia->ia_addr.sin_addr,
349 				    ifra->ifra_addr.sin_addr))
350 					break;
351 			}
352 		if ((cmd == SIOCDIFADDR || cmd == SIOCGIFALIAS) && ia == NULL)
353 			return (EADDRNOTAVAIL);
354 
355 #if 1 /*def COMPAT_43*/
356 		if (cmd == SIOCDIFADDR &&
357 		    ifra->ifra_addr.sin_family == AF_UNSPEC) {
358 			ifra->ifra_addr.sin_family = AF_INET;
359 		}
360 #endif
361 		/* FALLTHROUGH */
362 	case SIOCSIFADDR:
363 	case SIOCSIFDSTADDR:
364 		if (ifra->ifra_addr.sin_family != AF_INET)
365 			return (EAFNOSUPPORT);
366 		/* FALLTHROUGH */
367 	case SIOCSIFNETMASK:
368 		if (ifp == NULL)
369 			panic("in_control");
370 
371 		if (cmd == SIOCGIFALIAS)
372 			break;
373 
374 		if (ia == NULL &&
375 		    (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR))
376 			return (EADDRNOTAVAIL);
377 
378 		if (l == NULL)
379 			return (EPERM);
380 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
381 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
382 		    NULL) != 0)
383 			return (EPERM);
384 
385 		if (ia == NULL) {
386 			ia = malloc(sizeof(*ia), M_IFADDR, M_WAITOK|M_ZERO);
387 			if (ia == NULL)
388 				return (ENOBUFS);
389 			mutex_enter(softnet_lock);
390 			TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list);
391 			IFAREF(&ia->ia_ifa);
392 			ifa_insert(ifp, &ia->ia_ifa);
393 			ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
394 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
395 			ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
396 #ifdef IPSELSRC
397 			ia->ia_ifa.ifa_getifa = in_getifa;
398 #else /* IPSELSRC */
399 			ia->ia_ifa.ifa_getifa = NULL;
400 #endif /* IPSELSRC */
401 			ia->ia_sockmask.sin_len = 8;
402 			if (ifp->if_flags & IFF_BROADCAST) {
403 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
404 				ia->ia_broadaddr.sin_family = AF_INET;
405 			}
406 			ia->ia_ifp = ifp;
407 			ia->ia_idsalt = arc4random() % 65535;
408 			LIST_INIT(&ia->ia_multiaddrs);
409 			mutex_exit(softnet_lock);
410 			newifaddr = 1;
411 		}
412 		break;
413 
414 	case SIOCSIFBRDADDR:
415 		if (l == NULL)
416 			return (EPERM);
417 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
418 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
419 		    NULL) != 0)
420 			return (EPERM);
421 		/* FALLTHROUGH */
422 
423 	case SIOCGIFADDR:
424 	case SIOCGIFNETMASK:
425 	case SIOCGIFDSTADDR:
426 	case SIOCGIFBRDADDR:
427 		if (ia == NULL)
428 			return (EADDRNOTAVAIL);
429 		break;
430 	}
431 	error = 0;
432 	switch (cmd) {
433 
434 	case SIOCGIFADDR:
435 		ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr));
436 		break;
437 
438 	case SIOCGIFBRDADDR:
439 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
440 			return (EINVAL);
441 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr));
442 		break;
443 
444 	case SIOCGIFDSTADDR:
445 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
446 			return (EINVAL);
447 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr));
448 		break;
449 
450 	case SIOCGIFNETMASK:
451 		ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_sockmask));
452 		break;
453 
454 	case SIOCSIFDSTADDR:
455 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
456 			return (EINVAL);
457 		mutex_enter(softnet_lock);
458 		oldaddr = ia->ia_dstaddr;
459 		ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr));
460 		if ((error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ia)) != 0) {
461 			ia->ia_dstaddr = oldaddr;
462 			mutex_exit(softnet_lock);
463 			return error;
464 		}
465 		if (ia->ia_flags & IFA_ROUTE) {
466 			ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
467 			rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
468 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
469 			rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP);
470 		}
471 		mutex_exit(softnet_lock);
472 		break;
473 
474 	case SIOCSIFBRDADDR:
475 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
476 			return EINVAL;
477 		ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr));
478 		break;
479 
480 	case SIOCSIFADDR:
481 		mutex_enter(softnet_lock);
482 		error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)),
483 		    1);
484 #ifdef PFIL_HOOKS
485 		if (error == 0)
486 			(void)pfil_run_hooks(&if_pfil,
487 			    (struct mbuf **)SIOCSIFADDR, ifp, PFIL_IFADDR);
488 #endif
489 		mutex_exit(softnet_lock);
490 		break;
491 
492 	case SIOCSIFNETMASK:
493 		mutex_enter(softnet_lock);
494 		in_ifscrub(ifp, ia);
495 		ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr));
496 		ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
497 		error = in_ifinit(ifp, ia, NULL, 0);
498 		mutex_exit(softnet_lock);
499 		break;
500 
501 	case SIOCAIFADDR:
502 		maskIsNew = 0;
503 		hostIsNew = 1;
504 		mutex_enter(softnet_lock);
505 		if (ia->ia_addr.sin_family != AF_INET)
506 			;
507 		else if (ifra->ifra_addr.sin_len == 0) {
508 			ifra->ifra_addr = ia->ia_addr;
509 			hostIsNew = 0;
510 		} else if (in_hosteq(ia->ia_addr.sin_addr,
511 		           ifra->ifra_addr.sin_addr))
512 			hostIsNew = 0;
513 		if (ifra->ifra_mask.sin_len) {
514 			in_ifscrub(ifp, ia);
515 			ia->ia_sockmask = ifra->ifra_mask;
516 			ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
517 			maskIsNew = 1;
518 		}
519 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
520 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
521 			in_ifscrub(ifp, ia);
522 			ia->ia_dstaddr = ifra->ifra_dstaddr;
523 			maskIsNew  = 1; /* We lie; but the effect's the same */
524 		}
525 		if (ifra->ifra_addr.sin_family == AF_INET &&
526 		    (hostIsNew || maskIsNew)) {
527 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
528 		}
529 		if ((ifp->if_flags & IFF_BROADCAST) &&
530 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
531 			ia->ia_broadaddr = ifra->ifra_broadaddr;
532 #ifdef PFIL_HOOKS
533 		if (error == 0)
534 			(void)pfil_run_hooks(&if_pfil,
535 			    (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR);
536 #endif
537 		mutex_exit(softnet_lock);
538 		break;
539 
540 	case SIOCGIFALIAS:
541 		ifra->ifra_mask = ia->ia_sockmask;
542 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
543 		    (ia->ia_dstaddr.sin_family == AF_INET))
544 			ifra->ifra_dstaddr = ia->ia_dstaddr;
545 		else if ((ifp->if_flags & IFF_BROADCAST) &&
546 		    (ia->ia_broadaddr.sin_family == AF_INET))
547 			ifra->ifra_broadaddr = ia->ia_broadaddr;
548 		else
549 			memset(&ifra->ifra_broadaddr, 0,
550 			      sizeof(ifra->ifra_broadaddr));
551 		break;
552 
553 	case SIOCDIFADDR:
554 		mutex_enter(softnet_lock);
555 		in_purgeaddr(&ia->ia_ifa);
556 #ifdef PFIL_HOOKS
557 		(void)pfil_run_hooks(&if_pfil, (struct mbuf **)SIOCDIFADDR,
558 		    ifp, PFIL_IFADDR);
559 #endif
560 		mutex_exit(softnet_lock);
561 		break;
562 
563 #ifdef MROUTING
564 	case SIOCGETVIFCNT:
565 	case SIOCGETSGCNT:
566 		error = mrt_ioctl(so, cmd, data);
567 		break;
568 #endif /* MROUTING */
569 
570 	default:
571 		return ENOTTY;
572 	}
573 
574 	if (error != 0 && newifaddr) {
575 		KASSERT(ia != NULL);
576 		in_purgeaddr(&ia->ia_ifa);
577 	}
578 
579 	return error;
580 }
581 
582 void
583 in_purgeaddr(struct ifaddr *ifa)
584 {
585 	struct ifnet *ifp = ifa->ifa_ifp;
586 	struct in_ifaddr *ia = (void *) ifa;
587 
588 	in_ifscrub(ifp, ia);
589 	LIST_REMOVE(ia, ia_hash);
590 	ifa_remove(ifp, &ia->ia_ifa);
591 	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list);
592 	if (ia->ia_allhosts != NULL)
593 		in_delmulti(ia->ia_allhosts);
594 	IFAFREE(&ia->ia_ifa);
595 	in_setmaxmtu();
596 }
597 
598 void
599 in_purgeif(struct ifnet *ifp)		/* MUST be called at splsoftnet() */
600 {
601 	if_purgeaddrs(ifp, AF_INET, in_purgeaddr);
602 	igmp_purgeif(ifp);		/* manipulates pools */
603 #ifdef MROUTING
604 	ip_mrouter_detach(ifp);
605 #endif
606 }
607 
608 /*
609  * SIOC[GAD]LIFADDR.
610  *	SIOCGLIFADDR: get first address. (???)
611  *	SIOCGLIFADDR with IFLR_PREFIX:
612  *		get first address that matches the specified prefix.
613  *	SIOCALIFADDR: add the specified address.
614  *	SIOCALIFADDR with IFLR_PREFIX:
615  *		EINVAL since we can't deduce hostid part of the address.
616  *	SIOCDLIFADDR: delete the specified address.
617  *	SIOCDLIFADDR with IFLR_PREFIX:
618  *		delete the first address that matches the specified prefix.
619  * return values:
620  *	EINVAL on invalid parameters
621  *	EADDRNOTAVAIL on prefix match failed/specified address not found
622  *	other values may be returned from in_ioctl()
623  */
624 static int
625 in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data,
626     struct ifnet *ifp, struct lwp *l)
627 {
628 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
629 	struct ifaddr *ifa;
630 	struct sockaddr *sa;
631 
632 	/* sanity checks */
633 	if (data == NULL || ifp == NULL) {
634 		panic("invalid argument to in_lifaddr_ioctl");
635 		/*NOTRECHED*/
636 	}
637 
638 	switch (cmd) {
639 	case SIOCGLIFADDR:
640 		/* address must be specified on GET with IFLR_PREFIX */
641 		if ((iflr->flags & IFLR_PREFIX) == 0)
642 			break;
643 		/*FALLTHROUGH*/
644 	case SIOCALIFADDR:
645 	case SIOCDLIFADDR:
646 		/* address must be specified on ADD and DELETE */
647 		sa = (struct sockaddr *)&iflr->addr;
648 		if (sa->sa_family != AF_INET)
649 			return EINVAL;
650 		if (sa->sa_len != sizeof(struct sockaddr_in))
651 			return EINVAL;
652 		/* XXX need improvement */
653 		sa = (struct sockaddr *)&iflr->dstaddr;
654 		if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET)
655 			return EINVAL;
656 		if (sa->sa_len != 0 && sa->sa_len != sizeof(struct sockaddr_in))
657 			return EINVAL;
658 		break;
659 	default: /*shouldn't happen*/
660 #if 0
661 		panic("invalid cmd to in_lifaddr_ioctl");
662 		/*NOTREACHED*/
663 #else
664 		return EOPNOTSUPP;
665 #endif
666 	}
667 	if (sizeof(struct in_addr) * NBBY < iflr->prefixlen)
668 		return EINVAL;
669 
670 	switch (cmd) {
671 	case SIOCALIFADDR:
672 	    {
673 		struct in_aliasreq ifra;
674 
675 		if (iflr->flags & IFLR_PREFIX)
676 			return EINVAL;
677 
678 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
679 		memset(&ifra, 0, sizeof(ifra));
680 		memcpy(ifra.ifra_name, iflr->iflr_name,
681 			sizeof(ifra.ifra_name));
682 
683 		memcpy(&ifra.ifra_addr, &iflr->addr,
684 			((struct sockaddr *)&iflr->addr)->sa_len);
685 
686 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
687 			memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr,
688 				((struct sockaddr *)&iflr->dstaddr)->sa_len);
689 		}
690 
691 		ifra.ifra_mask.sin_family = AF_INET;
692 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
693 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
694 
695 		return in_control(so, SIOCAIFADDR, (void *)&ifra, ifp, l);
696 	    }
697 	case SIOCGLIFADDR:
698 	case SIOCDLIFADDR:
699 	    {
700 		struct in_ifaddr *ia;
701 		struct in_addr mask, candidate, match;
702 		struct sockaddr_in *sin;
703 		int cmp;
704 
705 		memset(&mask, 0, sizeof(mask));
706 		memset(&match, 0, sizeof(match));	/* XXX gcc */
707 		if (iflr->flags & IFLR_PREFIX) {
708 			/* lookup a prefix rather than address. */
709 			in_len2mask(&mask, iflr->prefixlen);
710 
711 			sin = (struct sockaddr_in *)&iflr->addr;
712 			match.s_addr = sin->sin_addr.s_addr;
713 			match.s_addr &= mask.s_addr;
714 
715 			/* if you set extra bits, that's wrong */
716 			if (match.s_addr != sin->sin_addr.s_addr)
717 				return EINVAL;
718 
719 			cmp = 1;
720 		} else {
721 			if (cmd == SIOCGLIFADDR) {
722 				/* on getting an address, take the 1st match */
723 				cmp = 0;	/*XXX*/
724 			} else {
725 				/* on deleting an address, do exact match */
726 				in_len2mask(&mask, 32);
727 				sin = (struct sockaddr_in *)&iflr->addr;
728 				match.s_addr = sin->sin_addr.s_addr;
729 
730 				cmp = 1;
731 			}
732 		}
733 
734 		IFADDR_FOREACH(ifa, ifp) {
735 			if (ifa->ifa_addr->sa_family != AF_INET)
736 				continue;
737 			if (cmp == 0)
738 				break;
739 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
740 			candidate.s_addr &= mask.s_addr;
741 			if (candidate.s_addr == match.s_addr)
742 				break;
743 		}
744 		if (ifa == NULL)
745 			return EADDRNOTAVAIL;
746 		ia = (struct in_ifaddr *)ifa;
747 
748 		if (cmd == SIOCGLIFADDR) {
749 			/* fill in the if_laddrreq structure */
750 			memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin_len);
751 
752 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
753 				memcpy(&iflr->dstaddr, &ia->ia_dstaddr,
754 					ia->ia_dstaddr.sin_len);
755 			} else
756 				memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr));
757 
758 			iflr->prefixlen =
759 				in_mask2len(&ia->ia_sockmask.sin_addr);
760 
761 			iflr->flags = 0;	/*XXX*/
762 
763 			return 0;
764 		} else {
765 			struct in_aliasreq ifra;
766 
767 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
768 			memset(&ifra, 0, sizeof(ifra));
769 			memcpy(ifra.ifra_name, iflr->iflr_name,
770 				sizeof(ifra.ifra_name));
771 
772 			memcpy(&ifra.ifra_addr, &ia->ia_addr,
773 				ia->ia_addr.sin_len);
774 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
775 				memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr,
776 					ia->ia_dstaddr.sin_len);
777 			}
778 			memcpy(&ifra.ifra_dstaddr, &ia->ia_sockmask,
779 				ia->ia_sockmask.sin_len);
780 
781 			return in_control(so, SIOCDIFADDR, (void *)&ifra,
782 				ifp, l);
783 		}
784 	    }
785 	}
786 
787 	return EOPNOTSUPP;	/*just for safety*/
788 }
789 
790 /*
791  * Delete any existing route for an interface.
792  */
793 void
794 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
795 {
796 
797 	in_scrubprefix(ia);
798 }
799 
800 /*
801  * Initialize an interface's internet address
802  * and routing table entry.
803  */
804 int
805 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
806     const struct sockaddr_in *sin, int scrub)
807 {
808 	u_int32_t i;
809 	struct sockaddr_in oldaddr;
810 	int s = splnet(), flags = RTF_UP, error;
811 
812 	if (sin == NULL)
813 		sin = &ia->ia_addr;
814 
815 	/*
816 	 * Set up new addresses.
817 	 */
818 	oldaddr = ia->ia_addr;
819 	if (ia->ia_addr.sin_family == AF_INET)
820 		LIST_REMOVE(ia, ia_hash);
821 	ia->ia_addr = *sin;
822 	LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
823 
824 	/*
825 	 * Give the interface a chance to initialize
826 	 * if this is its first address,
827 	 * and to validate the address if necessary.
828 	 */
829 	if ((error = (*ifp->if_ioctl)(ifp, SIOCINITIFADDR, ia)) != 0)
830 		goto bad;
831 	splx(s);
832 	if (scrub) {
833 		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
834 		in_ifscrub(ifp, ia);
835 		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
836 	}
837 
838 	i = ia->ia_addr.sin_addr.s_addr;
839 	if (IN_CLASSA(i))
840 		ia->ia_netmask = IN_CLASSA_NET;
841 	else if (IN_CLASSB(i))
842 		ia->ia_netmask = IN_CLASSB_NET;
843 	else
844 		ia->ia_netmask = IN_CLASSC_NET;
845 	/*
846 	 * The subnet mask usually includes at least the standard network part,
847 	 * but may may be smaller in the case of supernetting.
848 	 * If it is set, we believe it.
849 	 */
850 	if (ia->ia_subnetmask == 0) {
851 		ia->ia_subnetmask = ia->ia_netmask;
852 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
853 	} else
854 		ia->ia_netmask &= ia->ia_subnetmask;
855 
856 	ia->ia_net = i & ia->ia_netmask;
857 	ia->ia_subnet = i & ia->ia_subnetmask;
858 	in_socktrim(&ia->ia_sockmask);
859 	/* re-calculate the "in_maxmtu" value */
860 	in_setmaxmtu();
861 	/*
862 	 * Add route for the network.
863 	 */
864 	ia->ia_ifa.ifa_metric = ifp->if_metric;
865 	if (ifp->if_flags & IFF_BROADCAST) {
866 		ia->ia_broadaddr.sin_addr.s_addr =
867 			ia->ia_subnet | ~ia->ia_subnetmask;
868 		ia->ia_netbroadcast.s_addr =
869 			ia->ia_net | ~ia->ia_netmask;
870 	} else if (ifp->if_flags & IFF_LOOPBACK) {
871 		ia->ia_dstaddr = ia->ia_addr;
872 		flags |= RTF_HOST;
873 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
874 		if (ia->ia_dstaddr.sin_family != AF_INET)
875 			return (0);
876 		flags |= RTF_HOST;
877 	}
878 	error = in_addprefix(ia, flags);
879 	/*
880 	 * If the interface supports multicast, join the "all hosts"
881 	 * multicast group on that interface.
882 	 */
883 	if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
884 		struct in_addr addr;
885 
886 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
887 		ia->ia_allhosts = in_addmulti(&addr, ifp);
888 	}
889 	return (error);
890 bad:
891 	splx(s);
892 	LIST_REMOVE(ia, ia_hash);
893 	ia->ia_addr = oldaddr;
894 	if (ia->ia_addr.sin_family == AF_INET)
895 		LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
896 		    ia, ia_hash);
897 	return (error);
898 }
899 
900 #define rtinitflags(x) \
901 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
902 	    ? RTF_HOST : 0)
903 
904 /*
905  * add a route to prefix ("connected route" in cisco terminology).
906  * does nothing if there's some interface address with the same prefix already.
907  */
908 static int
909 in_addprefix(struct in_ifaddr *target, int flags)
910 {
911 	struct in_ifaddr *ia;
912 	struct in_addr prefix, mask, p;
913 	int error;
914 
915 	if ((flags & RTF_HOST) != 0)
916 		prefix = target->ia_dstaddr.sin_addr;
917 	else {
918 		prefix = target->ia_addr.sin_addr;
919 		mask = target->ia_sockmask.sin_addr;
920 		prefix.s_addr &= mask.s_addr;
921 	}
922 
923 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
924 		if (rtinitflags(ia))
925 			p = ia->ia_dstaddr.sin_addr;
926 		else {
927 			p = ia->ia_addr.sin_addr;
928 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
929 		}
930 
931 		if (prefix.s_addr != p.s_addr)
932 			continue;
933 
934 		/*
935 		 * if we got a matching prefix route inserted by other
936 		 * interface address, we don't need to bother
937 		 *
938 		 * XXX RADIX_MPATH implications here? -dyoung
939 		 */
940 		if (ia->ia_flags & IFA_ROUTE)
941 			return 0;
942 	}
943 
944 	/*
945 	 * noone seem to have prefix route.  insert it.
946 	 */
947 	error = rtinit(&target->ia_ifa, RTM_ADD, flags);
948 	if (error == 0)
949 		target->ia_flags |= IFA_ROUTE;
950 	else if (error == EEXIST) {
951 		/*
952 		 * the fact the route already exists is not an error.
953 		 */
954 		error = 0;
955 	}
956 	return error;
957 }
958 
959 /*
960  * remove a route to prefix ("connected route" in cisco terminology).
961  * re-installs the route by using another interface address, if there's one
962  * with the same prefix (otherwise we lose the route mistakenly).
963  */
964 static int
965 in_scrubprefix(struct in_ifaddr *target)
966 {
967 	struct in_ifaddr *ia;
968 	struct in_addr prefix, mask, p;
969 	int error;
970 
971 	if ((target->ia_flags & IFA_ROUTE) == 0)
972 		return 0;
973 
974 	if (rtinitflags(target))
975 		prefix = target->ia_dstaddr.sin_addr;
976 	else {
977 		prefix = target->ia_addr.sin_addr;
978 		mask = target->ia_sockmask.sin_addr;
979 		prefix.s_addr &= mask.s_addr;
980 	}
981 
982 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
983 		if (rtinitflags(ia))
984 			p = ia->ia_dstaddr.sin_addr;
985 		else {
986 			p = ia->ia_addr.sin_addr;
987 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
988 		}
989 
990 		if (prefix.s_addr != p.s_addr)
991 			continue;
992 
993 		/*
994 		 * if we got a matching prefix route, move IFA_ROUTE to him
995 		 */
996 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
997 			rtinit(&target->ia_ifa, RTM_DELETE,
998 			    rtinitflags(target));
999 			target->ia_flags &= ~IFA_ROUTE;
1000 
1001 			error = rtinit(&ia->ia_ifa, RTM_ADD,
1002 			    rtinitflags(ia) | RTF_UP);
1003 			if (error == 0)
1004 				ia->ia_flags |= IFA_ROUTE;
1005 			return error;
1006 		}
1007 	}
1008 
1009 	/*
1010 	 * noone seem to have prefix route.  remove it.
1011 	 */
1012 	rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
1013 	target->ia_flags &= ~IFA_ROUTE;
1014 	return 0;
1015 }
1016 
1017 #undef rtinitflags
1018 
1019 /*
1020  * Return 1 if the address might be a local broadcast address.
1021  */
1022 int
1023 in_broadcast(struct in_addr in, struct ifnet *ifp)
1024 {
1025 	struct ifaddr *ifa;
1026 
1027 	if (in.s_addr == INADDR_BROADCAST ||
1028 	    in_nullhost(in))
1029 		return 1;
1030 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
1031 		return 0;
1032 	/*
1033 	 * Look through the list of addresses for a match
1034 	 * with a broadcast address.
1035 	 */
1036 #define ia (ifatoia(ifa))
1037 	IFADDR_FOREACH(ifa, ifp)
1038 		if (ifa->ifa_addr->sa_family == AF_INET &&
1039 		    !in_hosteq(in, ia->ia_addr.sin_addr) &&
1040 		    (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
1041 		     in_hosteq(in, ia->ia_netbroadcast) ||
1042 		     (hostzeroisbroadcast &&
1043 		      /*
1044 		       * Check for old-style (host 0) broadcast.
1045 		       */
1046 		      (in.s_addr == ia->ia_subnet ||
1047 		       in.s_addr == ia->ia_net))))
1048 			return 1;
1049 	return (0);
1050 #undef ia
1051 }
1052 
1053 /*
1054  * Add an address to the list of IP multicast addresses for a given interface.
1055  */
1056 struct in_multi *
1057 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1058 {
1059 	struct sockaddr_in sin;
1060 	struct in_multi *inm;
1061 	struct ifreq ifr;
1062 	int s = splsoftnet();
1063 
1064 	/*
1065 	 * See if address already in list.
1066 	 */
1067 	IN_LOOKUP_MULTI(*ap, ifp, inm);
1068 	if (inm != NULL) {
1069 		/*
1070 		 * Found it; just increment the reference count.
1071 		 */
1072 		++inm->inm_refcount;
1073 	} else {
1074 		/*
1075 		 * New address; allocate a new multicast record
1076 		 * and link it into the interface's multicast list.
1077 		 */
1078 		inm = pool_get(&inmulti_pool, PR_NOWAIT);
1079 		if (inm == NULL) {
1080 			splx(s);
1081 			return (NULL);
1082 		}
1083 		inm->inm_addr = *ap;
1084 		inm->inm_ifp = ifp;
1085 		inm->inm_refcount = 1;
1086 		LIST_INSERT_HEAD(
1087 		    &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp),
1088 		    inm, inm_list);
1089 		/*
1090 		 * Ask the network driver to update its multicast reception
1091 		 * filter appropriately for the new address.
1092 		 */
1093 		sockaddr_in_init(&sin, ap, 0);
1094 		ifreq_setaddr(SIOCADDMULTI, &ifr, sintosa(&sin));
1095 		if ((*ifp->if_ioctl)(ifp, SIOCADDMULTI, &ifr) != 0) {
1096 			LIST_REMOVE(inm, inm_list);
1097 			pool_put(&inmulti_pool, inm);
1098 			splx(s);
1099 			return (NULL);
1100 		}
1101 		/*
1102 		 * Let IGMP know that we have joined a new IP multicast group.
1103 		 */
1104 		if (igmp_joingroup(inm) != 0) {
1105 			LIST_REMOVE(inm, inm_list);
1106 			pool_put(&inmulti_pool, inm);
1107 			splx(s);
1108 			return (NULL);
1109 		}
1110 		in_multientries++;
1111 	}
1112 	splx(s);
1113 	return (inm);
1114 }
1115 
1116 /*
1117  * Delete a multicast address record.
1118  */
1119 void
1120 in_delmulti(struct in_multi *inm)
1121 {
1122 	struct sockaddr_in sin;
1123 	struct ifreq ifr;
1124 	int s = splsoftnet();
1125 
1126 	if (--inm->inm_refcount == 0) {
1127 		/*
1128 		 * No remaining claims to this record; let IGMP know that
1129 		 * we are leaving the multicast group.
1130 		 */
1131 		igmp_leavegroup(inm);
1132 		/*
1133 		 * Unlink from list.
1134 		 */
1135 		LIST_REMOVE(inm, inm_list);
1136 		in_multientries--;
1137 		/*
1138 		 * Notify the network driver to update its multicast reception
1139 		 * filter.
1140 		 */
1141 		sockaddr_in_init(&sin, &inm->inm_addr, 0);
1142 		ifreq_setaddr(SIOCDELMULTI, &ifr, sintosa(&sin));
1143 		(*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI, &ifr);
1144 		pool_put(&inmulti_pool, inm);
1145 	}
1146 	splx(s);
1147 }
1148