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