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