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