xref: /freebsd/sys/netinet/in.c (revision 090e9752d7291db0c251a5576892e3bbbaea2479)
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_inet.h"
39 
40 #define IN_HISTORICAL_NETS		/* include class masks */
41 
42 #include <sys/param.h>
43 #include <sys/eventhandler.h>
44 #include <sys/systm.h>
45 #include <sys/sockio.h>
46 #include <sys/malloc.h>
47 #include <sys/priv.h>
48 #include <sys/socket.h>
49 #include <sys/jail.h>
50 #include <sys/kernel.h>
51 #include <sys/lock.h>
52 #include <sys/proc.h>
53 #include <sys/sysctl.h>
54 #include <sys/syslog.h>
55 #include <sys/sx.h>
56 
57 #include <net/if.h>
58 #include <net/if_var.h>
59 #include <net/if_arp.h>
60 #include <net/if_dl.h>
61 #include <net/if_llatbl.h>
62 #include <net/if_private.h>
63 #include <net/if_types.h>
64 #include <net/route.h>
65 #include <net/route/nhop.h>
66 #include <net/route/route_ctl.h>
67 #include <net/vnet.h>
68 
69 #include <netinet/if_ether.h>
70 #include <netinet/in.h>
71 #include <netinet/in_fib.h>
72 #include <netinet/in_var.h>
73 #include <netinet/in_pcb.h>
74 #include <netinet/ip_var.h>
75 #include <netinet/ip_carp.h>
76 #include <netinet/igmp_var.h>
77 #include <netinet/udp.h>
78 #include <netinet/udp_var.h>
79 
80 static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *);
81 static int in_difaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *);
82 static int in_gifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct ucred *);
83 
84 static void	in_socktrim(struct sockaddr_in *);
85 static void	in_purgemaddrs(struct ifnet *);
86 
87 static bool	ia_need_loopback_route(const struct in_ifaddr *);
88 
89 VNET_DEFINE_STATIC(int, nosameprefix);
90 #define	V_nosameprefix			VNET(nosameprefix)
91 SYSCTL_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_VNET | CTLFLAG_RW,
92 	&VNET_NAME(nosameprefix), 0,
93 	"Refuse to create same prefixes on different interfaces");
94 
95 VNET_DEFINE_STATIC(bool, broadcast_lowest);
96 #define	V_broadcast_lowest		VNET(broadcast_lowest)
97 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, broadcast_lowest, CTLFLAG_VNET | CTLFLAG_RW,
98 	&VNET_NAME(broadcast_lowest), 0,
99 	"Treat lowest address on a subnet (host 0) as broadcast");
100 
101 VNET_DEFINE(bool, ip_allow_net240) = false;
102 #define	V_ip_allow_net240		VNET(ip_allow_net240)
103 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, allow_net240,
104 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_allow_net240), 0,
105 	"Allow use of Experimental addresses, aka Class E (240/4)");
106 /* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-240 */
107 
108 VNET_DEFINE(bool, ip_allow_net0) = false;
109 SYSCTL_BOOL(_net_inet_ip, OID_AUTO, allow_net0,
110 	CTLFLAG_VNET | CTLFLAG_RW, &VNET_NAME(ip_allow_net0), 0,
111 	"Allow use of addresses in network 0/8");
112 /* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-0 */
113 
114 VNET_DEFINE(uint32_t, in_loopback_mask) = IN_LOOPBACK_MASK_DFLT;
115 #define	V_in_loopback_mask	VNET(in_loopback_mask)
116 static int sysctl_loopback_prefixlen(SYSCTL_HANDLER_ARGS);
117 SYSCTL_PROC(_net_inet_ip, OID_AUTO, loopback_prefixlen,
118 	CTLFLAG_VNET | CTLTYPE_INT | CTLFLAG_RW,
119 	NULL, 0, sysctl_loopback_prefixlen, "I",
120 	"Prefix length of address space reserved for loopback");
121 /* see https://datatracker.ietf.org/doc/draft-schoen-intarea-unicast-127 */
122 
123 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
124 #define	V_ripcbinfo			VNET(ripcbinfo)
125 
126 static struct sx in_control_sx;
127 SX_SYSINIT(in_control_sx, &in_control_sx, "in_control");
128 
129 /*
130  * Return 1 if an internet address is for a ``local'' host
131  * (one to which we have a connection).
132  */
133 int
134 in_localaddr(struct in_addr in)
135 {
136 	u_long i = ntohl(in.s_addr);
137 	struct in_ifaddr *ia;
138 
139 	NET_EPOCH_ASSERT();
140 
141 	CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
142 		if ((i & ia->ia_subnetmask) == ia->ia_subnet)
143 			return (1);
144 	}
145 
146 	return (0);
147 }
148 
149 /*
150  * Return 1 if an internet address is for the local host and configured
151  * on one of its interfaces.
152  */
153 bool
154 in_localip(struct in_addr in)
155 {
156 	struct in_ifaddr *ia;
157 
158 	NET_EPOCH_ASSERT();
159 
160 	CK_LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash)
161 		if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
162 			return (true);
163 
164 	return (false);
165 }
166 
167 /*
168  * Like in_localip(), but FIB-aware.
169  */
170 bool
171 in_localip_fib(struct in_addr in, uint16_t fib)
172 {
173 	struct in_ifaddr *ia;
174 
175 	NET_EPOCH_ASSERT();
176 
177 	CK_LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash)
178 		if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr &&
179 		    ia->ia_ifa.ifa_ifp->if_fib == fib)
180 			return (true);
181 
182 	return (false);
183 }
184 
185 /*
186  * Return 1 if an internet address is configured on an interface.
187  */
188 int
189 in_ifhasaddr(struct ifnet *ifp, struct in_addr in)
190 {
191 	struct ifaddr *ifa;
192 	struct in_ifaddr *ia;
193 
194 	NET_EPOCH_ASSERT();
195 
196 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
197 		if (ifa->ifa_addr->sa_family != AF_INET)
198 			continue;
199 		ia = (struct in_ifaddr *)ifa;
200 		if (ia->ia_addr.sin_addr.s_addr == in.s_addr)
201 			return (1);
202 	}
203 
204 	return (0);
205 }
206 
207 /*
208  * Return a reference to the interface address which is different to
209  * the supplied one but with same IP address value.
210  */
211 static struct in_ifaddr *
212 in_localip_more(struct in_ifaddr *original_ia)
213 {
214 	struct epoch_tracker et;
215 	in_addr_t original_addr = IA_SIN(original_ia)->sin_addr.s_addr;
216 	uint32_t original_fib = original_ia->ia_ifa.ifa_ifp->if_fib;
217 	struct in_ifaddr *ia;
218 
219 	NET_EPOCH_ENTER(et);
220 	CK_LIST_FOREACH(ia, INADDR_HASH(original_addr), ia_hash) {
221 		in_addr_t addr = IA_SIN(ia)->sin_addr.s_addr;
222 		uint32_t fib = ia->ia_ifa.ifa_ifp->if_fib;
223 		if (!V_rt_add_addr_allfibs && (original_fib != fib))
224 			continue;
225 		if ((original_ia != ia) && (original_addr == addr)) {
226 			ifa_ref(&ia->ia_ifa);
227 			NET_EPOCH_EXIT(et);
228 			return (ia);
229 		}
230 	}
231 	NET_EPOCH_EXIT(et);
232 
233 	return (NULL);
234 }
235 
236 /*
237  * Tries to find first IPv4 address in the provided fib.
238  * Prefers non-loopback addresses and return loopback IFF
239  * @loopback_ok is set.
240  *
241  * Returns ifa or NULL.
242  */
243 struct in_ifaddr *
244 in_findlocal(uint32_t fibnum, bool loopback_ok)
245 {
246 	struct in_ifaddr *ia = NULL, *ia_lo = NULL;
247 
248 	NET_EPOCH_ASSERT();
249 
250 	CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
251 		uint32_t ia_fib = ia->ia_ifa.ifa_ifp->if_fib;
252 		if (!V_rt_add_addr_allfibs && (fibnum != ia_fib))
253 			continue;
254 
255 		if (!IN_LOOPBACK(ntohl(IA_SIN(ia)->sin_addr.s_addr)))
256 			break;
257 		if (loopback_ok)
258 			ia_lo = ia;
259 	}
260 
261 	if (ia == NULL)
262 		ia = ia_lo;
263 
264 	return (ia);
265 }
266 
267 /*
268  * Determine whether an IP address is in a reserved set of addresses
269  * that may not be forwarded, or whether datagrams to that destination
270  * may be forwarded.
271  */
272 int
273 in_canforward(struct in_addr in)
274 {
275 	u_long i = ntohl(in.s_addr);
276 
277 	if (IN_MULTICAST(i) || IN_LINKLOCAL(i) || IN_LOOPBACK(i))
278 		return (0);
279 	if (IN_EXPERIMENTAL(i) && !V_ip_allow_net240)
280 		return (0);
281 	if (IN_ZERONET(i) && !V_ip_allow_net0)
282 		return (0);
283 	return (1);
284 }
285 
286 /*
287  * Sysctl to manage prefix of reserved loopback network; translate
288  * to/from mask.  The mask is always contiguous high-order 1 bits
289  * followed by all 0 bits.
290  */
291 static int
292 sysctl_loopback_prefixlen(SYSCTL_HANDLER_ARGS)
293 {
294 	int error, preflen;
295 
296 	/* ffs is 1-based; compensate. */
297 	preflen = 33 - ffs(V_in_loopback_mask);
298 	error = sysctl_handle_int(oidp, &preflen, 0, req);
299 	if (error || !req->newptr)
300 		return (error);
301 	if (preflen < 8 || preflen > 31)
302 		return (EINVAL);
303 	V_in_loopback_mask = 0xffffffff << (32 - preflen);
304 	return (0);
305 }
306 
307 /*
308  * Trim a mask in a sockaddr
309  */
310 static void
311 in_socktrim(struct sockaddr_in *ap)
312 {
313     char *cplim = (char *) &ap->sin_addr;
314     char *cp = (char *) (&ap->sin_addr + 1);
315 
316     ap->sin_len = 0;
317     while (--cp >= cplim)
318 	if (*cp) {
319 	    (ap)->sin_len = cp - (char *) (ap) + 1;
320 	    break;
321 	}
322 }
323 
324 /*
325  * Generic internet control operations (ioctl's).
326  */
327 int
328 in_control(struct socket *so, u_long cmd, void *data, struct ifnet *ifp,
329     struct thread *td)
330 {
331 	struct ifreq *ifr = (struct ifreq *)data;
332 	struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
333 	struct epoch_tracker et;
334 	struct ifaddr *ifa;
335 	struct in_ifaddr *ia;
336 	int error;
337 
338 	if (ifp == NULL)
339 		return (EADDRNOTAVAIL);
340 
341 	struct ucred *cred = (td != NULL) ? td->td_ucred : NULL;
342 
343 	/*
344 	 * Filter out 4 ioctls we implement directly.  Forward the rest
345 	 * to specific functions and ifp->if_ioctl().
346 	 */
347 	switch (cmd) {
348 	case SIOCGIFADDR:
349 	case SIOCGIFBRDADDR:
350 	case SIOCGIFDSTADDR:
351 	case SIOCGIFNETMASK:
352 		break;
353 	case SIOCGIFALIAS:
354 		sx_xlock(&in_control_sx);
355 		error = in_gifaddr_ioctl(cmd, data, ifp, cred);
356 		sx_xunlock(&in_control_sx);
357 		return (error);
358 	case SIOCDIFADDR:
359 		sx_xlock(&in_control_sx);
360 		error = in_difaddr_ioctl(cmd, data, ifp, cred);
361 		sx_xunlock(&in_control_sx);
362 		return (error);
363 	case OSIOCAIFADDR:	/* 9.x compat */
364 	case SIOCAIFADDR:
365 		sx_xlock(&in_control_sx);
366 		error = in_aifaddr_ioctl(cmd, data, ifp, cred);
367 		sx_xunlock(&in_control_sx);
368 		return (error);
369 	case SIOCSIFADDR:
370 	case SIOCSIFBRDADDR:
371 	case SIOCSIFDSTADDR:
372 	case SIOCSIFNETMASK:
373 		/* We no longer support that old commands. */
374 		return (EINVAL);
375 	default:
376 		if (ifp->if_ioctl == NULL)
377 			return (EOPNOTSUPP);
378 		return ((*ifp->if_ioctl)(ifp, cmd, data));
379 	}
380 
381 	if (addr->sin_addr.s_addr != INADDR_ANY &&
382 	    prison_check_ip4(cred, &addr->sin_addr) != 0)
383 		return (EADDRNOTAVAIL);
384 
385 	/*
386 	 * Find address for this interface, if it exists.  If an
387 	 * address was specified, find that one instead of the
388 	 * first one on the interface, if possible.
389 	 */
390 	NET_EPOCH_ENTER(et);
391 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
392 		if (ifa->ifa_addr->sa_family != AF_INET)
393 			continue;
394 		ia = (struct in_ifaddr *)ifa;
395 		if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
396 			break;
397 	}
398 	if (ifa == NULL)
399 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
400 			if (ifa->ifa_addr->sa_family == AF_INET) {
401 				ia = (struct in_ifaddr *)ifa;
402 				if (prison_check_ip4(cred,
403 				    &ia->ia_addr.sin_addr) == 0)
404 					break;
405 			}
406 
407 	if (ifa == NULL) {
408 		NET_EPOCH_EXIT(et);
409 		return (EADDRNOTAVAIL);
410 	}
411 
412 	error = 0;
413 	switch (cmd) {
414 	case SIOCGIFADDR:
415 		*addr = ia->ia_addr;
416 		break;
417 
418 	case SIOCGIFBRDADDR:
419 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
420 			error = EINVAL;
421 			break;
422 		}
423 		*addr = ia->ia_broadaddr;
424 		break;
425 
426 	case SIOCGIFDSTADDR:
427 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
428 			error = EINVAL;
429 			break;
430 		}
431 		*addr = ia->ia_dstaddr;
432 		break;
433 
434 	case SIOCGIFNETMASK:
435 		*addr = ia->ia_sockmask;
436 		break;
437 	}
438 
439 	NET_EPOCH_EXIT(et);
440 
441 	return (error);
442 }
443 
444 static int
445 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred)
446 {
447 	const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
448 	const struct sockaddr_in *addr = &ifra->ifra_addr;
449 	const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
450 	const struct sockaddr_in *mask = &ifra->ifra_mask;
451 	const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
452 	const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
453 	struct epoch_tracker et;
454 	struct ifaddr *ifa;
455 	struct in_ifaddr *ia;
456 	bool iaIsFirst;
457 	int error = 0;
458 
459 	error = priv_check_cred(cred, PRIV_NET_ADDIFADDR);
460 	if (error)
461 		return (error);
462 
463 	/*
464 	 * ifra_addr must be present and be of INET family.
465 	 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
466 	 */
467 	if (addr->sin_len != sizeof(struct sockaddr_in) ||
468 	    addr->sin_family != AF_INET)
469 		return (EINVAL);
470 	if (broadaddr->sin_len != 0 &&
471 	    (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
472 	    broadaddr->sin_family != AF_INET))
473 		return (EINVAL);
474 	if (mask->sin_len != 0 &&
475 	    (mask->sin_len != sizeof(struct sockaddr_in) ||
476 	    mask->sin_family != AF_INET))
477 		return (EINVAL);
478 	if ((ifp->if_flags & IFF_POINTOPOINT) &&
479 	    (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
480 	     dstaddr->sin_addr.s_addr == INADDR_ANY))
481 		return (EDESTADDRREQ);
482 	if (vhid != 0 && carp_attach_p == NULL)
483 		return (EPROTONOSUPPORT);
484 
485 	/*
486 	 * See whether address already exist.
487 	 */
488 	iaIsFirst = true;
489 	ia = NULL;
490 	NET_EPOCH_ENTER(et);
491 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
492 		struct in_ifaddr *it;
493 
494 		if (ifa->ifa_addr->sa_family != AF_INET)
495 			continue;
496 
497 		it = (struct in_ifaddr *)ifa;
498 		if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
499 		    prison_check_ip4(cred, &addr->sin_addr) == 0)
500 			ia = it;
501 		else
502 			iaIsFirst = false;
503 	}
504 	NET_EPOCH_EXIT(et);
505 
506 	if (ia != NULL)
507 		(void )in_difaddr_ioctl(cmd, data, ifp, cred);
508 
509 	ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
510 	ia = (struct in_ifaddr *)ifa;
511 	ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
512 	ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
513 	ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
514 	callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock,
515 	    CALLOUT_RETURNUNLOCKED);
516 
517 	ia->ia_ifp = ifp;
518 	ia->ia_addr = *addr;
519 	if (mask->sin_len != 0) {
520 		ia->ia_sockmask = *mask;
521 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
522 	} else {
523 		in_addr_t i = ntohl(addr->sin_addr.s_addr);
524 
525 		/*
526 	 	 * If netmask isn't supplied, use historical default.
527 		 * This is deprecated for interfaces other than loopback
528 		 * or point-to-point; warn in other cases.  In the future
529 		 * we should return an error rather than warning.
530 	 	 */
531 		if ((ifp->if_flags & (IFF_POINTOPOINT | IFF_LOOPBACK)) == 0)
532 			printf("%s: set address: WARNING: network mask "
533 			     "should be specified; using historical default\n",
534 			     ifp->if_xname);
535 		if (IN_CLASSA(i))
536 			ia->ia_subnetmask = IN_CLASSA_NET;
537 		else if (IN_CLASSB(i))
538 			ia->ia_subnetmask = IN_CLASSB_NET;
539 		else
540 			ia->ia_subnetmask = IN_CLASSC_NET;
541 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
542 	}
543 	ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
544 	in_socktrim(&ia->ia_sockmask);
545 
546 	if (ifp->if_flags & IFF_BROADCAST) {
547 		if (broadaddr->sin_len != 0) {
548 			ia->ia_broadaddr = *broadaddr;
549 		} else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
550 			ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
551 			ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
552 			ia->ia_broadaddr.sin_family = AF_INET;
553 		} else {
554 			ia->ia_broadaddr.sin_addr.s_addr =
555 			    htonl(ia->ia_subnet | ~ia->ia_subnetmask);
556 			ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
557 			ia->ia_broadaddr.sin_family = AF_INET;
558 		}
559 	}
560 
561 	if (ifp->if_flags & IFF_POINTOPOINT)
562 		ia->ia_dstaddr = *dstaddr;
563 
564 	if (vhid != 0) {
565 		error = (*carp_attach_p)(&ia->ia_ifa, vhid);
566 		if (error)
567 			return (error);
568 	}
569 
570 	/* if_addrhead is already referenced by ifa_alloc() */
571 	IF_ADDR_WLOCK(ifp);
572 	CK_STAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
573 	IF_ADDR_WUNLOCK(ifp);
574 
575 	ifa_ref(ifa);			/* in_ifaddrhead */
576 	sx_assert(&in_control_sx, SA_XLOCKED);
577 	CK_STAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
578 	CK_LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia,
579 	    ia_hash);
580 
581 	/*
582 	 * Give the interface a chance to initialize
583 	 * if this is its first address,
584 	 * and to validate the address if necessary.
585 	 */
586 	if (ifp->if_ioctl != NULL) {
587 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
588 		if (error)
589 			goto fail1;
590 	}
591 
592 	/*
593 	 * Add route for the network.
594 	 */
595 	if (vhid == 0) {
596 		error = in_addprefix(ia);
597 		if (error)
598 			goto fail1;
599 	}
600 
601 	/*
602 	 * Add a loopback route to self.
603 	 */
604 	if (vhid == 0 && ia_need_loopback_route(ia)) {
605 		struct in_ifaddr *eia;
606 
607 		eia = in_localip_more(ia);
608 
609 		if (eia == NULL) {
610 			error = ifa_add_loopback_route((struct ifaddr *)ia,
611 			    (struct sockaddr *)&ia->ia_addr);
612 			if (error)
613 				goto fail2;
614 		} else
615 			ifa_free(&eia->ia_ifa);
616 	}
617 
618 	if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
619 		struct in_addr allhosts_addr;
620 		struct in_ifinfo *ii;
621 
622 		ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
623 		allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
624 
625 		error = in_joingroup(ifp, &allhosts_addr, NULL,
626 			&ii->ii_allhosts);
627 	}
628 
629 	/*
630 	 * Note: we don't need extra reference for ifa, since we called
631 	 * with sx lock held, and ifaddr can not be deleted in concurrent
632 	 * thread.
633 	 */
634 	EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, ifa, IFADDR_EVENT_ADD);
635 
636 	return (error);
637 
638 fail2:
639 	if (vhid == 0)
640 		(void )in_scrubprefix(ia, LLE_STATIC);
641 
642 fail1:
643 	if (ia->ia_ifa.ifa_carp)
644 		(*carp_detach_p)(&ia->ia_ifa, false);
645 
646 	IF_ADDR_WLOCK(ifp);
647 	CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
648 	IF_ADDR_WUNLOCK(ifp);
649 	ifa_free(&ia->ia_ifa);		/* if_addrhead */
650 
651 	sx_assert(&in_control_sx, SA_XLOCKED);
652 	CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
653 	CK_LIST_REMOVE(ia, ia_hash);
654 	ifa_free(&ia->ia_ifa);		/* in_ifaddrhead */
655 
656 	return (error);
657 }
658 
659 static int
660 in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred)
661 {
662 	const struct ifreq *ifr = (struct ifreq *)data;
663 	const struct sockaddr_in *addr = (const struct sockaddr_in *)
664 	    &ifr->ifr_addr;
665 	struct ifaddr *ifa;
666 	struct in_ifaddr *ia;
667 	bool deleteAny, iaIsLast;
668 	int error;
669 
670 	if (cred != NULL) {
671 		error = priv_check_cred(cred, PRIV_NET_DELIFADDR);
672 		if (error)
673 			return (error);
674 	}
675 
676 	if (addr->sin_len != sizeof(struct sockaddr_in) ||
677 	    addr->sin_family != AF_INET)
678 		deleteAny = true;
679 	else
680 		deleteAny = false;
681 
682 	iaIsLast = true;
683 	ia = NULL;
684 	IF_ADDR_WLOCK(ifp);
685 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
686 		struct in_ifaddr *it;
687 
688 		if (ifa->ifa_addr->sa_family != AF_INET)
689 			continue;
690 
691 		it = (struct in_ifaddr *)ifa;
692 		if (deleteAny && ia == NULL && (cred == NULL ||
693 		    prison_check_ip4(cred, &it->ia_addr.sin_addr) == 0))
694 			ia = it;
695 
696 		if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
697 		    (cred == NULL || prison_check_ip4(cred,
698 		    &addr->sin_addr) == 0))
699 			ia = it;
700 
701 		if (it != ia)
702 			iaIsLast = false;
703 	}
704 
705 	if (ia == NULL) {
706 		IF_ADDR_WUNLOCK(ifp);
707 		return (EADDRNOTAVAIL);
708 	}
709 
710 	CK_STAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifaddr, ifa_link);
711 	IF_ADDR_WUNLOCK(ifp);
712 	ifa_free(&ia->ia_ifa);		/* if_addrhead */
713 
714 	sx_assert(&in_control_sx, SA_XLOCKED);
715 	CK_STAILQ_REMOVE(&V_in_ifaddrhead, ia, in_ifaddr, ia_link);
716 	CK_LIST_REMOVE(ia, ia_hash);
717 
718 	/*
719 	 * in_scrubprefix() kills the interface route.
720 	 */
721 	in_scrubprefix(ia, LLE_STATIC);
722 
723 	/*
724 	 * in_ifadown gets rid of all the rest of
725 	 * the routes.  This is not quite the right
726 	 * thing to do, but at least if we are running
727 	 * a routing process they will come back.
728 	 */
729 	in_ifadown(&ia->ia_ifa, 1);
730 
731 	if (ia->ia_ifa.ifa_carp)
732 		(*carp_detach_p)(&ia->ia_ifa, cmd == SIOCAIFADDR);
733 
734 	/*
735 	 * If this is the last IPv4 address configured on this
736 	 * interface, leave the all-hosts group.
737 	 * No state-change report need be transmitted.
738 	 */
739 	if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
740 		struct in_ifinfo *ii;
741 
742 		ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
743 		if (ii->ii_allhosts) {
744 			(void)in_leavegroup(ii->ii_allhosts, NULL);
745 			ii->ii_allhosts = NULL;
746 		}
747 	}
748 
749 	IF_ADDR_WLOCK(ifp);
750 	if (callout_stop(&ia->ia_garp_timer) == 1) {
751 		ifa_free(&ia->ia_ifa);
752 	}
753 	IF_ADDR_WUNLOCK(ifp);
754 
755 	EVENTHANDLER_INVOKE(ifaddr_event_ext, ifp, &ia->ia_ifa,
756 	    IFADDR_EVENT_DEL);
757 	ifa_free(&ia->ia_ifa);		/* in_ifaddrhead */
758 
759 	return (0);
760 }
761 
762 static int
763 in_gifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct ucred *cred)
764 {
765 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
766 	const struct sockaddr_in *addr = &ifra->ifra_addr;
767 	struct epoch_tracker et;
768 	struct ifaddr *ifa;
769 	struct in_ifaddr *ia;
770 
771 	/*
772 	 * ifra_addr must be present and be of INET family.
773 	 */
774 	if (addr->sin_len != sizeof(struct sockaddr_in) ||
775 	    addr->sin_family != AF_INET)
776 		return (EINVAL);
777 
778 	/*
779 	 * See whether address exist.
780 	 */
781 	ia = NULL;
782 	NET_EPOCH_ENTER(et);
783 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
784 		struct in_ifaddr *it;
785 
786 		if (ifa->ifa_addr->sa_family != AF_INET)
787 			continue;
788 
789 		it = (struct in_ifaddr *)ifa;
790 		if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
791 		    prison_check_ip4(cred, &addr->sin_addr) == 0) {
792 			ia = it;
793 			break;
794 		}
795 	}
796 	if (ia == NULL) {
797 		NET_EPOCH_EXIT(et);
798 		return (EADDRNOTAVAIL);
799 	}
800 
801 	ifra->ifra_mask = ia->ia_sockmask;
802 	if ((ifp->if_flags & IFF_POINTOPOINT) &&
803 	    ia->ia_dstaddr.sin_family == AF_INET)
804 		ifra->ifra_dstaddr = ia->ia_dstaddr;
805 	else if ((ifp->if_flags & IFF_BROADCAST) &&
806 	    ia->ia_broadaddr.sin_family == AF_INET)
807 		ifra->ifra_broadaddr = ia->ia_broadaddr;
808 	else
809 		memset(&ifra->ifra_broadaddr, 0,
810 		    sizeof(ifra->ifra_broadaddr));
811 
812 	NET_EPOCH_EXIT(et);
813 	return (0);
814 }
815 
816 static int
817 in_match_ifaddr(const struct rtentry *rt, const struct nhop_object *nh, void *arg)
818 {
819 
820 	if (nh->nh_ifa == (struct ifaddr *)arg)
821 		return (1);
822 
823 	return (0);
824 }
825 
826 static int
827 in_handle_prefix_route(uint32_t fibnum, int cmd,
828     struct sockaddr_in *dst, struct sockaddr_in *netmask, struct ifaddr *ifa,
829     struct ifnet *ifp)
830 {
831 
832 	NET_EPOCH_ASSERT();
833 
834 	/* Prepare gateway */
835 	struct sockaddr_dl_short sdl = {
836 		.sdl_family = AF_LINK,
837 		.sdl_len = sizeof(struct sockaddr_dl_short),
838 		.sdl_type = ifa->ifa_ifp->if_type,
839 		.sdl_index = ifa->ifa_ifp->if_index,
840 	};
841 
842 	struct rt_addrinfo info = {
843 		.rti_ifa = ifa,
844 		.rti_ifp = ifp,
845 		.rti_flags = RTF_PINNED | ((netmask != NULL) ? 0 : RTF_HOST),
846 		.rti_info = {
847 			[RTAX_DST] = (struct sockaddr *)dst,
848 			[RTAX_NETMASK] = (struct sockaddr *)netmask,
849 			[RTAX_GATEWAY] = (struct sockaddr *)&sdl,
850 		},
851 		/* Ensure we delete the prefix IFF prefix ifa matches */
852 		.rti_filter = in_match_ifaddr,
853 		.rti_filterdata = ifa,
854 	};
855 
856 	return (rib_handle_ifaddr_info(fibnum, cmd, &info));
857 }
858 
859 /*
860  * Routing table interaction with interface addresses.
861  *
862  * In general, two types of routes needs to be installed:
863  * a) "interface" or "prefix" route, telling user that the addresses
864  *   behind the ifa prefix are reached directly.
865  * b) "loopback" route installed for the ifa address, telling user that
866  *   the address belongs to local system.
867  *
868  * Handling for (a) and (b) differs in multi-fib aspects, hence they
869  *  are implemented in different functions below.
870  *
871  * The cases above may intersect - /32 interface aliases results in
872  *  the same prefix produced by (a) and (b). This blurs the definition
873  *  of the "loopback" route and complicate interactions. The interaction
874  *  table is defined below. The case numbers are used in the multiple
875  *  functions below to refer to the particular test case.
876  *
877  * There can be multiple options:
878  * 1) Adding address with prefix on non-p2p/non-loopback interface.
879  *  Example: 192.0.2.1/24. Action:
880  *  * add "prefix" route towards 192.0.2.0/24 via @ia interface,
881  *    using @ia as an address source.
882  *  * add "loopback" route towards 192.0.2.1 via V_loif, saving
883  *   @ia ifp in the gateway and using @ia as an address source.
884  *
885  * 2) Adding address with /32 mask to non-p2p/non-loopback interface.
886  *  Example: 192.0.2.2/32. Action:
887  *  * add "prefix" host route via V_loif, using @ia as an address source.
888  *
889  * 3) Adding address with or without prefix to p2p interface.
890  *  Example: 10.0.0.1/24->10.0.0.2. Action:
891  *  * add "prefix" host route towards 10.0.0.2 via this interface, using @ia
892  *    as an address source. Note: no sense in installing full /24 as the interface
893  *    is point-to-point.
894  *  * add "loopback" route towards 10.0.9.1 via V_loif, saving
895  *   @ia ifp in the gateway and using @ia as an address source.
896  *
897  * 4) Adding address with or without prefix to loopback interface.
898  *  Example: 192.0.2.1/24. Action:
899  *  * add "prefix" host route via @ia interface, using @ia as an address source.
900  *    Note: Skip installing /24 prefix as it would introduce TTL loop
901  *    for the traffic destined to these addresses.
902  */
903 
904 /*
905  * Checks if @ia needs to install loopback route to @ia address via
906  *  ifa_maintain_loopback_route().
907  *
908  * Return true on success.
909  */
910 static bool
911 ia_need_loopback_route(const struct in_ifaddr *ia)
912 {
913 	struct ifnet *ifp = ia->ia_ifp;
914 
915 	/* Case 4: Skip loopback interfaces */
916 	if ((ifp->if_flags & IFF_LOOPBACK) ||
917 	    (ia->ia_addr.sin_addr.s_addr == INADDR_ANY))
918 		return (false);
919 
920 	/* Clash avoidance: Skip p2p interfaces with both addresses are equal */
921 	if ((ifp->if_flags & IFF_POINTOPOINT) &&
922 	    ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)
923 		return (false);
924 
925 	/* Case 2: skip /32 prefixes */
926 	if (!(ifp->if_flags & IFF_POINTOPOINT) &&
927 	    (ia->ia_sockmask.sin_addr.s_addr == INADDR_BROADCAST))
928 		return (false);
929 
930 	return (true);
931 }
932 
933 /*
934  * Calculate "prefix" route corresponding to @ia.
935  */
936 static void
937 ia_getrtprefix(const struct in_ifaddr *ia, struct in_addr *prefix, struct in_addr *mask)
938 {
939 
940 	if (ia->ia_ifp->if_flags & IFF_POINTOPOINT) {
941 		/* Case 3: return host route for dstaddr */
942 		*prefix = ia->ia_dstaddr.sin_addr;
943 		mask->s_addr = INADDR_BROADCAST;
944 	} else if (ia->ia_ifp->if_flags & IFF_LOOPBACK) {
945 		/* Case 4: return host route for ifaddr */
946 		*prefix = ia->ia_addr.sin_addr;
947 		mask->s_addr = INADDR_BROADCAST;
948 	} else {
949 		/* Cases 1,2: return actual ia prefix */
950 		*prefix = ia->ia_addr.sin_addr;
951 		*mask = ia->ia_sockmask.sin_addr;
952 		prefix->s_addr &= mask->s_addr;
953 	}
954 }
955 
956 /*
957  * Adds or delete interface "prefix" route corresponding to @ifa.
958  *  Returns 0 on success or errno.
959  */
960 static int
961 in_handle_ifaddr_route(int cmd, struct in_ifaddr *ia)
962 {
963 	struct ifaddr *ifa = &ia->ia_ifa;
964 	struct in_addr daddr, maddr;
965 	struct sockaddr_in *pmask;
966 	struct epoch_tracker et;
967 	int error;
968 
969 	ia_getrtprefix(ia, &daddr, &maddr);
970 
971 	struct sockaddr_in mask = {
972 		.sin_family = AF_INET,
973 		.sin_len = sizeof(struct sockaddr_in),
974 		.sin_addr = maddr,
975 	};
976 
977 	pmask = (maddr.s_addr != INADDR_BROADCAST) ? &mask : NULL;
978 
979 	struct sockaddr_in dst = {
980 		.sin_family = AF_INET,
981 		.sin_len = sizeof(struct sockaddr_in),
982 		.sin_addr.s_addr = daddr.s_addr & maddr.s_addr,
983 	};
984 
985 	struct ifnet *ifp = ia->ia_ifp;
986 
987 	if ((maddr.s_addr == INADDR_BROADCAST) &&
988 	    (!(ia->ia_ifp->if_flags & (IFF_POINTOPOINT|IFF_LOOPBACK)))) {
989 		/* Case 2: host route on broadcast interface */
990 		ifp = V_loif;
991 	}
992 
993 	uint32_t fibnum = ifa->ifa_ifp->if_fib;
994 	NET_EPOCH_ENTER(et);
995 	error = in_handle_prefix_route(fibnum, cmd, &dst, pmask, ifa, ifp);
996 	NET_EPOCH_EXIT(et);
997 
998 	return (error);
999 }
1000 
1001 /*
1002  * Check if we have a route for the given prefix already.
1003  */
1004 static bool
1005 in_hasrtprefix(struct in_ifaddr *target)
1006 {
1007 	struct epoch_tracker et;
1008 	struct in_ifaddr *ia;
1009 	struct in_addr prefix, mask, p, m;
1010 	bool result = false;
1011 
1012 	ia_getrtprefix(target, &prefix, &mask);
1013 
1014 	/* Look for an existing address with the same prefix, mask, and fib */
1015 	NET_EPOCH_ENTER(et);
1016 	CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1017 		ia_getrtprefix(ia, &p, &m);
1018 
1019 		if (prefix.s_addr != p.s_addr ||
1020 		    mask.s_addr != m.s_addr)
1021 			continue;
1022 
1023 		if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
1024 			continue;
1025 
1026 		/*
1027 		 * If we got a matching prefix route inserted by other
1028 		 * interface address, we are done here.
1029 		 */
1030 		if (ia->ia_flags & IFA_ROUTE) {
1031 			result = true;
1032 			break;
1033 		}
1034 	}
1035 	NET_EPOCH_EXIT(et);
1036 
1037 	return (result);
1038 }
1039 
1040 int
1041 in_addprefix(struct in_ifaddr *target)
1042 {
1043 	int error;
1044 
1045 	if (in_hasrtprefix(target)) {
1046 		if (V_nosameprefix)
1047 			return (EEXIST);
1048 		else {
1049 			rt_addrmsg(RTM_ADD, &target->ia_ifa,
1050 			    target->ia_ifp->if_fib);
1051 			return (0);
1052 		}
1053 	}
1054 
1055 	/*
1056 	 * No-one seem to have this prefix route, so we try to insert it.
1057 	 */
1058 	rt_addrmsg(RTM_ADD, &target->ia_ifa, target->ia_ifp->if_fib);
1059 	error = in_handle_ifaddr_route(RTM_ADD, target);
1060 	if (!error)
1061 		target->ia_flags |= IFA_ROUTE;
1062 	return (error);
1063 }
1064 
1065 /*
1066  * Removes either all lle entries for given @ia, or lle
1067  * corresponding to @ia address.
1068  */
1069 static void
1070 in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags)
1071 {
1072 	struct sockaddr_in addr, mask;
1073 	struct sockaddr *saddr, *smask;
1074 	struct ifnet *ifp;
1075 
1076 	saddr = (struct sockaddr *)&addr;
1077 	bzero(&addr, sizeof(addr));
1078 	addr.sin_len = sizeof(addr);
1079 	addr.sin_family = AF_INET;
1080 	smask = (struct sockaddr *)&mask;
1081 	bzero(&mask, sizeof(mask));
1082 	mask.sin_len = sizeof(mask);
1083 	mask.sin_family = AF_INET;
1084 	mask.sin_addr.s_addr = ia->ia_subnetmask;
1085 	ifp = ia->ia_ifp;
1086 
1087 	if (all) {
1088 		/*
1089 		 * Remove all L2 entries matching given prefix.
1090 		 * Convert address to host representation to avoid
1091 		 * doing this on every callback. ia_subnetmask is already
1092 		 * stored in host representation.
1093 		 */
1094 		addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr);
1095 		lltable_prefix_free(AF_INET, saddr, smask, flags);
1096 	} else {
1097 		/* Remove interface address only */
1098 		addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr;
1099 		lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr);
1100 	}
1101 }
1102 
1103 /*
1104  * If there is no other address in the system that can serve a route to the
1105  * same prefix, remove the route.  Hand over the route to the new address
1106  * otherwise.
1107  */
1108 int
1109 in_scrubprefix(struct in_ifaddr *target, u_int flags)
1110 {
1111 	struct epoch_tracker et;
1112 	struct in_ifaddr *ia;
1113 	struct in_addr prefix, mask, p, m;
1114 	int error = 0;
1115 
1116 	/*
1117 	 * Remove the loopback route to the interface address.
1118 	 */
1119 	if (ia_need_loopback_route(target) && (flags & LLE_STATIC)) {
1120 		struct in_ifaddr *eia;
1121 
1122 		eia = in_localip_more(target);
1123 
1124 		if (eia != NULL) {
1125 			error = ifa_switch_loopback_route((struct ifaddr *)eia,
1126 			    (struct sockaddr *)&target->ia_addr);
1127 			ifa_free(&eia->ia_ifa);
1128 		} else {
1129 			error = ifa_del_loopback_route((struct ifaddr *)target,
1130 			    (struct sockaddr *)&target->ia_addr);
1131 		}
1132 	}
1133 
1134 	ia_getrtprefix(target, &prefix, &mask);
1135 
1136 	if ((target->ia_flags & IFA_ROUTE) == 0) {
1137 		rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1138 
1139 		/*
1140 		 * Removing address from !IFF_UP interface or
1141 		 * prefix which exists on other interface (along with route).
1142 		 * No entries should exist here except target addr.
1143 		 * Given that, delete this entry only.
1144 		 */
1145 		in_scrubprefixlle(target, 0, flags);
1146 		return (0);
1147 	}
1148 
1149 	NET_EPOCH_ENTER(et);
1150 	CK_STAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
1151 		ia_getrtprefix(ia, &p, &m);
1152 
1153 		if (prefix.s_addr != p.s_addr ||
1154 		    mask.s_addr != m.s_addr)
1155 			continue;
1156 
1157 		if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
1158 			continue;
1159 
1160 		/*
1161 		 * If we got a matching prefix address, move IFA_ROUTE and
1162 		 * the route itself to it.  Make sure that routing daemons
1163 		 * get a heads-up.
1164 		 */
1165 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
1166 			ifa_ref(&ia->ia_ifa);
1167 			NET_EPOCH_EXIT(et);
1168 			error = in_handle_ifaddr_route(RTM_DELETE, target);
1169 			if (error == 0)
1170 				target->ia_flags &= ~IFA_ROUTE;
1171 			else
1172 				log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
1173 					error);
1174 			/* Scrub all entries IFF interface is different */
1175 			in_scrubprefixlle(target, target->ia_ifp != ia->ia_ifp,
1176 			    flags);
1177 			error = in_handle_ifaddr_route(RTM_ADD, ia);
1178 			if (error == 0)
1179 				ia->ia_flags |= IFA_ROUTE;
1180 			else
1181 				log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
1182 					error);
1183 			ifa_free(&ia->ia_ifa);
1184 			return (error);
1185 		}
1186 	}
1187 	NET_EPOCH_EXIT(et);
1188 
1189 	/*
1190 	 * remove all L2 entries on the given prefix
1191 	 */
1192 	in_scrubprefixlle(target, 1, flags);
1193 
1194 	/*
1195 	 * As no-one seem to have this prefix, we can remove the route.
1196 	 */
1197 	rt_addrmsg(RTM_DELETE, &target->ia_ifa, target->ia_ifp->if_fib);
1198 	error = in_handle_ifaddr_route(RTM_DELETE, target);
1199 	if (error == 0)
1200 		target->ia_flags &= ~IFA_ROUTE;
1201 	else
1202 		log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
1203 	return (error);
1204 }
1205 
1206 void
1207 in_ifscrub_all(void)
1208 {
1209 	struct ifnet *ifp;
1210 	struct ifaddr *ifa, *nifa;
1211 	struct ifaliasreq ifr;
1212 
1213 	IFNET_RLOCK();
1214 	CK_STAILQ_FOREACH(ifp, &V_ifnet, if_link) {
1215 		/* Cannot lock here - lock recursion. */
1216 		/* NET_EPOCH_ENTER(et); */
1217 		CK_STAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
1218 			if (ifa->ifa_addr->sa_family != AF_INET)
1219 				continue;
1220 
1221 			/*
1222 			 * This is ugly but the only way for legacy IP to
1223 			 * cleanly remove addresses and everything attached.
1224 			 */
1225 			bzero(&ifr, sizeof(ifr));
1226 			ifr.ifra_addr = *ifa->ifa_addr;
1227 			if (ifa->ifa_dstaddr)
1228 			ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
1229 			(void)in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr,
1230 			    ifp, NULL);
1231 		}
1232 		/* NET_EPOCH_EXIT(et); */
1233 		in_purgemaddrs(ifp);
1234 		igmp_domifdetach(ifp);
1235 	}
1236 	IFNET_RUNLOCK();
1237 }
1238 
1239 int
1240 in_ifaddr_broadcast(struct in_addr in, struct in_ifaddr *ia)
1241 {
1242 
1243 	return ((in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1244 	     /*
1245 	      * Optionally check for old-style (host 0) broadcast, but
1246 	      * taking into account that RFC 3021 obsoletes it.
1247 	      */
1248 	    (V_broadcast_lowest && ia->ia_subnetmask != IN_RFC3021_MASK &&
1249 	    ntohl(in.s_addr) == ia->ia_subnet)) &&
1250 	     /*
1251 	      * Check for an all one subnetmask. These
1252 	      * only exist when an interface gets a secondary
1253 	      * address.
1254 	      */
1255 	    ia->ia_subnetmask != (u_long)0xffffffff);
1256 }
1257 
1258 /*
1259  * Return 1 if the address might be a local broadcast address.
1260  */
1261 int
1262 in_broadcast(struct in_addr in, struct ifnet *ifp)
1263 {
1264 	struct ifaddr *ifa;
1265 	int found;
1266 
1267 	NET_EPOCH_ASSERT();
1268 
1269 	if (in.s_addr == INADDR_BROADCAST ||
1270 	    in.s_addr == INADDR_ANY)
1271 		return (1);
1272 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
1273 		return (0);
1274 	found = 0;
1275 	/*
1276 	 * Look through the list of addresses for a match
1277 	 * with a broadcast address.
1278 	 */
1279 	CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1280 		if (ifa->ifa_addr->sa_family == AF_INET &&
1281 		    in_ifaddr_broadcast(in, (struct in_ifaddr *)ifa)) {
1282 			found = 1;
1283 			break;
1284 		}
1285 	return (found);
1286 }
1287 
1288 /*
1289  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1290  */
1291 void
1292 in_ifdetach(struct ifnet *ifp)
1293 {
1294 	IN_MULTI_LOCK();
1295 	in_pcbpurgeif0(&V_ripcbinfo, ifp);
1296 	in_pcbpurgeif0(&V_udbinfo, ifp);
1297 	in_pcbpurgeif0(&V_ulitecbinfo, ifp);
1298 	in_purgemaddrs(ifp);
1299 	IN_MULTI_UNLOCK();
1300 
1301 	/*
1302 	 * Make sure all multicast deletions invoking if_ioctl() are
1303 	 * completed before returning. Else we risk accessing a freed
1304 	 * ifnet structure pointer.
1305 	 */
1306 	inm_release_wait(NULL);
1307 }
1308 
1309 static void
1310 in_ifnet_event(void *arg __unused, struct ifnet *ifp, int event)
1311 {
1312 	struct epoch_tracker et;
1313 	struct ifaddr *ifa;
1314 	struct in_ifaddr *ia;
1315 	int error;
1316 
1317 	NET_EPOCH_ENTER(et);
1318 	switch (event) {
1319 	case IFNET_EVENT_DOWN:
1320 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1321 			if (ifa->ifa_addr->sa_family != AF_INET)
1322 				continue;
1323 			ia = (struct in_ifaddr *)ifa;
1324 			if ((ia->ia_flags & IFA_ROUTE) == 0)
1325 				continue;
1326 			ifa_ref(ifa);
1327 			/*
1328 			 * in_scrubprefix() kills the interface route.
1329 			 */
1330 			in_scrubprefix(ia, 0);
1331 			/*
1332 			 * in_ifadown gets rid of all the rest of the
1333 			 * routes.  This is not quite the right thing
1334 			 * to do, but at least if we are running a
1335 			 * routing process they will come back.
1336 			 */
1337 			in_ifadown(ifa, 0);
1338 			ifa_free(ifa);
1339 		}
1340 		break;
1341 
1342 	case IFNET_EVENT_UP:
1343 		CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1344 			if (ifa->ifa_addr->sa_family != AF_INET)
1345 				continue;
1346 			ia = (struct in_ifaddr *)ifa;
1347 			if (ia->ia_flags & IFA_ROUTE)
1348 				continue;
1349 			ifa_ref(ifa);
1350 			error = ifa_del_loopback_route(ifa, ifa->ifa_addr);
1351 			rt_addrmsg(RTM_ADD, ifa, ifa->ifa_ifp->if_fib);
1352 			error = in_handle_ifaddr_route(RTM_ADD, ia);
1353 			if (error == 0)
1354 				ia->ia_flags |= IFA_ROUTE;
1355 			error = ifa_add_loopback_route(ifa, ifa->ifa_addr);
1356 			ifa_free(ifa);
1357 		}
1358 		break;
1359 	}
1360 	NET_EPOCH_EXIT(et);
1361 }
1362 EVENTHANDLER_DEFINE(ifnet_event, in_ifnet_event, NULL, EVENTHANDLER_PRI_ANY);
1363 
1364 /*
1365  * Delete all IPv4 multicast address records, and associated link-layer
1366  * multicast address records, associated with ifp.
1367  * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1368  * XXX This should not race with ifma_protospec being set during
1369  * a new allocation, if it does, we have bigger problems.
1370  */
1371 static void
1372 in_purgemaddrs(struct ifnet *ifp)
1373 {
1374 	struct epoch_tracker	 et;
1375 	struct in_multi_head purgeinms;
1376 	struct in_multi		*inm;
1377 	struct ifmultiaddr	*ifma;
1378 
1379 	SLIST_INIT(&purgeinms);
1380 	IN_MULTI_LIST_LOCK();
1381 
1382 	/*
1383 	 * Extract list of in_multi associated with the detaching ifp
1384 	 * which the PF_INET layer is about to release.
1385 	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1386 	 * by code further down.
1387 	 */
1388 	IF_ADDR_WLOCK(ifp);
1389 	NET_EPOCH_ENTER(et);
1390 	CK_STAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1391 		inm = inm_ifmultiaddr_get_inm(ifma);
1392 		if (inm == NULL)
1393 			continue;
1394 		inm_rele_locked(&purgeinms, inm);
1395 	}
1396 	NET_EPOCH_EXIT(et);
1397 	IF_ADDR_WUNLOCK(ifp);
1398 
1399 	inm_release_list_deferred(&purgeinms);
1400 	igmp_ifdetach(ifp);
1401 	IN_MULTI_LIST_UNLOCK();
1402 }
1403 
1404 struct in_llentry {
1405 	struct llentry		base;
1406 };
1407 
1408 #define	IN_LLTBL_DEFAULT_HSIZE	32
1409 #define	IN_LLTBL_HASH(k, h) \
1410 	(((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
1411 
1412 /*
1413  * Do actual deallocation of @lle.
1414  */
1415 static void
1416 in_lltable_destroy_lle_unlocked(epoch_context_t ctx)
1417 {
1418 	struct llentry *lle;
1419 
1420 	lle = __containerof(ctx, struct llentry, lle_epoch_ctx);
1421 	LLE_LOCK_DESTROY(lle);
1422 	LLE_REQ_DESTROY(lle);
1423 	free(lle, M_LLTABLE);
1424 }
1425 
1426 /*
1427  * Called by LLE_FREE_LOCKED when number of references
1428  * drops to zero.
1429  */
1430 static void
1431 in_lltable_destroy_lle(struct llentry *lle)
1432 {
1433 
1434 	LLE_WUNLOCK(lle);
1435 	NET_EPOCH_CALL(in_lltable_destroy_lle_unlocked, &lle->lle_epoch_ctx);
1436 }
1437 
1438 static struct llentry *
1439 in_lltable_new(struct in_addr addr4, u_int flags)
1440 {
1441 	struct in_llentry *lle;
1442 
1443 	lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
1444 	if (lle == NULL)		/* NB: caller generates msg */
1445 		return NULL;
1446 
1447 	/*
1448 	 * For IPv4 this will trigger "arpresolve" to generate
1449 	 * an ARP request.
1450 	 */
1451 	lle->base.la_expire = time_uptime; /* mark expired */
1452 	lle->base.r_l3addr.addr4 = addr4;
1453 	lle->base.lle_refcnt = 1;
1454 	lle->base.lle_free = in_lltable_destroy_lle;
1455 	LLE_LOCK_INIT(&lle->base);
1456 	LLE_REQ_INIT(&lle->base);
1457 	callout_init(&lle->base.lle_timer, 1);
1458 
1459 	return (&lle->base);
1460 }
1461 
1462 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m)	(		\
1463 	((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
1464 
1465 static int
1466 in_lltable_match_prefix(const struct sockaddr *saddr,
1467     const struct sockaddr *smask, u_int flags, struct llentry *lle)
1468 {
1469 	struct in_addr addr, mask, lle_addr;
1470 
1471 	addr = ((const struct sockaddr_in *)saddr)->sin_addr;
1472 	mask = ((const struct sockaddr_in *)smask)->sin_addr;
1473 	lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
1474 
1475 	if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
1476 		return (0);
1477 
1478 	if (lle->la_flags & LLE_IFADDR) {
1479 		/*
1480 		 * Delete LLE_IFADDR records IFF address & flag matches.
1481 		 * Note that addr is the interface address within prefix
1482 		 * being matched.
1483 		 * Note also we should handle 'ifdown' cases without removing
1484 		 * ifaddr macs.
1485 		 */
1486 		if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0)
1487 			return (1);
1488 		return (0);
1489 	}
1490 
1491 	/* flags & LLE_STATIC means deleting both dynamic and static entries */
1492 	if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
1493 		return (1);
1494 
1495 	return (0);
1496 }
1497 
1498 static void
1499 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
1500 {
1501 	size_t pkts_dropped;
1502 
1503 	LLE_WLOCK_ASSERT(lle);
1504 	KASSERT(llt != NULL, ("lltable is NULL"));
1505 
1506 	/* Unlink entry from table if not already */
1507 	if ((lle->la_flags & LLE_LINKED) != 0) {
1508 		IF_AFDATA_WLOCK_ASSERT(llt->llt_ifp);
1509 		lltable_unlink_entry(llt, lle);
1510 	}
1511 
1512 	/* Drop hold queue */
1513 	pkts_dropped = llentry_free(lle);
1514 	ARPSTAT_ADD(dropped, pkts_dropped);
1515 }
1516 
1517 static int
1518 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1519 {
1520 	struct nhop_object *nh;
1521 	struct in_addr addr;
1522 
1523 	KASSERT(l3addr->sa_family == AF_INET,
1524 	    ("sin_family %d", l3addr->sa_family));
1525 
1526 	addr = ((const struct sockaddr_in *)l3addr)->sin_addr;
1527 
1528 	nh = fib4_lookup(ifp->if_fib, addr, 0, NHR_NONE, 0);
1529 	if (nh == NULL)
1530 		return (EINVAL);
1531 
1532 	/*
1533 	 * If the gateway for an existing host route matches the target L3
1534 	 * address, which is a special route inserted by some implementation
1535 	 * such as MANET, and the interface is of the correct type, then
1536 	 * allow for ARP to proceed.
1537 	 */
1538 	if (nh->nh_flags & NHF_GATEWAY) {
1539 		if (!(nh->nh_flags & NHF_HOST) || nh->nh_ifp->if_type != IFT_ETHER ||
1540 		    (nh->nh_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1541 		    memcmp(nh->gw_sa.sa_data, l3addr->sa_data,
1542 		    sizeof(in_addr_t)) != 0) {
1543 			return (EINVAL);
1544 		}
1545 	}
1546 
1547 	/*
1548 	 * Make sure that at least the destination address is covered
1549 	 * by the route. This is for handling the case where 2 or more
1550 	 * interfaces have the same prefix. An incoming packet arrives
1551 	 * on one interface and the corresponding outgoing packet leaves
1552 	 * another interface.
1553 	 */
1554 	if ((nh->nh_ifp != ifp) && (nh->nh_flags & NHF_HOST) == 0) {
1555 		struct in_ifaddr *ia = (struct in_ifaddr *)ifaof_ifpforaddr(l3addr, ifp);
1556 		struct in_addr dst_addr, mask_addr;
1557 
1558 		if (ia == NULL)
1559 			return (EINVAL);
1560 
1561 		/*
1562 		 * ifaof_ifpforaddr() returns _best matching_ IFA.
1563 		 * It is possible that ifa prefix does not cover our address.
1564 		 * Explicitly verify and fail if that's the case.
1565 		 */
1566 		dst_addr = IA_SIN(ia)->sin_addr;
1567 		mask_addr.s_addr = htonl(ia->ia_subnetmask);
1568 
1569 		if (!IN_ARE_MASKED_ADDR_EQUAL(dst_addr, addr, mask_addr))
1570 			return (EINVAL);
1571 	}
1572 
1573 	return (0);
1574 }
1575 
1576 static inline uint32_t
1577 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
1578 {
1579 
1580 	return (IN_LLTBL_HASH(dst.s_addr, hsize));
1581 }
1582 
1583 static uint32_t
1584 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
1585 {
1586 
1587 	return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
1588 }
1589 
1590 static void
1591 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
1592 {
1593 	struct sockaddr_in *sin;
1594 
1595 	sin = (struct sockaddr_in *)sa;
1596 	bzero(sin, sizeof(*sin));
1597 	sin->sin_family = AF_INET;
1598 	sin->sin_len = sizeof(*sin);
1599 	sin->sin_addr = lle->r_l3addr.addr4;
1600 }
1601 
1602 static inline struct llentry *
1603 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
1604 {
1605 	struct llentry *lle;
1606 	struct llentries *lleh;
1607 	u_int hashidx;
1608 
1609 	hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
1610 	lleh = &llt->lle_head[hashidx];
1611 	CK_LIST_FOREACH(lle, lleh, lle_next) {
1612 		if (lle->la_flags & LLE_DELETED)
1613 			continue;
1614 		if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
1615 			break;
1616 	}
1617 
1618 	return (lle);
1619 }
1620 
1621 static void
1622 in_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
1623 {
1624 
1625 	lle->la_flags |= LLE_DELETED;
1626 	EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1627 #ifdef DIAGNOSTIC
1628 	log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1629 #endif
1630 	llentry_free(lle);
1631 }
1632 
1633 static struct llentry *
1634 in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1635 {
1636 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1637 	struct ifnet *ifp = llt->llt_ifp;
1638 	struct llentry *lle;
1639 	char linkhdr[LLE_MAX_LINKHDR];
1640 	size_t linkhdrsize;
1641 	int lladdr_off;
1642 
1643 	KASSERT(l3addr->sa_family == AF_INET,
1644 	    ("sin_family %d", l3addr->sa_family));
1645 
1646 	/*
1647 	 * A route that covers the given address must have
1648 	 * been installed 1st because we are doing a resolution,
1649 	 * verify this.
1650 	 */
1651 	if (!(flags & LLE_IFADDR) &&
1652 	    in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1653 		return (NULL);
1654 
1655 	lle = in_lltable_new(sin->sin_addr, flags);
1656 	if (lle == NULL) {
1657 		log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1658 		return (NULL);
1659 	}
1660 	lle->la_flags = flags;
1661 	if (flags & LLE_STATIC)
1662 		lle->r_flags |= RLLE_VALID;
1663 	if ((flags & LLE_IFADDR) == LLE_IFADDR) {
1664 		linkhdrsize = LLE_MAX_LINKHDR;
1665 		if (lltable_calc_llheader(ifp, AF_INET, IF_LLADDR(ifp),
1666 		    linkhdr, &linkhdrsize, &lladdr_off) != 0) {
1667 			in_lltable_free_entry(llt, lle);
1668 			return (NULL);
1669 		}
1670 		lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
1671 		    lladdr_off);
1672 		lle->la_flags |= LLE_STATIC;
1673 		lle->r_flags |= (RLLE_VALID | RLLE_IFADDR);
1674 		lle->la_expire = 0;
1675 	}
1676 
1677 	return (lle);
1678 }
1679 
1680 /*
1681  * Return NULL if not found or marked for deletion.
1682  * If found return lle read locked.
1683  */
1684 static struct llentry *
1685 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1686 {
1687 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1688 	struct llentry *lle;
1689 
1690 	IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
1691 	KASSERT(l3addr->sa_family == AF_INET,
1692 	    ("sin_family %d", l3addr->sa_family));
1693 	KASSERT((flags & (LLE_UNLOCKED | LLE_EXCLUSIVE)) !=
1694 	    (LLE_UNLOCKED | LLE_EXCLUSIVE),
1695 	    ("wrong lle request flags: %#x", flags));
1696 
1697 	lle = in_lltable_find_dst(llt, sin->sin_addr);
1698 	if (lle == NULL)
1699 		return (NULL);
1700 	if (flags & LLE_UNLOCKED)
1701 		return (lle);
1702 
1703 	if (flags & LLE_EXCLUSIVE)
1704 		LLE_WLOCK(lle);
1705 	else
1706 		LLE_RLOCK(lle);
1707 
1708 	/*
1709 	 * If the afdata lock is not held, the LLE may have been unlinked while
1710 	 * we were blocked on the LLE lock.  Check for this case.
1711 	 */
1712 	if (__predict_false((lle->la_flags & LLE_LINKED) == 0)) {
1713 		if (flags & LLE_EXCLUSIVE)
1714 			LLE_WUNLOCK(lle);
1715 		else
1716 			LLE_RUNLOCK(lle);
1717 		return (NULL);
1718 	}
1719 	return (lle);
1720 }
1721 
1722 static int
1723 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
1724     struct sysctl_req *wr)
1725 {
1726 	struct ifnet *ifp = llt->llt_ifp;
1727 	/* XXX stack use */
1728 	struct {
1729 		struct rt_msghdr	rtm;
1730 		struct sockaddr_in	sin;
1731 		struct sockaddr_dl	sdl;
1732 	} arpc;
1733 	struct sockaddr_dl *sdl;
1734 	int error;
1735 
1736 	bzero(&arpc, sizeof(arpc));
1737 	/* skip deleted entries */
1738 	if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1739 		return (0);
1740 	/* Skip if jailed and not a valid IP of the prison. */
1741 	lltable_fill_sa_entry(lle,(struct sockaddr *)&arpc.sin);
1742 	if (prison_if(wr->td->td_ucred, (struct sockaddr *)&arpc.sin) != 0)
1743 		return (0);
1744 	/*
1745 	 * produce a msg made of:
1746 	 *  struct rt_msghdr;
1747 	 *  struct sockaddr_in; (IPv4)
1748 	 *  struct sockaddr_dl;
1749 	 */
1750 	arpc.rtm.rtm_msglen = sizeof(arpc);
1751 	arpc.rtm.rtm_version = RTM_VERSION;
1752 	arpc.rtm.rtm_type = RTM_GET;
1753 	arpc.rtm.rtm_flags = RTF_UP;
1754 	arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1755 
1756 	/* publish */
1757 	if (lle->la_flags & LLE_PUB)
1758 		arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1759 
1760 	sdl = &arpc.sdl;
1761 	sdl->sdl_family = AF_LINK;
1762 	sdl->sdl_len = sizeof(*sdl);
1763 	sdl->sdl_index = ifp->if_index;
1764 	sdl->sdl_type = ifp->if_type;
1765 	if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1766 		sdl->sdl_alen = ifp->if_addrlen;
1767 		bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1768 	} else {
1769 		sdl->sdl_alen = 0;
1770 		bzero(LLADDR(sdl), ifp->if_addrlen);
1771 	}
1772 
1773 	arpc.rtm.rtm_rmx.rmx_expire =
1774 	    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1775 	arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1776 	if (lle->la_flags & LLE_STATIC)
1777 		arpc.rtm.rtm_flags |= RTF_STATIC;
1778 	if (lle->la_flags & LLE_IFADDR)
1779 		arpc.rtm.rtm_flags |= RTF_PINNED;
1780 	arpc.rtm.rtm_index = ifp->if_index;
1781 	error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1782 
1783 	return (error);
1784 }
1785 
1786 static void
1787 in_lltable_post_resolved(struct lltable *llt, struct llentry *lle)
1788 {
1789 	struct ifnet *ifp = llt->llt_ifp;
1790 
1791 	/* gratuitous ARP */
1792 	if ((lle->la_flags & LLE_PUB) != 0)
1793 		arprequest(ifp, &lle->r_l3addr.addr4, &lle->r_l3addr.addr4,
1794 		    lle->ll_addr);
1795 }
1796 
1797 static struct lltable *
1798 in_lltattach(struct ifnet *ifp)
1799 {
1800 	struct lltable *llt;
1801 
1802 	llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
1803  	llt->llt_af = AF_INET;
1804  	llt->llt_ifp = ifp;
1805 
1806 	llt->llt_lookup = in_lltable_lookup;
1807 	llt->llt_alloc_entry = in_lltable_alloc;
1808 	llt->llt_delete_entry = in_lltable_delete_entry;
1809 	llt->llt_dump_entry = in_lltable_dump_entry;
1810 	llt->llt_hash = in_lltable_hash;
1811 	llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
1812 	llt->llt_free_entry = in_lltable_free_entry;
1813 	llt->llt_match_prefix = in_lltable_match_prefix;
1814 	llt->llt_mark_used = llentry_mark_used;
1815 	llt->llt_post_resolved = in_lltable_post_resolved;
1816  	lltable_link(llt);
1817 
1818 	return (llt);
1819 }
1820 
1821 struct lltable *
1822 in_lltable_get(struct ifnet *ifp)
1823 {
1824 	struct lltable *llt = NULL;
1825 
1826 	void *afdata_ptr = ifp->if_afdata[AF_INET];
1827 	if (afdata_ptr != NULL)
1828 		llt = ((struct in_ifinfo *)afdata_ptr)->ii_llt;
1829 	return (llt);
1830 }
1831 
1832 void *
1833 in_domifattach(struct ifnet *ifp)
1834 {
1835 	struct in_ifinfo *ii;
1836 
1837 	ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1838 
1839 	ii->ii_llt = in_lltattach(ifp);
1840 	ii->ii_igmp = igmp_domifattach(ifp);
1841 
1842 	return (ii);
1843 }
1844 
1845 void
1846 in_domifdetach(struct ifnet *ifp, void *aux)
1847 {
1848 	struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1849 
1850 	igmp_domifdetach(ifp);
1851 	lltable_free(ii->ii_llt);
1852 	free(ii, M_IFADDR);
1853 }
1854