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