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