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