xref: /freebsd/sys/net/if.c (revision 62f76486821669cfb9ceafa372dcdc7eb15d4e3c)
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>
58df8bae1dSRodney W. Grimes 
59df8bae1dSRodney W. Grimes #include <net/if.h>
60b106252cSBill Paul #include <net/if_arp.h>
61df8bae1dSRodney W. Grimes #include <net/if_dl.h>
6266ce51ceSArchie Cobbs #include <net/if_types.h>
6330aad87dSBrooks Davis #include <net/if_var.h>
649448326fSPoul-Henning Kamp #include <net/radix.h>
655500d3beSWarner Losh #include <net/route.h>
66df8bae1dSRodney W. Grimes 
670d0f9d1eSYoshinobu Inoue #if defined(INET) || defined(INET6)
6882cd038dSYoshinobu Inoue /*XXX*/
6982cd038dSYoshinobu Inoue #include <netinet/in.h>
700d0f9d1eSYoshinobu Inoue #include <netinet/in_var.h>
713411310dSYoshinobu Inoue #ifdef INET6
72978ee2edSJun-ichiro itojun Hagino #include <netinet6/in6_var.h>
73978ee2edSJun-ichiro itojun Hagino #include <netinet6/in6_ifattach.h>
743411310dSYoshinobu Inoue #endif
7582cd038dSYoshinobu Inoue #endif
76c0933269SPeter Wemm #ifdef INET
77c0933269SPeter Wemm #include <netinet/if_ether.h>
78c0933269SPeter Wemm #endif
7982cd038dSYoshinobu Inoue 
800b59d917SJonathan Lemon static int	ifconf(u_long, caddr_t);
81f9132cebSJonathan Lemon static void	if_grow(void);
82f9132cebSJonathan Lemon static void	if_init(void *);
83f9132cebSJonathan Lemon static void	if_check(void *);
84ffb5a104SJonathan Lemon static int	if_findindex(struct ifnet *);
850b59d917SJonathan Lemon static void	if_qflush(struct ifqueue *);
860b59d917SJonathan Lemon static void	if_slowtimo(void *);
878071913dSRuslan Ermilov static void	link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
880b59d917SJonathan Lemon static int	if_rtdel(struct radix_node *, void *);
890b59d917SJonathan Lemon static struct	if_clone *if_clone_lookup(const char *, int *);
900b59d917SJonathan Lemon static int	if_clone_list(struct if_clonereq *);
91f13ad206SJonathan Lemon static int	ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
9282cd038dSYoshinobu Inoue #ifdef INET6
9382cd038dSYoshinobu Inoue /*
9482cd038dSYoshinobu Inoue  * XXX: declare here to avoid to include many inet6 related files..
9582cd038dSYoshinobu Inoue  * should be more generalized?
9682cd038dSYoshinobu Inoue  */
97929ddbbbSAlfred Perlstein extern void	nd6_setmtu(struct ifnet *);
9882cd038dSYoshinobu Inoue #endif
9982cd038dSYoshinobu Inoue 
1000b59d917SJonathan Lemon int	if_index = 0;
101f9132cebSJonathan Lemon struct	ifindex_entry *ifindex_table = NULL;
1020b59d917SJonathan Lemon int	ifqmaxlen = IFQ_MAXLEN;
1030b59d917SJonathan Lemon struct	ifnethead ifnet;	/* depend on static init XXX */
10430aad87dSBrooks Davis int	if_cloners_count;
1050b59d917SJonathan Lemon LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
1060b59d917SJonathan Lemon 
107f9132cebSJonathan Lemon static int	if_indexlim = 8;
108f13ad206SJonathan Lemon static struct	klist ifklist;
109f9132cebSJonathan Lemon 
1109a2a57a1SJonathan Lemon static void	filt_netdetach(struct knote *kn);
1119a2a57a1SJonathan Lemon static int	filt_netdev(struct knote *kn, long hint);
1129a2a57a1SJonathan Lemon 
1139a2a57a1SJonathan Lemon static struct filterops netdev_filtops =
1149a2a57a1SJonathan Lemon     { 1, NULL, filt_netdetach, filt_netdev };
1159a2a57a1SJonathan Lemon 
1160b59d917SJonathan Lemon /*
1170b59d917SJonathan Lemon  * System initialization
1180b59d917SJonathan Lemon  */
119f9132cebSJonathan Lemon SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL)
120f9132cebSJonathan Lemon SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL)
1210b59d917SJonathan Lemon 
1220b59d917SJonathan Lemon MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
1230b59d917SJonathan Lemon MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
1243b16e7b2SMaxime Henrion MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
12530aad87dSBrooks Davis 
126f13ad206SJonathan Lemon #define CDEV_MAJOR	165
127f13ad206SJonathan Lemon 
128f13ad206SJonathan Lemon static d_open_t		netopen;
129f13ad206SJonathan Lemon static d_close_t	netclose;
130f13ad206SJonathan Lemon static d_ioctl_t	netioctl;
1319a2a57a1SJonathan Lemon static d_kqfilter_t	netkqfilter;
132f13ad206SJonathan Lemon 
133f13ad206SJonathan Lemon static struct cdevsw net_cdevsw = {
134f13ad206SJonathan Lemon 	/* open */	netopen,
135f13ad206SJonathan Lemon 	/* close */	netclose,
136f13ad206SJonathan Lemon 	/* read */	noread,
137f13ad206SJonathan Lemon 	/* write */	nowrite,
138f13ad206SJonathan Lemon 	/* ioctl */	netioctl,
139f13ad206SJonathan Lemon 	/* poll */	nopoll,
140f13ad206SJonathan Lemon 	/* mmap */	nommap,
141f13ad206SJonathan Lemon 	/* strategy */	nostrategy,
142f13ad206SJonathan Lemon 	/* name */	"net",
143f13ad206SJonathan Lemon 	/* maj */	CDEV_MAJOR,
144f13ad206SJonathan Lemon 	/* dump */	nodump,
145f13ad206SJonathan Lemon 	/* psize */	nopsize,
1469a2a57a1SJonathan Lemon 	/* flags */	D_KQFILTER,
1479a2a57a1SJonathan Lemon 	/* kqfilter */	netkqfilter,
148f13ad206SJonathan Lemon };
149f13ad206SJonathan Lemon 
150f13ad206SJonathan Lemon static int
151f13ad206SJonathan Lemon netopen(dev_t dev, int flag, int mode, struct thread *td)
152f13ad206SJonathan Lemon {
153f13ad206SJonathan Lemon 	return (0);
154f13ad206SJonathan Lemon }
155f13ad206SJonathan Lemon 
156f13ad206SJonathan Lemon static int
157f13ad206SJonathan Lemon netclose(dev_t dev, int flags, int fmt, struct thread *td)
158f13ad206SJonathan Lemon {
159f13ad206SJonathan Lemon 	return (0);
160f13ad206SJonathan Lemon }
161f13ad206SJonathan Lemon 
162f13ad206SJonathan Lemon static int
163f13ad206SJonathan Lemon netioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
164f13ad206SJonathan Lemon {
165f13ad206SJonathan Lemon 	struct ifnet *ifp;
166f13ad206SJonathan Lemon 	int error, idx;
167f13ad206SJonathan Lemon 
168f13ad206SJonathan Lemon 	/* only support interface specific ioctls */
169f13ad206SJonathan Lemon 	if (IOCGROUP(cmd) != 'i')
170f13ad206SJonathan Lemon 		return (EOPNOTSUPP);
171f13ad206SJonathan Lemon 	idx = minor(dev);
172f13ad206SJonathan Lemon 	if (idx == 0) {
173f13ad206SJonathan Lemon 		/*
174f13ad206SJonathan Lemon 		 * special network device, not interface.
175f13ad206SJonathan Lemon 		 */
176f13ad206SJonathan Lemon 		if (cmd == SIOCGIFCONF)
177f13ad206SJonathan Lemon 			return (ifconf(cmd, data));	/* XXX remove cmd */
178f13ad206SJonathan Lemon 		return (EOPNOTSUPP);
179f13ad206SJonathan Lemon 	}
180f13ad206SJonathan Lemon 
181f13ad206SJonathan Lemon 	ifp = ifnet_byindex(idx);
182f13ad206SJonathan Lemon 	if (ifp == NULL)
183f13ad206SJonathan Lemon 		return (ENXIO);
184f13ad206SJonathan Lemon 
185f13ad206SJonathan Lemon 	error = ifhwioctl(cmd, ifp, data, td);
186f13ad206SJonathan Lemon 	if (error == ENOIOCTL)
187f13ad206SJonathan Lemon 		error = EOPNOTSUPP;
188f13ad206SJonathan Lemon 	return (error);
189f13ad206SJonathan Lemon }
190f13ad206SJonathan Lemon 
1919a2a57a1SJonathan Lemon static int
1929a2a57a1SJonathan Lemon netkqfilter(dev_t dev, struct knote *kn)
1939a2a57a1SJonathan Lemon {
1949a2a57a1SJonathan Lemon 	struct klist *klist;
1959a2a57a1SJonathan Lemon 	struct ifnet *ifp;
1969a2a57a1SJonathan Lemon 	int idx;
1979a2a57a1SJonathan Lemon 
1989a2a57a1SJonathan Lemon 	idx = minor(dev);
1999a2a57a1SJonathan Lemon 	if (idx == 0) {
2009a2a57a1SJonathan Lemon 		klist = &ifklist;
2019a2a57a1SJonathan Lemon 	} else {
2029a2a57a1SJonathan Lemon 		ifp = ifnet_byindex(idx);
2039a2a57a1SJonathan Lemon 		if (ifp == NULL)
2049a2a57a1SJonathan Lemon 			return (1);
2059a2a57a1SJonathan Lemon 		klist = &ifp->if_klist;
2069a2a57a1SJonathan Lemon 	}
2079a2a57a1SJonathan Lemon 
2089a2a57a1SJonathan Lemon 	switch (kn->kn_filter) {
2099a2a57a1SJonathan Lemon 	case EVFILT_NETDEV:
2109a2a57a1SJonathan Lemon 		kn->kn_fop = &netdev_filtops;
2119a2a57a1SJonathan Lemon 		break;
2129a2a57a1SJonathan Lemon 	default:
2139a2a57a1SJonathan Lemon 		return (1);
2149a2a57a1SJonathan Lemon 	}
2159a2a57a1SJonathan Lemon 
2169a2a57a1SJonathan Lemon 	kn->kn_hook = (caddr_t)klist;
2179a2a57a1SJonathan Lemon 
2189a2a57a1SJonathan Lemon 	/* XXX locking? */
2199a2a57a1SJonathan Lemon 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
2209a2a57a1SJonathan Lemon 
2219a2a57a1SJonathan Lemon 	return (0);
2229a2a57a1SJonathan Lemon }
2239a2a57a1SJonathan Lemon 
2249a2a57a1SJonathan Lemon static void
2259a2a57a1SJonathan Lemon filt_netdetach(struct knote *kn)
2269a2a57a1SJonathan Lemon {
2279a2a57a1SJonathan Lemon 	struct klist *klist = (struct klist *)kn->kn_hook;
2289a2a57a1SJonathan Lemon 
2299a2a57a1SJonathan Lemon 	if (kn->kn_status & KN_DETACHED)
2309a2a57a1SJonathan Lemon 		return;
2319a2a57a1SJonathan Lemon 	SLIST_REMOVE(klist, kn, knote, kn_selnext);
2329a2a57a1SJonathan Lemon }
2339a2a57a1SJonathan Lemon 
2349a2a57a1SJonathan Lemon static int
2359a2a57a1SJonathan Lemon filt_netdev(struct knote *kn, long hint)
2369a2a57a1SJonathan Lemon {
2379a2a57a1SJonathan Lemon 
2389a2a57a1SJonathan Lemon 	/*
2399a2a57a1SJonathan Lemon 	 * Currently NOTE_EXIT is abused to indicate device detach.
2409a2a57a1SJonathan Lemon 	 */
2419a2a57a1SJonathan Lemon 	if (hint == NOTE_EXIT) {
2429a2a57a1SJonathan Lemon 		kn->kn_data = NOTE_LINKINV;
2439a2a57a1SJonathan Lemon                 kn->kn_status |= KN_DETACHED;
2449a2a57a1SJonathan Lemon                 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2459a2a57a1SJonathan Lemon                 return (1);
2469a2a57a1SJonathan Lemon         }
2479a2a57a1SJonathan Lemon 	kn->kn_data = hint;			/* current status */
2489a2a57a1SJonathan Lemon 	if (kn->kn_sfflags & hint)
2499a2a57a1SJonathan Lemon 		kn->kn_fflags |= hint;
2509a2a57a1SJonathan Lemon 	return (kn->kn_fflags != 0);
2519a2a57a1SJonathan Lemon }
2529a2a57a1SJonathan Lemon 
253df8bae1dSRodney W. Grimes /*
254df8bae1dSRodney W. Grimes  * Network interface utility routines.
255df8bae1dSRodney W. Grimes  *
256df8bae1dSRodney W. Grimes  * Routines with ifa_ifwith* names take sockaddr *'s as
257df8bae1dSRodney W. Grimes  * parameters.
258df8bae1dSRodney W. Grimes  */
2592b14f991SJulian Elischer /* ARGSUSED*/
260f9132cebSJonathan Lemon static void
261f9132cebSJonathan Lemon if_init(dummy)
262f9132cebSJonathan Lemon 	void *dummy;
263f9132cebSJonathan Lemon {
264f9132cebSJonathan Lemon 
265f9132cebSJonathan Lemon 	TAILQ_INIT(&ifnet);
266f13ad206SJonathan Lemon 	SLIST_INIT(&ifklist);
267f9132cebSJonathan Lemon 	if_grow();				/* create initial table */
268f13ad206SJonathan Lemon 	ifdev_byindex(0) = make_dev(&net_cdevsw, 0,
269f13ad206SJonathan Lemon 	    UID_ROOT, GID_WHEEL, 0600, "network");
270f9132cebSJonathan Lemon }
271f9132cebSJonathan Lemon 
272f9132cebSJonathan Lemon static void
273f9132cebSJonathan Lemon if_grow(void)
274f9132cebSJonathan Lemon {
275f9132cebSJonathan Lemon 	u_int n;
276f9132cebSJonathan Lemon 	struct ifindex_entry *e;
277f9132cebSJonathan Lemon 
278f9132cebSJonathan Lemon 	if_indexlim <<= 1;
279f9132cebSJonathan Lemon 	n = if_indexlim * sizeof(*e);
280f9132cebSJonathan Lemon 	e = malloc(n, M_IFADDR, M_WAITOK | M_ZERO);
281f9132cebSJonathan Lemon 	if (ifindex_table != NULL) {
282f9132cebSJonathan Lemon 		memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2);
283f9132cebSJonathan Lemon 		free((caddr_t)ifindex_table, M_IFADDR);
284f9132cebSJonathan Lemon 	}
285f9132cebSJonathan Lemon 	ifindex_table = e;
286f9132cebSJonathan Lemon }
287f9132cebSJonathan Lemon 
288f9132cebSJonathan Lemon /* ARGSUSED*/
289f9132cebSJonathan Lemon static void
290f9132cebSJonathan Lemon if_check(dummy)
29127501cb6SBruce Evans 	void *dummy;
292df8bae1dSRodney W. Grimes {
2938ba5bdaeSPeter Wemm 	struct ifnet *ifp;
2948ba5bdaeSPeter Wemm 	int s;
295df8bae1dSRodney W. Grimes 
2968ba5bdaeSPeter Wemm 	s = splimp();
297fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
298e0ea20bcSPoul-Henning Kamp 		if (ifp->if_snd.ifq_maxlen == 0) {
299e0ea20bcSPoul-Henning Kamp 			printf("%s%d XXX: driver didn't set ifq_maxlen\n",
300e0ea20bcSPoul-Henning Kamp 			    ifp->if_name, ifp->if_unit);
301df8bae1dSRodney W. Grimes 			ifp->if_snd.ifq_maxlen = ifqmaxlen;
302e0ea20bcSPoul-Henning Kamp 		}
3035e980e22SJohn Baldwin 		if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) {
304df5e1987SJonathan Lemon 			printf("%s%d XXX: driver didn't initialize queue mtx\n",
305df5e1987SJonathan Lemon 			    ifp->if_name, ifp->if_unit);
3066008862bSJohn Baldwin 			mtx_init(&ifp->if_snd.ifq_mtx, "unknown",
3076008862bSJohn Baldwin 			    MTX_NETWORK_LOCK, MTX_DEF);
308df5e1987SJonathan Lemon 		}
309df5e1987SJonathan Lemon 	}
3108ba5bdaeSPeter Wemm 	splx(s);
311df8bae1dSRodney W. Grimes 	if_slowtimo(0);
312df8bae1dSRodney W. Grimes }
313df8bae1dSRodney W. Grimes 
314ffb5a104SJonathan Lemon static int
315ffb5a104SJonathan Lemon if_findindex(struct ifnet *ifp)
316ffb5a104SJonathan Lemon {
317ffb5a104SJonathan Lemon 	int i, unit;
318ffb5a104SJonathan Lemon 	char eaddr[18], devname[32];
319d2b4566aSJonathan Lemon 	const char *name, *p;
320ffb5a104SJonathan Lemon 
321ffb5a104SJonathan Lemon 	switch (ifp->if_type) {
322ffb5a104SJonathan Lemon 	case IFT_ETHER:			/* these types use struct arpcom */
323ffb5a104SJonathan Lemon 	case IFT_FDDI:
324ffb5a104SJonathan Lemon 	case IFT_XETHER:
325ffb5a104SJonathan Lemon 	case IFT_ISO88025:
326ffb5a104SJonathan Lemon 	case IFT_L2VLAN:
327ffb5a104SJonathan Lemon 		snprintf(eaddr, 18, "%6D",
328ffb5a104SJonathan Lemon 		    ((struct arpcom *)ifp->if_softc)->ac_enaddr, ":");
329ffb5a104SJonathan Lemon 		break;
330ffb5a104SJonathan Lemon 	default:
331ffb5a104SJonathan Lemon 		eaddr[0] = '\0';
332ffb5a104SJonathan Lemon 		break;
333ffb5a104SJonathan Lemon 	}
334ffb5a104SJonathan Lemon 	snprintf(devname, 32, "%s%d", ifp->if_name, ifp->if_unit);
335ffb5a104SJonathan Lemon 	name = net_cdevsw.d_name;
336ffb5a104SJonathan Lemon 	i = 0;
337ffb5a104SJonathan Lemon 	while ((resource_find_dev(&i, name, &unit, NULL, NULL)) == 0) {
338ffb5a104SJonathan Lemon 		if (resource_string_value(name, unit, "ether", &p) == 0)
339ffb5a104SJonathan Lemon 			if (strcmp(p, eaddr) == 0)
340ffb5a104SJonathan Lemon 				goto found;
341ffb5a104SJonathan Lemon 		if (resource_string_value(name, unit, "dev", &p) == 0)
342ffb5a104SJonathan Lemon 			if (strcmp(p, devname) == 0)
343ffb5a104SJonathan Lemon 				goto found;
344ffb5a104SJonathan Lemon 	}
345ffb5a104SJonathan Lemon 	unit = 0;
346ffb5a104SJonathan Lemon found:
347ffb5a104SJonathan Lemon 	if (unit != 0) {
348ffb5a104SJonathan Lemon 		if (ifaddr_byindex(unit) == NULL)
349ffb5a104SJonathan Lemon 			return (unit);
350ffb5a104SJonathan Lemon 		printf("%s%d in use, cannot hardwire it to %s.\n",
351ffb5a104SJonathan Lemon 		    name, unit, devname);
352ffb5a104SJonathan Lemon 	}
353ffb5a104SJonathan Lemon 	for (unit = 1; ; unit++) {
35405153c61SBill Fenner 		if (unit <= if_index && ifaddr_byindex(unit) != NULL)
355ffb5a104SJonathan Lemon 			continue;
356ffb5a104SJonathan Lemon 		if (resource_string_value(name, unit, "ether", &p) == 0 ||
357ffb5a104SJonathan Lemon 		    resource_string_value(name, unit, "dev", &p) == 0)
358ffb5a104SJonathan Lemon 			continue;
359ffb5a104SJonathan Lemon 		break;
360ffb5a104SJonathan Lemon 	}
361ffb5a104SJonathan Lemon 	return (unit);
362ffb5a104SJonathan Lemon }
363ffb5a104SJonathan Lemon 
364df8bae1dSRodney W. Grimes /*
365df8bae1dSRodney W. Grimes  * Attach an interface to the
366df8bae1dSRodney W. Grimes  * list of "active" interfaces.
367df8bae1dSRodney W. Grimes  */
368df8bae1dSRodney W. Grimes void
369df8bae1dSRodney W. Grimes if_attach(ifp)
370df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
371df8bae1dSRodney W. Grimes {
372df8bae1dSRodney W. Grimes 	unsigned socksize, ifasize;
3731ce9bf88SPoul-Henning Kamp 	int namelen, masklen;
3741ce9bf88SPoul-Henning Kamp 	char workbuf[64];
375df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
376df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
377df8bae1dSRodney W. Grimes 
37829412182SGarrett Wollman 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
37959562606SGarrett Wollman 	/*
38059562606SGarrett Wollman 	 * XXX -
38159562606SGarrett Wollman 	 * The old code would work if the interface passed a pre-existing
38259562606SGarrett Wollman 	 * chain of ifaddrs to this code.  We don't trust our callers to
38359562606SGarrett Wollman 	 * properly initialize the tailq, however, so we no longer allow
38459562606SGarrett Wollman 	 * this unlikely case.
38559562606SGarrett Wollman 	 */
38659562606SGarrett Wollman 	TAILQ_INIT(&ifp->if_addrhead);
38782cd038dSYoshinobu Inoue 	TAILQ_INIT(&ifp->if_prefixhead);
3886817526dSPoul-Henning Kamp 	TAILQ_INIT(&ifp->if_multiaddrs);
3899a2a57a1SJonathan Lemon 	SLIST_INIT(&ifp->if_klist);
39098b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
391e70cd263SRobert Watson 
392e70cd263SRobert Watson #ifdef MAC
393e70cd263SRobert Watson 	mac_init_ifnet(ifp);
394e70cd263SRobert Watson 	mac_create_ifnet(ifp);
395e70cd263SRobert Watson #endif
396e70cd263SRobert Watson 
397ffb5a104SJonathan Lemon 	ifp->if_index = if_findindex(ifp);
39805153c61SBill Fenner 	if (ifp->if_index > if_index)
39905153c61SBill Fenner 		if_index = ifp->if_index;
400f9132cebSJonathan Lemon 	if (if_index >= if_indexlim)
401f9132cebSJonathan Lemon 		if_grow();
40282cd038dSYoshinobu Inoue 
403ffb5a104SJonathan Lemon 	ifnet_byindex(ifp->if_index) = ifp;
404ffb5a104SJonathan Lemon 	ifdev_byindex(ifp->if_index) = make_dev(&net_cdevsw, ifp->if_index,
405ffb5a104SJonathan Lemon 	    UID_ROOT, GID_WHEEL, 0600, "%s/%s%d",
406ffb5a104SJonathan Lemon 	    net_cdevsw.d_name, ifp->if_name, ifp->if_unit);
407ffb5a104SJonathan Lemon 	make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
408ffb5a104SJonathan Lemon 	    net_cdevsw.d_name, ifp->if_index);
409f13ad206SJonathan Lemon 
4106008862bSJohn Baldwin 	mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_name, "if send queue", MTX_DEF);
411df5e1987SJonathan Lemon 
412df8bae1dSRodney W. Grimes 	/*
413df8bae1dSRodney W. Grimes 	 * create a Link Level name for this device
414df8bae1dSRodney W. Grimes 	 */
4152127f260SArchie Cobbs 	namelen = snprintf(workbuf, sizeof(workbuf),
4162127f260SArchie Cobbs 	    "%s%d", ifp->if_name, ifp->if_unit);
417df8bae1dSRodney W. Grimes #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
4181ce9bf88SPoul-Henning Kamp 	masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
419df8bae1dSRodney W. Grimes 	socksize = masklen + ifp->if_addrlen;
420df8bae1dSRodney W. Grimes #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
421df8bae1dSRodney W. Grimes 	if (socksize < sizeof(*sdl))
422df8bae1dSRodney W. Grimes 		socksize = sizeof(*sdl);
4238a261b8fSDoug Rabson 	socksize = ROUNDUP(socksize);
424df8bae1dSRodney W. Grimes 	ifasize = sizeof(*ifa) + 2 * socksize;
4257cc0979fSDavid Malone 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
4269448326fSPoul-Henning Kamp 	if (ifa) {
427df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(ifa + 1);
428df8bae1dSRodney W. Grimes 		sdl->sdl_len = socksize;
429df8bae1dSRodney W. Grimes 		sdl->sdl_family = AF_LINK;
4301ce9bf88SPoul-Henning Kamp 		bcopy(workbuf, sdl->sdl_data, namelen);
4311ce9bf88SPoul-Henning Kamp 		sdl->sdl_nlen = namelen;
432df8bae1dSRodney W. Grimes 		sdl->sdl_index = ifp->if_index;
433df8bae1dSRodney W. Grimes 		sdl->sdl_type = ifp->if_type;
434ffb5a104SJonathan Lemon 		ifaddr_byindex(ifp->if_index) = ifa;
435df8bae1dSRodney W. Grimes 		ifa->ifa_ifp = ifp;
436df8bae1dSRodney W. Grimes 		ifa->ifa_rtrequest = link_rtrequest;
437df8bae1dSRodney W. Grimes 		ifa->ifa_addr = (struct sockaddr *)sdl;
438df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
439df8bae1dSRodney W. Grimes 		ifa->ifa_netmask = (struct sockaddr *)sdl;
440df8bae1dSRodney W. Grimes 		sdl->sdl_len = masklen;
441df8bae1dSRodney W. Grimes 		while (namelen != 0)
442df8bae1dSRodney W. Grimes 			sdl->sdl_data[--namelen] = 0xff;
44359562606SGarrett Wollman 		TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
444df8bae1dSRodney W. Grimes 	}
445322dcb8dSMax Khon 	ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
4467b6edd04SRuslan Ermilov 
4477b6edd04SRuslan Ermilov 	/* Announce the interface. */
4487b6edd04SRuslan Ermilov 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
449df8bae1dSRodney W. Grimes }
4506182fdbdSPeter Wemm 
4516182fdbdSPeter Wemm /*
4526182fdbdSPeter Wemm  * Detach an interface, removing it from the
4536182fdbdSPeter Wemm  * list of "active" interfaces.
4546182fdbdSPeter Wemm  */
4556182fdbdSPeter Wemm void
4566182fdbdSPeter Wemm if_detach(ifp)
4576182fdbdSPeter Wemm 	struct ifnet *ifp;
4586182fdbdSPeter Wemm {
4596182fdbdSPeter Wemm 	struct ifaddr *ifa;
4605500d3beSWarner Losh 	struct radix_node_head	*rnh;
4615500d3beSWarner Losh 	int s;
4625500d3beSWarner Losh 	int i;
4636182fdbdSPeter Wemm 
4646182fdbdSPeter Wemm 	/*
4656182fdbdSPeter Wemm 	 * Remove routes and flush queues.
4666182fdbdSPeter Wemm 	 */
4675500d3beSWarner Losh 	s = splnet();
4686182fdbdSPeter Wemm 	if_down(ifp);
4696182fdbdSPeter Wemm 
4706182fdbdSPeter Wemm 	/*
471f9132cebSJonathan Lemon 	 * Remove address from ifindex_table[] and maybe decrement if_index.
4726182fdbdSPeter Wemm 	 * Clean up all addresses.
4736182fdbdSPeter Wemm 	 */
474f9132cebSJonathan Lemon 	ifaddr_byindex(ifp->if_index) = NULL;
4750346e973SBrian Feldman 	revoke_and_destroy_dev(ifdev_byindex(ifp->if_index));
476f13ad206SJonathan Lemon 	ifdev_byindex(ifp->if_index) = NULL;
477f9132cebSJonathan Lemon 
478f9132cebSJonathan Lemon 	while (if_index > 0 && ifaddr_byindex(if_index) == NULL)
4796182fdbdSPeter Wemm 		if_index--;
4806182fdbdSPeter Wemm 
4816182fdbdSPeter Wemm 	for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
4826182fdbdSPeter Wemm 	     ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
4830d0f9d1eSYoshinobu Inoue #ifdef INET
484aa6be122SWarner Losh 		/* XXX: Ugly!! ad hoc just for INET */
485aa6be122SWarner Losh 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
486aa6be122SWarner Losh 			struct ifaliasreq ifr;
487aa6be122SWarner Losh 
488aa6be122SWarner Losh 			bzero(&ifr, sizeof(ifr));
489aa6be122SWarner Losh 			ifr.ifra_addr = *ifa->ifa_addr;
490aa6be122SWarner Losh 			if (ifa->ifa_dstaddr)
491aa6be122SWarner Losh 				ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
492aa6be122SWarner Losh 			if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
493aa6be122SWarner Losh 			    NULL) == 0)
494aa6be122SWarner Losh 				continue;
495aa6be122SWarner Losh 		}
4960d0f9d1eSYoshinobu Inoue #endif /* INET */
4970d0f9d1eSYoshinobu Inoue #ifdef INET6
4980d0f9d1eSYoshinobu Inoue 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
49933841545SHajimu UMEMOTO 			in6_purgeaddr(ifa);
500978ee2edSJun-ichiro itojun Hagino 			/* ifp_addrhead is already updated */
5010d0f9d1eSYoshinobu Inoue 			continue;
5020d0f9d1eSYoshinobu Inoue 		}
5030d0f9d1eSYoshinobu Inoue #endif /* INET6 */
5046182fdbdSPeter Wemm 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
5056182fdbdSPeter Wemm 		IFAFREE(ifa);
5066182fdbdSPeter Wemm 	}
5076182fdbdSPeter Wemm 
50833841545SHajimu UMEMOTO #ifdef INET6
50933841545SHajimu UMEMOTO 	/*
51033841545SHajimu UMEMOTO 	 * Remove all IPv6 kernel structs related to ifp.  This should be done
51133841545SHajimu UMEMOTO 	 * before removing routing entries below, since IPv6 interface direct
51233841545SHajimu UMEMOTO 	 * routes are expected to be removed by the IPv6-specific kernel API.
51333841545SHajimu UMEMOTO 	 * Otherwise, the kernel will detect some inconsistency and bark it.
51433841545SHajimu UMEMOTO 	 */
51533841545SHajimu UMEMOTO 	in6_ifdetach(ifp);
51633841545SHajimu UMEMOTO #endif
51733841545SHajimu UMEMOTO 
5185500d3beSWarner Losh 	/*
5195500d3beSWarner Losh 	 * Delete all remaining routes using this interface
5205500d3beSWarner Losh 	 * Unfortuneatly the only way to do this is to slog through
5215500d3beSWarner Losh 	 * the entire routing table looking for routes which point
5225500d3beSWarner Losh 	 * to this interface...oh well...
5235500d3beSWarner Losh 	 */
5245500d3beSWarner Losh 	for (i = 1; i <= AF_MAX; i++) {
5255500d3beSWarner Losh 		if ((rnh = rt_tables[i]) == NULL)
5265500d3beSWarner Losh 			continue;
5275500d3beSWarner Losh 		(void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
5285500d3beSWarner Losh 	}
5295500d3beSWarner Losh 
5307b6edd04SRuslan Ermilov 	/* Announce that the interface is gone. */
5317b6edd04SRuslan Ermilov 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
5327b6edd04SRuslan Ermilov 
533e70cd263SRobert Watson #ifdef MAC
534e70cd263SRobert Watson 	mac_destroy_ifnet(ifp);
535e70cd263SRobert Watson #endif /* MAC */
5369a2a57a1SJonathan Lemon 	KNOTE(&ifp->if_klist, NOTE_EXIT);
5376182fdbdSPeter Wemm 	TAILQ_REMOVE(&ifnet, ifp, if_link);
538df5e1987SJonathan Lemon 	mtx_destroy(&ifp->if_snd.ifq_mtx);
5395500d3beSWarner Losh 	splx(s);
5405500d3beSWarner Losh }
5415500d3beSWarner Losh 
5425500d3beSWarner Losh /*
5435500d3beSWarner Losh  * Delete Routes for a Network Interface
5445500d3beSWarner Losh  *
5455500d3beSWarner Losh  * Called for each routing entry via the rnh->rnh_walktree() call above
5465500d3beSWarner Losh  * to delete all route entries referencing a detaching network interface.
5475500d3beSWarner Losh  *
5485500d3beSWarner Losh  * Arguments:
5495500d3beSWarner Losh  *	rn	pointer to node in the routing table
5505500d3beSWarner Losh  *	arg	argument passed to rnh->rnh_walktree() - detaching interface
5515500d3beSWarner Losh  *
5525500d3beSWarner Losh  * Returns:
5535500d3beSWarner Losh  *	0	successful
5545500d3beSWarner Losh  *	errno	failed - reason indicated
5555500d3beSWarner Losh  *
5565500d3beSWarner Losh  */
5575500d3beSWarner Losh static int
5585500d3beSWarner Losh if_rtdel(rn, arg)
5595500d3beSWarner Losh 	struct radix_node	*rn;
5605500d3beSWarner Losh 	void			*arg;
5615500d3beSWarner Losh {
5625500d3beSWarner Losh 	struct rtentry	*rt = (struct rtentry *)rn;
5635500d3beSWarner Losh 	struct ifnet	*ifp = arg;
5645500d3beSWarner Losh 	int		err;
5655500d3beSWarner Losh 
5665500d3beSWarner Losh 	if (rt->rt_ifp == ifp) {
5675500d3beSWarner Losh 
5685500d3beSWarner Losh 		/*
5695500d3beSWarner Losh 		 * Protect (sorta) against walktree recursion problems
5705500d3beSWarner Losh 		 * with cloned routes
5715500d3beSWarner Losh 		 */
5725500d3beSWarner Losh 		if ((rt->rt_flags & RTF_UP) == 0)
5735500d3beSWarner Losh 			return (0);
5745500d3beSWarner Losh 
5755500d3beSWarner Losh 		err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
5765500d3beSWarner Losh 				rt_mask(rt), rt->rt_flags,
5775500d3beSWarner Losh 				(struct rtentry **) NULL);
5785500d3beSWarner Losh 		if (err) {
5795500d3beSWarner Losh 			log(LOG_WARNING, "if_rtdel: error %d\n", err);
5805500d3beSWarner Losh 		}
5815500d3beSWarner Losh 	}
5825500d3beSWarner Losh 
5835500d3beSWarner Losh 	return (0);
5846182fdbdSPeter Wemm }
5856182fdbdSPeter Wemm 
586df8bae1dSRodney W. Grimes /*
58730aad87dSBrooks Davis  * Create a clone network interface.
58830aad87dSBrooks Davis  */
58930aad87dSBrooks Davis int
59030aad87dSBrooks Davis if_clone_create(name, len)
59130aad87dSBrooks Davis 	char *name;
59230aad87dSBrooks Davis 	int len;
59330aad87dSBrooks Davis {
59430aad87dSBrooks Davis 	struct if_clone *ifc;
59530aad87dSBrooks Davis 	char *dp;
5963b16e7b2SMaxime Henrion 	int wildcard, bytoff, bitoff;
59730aad87dSBrooks Davis 	int unit;
59830aad87dSBrooks Davis 	int err;
59930aad87dSBrooks Davis 
60030aad87dSBrooks Davis 	ifc = if_clone_lookup(name, &unit);
60130aad87dSBrooks Davis 	if (ifc == NULL)
60230aad87dSBrooks Davis 		return (EINVAL);
60330aad87dSBrooks Davis 
60430aad87dSBrooks Davis 	if (ifunit(name) != NULL)
60530aad87dSBrooks Davis 		return (EEXIST);
60630aad87dSBrooks Davis 
6073b16e7b2SMaxime Henrion 	bytoff = bitoff = 0;
60830aad87dSBrooks Davis 	wildcard = (unit < 0);
6093b16e7b2SMaxime Henrion 	/*
6103b16e7b2SMaxime Henrion 	 * Find a free unit if none was given.
6113b16e7b2SMaxime Henrion 	 */
6123b16e7b2SMaxime Henrion 	if (wildcard) {
6133b16e7b2SMaxime Henrion 		while ((bytoff < ifc->ifc_bmlen)
6143b16e7b2SMaxime Henrion 		    && (ifc->ifc_units[bytoff] == 0xff))
6153b16e7b2SMaxime Henrion 			bytoff++;
6163b16e7b2SMaxime Henrion 		if (bytoff >= ifc->ifc_bmlen)
6173b16e7b2SMaxime Henrion 			return (ENOSPC);
6183b16e7b2SMaxime Henrion 		while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
6193b16e7b2SMaxime Henrion 			bitoff++;
6203b16e7b2SMaxime Henrion 		unit = (bytoff << 3) + bitoff;
6213b16e7b2SMaxime Henrion 	}
62230aad87dSBrooks Davis 
6233b16e7b2SMaxime Henrion 	if (unit > ifc->ifc_maxunit)
6243b16e7b2SMaxime Henrion 		return (ENXIO);
6253b16e7b2SMaxime Henrion 
6263b16e7b2SMaxime Henrion 	err = (*ifc->ifc_create)(ifc, unit);
62730aad87dSBrooks Davis 	if (err != 0)
62830aad87dSBrooks Davis 		return (err);
62930aad87dSBrooks Davis 
6303b16e7b2SMaxime Henrion 	if (!wildcard) {
6313b16e7b2SMaxime Henrion 		bytoff = unit >> 3;
6323b16e7b2SMaxime Henrion 		bitoff = unit - (bytoff << 3);
6333b16e7b2SMaxime Henrion 	}
6343b16e7b2SMaxime Henrion 
6353b16e7b2SMaxime Henrion 	/*
6363b16e7b2SMaxime Henrion 	 * Allocate the unit in the bitmap.
6373b16e7b2SMaxime Henrion 	 */
6383b16e7b2SMaxime Henrion 	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
6393b16e7b2SMaxime Henrion 	    ("%s: bit is already set", __func__));
6403b16e7b2SMaxime Henrion 	ifc->ifc_units[bytoff] |= (1 << bitoff);
6413b16e7b2SMaxime Henrion 
64230aad87dSBrooks Davis 	/* In the wildcard case, we need to update the name. */
64330aad87dSBrooks Davis 	if (wildcard) {
64430aad87dSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
64530aad87dSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
64630aad87dSBrooks Davis 		    len - (dp-name) - 1) {
64730aad87dSBrooks Davis 			/*
64830aad87dSBrooks Davis 			 * This can only be a programmer error and
64930aad87dSBrooks Davis 			 * there's no straightforward way to recover if
65030aad87dSBrooks Davis 			 * it happens.
65130aad87dSBrooks Davis 			 */
65230aad87dSBrooks Davis 			panic("if_clone_create(): interface name too long");
65330aad87dSBrooks Davis 		}
65430aad87dSBrooks Davis 
65530aad87dSBrooks Davis 	}
65630aad87dSBrooks Davis 
65730aad87dSBrooks Davis 	return (0);
65830aad87dSBrooks Davis }
65930aad87dSBrooks Davis 
66030aad87dSBrooks Davis /*
66130aad87dSBrooks Davis  * Destroy a clone network interface.
66230aad87dSBrooks Davis  */
66330aad87dSBrooks Davis int
66430aad87dSBrooks Davis if_clone_destroy(name)
66530aad87dSBrooks Davis 	const char *name;
66630aad87dSBrooks Davis {
66730aad87dSBrooks Davis 	struct if_clone *ifc;
66830aad87dSBrooks Davis 	struct ifnet *ifp;
6693b16e7b2SMaxime Henrion 	int bytoff, bitoff;
670ae5a19beSBrooks Davis 	int unit;
67130aad87dSBrooks Davis 
6723b16e7b2SMaxime Henrion 	ifc = if_clone_lookup(name, &unit);
67330aad87dSBrooks Davis 	if (ifc == NULL)
67430aad87dSBrooks Davis 		return (EINVAL);
67530aad87dSBrooks Davis 
676ae5a19beSBrooks Davis 	if (unit < ifc->ifc_minifs)
677ae5a19beSBrooks Davis 		return (EINVAL);
678ae5a19beSBrooks Davis 
67930aad87dSBrooks Davis 	ifp = ifunit(name);
68030aad87dSBrooks Davis 	if (ifp == NULL)
68130aad87dSBrooks Davis 		return (ENXIO);
68230aad87dSBrooks Davis 
68330aad87dSBrooks Davis 	if (ifc->ifc_destroy == NULL)
68430aad87dSBrooks Davis 		return (EOPNOTSUPP);
68530aad87dSBrooks Davis 
686ae5a19beSBrooks Davis 	(*ifc->ifc_destroy)(ifp);
6873b16e7b2SMaxime Henrion 
6883b16e7b2SMaxime Henrion 	/*
6893b16e7b2SMaxime Henrion 	 * Compute offset in the bitmap and deallocate the unit.
6903b16e7b2SMaxime Henrion 	 */
6913b16e7b2SMaxime Henrion 	bytoff = unit >> 3;
6923b16e7b2SMaxime Henrion 	bitoff = unit - (bytoff << 3);
6933b16e7b2SMaxime Henrion 	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
6943b16e7b2SMaxime Henrion 	    ("%s: bit is already cleared", __func__));
6953b16e7b2SMaxime Henrion 	ifc->ifc_units[bytoff] &= ~(1 << bitoff);
6963b16e7b2SMaxime Henrion 	return (0);
69730aad87dSBrooks Davis }
69830aad87dSBrooks Davis 
69930aad87dSBrooks Davis /*
70030aad87dSBrooks Davis  * Look up a network interface cloner.
70130aad87dSBrooks Davis  */
7020b59d917SJonathan Lemon static struct if_clone *
70330aad87dSBrooks Davis if_clone_lookup(name, unitp)
70430aad87dSBrooks Davis 	const char *name;
70530aad87dSBrooks Davis 	int *unitp;
70630aad87dSBrooks Davis {
70730aad87dSBrooks Davis 	struct if_clone *ifc;
70830aad87dSBrooks Davis 	const char *cp;
70930aad87dSBrooks Davis 	int i;
71030aad87dSBrooks Davis 
71130aad87dSBrooks Davis 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
71230aad87dSBrooks Davis 		for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
71330aad87dSBrooks Davis 			if (ifc->ifc_name[i] != *cp)
71430aad87dSBrooks Davis 				goto next_ifc;
71530aad87dSBrooks Davis 		}
71630aad87dSBrooks Davis 		goto found_name;
71730aad87dSBrooks Davis  next_ifc:
71830aad87dSBrooks Davis 		ifc = LIST_NEXT(ifc, ifc_list);
71930aad87dSBrooks Davis 	}
72030aad87dSBrooks Davis 
72130aad87dSBrooks Davis 	/* No match. */
72230aad87dSBrooks Davis 	return ((struct if_clone *)NULL);
72330aad87dSBrooks Davis 
72430aad87dSBrooks Davis  found_name:
72530aad87dSBrooks Davis 	if (*cp == '\0') {
72630aad87dSBrooks Davis 		i = -1;
72730aad87dSBrooks Davis 	} else {
72830aad87dSBrooks Davis 		for (i = 0; *cp != '\0'; cp++) {
72930aad87dSBrooks Davis 			if (*cp < '0' || *cp > '9') {
73030aad87dSBrooks Davis 				/* Bogus unit number. */
73130aad87dSBrooks Davis 				return (NULL);
73230aad87dSBrooks Davis 			}
73330aad87dSBrooks Davis 			i = (i * 10) + (*cp - '0');
73430aad87dSBrooks Davis 		}
73530aad87dSBrooks Davis 	}
73630aad87dSBrooks Davis 
73730aad87dSBrooks Davis 	if (unitp != NULL)
73830aad87dSBrooks Davis 		*unitp = i;
73930aad87dSBrooks Davis 	return (ifc);
74030aad87dSBrooks Davis }
74130aad87dSBrooks Davis 
74230aad87dSBrooks Davis /*
74330aad87dSBrooks Davis  * Register a network interface cloner.
74430aad87dSBrooks Davis  */
74530aad87dSBrooks Davis void
74630aad87dSBrooks Davis if_clone_attach(ifc)
74730aad87dSBrooks Davis 	struct if_clone *ifc;
74830aad87dSBrooks Davis {
749ae5a19beSBrooks Davis 	int bytoff, bitoff;
750ae5a19beSBrooks Davis 	int err;
7513b16e7b2SMaxime Henrion 	int len, maxclone;
752ae5a19beSBrooks Davis 	int unit;
75330aad87dSBrooks Davis 
754ae5a19beSBrooks Davis 	KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
755ae5a19beSBrooks Davis 	    ("%s: %s requested more units then allowed (%d > %d)",
756ae5a19beSBrooks Davis 	    __func__, ifc->ifc_name, ifc->ifc_minifs,
757ae5a19beSBrooks Davis 	    ifc->ifc_maxunit + 1));
7583b16e7b2SMaxime Henrion 	/*
7593b16e7b2SMaxime Henrion 	 * Compute bitmap size and allocate it.
7603b16e7b2SMaxime Henrion 	 */
7613b16e7b2SMaxime Henrion 	maxclone = ifc->ifc_maxunit + 1;
7623b16e7b2SMaxime Henrion 	len = maxclone >> 3;
7633b16e7b2SMaxime Henrion 	if ((len << 3) < maxclone)
7643b16e7b2SMaxime Henrion 		len++;
7653b16e7b2SMaxime Henrion 	ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO);
7663b16e7b2SMaxime Henrion 	ifc->ifc_bmlen = len;
767ae5a19beSBrooks Davis 
76830aad87dSBrooks Davis 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
76930aad87dSBrooks Davis 	if_cloners_count++;
770ae5a19beSBrooks Davis 
771ae5a19beSBrooks Davis 	for (unit = 0; unit < ifc->ifc_minifs; unit++) {
772ae5a19beSBrooks Davis 		err = (*ifc->ifc_create)(ifc, unit);
773ae5a19beSBrooks Davis 		KASSERT(err == 0,
774ae5a19beSBrooks Davis 		    ("%s: failed to create required interface %s%d",
775ae5a19beSBrooks Davis 		    __func__, ifc->ifc_name, unit));
776ae5a19beSBrooks Davis 
777ae5a19beSBrooks Davis 		/* Allocate the unit in the bitmap. */
778ae5a19beSBrooks Davis 		bytoff = unit >> 3;
779ae5a19beSBrooks Davis 		bitoff = unit - (bytoff << 3);
780ae5a19beSBrooks Davis 		ifc->ifc_units[bytoff] |= (1 << bitoff);
781ae5a19beSBrooks Davis 	}
78230aad87dSBrooks Davis }
78330aad87dSBrooks Davis 
78430aad87dSBrooks Davis /*
78530aad87dSBrooks Davis  * Unregister a network interface cloner.
78630aad87dSBrooks Davis  */
78730aad87dSBrooks Davis void
78830aad87dSBrooks Davis if_clone_detach(ifc)
78930aad87dSBrooks Davis 	struct if_clone *ifc;
79030aad87dSBrooks Davis {
79130aad87dSBrooks Davis 
79230aad87dSBrooks Davis 	LIST_REMOVE(ifc, ifc_list);
7933b16e7b2SMaxime Henrion 	free(ifc->ifc_units, M_CLONE);
79430aad87dSBrooks Davis 	if_cloners_count--;
79530aad87dSBrooks Davis }
79630aad87dSBrooks Davis 
79730aad87dSBrooks Davis /*
79830aad87dSBrooks Davis  * Provide list of interface cloners to userspace.
79930aad87dSBrooks Davis  */
8000b59d917SJonathan Lemon static int
80130aad87dSBrooks Davis if_clone_list(ifcr)
80230aad87dSBrooks Davis 	struct if_clonereq *ifcr;
80330aad87dSBrooks Davis {
80430aad87dSBrooks Davis 	char outbuf[IFNAMSIZ], *dst;
80530aad87dSBrooks Davis 	struct if_clone *ifc;
80630aad87dSBrooks Davis 	int count, error = 0;
80730aad87dSBrooks Davis 
80830aad87dSBrooks Davis 	ifcr->ifcr_total = if_cloners_count;
80930aad87dSBrooks Davis 	if ((dst = ifcr->ifcr_buffer) == NULL) {
81030aad87dSBrooks Davis 		/* Just asking how many there are. */
81130aad87dSBrooks Davis 		return (0);
81230aad87dSBrooks Davis 	}
81330aad87dSBrooks Davis 
81430aad87dSBrooks Davis 	if (ifcr->ifcr_count < 0)
81530aad87dSBrooks Davis 		return (EINVAL);
81630aad87dSBrooks Davis 
81730aad87dSBrooks Davis 	count = (if_cloners_count < ifcr->ifcr_count) ?
81830aad87dSBrooks Davis 	    if_cloners_count : ifcr->ifcr_count;
81930aad87dSBrooks Davis 
82030aad87dSBrooks Davis 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
82130aad87dSBrooks Davis 	     ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
82230aad87dSBrooks Davis 		strncpy(outbuf, ifc->ifc_name, IFNAMSIZ);
82330aad87dSBrooks Davis 		outbuf[IFNAMSIZ - 1] = '\0';	/* sanity */
82430aad87dSBrooks Davis 		error = copyout(outbuf, dst, IFNAMSIZ);
82530aad87dSBrooks Davis 		if (error)
82630aad87dSBrooks Davis 			break;
82730aad87dSBrooks Davis 	}
82830aad87dSBrooks Davis 
82930aad87dSBrooks Davis 	return (error);
83030aad87dSBrooks Davis }
83130aad87dSBrooks Davis 
83230aad87dSBrooks Davis /*
833df8bae1dSRodney W. Grimes  * Locate an interface based on a complete address.
834df8bae1dSRodney W. Grimes  */
835df8bae1dSRodney W. Grimes /*ARGSUSED*/
836df8bae1dSRodney W. Grimes struct ifaddr *
837df8bae1dSRodney W. Grimes ifa_ifwithaddr(addr)
8380b59d917SJonathan Lemon 	struct sockaddr *addr;
839df8bae1dSRodney W. Grimes {
8400b59d917SJonathan Lemon 	struct ifnet *ifp;
8410b59d917SJonathan Lemon 	struct ifaddr *ifa;
842df8bae1dSRodney W. Grimes 
843df8bae1dSRodney W. Grimes #define	equal(a1, a2) \
844df8bae1dSRodney W. Grimes   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
845fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(ifp, &ifnet, if_link)
84637d40066SPoul-Henning Kamp 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
847df8bae1dSRodney W. Grimes 			if (ifa->ifa_addr->sa_family != addr->sa_family)
848df8bae1dSRodney W. Grimes 				continue;
849df8bae1dSRodney W. Grimes 			if (equal(addr, ifa->ifa_addr))
8500b59d917SJonathan Lemon 				goto done;
85182cd038dSYoshinobu Inoue 			/* IP6 doesn't have broadcast */
8520b59d917SJonathan Lemon 			if ((ifp->if_flags & IFF_BROADCAST) &&
8530b59d917SJonathan Lemon 			    ifa->ifa_broadaddr &&
85482cd038dSYoshinobu Inoue 			    ifa->ifa_broadaddr->sa_len != 0 &&
855df8bae1dSRodney W. Grimes 			    equal(ifa->ifa_broadaddr, addr))
8560b59d917SJonathan Lemon 				goto done;
8570b59d917SJonathan Lemon 		}
8580b59d917SJonathan Lemon 	ifa = NULL;
8590b59d917SJonathan Lemon done:
860df8bae1dSRodney W. Grimes 	return (ifa);
861df8bae1dSRodney W. Grimes }
8620b59d917SJonathan Lemon 
863df8bae1dSRodney W. Grimes /*
864df8bae1dSRodney W. Grimes  * Locate the point to point interface with a given destination address.
865df8bae1dSRodney W. Grimes  */
866df8bae1dSRodney W. Grimes /*ARGSUSED*/
867df8bae1dSRodney W. Grimes struct ifaddr *
868df8bae1dSRodney W. Grimes ifa_ifwithdstaddr(addr)
8690b59d917SJonathan Lemon 	struct sockaddr *addr;
870df8bae1dSRodney W. Grimes {
8710b59d917SJonathan Lemon 	struct ifnet *ifp;
8720b59d917SJonathan Lemon 	struct ifaddr *ifa;
873df8bae1dSRodney W. Grimes 
8740b59d917SJonathan Lemon 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
8750b59d917SJonathan Lemon 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
8760b59d917SJonathan Lemon 			continue;
87737d40066SPoul-Henning Kamp 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
878df8bae1dSRodney W. Grimes 			if (ifa->ifa_addr->sa_family != addr->sa_family)
879df8bae1dSRodney W. Grimes 				continue;
88055088a1cSDavid Greenman 			if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
8810b59d917SJonathan Lemon 				goto done;
882df8bae1dSRodney W. Grimes 		}
8830b59d917SJonathan Lemon 	}
8840b59d917SJonathan Lemon 	ifa = NULL;
8850b59d917SJonathan Lemon done:
8860b59d917SJonathan Lemon 	return (ifa);
887df8bae1dSRodney W. Grimes }
888df8bae1dSRodney W. Grimes 
889df8bae1dSRodney W. Grimes /*
890df8bae1dSRodney W. Grimes  * Find an interface on a specific network.  If many, choice
891df8bae1dSRodney W. Grimes  * is most specific found.
892df8bae1dSRodney W. Grimes  */
893df8bae1dSRodney W. Grimes struct ifaddr *
894df8bae1dSRodney W. Grimes ifa_ifwithnet(addr)
895df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
896df8bae1dSRodney W. Grimes {
897df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
898df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
899df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
900df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
901df8bae1dSRodney W. Grimes 	char *addr_data = addr->sa_data, *cplim;
902df8bae1dSRodney W. Grimes 
9037e2a6151SJulian Elischer 	/*
9047e2a6151SJulian Elischer 	 * AF_LINK addresses can be looked up directly by their index number,
9057e2a6151SJulian Elischer 	 * so do that if we can.
9067e2a6151SJulian Elischer 	 */
907df8bae1dSRodney W. Grimes 	if (af == AF_LINK) {
908df8bae1dSRodney W. Grimes 	    register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
909df8bae1dSRodney W. Grimes 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
910f9132cebSJonathan Lemon 		return (ifaddr_byindex(sdl->sdl_index));
911df8bae1dSRodney W. Grimes 	}
9127e2a6151SJulian Elischer 
9137e2a6151SJulian Elischer 	/*
9147e2a6151SJulian Elischer 	 * Scan though each interface, looking for ones that have
9157e2a6151SJulian Elischer 	 * addresses in this address family.
9167e2a6151SJulian Elischer 	 */
917fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
91837d40066SPoul-Henning Kamp 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
919df8bae1dSRodney W. Grimes 			register char *cp, *cp2, *cp3;
920df8bae1dSRodney W. Grimes 
921523a02aaSDavid Greenman 			if (ifa->ifa_addr->sa_family != af)
922df8bae1dSRodney W. Grimes next:				continue;
923c61cd599SHajimu UMEMOTO 			if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
9247e2a6151SJulian Elischer 				/*
9257e2a6151SJulian Elischer 				 * This is a bit broken as it doesn't
9267e2a6151SJulian Elischer 				 * take into account that the remote end may
9277e2a6151SJulian Elischer 				 * be a single node in the network we are
9287e2a6151SJulian Elischer 				 * looking for.
9297e2a6151SJulian Elischer 				 * The trouble is that we don't know the
9307e2a6151SJulian Elischer 				 * netmask for the remote end.
9317e2a6151SJulian Elischer 				 */
932fcd6781aSGarrett Wollman 				if (ifa->ifa_dstaddr != 0
933fcd6781aSGarrett Wollman 				    && equal(addr, ifa->ifa_dstaddr))
9340b59d917SJonathan Lemon 					goto done;
9353740e2adSDavid Greenman 			} else {
9367e2a6151SJulian Elischer 				/*
9377ed8f465SJulian Elischer 				 * if we have a special address handler,
9387ed8f465SJulian Elischer 				 * then use it instead of the generic one.
9397ed8f465SJulian Elischer 				 */
9407ed8f465SJulian Elischer 	          		if (ifa->ifa_claim_addr) {
9410b59d917SJonathan Lemon 					if ((*ifa->ifa_claim_addr)(ifa, addr))
9420b59d917SJonathan Lemon 						goto done;
9437ed8f465SJulian Elischer 					continue;
9447ed8f465SJulian Elischer 				}
9457ed8f465SJulian Elischer 
9467ed8f465SJulian Elischer 				/*
9477e2a6151SJulian Elischer 				 * Scan all the bits in the ifa's address.
9487e2a6151SJulian Elischer 				 * If a bit dissagrees with what we are
9497e2a6151SJulian Elischer 				 * looking for, mask it with the netmask
9507e2a6151SJulian Elischer 				 * to see if it really matters.
9517e2a6151SJulian Elischer 				 * (A byte at a time)
9527e2a6151SJulian Elischer 				 */
953523a02aaSDavid Greenman 				if (ifa->ifa_netmask == 0)
954523a02aaSDavid Greenman 					continue;
955df8bae1dSRodney W. Grimes 				cp = addr_data;
956df8bae1dSRodney W. Grimes 				cp2 = ifa->ifa_addr->sa_data;
957df8bae1dSRodney W. Grimes 				cp3 = ifa->ifa_netmask->sa_data;
9587e2a6151SJulian Elischer 				cplim = ifa->ifa_netmask->sa_len
9597e2a6151SJulian Elischer 					+ (char *)ifa->ifa_netmask;
960df8bae1dSRodney W. Grimes 				while (cp3 < cplim)
961df8bae1dSRodney W. Grimes 					if ((*cp++ ^ *cp2++) & *cp3++)
9627e2a6151SJulian Elischer 						goto next; /* next address! */
9637e2a6151SJulian Elischer 				/*
9647e2a6151SJulian Elischer 				 * If the netmask of what we just found
9657e2a6151SJulian Elischer 				 * is more specific than what we had before
9667e2a6151SJulian Elischer 				 * (if we had one) then remember the new one
9677e2a6151SJulian Elischer 				 * before continuing to search
9687e2a6151SJulian Elischer 				 * for an even better one.
9697e2a6151SJulian Elischer 				 */
970df8bae1dSRodney W. Grimes 				if (ifa_maybe == 0 ||
971df8bae1dSRodney W. Grimes 				    rn_refines((caddr_t)ifa->ifa_netmask,
972df8bae1dSRodney W. Grimes 				    (caddr_t)ifa_maybe->ifa_netmask))
973df8bae1dSRodney W. Grimes 					ifa_maybe = ifa;
974df8bae1dSRodney W. Grimes 			}
975b2af64fdSDavid Greenman 		}
976b2af64fdSDavid Greenman 	}
9770b59d917SJonathan Lemon 	ifa = ifa_maybe;
9780b59d917SJonathan Lemon done:
9790b59d917SJonathan Lemon 	return (ifa);
980df8bae1dSRodney W. Grimes }
981df8bae1dSRodney W. Grimes 
982df8bae1dSRodney W. Grimes /*
983df8bae1dSRodney W. Grimes  * Find an interface address specific to an interface best matching
984df8bae1dSRodney W. Grimes  * a given address.
985df8bae1dSRodney W. Grimes  */
986df8bae1dSRodney W. Grimes struct ifaddr *
987df8bae1dSRodney W. Grimes ifaof_ifpforaddr(addr, ifp)
988df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
989df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
990df8bae1dSRodney W. Grimes {
991df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
992df8bae1dSRodney W. Grimes 	register char *cp, *cp2, *cp3;
993df8bae1dSRodney W. Grimes 	register char *cplim;
994df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = 0;
995df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
996df8bae1dSRodney W. Grimes 
997df8bae1dSRodney W. Grimes 	if (af >= AF_MAX)
998df8bae1dSRodney W. Grimes 		return (0);
99937d40066SPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1000df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != af)
1001df8bae1dSRodney W. Grimes 			continue;
1002381dd1d2SJulian Elischer 		if (ifa_maybe == 0)
1003df8bae1dSRodney W. Grimes 			ifa_maybe = ifa;
1004df8bae1dSRodney W. Grimes 		if (ifa->ifa_netmask == 0) {
1005df8bae1dSRodney W. Grimes 			if (equal(addr, ifa->ifa_addr) ||
1006df8bae1dSRodney W. Grimes 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
10072defe5cdSJonathan Lemon 				goto done;
1008df8bae1dSRodney W. Grimes 			continue;
1009df8bae1dSRodney W. Grimes 		}
1010b2af64fdSDavid Greenman 		if (ifp->if_flags & IFF_POINTOPOINT) {
1011b2af64fdSDavid Greenman 			if (equal(addr, ifa->ifa_dstaddr))
1012a8637146SJonathan Lemon 				goto done;
10133740e2adSDavid Greenman 		} else {
1014df8bae1dSRodney W. Grimes 			cp = addr->sa_data;
1015df8bae1dSRodney W. Grimes 			cp2 = ifa->ifa_addr->sa_data;
1016df8bae1dSRodney W. Grimes 			cp3 = ifa->ifa_netmask->sa_data;
1017df8bae1dSRodney W. Grimes 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1018df8bae1dSRodney W. Grimes 			for (; cp3 < cplim; cp3++)
1019df8bae1dSRodney W. Grimes 				if ((*cp++ ^ *cp2++) & *cp3)
1020df8bae1dSRodney W. Grimes 					break;
1021df8bae1dSRodney W. Grimes 			if (cp3 == cplim)
10222defe5cdSJonathan Lemon 				goto done;
1023df8bae1dSRodney W. Grimes 		}
1024b2af64fdSDavid Greenman 	}
1025f9132cebSJonathan Lemon 	ifa = ifa_maybe;
1026f9132cebSJonathan Lemon done:
1027f9132cebSJonathan Lemon 	return (ifa);
1028df8bae1dSRodney W. Grimes }
1029df8bae1dSRodney W. Grimes 
1030df8bae1dSRodney W. Grimes #include <net/route.h>
1031df8bae1dSRodney W. Grimes 
1032df8bae1dSRodney W. Grimes /*
1033df8bae1dSRodney W. Grimes  * Default action when installing a route with a Link Level gateway.
1034df8bae1dSRodney W. Grimes  * Lookup an appropriate real ifa to point to.
1035df8bae1dSRodney W. Grimes  * This should be moved to /sys/net/link.c eventually.
1036df8bae1dSRodney W. Grimes  */
10373bda9f9bSPoul-Henning Kamp static void
10388071913dSRuslan Ermilov link_rtrequest(cmd, rt, info)
1039df8bae1dSRodney W. Grimes 	int cmd;
1040df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
10418071913dSRuslan Ermilov 	struct rt_addrinfo *info;
1042df8bae1dSRodney W. Grimes {
1043df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
1044df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
1045df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1046df8bae1dSRodney W. Grimes 
1047df8bae1dSRodney W. Grimes 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1048df8bae1dSRodney W. Grimes 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1049df8bae1dSRodney W. Grimes 		return;
10509448326fSPoul-Henning Kamp 	ifa = ifaof_ifpforaddr(dst, ifp);
10519448326fSPoul-Henning Kamp 	if (ifa) {
1052df8bae1dSRodney W. Grimes 		IFAFREE(rt->rt_ifa);
1053df8bae1dSRodney W. Grimes 		rt->rt_ifa = ifa;
1054df8bae1dSRodney W. Grimes 		ifa->ifa_refcnt++;
1055df8bae1dSRodney W. Grimes 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
10568071913dSRuslan Ermilov 			ifa->ifa_rtrequest(cmd, rt, info);
1057df8bae1dSRodney W. Grimes 	}
1058df8bae1dSRodney W. Grimes }
1059df8bae1dSRodney W. Grimes 
1060df8bae1dSRodney W. Grimes /*
1061df8bae1dSRodney W. Grimes  * Mark an interface down and notify protocols of
1062df8bae1dSRodney W. Grimes  * the transition.
1063df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
1064df8bae1dSRodney W. Grimes  */
1065df8bae1dSRodney W. Grimes void
1066e8c2601dSPoul-Henning Kamp if_unroute(ifp, flag, fam)
1067df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
1068e8c2601dSPoul-Henning Kamp 	int flag, fam;
1069df8bae1dSRodney W. Grimes {
1070df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
1071df8bae1dSRodney W. Grimes 
1072e8c2601dSPoul-Henning Kamp 	ifp->if_flags &= ~flag;
107398b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
1074e8c2601dSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1075e8c2601dSPoul-Henning Kamp 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1076df8bae1dSRodney W. Grimes 			pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1077df8bae1dSRodney W. Grimes 	if_qflush(&ifp->if_snd);
1078df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
1079df8bae1dSRodney W. Grimes }
1080df8bae1dSRodney W. Grimes 
1081df8bae1dSRodney W. Grimes /*
1082df8bae1dSRodney W. Grimes  * Mark an interface up and notify protocols of
1083df8bae1dSRodney W. Grimes  * the transition.
1084df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
1085df8bae1dSRodney W. Grimes  */
1086df8bae1dSRodney W. Grimes void
1087e8c2601dSPoul-Henning Kamp if_route(ifp, flag, fam)
1088df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
1089e8c2601dSPoul-Henning Kamp 	int flag, fam;
1090df8bae1dSRodney W. Grimes {
1091176395b2SGarrett Wollman 	register struct ifaddr *ifa;
1092df8bae1dSRodney W. Grimes 
1093e8c2601dSPoul-Henning Kamp 	ifp->if_flags |= flag;
109498b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
1095e8c2601dSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1096e8c2601dSPoul-Henning Kamp 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1097df8bae1dSRodney W. Grimes 			pfctlinput(PRC_IFUP, ifa->ifa_addr);
1098df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
109982cd038dSYoshinobu Inoue #ifdef INET6
110082cd038dSYoshinobu Inoue 	in6_if_up(ifp);
110182cd038dSYoshinobu Inoue #endif
1102df8bae1dSRodney W. Grimes }
1103df8bae1dSRodney W. Grimes 
1104df8bae1dSRodney W. Grimes /*
1105e8c2601dSPoul-Henning Kamp  * Mark an interface down and notify protocols of
1106e8c2601dSPoul-Henning Kamp  * the transition.
1107e8c2601dSPoul-Henning Kamp  * NOTE: must be called at splnet or eqivalent.
1108e8c2601dSPoul-Henning Kamp  */
1109e8c2601dSPoul-Henning Kamp void
1110e8c2601dSPoul-Henning Kamp if_down(ifp)
1111e8c2601dSPoul-Henning Kamp 	register struct ifnet *ifp;
1112e8c2601dSPoul-Henning Kamp {
1113e8c2601dSPoul-Henning Kamp 
1114e8c2601dSPoul-Henning Kamp 	if_unroute(ifp, IFF_UP, AF_UNSPEC);
1115e8c2601dSPoul-Henning Kamp }
1116e8c2601dSPoul-Henning Kamp 
1117e8c2601dSPoul-Henning Kamp /*
1118e8c2601dSPoul-Henning Kamp  * Mark an interface up and notify protocols of
1119e8c2601dSPoul-Henning Kamp  * the transition.
1120e8c2601dSPoul-Henning Kamp  * NOTE: must be called at splnet or eqivalent.
1121e8c2601dSPoul-Henning Kamp  */
1122e8c2601dSPoul-Henning Kamp void
1123e8c2601dSPoul-Henning Kamp if_up(ifp)
1124e8c2601dSPoul-Henning Kamp 	register struct ifnet *ifp;
1125e8c2601dSPoul-Henning Kamp {
1126e8c2601dSPoul-Henning Kamp 
1127e8c2601dSPoul-Henning Kamp 	if_route(ifp, IFF_UP, AF_UNSPEC);
1128e8c2601dSPoul-Henning Kamp }
1129e8c2601dSPoul-Henning Kamp 
1130e8c2601dSPoul-Henning Kamp /*
1131df8bae1dSRodney W. Grimes  * Flush an interface queue.
1132df8bae1dSRodney W. Grimes  */
11333bda9f9bSPoul-Henning Kamp static void
1134df8bae1dSRodney W. Grimes if_qflush(ifq)
1135df8bae1dSRodney W. Grimes 	register struct ifqueue *ifq;
1136df8bae1dSRodney W. Grimes {
1137df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
1138df8bae1dSRodney W. Grimes 
1139df8bae1dSRodney W. Grimes 	n = ifq->ifq_head;
11409448326fSPoul-Henning Kamp 	while ((m = n) != 0) {
1141df8bae1dSRodney W. Grimes 		n = m->m_act;
1142df8bae1dSRodney W. Grimes 		m_freem(m);
1143df8bae1dSRodney W. Grimes 	}
1144df8bae1dSRodney W. Grimes 	ifq->ifq_head = 0;
1145df8bae1dSRodney W. Grimes 	ifq->ifq_tail = 0;
1146df8bae1dSRodney W. Grimes 	ifq->ifq_len = 0;
1147df8bae1dSRodney W. Grimes }
1148df8bae1dSRodney W. Grimes 
1149df8bae1dSRodney W. Grimes /*
1150df8bae1dSRodney W. Grimes  * Handle interface watchdog timer routines.  Called
1151df8bae1dSRodney W. Grimes  * from softclock, we decrement timers (if set) and
1152df8bae1dSRodney W. Grimes  * call the appropriate interface routine on expiration.
1153df8bae1dSRodney W. Grimes  */
11543bda9f9bSPoul-Henning Kamp static void
1155df8bae1dSRodney W. Grimes if_slowtimo(arg)
1156df8bae1dSRodney W. Grimes 	void *arg;
1157df8bae1dSRodney W. Grimes {
1158df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
1159df8bae1dSRodney W. Grimes 	int s = splimp();
1160df8bae1dSRodney W. Grimes 
1161fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1162df8bae1dSRodney W. Grimes 		if (ifp->if_timer == 0 || --ifp->if_timer)
1163df8bae1dSRodney W. Grimes 			continue;
1164df8bae1dSRodney W. Grimes 		if (ifp->if_watchdog)
11654a5f1499SDavid Greenman 			(*ifp->if_watchdog)(ifp);
1166df8bae1dSRodney W. Grimes 	}
1167df8bae1dSRodney W. Grimes 	splx(s);
1168df8bae1dSRodney W. Grimes 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1169df8bae1dSRodney W. Grimes }
1170df8bae1dSRodney W. Grimes 
1171df8bae1dSRodney W. Grimes /*
1172df8bae1dSRodney W. Grimes  * Map interface name to
1173df8bae1dSRodney W. Grimes  * interface structure pointer.
1174df8bae1dSRodney W. Grimes  */
1175df8bae1dSRodney W. Grimes struct ifnet *
117630aad87dSBrooks Davis ifunit(const char *name)
1177df8bae1dSRodney W. Grimes {
117801f0fef3SJulian Elischer 	char namebuf[IFNAMSIZ + 1];
11798b7805e4SBoris Popov 	struct ifnet *ifp;
118010930aadSJonathan Lemon 	dev_t dev;
1181df8bae1dSRodney W. Grimes 
118201f0fef3SJulian Elischer 	/*
118301f0fef3SJulian Elischer 	 * Now search all the interfaces for this name/number
118401f0fef3SJulian Elischer 	 */
118510930aadSJonathan Lemon 
118610930aadSJonathan Lemon 	/*
118710930aadSJonathan Lemon 	 * XXX
118810930aadSJonathan Lemon 	 * Devices should really be known as /dev/fooN, not /dev/net/fooN.
118910930aadSJonathan Lemon 	 */
119010930aadSJonathan Lemon 	snprintf(namebuf, IFNAMSIZ, "%s/%s", net_cdevsw.d_name, name);
1191fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
119210930aadSJonathan Lemon 		dev = ifdev_byindex(ifp->if_index);
119310930aadSJonathan Lemon 		if (strcmp(devtoname(dev), namebuf) == 0)
119410930aadSJonathan Lemon 			break;
119510930aadSJonathan Lemon 		if (dev_named(dev, name))
1196df8bae1dSRodney W. Grimes 			break;
1197df8bae1dSRodney W. Grimes 	}
1198df8bae1dSRodney W. Grimes 	return (ifp);
1199df8bae1dSRodney W. Grimes }
1200df8bae1dSRodney W. Grimes 
120182cd038dSYoshinobu Inoue /*
120282cd038dSYoshinobu Inoue  * Map interface name in a sockaddr_dl to
120382cd038dSYoshinobu Inoue  * interface structure pointer.
120482cd038dSYoshinobu Inoue  */
120582cd038dSYoshinobu Inoue struct ifnet *
120682cd038dSYoshinobu Inoue if_withname(sa)
120782cd038dSYoshinobu Inoue 	struct sockaddr *sa;
120882cd038dSYoshinobu Inoue {
120982cd038dSYoshinobu Inoue 	char ifname[IFNAMSIZ+1];
121082cd038dSYoshinobu Inoue 	struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
121182cd038dSYoshinobu Inoue 
121282cd038dSYoshinobu Inoue 	if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
121382cd038dSYoshinobu Inoue 	     (sdl->sdl_nlen > IFNAMSIZ) )
121482cd038dSYoshinobu Inoue 		return NULL;
121582cd038dSYoshinobu Inoue 
121682cd038dSYoshinobu Inoue 	/*
121782cd038dSYoshinobu Inoue 	 * ifunit wants a null-terminated name.  It may not be null-terminated
121882cd038dSYoshinobu Inoue 	 * in the sockaddr.  We don't want to change the caller's sockaddr,
121982cd038dSYoshinobu Inoue 	 * and there might not be room to put the trailing null anyway, so we
122082cd038dSYoshinobu Inoue 	 * make a local copy that we know we can null terminate safely.
122182cd038dSYoshinobu Inoue 	 */
122282cd038dSYoshinobu Inoue 
122382cd038dSYoshinobu Inoue 	bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
122482cd038dSYoshinobu Inoue 	ifname[sdl->sdl_nlen] = '\0';
122582cd038dSYoshinobu Inoue 	return ifunit(ifname);
122682cd038dSYoshinobu Inoue }
122782cd038dSYoshinobu Inoue 
1228df8bae1dSRodney W. Grimes /*
1229f13ad206SJonathan Lemon  * Hardware specific interface ioctls.
1230df8bae1dSRodney W. Grimes  */
1231f13ad206SJonathan Lemon static int
1232f13ad206SJonathan Lemon ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1233df8bae1dSRodney W. Grimes {
1234f13ad206SJonathan Lemon 	struct ifreq *ifr;
1235413dd0baSPoul-Henning Kamp 	struct ifstat *ifs;
1236f13ad206SJonathan Lemon 	int error = 0;
123762f76486SMaxim Sobolev 	int new_flags;
1238df8bae1dSRodney W. Grimes 
1239df8bae1dSRodney W. Grimes 	ifr = (struct ifreq *)data;
124030aad87dSBrooks Davis 	switch (cmd) {
1241de593450SJonathan Lemon 	case SIOCGIFINDEX:
1242de593450SJonathan Lemon 		ifr->ifr_index = ifp->if_index;
1243de593450SJonathan Lemon 		break;
1244de593450SJonathan Lemon 
1245df8bae1dSRodney W. Grimes 	case SIOCGIFFLAGS:
124662f76486SMaxim Sobolev 		ifr->ifr_flags = ifp->if_flags & 0xffff;
124762f76486SMaxim Sobolev 		ifr->ifr_flagshigh = ifp->if_flags >> 16;
1248df8bae1dSRodney W. Grimes 		break;
1249df8bae1dSRodney W. Grimes 
1250016da741SJonathan Lemon 	case SIOCGIFCAP:
1251016da741SJonathan Lemon 		ifr->ifr_reqcap = ifp->if_capabilities;
1252016da741SJonathan Lemon 		ifr->ifr_curcap = ifp->if_capenable;
1253016da741SJonathan Lemon 		break;
1254016da741SJonathan Lemon 
12558f293a63SRobert Watson #ifdef MAC
12568f293a63SRobert Watson 	case SIOCGIFMAC:
12578f293a63SRobert Watson 		error = mac_ioctl_ifnet_get(td->td_proc->p_ucred, ifr, ifp);
12588f293a63SRobert Watson 		break;
12598f293a63SRobert Watson #endif
12608f293a63SRobert Watson 
1261df8bae1dSRodney W. Grimes 	case SIOCGIFMETRIC:
1262df8bae1dSRodney W. Grimes 		ifr->ifr_metric = ifp->if_metric;
1263df8bae1dSRodney W. Grimes 		break;
1264df8bae1dSRodney W. Grimes 
1265a7028af7SDavid Greenman 	case SIOCGIFMTU:
1266a7028af7SDavid Greenman 		ifr->ifr_mtu = ifp->if_mtu;
1267a7028af7SDavid Greenman 		break;
1268a7028af7SDavid Greenman 
1269074c4a4eSGarrett Wollman 	case SIOCGIFPHYS:
1270074c4a4eSGarrett Wollman 		ifr->ifr_phys = ifp->if_physical;
1271074c4a4eSGarrett Wollman 		break;
1272074c4a4eSGarrett Wollman 
1273df8bae1dSRodney W. Grimes 	case SIOCSIFFLAGS:
127444731cabSJohn Baldwin 		error = suser(td);
12759448326fSPoul-Henning Kamp 		if (error)
1276df8bae1dSRodney W. Grimes 			return (error);
127762f76486SMaxim Sobolev 		new_flags = (ifr->ifr_flags & 0xffff) |
127862f76486SMaxim Sobolev 		    (ifr->ifr_flagshigh << 16);
1279cf4b9371SPoul-Henning Kamp 		if (ifp->if_flags & IFF_SMART) {
1280cf4b9371SPoul-Henning Kamp 			/* Smart drivers twiddle their own routes */
12812f55ead7SPoul-Henning Kamp 		} else if (ifp->if_flags & IFF_UP &&
128262f76486SMaxim Sobolev 		    (new_flags & IFF_UP) == 0) {
1283df8bae1dSRodney W. Grimes 			int s = splimp();
1284df8bae1dSRodney W. Grimes 			if_down(ifp);
1285df8bae1dSRodney W. Grimes 			splx(s);
128662f76486SMaxim Sobolev 		} else if (new_flags & IFF_UP &&
1287cf4b9371SPoul-Henning Kamp 		    (ifp->if_flags & IFF_UP) == 0) {
1288df8bae1dSRodney W. Grimes 			int s = splimp();
1289df8bae1dSRodney W. Grimes 			if_up(ifp);
1290df8bae1dSRodney W. Grimes 			splx(s);
1291df8bae1dSRodney W. Grimes 		}
1292df8bae1dSRodney W. Grimes 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
129362f76486SMaxim Sobolev 			(new_flags &~ IFF_CANTCHANGE);
1294df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl)
1295df8bae1dSRodney W. Grimes 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
129698b9590eSPoul-Henning Kamp 		getmicrotime(&ifp->if_lastchange);
1297df8bae1dSRodney W. Grimes 		break;
1298df8bae1dSRodney W. Grimes 
1299016da741SJonathan Lemon 	case SIOCSIFCAP:
130044731cabSJohn Baldwin 		error = suser(td);
1301016da741SJonathan Lemon 		if (error)
1302016da741SJonathan Lemon 			return (error);
1303016da741SJonathan Lemon 		if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1304016da741SJonathan Lemon 			return (EINVAL);
1305016da741SJonathan Lemon 		(void) (*ifp->if_ioctl)(ifp, cmd, data);
1306016da741SJonathan Lemon 		break;
1307016da741SJonathan Lemon 
13088f293a63SRobert Watson #ifdef MAC
13098f293a63SRobert Watson 	case SIOCSIFMAC:
13108f293a63SRobert Watson 		error = mac_ioctl_ifnet_set(td->td_proc->p_ucred, ifr, ifp);
13118f293a63SRobert Watson 		break;
13128f293a63SRobert Watson #endif
13138f293a63SRobert Watson 
1314df8bae1dSRodney W. Grimes 	case SIOCSIFMETRIC:
131544731cabSJohn Baldwin 		error = suser(td);
13169448326fSPoul-Henning Kamp 		if (error)
1317df8bae1dSRodney W. Grimes 			return (error);
1318df8bae1dSRodney W. Grimes 		ifp->if_metric = ifr->ifr_metric;
131998b9590eSPoul-Henning Kamp 		getmicrotime(&ifp->if_lastchange);
1320df8bae1dSRodney W. Grimes 		break;
1321df8bae1dSRodney W. Grimes 
1322074c4a4eSGarrett Wollman 	case SIOCSIFPHYS:
132344731cabSJohn Baldwin 		error = suser(td);
1324e39a0280SGary Palmer 		if (error)
1325e39a0280SGary Palmer 			return error;
1326e39a0280SGary Palmer 		if (!ifp->if_ioctl)
1327e39a0280SGary Palmer 		        return EOPNOTSUPP;
1328e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1329e39a0280SGary Palmer 		if (error == 0)
133098b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
1331e39a0280SGary Palmer 		return(error);
1332074c4a4eSGarrett Wollman 
1333a7028af7SDavid Greenman 	case SIOCSIFMTU:
133482cd038dSYoshinobu Inoue 	{
133582cd038dSYoshinobu Inoue 		u_long oldmtu = ifp->if_mtu;
133682cd038dSYoshinobu Inoue 
133744731cabSJohn Baldwin 		error = suser(td);
13389448326fSPoul-Henning Kamp 		if (error)
1339a7028af7SDavid Greenman 			return (error);
1340aab3beeeSBrian Somers 		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
134175ee03cbSDavid Greenman 			return (EINVAL);
1342f13ad206SJonathan Lemon 		if (ifp->if_ioctl == NULL)
1343f13ad206SJonathan Lemon 			return (EOPNOTSUPP);
1344e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
134548f71763SRuslan Ermilov 		if (error == 0) {
134698b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
134748f71763SRuslan Ermilov 			rt_ifmsg(ifp);
134848f71763SRuslan Ermilov 		}
134982cd038dSYoshinobu Inoue 		/*
135082cd038dSYoshinobu Inoue 		 * If the link MTU changed, do network layer specific procedure.
135182cd038dSYoshinobu Inoue 		 */
135282cd038dSYoshinobu Inoue 		if (ifp->if_mtu != oldmtu) {
135382cd038dSYoshinobu Inoue #ifdef INET6
135482cd038dSYoshinobu Inoue 			nd6_setmtu(ifp);
135582cd038dSYoshinobu Inoue #endif
135682cd038dSYoshinobu Inoue 		}
1357f13ad206SJonathan Lemon 		break;
135882cd038dSYoshinobu Inoue 	}
1359a7028af7SDavid Greenman 
1360df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
1361df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
136244731cabSJohn Baldwin 		error = suser(td);
13639448326fSPoul-Henning Kamp 		if (error)
1364df8bae1dSRodney W. Grimes 			return (error);
1365477180fbSGarrett Wollman 
1366477180fbSGarrett Wollman 		/* Don't allow group membership on non-multicast interfaces. */
1367477180fbSGarrett Wollman 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
1368f13ad206SJonathan Lemon 			return (EOPNOTSUPP);
1369477180fbSGarrett Wollman 
1370477180fbSGarrett Wollman 		/* Don't let users screw up protocols' entries. */
1371477180fbSGarrett Wollman 		if (ifr->ifr_addr.sa_family != AF_LINK)
1372f13ad206SJonathan Lemon 			return (EINVAL);
1373477180fbSGarrett Wollman 
1374477180fbSGarrett Wollman 		if (cmd == SIOCADDMULTI) {
1375477180fbSGarrett Wollman 			struct ifmultiaddr *ifma;
1376477180fbSGarrett Wollman 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1377477180fbSGarrett Wollman 		} else {
1378477180fbSGarrett Wollman 			error = if_delmulti(ifp, &ifr->ifr_addr);
1379477180fbSGarrett Wollman 		}
1380e39a0280SGary Palmer 		if (error == 0)
138198b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
1382f13ad206SJonathan Lemon 		break;
1383df8bae1dSRodney W. Grimes 
138441b3e8e5SJun-ichiro itojun Hagino 	case SIOCSIFPHYADDR:
138541b3e8e5SJun-ichiro itojun Hagino 	case SIOCDIFPHYADDR:
138641b3e8e5SJun-ichiro itojun Hagino #ifdef INET6
138741b3e8e5SJun-ichiro itojun Hagino 	case SIOCSIFPHYADDR_IN6:
138841b3e8e5SJun-ichiro itojun Hagino #endif
138933841545SHajimu UMEMOTO 	case SIOCSLIFPHYADDR:
1390a912e453SPeter Wemm         case SIOCSIFMEDIA:
1391d7189ec6SJoerg Wunsch 	case SIOCSIFGENERIC:
139244731cabSJohn Baldwin 		error = suser(td);
1393a912e453SPeter Wemm 		if (error)
1394a912e453SPeter Wemm 			return (error);
1395f13ad206SJonathan Lemon 		if (ifp->if_ioctl == NULL)
1396a912e453SPeter Wemm 			return (EOPNOTSUPP);
1397a912e453SPeter Wemm 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1398a912e453SPeter Wemm 		if (error == 0)
139998b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
1400f13ad206SJonathan Lemon 		break;
1401a912e453SPeter Wemm 
1402413dd0baSPoul-Henning Kamp 	case SIOCGIFSTATUS:
1403413dd0baSPoul-Henning Kamp 		ifs = (struct ifstat *)data;
1404413dd0baSPoul-Henning Kamp 		ifs->ascii[0] = '\0';
1405413dd0baSPoul-Henning Kamp 
140633841545SHajimu UMEMOTO 	case SIOCGIFPSRCADDR:
140733841545SHajimu UMEMOTO 	case SIOCGIFPDSTADDR:
140833841545SHajimu UMEMOTO 	case SIOCGLIFPHYADDR:
1409a912e453SPeter Wemm 	case SIOCGIFMEDIA:
1410d7189ec6SJoerg Wunsch 	case SIOCGIFGENERIC:
1411a912e453SPeter Wemm 		if (ifp->if_ioctl == 0)
1412a912e453SPeter Wemm 			return (EOPNOTSUPP);
1413f13ad206SJonathan Lemon 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1414f13ad206SJonathan Lemon 		break;
1415a912e453SPeter Wemm 
1416b106252cSBill Paul 	case SIOCSIFLLADDR:
141744731cabSJohn Baldwin 		error = suser(td);
1418b106252cSBill Paul 		if (error)
1419b106252cSBill Paul 			return (error);
1420f13ad206SJonathan Lemon 		error = if_setlladdr(ifp,
142166ce51ceSArchie Cobbs 		    ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1422f13ad206SJonathan Lemon 		break;
142366ce51ceSArchie Cobbs 
1424df8bae1dSRodney W. Grimes 	default:
1425f13ad206SJonathan Lemon 		error = ENOIOCTL;
1426f13ad206SJonathan Lemon 		break;
1427f13ad206SJonathan Lemon 	}
1428f13ad206SJonathan Lemon 	return (error);
1429f13ad206SJonathan Lemon }
1430f13ad206SJonathan Lemon 
1431f13ad206SJonathan Lemon /*
1432f13ad206SJonathan Lemon  * Interface ioctls.
1433f13ad206SJonathan Lemon  */
1434f13ad206SJonathan Lemon int
1435f13ad206SJonathan Lemon ifioctl(so, cmd, data, td)
1436f13ad206SJonathan Lemon 	struct socket *so;
1437f13ad206SJonathan Lemon 	u_long cmd;
1438f13ad206SJonathan Lemon 	caddr_t data;
1439f13ad206SJonathan Lemon 	struct thread *td;
1440f13ad206SJonathan Lemon {
1441f13ad206SJonathan Lemon 	struct ifnet *ifp;
1442f13ad206SJonathan Lemon 	struct ifreq *ifr;
1443f13ad206SJonathan Lemon 	int error;
144462f76486SMaxim Sobolev 	int oif_flags;
1445f13ad206SJonathan Lemon 
1446f13ad206SJonathan Lemon 	switch (cmd) {
1447f13ad206SJonathan Lemon 	case SIOCGIFCONF:
1448f13ad206SJonathan Lemon 	case OSIOCGIFCONF:
1449f13ad206SJonathan Lemon 		return (ifconf(cmd, data));
1450f13ad206SJonathan Lemon 	}
1451f13ad206SJonathan Lemon 	ifr = (struct ifreq *)data;
1452f13ad206SJonathan Lemon 
1453f13ad206SJonathan Lemon 	switch (cmd) {
1454f13ad206SJonathan Lemon 	case SIOCIFCREATE:
1455f13ad206SJonathan Lemon 	case SIOCIFDESTROY:
145644731cabSJohn Baldwin 		if ((error = suser(td)) != 0)
1457f13ad206SJonathan Lemon 			return (error);
1458f13ad206SJonathan Lemon 		return ((cmd == SIOCIFCREATE) ?
1459f13ad206SJonathan Lemon 			if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1460f13ad206SJonathan Lemon 			if_clone_destroy(ifr->ifr_name));
1461f13ad206SJonathan Lemon 
1462f13ad206SJonathan Lemon 	case SIOCIFGCLONERS:
1463f13ad206SJonathan Lemon 		return (if_clone_list((struct if_clonereq *)data));
1464f13ad206SJonathan Lemon 	}
1465f13ad206SJonathan Lemon 
1466f13ad206SJonathan Lemon 	ifp = ifunit(ifr->ifr_name);
1467f13ad206SJonathan Lemon 	if (ifp == 0)
1468f13ad206SJonathan Lemon 		return (ENXIO);
1469f13ad206SJonathan Lemon 
1470f13ad206SJonathan Lemon 	error = ifhwioctl(cmd, ifp, data, td);
1471f13ad206SJonathan Lemon 	if (error != ENOIOCTL)
1472f13ad206SJonathan Lemon 		return (error);
1473f13ad206SJonathan Lemon 
147482cd038dSYoshinobu Inoue 	oif_flags = ifp->if_flags;
1475df8bae1dSRodney W. Grimes 	if (so->so_proto == 0)
1476df8bae1dSRodney W. Grimes 		return (EOPNOTSUPP);
1477df8bae1dSRodney W. Grimes #ifndef COMPAT_43
147882cd038dSYoshinobu Inoue 	error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
14792c37256eSGarrett Wollman 								 data,
1480b40ce416SJulian Elischer 								 ifp, td));
1481df8bae1dSRodney W. Grimes #else
1482df8bae1dSRodney W. Grimes 	{
1483df8bae1dSRodney W. Grimes 		int ocmd = cmd;
1484df8bae1dSRodney W. Grimes 
1485df8bae1dSRodney W. Grimes 		switch (cmd) {
1486df8bae1dSRodney W. Grimes 
1487df8bae1dSRodney W. Grimes 		case SIOCSIFDSTADDR:
1488df8bae1dSRodney W. Grimes 		case SIOCSIFADDR:
1489df8bae1dSRodney W. Grimes 		case SIOCSIFBRDADDR:
1490df8bae1dSRodney W. Grimes 		case SIOCSIFNETMASK:
1491df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN
1492df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_family == 0 &&
1493df8bae1dSRodney W. Grimes 			    ifr->ifr_addr.sa_len < 16) {
1494df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1495df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
1496df8bae1dSRodney W. Grimes 			}
1497df8bae1dSRodney W. Grimes #else
1498df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_len == 0)
1499df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
1500df8bae1dSRodney W. Grimes #endif
1501df8bae1dSRodney W. Grimes 			break;
1502df8bae1dSRodney W. Grimes 
1503df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
1504df8bae1dSRodney W. Grimes 			cmd = SIOCGIFADDR;
1505df8bae1dSRodney W. Grimes 			break;
1506df8bae1dSRodney W. Grimes 
1507df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
1508df8bae1dSRodney W. Grimes 			cmd = SIOCGIFDSTADDR;
1509df8bae1dSRodney W. Grimes 			break;
1510df8bae1dSRodney W. Grimes 
1511df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
1512df8bae1dSRodney W. Grimes 			cmd = SIOCGIFBRDADDR;
1513df8bae1dSRodney W. Grimes 			break;
1514df8bae1dSRodney W. Grimes 
1515df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
1516df8bae1dSRodney W. Grimes 			cmd = SIOCGIFNETMASK;
1517df8bae1dSRodney W. Grimes 		}
15182c37256eSGarrett Wollman 		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
15192c37256eSGarrett Wollman 								   cmd,
15202c37256eSGarrett Wollman 								   data,
1521b40ce416SJulian Elischer 								   ifp, td));
1522df8bae1dSRodney W. Grimes 		switch (ocmd) {
1523df8bae1dSRodney W. Grimes 
1524df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
1525df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
1526df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
1527df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
1528df8bae1dSRodney W. Grimes 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
152982cd038dSYoshinobu Inoue 
153082cd038dSYoshinobu Inoue 		}
153182cd038dSYoshinobu Inoue 	}
153282cd038dSYoshinobu Inoue #endif /* COMPAT_43 */
153382cd038dSYoshinobu Inoue 
153482cd038dSYoshinobu Inoue 	if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
153582cd038dSYoshinobu Inoue #ifdef INET6
153688ff5695SSUZUKI Shinsuke 		DELAY(100);/* XXX: temporary workaround for fxp issue*/
153782cd038dSYoshinobu Inoue 		if (ifp->if_flags & IFF_UP) {
153882cd038dSYoshinobu Inoue 			int s = splimp();
153982cd038dSYoshinobu Inoue 			in6_if_up(ifp);
154082cd038dSYoshinobu Inoue 			splx(s);
154182cd038dSYoshinobu Inoue 		}
154282cd038dSYoshinobu Inoue #endif
1543df8bae1dSRodney W. Grimes 	}
1544df8bae1dSRodney W. Grimes 	return (error);
1545df8bae1dSRodney W. Grimes }
1546df8bae1dSRodney W. Grimes 
1547df8bae1dSRodney W. Grimes /*
1548963e4c2aSGarrett Wollman  * Set/clear promiscuous mode on interface ifp based on the truth value
1549963e4c2aSGarrett Wollman  * of pswitch.  The calls are reference counted so that only the first
1550963e4c2aSGarrett Wollman  * "on" request actually has an effect, as does the final "off" request.
1551963e4c2aSGarrett Wollman  * Results are undefined if the "off" and "on" requests are not matched.
1552963e4c2aSGarrett Wollman  */
1553963e4c2aSGarrett Wollman int
1554963e4c2aSGarrett Wollman ifpromisc(ifp, pswitch)
1555963e4c2aSGarrett Wollman 	struct ifnet *ifp;
1556963e4c2aSGarrett Wollman 	int pswitch;
1557963e4c2aSGarrett Wollman {
1558963e4c2aSGarrett Wollman 	struct ifreq ifr;
15594a26224cSGarrett Wollman 	int error;
15604f3c11a6SBill Fenner 	int oldflags, oldpcount;
1561963e4c2aSGarrett Wollman 
15624f3c11a6SBill Fenner 	oldpcount = ifp->if_pcount;
15632c514a31SBrian Somers 	oldflags = ifp->if_flags;
1564963e4c2aSGarrett Wollman 	if (pswitch) {
1565963e4c2aSGarrett Wollman 		/*
1566963e4c2aSGarrett Wollman 		 * If the device is not configured up, we cannot put it in
1567963e4c2aSGarrett Wollman 		 * promiscuous mode.
1568963e4c2aSGarrett Wollman 		 */
1569963e4c2aSGarrett Wollman 		if ((ifp->if_flags & IFF_UP) == 0)
1570963e4c2aSGarrett Wollman 			return (ENETDOWN);
1571963e4c2aSGarrett Wollman 		if (ifp->if_pcount++ != 0)
1572963e4c2aSGarrett Wollman 			return (0);
1573963e4c2aSGarrett Wollman 		ifp->if_flags |= IFF_PROMISC;
1574963e4c2aSGarrett Wollman 	} else {
1575963e4c2aSGarrett Wollman 		if (--ifp->if_pcount > 0)
1576963e4c2aSGarrett Wollman 			return (0);
1577963e4c2aSGarrett Wollman 		ifp->if_flags &= ~IFF_PROMISC;
1578963e4c2aSGarrett Wollman 	}
157962f76486SMaxim Sobolev 	ifr.ifr_flags = ifp->if_flags & 0xffff;
158062f76486SMaxim Sobolev 	ifr.ifr_flagshigh = ifp->if_flags >> 16;
15814a26224cSGarrett Wollman 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
15824f3c11a6SBill Fenner 	if (error == 0) {
15834f3c11a6SBill Fenner 		log(LOG_INFO, "%s%d: promiscuous mode %s\n",
15844f3c11a6SBill Fenner 		    ifp->if_name, ifp->if_unit,
15854f3c11a6SBill Fenner 		    (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
15864a26224cSGarrett Wollman 		rt_ifmsg(ifp);
15874f3c11a6SBill Fenner 	} else {
15884f3c11a6SBill Fenner 		ifp->if_pcount = oldpcount;
15892c514a31SBrian Somers 		ifp->if_flags = oldflags;
15904f3c11a6SBill Fenner 	}
15914a26224cSGarrett Wollman 	return error;
1592963e4c2aSGarrett Wollman }
1593963e4c2aSGarrett Wollman 
1594963e4c2aSGarrett Wollman /*
1595df8bae1dSRodney W. Grimes  * Return interface configuration
1596df8bae1dSRodney W. Grimes  * of system.  List may be used
1597df8bae1dSRodney W. Grimes  * in later ioctl's (above) to get
1598df8bae1dSRodney W. Grimes  * other information.
1599df8bae1dSRodney W. Grimes  */
1600df8bae1dSRodney W. Grimes /*ARGSUSED*/
16013bda9f9bSPoul-Henning Kamp static int
1602df8bae1dSRodney W. Grimes ifconf(cmd, data)
1603ecbb00a2SDoug Rabson 	u_long cmd;
1604df8bae1dSRodney W. Grimes 	caddr_t data;
1605df8bae1dSRodney W. Grimes {
16060b59d917SJonathan Lemon 	struct ifconf *ifc = (struct ifconf *)data;
16070b59d917SJonathan Lemon 	struct ifnet *ifp;
16080b59d917SJonathan Lemon 	struct ifaddr *ifa;
1609df8bae1dSRodney W. Grimes 	struct ifreq ifr, *ifrp;
1610df8bae1dSRodney W. Grimes 	int space = ifc->ifc_len, error = 0;
1611df8bae1dSRodney W. Grimes 
1612df8bae1dSRodney W. Grimes 	ifrp = ifc->ifc_req;
16130b59d917SJonathan Lemon 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
16141ce9bf88SPoul-Henning Kamp 		char workbuf[64];
161575c13541SPoul-Henning Kamp 		int ifnlen, addrs;
16162624cf89SGarrett Wollman 
16172defe5cdSJonathan Lemon 		if (space < sizeof(ifr))
16180b59d917SJonathan Lemon 			break;
16192127f260SArchie Cobbs 		ifnlen = snprintf(workbuf, sizeof(workbuf),
16202127f260SArchie Cobbs 		    "%s%d", ifp->if_name, ifp->if_unit);
16211ce9bf88SPoul-Henning Kamp 		if(ifnlen + 1 > sizeof ifr.ifr_name) {
16222624cf89SGarrett Wollman 			error = ENAMETOOLONG;
1623b3f1e629SGuido van Rooij 			break;
16242624cf89SGarrett Wollman 		} else {
16251ce9bf88SPoul-Henning Kamp 			strcpy(ifr.ifr_name, workbuf);
16262624cf89SGarrett Wollman 		}
16272624cf89SGarrett Wollman 
162875c13541SPoul-Henning Kamp 		addrs = 0;
16292defe5cdSJonathan Lemon 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
16302defe5cdSJonathan Lemon 			struct sockaddr *sa = ifa->ifa_addr;
16312defe5cdSJonathan Lemon 
16322defe5cdSJonathan Lemon 			if (space < sizeof(ifr))
16332defe5cdSJonathan Lemon 				break;
1634a854ed98SJohn Baldwin 			if (jailed(curthread->td_ucred) &&
1635a854ed98SJohn Baldwin 			    prison_if(curthread->td_ucred, sa))
163675c13541SPoul-Henning Kamp 				continue;
163775c13541SPoul-Henning Kamp 			addrs++;
1638df8bae1dSRodney W. Grimes #ifdef COMPAT_43
1639df8bae1dSRodney W. Grimes 			if (cmd == OSIOCGIFCONF) {
1640df8bae1dSRodney W. Grimes 				struct osockaddr *osa =
1641df8bae1dSRodney W. Grimes 					 (struct osockaddr *)&ifr.ifr_addr;
1642df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
1643df8bae1dSRodney W. Grimes 				osa->sa_family = sa->sa_family;
1644df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1645df8bae1dSRodney W. Grimes 						sizeof (ifr));
1646df8bae1dSRodney W. Grimes 				ifrp++;
1647df8bae1dSRodney W. Grimes 			} else
1648df8bae1dSRodney W. Grimes #endif
1649df8bae1dSRodney W. Grimes 			if (sa->sa_len <= sizeof(*sa)) {
1650df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
1651df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1652df8bae1dSRodney W. Grimes 						sizeof (ifr));
1653df8bae1dSRodney W. Grimes 				ifrp++;
1654df8bae1dSRodney W. Grimes 			} else {
1655d91a068eSGuido van Rooij 				if (space < sizeof (ifr) + sa->sa_len -
1656d91a068eSGuido van Rooij 					    sizeof(*sa))
1657b3f1e629SGuido van Rooij 					break;
1658df8bae1dSRodney W. Grimes 				space -= sa->sa_len - sizeof(*sa);
1659df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1660df8bae1dSRodney W. Grimes 						sizeof (ifr.ifr_name));
1661df8bae1dSRodney W. Grimes 				if (error == 0)
1662df8bae1dSRodney W. Grimes 				    error = copyout((caddr_t)sa,
1663df8bae1dSRodney W. Grimes 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1664df8bae1dSRodney W. Grimes 				ifrp = (struct ifreq *)
1665df8bae1dSRodney W. Grimes 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1666df8bae1dSRodney W. Grimes 			}
1667df8bae1dSRodney W. Grimes 			if (error)
1668df8bae1dSRodney W. Grimes 				break;
1669df8bae1dSRodney W. Grimes 			space -= sizeof (ifr);
1670df8bae1dSRodney W. Grimes 		}
1671b3f1e629SGuido van Rooij 		if (error)
1672b3f1e629SGuido van Rooij 			break;
167375c13541SPoul-Henning Kamp 		if (!addrs) {
167475c13541SPoul-Henning Kamp 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
167575c13541SPoul-Henning Kamp 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
167675c13541SPoul-Henning Kamp 			    sizeof (ifr));
167775c13541SPoul-Henning Kamp 			if (error)
167875c13541SPoul-Henning Kamp 				break;
1679b3f1e629SGuido van Rooij 			space -= sizeof (ifr);
1680b3f1e629SGuido van Rooij 			ifrp++;
168175c13541SPoul-Henning Kamp 		}
1682df8bae1dSRodney W. Grimes 	}
1683df8bae1dSRodney W. Grimes 	ifc->ifc_len -= space;
1684df8bae1dSRodney W. Grimes 	return (error);
1685df8bae1dSRodney W. Grimes }
1686df8bae1dSRodney W. Grimes 
16871158dfb7SGarrett Wollman /*
16881158dfb7SGarrett Wollman  * Just like if_promisc(), but for all-multicast-reception mode.
16891158dfb7SGarrett Wollman  */
16901158dfb7SGarrett Wollman int
16911158dfb7SGarrett Wollman if_allmulti(ifp, onswitch)
16921158dfb7SGarrett Wollman 	struct ifnet *ifp;
16931158dfb7SGarrett Wollman 	int onswitch;
16941158dfb7SGarrett Wollman {
16951158dfb7SGarrett Wollman 	int error = 0;
16961158dfb7SGarrett Wollman 	int s = splimp();
1697ee0a4f7eSSUZUKI Shinsuke 	struct ifreq ifr;
16981158dfb7SGarrett Wollman 
16991158dfb7SGarrett Wollman 	if (onswitch) {
17001158dfb7SGarrett Wollman 		if (ifp->if_amcount++ == 0) {
17011158dfb7SGarrett Wollman 			ifp->if_flags |= IFF_ALLMULTI;
170262f76486SMaxim Sobolev 			ifr.ifr_flags = ifp->if_flags & 0xffff;
170362f76486SMaxim Sobolev 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
1704ee0a4f7eSSUZUKI Shinsuke 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
17051158dfb7SGarrett Wollman 		}
17061158dfb7SGarrett Wollman 	} else {
17071158dfb7SGarrett Wollman 		if (ifp->if_amcount > 1) {
17081158dfb7SGarrett Wollman 			ifp->if_amcount--;
17091158dfb7SGarrett Wollman 		} else {
17101158dfb7SGarrett Wollman 			ifp->if_amcount = 0;
17111158dfb7SGarrett Wollman 			ifp->if_flags &= ~IFF_ALLMULTI;
171262f76486SMaxim Sobolev 			ifr.ifr_flags = ifp->if_flags & 0xffff;;
171362f76486SMaxim Sobolev 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
1714ee0a4f7eSSUZUKI Shinsuke 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
17151158dfb7SGarrett Wollman 		}
17161158dfb7SGarrett Wollman 	}
17171158dfb7SGarrett Wollman 	splx(s);
17184a26224cSGarrett Wollman 
17194a26224cSGarrett Wollman 	if (error == 0)
17204a26224cSGarrett Wollman 		rt_ifmsg(ifp);
17211158dfb7SGarrett Wollman 	return error;
17221158dfb7SGarrett Wollman }
17231158dfb7SGarrett Wollman 
17241158dfb7SGarrett Wollman /*
17251158dfb7SGarrett Wollman  * Add a multicast listenership to the interface in question.
17261158dfb7SGarrett Wollman  * The link layer provides a routine which converts
17271158dfb7SGarrett Wollman  */
17281158dfb7SGarrett Wollman int
1729373f88edSGarrett Wollman if_addmulti(ifp, sa, retifma)
17301158dfb7SGarrett Wollman 	struct ifnet *ifp;	/* interface to manipulate */
17311158dfb7SGarrett Wollman 	struct sockaddr *sa;	/* address to add */
1732b2053118SGarrett Wollman 	struct ifmultiaddr **retifma;
17331158dfb7SGarrett Wollman {
17341158dfb7SGarrett Wollman 	struct sockaddr *llsa, *dupsa;
17351158dfb7SGarrett Wollman 	int error, s;
17361158dfb7SGarrett Wollman 	struct ifmultiaddr *ifma;
17371158dfb7SGarrett Wollman 
173857af7922SJulian Elischer 	/*
173957af7922SJulian Elischer 	 * If the matching multicast address already exists
174057af7922SJulian Elischer 	 * then don't add a new one, just add a reference
174157af7922SJulian Elischer 	 */
17426817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
174357af7922SJulian Elischer 		if (equal(sa, ifma->ifma_addr)) {
17441158dfb7SGarrett Wollman 			ifma->ifma_refcount++;
174557af7922SJulian Elischer 			if (retifma)
174657af7922SJulian Elischer 				*retifma = ifma;
17471158dfb7SGarrett Wollman 			return 0;
17481158dfb7SGarrett Wollman 		}
174957af7922SJulian Elischer 	}
17501158dfb7SGarrett Wollman 
17511158dfb7SGarrett Wollman 	/*
17521158dfb7SGarrett Wollman 	 * Give the link layer a chance to accept/reject it, and also
17531158dfb7SGarrett Wollman 	 * find out which AF_LINK address this maps to, if it isn't one
17541158dfb7SGarrett Wollman 	 * already.
17551158dfb7SGarrett Wollman 	 */
17561158dfb7SGarrett Wollman 	if (ifp->if_resolvemulti) {
17571158dfb7SGarrett Wollman 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
17581158dfb7SGarrett Wollman 		if (error) return error;
17591158dfb7SGarrett Wollman 	} else {
17601158dfb7SGarrett Wollman 		llsa = 0;
17611158dfb7SGarrett Wollman 	}
17621158dfb7SGarrett Wollman 
17631158dfb7SGarrett Wollman 	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
17641158dfb7SGarrett Wollman 	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
17651158dfb7SGarrett Wollman 	bcopy(sa, dupsa, sa->sa_len);
17661158dfb7SGarrett Wollman 
17671158dfb7SGarrett Wollman 	ifma->ifma_addr = dupsa;
17681158dfb7SGarrett Wollman 	ifma->ifma_lladdr = llsa;
17691158dfb7SGarrett Wollman 	ifma->ifma_ifp = ifp;
17701158dfb7SGarrett Wollman 	ifma->ifma_refcount = 1;
1771373f88edSGarrett Wollman 	ifma->ifma_protospec = 0;
1772477180fbSGarrett Wollman 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1773373f88edSGarrett Wollman 
17741158dfb7SGarrett Wollman 	/*
17751158dfb7SGarrett Wollman 	 * Some network interfaces can scan the address list at
17761158dfb7SGarrett Wollman 	 * interrupt time; lock them out.
17771158dfb7SGarrett Wollman 	 */
17781158dfb7SGarrett Wollman 	s = splimp();
17796817526dSPoul-Henning Kamp 	TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
17801158dfb7SGarrett Wollman 	splx(s);
178113990766SJonathan Mini 	if (retifma != NULL)
1782373f88edSGarrett Wollman 		*retifma = ifma;
17831158dfb7SGarrett Wollman 
17841158dfb7SGarrett Wollman 	if (llsa != 0) {
17856817526dSPoul-Henning Kamp 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
17861158dfb7SGarrett Wollman 			if (equal(ifma->ifma_addr, llsa))
17871158dfb7SGarrett Wollman 				break;
17881158dfb7SGarrett Wollman 		}
17891158dfb7SGarrett Wollman 		if (ifma) {
17901158dfb7SGarrett Wollman 			ifma->ifma_refcount++;
17911158dfb7SGarrett Wollman 		} else {
17921158dfb7SGarrett Wollman 			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
17931158dfb7SGarrett Wollman 			       M_IFMADDR, M_WAITOK);
1794477180fbSGarrett Wollman 			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1795477180fbSGarrett Wollman 			       M_IFMADDR, M_WAITOK);
1796477180fbSGarrett Wollman 			bcopy(llsa, dupsa, llsa->sa_len);
1797477180fbSGarrett Wollman 			ifma->ifma_addr = dupsa;
17981158dfb7SGarrett Wollman 			ifma->ifma_ifp = ifp;
17991158dfb7SGarrett Wollman 			ifma->ifma_refcount = 1;
18001158dfb7SGarrett Wollman 			s = splimp();
18016817526dSPoul-Henning Kamp 			TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
18021158dfb7SGarrett Wollman 			splx(s);
18031158dfb7SGarrett Wollman 		}
180457af7922SJulian Elischer 	}
18051158dfb7SGarrett Wollman 	/*
18061158dfb7SGarrett Wollman 	 * We are certain we have added something, so call down to the
18071158dfb7SGarrett Wollman 	 * interface to let them know about it.
18081158dfb7SGarrett Wollman 	 */
18091158dfb7SGarrett Wollman 	s = splimp();
18101158dfb7SGarrett Wollman 	ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
18111158dfb7SGarrett Wollman 	splx(s);
18121158dfb7SGarrett Wollman 
18131158dfb7SGarrett Wollman 	return 0;
18141158dfb7SGarrett Wollman }
18151158dfb7SGarrett Wollman 
18161158dfb7SGarrett Wollman /*
18171158dfb7SGarrett Wollman  * Remove a reference to a multicast address on this interface.  Yell
18181158dfb7SGarrett Wollman  * if the request does not match an existing membership.
18191158dfb7SGarrett Wollman  */
18201158dfb7SGarrett Wollman int
18211158dfb7SGarrett Wollman if_delmulti(ifp, sa)
18221158dfb7SGarrett Wollman 	struct ifnet *ifp;
18231158dfb7SGarrett Wollman 	struct sockaddr *sa;
18241158dfb7SGarrett Wollman {
18251158dfb7SGarrett Wollman 	struct ifmultiaddr *ifma;
18261158dfb7SGarrett Wollman 	int s;
18271158dfb7SGarrett Wollman 
18286817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
18291158dfb7SGarrett Wollman 		if (equal(sa, ifma->ifma_addr))
18301158dfb7SGarrett Wollman 			break;
18311158dfb7SGarrett Wollman 	if (ifma == 0)
18321158dfb7SGarrett Wollman 		return ENOENT;
18331158dfb7SGarrett Wollman 
18341158dfb7SGarrett Wollman 	if (ifma->ifma_refcount > 1) {
18351158dfb7SGarrett Wollman 		ifma->ifma_refcount--;
18361158dfb7SGarrett Wollman 		return 0;
18371158dfb7SGarrett Wollman 	}
18381158dfb7SGarrett Wollman 
1839477180fbSGarrett Wollman 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
18401158dfb7SGarrett Wollman 	sa = ifma->ifma_lladdr;
18411158dfb7SGarrett Wollman 	s = splimp();
18426817526dSPoul-Henning Kamp 	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
1843ccb7cc8dSYaroslav Tykhiy 	/*
1844ccb7cc8dSYaroslav Tykhiy 	 * Make sure the interface driver is notified
1845ccb7cc8dSYaroslav Tykhiy 	 * in the case of a link layer mcast group being left.
1846ccb7cc8dSYaroslav Tykhiy 	 */
1847ccb7cc8dSYaroslav Tykhiy 	if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0)
1848ccb7cc8dSYaroslav Tykhiy 		ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
18491158dfb7SGarrett Wollman 	splx(s);
18501158dfb7SGarrett Wollman 	free(ifma->ifma_addr, M_IFMADDR);
18511158dfb7SGarrett Wollman 	free(ifma, M_IFMADDR);
18521158dfb7SGarrett Wollman 	if (sa == 0)
18531158dfb7SGarrett Wollman 		return 0;
18541158dfb7SGarrett Wollman 
18551158dfb7SGarrett Wollman 	/*
18561158dfb7SGarrett Wollman 	 * Now look for the link-layer address which corresponds to
18571158dfb7SGarrett Wollman 	 * this network address.  It had been squirreled away in
18581158dfb7SGarrett Wollman 	 * ifma->ifma_lladdr for this purpose (so we don't have
18591158dfb7SGarrett Wollman 	 * to call ifp->if_resolvemulti() again), and we saved that
18601158dfb7SGarrett Wollman 	 * value in sa above.  If some nasty deleted the
18611158dfb7SGarrett Wollman 	 * link-layer address out from underneath us, we can deal because
18621158dfb7SGarrett Wollman 	 * the address we stored was is not the same as the one which was
18631158dfb7SGarrett Wollman 	 * in the record for the link-layer address.  (So we don't complain
18641158dfb7SGarrett Wollman 	 * in that case.)
18651158dfb7SGarrett Wollman 	 */
18666817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
18671158dfb7SGarrett Wollman 		if (equal(sa, ifma->ifma_addr))
18681158dfb7SGarrett Wollman 			break;
18691158dfb7SGarrett Wollman 	if (ifma == 0)
18701158dfb7SGarrett Wollman 		return 0;
18711158dfb7SGarrett Wollman 
18721158dfb7SGarrett Wollman 	if (ifma->ifma_refcount > 1) {
18731158dfb7SGarrett Wollman 		ifma->ifma_refcount--;
18741158dfb7SGarrett Wollman 		return 0;
18751158dfb7SGarrett Wollman 	}
18761158dfb7SGarrett Wollman 
18771158dfb7SGarrett Wollman 	s = splimp();
18786817526dSPoul-Henning Kamp 	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
1879c7323482SBill Paul 	ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
18801158dfb7SGarrett Wollman 	splx(s);
18811158dfb7SGarrett Wollman 	free(ifma->ifma_addr, M_IFMADDR);
18821158dfb7SGarrett Wollman 	free(sa, M_IFMADDR);
18831158dfb7SGarrett Wollman 	free(ifma, M_IFMADDR);
18841158dfb7SGarrett Wollman 
18851158dfb7SGarrett Wollman 	return 0;
18861158dfb7SGarrett Wollman }
18871158dfb7SGarrett Wollman 
188866ce51ceSArchie Cobbs /*
188966ce51ceSArchie Cobbs  * Set the link layer address on an interface.
189066ce51ceSArchie Cobbs  *
189166ce51ceSArchie Cobbs  * At this time we only support certain types of interfaces,
189266ce51ceSArchie Cobbs  * and we don't allow the length of the address to change.
189366ce51ceSArchie Cobbs  */
189466ce51ceSArchie Cobbs int
189566ce51ceSArchie Cobbs if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
189666ce51ceSArchie Cobbs {
189766ce51ceSArchie Cobbs 	struct sockaddr_dl *sdl;
189866ce51ceSArchie Cobbs 	struct ifaddr *ifa;
1899d637e989SPeter Wemm 	struct ifreq ifr;
190066ce51ceSArchie Cobbs 
1901f9132cebSJonathan Lemon 	ifa = ifaddr_byindex(ifp->if_index);
190266ce51ceSArchie Cobbs 	if (ifa == NULL)
190366ce51ceSArchie Cobbs 		return (EINVAL);
190466ce51ceSArchie Cobbs 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
190566ce51ceSArchie Cobbs 	if (sdl == NULL)
190666ce51ceSArchie Cobbs 		return (EINVAL);
190766ce51ceSArchie Cobbs 	if (len != sdl->sdl_alen)	/* don't allow length to change */
190866ce51ceSArchie Cobbs 		return (EINVAL);
190966ce51ceSArchie Cobbs 	switch (ifp->if_type) {
191066ce51ceSArchie Cobbs 	case IFT_ETHER:			/* these types use struct arpcom */
191166ce51ceSArchie Cobbs 	case IFT_FDDI:
191266ce51ceSArchie Cobbs 	case IFT_XETHER:
191366ce51ceSArchie Cobbs 	case IFT_ISO88025:
1914b7bffa71SYaroslav Tykhiy 	case IFT_L2VLAN:
191566ce51ceSArchie Cobbs 		bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
191666ce51ceSArchie Cobbs 		bcopy(lladdr, LLADDR(sdl), len);
191766ce51ceSArchie Cobbs 		break;
191866ce51ceSArchie Cobbs 	default:
191966ce51ceSArchie Cobbs 		return (ENODEV);
192066ce51ceSArchie Cobbs 	}
192166ce51ceSArchie Cobbs 	/*
192266ce51ceSArchie Cobbs 	 * If the interface is already up, we need
192366ce51ceSArchie Cobbs 	 * to re-init it in order to reprogram its
192466ce51ceSArchie Cobbs 	 * address filter.
192566ce51ceSArchie Cobbs 	 */
192666ce51ceSArchie Cobbs 	if ((ifp->if_flags & IFF_UP) != 0) {
192766ce51ceSArchie Cobbs 		ifp->if_flags &= ~IFF_UP;
192862f76486SMaxim Sobolev 		ifr.ifr_flags = ifp->if_flags & 0xffff;
192962f76486SMaxim Sobolev 		ifr.ifr_flagshigh = ifp->if_flags >> 16;
1930ee0a4f7eSSUZUKI Shinsuke 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
193166ce51ceSArchie Cobbs 		ifp->if_flags |= IFF_UP;
193262f76486SMaxim Sobolev 		ifr.ifr_flags = ifp->if_flags & 0xffff;
193362f76486SMaxim Sobolev 		ifr.ifr_flagshigh = ifp->if_flags >> 16;
1934ee0a4f7eSSUZUKI Shinsuke 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1935b2c08f43SLuigi Rizzo #ifdef INET
1936b2c08f43SLuigi Rizzo 		/*
1937b2c08f43SLuigi Rizzo 		 * Also send gratuitous ARPs to notify other nodes about
1938b2c08f43SLuigi Rizzo 		 * the address change.
1939b2c08f43SLuigi Rizzo 		 */
1940b2c08f43SLuigi Rizzo 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1941b2c08f43SLuigi Rizzo 			if (ifa->ifa_addr != NULL &&
1942b2c08f43SLuigi Rizzo 			    ifa->ifa_addr->sa_family == AF_INET)
1943c0933269SPeter Wemm 				arp_ifinit(ifp, ifa);
1944b2c08f43SLuigi Rizzo 		}
1945b2c08f43SLuigi Rizzo #endif
194666ce51ceSArchie Cobbs 	}
194766ce51ceSArchie Cobbs 	return (0);
194866ce51ceSArchie Cobbs }
194966ce51ceSArchie Cobbs 
1950373f88edSGarrett Wollman struct ifmultiaddr *
1951373f88edSGarrett Wollman ifmaof_ifpforaddr(sa, ifp)
1952373f88edSGarrett Wollman 	struct sockaddr *sa;
1953373f88edSGarrett Wollman 	struct ifnet *ifp;
1954373f88edSGarrett Wollman {
1955373f88edSGarrett Wollman 	struct ifmultiaddr *ifma;
1956373f88edSGarrett Wollman 
19576817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1958373f88edSGarrett Wollman 		if (equal(ifma->ifma_addr, sa))
1959373f88edSGarrett Wollman 			break;
1960373f88edSGarrett Wollman 
1961373f88edSGarrett Wollman 	return ifma;
1962373f88edSGarrett Wollman }
1963373f88edSGarrett Wollman 
1964602d513cSGarrett Wollman SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
19652c37256eSGarrett Wollman SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
1966