xref: /freebsd/sys/netinet/in.c (revision 4a5216a6dc0c3ce4cf5f2d3ee8af0c3ff3402c4f)
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  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_carp.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sockio.h>
41 #include <sys/malloc.h>
42 #include <sys/priv.h>
43 #include <sys/socket.h>
44 #include <sys/kernel.h>
45 #include <sys/sysctl.h>
46 #include <sys/vimage.h>
47 
48 #include <net/if.h>
49 #include <net/if_types.h>
50 #include <net/route.h>
51 
52 #include <netinet/in.h>
53 #include <netinet/in_var.h>
54 #include <netinet/in_pcb.h>
55 #include <netinet/ip_var.h>
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_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, subnets_are_local,
71 	CTLFLAG_RW, subnetsarelocal, 0,
72 	"Treat all subnets as directly connected");
73 static int sameprefixcarponly = 0;
74 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, same_prefix_carp_only,
75 	CTLFLAG_RW, sameprefixcarponly, 0,
76 	"Refuse to create same prefixes on different interfaces");
77 
78 extern struct inpcbinfo ripcbinfo;
79 extern struct inpcbinfo udbinfo;
80 
81 /*
82  * Return 1 if an internet address is for a ``local'' host
83  * (one to which we have a connection).  If subnetsarelocal
84  * is true, this includes other subnets of the local net.
85  * Otherwise, it includes only the directly-connected (sub)nets.
86  */
87 int
88 in_localaddr(struct in_addr in)
89 {
90 	INIT_VNET_INET(curvnet);
91 	register u_long i = ntohl(in.s_addr);
92 	register struct in_ifaddr *ia;
93 
94 	if (V_subnetsarelocal) {
95 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link)
96 			if ((i & ia->ia_netmask) == ia->ia_net)
97 				return (1);
98 	} else {
99 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link)
100 			if ((i & ia->ia_subnetmask) == ia->ia_subnet)
101 				return (1);
102 	}
103 	return (0);
104 }
105 
106 /*
107  * Return 1 if an internet address is for the local host and configured
108  * on one of its interfaces.
109  */
110 int
111 in_localip(struct in_addr in)
112 {
113 	INIT_VNET_INET(curvnet);
114 	struct in_ifaddr *ia;
115 
116 	LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
117 		if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
118 			return 1;
119 	}
120 	return 0;
121 }
122 
123 /*
124  * Determine whether an IP address is in a reserved set of addresses
125  * that may not be forwarded, or whether datagrams to that destination
126  * may be forwarded.
127  */
128 int
129 in_canforward(struct in_addr in)
130 {
131 	register u_long i = ntohl(in.s_addr);
132 	register u_long net;
133 
134 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
135 		return (0);
136 	if (IN_CLASSA(i)) {
137 		net = i & IN_CLASSA_NET;
138 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
139 			return (0);
140 	}
141 	return (1);
142 }
143 
144 /*
145  * Trim a mask in a sockaddr
146  */
147 static void
148 in_socktrim(struct sockaddr_in *ap)
149 {
150     register char *cplim = (char *) &ap->sin_addr;
151     register char *cp = (char *) (&ap->sin_addr + 1);
152 
153     ap->sin_len = 0;
154     while (--cp >= cplim)
155 	if (*cp) {
156 	    (ap)->sin_len = cp - (char *) (ap) + 1;
157 	    break;
158 	}
159 }
160 
161 static int
162 in_mask2len(mask)
163 	struct in_addr *mask;
164 {
165 	int x, y;
166 	u_char *p;
167 
168 	p = (u_char *)mask;
169 	for (x = 0; x < sizeof(*mask); x++) {
170 		if (p[x] != 0xff)
171 			break;
172 	}
173 	y = 0;
174 	if (x < sizeof(*mask)) {
175 		for (y = 0; y < 8; y++) {
176 			if ((p[x] & (0x80 >> y)) == 0)
177 				break;
178 		}
179 	}
180 	return x * 8 + y;
181 }
182 
183 static void
184 in_len2mask(struct in_addr *mask, int len)
185 {
186 	int i;
187 	u_char *p;
188 
189 	p = (u_char *)mask;
190 	bzero(mask, sizeof(*mask));
191 	for (i = 0; i < len / 8; i++)
192 		p[i] = 0xff;
193 	if (len % 8)
194 		p[i] = (0xff00 >> (len % 8)) & 0xff;
195 }
196 
197 /*
198  * Generic internet control operations (ioctl's).
199  * Ifp is 0 if not an interface-specific ioctl.
200  */
201 /* ARGSUSED */
202 int
203 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
204     struct thread *td)
205 {
206 	INIT_VNET_INET(curvnet); /* both so and ifp can be NULL here! */
207 	register struct ifreq *ifr = (struct ifreq *)data;
208 	register struct in_ifaddr *ia = 0, *iap;
209 	register struct ifaddr *ifa;
210 	struct in_addr allhosts_addr;
211 	struct in_addr dst;
212 	struct in_ifaddr *oia;
213 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
214 	struct sockaddr_in oldaddr;
215 	int error, hostIsNew, iaIsNew, maskIsNew, s;
216 	int iaIsFirst;
217 
218 	iaIsFirst = 0;
219 	iaIsNew = 0;
220 	allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
221 
222 	switch (cmd) {
223 	case SIOCALIFADDR:
224 		if (td != NULL) {
225 			error = priv_check(td, PRIV_NET_ADDIFADDR);
226 			if (error)
227 				return (error);
228 		}
229 		if (!ifp)
230 			return EINVAL;
231 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
232 
233 	case SIOCDLIFADDR:
234 		if (td != NULL) {
235 			error = priv_check(td, PRIV_NET_DELIFADDR);
236 			if (error)
237 				return (error);
238 		}
239 		if (!ifp)
240 			return EINVAL;
241 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
242 
243 	case SIOCGLIFADDR:
244 		if (!ifp)
245 			return EINVAL;
246 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
247 	}
248 
249 	/*
250 	 * Find address for this interface, if it exists.
251 	 *
252 	 * If an alias address was specified, find that one instead of
253 	 * the first one on the interface, if possible.
254 	 */
255 	if (ifp) {
256 		dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
257 		LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
258 			if (iap->ia_ifp == ifp &&
259 			    iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
260 				ia = iap;
261 				break;
262 			}
263 		if (ia == NULL)
264 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
265 				iap = ifatoia(ifa);
266 				if (iap->ia_addr.sin_family == AF_INET) {
267 					ia = iap;
268 					break;
269 				}
270 			}
271 		if (ia == NULL)
272 			iaIsFirst = 1;
273 	}
274 
275 	switch (cmd) {
276 
277 	case SIOCAIFADDR:
278 	case SIOCDIFADDR:
279 		if (ifp == 0)
280 			return (EADDRNOTAVAIL);
281 		if (ifra->ifra_addr.sin_family == AF_INET) {
282 			for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
283 				if (ia->ia_ifp == ifp  &&
284 				    ia->ia_addr.sin_addr.s_addr ==
285 				    ifra->ifra_addr.sin_addr.s_addr)
286 					break;
287 			}
288 			if ((ifp->if_flags & IFF_POINTOPOINT)
289 			    && (cmd == SIOCAIFADDR)
290 			    && (ifra->ifra_dstaddr.sin_addr.s_addr
291 				== INADDR_ANY)) {
292 				return EDESTADDRREQ;
293 			}
294 		}
295 		if (cmd == SIOCDIFADDR && ia == 0)
296 			return (EADDRNOTAVAIL);
297 		/* FALLTHROUGH */
298 	case SIOCSIFADDR:
299 	case SIOCSIFNETMASK:
300 	case SIOCSIFDSTADDR:
301 		if (td != NULL) {
302 			error = priv_check(td, (cmd == SIOCDIFADDR) ?
303 			    PRIV_NET_DELIFADDR : PRIV_NET_ADDIFADDR);
304 			if (error)
305 				return (error);
306 		}
307 
308 		if (ifp == 0)
309 			return (EADDRNOTAVAIL);
310 		if (ia == (struct in_ifaddr *)0) {
311 			ia = (struct in_ifaddr *)
312 				malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
313 			if (ia == (struct in_ifaddr *)NULL)
314 				return (ENOBUFS);
315 			/*
316 			 * Protect from ipintr() traversing address list
317 			 * while we're modifying it.
318 			 */
319 			s = splnet();
320 			ifa = &ia->ia_ifa;
321 			IFA_LOCK_INIT(ifa);
322 			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
323 			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
324 			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
325 			ifa->ifa_refcnt = 1;
326 			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
327 
328 			ia->ia_sockmask.sin_len = 8;
329 			ia->ia_sockmask.sin_family = AF_INET;
330 			if (ifp->if_flags & IFF_BROADCAST) {
331 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
332 				ia->ia_broadaddr.sin_family = AF_INET;
333 			}
334 			ia->ia_ifp = ifp;
335 
336 			TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
337 			splx(s);
338 			iaIsNew = 1;
339 		}
340 		break;
341 
342 	case SIOCSIFBRDADDR:
343 		if (td != NULL) {
344 			error = priv_check(td, PRIV_NET_ADDIFADDR);
345 			if (error)
346 				return (error);
347 		}
348 		/* FALLTHROUGH */
349 
350 	case SIOCGIFADDR:
351 	case SIOCGIFNETMASK:
352 	case SIOCGIFDSTADDR:
353 	case SIOCGIFBRDADDR:
354 		if (ia == (struct in_ifaddr *)0)
355 			return (EADDRNOTAVAIL);
356 		break;
357 	}
358 	switch (cmd) {
359 
360 	case SIOCGIFADDR:
361 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
362 		return (0);
363 
364 	case SIOCGIFBRDADDR:
365 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
366 			return (EINVAL);
367 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
368 		return (0);
369 
370 	case SIOCGIFDSTADDR:
371 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
372 			return (EINVAL);
373 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
374 		return (0);
375 
376 	case SIOCGIFNETMASK:
377 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
378 		return (0);
379 
380 	case SIOCSIFDSTADDR:
381 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
382 			return (EINVAL);
383 		oldaddr = ia->ia_dstaddr;
384 		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
385 		if (ifp->if_ioctl) {
386 			IFF_LOCKGIANT(ifp);
387 			error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
388 			    (caddr_t)ia);
389 			IFF_UNLOCKGIANT(ifp);
390 			if (error) {
391 				ia->ia_dstaddr = oldaddr;
392 				return (error);
393 			}
394 		}
395 		if (ia->ia_flags & IFA_ROUTE) {
396 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
397 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
398 			ia->ia_ifa.ifa_dstaddr =
399 					(struct sockaddr *)&ia->ia_dstaddr;
400 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
401 		}
402 		return (0);
403 
404 	case SIOCSIFBRDADDR:
405 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
406 			return (EINVAL);
407 		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
408 		return (0);
409 
410 	case SIOCSIFADDR:
411 		error = in_ifinit(ifp, ia,
412 		    (struct sockaddr_in *) &ifr->ifr_addr, 1);
413 		if (error != 0 && iaIsNew)
414 			break;
415 		if (error == 0) {
416 			if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
417 				in_addmulti(&allhosts_addr, ifp);
418 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
419 		}
420 		return (0);
421 
422 	case SIOCSIFNETMASK:
423 		ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
424 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
425 		return (0);
426 
427 	case SIOCAIFADDR:
428 		maskIsNew = 0;
429 		hostIsNew = 1;
430 		error = 0;
431 		if (ia->ia_addr.sin_family == AF_INET) {
432 			if (ifra->ifra_addr.sin_len == 0) {
433 				ifra->ifra_addr = ia->ia_addr;
434 				hostIsNew = 0;
435 			} else if (ifra->ifra_addr.sin_addr.s_addr ==
436 					       ia->ia_addr.sin_addr.s_addr)
437 				hostIsNew = 0;
438 		}
439 		if (ifra->ifra_mask.sin_len) {
440 			in_ifscrub(ifp, ia);
441 			ia->ia_sockmask = ifra->ifra_mask;
442 			ia->ia_sockmask.sin_family = AF_INET;
443 			ia->ia_subnetmask =
444 			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
445 			maskIsNew = 1;
446 		}
447 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
448 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
449 			in_ifscrub(ifp, ia);
450 			ia->ia_dstaddr = ifra->ifra_dstaddr;
451 			maskIsNew  = 1; /* We lie; but the effect's the same */
452 		}
453 		if (ifra->ifra_addr.sin_family == AF_INET &&
454 		    (hostIsNew || maskIsNew))
455 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
456 		if (error != 0 && iaIsNew)
457 			break;
458 
459 		if ((ifp->if_flags & IFF_BROADCAST) &&
460 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
461 			ia->ia_broadaddr = ifra->ifra_broadaddr;
462 		if (error == 0) {
463 			if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST) != 0)
464 				in_addmulti(&allhosts_addr, ifp);
465 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
466 		}
467 		return (error);
468 
469 	case SIOCDIFADDR:
470 		/*
471 		 * in_ifscrub kills the interface route.
472 		 */
473 		in_ifscrub(ifp, ia);
474 		/*
475 		 * in_ifadown gets rid of all the rest of
476 		 * the routes.  This is not quite the right
477 		 * thing to do, but at least if we are running
478 		 * a routing process they will come back.
479 		 */
480 		in_ifadown(&ia->ia_ifa, 1);
481 		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
482 		error = 0;
483 		break;
484 
485 	default:
486 		if (ifp == 0 || ifp->if_ioctl == 0)
487 			return (EOPNOTSUPP);
488 		IFF_LOCKGIANT(ifp);
489 		error = (*ifp->if_ioctl)(ifp, cmd, data);
490 		IFF_UNLOCKGIANT(ifp);
491 		return (error);
492 	}
493 
494 	/*
495 	 * Protect from ipintr() traversing address list while we're modifying
496 	 * it.
497 	 */
498 	s = splnet();
499 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
500 	TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
501 	if (ia->ia_addr.sin_family == AF_INET) {
502 		LIST_REMOVE(ia, ia_hash);
503 		/*
504 		 * If this is the last IPv4 address configured on this
505 		 * interface, leave the all-hosts group.
506 		 * XXX: This is quite ugly because of locking and structure.
507 		 */
508 		oia = NULL;
509 		IFP_TO_IA(ifp, oia);
510 		if (oia == NULL) {
511 			struct in_multi *inm;
512 
513 			IFF_LOCKGIANT(ifp);
514 			IN_MULTI_LOCK();
515 			IN_LOOKUP_MULTI(allhosts_addr, ifp, inm);
516 			if (inm != NULL)
517 				in_delmulti_locked(inm);
518 			IN_MULTI_UNLOCK();
519 			IFF_UNLOCKGIANT(ifp);
520 		}
521 	}
522 	IFAFREE(&ia->ia_ifa);
523 	splx(s);
524 
525 	return (error);
526 }
527 
528 /*
529  * SIOC[GAD]LIFADDR.
530  *	SIOCGLIFADDR: get first address. (?!?)
531  *	SIOCGLIFADDR with IFLR_PREFIX:
532  *		get first address that matches the specified prefix.
533  *	SIOCALIFADDR: add the specified address.
534  *	SIOCALIFADDR with IFLR_PREFIX:
535  *		EINVAL since we can't deduce hostid part of the address.
536  *	SIOCDLIFADDR: delete the specified address.
537  *	SIOCDLIFADDR with IFLR_PREFIX:
538  *		delete the first address that matches the specified prefix.
539  * return values:
540  *	EINVAL on invalid parameters
541  *	EADDRNOTAVAIL on prefix match failed/specified address not found
542  *	other values may be returned from in_ioctl()
543  */
544 static int
545 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
546     struct ifnet *ifp, struct thread *td)
547 {
548 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
549 	struct ifaddr *ifa;
550 
551 	/* sanity checks */
552 	if (!data || !ifp) {
553 		panic("invalid argument to in_lifaddr_ioctl");
554 		/*NOTRECHED*/
555 	}
556 
557 	switch (cmd) {
558 	case SIOCGLIFADDR:
559 		/* address must be specified on GET with IFLR_PREFIX */
560 		if ((iflr->flags & IFLR_PREFIX) == 0)
561 			break;
562 		/*FALLTHROUGH*/
563 	case SIOCALIFADDR:
564 	case SIOCDLIFADDR:
565 		/* address must be specified on ADD and DELETE */
566 		if (iflr->addr.ss_family != AF_INET)
567 			return EINVAL;
568 		if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
569 			return EINVAL;
570 		/* XXX need improvement */
571 		if (iflr->dstaddr.ss_family
572 		 && iflr->dstaddr.ss_family != AF_INET)
573 			return EINVAL;
574 		if (iflr->dstaddr.ss_family
575 		 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
576 			return EINVAL;
577 		break;
578 	default: /*shouldn't happen*/
579 		return EOPNOTSUPP;
580 	}
581 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
582 		return EINVAL;
583 
584 	switch (cmd) {
585 	case SIOCALIFADDR:
586 	    {
587 		struct in_aliasreq ifra;
588 
589 		if (iflr->flags & IFLR_PREFIX)
590 			return EINVAL;
591 
592 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
593 		bzero(&ifra, sizeof(ifra));
594 		bcopy(iflr->iflr_name, ifra.ifra_name,
595 			sizeof(ifra.ifra_name));
596 
597 		bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
598 
599 		if (iflr->dstaddr.ss_family) {	/*XXX*/
600 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
601 				iflr->dstaddr.ss_len);
602 		}
603 
604 		ifra.ifra_mask.sin_family = AF_INET;
605 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
606 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
607 
608 		return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
609 	    }
610 	case SIOCGLIFADDR:
611 	case SIOCDLIFADDR:
612 	    {
613 		struct in_ifaddr *ia;
614 		struct in_addr mask, candidate, match;
615 		struct sockaddr_in *sin;
616 
617 		bzero(&mask, sizeof(mask));
618 		bzero(&match, sizeof(match));
619 		if (iflr->flags & IFLR_PREFIX) {
620 			/* lookup a prefix rather than address. */
621 			in_len2mask(&mask, iflr->prefixlen);
622 
623 			sin = (struct sockaddr_in *)&iflr->addr;
624 			match.s_addr = sin->sin_addr.s_addr;
625 			match.s_addr &= mask.s_addr;
626 
627 			/* if you set extra bits, that's wrong */
628 			if (match.s_addr != sin->sin_addr.s_addr)
629 				return EINVAL;
630 
631 		} else {
632 			/* on getting an address, take the 1st match */
633 			/* on deleting an address, do exact match */
634 			if (cmd != SIOCGLIFADDR) {
635 				in_len2mask(&mask, 32);
636 				sin = (struct sockaddr_in *)&iflr->addr;
637 				match.s_addr = sin->sin_addr.s_addr;
638 			}
639 		}
640 
641 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)	{
642 			if (ifa->ifa_addr->sa_family != AF_INET6)
643 				continue;
644 			if (match.s_addr == 0)
645 				break;
646 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
647 			candidate.s_addr &= mask.s_addr;
648 			if (candidate.s_addr == match.s_addr)
649 				break;
650 		}
651 		if (!ifa)
652 			return EADDRNOTAVAIL;
653 		ia = (struct in_ifaddr *)ifa;
654 
655 		if (cmd == SIOCGLIFADDR) {
656 			/* fill in the if_laddrreq structure */
657 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
658 
659 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
660 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
661 					ia->ia_dstaddr.sin_len);
662 			} else
663 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
664 
665 			iflr->prefixlen =
666 				in_mask2len(&ia->ia_sockmask.sin_addr);
667 
668 			iflr->flags = 0;	/*XXX*/
669 
670 			return 0;
671 		} else {
672 			struct in_aliasreq ifra;
673 
674 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
675 			bzero(&ifra, sizeof(ifra));
676 			bcopy(iflr->iflr_name, ifra.ifra_name,
677 				sizeof(ifra.ifra_name));
678 
679 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
680 				ia->ia_addr.sin_len);
681 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
682 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
683 					ia->ia_dstaddr.sin_len);
684 			}
685 			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
686 				ia->ia_sockmask.sin_len);
687 
688 			return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
689 					  ifp, td);
690 		}
691 	    }
692 	}
693 
694 	return EOPNOTSUPP;	/*just for safety*/
695 }
696 
697 /*
698  * Delete any existing route for an interface.
699  */
700 void
701 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
702 {
703 
704 	in_scrubprefix(ia);
705 }
706 
707 /*
708  * Initialize an interface's internet address
709  * and routing table entry.
710  */
711 static int
712 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
713     int scrub)
714 {
715 	INIT_VNET_INET(ifp->if_vnet);
716 	register u_long i = ntohl(sin->sin_addr.s_addr);
717 	struct sockaddr_in oldaddr;
718 	int s = splimp(), flags = RTF_UP, error = 0;
719 
720 	oldaddr = ia->ia_addr;
721 	if (oldaddr.sin_family == AF_INET)
722 		LIST_REMOVE(ia, ia_hash);
723 	ia->ia_addr = *sin;
724 	if (ia->ia_addr.sin_family == AF_INET)
725 		LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
726 		    ia, ia_hash);
727 	/*
728 	 * Give the interface a chance to initialize
729 	 * if this is its first address,
730 	 * and to validate the address if necessary.
731 	 */
732 	if (ifp->if_ioctl) {
733 		IFF_LOCKGIANT(ifp);
734 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
735 		IFF_UNLOCKGIANT(ifp);
736 		if (error) {
737 			splx(s);
738 			/* LIST_REMOVE(ia, ia_hash) is done in in_control */
739 			ia->ia_addr = oldaddr;
740 			if (ia->ia_addr.sin_family == AF_INET)
741 				LIST_INSERT_HEAD(INADDR_HASH(
742 				    ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
743 			else
744 				/*
745 				 * If oldaddr family is not AF_INET (e.g.
746 				 * interface has been just created) in_control
747 				 * does not call LIST_REMOVE, and we end up
748 				 * with bogus ia entries in hash
749 				 */
750 				LIST_REMOVE(ia, ia_hash);
751 			return (error);
752 		}
753 	}
754 	splx(s);
755 	if (scrub) {
756 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
757 		in_ifscrub(ifp, ia);
758 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
759 	}
760 	if (IN_CLASSA(i))
761 		ia->ia_netmask = IN_CLASSA_NET;
762 	else if (IN_CLASSB(i))
763 		ia->ia_netmask = IN_CLASSB_NET;
764 	else
765 		ia->ia_netmask = IN_CLASSC_NET;
766 	/*
767 	 * The subnet mask usually includes at least the standard network part,
768 	 * but may may be smaller in the case of supernetting.
769 	 * If it is set, we believe it.
770 	 */
771 	if (ia->ia_subnetmask == 0) {
772 		ia->ia_subnetmask = ia->ia_netmask;
773 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
774 	} else
775 		ia->ia_netmask &= ia->ia_subnetmask;
776 	ia->ia_net = i & ia->ia_netmask;
777 	ia->ia_subnet = i & ia->ia_subnetmask;
778 	in_socktrim(&ia->ia_sockmask);
779 #ifdef DEV_CARP
780 	/*
781 	 * XXX: carp(4) does not have interface route
782 	 */
783 	if (ifp->if_type == IFT_CARP)
784 		return (0);
785 #endif
786 	/*
787 	 * Add route for the network.
788 	 */
789 	ia->ia_ifa.ifa_metric = ifp->if_metric;
790 	if (ifp->if_flags & IFF_BROADCAST) {
791 		ia->ia_broadaddr.sin_addr.s_addr =
792 			htonl(ia->ia_subnet | ~ia->ia_subnetmask);
793 		ia->ia_netbroadcast.s_addr =
794 			htonl(ia->ia_net | ~ ia->ia_netmask);
795 	} else if (ifp->if_flags & IFF_LOOPBACK) {
796 		ia->ia_dstaddr = ia->ia_addr;
797 		flags |= RTF_HOST;
798 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
799 		if (ia->ia_dstaddr.sin_family != AF_INET)
800 			return (0);
801 		flags |= RTF_HOST;
802 	}
803 	if ((error = in_addprefix(ia, flags)) != 0)
804 		return (error);
805 
806 	return (error);
807 }
808 
809 #define rtinitflags(x) \
810 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
811 	    ? RTF_HOST : 0)
812 /*
813  * Check if we have a route for the given prefix already or add one accordingly.
814  */
815 static int
816 in_addprefix(struct in_ifaddr *target, int flags)
817 {
818 	INIT_VNET_INET(curvnet);
819 	struct in_ifaddr *ia;
820 	struct in_addr prefix, mask, p, m;
821 	int error;
822 
823 	if ((flags & RTF_HOST) != 0) {
824 		prefix = target->ia_dstaddr.sin_addr;
825 		mask.s_addr = 0;
826 	} else {
827 		prefix = target->ia_addr.sin_addr;
828 		mask = target->ia_sockmask.sin_addr;
829 		prefix.s_addr &= mask.s_addr;
830 	}
831 
832 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
833 		if (rtinitflags(ia)) {
834 			p = ia->ia_addr.sin_addr;
835 
836 			if (prefix.s_addr != p.s_addr)
837 				continue;
838 		} else {
839 			p = ia->ia_addr.sin_addr;
840 			m = ia->ia_sockmask.sin_addr;
841 			p.s_addr &= m.s_addr;
842 
843 			if (prefix.s_addr != p.s_addr ||
844 			    mask.s_addr != m.s_addr)
845 				continue;
846 		}
847 
848 		/*
849 		 * If we got a matching prefix route inserted by other
850 		 * interface address, we are done here.
851 		 */
852 		if (ia->ia_flags & IFA_ROUTE) {
853 			if (V_sameprefixcarponly &&
854 			    target->ia_ifp->if_type != IFT_CARP &&
855 			    ia->ia_ifp->if_type != IFT_CARP)
856 				return (EEXIST);
857 			else
858 				return (0);
859 		}
860 	}
861 
862 	/*
863 	 * No-one seem to have this prefix route, so we try to insert it.
864 	 */
865 	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
866 	if (!error)
867 		target->ia_flags |= IFA_ROUTE;
868 	return error;
869 }
870 
871 /*
872  * If there is no other address in the system that can serve a route to the
873  * same prefix, remove the route.  Hand over the route to the new address
874  * otherwise.
875  */
876 static int
877 in_scrubprefix(struct in_ifaddr *target)
878 {
879 	INIT_VNET_INET(curvnet);
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, &V_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  * Delete all IPv4 multicast address records, and associated link-layer
981  * multicast address records, associated with ifp.
982  */
983 static void
984 in_purgemaddrs(struct ifnet *ifp)
985 {
986 	INIT_VNET_INET(ifp->if_vnet);
987 	struct in_multi *inm;
988 	struct in_multi *oinm;
989 
990 #ifdef DIAGNOSTIC
991 	printf("%s: purging ifp %p\n", __func__, ifp);
992 #endif
993 	IFF_LOCKGIANT(ifp);
994 	IN_MULTI_LOCK();
995 	LIST_FOREACH_SAFE(inm, &V_in_multihead, inm_link, oinm) {
996 		if (inm->inm_ifp == ifp)
997 			in_delmulti_locked(inm);
998 	}
999 	IN_MULTI_UNLOCK();
1000 	IFF_UNLOCKGIANT(ifp);
1001 }
1002 
1003 /*
1004  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1005  */
1006 void
1007 in_ifdetach(struct ifnet *ifp)
1008 {
1009 	INIT_VNET_INET(ifp->if_vnet);
1010 
1011 	in_pcbpurgeif0(&V_ripcbinfo, ifp);
1012 	in_pcbpurgeif0(&V_udbinfo, ifp);
1013 	in_purgemaddrs(ifp);
1014 }
1015