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