xref: /freebsd/sys/netinet/in.c (revision c68159a6d8eede11766cf13896d0f7670dbd51aa)
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 
52 #include <netinet/igmp_var.h>
53 
54 #include "gif.h"
55 #if NGIF > 0
56 #include <net/if_gif.h>
57 #endif
58 
59 static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
60 
61 static int in_mask2len __P((struct in_addr *));
62 static void in_len2mask __P((struct in_addr *, int));
63 static int in_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
64 	struct ifnet *, struct proc *));
65 
66 static void	in_socktrim __P((struct sockaddr_in *));
67 static int	in_ifinit __P((struct ifnet *,
68 	    struct in_ifaddr *, struct sockaddr_in *, int));
69 
70 static int subnetsarelocal = 0;
71 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
72 	&subnetsarelocal, 0, "");
73 
74 struct in_multihead in_multihead; /* XXX BSS initialization */
75 
76 /*
77  * Return 1 if an internet address is for a ``local'' host
78  * (one to which we have a connection).  If subnetsarelocal
79  * is true, this includes other subnets of the local net.
80  * Otherwise, it includes only the directly-connected (sub)nets.
81  */
82 int
83 in_localaddr(in)
84 	struct in_addr in;
85 {
86 	register u_long i = ntohl(in.s_addr);
87 	register struct in_ifaddr *ia;
88 
89 	if (subnetsarelocal) {
90 		for (ia = in_ifaddrhead.tqh_first; ia;
91 		     ia = ia->ia_link.tqe_next)
92 			if ((i & ia->ia_netmask) == ia->ia_net)
93 				return (1);
94 	} else {
95 		for (ia = in_ifaddrhead.tqh_first; ia;
96 		     ia = ia->ia_link.tqe_next)
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, p)
190 	struct socket *so;
191 	u_long cmd;
192 	caddr_t data;
193 	register struct ifnet *ifp;
194 	struct proc *p;
195 {
196 	register struct ifreq *ifr = (struct ifreq *)data;
197 	register struct in_ifaddr *ia = 0, *iap;
198 	register struct ifaddr *ifa;
199 	struct in_ifaddr *oia;
200 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
201 	struct sockaddr_in oldaddr;
202 	int error, hostIsNew, maskIsNew, s;
203 	u_long i;
204 
205 #if NGIF > 0
206         if (ifp && ifp->if_type == IFT_GIF) {
207                 switch (cmd) {
208                 case SIOCSIFPHYADDR:
209 		case SIOCDIFPHYADDR:
210 			if (p &&
211 			    (error = suser(p)) != 0)
212         			return(error);
213                 case SIOCGIFPSRCADDR:
214                 case SIOCGIFPDSTADDR:
215                         return gif_ioctl(ifp, cmd, data);
216                 }
217         }
218 #endif
219 
220 	switch (cmd) {
221 	case SIOCALIFADDR:
222 	case SIOCDLIFADDR:
223 		if (p && (error = suser(p)) != 0)
224 			return error;
225 		/*fall through*/
226 	case SIOCGLIFADDR:
227 		if (!ifp)
228 			return EINVAL;
229 		return in_lifaddr_ioctl(so, cmd, data, ifp, p);
230 	}
231 
232 	/*
233 	 * Find address for this interface, if it exists.
234 	 *
235 	 * If an alias address was specified, find that one instead of
236 	 * the first one on the interface.
237 	 */
238 	if (ifp)
239 		for (iap = in_ifaddrhead.tqh_first; iap;
240 		     iap = iap->ia_link.tqe_next)
241 			if (iap->ia_ifp == ifp) {
242 				if (((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr ==
243 				    iap->ia_addr.sin_addr.s_addr) {
244 					ia = iap;
245 					break;
246 				} else if (ia == NULL) {
247 					ia = iap;
248 					if (ifr->ifr_addr.sa_family != AF_INET)
249 						break;
250 				}
251 			}
252 
253 	switch (cmd) {
254 
255 	case SIOCAIFADDR:
256 	case SIOCDIFADDR:
257 		if (ifp == 0)
258 			return (EADDRNOTAVAIL);
259 		if (ifra->ifra_addr.sin_family == AF_INET) {
260 			for (oia = ia; ia; ia = ia->ia_link.tqe_next) {
261 				if (ia->ia_ifp == ifp  &&
262 				    ia->ia_addr.sin_addr.s_addr ==
263 				    ifra->ifra_addr.sin_addr.s_addr)
264 					break;
265 			}
266 			if ((ifp->if_flags & IFF_POINTOPOINT)
267 			    && (cmd == SIOCAIFADDR)
268 			    && (ifra->ifra_dstaddr.sin_addr.s_addr
269 				== INADDR_ANY)) {
270 				return EDESTADDRREQ;
271 			}
272 		}
273 		if (cmd == SIOCDIFADDR && ia == 0)
274 			return (EADDRNOTAVAIL);
275 		/* FALLTHROUGH */
276 	case SIOCSIFADDR:
277 	case SIOCSIFNETMASK:
278 	case SIOCSIFDSTADDR:
279 		if (p && (error = suser(p)) != 0)
280 			return error;
281 
282 		if (ifp == 0)
283 			return (EADDRNOTAVAIL);
284 		if (ia == (struct in_ifaddr *)0) {
285 			ia = (struct in_ifaddr *)
286 				malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
287 			if (ia == (struct in_ifaddr *)NULL)
288 				return (ENOBUFS);
289 			/*
290 			 * Protect from ipintr() traversing address list
291 			 * while we're modifying it.
292 			 */
293 			s = splnet();
294 
295 			TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
296 			ifa = &ia->ia_ifa;
297 			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
298 
299 			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
300 			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
301 			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
302 			ia->ia_sockmask.sin_len = 8;
303 			if (ifp->if_flags & IFF_BROADCAST) {
304 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
305 				ia->ia_broadaddr.sin_family = AF_INET;
306 			}
307 			ia->ia_ifp = ifp;
308 			if (!(ifp->if_flags & IFF_LOOPBACK))
309 				in_interfaces++;
310 			splx(s);
311 		}
312 		break;
313 
314 	case SIOCSIFBRDADDR:
315 		if (p && (error = suser(p)) != 0)
316 			return error;
317 		/* FALLTHROUGH */
318 
319 	case SIOCGIFADDR:
320 	case SIOCGIFNETMASK:
321 	case SIOCGIFDSTADDR:
322 	case SIOCGIFBRDADDR:
323 		if (ia == (struct in_ifaddr *)0)
324 			return (EADDRNOTAVAIL);
325 		break;
326 	}
327 	switch (cmd) {
328 
329 	case SIOCGIFADDR:
330 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
331 		break;
332 
333 	case SIOCGIFBRDADDR:
334 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
335 			return (EINVAL);
336 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
337 		break;
338 
339 	case SIOCGIFDSTADDR:
340 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
341 			return (EINVAL);
342 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
343 		break;
344 
345 	case SIOCGIFNETMASK:
346 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
347 		break;
348 
349 	case SIOCSIFDSTADDR:
350 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
351 			return (EINVAL);
352 		oldaddr = ia->ia_dstaddr;
353 		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
354 		if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
355 					(ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
356 			ia->ia_dstaddr = oldaddr;
357 			return (error);
358 		}
359 		if (ia->ia_flags & IFA_ROUTE) {
360 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
361 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
362 			ia->ia_ifa.ifa_dstaddr =
363 					(struct sockaddr *)&ia->ia_dstaddr;
364 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
365 		}
366 		break;
367 
368 	case SIOCSIFBRDADDR:
369 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
370 			return (EINVAL);
371 		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
372 		break;
373 
374 	case SIOCSIFADDR:
375 		return (in_ifinit(ifp, ia,
376 		    (struct sockaddr_in *) &ifr->ifr_addr, 1));
377 
378 	case SIOCSIFNETMASK:
379 		i = ifra->ifra_addr.sin_addr.s_addr;
380 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr = i);
381 		break;
382 
383 	case SIOCAIFADDR:
384 		maskIsNew = 0;
385 		hostIsNew = 1;
386 		error = 0;
387 		if (ia->ia_addr.sin_family == AF_INET) {
388 			if (ifra->ifra_addr.sin_len == 0) {
389 				ifra->ifra_addr = ia->ia_addr;
390 				hostIsNew = 0;
391 			} else if (ifra->ifra_addr.sin_addr.s_addr ==
392 					       ia->ia_addr.sin_addr.s_addr)
393 				hostIsNew = 0;
394 		}
395 		if (ifra->ifra_mask.sin_len) {
396 			in_ifscrub(ifp, ia);
397 			ia->ia_sockmask = ifra->ifra_mask;
398 			ia->ia_subnetmask =
399 			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
400 			maskIsNew = 1;
401 		}
402 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
403 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
404 			in_ifscrub(ifp, ia);
405 			ia->ia_dstaddr = ifra->ifra_dstaddr;
406 			maskIsNew  = 1; /* We lie; but the effect's the same */
407 		}
408 		if (ifra->ifra_addr.sin_family == AF_INET &&
409 		    (hostIsNew || maskIsNew))
410 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
411 		if ((ifp->if_flags & IFF_BROADCAST) &&
412 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
413 			ia->ia_broadaddr = ifra->ifra_broadaddr;
414 		return (error);
415 
416 	case SIOCDIFADDR:
417 		in_ifscrub(ifp, ia);
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, p)
458 	struct socket *so;
459 	u_long cmd;
460 	caddr_t	data;
461 	struct ifnet *ifp;
462 	struct proc *p;
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, p);
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, p);
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 
709 	/*
710 	 * If the interface supports multicast, join the "all hosts"
711 	 * multicast group on that interface.
712 	 */
713 	if (ifp->if_flags & IFF_MULTICAST) {
714 		struct in_addr addr;
715 
716 		addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
717 		in_addmulti(&addr, ifp);
718 	}
719 	return (error);
720 }
721 
722 
723 /*
724  * Return 1 if the address might be a local broadcast address.
725  */
726 int
727 in_broadcast(in, ifp)
728 	struct in_addr in;
729         struct ifnet *ifp;
730 {
731 	register struct ifaddr *ifa;
732 	u_long t;
733 
734 	if (in.s_addr == INADDR_BROADCAST ||
735 	    in.s_addr == INADDR_ANY)
736 		return 1;
737 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
738 		return 0;
739 	t = ntohl(in.s_addr);
740 	/*
741 	 * Look through the list of addresses for a match
742 	 * with a broadcast address.
743 	 */
744 #define ia ((struct in_ifaddr *)ifa)
745 	for (ifa = ifp->if_addrhead.tqh_first; ifa;
746 	     ifa = ifa->ifa_link.tqe_next)
747 		if (ifa->ifa_addr->sa_family == AF_INET &&
748 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
749 		     in.s_addr == ia->ia_netbroadcast.s_addr ||
750 		     /*
751 		      * Check for old-style (host 0) broadcast.
752 		      */
753 		     t == ia->ia_subnet || t == ia->ia_net) &&
754 		     /*
755 		      * Check for an all one subnetmask. These
756 		      * only exist when an interface gets a secondary
757 		      * address.
758 		      */
759 		     ia->ia_subnetmask != (u_long)0xffffffff)
760 			    return 1;
761 	return (0);
762 #undef ia
763 }
764 /*
765  * Add an address to the list of IP multicast addresses for a given interface.
766  */
767 struct in_multi *
768 in_addmulti(ap, ifp)
769 	register struct in_addr *ap;
770 	register struct ifnet *ifp;
771 {
772 	register struct in_multi *inm;
773 	int error;
774 	struct sockaddr_in sin;
775 	struct ifmultiaddr *ifma;
776 	int s = splnet();
777 
778 	/*
779 	 * Call generic routine to add membership or increment
780 	 * refcount.  It wants addresses in the form of a sockaddr,
781 	 * so we build one here (being careful to zero the unused bytes).
782 	 */
783 	bzero(&sin, sizeof sin);
784 	sin.sin_family = AF_INET;
785 	sin.sin_len = sizeof sin;
786 	sin.sin_addr = *ap;
787 	error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
788 	if (error) {
789 		splx(s);
790 		return 0;
791 	}
792 
793 	/*
794 	 * If ifma->ifma_protospec is null, then if_addmulti() created
795 	 * a new record.  Otherwise, we are done.
796 	 */
797 	if (ifma->ifma_protospec != 0) {
798 		splx(s);
799 		return ifma->ifma_protospec;
800 	}
801 
802 	/* XXX - if_addmulti uses M_WAITOK.  Can this really be called
803 	   at interrupt time?  If so, need to fix if_addmulti. XXX */
804 	inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR,
805 	    M_NOWAIT | M_ZERO);
806 	if (inm == NULL) {
807 		splx(s);
808 		return (NULL);
809 	}
810 
811 	inm->inm_addr = *ap;
812 	inm->inm_ifp = ifp;
813 	inm->inm_ifma = ifma;
814 	ifma->ifma_protospec = inm;
815 	LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
816 
817 	/*
818 	 * Let IGMP know that we have joined a new IP multicast group.
819 	 */
820 	igmp_joingroup(inm);
821 	splx(s);
822 	return (inm);
823 }
824 
825 /*
826  * Delete a multicast address record.
827  */
828 void
829 in_delmulti(inm)
830 	register struct in_multi *inm;
831 {
832 	struct ifmultiaddr *ifma = inm->inm_ifma;
833 	struct in_multi my_inm;
834 	int s = splnet();
835 
836 	my_inm.inm_ifp = NULL ; /* don't send the leave msg */
837 	if (ifma->ifma_refcount == 1) {
838 		/*
839 		 * No remaining claims to this record; let IGMP know that
840 		 * we are leaving the multicast group.
841 		 * But do it after the if_delmulti() which might reset
842 		 * the interface and nuke the packet.
843 		 */
844 		my_inm = *inm ;
845 		ifma->ifma_protospec = 0;
846 		LIST_REMOVE(inm, inm_link);
847 		free(inm, M_IPMADDR);
848 	}
849 	/* XXX - should be separate API for when we have an ifma? */
850 	if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
851 	if (my_inm.inm_ifp != NULL)
852 		igmp_leavegroup(&my_inm);
853 	splx(s);
854 }
855