xref: /freebsd/sys/netinet/in.c (revision acd3428b7d3e94cef0e1881c868cb4b131d4ff41)
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 
68 static int subnetsarelocal = 0;
69 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
70 	&subnetsarelocal, 0, "Treat all subnets as directly connected");
71 static int sameprefixcarponly = 0;
72 SYSCTL_INT(_net_inet_ip, OID_AUTO, same_prefix_carp_only, CTLFLAG_RW,
73 	&sameprefixcarponly, 0,
74 	"Refuse to create same prefixes on different interfaces");
75 
76 /*
77  * The IPv4 multicast list (in_multihead and associated structures) are
78  * protected by the global in_multi_mtx.  See in_var.h for more details.  For
79  * now, in_multi_mtx is marked as recursible due to IGMP's calling back into
80  * ip_output() to send IGMP packets while holding the lock; this probably is
81  * not quite desirable.
82  */
83 struct in_multihead in_multihead; /* XXX BSS initialization */
84 struct mtx in_multi_mtx;
85 MTX_SYSINIT(in_multi_mtx, &in_multi_mtx, "in_multi_mtx", MTX_DEF | MTX_RECURSE);
86 
87 extern struct inpcbinfo ripcbinfo;
88 extern struct inpcbinfo udbinfo;
89 
90 /*
91  * Return 1 if an internet address is for a ``local'' host
92  * (one to which we have a connection).  If subnetsarelocal
93  * is true, this includes other subnets of the local net.
94  * Otherwise, it includes only the directly-connected (sub)nets.
95  */
96 int
97 in_localaddr(in)
98 	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(in)
121 	struct in_addr in;
122 {
123 	struct in_ifaddr *ia;
124 
125 	LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
126 		if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
127 			return 1;
128 	}
129 	return 0;
130 }
131 
132 /*
133  * Determine whether an IP address is in a reserved set of addresses
134  * that may not be forwarded, or whether datagrams to that destination
135  * may be forwarded.
136  */
137 int
138 in_canforward(in)
139 	struct in_addr in;
140 {
141 	register u_long i = ntohl(in.s_addr);
142 	register u_long net;
143 
144 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
145 		return (0);
146 	if (IN_CLASSA(i)) {
147 		net = i & IN_CLASSA_NET;
148 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
149 			return (0);
150 	}
151 	return (1);
152 }
153 
154 /*
155  * Trim a mask in a sockaddr
156  */
157 static void
158 in_socktrim(ap)
159 struct sockaddr_in *ap;
160 {
161     register char *cplim = (char *) &ap->sin_addr;
162     register char *cp = (char *) (&ap->sin_addr + 1);
163 
164     ap->sin_len = 0;
165     while (--cp >= cplim)
166 	if (*cp) {
167 	    (ap)->sin_len = cp - (char *) (ap) + 1;
168 	    break;
169 	}
170 }
171 
172 static int
173 in_mask2len(mask)
174 	struct in_addr *mask;
175 {
176 	int x, y;
177 	u_char *p;
178 
179 	p = (u_char *)mask;
180 	for (x = 0; x < sizeof(*mask); x++) {
181 		if (p[x] != 0xff)
182 			break;
183 	}
184 	y = 0;
185 	if (x < sizeof(*mask)) {
186 		for (y = 0; y < 8; y++) {
187 			if ((p[x] & (0x80 >> y)) == 0)
188 				break;
189 		}
190 	}
191 	return x * 8 + y;
192 }
193 
194 static void
195 in_len2mask(mask, len)
196 	struct in_addr *mask;
197 	int len;
198 {
199 	int i;
200 	u_char *p;
201 
202 	p = (u_char *)mask;
203 	bzero(mask, sizeof(*mask));
204 	for (i = 0; i < len / 8; i++)
205 		p[i] = 0xff;
206 	if (len % 8)
207 		p[i] = (0xff00 >> (len % 8)) & 0xff;
208 }
209 
210 /*
211  * Generic internet control operations (ioctl's).
212  * Ifp is 0 if not an interface-specific ioctl.
213  */
214 /* ARGSUSED */
215 int
216 in_control(so, cmd, data, ifp, td)
217 	struct socket *so;
218 	u_long cmd;
219 	caddr_t data;
220 	register struct ifnet *ifp;
221 	struct thread *td;
222 {
223 	register struct ifreq *ifr = (struct ifreq *)data;
224 	register struct in_ifaddr *ia = 0, *iap;
225 	register struct ifaddr *ifa;
226 	struct in_addr dst;
227 	struct in_ifaddr *oia;
228 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
229 	struct sockaddr_in oldaddr;
230 	int error, hostIsNew, iaIsNew, maskIsNew, s;
231 
232 	iaIsNew = 0;
233 
234 	switch (cmd) {
235 	case SIOCALIFADDR:
236 		if (td != NULL) {
237 			error = priv_check(td, PRIV_NET_ADDIFADDR);
238 			if (error)
239 				return (error);
240 		}
241 		if (!ifp)
242 			return EINVAL;
243 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
244 
245 	case SIOCDLIFADDR:
246 		if (td != NULL) {
247 			error = priv_check(td, PRIV_NET_DELIFADDR);
248 			if (error)
249 				return (error);
250 		}
251 		if (!ifp)
252 			return EINVAL;
253 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
254 
255 	case SIOCGLIFADDR:
256 		if (!ifp)
257 			return EINVAL;
258 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
259 	}
260 
261 	/*
262 	 * Find address for this interface, if it exists.
263 	 *
264 	 * If an alias address was specified, find that one instead of
265 	 * the first one on the interface, if possible.
266 	 */
267 	if (ifp) {
268 		dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
269 		LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
270 			if (iap->ia_ifp == ifp &&
271 			    iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
272 				ia = iap;
273 				break;
274 			}
275 		if (ia == NULL)
276 			TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
277 				iap = ifatoia(ifa);
278 				if (iap->ia_addr.sin_family == AF_INET) {
279 					ia = iap;
280 					break;
281 				}
282 			}
283 	}
284 
285 	switch (cmd) {
286 
287 	case SIOCAIFADDR:
288 	case SIOCDIFADDR:
289 		if (ifp == 0)
290 			return (EADDRNOTAVAIL);
291 		if (ifra->ifra_addr.sin_family == AF_INET) {
292 			for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
293 				if (ia->ia_ifp == ifp  &&
294 				    ia->ia_addr.sin_addr.s_addr ==
295 				    ifra->ifra_addr.sin_addr.s_addr)
296 					break;
297 			}
298 			if ((ifp->if_flags & IFF_POINTOPOINT)
299 			    && (cmd == SIOCAIFADDR)
300 			    && (ifra->ifra_dstaddr.sin_addr.s_addr
301 				== INADDR_ANY)) {
302 				return EDESTADDRREQ;
303 			}
304 		}
305 		if (cmd == SIOCDIFADDR && ia == 0)
306 			return (EADDRNOTAVAIL);
307 		/* FALLTHROUGH */
308 	case SIOCSIFADDR:
309 	case SIOCSIFNETMASK:
310 	case SIOCSIFDSTADDR:
311 		if (td != NULL) {
312 			error = priv_check(td, PRIV_NET_ADDIFADDR);
313 			if (error)
314 				return (error);
315 		}
316 
317 		if (ifp == 0)
318 			return (EADDRNOTAVAIL);
319 		if (ia == (struct in_ifaddr *)0) {
320 			ia = (struct in_ifaddr *)
321 				malloc(sizeof *ia, M_IFADDR, M_WAITOK | M_ZERO);
322 			if (ia == (struct in_ifaddr *)NULL)
323 				return (ENOBUFS);
324 			/*
325 			 * Protect from ipintr() traversing address list
326 			 * while we're modifying it.
327 			 */
328 			s = splnet();
329 			ifa = &ia->ia_ifa;
330 			IFA_LOCK_INIT(ifa);
331 			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
332 			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
333 			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
334 			ifa->ifa_refcnt = 1;
335 			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
336 
337 			ia->ia_sockmask.sin_len = 8;
338 			ia->ia_sockmask.sin_family = AF_INET;
339 			if (ifp->if_flags & IFF_BROADCAST) {
340 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
341 				ia->ia_broadaddr.sin_family = AF_INET;
342 			}
343 			ia->ia_ifp = ifp;
344 
345 			TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
346 			splx(s);
347 			iaIsNew = 1;
348 		}
349 		break;
350 
351 	case SIOCSIFBRDADDR:
352 		if (td != NULL) {
353 			error = priv_check(td, PRIV_NET_ADDIFADDR);
354 			if (error)
355 				return (error);
356 		}
357 		/* FALLTHROUGH */
358 
359 	case SIOCGIFADDR:
360 	case SIOCGIFNETMASK:
361 	case SIOCGIFDSTADDR:
362 	case SIOCGIFBRDADDR:
363 		if (ia == (struct in_ifaddr *)0)
364 			return (EADDRNOTAVAIL);
365 		break;
366 	}
367 	switch (cmd) {
368 
369 	case SIOCGIFADDR:
370 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
371 		return (0);
372 
373 	case SIOCGIFBRDADDR:
374 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
375 			return (EINVAL);
376 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
377 		return (0);
378 
379 	case SIOCGIFDSTADDR:
380 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
381 			return (EINVAL);
382 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
383 		return (0);
384 
385 	case SIOCGIFNETMASK:
386 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
387 		return (0);
388 
389 	case SIOCSIFDSTADDR:
390 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
391 			return (EINVAL);
392 		oldaddr = ia->ia_dstaddr;
393 		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
394 		if (ifp->if_ioctl) {
395 			IFF_LOCKGIANT(ifp);
396 			error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
397 			    (caddr_t)ia);
398 			IFF_UNLOCKGIANT(ifp);
399 			if (error) {
400 				ia->ia_dstaddr = oldaddr;
401 				return (error);
402 			}
403 		}
404 		if (ia->ia_flags & IFA_ROUTE) {
405 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
406 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
407 			ia->ia_ifa.ifa_dstaddr =
408 					(struct sockaddr *)&ia->ia_dstaddr;
409 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
410 		}
411 		return (0);
412 
413 	case SIOCSIFBRDADDR:
414 		if ((ifp->if_flags & IFF_BROADCAST) == 0)
415 			return (EINVAL);
416 		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
417 		return (0);
418 
419 	case SIOCSIFADDR:
420 		error = in_ifinit(ifp, ia,
421 		    (struct sockaddr_in *) &ifr->ifr_addr, 1);
422 		if (error != 0 && iaIsNew)
423 			break;
424 		if (error == 0)
425 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
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 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
470 		return (error);
471 
472 	case SIOCDIFADDR:
473 		/*
474 		 * in_ifscrub kills the interface route.
475 		 */
476 		in_ifscrub(ifp, ia);
477 		/*
478 		 * in_ifadown gets rid of all the rest of
479 		 * the routes.  This is not quite the right
480 		 * thing to do, but at least if we are running
481 		 * a routing process they will come back.
482 		 */
483 		in_ifadown(&ia->ia_ifa, 1);
484 		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
485 		error = 0;
486 		break;
487 
488 	default:
489 		if (ifp == 0 || ifp->if_ioctl == 0)
490 			return (EOPNOTSUPP);
491 		IFF_LOCKGIANT(ifp);
492 		error = (*ifp->if_ioctl)(ifp, cmd, data);
493 		IFF_UNLOCKGIANT(ifp);
494 		return (error);
495 	}
496 
497 	/*
498 	 * Protect from ipintr() traversing address list while we're modifying
499 	 * it.
500 	 */
501 	s = splnet();
502 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
503 	TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
504 	if (ia->ia_addr.sin_family == AF_INET)
505 		LIST_REMOVE(ia, ia_hash);
506 	IFAFREE(&ia->ia_ifa);
507 	splx(s);
508 
509 	return (error);
510 }
511 
512 /*
513  * SIOC[GAD]LIFADDR.
514  *	SIOCGLIFADDR: get first address. (?!?)
515  *	SIOCGLIFADDR with IFLR_PREFIX:
516  *		get first address that matches the specified prefix.
517  *	SIOCALIFADDR: add the specified address.
518  *	SIOCALIFADDR with IFLR_PREFIX:
519  *		EINVAL since we can't deduce hostid part of the address.
520  *	SIOCDLIFADDR: delete the specified address.
521  *	SIOCDLIFADDR with IFLR_PREFIX:
522  *		delete the first address that matches the specified prefix.
523  * return values:
524  *	EINVAL on invalid parameters
525  *	EADDRNOTAVAIL on prefix match failed/specified address not found
526  *	other values may be returned from in_ioctl()
527  */
528 static int
529 in_lifaddr_ioctl(so, cmd, data, ifp, td)
530 	struct socket *so;
531 	u_long cmd;
532 	caddr_t	data;
533 	struct ifnet *ifp;
534 	struct thread *td;
535 {
536 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
537 	struct ifaddr *ifa;
538 
539 	/* sanity checks */
540 	if (!data || !ifp) {
541 		panic("invalid argument to in_lifaddr_ioctl");
542 		/*NOTRECHED*/
543 	}
544 
545 	switch (cmd) {
546 	case SIOCGLIFADDR:
547 		/* address must be specified on GET with IFLR_PREFIX */
548 		if ((iflr->flags & IFLR_PREFIX) == 0)
549 			break;
550 		/*FALLTHROUGH*/
551 	case SIOCALIFADDR:
552 	case SIOCDLIFADDR:
553 		/* address must be specified on ADD and DELETE */
554 		if (iflr->addr.ss_family != AF_INET)
555 			return EINVAL;
556 		if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
557 			return EINVAL;
558 		/* XXX need improvement */
559 		if (iflr->dstaddr.ss_family
560 		 && iflr->dstaddr.ss_family != AF_INET)
561 			return EINVAL;
562 		if (iflr->dstaddr.ss_family
563 		 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
564 			return EINVAL;
565 		break;
566 	default: /*shouldn't happen*/
567 		return EOPNOTSUPP;
568 	}
569 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
570 		return EINVAL;
571 
572 	switch (cmd) {
573 	case SIOCALIFADDR:
574 	    {
575 		struct in_aliasreq ifra;
576 
577 		if (iflr->flags & IFLR_PREFIX)
578 			return EINVAL;
579 
580 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
581 		bzero(&ifra, sizeof(ifra));
582 		bcopy(iflr->iflr_name, ifra.ifra_name,
583 			sizeof(ifra.ifra_name));
584 
585 		bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
586 
587 		if (iflr->dstaddr.ss_family) {	/*XXX*/
588 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
589 				iflr->dstaddr.ss_len);
590 		}
591 
592 		ifra.ifra_mask.sin_family = AF_INET;
593 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
594 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
595 
596 		return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
597 	    }
598 	case SIOCGLIFADDR:
599 	case SIOCDLIFADDR:
600 	    {
601 		struct in_ifaddr *ia;
602 		struct in_addr mask, candidate, match;
603 		struct sockaddr_in *sin;
604 		int cmp;
605 
606 		bzero(&mask, sizeof(mask));
607 		if (iflr->flags & IFLR_PREFIX) {
608 			/* lookup a prefix rather than address. */
609 			in_len2mask(&mask, iflr->prefixlen);
610 
611 			sin = (struct sockaddr_in *)&iflr->addr;
612 			match.s_addr = sin->sin_addr.s_addr;
613 			match.s_addr &= mask.s_addr;
614 
615 			/* if you set extra bits, that's wrong */
616 			if (match.s_addr != sin->sin_addr.s_addr)
617 				return EINVAL;
618 
619 			cmp = 1;
620 		} else {
621 			if (cmd == SIOCGLIFADDR) {
622 				/* on getting an address, take the 1st match */
623 				cmp = 0;	/*XXX*/
624 			} else {
625 				/* on deleting an address, do exact match */
626 				in_len2mask(&mask, 32);
627 				sin = (struct sockaddr_in *)&iflr->addr;
628 				match.s_addr = sin->sin_addr.s_addr;
629 
630 				cmp = 1;
631 			}
632 		}
633 
634 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)	{
635 			if (ifa->ifa_addr->sa_family != AF_INET6)
636 				continue;
637 			if (!cmp)
638 				break;
639 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
640 			candidate.s_addr &= mask.s_addr;
641 			if (candidate.s_addr == match.s_addr)
642 				break;
643 		}
644 		if (!ifa)
645 			return EADDRNOTAVAIL;
646 		ia = (struct in_ifaddr *)ifa;
647 
648 		if (cmd == SIOCGLIFADDR) {
649 			/* fill in the if_laddrreq structure */
650 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
651 
652 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
653 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
654 					ia->ia_dstaddr.sin_len);
655 			} else
656 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
657 
658 			iflr->prefixlen =
659 				in_mask2len(&ia->ia_sockmask.sin_addr);
660 
661 			iflr->flags = 0;	/*XXX*/
662 
663 			return 0;
664 		} else {
665 			struct in_aliasreq ifra;
666 
667 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
668 			bzero(&ifra, sizeof(ifra));
669 			bcopy(iflr->iflr_name, ifra.ifra_name,
670 				sizeof(ifra.ifra_name));
671 
672 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
673 				ia->ia_addr.sin_len);
674 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
675 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
676 					ia->ia_dstaddr.sin_len);
677 			}
678 			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
679 				ia->ia_sockmask.sin_len);
680 
681 			return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
682 					  ifp, td);
683 		}
684 	    }
685 	}
686 
687 	return EOPNOTSUPP;	/*just for safety*/
688 }
689 
690 /*
691  * Delete any existing route for an interface.
692  */
693 void
694 in_ifscrub(ifp, ia)
695 	register struct ifnet *ifp;
696 	register struct in_ifaddr *ia;
697 {
698 	in_scrubprefix(ia);
699 }
700 
701 /*
702  * Initialize an interface's internet address
703  * and routing table entry.
704  */
705 static int
706 in_ifinit(ifp, ia, sin, scrub)
707 	register struct ifnet *ifp;
708 	register struct in_ifaddr *ia;
709 	struct sockaddr_in *sin;
710 	int scrub;
711 {
712 	register u_long i = ntohl(sin->sin_addr.s_addr);
713 	struct sockaddr_in oldaddr;
714 	int s = splimp(), flags = RTF_UP, error = 0;
715 
716 	oldaddr = ia->ia_addr;
717 	if (oldaddr.sin_family == AF_INET)
718 		LIST_REMOVE(ia, ia_hash);
719 	ia->ia_addr = *sin;
720 	if (ia->ia_addr.sin_family == AF_INET)
721 		LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
722 		    ia, ia_hash);
723 	/*
724 	 * Give the interface a chance to initialize
725 	 * if this is its first address,
726 	 * and to validate the address if necessary.
727 	 */
728 	if (ifp->if_ioctl) {
729 		IFF_LOCKGIANT(ifp);
730 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
731 		IFF_UNLOCKGIANT(ifp);
732 		if (error) {
733 			splx(s);
734 			/* LIST_REMOVE(ia, ia_hash) is done in in_control */
735 			ia->ia_addr = oldaddr;
736 			if (ia->ia_addr.sin_family == AF_INET)
737 				LIST_INSERT_HEAD(INADDR_HASH(
738 				    ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
739 			return (error);
740 		}
741 	}
742 	splx(s);
743 	if (scrub) {
744 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
745 		in_ifscrub(ifp, ia);
746 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
747 	}
748 	if (IN_CLASSA(i))
749 		ia->ia_netmask = IN_CLASSA_NET;
750 	else if (IN_CLASSB(i))
751 		ia->ia_netmask = IN_CLASSB_NET;
752 	else
753 		ia->ia_netmask = IN_CLASSC_NET;
754 	/*
755 	 * The subnet mask usually includes at least the standard network part,
756 	 * but may may be smaller in the case of supernetting.
757 	 * If it is set, we believe it.
758 	 */
759 	if (ia->ia_subnetmask == 0) {
760 		ia->ia_subnetmask = ia->ia_netmask;
761 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
762 	} else
763 		ia->ia_netmask &= ia->ia_subnetmask;
764 	ia->ia_net = i & ia->ia_netmask;
765 	ia->ia_subnet = i & ia->ia_subnetmask;
766 	in_socktrim(&ia->ia_sockmask);
767 #ifdef DEV_CARP
768 	/*
769 	 * XXX: carp(4) does not have interface route
770 	 */
771 	if (ifp->if_type == IFT_CARP)
772 		return (0);
773 #endif
774 	/*
775 	 * Add route for the network.
776 	 */
777 	ia->ia_ifa.ifa_metric = ifp->if_metric;
778 	if (ifp->if_flags & IFF_BROADCAST) {
779 		ia->ia_broadaddr.sin_addr.s_addr =
780 			htonl(ia->ia_subnet | ~ia->ia_subnetmask);
781 		ia->ia_netbroadcast.s_addr =
782 			htonl(ia->ia_net | ~ ia->ia_netmask);
783 	} else if (ifp->if_flags & IFF_LOOPBACK) {
784 		ia->ia_dstaddr = ia->ia_addr;
785 		flags |= RTF_HOST;
786 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
787 		if (ia->ia_dstaddr.sin_family != AF_INET)
788 			return (0);
789 		flags |= RTF_HOST;
790 	}
791 	if ((error = in_addprefix(ia, flags)) != 0)
792 		return (error);
793 
794 	/*
795 	 * If the interface supports multicast, join the "all hosts"
796 	 * multicast group on that interface.
797 	 */
798 	if (ifp->if_flags & IFF_MULTICAST) {
799 		struct in_addr addr;
800 
801 		addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
802 		in_addmulti(&addr, ifp);
803 	}
804 	return (error);
805 }
806 
807 #define rtinitflags(x) \
808 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
809 	    ? RTF_HOST : 0)
810 /*
811  * Check if we have a route for the given prefix already or add a one
812  * accordingly.
813  */
814 static int
815 in_addprefix(target, flags)
816 	struct in_ifaddr *target;
817 	int flags;
818 {
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 	else {
826 		prefix = target->ia_addr.sin_addr;
827 		mask = target->ia_sockmask.sin_addr;
828 		prefix.s_addr &= mask.s_addr;
829 	}
830 
831 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
832 		if (rtinitflags(ia)) {
833 			p = ia->ia_addr.sin_addr;
834 
835 			if (prefix.s_addr != p.s_addr)
836 				continue;
837 		} else {
838 			p = ia->ia_addr.sin_addr;
839 			m = ia->ia_sockmask.sin_addr;
840 			p.s_addr &= m.s_addr;
841 
842 			if (prefix.s_addr != p.s_addr ||
843 			    mask.s_addr != m.s_addr)
844 				continue;
845 		}
846 
847 		/*
848 		 * If we got a matching prefix route inserted by other
849 		 * interface address, we are done here.
850 		 */
851 		if (ia->ia_flags & IFA_ROUTE) {
852 			if (sameprefixcarponly &&
853 			    target->ia_ifp->if_type != IFT_CARP &&
854 			    ia->ia_ifp->if_type != IFT_CARP)
855 				return (EEXIST);
856 			else
857 				return (0);
858 		}
859 	}
860 
861 	/*
862 	 * No-one seem to have this prefix route, so we try to insert it.
863 	 */
864 	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
865 	if (!error)
866 		target->ia_flags |= IFA_ROUTE;
867 	return error;
868 }
869 
870 /*
871  * If there is no other address in the system that can serve a route to the
872  * same prefix, remove the route.  Hand over the route to the new address
873  * otherwise.
874  */
875 static int
876 in_scrubprefix(target)
877 	struct in_ifaddr *target;
878 {
879 	struct in_ifaddr *ia;
880 	struct in_addr prefix, mask, p;
881 	int error;
882 
883 	if ((target->ia_flags & IFA_ROUTE) == 0)
884 		return 0;
885 
886 	if (rtinitflags(target))
887 		prefix = target->ia_dstaddr.sin_addr;
888 	else {
889 		prefix = target->ia_addr.sin_addr;
890 		mask = target->ia_sockmask.sin_addr;
891 		prefix.s_addr &= mask.s_addr;
892 	}
893 
894 	TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link) {
895 		if (rtinitflags(ia))
896 			p = ia->ia_dstaddr.sin_addr;
897 		else {
898 			p = ia->ia_addr.sin_addr;
899 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
900 		}
901 
902 		if (prefix.s_addr != p.s_addr)
903 			continue;
904 
905 		/*
906 		 * If we got a matching prefix address, move IFA_ROUTE and
907 		 * the route itself to it.  Make sure that routing daemons
908 		 * get a heads-up.
909 		 *
910 		 * XXX: a special case for carp(4) interface
911 		 */
912 		if ((ia->ia_flags & IFA_ROUTE) == 0
913 #ifdef DEV_CARP
914 		    && (ia->ia_ifp->if_type != IFT_CARP)
915 #endif
916 							) {
917 			rtinit(&(target->ia_ifa), (int)RTM_DELETE,
918 			    rtinitflags(target));
919 			target->ia_flags &= ~IFA_ROUTE;
920 
921 			error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
922 			    rtinitflags(ia) | RTF_UP);
923 			if (error == 0)
924 				ia->ia_flags |= IFA_ROUTE;
925 			return error;
926 		}
927 	}
928 
929 	/*
930 	 * As no-one seem to have this prefix, we can remove the route.
931 	 */
932 	rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
933 	target->ia_flags &= ~IFA_ROUTE;
934 	return 0;
935 }
936 
937 #undef rtinitflags
938 
939 /*
940  * Return 1 if the address might be a local broadcast address.
941  */
942 int
943 in_broadcast(in, ifp)
944 	struct in_addr in;
945 	struct ifnet *ifp;
946 {
947 	register struct ifaddr *ifa;
948 	u_long t;
949 
950 	if (in.s_addr == INADDR_BROADCAST ||
951 	    in.s_addr == INADDR_ANY)
952 		return 1;
953 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
954 		return 0;
955 	t = ntohl(in.s_addr);
956 	/*
957 	 * Look through the list of addresses for a match
958 	 * with a broadcast address.
959 	 */
960 #define ia ((struct in_ifaddr *)ifa)
961 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
962 		if (ifa->ifa_addr->sa_family == AF_INET &&
963 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
964 		     in.s_addr == ia->ia_netbroadcast.s_addr ||
965 		     /*
966 		      * Check for old-style (host 0) broadcast.
967 		      */
968 		     t == ia->ia_subnet || t == ia->ia_net) &&
969 		     /*
970 		      * Check for an all one subnetmask. These
971 		      * only exist when an interface gets a secondary
972 		      * address.
973 		      */
974 		     ia->ia_subnetmask != (u_long)0xffffffff)
975 			    return 1;
976 	return (0);
977 #undef ia
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(ap, ifp)
984 	register struct in_addr *ap;
985 	register struct ifnet *ifp;
986 {
987 	register struct in_multi *inm;
988 	int error;
989 	struct sockaddr_in sin;
990 	struct ifmultiaddr *ifma;
991 
992 	IFF_LOCKGIANT(ifp);
993 	IN_MULTI_LOCK();
994 	/*
995 	 * Call generic routine to add membership or increment
996 	 * refcount.  It wants addresses in the form of a sockaddr,
997 	 * so we build one here (being careful to zero the unused bytes).
998 	 */
999 	bzero(&sin, sizeof sin);
1000 	sin.sin_family = AF_INET;
1001 	sin.sin_len = sizeof sin;
1002 	sin.sin_addr = *ap;
1003 	error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1004 	if (error) {
1005 		IN_MULTI_UNLOCK();
1006 		IFF_UNLOCKGIANT(ifp);
1007 		return 0;
1008 	}
1009 
1010 	/*
1011 	 * If ifma->ifma_protospec is null, then if_addmulti() created
1012 	 * a new record.  Otherwise, we are done.
1013 	 */
1014 	if (ifma->ifma_protospec != NULL) {
1015 		IN_MULTI_UNLOCK();
1016 		IFF_UNLOCKGIANT(ifp);
1017 		return ifma->ifma_protospec;
1018 	}
1019 
1020 	inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR,
1021 	    M_NOWAIT | M_ZERO);
1022 	if (inm == NULL) {
1023 		IN_MULTI_UNLOCK();
1024 		IFF_UNLOCKGIANT(ifp);
1025 		return (NULL);
1026 	}
1027 
1028 	inm->inm_addr = *ap;
1029 	inm->inm_ifp = ifp;
1030 	inm->inm_ifma = ifma;
1031 	ifma->ifma_protospec = inm;
1032 	LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1033 
1034 	/*
1035 	 * Let IGMP know that we have joined a new IP multicast group.
1036 	 */
1037 	igmp_joingroup(inm);
1038 	IN_MULTI_UNLOCK();
1039 	IFF_UNLOCKGIANT(ifp);
1040 	return (inm);
1041 }
1042 
1043 /*
1044  * Delete a multicast address record.
1045  */
1046 void
1047 in_delmulti(inm)
1048 	register struct in_multi *inm;
1049 {
1050 	struct ifnet *ifp;
1051 
1052 	ifp = inm->inm_ifp;
1053 	IFF_LOCKGIANT(ifp);
1054 	IN_MULTI_LOCK();
1055 	in_delmulti_locked(inm);
1056 	IN_MULTI_UNLOCK();
1057 	IFF_UNLOCKGIANT(ifp);
1058 }
1059 
1060 void
1061 in_delmulti_locked(inm)
1062 	register struct in_multi *inm;
1063 {
1064 	struct ifmultiaddr *ifma;
1065 	struct in_multi my_inm;
1066 
1067 	ifma = inm->inm_ifma;
1068 	my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1069 	if (ifma->ifma_refcount == 1) {
1070 		/*
1071 		 * No remaining claims to this record; let IGMP know that
1072 		 * we are leaving the multicast group.
1073 		 * But do it after the if_delmulti() which might reset
1074 		 * the interface and nuke the packet.
1075 		 */
1076 		my_inm = *inm ;
1077 		ifma->ifma_protospec = NULL;
1078 		LIST_REMOVE(inm, inm_link);
1079 		free(inm, M_IPMADDR);
1080 	}
1081 	/* XXX - should be separate API for when we have an ifma? */
1082 	if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1083 	if (my_inm.inm_ifp != NULL)
1084 		igmp_leavegroup(&my_inm);
1085 }
1086 
1087 /*
1088  * Delete all multicast address records associated with the ifp.
1089  */
1090 void
1091 in_delmulti_ifp(ifp)
1092 	register struct ifnet *ifp;
1093 {
1094 	struct in_multi *inm;
1095 	struct in_multi *oinm;
1096 
1097 	IFF_LOCKGIANT(ifp);
1098 	IN_MULTI_LOCK();
1099 	LIST_FOREACH_SAFE(inm, &in_multihead, inm_link, oinm) {
1100 		if (inm->inm_ifp == ifp)
1101 			in_delmulti_locked(inm);
1102 	}
1103 	IN_MULTI_UNLOCK();
1104 	IFF_UNLOCKGIANT(ifp);
1105 }
1106 
1107 /*
1108  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1109  */
1110 void
1111 in_ifdetach(ifp)
1112 	struct ifnet *ifp;
1113 {
1114 
1115 	in_pcbpurgeif0(&ripcbinfo, ifp);
1116 	in_pcbpurgeif0(&udbinfo, ifp);
1117 	in_delmulti_ifp(ifp);
1118 }
1119