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