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