xref: /freebsd/sys/netinet/in.c (revision d34048812292b714a0bf99967270d18fe3097c62)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (C) 2001 WIDE Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)in.c	8.4 (Berkeley) 1/9/95
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include "opt_mpath.h"
39 
40 #include <sys/param.h>
41 #include <sys/eventhandler.h>
42 #include <sys/systm.h>
43 #include <sys/sockio.h>
44 #include <sys/malloc.h>
45 #include <sys/priv.h>
46 #include <sys/socket.h>
47 #include <sys/jail.h>
48 #include <sys/kernel.h>
49 #include <sys/lock.h>
50 #include <sys/proc.h>
51 #include <sys/rmlock.h>
52 #include <sys/sysctl.h>
53 #include <sys/syslog.h>
54 #include <sys/sx.h>
55 
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_arp.h>
59 #include <net/if_dl.h>
60 #include <net/if_llatbl.h>
61 #include <net/if_types.h>
62 #include <net/route.h>
63 #include <net/vnet.h>
64 
65 #include <netinet/if_ether.h>
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/ip_var.h>
70 #include <netinet/ip_carp.h>
71 #include <netinet/igmp_var.h>
72 #include <netinet/udp.h>
73 #include <netinet/udp_var.h>
74 
75 static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
76 static int in_difaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
77 
78 static void	in_socktrim(struct sockaddr_in *);
79 static void	in_purgemaddrs(struct ifnet *);
80 
81 VNET_DEFINE_STATIC(int, nosameprefix);
82 #define	V_nosameprefix			VNET(nosameprefix)
83 SYSCTL_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_VNET | CTLFLAG_RW,
84 	&VNET_NAME(nosameprefix), 0,
85 	"Refuse to create same prefixes on different interfaces");
86 
87 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
88 #define	V_ripcbinfo			VNET(ripcbinfo)
89 
90 static struct sx in_control_sx;
91 SX_SYSINIT(in_control_sx, &in_control_sx, "in_control");
92 
93 /*
94  * Return 1 if an internet address is for a ``local'' host
95  * (one to which we have a connection).
96  */
97 int
98 in_localaddr(struct in_addr in)
99 {
100 	struct rm_priotracker in_ifa_tracker;
101 	u_long i = ntohl(in.s_addr);
102 	struct in_ifaddr *ia;
103 
104 	IN_IFADDR_RLOCK(&in_ifa_tracker);
105 	CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
106 		if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
107 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
108 			return (1);
109 		}
110 	}
111 	IN_IFADDR_RUNLOCK(&in_ifa_tracker);
112 	return (0);
113 }
114 
115 /*
116  * Return 1 if an internet address is for the local host and configured
117  * on one of its interfaces.
118  */
119 int
120 in_localip(struct in_addr in)
121 {
122 	struct rm_priotracker in_ifa_tracker;
123 	struct in_ifaddr *ia;
124 
125 	IN_IFADDR_RLOCK(&in_ifa_tracker);
126 	LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
127 		if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
128 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
129 			return (1);
130 		}
131 	}
132 	IN_IFADDR_RUNLOCK(&in_ifa_tracker);
133 	return (0);
134 }
135 
136 /*
137  * Return 1 if an internet address is configured on an interface.
138  */
139 int
140 in_ifhasaddr(struct ifnet *ifp, struct in_addr in)
141 {
142 	struct ifaddr *ifa;
143 	struct in_ifaddr *ia;
144 
145 	IF_ADDR_RLOCK(ifp);
146 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
147 		if (ifa->ifa_addr->sa_family != AF_INET)
148 			continue;
149 		ia = (struct in_ifaddr *)ifa;
150 		if (ia->ia_addr.sin_addr.s_addr == in.s_addr) {
151 			IF_ADDR_RUNLOCK(ifp);
152 			return (1);
153 		}
154 	}
155 	IF_ADDR_RUNLOCK(ifp);
156 
157 	return (0);
158 }
159 
160 /*
161  * Return a reference to the interface address which is different to
162  * the supplied one but with same IP address value.
163  */
164 static struct in_ifaddr *
165 in_localip_more(struct in_ifaddr *ia)
166 {
167 	struct rm_priotracker in_ifa_tracker;
168 	in_addr_t in = IA_SIN(ia)->sin_addr.s_addr;
169 	struct in_ifaddr *it;
170 
171 	IN_IFADDR_RLOCK(&in_ifa_tracker);
172 	LIST_FOREACH(it, INADDR_HASH(in), ia_hash) {
173 		if (it != ia && IA_SIN(it)->sin_addr.s_addr == in) {
174 			ifa_ref(&it->ia_ifa);
175 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
176 			return (it);
177 		}
178 	}
179 	IN_IFADDR_RUNLOCK(&in_ifa_tracker);
180 
181 	return (NULL);
182 }
183 
184 /*
185  * Determine whether an IP address is in a reserved set of addresses
186  * that may not be forwarded, or whether datagrams to that destination
187  * may be forwarded.
188  */
189 int
190 in_canforward(struct in_addr in)
191 {
192 	u_long i = ntohl(in.s_addr);
193 	u_long net;
194 
195 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
196 		return (0);
197 	if (IN_CLASSA(i)) {
198 		net = i & IN_CLASSA_NET;
199 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
200 			return (0);
201 	}
202 	return (1);
203 }
204 
205 /*
206  * Trim a mask in a sockaddr
207  */
208 static void
209 in_socktrim(struct sockaddr_in *ap)
210 {
211     char *cplim = (char *) &ap->sin_addr;
212     char *cp = (char *) (&ap->sin_addr + 1);
213 
214     ap->sin_len = 0;
215     while (--cp >= cplim)
216 	if (*cp) {
217 	    (ap)->sin_len = cp - (char *) (ap) + 1;
218 	    break;
219 	}
220 }
221 
222 /*
223  * Generic internet control operations (ioctl's).
224  */
225 int
226 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
227     struct thread *td)
228 {
229 	struct ifreq *ifr = (struct ifreq *)data;
230 	struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
231 	struct ifaddr *ifa;
232 	struct in_ifaddr *ia;
233 	int error;
234 
235 	if (ifp == NULL)
236 		return (EADDRNOTAVAIL);
237 
238 	/*
239 	 * Filter out 4 ioctls we implement directly.  Forward the rest
240 	 * to specific functions and ifp->if_ioctl().
241 	 */
242 	switch (cmd) {
243 	case SIOCGIFADDR:
244 	case SIOCGIFBRDADDR:
245 	case SIOCGIFDSTADDR:
246 	case SIOCGIFNETMASK:
247 		break;
248 	case SIOCDIFADDR:
249 		sx_xlock(&in_control_sx);
250 		error = in_difaddr_ioctl(cmd, data, ifp, td);
251 		sx_xunlock(&in_control_sx);
252 		return (error);
253 	case OSIOCAIFADDR:	/* 9.x compat */
254 	case SIOCAIFADDR:
255 		sx_xlock(&in_control_sx);
256 		error = in_aifaddr_ioctl(cmd, data, ifp, td);
257 		sx_xunlock(&in_control_sx);
258 		return (error);
259 	case SIOCSIFADDR:
260 	case SIOCSIFBRDADDR:
261 	case SIOCSIFDSTADDR:
262 	case SIOCSIFNETMASK:
263 		/* We no longer support that old commands. */
264 		return (EINVAL);
265 	default:
266 		if (ifp->if_ioctl == NULL)
267 			return (EOPNOTSUPP);
268 		return ((*ifp->if_ioctl)(ifp, cmd, data));
269 	}
270 
271 	if (addr->sin_addr.s_addr != INADDR_ANY &&
272 	    prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0)
273 		return (EADDRNOTAVAIL);
274 
275 	/*
276 	 * Find address for this interface, if it exists.  If an
277 	 * address was specified, find that one instead of the
278 	 * first one on the interface, if possible.
279 	 */
280 	IF_ADDR_RLOCK(ifp);
281 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
282 		if (ifa->ifa_addr->sa_family != AF_INET)
283 			continue;
284 		ia = (struct in_ifaddr *)ifa;
285 		if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
286 			break;
287 	}
288 	if (ifa == NULL)
289 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
290 			if (ifa->ifa_addr->sa_family == AF_INET) {
291 				ia = (struct in_ifaddr *)ifa;
292 				if (prison_check_ip4(td->td_ucred,
293 				    &ia->ia_addr.sin_addr) == 0)
294 					break;
295 			}
296 
297 	if (ifa == NULL) {
298 		IF_ADDR_RUNLOCK(ifp);
299 		return (EADDRNOTAVAIL);
300 	}
301 
302 	error = 0;
303 	switch (cmd) {
304 	case SIOCGIFADDR:
305 		*addr = ia->ia_addr;
306 		break;
307 
308 	case SIOCGIFBRDADDR:
309 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
310 			error = EINVAL;
311 			break;
312 		}
313 		*addr = ia->ia_broadaddr;
314 		break;
315 
316 	case SIOCGIFDSTADDR:
317 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
318 			error = EINVAL;
319 			break;
320 		}
321 		*addr = ia->ia_dstaddr;
322 		break;
323 
324 	case SIOCGIFNETMASK:
325 		*addr = ia->ia_sockmask;
326 		break;
327 	}
328 
329 	IF_ADDR_RUNLOCK(ifp);
330 
331 	return (error);
332 }
333 
334 static int
335 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
336 {
337 	const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
338 	const struct sockaddr_in *addr = &ifra->ifra_addr;
339 	const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
340 	const struct sockaddr_in *mask = &ifra->ifra_mask;
341 	const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
342 	const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
343 	struct ifaddr *ifa;
344 	struct in_ifaddr *ia;
345 	bool iaIsFirst;
346 	int error = 0;
347 
348 	error = priv_check(td, PRIV_NET_ADDIFADDR);
349 	if (error)
350 		return (error);
351 
352 	/*
353 	 * ifra_addr must be present and be of INET family.
354 	 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
355 	 */
356 	if (addr->sin_len != sizeof(struct sockaddr_in) ||
357 	    addr->sin_family != AF_INET)
358 		return (EINVAL);
359 	if (broadaddr->sin_len != 0 &&
360 	    (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
361 	    broadaddr->sin_family != AF_INET))
362 		return (EINVAL);
363 	if (mask->sin_len != 0 &&
364 	    (mask->sin_len != sizeof(struct sockaddr_in) ||
365 	    mask->sin_family != AF_INET))
366 		return (EINVAL);
367 	if ((ifp->if_flags & IFF_POINTOPOINT) &&
368 	    (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
369 	     dstaddr->sin_addr.s_addr == INADDR_ANY))
370 		return (EDESTADDRREQ);
371 	if (vhid > 0 && carp_attach_p == NULL)
372 		return (EPROTONOSUPPORT);
373 
374 	/*
375 	 * See whether address already exist.
376 	 */
377 	iaIsFirst = true;
378 	ia = NULL;
379 	IF_ADDR_RLOCK(ifp);
380 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
381 		struct in_ifaddr *it;
382 
383 		if (ifa->ifa_addr->sa_family != AF_INET)
384 			continue;
385 
386 		it = (struct in_ifaddr *)ifa;
387 		iaIsFirst = false;
388 		if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
389 		    prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0)
390 			ia = it;
391 	}
392 	IF_ADDR_RUNLOCK(ifp);
393 
394 	if (ia != NULL)
395 		(void )in_difaddr_ioctl(cmd, data, ifp, td);
396 
397 	ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
398 	ia = (struct in_ifaddr *)ifa;
399 	ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
400 	ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
401 	ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
402 	callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock,
403 	    CALLOUT_RETURNUNLOCKED);
404 
405 	ia->ia_ifp = ifp;
406 	ia->ia_addr = *addr;
407 	if (mask->sin_len != 0) {
408 		ia->ia_sockmask = *mask;
409 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
410 	} else {
411 		in_addr_t i = ntohl(addr->sin_addr.s_addr);
412 
413 		/*
414 	 	 * Be compatible with network classes, if netmask isn't
415 		 * supplied, guess it based on classes.
416 	 	 */
417 		if (IN_CLASSA(i))
418 			ia->ia_subnetmask = IN_CLASSA_NET;
419 		else if (IN_CLASSB(i))
420 			ia->ia_subnetmask = IN_CLASSB_NET;
421 		else
422 			ia->ia_subnetmask = IN_CLASSC_NET;
423 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
424 	}
425 	ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
426 	in_socktrim(&ia->ia_sockmask);
427 
428 	if (ifp->if_flags & IFF_BROADCAST) {
429 		if (broadaddr->sin_len != 0) {
430 			ia->ia_broadaddr = *broadaddr;
431 		} else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
432 			ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
433 			ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
434 			ia->ia_broadaddr.sin_family = AF_INET;
435 		} else {
436 			ia->ia_broadaddr.sin_addr.s_addr =
437 			    htonl(ia->ia_subnet | ~ia->ia_subnetmask);
438 			ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
439 			ia->ia_broadaddr.sin_family = AF_INET;
440 		}
441 	}
442 
443 	if (ifp->if_flags & IFF_POINTOPOINT)
444 		ia->ia_dstaddr = *dstaddr;
445 
446 	/* XXXGL: rtinit() needs this strange assignment. */
447 	if (ifp->if_flags & IFF_LOOPBACK)
448                 ia->ia_dstaddr = ia->ia_addr;
449 
450 	if (vhid != 0) {
451 		error = (*carp_attach_p)(&ia->ia_ifa, vhid);
452 		if (error)
453 			return (error);
454 	}
455 
456 	/* if_addrhead is already referenced by ifa_alloc() */
457 	IF_ADDR_WLOCK(ifp);
458 	CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
459 	IF_ADDR_WUNLOCK(ifp);
460 
461 	ifa_ref(ifa);			/* in_ifaddrhead */
462 	IN_IFADDR_WLOCK();
463 	CK_STAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
464 	LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
465 	IN_IFADDR_WUNLOCK();
466 
467 	/*
468 	 * Give the interface a chance to initialize
469 	 * if this is its first address,
470 	 * and to validate the address if necessary.
471 	 */
472 	if (ifp->if_ioctl != NULL) {
473 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
474 		if (error)
475 			goto fail1;
476 	}
477 
478 	/*
479 	 * Add route for the network.
480 	 */
481 	if (vhid == 0) {
482 		int flags = RTF_UP;
483 
484 		if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
485 			flags |= RTF_HOST;
486 
487 		error = in_addprefix(ia, flags);
488 		if (error)
489 			goto fail1;
490 	}
491 
492 	/*
493 	 * Add a loopback route to self.
494 	 */
495 	if (vhid == 0 && (ifp->if_flags & IFF_LOOPBACK) == 0 &&
496 	    ia->ia_addr.sin_addr.s_addr != INADDR_ANY &&
497 	    !((ifp->if_flags & IFF_POINTOPOINT) &&
498 	     ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)) {
499 		struct in_ifaddr *eia;
500 
501 		eia = in_localip_more(ia);
502 
503 		if (eia == NULL) {
504 			error = ifa_add_loopback_route((struct ifaddr *)ia,
505 			    (struct sockaddr *)&ia->ia_addr);
506 			if (error)
507 				goto fail2;
508 		} else
509 			ifa_free(&eia->ia_ifa);
510 	}
511 
512 	if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
513 		struct in_addr allhosts_addr;
514 		struct in_ifinfo *ii;
515 
516 		ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
517 		allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
518 
519 		error = in_joingroup(ifp, &allhosts_addr, NULL,
520 			&ii->ii_allhosts);
521 	}
522 
523 	/*
524 	 * Note: we don't need extra reference for ifa, since we called
525 	 * with sx lock held, and ifaddr can not be deleted in concurrent
526 	 * thread.
527 	 */
528 	EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, ifa, IFADDR_EVENT_ADD);
529 
530 	return (error);
531 
532 fail2:
533 	if (vhid == 0)
534 		(void )in_scrubprefix(ia, LLE_STATIC);
535 
536 fail1:
537 	if (ia->ia_ifa.ifa_carp)
538 		(*carp_detach_p)(&ia->ia_ifa, false);
539 
540 	IF_ADDR_WLOCK(ifp);
541 	CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
542 	IF_ADDR_WUNLOCK(ifp);
543 	ifa_free(&ia->ia_ifa);		/* if_addrhead */
544 
545 	IN_IFADDR_WLOCK();
546 	CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
547 	LIST_REMOVE(ia, ia_hash);
548 	IN_IFADDR_WUNLOCK();
549 	ifa_free(&ia->ia_ifa);		/* in_ifaddrhead */
550 
551 	return (error);
552 }
553 
554 static int
555 in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
556 {
557 	const struct ifreq *ifr = (struct ifreq *)data;
558 	const struct sockaddr_in *addr = (const struct sockaddr_in *)
559 	    &ifr->ifr_addr;
560 	struct ifaddr *ifa;
561 	struct in_ifaddr *ia;
562 	bool deleteAny, iaIsLast;
563 	int error;
564 
565 	if (td != NULL) {
566 		error = priv_check(td, PRIV_NET_DELIFADDR);
567 		if (error)
568 			return (error);
569 	}
570 
571 	if (addr->sin_len != sizeof(struct sockaddr_in) ||
572 	    addr->sin_family != AF_INET)
573 		deleteAny = true;
574 	else
575 		deleteAny = false;
576 
577 	iaIsLast = true;
578 	ia = NULL;
579 	IF_ADDR_WLOCK(ifp);
580 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
581 		struct in_ifaddr *it;
582 
583 		if (ifa->ifa_addr->sa_family != AF_INET)
584 			continue;
585 
586 		it = (struct in_ifaddr *)ifa;
587 		if (deleteAny && ia == NULL && (td == NULL ||
588 		    prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0))
589 			ia = it;
590 
591 		if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
592 		    (td == NULL || prison_check_ip4(td->td_ucred,
593 		    &addr->sin_addr) == 0))
594 			ia = it;
595 
596 		if (it != ia)
597 			iaIsLast = false;
598 	}
599 
600 	if (ia == NULL) {
601 		IF_ADDR_WUNLOCK(ifp);
602 		return (EADDRNOTAVAIL);
603 	}
604 
605 	CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
606 	IF_ADDR_WUNLOCK(ifp);
607 	ifa_free(&ia->ia_ifa);		/* if_addrhead */
608 
609 	IN_IFADDR_WLOCK();
610 	CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
611 	LIST_REMOVE(ia, ia_hash);
612 	IN_IFADDR_WUNLOCK();
613 
614 	/*
615 	 * in_scrubprefix() kills the interface route.
616 	 */
617 	in_scrubprefix(ia, LLE_STATIC);
618 
619 	/*
620 	 * in_ifadown gets rid of all the rest of
621 	 * the routes.  This is not quite the right
622 	 * thing to do, but at least if we are running
623 	 * a routing process they will come back.
624 	 */
625 	in_ifadown(&ia->ia_ifa, 1);
626 
627 	if (ia->ia_ifa.ifa_carp)
628 		(*carp_detach_p)(&ia->ia_ifa, cmd == SIOCAIFADDR);
629 
630 	/*
631 	 * If this is the last IPv4 address configured on this
632 	 * interface, leave the all-hosts group.
633 	 * No state-change report need be transmitted.
634 	 */
635 	if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
636 		struct in_ifinfo *ii;
637 
638 		ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
639 		if (ii->ii_allhosts) {
640 			(void)in_leavegroup(ii->ii_allhosts, NULL);
641 			ii->ii_allhosts = NULL;
642 		}
643 	}
644 
645 	IF_ADDR_WLOCK(ifp);
646 	if (callout_stop(&ia->ia_garp_timer) == 1) {
647 		ifa_free(&ia->ia_ifa);
648 	}
649 	IF_ADDR_WUNLOCK(ifp);
650 
651 	EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
652 	    IFADDR_EVENT_DEL);
653 	ifa_free(&ia->ia_ifa);		/* in_ifaddrhead */
654 
655 	return (0);
656 }
657 
658 #define rtinitflags(x) \
659 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
660 	    ? RTF_HOST : 0)
661 
662 /*
663  * Check if we have a route for the given prefix already or add one accordingly.
664  */
665 int
666 in_addprefix(struct in_ifaddr *target, int flags)
667 {
668 	struct rm_priotracker in_ifa_tracker;
669 	struct in_ifaddr *ia;
670 	struct in_addr prefix, mask, p, m;
671 	int error;
672 
673 	if ((flags & RTF_HOST) != 0) {
674 		prefix = target->ia_dstaddr.sin_addr;
675 		mask.s_addr = 0;
676 	} else {
677 		prefix = target->ia_addr.sin_addr;
678 		mask = target->ia_sockmask.sin_addr;
679 		prefix.s_addr &= mask.s_addr;
680 	}
681 
682 	IN_IFADDR_RLOCK(&in_ifa_tracker);
683 	/* Look for an existing address with the same prefix, mask, and fib */
684 	CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
685 		if (rtinitflags(ia)) {
686 			p = ia->ia_dstaddr.sin_addr;
687 
688 			if (prefix.s_addr != p.s_addr)
689 				continue;
690 		} else {
691 			p = ia->ia_addr.sin_addr;
692 			m = ia->ia_sockmask.sin_addr;
693 			p.s_addr &= m.s_addr;
694 
695 			if (prefix.s_addr != p.s_addr ||
696 			    mask.s_addr != m.s_addr)
697 				continue;
698 		}
699 		if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
700 			continue;
701 
702 		/*
703 		 * If we got a matching prefix route inserted by other
704 		 * interface address, we are done here.
705 		 */
706 		if (ia->ia_flags & IFA_ROUTE) {
707 #ifdef RADIX_MPATH
708 			if (ia->ia_addr.sin_addr.s_addr ==
709 			    target->ia_addr.sin_addr.s_addr) {
710 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
711 				return (EEXIST);
712 			} else
713 				break;
714 #endif
715 			if (V_nosameprefix) {
716 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
717 				return (EEXIST);
718 			} else {
719 				int fibnum;
720 
721 				fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS :
722 					target->ia_ifp->if_fib;
723 				rt_addrmsg(RTM_ADD, &target->ia_ifa, fibnum);
724 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
725 				return (0);
726 			}
727 		}
728 	}
729 	IN_IFADDR_RUNLOCK(&in_ifa_tracker);
730 
731 	/*
732 	 * No-one seem to have this prefix route, so we try to insert it.
733 	 */
734 	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
735 	if (!error)
736 		target->ia_flags |= IFA_ROUTE;
737 	return (error);
738 }
739 
740 /*
741  * Removes either all lle entries for given @ia, or lle
742  * corresponding to @ia address.
743  */
744 static void
745 in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags)
746 {
747 	struct sockaddr_in addr, mask;
748 	struct sockaddr *saddr, *smask;
749 	struct ifnet *ifp;
750 
751 	saddr = (struct sockaddr *)&addr;
752 	bzero(&addr, sizeof(addr));
753 	addr.sin_len = sizeof(addr);
754 	addr.sin_family = AF_INET;
755 	smask = (struct sockaddr *)&mask;
756 	bzero(&mask, sizeof(mask));
757 	mask.sin_len = sizeof(mask);
758 	mask.sin_family = AF_INET;
759 	mask.sin_addr.s_addr = ia->ia_subnetmask;
760 	ifp = ia->ia_ifp;
761 
762 	if (all) {
763 
764 		/*
765 		 * Remove all L2 entries matching given prefix.
766 		 * Convert address to host representation to avoid
767 		 * doing this on every callback. ia_subnetmask is already
768 		 * stored in host representation.
769 		 */
770 		addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr);
771 		lltable_prefix_free(AF_INET, saddr, smask, flags);
772 	} else {
773 		/* Remove interface address only */
774 		addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr;
775 		lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr);
776 	}
777 }
778 
779 /*
780  * If there is no other address in the system that can serve a route to the
781  * same prefix, remove the route.  Hand over the route to the new address
782  * otherwise.
783  */
784 int
785 in_scrubprefix(struct in_ifaddr *target, u_int flags)
786 {
787 	struct rm_priotracker in_ifa_tracker;
788 	struct in_ifaddr *ia;
789 	struct in_addr prefix, mask, p, m;
790 	int error = 0;
791 
792 	/*
793 	 * Remove the loopback route to the interface address.
794 	 */
795 	if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
796 	    !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
797 	    (flags & LLE_STATIC)) {
798 		struct in_ifaddr *eia;
799 
800 		/*
801 		 * XXXME: add fib-aware in_localip.
802 		 * We definitely don't want to switch between
803 		 * prefixes in different fibs.
804 		 */
805 		eia = in_localip_more(target);
806 
807 		if (eia != NULL) {
808 			error = ifa_switch_loopback_route((struct ifaddr *)eia,
809 			    (struct sockaddr *)&target->ia_addr);
810 			ifa_free(&eia->ia_ifa);
811 		} else {
812 			error = ifa_del_loopback_route((struct ifaddr *)target,
813 			    (struct sockaddr *)&target->ia_addr);
814 		}
815 	}
816 
817 	if (rtinitflags(target)) {
818 		prefix = target->ia_dstaddr.sin_addr;
819 		mask.s_addr = 0;
820 	} else {
821 		prefix = target->ia_addr.sin_addr;
822 		mask = target->ia_sockmask.sin_addr;
823 		prefix.s_addr &= mask.s_addr;
824 	}
825 
826 	if ((target->ia_flags & IFA_ROUTE) == 0) {
827 		int fibnum;
828 
829 		fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS :
830 			target->ia_ifp->if_fib;
831 		rt_addrmsg(RTM_DELETE, &target->ia_ifa, fibnum);
832 
833 		/*
834 		 * Removing address from !IFF_UP interface or
835 		 * prefix which exists on other interface (along with route).
836 		 * No entries should exist here except target addr.
837 		 * Given that, delete this entry only.
838 		 */
839 		in_scrubprefixlle(target, 0, flags);
840 		return (0);
841 	}
842 
843 	IN_IFADDR_RLOCK(&in_ifa_tracker);
844 	CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
845 		if (rtinitflags(ia)) {
846 			p = ia->ia_dstaddr.sin_addr;
847 
848 			if (prefix.s_addr != p.s_addr)
849 				continue;
850 		} else {
851 			p = ia->ia_addr.sin_addr;
852 			m = ia->ia_sockmask.sin_addr;
853 			p.s_addr &= m.s_addr;
854 
855 			if (prefix.s_addr != p.s_addr ||
856 			    mask.s_addr != m.s_addr)
857 				continue;
858 		}
859 
860 		if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
861 			continue;
862 
863 		/*
864 		 * If we got a matching prefix address, move IFA_ROUTE and
865 		 * the route itself to it.  Make sure that routing daemons
866 		 * get a heads-up.
867 		 */
868 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
869 			ifa_ref(&ia->ia_ifa);
870 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
871 			error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
872 			    rtinitflags(target));
873 			if (error == 0)
874 				target->ia_flags &= ~IFA_ROUTE;
875 			else
876 				log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
877 					error);
878 			/* Scrub all entries IFF interface is different */
879 			in_scrubprefixlle(target, target->ia_ifp != ia->ia_ifp,
880 			    flags);
881 			error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
882 			    rtinitflags(ia) | RTF_UP);
883 			if (error == 0)
884 				ia->ia_flags |= IFA_ROUTE;
885 			else
886 				log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
887 					error);
888 			ifa_free(&ia->ia_ifa);
889 			return (error);
890 		}
891 	}
892 	IN_IFADDR_RUNLOCK(&in_ifa_tracker);
893 
894 	/*
895 	 * remove all L2 entries on the given prefix
896 	 */
897 	in_scrubprefixlle(target, 1, flags);
898 
899 	/*
900 	 * As no-one seem to have this prefix, we can remove the route.
901 	 */
902 	error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
903 	if (error == 0)
904 		target->ia_flags &= ~IFA_ROUTE;
905 	else
906 		log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
907 	return (error);
908 }
909 
910 #undef rtinitflags
911 
912 void
913 in_ifscrub_all(void)
914 {
915 	struct ifnet *ifp;
916 	struct ifaddr *ifa, *nifa;
917 	struct ifaliasreq ifr;
918 
919 	IFNET_RLOCK();
920 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
921 		/* Cannot lock here - lock recursion. */
922 		/* IF_ADDR_RLOCK(ifp); */
923 		CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
924 			if (ifa->ifa_addr->sa_family != AF_INET)
925 				continue;
926 
927 			/*
928 			 * This is ugly but the only way for legacy IP to
929 			 * cleanly remove addresses and everything attached.
930 			 */
931 			bzero(&ifr, sizeof(ifr));
932 			ifr.ifra_addr = *ifa->ifa_addr;
933 			if (ifa->ifa_dstaddr)
934 			ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
935 			(void)in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr,
936 			    ifp, NULL);
937 		}
938 		/* IF_ADDR_RUNLOCK(ifp); */
939 		in_purgemaddrs(ifp);
940 		igmp_domifdetach(ifp);
941 	}
942 	IFNET_RUNLOCK();
943 }
944 
945 int
946 in_ifaddr_broadcast(struct in_addr in, struct in_ifaddr *ia)
947 {
948 
949 	return ((in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
950 	     /*
951 	      * Check for old-style (host 0) broadcast, but
952 	      * taking into account that RFC 3021 obsoletes it.
953 	      */
954 	    (ia->ia_subnetmask != IN_RFC3021_MASK &&
955 	    ntohl(in.s_addr) == ia->ia_subnet)) &&
956 	     /*
957 	      * Check for an all one subnetmask. These
958 	      * only exist when an interface gets a secondary
959 	      * address.
960 	      */
961 	    ia->ia_subnetmask != (u_long)0xffffffff);
962 }
963 
964 /*
965  * Return 1 if the address might be a local broadcast address.
966  */
967 int
968 in_broadcast(struct in_addr in, struct ifnet *ifp)
969 {
970 	struct ifaddr *ifa;
971 	int found;
972 
973 	if (in.s_addr == INADDR_BROADCAST ||
974 	    in.s_addr == INADDR_ANY)
975 		return (1);
976 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
977 		return (0);
978 	found = 0;
979 	/*
980 	 * Look through the list of addresses for a match
981 	 * with a broadcast address.
982 	 */
983 	IF_ADDR_RLOCK(ifp);
984 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
985 		if (ifa->ifa_addr->sa_family == AF_INET &&
986 		    in_ifaddr_broadcast(in, (struct in_ifaddr *)ifa)) {
987 			found = 1;
988 			break;
989 		}
990 	IF_ADDR_RUNLOCK(ifp);
991 	return (found);
992 }
993 
994 /*
995  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
996  */
997 void
998 in_ifdetach(struct ifnet *ifp)
999 {
1000 	IN_MULTI_LOCK();
1001 	in_pcbpurgeif0(&V_ripcbinfo, ifp);
1002 	in_pcbpurgeif0(&V_udbinfo, ifp);
1003 	in_pcbpurgeif0(&V_ulitecbinfo, ifp);
1004 	in_purgemaddrs(ifp);
1005 	IN_MULTI_UNLOCK();
1006 }
1007 
1008 /*
1009  * Delete all IPv4 multicast address records, and associated link-layer
1010  * multicast address records, associated with ifp.
1011  * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1012  * XXX This should not race with ifma_protospec being set during
1013  * a new allocation, if it does, we have bigger problems.
1014  */
1015 static void
1016 in_purgemaddrs(struct ifnet *ifp)
1017 {
1018 	struct in_multi_head purgeinms;
1019 	struct in_multi		*inm;
1020 	struct ifmultiaddr	*ifma, *next;
1021 
1022 	SLIST_INIT(&purgeinms);
1023 	IN_MULTI_LIST_LOCK();
1024 
1025 	/*
1026 	 * Extract list of in_multi associated with the detaching ifp
1027 	 * which the PF_INET layer is about to release.
1028 	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1029 	 * by code further down.
1030 	 */
1031 	IF_ADDR_WLOCK(ifp);
1032  restart:
1033 	CK_STAILQ_FOREACH_SAFE(ifma, &ifp->if_multiaddrs, ifma_link, next) {
1034 		if (ifma->ifma_addr->sa_family != AF_INET ||
1035 		    ifma->ifma_protospec == NULL)
1036 			continue;
1037 		inm = (struct in_multi *)ifma->ifma_protospec;
1038 		inm_rele_locked(&purgeinms, inm);
1039 		if (__predict_false(ifma_restart)) {
1040 			ifma_restart = true;
1041 			goto restart;
1042 		}
1043 	}
1044 	IF_ADDR_WUNLOCK(ifp);
1045 
1046 	inm_release_list_deferred(&purgeinms);
1047 	igmp_ifdetach(ifp);
1048 	IN_MULTI_LIST_UNLOCK();
1049 }
1050 
1051 struct in_llentry {
1052 	struct llentry		base;
1053 };
1054 
1055 #define	IN_LLTBL_DEFAULT_HSIZE	32
1056 #define	IN_LLTBL_HASH(k, h) \
1057 	(((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
1058 
1059 /*
1060  * Do actual deallocation of @lle.
1061  */
1062 static void
1063 in_lltable_destroy_lle_unlocked(epoch_context_t ctx)
1064 {
1065 	struct llentry *lle;
1066 
1067 	lle = __containerof(ctx, struct llentry, lle_epoch_ctx);
1068 	LLE_LOCK_DESTROY(lle);
1069 	LLE_REQ_DESTROY(lle);
1070 	free(lle, M_LLTABLE);
1071 }
1072 
1073 /*
1074  * Called by the datapath to indicate that
1075  * the entry was used.
1076  */
1077 static void
1078 in_lltable_mark_used(struct llentry *lle)
1079 {
1080 
1081 	LLE_REQ_LOCK(lle);
1082 	lle->r_skip_req = 0;
1083 	LLE_REQ_UNLOCK(lle);
1084 }
1085 
1086 /*
1087  * Called by LLE_FREE_LOCKED when number of references
1088  * drops to zero.
1089  */
1090 static void
1091 in_lltable_destroy_lle(struct llentry *lle)
1092 {
1093 
1094 	LLE_WUNLOCK(lle);
1095 	epoch_call(net_epoch_preempt,  &lle->lle_epoch_ctx, in_lltable_destroy_lle_unlocked);
1096 }
1097 
1098 static struct llentry *
1099 in_lltable_new(struct in_addr addr4, u_int flags)
1100 {
1101 	struct in_llentry *lle;
1102 
1103 	lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
1104 	if (lle == NULL)		/* NB: caller generates msg */
1105 		return NULL;
1106 
1107 	/*
1108 	 * For IPv4 this will trigger "arpresolve" to generate
1109 	 * an ARP request.
1110 	 */
1111 	lle->base.la_expire = time_uptime; /* mark expired */
1112 	lle->base.r_l3addr.addr4 = addr4;
1113 	lle->base.lle_refcnt = 1;
1114 	lle->base.lle_free = in_lltable_destroy_lle;
1115 	LLE_LOCK_INIT(&lle->base);
1116 	LLE_REQ_INIT(&lle->base);
1117 	callout_init(&lle->base.lle_timer, 1);
1118 
1119 	return (&lle->base);
1120 }
1121 
1122 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m)	(		\
1123 	((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
1124 
1125 static int
1126 in_lltable_match_prefix(const struct sockaddr *saddr,
1127     const struct sockaddr *smask, u_int flags, struct llentry *lle)
1128 {
1129 	struct in_addr addr, mask, lle_addr;
1130 
1131 	addr = ((const struct sockaddr_in *)saddr)->sin_addr;
1132 	mask = ((const struct sockaddr_in *)smask)->sin_addr;
1133 	lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
1134 
1135 	if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
1136 		return (0);
1137 
1138 	if (lle->la_flags & LLE_IFADDR) {
1139 
1140 		/*
1141 		 * Delete LLE_IFADDR records IFF address & flag matches.
1142 		 * Note that addr is the interface address within prefix
1143 		 * being matched.
1144 		 * Note also we should handle 'ifdown' cases without removing
1145 		 * ifaddr macs.
1146 		 */
1147 		if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0)
1148 			return (1);
1149 		return (0);
1150 	}
1151 
1152 	/* flags & LLE_STATIC means deleting both dynamic and static entries */
1153 	if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
1154 		return (1);
1155 
1156 	return (0);
1157 }
1158 
1159 static void
1160 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
1161 {
1162 	size_t pkts_dropped;
1163 
1164 	LLE_WLOCK_ASSERT(lle);
1165 	KASSERT(llt != NULL, ("lltable is NULL"));
1166 
1167 	/* Unlink entry from table if not already */
1168 	if ((lle->la_flags & LLE_LINKED) != 0) {
1169 		IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
1170 		lltable_unlink_entry(llt, lle);
1171 	}
1172 
1173 	/* Drop hold queue */
1174 	pkts_dropped = llentry_free(lle);
1175 	ARPSTAT_ADD(dropped, pkts_dropped);
1176 }
1177 
1178 static int
1179 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1180 {
1181 	struct rt_addrinfo info;
1182 	struct sockaddr_in rt_key, rt_mask;
1183 	struct sockaddr rt_gateway;
1184 	int rt_flags;
1185 
1186 	KASSERT(l3addr->sa_family == AF_INET,
1187 	    ("sin_family %d", l3addr->sa_family));
1188 
1189 	bzero(&rt_key, sizeof(rt_key));
1190 	rt_key.sin_len = sizeof(rt_key);
1191 	bzero(&rt_mask, sizeof(rt_mask));
1192 	rt_mask.sin_len = sizeof(rt_mask);
1193 	bzero(&rt_gateway, sizeof(rt_gateway));
1194 	rt_gateway.sa_len = sizeof(rt_gateway);
1195 
1196 	bzero(&info, sizeof(info));
1197 	info.rti_info[RTAX_DST] = (struct sockaddr *)&rt_key;
1198 	info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&rt_mask;
1199 	info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&rt_gateway;
1200 
1201 	if (rib_lookup_info(ifp->if_fib, l3addr, NHR_REF, 0, &info) != 0)
1202 		return (EINVAL);
1203 
1204 	rt_flags = info.rti_flags;
1205 
1206 	/*
1207 	 * If the gateway for an existing host route matches the target L3
1208 	 * address, which is a special route inserted by some implementation
1209 	 * such as MANET, and the interface is of the correct type, then
1210 	 * allow for ARP to proceed.
1211 	 */
1212 	if (rt_flags & RTF_GATEWAY) {
1213 		if (!(rt_flags & RTF_HOST) || !info.rti_ifp ||
1214 		    info.rti_ifp->if_type != IFT_ETHER ||
1215 		    (info.rti_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1216 		    memcmp(rt_gateway.sa_data, l3addr->sa_data,
1217 		    sizeof(in_addr_t)) != 0) {
1218 			rib_free_info(&info);
1219 			return (EINVAL);
1220 		}
1221 	}
1222 	rib_free_info(&info);
1223 
1224 	/*
1225 	 * Make sure that at least the destination address is covered
1226 	 * by the route. This is for handling the case where 2 or more
1227 	 * interfaces have the same prefix. An incoming packet arrives
1228 	 * on one interface and the corresponding outgoing packet leaves
1229 	 * another interface.
1230 	 */
1231 	if (!(rt_flags & RTF_HOST) && info.rti_ifp != ifp) {
1232 		const char *sa, *mask, *addr, *lim;
1233 		const struct sockaddr_in *l3sin;
1234 
1235 		mask = (const char *)&rt_mask;
1236 		/*
1237 		 * Just being extra cautious to avoid some custom
1238 		 * code getting into trouble.
1239 		 */
1240 		if ((info.rti_addrs & RTA_NETMASK) == 0)
1241 			return (EINVAL);
1242 
1243 		sa = (const char *)&rt_key;
1244 		addr = (const char *)l3addr;
1245 		l3sin = (const struct sockaddr_in *)l3addr;
1246 		lim = addr + l3sin->sin_len;
1247 
1248 		for ( ; addr < lim; sa++, mask++, addr++) {
1249 			if ((*sa ^ *addr) & *mask) {
1250 #ifdef DIAGNOSTIC
1251 				char addrbuf[INET_ADDRSTRLEN];
1252 
1253 				log(LOG_INFO, "IPv4 address: \"%s\" "
1254 				    "is not on the network\n",
1255 				    inet_ntoa_r(l3sin->sin_addr, addrbuf));
1256 #endif
1257 				return (EINVAL);
1258 			}
1259 		}
1260 	}
1261 
1262 	return (0);
1263 }
1264 
1265 static inline uint32_t
1266 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
1267 {
1268 
1269 	return (IN_LLTBL_HASH(dst.s_addr, hsize));
1270 }
1271 
1272 static uint32_t
1273 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
1274 {
1275 
1276 	return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
1277 }
1278 
1279 static void
1280 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
1281 {
1282 	struct sockaddr_in *sin;
1283 
1284 	sin = (struct sockaddr_in *)sa;
1285 	bzero(sin, sizeof(*sin));
1286 	sin->sin_family = AF_INET;
1287 	sin->sin_len = sizeof(*sin);
1288 	sin->sin_addr = lle->r_l3addr.addr4;
1289 }
1290 
1291 static inline struct llentry *
1292 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
1293 {
1294 	struct llentry *lle;
1295 	struct llentries *lleh;
1296 	u_int hashidx;
1297 
1298 	hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
1299 	lleh = &llt->lle_head[hashidx];
1300 	CK_LIST_FOREACH(lle, lleh, lle_next) {
1301 		if (lle->la_flags & LLE_DELETED)
1302 			continue;
1303 		if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
1304 			break;
1305 	}
1306 
1307 	return (lle);
1308 }
1309 
1310 static void
1311 in_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
1312 {
1313 
1314 	lle->la_flags |= LLE_DELETED;
1315 	EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1316 #ifdef DIAGNOSTIC
1317 	log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1318 #endif
1319 	llentry_free(lle);
1320 }
1321 
1322 static struct llentry *
1323 in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1324 {
1325 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1326 	struct ifnet *ifp = llt->llt_ifp;
1327 	struct llentry *lle;
1328 	char linkhdr[LLE_MAX_LINKHDR];
1329 	size_t linkhdrsize;
1330 	int lladdr_off;
1331 
1332 	KASSERT(l3addr->sa_family == AF_INET,
1333 	    ("sin_family %d", l3addr->sa_family));
1334 
1335 	/*
1336 	 * A route that covers the given address must have
1337 	 * been installed 1st because we are doing a resolution,
1338 	 * verify this.
1339 	 */
1340 	if (!(flags & LLE_IFADDR) &&
1341 	    in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1342 		return (NULL);
1343 
1344 	lle = in_lltable_new(sin->sin_addr, flags);
1345 	if (lle == NULL) {
1346 		log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1347 		return (NULL);
1348 	}
1349 	lle->la_flags = flags;
1350 	if (flags & LLE_STATIC)
1351 		lle->r_flags |= RLLE_VALID;
1352 	if ((flags & LLE_IFADDR) == LLE_IFADDR) {
1353 		linkhdrsize = LLE_MAX_LINKHDR;
1354 		if (lltable_calc_llheader(ifp, AF_INET, IF_LLADDR(ifp),
1355 		    linkhdr, &linkhdrsize, &lladdr_off) != 0) {
1356 			epoch_call(net_epoch_preempt,  &lle->lle_epoch_ctx, in_lltable_destroy_lle_unlocked);
1357 			return (NULL);
1358 		}
1359 		lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
1360 		    lladdr_off);
1361 		lle->la_flags |= LLE_STATIC;
1362 		lle->r_flags |= (RLLE_VALID | RLLE_IFADDR);
1363 	}
1364 
1365 	return (lle);
1366 }
1367 
1368 /*
1369  * Return NULL if not found or marked for deletion.
1370  * If found return lle read locked.
1371  */
1372 static struct llentry *
1373 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1374 {
1375 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1376 	struct llentry *lle;
1377 
1378 	IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
1379 	KASSERT(l3addr->sa_family == AF_INET,
1380 	    ("sin_family %d", l3addr->sa_family));
1381 	lle = in_lltable_find_dst(llt, sin->sin_addr);
1382 
1383 	if (lle == NULL)
1384 		return (NULL);
1385 
1386 	KASSERT((flags & (LLE_UNLOCKED|LLE_EXCLUSIVE)) !=
1387 	    (LLE_UNLOCKED|LLE_EXCLUSIVE),("wrong lle request flags: 0x%X",
1388 	    flags));
1389 
1390 	if (flags & LLE_UNLOCKED)
1391 		return (lle);
1392 
1393 	if (flags & LLE_EXCLUSIVE)
1394 		LLE_WLOCK(lle);
1395 	else
1396 		LLE_RLOCK(lle);
1397 
1398 	return (lle);
1399 }
1400 
1401 static int
1402 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
1403     struct sysctl_req *wr)
1404 {
1405 	struct ifnet *ifp = llt->llt_ifp;
1406 	/* XXX stack use */
1407 	struct {
1408 		struct rt_msghdr	rtm;
1409 		struct sockaddr_in	sin;
1410 		struct sockaddr_dl	sdl;
1411 	} arpc;
1412 	struct sockaddr_dl *sdl;
1413 	int error;
1414 
1415 	bzero(&arpc, sizeof(arpc));
1416 	/* skip deleted entries */
1417 	if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1418 		return (0);
1419 	/* Skip if jailed and not a valid IP of the prison. */
1420 	lltable_fill_sa_entry(lle,(struct sockaddr *)&arpc.sin);
1421 	if (prison_if(wr->td->td_ucred, (struct sockaddr *)&arpc.sin) != 0)
1422 		return (0);
1423 	/*
1424 	 * produce a msg made of:
1425 	 *  struct rt_msghdr;
1426 	 *  struct sockaddr_in; (IPv4)
1427 	 *  struct sockaddr_dl;
1428 	 */
1429 	arpc.rtm.rtm_msglen = sizeof(arpc);
1430 	arpc.rtm.rtm_version = RTM_VERSION;
1431 	arpc.rtm.rtm_type = RTM_GET;
1432 	arpc.rtm.rtm_flags = RTF_UP;
1433 	arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1434 
1435 	/* publish */
1436 	if (lle->la_flags & LLE_PUB)
1437 		arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1438 
1439 	sdl = &arpc.sdl;
1440 	sdl->sdl_family = AF_LINK;
1441 	sdl->sdl_len = sizeof(*sdl);
1442 	sdl->sdl_index = ifp->if_index;
1443 	sdl->sdl_type = ifp->if_type;
1444 	if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1445 		sdl->sdl_alen = ifp->if_addrlen;
1446 		bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1447 	} else {
1448 		sdl->sdl_alen = 0;
1449 		bzero(LLADDR(sdl), ifp->if_addrlen);
1450 	}
1451 
1452 	arpc.rtm.rtm_rmx.rmx_expire =
1453 	    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1454 	arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1455 	if (lle->la_flags & LLE_STATIC)
1456 		arpc.rtm.rtm_flags |= RTF_STATIC;
1457 	if (lle->la_flags & LLE_IFADDR)
1458 		arpc.rtm.rtm_flags |= RTF_PINNED;
1459 	arpc.rtm.rtm_index = ifp->if_index;
1460 	error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1461 
1462 	return (error);
1463 }
1464 
1465 static struct lltable *
1466 in_lltattach(struct ifnet *ifp)
1467 {
1468 	struct lltable *llt;
1469 
1470 	llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
1471  	llt->llt_af = AF_INET;
1472  	llt->llt_ifp = ifp;
1473 
1474 	llt->llt_lookup = in_lltable_lookup;
1475 	llt->llt_alloc_entry = in_lltable_alloc;
1476 	llt->llt_delete_entry = in_lltable_delete_entry;
1477 	llt->llt_dump_entry = in_lltable_dump_entry;
1478 	llt->llt_hash = in_lltable_hash;
1479 	llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
1480 	llt->llt_free_entry = in_lltable_free_entry;
1481 	llt->llt_match_prefix = in_lltable_match_prefix;
1482 	llt->llt_mark_used = in_lltable_mark_used;
1483  	lltable_link(llt);
1484 
1485 	return (llt);
1486 }
1487 
1488 void *
1489 in_domifattach(struct ifnet *ifp)
1490 {
1491 	struct in_ifinfo *ii;
1492 
1493 	ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1494 
1495 	ii->ii_llt = in_lltattach(ifp);
1496 	ii->ii_igmp = igmp_domifattach(ifp);
1497 
1498 	return (ii);
1499 }
1500 
1501 void
1502 in_domifdetach(struct ifnet *ifp, void *aux)
1503 {
1504 	struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1505 
1506 	igmp_domifdetach(ifp);
1507 	lltable_free(ii->ii_llt);
1508 	free(ii, M_IFADDR);
1509 }
1510