xref: /dragonfly/sys/netinet/in.c (revision 71126e33)
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.12 2004/09/10 14:02:01 joerg 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 
48 #include <net/if.h>
49 #include <net/if_types.h>
50 #include <net/route.h>
51 
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <netinet/in_pcb.h>
55 
56 #include <netinet/igmp_var.h>
57 
58 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
59 
60 static int in_mask2len (struct in_addr *);
61 static void in_len2mask (struct in_addr *, int);
62 static int in_lifaddr_ioctl (struct socket *, u_long, caddr_t,
63 	struct ifnet *, struct thread *);
64 
65 static void	in_socktrim (struct sockaddr_in *);
66 static int	in_ifinit (struct ifnet *,
67 	    struct in_ifaddr *, struct sockaddr_in *, int);
68 
69 static int subnetsarelocal = 0;
70 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
71 	&subnetsarelocal, 0, "");
72 
73 struct in_multihead in_multihead; /* XXX BSS initialization */
74 
75 extern struct inpcbinfo ripcbinfo;
76 extern struct inpcbinfo udbinfo;
77 
78 /*
79  * Return 1 if an internet address is for a ``local'' host
80  * (one to which we have a connection).  If subnetsarelocal
81  * is true, this includes other subnets of the local net.
82  * Otherwise, it includes only the directly-connected (sub)nets.
83  */
84 int
85 in_localaddr(in)
86 	struct in_addr in;
87 {
88 	u_long i = ntohl(in.s_addr);
89 	struct in_ifaddr *ia;
90 
91 	if (subnetsarelocal) {
92 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
93 			if ((i & ia->ia_netmask) == ia->ia_net)
94 				return (1);
95 	} else {
96 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
97 			if ((i & ia->ia_subnetmask) == ia->ia_subnet)
98 				return (1);
99 	}
100 	return (0);
101 }
102 
103 /*
104  * Determine whether an IP address is in a reserved set of addresses
105  * that may not be forwarded, or whether datagrams to that destination
106  * may be forwarded.
107  */
108 int
109 in_canforward(in)
110 	struct in_addr in;
111 {
112 	u_long i = ntohl(in.s_addr);
113 	u_long net;
114 
115 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
116 		return (0);
117 	if (IN_CLASSA(i)) {
118 		net = i & IN_CLASSA_NET;
119 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
120 			return (0);
121 	}
122 	return (1);
123 }
124 
125 /*
126  * Trim a mask in a sockaddr
127  */
128 static void
129 in_socktrim(ap)
130 struct sockaddr_in *ap;
131 {
132     char *cplim = (char *) &ap->sin_addr;
133     char *cp = (char *) (&ap->sin_addr + 1);
134 
135     ap->sin_len = 0;
136     while (--cp >= cplim)
137         if (*cp) {
138 	    (ap)->sin_len = cp - (char *) (ap) + 1;
139 	    break;
140 	}
141 }
142 
143 static int
144 in_mask2len(mask)
145 	struct in_addr *mask;
146 {
147 	int x, y;
148 	u_char *p;
149 
150 	p = (u_char *)mask;
151 	for (x = 0; x < sizeof(*mask); x++) {
152 		if (p[x] != 0xff)
153 			break;
154 	}
155 	y = 0;
156 	if (x < sizeof(*mask)) {
157 		for (y = 0; y < 8; y++) {
158 			if ((p[x] & (0x80 >> y)) == 0)
159 				break;
160 		}
161 	}
162 	return x * 8 + y;
163 }
164 
165 static void
166 in_len2mask(mask, len)
167 	struct in_addr *mask;
168 	int len;
169 {
170 	int i;
171 	u_char *p;
172 
173 	p = (u_char *)mask;
174 	bzero(mask, sizeof(*mask));
175 	for (i = 0; i < len / 8; i++)
176 		p[i] = 0xff;
177 	if (len % 8)
178 		p[i] = (0xff00 >> (len % 8)) & 0xff;
179 }
180 
181 static int in_interfaces;	/* number of external internet interfaces */
182 
183 /*
184  * Generic internet control operations (ioctl's).
185  * Ifp is 0 if not an interface-specific ioctl.
186  *
187  * NOTE! td might be NULL.
188  */
189 /* ARGSUSED */
190 int
191 in_control(so, cmd, data, ifp, td)
192 	struct socket *so;
193 	u_long cmd;
194 	caddr_t data;
195 	struct ifnet *ifp;
196 	struct thread *td;
197 {
198 	struct ifreq *ifr = (struct ifreq *)data;
199 	struct in_ifaddr *ia = 0, *iap;
200 	struct ifaddr *ifa;
201 	struct in_addr dst;
202 	struct in_ifaddr *oia;
203 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
204 	struct sockaddr_in oldaddr;
205 	int hostIsNew, iaIsNew, maskIsNew, s;
206 	int error = 0;
207 
208 	iaIsNew = 0;
209 
210 	switch (cmd) {
211 	case SIOCALIFADDR:
212 	case SIOCDLIFADDR:
213 		if (td && (error = suser(td)) != 0)
214 			return error;
215 		/*fall through*/
216 	case SIOCGLIFADDR:
217 		if (!ifp)
218 			return EINVAL;
219 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
220 	}
221 
222 	/*
223 	 * Find address for this interface, if it exists.
224 	 *
225 	 * If an alias address was specified, find that one instead of
226 	 * the first one on the interface, if possible
227 	 */
228 	if (ifp) {
229 		dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
230 		LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
231 			if (iap->ia_ifp == ifp &&
232 			    iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
233 				ia = iap;
234 				break;
235 			}
236 		if (ia == NULL)
237 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
238 				iap = ifatoia(ifa);
239 				if (iap->ia_addr.sin_family == AF_INET) {
240 					ia = iap;
241 					break;
242 				}
243 			}
244 	}
245 
246 	switch (cmd) {
247 
248 	case SIOCAIFADDR:
249 	case SIOCDIFADDR:
250 		if (ifp == 0)
251 			return (EADDRNOTAVAIL);
252 		if (ifra->ifra_addr.sin_family == AF_INET) {
253 			for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
254 				if (ia->ia_ifp == ifp  &&
255 				    ia->ia_addr.sin_addr.s_addr ==
256 				    ifra->ifra_addr.sin_addr.s_addr)
257 					break;
258 			}
259 			if ((ifp->if_flags & IFF_POINTOPOINT)
260 			    && (cmd == SIOCAIFADDR)
261 			    && (ifra->ifra_dstaddr.sin_addr.s_addr
262 				== INADDR_ANY)) {
263 				return EDESTADDRREQ;
264 			}
265 		}
266 		if (cmd == SIOCDIFADDR && ia == 0)
267 			return (EADDRNOTAVAIL);
268 		/* FALLTHROUGH */
269 	case SIOCSIFADDR:
270 	case SIOCSIFNETMASK:
271 	case SIOCSIFDSTADDR:
272 		if (td && (error = suser(td)) != 0)
273 			return error;
274 
275 		if (ifp == 0)
276 			return (EADDRNOTAVAIL);
277 		if (ia == (struct in_ifaddr *)0) {
278 			ia = (struct in_ifaddr *)
279 				malloc(sizeof *ia, M_IFADDR, M_WAITOK);
280 			if (ia == (struct in_ifaddr *)NULL)
281 				return (ENOBUFS);
282 			bzero((caddr_t)ia, sizeof *ia);
283 			/*
284 			 * Protect from ipintr() traversing address list
285 			 * while we're modifying it.
286 			 */
287 			s = splnet();
288 
289 			TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
290 			ifa = &ia->ia_ifa;
291 			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
292 
293 			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
294 			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
295 			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
296 			ia->ia_sockmask.sin_len = 8;
297 			ia->ia_sockmask.sin_family = AF_INET;
298 			if (ifp->if_flags & IFF_BROADCAST) {
299 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
300 				ia->ia_broadaddr.sin_family = AF_INET;
301 			}
302 			ia->ia_ifp = ifp;
303 			if (!(ifp->if_flags & IFF_LOOPBACK))
304 				in_interfaces++;
305 			iaIsNew = 1;
306 			splx(s);
307 		}
308 		break;
309 
310 	case SIOCSIFBRDADDR:
311 		if (td && (error = suser(td)) != 0)
312 			return error;
313 		/* FALLTHROUGH */
314 
315 	case SIOCGIFADDR:
316 	case SIOCGIFNETMASK:
317 	case SIOCGIFDSTADDR:
318 	case SIOCGIFBRDADDR:
319 		if (ia == (struct in_ifaddr *)0)
320 			return (EADDRNOTAVAIL);
321 		break;
322 	}
323 	switch (cmd) {
324 
325 	case SIOCGIFADDR:
326 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
327 		return (0);
328 
329 	case SIOCGIFBRDADDR:
330 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
331 			return (EINVAL);
332 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
333 		return (0);
334 
335 	case SIOCGIFDSTADDR:
336 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
337 			return (EINVAL);
338 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
339 		return (0);
340 
341 	case SIOCGIFNETMASK:
342 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
343 		return (0);
344 
345 	case SIOCSIFDSTADDR:
346 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
347 			return (EINVAL);
348 		oldaddr = ia->ia_dstaddr;
349 		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
350 		if (ifp->if_ioctl &&
351 		    (error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
352 					      td->td_proc->p_ucred))) {
353 			ia->ia_dstaddr = oldaddr;
354 			return (error);
355 		}
356 		if (ia->ia_flags & IFA_ROUTE) {
357 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
358 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
359 			ia->ia_ifa.ifa_dstaddr =
360 					(struct sockaddr *)&ia->ia_dstaddr;
361 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
362 		}
363 		return (0);
364 
365 	case SIOCSIFBRDADDR:
366 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
367 			return (EINVAL);
368 		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
369 		return (0);
370 
371 	case SIOCSIFADDR:
372 		error = in_ifinit(ifp, ia,
373 		    (struct sockaddr_in *) &ifr->ifr_addr, 1);
374 		if (error != 0 && iaIsNew)
375 			break;
376 		if (error == 0)
377 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
378 		return (0);
379 
380 	case SIOCSIFNETMASK:
381 		ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
382 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
383 		return (0);
384 
385 	case SIOCAIFADDR:
386 		maskIsNew = 0;
387 		hostIsNew = 1;
388 		error = 0;
389 		if (ia->ia_addr.sin_family == AF_INET) {
390 			if (ifra->ifra_addr.sin_len == 0) {
391 				ifra->ifra_addr = ia->ia_addr;
392 				hostIsNew = 0;
393 			} else if (ifra->ifra_addr.sin_addr.s_addr ==
394 					       ia->ia_addr.sin_addr.s_addr)
395 				hostIsNew = 0;
396 		}
397 		if (ifra->ifra_mask.sin_len) {
398 			in_ifscrub(ifp, ia);
399 			ia->ia_sockmask = ifra->ifra_mask;
400 			ia->ia_sockmask.sin_family = AF_INET;
401 			ia->ia_subnetmask =
402 			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
403 			maskIsNew = 1;
404 		}
405 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
406 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
407 			in_ifscrub(ifp, ia);
408 			ia->ia_dstaddr = ifra->ifra_dstaddr;
409 			maskIsNew  = 1; /* We lie; but the effect's the same */
410 		}
411 		if (ifra->ifra_addr.sin_family == AF_INET &&
412 		    (hostIsNew || maskIsNew))
413 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
414 
415 		if (error != 0 && iaIsNew)
416 			break;
417 
418 		if ((ifp->if_flags & IFF_BROADCAST) &&
419 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
420 			ia->ia_broadaddr = ifra->ifra_broadaddr;
421 		if (error == 0)
422 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
423 		return (error);
424 
425 	case SIOCDIFADDR:
426 		/*
427 		 * in_ifscrub kills the interface route.
428 		 */
429 		in_ifscrub(ifp, ia);
430 		/*
431 		 * in_ifadown gets rid of all the rest of
432 		 * the routes.  This is not quite the right
433 		 * thing to do, but at least if we are running
434 		 * a routing process they will come back.
435 		 */
436 		in_ifadown(&ia->ia_ifa, 1);
437 		/*
438 		 * XXX horrible hack to detect that we are being called
439 		 * from if_detach()
440 		 */
441 		if (!ifnet_addrs[ifp->if_index - 1]) {
442 			in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
443 			in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);
444 		}
445 		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
446 		error = 0;
447 		break;
448 
449 	default:
450 		if (ifp == NULL || ifp->if_ioctl == NULL)
451 			return (EOPNOTSUPP);
452 		return ((*ifp->if_ioctl)(ifp, cmd, data, td->td_proc->p_ucred));
453 	}
454 
455 	/*
456 	 * Protect from ipintr() traversing address list while we're modifying
457 	 * it.
458 	 */
459 	s = splnet();
460 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
461 	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
462 	LIST_REMOVE(ia, ia_hash);
463 	IFAFREE(&ia->ia_ifa);
464 	splx(s);
465 
466 	return (error);
467 }
468 
469 /*
470  * SIOC[GAD]LIFADDR.
471  *	SIOCGLIFADDR: get first address. (?!?)
472  *	SIOCGLIFADDR with IFLR_PREFIX:
473  *		get first address that matches the specified prefix.
474  *	SIOCALIFADDR: add the specified address.
475  *	SIOCALIFADDR with IFLR_PREFIX:
476  *		EINVAL since we can't deduce hostid part of the address.
477  *	SIOCDLIFADDR: delete the specified address.
478  *	SIOCDLIFADDR with IFLR_PREFIX:
479  *		delete the first address that matches the specified prefix.
480  * return values:
481  *	EINVAL on invalid parameters
482  *	EADDRNOTAVAIL on prefix match failed/specified address not found
483  *	other values may be returned from in_ioctl()
484  *
485  * NOTE! td might be NULL.
486  */
487 static int
488 in_lifaddr_ioctl(so, cmd, data, ifp, td)
489 	struct socket *so;
490 	u_long cmd;
491 	caddr_t	data;
492 	struct ifnet *ifp;
493 	struct thread *td;
494 {
495 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
496 	struct ifaddr *ifa;
497 
498 	/* sanity checks */
499 	if (!data || !ifp) {
500 		panic("invalid argument to in_lifaddr_ioctl");
501 		/*NOTRECHED*/
502 	}
503 
504 	switch (cmd) {
505 	case SIOCGLIFADDR:
506 		/* address must be specified on GET with IFLR_PREFIX */
507 		if ((iflr->flags & IFLR_PREFIX) == 0)
508 			break;
509 		/*FALLTHROUGH*/
510 	case SIOCALIFADDR:
511 	case SIOCDLIFADDR:
512 		/* address must be specified on ADD and DELETE */
513 		if (iflr->addr.ss_family != AF_INET)
514 			return EINVAL;
515 		if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
516 			return EINVAL;
517 		/* XXX need improvement */
518 		if (iflr->dstaddr.ss_family
519 		 && iflr->dstaddr.ss_family != AF_INET)
520 			return EINVAL;
521 		if (iflr->dstaddr.ss_family
522 		 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
523 			return EINVAL;
524 		break;
525 	default: /*shouldn't happen*/
526 		return EOPNOTSUPP;
527 	}
528 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
529 		return EINVAL;
530 
531 	switch (cmd) {
532 	case SIOCALIFADDR:
533 	    {
534 		struct in_aliasreq ifra;
535 
536 		if (iflr->flags & IFLR_PREFIX)
537 			return EINVAL;
538 
539 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
540 		bzero(&ifra, sizeof(ifra));
541 		bcopy(iflr->iflr_name, ifra.ifra_name,
542 			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), (int)RTM_DELETE, RTF_HOST);
662 	else
663 		rtinit(&(ia->ia_ifa), (int)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 s = splimp(), flags = RTF_UP, error = 0;
681 
682 	oldaddr = ia->ia_addr;
683 	if (oldaddr.sin_family == AF_INET)
684 		LIST_REMOVE(ia, ia_hash);
685 	ia->ia_addr = *sin;
686 	if (ia->ia_addr.sin_family == AF_INET)
687 		LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
688 		    ia, ia_hash);
689 	/*
690 	 * Give the interface a chance to initialize
691 	 * if this is its first address,
692 	 * and to validate the address if necessary.
693 	 */
694 	if (ifp->if_ioctl &&
695 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia,
696 	    			      (struct ucred *)NULL))) {
697 		splx(s);
698 		/* LIST_REMOVE(ia, ia_hash) is done in in_control */
699 		ia->ia_addr = oldaddr;
700 		if (ia->ia_addr.sin_family == AF_INET)
701 			LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
702 			    ia, ia_hash);
703 		return (error);
704 	}
705 	splx(s);
706 	if (scrub) {
707 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
708 		in_ifscrub(ifp, ia);
709 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
710 	}
711 	if (IN_CLASSA(i))
712 		ia->ia_netmask = IN_CLASSA_NET;
713 	else if (IN_CLASSB(i))
714 		ia->ia_netmask = IN_CLASSB_NET;
715 	else
716 		ia->ia_netmask = IN_CLASSC_NET;
717 	/*
718 	 * The subnet mask usually includes at least the standard network part,
719 	 * but may may be smaller in the case of supernetting.
720 	 * If it is set, we believe it.
721 	 */
722 	if (ia->ia_subnetmask == 0) {
723 		ia->ia_subnetmask = ia->ia_netmask;
724 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
725 	} else
726 		ia->ia_netmask &= ia->ia_subnetmask;
727 	ia->ia_net = i & ia->ia_netmask;
728 	ia->ia_subnet = i & ia->ia_subnetmask;
729 	in_socktrim(&ia->ia_sockmask);
730 	/*
731 	 * Add route for the network.
732 	 */
733 	ia->ia_ifa.ifa_metric = ifp->if_metric;
734 	if (ifp->if_flags & IFF_BROADCAST) {
735 		ia->ia_broadaddr.sin_addr.s_addr =
736 			htonl(ia->ia_subnet | ~ia->ia_subnetmask);
737 		ia->ia_netbroadcast.s_addr =
738 			htonl(ia->ia_net | ~ ia->ia_netmask);
739 	} else if (ifp->if_flags & IFF_LOOPBACK) {
740 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
741 		flags |= RTF_HOST;
742 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
743 		if (ia->ia_dstaddr.sin_family != AF_INET)
744 			return (0);
745 		flags |= RTF_HOST;
746 	}
747 
748 	/*-
749 	 * Don't add host routes for interface addresses of
750 	 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0.  This makes it
751 	 * possible to assign several such address pairs with consistent
752 	 * results (no host route) and is required by BOOTP.
753 	 *
754 	 * XXX: This is ugly !  There should be a way for the caller to
755 	 *      say that they don't want a host route.
756 	 */
757 	if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
758 	    ia->ia_netmask != IN_CLASSA_NET ||
759 	    ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
760 		if ((error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags)) != 0) {
761 			ia->ia_addr = oldaddr;
762 			return (error);
763 		}
764 		ia->ia_flags |= IFA_ROUTE;
765 	}
766 
767 	/*
768 	 * If the interface supports multicast, join the "all hosts"
769 	 * multicast group on that interface.
770 	 */
771 	if (ifp->if_flags & IFF_MULTICAST) {
772 		struct in_addr addr;
773 
774 		addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
775 		in_addmulti(&addr, ifp);
776 	}
777 	return (error);
778 }
779 
780 
781 /*
782  * Return 1 if the address might be a local broadcast address.
783  */
784 int
785 in_broadcast(in, ifp)
786 	struct in_addr in;
787         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 	int s = splnet();
834 
835 	/*
836 	 * Call generic routine to add membership or increment
837 	 * refcount.  It wants addresses in the form of a sockaddr,
838 	 * so we build one here (being careful to zero the unused bytes).
839 	 */
840 	bzero(&sin, sizeof sin);
841 	sin.sin_family = AF_INET;
842 	sin.sin_len = sizeof sin;
843 	sin.sin_addr = *ap;
844 	error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
845 	if (error) {
846 		splx(s);
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 		splx(s);
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 	splx(s);
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 = inm->inm_ifma;
884 	struct in_multi my_inm;
885 	int s = splnet();
886 
887 	my_inm.inm_ifp = NULL ; /* don't send the leave msg */
888 	if (ifma->ifma_refcount == 1) {
889 		/*
890 		 * No remaining claims to this record; let IGMP know that
891 		 * we are leaving the multicast group.
892 		 * But do it after the if_delmulti() which might reset
893 		 * the interface and nuke the packet.
894 		 */
895 		my_inm = *inm ;
896 		ifma->ifma_protospec = 0;
897 		LIST_REMOVE(inm, inm_link);
898 		free(inm, M_IPMADDR);
899 	}
900 	/* XXX - should be separate API for when we have an ifma? */
901 	if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
902 	if (my_inm.inm_ifp != NULL)
903 		igmp_leavegroup(&my_inm);
904 	splx(s);
905 }
906