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