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