xref: /openbsd-src/sys/netinet/in.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: in.c,v 1.129 2016/09/04 10:32:01 mpi 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 <netinet/in.h>
75 #include <netinet/in_var.h>
76 #include <netinet/igmp_var.h>
77 
78 #ifdef MROUTING
79 #include <netinet/ip_mroute.h>
80 #endif
81 
82 #include "ether.h"
83 
84 
85 void in_socktrim(struct sockaddr_in *);
86 int in_lifaddr_ioctl(u_long, caddr_t, struct ifnet *, int);
87 int in_ioctl(u_long, caddr_t, struct ifnet *, int);
88 
89 void in_purgeaddr(struct ifaddr *);
90 int in_addhost(struct in_ifaddr *, struct sockaddr_in *);
91 int in_scrubhost(struct in_ifaddr *, struct sockaddr_in *);
92 int in_insert_prefix(struct in_ifaddr *);
93 void in_remove_prefix(struct in_ifaddr *);
94 
95 /*
96  * Determine whether an IP address is in a reserved set of addresses
97  * that may not be forwarded, or whether datagrams to that destination
98  * may be forwarded.
99  */
100 int
101 in_canforward(struct in_addr in)
102 {
103 	u_int32_t net;
104 
105 	if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
106 		return (0);
107 	if (IN_CLASSA(in.s_addr)) {
108 		net = in.s_addr & IN_CLASSA_NET;
109 		if (net == 0 ||
110 		    net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
111 			return (0);
112 	}
113 	return (1);
114 }
115 
116 /*
117  * Trim a mask in a sockaddr
118  */
119 void
120 in_socktrim(struct sockaddr_in *ap)
121 {
122 	char *cplim = (char *) &ap->sin_addr;
123 	char *cp = (char *) (&ap->sin_addr + 1);
124 
125 	ap->sin_len = 0;
126 	while (--cp >= cplim)
127 		if (*cp) {
128 			(ap)->sin_len = cp - (char *) (ap) + 1;
129 			break;
130 		}
131 }
132 
133 int
134 in_mask2len(struct in_addr *mask)
135 {
136 	int x, y;
137 	u_char *p;
138 
139 	p = (u_char *)mask;
140 	for (x = 0; x < sizeof(*mask); x++) {
141 		if (p[x] != 0xff)
142 			break;
143 	}
144 	y = 0;
145 	if (x < sizeof(*mask)) {
146 		for (y = 0; y < 8; y++) {
147 			if ((p[x] & (0x80 >> y)) == 0)
148 				break;
149 		}
150 	}
151 	return x * 8 + y;
152 }
153 
154 void
155 in_len2mask(struct in_addr *mask, int len)
156 {
157 	int i;
158 	u_char *p;
159 
160 	p = (u_char *)mask;
161 	bzero(mask, sizeof(*mask));
162 	for (i = 0; i < len / 8; i++)
163 		p[i] = 0xff;
164 	if (len % 8)
165 		p[i] = (0xff00 >> (len % 8)) & 0xff;
166 }
167 
168 /*
169  * Generic internet control operations (ioctl's).
170  */
171 int
172 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp)
173 {
174 	int privileged;
175 
176 	privileged = 0;
177 	if ((so->so_state & SS_PRIV) != 0)
178 		privileged++;
179 
180 	switch (cmd) {
181 #ifdef MROUTING
182 	case SIOCGETVIFCNT:
183 	case SIOCGETSGCNT:
184 		return (mrt_ioctl(so, cmd, data));
185 #endif /* MROUTING */
186 	case SIOCALIFADDR:
187 	case SIOCDLIFADDR:
188 		if (!privileged)
189 			return (EPERM);
190 		/* FALLTHROUGH */
191 	case SIOCGLIFADDR:
192 		if (ifp == NULL)
193 			return (EINVAL);
194 		return in_lifaddr_ioctl(cmd, data, ifp, privileged);
195 	default:
196 		if (ifp == NULL)
197 			return (EOPNOTSUPP);
198 	}
199 
200 	return (in_ioctl(cmd, data, ifp, privileged));
201 }
202 
203 int
204 in_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, int privileged)
205 {
206 	struct ifreq *ifr = (struct ifreq *)data;
207 	struct ifaddr *ifa;
208 	struct in_ifaddr *ia = NULL;
209 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
210 	struct sockaddr_in oldaddr;
211 	int error;
212 	int newifaddr;
213 	int s;
214 
215 	TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
216 		if (ifa->ifa_addr->sa_family == AF_INET) {
217 			ia = ifatoia(ifa);
218 			break;
219 		}
220 	}
221 
222 	switch (cmd) {
223 
224 	case SIOCAIFADDR:
225 	case SIOCDIFADDR:
226 		if (ifra->ifra_addr.sin_family == AF_INET) {
227 			for (; ifa != NULL; ifa = TAILQ_NEXT(ifa, ifa_list)) {
228 				if ((ifa->ifa_addr->sa_family == AF_INET) &&
229 				    ifatoia(ifa)->ia_addr.sin_addr.s_addr ==
230 				    ifra->ifra_addr.sin_addr.s_addr)
231 					break;
232 			}
233 			ia = ifatoia(ifa);
234 		}
235 		if (cmd == SIOCDIFADDR && ia == NULL)
236 			return (EADDRNOTAVAIL);
237 		/* FALLTHROUGH */
238 	case SIOCSIFADDR:
239 		if (!privileged)
240 			return (EPERM);
241 
242 		if (ia == NULL) {
243 			ia = malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
244 			ia->ia_addr.sin_family = AF_INET;
245 			ia->ia_addr.sin_len = sizeof(ia->ia_addr);
246 			ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
247 			ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
248 			ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
249 			ia->ia_sockmask.sin_len = 8;
250 			if (ifp->if_flags & IFF_BROADCAST) {
251 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
252 				ia->ia_broadaddr.sin_family = AF_INET;
253 			}
254 			ia->ia_ifp = ifp;
255 
256 			newifaddr = 1;
257 		} else
258 			newifaddr = 0;
259 		break;
260 
261 	case SIOCSIFNETMASK:
262 	case SIOCSIFDSTADDR:
263 	case SIOCSIFBRDADDR:
264 		if (!privileged)
265 			return (EPERM);
266 		/* FALLTHROUGH */
267 
268 	case SIOCGIFADDR:
269 	case SIOCGIFNETMASK:
270 	case SIOCGIFDSTADDR:
271 	case SIOCGIFBRDADDR:
272 		if (ia && satosin(&ifr->ifr_addr)->sin_addr.s_addr) {
273 			for (; ifa != NULL; ifa = TAILQ_NEXT(ifa, ifa_list)) {
274 				if ((ifa->ifa_addr->sa_family == AF_INET) &&
275 				    ifatoia(ifa)->ia_addr.sin_addr.s_addr ==
276 				    satosin(&ifr->ifr_addr)->sin_addr.s_addr) {
277 					ia = ifatoia(ifa);
278 					break;
279 				}
280 			}
281 		}
282 		if (ia == NULL)
283 			return (EADDRNOTAVAIL);
284 		break;
285 	}
286 	switch (cmd) {
287 
288 	case SIOCGIFADDR:
289 		*satosin(&ifr->ifr_addr) = ia->ia_addr;
290 		break;
291 
292 	case SIOCGIFBRDADDR:
293 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
294 			return (EINVAL);
295 		*satosin(&ifr->ifr_dstaddr) = ia->ia_broadaddr;
296 		break;
297 
298 	case SIOCGIFDSTADDR:
299 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
300 			return (EINVAL);
301 		*satosin(&ifr->ifr_dstaddr) = ia->ia_dstaddr;
302 		break;
303 
304 	case SIOCGIFNETMASK:
305 		*satosin(&ifr->ifr_addr) = ia->ia_sockmask;
306 		break;
307 
308 	case SIOCSIFDSTADDR:
309 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
310 			return (EINVAL);
311 		s = splsoftnet();
312 		oldaddr = ia->ia_dstaddr;
313 		ia->ia_dstaddr = *satosin(&ifr->ifr_dstaddr);
314 		if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
315 					(ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
316 			ia->ia_dstaddr = oldaddr;
317 			splx(s);
318 			return (error);
319 		}
320 		in_scrubhost(ia, &oldaddr);
321 		in_addhost(ia, &ia->ia_dstaddr);
322 		splx(s);
323 		break;
324 
325 	case SIOCSIFBRDADDR:
326 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
327 			return (EINVAL);
328 		ifa_update_broadaddr(ifp, &ia->ia_ifa, &ifr->ifr_broadaddr);
329 		break;
330 
331 	case SIOCSIFADDR:
332 		s = splsoftnet();
333 		in_ifscrub(ifp, ia);
334 		error = in_ifinit(ifp, ia, satosin(&ifr->ifr_addr), newifaddr);
335 		if (!error)
336 			dohooks(ifp->if_addrhooks, 0);
337 		splx(s);
338 		return (error);
339 
340 	case SIOCSIFNETMASK:
341 		ia->ia_netmask = ia->ia_sockmask.sin_addr.s_addr =
342 		    ifra->ifra_addr.sin_addr.s_addr;
343 		break;
344 
345 	case SIOCAIFADDR: {
346 		int needinit = 0;
347 
348 		error = 0;
349 
350 		s = splsoftnet();
351 		if (ia->ia_addr.sin_family == AF_INET) {
352 			if (ifra->ifra_addr.sin_len == 0)
353 				ifra->ifra_addr = ia->ia_addr;
354 			else if (ifra->ifra_addr.sin_addr.s_addr !=
355 			    ia->ia_addr.sin_addr.s_addr || newifaddr)
356 				needinit = 1;
357 		}
358 		if (ifra->ifra_mask.sin_len) {
359 			in_ifscrub(ifp, ia);
360 			ia->ia_sockmask = ifra->ifra_mask;
361 			ia->ia_netmask = ia->ia_sockmask.sin_addr.s_addr;
362 			needinit = 1;
363 		}
364 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
365 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
366 			in_ifscrub(ifp, ia);
367 			ia->ia_dstaddr = ifra->ifra_dstaddr;
368 			needinit  = 1;
369 		}
370 		if ((ifp->if_flags & IFF_BROADCAST) &&
371 		    (ifra->ifra_broadaddr.sin_family == AF_INET)) {
372 			if (newifaddr)
373 				ia->ia_broadaddr = ifra->ifra_broadaddr;
374 			else
375 				ifa_update_broadaddr(ifp, &ia->ia_ifa,
376 				    sintosa(&ifra->ifra_broadaddr));
377 		}
378 		if (ifra->ifra_addr.sin_family == AF_INET && needinit) {
379 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, newifaddr);
380 		}
381 		if (!error)
382 			dohooks(ifp->if_addrhooks, 0);
383 		splx(s);
384 		return (error);
385 		}
386 	case SIOCDIFADDR:
387 		/*
388 		 * Even if the individual steps were safe, shouldn't
389 		 * these kinds of changes happen atomically?  What
390 		 * should happen to a packet that was routed after
391 		 * the scrub but before the other steps?
392 		 */
393 		s = splsoftnet();
394 		in_purgeaddr(&ia->ia_ifa);
395 		dohooks(ifp->if_addrhooks, 0);
396 		splx(s);
397 		break;
398 
399 	default:
400 		if (ifp->if_ioctl == NULL)
401 			return (EOPNOTSUPP);
402 		return ((*ifp->if_ioctl)(ifp, cmd, data));
403 	}
404 	return (0);
405 }
406 
407 /*
408  * SIOC[GAD]LIFADDR.
409  *	SIOCGLIFADDR: get first address. (???)
410  *	SIOCGLIFADDR with IFLR_PREFIX:
411  *		get first address that matches the specified prefix.
412  *	SIOCALIFADDR: add the specified address.
413  *	SIOCALIFADDR with IFLR_PREFIX:
414  *		EINVAL since we can't deduce hostid part of the address.
415  *	SIOCDLIFADDR: delete the specified address.
416  *	SIOCDLIFADDR with IFLR_PREFIX:
417  *		delete the first address that matches the specified prefix.
418  * return values:
419  *	EINVAL on invalid parameters
420  *	EADDRNOTAVAIL on prefix match failed/specified address not found
421  *	other values may be returned from in_ioctl()
422  */
423 int
424 in_lifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, int privileged)
425 {
426 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
427 	struct ifaddr *ifa;
428 	struct sockaddr *sa;
429 
430 	/* sanity checks */
431 	if (!data || !ifp) {
432 		panic("invalid argument to in_lifaddr_ioctl");
433 		/*NOTRECHED*/
434 	}
435 
436 	switch (cmd) {
437 	case SIOCGLIFADDR:
438 		/* address must be specified on GET with IFLR_PREFIX */
439 		if ((iflr->flags & IFLR_PREFIX) == 0)
440 			break;
441 		/*FALLTHROUGH*/
442 	case SIOCALIFADDR:
443 	case SIOCDLIFADDR:
444 		/* address must be specified on ADD and DELETE */
445 		sa = (struct sockaddr *)&iflr->addr;
446 		if (sa->sa_family != AF_INET)
447 			return EINVAL;
448 		if (sa->sa_len != sizeof(struct sockaddr_in))
449 			return EINVAL;
450 		/* XXX need improvement */
451 		sa = (struct sockaddr *)&iflr->dstaddr;
452 		if (sa->sa_family
453 		 && sa->sa_family != AF_INET)
454 			return EINVAL;
455 		if (sa->sa_len && sa->sa_len != sizeof(struct sockaddr_in))
456 			return EINVAL;
457 		break;
458 	default: /*shouldn't happen*/
459 #if 0
460 		panic("invalid cmd to in_lifaddr_ioctl");
461 		/*NOTREACHED*/
462 #else
463 		return EOPNOTSUPP;
464 #endif
465 	}
466 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
467 		return EINVAL;
468 
469 	switch (cmd) {
470 	case SIOCALIFADDR:
471 	    {
472 		struct in_aliasreq ifra;
473 
474 		if (iflr->flags & IFLR_PREFIX)
475 			return EINVAL;
476 
477 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
478 		bzero(&ifra, sizeof(ifra));
479 		memcpy(ifra.ifra_name, iflr->iflr_name,
480 		    sizeof(ifra.ifra_name));
481 
482 		memcpy(&ifra.ifra_addr, &iflr->addr,
483 		    ((struct sockaddr *)&iflr->addr)->sa_len);
484 
485 		if (((struct sockaddr *)&iflr->dstaddr)->sa_family) {	/*XXX*/
486 			memcpy(&ifra.ifra_dstaddr, &iflr->dstaddr,
487 			    ((struct sockaddr *)&iflr->dstaddr)->sa_len);
488 		}
489 
490 		ifra.ifra_mask.sin_family = AF_INET;
491 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
492 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
493 
494 		return in_ioctl(SIOCAIFADDR, (caddr_t)&ifra, ifp, privileged);
495 	    }
496 	case SIOCGLIFADDR:
497 	case SIOCDLIFADDR:
498 	    {
499 		struct in_ifaddr *ia;
500 		struct in_addr mask, candidate, match;
501 		struct sockaddr_in *sin;
502 		int cmp;
503 
504 		bzero(&mask, sizeof(mask));
505 		if (iflr->flags & IFLR_PREFIX) {
506 			/* lookup a prefix rather than address. */
507 			in_len2mask(&mask, iflr->prefixlen);
508 
509 			sin = (struct sockaddr_in *)&iflr->addr;
510 			match.s_addr = sin->sin_addr.s_addr;
511 			match.s_addr &= mask.s_addr;
512 
513 			/* if you set extra bits, that's wrong */
514 			if (match.s_addr != sin->sin_addr.s_addr)
515 				return EINVAL;
516 
517 			cmp = 1;
518 		} else {
519 			if (cmd == SIOCGLIFADDR) {
520 				/* on getting an address, take the 1st match */
521 				cmp = 0;	/*XXX*/
522 			} else {
523 				/* on deleting an address, do exact match */
524 				in_len2mask(&mask, 32);
525 				sin = (struct sockaddr_in *)&iflr->addr;
526 				match.s_addr = sin->sin_addr.s_addr;
527 
528 				cmp = 1;
529 			}
530 		}
531 
532 		TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
533 			if (ifa->ifa_addr->sa_family != AF_INET)
534 				continue;
535 			if (!cmp)
536 				break;
537 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
538 			candidate.s_addr &= mask.s_addr;
539 			if (candidate.s_addr == match.s_addr)
540 				break;
541 		}
542 		if (!ifa)
543 			return EADDRNOTAVAIL;
544 		ia = ifatoia(ifa);
545 
546 		if (cmd == SIOCGLIFADDR) {
547 			/* fill in the if_laddrreq structure */
548 			memcpy(&iflr->addr, &ia->ia_addr, ia->ia_addr.sin_len);
549 
550 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
551 				memcpy(&iflr->dstaddr, &ia->ia_dstaddr,
552 				    ia->ia_dstaddr.sin_len);
553 			} else
554 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
555 
556 			iflr->prefixlen =
557 				in_mask2len(&ia->ia_sockmask.sin_addr);
558 
559 			iflr->flags = 0;	/*XXX*/
560 
561 			return 0;
562 		} else {
563 			struct in_aliasreq ifra;
564 
565 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
566 			bzero(&ifra, sizeof(ifra));
567 			memcpy(ifra.ifra_name, iflr->iflr_name,
568 			    sizeof(ifra.ifra_name));
569 
570 			memcpy(&ifra.ifra_addr, &ia->ia_addr,
571 			    ia->ia_addr.sin_len);
572 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
573 				memcpy(&ifra.ifra_dstaddr, &ia->ia_dstaddr,
574 				    ia->ia_dstaddr.sin_len);
575 			}
576 			memcpy(&ifra.ifra_dstaddr, &ia->ia_sockmask,
577 			    ia->ia_sockmask.sin_len);
578 
579 			return in_ioctl(SIOCDIFADDR, (caddr_t)&ifra, ifp,
580 			    privileged);
581 		}
582 	    }
583 	}
584 
585 	return EOPNOTSUPP;	/*just for safety*/
586 }
587 
588 /*
589  * Delete any existing route for an interface.
590  */
591 void
592 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
593 {
594 	if (ISSET(ifp->if_flags, IFF_POINTOPOINT))
595 		in_scrubhost(ia, &ia->ia_dstaddr);
596 	else if (!ISSET(ifp->if_flags, IFF_LOOPBACK))
597 		in_remove_prefix(ia);
598 }
599 
600 /*
601  * Initialize an interface's internet address
602  * and routing table entry.
603  */
604 int
605 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
606     int newaddr)
607 {
608 	u_int32_t i = sin->sin_addr.s_addr;
609 	struct sockaddr_in oldaddr;
610 	int error = 0;
611 
612 	splsoftassert(IPL_SOFTNET);
613 
614 	/*
615 	 * Always remove the address from the tree to make sure its
616 	 * position gets updated in case the key changes.
617 	 */
618 	if (!newaddr) {
619 		rt_ifa_dellocal(&ia->ia_ifa);
620 		ifa_del(ifp, &ia->ia_ifa);
621 	}
622 	oldaddr = ia->ia_addr;
623 	ia->ia_addr = *sin;
624 
625 	/*
626 	 * Give the interface a chance to initialize
627 	 * if this is its first address,
628 	 * and to validate the address if necessary.
629 	 */
630 	if (ifp->if_ioctl &&
631 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
632 		ia->ia_addr = oldaddr;
633 	}
634 
635 	/*
636 	 * Add the address to the local list and the global tree.  If an
637 	 * error occured, put back the original address.
638 	 */
639 	ifa_add(ifp, &ia->ia_ifa);
640 	rt_ifa_addlocal(&ia->ia_ifa);
641 
642 	if (error)
643 		goto out;
644 
645 	if (ia->ia_netmask == 0) {
646 		if (IN_CLASSA(i))
647 			ia->ia_netmask = IN_CLASSA_NET;
648 		else if (IN_CLASSB(i))
649 			ia->ia_netmask = IN_CLASSB_NET;
650 		else
651 			ia->ia_netmask = IN_CLASSC_NET;
652 		ia->ia_sockmask.sin_addr.s_addr = ia->ia_netmask;
653 	}
654 
655 	ia->ia_net = i & ia->ia_netmask;
656 	in_socktrim(&ia->ia_sockmask);
657 	/*
658 	 * Add route for the network.
659 	 */
660 	ia->ia_ifa.ifa_metric = ifp->if_metric;
661 	if (ISSET(ifp->if_flags, IFF_BROADCAST)) {
662 		if (IN_RFC3021_SUBNET(ia->ia_netmask))
663 			ia->ia_broadaddr.sin_addr.s_addr = 0;
664 		else {
665 			ia->ia_broadaddr.sin_addr.s_addr =
666 			    ia->ia_net | ~ia->ia_netmask;
667 		}
668 	}
669 
670 	if (ISSET(ifp->if_flags, IFF_POINTOPOINT)) {
671 		/* XXX We should not even call in_ifinit() in this case. */
672 		if (ia->ia_dstaddr.sin_family != AF_INET)
673 			goto out;
674 		error = in_addhost(ia, &ia->ia_dstaddr);
675 	} else if (!ISSET(ifp->if_flags, IFF_LOOPBACK)) {
676 		error = in_insert_prefix(ia);
677 	}
678 
679 	/*
680 	 * If the interface supports multicast, join the "all hosts"
681 	 * multicast group on that interface.
682 	 */
683 	if ((ifp->if_flags & IFF_MULTICAST) && ia->ia_allhosts == NULL) {
684 		struct in_addr addr;
685 
686 		addr.s_addr = INADDR_ALLHOSTS_GROUP;
687 		ia->ia_allhosts = in_addmulti(&addr, ifp);
688 	}
689 
690 out:
691 	if (error && newaddr)
692 		in_purgeaddr(&ia->ia_ifa);
693 
694 	return (error);
695 }
696 
697 void
698 in_purgeaddr(struct ifaddr *ifa)
699 {
700 	struct ifnet *ifp = ifa->ifa_ifp;
701 	struct in_ifaddr *ia = ifatoia(ifa);
702 	extern int ifatrash;
703 
704 	splsoftassert(IPL_SOFTNET);
705 
706 	in_ifscrub(ifp, ia);
707 
708 	rt_ifa_dellocal(&ia->ia_ifa);
709 	rt_ifa_purge(&ia->ia_ifa);
710 	ifa_del(ifp, &ia->ia_ifa);
711 
712 	if (ia->ia_allhosts != NULL) {
713 		in_delmulti(ia->ia_allhosts);
714 		ia->ia_allhosts = NULL;
715 	}
716 
717 	ifatrash++;
718 	ia->ia_ifp = NULL;
719 	ifafree(&ia->ia_ifa);
720 }
721 
722 int
723 in_addhost(struct in_ifaddr *ia, struct sockaddr_in *dst)
724 {
725 	return rt_ifa_add(&ia->ia_ifa, RTF_HOST, sintosa(dst));
726 }
727 
728 int
729 in_scrubhost(struct in_ifaddr *ia, struct sockaddr_in *dst)
730 {
731 	return rt_ifa_del(&ia->ia_ifa, RTF_HOST, sintosa(dst));
732 }
733 
734 /*
735  * Insert the cloning and broadcast routes for this subnet.
736  */
737 int
738 in_insert_prefix(struct in_ifaddr *ia)
739 {
740 	struct ifaddr *ifa = &ia->ia_ifa;
741 	int error;
742 
743 	error = rt_ifa_add(ifa, RTF_CLONING | RTF_CONNECTED, ifa->ifa_addr);
744 	if (error)
745 		return (error);
746 
747 	if (ia->ia_broadaddr.sin_addr.s_addr != 0)
748 		error = rt_ifa_add(ifa, RTF_HOST | RTF_BROADCAST,
749 		    ifa->ifa_broadaddr);
750 
751 	return (error);
752 }
753 
754 void
755 in_remove_prefix(struct in_ifaddr *ia)
756 {
757 	struct ifaddr *ifa = &ia->ia_ifa;
758 
759 	rt_ifa_del(ifa, RTF_CLONING | RTF_CONNECTED, ifa->ifa_addr);
760 
761 	if (ia->ia_broadaddr.sin_addr.s_addr != 0)
762 		rt_ifa_del(ifa, RTF_HOST | RTF_BROADCAST, ifa->ifa_broadaddr);
763 }
764 
765 /*
766  * Return 1 if the address is a local broadcast address.
767  */
768 int
769 in_broadcast(struct in_addr in, u_int rtableid)
770 {
771 	struct ifnet *ifn;
772 	struct ifaddr *ifa;
773 	u_int rdomain;
774 
775 	rdomain = rtable_l2(rtableid);
776 
777 #define ia (ifatoia(ifa))
778 	TAILQ_FOREACH(ifn, &ifnet, if_list) {
779 		if (ifn->if_rdomain != rdomain)
780 			continue;
781 		if ((ifn->if_flags & IFF_BROADCAST) == 0)
782 			continue;
783 		TAILQ_FOREACH(ifa, &ifn->if_addrlist, ifa_list)
784 			if (ifa->ifa_addr->sa_family == AF_INET &&
785 			    in.s_addr != ia->ia_addr.sin_addr.s_addr &&
786 			    in.s_addr == ia->ia_broadaddr.sin_addr.s_addr)
787 				return 1;
788 	}
789 	return (0);
790 #undef ia
791 }
792 
793 /*
794  * Add an address to the list of IP multicast addresses for a given interface.
795  */
796 struct in_multi *
797 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
798 {
799 	struct in_multi *inm;
800 	struct ifreq ifr;
801 	int s;
802 
803 	/*
804 	 * See if address already in list.
805 	 */
806 	IN_LOOKUP_MULTI(*ap, ifp, inm);
807 	if (inm != NULL) {
808 		/*
809 		 * Found it; just increment the reference count.
810 		 */
811 		++inm->inm_refcnt;
812 	} else {
813 		if (ifp->if_ioctl == NULL)
814 			return (NULL);
815 
816 		/*
817 		 * New address; allocate a new multicast record
818 		 * and link it into the interface's multicast list.
819 		 */
820 		inm = malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT | M_ZERO);
821 		if (inm == NULL)
822 			return (NULL);
823 
824 		inm->inm_sin.sin_len = sizeof(struct sockaddr_in);
825 		inm->inm_sin.sin_family = AF_INET;
826 		inm->inm_sin.sin_addr = *ap;
827 		inm->inm_refcnt = 1;
828 		inm->inm_ifidx = ifp->if_index;
829 		inm->inm_ifma.ifma_addr = sintosa(&inm->inm_sin);
830 
831 		/*
832 		 * Ask the network driver to update its multicast reception
833 		 * filter appropriately for the new address.
834 		 */
835 		memset(&ifr, 0, sizeof(ifr));
836 		memcpy(&ifr.ifr_addr, &inm->inm_sin, sizeof(inm->inm_sin));
837 		if ((*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
838 			free(inm, M_IPMADDR, sizeof(*inm));
839 			return (NULL);
840 		}
841 
842 		s = splsoftnet();
843 		TAILQ_INSERT_HEAD(&ifp->if_maddrlist, &inm->inm_ifma,
844 		    ifma_list);
845 		splx(s);
846 
847 		/*
848 		 * Let IGMP know that we have joined a new IP multicast group.
849 		 */
850 		igmp_joingroup(inm);
851 	}
852 
853 	return (inm);
854 }
855 
856 /*
857  * Delete a multicast address record.
858  */
859 void
860 in_delmulti(struct in_multi *inm)
861 {
862 	struct ifreq ifr;
863 	struct ifnet *ifp;
864 	int s;
865 
866 	if (--inm->inm_refcnt == 0) {
867 		/*
868 		 * No remaining claims to this record; let IGMP know that
869 		 * we are leaving the multicast group.
870 		 */
871 		igmp_leavegroup(inm);
872 		ifp = if_get(inm->inm_ifidx);
873 
874 		/*
875 		 * Notify the network driver to update its multicast
876 		 * reception filter.
877 		 */
878 		if (ifp != NULL) {
879 			memset(&ifr, 0, sizeof(ifr));
880 			satosin(&ifr.ifr_addr)->sin_len =
881 			    sizeof(struct sockaddr_in);
882 			satosin(&ifr.ifr_addr)->sin_family = AF_INET;
883 			satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr;
884 			(*ifp->if_ioctl)(ifp, SIOCDELMULTI, (caddr_t)&ifr);
885 
886 			s = splsoftnet();
887 			TAILQ_REMOVE(&ifp->if_maddrlist, &inm->inm_ifma,
888 			    ifma_list);
889 			splx(s);
890 		}
891 		if_put(ifp);
892 
893 		free(inm, M_IPMADDR, sizeof(*inm));
894 	}
895 }
896 
897 /*
898  * Return 1 if the multicast group represented by ``ap'' has been
899  * joined by interface ``ifp'', 0 otherwise.
900  */
901 int
902 in_hasmulti(struct in_addr *ap, struct ifnet *ifp)
903 {
904 	struct in_multi *inm;
905 	int joined;
906 
907 	KERNEL_LOCK();
908 	IN_LOOKUP_MULTI(*ap, ifp, inm);
909 	joined = (inm != NULL);
910 	KERNEL_UNLOCK();
911 
912 	return (joined);
913 }
914 
915 void
916 in_ifdetach(struct ifnet *ifp)
917 {
918 	struct ifaddr *ifa, *next;
919 
920 	/* nuke any of IPv4 addresses we have */
921 	TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrlist, ifa_list, next) {
922 		if (ifa->ifa_addr->sa_family != AF_INET)
923 			continue;
924 		in_purgeaddr(ifa);
925 		dohooks(ifp->if_addrhooks, 0);
926 	}
927 }
928 
929 void
930 in_prefixlen2mask(struct in_addr *maskp, int plen)
931 {
932 	if (plen == 0)
933 		maskp->s_addr = 0;
934 	else
935 		maskp->s_addr = htonl(0xffffffff << (32 - plen));
936 }
937