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