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