xref: /freebsd/sys/netinet/in.c (revision ab00ac327a66a53edaac95b536b209db3ae2cd9f)
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  * 3. 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(u_long, 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 	u_long i = ntohl(in.s_addr);
100 	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 	u_long i = ntohl(in.s_addr);
191 	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     char *cplim = (char *) &ap->sin_addr;
210     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(cmd, 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(cmd, 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 	callout_init_rw(&ia->ia_garp_timer, &ifp->if_addr_lock,
401 	    CALLOUT_RETURNUNLOCKED);
402 
403 	ia->ia_ifp = ifp;
404 	ia->ia_addr = *addr;
405 	if (mask->sin_len != 0) {
406 		ia->ia_sockmask = *mask;
407 		ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
408 	} else {
409 		in_addr_t i = ntohl(addr->sin_addr.s_addr);
410 
411 		/*
412 	 	 * Be compatible with network classes, if netmask isn't
413 		 * supplied, guess it based on classes.
414 	 	 */
415 		if (IN_CLASSA(i))
416 			ia->ia_subnetmask = IN_CLASSA_NET;
417 		else if (IN_CLASSB(i))
418 			ia->ia_subnetmask = IN_CLASSB_NET;
419 		else
420 			ia->ia_subnetmask = IN_CLASSC_NET;
421 		ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
422 	}
423 	ia->ia_subnet = ntohl(addr->sin_addr.s_addr) & ia->ia_subnetmask;
424 	in_socktrim(&ia->ia_sockmask);
425 
426 	if (ifp->if_flags & IFF_BROADCAST) {
427 		if (broadaddr->sin_len != 0) {
428 			ia->ia_broadaddr = *broadaddr;
429 		} else if (ia->ia_subnetmask == IN_RFC3021_MASK) {
430 			ia->ia_broadaddr.sin_addr.s_addr = INADDR_BROADCAST;
431 			ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
432 			ia->ia_broadaddr.sin_family = AF_INET;
433 		} else {
434 			ia->ia_broadaddr.sin_addr.s_addr =
435 			    htonl(ia->ia_subnet | ~ia->ia_subnetmask);
436 			ia->ia_broadaddr.sin_len = sizeof(struct sockaddr_in);
437 			ia->ia_broadaddr.sin_family = AF_INET;
438 		}
439 	}
440 
441 	if (ifp->if_flags & IFF_POINTOPOINT)
442 		ia->ia_dstaddr = *dstaddr;
443 
444 	/* XXXGL: rtinit() needs this strange assignment. */
445 	if (ifp->if_flags & IFF_LOOPBACK)
446                 ia->ia_dstaddr = ia->ia_addr;
447 
448 	if (vhid != 0) {
449 		error = (*carp_attach_p)(&ia->ia_ifa, vhid);
450 		if (error)
451 			return (error);
452 	}
453 
454 	/* if_addrhead is already referenced by ifa_alloc() */
455 	IF_ADDR_WLOCK(ifp);
456 	TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
457 	IF_ADDR_WUNLOCK(ifp);
458 
459 	ifa_ref(ifa);			/* in_ifaddrhead */
460 	IN_IFADDR_WLOCK();
461 	TAILQ_INSERT_TAIL(&V_in_ifaddrhead, ia, ia_link);
462 	LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
463 	IN_IFADDR_WUNLOCK();
464 
465 	/*
466 	 * Give the interface a chance to initialize
467 	 * if this is its first address,
468 	 * and to validate the address if necessary.
469 	 */
470 	if (ifp->if_ioctl != NULL) {
471 		error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia);
472 		if (error)
473 			goto fail1;
474 	}
475 
476 	/*
477 	 * Add route for the network.
478 	 */
479 	if (vhid == 0) {
480 		int flags = RTF_UP;
481 
482 		if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
483 			flags |= RTF_HOST;
484 
485 		error = in_addprefix(ia, flags);
486 		if (error)
487 			goto fail1;
488 	}
489 
490 	/*
491 	 * Add a loopback route to self.
492 	 */
493 	if (vhid == 0 && (ifp->if_flags & IFF_LOOPBACK) == 0 &&
494 	    ia->ia_addr.sin_addr.s_addr != INADDR_ANY &&
495 	    !((ifp->if_flags & IFF_POINTOPOINT) &&
496 	     ia->ia_dstaddr.sin_addr.s_addr == ia->ia_addr.sin_addr.s_addr)) {
497 		struct in_ifaddr *eia;
498 
499 		eia = in_localip_more(ia);
500 
501 		if (eia == NULL) {
502 			error = ifa_add_loopback_route((struct ifaddr *)ia,
503 			    (struct sockaddr *)&ia->ia_addr);
504 			if (error)
505 				goto fail2;
506 		} else
507 			ifa_free(&eia->ia_ifa);
508 	}
509 
510 	if (iaIsFirst && (ifp->if_flags & IFF_MULTICAST)) {
511 		struct in_addr allhosts_addr;
512 		struct in_ifinfo *ii;
513 
514 		ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
515 		allhosts_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
516 
517 		error = in_joingroup(ifp, &allhosts_addr, NULL,
518 			&ii->ii_allhosts);
519 	}
520 
521 	EVENTHANDLER_INVOKE(ifaddr_event, ifp);
522 
523 	return (error);
524 
525 fail2:
526 	if (vhid == 0)
527 		(void )in_scrubprefix(ia, LLE_STATIC);
528 
529 fail1:
530 	if (ia->ia_ifa.ifa_carp)
531 		(*carp_detach_p)(&ia->ia_ifa, false);
532 
533 	IF_ADDR_WLOCK(ifp);
534 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
535 	IF_ADDR_WUNLOCK(ifp);
536 	ifa_free(&ia->ia_ifa);		/* if_addrhead */
537 
538 	IN_IFADDR_WLOCK();
539 	TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
540 	LIST_REMOVE(ia, ia_hash);
541 	IN_IFADDR_WUNLOCK();
542 	ifa_free(&ia->ia_ifa);		/* in_ifaddrhead */
543 
544 	return (error);
545 }
546 
547 static int
548 in_difaddr_ioctl(u_long cmd, caddr_t data, struct ifnet *ifp, struct thread *td)
549 {
550 	const struct ifreq *ifr = (struct ifreq *)data;
551 	const struct sockaddr_in *addr = (const struct sockaddr_in *)
552 	    &ifr->ifr_addr;
553 	struct ifaddr *ifa;
554 	struct in_ifaddr *ia;
555 	bool deleteAny, iaIsLast;
556 	int error;
557 
558 	if (td != NULL) {
559 		error = priv_check(td, PRIV_NET_DELIFADDR);
560 		if (error)
561 			return (error);
562 	}
563 
564 	if (addr->sin_len != sizeof(struct sockaddr_in) ||
565 	    addr->sin_family != AF_INET)
566 		deleteAny = true;
567 	else
568 		deleteAny = false;
569 
570 	iaIsLast = true;
571 	ia = NULL;
572 	IF_ADDR_WLOCK(ifp);
573 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
574 		struct in_ifaddr *it;
575 
576 		if (ifa->ifa_addr->sa_family != AF_INET)
577 			continue;
578 
579 		it = (struct in_ifaddr *)ifa;
580 		if (deleteAny && ia == NULL && (td == NULL ||
581 		    prison_check_ip4(td->td_ucred, &it->ia_addr.sin_addr) == 0))
582 			ia = it;
583 
584 		if (it->ia_addr.sin_addr.s_addr == addr->sin_addr.s_addr &&
585 		    (td == NULL || prison_check_ip4(td->td_ucred,
586 		    &addr->sin_addr) == 0))
587 			ia = it;
588 
589 		if (it != ia)
590 			iaIsLast = false;
591 	}
592 
593 	if (ia == NULL) {
594 		IF_ADDR_WUNLOCK(ifp);
595 		return (EADDRNOTAVAIL);
596 	}
597 
598 	TAILQ_REMOVE(&ifp->if_addrhead, &ia->ia_ifa, ifa_link);
599 	IF_ADDR_WUNLOCK(ifp);
600 	ifa_free(&ia->ia_ifa);		/* if_addrhead */
601 
602 	IN_IFADDR_WLOCK();
603 	TAILQ_REMOVE(&V_in_ifaddrhead, ia, ia_link);
604 	LIST_REMOVE(ia, ia_hash);
605 	IN_IFADDR_WUNLOCK();
606 
607 	/*
608 	 * in_scrubprefix() kills the interface route.
609 	 */
610 	in_scrubprefix(ia, LLE_STATIC);
611 
612 	/*
613 	 * in_ifadown gets rid of all the rest of
614 	 * the routes.  This is not quite the right
615 	 * thing to do, but at least if we are running
616 	 * a routing process they will come back.
617 	 */
618 	in_ifadown(&ia->ia_ifa, 1);
619 
620 	if (ia->ia_ifa.ifa_carp)
621 		(*carp_detach_p)(&ia->ia_ifa,
622 		    (cmd == SIOCDIFADDR) ? false : true);
623 
624 	/*
625 	 * If this is the last IPv4 address configured on this
626 	 * interface, leave the all-hosts group.
627 	 * No state-change report need be transmitted.
628 	 */
629 	if (iaIsLast && (ifp->if_flags & IFF_MULTICAST)) {
630 		struct in_ifinfo *ii;
631 
632 		ii = ((struct in_ifinfo *)ifp->if_afdata[AF_INET]);
633 		IN_MULTI_LOCK();
634 		if (ii->ii_allhosts) {
635 			(void)in_leavegroup_locked(ii->ii_allhosts, NULL);
636 			ii->ii_allhosts = NULL;
637 		}
638 		IN_MULTI_UNLOCK();
639 	}
640 
641 	IF_ADDR_WLOCK(ifp);
642 	if (callout_stop(&ia->ia_garp_timer) == 1) {
643 		ifa_free(&ia->ia_ifa);
644 	}
645 	IF_ADDR_WUNLOCK(ifp);
646 
647 	EVENTHANDLER_INVOKE(ifaddr_event, ifp);
648 	ifa_free(&ia->ia_ifa);		/* in_ifaddrhead */
649 
650 	return (0);
651 }
652 
653 #define rtinitflags(x) \
654 	((((x)->ia_ifp->if_flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) != 0) \
655 	    ? RTF_HOST : 0)
656 
657 /*
658  * Check if we have a route for the given prefix already or add one accordingly.
659  */
660 int
661 in_addprefix(struct in_ifaddr *target, int flags)
662 {
663 	struct rm_priotracker in_ifa_tracker;
664 	struct in_ifaddr *ia;
665 	struct in_addr prefix, mask, p, m;
666 	int error;
667 
668 	if ((flags & RTF_HOST) != 0) {
669 		prefix = target->ia_dstaddr.sin_addr;
670 		mask.s_addr = 0;
671 	} else {
672 		prefix = target->ia_addr.sin_addr;
673 		mask = target->ia_sockmask.sin_addr;
674 		prefix.s_addr &= mask.s_addr;
675 	}
676 
677 	IN_IFADDR_RLOCK(&in_ifa_tracker);
678 	/* Look for an existing address with the same prefix, mask, and fib */
679 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
680 		if (rtinitflags(ia)) {
681 			p = ia->ia_dstaddr.sin_addr;
682 
683 			if (prefix.s_addr != p.s_addr)
684 				continue;
685 		} else {
686 			p = ia->ia_addr.sin_addr;
687 			m = ia->ia_sockmask.sin_addr;
688 			p.s_addr &= m.s_addr;
689 
690 			if (prefix.s_addr != p.s_addr ||
691 			    mask.s_addr != m.s_addr)
692 				continue;
693 		}
694 		if (target->ia_ifp->if_fib != ia->ia_ifp->if_fib)
695 			continue;
696 
697 		/*
698 		 * If we got a matching prefix route inserted by other
699 		 * interface address, we are done here.
700 		 */
701 		if (ia->ia_flags & IFA_ROUTE) {
702 #ifdef RADIX_MPATH
703 			if (ia->ia_addr.sin_addr.s_addr ==
704 			    target->ia_addr.sin_addr.s_addr) {
705 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
706 				return (EEXIST);
707 			} else
708 				break;
709 #endif
710 			if (V_nosameprefix) {
711 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
712 				return (EEXIST);
713 			} else {
714 				int fibnum;
715 
716 				fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS :
717 					target->ia_ifp->if_fib;
718 				rt_addrmsg(RTM_ADD, &target->ia_ifa, fibnum);
719 				IN_IFADDR_RUNLOCK(&in_ifa_tracker);
720 				return (0);
721 			}
722 		}
723 	}
724 	IN_IFADDR_RUNLOCK(&in_ifa_tracker);
725 
726 	/*
727 	 * No-one seem to have this prefix route, so we try to insert it.
728 	 */
729 	error = rtinit(&target->ia_ifa, (int)RTM_ADD, flags);
730 	if (!error)
731 		target->ia_flags |= IFA_ROUTE;
732 	return (error);
733 }
734 
735 /*
736  * Removes either all lle entries for given @ia, or lle
737  * corresponding to @ia address.
738  */
739 static void
740 in_scrubprefixlle(struct in_ifaddr *ia, int all, u_int flags)
741 {
742 	struct sockaddr_in addr, mask;
743 	struct sockaddr *saddr, *smask;
744 	struct ifnet *ifp;
745 
746 	saddr = (struct sockaddr *)&addr;
747 	bzero(&addr, sizeof(addr));
748 	addr.sin_len = sizeof(addr);
749 	addr.sin_family = AF_INET;
750 	smask = (struct sockaddr *)&mask;
751 	bzero(&mask, sizeof(mask));
752 	mask.sin_len = sizeof(mask);
753 	mask.sin_family = AF_INET;
754 	mask.sin_addr.s_addr = ia->ia_subnetmask;
755 	ifp = ia->ia_ifp;
756 
757 	if (all) {
758 
759 		/*
760 		 * Remove all L2 entries matching given prefix.
761 		 * Convert address to host representation to avoid
762 		 * doing this on every callback. ia_subnetmask is already
763 		 * stored in host representation.
764 		 */
765 		addr.sin_addr.s_addr = ntohl(ia->ia_addr.sin_addr.s_addr);
766 		lltable_prefix_free(AF_INET, saddr, smask, flags);
767 	} else {
768 		/* Remove interface address only */
769 		addr.sin_addr.s_addr = ia->ia_addr.sin_addr.s_addr;
770 		lltable_delete_addr(LLTABLE(ifp), LLE_IFADDR, saddr);
771 	}
772 }
773 
774 /*
775  * If there is no other address in the system that can serve a route to the
776  * same prefix, remove the route.  Hand over the route to the new address
777  * otherwise.
778  */
779 int
780 in_scrubprefix(struct in_ifaddr *target, u_int flags)
781 {
782 	struct rm_priotracker in_ifa_tracker;
783 	struct in_ifaddr *ia;
784 	struct in_addr prefix, mask, p, m;
785 	int error = 0;
786 
787 	/*
788 	 * Remove the loopback route to the interface address.
789 	 */
790 	if ((target->ia_addr.sin_addr.s_addr != INADDR_ANY) &&
791 	    !(target->ia_ifp->if_flags & IFF_LOOPBACK) &&
792 	    (flags & LLE_STATIC)) {
793 		struct in_ifaddr *eia;
794 
795 		/*
796 		 * XXXME: add fib-aware in_localip.
797 		 * We definitely don't want to switch between
798 		 * prefixes in different fibs.
799 		 */
800 		eia = in_localip_more(target);
801 
802 		if (eia != NULL) {
803 			error = ifa_switch_loopback_route((struct ifaddr *)eia,
804 			    (struct sockaddr *)&target->ia_addr);
805 			ifa_free(&eia->ia_ifa);
806 		} else {
807 			error = ifa_del_loopback_route((struct ifaddr *)target,
808 			    (struct sockaddr *)&target->ia_addr);
809 		}
810 	}
811 
812 	if (rtinitflags(target)) {
813 		prefix = target->ia_dstaddr.sin_addr;
814 		mask.s_addr = 0;
815 	} else {
816 		prefix = target->ia_addr.sin_addr;
817 		mask = target->ia_sockmask.sin_addr;
818 		prefix.s_addr &= mask.s_addr;
819 	}
820 
821 	if ((target->ia_flags & IFA_ROUTE) == 0) {
822 		int fibnum;
823 
824 		fibnum = V_rt_add_addr_allfibs ? RT_ALL_FIBS :
825 			target->ia_ifp->if_fib;
826 		rt_addrmsg(RTM_DELETE, &target->ia_ifa, fibnum);
827 
828 		/*
829 		 * Removing address from !IFF_UP interface or
830 		 * prefix which exists on other interface (along with route).
831 		 * No entries should exist here except target addr.
832 		 * Given that, delete this entry only.
833 		 */
834 		in_scrubprefixlle(target, 0, flags);
835 		return (0);
836 	}
837 
838 	IN_IFADDR_RLOCK(&in_ifa_tracker);
839 	TAILQ_FOREACH(ia, &V_in_ifaddrhead, ia_link) {
840 		if (rtinitflags(ia)) {
841 			p = ia->ia_dstaddr.sin_addr;
842 
843 			if (prefix.s_addr != p.s_addr)
844 				continue;
845 		} else {
846 			p = ia->ia_addr.sin_addr;
847 			m = ia->ia_sockmask.sin_addr;
848 			p.s_addr &= m.s_addr;
849 
850 			if (prefix.s_addr != p.s_addr ||
851 			    mask.s_addr != m.s_addr)
852 				continue;
853 		}
854 
855 		if ((ia->ia_ifp->if_flags & IFF_UP) == 0)
856 			continue;
857 
858 		/*
859 		 * If we got a matching prefix address, move IFA_ROUTE and
860 		 * the route itself to it.  Make sure that routing daemons
861 		 * get a heads-up.
862 		 */
863 		if ((ia->ia_flags & IFA_ROUTE) == 0) {
864 			ifa_ref(&ia->ia_ifa);
865 			IN_IFADDR_RUNLOCK(&in_ifa_tracker);
866 			error = rtinit(&(target->ia_ifa), (int)RTM_DELETE,
867 			    rtinitflags(target));
868 			if (error == 0)
869 				target->ia_flags &= ~IFA_ROUTE;
870 			else
871 				log(LOG_INFO, "in_scrubprefix: err=%d, old prefix delete failed\n",
872 					error);
873 			/* Scrub all entries IFF interface is different */
874 			in_scrubprefixlle(target, target->ia_ifp != ia->ia_ifp,
875 			    flags);
876 			error = rtinit(&ia->ia_ifa, (int)RTM_ADD,
877 			    rtinitflags(ia) | RTF_UP);
878 			if (error == 0)
879 				ia->ia_flags |= IFA_ROUTE;
880 			else
881 				log(LOG_INFO, "in_scrubprefix: err=%d, new prefix add failed\n",
882 					error);
883 			ifa_free(&ia->ia_ifa);
884 			return (error);
885 		}
886 	}
887 	IN_IFADDR_RUNLOCK(&in_ifa_tracker);
888 
889 	/*
890 	 * remove all L2 entries on the given prefix
891 	 */
892 	in_scrubprefixlle(target, 1, flags);
893 
894 	/*
895 	 * As no-one seem to have this prefix, we can remove the route.
896 	 */
897 	error = rtinit(&(target->ia_ifa), (int)RTM_DELETE, rtinitflags(target));
898 	if (error == 0)
899 		target->ia_flags &= ~IFA_ROUTE;
900 	else
901 		log(LOG_INFO, "in_scrubprefix: err=%d, prefix delete failed\n", error);
902 	return (error);
903 }
904 
905 #undef rtinitflags
906 
907 void
908 in_ifscrub_all(void)
909 {
910 	struct ifnet *ifp;
911 	struct ifaddr *ifa, *nifa;
912 	struct ifaliasreq ifr;
913 
914 	IFNET_RLOCK();
915 	TAILQ_FOREACH(ifp, &V_ifnet, if_link) {
916 		/* Cannot lock here - lock recursion. */
917 		/* IF_ADDR_RLOCK(ifp); */
918 		TAILQ_FOREACH_SAFE(ifa, &ifp->if_addrhead, ifa_link, nifa) {
919 			if (ifa->ifa_addr->sa_family != AF_INET)
920 				continue;
921 
922 			/*
923 			 * This is ugly but the only way for legacy IP to
924 			 * cleanly remove addresses and everything attached.
925 			 */
926 			bzero(&ifr, sizeof(ifr));
927 			ifr.ifra_addr = *ifa->ifa_addr;
928 			if (ifa->ifa_dstaddr)
929 			ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
930 			(void)in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr,
931 			    ifp, NULL);
932 		}
933 		/* IF_ADDR_RUNLOCK(ifp); */
934 		in_purgemaddrs(ifp);
935 		igmp_domifdetach(ifp);
936 	}
937 	IFNET_RUNLOCK();
938 }
939 
940 int
941 in_ifaddr_broadcast(struct in_addr in, struct in_ifaddr *ia)
942 {
943 
944 	return ((in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
945 	     /*
946 	      * Check for old-style (host 0) broadcast, but
947 	      * taking into account that RFC 3021 obsoletes it.
948 	      */
949 	    (ia->ia_subnetmask != IN_RFC3021_MASK &&
950 	    ntohl(in.s_addr) == ia->ia_subnet)) &&
951 	     /*
952 	      * Check for an all one subnetmask. These
953 	      * only exist when an interface gets a secondary
954 	      * address.
955 	      */
956 	    ia->ia_subnetmask != (u_long)0xffffffff);
957 }
958 
959 /*
960  * Return 1 if the address might be a local broadcast address.
961  */
962 int
963 in_broadcast(struct in_addr in, struct ifnet *ifp)
964 {
965 	struct ifaddr *ifa;
966 	int found;
967 
968 	if (in.s_addr == INADDR_BROADCAST ||
969 	    in.s_addr == INADDR_ANY)
970 		return (1);
971 	if ((ifp->if_flags & IFF_BROADCAST) == 0)
972 		return (0);
973 	found = 0;
974 	/*
975 	 * Look through the list of addresses for a match
976 	 * with a broadcast address.
977 	 */
978 	IF_ADDR_RLOCK(ifp);
979 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
980 		if (ifa->ifa_addr->sa_family == AF_INET &&
981 		    in_ifaddr_broadcast(in, (struct in_ifaddr *)ifa)) {
982 			found = 1;
983 			break;
984 		}
985 	IF_ADDR_RUNLOCK(ifp);
986 	return (found);
987 }
988 
989 /*
990  * On interface removal, clean up IPv4 data structures hung off of the ifnet.
991  */
992 void
993 in_ifdetach(struct ifnet *ifp)
994 {
995 
996 	in_pcbpurgeif0(&V_ripcbinfo, ifp);
997 	in_pcbpurgeif0(&V_udbinfo, ifp);
998 	in_pcbpurgeif0(&V_ulitecbinfo, ifp);
999 	in_purgemaddrs(ifp);
1000 }
1001 
1002 /*
1003  * Delete all IPv4 multicast address records, and associated link-layer
1004  * multicast address records, associated with ifp.
1005  * XXX It looks like domifdetach runs AFTER the link layer cleanup.
1006  * XXX This should not race with ifma_protospec being set during
1007  * a new allocation, if it does, we have bigger problems.
1008  */
1009 static void
1010 in_purgemaddrs(struct ifnet *ifp)
1011 {
1012 	LIST_HEAD(,in_multi) purgeinms;
1013 	struct in_multi		*inm, *tinm;
1014 	struct ifmultiaddr	*ifma;
1015 
1016 	LIST_INIT(&purgeinms);
1017 	IN_MULTI_LOCK();
1018 
1019 	/*
1020 	 * Extract list of in_multi associated with the detaching ifp
1021 	 * which the PF_INET layer is about to release.
1022 	 * We need to do this as IF_ADDR_LOCK() may be re-acquired
1023 	 * by code further down.
1024 	 */
1025 	IF_ADDR_RLOCK(ifp);
1026 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
1027 		if (ifma->ifma_addr->sa_family != AF_INET ||
1028 		    ifma->ifma_protospec == NULL)
1029 			continue;
1030 #if 0
1031 		KASSERT(ifma->ifma_protospec != NULL,
1032 		    ("%s: ifma_protospec is NULL", __func__));
1033 #endif
1034 		inm = (struct in_multi *)ifma->ifma_protospec;
1035 		LIST_INSERT_HEAD(&purgeinms, inm, inm_link);
1036 	}
1037 	IF_ADDR_RUNLOCK(ifp);
1038 
1039 	LIST_FOREACH_SAFE(inm, &purgeinms, inm_link, tinm) {
1040 		LIST_REMOVE(inm, inm_link);
1041 		inm_release_locked(inm);
1042 	}
1043 	igmp_ifdetach(ifp);
1044 
1045 	IN_MULTI_UNLOCK();
1046 }
1047 
1048 struct in_llentry {
1049 	struct llentry		base;
1050 };
1051 
1052 #define	IN_LLTBL_DEFAULT_HSIZE	32
1053 #define	IN_LLTBL_HASH(k, h) \
1054 	(((((((k >> 8) ^ k) >> 8) ^ k) >> 8) ^ k) & ((h) - 1))
1055 
1056 /*
1057  * Do actual deallocation of @lle.
1058  */
1059 static void
1060 in_lltable_destroy_lle_unlocked(struct llentry *lle)
1061 {
1062 
1063 	LLE_LOCK_DESTROY(lle);
1064 	LLE_REQ_DESTROY(lle);
1065 	free(lle, M_LLTABLE);
1066 }
1067 
1068 /*
1069  * Called by LLE_FREE_LOCKED when number of references
1070  * drops to zero.
1071  */
1072 static void
1073 in_lltable_destroy_lle(struct llentry *lle)
1074 {
1075 
1076 	LLE_WUNLOCK(lle);
1077 	in_lltable_destroy_lle_unlocked(lle);
1078 }
1079 
1080 static struct llentry *
1081 in_lltable_new(struct in_addr addr4, u_int flags)
1082 {
1083 	struct in_llentry *lle;
1084 
1085 	lle = malloc(sizeof(struct in_llentry), M_LLTABLE, M_NOWAIT | M_ZERO);
1086 	if (lle == NULL)		/* NB: caller generates msg */
1087 		return NULL;
1088 
1089 	/*
1090 	 * For IPv4 this will trigger "arpresolve" to generate
1091 	 * an ARP request.
1092 	 */
1093 	lle->base.la_expire = time_uptime; /* mark expired */
1094 	lle->base.r_l3addr.addr4 = addr4;
1095 	lle->base.lle_refcnt = 1;
1096 	lle->base.lle_free = in_lltable_destroy_lle;
1097 	LLE_LOCK_INIT(&lle->base);
1098 	LLE_REQ_INIT(&lle->base);
1099 	callout_init(&lle->base.lle_timer, 1);
1100 
1101 	return (&lle->base);
1102 }
1103 
1104 #define IN_ARE_MASKED_ADDR_EQUAL(d, a, m)	(		\
1105 	((((d).s_addr ^ (a).s_addr) & (m).s_addr)) == 0 )
1106 
1107 static int
1108 in_lltable_match_prefix(const struct sockaddr *saddr,
1109     const struct sockaddr *smask, u_int flags, struct llentry *lle)
1110 {
1111 	struct in_addr addr, mask, lle_addr;
1112 
1113 	addr = ((const struct sockaddr_in *)saddr)->sin_addr;
1114 	mask = ((const struct sockaddr_in *)smask)->sin_addr;
1115 	lle_addr.s_addr = ntohl(lle->r_l3addr.addr4.s_addr);
1116 
1117 	if (IN_ARE_MASKED_ADDR_EQUAL(lle_addr, addr, mask) == 0)
1118 		return (0);
1119 
1120 	if (lle->la_flags & LLE_IFADDR) {
1121 
1122 		/*
1123 		 * Delete LLE_IFADDR records IFF address & flag matches.
1124 		 * Note that addr is the interface address within prefix
1125 		 * being matched.
1126 		 * Note also we should handle 'ifdown' cases without removing
1127 		 * ifaddr macs.
1128 		 */
1129 		if (addr.s_addr == lle_addr.s_addr && (flags & LLE_STATIC) != 0)
1130 			return (1);
1131 		return (0);
1132 	}
1133 
1134 	/* flags & LLE_STATIC means deleting both dynamic and static entries */
1135 	if ((flags & LLE_STATIC) || !(lle->la_flags & LLE_STATIC))
1136 		return (1);
1137 
1138 	return (0);
1139 }
1140 
1141 static void
1142 in_lltable_free_entry(struct lltable *llt, struct llentry *lle)
1143 {
1144 	struct ifnet *ifp;
1145 	size_t pkts_dropped;
1146 
1147 	LLE_WLOCK_ASSERT(lle);
1148 	KASSERT(llt != NULL, ("lltable is NULL"));
1149 
1150 	/* Unlink entry from table if not already */
1151 	if ((lle->la_flags & LLE_LINKED) != 0) {
1152 		ifp = llt->llt_ifp;
1153 		IF_AFDATA_WLOCK_ASSERT(ifp);
1154 		lltable_unlink_entry(llt, lle);
1155 	}
1156 
1157 	/* cancel timer */
1158 	if (callout_stop(&lle->lle_timer) > 0)
1159 		LLE_REMREF(lle);
1160 
1161 	/* Drop hold queue */
1162 	pkts_dropped = llentry_free(lle);
1163 	ARPSTAT_ADD(dropped, pkts_dropped);
1164 }
1165 
1166 static int
1167 in_lltable_rtcheck(struct ifnet *ifp, u_int flags, const struct sockaddr *l3addr)
1168 {
1169 	struct rt_addrinfo info;
1170 	struct sockaddr_in rt_key, rt_mask;
1171 	struct sockaddr rt_gateway;
1172 	int rt_flags;
1173 
1174 	KASSERT(l3addr->sa_family == AF_INET,
1175 	    ("sin_family %d", l3addr->sa_family));
1176 
1177 	bzero(&rt_key, sizeof(rt_key));
1178 	rt_key.sin_len = sizeof(rt_key);
1179 	bzero(&rt_mask, sizeof(rt_mask));
1180 	rt_mask.sin_len = sizeof(rt_mask);
1181 	bzero(&rt_gateway, sizeof(rt_gateway));
1182 	rt_gateway.sa_len = sizeof(rt_gateway);
1183 
1184 	bzero(&info, sizeof(info));
1185 	info.rti_info[RTAX_DST] = (struct sockaddr *)&rt_key;
1186 	info.rti_info[RTAX_NETMASK] = (struct sockaddr *)&rt_mask;
1187 	info.rti_info[RTAX_GATEWAY] = (struct sockaddr *)&rt_gateway;
1188 
1189 	if (rib_lookup_info(ifp->if_fib, l3addr, NHR_REF, 0, &info) != 0)
1190 		return (EINVAL);
1191 
1192 	rt_flags = info.rti_flags;
1193 
1194 	/*
1195 	 * If the gateway for an existing host route matches the target L3
1196 	 * address, which is a special route inserted by some implementation
1197 	 * such as MANET, and the interface is of the correct type, then
1198 	 * allow for ARP to proceed.
1199 	 */
1200 	if (rt_flags & RTF_GATEWAY) {
1201 		if (!(rt_flags & RTF_HOST) || !info.rti_ifp ||
1202 		    info.rti_ifp->if_type != IFT_ETHER ||
1203 		    (info.rti_ifp->if_flags & (IFF_NOARP | IFF_STATICARP)) != 0 ||
1204 		    memcmp(rt_gateway.sa_data, l3addr->sa_data,
1205 		    sizeof(in_addr_t)) != 0) {
1206 			rib_free_info(&info);
1207 			return (EINVAL);
1208 		}
1209 	}
1210 	rib_free_info(&info);
1211 
1212 	/*
1213 	 * Make sure that at least the destination address is covered
1214 	 * by the route. This is for handling the case where 2 or more
1215 	 * interfaces have the same prefix. An incoming packet arrives
1216 	 * on one interface and the corresponding outgoing packet leaves
1217 	 * another interface.
1218 	 */
1219 	if (!(rt_flags & RTF_HOST) && info.rti_ifp != ifp) {
1220 		const char *sa, *mask, *addr, *lim;
1221 		const struct sockaddr_in *l3sin;
1222 
1223 		mask = (const char *)&rt_mask;
1224 		/*
1225 		 * Just being extra cautious to avoid some custom
1226 		 * code getting into trouble.
1227 		 */
1228 		if ((info.rti_addrs & RTA_NETMASK) == 0)
1229 			return (EINVAL);
1230 
1231 		sa = (const char *)&rt_key;
1232 		addr = (const char *)l3addr;
1233 		l3sin = (const struct sockaddr_in *)l3addr;
1234 		lim = addr + l3sin->sin_len;
1235 
1236 		for ( ; addr < lim; sa++, mask++, addr++) {
1237 			if ((*sa ^ *addr) & *mask) {
1238 #ifdef DIAGNOSTIC
1239 				char addrbuf[INET_ADDRSTRLEN];
1240 
1241 				log(LOG_INFO, "IPv4 address: \"%s\" "
1242 				    "is not on the network\n",
1243 				    inet_ntoa_r(l3sin->sin_addr, addrbuf));
1244 #endif
1245 				return (EINVAL);
1246 			}
1247 		}
1248 	}
1249 
1250 	return (0);
1251 }
1252 
1253 static inline uint32_t
1254 in_lltable_hash_dst(const struct in_addr dst, uint32_t hsize)
1255 {
1256 
1257 	return (IN_LLTBL_HASH(dst.s_addr, hsize));
1258 }
1259 
1260 static uint32_t
1261 in_lltable_hash(const struct llentry *lle, uint32_t hsize)
1262 {
1263 
1264 	return (in_lltable_hash_dst(lle->r_l3addr.addr4, hsize));
1265 }
1266 
1267 static void
1268 in_lltable_fill_sa_entry(const struct llentry *lle, struct sockaddr *sa)
1269 {
1270 	struct sockaddr_in *sin;
1271 
1272 	sin = (struct sockaddr_in *)sa;
1273 	bzero(sin, sizeof(*sin));
1274 	sin->sin_family = AF_INET;
1275 	sin->sin_len = sizeof(*sin);
1276 	sin->sin_addr = lle->r_l3addr.addr4;
1277 }
1278 
1279 static inline struct llentry *
1280 in_lltable_find_dst(struct lltable *llt, struct in_addr dst)
1281 {
1282 	struct llentry *lle;
1283 	struct llentries *lleh;
1284 	u_int hashidx;
1285 
1286 	hashidx = in_lltable_hash_dst(dst, llt->llt_hsize);
1287 	lleh = &llt->lle_head[hashidx];
1288 	LIST_FOREACH(lle, lleh, lle_next) {
1289 		if (lle->la_flags & LLE_DELETED)
1290 			continue;
1291 		if (lle->r_l3addr.addr4.s_addr == dst.s_addr)
1292 			break;
1293 	}
1294 
1295 	return (lle);
1296 }
1297 
1298 static void
1299 in_lltable_delete_entry(struct lltable *llt, struct llentry *lle)
1300 {
1301 
1302 	lle->la_flags |= LLE_DELETED;
1303 	EVENTHANDLER_INVOKE(lle_event, lle, LLENTRY_DELETED);
1304 #ifdef DIAGNOSTIC
1305 	log(LOG_INFO, "ifaddr cache = %p is deleted\n", lle);
1306 #endif
1307 	llentry_free(lle);
1308 }
1309 
1310 static struct llentry *
1311 in_lltable_alloc(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1312 {
1313 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1314 	struct ifnet *ifp = llt->llt_ifp;
1315 	struct llentry *lle;
1316 	char linkhdr[LLE_MAX_LINKHDR];
1317 	size_t linkhdrsize;
1318 	int lladdr_off;
1319 
1320 	KASSERT(l3addr->sa_family == AF_INET,
1321 	    ("sin_family %d", l3addr->sa_family));
1322 
1323 	/*
1324 	 * A route that covers the given address must have
1325 	 * been installed 1st because we are doing a resolution,
1326 	 * verify this.
1327 	 */
1328 	if (!(flags & LLE_IFADDR) &&
1329 	    in_lltable_rtcheck(ifp, flags, l3addr) != 0)
1330 		return (NULL);
1331 
1332 	lle = in_lltable_new(sin->sin_addr, flags);
1333 	if (lle == NULL) {
1334 		log(LOG_INFO, "lla_lookup: new lle malloc failed\n");
1335 		return (NULL);
1336 	}
1337 	lle->la_flags = flags;
1338 	if (flags & LLE_STATIC)
1339 		lle->r_flags |= RLLE_VALID;
1340 	if ((flags & LLE_IFADDR) == LLE_IFADDR) {
1341 		linkhdrsize = LLE_MAX_LINKHDR;
1342 		if (lltable_calc_llheader(ifp, AF_INET, IF_LLADDR(ifp),
1343 		    linkhdr, &linkhdrsize, &lladdr_off) != 0) {
1344 			in_lltable_destroy_lle_unlocked(lle);
1345 			return (NULL);
1346 		}
1347 		lltable_set_entry_addr(ifp, lle, linkhdr, linkhdrsize,
1348 		    lladdr_off);
1349 		lle->la_flags |= LLE_STATIC;
1350 		lle->r_flags |= (RLLE_VALID | RLLE_IFADDR);
1351 	}
1352 
1353 	return (lle);
1354 }
1355 
1356 /*
1357  * Return NULL if not found or marked for deletion.
1358  * If found return lle read locked.
1359  */
1360 static struct llentry *
1361 in_lltable_lookup(struct lltable *llt, u_int flags, const struct sockaddr *l3addr)
1362 {
1363 	const struct sockaddr_in *sin = (const struct sockaddr_in *)l3addr;
1364 	struct llentry *lle;
1365 
1366 	IF_AFDATA_LOCK_ASSERT(llt->llt_ifp);
1367 	KASSERT(l3addr->sa_family == AF_INET,
1368 	    ("sin_family %d", l3addr->sa_family));
1369 	lle = in_lltable_find_dst(llt, sin->sin_addr);
1370 
1371 	if (lle == NULL)
1372 		return (NULL);
1373 
1374 	KASSERT((flags & (LLE_UNLOCKED|LLE_EXCLUSIVE)) !=
1375 	    (LLE_UNLOCKED|LLE_EXCLUSIVE),("wrong lle request flags: 0x%X",
1376 	    flags));
1377 
1378 	if (flags & LLE_UNLOCKED)
1379 		return (lle);
1380 
1381 	if (flags & LLE_EXCLUSIVE)
1382 		LLE_WLOCK(lle);
1383 	else
1384 		LLE_RLOCK(lle);
1385 
1386 	return (lle);
1387 }
1388 
1389 static int
1390 in_lltable_dump_entry(struct lltable *llt, struct llentry *lle,
1391     struct sysctl_req *wr)
1392 {
1393 	struct ifnet *ifp = llt->llt_ifp;
1394 	/* XXX stack use */
1395 	struct {
1396 		struct rt_msghdr	rtm;
1397 		struct sockaddr_in	sin;
1398 		struct sockaddr_dl	sdl;
1399 	} arpc;
1400 	struct sockaddr_dl *sdl;
1401 	int error;
1402 
1403 	bzero(&arpc, sizeof(arpc));
1404 			/* skip deleted entries */
1405 			if ((lle->la_flags & LLE_DELETED) == LLE_DELETED)
1406 				return (0);
1407 			/* Skip if jailed and not a valid IP of the prison. */
1408 			lltable_fill_sa_entry(lle,(struct sockaddr *)&arpc.sin);
1409 			if (prison_if(wr->td->td_ucred,
1410 			    (struct sockaddr *)&arpc.sin) != 0)
1411 				return (0);
1412 			/*
1413 			 * produce a msg made of:
1414 			 *  struct rt_msghdr;
1415 			 *  struct sockaddr_in; (IPv4)
1416 			 *  struct sockaddr_dl;
1417 			 */
1418 			arpc.rtm.rtm_msglen = sizeof(arpc);
1419 			arpc.rtm.rtm_version = RTM_VERSION;
1420 			arpc.rtm.rtm_type = RTM_GET;
1421 			arpc.rtm.rtm_flags = RTF_UP;
1422 			arpc.rtm.rtm_addrs = RTA_DST | RTA_GATEWAY;
1423 
1424 			/* publish */
1425 			if (lle->la_flags & LLE_PUB)
1426 				arpc.rtm.rtm_flags |= RTF_ANNOUNCE;
1427 
1428 			sdl = &arpc.sdl;
1429 			sdl->sdl_family = AF_LINK;
1430 			sdl->sdl_len = sizeof(*sdl);
1431 			sdl->sdl_index = ifp->if_index;
1432 			sdl->sdl_type = ifp->if_type;
1433 			if ((lle->la_flags & LLE_VALID) == LLE_VALID) {
1434 				sdl->sdl_alen = ifp->if_addrlen;
1435 				bcopy(lle->ll_addr, LLADDR(sdl), ifp->if_addrlen);
1436 			} else {
1437 				sdl->sdl_alen = 0;
1438 				bzero(LLADDR(sdl), ifp->if_addrlen);
1439 			}
1440 
1441 			arpc.rtm.rtm_rmx.rmx_expire =
1442 			    lle->la_flags & LLE_STATIC ? 0 : lle->la_expire;
1443 			arpc.rtm.rtm_flags |= (RTF_HOST | RTF_LLDATA);
1444 			if (lle->la_flags & LLE_STATIC)
1445 				arpc.rtm.rtm_flags |= RTF_STATIC;
1446 			if (lle->la_flags & LLE_IFADDR)
1447 				arpc.rtm.rtm_flags |= RTF_PINNED;
1448 			arpc.rtm.rtm_index = ifp->if_index;
1449 			error = SYSCTL_OUT(wr, &arpc, sizeof(arpc));
1450 
1451 	return (error);
1452 }
1453 
1454 static struct lltable *
1455 in_lltattach(struct ifnet *ifp)
1456 {
1457 	struct lltable *llt;
1458 
1459 	llt = lltable_allocate_htbl(IN_LLTBL_DEFAULT_HSIZE);
1460  	llt->llt_af = AF_INET;
1461  	llt->llt_ifp = ifp;
1462 
1463 	llt->llt_lookup = in_lltable_lookup;
1464 	llt->llt_alloc_entry = in_lltable_alloc;
1465 	llt->llt_delete_entry = in_lltable_delete_entry;
1466 	llt->llt_dump_entry = in_lltable_dump_entry;
1467 	llt->llt_hash = in_lltable_hash;
1468 	llt->llt_fill_sa_entry = in_lltable_fill_sa_entry;
1469 	llt->llt_free_entry = in_lltable_free_entry;
1470 	llt->llt_match_prefix = in_lltable_match_prefix;
1471  	lltable_link(llt);
1472 
1473 	return (llt);
1474 }
1475 
1476 void *
1477 in_domifattach(struct ifnet *ifp)
1478 {
1479 	struct in_ifinfo *ii;
1480 
1481 	ii = malloc(sizeof(struct in_ifinfo), M_IFADDR, M_WAITOK|M_ZERO);
1482 
1483 	ii->ii_llt = in_lltattach(ifp);
1484 	ii->ii_igmp = igmp_domifattach(ifp);
1485 
1486 	return (ii);
1487 }
1488 
1489 void
1490 in_domifdetach(struct ifnet *ifp, void *aux)
1491 {
1492 	struct in_ifinfo *ii = (struct in_ifinfo *)aux;
1493 
1494 	igmp_domifdetach(ifp);
1495 	lltable_free(ii->ii_llt);
1496 	free(ii, M_IFADDR);
1497 }
1498