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