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