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