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