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