xref: /freebsd/sys/netinet/in.c (revision 40a8ac8f62b535d30349faf28cf47106b7041b83)
1 /*-
2  * Copyright (c) 1982, 1986, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (C) 2001 WIDE Project.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 4. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *	@(#)in.c	8.4 (Berkeley) 1/9/95
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_mpath.h"
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/proc.h>
48 #include <sys/sysctl.h>
49 #include <sys/syslog.h>
50 #include <sys/sx.h>
51 
52 #include <net/if.h>
53 #include <net/if_var.h>
54 #include <net/if_arp.h>
55 #include <net/if_dl.h>
56 #include <net/if_llatbl.h>
57 #include <net/if_types.h>
58 #include <net/route.h>
59 #include <net/vnet.h>
60 
61 #include <netinet/if_ether.h>
62 #include <netinet/in.h>
63 #include <netinet/in_var.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/ip_var.h>
66 #include <netinet/ip_carp.h>
67 #include <netinet/igmp_var.h>
68 #include <netinet/udp.h>
69 #include <netinet/udp_var.h>
70 
71 static int in_aifaddr_ioctl(u_long, caddr_t, struct ifnet *, struct thread *);
72 static int in_difaddr_ioctl(caddr_t, struct ifnet *, struct thread *);
73 
74 static void	in_socktrim(struct sockaddr_in *);
75 static void	in_purgemaddrs(struct ifnet *);
76 
77 static VNET_DEFINE(int, nosameprefix);
78 #define	V_nosameprefix			VNET(nosameprefix)
79 SYSCTL_VNET_INT(_net_inet_ip, OID_AUTO, no_same_prefix, CTLFLAG_RW,
80 	&VNET_NAME(nosameprefix), 0,
81 	"Refuse to create same prefixes on different interfaces");
82 
83 VNET_DECLARE(struct inpcbinfo, ripcbinfo);
84 #define	V_ripcbinfo			VNET(ripcbinfo)
85 
86 static struct sx in_control_sx;
87 SX_SYSINIT(in_control_sx, &in_control_sx, "in_control");
88 
89 /*
90  * Return 1 if an internet address is for a ``local'' host
91  * (one to which we have a connection).
92  */
93 int
94 in_localaddr(struct in_addr in)
95 {
96 	register u_long i = ntohl(in.s_addr);
97 	register struct in_ifaddr *ia;
98 
99 	IN_IFADDR_RLOCK();
100 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
101 		if ((i & ia->ia_subnetmask) == ia->ia_subnet) {
102 			IN_IFADDR_RUNLOCK();
103 			return (1);
104 		}
105 	}
106 	IN_IFADDR_RUNLOCK();
107 	return (0);
108 }
109 
110 /*
111  * Return 1 if an internet address is for the local host and configured
112  * on one of its interfaces.
113  */
114 int
115 in_localip(struct in_addr in)
116 {
117 	struct in_ifaddr *ia;
118 
119 	IN_IFADDR_RLOCK();
120 	LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
121 		if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr) {
122 			IN_IFADDR_RUNLOCK();
123 			return (1);
124 		}
125 	}
126 	IN_IFADDR_RUNLOCK();
127 	return (0);
128 }
129 
130 /*
131  * Return a reference to the interface address which is different to
132  * the supplied one but with same IP address value.
133  */
134 static struct in_ifaddr *
135 in_localip_more(struct in_ifaddr *ia)
136 {
137 	in_addr_t in = IA_SIN(ia)->sin_addr.s_addr;
138 	struct in_ifaddr *it;
139 
140 	IN_IFADDR_RLOCK();
141 	LIST_FOREACH(it, INADDR_HASH(in), ia_hash) {
142 		if (it != ia && IA_SIN(it)->sin_addr.s_addr == in) {
143 			ifa_ref(&it->ia_ifa);
144 			IN_IFADDR_RUNLOCK();
145 			return (it);
146 		}
147 	}
148 	IN_IFADDR_RUNLOCK();
149 
150 	return (NULL);
151 }
152 
153 /*
154  * Determine whether an IP address is in a reserved set of addresses
155  * that may not be forwarded, or whether datagrams to that destination
156  * may be forwarded.
157  */
158 int
159 in_canforward(struct in_addr in)
160 {
161 	register u_long i = ntohl(in.s_addr);
162 	register u_long net;
163 
164 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
165 		return (0);
166 	if (IN_CLASSA(i)) {
167 		net = i & IN_CLASSA_NET;
168 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
169 			return (0);
170 	}
171 	return (1);
172 }
173 
174 /*
175  * Trim a mask in a sockaddr
176  */
177 static void
178 in_socktrim(struct sockaddr_in *ap)
179 {
180     register char *cplim = (char *) &ap->sin_addr;
181     register char *cp = (char *) (&ap->sin_addr + 1);
182 
183     ap->sin_len = 0;
184     while (--cp >= cplim)
185 	if (*cp) {
186 	    (ap)->sin_len = cp - (char *) (ap) + 1;
187 	    break;
188 	}
189 }
190 
191 /*
192  * Generic internet control operations (ioctl's).
193  */
194 int
195 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
196     struct thread *td)
197 {
198 	struct ifreq *ifr = (struct ifreq *)data;
199 	struct sockaddr_in *addr = (struct sockaddr_in *)&ifr->ifr_addr;
200 	struct ifaddr *ifa;
201 	struct in_ifaddr *ia;
202 	int error;
203 
204 	if (ifp == NULL)
205 		return (EADDRNOTAVAIL);
206 
207 	/*
208 	 * Filter out 4 ioctls we implement directly.  Forward the rest
209 	 * to specific functions and ifp->if_ioctl().
210 	 */
211 	switch (cmd) {
212 	case SIOCGIFADDR:
213 	case SIOCGIFBRDADDR:
214 	case SIOCGIFDSTADDR:
215 	case SIOCGIFNETMASK:
216 		break;
217 	case SIOCDIFADDR:
218 		sx_xlock(&in_control_sx);
219 		error = in_difaddr_ioctl(data, ifp, td);
220 		sx_xunlock(&in_control_sx);
221 		return (error);
222 	case OSIOCAIFADDR:	/* 9.x compat */
223 	case SIOCAIFADDR:
224 		sx_xlock(&in_control_sx);
225 		error = in_aifaddr_ioctl(cmd, data, ifp, td);
226 		sx_xunlock(&in_control_sx);
227 		return (error);
228 	case SIOCSIFADDR:
229 	case SIOCSIFBRDADDR:
230 	case SIOCSIFDSTADDR:
231 	case SIOCSIFNETMASK:
232 		/* We no longer support that old commands. */
233 		return (EINVAL);
234 	default:
235 		if (ifp->if_ioctl == NULL)
236 			return (EOPNOTSUPP);
237 		return ((*ifp->if_ioctl)(ifp, cmd, data));
238 	}
239 
240 	if (addr->sin_addr.s_addr != INADDR_ANY &&
241 	    prison_check_ip4(td->td_ucred, &addr->sin_addr) != 0)
242 		return (EADDRNOTAVAIL);
243 
244 	/*
245 	 * Find address for this interface, if it exists.  If an
246 	 * address was specified, find that one instead of the
247 	 * first one on the interface, if possible.
248 	 */
249 	IF_ADDR_RLOCK(ifp);
250 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
251 		if (ifa->ifa_addr->sa_family != AF_INET)
252 			continue;
253 		ia = (struct in_ifaddr *)ifa;
254 		if (ia->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr)
255 			break;
256 	}
257 	if (ifa == NULL)
258 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
259 			if (ifa->ifa_addr->sa_family == AF_INET) {
260 				ia = (struct in_ifaddr *)ifa;
261 				if (prison_check_ip4(td->td_ucred,
262 				    &ia->ia_addr.sin_addr) == 0)
263 					break;
264 			}
265 
266 	if (ifa == NULL) {
267 		IF_ADDR_RUNLOCK(ifp);
268 		return (EADDRNOTAVAIL);
269 	}
270 
271 	error = 0;
272 	switch (cmd) {
273 	case SIOCGIFADDR:
274 		*addr = ia->ia_addr;
275 		break;
276 
277 	case SIOCGIFBRDADDR:
278 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
279 			error = EINVAL;
280 			break;
281 		}
282 		*addr = ia->ia_broadaddr;
283 		break;
284 
285 	case SIOCGIFDSTADDR:
286 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
287 			error = EINVAL;
288 			break;
289 		}
290 		*addr = ia->ia_dstaddr;
291 		break;
292 
293 	case SIOCGIFNETMASK:
294 		*addr = ia->ia_sockmask;
295 		break;
296 	}
297 
298 	IF_ADDR_RUNLOCK(ifp);
299 
300 	return (error);
301 }
302 
303 static int
304 in_aifaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
305 {
306 	const struct in_aliasreq *ifra = (struct in_aliasreq *)data;
307 	const struct sockaddr_in *addr = &ifra->ifra_addr;
308 	const struct sockaddr_in *broadaddr = &ifra->ifra_broadaddr;
309 	const struct sockaddr_in *mask = &ifra->ifra_mask;
310 	const struct sockaddr_in *dstaddr = &ifra->ifra_dstaddr;
311 	const int vhid = (cmd == SIOCAIFADDR) ? ifra->ifra_vhid : 0;
312 	struct ifaddr *ifa;
313 	struct in_ifaddr *ia;
314 	bool iaIsFirst;
315 	int error = 0;
316 
317 	error = priv_check(td, PRIV_NET_ADDIFADDR);
318 	if (error)
319 		return (error);
320 
321 	/*
322 	 * ifra_addr must be present and be of INET family.
323 	 * ifra_broadaddr/ifra_dstaddr and ifra_mask are optional.
324 	 */
325 	if (addr->sin_len != sizeof(struct sockaddr_in) ||
326 	    addr->sin_family != AF_INET)
327 		return (EINVAL);
328 	if (broadaddr->sin_len != 0 &&
329 	    (broadaddr->sin_len != sizeof(struct sockaddr_in) ||
330 	    broadaddr->sin_family != AF_INET))
331 		return (EINVAL);
332 	if (mask->sin_len != 0 &&
333 	    (mask->sin_len != sizeof(struct sockaddr_in) ||
334 	    mask->sin_family != AF_INET))
335 		return (EINVAL);
336 	if ((ifp->if_flags & IFF_POINTOPOINT) &&
337 	    (dstaddr->sin_len != sizeof(struct sockaddr_in) ||
338 	     dstaddr->sin_addr.s_addr == INADDR_ANY))
339 		return (EDESTADDRREQ);
340 	if (vhid > 0 && carp_attach_p == NULL)
341 		return (EPROTONOSUPPORT);
342 
343 	/*
344 	 * See whether address already exist.
345 	 */
346 	iaIsFirst = true;
347 	ia = NULL;
348 	IF_ADDR_RLOCK(ifp);
349 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
350 		struct in_ifaddr *it;
351 
352 		if (ifa->ifa_addr->sa_family != AF_INET)
353 			continue;
354 
355 		it = (struct in_ifaddr *)ifa;
356 		iaIsFirst = false;
357 		if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
358 		    prison_check_ip4(td->td_ucred, &addr->sin_addr) == 0)
359 			ia = it;
360 	}
361 	IF_ADDR_RUNLOCK(ifp);
362 
363 	if (ia != NULL)
364 		(void )in_difaddr_ioctl(data, ifp, td);
365 
366 	ifa = ifa_alloc(sizeof(struct in_ifaddr), M_WAITOK);
367 	ia = (struct in_ifaddr *)ifa;
368 	ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
369 	ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
370 	ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
371 
372 	ia->ia_ifp = ifp;
373 	ia->ia_addr = *addr;
374 	if (mask->sin_len != 0) {
375 		ia->ia_sockmask = *mask;
376 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
377 	} else {
378 		in_addr_t i = ntohl(addr->sin_addr.s_addr);
379 
380 		/*
381 	 	 * Be compatible with network classes, if netmask isn't
382 		 * supplied, guess it based on classes.
383 	 	 */
384 		if (IN_CLASSA(i))
385 			ia->ia_subnetmask = IN_CLASSA_NET;
386 		else if (IN_CLASSB(i))
387 			ia->ia_subnetmask = IN_CLASSB_NET;
388 		else
389 			ia->ia_subnetmask = IN_CLASSC_NET;
390 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
391 	}
392 	ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
393 	in_socktrim(&ia->ia_sockmask);
394 
395 	if (ifp->if_flags & IFF_BROADCAST) {
396 		if (broadaddr->sin_len != 0) {
397 			ia->ia_broadaddr = *broadaddr;
398 		} else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
399 			ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
400 			ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
401 			ia->ia_broadaddr.sin_family = AF_INET;
402 		} else {
403 			ia->ia_broadaddr.sin_addr.s_addr =
404 			    htonl(ia->ia_subnet | ~ia->ia_subnetmask);
405 			ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
406 			ia->ia_broadaddr.sin_family = AF_INET;
407 		}
408 	}
409 
410 	if (ifp->if_flags & IFF_POINTOPOINT)
411 		ia->ia_dstaddr = *dstaddr;
412 
413 	/* XXXGL: rtinit() needs this strange assignment. */
414 	if (ifp->if_flags & IFF_LOOPBACK)
415                 ia->ia_dstaddr = ia->ia_addr;
416 
417 	if (vhid != 0) {
418 		error = (*carp_attach_p)(&ia->ia_ifa, vhid);
419 		if (error)
420 			return (error);
421 	}
422 
423 	/* if_addrhead is already referenced by ifa_alloc() */
424 	IF_ADDR_WLOCK(ifp);
425 	TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
426 	IF_ADDR_WUNLOCK(ifp);
427 
428 	ifa_ref(ifa);			/* in_ifaddrhead */
429 	IN_IFADDR_WLOCK();
430 	TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
431 	LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
432 	IN_IFADDR_WUNLOCK();
433 
434 	/*
435 	 * Give the interface a chance to initialize
436 	 * if this is its first address,
437 	 * and to validate the address if necessary.
438 	 */
439 	if (ifp->if_ioctl != NULL) {
440 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
441 		if (error)
442 			goto fail1;
443 	}
444 
445 	/*
446 	 * Add route for the network.
447 	 */
448 	if (vhid == 0) {
449 		int flags = RTF_UP;
450 
451 		if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
452 			flags |= RTF_HOST;
453 
454 		error = in_addprefix(ia, flags);
455 		if (error)
456 			goto fail1;
457 	}
458 
459 	/*
460 	 * Add a loopback route to self.
461 	 */
462 	if (vhid == 0 && (ifp->if_flags & IFF_LOOPBACK) == 0 &&
463 	    ia->ia_addr.sin_addr.s_addr != INADDR_ANY &&
464 	    !((ifp->if_flags & IFF_POINTOPOINT) &&
465 	     ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)) {
466 		struct in_ifaddr *eia;
467 
468 		eia = in_localip_more(ia);
469 
470 		if (eia == NULL) {
471 			error = ifa_add_loopback_route((struct ifaddr *)ia,
472 			    (struct sockaddr *)&ia->ia_addr);
473 			if (error)
474 				goto fail2;
475 		} else
476 			ifa_free(&eia->ia_ifa);
477 	}
478 
479 	if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
480 		struct in_addr allhosts_addr;
481 		struct in_ifinfo *ii;
482 
483 		ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
484 		allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
485 
486 		error = in_joingroup(ifp, &allhosts_addr, NULL,
487 			&ii->ii_allhosts);
488 	}
489 
490 	EVENTHANDLER_INVOKE(ifaddr_event, ifp);
491 
492 	return (error);
493 
494 fail2:
495 	if (vhid == 0)
496 		(void )in_scrubprefix(ia, LLE_STATIC);
497 
498 fail1:
499 	if (ia->ia_ifa.ifa_carp)
500 		(*carp_detach_p)(&ia->ia_ifa);
501 
502 	IF_ADDR_WLOCK(ifp);
503 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
504 	IF_ADDR_WUNLOCK(ifp);
505 	ifa_free(&ia->ia_ifa);		/* if_addrhead */
506 
507 	IN_IFADDR_WLOCK();
508 	TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
509 	LIST_REMOVE(ia, ia_hash);
510 	IN_IFADDR_WUNLOCK();
511 	ifa_free(&ia->ia_ifa);		/* in_ifaddrhead */
512 
513 	return (error);
514 }
515 
516 static int
517 in_difaddr_ioctl(caddr_t data, struct ifnet *ifp, struct thread *td)
518 {
519 	const struct ifreq *ifr = (struct ifreq *)data;
520 	const struct sockaddr_in *addr = (const struct sockaddr_in *)
521 	    &ifr->ifr_addr;
522 	struct ifaddr *ifa;
523 	struct in_ifaddr *ia;
524 	bool deleteAny, iaIsLast;
525 	int error;
526 
527 	if (td != NULL) {
528 		error = priv_check(td, PRIV_NET_DELIFADDR);
529 		if (error)
530 			return (error);
531 	}
532 
533 	if (addr->sin_len != sizeof(struct sockaddr_in) ||
534 	    addr->sin_family != AF_INET)
535 		deleteAny = true;
536 	else
537 		deleteAny = false;
538 
539 	iaIsLast = true;
540 	ia = NULL;
541 	IF_ADDR_WLOCK(ifp);
542 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
543 		struct in_ifaddr *it;
544 
545 		if (ifa->ifa_addr->sa_family != AF_INET)
546 			continue;
547 
548 		it = (struct in_ifaddr *)ifa;
549 		if (deleteAny && ia == NULL && (td == NULL ||
550 		    prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0))
551 			ia = it;
552 
553 		if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
554 		    (td == NULL || prison_check_ip4(td->td_ucred,
555 		    &addr->sin_addr) == 0))
556 			ia = it;
557 
558 		if (it != ia)
559 			iaIsLast = false;
560 	}
561 
562 	if (ia == NULL) {
563 		IF_ADDR_WUNLOCK(ifp);
564 		return (EADDRNOTAVAIL);
565 	}
566 
567 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
568 	IF_ADDR_WUNLOCK(ifp);
569 	ifa_free(&ia->ia_ifa);		/* if_addrhead */
570 
571 	IN_IFADDR_WLOCK();
572 	TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
573 	LIST_REMOVE(ia, ia_hash);
574 	IN_IFADDR_WUNLOCK();
575 
576 	/*
577 	 * in_scrubprefix() kills the interface route.
578 	 */
579 	in_scrubprefix(ia, LLE_STATIC);
580 
581 	/*
582 	 * in_ifadown gets rid of all the rest of
583 	 * the routes.  This is not quite the right
584 	 * thing to do, but at least if we are running
585 	 * a routing process they will come back.
586 	 */
587 	in_ifadown(&ia->ia_ifa, 1);
588 
589 	if (ia->ia_ifa.ifa_carp)
590 		(*carp_detach_p)(&ia->ia_ifa);
591 
592 	/*
593 	 * If this is the last IPv4 address configured on this
594 	 * interface, leave the all-hosts group.
595 	 * No state-change report need be transmitted.
596 	 */
597 	if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
598 		struct in_ifinfo *ii;
599 
600 		ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
601 		IN_MULTI_LOCK();
602 		if (ii->ii_allhosts) {
603 			(void)in_leavegroup_locked(ii->ii_allhosts, NULL);
604 			ii->ii_allhosts = NULL;
605 		}
606 		IN_MULTI_UNLOCK();
607 	}
608 
609 	EVENTHANDLER_INVOKE(ifaddr_event, ifp);
610 	ifa_free(&ia->ia_ifa);		/* in_ifaddrhead */
611 
612 	return (0);
613 }
614 
615 #define rtinitflags(x) \
616 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
617 	    ? RTF_HOST : 0)
618 
619 /*
620  * Check if we have a route for the given prefix already or add one accordingly.
621  */
622 int
623 in_addprefix(struct in_ifaddr *target, int flags)
624 {
625 	struct in_ifaddr *ia;
626 	struct in_addr prefix, mask, p, m;
627 	int error;
628 
629 	if ((flags & RTF_HOST) != 0) {
630 		prefix = target->ia_dstaddr.sin_addr;
631 		mask.s_addr = 0;
632 	} else {
633 		prefix = target->ia_addr.sin_addr;
634 		mask = target->ia_sockmask.sin_addr;
635 		prefix.s_addr &= mask.s_addr;
636 	}
637 
638 	IN_IFADDR_RLOCK();
639 	/* Look for an existing address with the same prefix, mask, and fib */
640 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
641 		if (rtinitflags(ia)) {
642 			p = ia->ia_dstaddr.sin_addr;
643 
644 			if (prefix.s_addr != p.s_addr)
645 				continue;
646 		} else {
647 			p = ia->ia_addr.sin_addr;
648 			m = ia->ia_sockmask.sin_addr;
649 			p.s_addr &= m.s_addr;
650 
651 			if (prefix.s_addr != p.s_addr ||
652 			    mask.s_addr != m.s_addr)
653 				continue;
654 		}
655 		if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
656 			continue;
657 
658 		/*
659 		 * If we got a matching prefix route inserted by other
660 		 * interface address, we are done here.
661 		 */
662 		if (ia->ia_flags & IFA_ROUTE) {
663 #ifdef RADIX_MPATH
664 			if (ia->ia_addr.sin_addr.s_addr ==
665 			    target->ia_addr.sin_addr.s_addr) {
666 				IN_IFADDR_RUNLOCK();
667 				return (EEXIST);
668 			} else
669 				break;
670 #endif
671 			if (V_nosameprefix) {
672 				IN_IFADDR_RUNLOCK();
673 				return (EEXIST);
674 			} else {
675 				int fibnum;
676 
677 				fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS :
678 					target->ia_ifp->if_fib;
679 				rt_addrmsg(RTM_ADD, &target->ia_ifa, fibnum);
680 				IN_IFADDR_RUNLOCK();
681 				return (0);
682 			}
683 		}
684 	}
685 	IN_IFADDR_RUNLOCK();
686 
687 	/*
688 	 * No-one seem to have this prefix route, so we try to insert it.
689 	 */
690 	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
691 	if (!error)
692 		target->ia_flags |= IFA_ROUTE;
693 	return (error);
694 }
695 
696 /*
697  * If there is no other address in the system that can serve a route to the
698  * same prefix, remove the route.  Hand over the route to the new address
699  * otherwise.
700  */
701 int
702 in_scrubprefix(struct in_ifaddr *target, u_int flags)
703 {
704 	struct in_ifaddr *ia;
705 	struct in_addr prefix, mask, p, m;
706 	int error = 0;
707 	struct sockaddr_in prefix0, mask0;
708 
709 	/*
710 	 * Remove the loopback route to the interface address.
711 	 */
712 	if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
713 	    !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
714 	    (flags & LLE_STATIC)) {
715 		struct in_ifaddr *eia;
716 
717 		eia = in_localip_more(target);
718 
719 		if (eia != NULL) {
720 			int fibnum = target->ia_ifp->if_fib;
721 
722 			error = ifa_switch_loopback_route((struct ifaddr *)eia,
723 			    (struct sockaddr *)&target->ia_addr, fibnum);
724 			ifa_free(&eia->ia_ifa);
725 		} else {
726 			error = ifa_del_loopback_route((struct ifaddr *)target,
727 			    (struct sockaddr *)&target->ia_addr);
728 		}
729 
730 		if (!(target->ia_ifp->if_flags & IFF_NOARP))
731 			/* remove arp cache */
732 			arp_ifscrub(target->ia_ifp,
733 			    IA_SIN(target)->sin_addr.s_addr);
734 	}
735 
736 	if (rtinitflags(target)) {
737 		prefix = target->ia_dstaddr.sin_addr;
738 		mask.s_addr = 0;
739 	} else {
740 		prefix = target->ia_addr.sin_addr;
741 		mask = target->ia_sockmask.sin_addr;
742 		prefix.s_addr &= mask.s_addr;
743 	}
744 
745 	if ((target->ia_flags & IFA_ROUTE) == 0) {
746 		int fibnum;
747 
748 		fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS :
749 			target->ia_ifp->if_fib;
750 		rt_addrmsg(RTM_DELETE, &target->ia_ifa, fibnum);
751 		return (0);
752 	}
753 
754 	IN_IFADDR_RLOCK();
755 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
756 		if (rtinitflags(ia)) {
757 			p = ia->ia_dstaddr.sin_addr;
758 
759 			if (prefix.s_addr != p.s_addr)
760 				continue;
761 		} else {
762 			p = ia->ia_addr.sin_addr;
763 			m = ia->ia_sockmask.sin_addr;
764 			p.s_addr &= m.s_addr;
765 
766 			if (prefix.s_addr != p.s_addr ||
767 			    mask.s_addr != m.s_addr)
768 				continue;
769 		}
770 
771 		if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
772 			continue;
773 
774 		/*
775 		 * If we got a matching prefix address, move IFA_ROUTE and
776 		 * the route itself to it.  Make sure that routing daemons
777 		 * get a heads-up.
778 		 */
779 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
780 			ifa_ref(&ia->ia_ifa);
781 			IN_IFADDR_RUNLOCK();
782 			error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
783 			    rtinitflags(target));
784 			if (error == 0)
785 				target->ia_flags &= ~IFA_ROUTE;
786 			else
787 				log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
788 					error);
789 			error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
790 			    rtinitflags(ia) | RTF_UP);
791 			if (error == 0)
792 				ia->ia_flags |= IFA_ROUTE;
793 			else
794 				log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
795 					error);
796 			ifa_free(&ia->ia_ifa);
797 			return (error);
798 		}
799 	}
800 	IN_IFADDR_RUNLOCK();
801 
802 	/*
803 	 * remove all L2 entries on the given prefix
804 	 */
805 	bzero(&prefix0, sizeof(prefix0));
806 	prefix0.sin_len = sizeof(prefix0);
807 	prefix0.sin_family = AF_INET;
808 	prefix0.sin_addr.s_addr = target->ia_subnet;
809 	bzero(&mask0, sizeof(mask0));
810 	mask0.sin_len = sizeof(mask0);
811 	mask0.sin_family = AF_INET;
812 	mask0.sin_addr.s_addr = target->ia_subnetmask;
813 	lltable_prefix_free(AF_INET, (struct sockaddr *)&prefix0,
814 	    (struct sockaddr *)&mask0, flags);
815 
816 	/*
817 	 * As no-one seem to have this prefix, we can remove the route.
818 	 */
819 	error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
820 	if (error == 0)
821 		target->ia_flags &= ~IFA_ROUTE;
822 	else
823 		log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
824 	return (error);
825 }
826 
827 #undef rtinitflags
828 
829 /*
830  * Return 1 if the address might be a local broadcast address.
831  */
832 int
833 in_broadcast(struct in_addr in, struct ifnet *ifp)
834 {
835 	register struct ifaddr *ifa;
836 	u_long t;
837 
838 	if (in.s_addr == INADDR_BROADCAST ||
839 	    in.s_addr == INADDR_ANY)
840 		return (1);
841 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
842 		return (0);
843 	t = ntohl(in.s_addr);
844 	/*
845 	 * Look through the list of addresses for a match
846 	 * with a broadcast address.
847 	 */
848 #define ia ((struct in_ifaddr *)ifa)
849 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
850 		if (ifa->ifa_addr->sa_family == AF_INET &&
851 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
852 		     /*
853 		      * Check for old-style (host 0) broadcast, but
854 		      * taking into account that RFC 3021 obsoletes it.
855 		      */
856 		    (ia->ia_subnetmask != IN_RFC3021_MASK &&
857 		    t == ia->ia_subnet)) &&
858 		     /*
859 		      * Check for an all one subnetmask. These
860 		      * only exist when an interface gets a secondary
861 		      * address.
862 		      */
863 		    ia->ia_subnetmask != (u_long)0xffffffff)
864 			    return (1);
865 	return (0);
866 #undef ia
867 }
868 
869 /*
870  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
871  */
872 void
873 in_ifdetach(struct ifnet *ifp)
874 {
875 
876 	in_pcbpurgeif0(&V_ripcbinfo, ifp);
877 	in_pcbpurgeif0(&V_udbinfo, ifp);
878 	in_pcbpurgeif0(&V_ulitecbinfo, ifp);
879 	in_purgemaddrs(ifp);
880 }
881 
882 /*
883  * Delete all IPv4 multicast address records, and associated link-layer
884  * multicast address records, associated with ifp.
885  * XXX It looks like domifdetach runs AFTER the link layer cleanup.
886  * XXX This should not race with ifma_protospec being set during
887  * a new allocation, if it does, we have bigger problems.
888  */
889 static void
890 in_purgemaddrs(struct ifnet *ifp)
891 {
892 	LIST_HEAD(,in_multi) purgeinms;
893 	struct in_multi		*inm, *tinm;
894 	struct ifmultiaddr	*ifma;
895 
896 	LIST_INIT(&purgeinms);
897 	IN_MULTI_LOCK();
898 
899 	/*
900 	 * Extract list of in_multi associated with the detaching ifp
901 	 * which the PF_INET layer is about to release.
902 	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
903 	 * by code further down.
904 	 */
905 	IF_ADDR_RLOCK(ifp);
906 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
907 		if (ifma->ifma_addr->sa_family != AF_INET ||
908 		    ifma->ifma_protospec == NULL)
909 			continue;
910 #if 0
911 		KASSERT(ifma->ifma_protospec != NULL,
912 		    ("%s: ifma_protospec is NULL", __func__));
913 #endif
914 		inm = (struct in_multi *)ifma->ifma_protospec;
915 		LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
916 	}
917 	IF_ADDR_RUNLOCK(ifp);
918 
919 	LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
920 		LIST_REMOVE(inm, inm_link);
921 		inm_release_locked(inm);
922 	}
923 	igmp_ifdetach(ifp);
924 
925 	IN_MULTI_UNLOCK();
926 }
927 
928 struct in_llentry {
929 	struct llentry		base;
930 	struct sockaddr_in	l3_addr4;
931 };
932 
933 /*
934  * Deletes an address from the address table.
935  * This function is called by the timer functions
936  * such as arptimer() and nd6_llinfo_timer(), and
937  * the caller does the locking.
938  */
939 static void
940 in_lltable_free(struct lltable *llt, struct llentry *lle)
941 {
942 	LLE_WUNLOCK(lle);
943 	LLE_LOCK_DESTROY(lle);
944 	free(lle, M_LLTABLE);
945 }
946 
947 static struct llentry *
948 in_lltable_new(const struct sockaddr *l3addr, u_int flags)
949 {
950 	struct in_llentry *lle;
951 
952 	lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
953 	if (lle == NULL)		/* NB: caller generates msg */
954 		return NULL;
955 
956 	/*
957 	 * For IPv4 this will trigger "arpresolve" to generate
958 	 * an ARP request.
959 	 */
960 	lle->base.la_expire = time_uptime; /* mark expired */
961 	lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
962 	lle->base.lle_refcnt = 1;
963 	lle->base.lle_free = in_lltable_free;
964 	LLE_LOCK_INIT(&lle->base);
965 	callout_init_rw(&lle->base.la_timer, &lle->base.lle_lock,
966 	    CALLOUT_RETURNUNLOCKED);
967 
968 	return (&lle->base);
969 }
970 
971 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m)	(			\
972 	    (((ntohl((d)->sin_addr.s_addr) ^ (a)->sin_addr.s_addr) & (m)->sin_addr.s_addr)) == 0 )
973 
974 static void
975 in_lltable_prefix_free(struct lltable *llt, const struct sockaddr *prefix,
976     const struct sockaddr *mask, u_int flags)
977 {
978 	const struct sockaddr_in *pfx = (const struct sockaddr_in *)prefix;
979 	const struct sockaddr_in *msk = (const struct sockaddr_in *)mask;
980 	struct llentry *lle, *next;
981 	int i;
982 	size_t pkts_dropped;
983 
984 	IF_AFDATA_WLOCK(llt->llt_ifp);
985 	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
986 		LIST_FOREACH_SAFE(lle, &llt->lle_head[i], lle_next, next) {
987 			/*
988 			 * (flags & LLE_STATIC) means deleting all entries
989 			 * including static ARP entries.
990 			 */
991 			if (IN_ARE_MASKED_ADDR_EQUAL(satosin(L3_ADDR(lle)),
992 			    pfx, msk) && ((flags & LLE_STATIC) ||
993 			    !(lle->la_flags & LLE_STATIC))) {
994 				LLE_WLOCK(lle);
995 				if (callout_stop(&lle->la_timer))
996 					LLE_REMREF(lle);
997 				pkts_dropped = llentry_free(lle);
998 				ARPSTAT_ADD(dropped, pkts_dropped);
999 			}
1000 		}
1001 	}
1002 	IF_AFDATA_WUNLOCK(llt->llt_ifp);
1003 }
1004 
1005 
1006 static int
1007 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1008 {
1009 	struct rtentry *rt;
1010 
1011 	KASSERT(l3addr->sa_family == AF_INET,
1012 	    ("sin_family %d", l3addr->sa_family));
1013 
1014 	/* XXX rtalloc1_fib should take a const param */
1015 	rt = rtalloc1_fib(__DECONST(struct sockaddr *, l3addr), 0, 0,
1016 	    ifp->if_fib);
1017 
1018 	if (rt == NULL)
1019 		return (EINVAL);
1020 
1021 	/*
1022 	 * If the gateway for an existing host route matches the target L3
1023 	 * address, which is a special route inserted by some implementation
1024 	 * such as MANET, and the interface is of the correct type, then
1025 	 * allow for ARP to proceed.
1026 	 */
1027 	if (rt->rt_flags & RTF_GATEWAY) {
1028 		if (!(rt->rt_flags & RTF_HOST) || !rt->rt_ifp ||
1029 		    rt->rt_ifp->if_type != IFT_ETHER ||
1030 		    (rt->rt_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1031 		    memcmp(rt->rt_gateway->sa_data, l3addr->sa_data,
1032 		    sizeof(in_addr_t)) != 0) {
1033 			RTFREE_LOCKED(rt);
1034 			return (EINVAL);
1035 		}
1036 	}
1037 
1038 	/*
1039 	 * Make sure that at least the destination address is covered
1040 	 * by the route. This is for handling the case where 2 or more
1041 	 * interfaces have the same prefix. An incoming packet arrives
1042 	 * on one interface and the corresponding outgoing packet leaves
1043 	 * another interface.
1044 	 */
1045 	if (!(rt->rt_flags & RTF_HOST) && rt->rt_ifp != ifp) {
1046 		const char *sa, *mask, *addr, *lim;
1047 		int len;
1048 
1049 		mask = (const char *)rt_mask(rt);
1050 		/*
1051 		 * Just being extra cautious to avoid some custom
1052 		 * code getting into trouble.
1053 		 */
1054 		if (mask == NULL) {
1055 			RTFREE_LOCKED(rt);
1056 			return (EINVAL);
1057 		}
1058 
1059 		sa = (const char *)rt_key(rt);
1060 		addr = (const char *)l3addr;
1061 		len = ((const struct sockaddr_in *)l3addr)->sin_len;
1062 		lim = addr + len;
1063 
1064 		for ( ; addr < lim; sa++, mask++, addr++) {
1065 			if ((*sa ^ *addr) & *mask) {
1066 #ifdef DIAGNOSTIC
1067 				log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
1068 				    inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
1069 #endif
1070 				RTFREE_LOCKED(rt);
1071 				return (EINVAL);
1072 			}
1073 		}
1074 	}
1075 
1076 	RTFREE_LOCKED(rt);
1077 	return (0);
1078 }
1079 
1080 /*
1081  * Return NULL if not found or marked for deletion.
1082  * If found return lle read locked.
1083  */
1084 static struct llentry *
1085 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1086 {
1087 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1088 	struct ifnet *ifp = llt->llt_ifp;
1089 	struct llentry *lle;
1090 	struct llentries *lleh;
1091 	u_int hashkey;
1092 
1093 	IF_AFDATA_LOCK_ASSERT(ifp);
1094 	KASSERT(l3addr->sa_family == AF_INET,
1095 	    ("sin_family %d", l3addr->sa_family));
1096 
1097 	hashkey = sin->sin_addr.s_addr;
1098 	lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
1099 	LIST_FOREACH(lle, lleh, lle_next) {
1100 		struct sockaddr_in *sa2 = satosin(L3_ADDR(lle));
1101 		if (lle->la_flags & LLE_DELETED)
1102 			continue;
1103 		if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
1104 			break;
1105 	}
1106 	if (lle == NULL) {
1107 #ifdef DIAGNOSTIC
1108 		if (flags & LLE_DELETE)
1109 			log(LOG_INFO, "interface address is missing from cache = %p  in delete\n", lle);
1110 #endif
1111 		if (!(flags & LLE_CREATE))
1112 			return (NULL);
1113 		IF_AFDATA_WLOCK_ASSERT(ifp);
1114 		/*
1115 		 * A route that covers the given address must have
1116 		 * been installed 1st because we are doing a resolution,
1117 		 * verify this.
1118 		 */
1119 		if (!(flags & LLE_IFADDR) &&
1120 		    in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1121 			goto done;
1122 
1123 		lle = in_lltable_new(l3addr, flags);
1124 		if (lle == NULL) {
1125 			log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1126 			goto done;
1127 		}
1128 		lle->la_flags = flags & ~LLE_CREATE;
1129 		if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
1130 			bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
1131 			lle->la_flags |= (LLE_VALID | LLE_STATIC);
1132 		}
1133 
1134 		lle->lle_tbl  = llt;
1135 		lle->lle_head = lleh;
1136 		lle->la_flags |= LLE_LINKED;
1137 		LIST_INSERT_HEAD(lleh, lle, lle_next);
1138 	} else if (flags & LLE_DELETE) {
1139 		if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
1140 			LLE_WLOCK(lle);
1141 			lle->la_flags |= LLE_DELETED;
1142 			EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1143 #ifdef DIAGNOSTIC
1144 			log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1145 #endif
1146 			if ((lle->la_flags &
1147 			    (LLE_STATIC | LLE_IFADDR)) == LLE_STATIC)
1148 				llentry_free(lle);
1149 			else
1150 				LLE_WUNLOCK(lle);
1151 		}
1152 		lle = (void *)-1;
1153 
1154 	}
1155 	if (LLE_IS_VALID(lle)) {
1156 		if (flags & LLE_EXCLUSIVE)
1157 			LLE_WLOCK(lle);
1158 		else
1159 			LLE_RLOCK(lle);
1160 	}
1161 done:
1162 	return (lle);
1163 }
1164 
1165 static int
1166 in_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
1167 {
1168 #define	SIN(lle)	((struct sockaddr_in *) L3_ADDR(lle))
1169 	struct ifnet *ifp = llt->llt_ifp;
1170 	struct llentry *lle;
1171 	/* XXX stack use */
1172 	struct {
1173 		struct rt_msghdr	rtm;
1174 		struct sockaddr_in	sin;
1175 		struct sockaddr_dl	sdl;
1176 	} arpc;
1177 	int error, i;
1178 
1179 	LLTABLE_LOCK_ASSERT();
1180 
1181 	error = 0;
1182 	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
1183 		LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
1184 			struct sockaddr_dl *sdl;
1185 
1186 			/* skip deleted entries */
1187 			if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1188 				continue;
1189 			/* Skip if jailed and not a valid IP of the prison. */
1190 			if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
1191 				continue;
1192 			/*
1193 			 * produce a msg made of:
1194 			 *  struct rt_msghdr;
1195 			 *  struct sockaddr_in; (IPv4)
1196 			 *  struct sockaddr_dl;
1197 			 */
1198 			bzero(&arpc, sizeof(arpc));
1199 			arpc.rtm.rtm_msglen = sizeof(arpc);
1200 			arpc.rtm.rtm_version = RTM_VERSION;
1201 			arpc.rtm.rtm_type = RTM_GET;
1202 			arpc.rtm.rtm_flags = RTF_UP;
1203 			arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1204 			arpc.sin.sin_family = AF_INET;
1205 			arpc.sin.sin_len = sizeof(arpc.sin);
1206 			arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
1207 
1208 			/* publish */
1209 			if (lle->la_flags & LLE_PUB)
1210 				arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1211 
1212 			sdl = &arpc.sdl;
1213 			sdl->sdl_family = AF_LINK;
1214 			sdl->sdl_len = sizeof(*sdl);
1215 			sdl->sdl_index = ifp->if_index;
1216 			sdl->sdl_type = ifp->if_type;
1217 			if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1218 				sdl->sdl_alen = ifp->if_addrlen;
1219 				bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1220 			} else {
1221 				sdl->sdl_alen = 0;
1222 				bzero(LLADDR(sdl), ifp->if_addrlen);
1223 			}
1224 
1225 			arpc.rtm.rtm_rmx.rmx_expire =
1226 			    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1227 			arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1228 			if (lle->la_flags & LLE_STATIC)
1229 				arpc.rtm.rtm_flags |= RTF_STATIC;
1230 			arpc.rtm.rtm_index = ifp->if_index;
1231 			error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1232 			if (error)
1233 				break;
1234 		}
1235 	}
1236 	return error;
1237 #undef SIN
1238 }
1239 
1240 void *
1241 in_domifattach(struct ifnet *ifp)
1242 {
1243 	struct in_ifinfo *ii;
1244 	struct lltable *llt;
1245 
1246 	ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1247 
1248 	llt = lltable_init(ifp, AF_INET);
1249 	if (llt != NULL) {
1250 		llt->llt_prefix_free = in_lltable_prefix_free;
1251 		llt->llt_lookup = in_lltable_lookup;
1252 		llt->llt_dump = in_lltable_dump;
1253 	}
1254 	ii->ii_llt = llt;
1255 
1256 	ii->ii_igmp = igmp_domifattach(ifp);
1257 
1258 	return ii;
1259 }
1260 
1261 void
1262 in_domifdetach(struct ifnet *ifp, void *aux)
1263 {
1264 	struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1265 
1266 	igmp_domifdetach(ifp);
1267 	lltable_free(ii->ii_llt);
1268 	free(ii, M_IFADDR);
1269 }
1270