xref: /freebsd/sys/netinet/in.c (revision 82431678fce5c893ef9c7418ad6d998ad4187de6)
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_carp.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/sockio.h>
41 #include <sys/malloc.h>
42 #include <sys/priv.h>
43 #include <sys/socket.h>
44 #include <sys/jail.h>
45 #include <sys/kernel.h>
46 #include <sys/proc.h>
47 #include <sys/sysctl.h>
48 #include <sys/vimage.h>
49 
50 #include <net/if.h>
51 #include <net/if_llatbl.h>
52 #include <net/if_types.h>
53 #include <net/route.h>
54 
55 #include <netinet/in.h>
56 #include <netinet/in_var.h>
57 #include <netinet/in_pcb.h>
58 #include <netinet/ip_var.h>
59 #include <netinet/vinet.h>
60 #include <netinet/igmp_var.h>
61 
62 static int in_mask2len(struct in_addr *);
63 static void in_len2mask(struct in_addr *, int);
64 static int in_lifaddr_ioctl(struct socket *, u_long, caddr_t,
65 	struct ifnet *, struct thread *);
66 
67 static int	in_addprefix(struct in_ifaddr *, int);
68 static int	in_scrubprefix(struct in_ifaddr *);
69 static void	in_socktrim(struct sockaddr_in *);
70 static int	in_ifinit(struct ifnet *,
71 	    struct in_ifaddr *, struct sockaddr_in *, int);
72 static void	in_purgemaddrs(struct ifnet *);
73 
74 #ifdef VIMAGE_GLOBALS
75 static int subnetsarelocal;
76 static int sameprefixcarponly;
77 extern struct inpcbinfo ripcbinfo;
78 #endif
79 
80 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, subnets_are_local,
81 	CTLFLAG_RW, subnetsarelocal, 0,
82 	"Treat all subnets as directly connected");
83 SYSCTL_V_INT(V_NET, vnet_inet, _net_inet_ip, OID_AUTO, same_prefix_carp_only,
84 	CTLFLAG_RW, sameprefixcarponly, 0,
85 	"Refuse to create same prefixes on different interfaces");
86 
87 /*
88  * Return 1 if an internet address is for a ``local'' host
89  * (one to which we have a connection).  If subnetsarelocal
90  * is true, this includes other subnets of the local net.
91  * Otherwise, it includes only the directly-connected (sub)nets.
92  */
93 int
94 in_localaddr(struct in_addr in)
95 {
96 	INIT_VNET_INET(curvnet);
97 	register u_long i = ntohl(in.s_addr);
98 	register struct in_ifaddr *ia;
99 
100 	if (V_subnetsarelocal) {
101 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link)
102 			if ((i & ia->ia_netmask) == ia->ia_net)
103 				return (1);
104 	} else {
105 		TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link)
106 			if ((i & ia->ia_subnetmask) == ia->ia_subnet)
107 				return (1);
108 	}
109 	return (0);
110 }
111 
112 /*
113  * Return 1 if an internet address is for the local host and configured
114  * on one of its interfaces.
115  */
116 int
117 in_localip(struct in_addr in)
118 {
119 	INIT_VNET_INET(curvnet);
120 	struct in_ifaddr *ia;
121 
122 	LIST_FOREACH(ia, INADDR_HASH(in.s_addr), ia_hash) {
123 		if (IA_SIN(ia)->sin_addr.s_addr == in.s_addr)
124 			return (1);
125 	}
126 	return (0);
127 }
128 
129 /*
130  * Determine whether an IP address is in a reserved set of addresses
131  * that may not be forwarded, or whether datagrams to that destination
132  * may be forwarded.
133  */
134 int
135 in_canforward(struct in_addr in)
136 {
137 	register u_long i = ntohl(in.s_addr);
138 	register u_long net;
139 
140 	if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i) || IN_LINKLOCAL(i))
141 		return (0);
142 	if (IN_CLASSA(i)) {
143 		net = i & IN_CLASSA_NET;
144 		if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
145 			return (0);
146 	}
147 	return (1);
148 }
149 
150 /*
151  * Trim a mask in a sockaddr
152  */
153 static void
154 in_socktrim(struct sockaddr_in *ap)
155 {
156     register char *cplim = (char *) &ap->sin_addr;
157     register char *cp = (char *) (&ap->sin_addr + 1);
158 
159     ap->sin_len = 0;
160     while (--cp >= cplim)
161 	if (*cp) {
162 	    (ap)->sin_len = cp - (char *) (ap) + 1;
163 	    break;
164 	}
165 }
166 
167 static int
168 in_mask2len(mask)
169 	struct in_addr *mask;
170 {
171 	int x, y;
172 	u_char *p;
173 
174 	p = (u_char *)mask;
175 	for (x = 0; x < sizeof(*mask); x++) {
176 		if (p[x] != 0xff)
177 			break;
178 	}
179 	y = 0;
180 	if (x < sizeof(*mask)) {
181 		for (y = 0; y < 8; y++) {
182 			if ((p[x] & (0x80 >> y)) == 0)
183 				break;
184 		}
185 	}
186 	return (x * 8 + y);
187 }
188 
189 static void
190 in_len2mask(struct in_addr *mask, int len)
191 {
192 	int i;
193 	u_char *p;
194 
195 	p = (u_char *)mask;
196 	bzero(mask, sizeof(*mask));
197 	for (i = 0; i < len / 8; i++)
198 		p[i] = 0xff;
199 	if (len % 8)
200 		p[i] = (0xff00 >> (len % 8)) & 0xff;
201 }
202 
203 /*
204  * Generic internet control operations (ioctl's).
205  *
206  * ifp is NULL if not an interface-specific ioctl.
207  */
208 /* ARGSUSED */
209 int
210 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
211     struct thread *td)
212 {
213 	INIT_VNET_INET(curvnet); /* both so and ifp can be NULL here! */
214 	register struct ifreq *ifr = (struct ifreq *)data;
215 	register struct in_ifaddr *ia, *iap;
216 	register struct ifaddr *ifa;
217 	struct in_addr allhosts_addr;
218 	struct in_addr dst;
219 	struct in_ifaddr *oia;
220 	struct in_ifinfo *ii;
221 	struct in_aliasreq *ifra = (struct in_aliasreq *)data;
222 	struct sockaddr_in oldaddr;
223 	int error, hostIsNew, iaIsNew, maskIsNew, s;
224 	int iaIsFirst;
225 
226 	ia = NULL;
227 	iaIsFirst = 0;
228 	iaIsNew = 0;
229 	allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
230 
231 	/*
232 	 * Filter out ioctls we implement directly; forward the rest on to
233 	 * in_lifaddr_ioctl() and ifp->if_ioctl().
234 	 */
235 	switch (cmd) {
236 	case SIOCAIFADDR:
237 	case SIOCDIFADDR:
238 	case SIOCGIFADDR:
239 	case SIOCGIFBRDADDR:
240 	case SIOCGIFDSTADDR:
241 	case SIOCGIFNETMASK:
242 	case SIOCSIFADDR:
243 	case SIOCSIFBRDADDR:
244 	case SIOCSIFDSTADDR:
245 	case SIOCSIFNETMASK:
246 		break;
247 
248 	case SIOCALIFADDR:
249 		if (td != NULL) {
250 			error = priv_check(td, PRIV_NET_ADDIFADDR);
251 			if (error)
252 				return (error);
253 		}
254 		if (ifp == NULL)
255 			return (EINVAL);
256 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
257 
258 	case SIOCDLIFADDR:
259 		if (td != NULL) {
260 			error = priv_check(td, PRIV_NET_DELIFADDR);
261 			if (error)
262 				return (error);
263 		}
264 		if (ifp == NULL)
265 			return (EINVAL);
266 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
267 
268 	case SIOCGLIFADDR:
269 		if (ifp == NULL)
270 			return (EINVAL);
271 		return in_lifaddr_ioctl(so, cmd, data, ifp, td);
272 
273 	default:
274 		if (ifp == NULL || ifp->if_ioctl == NULL)
275 			return (EOPNOTSUPP);
276 		return ((*ifp->if_ioctl)(ifp, cmd, data));
277 	}
278 
279 	if (ifp == NULL)
280 		return (EADDRNOTAVAIL);
281 
282 	/*
283 	 * Security checks before we get involved in any work.
284 	 */
285 	switch (cmd) {
286 	case SIOCAIFADDR:
287 	case SIOCSIFADDR:
288 	case SIOCSIFBRDADDR:
289 	case SIOCSIFNETMASK:
290 	case SIOCSIFDSTADDR:
291 		if (td != NULL) {
292 			error = priv_check(td, PRIV_NET_ADDIFADDR);
293 			if (error)
294 				return (error);
295 		}
296 		break;
297 
298 	case SIOCDIFADDR:
299 		if (td != NULL) {
300 			error = priv_check(td, PRIV_NET_DELIFADDR);
301 			if (error)
302 				return (error);
303 		}
304 		break;
305 	}
306 
307 	/*
308 	 * Find address for this interface, if it exists.
309 	 *
310 	 * If an alias address was specified, find that one instead of the
311 	 * first one on the interface, if possible.
312 	 */
313 	dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
314 	LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash) {
315 		if (iap->ia_ifp == ifp &&
316 		    iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
317 			if (td == NULL || prison_check_ip4(td->td_ucred,
318 			    &dst) == 0)
319 				ia = iap;
320 			break;
321 		}
322 	}
323 	IF_ADDR_LOCK(ifp);
324 	if (ia == NULL) {
325 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
326 			iap = ifatoia(ifa);
327 			if (iap->ia_addr.sin_family == AF_INET) {
328 				if (td != NULL &&
329 				    prison_check_ip4(td->td_ucred,
330 				    &iap->ia_addr.sin_addr) != 0)
331 					continue;
332 				ia = iap;
333 				break;
334 			}
335 		}
336 	}
337 	if (ia == NULL)
338 		iaIsFirst = 1;
339 
340 	error = 0;
341 	switch (cmd) {
342 	case SIOCAIFADDR:
343 	case SIOCDIFADDR:
344 		if (ifra->ifra_addr.sin_family == AF_INET) {
345 			for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
346 				if (ia->ia_ifp == ifp  &&
347 				    ia->ia_addr.sin_addr.s_addr ==
348 				    ifra->ifra_addr.sin_addr.s_addr)
349 					break;
350 			}
351 			if ((ifp->if_flags & IFF_POINTOPOINT)
352 			    && (cmd == SIOCAIFADDR)
353 			    && (ifra->ifra_dstaddr.sin_addr.s_addr
354 				== INADDR_ANY)) {
355 				error = EDESTADDRREQ;
356 				goto out_unlock;
357 			}
358 		}
359 		if (cmd == SIOCDIFADDR && ia == NULL) {
360 			error = EADDRNOTAVAIL;
361 			goto out_unlock;
362 		}
363 		/* FALLTHROUGH */
364 	case SIOCSIFADDR:
365 	case SIOCSIFNETMASK:
366 	case SIOCSIFDSTADDR:
367 		if (ia == NULL) {
368 			ia = (struct in_ifaddr *)
369 				malloc(sizeof *ia, M_IFADDR, M_NOWAIT |
370 				    M_ZERO);
371 			if (ia == NULL) {
372 				error = ENOBUFS;
373 				goto out_unlock;
374 			}
375 
376 			ifa = &ia->ia_ifa;
377 			IFA_LOCK_INIT(ifa);
378 			ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
379 			ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
380 			ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
381 			ifa->ifa_refcnt = 1;
382 
383 			ia->ia_sockmask.sin_len = 8;
384 			ia->ia_sockmask.sin_family = AF_INET;
385 			if (ifp->if_flags & IFF_BROADCAST) {
386 				ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
387 				ia->ia_broadaddr.sin_family = AF_INET;
388 			}
389 			ia->ia_ifp = ifp;
390 
391 			TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
392 			s = splnet();
393 			TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
394 			splx(s);
395 			iaIsNew = 1;
396 		}
397 		break;
398 
399 	case SIOCSIFBRDADDR:
400 	case SIOCGIFADDR:
401 	case SIOCGIFNETMASK:
402 	case SIOCGIFDSTADDR:
403 	case SIOCGIFBRDADDR:
404 		if (ia == NULL) {
405 			error = EADDRNOTAVAIL;
406 			goto out_unlock;
407 		}
408 		break;
409 	}
410 
411 	/*
412 	 * Most paths in this switch return directly or via out_unlock.  Only
413 	 * paths that remove the address break in order to hit common removal
414 	 * code.
415 	 *
416 	 * XXXRW: We enter the switch with IF_ADDR_LOCK() held, but leave
417 	 * without it.  This is a bug.
418 	 */
419 	IF_ADDR_LOCK_ASSERT(ifp);
420 	switch (cmd) {
421 	case SIOCGIFADDR:
422 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
423 		goto out_unlock;
424 
425 	case SIOCGIFBRDADDR:
426 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
427 			error = EINVAL;
428 			goto out_unlock;
429 		}
430 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
431 		goto out_unlock;
432 
433 	case SIOCGIFDSTADDR:
434 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
435 			error = EINVAL;
436 			goto out_unlock;
437 		}
438 		*((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
439 		goto out_unlock;
440 
441 	case SIOCGIFNETMASK:
442 		*((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
443 		goto out_unlock;
444 
445 	case SIOCSIFDSTADDR:
446 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0) {
447 			error = EINVAL;
448 			goto out_unlock;
449 		}
450 		oldaddr = ia->ia_dstaddr;
451 		ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
452 		IF_ADDR_UNLOCK(ifp);
453 
454 		/*
455 		 * XXXRW: Locks dropped for if_ioctl and rtinit, but ia is
456 		 * still being used.
457 		 */
458 		if (ifp->if_ioctl != NULL) {
459 			error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR,
460 			    (caddr_t)ia);
461 			if (error) {
462 				ia->ia_dstaddr = oldaddr;
463 				return (error);
464 			}
465 		}
466 		if (ia->ia_flags & IFA_ROUTE) {
467 			ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
468 			rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
469 			ia->ia_ifa.ifa_dstaddr =
470 					(struct sockaddr *)&ia->ia_dstaddr;
471 			rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
472 		}
473 		return (0);
474 
475 	case SIOCSIFBRDADDR:
476 		if ((ifp->if_flags & IFF_BROADCAST) == 0) {
477 			error = EINVAL;
478 			goto out_unlock;
479 		}
480 		ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
481 		goto out_unlock;
482 
483 	case SIOCSIFADDR:
484 		IF_ADDR_UNLOCK(ifp);
485 
486 		/*
487 		 * XXXRW: Locks dropped for in_ifinit and in_joingroup, but ia
488 		 * is still being used.
489 		 */
490 		error = in_ifinit(ifp, ia,
491 		    (struct sockaddr_in *) &ifr->ifr_addr, 1);
492 		if (error != 0 && iaIsNew)
493 			break;
494 		if (error == 0) {
495 			ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
496 			if (iaIsFirst &&
497 			    (ifp->if_flags & IFF_MULTICAST) != 0) {
498 				error = in_joingroup(ifp, &allhosts_addr,
499 				    NULL, &ii->ii_allhosts);
500 			}
501 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
502 		}
503 		return (0);
504 
505 	case SIOCSIFNETMASK:
506 		ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
507 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
508 		goto out_unlock;
509 
510 	case SIOCAIFADDR:
511 		maskIsNew = 0;
512 		hostIsNew = 1;
513 		error = 0;
514 		if (ia->ia_addr.sin_family == AF_INET) {
515 			if (ifra->ifra_addr.sin_len == 0) {
516 				ifra->ifra_addr = ia->ia_addr;
517 				hostIsNew = 0;
518 			} else if (ifra->ifra_addr.sin_addr.s_addr ==
519 					       ia->ia_addr.sin_addr.s_addr)
520 				hostIsNew = 0;
521 		}
522 		IF_ADDR_UNLOCK(ifp);
523 
524 		/*
525 		 * XXXRW: Locks dropped for in_ifscrub and in_ifinit, but ia
526 		 * is still being used.
527 		 */
528 		if (ifra->ifra_mask.sin_len) {
529 			in_ifscrub(ifp, ia);
530 			ia->ia_sockmask = ifra->ifra_mask;
531 			ia->ia_sockmask.sin_family = AF_INET;
532 			ia->ia_subnetmask =
533 			     ntohl(ia->ia_sockmask.sin_addr.s_addr);
534 			maskIsNew = 1;
535 		}
536 		if ((ifp->if_flags & IFF_POINTOPOINT) &&
537 		    (ifra->ifra_dstaddr.sin_family == AF_INET)) {
538 			in_ifscrub(ifp, ia);
539 			ia->ia_dstaddr = ifra->ifra_dstaddr;
540 			maskIsNew  = 1; /* We lie; but the effect's the same */
541 		}
542 		if (ifra->ifra_addr.sin_family == AF_INET &&
543 		    (hostIsNew || maskIsNew))
544 			error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
545 		if (error != 0 && iaIsNew)
546 			break;
547 
548 		if ((ifp->if_flags & IFF_BROADCAST) &&
549 		    (ifra->ifra_broadaddr.sin_family == AF_INET))
550 			ia->ia_broadaddr = ifra->ifra_broadaddr;
551 		if (error == 0) {
552 			ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
553 			if (iaIsFirst &&
554 			    (ifp->if_flags & IFF_MULTICAST) != 0) {
555 				error = in_joingroup(ifp, &allhosts_addr,
556 				    NULL, &ii->ii_allhosts);
557 			}
558 			EVENTHANDLER_INVOKE(ifaddr_event, ifp);
559 		}
560 		return (error);
561 
562 	case SIOCDIFADDR:
563 		IF_ADDR_UNLOCK(ifp);
564 
565 		/*
566 		 * XXXRW: Locks dropped for in_ifscrub and in_ifadown, but ia
567 		 * is still being used.
568 		 *
569 		 * in_ifscrub kills the interface route.
570 		 */
571 		in_ifscrub(ifp, ia);
572 
573 		/*
574 		 * in_ifadown gets rid of all the rest of
575 		 * the routes.  This is not quite the right
576 		 * thing to do, but at least if we are running
577 		 * a routing process they will come back.
578 		 */
579 		in_ifadown(&ia->ia_ifa, 1);
580 		EVENTHANDLER_INVOKE(ifaddr_event, ifp);
581 		error = 0;
582 		break;
583 
584 	default:
585 		panic("in_control: unsupported ioctl");
586 	}
587 
588 	/*
589 	 * XXXRW: In a more ideal world, we would still be holding
590 	 * IF_ADDR_LOCK here.
591 	 */
592 	IF_ADDR_LOCK(ifp);
593 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
594 	IF_ADDR_UNLOCK(ifp);
595 	s = splnet();
596 	TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
597 	if (ia->ia_addr.sin_family == AF_INET) {
598 		LIST_REMOVE(ia, ia_hash);
599 		/*
600 		 * If this is the last IPv4 address configured on this
601 		 * interface, leave the all-hosts group.
602 		 * No state-change report need be transmitted.
603 		 */
604 		oia = NULL;
605 		IFP_TO_IA(ifp, oia);
606 		if (oia == NULL) {
607 			ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
608 			IN_MULTI_LOCK();
609 			if (ii->ii_allhosts) {
610 				(void)in_leavegroup_locked(ii->ii_allhosts,
611 				    NULL);
612 				ii->ii_allhosts = NULL;
613 			}
614 			IN_MULTI_UNLOCK();
615 		}
616 	}
617 	IFAFREE(&ia->ia_ifa);
618 	splx(s);
619 
620 	return (error);
621 
622 out_unlock:
623 	IF_ADDR_UNLOCK(ifp);
624 	return (error);
625 }
626 
627 /*
628  * SIOC[GAD]LIFADDR.
629  *	SIOCGLIFADDR: get first address. (?!?)
630  *	SIOCGLIFADDR with IFLR_PREFIX:
631  *		get first address that matches the specified prefix.
632  *	SIOCALIFADDR: add the specified address.
633  *	SIOCALIFADDR with IFLR_PREFIX:
634  *		EINVAL since we can't deduce hostid part of the address.
635  *	SIOCDLIFADDR: delete the specified address.
636  *	SIOCDLIFADDR with IFLR_PREFIX:
637  *		delete the first address that matches the specified prefix.
638  * return values:
639  *	EINVAL on invalid parameters
640  *	EADDRNOTAVAIL on prefix match failed/specified address not found
641  *	other values may be returned from in_ioctl()
642  */
643 static int
644 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data,
645     struct ifnet *ifp, struct thread *td)
646 {
647 	struct if_laddrreq *iflr = (struct if_laddrreq *)data;
648 	struct ifaddr *ifa;
649 
650 	/* sanity checks */
651 	if (data == NULL || ifp == NULL) {
652 		panic("invalid argument to in_lifaddr_ioctl");
653 		/*NOTRECHED*/
654 	}
655 
656 	switch (cmd) {
657 	case SIOCGLIFADDR:
658 		/* address must be specified on GET with IFLR_PREFIX */
659 		if ((iflr->flags & IFLR_PREFIX) == 0)
660 			break;
661 		/*FALLTHROUGH*/
662 	case SIOCALIFADDR:
663 	case SIOCDLIFADDR:
664 		/* address must be specified on ADD and DELETE */
665 		if (iflr->addr.ss_family != AF_INET)
666 			return (EINVAL);
667 		if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
668 			return (EINVAL);
669 		/* XXX need improvement */
670 		if (iflr->dstaddr.ss_family
671 		 && iflr->dstaddr.ss_family != AF_INET)
672 			return (EINVAL);
673 		if (iflr->dstaddr.ss_family
674 		 && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
675 			return (EINVAL);
676 		break;
677 	default: /*shouldn't happen*/
678 		return (EOPNOTSUPP);
679 	}
680 	if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
681 		return (EINVAL);
682 
683 	switch (cmd) {
684 	case SIOCALIFADDR:
685 	    {
686 		struct in_aliasreq ifra;
687 
688 		if (iflr->flags & IFLR_PREFIX)
689 			return (EINVAL);
690 
691 		/* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
692 		bzero(&ifra, sizeof(ifra));
693 		bcopy(iflr->iflr_name, ifra.ifra_name,
694 			sizeof(ifra.ifra_name));
695 
696 		bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
697 
698 		if (iflr->dstaddr.ss_family) {	/*XXX*/
699 			bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
700 				iflr->dstaddr.ss_len);
701 		}
702 
703 		ifra.ifra_mask.sin_family = AF_INET;
704 		ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
705 		in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
706 
707 		return (in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td));
708 	    }
709 	case SIOCGLIFADDR:
710 	case SIOCDLIFADDR:
711 	    {
712 		struct in_ifaddr *ia;
713 		struct in_addr mask, candidate, match;
714 		struct sockaddr_in *sin;
715 
716 		bzero(&mask, sizeof(mask));
717 		bzero(&match, sizeof(match));
718 		if (iflr->flags & IFLR_PREFIX) {
719 			/* lookup a prefix rather than address. */
720 			in_len2mask(&mask, iflr->prefixlen);
721 
722 			sin = (struct sockaddr_in *)&iflr->addr;
723 			match.s_addr = sin->sin_addr.s_addr;
724 			match.s_addr &= mask.s_addr;
725 
726 			/* if you set extra bits, that's wrong */
727 			if (match.s_addr != sin->sin_addr.s_addr)
728 				return (EINVAL);
729 
730 		} else {
731 			/* on getting an address, take the 1st match */
732 			/* on deleting an address, do exact match */
733 			if (cmd != SIOCGLIFADDR) {
734 				in_len2mask(&mask, 32);
735 				sin = (struct sockaddr_in *)&iflr->addr;
736 				match.s_addr = sin->sin_addr.s_addr;
737 			}
738 		}
739 
740 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)	{
741 			if (ifa->ifa_addr->sa_family != AF_INET6)
742 				continue;
743 			if (match.s_addr == 0)
744 				break;
745 			candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
746 			candidate.s_addr &= mask.s_addr;
747 			if (candidate.s_addr == match.s_addr)
748 				break;
749 		}
750 		if (ifa == NULL)
751 			return (EADDRNOTAVAIL);
752 		ia = (struct in_ifaddr *)ifa;
753 
754 		if (cmd == SIOCGLIFADDR) {
755 			/* fill in the if_laddrreq structure */
756 			bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
757 
758 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
759 				bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
760 					ia->ia_dstaddr.sin_len);
761 			} else
762 				bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
763 
764 			iflr->prefixlen =
765 				in_mask2len(&ia->ia_sockmask.sin_addr);
766 
767 			iflr->flags = 0;	/*XXX*/
768 
769 			return (0);
770 		} else {
771 			struct in_aliasreq ifra;
772 
773 			/* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
774 			bzero(&ifra, sizeof(ifra));
775 			bcopy(iflr->iflr_name, ifra.ifra_name,
776 				sizeof(ifra.ifra_name));
777 
778 			bcopy(&ia->ia_addr, &ifra.ifra_addr,
779 				ia->ia_addr.sin_len);
780 			if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
781 				bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
782 					ia->ia_dstaddr.sin_len);
783 			}
784 			bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
785 				ia->ia_sockmask.sin_len);
786 
787 			return (in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
788 			    ifp, td));
789 		}
790 	    }
791 	}
792 
793 	return (EOPNOTSUPP);	/*just for safety*/
794 }
795 
796 /*
797  * Delete any existing route for an interface.
798  */
799 void
800 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
801 {
802 
803 	in_scrubprefix(ia);
804 }
805 
806 /*
807  * Initialize an interface's internet address
808  * and routing table entry.
809  */
810 static int
811 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin,
812     int scrub)
813 {
814 	INIT_VNET_INET(ifp->if_vnet);
815 	register u_long i = ntohl(sin->sin_addr.s_addr);
816 	struct sockaddr_in oldaddr;
817 	int s = splimp(), flags = RTF_UP, error = 0;
818 
819 	oldaddr = ia->ia_addr;
820 	if (oldaddr.sin_family == AF_INET)
821 		LIST_REMOVE(ia, ia_hash);
822 	ia->ia_addr = *sin;
823 	if (ia->ia_addr.sin_family == AF_INET)
824 		LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
825 		    ia, ia_hash);
826 	/*
827 	 * Give the interface a chance to initialize
828 	 * if this is its first address,
829 	 * and to validate the address if necessary.
830 	 */
831 	if (ifp->if_ioctl != NULL) {
832 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
833 		if (error) {
834 			splx(s);
835 			/* LIST_REMOVE(ia, ia_hash) is done in in_control */
836 			ia->ia_addr = oldaddr;
837 			if (ia->ia_addr.sin_family == AF_INET)
838 				LIST_INSERT_HEAD(INADDR_HASH(
839 				    ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
840 			else
841 				/*
842 				 * If oldaddr family is not AF_INET (e.g.
843 				 * interface has been just created) in_control
844 				 * does not call LIST_REMOVE, and we end up
845 				 * with bogus ia entries in hash
846 				 */
847 				LIST_REMOVE(ia, ia_hash);
848 			return (error);
849 		}
850 	}
851 	splx(s);
852 	if (scrub) {
853 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
854 		in_ifscrub(ifp, ia);
855 		ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
856 	}
857 	if (IN_CLASSA(i))
858 		ia->ia_netmask = IN_CLASSA_NET;
859 	else if (IN_CLASSB(i))
860 		ia->ia_netmask = IN_CLASSB_NET;
861 	else
862 		ia->ia_netmask = IN_CLASSC_NET;
863 	/*
864 	 * The subnet mask usually includes at least the standard network part,
865 	 * but may may be smaller in the case of supernetting.
866 	 * If it is set, we believe it.
867 	 */
868 	if (ia->ia_subnetmask == 0) {
869 		ia->ia_subnetmask = ia->ia_netmask;
870 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
871 	} else
872 		ia->ia_netmask &= ia->ia_subnetmask;
873 	ia->ia_net = i & ia->ia_netmask;
874 	ia->ia_subnet = i & ia->ia_subnetmask;
875 	in_socktrim(&ia->ia_sockmask);
876 #ifdef DEV_CARP
877 	/*
878 	 * XXX: carp(4) does not have interface route
879 	 */
880 	if (ifp->if_type == IFT_CARP)
881 		return (0);
882 #endif
883 	/*
884 	 * Add route for the network.
885 	 */
886 	ia->ia_ifa.ifa_metric = ifp->if_metric;
887 	if (ifp->if_flags & IFF_BROADCAST) {
888 		ia->ia_broadaddr.sin_addr.s_addr =
889 			htonl(ia->ia_subnet | ~ia->ia_subnetmask);
890 		ia->ia_netbroadcast.s_addr =
891 			htonl(ia->ia_net | ~ ia->ia_netmask);
892 	} else if (ifp->if_flags & IFF_LOOPBACK) {
893 		ia->ia_dstaddr = ia->ia_addr;
894 		flags |= RTF_HOST;
895 	} else if (ifp->if_flags & IFF_POINTOPOINT) {
896 		if (ia->ia_dstaddr.sin_family != AF_INET)
897 			return (0);
898 		flags |= RTF_HOST;
899 	}
900 	if ((error = in_addprefix(ia, flags)) != 0)
901 		return (error);
902 
903 	return (error);
904 }
905 
906 #define rtinitflags(x) \
907 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
908 	    ? RTF_HOST : 0)
909 /*
910  * Check if we have a route for the given prefix already or add one accordingly.
911  */
912 static int
913 in_addprefix(struct in_ifaddr *target, int flags)
914 {
915 	INIT_VNET_INET(curvnet);
916 	struct in_ifaddr *ia;
917 	struct in_addr prefix, mask, p, m;
918 	int error;
919 
920 	if ((flags & RTF_HOST) != 0) {
921 		prefix = target->ia_dstaddr.sin_addr;
922 		mask.s_addr = 0;
923 	} else {
924 		prefix = target->ia_addr.sin_addr;
925 		mask = target->ia_sockmask.sin_addr;
926 		prefix.s_addr &= mask.s_addr;
927 	}
928 
929 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
930 		if (rtinitflags(ia)) {
931 			p = ia->ia_addr.sin_addr;
932 
933 			if (prefix.s_addr != p.s_addr)
934 				continue;
935 		} else {
936 			p = ia->ia_addr.sin_addr;
937 			m = ia->ia_sockmask.sin_addr;
938 			p.s_addr &= m.s_addr;
939 
940 			if (prefix.s_addr != p.s_addr ||
941 			    mask.s_addr != m.s_addr)
942 				continue;
943 		}
944 
945 		/*
946 		 * If we got a matching prefix route inserted by other
947 		 * interface address, we are done here.
948 		 */
949 		if (ia->ia_flags & IFA_ROUTE) {
950 			if (V_sameprefixcarponly &&
951 			    target->ia_ifp->if_type != IFT_CARP &&
952 			    ia->ia_ifp->if_type != IFT_CARP)
953 				return (EEXIST);
954 			else
955 				return (0);
956 		}
957 	}
958 
959 	/*
960 	 * No-one seem to have this prefix route, so we try to insert it.
961 	 */
962 	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
963 	if (!error)
964 		target->ia_flags |= IFA_ROUTE;
965 	return (error);
966 }
967 
968 extern void arp_ifscrub(struct ifnet *ifp, uint32_t addr);
969 
970 /*
971  * If there is no other address in the system that can serve a route to the
972  * same prefix, remove the route.  Hand over the route to the new address
973  * otherwise.
974  */
975 static int
976 in_scrubprefix(struct in_ifaddr *target)
977 {
978 	INIT_VNET_INET(curvnet);
979 	struct in_ifaddr *ia;
980 	struct in_addr prefix, mask, p;
981 	int error;
982 
983 	if ((target->ia_flags & IFA_ROUTE) == 0)
984 		return (0);
985 
986 	if (rtinitflags(target))
987 		prefix = target->ia_dstaddr.sin_addr;
988 	else {
989 		prefix = target->ia_addr.sin_addr;
990 		mask = target->ia_sockmask.sin_addr;
991 		prefix.s_addr &= mask.s_addr;
992 		/* remove arp cache */
993 		arp_ifscrub(target->ia_ifp, IA_SIN(target)->sin_addr.s_addr);
994 	}
995 
996 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
997 		if (rtinitflags(ia))
998 			p = ia->ia_dstaddr.sin_addr;
999 		else {
1000 			p = ia->ia_addr.sin_addr;
1001 			p.s_addr &= ia->ia_sockmask.sin_addr.s_addr;
1002 		}
1003 
1004 		if (prefix.s_addr != p.s_addr)
1005 			continue;
1006 
1007 		/*
1008 		 * If we got a matching prefix address, move IFA_ROUTE and
1009 		 * the route itself to it.  Make sure that routing daemons
1010 		 * get a heads-up.
1011 		 *
1012 		 * XXX: a special case for carp(4) interface
1013 		 */
1014 		if ((ia->ia_flags & IFA_ROUTE) == 0
1015 #ifdef DEV_CARP
1016 		    && (ia->ia_ifp->if_type != IFT_CARP)
1017 #endif
1018 							) {
1019 			rtinit(&(target->ia_ifa), (int)RTM_DELETE,
1020 			    rtinitflags(target));
1021 			target->ia_flags &= ~IFA_ROUTE;
1022 
1023 			error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
1024 			    rtinitflags(ia) | RTF_UP);
1025 			if (error == 0)
1026 				ia->ia_flags |= IFA_ROUTE;
1027 			return (error);
1028 		}
1029 	}
1030 
1031 	/*
1032 	 * As no-one seem to have this prefix, we can remove the route.
1033 	 */
1034 	rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
1035 	target->ia_flags &= ~IFA_ROUTE;
1036 	return (0);
1037 }
1038 
1039 #undef rtinitflags
1040 
1041 /*
1042  * Return 1 if the address might be a local broadcast address.
1043  */
1044 int
1045 in_broadcast(struct in_addr in, struct ifnet *ifp)
1046 {
1047 	register struct ifaddr *ifa;
1048 	u_long t;
1049 
1050 	if (in.s_addr == INADDR_BROADCAST ||
1051 	    in.s_addr == INADDR_ANY)
1052 		return (1);
1053 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
1054 		return (0);
1055 	t = ntohl(in.s_addr);
1056 	/*
1057 	 * Look through the list of addresses for a match
1058 	 * with a broadcast address.
1059 	 */
1060 #define ia ((struct in_ifaddr *)ifa)
1061 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1062 		if (ifa->ifa_addr->sa_family == AF_INET &&
1063 		    (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1064 		     in.s_addr == ia->ia_netbroadcast.s_addr ||
1065 		     /*
1066 		      * Check for old-style (host 0) broadcast.
1067 		      */
1068 		     t == ia->ia_subnet || t == ia->ia_net) &&
1069 		     /*
1070 		      * Check for an all one subnetmask. These
1071 		      * only exist when an interface gets a secondary
1072 		      * address.
1073 		      */
1074 		     ia->ia_subnetmask != (u_long)0xffffffff)
1075 			    return (1);
1076 	return (0);
1077 #undef ia
1078 }
1079 
1080 /*
1081  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
1082  */
1083 void
1084 in_ifdetach(struct ifnet *ifp)
1085 {
1086 	INIT_VNET_INET(ifp->if_vnet);
1087 
1088 	in_pcbpurgeif0(&V_ripcbinfo, ifp);
1089 	in_pcbpurgeif0(&V_udbinfo, ifp);
1090 	in_purgemaddrs(ifp);
1091 }
1092 
1093 /*
1094  * Delete all IPv4 multicast address records, and associated link-layer
1095  * multicast address records, associated with ifp.
1096  * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1097  * XXX This should not race with ifma_protospec being set during
1098  * a new allocation, if it does, we have bigger problems.
1099  */
1100 static void
1101 in_purgemaddrs(struct ifnet *ifp)
1102 {
1103 	LIST_HEAD(,in_multi) purgeinms;
1104 	struct in_multi		*inm, *tinm;
1105 	struct ifmultiaddr	*ifma;
1106 
1107 	LIST_INIT(&purgeinms);
1108 	IN_MULTI_LOCK();
1109 
1110 	/*
1111 	 * Extract list of in_multi associated with the detaching ifp
1112 	 * which the PF_INET layer is about to release.
1113 	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1114 	 * by code further down.
1115 	 */
1116 	IF_ADDR_LOCK(ifp);
1117 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1118 		if (ifma->ifma_addr->sa_family != AF_INET ||
1119 		    ifma->ifma_protospec == NULL)
1120 			continue;
1121 #if 0
1122 		KASSERT(ifma->ifma_protospec != NULL,
1123 		    ("%s: ifma_protospec is NULL", __func__));
1124 #endif
1125 		inm = (struct in_multi *)ifma->ifma_protospec;
1126 		LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
1127 	}
1128 	IF_ADDR_UNLOCK(ifp);
1129 
1130 	LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
1131 		LIST_REMOVE(inm, inm_link);
1132 		inm_release_locked(inm);
1133 	}
1134 	igmp_ifdetach(ifp);
1135 
1136 	IN_MULTI_UNLOCK();
1137 }
1138 
1139 #include <sys/syslog.h>
1140 #include <net/if_dl.h>
1141 #include <netinet/if_ether.h>
1142 
1143 struct in_llentry {
1144 	struct llentry		base;
1145 	struct sockaddr_in	l3_addr4;
1146 };
1147 
1148 static struct llentry *
1149 in_lltable_new(const struct sockaddr *l3addr, u_int flags)
1150 {
1151 	struct in_llentry *lle;
1152 
1153 	lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_DONTWAIT | M_ZERO);
1154 	if (lle == NULL)		/* NB: caller generates msg */
1155 		return NULL;
1156 
1157 	callout_init(&lle->base.la_timer, CALLOUT_MPSAFE);
1158 	/*
1159 	 * For IPv4 this will trigger "arpresolve" to generate
1160 	 * an ARP request.
1161 	 */
1162 	lle->base.la_expire = time_second; /* mark expired */
1163 	lle->l3_addr4 = *(const struct sockaddr_in *)l3addr;
1164 	lle->base.lle_refcnt = 1;
1165 	LLE_LOCK_INIT(&lle->base);
1166 	return &lle->base;
1167 }
1168 
1169 /*
1170  * Deletes an address from the address table.
1171  * This function is called by the timer functions
1172  * such as arptimer() and nd6_llinfo_timer(), and
1173  * the caller does the locking.
1174  */
1175 static void
1176 in_lltable_free(struct lltable *llt, struct llentry *lle)
1177 {
1178 	LLE_WUNLOCK(lle);
1179 	LLE_LOCK_DESTROY(lle);
1180 	free(lle, M_LLTABLE);
1181 }
1182 
1183 static int
1184 in_lltable_rtcheck(struct ifnet *ifp, const struct sockaddr *l3addr)
1185 {
1186 	struct rtentry *rt;
1187 
1188 	KASSERT(l3addr->sa_family == AF_INET,
1189 	    ("sin_family %d", l3addr->sa_family));
1190 
1191 	/* XXX rtalloc1 should take a const param */
1192 	rt = rtalloc1(__DECONST(struct sockaddr *, l3addr), 0, 0);
1193 	if (rt == NULL || (rt->rt_flags & RTF_GATEWAY) || rt->rt_ifp != ifp) {
1194 		log(LOG_INFO, "IPv4 address: \"%s\" is not on the network\n",
1195 		    inet_ntoa(((const struct sockaddr_in *)l3addr)->sin_addr));
1196 		if (rt != NULL)
1197 			RTFREE_LOCKED(rt);
1198 		return (EINVAL);
1199 	}
1200 	RTFREE_LOCKED(rt);
1201 	return 0;
1202 }
1203 
1204 /*
1205  * Return NULL if not found or marked for deletion.
1206  * If found return lle read locked.
1207  */
1208 static struct llentry *
1209 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1210 {
1211 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1212 	struct ifnet *ifp = llt->llt_ifp;
1213 	struct llentry *lle;
1214 	struct llentries *lleh;
1215 	u_int hashkey;
1216 
1217 	IF_AFDATA_LOCK_ASSERT(ifp);
1218 	KASSERT(l3addr->sa_family == AF_INET,
1219 	    ("sin_family %d", l3addr->sa_family));
1220 
1221 	hashkey = sin->sin_addr.s_addr;
1222 	lleh = &llt->lle_head[LLATBL_HASH(hashkey, LLTBL_HASHMASK)];
1223 	LIST_FOREACH(lle, lleh, lle_next) {
1224 		struct sockaddr_in *sa2 = (struct sockaddr_in *)L3_ADDR(lle);
1225 		if (lle->la_flags & LLE_DELETED)
1226 			continue;
1227 		if (sa2->sin_addr.s_addr == sin->sin_addr.s_addr)
1228 			break;
1229 	}
1230 	if (lle == NULL) {
1231 #ifdef DIAGNOSTICS
1232 		if (flags & LLE_DELETE)
1233 			log(LOG_INFO, "interface address is missing from cache = %p  in delete\n", lle);
1234 #endif
1235 		if (!(flags & LLE_CREATE))
1236 			return (NULL);
1237 		/*
1238 		 * A route that covers the given address must have
1239 		 * been installed 1st because we are doing a resolution,
1240 		 * verify this.
1241 		 */
1242 		if (!(flags & LLE_IFADDR) &&
1243 		    in_lltable_rtcheck(ifp, l3addr) != 0)
1244 			goto done;
1245 
1246 		lle = in_lltable_new(l3addr, flags);
1247 		if (lle == NULL) {
1248 			log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1249 			goto done;
1250 		}
1251 		lle->la_flags = flags & ~LLE_CREATE;
1252 		if ((flags & (LLE_CREATE | LLE_IFADDR)) == (LLE_CREATE | LLE_IFADDR)) {
1253 			bcopy(IF_LLADDR(ifp), &lle->ll_addr, ifp->if_addrlen);
1254 			lle->la_flags |= (LLE_VALID | LLE_STATIC);
1255 		}
1256 
1257 		lle->lle_tbl  = llt;
1258 		lle->lle_head = lleh;
1259 		LIST_INSERT_HEAD(lleh, lle, lle_next);
1260 	} else if (flags & LLE_DELETE) {
1261 		if (!(lle->la_flags & LLE_IFADDR) || (flags & LLE_IFADDR)) {
1262 			LLE_WLOCK(lle);
1263 			lle->la_flags = LLE_DELETED;
1264 			LLE_WUNLOCK(lle);
1265 #ifdef DIAGNOSTICS
1266 			log(LOG_INFO, "ifaddr cache = %p  is deleted\n", lle);
1267 #endif
1268 		}
1269 		lle = (void *)-1;
1270 
1271 	}
1272 	if (LLE_IS_VALID(lle)) {
1273 		if (flags & LLE_EXCLUSIVE)
1274 			LLE_WLOCK(lle);
1275 		else
1276 			LLE_RLOCK(lle);
1277 	}
1278 done:
1279 	return (lle);
1280 }
1281 
1282 static int
1283 in_lltable_dump(struct lltable *llt, struct sysctl_req *wr)
1284 {
1285 #define	SIN(lle)	((struct sockaddr_in *) L3_ADDR(lle))
1286 	struct ifnet *ifp = llt->llt_ifp;
1287 	struct llentry *lle;
1288 	/* XXX stack use */
1289 	struct {
1290 		struct rt_msghdr	rtm;
1291 		struct sockaddr_inarp	sin;
1292 		struct sockaddr_dl	sdl;
1293 	} arpc;
1294 	int error, i;
1295 
1296 	/* XXXXX
1297 	 * current IFNET_RLOCK() is mapped to IFNET_WLOCK()
1298 	 * so it is okay to use this ASSERT, change it when
1299 	 * IFNET lock is finalized
1300 	 */
1301 	IFNET_WLOCK_ASSERT();
1302 
1303 	error = 0;
1304 	for (i = 0; i < LLTBL_HASHTBL_SIZE; i++) {
1305 		LIST_FOREACH(lle, &llt->lle_head[i], lle_next) {
1306 			struct sockaddr_dl *sdl;
1307 
1308 			/* skip deleted entries */
1309 			if ((lle->la_flags & (LLE_DELETED|LLE_VALID)) != LLE_VALID)
1310 				continue;
1311 			/* Skip if jailed and not a valid IP of the prison. */
1312 			if (prison_if(wr->td->td_ucred, L3_ADDR(lle)) != 0)
1313 				continue;
1314 			/*
1315 			 * produce a msg made of:
1316 			 *  struct rt_msghdr;
1317 			 *  struct sockaddr_inarp; (IPv4)
1318 			 *  struct sockaddr_dl;
1319 			 */
1320 			bzero(&arpc, sizeof(arpc));
1321 			arpc.rtm.rtm_msglen = sizeof(arpc);
1322 			arpc.rtm.rtm_version = RTM_VERSION;
1323 			arpc.rtm.rtm_type = RTM_GET;
1324 			arpc.rtm.rtm_flags = RTF_UP;
1325 			arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1326 			arpc.sin.sin_family = AF_INET;
1327 			arpc.sin.sin_len = sizeof(arpc.sin);
1328 			arpc.sin.sin_addr.s_addr = SIN(lle)->sin_addr.s_addr;
1329 
1330 			/* publish */
1331 			if (lle->la_flags & LLE_PUB) {
1332 				arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1333 				/* proxy only */
1334 				if (lle->la_flags & LLE_PROXY)
1335 					arpc.sin.sin_other = SIN_PROXY;
1336 			}
1337 
1338 			sdl = &arpc.sdl;
1339 			sdl->sdl_family = AF_LINK;
1340 			sdl->sdl_len = sizeof(*sdl);
1341 			sdl->sdl_alen = ifp->if_addrlen;
1342 			sdl->sdl_index = ifp->if_index;
1343 			sdl->sdl_type = ifp->if_type;
1344 			bcopy(&lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1345 
1346 			arpc.rtm.rtm_rmx.rmx_expire =
1347 			    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1348 			arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1349 			if (lle->la_flags & LLE_STATIC)
1350 				arpc.rtm.rtm_flags |= RTF_STATIC;
1351 			arpc.rtm.rtm_index = ifp->if_index;
1352 			error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1353 			if (error)
1354 				break;
1355 		}
1356 	}
1357 	return error;
1358 #undef SIN
1359 }
1360 
1361 void *
1362 in_domifattach(struct ifnet *ifp)
1363 {
1364 	struct in_ifinfo *ii;
1365 	struct lltable *llt;
1366 
1367 	ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1368 
1369 	llt = lltable_init(ifp, AF_INET);
1370 	if (llt != NULL) {
1371 		llt->llt_new = in_lltable_new;
1372 		llt->llt_free = in_lltable_free;
1373 		llt->llt_rtcheck = in_lltable_rtcheck;
1374 		llt->llt_lookup = in_lltable_lookup;
1375 		llt->llt_dump = in_lltable_dump;
1376 	}
1377 	ii->ii_llt = llt;
1378 
1379 	ii->ii_igmp = igmp_domifattach(ifp);
1380 
1381 	return ii;
1382 }
1383 
1384 void
1385 in_domifdetach(struct ifnet *ifp, void *aux)
1386 {
1387 	struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1388 
1389 	igmp_domifdetach(ifp);
1390 	lltable_free(ii->ii_llt);
1391 	free(ii, M_IFADDR);
1392 }
1393