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