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