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