xref: /dflybsd-src/sys/netinet/in.c (revision 50b86318b10455d5aebccd83a84238c45968fe7d)
1 /*
2  * Copyright (c) 1982, 1986, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)in.c	8.4 (Berkeley) 1/9/95
34  * $FreeBSD: src/sys/netinet/in.c,v 1.44.2.14 2002/11/08 00:45:50 suz Exp $
35  * $DragonFly: src/sys/netinet/in.c,v 1.15 2005/06/02 23:52:42 dillon Exp $
36  */
37 
38 #include "opt_bootp.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sockio.h>
42 #include <sys/malloc.h>
43 #include <sys/proc.h>
44 #include <sys/socket.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/thread2.h>
48 
49 #include <net/if.h>
50 #include <net/if_types.h>
51 #include <net/route.h>
52 
53 #include <netinet/in.h>
54 #include <netinet/in_var.h>
55 #include <netinet/in_pcb.h>
56 
57 #include <netinet/igmp_var.h>
58 
59 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
60 
61 static int in_mask2len (struct in_addr *);
62 static void in_len2mask (struct in_addr *, int);
63 static int in_lifaddr_ioctl (struct socket *, u_long, caddr_t,
64 	struct ifnet *, struct thread *);
65 
66 static void	in_socktrim (struct sockaddr_in *);
67 static int	in_ifinit (struct ifnet *,
68 	    struct in_ifaddr *, struct sockaddr_in *, int);
69 
70 static int subnetsarelocal = 0;
71 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
72 	&subnetsarelocal, 0, "");
73 
74 struct in_multihead in_multihead; /* XXX BSS initialization */
75 
76 extern struct inpcbinfo ripcbinfo;
77 extern struct inpcbinfo udbinfo;
78 
79 /*
80  * Return 1 if an internet address is for a ``local'' host
81  * (one to which we have a connection).  If subnetsarelocal
82  * is true, this includes other subnets of the local net.
83  * Otherwise, it includes only the directly-connected (sub)nets.
84  */
85 int
86 in_localaddr(in)
87 	struct in_addr in;
88 {
89 	u_long i = ntohl(in.s_addr);
90 	struct in_ifaddr *ia;
91 
92 	if (subnetsarelocal) {
93 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
94 			if ((i & ia->ia_netmask) == ia->ia_net)
95 				return (1);
96 	} else {
97 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
98 			if ((i & ia->ia_subnetmask) == ia->ia_subnet)
99 				return (1);
100 	}
101 	return (0);
102 }
103 
104 /*
105  * Determine whether an IP address is in a reserved set of addresses
106  * that may not be forwarded, or whether datagrams to that destination
107  * may be forwarded.
108  */
109 int
110 in_canforward(in)
111 	struct in_addr in;
112 {
113 	u_long i = ntohl(in.s_addr);
114 	u_long net;
115 
116 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
117 		return (0);
118 	if (IN_CLASSA(i)) {
119 		net = i & IN_CLASSA_NET;
120 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
121 			return (0);
122 	}
123 	return (1);
124 }
125 
126 /*
127  * Trim a mask in a sockaddr
128  */
129 static void
130 in_socktrim(ap)
131 struct sockaddr_in *ap;
132 {
133     char *cplim = (char *) &ap->sin_addr;
134     char *cp = (char *) (&ap->sin_addr + 1);
135 
136     ap->sin_len = 0;
137     while (--cp >= cplim)
138 	if (*cp) {
139 	    (ap)->sin_len = cp - (char *) (ap) + 1;
140 	    break;
141 	}
142 }
143 
144 static int
145 in_mask2len(mask)
146 	struct in_addr *mask;
147 {
148 	int x, y;
149 	u_char *p;
150 
151 	p = (u_char *)mask;
152 	for (x = 0; x < sizeof *mask; x++) {
153 		if (p[x] != 0xff)
154 			break;
155 	}
156 	y = 0;
157 	if (x < sizeof *mask) {
158 		for (y = 0; y < 8; y++) {
159 			if ((p[x] & (0x80 >> y)) == 0)
160 				break;
161 		}
162 	}
163 	return x * 8 + y;
164 }
165 
166 static void
167 in_len2mask(mask, len)
168 	struct in_addr *mask;
169 	int len;
170 {
171 	int i;
172 	u_char *p;
173 
174 	p = (u_char *)mask;
175 	bzero(mask, sizeof *mask);
176 	for (i = 0; i < len / 8; i++)
177 		p[i] = 0xff;
178 	if (len % 8)
179 		p[i] = (0xff00 >> (len % 8)) & 0xff;
180 }
181 
182 static int in_interfaces;	/* number of external internet interfaces */
183 
184 /*
185  * Generic internet control operations (ioctl's).
186  * Ifp is 0 if not an interface-specific ioctl.
187  *
188  * NOTE! td might be NULL.
189  */
190 /* ARGSUSED */
191 int
192 in_control(so, cmd, data, ifp, td)
193 	struct socket *so;
194 	u_long cmd;
195 	caddr_t data;
196 	struct ifnet *ifp;
197 	struct thread *td;
198 {
199 	struct ifreq *ifr = (struct ifreq *)data;
200 	struct in_ifaddr *ia = 0, *iap;
201 	struct ifaddr *ifa;
202 	struct in_addr dst;
203 	struct in_ifaddr *oia;
204 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
205 	struct sockaddr_in oldaddr;
206 	int hostIsNew, iaIsNew, maskIsNew;
207 	int error = 0;
208 
209 	iaIsNew = 0;
210 
211 	switch (cmd) {
212 	case SIOCALIFADDR:
213 	case SIOCDLIFADDR:
214 		if (td && (error = suser(td)) != 0)
215 			return error;
216 		/*fall through*/
217 	case SIOCGLIFADDR:
218 		if (!ifp)
219 			return EINVAL;
220 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
221 	}
222 
223 	/*
224 	 * Find address for this interface, if it exists.
225 	 *
226 	 * If an alias address was specified, find that one instead of
227 	 * the first one on the interface, if possible
228 	 */
229 	if (ifp) {
230 		dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
231 		LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
232 			if (iap->ia_ifp == ifp &&
233 			    iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
234 				ia = iap;
235 				break;
236 			}
237 		if (ia == NULL)
238 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
239 				iap = ifatoia(ifa);
240 				if (iap->ia_addr.sin_family == AF_INET) {
241 					ia = iap;
242 					break;
243 				}
244 			}
245 	}
246 
247 	switch (cmd) {
248 
249 	case SIOCAIFADDR:
250 	case SIOCDIFADDR:
251 		if (ifp == 0)
252 			return (EADDRNOTAVAIL);
253 		if (ifra->ifra_addr.sin_family == AF_INET) {
254 			for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
255 				if (ia->ia_ifp == ifp  &&
256 				    ia->ia_addr.sin_addr.s_addr ==
257 				    ifra->ifra_addr.sin_addr.s_addr)
258 					break;
259 			}
260 			if ((ifp->if_flags & IFF_POINTOPOINT)
261 			    && (cmd == SIOCAIFADDR)
262 			    && (ifra->ifra_dstaddr.sin_addr.s_addr
263 				== INADDR_ANY)) {
264 				return EDESTADDRREQ;
265 			}
266 		}
267 		if (cmd == SIOCDIFADDR && ia == 0)
268 			return (EADDRNOTAVAIL);
269 		/* FALLTHROUGH */
270 	case SIOCSIFADDR:
271 	case SIOCSIFNETMASK:
272 	case SIOCSIFDSTADDR:
273 		if (td && (error = suser(td)) != 0)
274 			return error;
275 
276 		if (ifp == 0)
277 			return (EADDRNOTAVAIL);
278 		if (ia == (struct in_ifaddr *)0) {
279 			ia = (struct in_ifaddr *)
280 				malloc(sizeof *ia, M_IFADDR, M_WAITOK);
281 			if (ia == (struct in_ifaddr *)NULL)
282 				return (ENOBUFS);
283 			bzero(ia, sizeof *ia);
284 			/*
285 			 * Protect from ipintr() traversing address list
286 			 * while we're modifying it.
287 			 */
288 			crit_enter();
289 
290 			TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
291 			ifa = &ia->ia_ifa;
292 			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
293 
294 			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
295 			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
296 			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
297 			ia->ia_sockmask.sin_len = 8;
298 			ia->ia_sockmask.sin_family = AF_INET;
299 			if (ifp->if_flags & IFF_BROADCAST) {
300 				ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
301 				ia->ia_broadaddr.sin_family = AF_INET;
302 			}
303 			ia->ia_ifp = ifp;
304 			if (!(ifp->if_flags & IFF_LOOPBACK))
305 				in_interfaces++;
306 			iaIsNew = 1;
307 			crit_exit();
308 		}
309 		break;
310 
311 	case SIOCSIFBRDADDR:
312 		if (td && (error = suser(td)) != 0)
313 			return error;
314 		/* FALLTHROUGH */
315 
316 	case SIOCGIFADDR:
317 	case SIOCGIFNETMASK:
318 	case SIOCGIFDSTADDR:
319 	case SIOCGIFBRDADDR:
320 		if (ia == (struct in_ifaddr *)0)
321 			return (EADDRNOTAVAIL);
322 		break;
323 	}
324 	switch (cmd) {
325 
326 	case SIOCGIFADDR:
327 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
328 		return (0);
329 
330 	case SIOCGIFBRDADDR:
331 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
332 			return (EINVAL);
333 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
334 		return (0);
335 
336 	case SIOCGIFDSTADDR:
337 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
338 			return (EINVAL);
339 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
340 		return (0);
341 
342 	case SIOCGIFNETMASK:
343 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
344 		return (0);
345 
346 	case SIOCSIFDSTADDR:
347 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
348 			return (EINVAL);
349 		oldaddr = ia->ia_dstaddr;
350 		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
351 		if (ifp->if_ioctl &&
352 		    (error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
353 					      td->td_proc->p_ucred))) {
354 			ia->ia_dstaddr = oldaddr;
355 			return (error);
356 		}
357 		if (ia->ia_flags & IFA_ROUTE) {
358 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
359 			rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
360 			ia->ia_ifa.ifa_dstaddr =
361 					(struct sockaddr *)&ia->ia_dstaddr;
362 			rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
363 		}
364 		return (0);
365 
366 	case SIOCSIFBRDADDR:
367 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
368 			return (EINVAL);
369 		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
370 		return (0);
371 
372 	case SIOCSIFADDR:
373 		error = in_ifinit(ifp, ia,
374 		    (struct sockaddr_in *) &ifr->ifr_addr, 1);
375 		if (error != 0 && iaIsNew)
376 			break;
377 		if (error == 0)
378 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
379 		return (0);
380 
381 	case SIOCSIFNETMASK:
382 		ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
383 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
384 		return (0);
385 
386 	case SIOCAIFADDR:
387 		maskIsNew = 0;
388 		hostIsNew = 1;
389 		error = 0;
390 		if (ia->ia_addr.sin_family == AF_INET) {
391 			if (ifra->ifra_addr.sin_len == 0) {
392 				ifra->ifra_addr = ia->ia_addr;
393 				hostIsNew = 0;
394 			} else if (ifra->ifra_addr.sin_addr.s_addr ==
395 					       ia->ia_addr.sin_addr.s_addr)
396 				hostIsNew = 0;
397 		}
398 		if (ifra->ifra_mask.sin_len) {
399 			in_ifscrub(ifp, ia);
400 			ia->ia_sockmask = ifra->ifra_mask;
401 			ia->ia_sockmask.sin_family = AF_INET;
402 			ia->ia_subnetmask =
403 			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
404 			maskIsNew = 1;
405 		}
406 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
407 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
408 			in_ifscrub(ifp, ia);
409 			ia->ia_dstaddr = ifra->ifra_dstaddr;
410 			maskIsNew  = 1; /* We lie; but the effect's the same */
411 		}
412 		if (ifra->ifra_addr.sin_family == AF_INET &&
413 		    (hostIsNew || maskIsNew))
414 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
415 
416 		if (error != 0 && iaIsNew)
417 			break;
418 
419 		if ((ifp->if_flags & IFF_BROADCAST) &&
420 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
421 			ia->ia_broadaddr = ifra->ifra_broadaddr;
422 		if (error == 0)
423 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
424 		return (error);
425 
426 	case SIOCDIFADDR:
427 		/*
428 		 * in_ifscrub kills the interface route.
429 		 */
430 		in_ifscrub(ifp, ia);
431 		/*
432 		 * in_ifadown gets rid of all the rest of
433 		 * the routes.  This is not quite the right
434 		 * thing to do, but at least if we are running
435 		 * a routing process they will come back.
436 		 */
437 		in_ifadown(&ia->ia_ifa, 1);
438 		/*
439 		 * XXX horrible hack to detect that we are being called
440 		 * from if_detach()
441 		 */
442 		if (!ifnet_addrs[ifp->if_index - 1]) {
443 			in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
444 			in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);
445 		}
446 		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
447 		error = 0;
448 		break;
449 
450 	default:
451 		if (ifp == NULL || ifp->if_ioctl == NULL)
452 			return (EOPNOTSUPP);
453 		return ((*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred));
454 	}
455 
456 	/*
457 	 * Protect from ipintr() traversing address list while we're modifying
458 	 * it.
459 	 */
460 	crit_enter();
461 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
462 	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
463 	LIST_REMOVE(ia, ia_hash);
464 	IFAFREE(&ia->ia_ifa);
465 	crit_exit();
466 
467 	return (error);
468 }
469 
470 /*
471  * SIOC[GAD]LIFADDR.
472  *	SIOCGLIFADDR: get first address. (?!?)
473  *	SIOCGLIFADDR with IFLR_PREFIX:
474  *		get first address that matches the specified prefix.
475  *	SIOCALIFADDR: add the specified address.
476  *	SIOCALIFADDR with IFLR_PREFIX:
477  *		EINVAL since we can't deduce hostid part of the address.
478  *	SIOCDLIFADDR: delete the specified address.
479  *	SIOCDLIFADDR with IFLR_PREFIX:
480  *		delete the first address that matches the specified prefix.
481  * return values:
482  *	EINVAL on invalid parameters
483  *	EADDRNOTAVAIL on prefix match failed/specified address not found
484  *	other values may be returned from in_ioctl()
485  *
486  * NOTE! td might be NULL.
487  */
488 static int
489 in_lifaddr_ioctl(so, cmd, data, ifp, td)
490 	struct socket *so;
491 	u_long cmd;
492 	caddr_t	data;
493 	struct ifnet *ifp;
494 	struct thread *td;
495 {
496 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
497 	struct ifaddr *ifa;
498 
499 	/* sanity checks */
500 	if (!data || !ifp) {
501 		panic("invalid argument to in_lifaddr_ioctl");
502 		/*NOTRECHED*/
503 	}
504 
505 	switch (cmd) {
506 	case SIOCGLIFADDR:
507 		/* address must be specified on GET with IFLR_PREFIX */
508 		if ((iflr->flags & IFLR_PREFIX) == 0)
509 			break;
510 		/*FALLTHROUGH*/
511 	case SIOCALIFADDR:
512 	case SIOCDLIFADDR:
513 		/* address must be specified on ADD and DELETE */
514 		if (iflr->addr.ss_family != AF_INET)
515 			return EINVAL;
516 		if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
517 			return EINVAL;
518 		/* XXX need improvement */
519 		if (iflr->dstaddr.ss_family
520 		 && iflr->dstaddr.ss_family != AF_INET)
521 			return EINVAL;
522 		if (iflr->dstaddr.ss_family
523 		 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
524 			return EINVAL;
525 		break;
526 	default: /*shouldn't happen*/
527 		return EOPNOTSUPP;
528 	}
529 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
530 		return EINVAL;
531 
532 	switch (cmd) {
533 	case SIOCALIFADDR:
534 	    {
535 		struct in_aliasreq ifra;
536 
537 		if (iflr->flags & IFLR_PREFIX)
538 			return EINVAL;
539 
540 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
541 		bzero(&ifra, sizeof ifra);
542 		bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
543 
544 		bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
545 
546 		if (iflr->dstaddr.ss_family) {	/*XXX*/
547 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
548 				iflr->dstaddr.ss_len);
549 		}
550 
551 		ifra.ifra_mask.sin_family = AF_INET;
552 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
553 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
554 
555 		return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
556 	    }
557 	case SIOCGLIFADDR:
558 	case SIOCDLIFADDR:
559 	    {
560 		struct in_ifaddr *ia;
561 		struct in_addr mask, candidate, match;
562 		struct sockaddr_in *sin;
563 		int cmp;
564 
565 		bzero(&mask, sizeof mask);
566 		if (iflr->flags & IFLR_PREFIX) {
567 			/* lookup a prefix rather than address. */
568 			in_len2mask(&mask, iflr->prefixlen);
569 
570 			sin = (struct sockaddr_in *)&iflr->addr;
571 			match.s_addr = sin->sin_addr.s_addr;
572 			match.s_addr &= mask.s_addr;
573 
574 			/* if you set extra bits, that's wrong */
575 			if (match.s_addr != sin->sin_addr.s_addr)
576 				return EINVAL;
577 
578 			cmp = 1;
579 		} else {
580 			if (cmd == SIOCGLIFADDR) {
581 				/* on getting an address, take the 1st match */
582 				cmp = 0;	/*XXX*/
583 			} else {
584 				/* on deleting an address, do exact match */
585 				in_len2mask(&mask, 32);
586 				sin = (struct sockaddr_in *)&iflr->addr;
587 				match.s_addr = sin->sin_addr.s_addr;
588 
589 				cmp = 1;
590 			}
591 		}
592 
593 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)	{
594 			if (ifa->ifa_addr->sa_family != AF_INET6)
595 				continue;
596 			if (!cmp)
597 				break;
598 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
599 			candidate.s_addr &= mask.s_addr;
600 			if (candidate.s_addr == match.s_addr)
601 				break;
602 		}
603 		if (!ifa)
604 			return EADDRNOTAVAIL;
605 		ia = (struct in_ifaddr *)ifa;
606 
607 		if (cmd == SIOCGLIFADDR) {
608 			/* fill in the if_laddrreq structure */
609 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
610 
611 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
612 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
613 					ia->ia_dstaddr.sin_len);
614 			} else
615 				bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
616 
617 			iflr->prefixlen =
618 				in_mask2len(&ia->ia_sockmask.sin_addr);
619 
620 			iflr->flags = 0;	/*XXX*/
621 
622 			return 0;
623 		} else {
624 			struct in_aliasreq ifra;
625 
626 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
627 			bzero(&ifra, sizeof ifra);
628 			bcopy(iflr->iflr_name, ifra.ifra_name,
629 				sizeof ifra.ifra_name);
630 
631 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
632 				ia->ia_addr.sin_len);
633 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
634 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
635 					ia->ia_dstaddr.sin_len);
636 			}
637 			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
638 				ia->ia_sockmask.sin_len);
639 
640 			return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
641 					  ifp, td);
642 		}
643 	    }
644 	}
645 
646 	return EOPNOTSUPP;	/*just for safety*/
647 }
648 
649 /*
650  * Delete any existing route for an interface.
651  */
652 void
653 in_ifscrub(ifp, ia)
654 	struct ifnet *ifp;
655 	struct in_ifaddr *ia;
656 {
657 
658 	if ((ia->ia_flags & IFA_ROUTE) == 0)
659 		return;
660 	if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
661 		rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
662 	else
663 		rtinit(&ia->ia_ifa, RTM_DELETE, 0);
664 	ia->ia_flags &= ~IFA_ROUTE;
665 }
666 
667 /*
668  * Initialize an interface's internet address
669  * and routing table entry.
670  */
671 static int
672 in_ifinit(ifp, ia, sin, scrub)
673 	struct ifnet *ifp;
674 	struct in_ifaddr *ia;
675 	struct sockaddr_in *sin;
676 	int scrub;
677 {
678 	u_long i = ntohl(sin->sin_addr.s_addr);
679 	struct sockaddr_in oldaddr;
680 	int flags = RTF_UP, error = 0;
681 
682 	crit_enter();
683 
684 	oldaddr = ia->ia_addr;
685 	if (oldaddr.sin_family == AF_INET)
686 		LIST_REMOVE(ia, ia_hash);
687 	ia->ia_addr = *sin;
688 	if (ia->ia_addr.sin_family == AF_INET)
689 		LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
690 		    ia, ia_hash);
691 	/*
692 	 * Give the interface a chance to initialize
693 	 * if this is its first address,
694 	 * and to validate the address if necessary.
695 	 */
696 	if (ifp->if_ioctl &&
697 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia,
698 				      (struct ucred *)NULL))) {
699 		crit_exit();
700 		/* LIST_REMOVE(ia, ia_hash) is done in in_control */
701 		ia->ia_addr = oldaddr;
702 		if (ia->ia_addr.sin_family == AF_INET)
703 			LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
704 			    ia, ia_hash);
705 		return (error);
706 	}
707 	crit_exit();
708 	if (scrub) {
709 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
710 		in_ifscrub(ifp, ia);
711 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
712 	}
713 	if (IN_CLASSA(i))
714 		ia->ia_netmask = IN_CLASSA_NET;
715 	else if (IN_CLASSB(i))
716 		ia->ia_netmask = IN_CLASSB_NET;
717 	else
718 		ia->ia_netmask = IN_CLASSC_NET;
719 	/*
720 	 * The subnet mask usually includes at least the standard network part,
721 	 * but may may be smaller in the case of supernetting.
722 	 * If it is set, we believe it.
723 	 */
724 	if (ia->ia_subnetmask == 0) {
725 		ia->ia_subnetmask = ia->ia_netmask;
726 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
727 	} else
728 		ia->ia_netmask &= ia->ia_subnetmask;
729 	ia->ia_net = i & ia->ia_netmask;
730 	ia->ia_subnet = i & ia->ia_subnetmask;
731 	in_socktrim(&ia->ia_sockmask);
732 	/*
733 	 * Add route for the network.
734 	 */
735 	ia->ia_ifa.ifa_metric = ifp->if_metric;
736 	if (ifp->if_flags & IFF_BROADCAST) {
737 		ia->ia_broadaddr.sin_addr.s_addr =
738 			htonl(ia->ia_subnet | ~ia->ia_subnetmask);
739 		ia->ia_netbroadcast.s_addr =
740 			htonl(ia->ia_net | ~ ia->ia_netmask);
741 	} else if (ifp->if_flags & IFF_LOOPBACK) {
742 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
743 		flags |= RTF_HOST;
744 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
745 		if (ia->ia_dstaddr.sin_family != AF_INET)
746 			return (0);
747 		flags |= RTF_HOST;
748 	}
749 
750 	/*-
751 	 * Don't add host routes for interface addresses of
752 	 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0.  This makes it
753 	 * possible to assign several such address pairs with consistent
754 	 * results (no host route) and is required by BOOTP.
755 	 *
756 	 * XXX: This is ugly !  There should be a way for the caller to
757 	 *      say that they don't want a host route.
758 	 */
759 	if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
760 	    ia->ia_netmask != IN_CLASSA_NET ||
761 	    ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
762 		if ((error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags)) != 0) {
763 			ia->ia_addr = oldaddr;
764 			return (error);
765 		}
766 		ia->ia_flags |= IFA_ROUTE;
767 	}
768 
769 	/*
770 	 * If the interface supports multicast, join the "all hosts"
771 	 * multicast group on that interface.
772 	 */
773 	if (ifp->if_flags & IFF_MULTICAST) {
774 		struct in_addr addr;
775 
776 		addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
777 		in_addmulti(&addr, ifp);
778 	}
779 	return (error);
780 }
781 
782 
783 /*
784  * Return 1 if the address might be a local broadcast address.
785  */
786 int
787 in_broadcast(struct in_addr in, struct ifnet *ifp)
788 {
789 	struct ifaddr *ifa;
790 	u_long t;
791 
792 	if (in.s_addr == INADDR_BROADCAST ||
793 	    in.s_addr == INADDR_ANY)
794 		return 1;
795 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
796 		return 0;
797 	t = ntohl(in.s_addr);
798 	/*
799 	 * Look through the list of addresses for a match
800 	 * with a broadcast address.
801 	 */
802 #define ia ((struct in_ifaddr *)ifa)
803 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
804 		if (ifa->ifa_addr->sa_family == AF_INET &&
805 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
806 		     in.s_addr == ia->ia_netbroadcast.s_addr ||
807 		     /*
808 		      * Check for old-style (host 0) broadcast.
809 		      */
810 		     t == ia->ia_subnet || t == ia->ia_net) &&
811 		     /*
812 		      * Check for an all one subnetmask. These
813 		      * only exist when an interface gets a secondary
814 		      * address.
815 		      */
816 		     ia->ia_subnetmask != (u_long)0xffffffff)
817 			    return 1;
818 	return (0);
819 #undef ia
820 }
821 /*
822  * Add an address to the list of IP multicast addresses for a given interface.
823  */
824 struct in_multi *
825 in_addmulti(ap, ifp)
826 	struct in_addr *ap;
827 	struct ifnet *ifp;
828 {
829 	struct in_multi *inm;
830 	int error;
831 	struct sockaddr_in sin;
832 	struct ifmultiaddr *ifma;
833 
834 	/*
835 	 * Call generic routine to add membership or increment
836 	 * refcount.  It wants addresses in the form of a sockaddr,
837 	 * so we build one here (being careful to zero the unused bytes).
838 	 */
839 	bzero(&sin, sizeof sin);
840 	sin.sin_family = AF_INET;
841 	sin.sin_len = sizeof sin;
842 	sin.sin_addr = *ap;
843 	crit_enter();
844 	error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
845 	if (error) {
846 		crit_exit();
847 		return 0;
848 	}
849 
850 	/*
851 	 * If ifma->ifma_protospec is null, then if_addmulti() created
852 	 * a new record.  Otherwise, we are done.
853 	 */
854 	if (ifma->ifma_protospec != 0) {
855 		crit_exit();
856 		return ifma->ifma_protospec;
857 	}
858 
859 	/* XXX - if_addmulti uses M_WAITOK.  Can this really be called
860 	   at interrupt time?  If so, need to fix if_addmulti. XXX */
861 	inm = malloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
862 	inm->inm_addr = *ap;
863 	inm->inm_ifp = ifp;
864 	inm->inm_ifma = ifma;
865 	ifma->ifma_protospec = inm;
866 	LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
867 
868 	/*
869 	 * Let IGMP know that we have joined a new IP multicast group.
870 	 */
871 	igmp_joingroup(inm);
872 	crit_exit();
873 	return (inm);
874 }
875 
876 /*
877  * Delete a multicast address record.
878  */
879 void
880 in_delmulti(inm)
881 	struct in_multi *inm;
882 {
883 	struct ifmultiaddr *ifma;
884 	struct in_multi my_inm;
885 
886 	crit_enter();
887 	ifma = inm->inm_ifma;
888 	my_inm.inm_ifp = NULL ; /* don't send the leave msg */
889 	if (ifma->ifma_refcount == 1) {
890 		/*
891 		 * No remaining claims to this record; let IGMP know that
892 		 * we are leaving the multicast group.
893 		 * But do it after the if_delmulti() which might reset
894 		 * the interface and nuke the packet.
895 		 */
896 		my_inm = *inm ;
897 		ifma->ifma_protospec = 0;
898 		LIST_REMOVE(inm, inm_link);
899 		free(inm, M_IPMADDR);
900 	}
901 	/* XXX - should be separate API for when we have an ifma? */
902 	if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
903 	if (my_inm.inm_ifp != NULL)
904 		igmp_leavegroup(&my_inm);
905 	crit_exit();
906 }
907