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