xref: /freebsd/sys/netinet/in.c (revision 94942af266ac119ede0ca836f9aa5a5ac0582938)
1 /*-
2  * Copyright (c) 1982, 1986, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (C) 2001 WIDE Project.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *	@(#)in.c	8.4 (Berkeley) 1/9/95
31  * $FreeBSD$
32  */
33 
34 #include "opt_carp.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/sockio.h>
39 #include <sys/malloc.h>
40 #include <sys/priv.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(struct in_addr *);
58 static void in_len2mask(struct in_addr *, int);
59 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
60 	struct ifnet *, struct thread *);
61 
62 static int	in_addprefix(struct in_ifaddr *, int);
63 static int	in_scrubprefix(struct in_ifaddr *);
64 static void	in_socktrim(struct sockaddr_in *);
65 static int	in_ifinit(struct ifnet *,
66 	    struct in_ifaddr *, struct sockaddr_in *, int);
67 static void	in_purgemaddrs(struct ifnet *);
68 
69 static int subnetsarelocal = 0;
70 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
71 	&subnetsarelocal, 0, "Treat all subnets as directly connected");
72 static int sameprefixcarponly = 0;
73 SYSCTL_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW,
74 	&sameprefixcarponly, 0,
75 	"Refuse to create same prefixes on different interfaces");
76 
77 /*
78  * The IPv4 multicast list (in_multihead and associated structures) are
79  * protected by the global in_multi_mtx.  See in_var.h for more details.  For
80  * now, in_multi_mtx is marked as recursible due to IGMP's calling back into
81  * ip_output() to send IGMP packets while holding the lock; this probably is
82  * not quite desirable.
83  */
84 struct in_multihead in_multihead; /* XXX BSS initialization */
85 struct mtx in_multi_mtx;
86 MTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF | MTX_RECURSE);
87 
88 extern struct inpcbinfo ripcbinfo;
89 extern struct inpcbinfo udbinfo;
90 
91 /*
92  * Return 1 if an internet address is for a ``local'' host
93  * (one to which we have a connection).  If subnetsarelocal
94  * is true, this includes other subnets of the local net.
95  * Otherwise, it includes only the directly-connected (sub)nets.
96  */
97 int
98 in_localaddr(struct in_addr in)
99 {
100 	register u_long i = ntohl(in.s_addr);
101 	register struct in_ifaddr *ia;
102 
103 	if (subnetsarelocal) {
104 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
105 			if ((i & ia->ia_netmask) == ia->ia_net)
106 				return (1);
107 	} else {
108 		TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
109 			if ((i & ia->ia_subnetmask) == ia->ia_subnet)
110 				return (1);
111 	}
112 	return (0);
113 }
114 
115 /*
116  * Return 1 if an internet address is for the local host and configured
117  * on one of its interfaces.
118  */
119 int
120 in_localip(struct in_addr in)
121 {
122 	struct in_ifaddr *ia;
123 
124 	LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
125 		if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
126 			return 1;
127 	}
128 	return 0;
129 }
130 
131 /*
132  * Determine whether an IP address is in a reserved set of addresses
133  * that may not be forwarded, or whether datagrams to that destination
134  * may be forwarded.
135  */
136 int
137 in_canforward(struct in_addr in)
138 {
139 	register u_long i = ntohl(in.s_addr);
140 	register u_long net;
141 
142 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
143 		return (0);
144 	if (IN_CLASSA(i)) {
145 		net = i & IN_CLASSA_NET;
146 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
147 			return (0);
148 	}
149 	return (1);
150 }
151 
152 /*
153  * Trim a mask in a sockaddr
154  */
155 static void
156 in_socktrim(struct sockaddr_in *ap)
157 {
158     register char *cplim = (char *) &ap->sin_addr;
159     register char *cp = (char *) (&ap->sin_addr + 1);
160 
161     ap->sin_len = 0;
162     while (--cp >= cplim)
163 	if (*cp) {
164 	    (ap)->sin_len = cp - (char *) (ap) + 1;
165 	    break;
166 	}
167 }
168 
169 static int
170 in_mask2len(mask)
171 	struct in_addr *mask;
172 {
173 	int x, y;
174 	u_char *p;
175 
176 	p = (u_char *)mask;
177 	for (x = 0; x < sizeof(*mask); x++) {
178 		if (p[x] != 0xff)
179 			break;
180 	}
181 	y = 0;
182 	if (x < sizeof(*mask)) {
183 		for (y = 0; y < 8; y++) {
184 			if ((p[x] & (0x80 >> y)) == 0)
185 				break;
186 		}
187 	}
188 	return x * 8 + y;
189 }
190 
191 static void
192 in_len2mask(struct in_addr *mask, int len)
193 {
194 	int i;
195 	u_char *p;
196 
197 	p = (u_char *)mask;
198 	bzero(mask, sizeof(*mask));
199 	for (i = 0; i < len / 8; i++)
200 		p[i] = 0xff;
201 	if (len % 8)
202 		p[i] = (0xff00 >> (len % 8)) & 0xff;
203 }
204 
205 /*
206  * Generic internet control operations (ioctl's).
207  * Ifp is 0 if not an interface-specific ioctl.
208  */
209 /* ARGSUSED */
210 int
211 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
212     struct thread *td)
213 {
214 	register struct ifreq *ifr = (struct ifreq *)data;
215 	register struct in_ifaddr *ia = 0, *iap;
216 	register struct ifaddr *ifa;
217 	struct in_addr allhosts_addr;
218 	struct in_addr dst;
219 	struct in_ifaddr *oia;
220 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
221 	struct sockaddr_in oldaddr;
222 	int error, hostIsNew, iaIsNew, maskIsNew, s;
223 	int iaIsFirst;
224 
225 	iaIsFirst = 0;
226 	iaIsNew = 0;
227 	allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
228 
229 	switch (cmd) {
230 	case SIOCALIFADDR:
231 		if (td != NULL) {
232 			error = priv_check(td, PRIV_NET_ADDIFADDR);
233 			if (error)
234 				return (error);
235 		}
236 		if (!ifp)
237 			return EINVAL;
238 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
239 
240 	case SIOCDLIFADDR:
241 		if (td != NULL) {
242 			error = priv_check(td, PRIV_NET_DELIFADDR);
243 			if (error)
244 				return (error);
245 		}
246 		if (!ifp)
247 			return EINVAL;
248 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
249 
250 	case SIOCGLIFADDR:
251 		if (!ifp)
252 			return EINVAL;
253 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
254 	}
255 
256 	/*
257 	 * Find address for this interface, if it exists.
258 	 *
259 	 * If an alias address was specified, find that one instead of
260 	 * the first one on the interface, if possible.
261 	 */
262 	if (ifp) {
263 		dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
264 		LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
265 			if (iap->ia_ifp == ifp &&
266 			    iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
267 				ia = iap;
268 				break;
269 			}
270 		if (ia == NULL)
271 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
272 				iap = ifatoia(ifa);
273 				if (iap->ia_addr.sin_family == AF_INET) {
274 					ia = iap;
275 					break;
276 				}
277 			}
278 		if (ia == NULL)
279 			iaIsFirst = 1;
280 	}
281 
282 	switch (cmd) {
283 
284 	case SIOCAIFADDR:
285 	case SIOCDIFADDR:
286 		if (ifp == 0)
287 			return (EADDRNOTAVAIL);
288 		if (ifra->ifra_addr.sin_family == AF_INET) {
289 			for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
290 				if (ia->ia_ifp == ifp  &&
291 				    ia->ia_addr.sin_addr.s_addr ==
292 				    ifra->ifra_addr.sin_addr.s_addr)
293 					break;
294 			}
295 			if ((ifp->if_flags & IFF_POINTOPOINT)
296 			    && (cmd == SIOCAIFADDR)
297 			    && (ifra->ifra_dstaddr.sin_addr.s_addr
298 				== INADDR_ANY)) {
299 				return EDESTADDRREQ;
300 			}
301 		}
302 		if (cmd == SIOCDIFADDR && ia == 0)
303 			return (EADDRNOTAVAIL);
304 		/* FALLTHROUGH */
305 	case SIOCSIFADDR:
306 	case SIOCSIFNETMASK:
307 	case SIOCSIFDSTADDR:
308 		if (td != NULL) {
309 			error = priv_check(td, PRIV_NET_ADDIFADDR);
310 			if (error)
311 				return (error);
312 		}
313 
314 		if (ifp == 0)
315 			return (EADDRNOTAVAIL);
316 		if (ia == (struct in_ifaddr *)0) {
317 			ia = (struct in_ifaddr *)
318 				malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
319 			if (ia == (struct in_ifaddr *)NULL)
320 				return (ENOBUFS);
321 			/*
322 			 * Protect from ipintr() traversing address list
323 			 * while we're modifying it.
324 			 */
325 			s = splnet();
326 			ifa = &ia->ia_ifa;
327 			IFA_LOCK_INIT(ifa);
328 			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
329 			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
330 			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
331 			ifa->ifa_refcnt = 1;
332 			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
333 
334 			ia->ia_sockmask.sin_len = 8;
335 			ia->ia_sockmask.sin_family = AF_INET;
336 			if (ifp->if_flags & IFF_BROADCAST) {
337 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
338 				ia->ia_broadaddr.sin_family = AF_INET;
339 			}
340 			ia->ia_ifp = ifp;
341 
342 			TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
343 			splx(s);
344 			iaIsNew = 1;
345 		}
346 		break;
347 
348 	case SIOCSIFBRDADDR:
349 		if (td != NULL) {
350 			error = priv_check(td, PRIV_NET_ADDIFADDR);
351 			if (error)
352 				return (error);
353 		}
354 		/* FALLTHROUGH */
355 
356 	case SIOCGIFADDR:
357 	case SIOCGIFNETMASK:
358 	case SIOCGIFDSTADDR:
359 	case SIOCGIFBRDADDR:
360 		if (ia == (struct in_ifaddr *)0)
361 			return (EADDRNOTAVAIL);
362 		break;
363 	}
364 	switch (cmd) {
365 
366 	case SIOCGIFADDR:
367 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
368 		return (0);
369 
370 	case SIOCGIFBRDADDR:
371 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
372 			return (EINVAL);
373 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
374 		return (0);
375 
376 	case SIOCGIFDSTADDR:
377 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
378 			return (EINVAL);
379 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
380 		return (0);
381 
382 	case SIOCGIFNETMASK:
383 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
384 		return (0);
385 
386 	case SIOCSIFDSTADDR:
387 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
388 			return (EINVAL);
389 		oldaddr = ia->ia_dstaddr;
390 		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
391 		if (ifp->if_ioctl) {
392 			IFF_LOCKGIANT(ifp);
393 			error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
394 			    (caddr_t)ia);
395 			IFF_UNLOCKGIANT(ifp);
396 			if (error) {
397 				ia->ia_dstaddr = oldaddr;
398 				return (error);
399 			}
400 		}
401 		if (ia->ia_flags & IFA_ROUTE) {
402 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
403 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
404 			ia->ia_ifa.ifa_dstaddr =
405 					(struct sockaddr *)&ia->ia_dstaddr;
406 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
407 		}
408 		return (0);
409 
410 	case SIOCSIFBRDADDR:
411 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
412 			return (EINVAL);
413 		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
414 		return (0);
415 
416 	case SIOCSIFADDR:
417 		error = in_ifinit(ifp, ia,
418 		    (struct sockaddr_in *) &ifr->ifr_addr, 1);
419 		if (error != 0 && iaIsNew)
420 			break;
421 		if (error == 0) {
422 			if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
423 				in_addmulti(&allhosts_addr, ifp);
424 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
425 		}
426 		return (0);
427 
428 	case SIOCSIFNETMASK:
429 		ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
430 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
431 		return (0);
432 
433 	case SIOCAIFADDR:
434 		maskIsNew = 0;
435 		hostIsNew = 1;
436 		error = 0;
437 		if (ia->ia_addr.sin_family == AF_INET) {
438 			if (ifra->ifra_addr.sin_len == 0) {
439 				ifra->ifra_addr = ia->ia_addr;
440 				hostIsNew = 0;
441 			} else if (ifra->ifra_addr.sin_addr.s_addr ==
442 					       ia->ia_addr.sin_addr.s_addr)
443 				hostIsNew = 0;
444 		}
445 		if (ifra->ifra_mask.sin_len) {
446 			in_ifscrub(ifp, ia);
447 			ia->ia_sockmask = ifra->ifra_mask;
448 			ia->ia_sockmask.sin_family = AF_INET;
449 			ia->ia_subnetmask =
450 			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
451 			maskIsNew = 1;
452 		}
453 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
454 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
455 			in_ifscrub(ifp, ia);
456 			ia->ia_dstaddr = ifra->ifra_dstaddr;
457 			maskIsNew  = 1; /* We lie; but the effect's the same */
458 		}
459 		if (ifra->ifra_addr.sin_family == AF_INET &&
460 		    (hostIsNew || maskIsNew))
461 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
462 		if (error != 0 && iaIsNew)
463 			break;
464 
465 		if ((ifp->if_flags & IFF_BROADCAST) &&
466 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
467 			ia->ia_broadaddr = ifra->ifra_broadaddr;
468 		if (error == 0) {
469 			if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
470 				in_addmulti(&allhosts_addr, ifp);
471 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
472 		}
473 		return (error);
474 
475 	case SIOCDIFADDR:
476 		/*
477 		 * in_ifscrub kills the interface route.
478 		 */
479 		in_ifscrub(ifp, ia);
480 		/*
481 		 * in_ifadown gets rid of all the rest of
482 		 * the routes.  This is not quite the right
483 		 * thing to do, but at least if we are running
484 		 * a routing process they will come back.
485 		 */
486 		in_ifadown(&ia->ia_ifa, 1);
487 		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
488 		error = 0;
489 		break;
490 
491 	default:
492 		if (ifp == 0 || ifp->if_ioctl == 0)
493 			return (EOPNOTSUPP);
494 		IFF_LOCKGIANT(ifp);
495 		error = (*ifp->if_ioctl)(ifp, cmd, data);
496 		IFF_UNLOCKGIANT(ifp);
497 		return (error);
498 	}
499 
500 	/*
501 	 * Protect from ipintr() traversing address list while we're modifying
502 	 * it.
503 	 */
504 	s = splnet();
505 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
506 	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
507 	if (ia->ia_addr.sin_family == AF_INET) {
508 		LIST_REMOVE(ia, ia_hash);
509 		/*
510 		 * If this is the last IPv4 address configured on this
511 		 * interface, leave the all-hosts group.
512 		 * XXX: This is quite ugly because of locking and structure.
513 		 */
514 		oia = NULL;
515 		IFP_TO_IA(ifp, oia);
516 		if (oia == NULL) {
517 			struct in_multi *inm;
518 
519 			IFF_LOCKGIANT(ifp);
520 			IN_MULTI_LOCK();
521 			IN_LOOKUP_MULTI(allhosts_addr, ifp, inm);
522 			if (inm != NULL)
523 				in_delmulti_locked(inm);
524 			IN_MULTI_UNLOCK();
525 			IFF_UNLOCKGIANT(ifp);
526 		}
527 	}
528 	IFAFREE(&ia->ia_ifa);
529 	splx(s);
530 
531 	return (error);
532 }
533 
534 /*
535  * SIOC[GAD]LIFADDR.
536  *	SIOCGLIFADDR: get first address. (?!?)
537  *	SIOCGLIFADDR with IFLR_PREFIX:
538  *		get first address that matches the specified prefix.
539  *	SIOCALIFADDR: add the specified address.
540  *	SIOCALIFADDR with IFLR_PREFIX:
541  *		EINVAL since we can't deduce hostid part of the address.
542  *	SIOCDLIFADDR: delete the specified address.
543  *	SIOCDLIFADDR with IFLR_PREFIX:
544  *		delete the first address that matches the specified prefix.
545  * return values:
546  *	EINVAL on invalid parameters
547  *	EADDRNOTAVAIL on prefix match failed/specified address not found
548  *	other values may be returned from in_ioctl()
549  */
550 static int
551 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
552     struct ifnet *ifp, struct thread *td)
553 {
554 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
555 	struct ifaddr *ifa;
556 
557 	/* sanity checks */
558 	if (!data || !ifp) {
559 		panic("invalid argument to in_lifaddr_ioctl");
560 		/*NOTRECHED*/
561 	}
562 
563 	switch (cmd) {
564 	case SIOCGLIFADDR:
565 		/* address must be specified on GET with IFLR_PREFIX */
566 		if ((iflr->flags & IFLR_PREFIX) == 0)
567 			break;
568 		/*FALLTHROUGH*/
569 	case SIOCALIFADDR:
570 	case SIOCDLIFADDR:
571 		/* address must be specified on ADD and DELETE */
572 		if (iflr->addr.ss_family != AF_INET)
573 			return EINVAL;
574 		if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
575 			return EINVAL;
576 		/* XXX need improvement */
577 		if (iflr->dstaddr.ss_family
578 		 && iflr->dstaddr.ss_family != AF_INET)
579 			return EINVAL;
580 		if (iflr->dstaddr.ss_family
581 		 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
582 			return EINVAL;
583 		break;
584 	default: /*shouldn't happen*/
585 		return EOPNOTSUPP;
586 	}
587 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
588 		return EINVAL;
589 
590 	switch (cmd) {
591 	case SIOCALIFADDR:
592 	    {
593 		struct in_aliasreq ifra;
594 
595 		if (iflr->flags & IFLR_PREFIX)
596 			return EINVAL;
597 
598 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
599 		bzero(&ifra, sizeof(ifra));
600 		bcopy(iflr->iflr_name, ifra.ifra_name,
601 			sizeof(ifra.ifra_name));
602 
603 		bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
604 
605 		if (iflr->dstaddr.ss_family) {	/*XXX*/
606 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
607 				iflr->dstaddr.ss_len);
608 		}
609 
610 		ifra.ifra_mask.sin_family = AF_INET;
611 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
612 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
613 
614 		return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
615 	    }
616 	case SIOCGLIFADDR:
617 	case SIOCDLIFADDR:
618 	    {
619 		struct in_ifaddr *ia;
620 		struct in_addr mask, candidate, match;
621 		struct sockaddr_in *sin;
622 		int cmp;
623 
624 		bzero(&mask, sizeof(mask));
625 		if (iflr->flags & IFLR_PREFIX) {
626 			/* lookup a prefix rather than address. */
627 			in_len2mask(&mask, iflr->prefixlen);
628 
629 			sin = (struct sockaddr_in *)&iflr->addr;
630 			match.s_addr = sin->sin_addr.s_addr;
631 			match.s_addr &= mask.s_addr;
632 
633 			/* if you set extra bits, that's wrong */
634 			if (match.s_addr != sin->sin_addr.s_addr)
635 				return EINVAL;
636 
637 			cmp = 1;
638 		} else {
639 			if (cmd == SIOCGLIFADDR) {
640 				/* on getting an address, take the 1st match */
641 				cmp = 0;	/*XXX*/
642 			} else {
643 				/* on deleting an address, do exact match */
644 				in_len2mask(&mask, 32);
645 				sin = (struct sockaddr_in *)&iflr->addr;
646 				match.s_addr = sin->sin_addr.s_addr;
647 
648 				cmp = 1;
649 			}
650 		}
651 
652 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)	{
653 			if (ifa->ifa_addr->sa_family != AF_INET6)
654 				continue;
655 			if (!cmp)
656 				break;
657 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
658 			candidate.s_addr &= mask.s_addr;
659 			if (candidate.s_addr == match.s_addr)
660 				break;
661 		}
662 		if (!ifa)
663 			return EADDRNOTAVAIL;
664 		ia = (struct in_ifaddr *)ifa;
665 
666 		if (cmd == SIOCGLIFADDR) {
667 			/* fill in the if_laddrreq structure */
668 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
669 
670 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
671 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
672 					ia->ia_dstaddr.sin_len);
673 			} else
674 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
675 
676 			iflr->prefixlen =
677 				in_mask2len(&ia->ia_sockmask.sin_addr);
678 
679 			iflr->flags = 0;	/*XXX*/
680 
681 			return 0;
682 		} else {
683 			struct in_aliasreq ifra;
684 
685 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
686 			bzero(&ifra, sizeof(ifra));
687 			bcopy(iflr->iflr_name, ifra.ifra_name,
688 				sizeof(ifra.ifra_name));
689 
690 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
691 				ia->ia_addr.sin_len);
692 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
693 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
694 					ia->ia_dstaddr.sin_len);
695 			}
696 			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
697 				ia->ia_sockmask.sin_len);
698 
699 			return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
700 					  ifp, td);
701 		}
702 	    }
703 	}
704 
705 	return EOPNOTSUPP;	/*just for safety*/
706 }
707 
708 /*
709  * Delete any existing route for an interface.
710  */
711 void
712 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
713 {
714 
715 	in_scrubprefix(ia);
716 }
717 
718 /*
719  * Initialize an interface's internet address
720  * and routing table entry.
721  */
722 static int
723 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
724     int scrub)
725 {
726 	register u_long i = ntohl(sin->sin_addr.s_addr);
727 	struct sockaddr_in oldaddr;
728 	int s = splimp(), flags = RTF_UP, error = 0;
729 
730 	oldaddr = ia->ia_addr;
731 	if (oldaddr.sin_family == AF_INET)
732 		LIST_REMOVE(ia, ia_hash);
733 	ia->ia_addr = *sin;
734 	if (ia->ia_addr.sin_family == AF_INET)
735 		LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
736 		    ia, ia_hash);
737 	/*
738 	 * Give the interface a chance to initialize
739 	 * if this is its first address,
740 	 * and to validate the address if necessary.
741 	 */
742 	if (ifp->if_ioctl) {
743 		IFF_LOCKGIANT(ifp);
744 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
745 		IFF_UNLOCKGIANT(ifp);
746 		if (error) {
747 			splx(s);
748 			/* LIST_REMOVE(ia, ia_hash) is done in in_control */
749 			ia->ia_addr = oldaddr;
750 			if (ia->ia_addr.sin_family == AF_INET)
751 				LIST_INSERT_HEAD(INADDR_HASH(
752 				    ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
753 			return (error);
754 		}
755 	}
756 	splx(s);
757 	if (scrub) {
758 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
759 		in_ifscrub(ifp, ia);
760 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
761 	}
762 	if (IN_CLASSA(i))
763 		ia->ia_netmask = IN_CLASSA_NET;
764 	else if (IN_CLASSB(i))
765 		ia->ia_netmask = IN_CLASSB_NET;
766 	else
767 		ia->ia_netmask = IN_CLASSC_NET;
768 	/*
769 	 * The subnet mask usually includes at least the standard network part,
770 	 * but may may be smaller in the case of supernetting.
771 	 * If it is set, we believe it.
772 	 */
773 	if (ia->ia_subnetmask == 0) {
774 		ia->ia_subnetmask = ia->ia_netmask;
775 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
776 	} else
777 		ia->ia_netmask &= ia->ia_subnetmask;
778 	ia->ia_net = i & ia->ia_netmask;
779 	ia->ia_subnet = i & ia->ia_subnetmask;
780 	in_socktrim(&ia->ia_sockmask);
781 #ifdef DEV_CARP
782 	/*
783 	 * XXX: carp(4) does not have interface route
784 	 */
785 	if (ifp->if_type == IFT_CARP)
786 		return (0);
787 #endif
788 	/*
789 	 * Add route for the network.
790 	 */
791 	ia->ia_ifa.ifa_metric = ifp->if_metric;
792 	if (ifp->if_flags & IFF_BROADCAST) {
793 		ia->ia_broadaddr.sin_addr.s_addr =
794 			htonl(ia->ia_subnet | ~ia->ia_subnetmask);
795 		ia->ia_netbroadcast.s_addr =
796 			htonl(ia->ia_net | ~ ia->ia_netmask);
797 	} else if (ifp->if_flags & IFF_LOOPBACK) {
798 		ia->ia_dstaddr = ia->ia_addr;
799 		flags |= RTF_HOST;
800 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
801 		if (ia->ia_dstaddr.sin_family != AF_INET)
802 			return (0);
803 		flags |= RTF_HOST;
804 	}
805 	if ((error = in_addprefix(ia, flags)) != 0)
806 		return (error);
807 
808 	return (error);
809 }
810 
811 #define rtinitflags(x) \
812 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
813 	    ? RTF_HOST : 0)
814 /*
815  * Check if we have a route for the given prefix already or add a one
816  * accordingly.
817  */
818 static int
819 in_addprefix(struct in_ifaddr *target, int flags)
820 {
821 	struct in_ifaddr *ia;
822 	struct in_addr prefix, mask, p, m;
823 	int error;
824 
825 	if ((flags & RTF_HOST) != 0)
826 		prefix = target->ia_dstaddr.sin_addr;
827 	else {
828 		prefix = target->ia_addr.sin_addr;
829 		mask = target->ia_sockmask.sin_addr;
830 		prefix.s_addr &= mask.s_addr;
831 	}
832 
833 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
834 		if (rtinitflags(ia)) {
835 			p = ia->ia_addr.sin_addr;
836 
837 			if (prefix.s_addr != p.s_addr)
838 				continue;
839 		} else {
840 			p = ia->ia_addr.sin_addr;
841 			m = ia->ia_sockmask.sin_addr;
842 			p.s_addr &= m.s_addr;
843 
844 			if (prefix.s_addr != p.s_addr ||
845 			    mask.s_addr != m.s_addr)
846 				continue;
847 		}
848 
849 		/*
850 		 * If we got a matching prefix route inserted by other
851 		 * interface address, we are done here.
852 		 */
853 		if (ia->ia_flags & IFA_ROUTE) {
854 			if (sameprefixcarponly &&
855 			    target->ia_ifp->if_type != IFT_CARP &&
856 			    ia->ia_ifp->if_type != IFT_CARP)
857 				return (EEXIST);
858 			else
859 				return (0);
860 		}
861 	}
862 
863 	/*
864 	 * No-one seem to have this prefix route, so we try to insert it.
865 	 */
866 	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
867 	if (!error)
868 		target->ia_flags |= IFA_ROUTE;
869 	return error;
870 }
871 
872 /*
873  * If there is no other address in the system that can serve a route to the
874  * same prefix, remove the route.  Hand over the route to the new address
875  * otherwise.
876  */
877 static int
878 in_scrubprefix(struct in_ifaddr *target)
879 {
880 	struct in_ifaddr *ia;
881 	struct in_addr prefix, mask, p;
882 	int error;
883 
884 	if ((target->ia_flags & IFA_ROUTE) == 0)
885 		return 0;
886 
887 	if (rtinitflags(target))
888 		prefix = target->ia_dstaddr.sin_addr;
889 	else {
890 		prefix = target->ia_addr.sin_addr;
891 		mask = target->ia_sockmask.sin_addr;
892 		prefix.s_addr &= mask.s_addr;
893 	}
894 
895 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
896 		if (rtinitflags(ia))
897 			p = ia->ia_dstaddr.sin_addr;
898 		else {
899 			p = ia->ia_addr.sin_addr;
900 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
901 		}
902 
903 		if (prefix.s_addr != p.s_addr)
904 			continue;
905 
906 		/*
907 		 * If we got a matching prefix address, move IFA_ROUTE and
908 		 * the route itself to it.  Make sure that routing daemons
909 		 * get a heads-up.
910 		 *
911 		 * XXX: a special case for carp(4) interface
912 		 */
913 		if ((ia->ia_flags & IFA_ROUTE) == 0
914 #ifdef DEV_CARP
915 		    && (ia->ia_ifp->if_type != IFT_CARP)
916 #endif
917 							) {
918 			rtinit(&(target->ia_ifa), (int)RTM_DELETE,
919 			    rtinitflags(target));
920 			target->ia_flags &= ~IFA_ROUTE;
921 
922 			error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
923 			    rtinitflags(ia) | RTF_UP);
924 			if (error == 0)
925 				ia->ia_flags |= IFA_ROUTE;
926 			return error;
927 		}
928 	}
929 
930 	/*
931 	 * As no-one seem to have this prefix, we can remove the route.
932 	 */
933 	rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
934 	target->ia_flags &= ~IFA_ROUTE;
935 	return 0;
936 }
937 
938 #undef rtinitflags
939 
940 /*
941  * Return 1 if the address might be a local broadcast address.
942  */
943 int
944 in_broadcast(struct in_addr in, struct ifnet *ifp)
945 {
946 	register struct ifaddr *ifa;
947 	u_long t;
948 
949 	if (in.s_addr == INADDR_BROADCAST ||
950 	    in.s_addr == INADDR_ANY)
951 		return 1;
952 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
953 		return 0;
954 	t = ntohl(in.s_addr);
955 	/*
956 	 * Look through the list of addresses for a match
957 	 * with a broadcast address.
958 	 */
959 #define ia ((struct in_ifaddr *)ifa)
960 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
961 		if (ifa->ifa_addr->sa_family == AF_INET &&
962 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
963 		     in.s_addr == ia->ia_netbroadcast.s_addr ||
964 		     /*
965 		      * Check for old-style (host 0) broadcast.
966 		      */
967 		     t == ia->ia_subnet || t == ia->ia_net) &&
968 		     /*
969 		      * Check for an all one subnetmask. These
970 		      * only exist when an interface gets a secondary
971 		      * address.
972 		      */
973 		     ia->ia_subnetmask != (u_long)0xffffffff)
974 			    return 1;
975 	return (0);
976 #undef ia
977 }
978 
979 /*
980  * Add an address to the list of IP multicast addresses for a given interface.
981  */
982 struct in_multi *
983 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
984 {
985 	struct in_multi *inm;
986 
987 	inm = NULL;
988 
989 	IFF_LOCKGIANT(ifp);
990 	IN_MULTI_LOCK();
991 
992 	IN_LOOKUP_MULTI(*ap, ifp, inm);
993 	if (inm != NULL) {
994 		/*
995 		 * If we already joined this group, just bump the
996 		 * refcount and return it.
997 		 */
998 		KASSERT(inm->inm_refcount >= 1,
999 		    ("%s: bad refcount %d", __func__, inm->inm_refcount));
1000 		++inm->inm_refcount;
1001 	} else do {
1002 		struct sockaddr_in sin;
1003 		struct ifmultiaddr *ifma;
1004 		struct in_multi *ninm;
1005 		int error;
1006 
1007 		bzero(&sin, sizeof sin);
1008 		sin.sin_family = AF_INET;
1009 		sin.sin_len = sizeof(struct sockaddr_in);
1010 		sin.sin_addr = *ap;
1011 
1012 		/*
1013 		 * Check if a link-layer group is already associated
1014 		 * with this network-layer group on the given ifnet.
1015 		 * If so, bump the refcount on the existing network-layer
1016 		 * group association and return it.
1017 		 */
1018 		error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1019 		if (error)
1020 			break;
1021 		if (ifma->ifma_protospec != NULL) {
1022 			inm = (struct in_multi *)ifma->ifma_protospec;
1023 #ifdef INVARIANTS
1024 			if (inm->inm_ifma != ifma || inm->inm_ifp != ifp ||
1025 			    inm->inm_addr.s_addr != ap->s_addr)
1026 				panic("%s: ifma is inconsistent", __func__);
1027 #endif
1028 			++inm->inm_refcount;
1029 			break;
1030 		}
1031 
1032 		/*
1033 		 * A new membership is needed; construct it and
1034 		 * perform the IGMP join.
1035 		 */
1036 		ninm = malloc(sizeof(*ninm), M_IPMADDR, M_NOWAIT | M_ZERO);
1037 		if (ninm == NULL) {
1038 			if_delmulti_ifma(ifma);
1039 			break;
1040 		}
1041 		ninm->inm_addr = *ap;
1042 		ninm->inm_ifp = ifp;
1043 		ninm->inm_ifma = ifma;
1044 		ninm->inm_refcount = 1;
1045 		ifma->ifma_protospec = ninm;
1046 		LIST_INSERT_HEAD(&in_multihead, ninm, inm_link);
1047 
1048 		igmp_joingroup(ninm);
1049 
1050 		inm = ninm;
1051 	} while (0);
1052 
1053 	IN_MULTI_UNLOCK();
1054 	IFF_UNLOCKGIANT(ifp);
1055 
1056 	return (inm);
1057 }
1058 
1059 /*
1060  * Delete a multicast address record.
1061  * It is OK to call this routine if the underlying ifnet went away.
1062  *
1063  * XXX: To deal with the ifp going away, we cheat; the link-layer code in net
1064  * will set ifma_ifp to NULL when the associated ifnet instance is detached
1065  * from the system.
1066  * The only reason we need to violate layers and check ifma_ifp here at all
1067  * is because certain hardware drivers still require Giant to be held,
1068  * and it must always be taken before other locks.
1069  */
1070 void
1071 in_delmulti(struct in_multi *inm)
1072 {
1073 	struct ifnet *ifp;
1074 
1075 	KASSERT(inm->inm_ifma != NULL, ("%s: no ifma", __func__));
1076 	ifp = inm->inm_ifma->ifma_ifp;
1077 
1078 	if (ifp != NULL) {
1079 		/*
1080 		 * Sanity check that netinet's notion of ifp is the
1081 		 * same as net's.
1082 		 */
1083 		KASSERT(inm->inm_ifp == ifp, ("%s: bad ifp", __func__));
1084 		IFF_LOCKGIANT(ifp);
1085 	}
1086 
1087 	IN_MULTI_LOCK();
1088 	in_delmulti_locked(inm);
1089 	IN_MULTI_UNLOCK();
1090 
1091 	if (ifp != NULL)
1092 		IFF_UNLOCKGIANT(ifp);
1093 }
1094 
1095 /*
1096  * Delete a multicast address record, with locks held.
1097  *
1098  * It is OK to call this routine if the ifp went away.
1099  * Assumes that caller holds the IN_MULTI lock, and that
1100  * Giant was taken before other locks if required by the hardware.
1101  */
1102 void
1103 in_delmulti_locked(struct in_multi *inm)
1104 {
1105 	struct ifmultiaddr *ifma;
1106 
1107 	IN_MULTI_LOCK_ASSERT();
1108 	KASSERT(inm->inm_refcount >= 1, ("%s: freeing freed inm", __func__));
1109 
1110 	if (--inm->inm_refcount == 0) {
1111 		igmp_leavegroup(inm);
1112 
1113 		ifma = inm->inm_ifma;
1114 #ifdef DIAGNOSTIC
1115 		printf("%s: purging ifma %p\n", __func__, ifma);
1116 #endif
1117 		KASSERT(ifma->ifma_protospec == inm,
1118 		    ("%s: ifma_protospec != inm", __func__));
1119 		ifma->ifma_protospec = NULL;
1120 
1121 		LIST_REMOVE(inm, inm_link);
1122 		free(inm, M_IPMADDR);
1123 
1124 		if_delmulti_ifma(ifma);
1125 	}
1126 }
1127 
1128 /*
1129  * Delete all IPv4 multicast address records, and associated link-layer
1130  * multicast address records, associated with ifp.
1131  */
1132 static void
1133 in_purgemaddrs(struct ifnet *ifp)
1134 {
1135 	struct in_multi *inm;
1136 	struct in_multi *oinm;
1137 
1138 #ifdef DIAGNOSTIC
1139 	printf("%s: purging ifp %p\n", __func__, ifp);
1140 #endif
1141 	IFF_LOCKGIANT(ifp);
1142 	IN_MULTI_LOCK();
1143 	LIST_FOREACH_SAFE(inm, &in_multihead, inm_link, oinm) {
1144 		if (inm->inm_ifp == ifp)
1145 			in_delmulti_locked(inm);
1146 	}
1147 	IN_MULTI_UNLOCK();
1148 	IFF_UNLOCKGIANT(ifp);
1149 }
1150 
1151 /*
1152  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1153  */
1154 void
1155 in_ifdetach(struct ifnet *ifp)
1156 {
1157 
1158 	in_pcbpurgeif0(&ripcbinfo, ifp);
1159 	in_pcbpurgeif0(&udbinfo, ifp);
1160 	in_purgemaddrs(ifp);
1161 }
1162