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