xref: /freebsd/sys/netinet/in.c (revision 09d325677d53a12c79a43664ff29871e92247629)
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_mpath.h"
37 
38 #include <sys/param.h>
39 #include <sys/eventhandler.h>
40 #include <sys/systm.h>
41 #include <sys/sockio.h>
42 #include <sys/malloc.h>
43 #include <sys/priv.h>
44 #include <sys/socket.h>
45 #include <sys/jail.h>
46 #include <sys/kernel.h>
47 #include <sys/proc.h>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
50 
51 #include <net/if.h>
52 #include <net/if_var.h>
53 #include <net/if_arp.h>
54 #include <net/if_dl.h>
55 #include <net/if_llatbl.h>
56 #include <net/if_types.h>
57 #include <net/route.h>
58 #include <net/vnet.h>
59 
60 #include <netinet/if_ether.h>
61 #include <netinet/in.h>
62 #include <netinet/in_var.h>
63 #include <netinet/in_pcb.h>
64 #include <netinet/ip_var.h>
65 #include <netinet/ip_carp.h>
66 #include <netinet/igmp_var.h>
67 #include <netinet/udp.h>
68 #include <netinet/udp_var.h>
69 
70 static int in_mask2len(struct in_addr *);
71 static void in_len2mask(struct in_addr *, int);
72 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
73 	struct ifnet *, struct thread *);
74 
75 static void	in_socktrim(struct sockaddr_in *);
76 static int	in_ifinit(struct ifnet *, struct in_ifaddr *,
77 		    struct sockaddr_in *, int, int);
78 static void	in_purgemaddrs(struct ifnet *);
79 
80 static VNET_DEFINE(int, nosameprefix);
81 #define	V_nosameprefix			VNET(nosameprefix)
82 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_RW,
83 	&VNET_NAME(nosameprefix), 0,
84 	"Refuse to create same prefixes on different interfaces");
85 
86 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
87 #define	V_ripcbinfo			VNET(ripcbinfo)
88 
89 /*
90  * Return 1 if an internet address is for a ``local'' host
91  * (one to which we have a connection).
92  */
93 int
94 in_localaddr(struct in_addr in)
95 {
96 	register u_long i = ntohl(in.s_addr);
97 	register struct in_ifaddr *ia;
98 
99 	IN_IFADDR_RLOCK();
100 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
101 		if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
102 			IN_IFADDR_RUNLOCK();
103 			return (1);
104 		}
105 	}
106 	IN_IFADDR_RUNLOCK();
107 	return (0);
108 }
109 
110 /*
111  * Return 1 if an internet address is for the local host and configured
112  * on one of its interfaces.
113  */
114 int
115 in_localip(struct in_addr in)
116 {
117 	struct in_ifaddr *ia;
118 
119 	IN_IFADDR_RLOCK();
120 	LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
121 		if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
122 			IN_IFADDR_RUNLOCK();
123 			return (1);
124 		}
125 	}
126 	IN_IFADDR_RUNLOCK();
127 	return (0);
128 }
129 
130 /*
131  * Determine whether an IP address is in a reserved set of addresses
132  * that may not be forwarded, or whether datagrams to that destination
133  * may be forwarded.
134  */
135 int
136 in_canforward(struct in_addr in)
137 {
138 	register u_long i = ntohl(in.s_addr);
139 	register u_long net;
140 
141 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
142 		return (0);
143 	if (IN_CLASSA(i)) {
144 		net = i & IN_CLASSA_NET;
145 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
146 			return (0);
147 	}
148 	return (1);
149 }
150 
151 /*
152  * Trim a mask in a sockaddr
153  */
154 static void
155 in_socktrim(struct sockaddr_in *ap)
156 {
157     register char *cplim = (char *) &ap->sin_addr;
158     register char *cp = (char *) (&ap->sin_addr + 1);
159 
160     ap->sin_len = 0;
161     while (--cp >= cplim)
162 	if (*cp) {
163 	    (ap)->sin_len = cp - (char *) (ap) + 1;
164 	    break;
165 	}
166 }
167 
168 static int
169 in_mask2len(mask)
170 	struct in_addr *mask;
171 {
172 	int x, y;
173 	u_char *p;
174 
175 	p = (u_char *)mask;
176 	for (x = 0; x < sizeof(*mask); x++) {
177 		if (p[x] != 0xff)
178 			break;
179 	}
180 	y = 0;
181 	if (x < sizeof(*mask)) {
182 		for (y = 0; y < 8; y++) {
183 			if ((p[x] & (0x80 >> y)) == 0)
184 				break;
185 		}
186 	}
187 	return (x * 8 + y);
188 }
189 
190 static void
191 in_len2mask(struct in_addr *mask, int len)
192 {
193 	int i;
194 	u_char *p;
195 
196 	p = (u_char *)mask;
197 	bzero(mask, sizeof(*mask));
198 	for (i = 0; i < len / 8; i++)
199 		p[i] = 0xff;
200 	if (len % 8)
201 		p[i] = (0xff00 >> (len % 8)) & 0xff;
202 }
203 
204 /*
205  * Generic internet control operations (ioctl's).
206  *
207  * ifp is NULL 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, *iap;
216 	register struct ifaddr *ifa;
217 	struct in_addr allhosts_addr;
218 	struct in_addr dst;
219 	struct in_ifinfo *ii;
220 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
221 	int error, hostIsNew, iaIsNew, maskIsNew;
222 	int iaIsFirst;
223 	u_long ocmd = cmd;
224 
225 	/*
226 	 * Pre-10.x compat: OSIOCAIFADDR passes a shorter
227 	 * struct in_aliasreq, without ifra_vhid.
228 	 */
229 	if (cmd == OSIOCAIFADDR)
230 		cmd = SIOCAIFADDR;
231 
232 	ia = NULL;
233 	iaIsFirst = 0;
234 	iaIsNew = 0;
235 	allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
236 
237 	/*
238 	 * Filter out ioctls we implement directly; forward the rest on to
239 	 * in_lifaddr_ioctl() and ifp->if_ioctl().
240 	 */
241 	switch (cmd) {
242 	case SIOCGIFADDR:
243 	case SIOCGIFBRDADDR:
244 	case SIOCGIFDSTADDR:
245 	case SIOCGIFNETMASK:
246 	case SIOCDIFADDR:
247 		break;
248 	case SIOCAIFADDR:
249 		/*
250 		 * ifra_addr must be present and be of INET family.
251 		 * ifra_broadaddr and ifra_mask are optional.
252 		 */
253 		if (ifra->ifra_addr.sin_len != sizeof(struct sockaddr_in) ||
254 		    ifra->ifra_addr.sin_family != AF_INET)
255 			return (EINVAL);
256 		if (ifra->ifra_broadaddr.sin_len != 0 &&
257 		    (ifra->ifra_broadaddr.sin_len !=
258 		    sizeof(struct sockaddr_in) ||
259 		    ifra->ifra_broadaddr.sin_family != AF_INET))
260 			return (EINVAL);
261 #if 0
262 		/*
263 		 * ifconfig(8) in pre-10.x doesn't set sin_family for the
264 		 * mask. The code is disabled for the 10.x timeline, to
265 		 * make SIOCAIFADDR compatible with 9.x ifconfig(8).
266 		 * The code should be enabled in 11.x
267 		 */
268 		if (ifra->ifra_mask.sin_len != 0 &&
269 		    (ifra->ifra_mask.sin_len != sizeof(struct sockaddr_in) ||
270 		    ifra->ifra_mask.sin_family != AF_INET))
271 			return (EINVAL);
272 #endif
273 		break;
274 	case SIOCSIFADDR:
275 	case SIOCSIFBRDADDR:
276 	case SIOCSIFDSTADDR:
277 	case SIOCSIFNETMASK:
278 		/* We no longer support that old commands. */
279 		return (EINVAL);
280 
281 	case SIOCALIFADDR:
282 		if (td != NULL) {
283 			error = priv_check(td, PRIV_NET_ADDIFADDR);
284 			if (error)
285 				return (error);
286 		}
287 		if (ifp == NULL)
288 			return (EINVAL);
289 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
290 
291 	case SIOCDLIFADDR:
292 		if (td != NULL) {
293 			error = priv_check(td, PRIV_NET_DELIFADDR);
294 			if (error)
295 				return (error);
296 		}
297 		if (ifp == NULL)
298 			return (EINVAL);
299 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
300 
301 	case SIOCGLIFADDR:
302 		if (ifp == NULL)
303 			return (EINVAL);
304 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
305 
306 	default:
307 		if (ifp == NULL || ifp->if_ioctl == NULL)
308 			return (EOPNOTSUPP);
309 		return ((*ifp->if_ioctl)(ifp, cmd, data));
310 	}
311 
312 	if (ifp == NULL)
313 		return (EADDRNOTAVAIL);
314 
315 	/*
316 	 * Security checks before we get involved in any work.
317 	 */
318 	switch (cmd) {
319 	case SIOCAIFADDR:
320 		if (td != NULL) {
321 			error = priv_check(td, PRIV_NET_ADDIFADDR);
322 			if (error)
323 				return (error);
324 		}
325 		break;
326 
327 	case SIOCDIFADDR:
328 		if (td != NULL) {
329 			error = priv_check(td, PRIV_NET_DELIFADDR);
330 			if (error)
331 				return (error);
332 		}
333 		break;
334 	}
335 
336 	/*
337 	 * Find address for this interface, if it exists.
338 	 *
339 	 * If an alias address was specified, find that one instead of the
340 	 * first one on the interface, if possible.
341 	 */
342 	dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
343 	IN_IFADDR_RLOCK();
344 	LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) {
345 		if (iap->ia_ifp == ifp &&
346 		    iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
347 			if (td == NULL || prison_check_ip4(td->td_ucred,
348 			    &dst) == 0)
349 				ia = iap;
350 			break;
351 		}
352 	}
353 	if (ia != NULL)
354 		ifa_ref(&ia->ia_ifa);
355 	IN_IFADDR_RUNLOCK();
356 	if (ia == NULL) {
357 		IF_ADDR_RLOCK(ifp);
358 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
359 			iap = ifatoia(ifa);
360 			if (iap->ia_addr.sin_family == AF_INET) {
361 				if (td != NULL &&
362 				    prison_check_ip4(td->td_ucred,
363 				    &iap->ia_addr.sin_addr) != 0)
364 					continue;
365 				ia = iap;
366 				break;
367 			}
368 		}
369 		if (ia != NULL)
370 			ifa_ref(&ia->ia_ifa);
371 		IF_ADDR_RUNLOCK(ifp);
372 	}
373 	if (ia == NULL)
374 		iaIsFirst = 1;
375 
376 	error = 0;
377 	switch (cmd) {
378 	case SIOCAIFADDR:
379 	case SIOCDIFADDR:
380 		if (ifra->ifra_addr.sin_family == AF_INET) {
381 			struct in_ifaddr *oia;
382 
383 			IN_IFADDR_RLOCK();
384 			for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
385 				if (ia->ia_ifp == ifp  &&
386 				    ia->ia_addr.sin_addr.s_addr ==
387 				    ifra->ifra_addr.sin_addr.s_addr)
388 					break;
389 			}
390 			if (ia != NULL && ia != oia)
391 				ifa_ref(&ia->ia_ifa);
392 			if (oia != NULL && ia != oia)
393 				ifa_free(&oia->ia_ifa);
394 			IN_IFADDR_RUNLOCK();
395 			if ((ifp->if_flags & IFF_POINTOPOINT)
396 			    && (cmd == SIOCAIFADDR)
397 			    && (ifra->ifra_dstaddr.sin_addr.s_addr
398 				== INADDR_ANY)) {
399 				error = EDESTADDRREQ;
400 				goto out;
401 			}
402 		}
403 		if (cmd == SIOCDIFADDR && ia == NULL) {
404 			error = EADDRNOTAVAIL;
405 			goto out;
406 		}
407 		if (ia == NULL) {
408 			ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
409 			ia = (struct in_ifaddr *)ifa;
410 			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
411 			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
412 			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
413 
414 			ia->ia_sockmask.sin_len = 8;
415 			ia->ia_sockmask.sin_family = AF_INET;
416 			if (ifp->if_flags & IFF_BROADCAST) {
417 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
418 				ia->ia_broadaddr.sin_family = AF_INET;
419 			}
420 			ia->ia_ifp = ifp;
421 
422 			ifa_ref(ifa);			/* if_addrhead */
423 			IF_ADDR_WLOCK(ifp);
424 			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
425 			IF_ADDR_WUNLOCK(ifp);
426 			ifa_ref(ifa);			/* in_ifaddrhead */
427 			IN_IFADDR_WLOCK();
428 			TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
429 			IN_IFADDR_WUNLOCK();
430 			iaIsNew = 1;
431 		}
432 		break;
433 
434 	case SIOCGIFADDR:
435 	case SIOCGIFNETMASK:
436 	case SIOCGIFDSTADDR:
437 	case SIOCGIFBRDADDR:
438 		if (ia == NULL) {
439 			error = EADDRNOTAVAIL;
440 			goto out;
441 		}
442 		break;
443 	}
444 
445 	/*
446 	 * Most paths in this switch return directly or via out.  Only paths
447 	 * that remove the address break in order to hit common removal code.
448 	 */
449 	switch (cmd) {
450 	case SIOCGIFADDR:
451 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
452 		goto out;
453 
454 	case SIOCGIFBRDADDR:
455 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
456 			error = EINVAL;
457 			goto out;
458 		}
459 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
460 		goto out;
461 
462 	case SIOCGIFDSTADDR:
463 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
464 			error = EINVAL;
465 			goto out;
466 		}
467 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
468 		goto out;
469 
470 	case SIOCGIFNETMASK:
471 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
472 		goto out;
473 
474 	case SIOCAIFADDR:
475 		maskIsNew = 0;
476 		hostIsNew = 1;
477 		error = 0;
478 		if (ifra->ifra_addr.sin_addr.s_addr ==
479 			    ia->ia_addr.sin_addr.s_addr)
480 			hostIsNew = 0;
481 		if (ifra->ifra_mask.sin_len) {
482 			/*
483 			 * QL: XXX
484 			 * Need to scrub the prefix here in case
485 			 * the issued command is SIOCAIFADDR with
486 			 * the same address, but with a different
487 			 * prefix length. And if the prefix length
488 			 * is the same as before, then the call is
489 			 * un-necessarily executed here.
490 			 */
491 			in_scrubprefix(ia, LLE_STATIC);
492 			ia->ia_sockmask = ifra->ifra_mask;
493 			ia->ia_sockmask.sin_family = AF_INET;
494 			ia->ia_subnetmask =
495 			    ntohl(ia->ia_sockmask.sin_addr.s_addr);
496 			maskIsNew = 1;
497 		}
498 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
499 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
500 			in_scrubprefix(ia, LLE_STATIC);
501 			ia->ia_dstaddr = ifra->ifra_dstaddr;
502 			maskIsNew  = 1; /* We lie; but the effect's the same */
503 		}
504 		if (hostIsNew || maskIsNew)
505 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, maskIsNew,
506 			    (ocmd == cmd ? ifra->ifra_vhid : 0));
507 		if (error != 0 && iaIsNew)
508 			break;
509 
510 		if ((ifp->if_flags & IFF_BROADCAST) &&
511 		    ifra->ifra_broadaddr.sin_len)
512 			ia->ia_broadaddr = ifra->ifra_broadaddr;
513 		if (error == 0) {
514 			ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
515 			if (iaIsFirst &&
516 			    (ifp->if_flags & IFF_MULTICAST) != 0) {
517 				error = in_joingroup(ifp, &allhosts_addr,
518 				    NULL, &ii->ii_allhosts);
519 			}
520 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
521 		}
522 		goto out;
523 
524 	case SIOCDIFADDR:
525 		/*
526 		 * in_scrubprefix() kills the interface route.
527 		 */
528 		in_scrubprefix(ia, LLE_STATIC);
529 
530 		/*
531 		 * in_ifadown gets rid of all the rest of
532 		 * the routes.  This is not quite the right
533 		 * thing to do, but at least if we are running
534 		 * a routing process they will come back.
535 		 */
536 		in_ifadown(&ia->ia_ifa, 1);
537 		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
538 		error = 0;
539 		break;
540 
541 	default:
542 		panic("in_control: unsupported ioctl");
543 	}
544 
545 	if (ia->ia_ifa.ifa_carp)
546 		(*carp_detach_p)(&ia->ia_ifa);
547 
548 	IF_ADDR_WLOCK(ifp);
549 	/* Re-check that ia is still part of the list. */
550 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
551 		if (ifa == &ia->ia_ifa)
552 			break;
553 	}
554 	if (ifa == NULL) {
555 		/*
556 		 * If we lost the race with another thread, there is no need to
557 		 * try it again for the next loop as there is no other exit
558 		 * path between here and out.
559 		 */
560 		IF_ADDR_WUNLOCK(ifp);
561 		error = EADDRNOTAVAIL;
562 		goto out;
563 	}
564 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
565 	IF_ADDR_WUNLOCK(ifp);
566 	ifa_free(&ia->ia_ifa);		      /* if_addrhead */
567 
568 	IN_IFADDR_WLOCK();
569 	TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
570 
571 	LIST_REMOVE(ia, ia_hash);
572 	IN_IFADDR_WUNLOCK();
573 	/*
574 	 * If this is the last IPv4 address configured on this
575 	 * interface, leave the all-hosts group.
576 	 * No state-change report need be transmitted.
577 	 */
578 	IFP_TO_IA(ifp, iap);
579 	if (iap == NULL) {
580 		ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
581 		IN_MULTI_LOCK();
582 		if (ii->ii_allhosts) {
583 			(void)in_leavegroup_locked(ii->ii_allhosts, NULL);
584 			ii->ii_allhosts = NULL;
585 		}
586 		IN_MULTI_UNLOCK();
587 	} else
588 		ifa_free(&iap->ia_ifa);
589 
590 	ifa_free(&ia->ia_ifa);				/* in_ifaddrhead */
591 out:
592 	if (ia != NULL)
593 		ifa_free(&ia->ia_ifa);
594 	return (error);
595 }
596 
597 /*
598  * SIOC[GAD]LIFADDR.
599  *	SIOCGLIFADDR: get first address. (?!?)
600  *	SIOCGLIFADDR with IFLR_PREFIX:
601  *		get first address that matches the specified prefix.
602  *	SIOCALIFADDR: add the specified address.
603  *	SIOCALIFADDR with IFLR_PREFIX:
604  *		EINVAL since we can't deduce hostid part of the address.
605  *	SIOCDLIFADDR: delete the specified address.
606  *	SIOCDLIFADDR with IFLR_PREFIX:
607  *		delete the first address that matches the specified prefix.
608  * return values:
609  *	EINVAL on invalid parameters
610  *	EADDRNOTAVAIL on prefix match failed/specified address not found
611  *	other values may be returned from in_ioctl()
612  */
613 static int
614 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
615     struct ifnet *ifp, struct thread *td)
616 {
617 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
618 	struct ifaddr *ifa;
619 
620 	/* sanity checks */
621 	if (data == NULL || ifp == NULL) {
622 		panic("invalid argument to in_lifaddr_ioctl");
623 		/*NOTRECHED*/
624 	}
625 
626 	switch (cmd) {
627 	case SIOCGLIFADDR:
628 		/* address must be specified on GET with IFLR_PREFIX */
629 		if ((iflr->flags & IFLR_PREFIX) == 0)
630 			break;
631 		/*FALLTHROUGH*/
632 	case SIOCALIFADDR:
633 	case SIOCDLIFADDR:
634 		/* address must be specified on ADD and DELETE */
635 		if (iflr->addr.ss_family != AF_INET)
636 			return (EINVAL);
637 		if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
638 			return (EINVAL);
639 		/* XXX need improvement */
640 		if (iflr->dstaddr.ss_family
641 		 && iflr->dstaddr.ss_family != AF_INET)
642 			return (EINVAL);
643 		if (iflr->dstaddr.ss_family
644 		 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
645 			return (EINVAL);
646 		break;
647 	default: /*shouldn't happen*/
648 		return (EOPNOTSUPP);
649 	}
650 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
651 		return (EINVAL);
652 
653 	switch (cmd) {
654 	case SIOCALIFADDR:
655 	    {
656 		struct in_aliasreq ifra;
657 
658 		if (iflr->flags & IFLR_PREFIX)
659 			return (EINVAL);
660 
661 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR). */
662 		bzero(&ifra, sizeof(ifra));
663 		bcopy(iflr->iflr_name, ifra.ifra_name,
664 			sizeof(ifra.ifra_name));
665 
666 		bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
667 
668 		if (iflr->dstaddr.ss_family) {	/*XXX*/
669 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
670 				iflr->dstaddr.ss_len);
671 		}
672 
673 		ifra.ifra_mask.sin_family = AF_INET;
674 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
675 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
676 
677 		return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td));
678 	    }
679 	case SIOCGLIFADDR:
680 	case SIOCDLIFADDR:
681 	    {
682 		struct in_ifaddr *ia;
683 		struct in_addr mask, candidate, match;
684 		struct sockaddr_in *sin;
685 
686 		bzero(&mask, sizeof(mask));
687 		bzero(&match, sizeof(match));
688 		if (iflr->flags & IFLR_PREFIX) {
689 			/* lookup a prefix rather than address. */
690 			in_len2mask(&mask, iflr->prefixlen);
691 
692 			sin = (struct sockaddr_in *)&iflr->addr;
693 			match.s_addr = sin->sin_addr.s_addr;
694 			match.s_addr &= mask.s_addr;
695 
696 			/* if you set extra bits, that's wrong */
697 			if (match.s_addr != sin->sin_addr.s_addr)
698 				return (EINVAL);
699 
700 		} else {
701 			/* on getting an address, take the 1st match */
702 			/* on deleting an address, do exact match */
703 			if (cmd != SIOCGLIFADDR) {
704 				in_len2mask(&mask, 32);
705 				sin = (struct sockaddr_in *)&iflr->addr;
706 				match.s_addr = sin->sin_addr.s_addr;
707 			}
708 		}
709 
710 		IF_ADDR_RLOCK(ifp);
711 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)	{
712 			if (ifa->ifa_addr->sa_family != AF_INET)
713 				continue;
714 			if (match.s_addr == 0)
715 				break;
716 			sin = (struct sockaddr_in *)&ifa->ifa_addr;
717 			candidate.s_addr = sin->sin_addr.s_addr;
718 			candidate.s_addr &= mask.s_addr;
719 			if (candidate.s_addr == match.s_addr)
720 				break;
721 		}
722 		if (ifa != NULL)
723 			ifa_ref(ifa);
724 		IF_ADDR_RUNLOCK(ifp);
725 		if (ifa == NULL)
726 			return (EADDRNOTAVAIL);
727 		ia = (struct in_ifaddr *)ifa;
728 
729 		if (cmd == SIOCGLIFADDR) {
730 			/* fill in the if_laddrreq structure */
731 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
732 
733 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
734 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
735 					ia->ia_dstaddr.sin_len);
736 			} else
737 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
738 
739 			iflr->prefixlen =
740 				in_mask2len(&ia->ia_sockmask.sin_addr);
741 
742 			iflr->flags = 0;	/*XXX*/
743 			ifa_free(ifa);
744 
745 			return (0);
746 		} else {
747 			struct in_aliasreq ifra;
748 
749 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR) */
750 			bzero(&ifra, sizeof(ifra));
751 			bcopy(iflr->iflr_name, ifra.ifra_name,
752 				sizeof(ifra.ifra_name));
753 
754 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
755 				ia->ia_addr.sin_len);
756 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
757 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
758 					ia->ia_dstaddr.sin_len);
759 			}
760 			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
761 				ia->ia_sockmask.sin_len);
762 			ifa_free(ifa);
763 
764 			return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
765 			    ifp, td));
766 		}
767 	    }
768 	}
769 
770 	return (EOPNOTSUPP);	/*just for safety*/
771 }
772 
773 /*
774  * Initialize an interface's internet address
775  * and routing table entry.
776  */
777 static int
778 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
779     int masksupplied, int vhid)
780 {
781 	register u_long i = ntohl(sin->sin_addr.s_addr);
782 	int flags, error = 0;
783 
784 	IN_IFADDR_WLOCK();
785 	if (ia->ia_addr.sin_family == AF_INET)
786 		LIST_REMOVE(ia, ia_hash);
787 	ia->ia_addr = *sin;
788 	LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
789 	    ia, ia_hash);
790 	IN_IFADDR_WUNLOCK();
791 
792 	if (vhid > 0) {
793 		if (carp_attach_p != NULL)
794 			error = (*carp_attach_p)(&ia->ia_ifa, vhid);
795 		else
796 			error = EPROTONOSUPPORT;
797 	}
798 	if (error)
799 		return (error);
800 
801 	/*
802 	 * Give the interface a chance to initialize
803 	 * if this is its first address,
804 	 * and to validate the address if necessary.
805 	 */
806 	if (ifp->if_ioctl != NULL &&
807 	    (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)) != 0)
808 			/* LIST_REMOVE(ia, ia_hash) is done in in_control */
809 			return (error);
810 
811 	/*
812 	 * Be compatible with network classes, if netmask isn't supplied,
813 	 * guess it based on classes.
814 	 */
815 	if (!masksupplied) {
816 		if (IN_CLASSA(i))
817 			ia->ia_subnetmask = IN_CLASSA_NET;
818 		else if (IN_CLASSB(i))
819 			ia->ia_subnetmask = IN_CLASSB_NET;
820 		else
821 			ia->ia_subnetmask = IN_CLASSC_NET;
822 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
823 	}
824 	ia->ia_subnet = i & ia->ia_subnetmask;
825 	in_socktrim(&ia->ia_sockmask);
826 
827 	/*
828 	 * Add route for the network.
829 	 */
830 	flags = RTF_UP;
831 	ia->ia_ifa.ifa_metric = ifp->if_metric;
832 	if (ifp->if_flags & IFF_BROADCAST) {
833 		if (ia->ia_subnetmask == IN_RFC3021_MASK)
834 			ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
835 		else
836 			ia->ia_broadaddr.sin_addr.s_addr =
837 			    htonl(ia->ia_subnet | ~ia->ia_subnetmask);
838 	} else if (ifp->if_flags & IFF_LOOPBACK) {
839 		ia->ia_dstaddr = ia->ia_addr;
840 		flags |= RTF_HOST;
841 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
842 		if (ia->ia_dstaddr.sin_family != AF_INET)
843 			return (0);
844 		flags |= RTF_HOST;
845 	}
846 	if (!vhid && (error = in_addprefix(ia, flags)) != 0)
847 		return (error);
848 
849 	if (ia->ia_addr.sin_addr.s_addr == INADDR_ANY)
850 		return (0);
851 
852 	if (ifp->if_flags & IFF_POINTOPOINT &&
853 	    ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
854 			return (0);
855 
856 	/*
857 	 * add a loopback route to self
858 	 */
859 	if (V_useloopback && !vhid && !(ifp->if_flags & IFF_LOOPBACK)) {
860 		struct route ia_ro;
861 
862 		bzero(&ia_ro, sizeof(ia_ro));
863 		*((struct sockaddr_in *)(&ia_ro.ro_dst)) = ia->ia_addr;
864 		rtalloc_ign_fib(&ia_ro, 0, RT_DEFAULT_FIB);
865 		if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
866 		    (ia_ro.ro_rt->rt_ifp == V_loif)) {
867 			RT_LOCK(ia_ro.ro_rt);
868 			RT_ADDREF(ia_ro.ro_rt);
869 			RTFREE_LOCKED(ia_ro.ro_rt);
870 		} else
871 			error = ifa_add_loopback_route((struct ifaddr *)ia,
872 			    (struct sockaddr *)&ia->ia_addr);
873 		if (error == 0)
874 			ia->ia_flags |= IFA_RTSELF;
875 		if (ia_ro.ro_rt != NULL)
876 			RTFREE(ia_ro.ro_rt);
877 	}
878 
879 	return (error);
880 }
881 
882 #define rtinitflags(x) \
883 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
884 	    ? RTF_HOST : 0)
885 
886 /*
887  * Generate a routing message when inserting or deleting
888  * an interface address alias.
889  */
890 static void in_addralias_rtmsg(int cmd, struct in_addr *prefix,
891     struct in_ifaddr *target)
892 {
893 	struct route pfx_ro;
894 	struct sockaddr_in *pfx_addr;
895 	struct rtentry msg_rt;
896 
897 	/* QL: XXX
898 	 * This is a bit questionable because there is no
899 	 * additional route entry added/deleted for an address
900 	 * alias. Therefore this route report is inaccurate.
901 	 */
902 	bzero(&pfx_ro, sizeof(pfx_ro));
903 	pfx_addr = (struct sockaddr_in *)(&pfx_ro.ro_dst);
904 	pfx_addr->sin_len = sizeof(*pfx_addr);
905 	pfx_addr->sin_family = AF_INET;
906 	pfx_addr->sin_addr = *prefix;
907 	rtalloc_ign_fib(&pfx_ro, 0, 0);
908 	if (pfx_ro.ro_rt != NULL) {
909 		msg_rt = *pfx_ro.ro_rt;
910 
911 		/* QL: XXX
912 		 * Point the gateway to the new interface
913 		 * address as if a new prefix route entry has
914 		 * been added through the new address alias.
915 		 * All other parts of the rtentry is accurate,
916 		 * e.g., rt_key, rt_mask, rt_ifp etc.
917 		 */
918 		msg_rt.rt_gateway = (struct sockaddr *)&target->ia_addr;
919 		rt_newaddrmsg(cmd, (struct ifaddr *)target, 0, &msg_rt);
920 		RTFREE(pfx_ro.ro_rt);
921 	}
922 	return;
923 }
924 
925 /*
926  * Check if we have a route for the given prefix already or add one accordingly.
927  */
928 int
929 in_addprefix(struct in_ifaddr *target, int flags)
930 {
931 	struct in_ifaddr *ia;
932 	struct in_addr prefix, mask, p, m;
933 	int error;
934 
935 	if ((flags & RTF_HOST) != 0) {
936 		prefix = target->ia_dstaddr.sin_addr;
937 		mask.s_addr = 0;
938 	} else {
939 		prefix = target->ia_addr.sin_addr;
940 		mask = target->ia_sockmask.sin_addr;
941 		prefix.s_addr &= mask.s_addr;
942 	}
943 
944 	IN_IFADDR_RLOCK();
945 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
946 		if (rtinitflags(ia)) {
947 			p = ia->ia_dstaddr.sin_addr;
948 
949 			if (prefix.s_addr != p.s_addr)
950 				continue;
951 		} else {
952 			p = ia->ia_addr.sin_addr;
953 			m = ia->ia_sockmask.sin_addr;
954 			p.s_addr &= m.s_addr;
955 
956 			if (prefix.s_addr != p.s_addr ||
957 			    mask.s_addr != m.s_addr)
958 				continue;
959 		}
960 
961 		/*
962 		 * If we got a matching prefix route inserted by other
963 		 * interface address, we are done here.
964 		 */
965 		if (ia->ia_flags & IFA_ROUTE) {
966 #ifdef RADIX_MPATH
967 			if (ia->ia_addr.sin_addr.s_addr ==
968 			    target->ia_addr.sin_addr.s_addr) {
969 				IN_IFADDR_RUNLOCK();
970 				return (EEXIST);
971 			} else
972 				break;
973 #endif
974 			if (V_nosameprefix) {
975 				IN_IFADDR_RUNLOCK();
976 				return (EEXIST);
977 			} else {
978 				in_addralias_rtmsg(RTM_ADD, &prefix, target);
979 				IN_IFADDR_RUNLOCK();
980 				return (0);
981 			}
982 		}
983 	}
984 	IN_IFADDR_RUNLOCK();
985 
986 	/*
987 	 * No-one seem to have this prefix route, so we try to insert it.
988 	 */
989 	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
990 	if (!error)
991 		target->ia_flags |= IFA_ROUTE;
992 	return (error);
993 }
994 
995 /*
996  * If there is no other address in the system that can serve a route to the
997  * same prefix, remove the route.  Hand over the route to the new address
998  * otherwise.
999  */
1000 int
1001 in_scrubprefix(struct in_ifaddr *target, u_int flags)
1002 {
1003 	struct in_ifaddr *ia;
1004 	struct in_addr prefix, mask, p, m;
1005 	int error = 0;
1006 	struct sockaddr_in prefix0, mask0;
1007 
1008 	/*
1009 	 * Remove the loopback route to the interface address.
1010 	 * The "useloopback" setting is not consulted because if the
1011 	 * user configures an interface address, turns off this
1012 	 * setting, and then tries to delete that interface address,
1013 	 * checking the current setting of "useloopback" would leave
1014 	 * that interface address loopback route untouched, which
1015 	 * would be wrong. Therefore the interface address loopback route
1016 	 * deletion is unconditional.
1017 	 */
1018 	if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
1019 	    !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
1020 	    (target->ia_flags & IFA_RTSELF)) {
1021 		struct route ia_ro;
1022 		int freeit = 0;
1023 
1024 		bzero(&ia_ro, sizeof(ia_ro));
1025 		*((struct sockaddr_in *)(&ia_ro.ro_dst)) = target->ia_addr;
1026 		rtalloc_ign_fib(&ia_ro, 0, 0);
1027 		if ((ia_ro.ro_rt != NULL) && (ia_ro.ro_rt->rt_ifp != NULL) &&
1028 		    (ia_ro.ro_rt->rt_ifp == V_loif)) {
1029 			RT_LOCK(ia_ro.ro_rt);
1030 			if (ia_ro.ro_rt->rt_refcnt <= 1)
1031 				freeit = 1;
1032 			else if (flags & LLE_STATIC) {
1033 				RT_REMREF(ia_ro.ro_rt);
1034 				target->ia_flags &= ~IFA_RTSELF;
1035 			}
1036 			RTFREE_LOCKED(ia_ro.ro_rt);
1037 		}
1038 		if (freeit && (flags & LLE_STATIC)) {
1039 			error = ifa_del_loopback_route((struct ifaddr *)target,
1040 			    (struct sockaddr *)&target->ia_addr);
1041 			if (error == 0)
1042 				target->ia_flags &= ~IFA_RTSELF;
1043 		}
1044 		if ((flags & LLE_STATIC) &&
1045 			!(target->ia_ifp->if_flags & IFF_NOARP))
1046 			/* remove arp cache */
1047 			arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr);
1048 	}
1049 
1050 	if (rtinitflags(target)) {
1051 		prefix = target->ia_dstaddr.sin_addr;
1052 		mask.s_addr = 0;
1053 	} else {
1054 		prefix = target->ia_addr.sin_addr;
1055 		mask = target->ia_sockmask.sin_addr;
1056 		prefix.s_addr &= mask.s_addr;
1057 	}
1058 
1059 	if ((target->ia_flags & IFA_ROUTE) == 0) {
1060 		in_addralias_rtmsg(RTM_DELETE, &prefix, target);
1061 		return (0);
1062 	}
1063 
1064 	IN_IFADDR_RLOCK();
1065 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1066 		if (rtinitflags(ia)) {
1067 			p = ia->ia_dstaddr.sin_addr;
1068 
1069 			if (prefix.s_addr != p.s_addr)
1070 				continue;
1071 		} else {
1072 			p = ia->ia_addr.sin_addr;
1073 			m = ia->ia_sockmask.sin_addr;
1074 			p.s_addr &= m.s_addr;
1075 
1076 			if (prefix.s_addr != p.s_addr ||
1077 			    mask.s_addr != m.s_addr)
1078 				continue;
1079 		}
1080 
1081 		if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1082 			continue;
1083 
1084 		/*
1085 		 * If we got a matching prefix address, move IFA_ROUTE and
1086 		 * the route itself to it.  Make sure that routing daemons
1087 		 * get a heads-up.
1088 		 */
1089 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
1090 			ifa_ref(&ia->ia_ifa);
1091 			IN_IFADDR_RUNLOCK();
1092 			error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
1093 			    rtinitflags(target));
1094 			if (error == 0)
1095 				target->ia_flags &= ~IFA_ROUTE;
1096 			else
1097 				log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
1098 					error);
1099 			error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
1100 			    rtinitflags(ia) | RTF_UP);
1101 			if (error == 0)
1102 				ia->ia_flags |= IFA_ROUTE;
1103 			else
1104 				log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
1105 					error);
1106 			ifa_free(&ia->ia_ifa);
1107 			return (error);
1108 		}
1109 	}
1110 	IN_IFADDR_RUNLOCK();
1111 
1112 	/*
1113 	 * remove all L2 entries on the given prefix
1114 	 */
1115 	bzero(&prefix0, sizeof(prefix0));
1116 	prefix0.sin_len = sizeof(prefix0);
1117 	prefix0.sin_family = AF_INET;
1118 	prefix0.sin_addr.s_addr = target->ia_subnet;
1119 	bzero(&mask0, sizeof(mask0));
1120 	mask0.sin_len = sizeof(mask0);
1121 	mask0.sin_family = AF_INET;
1122 	mask0.sin_addr.s_addr = target->ia_subnetmask;
1123 	lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0,
1124 	    (struct sockaddr *)&mask0, flags);
1125 
1126 	/*
1127 	 * As no-one seem to have this prefix, we can remove the route.
1128 	 */
1129 	error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
1130 	if (error == 0)
1131 		target->ia_flags &= ~IFA_ROUTE;
1132 	else
1133 		log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
1134 	return (error);
1135 }
1136 
1137 #undef rtinitflags
1138 
1139 /*
1140  * Return 1 if the address might be a local broadcast address.
1141  */
1142 int
1143 in_broadcast(struct in_addr in, struct ifnet *ifp)
1144 {
1145 	register struct ifaddr *ifa;
1146 	u_long t;
1147 
1148 	if (in.s_addr == INADDR_BROADCAST ||
1149 	    in.s_addr == INADDR_ANY)
1150 		return (1);
1151 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
1152 		return (0);
1153 	t = ntohl(in.s_addr);
1154 	/*
1155 	 * Look through the list of addresses for a match
1156 	 * with a broadcast address.
1157 	 */
1158 #define ia ((struct in_ifaddr *)ifa)
1159 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1160 		if (ifa->ifa_addr->sa_family == AF_INET &&
1161 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1162 		     /*
1163 		      * Check for old-style (host 0) broadcast, but
1164 		      * taking into account that RFC 3021 obsoletes it.
1165 		      */
1166 		    (ia->ia_subnetmask != IN_RFC3021_MASK &&
1167 		    t == ia->ia_subnet)) &&
1168 		     /*
1169 		      * Check for an all one subnetmask. These
1170 		      * only exist when an interface gets a secondary
1171 		      * address.
1172 		      */
1173 		    ia->ia_subnetmask != (u_long)0xffffffff)
1174 			    return (1);
1175 	return (0);
1176 #undef ia
1177 }
1178 
1179 /*
1180  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1181  */
1182 void
1183 in_ifdetach(struct ifnet *ifp)
1184 {
1185 
1186 	in_pcbpurgeif0(&V_ripcbinfo, ifp);
1187 	in_pcbpurgeif0(&V_udbinfo, ifp);
1188 	in_purgemaddrs(ifp);
1189 }
1190 
1191 /*
1192  * Delete all IPv4 multicast address records, and associated link-layer
1193  * multicast address records, associated with ifp.
1194  * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1195  * XXX This should not race with ifma_protospec being set during
1196  * a new allocation, if it does, we have bigger problems.
1197  */
1198 static void
1199 in_purgemaddrs(struct ifnet *ifp)
1200 {
1201 	LIST_HEAD(,in_multi) purgeinms;
1202 	struct in_multi		*inm, *tinm;
1203 	struct ifmultiaddr	*ifma;
1204 
1205 	LIST_INIT(&purgeinms);
1206 	IN_MULTI_LOCK();
1207 
1208 	/*
1209 	 * Extract list of in_multi associated with the detaching ifp
1210 	 * which the PF_INET layer is about to release.
1211 	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1212 	 * by code further down.
1213 	 */
1214 	IF_ADDR_RLOCK(ifp);
1215 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1216 		if (ifma->ifma_addr->sa_family != AF_INET ||
1217 		    ifma->ifma_protospec == NULL)
1218 			continue;
1219 #if 0
1220 		KASSERT(ifma->ifma_protospec != NULL,
1221 		    ("%s: ifma_protospec is NULL", __func__));
1222 #endif
1223 		inm = (struct in_multi *)ifma->ifma_protospec;
1224 		LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
1225 	}
1226 	IF_ADDR_RUNLOCK(ifp);
1227 
1228 	LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
1229 		LIST_REMOVE(inm, inm_link);
1230 		inm_release_locked(inm);
1231 	}
1232 	igmp_ifdetach(ifp);
1233 
1234 	IN_MULTI_UNLOCK();
1235 }
1236 
1237 struct in_llentry {
1238 	struct llentry		base;
1239 	struct sockaddr_in	l3_addr4;
1240 };
1241 
1242 /*
1243  * Deletes an address from the address table.
1244  * This function is called by the timer functions
1245  * such as arptimer() and nd6_llinfo_timer(), and
1246  * the caller does the locking.
1247  */
1248 static void
1249 in_lltable_free(struct lltable *llt, struct llentry *lle)
1250 {
1251 	LLE_WUNLOCK(lle);
1252 	LLE_LOCK_DESTROY(lle);
1253 	free(lle, M_LLTABLE);
1254 }
1255 
1256 static struct llentry *
1257 in_lltable_new(const struct sockaddr *l3addr, u_int flags)
1258 {
1259 	struct in_llentry *lle;
1260 
1261 	lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
1262 	if (lle == NULL)		/* NB: caller generates msg */
1263 		return NULL;
1264 
1265 	/*
1266 	 * For IPv4 this will trigger "arpresolve" to generate
1267 	 * an ARP request.
1268 	 */
1269 	lle->base.la_expire = time_uptime; /* mark expired */
1270 	lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
1271 	lle->base.lle_refcnt = 1;
1272 	lle->base.lle_free = in_lltable_free;
1273 	LLE_LOCK_INIT(&lle->base);
1274 	callout_init_rw(&lle->base.la_timer, &lle->base.lle_lock,
1275 	    CALLOUT_RETURNUNLOCKED);
1276 
1277 	return (&lle->base);
1278 }
1279 
1280 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m)	(			\
1281 	    (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
1282 
1283 static void
1284 in_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix,
1285     const struct sockaddr *mask, u_int flags)
1286 {
1287 	const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
1288 	const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
1289 	struct llentry *lle, *next;
1290 	int i;
1291 	size_t pkts_dropped;
1292 
1293 	IF_AFDATA_WLOCK(llt->llt_ifp);
1294 	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
1295 		LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
1296 			/*
1297 			 * (flags & LLE_STATIC) means deleting all entries
1298 			 * including static ARP entries.
1299 			 */
1300 			if (IN_ARE_MASKED_ADDR_EQUAL(satosin(L3_ADDR(lle)),
1301 			    pfx, msk) && ((flags & LLE_STATIC) ||
1302 			    !(lle->la_flags & LLE_STATIC))) {
1303 				LLE_WLOCK(lle);
1304 				if (callout_stop(&lle->la_timer))
1305 					LLE_REMREF(lle);
1306 				pkts_dropped = llentry_free(lle);
1307 				ARPSTAT_ADD(dropped, pkts_dropped);
1308 			}
1309 		}
1310 	}
1311 	IF_AFDATA_WUNLOCK(llt->llt_ifp);
1312 }
1313 
1314 
1315 static int
1316 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1317 {
1318 	struct rtentry *rt;
1319 
1320 	KASSERT(l3addr->sa_family == AF_INET,
1321 	    ("sin_family %d", l3addr->sa_family));
1322 
1323 	/* XXX rtalloc1 should take a const param */
1324 	rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
1325 
1326 	if (rt == NULL)
1327 		return (EINVAL);
1328 
1329 	/*
1330 	 * If the gateway for an existing host route matches the target L3
1331 	 * address, which is a special route inserted by some implementation
1332 	 * such as MANET, and the interface is of the correct type, then
1333 	 * allow for ARP to proceed.
1334 	 */
1335 	if (rt->rt_flags & RTF_GATEWAY) {
1336 		if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp ||
1337 		    rt->rt_ifp->if_type != IFT_ETHER ||
1338 		    (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1339 		    memcmp(rt->rt_gateway->sa_data, l3addr->sa_data,
1340 		    sizeof(in_addr_t)) != 0) {
1341 			RTFREE_LOCKED(rt);
1342 			return (EINVAL);
1343 		}
1344 	}
1345 
1346 	/*
1347 	 * Make sure that at least the destination address is covered
1348 	 * by the route. This is for handling the case where 2 or more
1349 	 * interfaces have the same prefix. An incoming packet arrives
1350 	 * on one interface and the corresponding outgoing packet leaves
1351 	 * another interface.
1352 	 */
1353 	if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) {
1354 		const char *sa, *mask, *addr, *lim;
1355 		int len;
1356 
1357 		mask = (const char *)rt_mask(rt);
1358 		/*
1359 		 * Just being extra cautious to avoid some custom
1360 		 * code getting into trouble.
1361 		 */
1362 		if (mask == NULL) {
1363 			RTFREE_LOCKED(rt);
1364 			return (EINVAL);
1365 		}
1366 
1367 		sa = (const char *)rt_key(rt);
1368 		addr = (const char *)l3addr;
1369 		len = ((const struct sockaddr_in *)l3addr)->sin_len;
1370 		lim = addr + len;
1371 
1372 		for ( ; addr < lim; sa++, mask++, addr++) {
1373 			if ((*sa ^ *addr) & *mask) {
1374 #ifdef DIAGNOSTIC
1375 				log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
1376 				    inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
1377 #endif
1378 				RTFREE_LOCKED(rt);
1379 				return (EINVAL);
1380 			}
1381 		}
1382 	}
1383 
1384 	RTFREE_LOCKED(rt);
1385 	return (0);
1386 }
1387 
1388 /*
1389  * Return NULL if not found or marked for deletion.
1390  * If found return lle read locked.
1391  */
1392 static struct llentry *
1393 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1394 {
1395 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1396 	struct ifnet *ifp = llt->llt_ifp;
1397 	struct llentry *lle;
1398 	struct llentries *lleh;
1399 	u_int hashkey;
1400 
1401 	IF_AFDATA_LOCK_ASSERT(ifp);
1402 	KASSERT(l3addr->sa_family == AF_INET,
1403 	    ("sin_family %d", l3addr->sa_family));
1404 
1405 	hashkey = sin->sin_addr.s_addr;
1406 	lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
1407 	LIST_FOREACH(lle, lleh, lle_next) {
1408 		struct sockaddr_in *sa2 = satosin(L3_ADDR(lle));
1409 		if (lle->la_flags & LLE_DELETED)
1410 			continue;
1411 		if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
1412 			break;
1413 	}
1414 	if (lle == NULL) {
1415 #ifdef DIAGNOSTIC
1416 		if (flags & LLE_DELETE)
1417 			log(LOG_INFO, "interface address is missing from cache = %p  in delete\n", lle);
1418 #endif
1419 		if (!(flags & LLE_CREATE))
1420 			return (NULL);
1421 		/*
1422 		 * A route that covers the given address must have
1423 		 * been installed 1st because we are doing a resolution,
1424 		 * verify this.
1425 		 */
1426 		if (!(flags & LLE_IFADDR) &&
1427 		    in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1428 			goto done;
1429 
1430 		lle = in_lltable_new(l3addr, flags);
1431 		if (lle == NULL) {
1432 			log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1433 			goto done;
1434 		}
1435 		lle->la_flags = flags & ~LLE_CREATE;
1436 		if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
1437 			bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
1438 			lle->la_flags |= (LLE_VALID | LLE_STATIC);
1439 		}
1440 
1441 		lle->lle_tbl  = llt;
1442 		lle->lle_head = lleh;
1443 		lle->la_flags |= LLE_LINKED;
1444 		LIST_INSERT_HEAD(lleh, lle, lle_next);
1445 	} else if (flags & LLE_DELETE) {
1446 		if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
1447 			LLE_WLOCK(lle);
1448 			lle->la_flags |= LLE_DELETED;
1449 			EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1450 #ifdef DIAGNOSTIC
1451 			log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1452 #endif
1453 			if ((lle->la_flags &
1454 			    (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
1455 				llentry_free(lle);
1456 			else
1457 				LLE_WUNLOCK(lle);
1458 		}
1459 		lle = (void *)-1;
1460 
1461 	}
1462 	if (LLE_IS_VALID(lle)) {
1463 		if (flags & LLE_EXCLUSIVE)
1464 			LLE_WLOCK(lle);
1465 		else
1466 			LLE_RLOCK(lle);
1467 	}
1468 done:
1469 	return (lle);
1470 }
1471 
1472 static int
1473 in_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
1474 {
1475 #define	SIN(lle)	((struct sockaddr_in *) L3_ADDR(lle))
1476 	struct ifnet *ifp = llt->llt_ifp;
1477 	struct llentry *lle;
1478 	/* XXX stack use */
1479 	struct {
1480 		struct rt_msghdr	rtm;
1481 		struct sockaddr_in	sin;
1482 		struct sockaddr_dl	sdl;
1483 	} arpc;
1484 	int error, i;
1485 
1486 	LLTABLE_LOCK_ASSERT();
1487 
1488 	error = 0;
1489 	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
1490 		LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
1491 			struct sockaddr_dl *sdl;
1492 
1493 			/* skip deleted entries */
1494 			if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1495 				continue;
1496 			/* Skip if jailed and not a valid IP of the prison. */
1497 			if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
1498 				continue;
1499 			/*
1500 			 * produce a msg made of:
1501 			 *  struct rt_msghdr;
1502 			 *  struct sockaddr_in; (IPv4)
1503 			 *  struct sockaddr_dl;
1504 			 */
1505 			bzero(&arpc, sizeof(arpc));
1506 			arpc.rtm.rtm_msglen = sizeof(arpc);
1507 			arpc.rtm.rtm_version = RTM_VERSION;
1508 			arpc.rtm.rtm_type = RTM_GET;
1509 			arpc.rtm.rtm_flags = RTF_UP;
1510 			arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1511 			arpc.sin.sin_family = AF_INET;
1512 			arpc.sin.sin_len = sizeof(arpc.sin);
1513 			arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
1514 
1515 			/* publish */
1516 			if (lle->la_flags & LLE_PUB)
1517 				arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1518 
1519 			sdl = &arpc.sdl;
1520 			sdl->sdl_family = AF_LINK;
1521 			sdl->sdl_len = sizeof(*sdl);
1522 			sdl->sdl_index = ifp->if_index;
1523 			sdl->sdl_type = ifp->if_type;
1524 			if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1525 				sdl->sdl_alen = ifp->if_addrlen;
1526 				bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1527 			} else {
1528 				sdl->sdl_alen = 0;
1529 				bzero(LLADDR(sdl), ifp->if_addrlen);
1530 			}
1531 
1532 			arpc.rtm.rtm_rmx.rmx_expire =
1533 			    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1534 			arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1535 			if (lle->la_flags & LLE_STATIC)
1536 				arpc.rtm.rtm_flags |= RTF_STATIC;
1537 			arpc.rtm.rtm_index = ifp->if_index;
1538 			error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1539 			if (error)
1540 				break;
1541 		}
1542 	}
1543 	return error;
1544 #undef SIN
1545 }
1546 
1547 void *
1548 in_domifattach(struct ifnet *ifp)
1549 {
1550 	struct in_ifinfo *ii;
1551 	struct lltable *llt;
1552 
1553 	ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1554 
1555 	llt = lltable_init(ifp, AF_INET);
1556 	if (llt != NULL) {
1557 		llt->llt_prefix_free = in_lltable_prefix_free;
1558 		llt->llt_lookup = in_lltable_lookup;
1559 		llt->llt_dump = in_lltable_dump;
1560 	}
1561 	ii->ii_llt = llt;
1562 
1563 	ii->ii_igmp = igmp_domifattach(ifp);
1564 
1565 	return ii;
1566 }
1567 
1568 void
1569 in_domifdetach(struct ifnet *ifp, void *aux)
1570 {
1571 	struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1572 
1573 	igmp_domifdetach(ifp);
1574 	lltable_free(ii->ii_llt);
1575 	free(ii, M_IFADDR);
1576 }
1577