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