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