xref: /netbsd-src/sys/netinet/in.c (revision a5847cc334d9a7029f6352b847e9e8d71a0f9e0c)
1 /*	$NetBSD: in.c,v 1.140 2011/10/28 22:23:54 dyoung 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.140 2011/10/28 22:23:54 dyoung 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 (cmd == SIOCDIFADDR &&
356 		    ifra->ifra_addr.sin_family == AF_UNSPEC) {
357 			ifra->ifra_addr.sin_family = AF_INET;
358 		}
359 		/* FALLTHROUGH */
360 	case SIOCSIFADDR:
361 	case SIOCSIFDSTADDR:
362 		if (ifra->ifra_addr.sin_family != AF_INET)
363 			return (EAFNOSUPPORT);
364 		/* FALLTHROUGH */
365 	case SIOCSIFNETMASK:
366 		if (ifp == NULL)
367 			panic("in_control");
368 
369 		if (cmd == SIOCGIFALIAS)
370 			break;
371 
372 		if (ia == NULL &&
373 		    (cmd == SIOCSIFNETMASK || cmd == SIOCSIFDSTADDR))
374 			return (EADDRNOTAVAIL);
375 
376 		if (l == NULL)
377 			return (EPERM);
378 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
379 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
380 		    NULL) != 0)
381 			return (EPERM);
382 
383 		if (ia == NULL) {
384 			ia = malloc(sizeof(*ia), M_IFADDR, M_WAITOK|M_ZERO);
385 			if (ia == NULL)
386 				return (ENOBUFS);
387 			TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_list);
388 			IFAREF(&ia->ia_ifa);
389 			ifa_insert(ifp, &ia->ia_ifa);
390 			ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
391 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
392 			ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
393 #ifdef IPSELSRC
394 			ia->ia_ifa.ifa_getifa = in_getifa;
395 #else /* IPSELSRC */
396 			ia->ia_ifa.ifa_getifa = NULL;
397 #endif /* IPSELSRC */
398 			ia->ia_sockmask.sin_len = 8;
399 			if (ifp->if_flags & IFF_BROADCAST) {
400 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
401 				ia->ia_broadaddr.sin_family = AF_INET;
402 			}
403 			ia->ia_ifp = ifp;
404 			ia->ia_idsalt = arc4random() % 65535;
405 			LIST_INIT(&ia->ia_multiaddrs);
406 			newifaddr = 1;
407 		}
408 		break;
409 
410 	case SIOCSIFBRDADDR:
411 		if (l == NULL)
412 			return (EPERM);
413 		if (kauth_authorize_network(l->l_cred, KAUTH_NETWORK_INTERFACE,
414 		    KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
415 		    NULL) != 0)
416 			return (EPERM);
417 		/* FALLTHROUGH */
418 
419 	case SIOCGIFADDR:
420 	case SIOCGIFNETMASK:
421 	case SIOCGIFDSTADDR:
422 	case SIOCGIFBRDADDR:
423 		if (ia == NULL)
424 			return (EADDRNOTAVAIL);
425 		break;
426 	}
427 	error = 0;
428 	switch (cmd) {
429 
430 	case SIOCGIFADDR:
431 		ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_addr));
432 		break;
433 
434 	case SIOCGIFBRDADDR:
435 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
436 			return (EINVAL);
437 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_broadaddr));
438 		break;
439 
440 	case SIOCGIFDSTADDR:
441 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
442 			return (EINVAL);
443 		ifreq_setdstaddr(cmd, ifr, sintocsa(&ia->ia_dstaddr));
444 		break;
445 
446 	case SIOCGIFNETMASK:
447 		ifreq_setaddr(cmd, ifr, sintocsa(&ia->ia_sockmask));
448 		break;
449 
450 	case SIOCSIFDSTADDR:
451 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
452 			return (EINVAL);
453 		oldaddr = ia->ia_dstaddr;
454 		ia->ia_dstaddr = *satocsin(ifreq_getdstaddr(cmd, ifr));
455 		if ((error = if_addr_init(ifp, &ia->ia_ifa, false)) != 0) {
456 			ia->ia_dstaddr = oldaddr;
457 			return error;
458 		}
459 		if (ia->ia_flags & IFA_ROUTE) {
460 			ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
461 			rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
462 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
463 			rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST|RTF_UP);
464 		}
465 		break;
466 
467 	case SIOCSIFBRDADDR:
468 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
469 			return EINVAL;
470 		ia->ia_broadaddr = *satocsin(ifreq_getbroadaddr(cmd, ifr));
471 		break;
472 
473 	case SIOCSIFADDR:
474 		error = in_ifinit(ifp, ia, satocsin(ifreq_getaddr(cmd, ifr)),
475 		    1);
476 #ifdef PFIL_HOOKS
477 		if (error == 0)
478 			(void)pfil_run_hooks(&if_pfil,
479 			    (struct mbuf **)SIOCSIFADDR, ifp, PFIL_IFADDR);
480 #endif
481 		break;
482 
483 	case SIOCSIFNETMASK:
484 		in_ifscrub(ifp, ia);
485 		ia->ia_sockmask = *satocsin(ifreq_getaddr(cmd, ifr));
486 		ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
487 		error = in_ifinit(ifp, ia, NULL, 0);
488 		break;
489 
490 	case SIOCAIFADDR:
491 		maskIsNew = 0;
492 		hostIsNew = 1;
493 		if (ia->ia_addr.sin_family != AF_INET)
494 			;
495 		else if (ifra->ifra_addr.sin_len == 0) {
496 			ifra->ifra_addr = ia->ia_addr;
497 			hostIsNew = 0;
498 		} else if (in_hosteq(ia->ia_addr.sin_addr,
499 		           ifra->ifra_addr.sin_addr))
500 			hostIsNew = 0;
501 		if (ifra->ifra_mask.sin_len) {
502 			in_ifscrub(ifp, ia);
503 			ia->ia_sockmask = ifra->ifra_mask;
504 			ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
505 			maskIsNew = 1;
506 		}
507 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
508 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
509 			in_ifscrub(ifp, ia);
510 			ia->ia_dstaddr = ifra->ifra_dstaddr;
511 			maskIsNew  = 1; /* We lie; but the effect's the same */
512 		}
513 		if (ifra->ifra_addr.sin_family == AF_INET &&
514 		    (hostIsNew || maskIsNew)) {
515 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
516 		}
517 		if ((ifp->if_flags & IFF_BROADCAST) &&
518 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
519 			ia->ia_broadaddr = ifra->ifra_broadaddr;
520 #ifdef PFIL_HOOKS
521 		if (error == 0)
522 			(void)pfil_run_hooks(&if_pfil,
523 			    (struct mbuf **)SIOCAIFADDR, ifp, PFIL_IFADDR);
524 #endif
525 		break;
526 
527 	case SIOCGIFALIAS:
528 		ifra->ifra_mask = ia->ia_sockmask;
529 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
530 		    (ia->ia_dstaddr.sin_family == AF_INET))
531 			ifra->ifra_dstaddr = ia->ia_dstaddr;
532 		else if ((ifp->if_flags & IFF_BROADCAST) &&
533 		    (ia->ia_broadaddr.sin_family == AF_INET))
534 			ifra->ifra_broadaddr = ia->ia_broadaddr;
535 		else
536 			memset(&ifra->ifra_broadaddr, 0,
537 			      sizeof(ifra->ifra_broadaddr));
538 		break;
539 
540 	case SIOCDIFADDR:
541 		in_purgeaddr(&ia->ia_ifa);
542 #ifdef PFIL_HOOKS
543 		(void)pfil_run_hooks(&if_pfil, (struct mbuf **)SIOCDIFADDR,
544 		    ifp, PFIL_IFADDR);
545 #endif
546 		break;
547 
548 #ifdef MROUTING
549 	case SIOCGETVIFCNT:
550 	case SIOCGETSGCNT:
551 		error = mrt_ioctl(so, cmd, data);
552 		break;
553 #endif /* MROUTING */
554 
555 	default:
556 		return ENOTTY;
557 	}
558 
559 	if (error != 0 && newifaddr) {
560 		KASSERT(ia != NULL);
561 		in_purgeaddr(&ia->ia_ifa);
562 	}
563 
564 	return error;
565 }
566 
567 void
568 in_purgeaddr(struct ifaddr *ifa)
569 {
570 	struct ifnet *ifp = ifa->ifa_ifp;
571 	struct in_ifaddr *ia = (void *) ifa;
572 
573 	in_ifscrub(ifp, ia);
574 	LIST_REMOVE(ia, ia_hash);
575 	ifa_remove(ifp, &ia->ia_ifa);
576 	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_list);
577 	if (ia->ia_allhosts != NULL)
578 		in_delmulti(ia->ia_allhosts);
579 	IFAFREE(&ia->ia_ifa);
580 	in_setmaxmtu();
581 }
582 
583 void
584 in_purgeif(struct ifnet *ifp)		/* MUST be called at splsoftnet() */
585 {
586 	if_purgeaddrs(ifp, AF_INET, in_purgeaddr);
587 	igmp_purgeif(ifp);		/* manipulates pools */
588 #ifdef MROUTING
589 	ip_mrouter_detach(ifp);
590 #endif
591 }
592 
593 /*
594  * SIOC[GAD]LIFADDR.
595  *	SIOCGLIFADDR: get first address. (???)
596  *	SIOCGLIFADDR with IFLR_PREFIX:
597  *		get first address that matches the specified prefix.
598  *	SIOCALIFADDR: add the specified address.
599  *	SIOCALIFADDR with IFLR_PREFIX:
600  *		EINVAL since we can't deduce hostid part of the address.
601  *	SIOCDLIFADDR: delete the specified address.
602  *	SIOCDLIFADDR with IFLR_PREFIX:
603  *		delete the first address that matches the specified prefix.
604  * return values:
605  *	EINVAL on invalid parameters
606  *	EADDRNOTAVAIL on prefix match failed/specified address not found
607  *	other values may be returned from in_ioctl()
608  */
609 static int
610 in_lifaddr_ioctl(struct socket *so, u_long cmd, void *data,
611     struct ifnet *ifp, struct lwp *l)
612 {
613 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
614 	struct ifaddr *ifa;
615 	struct sockaddr *sa;
616 
617 	/* sanity checks */
618 	if (data == NULL || ifp == NULL) {
619 		panic("invalid argument to in_lifaddr_ioctl");
620 		/*NOTRECHED*/
621 	}
622 
623 	switch (cmd) {
624 	case SIOCGLIFADDR:
625 		/* address must be specified on GET with IFLR_PREFIX */
626 		if ((iflr->flags & IFLR_PREFIX) == 0)
627 			break;
628 		/*FALLTHROUGH*/
629 	case SIOCALIFADDR:
630 	case SIOCDLIFADDR:
631 		/* address must be specified on ADD and DELETE */
632 		sa = (struct sockaddr *)&iflr->addr;
633 		if (sa->sa_family != AF_INET)
634 			return EINVAL;
635 		if (sa->sa_len != sizeof(struct sockaddr_in))
636 			return EINVAL;
637 		/* XXX need improvement */
638 		sa = (struct sockaddr *)&iflr->dstaddr;
639 		if (sa->sa_family != AF_UNSPEC && sa->sa_family != AF_INET)
640 			return EINVAL;
641 		if (sa->sa_len != 0 && sa->sa_len != sizeof(struct sockaddr_in))
642 			return EINVAL;
643 		break;
644 	default: /*shouldn't happen*/
645 #if 0
646 		panic("invalid cmd to in_lifaddr_ioctl");
647 		/*NOTREACHED*/
648 #else
649 		return EOPNOTSUPP;
650 #endif
651 	}
652 	if (sizeof(struct in_addr) * NBBY < iflr->prefixlen)
653 		return EINVAL;
654 
655 	switch (cmd) {
656 	case SIOCALIFADDR:
657 	    {
658 		struct in_aliasreq ifra;
659 
660 		if (iflr->flags & IFLR_PREFIX)
661 			return EINVAL;
662 
663 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
664 		memset(&ifra, 0, sizeof(ifra));
665 		memcpy(ifra.ifra_name, iflr->iflr_name,
666 			sizeof(ifra.ifra_name));
667 
668 		memcpy(&ifra.ifra_addr, &iflr->addr,
669 			((struct sockaddr *)&iflr->addr)->sa_len);
670 
671 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
672 			memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr,
673 				((struct sockaddr *)&iflr->dstaddr)->sa_len);
674 		}
675 
676 		ifra.ifra_mask.sin_family = AF_INET;
677 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
678 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
679 
680 		return in_control(so, SIOCAIFADDR, (void *)&ifra, ifp, l);
681 	    }
682 	case SIOCGLIFADDR:
683 	case SIOCDLIFADDR:
684 	    {
685 		struct in_ifaddr *ia;
686 		struct in_addr mask, candidate, match;
687 		struct sockaddr_in *sin;
688 		int cmp;
689 
690 		memset(&mask, 0, sizeof(mask));
691 		memset(&match, 0, sizeof(match));	/* XXX gcc */
692 		if (iflr->flags & IFLR_PREFIX) {
693 			/* lookup a prefix rather than address. */
694 			in_len2mask(&mask, iflr->prefixlen);
695 
696 			sin = (struct sockaddr_in *)&iflr->addr;
697 			match.s_addr = sin->sin_addr.s_addr;
698 			match.s_addr &= mask.s_addr;
699 
700 			/* if you set extra bits, that's wrong */
701 			if (match.s_addr != sin->sin_addr.s_addr)
702 				return EINVAL;
703 
704 			cmp = 1;
705 		} else {
706 			if (cmd == SIOCGLIFADDR) {
707 				/* on getting an address, take the 1st match */
708 				cmp = 0;	/*XXX*/
709 			} else {
710 				/* on deleting an address, do exact match */
711 				in_len2mask(&mask, 32);
712 				sin = (struct sockaddr_in *)&iflr->addr;
713 				match.s_addr = sin->sin_addr.s_addr;
714 
715 				cmp = 1;
716 			}
717 		}
718 
719 		IFADDR_FOREACH(ifa, ifp) {
720 			if (ifa->ifa_addr->sa_family != AF_INET)
721 				continue;
722 			if (cmp == 0)
723 				break;
724 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
725 			candidate.s_addr &= mask.s_addr;
726 			if (candidate.s_addr == match.s_addr)
727 				break;
728 		}
729 		if (ifa == NULL)
730 			return EADDRNOTAVAIL;
731 		ia = (struct in_ifaddr *)ifa;
732 
733 		if (cmd == SIOCGLIFADDR) {
734 			/* fill in the if_laddrreq structure */
735 			memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin_len);
736 
737 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
738 				memcpy(&iflr->dstaddr, &ia->ia_dstaddr,
739 					ia->ia_dstaddr.sin_len);
740 			} else
741 				memset(&iflr->dstaddr, 0, sizeof(iflr->dstaddr));
742 
743 			iflr->prefixlen =
744 				in_mask2len(&ia->ia_sockmask.sin_addr);
745 
746 			iflr->flags = 0;	/*XXX*/
747 
748 			return 0;
749 		} else {
750 			struct in_aliasreq ifra;
751 
752 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
753 			memset(&ifra, 0, sizeof(ifra));
754 			memcpy(ifra.ifra_name, iflr->iflr_name,
755 				sizeof(ifra.ifra_name));
756 
757 			memcpy(&ifra.ifra_addr, &ia->ia_addr,
758 				ia->ia_addr.sin_len);
759 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
760 				memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr,
761 					ia->ia_dstaddr.sin_len);
762 			}
763 			memcpy(&ifra.ifra_dstaddr, &ia->ia_sockmask,
764 				ia->ia_sockmask.sin_len);
765 
766 			return in_control(so, SIOCDIFADDR, (void *)&ifra,
767 				ifp, l);
768 		}
769 	    }
770 	}
771 
772 	return EOPNOTSUPP;	/*just for safety*/
773 }
774 
775 /*
776  * Delete any existing route for an interface.
777  */
778 void
779 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
780 {
781 
782 	in_scrubprefix(ia);
783 }
784 
785 /*
786  * Initialize an interface's internet address
787  * and routing table entry.
788  */
789 int
790 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
791     const struct sockaddr_in *sin, int scrub)
792 {
793 	u_int32_t i;
794 	struct sockaddr_in oldaddr;
795 	int s = splnet(), flags = RTF_UP, error;
796 
797 	if (sin == NULL)
798 		sin = &ia->ia_addr;
799 
800 	/*
801 	 * Set up new addresses.
802 	 */
803 	oldaddr = ia->ia_addr;
804 	if (ia->ia_addr.sin_family == AF_INET)
805 		LIST_REMOVE(ia, ia_hash);
806 	ia->ia_addr = *sin;
807 	LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
808 
809 	/*
810 	 * Give the interface a chance to initialize
811 	 * if this is its first address,
812 	 * and to validate the address if necessary.
813 	 */
814 	if ((error = if_addr_init(ifp, &ia->ia_ifa, true)) != 0)
815 		goto bad;
816 	splx(s);
817 	if (scrub) {
818 		ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
819 		in_ifscrub(ifp, ia);
820 		ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
821 	}
822 
823 	i = ia->ia_addr.sin_addr.s_addr;
824 	if (IN_CLASSA(i))
825 		ia->ia_netmask = IN_CLASSA_NET;
826 	else if (IN_CLASSB(i))
827 		ia->ia_netmask = IN_CLASSB_NET;
828 	else
829 		ia->ia_netmask = IN_CLASSC_NET;
830 	/*
831 	 * The subnet mask usually includes at least the standard network part,
832 	 * but may may be smaller in the case of supernetting.
833 	 * If it is set, we believe it.
834 	 */
835 	if (ia->ia_subnetmask == 0) {
836 		ia->ia_subnetmask = ia->ia_netmask;
837 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
838 	} else
839 		ia->ia_netmask &= ia->ia_subnetmask;
840 
841 	ia->ia_net = i & ia->ia_netmask;
842 	ia->ia_subnet = i & ia->ia_subnetmask;
843 	in_socktrim(&ia->ia_sockmask);
844 	/* re-calculate the "in_maxmtu" value */
845 	in_setmaxmtu();
846 	/*
847 	 * Add route for the network.
848 	 */
849 	ia->ia_ifa.ifa_metric = ifp->if_metric;
850 	if (ifp->if_flags & IFF_BROADCAST) {
851 		ia->ia_broadaddr.sin_addr.s_addr =
852 			ia->ia_subnet | ~ia->ia_subnetmask;
853 		ia->ia_netbroadcast.s_addr =
854 			ia->ia_net | ~ia->ia_netmask;
855 	} else if (ifp->if_flags & IFF_LOOPBACK) {
856 		ia->ia_dstaddr = ia->ia_addr;
857 		flags |= RTF_HOST;
858 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
859 		if (ia->ia_dstaddr.sin_family != AF_INET)
860 			return (0);
861 		flags |= RTF_HOST;
862 	}
863 	error = in_addprefix(ia, flags);
864 	/*
865 	 * If the interface supports multicast, join the "all hosts"
866 	 * multicast group on that interface.
867 	 */
868 	if ((ifp->if_flags & IFF_MULTICAST) != 0 && ia->ia_allhosts == NULL) {
869 		struct in_addr addr;
870 
871 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
872 		ia->ia_allhosts = in_addmulti(&addr, ifp);
873 	}
874 	return (error);
875 bad:
876 	splx(s);
877 	LIST_REMOVE(ia, ia_hash);
878 	ia->ia_addr = oldaddr;
879 	if (ia->ia_addr.sin_family == AF_INET)
880 		LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
881 		    ia, ia_hash);
882 	return (error);
883 }
884 
885 #define rtinitflags(x) \
886 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
887 	    ? RTF_HOST : 0)
888 
889 /*
890  * add a route to prefix ("connected route" in cisco terminology).
891  * does nothing if there's some interface address with the same prefix already.
892  */
893 static int
894 in_addprefix(struct in_ifaddr *target, int flags)
895 {
896 	struct in_ifaddr *ia;
897 	struct in_addr prefix, mask, p;
898 	int error;
899 
900 	if ((flags & RTF_HOST) != 0)
901 		prefix = target->ia_dstaddr.sin_addr;
902 	else {
903 		prefix = target->ia_addr.sin_addr;
904 		mask = target->ia_sockmask.sin_addr;
905 		prefix.s_addr &= mask.s_addr;
906 	}
907 
908 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
909 		if (rtinitflags(ia))
910 			p = ia->ia_dstaddr.sin_addr;
911 		else {
912 			p = ia->ia_addr.sin_addr;
913 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
914 		}
915 
916 		if (prefix.s_addr != p.s_addr)
917 			continue;
918 
919 		/*
920 		 * if we got a matching prefix route inserted by other
921 		 * interface address, we don't need to bother
922 		 *
923 		 * XXX RADIX_MPATH implications here? -dyoung
924 		 */
925 		if (ia->ia_flags & IFA_ROUTE)
926 			return 0;
927 	}
928 
929 	/*
930 	 * noone seem to have prefix route.  insert it.
931 	 */
932 	error = rtinit(&target->ia_ifa, RTM_ADD, flags);
933 	if (error == 0)
934 		target->ia_flags |= IFA_ROUTE;
935 	else if (error == EEXIST) {
936 		/*
937 		 * the fact the route already exists is not an error.
938 		 */
939 		error = 0;
940 	}
941 	return error;
942 }
943 
944 /*
945  * remove a route to prefix ("connected route" in cisco terminology).
946  * re-installs the route by using another interface address, if there's one
947  * with the same prefix (otherwise we lose the route mistakenly).
948  */
949 static int
950 in_scrubprefix(struct in_ifaddr *target)
951 {
952 	struct in_ifaddr *ia;
953 	struct in_addr prefix, mask, p;
954 	int error;
955 
956 	if ((target->ia_flags & IFA_ROUTE) == 0)
957 		return 0;
958 
959 	if (rtinitflags(target))
960 		prefix = target->ia_dstaddr.sin_addr;
961 	else {
962 		prefix = target->ia_addr.sin_addr;
963 		mask = target->ia_sockmask.sin_addr;
964 		prefix.s_addr &= mask.s_addr;
965 	}
966 
967 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_list) {
968 		if (rtinitflags(ia))
969 			p = ia->ia_dstaddr.sin_addr;
970 		else {
971 			p = ia->ia_addr.sin_addr;
972 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
973 		}
974 
975 		if (prefix.s_addr != p.s_addr)
976 			continue;
977 
978 		/*
979 		 * if we got a matching prefix route, move IFA_ROUTE to him
980 		 */
981 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
982 			rtinit(&target->ia_ifa, RTM_DELETE,
983 			    rtinitflags(target));
984 			target->ia_flags &= ~IFA_ROUTE;
985 
986 			error = rtinit(&ia->ia_ifa, RTM_ADD,
987 			    rtinitflags(ia) | RTF_UP);
988 			if (error == 0)
989 				ia->ia_flags |= IFA_ROUTE;
990 			return error;
991 		}
992 	}
993 
994 	/*
995 	 * noone seem to have prefix route.  remove it.
996 	 */
997 	rtinit(&target->ia_ifa, RTM_DELETE, rtinitflags(target));
998 	target->ia_flags &= ~IFA_ROUTE;
999 	return 0;
1000 }
1001 
1002 #undef rtinitflags
1003 
1004 /*
1005  * Return 1 if the address might be a local broadcast address.
1006  */
1007 int
1008 in_broadcast(struct in_addr in, struct ifnet *ifp)
1009 {
1010 	struct ifaddr *ifa;
1011 
1012 	if (in.s_addr == INADDR_BROADCAST ||
1013 	    in_nullhost(in))
1014 		return 1;
1015 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
1016 		return 0;
1017 	/*
1018 	 * Look through the list of addresses for a match
1019 	 * with a broadcast address.
1020 	 */
1021 #define ia (ifatoia(ifa))
1022 	IFADDR_FOREACH(ifa, ifp)
1023 		if (ifa->ifa_addr->sa_family == AF_INET &&
1024 		    !in_hosteq(in, ia->ia_addr.sin_addr) &&
1025 		    (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
1026 		     in_hosteq(in, ia->ia_netbroadcast) ||
1027 		     (hostzeroisbroadcast &&
1028 		      /*
1029 		       * Check for old-style (host 0) broadcast.
1030 		       */
1031 		      (in.s_addr == ia->ia_subnet ||
1032 		       in.s_addr == ia->ia_net))))
1033 			return 1;
1034 	return (0);
1035 #undef ia
1036 }
1037 
1038 /*
1039  * Add an address to the list of IP multicast addresses for a given interface.
1040  */
1041 struct in_multi *
1042 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1043 {
1044 	struct sockaddr_in sin;
1045 	struct in_multi *inm;
1046 	int s = splsoftnet();
1047 
1048 	/*
1049 	 * See if address already in list.
1050 	 */
1051 	IN_LOOKUP_MULTI(*ap, ifp, inm);
1052 	if (inm != NULL) {
1053 		/*
1054 		 * Found it; just increment the reference count.
1055 		 */
1056 		++inm->inm_refcount;
1057 	} else {
1058 		/*
1059 		 * New address; allocate a new multicast record
1060 		 * and link it into the interface's multicast list.
1061 		 */
1062 		inm = pool_get(&inmulti_pool, PR_NOWAIT);
1063 		if (inm == NULL) {
1064 			splx(s);
1065 			return (NULL);
1066 		}
1067 		inm->inm_addr = *ap;
1068 		inm->inm_ifp = ifp;
1069 		inm->inm_refcount = 1;
1070 		LIST_INSERT_HEAD(
1071 		    &IN_MULTI_HASH(inm->inm_addr.s_addr, ifp),
1072 		    inm, inm_list);
1073 		/*
1074 		 * Ask the network driver to update its multicast reception
1075 		 * filter appropriately for the new address.
1076 		 */
1077 		sockaddr_in_init(&sin, ap, 0);
1078 		if (if_mcast_op(ifp, SIOCADDMULTI, sintosa(&sin)) != 0) {
1079 			LIST_REMOVE(inm, inm_list);
1080 			pool_put(&inmulti_pool, inm);
1081 			splx(s);
1082 			return (NULL);
1083 		}
1084 		/*
1085 		 * Let IGMP know that we have joined a new IP multicast group.
1086 		 */
1087 		if (igmp_joingroup(inm) != 0) {
1088 			LIST_REMOVE(inm, inm_list);
1089 			pool_put(&inmulti_pool, inm);
1090 			splx(s);
1091 			return (NULL);
1092 		}
1093 		in_multientries++;
1094 	}
1095 	splx(s);
1096 	return (inm);
1097 }
1098 
1099 /*
1100  * Delete a multicast address record.
1101  */
1102 void
1103 in_delmulti(struct in_multi *inm)
1104 {
1105 	struct sockaddr_in sin;
1106 	int s = splsoftnet();
1107 
1108 	if (--inm->inm_refcount == 0) {
1109 		/*
1110 		 * No remaining claims to this record; let IGMP know that
1111 		 * we are leaving the multicast group.
1112 		 */
1113 		igmp_leavegroup(inm);
1114 		/*
1115 		 * Unlink from list.
1116 		 */
1117 		LIST_REMOVE(inm, inm_list);
1118 		in_multientries--;
1119 		/*
1120 		 * Notify the network driver to update its multicast reception
1121 		 * filter.
1122 		 */
1123 		sockaddr_in_init(&sin, &inm->inm_addr, 0);
1124 		if_mcast_op(inm->inm_ifp, SIOCDELMULTI, sintosa(&sin));
1125 		pool_put(&inmulti_pool, inm);
1126 	}
1127 	splx(s);
1128 }
1129