xref: /dflybsd-src/sys/netinet/in.c (revision e7b4468ce80913950cd099c393f3ce6ece6fcb2c)
1 /*
2  * Copyright (c) 1982, 1986, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)in.c	8.4 (Berkeley) 1/9/95
34  * $FreeBSD: src/sys/netinet/in.c,v 1.44.2.14 2002/11/08 00:45:50 suz Exp $
35  * $DragonFly: src/sys/netinet/in.c,v 1.25 2008/01/03 11:41:05 sephe Exp $
36  */
37 
38 #include "opt_bootp.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/sockio.h>
42 #include <sys/malloc.h>
43 #include <sys/proc.h>
44 #include <sys/socket.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/thread2.h>
48 
49 #include <net/if.h>
50 #include <net/if_types.h>
51 #include <net/route.h>
52 
53 #include <netinet/in.h>
54 #include <netinet/in_var.h>
55 #include <netinet/in_pcb.h>
56 
57 #include <netinet/igmp_var.h>
58 
59 MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
60 
61 static int in_mask2len (struct in_addr *);
62 static void in_len2mask (struct in_addr *, int);
63 static int in_lifaddr_ioctl (struct socket *, u_long, caddr_t,
64 	struct ifnet *, struct thread *);
65 
66 static void	in_socktrim (struct sockaddr_in *);
67 static int	in_ifinit (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 extern struct inpcbinfo ripcbinfo;
77 extern struct inpcbinfo udbinfo;
78 
79 /*
80  * Return 1 if an internet address is for a ``local'' host
81  * (one to which we have a connection).  If subnetsarelocal
82  * is true, this includes other subnets of the local net.
83  * Otherwise, it includes only the directly-connected (sub)nets.
84  */
85 int
86 in_localaddr(struct in_addr in)
87 {
88 	u_long i = ntohl(in.s_addr);
89 	struct in_ifaddr *ia;
90 
91 	if (subnetsarelocal) {
92 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
93 			if ((i & ia->ia_netmask) == ia->ia_net)
94 				return (1);
95 	} else {
96 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
97 			if ((i & ia->ia_subnetmask) == ia->ia_subnet)
98 				return (1);
99 	}
100 	return (0);
101 }
102 
103 /*
104  * Determine whether an IP address is in a reserved set of addresses
105  * that may not be forwarded, or whether datagrams to that destination
106  * may be forwarded.
107  */
108 int
109 in_canforward(struct in_addr in)
110 {
111 	u_long i = ntohl(in.s_addr);
112 	u_long net;
113 
114 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
115 		return (0);
116 	if (IN_CLASSA(i)) {
117 		net = i & IN_CLASSA_NET;
118 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
119 			return (0);
120 	}
121 	return (1);
122 }
123 
124 /*
125  * Trim a mask in a sockaddr
126  */
127 static void
128 in_socktrim(struct sockaddr_in *ap)
129 {
130     char *cplim = (char *) &ap->sin_addr;
131     char *cp = (char *) (&ap->sin_addr + 1);
132 
133     ap->sin_len = 0;
134     while (--cp >= cplim)
135 	if (*cp) {
136 	    (ap)->sin_len = cp - (char *) (ap) + 1;
137 	    break;
138 	}
139 }
140 
141 static int
142 in_mask2len(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(struct in_addr *mask, int len)
164 {
165 	int i;
166 	u_char *p;
167 
168 	p = (u_char *)mask;
169 	bzero(mask, sizeof *mask);
170 	for (i = 0; i < len / 8; i++)
171 		p[i] = 0xff;
172 	if (len % 8)
173 		p[i] = (0xff00 >> (len % 8)) & 0xff;
174 }
175 
176 static int in_interfaces;	/* number of external internet interfaces */
177 
178 /*
179  * Generic internet control operations (ioctl's).
180  * Ifp is 0 if not an interface-specific ioctl.
181  *
182  * NOTE! td might be NULL.
183  */
184 /* ARGSUSED */
185 int
186 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
187 	   struct thread *td)
188 {
189 	struct ifreq *ifr = (struct ifreq *)data;
190 	struct in_ifaddr *ia = 0, *iap;
191 	struct ifaddr *ifa;
192 	struct in_addr dst;
193 	struct in_ifaddr *oia;
194 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
195 	struct sockaddr_in oldaddr;
196 	int hostIsNew, iaIsNew, maskIsNew;
197 	int error = 0;
198 
199 	iaIsNew = 0;
200 
201 	switch (cmd) {
202 	case SIOCALIFADDR:
203 	case SIOCDLIFADDR:
204 		if (td && (error = suser(td)) != 0)
205 			return error;
206 		/*fall through*/
207 	case SIOCGLIFADDR:
208 		if (!ifp)
209 			return EINVAL;
210 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
211 	}
212 
213 	/*
214 	 * Find address for this interface, if it exists.
215 	 *
216 	 * If an alias address was specified, find that one instead of
217 	 * the first one on the interface, if possible
218 	 */
219 	if (ifp) {
220 		dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
221 		LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
222 			if (iap->ia_ifp == ifp &&
223 			    iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
224 				ia = iap;
225 				break;
226 			}
227 		if (ia == NULL)
228 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
229 				iap = ifatoia(ifa);
230 				if (iap->ia_addr.sin_family == AF_INET) {
231 					ia = iap;
232 					break;
233 				}
234 			}
235 	}
236 
237 	switch (cmd) {
238 
239 	case SIOCAIFADDR:
240 	case SIOCDIFADDR:
241 		if (ifp == NULL)
242 			return (EADDRNOTAVAIL);
243 		if (ifra->ifra_addr.sin_family == AF_INET) {
244 			for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
245 				if (ia->ia_ifp == ifp  &&
246 				    ia->ia_addr.sin_addr.s_addr ==
247 				    ifra->ifra_addr.sin_addr.s_addr)
248 					break;
249 			}
250 			if ((ifp->if_flags & IFF_POINTOPOINT)
251 			    && (cmd == SIOCAIFADDR)
252 			    && (ifra->ifra_dstaddr.sin_addr.s_addr
253 				== INADDR_ANY)) {
254 				return EDESTADDRREQ;
255 			}
256 		}
257 		if (cmd == SIOCDIFADDR && ia == NULL)
258 			return (EADDRNOTAVAIL);
259 		/* FALLTHROUGH */
260 	case SIOCSIFADDR:
261 	case SIOCSIFNETMASK:
262 	case SIOCSIFDSTADDR:
263 		if (td && (error = suser(td)) != 0)
264 			return error;
265 
266 		if (ifp == NULL)
267 			return (EADDRNOTAVAIL);
268 		if (ia == NULL) {
269 			ia = kmalloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
270 
271 			/*
272 			 * Protect from NETISR_IP traversing address list
273 			 * while we're modifying it.
274 			 */
275 			crit_enter();
276 
277 			TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
278 			ifa = &ia->ia_ifa;
279 			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
280 
281 			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
282 			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
283 			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
284 			ia->ia_sockmask.sin_len = 8;
285 			ia->ia_sockmask.sin_family = AF_INET;
286 			if (ifp->if_flags & IFF_BROADCAST) {
287 				ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
288 				ia->ia_broadaddr.sin_family = AF_INET;
289 			}
290 			ia->ia_ifp = ifp;
291 			if (!(ifp->if_flags & IFF_LOOPBACK))
292 				in_interfaces++;
293 			iaIsNew = 1;
294 			crit_exit();
295 		}
296 		break;
297 
298 	case SIOCSIFBRDADDR:
299 		if (td && (error = suser(td)) != 0)
300 			return error;
301 		/* FALLTHROUGH */
302 
303 	case SIOCGIFADDR:
304 	case SIOCGIFNETMASK:
305 	case SIOCGIFDSTADDR:
306 	case SIOCGIFBRDADDR:
307 		if (ia == NULL)
308 			return (EADDRNOTAVAIL);
309 		break;
310 	}
311 	switch (cmd) {
312 
313 	case SIOCGIFADDR:
314 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
315 		return (0);
316 
317 	case SIOCGIFBRDADDR:
318 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
319 			return (EINVAL);
320 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
321 		return (0);
322 
323 	case SIOCGIFDSTADDR:
324 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
325 			return (EINVAL);
326 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
327 		return (0);
328 
329 	case SIOCGIFNETMASK:
330 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
331 		return (0);
332 
333 	case SIOCSIFDSTADDR:
334 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
335 			return (EINVAL);
336 		oldaddr = ia->ia_dstaddr;
337 		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
338 		lwkt_serialize_enter(ifp->if_serializer);
339 		if (ifp->if_ioctl &&
340 		    (error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
341 					      td->td_proc->p_ucred))) {
342 			ia->ia_dstaddr = oldaddr;
343 			lwkt_serialize_exit(ifp->if_serializer);
344 			return (error);
345 		}
346 		if (ia->ia_flags & IFA_ROUTE) {
347 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
348 			rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
349 			ia->ia_ifa.ifa_dstaddr =
350 					(struct sockaddr *)&ia->ia_dstaddr;
351 			rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
352 		}
353 		lwkt_serialize_exit(ifp->if_serializer);
354 		return (0);
355 
356 	case SIOCSIFBRDADDR:
357 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
358 			return (EINVAL);
359 		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
360 		return (0);
361 
362 	case SIOCSIFADDR:
363 		error = in_ifinit(ifp, ia,
364 		    (struct sockaddr_in *) &ifr->ifr_addr, 1);
365 		if (error != 0 && iaIsNew)
366 			break;
367 		if (error == 0)
368 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
369 		return (0);
370 
371 	case SIOCSIFNETMASK:
372 		ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
373 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
374 		return (0);
375 
376 	case SIOCAIFADDR:
377 		maskIsNew = 0;
378 		hostIsNew = 1;
379 		error = 0;
380 		if (ia->ia_addr.sin_family == AF_INET) {
381 			if (ifra->ifra_addr.sin_len == 0) {
382 				ifra->ifra_addr = ia->ia_addr;
383 				hostIsNew = 0;
384 			} else if (ifra->ifra_addr.sin_addr.s_addr ==
385 					       ia->ia_addr.sin_addr.s_addr)
386 				hostIsNew = 0;
387 		}
388 		if (ifra->ifra_mask.sin_len) {
389 			in_ifscrub(ifp, ia);
390 			ia->ia_sockmask = ifra->ifra_mask;
391 			ia->ia_sockmask.sin_family = AF_INET;
392 			ia->ia_subnetmask =
393 			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
394 			maskIsNew = 1;
395 		}
396 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
397 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
398 			in_ifscrub(ifp, ia);
399 			ia->ia_dstaddr = ifra->ifra_dstaddr;
400 			maskIsNew  = 1; /* We lie; but the effect's the same */
401 		}
402 		if (ifra->ifra_addr.sin_family == AF_INET &&
403 		    (hostIsNew || maskIsNew))
404 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
405 
406 		if (error != 0 && iaIsNew)
407 			break;
408 
409 		if ((ifp->if_flags & IFF_BROADCAST) &&
410 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
411 			ia->ia_broadaddr = ifra->ifra_broadaddr;
412 		if (error == 0)
413 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
414 		return (error);
415 
416 	case SIOCDIFADDR:
417 		/*
418 		 * in_ifscrub kills the interface route.
419 		 */
420 		in_ifscrub(ifp, ia);
421 		/*
422 		 * in_ifadown gets rid of all the rest of
423 		 * the routes.  This is not quite the right
424 		 * thing to do, but at least if we are running
425 		 * a routing process they will come back.
426 		 */
427 		in_ifadown(&ia->ia_ifa, 1);
428 		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
429 		error = 0;
430 		break;
431 
432 	default:
433 		if (ifp == NULL || ifp->if_ioctl == NULL)
434 			return (EOPNOTSUPP);
435 		lwkt_serialize_enter(ifp->if_serializer);
436 		error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
437 		lwkt_serialize_exit(ifp->if_serializer);
438 		return (error);
439 	}
440 
441 	/*
442 	 * Protect from NETISR_IP traversing address list while we're modifying
443 	 * it.
444 	 */
445 	lwkt_serialize_enter(ifp->if_serializer);
446 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
447 	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
448 	LIST_REMOVE(ia, ia_hash);
449 	IFAFREE(&ia->ia_ifa);
450 	lwkt_serialize_exit(ifp->if_serializer);
451 
452 	return (error);
453 }
454 
455 /*
456  * SIOC[GAD]LIFADDR.
457  *	SIOCGLIFADDR: get first address. (?!?)
458  *	SIOCGLIFADDR with IFLR_PREFIX:
459  *		get first address that matches the specified prefix.
460  *	SIOCALIFADDR: add the specified address.
461  *	SIOCALIFADDR with IFLR_PREFIX:
462  *		EINVAL since we can't deduce hostid part of the address.
463  *	SIOCDLIFADDR: delete the specified address.
464  *	SIOCDLIFADDR with IFLR_PREFIX:
465  *		delete the first address that matches the specified prefix.
466  * return values:
467  *	EINVAL on invalid parameters
468  *	EADDRNOTAVAIL on prefix match failed/specified address not found
469  *	other values may be returned from in_ioctl()
470  *
471  * NOTE! td might be NULL.
472  */
473 static int
474 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
475 		 struct thread *td)
476 {
477 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
478 	struct ifaddr *ifa;
479 
480 	/* sanity checks */
481 	if (!data || !ifp) {
482 		panic("invalid argument to in_lifaddr_ioctl");
483 		/*NOTRECHED*/
484 	}
485 
486 	switch (cmd) {
487 	case SIOCGLIFADDR:
488 		/* address must be specified on GET with IFLR_PREFIX */
489 		if ((iflr->flags & IFLR_PREFIX) == 0)
490 			break;
491 		/*FALLTHROUGH*/
492 	case SIOCALIFADDR:
493 	case SIOCDLIFADDR:
494 		/* address must be specified on ADD and DELETE */
495 		if (iflr->addr.ss_family != AF_INET)
496 			return EINVAL;
497 		if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
498 			return EINVAL;
499 		/* XXX need improvement */
500 		if (iflr->dstaddr.ss_family
501 		 && iflr->dstaddr.ss_family != AF_INET)
502 			return EINVAL;
503 		if (iflr->dstaddr.ss_family
504 		 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
505 			return EINVAL;
506 		break;
507 	default: /*shouldn't happen*/
508 		return EOPNOTSUPP;
509 	}
510 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
511 		return EINVAL;
512 
513 	switch (cmd) {
514 	case SIOCALIFADDR:
515 	    {
516 		struct in_aliasreq ifra;
517 
518 		if (iflr->flags & IFLR_PREFIX)
519 			return EINVAL;
520 
521 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
522 		bzero(&ifra, sizeof ifra);
523 		bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
524 
525 		bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
526 
527 		if (iflr->dstaddr.ss_family) {	/*XXX*/
528 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
529 				iflr->dstaddr.ss_len);
530 		}
531 
532 		ifra.ifra_mask.sin_family = AF_INET;
533 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
534 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
535 
536 		return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
537 	    }
538 	case SIOCGLIFADDR:
539 	case SIOCDLIFADDR:
540 	    {
541 		struct in_ifaddr *ia;
542 		struct in_addr mask, candidate, match;
543 		struct sockaddr_in *sin;
544 		int cmp;
545 
546 		bzero(&mask, sizeof mask);
547 		if (iflr->flags & IFLR_PREFIX) {
548 			/* lookup a prefix rather than address. */
549 			in_len2mask(&mask, iflr->prefixlen);
550 
551 			sin = (struct sockaddr_in *)&iflr->addr;
552 			match.s_addr = sin->sin_addr.s_addr;
553 			match.s_addr &= mask.s_addr;
554 
555 			/* if you set extra bits, that's wrong */
556 			if (match.s_addr != sin->sin_addr.s_addr)
557 				return EINVAL;
558 
559 			cmp = 1;
560 		} else {
561 			if (cmd == SIOCGLIFADDR) {
562 				/* on getting an address, take the 1st match */
563 				match.s_addr = 0; /* gcc4 warning */
564 				cmp = 0;	/*XXX*/
565 			} else {
566 				/* on deleting an address, do exact match */
567 				in_len2mask(&mask, 32);
568 				sin = (struct sockaddr_in *)&iflr->addr;
569 				match.s_addr = sin->sin_addr.s_addr;
570 
571 				cmp = 1;
572 			}
573 		}
574 
575 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)	{
576 			if (ifa->ifa_addr->sa_family != AF_INET6)
577 				continue;
578 			if (!cmp)
579 				break;
580 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
581 			candidate.s_addr &= mask.s_addr;
582 			if (candidate.s_addr == match.s_addr)
583 				break;
584 		}
585 		if (!ifa)
586 			return EADDRNOTAVAIL;
587 		ia = (struct in_ifaddr *)ifa;
588 
589 		if (cmd == SIOCGLIFADDR) {
590 			/* fill in the if_laddrreq structure */
591 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
592 
593 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
594 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
595 					ia->ia_dstaddr.sin_len);
596 			} else
597 				bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
598 
599 			iflr->prefixlen =
600 				in_mask2len(&ia->ia_sockmask.sin_addr);
601 
602 			iflr->flags = 0;	/*XXX*/
603 
604 			return 0;
605 		} else {
606 			struct in_aliasreq ifra;
607 
608 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
609 			bzero(&ifra, sizeof ifra);
610 			bcopy(iflr->iflr_name, ifra.ifra_name,
611 				sizeof ifra.ifra_name);
612 
613 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
614 				ia->ia_addr.sin_len);
615 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
616 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
617 					ia->ia_dstaddr.sin_len);
618 			}
619 			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
620 				ia->ia_sockmask.sin_len);
621 
622 			return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
623 					  ifp, td);
624 		}
625 	    }
626 	}
627 
628 	return EOPNOTSUPP;	/*just for safety*/
629 }
630 
631 /*
632  * Delete any existing route for an interface.
633  */
634 void
635 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
636 {
637 
638 	if ((ia->ia_flags & IFA_ROUTE) == 0)
639 		return;
640 	if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
641 		rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
642 	else
643 		rtinit(&ia->ia_ifa, RTM_DELETE, 0);
644 	ia->ia_flags &= ~IFA_ROUTE;
645 }
646 
647 /*
648  * Initialize an interface's internet address
649  * and routing table entry.
650  */
651 static int
652 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin, int scrub)
653 {
654 	u_long i = ntohl(sin->sin_addr.s_addr);
655 	struct sockaddr_in oldaddr;
656 	int flags = RTF_UP, error = 0;
657 
658 	lwkt_serialize_enter(ifp->if_serializer);
659 
660 	oldaddr = ia->ia_addr;
661 	if (oldaddr.sin_family == AF_INET)
662 		LIST_REMOVE(ia, ia_hash);
663 	ia->ia_addr = *sin;
664 	if (ia->ia_addr.sin_family == AF_INET)
665 		LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
666 		    ia, ia_hash);
667 	/*
668 	 * Give the interface a chance to initialize
669 	 * if this is its first address,
670 	 * and to validate the address if necessary.
671 	 */
672 	if (ifp->if_ioctl &&
673 	    (error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL))) {
674 		lwkt_serialize_exit(ifp->if_serializer);
675 		/* LIST_REMOVE(ia, ia_hash) is done in in_control */
676 		ia->ia_addr = oldaddr;
677 		if (ia->ia_addr.sin_family == AF_INET)
678 			LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
679 			    ia, ia_hash);
680 		return (error);
681 	}
682 	lwkt_serialize_exit(ifp->if_serializer);
683 	if (scrub) {
684 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
685 		in_ifscrub(ifp, ia);
686 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
687 	}
688 	if (IN_CLASSA(i))
689 		ia->ia_netmask = IN_CLASSA_NET;
690 	else if (IN_CLASSB(i))
691 		ia->ia_netmask = IN_CLASSB_NET;
692 	else
693 		ia->ia_netmask = IN_CLASSC_NET;
694 	/*
695 	 * The subnet mask usually includes at least the standard network part,
696 	 * but may may be smaller in the case of supernetting.
697 	 * If it is set, we believe it.
698 	 */
699 	if (ia->ia_subnetmask == 0) {
700 		ia->ia_subnetmask = ia->ia_netmask;
701 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
702 	} else
703 		ia->ia_netmask &= ia->ia_subnetmask;
704 	ia->ia_net = i & ia->ia_netmask;
705 	ia->ia_subnet = i & ia->ia_subnetmask;
706 	in_socktrim(&ia->ia_sockmask);
707 	/*
708 	 * Add route for the network.
709 	 */
710 	ia->ia_ifa.ifa_metric = ifp->if_metric;
711 	if (ifp->if_flags & IFF_BROADCAST) {
712 		ia->ia_broadaddr.sin_addr.s_addr =
713 			htonl(ia->ia_subnet | ~ia->ia_subnetmask);
714 		ia->ia_netbroadcast.s_addr =
715 			htonl(ia->ia_net | ~ ia->ia_netmask);
716 	} else if (ifp->if_flags & IFF_LOOPBACK) {
717 		ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
718 		flags |= RTF_HOST;
719 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
720 		if (ia->ia_dstaddr.sin_family != AF_INET)
721 			return (0);
722 		flags |= RTF_HOST;
723 	}
724 
725 	/*-
726 	 * Don't add host routes for interface addresses of
727 	 * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0.  This makes it
728 	 * possible to assign several such address pairs with consistent
729 	 * results (no host route) and is required by BOOTP.
730 	 *
731 	 * XXX: This is ugly !  There should be a way for the caller to
732 	 *      say that they don't want a host route.
733 	 */
734 	if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
735 	    ia->ia_netmask != IN_CLASSA_NET ||
736 	    ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
737 		if ((error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags)) != 0) {
738 			ia->ia_addr = oldaddr;
739 			return (error);
740 		}
741 		ia->ia_flags |= IFA_ROUTE;
742 	}
743 
744 	/*
745 	 * If the interface supports multicast, join the "all hosts"
746 	 * multicast group on that interface.
747 	 */
748 	if (ifp->if_flags & IFF_MULTICAST) {
749 		struct in_addr addr;
750 
751 		addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
752 		in_addmulti(&addr, ifp);
753 	}
754 	return (error);
755 }
756 
757 
758 /*
759  * Return 1 if the address might be a local broadcast address.
760  */
761 int
762 in_broadcast(struct in_addr in, struct ifnet *ifp)
763 {
764 	struct ifaddr *ifa;
765 	u_long t;
766 
767 	if (in.s_addr == INADDR_BROADCAST ||
768 	    in.s_addr == INADDR_ANY)
769 		return 1;
770 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
771 		return 0;
772 	t = ntohl(in.s_addr);
773 	/*
774 	 * Look through the list of addresses for a match
775 	 * with a broadcast address.
776 	 */
777 #define ia ((struct in_ifaddr *)ifa)
778 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
779 		if (ifa->ifa_addr->sa_family == AF_INET &&
780 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
781 		     in.s_addr == ia->ia_netbroadcast.s_addr ||
782 		     /*
783 		      * Check for old-style (host 0) broadcast.
784 		      */
785 		     t == ia->ia_subnet || t == ia->ia_net) &&
786 		     /*
787 		      * Check for an all one subnetmask. These
788 		      * only exist when an interface gets a secondary
789 		      * address.
790 		      */
791 		     ia->ia_subnetmask != (u_long)0xffffffff)
792 			    return 1;
793 	return (0);
794 #undef ia
795 }
796 /*
797  * Add an address to the list of IP multicast addresses for a given interface.
798  */
799 struct in_multi *
800 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
801 {
802 	struct in_multi *inm;
803 	int error;
804 	struct sockaddr_in sin;
805 	struct ifmultiaddr *ifma;
806 
807 	/*
808 	 * Call generic routine to add membership or increment
809 	 * refcount.  It wants addresses in the form of a sockaddr,
810 	 * so we build one here (being careful to zero the unused bytes).
811 	 */
812 	bzero(&sin, sizeof sin);
813 	sin.sin_family = AF_INET;
814 	sin.sin_len = sizeof sin;
815 	sin.sin_addr = *ap;
816 	crit_enter();
817 	error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
818 	if (error) {
819 		crit_exit();
820 		return 0;
821 	}
822 
823 	/*
824 	 * If ifma->ifma_protospec is null, then if_addmulti() created
825 	 * a new record.  Otherwise, we are done.
826 	 */
827 	if (ifma->ifma_protospec != 0) {
828 		crit_exit();
829 		return ifma->ifma_protospec;
830 	}
831 
832 	/* XXX - if_addmulti uses M_WAITOK.  Can this really be called
833 	   at interrupt time?  If so, need to fix if_addmulti. XXX */
834 	inm = kmalloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
835 	inm->inm_addr = *ap;
836 	inm->inm_ifp = ifp;
837 	inm->inm_ifma = ifma;
838 	ifma->ifma_protospec = inm;
839 	LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
840 
841 	/*
842 	 * Let IGMP know that we have joined a new IP multicast group.
843 	 */
844 	igmp_joingroup(inm);
845 	crit_exit();
846 	return (inm);
847 }
848 
849 /*
850  * Delete a multicast address record.
851  */
852 void
853 in_delmulti(struct in_multi *inm)
854 {
855 	struct ifmultiaddr *ifma;
856 	struct in_multi my_inm;
857 
858 	crit_enter();
859 	ifma = inm->inm_ifma;
860 	my_inm.inm_ifp = NULL ; /* don't send the leave msg */
861 	if (ifma->ifma_refcount == 1) {
862 		/*
863 		 * No remaining claims to this record; let IGMP know that
864 		 * we are leaving the multicast group.
865 		 * But do it after the if_delmulti() which might reset
866 		 * the interface and nuke the packet.
867 		 */
868 		my_inm = *inm ;
869 		ifma->ifma_protospec = 0;
870 		LIST_REMOVE(inm, inm_link);
871 		kfree(inm, M_IPMADDR);
872 	}
873 	/* XXX - should be separate API for when we have an ifma? */
874 	if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
875 	if (my_inm.inm_ifp != NULL)
876 		igmp_leavegroup(&my_inm);
877 	crit_exit();
878 }
879 
880 void
881 in_ifdetach(struct ifnet *ifp)
882 {
883 	in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
884 	in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);
885 }
886