xref: /freebsd/sys/net/if.c (revision b30a244c34af22d80a4fcba8aa7b423947f8ef42)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1980, 1986, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
3366afbd68SRuslan Ermilov  *	@(#)if.c	8.5 (Berkeley) 1/9/95
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
375591b823SEivind Eklund #include "opt_compat.h"
38cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
390d0f9d1eSYoshinobu Inoue #include "opt_inet.h"
40e70cd263SRobert Watson #include "opt_mac.h"
415591b823SEivind Eklund 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43f13ad206SJonathan Lemon #include <sys/conf.h>
44e70cd263SRobert Watson #include <sys/mac.h>
454d1d4912SBruce Evans #include <sys/malloc.h>
46d2b4566aSJonathan Lemon #include <sys/bus.h>
47df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
48df8bae1dSRodney W. Grimes #include <sys/systm.h>
49df8bae1dSRodney W. Grimes #include <sys/proc.h>
50df8bae1dSRodney W. Grimes #include <sys/socket.h>
51df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
52df8bae1dSRodney W. Grimes #include <sys/protosw.h>
53df8bae1dSRodney W. Grimes #include <sys/kernel.h>
5451a53488SBruce Evans #include <sys/sockio.h>
55963e4c2aSGarrett Wollman #include <sys/syslog.h>
56602d513cSGarrett Wollman #include <sys/sysctl.h>
5791421ba2SRobert Watson #include <sys/jail.h>
58fa882e87SBrooks Davis #include <machine/stdarg.h>
59df8bae1dSRodney W. Grimes 
60df8bae1dSRodney W. Grimes #include <net/if.h>
61b106252cSBill Paul #include <net/if_arp.h>
62df8bae1dSRodney W. Grimes #include <net/if_dl.h>
6366ce51ceSArchie Cobbs #include <net/if_types.h>
6430aad87dSBrooks Davis #include <net/if_var.h>
659448326fSPoul-Henning Kamp #include <net/radix.h>
665500d3beSWarner Losh #include <net/route.h>
67df8bae1dSRodney W. Grimes 
680d0f9d1eSYoshinobu Inoue #if defined(INET) || defined(INET6)
6982cd038dSYoshinobu Inoue /*XXX*/
7082cd038dSYoshinobu Inoue #include <netinet/in.h>
710d0f9d1eSYoshinobu Inoue #include <netinet/in_var.h>
723411310dSYoshinobu Inoue #ifdef INET6
73978ee2edSJun-ichiro itojun Hagino #include <netinet6/in6_var.h>
74978ee2edSJun-ichiro itojun Hagino #include <netinet6/in6_ifattach.h>
753411310dSYoshinobu Inoue #endif
7682cd038dSYoshinobu Inoue #endif
77c0933269SPeter Wemm #ifdef INET
78c0933269SPeter Wemm #include <netinet/if_ether.h>
79c0933269SPeter Wemm #endif
8082cd038dSYoshinobu Inoue 
810b59d917SJonathan Lemon static int	ifconf(u_long, caddr_t);
82f9132cebSJonathan Lemon static void	if_grow(void);
83f9132cebSJonathan Lemon static void	if_init(void *);
84f9132cebSJonathan Lemon static void	if_check(void *);
85ffb5a104SJonathan Lemon static int	if_findindex(struct ifnet *);
860b59d917SJonathan Lemon static void	if_qflush(struct ifqueue *);
870b59d917SJonathan Lemon static void	if_slowtimo(void *);
888071913dSRuslan Ermilov static void	link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
890b59d917SJonathan Lemon static int	if_rtdel(struct radix_node *, void *);
900b59d917SJonathan Lemon static struct	if_clone *if_clone_lookup(const char *, int *);
910b59d917SJonathan Lemon static int	if_clone_list(struct if_clonereq *);
92f13ad206SJonathan Lemon static int	ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
9382cd038dSYoshinobu Inoue #ifdef INET6
9482cd038dSYoshinobu Inoue /*
9582cd038dSYoshinobu Inoue  * XXX: declare here to avoid to include many inet6 related files..
9682cd038dSYoshinobu Inoue  * should be more generalized?
9782cd038dSYoshinobu Inoue  */
98929ddbbbSAlfred Perlstein extern void	nd6_setmtu(struct ifnet *);
9982cd038dSYoshinobu Inoue #endif
10082cd038dSYoshinobu Inoue 
1010b59d917SJonathan Lemon int	if_index = 0;
102f9132cebSJonathan Lemon struct	ifindex_entry *ifindex_table = NULL;
1030b59d917SJonathan Lemon int	ifqmaxlen = IFQ_MAXLEN;
1040b59d917SJonathan Lemon struct	ifnethead ifnet;	/* depend on static init XXX */
105b30a244cSJeffrey Hsu struct	mtx ifnet_lock;
10630aad87dSBrooks Davis int	if_cloners_count;
1070b59d917SJonathan Lemon LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
1080b59d917SJonathan Lemon 
109f9132cebSJonathan Lemon static int	if_indexlim = 8;
110f13ad206SJonathan Lemon static struct	klist ifklist;
111f9132cebSJonathan Lemon 
1129a2a57a1SJonathan Lemon static void	filt_netdetach(struct knote *kn);
1139a2a57a1SJonathan Lemon static int	filt_netdev(struct knote *kn, long hint);
1149a2a57a1SJonathan Lemon 
1159a2a57a1SJonathan Lemon static struct filterops netdev_filtops =
1169a2a57a1SJonathan Lemon     { 1, NULL, filt_netdetach, filt_netdev };
1179a2a57a1SJonathan Lemon 
1180b59d917SJonathan Lemon /*
1190b59d917SJonathan Lemon  * System initialization
1200b59d917SJonathan Lemon  */
121f9132cebSJonathan Lemon SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL)
122f9132cebSJonathan Lemon SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL)
1230b59d917SJonathan Lemon 
1240b59d917SJonathan Lemon MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
1250b59d917SJonathan Lemon MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
1263b16e7b2SMaxime Henrion MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
12730aad87dSBrooks Davis 
128f13ad206SJonathan Lemon #define CDEV_MAJOR	165
129f13ad206SJonathan Lemon 
130f13ad206SJonathan Lemon static d_open_t		netopen;
131f13ad206SJonathan Lemon static d_close_t	netclose;
132f13ad206SJonathan Lemon static d_ioctl_t	netioctl;
1339a2a57a1SJonathan Lemon static d_kqfilter_t	netkqfilter;
134f13ad206SJonathan Lemon 
135f13ad206SJonathan Lemon static struct cdevsw net_cdevsw = {
136f13ad206SJonathan Lemon 	/* open */	netopen,
137f13ad206SJonathan Lemon 	/* close */	netclose,
138f13ad206SJonathan Lemon 	/* read */	noread,
139f13ad206SJonathan Lemon 	/* write */	nowrite,
140f13ad206SJonathan Lemon 	/* ioctl */	netioctl,
141f13ad206SJonathan Lemon 	/* poll */	nopoll,
142f13ad206SJonathan Lemon 	/* mmap */	nommap,
143f13ad206SJonathan Lemon 	/* strategy */	nostrategy,
144f13ad206SJonathan Lemon 	/* name */	"net",
145f13ad206SJonathan Lemon 	/* maj */	CDEV_MAJOR,
146f13ad206SJonathan Lemon 	/* dump */	nodump,
147f13ad206SJonathan Lemon 	/* psize */	nopsize,
1489a2a57a1SJonathan Lemon 	/* flags */	D_KQFILTER,
1499a2a57a1SJonathan Lemon 	/* kqfilter */	netkqfilter,
150f13ad206SJonathan Lemon };
151f13ad206SJonathan Lemon 
152f13ad206SJonathan Lemon static int
153f13ad206SJonathan Lemon netopen(dev_t dev, int flag, int mode, struct thread *td)
154f13ad206SJonathan Lemon {
155f13ad206SJonathan Lemon 	return (0);
156f13ad206SJonathan Lemon }
157f13ad206SJonathan Lemon 
158f13ad206SJonathan Lemon static int
159f13ad206SJonathan Lemon netclose(dev_t dev, int flags, int fmt, struct thread *td)
160f13ad206SJonathan Lemon {
161f13ad206SJonathan Lemon 	return (0);
162f13ad206SJonathan Lemon }
163f13ad206SJonathan Lemon 
164f13ad206SJonathan Lemon static int
165f13ad206SJonathan Lemon netioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
166f13ad206SJonathan Lemon {
167f13ad206SJonathan Lemon 	struct ifnet *ifp;
168f13ad206SJonathan Lemon 	int error, idx;
169f13ad206SJonathan Lemon 
170f13ad206SJonathan Lemon 	/* only support interface specific ioctls */
171f13ad206SJonathan Lemon 	if (IOCGROUP(cmd) != 'i')
172f13ad206SJonathan Lemon 		return (EOPNOTSUPP);
173f13ad206SJonathan Lemon 	idx = minor(dev);
174f13ad206SJonathan Lemon 	if (idx == 0) {
175f13ad206SJonathan Lemon 		/*
176f13ad206SJonathan Lemon 		 * special network device, not interface.
177f13ad206SJonathan Lemon 		 */
178f13ad206SJonathan Lemon 		if (cmd == SIOCGIFCONF)
179f13ad206SJonathan Lemon 			return (ifconf(cmd, data));	/* XXX remove cmd */
180f13ad206SJonathan Lemon 		return (EOPNOTSUPP);
181f13ad206SJonathan Lemon 	}
182f13ad206SJonathan Lemon 
183f13ad206SJonathan Lemon 	ifp = ifnet_byindex(idx);
184f13ad206SJonathan Lemon 	if (ifp == NULL)
185f13ad206SJonathan Lemon 		return (ENXIO);
186f13ad206SJonathan Lemon 
187f13ad206SJonathan Lemon 	error = ifhwioctl(cmd, ifp, data, td);
188f13ad206SJonathan Lemon 	if (error == ENOIOCTL)
189f13ad206SJonathan Lemon 		error = EOPNOTSUPP;
190f13ad206SJonathan Lemon 	return (error);
191f13ad206SJonathan Lemon }
192f13ad206SJonathan Lemon 
1939a2a57a1SJonathan Lemon static int
1949a2a57a1SJonathan Lemon netkqfilter(dev_t dev, struct knote *kn)
1959a2a57a1SJonathan Lemon {
1969a2a57a1SJonathan Lemon 	struct klist *klist;
1979a2a57a1SJonathan Lemon 	struct ifnet *ifp;
1989a2a57a1SJonathan Lemon 	int idx;
1999a2a57a1SJonathan Lemon 
2009a2a57a1SJonathan Lemon 	idx = minor(dev);
2019a2a57a1SJonathan Lemon 	if (idx == 0) {
2029a2a57a1SJonathan Lemon 		klist = &ifklist;
2039a2a57a1SJonathan Lemon 	} else {
2049a2a57a1SJonathan Lemon 		ifp = ifnet_byindex(idx);
2059a2a57a1SJonathan Lemon 		if (ifp == NULL)
2069a2a57a1SJonathan Lemon 			return (1);
2079a2a57a1SJonathan Lemon 		klist = &ifp->if_klist;
2089a2a57a1SJonathan Lemon 	}
2099a2a57a1SJonathan Lemon 
2109a2a57a1SJonathan Lemon 	switch (kn->kn_filter) {
2119a2a57a1SJonathan Lemon 	case EVFILT_NETDEV:
2129a2a57a1SJonathan Lemon 		kn->kn_fop = &netdev_filtops;
2139a2a57a1SJonathan Lemon 		break;
2149a2a57a1SJonathan Lemon 	default:
2159a2a57a1SJonathan Lemon 		return (1);
2169a2a57a1SJonathan Lemon 	}
2179a2a57a1SJonathan Lemon 
2189a2a57a1SJonathan Lemon 	kn->kn_hook = (caddr_t)klist;
2199a2a57a1SJonathan Lemon 
2209a2a57a1SJonathan Lemon 	/* XXX locking? */
2219a2a57a1SJonathan Lemon 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
2229a2a57a1SJonathan Lemon 
2239a2a57a1SJonathan Lemon 	return (0);
2249a2a57a1SJonathan Lemon }
2259a2a57a1SJonathan Lemon 
2269a2a57a1SJonathan Lemon static void
2279a2a57a1SJonathan Lemon filt_netdetach(struct knote *kn)
2289a2a57a1SJonathan Lemon {
2299a2a57a1SJonathan Lemon 	struct klist *klist = (struct klist *)kn->kn_hook;
2309a2a57a1SJonathan Lemon 
2319a2a57a1SJonathan Lemon 	if (kn->kn_status & KN_DETACHED)
2329a2a57a1SJonathan Lemon 		return;
2339a2a57a1SJonathan Lemon 	SLIST_REMOVE(klist, kn, knote, kn_selnext);
2349a2a57a1SJonathan Lemon }
2359a2a57a1SJonathan Lemon 
2369a2a57a1SJonathan Lemon static int
2379a2a57a1SJonathan Lemon filt_netdev(struct knote *kn, long hint)
2389a2a57a1SJonathan Lemon {
2399a2a57a1SJonathan Lemon 
2409a2a57a1SJonathan Lemon 	/*
2419a2a57a1SJonathan Lemon 	 * Currently NOTE_EXIT is abused to indicate device detach.
2429a2a57a1SJonathan Lemon 	 */
2439a2a57a1SJonathan Lemon 	if (hint == NOTE_EXIT) {
2449a2a57a1SJonathan Lemon 		kn->kn_data = NOTE_LINKINV;
2459a2a57a1SJonathan Lemon                 kn->kn_status |= KN_DETACHED;
2469a2a57a1SJonathan Lemon                 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2479a2a57a1SJonathan Lemon                 return (1);
2489a2a57a1SJonathan Lemon         }
2499a2a57a1SJonathan Lemon 	kn->kn_data = hint;			/* current status */
2509a2a57a1SJonathan Lemon 	if (kn->kn_sfflags & hint)
2519a2a57a1SJonathan Lemon 		kn->kn_fflags |= hint;
2529a2a57a1SJonathan Lemon 	return (kn->kn_fflags != 0);
2539a2a57a1SJonathan Lemon }
2549a2a57a1SJonathan Lemon 
255df8bae1dSRodney W. Grimes /*
256df8bae1dSRodney W. Grimes  * Network interface utility routines.
257df8bae1dSRodney W. Grimes  *
258df8bae1dSRodney W. Grimes  * Routines with ifa_ifwith* names take sockaddr *'s as
259df8bae1dSRodney W. Grimes  * parameters.
260df8bae1dSRodney W. Grimes  */
2612b14f991SJulian Elischer /* ARGSUSED*/
262f9132cebSJonathan Lemon static void
263f9132cebSJonathan Lemon if_init(dummy)
264f9132cebSJonathan Lemon 	void *dummy;
265f9132cebSJonathan Lemon {
266f9132cebSJonathan Lemon 
267b30a244cSJeffrey Hsu 	IFNET_LOCK_INIT();
268f9132cebSJonathan Lemon 	TAILQ_INIT(&ifnet);
269f13ad206SJonathan Lemon 	SLIST_INIT(&ifklist);
270f9132cebSJonathan Lemon 	if_grow();				/* create initial table */
271f13ad206SJonathan Lemon 	ifdev_byindex(0) = make_dev(&net_cdevsw, 0,
272f13ad206SJonathan Lemon 	    UID_ROOT, GID_WHEEL, 0600, "network");
273f9132cebSJonathan Lemon }
274f9132cebSJonathan Lemon 
275f9132cebSJonathan Lemon static void
276f9132cebSJonathan Lemon if_grow(void)
277f9132cebSJonathan Lemon {
278f9132cebSJonathan Lemon 	u_int n;
279f9132cebSJonathan Lemon 	struct ifindex_entry *e;
280f9132cebSJonathan Lemon 
281f9132cebSJonathan Lemon 	if_indexlim <<= 1;
282f9132cebSJonathan Lemon 	n = if_indexlim * sizeof(*e);
283f9132cebSJonathan Lemon 	e = malloc(n, M_IFADDR, M_WAITOK | M_ZERO);
284f9132cebSJonathan Lemon 	if (ifindex_table != NULL) {
285f9132cebSJonathan Lemon 		memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2);
286f9132cebSJonathan Lemon 		free((caddr_t)ifindex_table, M_IFADDR);
287f9132cebSJonathan Lemon 	}
288f9132cebSJonathan Lemon 	ifindex_table = e;
289f9132cebSJonathan Lemon }
290f9132cebSJonathan Lemon 
291f9132cebSJonathan Lemon /* ARGSUSED*/
292f9132cebSJonathan Lemon static void
293f9132cebSJonathan Lemon if_check(dummy)
29427501cb6SBruce Evans 	void *dummy;
295df8bae1dSRodney W. Grimes {
2968ba5bdaeSPeter Wemm 	struct ifnet *ifp;
2978ba5bdaeSPeter Wemm 	int s;
298df8bae1dSRodney W. Grimes 
2998ba5bdaeSPeter Wemm 	s = splimp();
300b30a244cSJeffrey Hsu 	IFNET_RLOCK();	/* could sleep on rare error; mostly okay XXX */
301fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
302e0ea20bcSPoul-Henning Kamp 		if (ifp->if_snd.ifq_maxlen == 0) {
303e0ea20bcSPoul-Henning Kamp 			printf("%s%d XXX: driver didn't set ifq_maxlen\n",
304e0ea20bcSPoul-Henning Kamp 			    ifp->if_name, ifp->if_unit);
305df8bae1dSRodney W. Grimes 			ifp->if_snd.ifq_maxlen = ifqmaxlen;
306e0ea20bcSPoul-Henning Kamp 		}
3075e980e22SJohn Baldwin 		if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) {
308df5e1987SJonathan Lemon 			printf("%s%d XXX: driver didn't initialize queue mtx\n",
309df5e1987SJonathan Lemon 			    ifp->if_name, ifp->if_unit);
3106008862bSJohn Baldwin 			mtx_init(&ifp->if_snd.ifq_mtx, "unknown",
3116008862bSJohn Baldwin 			    MTX_NETWORK_LOCK, MTX_DEF);
312df5e1987SJonathan Lemon 		}
313df5e1987SJonathan Lemon 	}
314b30a244cSJeffrey Hsu 	IFNET_RUNLOCK();
3158ba5bdaeSPeter Wemm 	splx(s);
316df8bae1dSRodney W. Grimes 	if_slowtimo(0);
317df8bae1dSRodney W. Grimes }
318df8bae1dSRodney W. Grimes 
319ffb5a104SJonathan Lemon static int
320ffb5a104SJonathan Lemon if_findindex(struct ifnet *ifp)
321ffb5a104SJonathan Lemon {
322ffb5a104SJonathan Lemon 	int i, unit;
323ffb5a104SJonathan Lemon 	char eaddr[18], devname[32];
324d2b4566aSJonathan Lemon 	const char *name, *p;
325ffb5a104SJonathan Lemon 
326ffb5a104SJonathan Lemon 	switch (ifp->if_type) {
327ffb5a104SJonathan Lemon 	case IFT_ETHER:			/* these types use struct arpcom */
328ffb5a104SJonathan Lemon 	case IFT_FDDI:
329ffb5a104SJonathan Lemon 	case IFT_XETHER:
330ffb5a104SJonathan Lemon 	case IFT_ISO88025:
331ffb5a104SJonathan Lemon 	case IFT_L2VLAN:
332ffb5a104SJonathan Lemon 		snprintf(eaddr, 18, "%6D",
333ffb5a104SJonathan Lemon 		    ((struct arpcom *)ifp->if_softc)->ac_enaddr, ":");
334ffb5a104SJonathan Lemon 		break;
335ffb5a104SJonathan Lemon 	default:
336ffb5a104SJonathan Lemon 		eaddr[0] = '\0';
337ffb5a104SJonathan Lemon 		break;
338ffb5a104SJonathan Lemon 	}
339ffb5a104SJonathan Lemon 	snprintf(devname, 32, "%s%d", ifp->if_name, ifp->if_unit);
340ffb5a104SJonathan Lemon 	name = net_cdevsw.d_name;
341ffb5a104SJonathan Lemon 	i = 0;
342ffb5a104SJonathan Lemon 	while ((resource_find_dev(&i, name, &unit, NULL, NULL)) == 0) {
343ffb5a104SJonathan Lemon 		if (resource_string_value(name, unit, "ether", &p) == 0)
344ffb5a104SJonathan Lemon 			if (strcmp(p, eaddr) == 0)
345ffb5a104SJonathan Lemon 				goto found;
346ffb5a104SJonathan Lemon 		if (resource_string_value(name, unit, "dev", &p) == 0)
347ffb5a104SJonathan Lemon 			if (strcmp(p, devname) == 0)
348ffb5a104SJonathan Lemon 				goto found;
349ffb5a104SJonathan Lemon 	}
350ffb5a104SJonathan Lemon 	unit = 0;
351ffb5a104SJonathan Lemon found:
352ffb5a104SJonathan Lemon 	if (unit != 0) {
353ffb5a104SJonathan Lemon 		if (ifaddr_byindex(unit) == NULL)
354ffb5a104SJonathan Lemon 			return (unit);
355ffb5a104SJonathan Lemon 		printf("%s%d in use, cannot hardwire it to %s.\n",
356ffb5a104SJonathan Lemon 		    name, unit, devname);
357ffb5a104SJonathan Lemon 	}
358ffb5a104SJonathan Lemon 	for (unit = 1; ; unit++) {
35905153c61SBill Fenner 		if (unit <= if_index && ifaddr_byindex(unit) != NULL)
360ffb5a104SJonathan Lemon 			continue;
361ffb5a104SJonathan Lemon 		if (resource_string_value(name, unit, "ether", &p) == 0 ||
362ffb5a104SJonathan Lemon 		    resource_string_value(name, unit, "dev", &p) == 0)
363ffb5a104SJonathan Lemon 			continue;
364ffb5a104SJonathan Lemon 		break;
365ffb5a104SJonathan Lemon 	}
366ffb5a104SJonathan Lemon 	return (unit);
367ffb5a104SJonathan Lemon }
368ffb5a104SJonathan Lemon 
369df8bae1dSRodney W. Grimes /*
370df8bae1dSRodney W. Grimes  * Attach an interface to the
371df8bae1dSRodney W. Grimes  * list of "active" interfaces.
372df8bae1dSRodney W. Grimes  */
373df8bae1dSRodney W. Grimes void
374df8bae1dSRodney W. Grimes if_attach(ifp)
375df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
376df8bae1dSRodney W. Grimes {
377df8bae1dSRodney W. Grimes 	unsigned socksize, ifasize;
3781ce9bf88SPoul-Henning Kamp 	int namelen, masklen;
3791ce9bf88SPoul-Henning Kamp 	char workbuf[64];
380df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
381df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
382df8bae1dSRodney W. Grimes 
383b30a244cSJeffrey Hsu 	IFNET_WLOCK();
38429412182SGarrett Wollman 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
385b30a244cSJeffrey Hsu 	IFNET_WUNLOCK();
38659562606SGarrett Wollman 	/*
38759562606SGarrett Wollman 	 * XXX -
38859562606SGarrett Wollman 	 * The old code would work if the interface passed a pre-existing
38959562606SGarrett Wollman 	 * chain of ifaddrs to this code.  We don't trust our callers to
39059562606SGarrett Wollman 	 * properly initialize the tailq, however, so we no longer allow
39159562606SGarrett Wollman 	 * this unlikely case.
39259562606SGarrett Wollman 	 */
39359562606SGarrett Wollman 	TAILQ_INIT(&ifp->if_addrhead);
39482cd038dSYoshinobu Inoue 	TAILQ_INIT(&ifp->if_prefixhead);
3956817526dSPoul-Henning Kamp 	TAILQ_INIT(&ifp->if_multiaddrs);
3969a2a57a1SJonathan Lemon 	SLIST_INIT(&ifp->if_klist);
39798b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
398e70cd263SRobert Watson 
399e70cd263SRobert Watson #ifdef MAC
400e70cd263SRobert Watson 	mac_init_ifnet(ifp);
401e70cd263SRobert Watson 	mac_create_ifnet(ifp);
402e70cd263SRobert Watson #endif
403e70cd263SRobert Watson 
404ffb5a104SJonathan Lemon 	ifp->if_index = if_findindex(ifp);
40505153c61SBill Fenner 	if (ifp->if_index > if_index)
40605153c61SBill Fenner 		if_index = ifp->if_index;
407f9132cebSJonathan Lemon 	if (if_index >= if_indexlim)
408f9132cebSJonathan Lemon 		if_grow();
40982cd038dSYoshinobu Inoue 
410ffb5a104SJonathan Lemon 	ifnet_byindex(ifp->if_index) = ifp;
411ffb5a104SJonathan Lemon 	ifdev_byindex(ifp->if_index) = make_dev(&net_cdevsw, ifp->if_index,
412ffb5a104SJonathan Lemon 	    UID_ROOT, GID_WHEEL, 0600, "%s/%s%d",
413ffb5a104SJonathan Lemon 	    net_cdevsw.d_name, ifp->if_name, ifp->if_unit);
414ffb5a104SJonathan Lemon 	make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
415ffb5a104SJonathan Lemon 	    net_cdevsw.d_name, ifp->if_index);
416f13ad206SJonathan Lemon 
4176008862bSJohn Baldwin 	mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_name, "if send queue", MTX_DEF);
418df5e1987SJonathan Lemon 
419df8bae1dSRodney W. Grimes 	/*
420df8bae1dSRodney W. Grimes 	 * create a Link Level name for this device
421df8bae1dSRodney W. Grimes 	 */
4222127f260SArchie Cobbs 	namelen = snprintf(workbuf, sizeof(workbuf),
4232127f260SArchie Cobbs 	    "%s%d", ifp->if_name, ifp->if_unit);
424df8bae1dSRodney W. Grimes #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
4251ce9bf88SPoul-Henning Kamp 	masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
426df8bae1dSRodney W. Grimes 	socksize = masklen + ifp->if_addrlen;
427df8bae1dSRodney W. Grimes #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
428df8bae1dSRodney W. Grimes 	if (socksize < sizeof(*sdl))
429df8bae1dSRodney W. Grimes 		socksize = sizeof(*sdl);
4308a261b8fSDoug Rabson 	socksize = ROUNDUP(socksize);
431df8bae1dSRodney W. Grimes 	ifasize = sizeof(*ifa) + 2 * socksize;
4320f43e1aaSSam Leffler 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
4339448326fSPoul-Henning Kamp 	if (ifa) {
43419fc74fbSJeffrey Hsu 		IFA_LOCK_INIT(ifa);
435df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(ifa + 1);
436df8bae1dSRodney W. Grimes 		sdl->sdl_len = socksize;
437df8bae1dSRodney W. Grimes 		sdl->sdl_family = AF_LINK;
4381ce9bf88SPoul-Henning Kamp 		bcopy(workbuf, sdl->sdl_data, namelen);
4391ce9bf88SPoul-Henning Kamp 		sdl->sdl_nlen = namelen;
440df8bae1dSRodney W. Grimes 		sdl->sdl_index = ifp->if_index;
441df8bae1dSRodney W. Grimes 		sdl->sdl_type = ifp->if_type;
442ffb5a104SJonathan Lemon 		ifaddr_byindex(ifp->if_index) = ifa;
443df8bae1dSRodney W. Grimes 		ifa->ifa_ifp = ifp;
444df8bae1dSRodney W. Grimes 		ifa->ifa_rtrequest = link_rtrequest;
445df8bae1dSRodney W. Grimes 		ifa->ifa_addr = (struct sockaddr *)sdl;
446df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
447df8bae1dSRodney W. Grimes 		ifa->ifa_netmask = (struct sockaddr *)sdl;
448df8bae1dSRodney W. Grimes 		sdl->sdl_len = masklen;
449df8bae1dSRodney W. Grimes 		while (namelen != 0)
450df8bae1dSRodney W. Grimes 			sdl->sdl_data[--namelen] = 0xff;
45119fc74fbSJeffrey Hsu 		ifa->ifa_refcnt = 1;
45259562606SGarrett Wollman 		TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
453df8bae1dSRodney W. Grimes 	}
454322dcb8dSMax Khon 	ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
4557b6edd04SRuslan Ermilov 
4567b6edd04SRuslan Ermilov 	/* Announce the interface. */
4577b6edd04SRuslan Ermilov 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
458df8bae1dSRodney W. Grimes }
4596182fdbdSPeter Wemm 
4606182fdbdSPeter Wemm /*
4616182fdbdSPeter Wemm  * Detach an interface, removing it from the
4626182fdbdSPeter Wemm  * list of "active" interfaces.
4636182fdbdSPeter Wemm  */
4646182fdbdSPeter Wemm void
4656182fdbdSPeter Wemm if_detach(ifp)
4666182fdbdSPeter Wemm 	struct ifnet *ifp;
4676182fdbdSPeter Wemm {
4686182fdbdSPeter Wemm 	struct ifaddr *ifa;
4695500d3beSWarner Losh 	struct radix_node_head	*rnh;
4705500d3beSWarner Losh 	int s;
4715500d3beSWarner Losh 	int i;
4726182fdbdSPeter Wemm 
4736182fdbdSPeter Wemm 	/*
4746182fdbdSPeter Wemm 	 * Remove routes and flush queues.
4756182fdbdSPeter Wemm 	 */
4765500d3beSWarner Losh 	s = splnet();
4776182fdbdSPeter Wemm 	if_down(ifp);
4786182fdbdSPeter Wemm 
4796182fdbdSPeter Wemm 	/*
480f9132cebSJonathan Lemon 	 * Remove address from ifindex_table[] and maybe decrement if_index.
4816182fdbdSPeter Wemm 	 * Clean up all addresses.
4826182fdbdSPeter Wemm 	 */
483f9132cebSJonathan Lemon 	ifaddr_byindex(ifp->if_index) = NULL;
4840346e973SBrian Feldman 	revoke_and_destroy_dev(ifdev_byindex(ifp->if_index));
485f13ad206SJonathan Lemon 	ifdev_byindex(ifp->if_index) = NULL;
486f9132cebSJonathan Lemon 
487f9132cebSJonathan Lemon 	while (if_index > 0 && ifaddr_byindex(if_index) == NULL)
4886182fdbdSPeter Wemm 		if_index--;
4896182fdbdSPeter Wemm 
4906182fdbdSPeter Wemm 	for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
4916182fdbdSPeter Wemm 	     ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
4920d0f9d1eSYoshinobu Inoue #ifdef INET
493aa6be122SWarner Losh 		/* XXX: Ugly!! ad hoc just for INET */
494aa6be122SWarner Losh 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
495aa6be122SWarner Losh 			struct ifaliasreq ifr;
496aa6be122SWarner Losh 
497aa6be122SWarner Losh 			bzero(&ifr, sizeof(ifr));
498aa6be122SWarner Losh 			ifr.ifra_addr = *ifa->ifa_addr;
499aa6be122SWarner Losh 			if (ifa->ifa_dstaddr)
500aa6be122SWarner Losh 				ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
501aa6be122SWarner Losh 			if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
502aa6be122SWarner Losh 			    NULL) == 0)
503aa6be122SWarner Losh 				continue;
504aa6be122SWarner Losh 		}
5050d0f9d1eSYoshinobu Inoue #endif /* INET */
5060d0f9d1eSYoshinobu Inoue #ifdef INET6
5070d0f9d1eSYoshinobu Inoue 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
50833841545SHajimu UMEMOTO 			in6_purgeaddr(ifa);
509978ee2edSJun-ichiro itojun Hagino 			/* ifp_addrhead is already updated */
5100d0f9d1eSYoshinobu Inoue 			continue;
5110d0f9d1eSYoshinobu Inoue 		}
5120d0f9d1eSYoshinobu Inoue #endif /* INET6 */
5136182fdbdSPeter Wemm 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
5146182fdbdSPeter Wemm 		IFAFREE(ifa);
5156182fdbdSPeter Wemm 	}
5166182fdbdSPeter Wemm 
51733841545SHajimu UMEMOTO #ifdef INET6
51833841545SHajimu UMEMOTO 	/*
51933841545SHajimu UMEMOTO 	 * Remove all IPv6 kernel structs related to ifp.  This should be done
52033841545SHajimu UMEMOTO 	 * before removing routing entries below, since IPv6 interface direct
52133841545SHajimu UMEMOTO 	 * routes are expected to be removed by the IPv6-specific kernel API.
52233841545SHajimu UMEMOTO 	 * Otherwise, the kernel will detect some inconsistency and bark it.
52333841545SHajimu UMEMOTO 	 */
52433841545SHajimu UMEMOTO 	in6_ifdetach(ifp);
52533841545SHajimu UMEMOTO #endif
52633841545SHajimu UMEMOTO 
5275500d3beSWarner Losh 	/*
5285500d3beSWarner Losh 	 * Delete all remaining routes using this interface
5295500d3beSWarner Losh 	 * Unfortuneatly the only way to do this is to slog through
5305500d3beSWarner Losh 	 * the entire routing table looking for routes which point
5315500d3beSWarner Losh 	 * to this interface...oh well...
5325500d3beSWarner Losh 	 */
5335500d3beSWarner Losh 	for (i = 1; i <= AF_MAX; i++) {
5345500d3beSWarner Losh 		if ((rnh = rt_tables[i]) == NULL)
5355500d3beSWarner Losh 			continue;
5365500d3beSWarner Losh 		(void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
5375500d3beSWarner Losh 	}
5385500d3beSWarner Losh 
5397b6edd04SRuslan Ermilov 	/* Announce that the interface is gone. */
5407b6edd04SRuslan Ermilov 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
5417b6edd04SRuslan Ermilov 
542e70cd263SRobert Watson #ifdef MAC
543e70cd263SRobert Watson 	mac_destroy_ifnet(ifp);
544e70cd263SRobert Watson #endif /* MAC */
5459a2a57a1SJonathan Lemon 	KNOTE(&ifp->if_klist, NOTE_EXIT);
546b30a244cSJeffrey Hsu 	IFNET_WLOCK();
5476182fdbdSPeter Wemm 	TAILQ_REMOVE(&ifnet, ifp, if_link);
548b30a244cSJeffrey Hsu 	IFNET_WUNLOCK();
549df5e1987SJonathan Lemon 	mtx_destroy(&ifp->if_snd.ifq_mtx);
5505500d3beSWarner Losh 	splx(s);
5515500d3beSWarner Losh }
5525500d3beSWarner Losh 
5535500d3beSWarner Losh /*
5545500d3beSWarner Losh  * Delete Routes for a Network Interface
5555500d3beSWarner Losh  *
5565500d3beSWarner Losh  * Called for each routing entry via the rnh->rnh_walktree() call above
5575500d3beSWarner Losh  * to delete all route entries referencing a detaching network interface.
5585500d3beSWarner Losh  *
5595500d3beSWarner Losh  * Arguments:
5605500d3beSWarner Losh  *	rn	pointer to node in the routing table
5615500d3beSWarner Losh  *	arg	argument passed to rnh->rnh_walktree() - detaching interface
5625500d3beSWarner Losh  *
5635500d3beSWarner Losh  * Returns:
5645500d3beSWarner Losh  *	0	successful
5655500d3beSWarner Losh  *	errno	failed - reason indicated
5665500d3beSWarner Losh  *
5675500d3beSWarner Losh  */
5685500d3beSWarner Losh static int
5695500d3beSWarner Losh if_rtdel(rn, arg)
5705500d3beSWarner Losh 	struct radix_node	*rn;
5715500d3beSWarner Losh 	void			*arg;
5725500d3beSWarner Losh {
5735500d3beSWarner Losh 	struct rtentry	*rt = (struct rtentry *)rn;
5745500d3beSWarner Losh 	struct ifnet	*ifp = arg;
5755500d3beSWarner Losh 	int		err;
5765500d3beSWarner Losh 
5775500d3beSWarner Losh 	if (rt->rt_ifp == ifp) {
5785500d3beSWarner Losh 
5795500d3beSWarner Losh 		/*
5805500d3beSWarner Losh 		 * Protect (sorta) against walktree recursion problems
5815500d3beSWarner Losh 		 * with cloned routes
5825500d3beSWarner Losh 		 */
5835500d3beSWarner Losh 		if ((rt->rt_flags & RTF_UP) == 0)
5845500d3beSWarner Losh 			return (0);
5855500d3beSWarner Losh 
5865500d3beSWarner Losh 		err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
5875500d3beSWarner Losh 				rt_mask(rt), rt->rt_flags,
5885500d3beSWarner Losh 				(struct rtentry **) NULL);
5895500d3beSWarner Losh 		if (err) {
5905500d3beSWarner Losh 			log(LOG_WARNING, "if_rtdel: error %d\n", err);
5915500d3beSWarner Losh 		}
5925500d3beSWarner Losh 	}
5935500d3beSWarner Losh 
5945500d3beSWarner Losh 	return (0);
5956182fdbdSPeter Wemm }
5966182fdbdSPeter Wemm 
597df8bae1dSRodney W. Grimes /*
59830aad87dSBrooks Davis  * Create a clone network interface.
59930aad87dSBrooks Davis  */
60030aad87dSBrooks Davis int
60130aad87dSBrooks Davis if_clone_create(name, len)
60230aad87dSBrooks Davis 	char *name;
60330aad87dSBrooks Davis 	int len;
60430aad87dSBrooks Davis {
60530aad87dSBrooks Davis 	struct if_clone *ifc;
60630aad87dSBrooks Davis 	char *dp;
6073b16e7b2SMaxime Henrion 	int wildcard, bytoff, bitoff;
60830aad87dSBrooks Davis 	int unit;
60930aad87dSBrooks Davis 	int err;
61030aad87dSBrooks Davis 
61130aad87dSBrooks Davis 	ifc = if_clone_lookup(name, &unit);
61230aad87dSBrooks Davis 	if (ifc == NULL)
61330aad87dSBrooks Davis 		return (EINVAL);
61430aad87dSBrooks Davis 
61530aad87dSBrooks Davis 	if (ifunit(name) != NULL)
61630aad87dSBrooks Davis 		return (EEXIST);
61730aad87dSBrooks Davis 
6183b16e7b2SMaxime Henrion 	bytoff = bitoff = 0;
61930aad87dSBrooks Davis 	wildcard = (unit < 0);
6203b16e7b2SMaxime Henrion 	/*
6213b16e7b2SMaxime Henrion 	 * Find a free unit if none was given.
6223b16e7b2SMaxime Henrion 	 */
6233b16e7b2SMaxime Henrion 	if (wildcard) {
6243b16e7b2SMaxime Henrion 		while ((bytoff < ifc->ifc_bmlen)
6253b16e7b2SMaxime Henrion 		    && (ifc->ifc_units[bytoff] == 0xff))
6263b16e7b2SMaxime Henrion 			bytoff++;
6273b16e7b2SMaxime Henrion 		if (bytoff >= ifc->ifc_bmlen)
6283b16e7b2SMaxime Henrion 			return (ENOSPC);
6293b16e7b2SMaxime Henrion 		while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
6303b16e7b2SMaxime Henrion 			bitoff++;
6313b16e7b2SMaxime Henrion 		unit = (bytoff << 3) + bitoff;
6323b16e7b2SMaxime Henrion 	}
63330aad87dSBrooks Davis 
6343b16e7b2SMaxime Henrion 	if (unit > ifc->ifc_maxunit)
6353b16e7b2SMaxime Henrion 		return (ENXIO);
6363b16e7b2SMaxime Henrion 
6373b16e7b2SMaxime Henrion 	err = (*ifc->ifc_create)(ifc, unit);
63830aad87dSBrooks Davis 	if (err != 0)
63930aad87dSBrooks Davis 		return (err);
64030aad87dSBrooks Davis 
6413b16e7b2SMaxime Henrion 	if (!wildcard) {
6423b16e7b2SMaxime Henrion 		bytoff = unit >> 3;
6433b16e7b2SMaxime Henrion 		bitoff = unit - (bytoff << 3);
6443b16e7b2SMaxime Henrion 	}
6453b16e7b2SMaxime Henrion 
6463b16e7b2SMaxime Henrion 	/*
6473b16e7b2SMaxime Henrion 	 * Allocate the unit in the bitmap.
6483b16e7b2SMaxime Henrion 	 */
6493b16e7b2SMaxime Henrion 	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
6503b16e7b2SMaxime Henrion 	    ("%s: bit is already set", __func__));
6513b16e7b2SMaxime Henrion 	ifc->ifc_units[bytoff] |= (1 << bitoff);
6523b16e7b2SMaxime Henrion 
65330aad87dSBrooks Davis 	/* In the wildcard case, we need to update the name. */
65430aad87dSBrooks Davis 	if (wildcard) {
65530aad87dSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
65630aad87dSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
65730aad87dSBrooks Davis 		    len - (dp-name) - 1) {
65830aad87dSBrooks Davis 			/*
65930aad87dSBrooks Davis 			 * This can only be a programmer error and
66030aad87dSBrooks Davis 			 * there's no straightforward way to recover if
66130aad87dSBrooks Davis 			 * it happens.
66230aad87dSBrooks Davis 			 */
66330aad87dSBrooks Davis 			panic("if_clone_create(): interface name too long");
66430aad87dSBrooks Davis 		}
66530aad87dSBrooks Davis 
66630aad87dSBrooks Davis 	}
66730aad87dSBrooks Davis 
66830aad87dSBrooks Davis 	return (0);
66930aad87dSBrooks Davis }
67030aad87dSBrooks Davis 
67130aad87dSBrooks Davis /*
67230aad87dSBrooks Davis  * Destroy a clone network interface.
67330aad87dSBrooks Davis  */
67430aad87dSBrooks Davis int
67530aad87dSBrooks Davis if_clone_destroy(name)
67630aad87dSBrooks Davis 	const char *name;
67730aad87dSBrooks Davis {
67830aad87dSBrooks Davis 	struct if_clone *ifc;
67930aad87dSBrooks Davis 	struct ifnet *ifp;
6803b16e7b2SMaxime Henrion 	int bytoff, bitoff;
681ae5a19beSBrooks Davis 	int unit;
68230aad87dSBrooks Davis 
6833b16e7b2SMaxime Henrion 	ifc = if_clone_lookup(name, &unit);
68430aad87dSBrooks Davis 	if (ifc == NULL)
68530aad87dSBrooks Davis 		return (EINVAL);
68630aad87dSBrooks Davis 
687ae5a19beSBrooks Davis 	if (unit < ifc->ifc_minifs)
688ae5a19beSBrooks Davis 		return (EINVAL);
689ae5a19beSBrooks Davis 
69030aad87dSBrooks Davis 	ifp = ifunit(name);
69130aad87dSBrooks Davis 	if (ifp == NULL)
69230aad87dSBrooks Davis 		return (ENXIO);
69330aad87dSBrooks Davis 
69430aad87dSBrooks Davis 	if (ifc->ifc_destroy == NULL)
69530aad87dSBrooks Davis 		return (EOPNOTSUPP);
69630aad87dSBrooks Davis 
697ae5a19beSBrooks Davis 	(*ifc->ifc_destroy)(ifp);
6983b16e7b2SMaxime Henrion 
6993b16e7b2SMaxime Henrion 	/*
7003b16e7b2SMaxime Henrion 	 * Compute offset in the bitmap and deallocate the unit.
7013b16e7b2SMaxime Henrion 	 */
7023b16e7b2SMaxime Henrion 	bytoff = unit >> 3;
7033b16e7b2SMaxime Henrion 	bitoff = unit - (bytoff << 3);
7043b16e7b2SMaxime Henrion 	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
7053b16e7b2SMaxime Henrion 	    ("%s: bit is already cleared", __func__));
7063b16e7b2SMaxime Henrion 	ifc->ifc_units[bytoff] &= ~(1 << bitoff);
7073b16e7b2SMaxime Henrion 	return (0);
70830aad87dSBrooks Davis }
70930aad87dSBrooks Davis 
71030aad87dSBrooks Davis /*
71130aad87dSBrooks Davis  * Look up a network interface cloner.
71230aad87dSBrooks Davis  */
7130b59d917SJonathan Lemon static struct if_clone *
71430aad87dSBrooks Davis if_clone_lookup(name, unitp)
71530aad87dSBrooks Davis 	const char *name;
71630aad87dSBrooks Davis 	int *unitp;
71730aad87dSBrooks Davis {
71830aad87dSBrooks Davis 	struct if_clone *ifc;
71930aad87dSBrooks Davis 	const char *cp;
72030aad87dSBrooks Davis 	int i;
72130aad87dSBrooks Davis 
72230aad87dSBrooks Davis 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
72330aad87dSBrooks Davis 		for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
72430aad87dSBrooks Davis 			if (ifc->ifc_name[i] != *cp)
72530aad87dSBrooks Davis 				goto next_ifc;
72630aad87dSBrooks Davis 		}
72730aad87dSBrooks Davis 		goto found_name;
72830aad87dSBrooks Davis  next_ifc:
72930aad87dSBrooks Davis 		ifc = LIST_NEXT(ifc, ifc_list);
73030aad87dSBrooks Davis 	}
73130aad87dSBrooks Davis 
73230aad87dSBrooks Davis 	/* No match. */
73330aad87dSBrooks Davis 	return ((struct if_clone *)NULL);
73430aad87dSBrooks Davis 
73530aad87dSBrooks Davis  found_name:
73630aad87dSBrooks Davis 	if (*cp == '\0') {
73730aad87dSBrooks Davis 		i = -1;
73830aad87dSBrooks Davis 	} else {
73930aad87dSBrooks Davis 		for (i = 0; *cp != '\0'; cp++) {
74030aad87dSBrooks Davis 			if (*cp < '0' || *cp > '9') {
74130aad87dSBrooks Davis 				/* Bogus unit number. */
74230aad87dSBrooks Davis 				return (NULL);
74330aad87dSBrooks Davis 			}
74430aad87dSBrooks Davis 			i = (i * 10) + (*cp - '0');
74530aad87dSBrooks Davis 		}
74630aad87dSBrooks Davis 	}
74730aad87dSBrooks Davis 
74830aad87dSBrooks Davis 	if (unitp != NULL)
74930aad87dSBrooks Davis 		*unitp = i;
75030aad87dSBrooks Davis 	return (ifc);
75130aad87dSBrooks Davis }
75230aad87dSBrooks Davis 
75330aad87dSBrooks Davis /*
75430aad87dSBrooks Davis  * Register a network interface cloner.
75530aad87dSBrooks Davis  */
75630aad87dSBrooks Davis void
75730aad87dSBrooks Davis if_clone_attach(ifc)
75830aad87dSBrooks Davis 	struct if_clone *ifc;
75930aad87dSBrooks Davis {
760ae5a19beSBrooks Davis 	int bytoff, bitoff;
761ae5a19beSBrooks Davis 	int err;
7623b16e7b2SMaxime Henrion 	int len, maxclone;
763ae5a19beSBrooks Davis 	int unit;
76430aad87dSBrooks Davis 
765ae5a19beSBrooks Davis 	KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
766ae5a19beSBrooks Davis 	    ("%s: %s requested more units then allowed (%d > %d)",
767ae5a19beSBrooks Davis 	    __func__, ifc->ifc_name, ifc->ifc_minifs,
768ae5a19beSBrooks Davis 	    ifc->ifc_maxunit + 1));
7693b16e7b2SMaxime Henrion 	/*
7703b16e7b2SMaxime Henrion 	 * Compute bitmap size and allocate it.
7713b16e7b2SMaxime Henrion 	 */
7723b16e7b2SMaxime Henrion 	maxclone = ifc->ifc_maxunit + 1;
7733b16e7b2SMaxime Henrion 	len = maxclone >> 3;
7743b16e7b2SMaxime Henrion 	if ((len << 3) < maxclone)
7753b16e7b2SMaxime Henrion 		len++;
7763b16e7b2SMaxime Henrion 	ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO);
7773b16e7b2SMaxime Henrion 	ifc->ifc_bmlen = len;
778ae5a19beSBrooks Davis 
77930aad87dSBrooks Davis 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
78030aad87dSBrooks Davis 	if_cloners_count++;
781ae5a19beSBrooks Davis 
782ae5a19beSBrooks Davis 	for (unit = 0; unit < ifc->ifc_minifs; unit++) {
783ae5a19beSBrooks Davis 		err = (*ifc->ifc_create)(ifc, unit);
784ae5a19beSBrooks Davis 		KASSERT(err == 0,
785ae5a19beSBrooks Davis 		    ("%s: failed to create required interface %s%d",
786ae5a19beSBrooks Davis 		    __func__, ifc->ifc_name, unit));
787ae5a19beSBrooks Davis 
788ae5a19beSBrooks Davis 		/* Allocate the unit in the bitmap. */
789ae5a19beSBrooks Davis 		bytoff = unit >> 3;
790ae5a19beSBrooks Davis 		bitoff = unit - (bytoff << 3);
791ae5a19beSBrooks Davis 		ifc->ifc_units[bytoff] |= (1 << bitoff);
792ae5a19beSBrooks Davis 	}
79330aad87dSBrooks Davis }
79430aad87dSBrooks Davis 
79530aad87dSBrooks Davis /*
79630aad87dSBrooks Davis  * Unregister a network interface cloner.
79730aad87dSBrooks Davis  */
79830aad87dSBrooks Davis void
79930aad87dSBrooks Davis if_clone_detach(ifc)
80030aad87dSBrooks Davis 	struct if_clone *ifc;
80130aad87dSBrooks Davis {
80230aad87dSBrooks Davis 
80330aad87dSBrooks Davis 	LIST_REMOVE(ifc, ifc_list);
8043b16e7b2SMaxime Henrion 	free(ifc->ifc_units, M_CLONE);
80530aad87dSBrooks Davis 	if_cloners_count--;
80630aad87dSBrooks Davis }
80730aad87dSBrooks Davis 
80830aad87dSBrooks Davis /*
80930aad87dSBrooks Davis  * Provide list of interface cloners to userspace.
81030aad87dSBrooks Davis  */
8110b59d917SJonathan Lemon static int
81230aad87dSBrooks Davis if_clone_list(ifcr)
81330aad87dSBrooks Davis 	struct if_clonereq *ifcr;
81430aad87dSBrooks Davis {
81530aad87dSBrooks Davis 	char outbuf[IFNAMSIZ], *dst;
81630aad87dSBrooks Davis 	struct if_clone *ifc;
81730aad87dSBrooks Davis 	int count, error = 0;
81830aad87dSBrooks Davis 
81930aad87dSBrooks Davis 	ifcr->ifcr_total = if_cloners_count;
82030aad87dSBrooks Davis 	if ((dst = ifcr->ifcr_buffer) == NULL) {
82130aad87dSBrooks Davis 		/* Just asking how many there are. */
82230aad87dSBrooks Davis 		return (0);
82330aad87dSBrooks Davis 	}
82430aad87dSBrooks Davis 
82530aad87dSBrooks Davis 	if (ifcr->ifcr_count < 0)
82630aad87dSBrooks Davis 		return (EINVAL);
82730aad87dSBrooks Davis 
82830aad87dSBrooks Davis 	count = (if_cloners_count < ifcr->ifcr_count) ?
82930aad87dSBrooks Davis 	    if_cloners_count : ifcr->ifcr_count;
83030aad87dSBrooks Davis 
83130aad87dSBrooks Davis 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
83230aad87dSBrooks Davis 	     ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
83330aad87dSBrooks Davis 		strncpy(outbuf, ifc->ifc_name, IFNAMSIZ);
83430aad87dSBrooks Davis 		outbuf[IFNAMSIZ - 1] = '\0';	/* sanity */
83530aad87dSBrooks Davis 		error = copyout(outbuf, dst, IFNAMSIZ);
83630aad87dSBrooks Davis 		if (error)
83730aad87dSBrooks Davis 			break;
83830aad87dSBrooks Davis 	}
83930aad87dSBrooks Davis 
84030aad87dSBrooks Davis 	return (error);
84130aad87dSBrooks Davis }
84230aad87dSBrooks Davis 
84319fc74fbSJeffrey Hsu #define	equal(a1, a2) \
84419fc74fbSJeffrey Hsu   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
84519fc74fbSJeffrey Hsu 
84630aad87dSBrooks Davis /*
847df8bae1dSRodney W. Grimes  * Locate an interface based on a complete address.
848df8bae1dSRodney W. Grimes  */
849df8bae1dSRodney W. Grimes /*ARGSUSED*/
850df8bae1dSRodney W. Grimes struct ifaddr *
851df8bae1dSRodney W. Grimes ifa_ifwithaddr(addr)
8520b59d917SJonathan Lemon 	struct sockaddr *addr;
853df8bae1dSRodney W. Grimes {
8540b59d917SJonathan Lemon 	struct ifnet *ifp;
8550b59d917SJonathan Lemon 	struct ifaddr *ifa;
856df8bae1dSRodney W. Grimes 
857b30a244cSJeffrey Hsu 	IFNET_RLOCK();
858fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(ifp, &ifnet, if_link)
85937d40066SPoul-Henning Kamp 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
860df8bae1dSRodney W. Grimes 			if (ifa->ifa_addr->sa_family != addr->sa_family)
861df8bae1dSRodney W. Grimes 				continue;
862df8bae1dSRodney W. Grimes 			if (equal(addr, ifa->ifa_addr))
8630b59d917SJonathan Lemon 				goto done;
86482cd038dSYoshinobu Inoue 			/* IP6 doesn't have broadcast */
8650b59d917SJonathan Lemon 			if ((ifp->if_flags & IFF_BROADCAST) &&
8660b59d917SJonathan Lemon 			    ifa->ifa_broadaddr &&
86782cd038dSYoshinobu Inoue 			    ifa->ifa_broadaddr->sa_len != 0 &&
868df8bae1dSRodney W. Grimes 			    equal(ifa->ifa_broadaddr, addr))
8690b59d917SJonathan Lemon 				goto done;
8700b59d917SJonathan Lemon 		}
8710b59d917SJonathan Lemon 	ifa = NULL;
8720b59d917SJonathan Lemon done:
873b30a244cSJeffrey Hsu 	IFNET_RUNLOCK();
874df8bae1dSRodney W. Grimes 	return (ifa);
875df8bae1dSRodney W. Grimes }
8760b59d917SJonathan Lemon 
877df8bae1dSRodney W. Grimes /*
878df8bae1dSRodney W. Grimes  * Locate the point to point interface with a given destination address.
879df8bae1dSRodney W. Grimes  */
880df8bae1dSRodney W. Grimes /*ARGSUSED*/
881df8bae1dSRodney W. Grimes struct ifaddr *
882df8bae1dSRodney W. Grimes ifa_ifwithdstaddr(addr)
8830b59d917SJonathan Lemon 	struct sockaddr *addr;
884df8bae1dSRodney W. Grimes {
8850b59d917SJonathan Lemon 	struct ifnet *ifp;
8860b59d917SJonathan Lemon 	struct ifaddr *ifa;
887df8bae1dSRodney W. Grimes 
888b30a244cSJeffrey Hsu 	IFNET_RLOCK();
8890b59d917SJonathan Lemon 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
8900b59d917SJonathan Lemon 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
8910b59d917SJonathan Lemon 			continue;
89237d40066SPoul-Henning Kamp 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
893df8bae1dSRodney W. Grimes 			if (ifa->ifa_addr->sa_family != addr->sa_family)
894df8bae1dSRodney W. Grimes 				continue;
89555088a1cSDavid Greenman 			if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
8960b59d917SJonathan Lemon 				goto done;
897df8bae1dSRodney W. Grimes 		}
8980b59d917SJonathan Lemon 	}
8990b59d917SJonathan Lemon 	ifa = NULL;
9000b59d917SJonathan Lemon done:
901b30a244cSJeffrey Hsu 	IFNET_RUNLOCK();
9020b59d917SJonathan Lemon 	return (ifa);
903df8bae1dSRodney W. Grimes }
904df8bae1dSRodney W. Grimes 
905df8bae1dSRodney W. Grimes /*
906df8bae1dSRodney W. Grimes  * Find an interface on a specific network.  If many, choice
907df8bae1dSRodney W. Grimes  * is most specific found.
908df8bae1dSRodney W. Grimes  */
909df8bae1dSRodney W. Grimes struct ifaddr *
910df8bae1dSRodney W. Grimes ifa_ifwithnet(addr)
911df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
912df8bae1dSRodney W. Grimes {
913df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
914df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
915df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
916df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
917df8bae1dSRodney W. Grimes 	char *addr_data = addr->sa_data, *cplim;
918df8bae1dSRodney W. Grimes 
9197e2a6151SJulian Elischer 	/*
9207e2a6151SJulian Elischer 	 * AF_LINK addresses can be looked up directly by their index number,
9217e2a6151SJulian Elischer 	 * so do that if we can.
9227e2a6151SJulian Elischer 	 */
923df8bae1dSRodney W. Grimes 	if (af == AF_LINK) {
924df8bae1dSRodney W. Grimes 	    register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
925df8bae1dSRodney W. Grimes 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
926f9132cebSJonathan Lemon 		return (ifaddr_byindex(sdl->sdl_index));
927df8bae1dSRodney W. Grimes 	}
9287e2a6151SJulian Elischer 
9297e2a6151SJulian Elischer 	/*
9307e2a6151SJulian Elischer 	 * Scan though each interface, looking for ones that have
9317e2a6151SJulian Elischer 	 * addresses in this address family.
9327e2a6151SJulian Elischer 	 */
933b30a244cSJeffrey Hsu 	IFNET_RLOCK();
934fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
93537d40066SPoul-Henning Kamp 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
936df8bae1dSRodney W. Grimes 			register char *cp, *cp2, *cp3;
937df8bae1dSRodney W. Grimes 
938523a02aaSDavid Greenman 			if (ifa->ifa_addr->sa_family != af)
939df8bae1dSRodney W. Grimes next:				continue;
940c61cd599SHajimu UMEMOTO 			if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
9417e2a6151SJulian Elischer 				/*
9427e2a6151SJulian Elischer 				 * This is a bit broken as it doesn't
9437e2a6151SJulian Elischer 				 * take into account that the remote end may
9447e2a6151SJulian Elischer 				 * be a single node in the network we are
9457e2a6151SJulian Elischer 				 * looking for.
9467e2a6151SJulian Elischer 				 * The trouble is that we don't know the
9477e2a6151SJulian Elischer 				 * netmask for the remote end.
9487e2a6151SJulian Elischer 				 */
949fcd6781aSGarrett Wollman 				if (ifa->ifa_dstaddr != 0
950fcd6781aSGarrett Wollman 				    && equal(addr, ifa->ifa_dstaddr))
9510b59d917SJonathan Lemon 					goto done;
9523740e2adSDavid Greenman 			} else {
9537e2a6151SJulian Elischer 				/*
9547ed8f465SJulian Elischer 				 * if we have a special address handler,
9557ed8f465SJulian Elischer 				 * then use it instead of the generic one.
9567ed8f465SJulian Elischer 				 */
9577ed8f465SJulian Elischer 	          		if (ifa->ifa_claim_addr) {
9580b59d917SJonathan Lemon 					if ((*ifa->ifa_claim_addr)(ifa, addr))
9590b59d917SJonathan Lemon 						goto done;
9607ed8f465SJulian Elischer 					continue;
9617ed8f465SJulian Elischer 				}
9627ed8f465SJulian Elischer 
9637ed8f465SJulian Elischer 				/*
9647e2a6151SJulian Elischer 				 * Scan all the bits in the ifa's address.
9657e2a6151SJulian Elischer 				 * If a bit dissagrees with what we are
9667e2a6151SJulian Elischer 				 * looking for, mask it with the netmask
9677e2a6151SJulian Elischer 				 * to see if it really matters.
9687e2a6151SJulian Elischer 				 * (A byte at a time)
9697e2a6151SJulian Elischer 				 */
970523a02aaSDavid Greenman 				if (ifa->ifa_netmask == 0)
971523a02aaSDavid Greenman 					continue;
972df8bae1dSRodney W. Grimes 				cp = addr_data;
973df8bae1dSRodney W. Grimes 				cp2 = ifa->ifa_addr->sa_data;
974df8bae1dSRodney W. Grimes 				cp3 = ifa->ifa_netmask->sa_data;
9757e2a6151SJulian Elischer 				cplim = ifa->ifa_netmask->sa_len
9767e2a6151SJulian Elischer 					+ (char *)ifa->ifa_netmask;
977df8bae1dSRodney W. Grimes 				while (cp3 < cplim)
978df8bae1dSRodney W. Grimes 					if ((*cp++ ^ *cp2++) & *cp3++)
9797e2a6151SJulian Elischer 						goto next; /* next address! */
9807e2a6151SJulian Elischer 				/*
9817e2a6151SJulian Elischer 				 * If the netmask of what we just found
9827e2a6151SJulian Elischer 				 * is more specific than what we had before
9837e2a6151SJulian Elischer 				 * (if we had one) then remember the new one
9847e2a6151SJulian Elischer 				 * before continuing to search
9857e2a6151SJulian Elischer 				 * for an even better one.
9867e2a6151SJulian Elischer 				 */
987df8bae1dSRodney W. Grimes 				if (ifa_maybe == 0 ||
988df8bae1dSRodney W. Grimes 				    rn_refines((caddr_t)ifa->ifa_netmask,
989df8bae1dSRodney W. Grimes 				    (caddr_t)ifa_maybe->ifa_netmask))
990df8bae1dSRodney W. Grimes 					ifa_maybe = ifa;
991df8bae1dSRodney W. Grimes 			}
992b2af64fdSDavid Greenman 		}
993b2af64fdSDavid Greenman 	}
9940b59d917SJonathan Lemon 	ifa = ifa_maybe;
9950b59d917SJonathan Lemon done:
996b30a244cSJeffrey Hsu 	IFNET_RUNLOCK();
9970b59d917SJonathan Lemon 	return (ifa);
998df8bae1dSRodney W. Grimes }
999df8bae1dSRodney W. Grimes 
1000df8bae1dSRodney W. Grimes /*
1001df8bae1dSRodney W. Grimes  * Find an interface address specific to an interface best matching
1002df8bae1dSRodney W. Grimes  * a given address.
1003df8bae1dSRodney W. Grimes  */
1004df8bae1dSRodney W. Grimes struct ifaddr *
1005df8bae1dSRodney W. Grimes ifaof_ifpforaddr(addr, ifp)
1006df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
1007df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
1008df8bae1dSRodney W. Grimes {
1009df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
1010df8bae1dSRodney W. Grimes 	register char *cp, *cp2, *cp3;
1011df8bae1dSRodney W. Grimes 	register char *cplim;
1012df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = 0;
1013df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
1014df8bae1dSRodney W. Grimes 
1015df8bae1dSRodney W. Grimes 	if (af >= AF_MAX)
1016df8bae1dSRodney W. Grimes 		return (0);
101737d40066SPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1018df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != af)
1019df8bae1dSRodney W. Grimes 			continue;
1020381dd1d2SJulian Elischer 		if (ifa_maybe == 0)
1021df8bae1dSRodney W. Grimes 			ifa_maybe = ifa;
1022df8bae1dSRodney W. Grimes 		if (ifa->ifa_netmask == 0) {
1023df8bae1dSRodney W. Grimes 			if (equal(addr, ifa->ifa_addr) ||
1024df8bae1dSRodney W. Grimes 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
10252defe5cdSJonathan Lemon 				goto done;
1026df8bae1dSRodney W. Grimes 			continue;
1027df8bae1dSRodney W. Grimes 		}
1028b2af64fdSDavid Greenman 		if (ifp->if_flags & IFF_POINTOPOINT) {
1029b2af64fdSDavid Greenman 			if (equal(addr, ifa->ifa_dstaddr))
1030a8637146SJonathan Lemon 				goto done;
10313740e2adSDavid Greenman 		} else {
1032df8bae1dSRodney W. Grimes 			cp = addr->sa_data;
1033df8bae1dSRodney W. Grimes 			cp2 = ifa->ifa_addr->sa_data;
1034df8bae1dSRodney W. Grimes 			cp3 = ifa->ifa_netmask->sa_data;
1035df8bae1dSRodney W. Grimes 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1036df8bae1dSRodney W. Grimes 			for (; cp3 < cplim; cp3++)
1037df8bae1dSRodney W. Grimes 				if ((*cp++ ^ *cp2++) & *cp3)
1038df8bae1dSRodney W. Grimes 					break;
1039df8bae1dSRodney W. Grimes 			if (cp3 == cplim)
10402defe5cdSJonathan Lemon 				goto done;
1041df8bae1dSRodney W. Grimes 		}
1042b2af64fdSDavid Greenman 	}
1043f9132cebSJonathan Lemon 	ifa = ifa_maybe;
1044f9132cebSJonathan Lemon done:
1045f9132cebSJonathan Lemon 	return (ifa);
1046df8bae1dSRodney W. Grimes }
1047df8bae1dSRodney W. Grimes 
1048df8bae1dSRodney W. Grimes #include <net/route.h>
1049df8bae1dSRodney W. Grimes 
1050df8bae1dSRodney W. Grimes /*
1051df8bae1dSRodney W. Grimes  * Default action when installing a route with a Link Level gateway.
1052df8bae1dSRodney W. Grimes  * Lookup an appropriate real ifa to point to.
1053df8bae1dSRodney W. Grimes  * This should be moved to /sys/net/link.c eventually.
1054df8bae1dSRodney W. Grimes  */
10553bda9f9bSPoul-Henning Kamp static void
10568071913dSRuslan Ermilov link_rtrequest(cmd, rt, info)
1057df8bae1dSRodney W. Grimes 	int cmd;
1058df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
10598071913dSRuslan Ermilov 	struct rt_addrinfo *info;
1060df8bae1dSRodney W. Grimes {
1061df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
1062df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
1063df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1064df8bae1dSRodney W. Grimes 
1065df8bae1dSRodney W. Grimes 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1066df8bae1dSRodney W. Grimes 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1067df8bae1dSRodney W. Grimes 		return;
10689448326fSPoul-Henning Kamp 	ifa = ifaof_ifpforaddr(dst, ifp);
10699448326fSPoul-Henning Kamp 	if (ifa) {
1070df8bae1dSRodney W. Grimes 		IFAFREE(rt->rt_ifa);
107119fc74fbSJeffrey Hsu 		IFAREF(ifa);		/* XXX */
1072df8bae1dSRodney W. Grimes 		rt->rt_ifa = ifa;
1073df8bae1dSRodney W. Grimes 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
10748071913dSRuslan Ermilov 			ifa->ifa_rtrequest(cmd, rt, info);
1075df8bae1dSRodney W. Grimes 	}
1076df8bae1dSRodney W. Grimes }
1077df8bae1dSRodney W. Grimes 
1078df8bae1dSRodney W. Grimes /*
1079df8bae1dSRodney W. Grimes  * Mark an interface down and notify protocols of
1080df8bae1dSRodney W. Grimes  * the transition.
1081df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
1082df8bae1dSRodney W. Grimes  */
1083df8bae1dSRodney W. Grimes void
1084e8c2601dSPoul-Henning Kamp if_unroute(ifp, flag, fam)
1085df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
1086e8c2601dSPoul-Henning Kamp 	int flag, fam;
1087df8bae1dSRodney W. Grimes {
1088df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
1089df8bae1dSRodney W. Grimes 
1090e8c2601dSPoul-Henning Kamp 	ifp->if_flags &= ~flag;
109198b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
1092e8c2601dSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1093e8c2601dSPoul-Henning Kamp 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1094df8bae1dSRodney W. Grimes 			pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1095df8bae1dSRodney W. Grimes 	if_qflush(&ifp->if_snd);
1096df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
1097df8bae1dSRodney W. Grimes }
1098df8bae1dSRodney W. Grimes 
1099df8bae1dSRodney W. Grimes /*
1100df8bae1dSRodney W. Grimes  * Mark an interface up and notify protocols of
1101df8bae1dSRodney W. Grimes  * the transition.
1102df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
1103df8bae1dSRodney W. Grimes  */
1104df8bae1dSRodney W. Grimes void
1105e8c2601dSPoul-Henning Kamp if_route(ifp, flag, fam)
1106df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
1107e8c2601dSPoul-Henning Kamp 	int flag, fam;
1108df8bae1dSRodney W. Grimes {
1109176395b2SGarrett Wollman 	register struct ifaddr *ifa;
1110df8bae1dSRodney W. Grimes 
1111e8c2601dSPoul-Henning Kamp 	ifp->if_flags |= flag;
111298b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
1113e8c2601dSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1114e8c2601dSPoul-Henning Kamp 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1115df8bae1dSRodney W. Grimes 			pfctlinput(PRC_IFUP, ifa->ifa_addr);
1116df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
111782cd038dSYoshinobu Inoue #ifdef INET6
111882cd038dSYoshinobu Inoue 	in6_if_up(ifp);
111982cd038dSYoshinobu Inoue #endif
1120df8bae1dSRodney W. Grimes }
1121df8bae1dSRodney W. Grimes 
1122df8bae1dSRodney W. Grimes /*
1123e8c2601dSPoul-Henning Kamp  * Mark an interface down and notify protocols of
1124e8c2601dSPoul-Henning Kamp  * the transition.
1125e8c2601dSPoul-Henning Kamp  * NOTE: must be called at splnet or eqivalent.
1126e8c2601dSPoul-Henning Kamp  */
1127e8c2601dSPoul-Henning Kamp void
1128e8c2601dSPoul-Henning Kamp if_down(ifp)
1129e8c2601dSPoul-Henning Kamp 	register struct ifnet *ifp;
1130e8c2601dSPoul-Henning Kamp {
1131e8c2601dSPoul-Henning Kamp 
1132e8c2601dSPoul-Henning Kamp 	if_unroute(ifp, IFF_UP, AF_UNSPEC);
1133e8c2601dSPoul-Henning Kamp }
1134e8c2601dSPoul-Henning Kamp 
1135e8c2601dSPoul-Henning Kamp /*
1136e8c2601dSPoul-Henning Kamp  * Mark an interface up and notify protocols of
1137e8c2601dSPoul-Henning Kamp  * the transition.
1138e8c2601dSPoul-Henning Kamp  * NOTE: must be called at splnet or eqivalent.
1139e8c2601dSPoul-Henning Kamp  */
1140e8c2601dSPoul-Henning Kamp void
1141e8c2601dSPoul-Henning Kamp if_up(ifp)
1142e8c2601dSPoul-Henning Kamp 	register struct ifnet *ifp;
1143e8c2601dSPoul-Henning Kamp {
1144e8c2601dSPoul-Henning Kamp 
1145e8c2601dSPoul-Henning Kamp 	if_route(ifp, IFF_UP, AF_UNSPEC);
1146e8c2601dSPoul-Henning Kamp }
1147e8c2601dSPoul-Henning Kamp 
1148e8c2601dSPoul-Henning Kamp /*
1149df8bae1dSRodney W. Grimes  * Flush an interface queue.
1150df8bae1dSRodney W. Grimes  */
11513bda9f9bSPoul-Henning Kamp static void
1152df8bae1dSRodney W. Grimes if_qflush(ifq)
1153df8bae1dSRodney W. Grimes 	register struct ifqueue *ifq;
1154df8bae1dSRodney W. Grimes {
1155df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
1156df8bae1dSRodney W. Grimes 
1157df8bae1dSRodney W. Grimes 	n = ifq->ifq_head;
11589448326fSPoul-Henning Kamp 	while ((m = n) != 0) {
1159df8bae1dSRodney W. Grimes 		n = m->m_act;
1160df8bae1dSRodney W. Grimes 		m_freem(m);
1161df8bae1dSRodney W. Grimes 	}
1162df8bae1dSRodney W. Grimes 	ifq->ifq_head = 0;
1163df8bae1dSRodney W. Grimes 	ifq->ifq_tail = 0;
1164df8bae1dSRodney W. Grimes 	ifq->ifq_len = 0;
1165df8bae1dSRodney W. Grimes }
1166df8bae1dSRodney W. Grimes 
1167df8bae1dSRodney W. Grimes /*
1168df8bae1dSRodney W. Grimes  * Handle interface watchdog timer routines.  Called
1169df8bae1dSRodney W. Grimes  * from softclock, we decrement timers (if set) and
1170df8bae1dSRodney W. Grimes  * call the appropriate interface routine on expiration.
1171df8bae1dSRodney W. Grimes  */
11723bda9f9bSPoul-Henning Kamp static void
1173df8bae1dSRodney W. Grimes if_slowtimo(arg)
1174df8bae1dSRodney W. Grimes 	void *arg;
1175df8bae1dSRodney W. Grimes {
1176df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
1177df8bae1dSRodney W. Grimes 	int s = splimp();
1178df8bae1dSRodney W. Grimes 
1179b30a244cSJeffrey Hsu 	IFNET_RLOCK();
1180fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1181df8bae1dSRodney W. Grimes 		if (ifp->if_timer == 0 || --ifp->if_timer)
1182df8bae1dSRodney W. Grimes 			continue;
1183df8bae1dSRodney W. Grimes 		if (ifp->if_watchdog)
11844a5f1499SDavid Greenman 			(*ifp->if_watchdog)(ifp);
1185df8bae1dSRodney W. Grimes 	}
1186b30a244cSJeffrey Hsu 	IFNET_RUNLOCK();
1187df8bae1dSRodney W. Grimes 	splx(s);
1188df8bae1dSRodney W. Grimes 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1189df8bae1dSRodney W. Grimes }
1190df8bae1dSRodney W. Grimes 
1191df8bae1dSRodney W. Grimes /*
1192df8bae1dSRodney W. Grimes  * Map interface name to
1193df8bae1dSRodney W. Grimes  * interface structure pointer.
1194df8bae1dSRodney W. Grimes  */
1195df8bae1dSRodney W. Grimes struct ifnet *
119630aad87dSBrooks Davis ifunit(const char *name)
1197df8bae1dSRodney W. Grimes {
119801f0fef3SJulian Elischer 	char namebuf[IFNAMSIZ + 1];
11998b7805e4SBoris Popov 	struct ifnet *ifp;
120010930aadSJonathan Lemon 	dev_t dev;
1201df8bae1dSRodney W. Grimes 
120201f0fef3SJulian Elischer 	/*
120301f0fef3SJulian Elischer 	 * Now search all the interfaces for this name/number
120401f0fef3SJulian Elischer 	 */
120510930aadSJonathan Lemon 
120610930aadSJonathan Lemon 	/*
120710930aadSJonathan Lemon 	 * XXX
120810930aadSJonathan Lemon 	 * Devices should really be known as /dev/fooN, not /dev/net/fooN.
120910930aadSJonathan Lemon 	 */
121010930aadSJonathan Lemon 	snprintf(namebuf, IFNAMSIZ, "%s/%s", net_cdevsw.d_name, name);
1211b30a244cSJeffrey Hsu 	IFNET_RLOCK();
1212fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
121310930aadSJonathan Lemon 		dev = ifdev_byindex(ifp->if_index);
121410930aadSJonathan Lemon 		if (strcmp(devtoname(dev), namebuf) == 0)
121510930aadSJonathan Lemon 			break;
121610930aadSJonathan Lemon 		if (dev_named(dev, name))
1217df8bae1dSRodney W. Grimes 			break;
1218df8bae1dSRodney W. Grimes 	}
1219b30a244cSJeffrey Hsu 	IFNET_RUNLOCK();
1220df8bae1dSRodney W. Grimes 	return (ifp);
1221df8bae1dSRodney W. Grimes }
1222df8bae1dSRodney W. Grimes 
122382cd038dSYoshinobu Inoue /*
122482cd038dSYoshinobu Inoue  * Map interface name in a sockaddr_dl to
122582cd038dSYoshinobu Inoue  * interface structure pointer.
122682cd038dSYoshinobu Inoue  */
122782cd038dSYoshinobu Inoue struct ifnet *
122882cd038dSYoshinobu Inoue if_withname(sa)
122982cd038dSYoshinobu Inoue 	struct sockaddr *sa;
123082cd038dSYoshinobu Inoue {
123182cd038dSYoshinobu Inoue 	char ifname[IFNAMSIZ+1];
123282cd038dSYoshinobu Inoue 	struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
123382cd038dSYoshinobu Inoue 
123482cd038dSYoshinobu Inoue 	if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
123582cd038dSYoshinobu Inoue 	     (sdl->sdl_nlen > IFNAMSIZ) )
123682cd038dSYoshinobu Inoue 		return NULL;
123782cd038dSYoshinobu Inoue 
123882cd038dSYoshinobu Inoue 	/*
12396e82956cSJuli Mallett 	 * ifunit wants a NUL-terminated string.  It may not be NUL-terminated
12406e82956cSJuli Mallett 	 * in the sockaddr, and we don't want to change the caller's sockaddr
12416e82956cSJuli Mallett 	 * (there might not be room to add the trailing NUL anyway), so we make
12426e82956cSJuli Mallett 	 * a local copy that we know we can NUL-terminate safely.
124382cd038dSYoshinobu Inoue 	 */
124482cd038dSYoshinobu Inoue 
124582cd038dSYoshinobu Inoue 	bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
124682cd038dSYoshinobu Inoue 	ifname[sdl->sdl_nlen] = '\0';
124782cd038dSYoshinobu Inoue 	return ifunit(ifname);
124882cd038dSYoshinobu Inoue }
124982cd038dSYoshinobu Inoue 
1250df8bae1dSRodney W. Grimes /*
1251f13ad206SJonathan Lemon  * Hardware specific interface ioctls.
1252df8bae1dSRodney W. Grimes  */
1253f13ad206SJonathan Lemon static int
1254f13ad206SJonathan Lemon ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1255df8bae1dSRodney W. Grimes {
1256f13ad206SJonathan Lemon 	struct ifreq *ifr;
1257413dd0baSPoul-Henning Kamp 	struct ifstat *ifs;
1258f13ad206SJonathan Lemon 	int error = 0;
125962f76486SMaxim Sobolev 	int new_flags;
1260df8bae1dSRodney W. Grimes 
1261df8bae1dSRodney W. Grimes 	ifr = (struct ifreq *)data;
126230aad87dSBrooks Davis 	switch (cmd) {
1263de593450SJonathan Lemon 	case SIOCGIFINDEX:
1264de593450SJonathan Lemon 		ifr->ifr_index = ifp->if_index;
1265de593450SJonathan Lemon 		break;
1266de593450SJonathan Lemon 
1267df8bae1dSRodney W. Grimes 	case SIOCGIFFLAGS:
126862f76486SMaxim Sobolev 		ifr->ifr_flags = ifp->if_flags & 0xffff;
126962f76486SMaxim Sobolev 		ifr->ifr_flagshigh = ifp->if_flags >> 16;
1270df8bae1dSRodney W. Grimes 		break;
1271df8bae1dSRodney W. Grimes 
1272016da741SJonathan Lemon 	case SIOCGIFCAP:
1273016da741SJonathan Lemon 		ifr->ifr_reqcap = ifp->if_capabilities;
1274016da741SJonathan Lemon 		ifr->ifr_curcap = ifp->if_capenable;
1275016da741SJonathan Lemon 		break;
1276016da741SJonathan Lemon 
12778f293a63SRobert Watson #ifdef MAC
12788f293a63SRobert Watson 	case SIOCGIFMAC:
12798f293a63SRobert Watson 		error = mac_ioctl_ifnet_get(td->td_proc->p_ucred, ifr, ifp);
12808f293a63SRobert Watson 		break;
12818f293a63SRobert Watson #endif
12828f293a63SRobert Watson 
1283df8bae1dSRodney W. Grimes 	case SIOCGIFMETRIC:
1284df8bae1dSRodney W. Grimes 		ifr->ifr_metric = ifp->if_metric;
1285df8bae1dSRodney W. Grimes 		break;
1286df8bae1dSRodney W. Grimes 
1287a7028af7SDavid Greenman 	case SIOCGIFMTU:
1288a7028af7SDavid Greenman 		ifr->ifr_mtu = ifp->if_mtu;
1289a7028af7SDavid Greenman 		break;
1290a7028af7SDavid Greenman 
1291074c4a4eSGarrett Wollman 	case SIOCGIFPHYS:
1292074c4a4eSGarrett Wollman 		ifr->ifr_phys = ifp->if_physical;
1293074c4a4eSGarrett Wollman 		break;
1294074c4a4eSGarrett Wollman 
1295df8bae1dSRodney W. Grimes 	case SIOCSIFFLAGS:
129644731cabSJohn Baldwin 		error = suser(td);
12979448326fSPoul-Henning Kamp 		if (error)
1298df8bae1dSRodney W. Grimes 			return (error);
129962f76486SMaxim Sobolev 		new_flags = (ifr->ifr_flags & 0xffff) |
130062f76486SMaxim Sobolev 		    (ifr->ifr_flagshigh << 16);
1301cf4b9371SPoul-Henning Kamp 		if (ifp->if_flags & IFF_SMART) {
1302cf4b9371SPoul-Henning Kamp 			/* Smart drivers twiddle their own routes */
13032f55ead7SPoul-Henning Kamp 		} else if (ifp->if_flags & IFF_UP &&
130462f76486SMaxim Sobolev 		    (new_flags & IFF_UP) == 0) {
1305df8bae1dSRodney W. Grimes 			int s = splimp();
1306df8bae1dSRodney W. Grimes 			if_down(ifp);
1307df8bae1dSRodney W. Grimes 			splx(s);
130862f76486SMaxim Sobolev 		} else if (new_flags & IFF_UP &&
1309cf4b9371SPoul-Henning Kamp 		    (ifp->if_flags & IFF_UP) == 0) {
1310df8bae1dSRodney W. Grimes 			int s = splimp();
1311df8bae1dSRodney W. Grimes 			if_up(ifp);
1312df8bae1dSRodney W. Grimes 			splx(s);
1313df8bae1dSRodney W. Grimes 		}
1314df8bae1dSRodney W. Grimes 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
131562f76486SMaxim Sobolev 			(new_flags &~ IFF_CANTCHANGE);
1316ffb079beSMaxim Sobolev 		if (new_flags & IFF_PPROMISC) {
1317ffb079beSMaxim Sobolev 			/* Permanently promiscuous mode requested */
1318ffb079beSMaxim Sobolev 			ifp->if_flags |= IFF_PROMISC;
1319ffb079beSMaxim Sobolev 		} else if (ifp->if_pcount == 0) {
1320ffb079beSMaxim Sobolev 			ifp->if_flags &= ~IFF_PROMISC;
1321ffb079beSMaxim Sobolev 		}
1322df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl)
1323df8bae1dSRodney W. Grimes 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
132498b9590eSPoul-Henning Kamp 		getmicrotime(&ifp->if_lastchange);
1325df8bae1dSRodney W. Grimes 		break;
1326df8bae1dSRodney W. Grimes 
1327016da741SJonathan Lemon 	case SIOCSIFCAP:
132844731cabSJohn Baldwin 		error = suser(td);
1329016da741SJonathan Lemon 		if (error)
1330016da741SJonathan Lemon 			return (error);
1331016da741SJonathan Lemon 		if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1332016da741SJonathan Lemon 			return (EINVAL);
1333016da741SJonathan Lemon 		(void) (*ifp->if_ioctl)(ifp, cmd, data);
1334016da741SJonathan Lemon 		break;
1335016da741SJonathan Lemon 
13368f293a63SRobert Watson #ifdef MAC
13378f293a63SRobert Watson 	case SIOCSIFMAC:
13388f293a63SRobert Watson 		error = mac_ioctl_ifnet_set(td->td_proc->p_ucred, ifr, ifp);
13398f293a63SRobert Watson 		break;
13408f293a63SRobert Watson #endif
13418f293a63SRobert Watson 
1342df8bae1dSRodney W. Grimes 	case SIOCSIFMETRIC:
134344731cabSJohn Baldwin 		error = suser(td);
13449448326fSPoul-Henning Kamp 		if (error)
1345df8bae1dSRodney W. Grimes 			return (error);
1346df8bae1dSRodney W. Grimes 		ifp->if_metric = ifr->ifr_metric;
134798b9590eSPoul-Henning Kamp 		getmicrotime(&ifp->if_lastchange);
1348df8bae1dSRodney W. Grimes 		break;
1349df8bae1dSRodney W. Grimes 
1350074c4a4eSGarrett Wollman 	case SIOCSIFPHYS:
135144731cabSJohn Baldwin 		error = suser(td);
1352e39a0280SGary Palmer 		if (error)
1353e39a0280SGary Palmer 			return error;
1354e39a0280SGary Palmer 		if (!ifp->if_ioctl)
1355e39a0280SGary Palmer 		        return EOPNOTSUPP;
1356e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1357e39a0280SGary Palmer 		if (error == 0)
135898b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
1359e39a0280SGary Palmer 		return(error);
1360074c4a4eSGarrett Wollman 
1361a7028af7SDavid Greenman 	case SIOCSIFMTU:
136282cd038dSYoshinobu Inoue 	{
136382cd038dSYoshinobu Inoue 		u_long oldmtu = ifp->if_mtu;
136482cd038dSYoshinobu Inoue 
136544731cabSJohn Baldwin 		error = suser(td);
13669448326fSPoul-Henning Kamp 		if (error)
1367a7028af7SDavid Greenman 			return (error);
1368aab3beeeSBrian Somers 		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
136975ee03cbSDavid Greenman 			return (EINVAL);
1370f13ad206SJonathan Lemon 		if (ifp->if_ioctl == NULL)
1371f13ad206SJonathan Lemon 			return (EOPNOTSUPP);
1372e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
137348f71763SRuslan Ermilov 		if (error == 0) {
137498b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
137548f71763SRuslan Ermilov 			rt_ifmsg(ifp);
137648f71763SRuslan Ermilov 		}
137782cd038dSYoshinobu Inoue 		/*
137882cd038dSYoshinobu Inoue 		 * If the link MTU changed, do network layer specific procedure.
137982cd038dSYoshinobu Inoue 		 */
138082cd038dSYoshinobu Inoue 		if (ifp->if_mtu != oldmtu) {
138182cd038dSYoshinobu Inoue #ifdef INET6
138282cd038dSYoshinobu Inoue 			nd6_setmtu(ifp);
138382cd038dSYoshinobu Inoue #endif
138482cd038dSYoshinobu Inoue 		}
1385f13ad206SJonathan Lemon 		break;
138682cd038dSYoshinobu Inoue 	}
1387a7028af7SDavid Greenman 
1388df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
1389df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
139044731cabSJohn Baldwin 		error = suser(td);
13919448326fSPoul-Henning Kamp 		if (error)
1392df8bae1dSRodney W. Grimes 			return (error);
1393477180fbSGarrett Wollman 
1394477180fbSGarrett Wollman 		/* Don't allow group membership on non-multicast interfaces. */
1395477180fbSGarrett Wollman 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
1396f13ad206SJonathan Lemon 			return (EOPNOTSUPP);
1397477180fbSGarrett Wollman 
1398477180fbSGarrett Wollman 		/* Don't let users screw up protocols' entries. */
1399477180fbSGarrett Wollman 		if (ifr->ifr_addr.sa_family != AF_LINK)
1400f13ad206SJonathan Lemon 			return (EINVAL);
1401477180fbSGarrett Wollman 
1402477180fbSGarrett Wollman 		if (cmd == SIOCADDMULTI) {
1403477180fbSGarrett Wollman 			struct ifmultiaddr *ifma;
1404477180fbSGarrett Wollman 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1405477180fbSGarrett Wollman 		} else {
1406477180fbSGarrett Wollman 			error = if_delmulti(ifp, &ifr->ifr_addr);
1407477180fbSGarrett Wollman 		}
1408e39a0280SGary Palmer 		if (error == 0)
140998b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
1410f13ad206SJonathan Lemon 		break;
1411df8bae1dSRodney W. Grimes 
141241b3e8e5SJun-ichiro itojun Hagino 	case SIOCSIFPHYADDR:
141341b3e8e5SJun-ichiro itojun Hagino 	case SIOCDIFPHYADDR:
141441b3e8e5SJun-ichiro itojun Hagino #ifdef INET6
141541b3e8e5SJun-ichiro itojun Hagino 	case SIOCSIFPHYADDR_IN6:
141641b3e8e5SJun-ichiro itojun Hagino #endif
141733841545SHajimu UMEMOTO 	case SIOCSLIFPHYADDR:
1418a912e453SPeter Wemm         case SIOCSIFMEDIA:
1419d7189ec6SJoerg Wunsch 	case SIOCSIFGENERIC:
142044731cabSJohn Baldwin 		error = suser(td);
1421a912e453SPeter Wemm 		if (error)
1422a912e453SPeter Wemm 			return (error);
1423f13ad206SJonathan Lemon 		if (ifp->if_ioctl == NULL)
1424a912e453SPeter Wemm 			return (EOPNOTSUPP);
1425a912e453SPeter Wemm 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1426a912e453SPeter Wemm 		if (error == 0)
142798b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
1428f13ad206SJonathan Lemon 		break;
1429a912e453SPeter Wemm 
1430413dd0baSPoul-Henning Kamp 	case SIOCGIFSTATUS:
1431413dd0baSPoul-Henning Kamp 		ifs = (struct ifstat *)data;
1432413dd0baSPoul-Henning Kamp 		ifs->ascii[0] = '\0';
1433413dd0baSPoul-Henning Kamp 
143433841545SHajimu UMEMOTO 	case SIOCGIFPSRCADDR:
143533841545SHajimu UMEMOTO 	case SIOCGIFPDSTADDR:
143633841545SHajimu UMEMOTO 	case SIOCGLIFPHYADDR:
1437a912e453SPeter Wemm 	case SIOCGIFMEDIA:
1438d7189ec6SJoerg Wunsch 	case SIOCGIFGENERIC:
1439a912e453SPeter Wemm 		if (ifp->if_ioctl == 0)
1440a912e453SPeter Wemm 			return (EOPNOTSUPP);
1441f13ad206SJonathan Lemon 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1442f13ad206SJonathan Lemon 		break;
1443a912e453SPeter Wemm 
1444b106252cSBill Paul 	case SIOCSIFLLADDR:
144544731cabSJohn Baldwin 		error = suser(td);
1446b106252cSBill Paul 		if (error)
1447b106252cSBill Paul 			return (error);
1448f13ad206SJonathan Lemon 		error = if_setlladdr(ifp,
144966ce51ceSArchie Cobbs 		    ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1450f13ad206SJonathan Lemon 		break;
145166ce51ceSArchie Cobbs 
1452df8bae1dSRodney W. Grimes 	default:
1453f13ad206SJonathan Lemon 		error = ENOIOCTL;
1454f13ad206SJonathan Lemon 		break;
1455f13ad206SJonathan Lemon 	}
1456f13ad206SJonathan Lemon 	return (error);
1457f13ad206SJonathan Lemon }
1458f13ad206SJonathan Lemon 
1459f13ad206SJonathan Lemon /*
1460f13ad206SJonathan Lemon  * Interface ioctls.
1461f13ad206SJonathan Lemon  */
1462f13ad206SJonathan Lemon int
1463f13ad206SJonathan Lemon ifioctl(so, cmd, data, td)
1464f13ad206SJonathan Lemon 	struct socket *so;
1465f13ad206SJonathan Lemon 	u_long cmd;
1466f13ad206SJonathan Lemon 	caddr_t data;
1467f13ad206SJonathan Lemon 	struct thread *td;
1468f13ad206SJonathan Lemon {
1469f13ad206SJonathan Lemon 	struct ifnet *ifp;
1470f13ad206SJonathan Lemon 	struct ifreq *ifr;
1471f13ad206SJonathan Lemon 	int error;
147262f76486SMaxim Sobolev 	int oif_flags;
1473f13ad206SJonathan Lemon 
1474f13ad206SJonathan Lemon 	switch (cmd) {
1475f13ad206SJonathan Lemon 	case SIOCGIFCONF:
1476f13ad206SJonathan Lemon 	case OSIOCGIFCONF:
1477f13ad206SJonathan Lemon 		return (ifconf(cmd, data));
1478f13ad206SJonathan Lemon 	}
1479f13ad206SJonathan Lemon 	ifr = (struct ifreq *)data;
1480f13ad206SJonathan Lemon 
1481f13ad206SJonathan Lemon 	switch (cmd) {
1482f13ad206SJonathan Lemon 	case SIOCIFCREATE:
1483f13ad206SJonathan Lemon 	case SIOCIFDESTROY:
148444731cabSJohn Baldwin 		if ((error = suser(td)) != 0)
1485f13ad206SJonathan Lemon 			return (error);
1486f13ad206SJonathan Lemon 		return ((cmd == SIOCIFCREATE) ?
1487f13ad206SJonathan Lemon 			if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1488f13ad206SJonathan Lemon 			if_clone_destroy(ifr->ifr_name));
1489f13ad206SJonathan Lemon 
1490f13ad206SJonathan Lemon 	case SIOCIFGCLONERS:
1491f13ad206SJonathan Lemon 		return (if_clone_list((struct if_clonereq *)data));
1492f13ad206SJonathan Lemon 	}
1493f13ad206SJonathan Lemon 
1494f13ad206SJonathan Lemon 	ifp = ifunit(ifr->ifr_name);
1495f13ad206SJonathan Lemon 	if (ifp == 0)
1496f13ad206SJonathan Lemon 		return (ENXIO);
1497f13ad206SJonathan Lemon 
1498f13ad206SJonathan Lemon 	error = ifhwioctl(cmd, ifp, data, td);
1499f13ad206SJonathan Lemon 	if (error != ENOIOCTL)
1500f13ad206SJonathan Lemon 		return (error);
1501f13ad206SJonathan Lemon 
150282cd038dSYoshinobu Inoue 	oif_flags = ifp->if_flags;
1503df8bae1dSRodney W. Grimes 	if (so->so_proto == 0)
1504df8bae1dSRodney W. Grimes 		return (EOPNOTSUPP);
1505df8bae1dSRodney W. Grimes #ifndef COMPAT_43
150682cd038dSYoshinobu Inoue 	error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
15072c37256eSGarrett Wollman 								 data,
1508b40ce416SJulian Elischer 								 ifp, td));
1509df8bae1dSRodney W. Grimes #else
1510df8bae1dSRodney W. Grimes 	{
1511df8bae1dSRodney W. Grimes 		int ocmd = cmd;
1512df8bae1dSRodney W. Grimes 
1513df8bae1dSRodney W. Grimes 		switch (cmd) {
1514df8bae1dSRodney W. Grimes 
1515df8bae1dSRodney W. Grimes 		case SIOCSIFDSTADDR:
1516df8bae1dSRodney W. Grimes 		case SIOCSIFADDR:
1517df8bae1dSRodney W. Grimes 		case SIOCSIFBRDADDR:
1518df8bae1dSRodney W. Grimes 		case SIOCSIFNETMASK:
1519df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN
1520df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_family == 0 &&
1521df8bae1dSRodney W. Grimes 			    ifr->ifr_addr.sa_len < 16) {
1522df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1523df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
1524df8bae1dSRodney W. Grimes 			}
1525df8bae1dSRodney W. Grimes #else
1526df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_len == 0)
1527df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
1528df8bae1dSRodney W. Grimes #endif
1529df8bae1dSRodney W. Grimes 			break;
1530df8bae1dSRodney W. Grimes 
1531df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
1532df8bae1dSRodney W. Grimes 			cmd = SIOCGIFADDR;
1533df8bae1dSRodney W. Grimes 			break;
1534df8bae1dSRodney W. Grimes 
1535df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
1536df8bae1dSRodney W. Grimes 			cmd = SIOCGIFDSTADDR;
1537df8bae1dSRodney W. Grimes 			break;
1538df8bae1dSRodney W. Grimes 
1539df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
1540df8bae1dSRodney W. Grimes 			cmd = SIOCGIFBRDADDR;
1541df8bae1dSRodney W. Grimes 			break;
1542df8bae1dSRodney W. Grimes 
1543df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
1544df8bae1dSRodney W. Grimes 			cmd = SIOCGIFNETMASK;
1545df8bae1dSRodney W. Grimes 		}
15462c37256eSGarrett Wollman 		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
15472c37256eSGarrett Wollman 								   cmd,
15482c37256eSGarrett Wollman 								   data,
1549b40ce416SJulian Elischer 								   ifp, td));
1550df8bae1dSRodney W. Grimes 		switch (ocmd) {
1551df8bae1dSRodney W. Grimes 
1552df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
1553df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
1554df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
1555df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
1556df8bae1dSRodney W. Grimes 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
155782cd038dSYoshinobu Inoue 
155882cd038dSYoshinobu Inoue 		}
155982cd038dSYoshinobu Inoue 	}
156082cd038dSYoshinobu Inoue #endif /* COMPAT_43 */
156182cd038dSYoshinobu Inoue 
156282cd038dSYoshinobu Inoue 	if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
156382cd038dSYoshinobu Inoue #ifdef INET6
156488ff5695SSUZUKI Shinsuke 		DELAY(100);/* XXX: temporary workaround for fxp issue*/
156582cd038dSYoshinobu Inoue 		if (ifp->if_flags & IFF_UP) {
156682cd038dSYoshinobu Inoue 			int s = splimp();
156782cd038dSYoshinobu Inoue 			in6_if_up(ifp);
156882cd038dSYoshinobu Inoue 			splx(s);
156982cd038dSYoshinobu Inoue 		}
157082cd038dSYoshinobu Inoue #endif
1571df8bae1dSRodney W. Grimes 	}
1572df8bae1dSRodney W. Grimes 	return (error);
1573df8bae1dSRodney W. Grimes }
1574df8bae1dSRodney W. Grimes 
1575df8bae1dSRodney W. Grimes /*
1576963e4c2aSGarrett Wollman  * Set/clear promiscuous mode on interface ifp based on the truth value
1577963e4c2aSGarrett Wollman  * of pswitch.  The calls are reference counted so that only the first
1578963e4c2aSGarrett Wollman  * "on" request actually has an effect, as does the final "off" request.
1579963e4c2aSGarrett Wollman  * Results are undefined if the "off" and "on" requests are not matched.
1580963e4c2aSGarrett Wollman  */
1581963e4c2aSGarrett Wollman int
1582963e4c2aSGarrett Wollman ifpromisc(ifp, pswitch)
1583963e4c2aSGarrett Wollman 	struct ifnet *ifp;
1584963e4c2aSGarrett Wollman 	int pswitch;
1585963e4c2aSGarrett Wollman {
1586963e4c2aSGarrett Wollman 	struct ifreq ifr;
15874a26224cSGarrett Wollman 	int error;
15884f3c11a6SBill Fenner 	int oldflags, oldpcount;
1589963e4c2aSGarrett Wollman 
15904f3c11a6SBill Fenner 	oldpcount = ifp->if_pcount;
15912c514a31SBrian Somers 	oldflags = ifp->if_flags;
1592ffb079beSMaxim Sobolev 	if (ifp->if_flags & IFF_PPROMISC) {
1593ffb079beSMaxim Sobolev 		/* Do nothing if device is in permanently promiscuous mode */
1594ffb079beSMaxim Sobolev 		ifp->if_pcount += pswitch ? 1 : -1;
1595ffb079beSMaxim Sobolev 		return (0);
1596ffb079beSMaxim Sobolev 	}
1597963e4c2aSGarrett Wollman 	if (pswitch) {
1598963e4c2aSGarrett Wollman 		/*
1599963e4c2aSGarrett Wollman 		 * If the device is not configured up, we cannot put it in
1600963e4c2aSGarrett Wollman 		 * promiscuous mode.
1601963e4c2aSGarrett Wollman 		 */
1602963e4c2aSGarrett Wollman 		if ((ifp->if_flags & IFF_UP) == 0)
1603963e4c2aSGarrett Wollman 			return (ENETDOWN);
1604963e4c2aSGarrett Wollman 		if (ifp->if_pcount++ != 0)
1605963e4c2aSGarrett Wollman 			return (0);
1606963e4c2aSGarrett Wollman 		ifp->if_flags |= IFF_PROMISC;
1607963e4c2aSGarrett Wollman 	} else {
1608963e4c2aSGarrett Wollman 		if (--ifp->if_pcount > 0)
1609963e4c2aSGarrett Wollman 			return (0);
1610963e4c2aSGarrett Wollman 		ifp->if_flags &= ~IFF_PROMISC;
1611963e4c2aSGarrett Wollman 	}
161262f76486SMaxim Sobolev 	ifr.ifr_flags = ifp->if_flags & 0xffff;
161362f76486SMaxim Sobolev 	ifr.ifr_flagshigh = ifp->if_flags >> 16;
16144a26224cSGarrett Wollman 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
16154f3c11a6SBill Fenner 	if (error == 0) {
16164f3c11a6SBill Fenner 		log(LOG_INFO, "%s%d: promiscuous mode %s\n",
16174f3c11a6SBill Fenner 		    ifp->if_name, ifp->if_unit,
16184f3c11a6SBill Fenner 		    (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
16194a26224cSGarrett Wollman 		rt_ifmsg(ifp);
16204f3c11a6SBill Fenner 	} else {
16214f3c11a6SBill Fenner 		ifp->if_pcount = oldpcount;
16222c514a31SBrian Somers 		ifp->if_flags = oldflags;
16234f3c11a6SBill Fenner 	}
16244a26224cSGarrett Wollman 	return error;
1625963e4c2aSGarrett Wollman }
1626963e4c2aSGarrett Wollman 
1627963e4c2aSGarrett Wollman /*
1628df8bae1dSRodney W. Grimes  * Return interface configuration
1629df8bae1dSRodney W. Grimes  * of system.  List may be used
1630df8bae1dSRodney W. Grimes  * in later ioctl's (above) to get
1631df8bae1dSRodney W. Grimes  * other information.
1632df8bae1dSRodney W. Grimes  */
1633df8bae1dSRodney W. Grimes /*ARGSUSED*/
16343bda9f9bSPoul-Henning Kamp static int
1635df8bae1dSRodney W. Grimes ifconf(cmd, data)
1636ecbb00a2SDoug Rabson 	u_long cmd;
1637df8bae1dSRodney W. Grimes 	caddr_t data;
1638df8bae1dSRodney W. Grimes {
16390b59d917SJonathan Lemon 	struct ifconf *ifc = (struct ifconf *)data;
16400b59d917SJonathan Lemon 	struct ifnet *ifp;
16410b59d917SJonathan Lemon 	struct ifaddr *ifa;
1642df8bae1dSRodney W. Grimes 	struct ifreq ifr, *ifrp;
1643df8bae1dSRodney W. Grimes 	int space = ifc->ifc_len, error = 0;
1644df8bae1dSRodney W. Grimes 
1645df8bae1dSRodney W. Grimes 	ifrp = ifc->ifc_req;
1646b30a244cSJeffrey Hsu 	IFNET_RLOCK();		/* could sleep XXX */
16470b59d917SJonathan Lemon 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
16481ce9bf88SPoul-Henning Kamp 		char workbuf[64];
164975c13541SPoul-Henning Kamp 		int ifnlen, addrs;
16502624cf89SGarrett Wollman 
16512defe5cdSJonathan Lemon 		if (space < sizeof(ifr))
16520b59d917SJonathan Lemon 			break;
16532127f260SArchie Cobbs 		ifnlen = snprintf(workbuf, sizeof(workbuf),
16542127f260SArchie Cobbs 		    "%s%d", ifp->if_name, ifp->if_unit);
16551ce9bf88SPoul-Henning Kamp 		if(ifnlen + 1 > sizeof ifr.ifr_name) {
16562624cf89SGarrett Wollman 			error = ENAMETOOLONG;
1657b3f1e629SGuido van Rooij 			break;
16582624cf89SGarrett Wollman 		} else {
16591ce9bf88SPoul-Henning Kamp 			strcpy(ifr.ifr_name, workbuf);
16602624cf89SGarrett Wollman 		}
16612624cf89SGarrett Wollman 
166275c13541SPoul-Henning Kamp 		addrs = 0;
16632defe5cdSJonathan Lemon 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
16642defe5cdSJonathan Lemon 			struct sockaddr *sa = ifa->ifa_addr;
16652defe5cdSJonathan Lemon 
16662defe5cdSJonathan Lemon 			if (space < sizeof(ifr))
16672defe5cdSJonathan Lemon 				break;
1668a854ed98SJohn Baldwin 			if (jailed(curthread->td_ucred) &&
1669a854ed98SJohn Baldwin 			    prison_if(curthread->td_ucred, sa))
167075c13541SPoul-Henning Kamp 				continue;
167175c13541SPoul-Henning Kamp 			addrs++;
1672df8bae1dSRodney W. Grimes #ifdef COMPAT_43
1673df8bae1dSRodney W. Grimes 			if (cmd == OSIOCGIFCONF) {
1674df8bae1dSRodney W. Grimes 				struct osockaddr *osa =
1675df8bae1dSRodney W. Grimes 					 (struct osockaddr *)&ifr.ifr_addr;
1676df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
1677df8bae1dSRodney W. Grimes 				osa->sa_family = sa->sa_family;
1678df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1679df8bae1dSRodney W. Grimes 						sizeof (ifr));
1680df8bae1dSRodney W. Grimes 				ifrp++;
1681df8bae1dSRodney W. Grimes 			} else
1682df8bae1dSRodney W. Grimes #endif
1683df8bae1dSRodney W. Grimes 			if (sa->sa_len <= sizeof(*sa)) {
1684df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
1685df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1686df8bae1dSRodney W. Grimes 						sizeof (ifr));
1687df8bae1dSRodney W. Grimes 				ifrp++;
1688df8bae1dSRodney W. Grimes 			} else {
1689d91a068eSGuido van Rooij 				if (space < sizeof (ifr) + sa->sa_len -
1690d91a068eSGuido van Rooij 					    sizeof(*sa))
1691b3f1e629SGuido van Rooij 					break;
1692df8bae1dSRodney W. Grimes 				space -= sa->sa_len - sizeof(*sa);
1693df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1694df8bae1dSRodney W. Grimes 						sizeof (ifr.ifr_name));
1695df8bae1dSRodney W. Grimes 				if (error == 0)
1696df8bae1dSRodney W. Grimes 				    error = copyout((caddr_t)sa,
1697df8bae1dSRodney W. Grimes 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1698df8bae1dSRodney W. Grimes 				ifrp = (struct ifreq *)
1699df8bae1dSRodney W. Grimes 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1700df8bae1dSRodney W. Grimes 			}
1701df8bae1dSRodney W. Grimes 			if (error)
1702df8bae1dSRodney W. Grimes 				break;
1703df8bae1dSRodney W. Grimes 			space -= sizeof (ifr);
1704df8bae1dSRodney W. Grimes 		}
1705b3f1e629SGuido van Rooij 		if (error)
1706b3f1e629SGuido van Rooij 			break;
170775c13541SPoul-Henning Kamp 		if (!addrs) {
170875c13541SPoul-Henning Kamp 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
170975c13541SPoul-Henning Kamp 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
171075c13541SPoul-Henning Kamp 			    sizeof (ifr));
171175c13541SPoul-Henning Kamp 			if (error)
171275c13541SPoul-Henning Kamp 				break;
1713b3f1e629SGuido van Rooij 			space -= sizeof (ifr);
1714b3f1e629SGuido van Rooij 			ifrp++;
171575c13541SPoul-Henning Kamp 		}
1716df8bae1dSRodney W. Grimes 	}
1717b30a244cSJeffrey Hsu 	IFNET_RUNLOCK();
1718df8bae1dSRodney W. Grimes 	ifc->ifc_len -= space;
1719df8bae1dSRodney W. Grimes 	return (error);
1720df8bae1dSRodney W. Grimes }
1721df8bae1dSRodney W. Grimes 
17221158dfb7SGarrett Wollman /*
17231158dfb7SGarrett Wollman  * Just like if_promisc(), but for all-multicast-reception mode.
17241158dfb7SGarrett Wollman  */
17251158dfb7SGarrett Wollman int
17261158dfb7SGarrett Wollman if_allmulti(ifp, onswitch)
17271158dfb7SGarrett Wollman 	struct ifnet *ifp;
17281158dfb7SGarrett Wollman 	int onswitch;
17291158dfb7SGarrett Wollman {
17301158dfb7SGarrett Wollman 	int error = 0;
17311158dfb7SGarrett Wollman 	int s = splimp();
1732ee0a4f7eSSUZUKI Shinsuke 	struct ifreq ifr;
17331158dfb7SGarrett Wollman 
17341158dfb7SGarrett Wollman 	if (onswitch) {
17351158dfb7SGarrett Wollman 		if (ifp->if_amcount++ == 0) {
17361158dfb7SGarrett Wollman 			ifp->if_flags |= IFF_ALLMULTI;
173762f76486SMaxim Sobolev 			ifr.ifr_flags = ifp->if_flags & 0xffff;
173862f76486SMaxim Sobolev 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
1739ee0a4f7eSSUZUKI Shinsuke 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
17401158dfb7SGarrett Wollman 		}
17411158dfb7SGarrett Wollman 	} else {
17421158dfb7SGarrett Wollman 		if (ifp->if_amcount > 1) {
17431158dfb7SGarrett Wollman 			ifp->if_amcount--;
17441158dfb7SGarrett Wollman 		} else {
17451158dfb7SGarrett Wollman 			ifp->if_amcount = 0;
17461158dfb7SGarrett Wollman 			ifp->if_flags &= ~IFF_ALLMULTI;
174762f76486SMaxim Sobolev 			ifr.ifr_flags = ifp->if_flags & 0xffff;;
174862f76486SMaxim Sobolev 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
1749ee0a4f7eSSUZUKI Shinsuke 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
17501158dfb7SGarrett Wollman 		}
17511158dfb7SGarrett Wollman 	}
17521158dfb7SGarrett Wollman 	splx(s);
17534a26224cSGarrett Wollman 
17544a26224cSGarrett Wollman 	if (error == 0)
17554a26224cSGarrett Wollman 		rt_ifmsg(ifp);
17561158dfb7SGarrett Wollman 	return error;
17571158dfb7SGarrett Wollman }
17581158dfb7SGarrett Wollman 
17591158dfb7SGarrett Wollman /*
17601158dfb7SGarrett Wollman  * Add a multicast listenership to the interface in question.
17611158dfb7SGarrett Wollman  * The link layer provides a routine which converts
17621158dfb7SGarrett Wollman  */
17631158dfb7SGarrett Wollman int
1764373f88edSGarrett Wollman if_addmulti(ifp, sa, retifma)
17651158dfb7SGarrett Wollman 	struct ifnet *ifp;	/* interface to manipulate */
17661158dfb7SGarrett Wollman 	struct sockaddr *sa;	/* address to add */
1767b2053118SGarrett Wollman 	struct ifmultiaddr **retifma;
17681158dfb7SGarrett Wollman {
17691158dfb7SGarrett Wollman 	struct sockaddr *llsa, *dupsa;
17701158dfb7SGarrett Wollman 	int error, s;
17711158dfb7SGarrett Wollman 	struct ifmultiaddr *ifma;
17721158dfb7SGarrett Wollman 
177357af7922SJulian Elischer 	/*
177457af7922SJulian Elischer 	 * If the matching multicast address already exists
177557af7922SJulian Elischer 	 * then don't add a new one, just add a reference
177657af7922SJulian Elischer 	 */
17776817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
177857af7922SJulian Elischer 		if (equal(sa, ifma->ifma_addr)) {
17791158dfb7SGarrett Wollman 			ifma->ifma_refcount++;
178057af7922SJulian Elischer 			if (retifma)
178157af7922SJulian Elischer 				*retifma = ifma;
17821158dfb7SGarrett Wollman 			return 0;
17831158dfb7SGarrett Wollman 		}
178457af7922SJulian Elischer 	}
17851158dfb7SGarrett Wollman 
17861158dfb7SGarrett Wollman 	/*
17871158dfb7SGarrett Wollman 	 * Give the link layer a chance to accept/reject it, and also
17881158dfb7SGarrett Wollman 	 * find out which AF_LINK address this maps to, if it isn't one
17891158dfb7SGarrett Wollman 	 * already.
17901158dfb7SGarrett Wollman 	 */
17911158dfb7SGarrett Wollman 	if (ifp->if_resolvemulti) {
17921158dfb7SGarrett Wollman 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
17931158dfb7SGarrett Wollman 		if (error) return error;
17941158dfb7SGarrett Wollman 	} else {
17951158dfb7SGarrett Wollman 		llsa = 0;
17961158dfb7SGarrett Wollman 	}
17971158dfb7SGarrett Wollman 
17981158dfb7SGarrett Wollman 	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
17991158dfb7SGarrett Wollman 	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
18001158dfb7SGarrett Wollman 	bcopy(sa, dupsa, sa->sa_len);
18011158dfb7SGarrett Wollman 
18021158dfb7SGarrett Wollman 	ifma->ifma_addr = dupsa;
18031158dfb7SGarrett Wollman 	ifma->ifma_lladdr = llsa;
18041158dfb7SGarrett Wollman 	ifma->ifma_ifp = ifp;
18051158dfb7SGarrett Wollman 	ifma->ifma_refcount = 1;
1806373f88edSGarrett Wollman 	ifma->ifma_protospec = 0;
1807477180fbSGarrett Wollman 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1808373f88edSGarrett Wollman 
18091158dfb7SGarrett Wollman 	/*
18101158dfb7SGarrett Wollman 	 * Some network interfaces can scan the address list at
18111158dfb7SGarrett Wollman 	 * interrupt time; lock them out.
18121158dfb7SGarrett Wollman 	 */
18131158dfb7SGarrett Wollman 	s = splimp();
18146817526dSPoul-Henning Kamp 	TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
18151158dfb7SGarrett Wollman 	splx(s);
181613990766SJonathan Mini 	if (retifma != NULL)
1817373f88edSGarrett Wollman 		*retifma = ifma;
18181158dfb7SGarrett Wollman 
18191158dfb7SGarrett Wollman 	if (llsa != 0) {
18206817526dSPoul-Henning Kamp 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
18211158dfb7SGarrett Wollman 			if (equal(ifma->ifma_addr, llsa))
18221158dfb7SGarrett Wollman 				break;
18231158dfb7SGarrett Wollman 		}
18241158dfb7SGarrett Wollman 		if (ifma) {
18251158dfb7SGarrett Wollman 			ifma->ifma_refcount++;
18261158dfb7SGarrett Wollman 		} else {
18271158dfb7SGarrett Wollman 			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
18281158dfb7SGarrett Wollman 			       M_IFMADDR, M_WAITOK);
1829477180fbSGarrett Wollman 			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1830477180fbSGarrett Wollman 			       M_IFMADDR, M_WAITOK);
1831477180fbSGarrett Wollman 			bcopy(llsa, dupsa, llsa->sa_len);
1832477180fbSGarrett Wollman 			ifma->ifma_addr = dupsa;
18331158dfb7SGarrett Wollman 			ifma->ifma_ifp = ifp;
18341158dfb7SGarrett Wollman 			ifma->ifma_refcount = 1;
18351158dfb7SGarrett Wollman 			s = splimp();
18366817526dSPoul-Henning Kamp 			TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
18371158dfb7SGarrett Wollman 			splx(s);
18381158dfb7SGarrett Wollman 		}
183957af7922SJulian Elischer 	}
18401158dfb7SGarrett Wollman 	/*
18411158dfb7SGarrett Wollman 	 * We are certain we have added something, so call down to the
18421158dfb7SGarrett Wollman 	 * interface to let them know about it.
18431158dfb7SGarrett Wollman 	 */
18441158dfb7SGarrett Wollman 	s = splimp();
18451158dfb7SGarrett Wollman 	ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
18461158dfb7SGarrett Wollman 	splx(s);
18471158dfb7SGarrett Wollman 
18481158dfb7SGarrett Wollman 	return 0;
18491158dfb7SGarrett Wollman }
18501158dfb7SGarrett Wollman 
18511158dfb7SGarrett Wollman /*
18521158dfb7SGarrett Wollman  * Remove a reference to a multicast address on this interface.  Yell
18531158dfb7SGarrett Wollman  * if the request does not match an existing membership.
18541158dfb7SGarrett Wollman  */
18551158dfb7SGarrett Wollman int
18561158dfb7SGarrett Wollman if_delmulti(ifp, sa)
18571158dfb7SGarrett Wollman 	struct ifnet *ifp;
18581158dfb7SGarrett Wollman 	struct sockaddr *sa;
18591158dfb7SGarrett Wollman {
18601158dfb7SGarrett Wollman 	struct ifmultiaddr *ifma;
18611158dfb7SGarrett Wollman 	int s;
18621158dfb7SGarrett Wollman 
18636817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
18641158dfb7SGarrett Wollman 		if (equal(sa, ifma->ifma_addr))
18651158dfb7SGarrett Wollman 			break;
18661158dfb7SGarrett Wollman 	if (ifma == 0)
18671158dfb7SGarrett Wollman 		return ENOENT;
18681158dfb7SGarrett Wollman 
18691158dfb7SGarrett Wollman 	if (ifma->ifma_refcount > 1) {
18701158dfb7SGarrett Wollman 		ifma->ifma_refcount--;
18711158dfb7SGarrett Wollman 		return 0;
18721158dfb7SGarrett Wollman 	}
18731158dfb7SGarrett Wollman 
1874477180fbSGarrett Wollman 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
18751158dfb7SGarrett Wollman 	sa = ifma->ifma_lladdr;
18761158dfb7SGarrett Wollman 	s = splimp();
18776817526dSPoul-Henning Kamp 	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
1878ccb7cc8dSYaroslav Tykhiy 	/*
1879ccb7cc8dSYaroslav Tykhiy 	 * Make sure the interface driver is notified
1880ccb7cc8dSYaroslav Tykhiy 	 * in the case of a link layer mcast group being left.
1881ccb7cc8dSYaroslav Tykhiy 	 */
1882ccb7cc8dSYaroslav Tykhiy 	if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0)
1883ccb7cc8dSYaroslav Tykhiy 		ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
18841158dfb7SGarrett Wollman 	splx(s);
18851158dfb7SGarrett Wollman 	free(ifma->ifma_addr, M_IFMADDR);
18861158dfb7SGarrett Wollman 	free(ifma, M_IFMADDR);
18871158dfb7SGarrett Wollman 	if (sa == 0)
18881158dfb7SGarrett Wollman 		return 0;
18891158dfb7SGarrett Wollman 
18901158dfb7SGarrett Wollman 	/*
18911158dfb7SGarrett Wollman 	 * Now look for the link-layer address which corresponds to
18921158dfb7SGarrett Wollman 	 * this network address.  It had been squirreled away in
18931158dfb7SGarrett Wollman 	 * ifma->ifma_lladdr for this purpose (so we don't have
18941158dfb7SGarrett Wollman 	 * to call ifp->if_resolvemulti() again), and we saved that
18951158dfb7SGarrett Wollman 	 * value in sa above.  If some nasty deleted the
18961158dfb7SGarrett Wollman 	 * link-layer address out from underneath us, we can deal because
18971158dfb7SGarrett Wollman 	 * the address we stored was is not the same as the one which was
18981158dfb7SGarrett Wollman 	 * in the record for the link-layer address.  (So we don't complain
18991158dfb7SGarrett Wollman 	 * in that case.)
19001158dfb7SGarrett Wollman 	 */
19016817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
19021158dfb7SGarrett Wollman 		if (equal(sa, ifma->ifma_addr))
19031158dfb7SGarrett Wollman 			break;
19041158dfb7SGarrett Wollman 	if (ifma == 0)
19051158dfb7SGarrett Wollman 		return 0;
19061158dfb7SGarrett Wollman 
19071158dfb7SGarrett Wollman 	if (ifma->ifma_refcount > 1) {
19081158dfb7SGarrett Wollman 		ifma->ifma_refcount--;
19091158dfb7SGarrett Wollman 		return 0;
19101158dfb7SGarrett Wollman 	}
19111158dfb7SGarrett Wollman 
19121158dfb7SGarrett Wollman 	s = splimp();
19136817526dSPoul-Henning Kamp 	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
1914c7323482SBill Paul 	ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
19151158dfb7SGarrett Wollman 	splx(s);
19161158dfb7SGarrett Wollman 	free(ifma->ifma_addr, M_IFMADDR);
19171158dfb7SGarrett Wollman 	free(sa, M_IFMADDR);
19181158dfb7SGarrett Wollman 	free(ifma, M_IFMADDR);
19191158dfb7SGarrett Wollman 
19201158dfb7SGarrett Wollman 	return 0;
19211158dfb7SGarrett Wollman }
19221158dfb7SGarrett Wollman 
192366ce51ceSArchie Cobbs /*
192466ce51ceSArchie Cobbs  * Set the link layer address on an interface.
192566ce51ceSArchie Cobbs  *
192666ce51ceSArchie Cobbs  * At this time we only support certain types of interfaces,
192766ce51ceSArchie Cobbs  * and we don't allow the length of the address to change.
192866ce51ceSArchie Cobbs  */
192966ce51ceSArchie Cobbs int
193066ce51ceSArchie Cobbs if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
193166ce51ceSArchie Cobbs {
193266ce51ceSArchie Cobbs 	struct sockaddr_dl *sdl;
193366ce51ceSArchie Cobbs 	struct ifaddr *ifa;
1934d637e989SPeter Wemm 	struct ifreq ifr;
193566ce51ceSArchie Cobbs 
1936f9132cebSJonathan Lemon 	ifa = ifaddr_byindex(ifp->if_index);
193766ce51ceSArchie Cobbs 	if (ifa == NULL)
193866ce51ceSArchie Cobbs 		return (EINVAL);
193966ce51ceSArchie Cobbs 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
194066ce51ceSArchie Cobbs 	if (sdl == NULL)
194166ce51ceSArchie Cobbs 		return (EINVAL);
194266ce51ceSArchie Cobbs 	if (len != sdl->sdl_alen)	/* don't allow length to change */
194366ce51ceSArchie Cobbs 		return (EINVAL);
194466ce51ceSArchie Cobbs 	switch (ifp->if_type) {
194566ce51ceSArchie Cobbs 	case IFT_ETHER:			/* these types use struct arpcom */
194666ce51ceSArchie Cobbs 	case IFT_FDDI:
194766ce51ceSArchie Cobbs 	case IFT_XETHER:
194866ce51ceSArchie Cobbs 	case IFT_ISO88025:
1949b7bffa71SYaroslav Tykhiy 	case IFT_L2VLAN:
195066ce51ceSArchie Cobbs 		bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
195166ce51ceSArchie Cobbs 		bcopy(lladdr, LLADDR(sdl), len);
195266ce51ceSArchie Cobbs 		break;
195366ce51ceSArchie Cobbs 	default:
195466ce51ceSArchie Cobbs 		return (ENODEV);
195566ce51ceSArchie Cobbs 	}
195666ce51ceSArchie Cobbs 	/*
195766ce51ceSArchie Cobbs 	 * If the interface is already up, we need
195866ce51ceSArchie Cobbs 	 * to re-init it in order to reprogram its
195966ce51ceSArchie Cobbs 	 * address filter.
196066ce51ceSArchie Cobbs 	 */
196166ce51ceSArchie Cobbs 	if ((ifp->if_flags & IFF_UP) != 0) {
196266ce51ceSArchie Cobbs 		ifp->if_flags &= ~IFF_UP;
196362f76486SMaxim Sobolev 		ifr.ifr_flags = ifp->if_flags & 0xffff;
196462f76486SMaxim Sobolev 		ifr.ifr_flagshigh = ifp->if_flags >> 16;
1965ee0a4f7eSSUZUKI Shinsuke 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
196666ce51ceSArchie Cobbs 		ifp->if_flags |= IFF_UP;
196762f76486SMaxim Sobolev 		ifr.ifr_flags = ifp->if_flags & 0xffff;
196862f76486SMaxim Sobolev 		ifr.ifr_flagshigh = ifp->if_flags >> 16;
1969ee0a4f7eSSUZUKI Shinsuke 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1970b2c08f43SLuigi Rizzo #ifdef INET
1971b2c08f43SLuigi Rizzo 		/*
1972b2c08f43SLuigi Rizzo 		 * Also send gratuitous ARPs to notify other nodes about
1973b2c08f43SLuigi Rizzo 		 * the address change.
1974b2c08f43SLuigi Rizzo 		 */
1975b2c08f43SLuigi Rizzo 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1976b2c08f43SLuigi Rizzo 			if (ifa->ifa_addr != NULL &&
1977b2c08f43SLuigi Rizzo 			    ifa->ifa_addr->sa_family == AF_INET)
1978c0933269SPeter Wemm 				arp_ifinit(ifp, ifa);
1979b2c08f43SLuigi Rizzo 		}
1980b2c08f43SLuigi Rizzo #endif
198166ce51ceSArchie Cobbs 	}
198266ce51ceSArchie Cobbs 	return (0);
198366ce51ceSArchie Cobbs }
198466ce51ceSArchie Cobbs 
1985373f88edSGarrett Wollman struct ifmultiaddr *
1986373f88edSGarrett Wollman ifmaof_ifpforaddr(sa, ifp)
1987373f88edSGarrett Wollman 	struct sockaddr *sa;
1988373f88edSGarrett Wollman 	struct ifnet *ifp;
1989373f88edSGarrett Wollman {
1990373f88edSGarrett Wollman 	struct ifmultiaddr *ifma;
1991373f88edSGarrett Wollman 
19926817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1993373f88edSGarrett Wollman 		if (equal(ifma->ifma_addr, sa))
1994373f88edSGarrett Wollman 			break;
1995373f88edSGarrett Wollman 
1996373f88edSGarrett Wollman 	return ifma;
1997373f88edSGarrett Wollman }
1998373f88edSGarrett Wollman 
1999fa882e87SBrooks Davis int
2000fa882e87SBrooks Davis if_printf(struct ifnet *ifp, const char * fmt, ...)
2001fa882e87SBrooks Davis {
2002fa882e87SBrooks Davis 	va_list ap;
2003fa882e87SBrooks Davis 	int retval;
2004fa882e87SBrooks Davis 
2005fa882e87SBrooks Davis 	retval = printf("%s%d: ", ifp->if_name, ifp->if_unit);
2006fa882e87SBrooks Davis 	va_start(ap, fmt);
2007fa882e87SBrooks Davis 	retval += vprintf(fmt, ap);
2008fa882e87SBrooks Davis 	va_end(ap);
2009fa882e87SBrooks Davis 	return (retval);
2010fa882e87SBrooks Davis }
2011fa882e87SBrooks Davis 
2012602d513cSGarrett Wollman SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
20132c37256eSGarrett Wollman SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
2014