xref: /freebsd/sys/net/if.c (revision fa882e87a5c1810999947ef22360f8974394d1a5)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1980, 1986, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
3366afbd68SRuslan Ermilov  *	@(#)if.c	8.5 (Berkeley) 1/9/95
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
375591b823SEivind Eklund #include "opt_compat.h"
38cfa1ca9dSYoshinobu Inoue #include "opt_inet6.h"
390d0f9d1eSYoshinobu Inoue #include "opt_inet.h"
40e70cd263SRobert Watson #include "opt_mac.h"
415591b823SEivind Eklund 
42df8bae1dSRodney W. Grimes #include <sys/param.h>
43f13ad206SJonathan Lemon #include <sys/conf.h>
44e70cd263SRobert Watson #include <sys/mac.h>
454d1d4912SBruce Evans #include <sys/malloc.h>
46d2b4566aSJonathan Lemon #include <sys/bus.h>
47df8bae1dSRodney W. Grimes #include <sys/mbuf.h>
48df8bae1dSRodney W. Grimes #include <sys/systm.h>
49df8bae1dSRodney W. Grimes #include <sys/proc.h>
50df8bae1dSRodney W. Grimes #include <sys/socket.h>
51df8bae1dSRodney W. Grimes #include <sys/socketvar.h>
52df8bae1dSRodney W. Grimes #include <sys/protosw.h>
53df8bae1dSRodney W. Grimes #include <sys/kernel.h>
5451a53488SBruce Evans #include <sys/sockio.h>
55963e4c2aSGarrett Wollman #include <sys/syslog.h>
56602d513cSGarrett Wollman #include <sys/sysctl.h>
5791421ba2SRobert Watson #include <sys/jail.h>
58fa882e87SBrooks Davis #include <machine/stdarg.h>
59df8bae1dSRodney W. Grimes 
60df8bae1dSRodney W. Grimes #include <net/if.h>
61b106252cSBill Paul #include <net/if_arp.h>
62df8bae1dSRodney W. Grimes #include <net/if_dl.h>
6366ce51ceSArchie Cobbs #include <net/if_types.h>
6430aad87dSBrooks Davis #include <net/if_var.h>
659448326fSPoul-Henning Kamp #include <net/radix.h>
665500d3beSWarner Losh #include <net/route.h>
67df8bae1dSRodney W. Grimes 
680d0f9d1eSYoshinobu Inoue #if defined(INET) || defined(INET6)
6982cd038dSYoshinobu Inoue /*XXX*/
7082cd038dSYoshinobu Inoue #include <netinet/in.h>
710d0f9d1eSYoshinobu Inoue #include <netinet/in_var.h>
723411310dSYoshinobu Inoue #ifdef INET6
73978ee2edSJun-ichiro itojun Hagino #include <netinet6/in6_var.h>
74978ee2edSJun-ichiro itojun Hagino #include <netinet6/in6_ifattach.h>
753411310dSYoshinobu Inoue #endif
7682cd038dSYoshinobu Inoue #endif
77c0933269SPeter Wemm #ifdef INET
78c0933269SPeter Wemm #include <netinet/if_ether.h>
79c0933269SPeter Wemm #endif
8082cd038dSYoshinobu Inoue 
810b59d917SJonathan Lemon static int	ifconf(u_long, caddr_t);
82f9132cebSJonathan Lemon static void	if_grow(void);
83f9132cebSJonathan Lemon static void	if_init(void *);
84f9132cebSJonathan Lemon static void	if_check(void *);
85ffb5a104SJonathan Lemon static int	if_findindex(struct ifnet *);
860b59d917SJonathan Lemon static void	if_qflush(struct ifqueue *);
870b59d917SJonathan Lemon static void	if_slowtimo(void *);
888071913dSRuslan Ermilov static void	link_rtrequest(int, struct rtentry *, struct rt_addrinfo *);
890b59d917SJonathan Lemon static int	if_rtdel(struct radix_node *, void *);
900b59d917SJonathan Lemon static struct	if_clone *if_clone_lookup(const char *, int *);
910b59d917SJonathan Lemon static int	if_clone_list(struct if_clonereq *);
92f13ad206SJonathan Lemon static int	ifhwioctl(u_long, struct ifnet *, caddr_t, struct thread *);
9382cd038dSYoshinobu Inoue #ifdef INET6
9482cd038dSYoshinobu Inoue /*
9582cd038dSYoshinobu Inoue  * XXX: declare here to avoid to include many inet6 related files..
9682cd038dSYoshinobu Inoue  * should be more generalized?
9782cd038dSYoshinobu Inoue  */
98929ddbbbSAlfred Perlstein extern void	nd6_setmtu(struct ifnet *);
9982cd038dSYoshinobu Inoue #endif
10082cd038dSYoshinobu Inoue 
1010b59d917SJonathan Lemon int	if_index = 0;
102f9132cebSJonathan Lemon struct	ifindex_entry *ifindex_table = NULL;
1030b59d917SJonathan Lemon int	ifqmaxlen = IFQ_MAXLEN;
1040b59d917SJonathan Lemon struct	ifnethead ifnet;	/* depend on static init XXX */
10530aad87dSBrooks Davis int	if_cloners_count;
1060b59d917SJonathan Lemon LIST_HEAD(, if_clone) if_cloners = LIST_HEAD_INITIALIZER(if_cloners);
1070b59d917SJonathan Lemon 
108f9132cebSJonathan Lemon static int	if_indexlim = 8;
109f13ad206SJonathan Lemon static struct	klist ifklist;
110f9132cebSJonathan Lemon 
1119a2a57a1SJonathan Lemon static void	filt_netdetach(struct knote *kn);
1129a2a57a1SJonathan Lemon static int	filt_netdev(struct knote *kn, long hint);
1139a2a57a1SJonathan Lemon 
1149a2a57a1SJonathan Lemon static struct filterops netdev_filtops =
1159a2a57a1SJonathan Lemon     { 1, NULL, filt_netdetach, filt_netdev };
1169a2a57a1SJonathan Lemon 
1170b59d917SJonathan Lemon /*
1180b59d917SJonathan Lemon  * System initialization
1190b59d917SJonathan Lemon  */
120f9132cebSJonathan Lemon SYSINIT(interfaces, SI_SUB_INIT_IF, SI_ORDER_FIRST, if_init, NULL)
121f9132cebSJonathan Lemon SYSINIT(interface_check, SI_SUB_PROTO_IF, SI_ORDER_FIRST, if_check, NULL)
1220b59d917SJonathan Lemon 
1230b59d917SJonathan Lemon MALLOC_DEFINE(M_IFADDR, "ifaddr", "interface address");
1240b59d917SJonathan Lemon MALLOC_DEFINE(M_IFMADDR, "ether_multi", "link-level multicast address");
1253b16e7b2SMaxime Henrion MALLOC_DEFINE(M_CLONE, "clone", "interface cloning framework");
12630aad87dSBrooks Davis 
127f13ad206SJonathan Lemon #define CDEV_MAJOR	165
128f13ad206SJonathan Lemon 
129f13ad206SJonathan Lemon static d_open_t		netopen;
130f13ad206SJonathan Lemon static d_close_t	netclose;
131f13ad206SJonathan Lemon static d_ioctl_t	netioctl;
1329a2a57a1SJonathan Lemon static d_kqfilter_t	netkqfilter;
133f13ad206SJonathan Lemon 
134f13ad206SJonathan Lemon static struct cdevsw net_cdevsw = {
135f13ad206SJonathan Lemon 	/* open */	netopen,
136f13ad206SJonathan Lemon 	/* close */	netclose,
137f13ad206SJonathan Lemon 	/* read */	noread,
138f13ad206SJonathan Lemon 	/* write */	nowrite,
139f13ad206SJonathan Lemon 	/* ioctl */	netioctl,
140f13ad206SJonathan Lemon 	/* poll */	nopoll,
141f13ad206SJonathan Lemon 	/* mmap */	nommap,
142f13ad206SJonathan Lemon 	/* strategy */	nostrategy,
143f13ad206SJonathan Lemon 	/* name */	"net",
144f13ad206SJonathan Lemon 	/* maj */	CDEV_MAJOR,
145f13ad206SJonathan Lemon 	/* dump */	nodump,
146f13ad206SJonathan Lemon 	/* psize */	nopsize,
1479a2a57a1SJonathan Lemon 	/* flags */	D_KQFILTER,
1489a2a57a1SJonathan Lemon 	/* kqfilter */	netkqfilter,
149f13ad206SJonathan Lemon };
150f13ad206SJonathan Lemon 
151f13ad206SJonathan Lemon static int
152f13ad206SJonathan Lemon netopen(dev_t dev, int flag, int mode, struct thread *td)
153f13ad206SJonathan Lemon {
154f13ad206SJonathan Lemon 	return (0);
155f13ad206SJonathan Lemon }
156f13ad206SJonathan Lemon 
157f13ad206SJonathan Lemon static int
158f13ad206SJonathan Lemon netclose(dev_t dev, int flags, int fmt, struct thread *td)
159f13ad206SJonathan Lemon {
160f13ad206SJonathan Lemon 	return (0);
161f13ad206SJonathan Lemon }
162f13ad206SJonathan Lemon 
163f13ad206SJonathan Lemon static int
164f13ad206SJonathan Lemon netioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct thread *td)
165f13ad206SJonathan Lemon {
166f13ad206SJonathan Lemon 	struct ifnet *ifp;
167f13ad206SJonathan Lemon 	int error, idx;
168f13ad206SJonathan Lemon 
169f13ad206SJonathan Lemon 	/* only support interface specific ioctls */
170f13ad206SJonathan Lemon 	if (IOCGROUP(cmd) != 'i')
171f13ad206SJonathan Lemon 		return (EOPNOTSUPP);
172f13ad206SJonathan Lemon 	idx = minor(dev);
173f13ad206SJonathan Lemon 	if (idx == 0) {
174f13ad206SJonathan Lemon 		/*
175f13ad206SJonathan Lemon 		 * special network device, not interface.
176f13ad206SJonathan Lemon 		 */
177f13ad206SJonathan Lemon 		if (cmd == SIOCGIFCONF)
178f13ad206SJonathan Lemon 			return (ifconf(cmd, data));	/* XXX remove cmd */
179f13ad206SJonathan Lemon 		return (EOPNOTSUPP);
180f13ad206SJonathan Lemon 	}
181f13ad206SJonathan Lemon 
182f13ad206SJonathan Lemon 	ifp = ifnet_byindex(idx);
183f13ad206SJonathan Lemon 	if (ifp == NULL)
184f13ad206SJonathan Lemon 		return (ENXIO);
185f13ad206SJonathan Lemon 
186f13ad206SJonathan Lemon 	error = ifhwioctl(cmd, ifp, data, td);
187f13ad206SJonathan Lemon 	if (error == ENOIOCTL)
188f13ad206SJonathan Lemon 		error = EOPNOTSUPP;
189f13ad206SJonathan Lemon 	return (error);
190f13ad206SJonathan Lemon }
191f13ad206SJonathan Lemon 
1929a2a57a1SJonathan Lemon static int
1939a2a57a1SJonathan Lemon netkqfilter(dev_t dev, struct knote *kn)
1949a2a57a1SJonathan Lemon {
1959a2a57a1SJonathan Lemon 	struct klist *klist;
1969a2a57a1SJonathan Lemon 	struct ifnet *ifp;
1979a2a57a1SJonathan Lemon 	int idx;
1989a2a57a1SJonathan Lemon 
1999a2a57a1SJonathan Lemon 	idx = minor(dev);
2009a2a57a1SJonathan Lemon 	if (idx == 0) {
2019a2a57a1SJonathan Lemon 		klist = &ifklist;
2029a2a57a1SJonathan Lemon 	} else {
2039a2a57a1SJonathan Lemon 		ifp = ifnet_byindex(idx);
2049a2a57a1SJonathan Lemon 		if (ifp == NULL)
2059a2a57a1SJonathan Lemon 			return (1);
2069a2a57a1SJonathan Lemon 		klist = &ifp->if_klist;
2079a2a57a1SJonathan Lemon 	}
2089a2a57a1SJonathan Lemon 
2099a2a57a1SJonathan Lemon 	switch (kn->kn_filter) {
2109a2a57a1SJonathan Lemon 	case EVFILT_NETDEV:
2119a2a57a1SJonathan Lemon 		kn->kn_fop = &netdev_filtops;
2129a2a57a1SJonathan Lemon 		break;
2139a2a57a1SJonathan Lemon 	default:
2149a2a57a1SJonathan Lemon 		return (1);
2159a2a57a1SJonathan Lemon 	}
2169a2a57a1SJonathan Lemon 
2179a2a57a1SJonathan Lemon 	kn->kn_hook = (caddr_t)klist;
2189a2a57a1SJonathan Lemon 
2199a2a57a1SJonathan Lemon 	/* XXX locking? */
2209a2a57a1SJonathan Lemon 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
2219a2a57a1SJonathan Lemon 
2229a2a57a1SJonathan Lemon 	return (0);
2239a2a57a1SJonathan Lemon }
2249a2a57a1SJonathan Lemon 
2259a2a57a1SJonathan Lemon static void
2269a2a57a1SJonathan Lemon filt_netdetach(struct knote *kn)
2279a2a57a1SJonathan Lemon {
2289a2a57a1SJonathan Lemon 	struct klist *klist = (struct klist *)kn->kn_hook;
2299a2a57a1SJonathan Lemon 
2309a2a57a1SJonathan Lemon 	if (kn->kn_status & KN_DETACHED)
2319a2a57a1SJonathan Lemon 		return;
2329a2a57a1SJonathan Lemon 	SLIST_REMOVE(klist, kn, knote, kn_selnext);
2339a2a57a1SJonathan Lemon }
2349a2a57a1SJonathan Lemon 
2359a2a57a1SJonathan Lemon static int
2369a2a57a1SJonathan Lemon filt_netdev(struct knote *kn, long hint)
2379a2a57a1SJonathan Lemon {
2389a2a57a1SJonathan Lemon 
2399a2a57a1SJonathan Lemon 	/*
2409a2a57a1SJonathan Lemon 	 * Currently NOTE_EXIT is abused to indicate device detach.
2419a2a57a1SJonathan Lemon 	 */
2429a2a57a1SJonathan Lemon 	if (hint == NOTE_EXIT) {
2439a2a57a1SJonathan Lemon 		kn->kn_data = NOTE_LINKINV;
2449a2a57a1SJonathan Lemon                 kn->kn_status |= KN_DETACHED;
2459a2a57a1SJonathan Lemon                 kn->kn_flags |= (EV_EOF | EV_ONESHOT);
2469a2a57a1SJonathan Lemon                 return (1);
2479a2a57a1SJonathan Lemon         }
2489a2a57a1SJonathan Lemon 	kn->kn_data = hint;			/* current status */
2499a2a57a1SJonathan Lemon 	if (kn->kn_sfflags & hint)
2509a2a57a1SJonathan Lemon 		kn->kn_fflags |= hint;
2519a2a57a1SJonathan Lemon 	return (kn->kn_fflags != 0);
2529a2a57a1SJonathan Lemon }
2539a2a57a1SJonathan Lemon 
254df8bae1dSRodney W. Grimes /*
255df8bae1dSRodney W. Grimes  * Network interface utility routines.
256df8bae1dSRodney W. Grimes  *
257df8bae1dSRodney W. Grimes  * Routines with ifa_ifwith* names take sockaddr *'s as
258df8bae1dSRodney W. Grimes  * parameters.
259df8bae1dSRodney W. Grimes  */
2602b14f991SJulian Elischer /* ARGSUSED*/
261f9132cebSJonathan Lemon static void
262f9132cebSJonathan Lemon if_init(dummy)
263f9132cebSJonathan Lemon 	void *dummy;
264f9132cebSJonathan Lemon {
265f9132cebSJonathan Lemon 
266f9132cebSJonathan Lemon 	TAILQ_INIT(&ifnet);
267f13ad206SJonathan Lemon 	SLIST_INIT(&ifklist);
268f9132cebSJonathan Lemon 	if_grow();				/* create initial table */
269f13ad206SJonathan Lemon 	ifdev_byindex(0) = make_dev(&net_cdevsw, 0,
270f13ad206SJonathan Lemon 	    UID_ROOT, GID_WHEEL, 0600, "network");
271f9132cebSJonathan Lemon }
272f9132cebSJonathan Lemon 
273f9132cebSJonathan Lemon static void
274f9132cebSJonathan Lemon if_grow(void)
275f9132cebSJonathan Lemon {
276f9132cebSJonathan Lemon 	u_int n;
277f9132cebSJonathan Lemon 	struct ifindex_entry *e;
278f9132cebSJonathan Lemon 
279f9132cebSJonathan Lemon 	if_indexlim <<= 1;
280f9132cebSJonathan Lemon 	n = if_indexlim * sizeof(*e);
281f9132cebSJonathan Lemon 	e = malloc(n, M_IFADDR, M_WAITOK | M_ZERO);
282f9132cebSJonathan Lemon 	if (ifindex_table != NULL) {
283f9132cebSJonathan Lemon 		memcpy((caddr_t)e, (caddr_t)ifindex_table, n/2);
284f9132cebSJonathan Lemon 		free((caddr_t)ifindex_table, M_IFADDR);
285f9132cebSJonathan Lemon 	}
286f9132cebSJonathan Lemon 	ifindex_table = e;
287f9132cebSJonathan Lemon }
288f9132cebSJonathan Lemon 
289f9132cebSJonathan Lemon /* ARGSUSED*/
290f9132cebSJonathan Lemon static void
291f9132cebSJonathan Lemon if_check(dummy)
29227501cb6SBruce Evans 	void *dummy;
293df8bae1dSRodney W. Grimes {
2948ba5bdaeSPeter Wemm 	struct ifnet *ifp;
2958ba5bdaeSPeter Wemm 	int s;
296df8bae1dSRodney W. Grimes 
2978ba5bdaeSPeter Wemm 	s = splimp();
298fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
299e0ea20bcSPoul-Henning Kamp 		if (ifp->if_snd.ifq_maxlen == 0) {
300e0ea20bcSPoul-Henning Kamp 			printf("%s%d XXX: driver didn't set ifq_maxlen\n",
301e0ea20bcSPoul-Henning Kamp 			    ifp->if_name, ifp->if_unit);
302df8bae1dSRodney W. Grimes 			ifp->if_snd.ifq_maxlen = ifqmaxlen;
303e0ea20bcSPoul-Henning Kamp 		}
3045e980e22SJohn Baldwin 		if (!mtx_initialized(&ifp->if_snd.ifq_mtx)) {
305df5e1987SJonathan Lemon 			printf("%s%d XXX: driver didn't initialize queue mtx\n",
306df5e1987SJonathan Lemon 			    ifp->if_name, ifp->if_unit);
3076008862bSJohn Baldwin 			mtx_init(&ifp->if_snd.ifq_mtx, "unknown",
3086008862bSJohn Baldwin 			    MTX_NETWORK_LOCK, MTX_DEF);
309df5e1987SJonathan Lemon 		}
310df5e1987SJonathan Lemon 	}
3118ba5bdaeSPeter Wemm 	splx(s);
312df8bae1dSRodney W. Grimes 	if_slowtimo(0);
313df8bae1dSRodney W. Grimes }
314df8bae1dSRodney W. Grimes 
315ffb5a104SJonathan Lemon static int
316ffb5a104SJonathan Lemon if_findindex(struct ifnet *ifp)
317ffb5a104SJonathan Lemon {
318ffb5a104SJonathan Lemon 	int i, unit;
319ffb5a104SJonathan Lemon 	char eaddr[18], devname[32];
320d2b4566aSJonathan Lemon 	const char *name, *p;
321ffb5a104SJonathan Lemon 
322ffb5a104SJonathan Lemon 	switch (ifp->if_type) {
323ffb5a104SJonathan Lemon 	case IFT_ETHER:			/* these types use struct arpcom */
324ffb5a104SJonathan Lemon 	case IFT_FDDI:
325ffb5a104SJonathan Lemon 	case IFT_XETHER:
326ffb5a104SJonathan Lemon 	case IFT_ISO88025:
327ffb5a104SJonathan Lemon 	case IFT_L2VLAN:
328ffb5a104SJonathan Lemon 		snprintf(eaddr, 18, "%6D",
329ffb5a104SJonathan Lemon 		    ((struct arpcom *)ifp->if_softc)->ac_enaddr, ":");
330ffb5a104SJonathan Lemon 		break;
331ffb5a104SJonathan Lemon 	default:
332ffb5a104SJonathan Lemon 		eaddr[0] = '\0';
333ffb5a104SJonathan Lemon 		break;
334ffb5a104SJonathan Lemon 	}
335ffb5a104SJonathan Lemon 	snprintf(devname, 32, "%s%d", ifp->if_name, ifp->if_unit);
336ffb5a104SJonathan Lemon 	name = net_cdevsw.d_name;
337ffb5a104SJonathan Lemon 	i = 0;
338ffb5a104SJonathan Lemon 	while ((resource_find_dev(&i, name, &unit, NULL, NULL)) == 0) {
339ffb5a104SJonathan Lemon 		if (resource_string_value(name, unit, "ether", &p) == 0)
340ffb5a104SJonathan Lemon 			if (strcmp(p, eaddr) == 0)
341ffb5a104SJonathan Lemon 				goto found;
342ffb5a104SJonathan Lemon 		if (resource_string_value(name, unit, "dev", &p) == 0)
343ffb5a104SJonathan Lemon 			if (strcmp(p, devname) == 0)
344ffb5a104SJonathan Lemon 				goto found;
345ffb5a104SJonathan Lemon 	}
346ffb5a104SJonathan Lemon 	unit = 0;
347ffb5a104SJonathan Lemon found:
348ffb5a104SJonathan Lemon 	if (unit != 0) {
349ffb5a104SJonathan Lemon 		if (ifaddr_byindex(unit) == NULL)
350ffb5a104SJonathan Lemon 			return (unit);
351ffb5a104SJonathan Lemon 		printf("%s%d in use, cannot hardwire it to %s.\n",
352ffb5a104SJonathan Lemon 		    name, unit, devname);
353ffb5a104SJonathan Lemon 	}
354ffb5a104SJonathan Lemon 	for (unit = 1; ; unit++) {
35505153c61SBill Fenner 		if (unit <= if_index && ifaddr_byindex(unit) != NULL)
356ffb5a104SJonathan Lemon 			continue;
357ffb5a104SJonathan Lemon 		if (resource_string_value(name, unit, "ether", &p) == 0 ||
358ffb5a104SJonathan Lemon 		    resource_string_value(name, unit, "dev", &p) == 0)
359ffb5a104SJonathan Lemon 			continue;
360ffb5a104SJonathan Lemon 		break;
361ffb5a104SJonathan Lemon 	}
362ffb5a104SJonathan Lemon 	return (unit);
363ffb5a104SJonathan Lemon }
364ffb5a104SJonathan Lemon 
365df8bae1dSRodney W. Grimes /*
366df8bae1dSRodney W. Grimes  * Attach an interface to the
367df8bae1dSRodney W. Grimes  * list of "active" interfaces.
368df8bae1dSRodney W. Grimes  */
369df8bae1dSRodney W. Grimes void
370df8bae1dSRodney W. Grimes if_attach(ifp)
371df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
372df8bae1dSRodney W. Grimes {
373df8bae1dSRodney W. Grimes 	unsigned socksize, ifasize;
3741ce9bf88SPoul-Henning Kamp 	int namelen, masklen;
3751ce9bf88SPoul-Henning Kamp 	char workbuf[64];
376df8bae1dSRodney W. Grimes 	register struct sockaddr_dl *sdl;
377df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
378df8bae1dSRodney W. Grimes 
37929412182SGarrett Wollman 	TAILQ_INSERT_TAIL(&ifnet, ifp, if_link);
38059562606SGarrett Wollman 	/*
38159562606SGarrett Wollman 	 * XXX -
38259562606SGarrett Wollman 	 * The old code would work if the interface passed a pre-existing
38359562606SGarrett Wollman 	 * chain of ifaddrs to this code.  We don't trust our callers to
38459562606SGarrett Wollman 	 * properly initialize the tailq, however, so we no longer allow
38559562606SGarrett Wollman 	 * this unlikely case.
38659562606SGarrett Wollman 	 */
38759562606SGarrett Wollman 	TAILQ_INIT(&ifp->if_addrhead);
38882cd038dSYoshinobu Inoue 	TAILQ_INIT(&ifp->if_prefixhead);
3896817526dSPoul-Henning Kamp 	TAILQ_INIT(&ifp->if_multiaddrs);
3909a2a57a1SJonathan Lemon 	SLIST_INIT(&ifp->if_klist);
39198b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
392e70cd263SRobert Watson 
393e70cd263SRobert Watson #ifdef MAC
394e70cd263SRobert Watson 	mac_init_ifnet(ifp);
395e70cd263SRobert Watson 	mac_create_ifnet(ifp);
396e70cd263SRobert Watson #endif
397e70cd263SRobert Watson 
398ffb5a104SJonathan Lemon 	ifp->if_index = if_findindex(ifp);
39905153c61SBill Fenner 	if (ifp->if_index > if_index)
40005153c61SBill Fenner 		if_index = ifp->if_index;
401f9132cebSJonathan Lemon 	if (if_index >= if_indexlim)
402f9132cebSJonathan Lemon 		if_grow();
40382cd038dSYoshinobu Inoue 
404ffb5a104SJonathan Lemon 	ifnet_byindex(ifp->if_index) = ifp;
405ffb5a104SJonathan Lemon 	ifdev_byindex(ifp->if_index) = make_dev(&net_cdevsw, ifp->if_index,
406ffb5a104SJonathan Lemon 	    UID_ROOT, GID_WHEEL, 0600, "%s/%s%d",
407ffb5a104SJonathan Lemon 	    net_cdevsw.d_name, ifp->if_name, ifp->if_unit);
408ffb5a104SJonathan Lemon 	make_dev_alias(ifdev_byindex(ifp->if_index), "%s%d",
409ffb5a104SJonathan Lemon 	    net_cdevsw.d_name, ifp->if_index);
410f13ad206SJonathan Lemon 
4116008862bSJohn Baldwin 	mtx_init(&ifp->if_snd.ifq_mtx, ifp->if_name, "if send queue", MTX_DEF);
412df5e1987SJonathan Lemon 
413df8bae1dSRodney W. Grimes 	/*
414df8bae1dSRodney W. Grimes 	 * create a Link Level name for this device
415df8bae1dSRodney W. Grimes 	 */
4162127f260SArchie Cobbs 	namelen = snprintf(workbuf, sizeof(workbuf),
4172127f260SArchie Cobbs 	    "%s%d", ifp->if_name, ifp->if_unit);
418df8bae1dSRodney W. Grimes #define _offsetof(t, m) ((int)((caddr_t)&((t *)0)->m))
4191ce9bf88SPoul-Henning Kamp 	masklen = _offsetof(struct sockaddr_dl, sdl_data[0]) + namelen;
420df8bae1dSRodney W. Grimes 	socksize = masklen + ifp->if_addrlen;
421df8bae1dSRodney W. Grimes #define ROUNDUP(a) (1 + (((a) - 1) | (sizeof(long) - 1)))
422df8bae1dSRodney W. Grimes 	if (socksize < sizeof(*sdl))
423df8bae1dSRodney W. Grimes 		socksize = sizeof(*sdl);
4248a261b8fSDoug Rabson 	socksize = ROUNDUP(socksize);
425df8bae1dSRodney W. Grimes 	ifasize = sizeof(*ifa) + 2 * socksize;
4267cc0979fSDavid Malone 	ifa = (struct ifaddr *)malloc(ifasize, M_IFADDR, M_WAITOK | M_ZERO);
4279448326fSPoul-Henning Kamp 	if (ifa) {
428df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(ifa + 1);
429df8bae1dSRodney W. Grimes 		sdl->sdl_len = socksize;
430df8bae1dSRodney W. Grimes 		sdl->sdl_family = AF_LINK;
4311ce9bf88SPoul-Henning Kamp 		bcopy(workbuf, sdl->sdl_data, namelen);
4321ce9bf88SPoul-Henning Kamp 		sdl->sdl_nlen = namelen;
433df8bae1dSRodney W. Grimes 		sdl->sdl_index = ifp->if_index;
434df8bae1dSRodney W. Grimes 		sdl->sdl_type = ifp->if_type;
435ffb5a104SJonathan Lemon 		ifaddr_byindex(ifp->if_index) = ifa;
436df8bae1dSRodney W. Grimes 		ifa->ifa_ifp = ifp;
437df8bae1dSRodney W. Grimes 		ifa->ifa_rtrequest = link_rtrequest;
438df8bae1dSRodney W. Grimes 		ifa->ifa_addr = (struct sockaddr *)sdl;
439df8bae1dSRodney W. Grimes 		sdl = (struct sockaddr_dl *)(socksize + (caddr_t)sdl);
440df8bae1dSRodney W. Grimes 		ifa->ifa_netmask = (struct sockaddr *)sdl;
441df8bae1dSRodney W. Grimes 		sdl->sdl_len = masklen;
442df8bae1dSRodney W. Grimes 		while (namelen != 0)
443df8bae1dSRodney W. Grimes 			sdl->sdl_data[--namelen] = 0xff;
44459562606SGarrett Wollman 		TAILQ_INSERT_HEAD(&ifp->if_addrhead, ifa, ifa_link);
445df8bae1dSRodney W. Grimes 	}
446322dcb8dSMax Khon 	ifp->if_broadcastaddr = 0; /* reliably crash if used uninitialized */
4477b6edd04SRuslan Ermilov 
4487b6edd04SRuslan Ermilov 	/* Announce the interface. */
4497b6edd04SRuslan Ermilov 	rt_ifannouncemsg(ifp, IFAN_ARRIVAL);
450df8bae1dSRodney W. Grimes }
4516182fdbdSPeter Wemm 
4526182fdbdSPeter Wemm /*
4536182fdbdSPeter Wemm  * Detach an interface, removing it from the
4546182fdbdSPeter Wemm  * list of "active" interfaces.
4556182fdbdSPeter Wemm  */
4566182fdbdSPeter Wemm void
4576182fdbdSPeter Wemm if_detach(ifp)
4586182fdbdSPeter Wemm 	struct ifnet *ifp;
4596182fdbdSPeter Wemm {
4606182fdbdSPeter Wemm 	struct ifaddr *ifa;
4615500d3beSWarner Losh 	struct radix_node_head	*rnh;
4625500d3beSWarner Losh 	int s;
4635500d3beSWarner Losh 	int i;
4646182fdbdSPeter Wemm 
4656182fdbdSPeter Wemm 	/*
4666182fdbdSPeter Wemm 	 * Remove routes and flush queues.
4676182fdbdSPeter Wemm 	 */
4685500d3beSWarner Losh 	s = splnet();
4696182fdbdSPeter Wemm 	if_down(ifp);
4706182fdbdSPeter Wemm 
4716182fdbdSPeter Wemm 	/*
472f9132cebSJonathan Lemon 	 * Remove address from ifindex_table[] and maybe decrement if_index.
4736182fdbdSPeter Wemm 	 * Clean up all addresses.
4746182fdbdSPeter Wemm 	 */
475f9132cebSJonathan Lemon 	ifaddr_byindex(ifp->if_index) = NULL;
4760346e973SBrian Feldman 	revoke_and_destroy_dev(ifdev_byindex(ifp->if_index));
477f13ad206SJonathan Lemon 	ifdev_byindex(ifp->if_index) = NULL;
478f9132cebSJonathan Lemon 
479f9132cebSJonathan Lemon 	while (if_index > 0 && ifaddr_byindex(if_index) == NULL)
4806182fdbdSPeter Wemm 		if_index--;
4816182fdbdSPeter Wemm 
4826182fdbdSPeter Wemm 	for (ifa = TAILQ_FIRST(&ifp->if_addrhead); ifa;
4836182fdbdSPeter Wemm 	     ifa = TAILQ_FIRST(&ifp->if_addrhead)) {
4840d0f9d1eSYoshinobu Inoue #ifdef INET
485aa6be122SWarner Losh 		/* XXX: Ugly!! ad hoc just for INET */
486aa6be122SWarner Losh 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) {
487aa6be122SWarner Losh 			struct ifaliasreq ifr;
488aa6be122SWarner Losh 
489aa6be122SWarner Losh 			bzero(&ifr, sizeof(ifr));
490aa6be122SWarner Losh 			ifr.ifra_addr = *ifa->ifa_addr;
491aa6be122SWarner Losh 			if (ifa->ifa_dstaddr)
492aa6be122SWarner Losh 				ifr.ifra_broadaddr = *ifa->ifa_dstaddr;
493aa6be122SWarner Losh 			if (in_control(NULL, SIOCDIFADDR, (caddr_t)&ifr, ifp,
494aa6be122SWarner Losh 			    NULL) == 0)
495aa6be122SWarner Losh 				continue;
496aa6be122SWarner Losh 		}
4970d0f9d1eSYoshinobu Inoue #endif /* INET */
4980d0f9d1eSYoshinobu Inoue #ifdef INET6
4990d0f9d1eSYoshinobu Inoue 		if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET6) {
50033841545SHajimu UMEMOTO 			in6_purgeaddr(ifa);
501978ee2edSJun-ichiro itojun Hagino 			/* ifp_addrhead is already updated */
5020d0f9d1eSYoshinobu Inoue 			continue;
5030d0f9d1eSYoshinobu Inoue 		}
5040d0f9d1eSYoshinobu Inoue #endif /* INET6 */
5056182fdbdSPeter Wemm 		TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
5066182fdbdSPeter Wemm 		IFAFREE(ifa);
5076182fdbdSPeter Wemm 	}
5086182fdbdSPeter Wemm 
50933841545SHajimu UMEMOTO #ifdef INET6
51033841545SHajimu UMEMOTO 	/*
51133841545SHajimu UMEMOTO 	 * Remove all IPv6 kernel structs related to ifp.  This should be done
51233841545SHajimu UMEMOTO 	 * before removing routing entries below, since IPv6 interface direct
51333841545SHajimu UMEMOTO 	 * routes are expected to be removed by the IPv6-specific kernel API.
51433841545SHajimu UMEMOTO 	 * Otherwise, the kernel will detect some inconsistency and bark it.
51533841545SHajimu UMEMOTO 	 */
51633841545SHajimu UMEMOTO 	in6_ifdetach(ifp);
51733841545SHajimu UMEMOTO #endif
51833841545SHajimu UMEMOTO 
5195500d3beSWarner Losh 	/*
5205500d3beSWarner Losh 	 * Delete all remaining routes using this interface
5215500d3beSWarner Losh 	 * Unfortuneatly the only way to do this is to slog through
5225500d3beSWarner Losh 	 * the entire routing table looking for routes which point
5235500d3beSWarner Losh 	 * to this interface...oh well...
5245500d3beSWarner Losh 	 */
5255500d3beSWarner Losh 	for (i = 1; i <= AF_MAX; i++) {
5265500d3beSWarner Losh 		if ((rnh = rt_tables[i]) == NULL)
5275500d3beSWarner Losh 			continue;
5285500d3beSWarner Losh 		(void) rnh->rnh_walktree(rnh, if_rtdel, ifp);
5295500d3beSWarner Losh 	}
5305500d3beSWarner Losh 
5317b6edd04SRuslan Ermilov 	/* Announce that the interface is gone. */
5327b6edd04SRuslan Ermilov 	rt_ifannouncemsg(ifp, IFAN_DEPARTURE);
5337b6edd04SRuslan Ermilov 
534e70cd263SRobert Watson #ifdef MAC
535e70cd263SRobert Watson 	mac_destroy_ifnet(ifp);
536e70cd263SRobert Watson #endif /* MAC */
5379a2a57a1SJonathan Lemon 	KNOTE(&ifp->if_klist, NOTE_EXIT);
5386182fdbdSPeter Wemm 	TAILQ_REMOVE(&ifnet, ifp, if_link);
539df5e1987SJonathan Lemon 	mtx_destroy(&ifp->if_snd.ifq_mtx);
5405500d3beSWarner Losh 	splx(s);
5415500d3beSWarner Losh }
5425500d3beSWarner Losh 
5435500d3beSWarner Losh /*
5445500d3beSWarner Losh  * Delete Routes for a Network Interface
5455500d3beSWarner Losh  *
5465500d3beSWarner Losh  * Called for each routing entry via the rnh->rnh_walktree() call above
5475500d3beSWarner Losh  * to delete all route entries referencing a detaching network interface.
5485500d3beSWarner Losh  *
5495500d3beSWarner Losh  * Arguments:
5505500d3beSWarner Losh  *	rn	pointer to node in the routing table
5515500d3beSWarner Losh  *	arg	argument passed to rnh->rnh_walktree() - detaching interface
5525500d3beSWarner Losh  *
5535500d3beSWarner Losh  * Returns:
5545500d3beSWarner Losh  *	0	successful
5555500d3beSWarner Losh  *	errno	failed - reason indicated
5565500d3beSWarner Losh  *
5575500d3beSWarner Losh  */
5585500d3beSWarner Losh static int
5595500d3beSWarner Losh if_rtdel(rn, arg)
5605500d3beSWarner Losh 	struct radix_node	*rn;
5615500d3beSWarner Losh 	void			*arg;
5625500d3beSWarner Losh {
5635500d3beSWarner Losh 	struct rtentry	*rt = (struct rtentry *)rn;
5645500d3beSWarner Losh 	struct ifnet	*ifp = arg;
5655500d3beSWarner Losh 	int		err;
5665500d3beSWarner Losh 
5675500d3beSWarner Losh 	if (rt->rt_ifp == ifp) {
5685500d3beSWarner Losh 
5695500d3beSWarner Losh 		/*
5705500d3beSWarner Losh 		 * Protect (sorta) against walktree recursion problems
5715500d3beSWarner Losh 		 * with cloned routes
5725500d3beSWarner Losh 		 */
5735500d3beSWarner Losh 		if ((rt->rt_flags & RTF_UP) == 0)
5745500d3beSWarner Losh 			return (0);
5755500d3beSWarner Losh 
5765500d3beSWarner Losh 		err = rtrequest(RTM_DELETE, rt_key(rt), rt->rt_gateway,
5775500d3beSWarner Losh 				rt_mask(rt), rt->rt_flags,
5785500d3beSWarner Losh 				(struct rtentry **) NULL);
5795500d3beSWarner Losh 		if (err) {
5805500d3beSWarner Losh 			log(LOG_WARNING, "if_rtdel: error %d\n", err);
5815500d3beSWarner Losh 		}
5825500d3beSWarner Losh 	}
5835500d3beSWarner Losh 
5845500d3beSWarner Losh 	return (0);
5856182fdbdSPeter Wemm }
5866182fdbdSPeter Wemm 
587df8bae1dSRodney W. Grimes /*
58830aad87dSBrooks Davis  * Create a clone network interface.
58930aad87dSBrooks Davis  */
59030aad87dSBrooks Davis int
59130aad87dSBrooks Davis if_clone_create(name, len)
59230aad87dSBrooks Davis 	char *name;
59330aad87dSBrooks Davis 	int len;
59430aad87dSBrooks Davis {
59530aad87dSBrooks Davis 	struct if_clone *ifc;
59630aad87dSBrooks Davis 	char *dp;
5973b16e7b2SMaxime Henrion 	int wildcard, bytoff, bitoff;
59830aad87dSBrooks Davis 	int unit;
59930aad87dSBrooks Davis 	int err;
60030aad87dSBrooks Davis 
60130aad87dSBrooks Davis 	ifc = if_clone_lookup(name, &unit);
60230aad87dSBrooks Davis 	if (ifc == NULL)
60330aad87dSBrooks Davis 		return (EINVAL);
60430aad87dSBrooks Davis 
60530aad87dSBrooks Davis 	if (ifunit(name) != NULL)
60630aad87dSBrooks Davis 		return (EEXIST);
60730aad87dSBrooks Davis 
6083b16e7b2SMaxime Henrion 	bytoff = bitoff = 0;
60930aad87dSBrooks Davis 	wildcard = (unit < 0);
6103b16e7b2SMaxime Henrion 	/*
6113b16e7b2SMaxime Henrion 	 * Find a free unit if none was given.
6123b16e7b2SMaxime Henrion 	 */
6133b16e7b2SMaxime Henrion 	if (wildcard) {
6143b16e7b2SMaxime Henrion 		while ((bytoff < ifc->ifc_bmlen)
6153b16e7b2SMaxime Henrion 		    && (ifc->ifc_units[bytoff] == 0xff))
6163b16e7b2SMaxime Henrion 			bytoff++;
6173b16e7b2SMaxime Henrion 		if (bytoff >= ifc->ifc_bmlen)
6183b16e7b2SMaxime Henrion 			return (ENOSPC);
6193b16e7b2SMaxime Henrion 		while ((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0)
6203b16e7b2SMaxime Henrion 			bitoff++;
6213b16e7b2SMaxime Henrion 		unit = (bytoff << 3) + bitoff;
6223b16e7b2SMaxime Henrion 	}
62330aad87dSBrooks Davis 
6243b16e7b2SMaxime Henrion 	if (unit > ifc->ifc_maxunit)
6253b16e7b2SMaxime Henrion 		return (ENXIO);
6263b16e7b2SMaxime Henrion 
6273b16e7b2SMaxime Henrion 	err = (*ifc->ifc_create)(ifc, unit);
62830aad87dSBrooks Davis 	if (err != 0)
62930aad87dSBrooks Davis 		return (err);
63030aad87dSBrooks Davis 
6313b16e7b2SMaxime Henrion 	if (!wildcard) {
6323b16e7b2SMaxime Henrion 		bytoff = unit >> 3;
6333b16e7b2SMaxime Henrion 		bitoff = unit - (bytoff << 3);
6343b16e7b2SMaxime Henrion 	}
6353b16e7b2SMaxime Henrion 
6363b16e7b2SMaxime Henrion 	/*
6373b16e7b2SMaxime Henrion 	 * Allocate the unit in the bitmap.
6383b16e7b2SMaxime Henrion 	 */
6393b16e7b2SMaxime Henrion 	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) == 0,
6403b16e7b2SMaxime Henrion 	    ("%s: bit is already set", __func__));
6413b16e7b2SMaxime Henrion 	ifc->ifc_units[bytoff] |= (1 << bitoff);
6423b16e7b2SMaxime Henrion 
64330aad87dSBrooks Davis 	/* In the wildcard case, we need to update the name. */
64430aad87dSBrooks Davis 	if (wildcard) {
64530aad87dSBrooks Davis 		for (dp = name; *dp != '\0'; dp++);
64630aad87dSBrooks Davis 		if (snprintf(dp, len - (dp-name), "%d", unit) >
64730aad87dSBrooks Davis 		    len - (dp-name) - 1) {
64830aad87dSBrooks Davis 			/*
64930aad87dSBrooks Davis 			 * This can only be a programmer error and
65030aad87dSBrooks Davis 			 * there's no straightforward way to recover if
65130aad87dSBrooks Davis 			 * it happens.
65230aad87dSBrooks Davis 			 */
65330aad87dSBrooks Davis 			panic("if_clone_create(): interface name too long");
65430aad87dSBrooks Davis 		}
65530aad87dSBrooks Davis 
65630aad87dSBrooks Davis 	}
65730aad87dSBrooks Davis 
65830aad87dSBrooks Davis 	return (0);
65930aad87dSBrooks Davis }
66030aad87dSBrooks Davis 
66130aad87dSBrooks Davis /*
66230aad87dSBrooks Davis  * Destroy a clone network interface.
66330aad87dSBrooks Davis  */
66430aad87dSBrooks Davis int
66530aad87dSBrooks Davis if_clone_destroy(name)
66630aad87dSBrooks Davis 	const char *name;
66730aad87dSBrooks Davis {
66830aad87dSBrooks Davis 	struct if_clone *ifc;
66930aad87dSBrooks Davis 	struct ifnet *ifp;
6703b16e7b2SMaxime Henrion 	int bytoff, bitoff;
671ae5a19beSBrooks Davis 	int unit;
67230aad87dSBrooks Davis 
6733b16e7b2SMaxime Henrion 	ifc = if_clone_lookup(name, &unit);
67430aad87dSBrooks Davis 	if (ifc == NULL)
67530aad87dSBrooks Davis 		return (EINVAL);
67630aad87dSBrooks Davis 
677ae5a19beSBrooks Davis 	if (unit < ifc->ifc_minifs)
678ae5a19beSBrooks Davis 		return (EINVAL);
679ae5a19beSBrooks Davis 
68030aad87dSBrooks Davis 	ifp = ifunit(name);
68130aad87dSBrooks Davis 	if (ifp == NULL)
68230aad87dSBrooks Davis 		return (ENXIO);
68330aad87dSBrooks Davis 
68430aad87dSBrooks Davis 	if (ifc->ifc_destroy == NULL)
68530aad87dSBrooks Davis 		return (EOPNOTSUPP);
68630aad87dSBrooks Davis 
687ae5a19beSBrooks Davis 	(*ifc->ifc_destroy)(ifp);
6883b16e7b2SMaxime Henrion 
6893b16e7b2SMaxime Henrion 	/*
6903b16e7b2SMaxime Henrion 	 * Compute offset in the bitmap and deallocate the unit.
6913b16e7b2SMaxime Henrion 	 */
6923b16e7b2SMaxime Henrion 	bytoff = unit >> 3;
6933b16e7b2SMaxime Henrion 	bitoff = unit - (bytoff << 3);
6943b16e7b2SMaxime Henrion 	KASSERT((ifc->ifc_units[bytoff] & (1 << bitoff)) != 0,
6953b16e7b2SMaxime Henrion 	    ("%s: bit is already cleared", __func__));
6963b16e7b2SMaxime Henrion 	ifc->ifc_units[bytoff] &= ~(1 << bitoff);
6973b16e7b2SMaxime Henrion 	return (0);
69830aad87dSBrooks Davis }
69930aad87dSBrooks Davis 
70030aad87dSBrooks Davis /*
70130aad87dSBrooks Davis  * Look up a network interface cloner.
70230aad87dSBrooks Davis  */
7030b59d917SJonathan Lemon static struct if_clone *
70430aad87dSBrooks Davis if_clone_lookup(name, unitp)
70530aad87dSBrooks Davis 	const char *name;
70630aad87dSBrooks Davis 	int *unitp;
70730aad87dSBrooks Davis {
70830aad87dSBrooks Davis 	struct if_clone *ifc;
70930aad87dSBrooks Davis 	const char *cp;
71030aad87dSBrooks Davis 	int i;
71130aad87dSBrooks Davis 
71230aad87dSBrooks Davis 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL;) {
71330aad87dSBrooks Davis 		for (cp = name, i = 0; i < ifc->ifc_namelen; i++, cp++) {
71430aad87dSBrooks Davis 			if (ifc->ifc_name[i] != *cp)
71530aad87dSBrooks Davis 				goto next_ifc;
71630aad87dSBrooks Davis 		}
71730aad87dSBrooks Davis 		goto found_name;
71830aad87dSBrooks Davis  next_ifc:
71930aad87dSBrooks Davis 		ifc = LIST_NEXT(ifc, ifc_list);
72030aad87dSBrooks Davis 	}
72130aad87dSBrooks Davis 
72230aad87dSBrooks Davis 	/* No match. */
72330aad87dSBrooks Davis 	return ((struct if_clone *)NULL);
72430aad87dSBrooks Davis 
72530aad87dSBrooks Davis  found_name:
72630aad87dSBrooks Davis 	if (*cp == '\0') {
72730aad87dSBrooks Davis 		i = -1;
72830aad87dSBrooks Davis 	} else {
72930aad87dSBrooks Davis 		for (i = 0; *cp != '\0'; cp++) {
73030aad87dSBrooks Davis 			if (*cp < '0' || *cp > '9') {
73130aad87dSBrooks Davis 				/* Bogus unit number. */
73230aad87dSBrooks Davis 				return (NULL);
73330aad87dSBrooks Davis 			}
73430aad87dSBrooks Davis 			i = (i * 10) + (*cp - '0');
73530aad87dSBrooks Davis 		}
73630aad87dSBrooks Davis 	}
73730aad87dSBrooks Davis 
73830aad87dSBrooks Davis 	if (unitp != NULL)
73930aad87dSBrooks Davis 		*unitp = i;
74030aad87dSBrooks Davis 	return (ifc);
74130aad87dSBrooks Davis }
74230aad87dSBrooks Davis 
74330aad87dSBrooks Davis /*
74430aad87dSBrooks Davis  * Register a network interface cloner.
74530aad87dSBrooks Davis  */
74630aad87dSBrooks Davis void
74730aad87dSBrooks Davis if_clone_attach(ifc)
74830aad87dSBrooks Davis 	struct if_clone *ifc;
74930aad87dSBrooks Davis {
750ae5a19beSBrooks Davis 	int bytoff, bitoff;
751ae5a19beSBrooks Davis 	int err;
7523b16e7b2SMaxime Henrion 	int len, maxclone;
753ae5a19beSBrooks Davis 	int unit;
75430aad87dSBrooks Davis 
755ae5a19beSBrooks Davis 	KASSERT(ifc->ifc_minifs - 1 <= ifc->ifc_maxunit,
756ae5a19beSBrooks Davis 	    ("%s: %s requested more units then allowed (%d > %d)",
757ae5a19beSBrooks Davis 	    __func__, ifc->ifc_name, ifc->ifc_minifs,
758ae5a19beSBrooks Davis 	    ifc->ifc_maxunit + 1));
7593b16e7b2SMaxime Henrion 	/*
7603b16e7b2SMaxime Henrion 	 * Compute bitmap size and allocate it.
7613b16e7b2SMaxime Henrion 	 */
7623b16e7b2SMaxime Henrion 	maxclone = ifc->ifc_maxunit + 1;
7633b16e7b2SMaxime Henrion 	len = maxclone >> 3;
7643b16e7b2SMaxime Henrion 	if ((len << 3) < maxclone)
7653b16e7b2SMaxime Henrion 		len++;
7663b16e7b2SMaxime Henrion 	ifc->ifc_units = malloc(len, M_CLONE, M_WAITOK | M_ZERO);
7673b16e7b2SMaxime Henrion 	ifc->ifc_bmlen = len;
768ae5a19beSBrooks Davis 
76930aad87dSBrooks Davis 	LIST_INSERT_HEAD(&if_cloners, ifc, ifc_list);
77030aad87dSBrooks Davis 	if_cloners_count++;
771ae5a19beSBrooks Davis 
772ae5a19beSBrooks Davis 	for (unit = 0; unit < ifc->ifc_minifs; unit++) {
773ae5a19beSBrooks Davis 		err = (*ifc->ifc_create)(ifc, unit);
774ae5a19beSBrooks Davis 		KASSERT(err == 0,
775ae5a19beSBrooks Davis 		    ("%s: failed to create required interface %s%d",
776ae5a19beSBrooks Davis 		    __func__, ifc->ifc_name, unit));
777ae5a19beSBrooks Davis 
778ae5a19beSBrooks Davis 		/* Allocate the unit in the bitmap. */
779ae5a19beSBrooks Davis 		bytoff = unit >> 3;
780ae5a19beSBrooks Davis 		bitoff = unit - (bytoff << 3);
781ae5a19beSBrooks Davis 		ifc->ifc_units[bytoff] |= (1 << bitoff);
782ae5a19beSBrooks Davis 	}
78330aad87dSBrooks Davis }
78430aad87dSBrooks Davis 
78530aad87dSBrooks Davis /*
78630aad87dSBrooks Davis  * Unregister a network interface cloner.
78730aad87dSBrooks Davis  */
78830aad87dSBrooks Davis void
78930aad87dSBrooks Davis if_clone_detach(ifc)
79030aad87dSBrooks Davis 	struct if_clone *ifc;
79130aad87dSBrooks Davis {
79230aad87dSBrooks Davis 
79330aad87dSBrooks Davis 	LIST_REMOVE(ifc, ifc_list);
7943b16e7b2SMaxime Henrion 	free(ifc->ifc_units, M_CLONE);
79530aad87dSBrooks Davis 	if_cloners_count--;
79630aad87dSBrooks Davis }
79730aad87dSBrooks Davis 
79830aad87dSBrooks Davis /*
79930aad87dSBrooks Davis  * Provide list of interface cloners to userspace.
80030aad87dSBrooks Davis  */
8010b59d917SJonathan Lemon static int
80230aad87dSBrooks Davis if_clone_list(ifcr)
80330aad87dSBrooks Davis 	struct if_clonereq *ifcr;
80430aad87dSBrooks Davis {
80530aad87dSBrooks Davis 	char outbuf[IFNAMSIZ], *dst;
80630aad87dSBrooks Davis 	struct if_clone *ifc;
80730aad87dSBrooks Davis 	int count, error = 0;
80830aad87dSBrooks Davis 
80930aad87dSBrooks Davis 	ifcr->ifcr_total = if_cloners_count;
81030aad87dSBrooks Davis 	if ((dst = ifcr->ifcr_buffer) == NULL) {
81130aad87dSBrooks Davis 		/* Just asking how many there are. */
81230aad87dSBrooks Davis 		return (0);
81330aad87dSBrooks Davis 	}
81430aad87dSBrooks Davis 
81530aad87dSBrooks Davis 	if (ifcr->ifcr_count < 0)
81630aad87dSBrooks Davis 		return (EINVAL);
81730aad87dSBrooks Davis 
81830aad87dSBrooks Davis 	count = (if_cloners_count < ifcr->ifcr_count) ?
81930aad87dSBrooks Davis 	    if_cloners_count : ifcr->ifcr_count;
82030aad87dSBrooks Davis 
82130aad87dSBrooks Davis 	for (ifc = LIST_FIRST(&if_cloners); ifc != NULL && count != 0;
82230aad87dSBrooks Davis 	     ifc = LIST_NEXT(ifc, ifc_list), count--, dst += IFNAMSIZ) {
82330aad87dSBrooks Davis 		strncpy(outbuf, ifc->ifc_name, IFNAMSIZ);
82430aad87dSBrooks Davis 		outbuf[IFNAMSIZ - 1] = '\0';	/* sanity */
82530aad87dSBrooks Davis 		error = copyout(outbuf, dst, IFNAMSIZ);
82630aad87dSBrooks Davis 		if (error)
82730aad87dSBrooks Davis 			break;
82830aad87dSBrooks Davis 	}
82930aad87dSBrooks Davis 
83030aad87dSBrooks Davis 	return (error);
83130aad87dSBrooks Davis }
83230aad87dSBrooks Davis 
83330aad87dSBrooks Davis /*
834df8bae1dSRodney W. Grimes  * Locate an interface based on a complete address.
835df8bae1dSRodney W. Grimes  */
836df8bae1dSRodney W. Grimes /*ARGSUSED*/
837df8bae1dSRodney W. Grimes struct ifaddr *
838df8bae1dSRodney W. Grimes ifa_ifwithaddr(addr)
8390b59d917SJonathan Lemon 	struct sockaddr *addr;
840df8bae1dSRodney W. Grimes {
8410b59d917SJonathan Lemon 	struct ifnet *ifp;
8420b59d917SJonathan Lemon 	struct ifaddr *ifa;
843df8bae1dSRodney W. Grimes 
844df8bae1dSRodney W. Grimes #define	equal(a1, a2) \
845df8bae1dSRodney W. Grimes   (bcmp((caddr_t)(a1), (caddr_t)(a2), ((struct sockaddr *)(a1))->sa_len) == 0)
846fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(ifp, &ifnet, if_link)
84737d40066SPoul-Henning Kamp 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
848df8bae1dSRodney W. Grimes 			if (ifa->ifa_addr->sa_family != addr->sa_family)
849df8bae1dSRodney W. Grimes 				continue;
850df8bae1dSRodney W. Grimes 			if (equal(addr, ifa->ifa_addr))
8510b59d917SJonathan Lemon 				goto done;
85282cd038dSYoshinobu Inoue 			/* IP6 doesn't have broadcast */
8530b59d917SJonathan Lemon 			if ((ifp->if_flags & IFF_BROADCAST) &&
8540b59d917SJonathan Lemon 			    ifa->ifa_broadaddr &&
85582cd038dSYoshinobu Inoue 			    ifa->ifa_broadaddr->sa_len != 0 &&
856df8bae1dSRodney W. Grimes 			    equal(ifa->ifa_broadaddr, addr))
8570b59d917SJonathan Lemon 				goto done;
8580b59d917SJonathan Lemon 		}
8590b59d917SJonathan Lemon 	ifa = NULL;
8600b59d917SJonathan Lemon done:
861df8bae1dSRodney W. Grimes 	return (ifa);
862df8bae1dSRodney W. Grimes }
8630b59d917SJonathan Lemon 
864df8bae1dSRodney W. Grimes /*
865df8bae1dSRodney W. Grimes  * Locate the point to point interface with a given destination address.
866df8bae1dSRodney W. Grimes  */
867df8bae1dSRodney W. Grimes /*ARGSUSED*/
868df8bae1dSRodney W. Grimes struct ifaddr *
869df8bae1dSRodney W. Grimes ifa_ifwithdstaddr(addr)
8700b59d917SJonathan Lemon 	struct sockaddr *addr;
871df8bae1dSRodney W. Grimes {
8720b59d917SJonathan Lemon 	struct ifnet *ifp;
8730b59d917SJonathan Lemon 	struct ifaddr *ifa;
874df8bae1dSRodney W. Grimes 
8750b59d917SJonathan Lemon 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
8760b59d917SJonathan Lemon 		if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
8770b59d917SJonathan Lemon 			continue;
87837d40066SPoul-Henning Kamp 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
879df8bae1dSRodney W. Grimes 			if (ifa->ifa_addr->sa_family != addr->sa_family)
880df8bae1dSRodney W. Grimes 				continue;
88155088a1cSDavid Greenman 			if (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr))
8820b59d917SJonathan Lemon 				goto done;
883df8bae1dSRodney W. Grimes 		}
8840b59d917SJonathan Lemon 	}
8850b59d917SJonathan Lemon 	ifa = NULL;
8860b59d917SJonathan Lemon done:
8870b59d917SJonathan Lemon 	return (ifa);
888df8bae1dSRodney W. Grimes }
889df8bae1dSRodney W. Grimes 
890df8bae1dSRodney W. Grimes /*
891df8bae1dSRodney W. Grimes  * Find an interface on a specific network.  If many, choice
892df8bae1dSRodney W. Grimes  * is most specific found.
893df8bae1dSRodney W. Grimes  */
894df8bae1dSRodney W. Grimes struct ifaddr *
895df8bae1dSRodney W. Grimes ifa_ifwithnet(addr)
896df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
897df8bae1dSRodney W. Grimes {
898df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
899df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
900df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = (struct ifaddr *) 0;
901df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
902df8bae1dSRodney W. Grimes 	char *addr_data = addr->sa_data, *cplim;
903df8bae1dSRodney W. Grimes 
9047e2a6151SJulian Elischer 	/*
9057e2a6151SJulian Elischer 	 * AF_LINK addresses can be looked up directly by their index number,
9067e2a6151SJulian Elischer 	 * so do that if we can.
9077e2a6151SJulian Elischer 	 */
908df8bae1dSRodney W. Grimes 	if (af == AF_LINK) {
909df8bae1dSRodney W. Grimes 	    register struct sockaddr_dl *sdl = (struct sockaddr_dl *)addr;
910df8bae1dSRodney W. Grimes 	    if (sdl->sdl_index && sdl->sdl_index <= if_index)
911f9132cebSJonathan Lemon 		return (ifaddr_byindex(sdl->sdl_index));
912df8bae1dSRodney W. Grimes 	}
9137e2a6151SJulian Elischer 
9147e2a6151SJulian Elischer 	/*
9157e2a6151SJulian Elischer 	 * Scan though each interface, looking for ones that have
9167e2a6151SJulian Elischer 	 * addresses in this address family.
9177e2a6151SJulian Elischer 	 */
918fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
91937d40066SPoul-Henning Kamp 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
920df8bae1dSRodney W. Grimes 			register char *cp, *cp2, *cp3;
921df8bae1dSRodney W. Grimes 
922523a02aaSDavid Greenman 			if (ifa->ifa_addr->sa_family != af)
923df8bae1dSRodney W. Grimes next:				continue;
924c61cd599SHajimu UMEMOTO 			if (af == AF_INET && ifp->if_flags & IFF_POINTOPOINT) {
9257e2a6151SJulian Elischer 				/*
9267e2a6151SJulian Elischer 				 * This is a bit broken as it doesn't
9277e2a6151SJulian Elischer 				 * take into account that the remote end may
9287e2a6151SJulian Elischer 				 * be a single node in the network we are
9297e2a6151SJulian Elischer 				 * looking for.
9307e2a6151SJulian Elischer 				 * The trouble is that we don't know the
9317e2a6151SJulian Elischer 				 * netmask for the remote end.
9327e2a6151SJulian Elischer 				 */
933fcd6781aSGarrett Wollman 				if (ifa->ifa_dstaddr != 0
934fcd6781aSGarrett Wollman 				    && equal(addr, ifa->ifa_dstaddr))
9350b59d917SJonathan Lemon 					goto done;
9363740e2adSDavid Greenman 			} else {
9377e2a6151SJulian Elischer 				/*
9387ed8f465SJulian Elischer 				 * if we have a special address handler,
9397ed8f465SJulian Elischer 				 * then use it instead of the generic one.
9407ed8f465SJulian Elischer 				 */
9417ed8f465SJulian Elischer 	          		if (ifa->ifa_claim_addr) {
9420b59d917SJonathan Lemon 					if ((*ifa->ifa_claim_addr)(ifa, addr))
9430b59d917SJonathan Lemon 						goto done;
9447ed8f465SJulian Elischer 					continue;
9457ed8f465SJulian Elischer 				}
9467ed8f465SJulian Elischer 
9477ed8f465SJulian Elischer 				/*
9487e2a6151SJulian Elischer 				 * Scan all the bits in the ifa's address.
9497e2a6151SJulian Elischer 				 * If a bit dissagrees with what we are
9507e2a6151SJulian Elischer 				 * looking for, mask it with the netmask
9517e2a6151SJulian Elischer 				 * to see if it really matters.
9527e2a6151SJulian Elischer 				 * (A byte at a time)
9537e2a6151SJulian Elischer 				 */
954523a02aaSDavid Greenman 				if (ifa->ifa_netmask == 0)
955523a02aaSDavid Greenman 					continue;
956df8bae1dSRodney W. Grimes 				cp = addr_data;
957df8bae1dSRodney W. Grimes 				cp2 = ifa->ifa_addr->sa_data;
958df8bae1dSRodney W. Grimes 				cp3 = ifa->ifa_netmask->sa_data;
9597e2a6151SJulian Elischer 				cplim = ifa->ifa_netmask->sa_len
9607e2a6151SJulian Elischer 					+ (char *)ifa->ifa_netmask;
961df8bae1dSRodney W. Grimes 				while (cp3 < cplim)
962df8bae1dSRodney W. Grimes 					if ((*cp++ ^ *cp2++) & *cp3++)
9637e2a6151SJulian Elischer 						goto next; /* next address! */
9647e2a6151SJulian Elischer 				/*
9657e2a6151SJulian Elischer 				 * If the netmask of what we just found
9667e2a6151SJulian Elischer 				 * is more specific than what we had before
9677e2a6151SJulian Elischer 				 * (if we had one) then remember the new one
9687e2a6151SJulian Elischer 				 * before continuing to search
9697e2a6151SJulian Elischer 				 * for an even better one.
9707e2a6151SJulian Elischer 				 */
971df8bae1dSRodney W. Grimes 				if (ifa_maybe == 0 ||
972df8bae1dSRodney W. Grimes 				    rn_refines((caddr_t)ifa->ifa_netmask,
973df8bae1dSRodney W. Grimes 				    (caddr_t)ifa_maybe->ifa_netmask))
974df8bae1dSRodney W. Grimes 					ifa_maybe = ifa;
975df8bae1dSRodney W. Grimes 			}
976b2af64fdSDavid Greenman 		}
977b2af64fdSDavid Greenman 	}
9780b59d917SJonathan Lemon 	ifa = ifa_maybe;
9790b59d917SJonathan Lemon done:
9800b59d917SJonathan Lemon 	return (ifa);
981df8bae1dSRodney W. Grimes }
982df8bae1dSRodney W. Grimes 
983df8bae1dSRodney W. Grimes /*
984df8bae1dSRodney W. Grimes  * Find an interface address specific to an interface best matching
985df8bae1dSRodney W. Grimes  * a given address.
986df8bae1dSRodney W. Grimes  */
987df8bae1dSRodney W. Grimes struct ifaddr *
988df8bae1dSRodney W. Grimes ifaof_ifpforaddr(addr, ifp)
989df8bae1dSRodney W. Grimes 	struct sockaddr *addr;
990df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
991df8bae1dSRodney W. Grimes {
992df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
993df8bae1dSRodney W. Grimes 	register char *cp, *cp2, *cp3;
994df8bae1dSRodney W. Grimes 	register char *cplim;
995df8bae1dSRodney W. Grimes 	struct ifaddr *ifa_maybe = 0;
996df8bae1dSRodney W. Grimes 	u_int af = addr->sa_family;
997df8bae1dSRodney W. Grimes 
998df8bae1dSRodney W. Grimes 	if (af >= AF_MAX)
999df8bae1dSRodney W. Grimes 		return (0);
100037d40066SPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1001df8bae1dSRodney W. Grimes 		if (ifa->ifa_addr->sa_family != af)
1002df8bae1dSRodney W. Grimes 			continue;
1003381dd1d2SJulian Elischer 		if (ifa_maybe == 0)
1004df8bae1dSRodney W. Grimes 			ifa_maybe = ifa;
1005df8bae1dSRodney W. Grimes 		if (ifa->ifa_netmask == 0) {
1006df8bae1dSRodney W. Grimes 			if (equal(addr, ifa->ifa_addr) ||
1007df8bae1dSRodney W. Grimes 			    (ifa->ifa_dstaddr && equal(addr, ifa->ifa_dstaddr)))
10082defe5cdSJonathan Lemon 				goto done;
1009df8bae1dSRodney W. Grimes 			continue;
1010df8bae1dSRodney W. Grimes 		}
1011b2af64fdSDavid Greenman 		if (ifp->if_flags & IFF_POINTOPOINT) {
1012b2af64fdSDavid Greenman 			if (equal(addr, ifa->ifa_dstaddr))
1013a8637146SJonathan Lemon 				goto done;
10143740e2adSDavid Greenman 		} else {
1015df8bae1dSRodney W. Grimes 			cp = addr->sa_data;
1016df8bae1dSRodney W. Grimes 			cp2 = ifa->ifa_addr->sa_data;
1017df8bae1dSRodney W. Grimes 			cp3 = ifa->ifa_netmask->sa_data;
1018df8bae1dSRodney W. Grimes 			cplim = ifa->ifa_netmask->sa_len + (char *)ifa->ifa_netmask;
1019df8bae1dSRodney W. Grimes 			for (; cp3 < cplim; cp3++)
1020df8bae1dSRodney W. Grimes 				if ((*cp++ ^ *cp2++) & *cp3)
1021df8bae1dSRodney W. Grimes 					break;
1022df8bae1dSRodney W. Grimes 			if (cp3 == cplim)
10232defe5cdSJonathan Lemon 				goto done;
1024df8bae1dSRodney W. Grimes 		}
1025b2af64fdSDavid Greenman 	}
1026f9132cebSJonathan Lemon 	ifa = ifa_maybe;
1027f9132cebSJonathan Lemon done:
1028f9132cebSJonathan Lemon 	return (ifa);
1029df8bae1dSRodney W. Grimes }
1030df8bae1dSRodney W. Grimes 
1031df8bae1dSRodney W. Grimes #include <net/route.h>
1032df8bae1dSRodney W. Grimes 
1033df8bae1dSRodney W. Grimes /*
1034df8bae1dSRodney W. Grimes  * Default action when installing a route with a Link Level gateway.
1035df8bae1dSRodney W. Grimes  * Lookup an appropriate real ifa to point to.
1036df8bae1dSRodney W. Grimes  * This should be moved to /sys/net/link.c eventually.
1037df8bae1dSRodney W. Grimes  */
10383bda9f9bSPoul-Henning Kamp static void
10398071913dSRuslan Ermilov link_rtrequest(cmd, rt, info)
1040df8bae1dSRodney W. Grimes 	int cmd;
1041df8bae1dSRodney W. Grimes 	register struct rtentry *rt;
10428071913dSRuslan Ermilov 	struct rt_addrinfo *info;
1043df8bae1dSRodney W. Grimes {
1044df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
1045df8bae1dSRodney W. Grimes 	struct sockaddr *dst;
1046df8bae1dSRodney W. Grimes 	struct ifnet *ifp;
1047df8bae1dSRodney W. Grimes 
1048df8bae1dSRodney W. Grimes 	if (cmd != RTM_ADD || ((ifa = rt->rt_ifa) == 0) ||
1049df8bae1dSRodney W. Grimes 	    ((ifp = ifa->ifa_ifp) == 0) || ((dst = rt_key(rt)) == 0))
1050df8bae1dSRodney W. Grimes 		return;
10519448326fSPoul-Henning Kamp 	ifa = ifaof_ifpforaddr(dst, ifp);
10529448326fSPoul-Henning Kamp 	if (ifa) {
1053df8bae1dSRodney W. Grimes 		IFAFREE(rt->rt_ifa);
1054df8bae1dSRodney W. Grimes 		rt->rt_ifa = ifa;
1055df8bae1dSRodney W. Grimes 		ifa->ifa_refcnt++;
1056df8bae1dSRodney W. Grimes 		if (ifa->ifa_rtrequest && ifa->ifa_rtrequest != link_rtrequest)
10578071913dSRuslan Ermilov 			ifa->ifa_rtrequest(cmd, rt, info);
1058df8bae1dSRodney W. Grimes 	}
1059df8bae1dSRodney W. Grimes }
1060df8bae1dSRodney W. Grimes 
1061df8bae1dSRodney W. Grimes /*
1062df8bae1dSRodney W. Grimes  * Mark an interface down and notify protocols of
1063df8bae1dSRodney W. Grimes  * the transition.
1064df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
1065df8bae1dSRodney W. Grimes  */
1066df8bae1dSRodney W. Grimes void
1067e8c2601dSPoul-Henning Kamp if_unroute(ifp, flag, fam)
1068df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
1069e8c2601dSPoul-Henning Kamp 	int flag, fam;
1070df8bae1dSRodney W. Grimes {
1071df8bae1dSRodney W. Grimes 	register struct ifaddr *ifa;
1072df8bae1dSRodney W. Grimes 
1073e8c2601dSPoul-Henning Kamp 	ifp->if_flags &= ~flag;
107498b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
1075e8c2601dSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1076e8c2601dSPoul-Henning Kamp 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1077df8bae1dSRodney W. Grimes 			pfctlinput(PRC_IFDOWN, ifa->ifa_addr);
1078df8bae1dSRodney W. Grimes 	if_qflush(&ifp->if_snd);
1079df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
1080df8bae1dSRodney W. Grimes }
1081df8bae1dSRodney W. Grimes 
1082df8bae1dSRodney W. Grimes /*
1083df8bae1dSRodney W. Grimes  * Mark an interface up and notify protocols of
1084df8bae1dSRodney W. Grimes  * the transition.
1085df8bae1dSRodney W. Grimes  * NOTE: must be called at splnet or eqivalent.
1086df8bae1dSRodney W. Grimes  */
1087df8bae1dSRodney W. Grimes void
1088e8c2601dSPoul-Henning Kamp if_route(ifp, flag, fam)
1089df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
1090e8c2601dSPoul-Henning Kamp 	int flag, fam;
1091df8bae1dSRodney W. Grimes {
1092176395b2SGarrett Wollman 	register struct ifaddr *ifa;
1093df8bae1dSRodney W. Grimes 
1094e8c2601dSPoul-Henning Kamp 	ifp->if_flags |= flag;
109598b9590eSPoul-Henning Kamp 	getmicrotime(&ifp->if_lastchange);
1096e8c2601dSPoul-Henning Kamp 	TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
1097e8c2601dSPoul-Henning Kamp 		if (fam == PF_UNSPEC || (fam == ifa->ifa_addr->sa_family))
1098df8bae1dSRodney W. Grimes 			pfctlinput(PRC_IFUP, ifa->ifa_addr);
1099df8bae1dSRodney W. Grimes 	rt_ifmsg(ifp);
110082cd038dSYoshinobu Inoue #ifdef INET6
110182cd038dSYoshinobu Inoue 	in6_if_up(ifp);
110282cd038dSYoshinobu Inoue #endif
1103df8bae1dSRodney W. Grimes }
1104df8bae1dSRodney W. Grimes 
1105df8bae1dSRodney W. Grimes /*
1106e8c2601dSPoul-Henning Kamp  * Mark an interface down and notify protocols of
1107e8c2601dSPoul-Henning Kamp  * the transition.
1108e8c2601dSPoul-Henning Kamp  * NOTE: must be called at splnet or eqivalent.
1109e8c2601dSPoul-Henning Kamp  */
1110e8c2601dSPoul-Henning Kamp void
1111e8c2601dSPoul-Henning Kamp if_down(ifp)
1112e8c2601dSPoul-Henning Kamp 	register struct ifnet *ifp;
1113e8c2601dSPoul-Henning Kamp {
1114e8c2601dSPoul-Henning Kamp 
1115e8c2601dSPoul-Henning Kamp 	if_unroute(ifp, IFF_UP, AF_UNSPEC);
1116e8c2601dSPoul-Henning Kamp }
1117e8c2601dSPoul-Henning Kamp 
1118e8c2601dSPoul-Henning Kamp /*
1119e8c2601dSPoul-Henning Kamp  * Mark an interface up and notify protocols of
1120e8c2601dSPoul-Henning Kamp  * the transition.
1121e8c2601dSPoul-Henning Kamp  * NOTE: must be called at splnet or eqivalent.
1122e8c2601dSPoul-Henning Kamp  */
1123e8c2601dSPoul-Henning Kamp void
1124e8c2601dSPoul-Henning Kamp if_up(ifp)
1125e8c2601dSPoul-Henning Kamp 	register struct ifnet *ifp;
1126e8c2601dSPoul-Henning Kamp {
1127e8c2601dSPoul-Henning Kamp 
1128e8c2601dSPoul-Henning Kamp 	if_route(ifp, IFF_UP, AF_UNSPEC);
1129e8c2601dSPoul-Henning Kamp }
1130e8c2601dSPoul-Henning Kamp 
1131e8c2601dSPoul-Henning Kamp /*
1132df8bae1dSRodney W. Grimes  * Flush an interface queue.
1133df8bae1dSRodney W. Grimes  */
11343bda9f9bSPoul-Henning Kamp static void
1135df8bae1dSRodney W. Grimes if_qflush(ifq)
1136df8bae1dSRodney W. Grimes 	register struct ifqueue *ifq;
1137df8bae1dSRodney W. Grimes {
1138df8bae1dSRodney W. Grimes 	register struct mbuf *m, *n;
1139df8bae1dSRodney W. Grimes 
1140df8bae1dSRodney W. Grimes 	n = ifq->ifq_head;
11419448326fSPoul-Henning Kamp 	while ((m = n) != 0) {
1142df8bae1dSRodney W. Grimes 		n = m->m_act;
1143df8bae1dSRodney W. Grimes 		m_freem(m);
1144df8bae1dSRodney W. Grimes 	}
1145df8bae1dSRodney W. Grimes 	ifq->ifq_head = 0;
1146df8bae1dSRodney W. Grimes 	ifq->ifq_tail = 0;
1147df8bae1dSRodney W. Grimes 	ifq->ifq_len = 0;
1148df8bae1dSRodney W. Grimes }
1149df8bae1dSRodney W. Grimes 
1150df8bae1dSRodney W. Grimes /*
1151df8bae1dSRodney W. Grimes  * Handle interface watchdog timer routines.  Called
1152df8bae1dSRodney W. Grimes  * from softclock, we decrement timers (if set) and
1153df8bae1dSRodney W. Grimes  * call the appropriate interface routine on expiration.
1154df8bae1dSRodney W. Grimes  */
11553bda9f9bSPoul-Henning Kamp static void
1156df8bae1dSRodney W. Grimes if_slowtimo(arg)
1157df8bae1dSRodney W. Grimes 	void *arg;
1158df8bae1dSRodney W. Grimes {
1159df8bae1dSRodney W. Grimes 	register struct ifnet *ifp;
1160df8bae1dSRodney W. Grimes 	int s = splimp();
1161df8bae1dSRodney W. Grimes 
1162fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
1163df8bae1dSRodney W. Grimes 		if (ifp->if_timer == 0 || --ifp->if_timer)
1164df8bae1dSRodney W. Grimes 			continue;
1165df8bae1dSRodney W. Grimes 		if (ifp->if_watchdog)
11664a5f1499SDavid Greenman 			(*ifp->if_watchdog)(ifp);
1167df8bae1dSRodney W. Grimes 	}
1168df8bae1dSRodney W. Grimes 	splx(s);
1169df8bae1dSRodney W. Grimes 	timeout(if_slowtimo, (void *)0, hz / IFNET_SLOWHZ);
1170df8bae1dSRodney W. Grimes }
1171df8bae1dSRodney W. Grimes 
1172df8bae1dSRodney W. Grimes /*
1173df8bae1dSRodney W. Grimes  * Map interface name to
1174df8bae1dSRodney W. Grimes  * interface structure pointer.
1175df8bae1dSRodney W. Grimes  */
1176df8bae1dSRodney W. Grimes struct ifnet *
117730aad87dSBrooks Davis ifunit(const char *name)
1178df8bae1dSRodney W. Grimes {
117901f0fef3SJulian Elischer 	char namebuf[IFNAMSIZ + 1];
11808b7805e4SBoris Popov 	struct ifnet *ifp;
118110930aadSJonathan Lemon 	dev_t dev;
1182df8bae1dSRodney W. Grimes 
118301f0fef3SJulian Elischer 	/*
118401f0fef3SJulian Elischer 	 * Now search all the interfaces for this name/number
118501f0fef3SJulian Elischer 	 */
118610930aadSJonathan Lemon 
118710930aadSJonathan Lemon 	/*
118810930aadSJonathan Lemon 	 * XXX
118910930aadSJonathan Lemon 	 * Devices should really be known as /dev/fooN, not /dev/net/fooN.
119010930aadSJonathan Lemon 	 */
119110930aadSJonathan Lemon 	snprintf(namebuf, IFNAMSIZ, "%s/%s", net_cdevsw.d_name, name);
1192fc2ffbe6SPoul-Henning Kamp 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
119310930aadSJonathan Lemon 		dev = ifdev_byindex(ifp->if_index);
119410930aadSJonathan Lemon 		if (strcmp(devtoname(dev), namebuf) == 0)
119510930aadSJonathan Lemon 			break;
119610930aadSJonathan Lemon 		if (dev_named(dev, name))
1197df8bae1dSRodney W. Grimes 			break;
1198df8bae1dSRodney W. Grimes 	}
1199df8bae1dSRodney W. Grimes 	return (ifp);
1200df8bae1dSRodney W. Grimes }
1201df8bae1dSRodney W. Grimes 
120282cd038dSYoshinobu Inoue /*
120382cd038dSYoshinobu Inoue  * Map interface name in a sockaddr_dl to
120482cd038dSYoshinobu Inoue  * interface structure pointer.
120582cd038dSYoshinobu Inoue  */
120682cd038dSYoshinobu Inoue struct ifnet *
120782cd038dSYoshinobu Inoue if_withname(sa)
120882cd038dSYoshinobu Inoue 	struct sockaddr *sa;
120982cd038dSYoshinobu Inoue {
121082cd038dSYoshinobu Inoue 	char ifname[IFNAMSIZ+1];
121182cd038dSYoshinobu Inoue 	struct sockaddr_dl *sdl = (struct sockaddr_dl *)sa;
121282cd038dSYoshinobu Inoue 
121382cd038dSYoshinobu Inoue 	if ( (sa->sa_family != AF_LINK) || (sdl->sdl_nlen == 0) ||
121482cd038dSYoshinobu Inoue 	     (sdl->sdl_nlen > IFNAMSIZ) )
121582cd038dSYoshinobu Inoue 		return NULL;
121682cd038dSYoshinobu Inoue 
121782cd038dSYoshinobu Inoue 	/*
12186e82956cSJuli Mallett 	 * ifunit wants a NUL-terminated string.  It may not be NUL-terminated
12196e82956cSJuli Mallett 	 * in the sockaddr, and we don't want to change the caller's sockaddr
12206e82956cSJuli Mallett 	 * (there might not be room to add the trailing NUL anyway), so we make
12216e82956cSJuli Mallett 	 * a local copy that we know we can NUL-terminate safely.
122282cd038dSYoshinobu Inoue 	 */
122382cd038dSYoshinobu Inoue 
122482cd038dSYoshinobu Inoue 	bcopy(sdl->sdl_data, ifname, sdl->sdl_nlen);
122582cd038dSYoshinobu Inoue 	ifname[sdl->sdl_nlen] = '\0';
122682cd038dSYoshinobu Inoue 	return ifunit(ifname);
122782cd038dSYoshinobu Inoue }
122882cd038dSYoshinobu Inoue 
1229df8bae1dSRodney W. Grimes /*
1230f13ad206SJonathan Lemon  * Hardware specific interface ioctls.
1231df8bae1dSRodney W. Grimes  */
1232f13ad206SJonathan Lemon static int
1233f13ad206SJonathan Lemon ifhwioctl(u_long cmd, struct ifnet *ifp, caddr_t data, struct thread *td)
1234df8bae1dSRodney W. Grimes {
1235f13ad206SJonathan Lemon 	struct ifreq *ifr;
1236413dd0baSPoul-Henning Kamp 	struct ifstat *ifs;
1237f13ad206SJonathan Lemon 	int error = 0;
123862f76486SMaxim Sobolev 	int new_flags;
1239df8bae1dSRodney W. Grimes 
1240df8bae1dSRodney W. Grimes 	ifr = (struct ifreq *)data;
124130aad87dSBrooks Davis 	switch (cmd) {
1242de593450SJonathan Lemon 	case SIOCGIFINDEX:
1243de593450SJonathan Lemon 		ifr->ifr_index = ifp->if_index;
1244de593450SJonathan Lemon 		break;
1245de593450SJonathan Lemon 
1246df8bae1dSRodney W. Grimes 	case SIOCGIFFLAGS:
124762f76486SMaxim Sobolev 		ifr->ifr_flags = ifp->if_flags & 0xffff;
124862f76486SMaxim Sobolev 		ifr->ifr_flagshigh = ifp->if_flags >> 16;
1249df8bae1dSRodney W. Grimes 		break;
1250df8bae1dSRodney W. Grimes 
1251016da741SJonathan Lemon 	case SIOCGIFCAP:
1252016da741SJonathan Lemon 		ifr->ifr_reqcap = ifp->if_capabilities;
1253016da741SJonathan Lemon 		ifr->ifr_curcap = ifp->if_capenable;
1254016da741SJonathan Lemon 		break;
1255016da741SJonathan Lemon 
12568f293a63SRobert Watson #ifdef MAC
12578f293a63SRobert Watson 	case SIOCGIFMAC:
12588f293a63SRobert Watson 		error = mac_ioctl_ifnet_get(td->td_proc->p_ucred, ifr, ifp);
12598f293a63SRobert Watson 		break;
12608f293a63SRobert Watson #endif
12618f293a63SRobert Watson 
1262df8bae1dSRodney W. Grimes 	case SIOCGIFMETRIC:
1263df8bae1dSRodney W. Grimes 		ifr->ifr_metric = ifp->if_metric;
1264df8bae1dSRodney W. Grimes 		break;
1265df8bae1dSRodney W. Grimes 
1266a7028af7SDavid Greenman 	case SIOCGIFMTU:
1267a7028af7SDavid Greenman 		ifr->ifr_mtu = ifp->if_mtu;
1268a7028af7SDavid Greenman 		break;
1269a7028af7SDavid Greenman 
1270074c4a4eSGarrett Wollman 	case SIOCGIFPHYS:
1271074c4a4eSGarrett Wollman 		ifr->ifr_phys = ifp->if_physical;
1272074c4a4eSGarrett Wollman 		break;
1273074c4a4eSGarrett Wollman 
1274df8bae1dSRodney W. Grimes 	case SIOCSIFFLAGS:
127544731cabSJohn Baldwin 		error = suser(td);
12769448326fSPoul-Henning Kamp 		if (error)
1277df8bae1dSRodney W. Grimes 			return (error);
127862f76486SMaxim Sobolev 		new_flags = (ifr->ifr_flags & 0xffff) |
127962f76486SMaxim Sobolev 		    (ifr->ifr_flagshigh << 16);
1280cf4b9371SPoul-Henning Kamp 		if (ifp->if_flags & IFF_SMART) {
1281cf4b9371SPoul-Henning Kamp 			/* Smart drivers twiddle their own routes */
12822f55ead7SPoul-Henning Kamp 		} else if (ifp->if_flags & IFF_UP &&
128362f76486SMaxim Sobolev 		    (new_flags & IFF_UP) == 0) {
1284df8bae1dSRodney W. Grimes 			int s = splimp();
1285df8bae1dSRodney W. Grimes 			if_down(ifp);
1286df8bae1dSRodney W. Grimes 			splx(s);
128762f76486SMaxim Sobolev 		} else if (new_flags & IFF_UP &&
1288cf4b9371SPoul-Henning Kamp 		    (ifp->if_flags & IFF_UP) == 0) {
1289df8bae1dSRodney W. Grimes 			int s = splimp();
1290df8bae1dSRodney W. Grimes 			if_up(ifp);
1291df8bae1dSRodney W. Grimes 			splx(s);
1292df8bae1dSRodney W. Grimes 		}
1293df8bae1dSRodney W. Grimes 		ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) |
129462f76486SMaxim Sobolev 			(new_flags &~ IFF_CANTCHANGE);
1295ffb079beSMaxim Sobolev 		if (new_flags & IFF_PPROMISC) {
1296ffb079beSMaxim Sobolev 			/* Permanently promiscuous mode requested */
1297ffb079beSMaxim Sobolev 			ifp->if_flags |= IFF_PROMISC;
1298ffb079beSMaxim Sobolev 		} else if (ifp->if_pcount == 0) {
1299ffb079beSMaxim Sobolev 			ifp->if_flags &= ~IFF_PROMISC;
1300ffb079beSMaxim Sobolev 		}
1301df8bae1dSRodney W. Grimes 		if (ifp->if_ioctl)
1302df8bae1dSRodney W. Grimes 			(void) (*ifp->if_ioctl)(ifp, cmd, data);
130398b9590eSPoul-Henning Kamp 		getmicrotime(&ifp->if_lastchange);
1304df8bae1dSRodney W. Grimes 		break;
1305df8bae1dSRodney W. Grimes 
1306016da741SJonathan Lemon 	case SIOCSIFCAP:
130744731cabSJohn Baldwin 		error = suser(td);
1308016da741SJonathan Lemon 		if (error)
1309016da741SJonathan Lemon 			return (error);
1310016da741SJonathan Lemon 		if (ifr->ifr_reqcap & ~ifp->if_capabilities)
1311016da741SJonathan Lemon 			return (EINVAL);
1312016da741SJonathan Lemon 		(void) (*ifp->if_ioctl)(ifp, cmd, data);
1313016da741SJonathan Lemon 		break;
1314016da741SJonathan Lemon 
13158f293a63SRobert Watson #ifdef MAC
13168f293a63SRobert Watson 	case SIOCSIFMAC:
13178f293a63SRobert Watson 		error = mac_ioctl_ifnet_set(td->td_proc->p_ucred, ifr, ifp);
13188f293a63SRobert Watson 		break;
13198f293a63SRobert Watson #endif
13208f293a63SRobert Watson 
1321df8bae1dSRodney W. Grimes 	case SIOCSIFMETRIC:
132244731cabSJohn Baldwin 		error = suser(td);
13239448326fSPoul-Henning Kamp 		if (error)
1324df8bae1dSRodney W. Grimes 			return (error);
1325df8bae1dSRodney W. Grimes 		ifp->if_metric = ifr->ifr_metric;
132698b9590eSPoul-Henning Kamp 		getmicrotime(&ifp->if_lastchange);
1327df8bae1dSRodney W. Grimes 		break;
1328df8bae1dSRodney W. Grimes 
1329074c4a4eSGarrett Wollman 	case SIOCSIFPHYS:
133044731cabSJohn Baldwin 		error = suser(td);
1331e39a0280SGary Palmer 		if (error)
1332e39a0280SGary Palmer 			return error;
1333e39a0280SGary Palmer 		if (!ifp->if_ioctl)
1334e39a0280SGary Palmer 		        return EOPNOTSUPP;
1335e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1336e39a0280SGary Palmer 		if (error == 0)
133798b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
1338e39a0280SGary Palmer 		return(error);
1339074c4a4eSGarrett Wollman 
1340a7028af7SDavid Greenman 	case SIOCSIFMTU:
134182cd038dSYoshinobu Inoue 	{
134282cd038dSYoshinobu Inoue 		u_long oldmtu = ifp->if_mtu;
134382cd038dSYoshinobu Inoue 
134444731cabSJohn Baldwin 		error = suser(td);
13459448326fSPoul-Henning Kamp 		if (error)
1346a7028af7SDavid Greenman 			return (error);
1347aab3beeeSBrian Somers 		if (ifr->ifr_mtu < IF_MINMTU || ifr->ifr_mtu > IF_MAXMTU)
134875ee03cbSDavid Greenman 			return (EINVAL);
1349f13ad206SJonathan Lemon 		if (ifp->if_ioctl == NULL)
1350f13ad206SJonathan Lemon 			return (EOPNOTSUPP);
1351e39a0280SGary Palmer 		error = (*ifp->if_ioctl)(ifp, cmd, data);
135248f71763SRuslan Ermilov 		if (error == 0) {
135398b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
135448f71763SRuslan Ermilov 			rt_ifmsg(ifp);
135548f71763SRuslan Ermilov 		}
135682cd038dSYoshinobu Inoue 		/*
135782cd038dSYoshinobu Inoue 		 * If the link MTU changed, do network layer specific procedure.
135882cd038dSYoshinobu Inoue 		 */
135982cd038dSYoshinobu Inoue 		if (ifp->if_mtu != oldmtu) {
136082cd038dSYoshinobu Inoue #ifdef INET6
136182cd038dSYoshinobu Inoue 			nd6_setmtu(ifp);
136282cd038dSYoshinobu Inoue #endif
136382cd038dSYoshinobu Inoue 		}
1364f13ad206SJonathan Lemon 		break;
136582cd038dSYoshinobu Inoue 	}
1366a7028af7SDavid Greenman 
1367df8bae1dSRodney W. Grimes 	case SIOCADDMULTI:
1368df8bae1dSRodney W. Grimes 	case SIOCDELMULTI:
136944731cabSJohn Baldwin 		error = suser(td);
13709448326fSPoul-Henning Kamp 		if (error)
1371df8bae1dSRodney W. Grimes 			return (error);
1372477180fbSGarrett Wollman 
1373477180fbSGarrett Wollman 		/* Don't allow group membership on non-multicast interfaces. */
1374477180fbSGarrett Wollman 		if ((ifp->if_flags & IFF_MULTICAST) == 0)
1375f13ad206SJonathan Lemon 			return (EOPNOTSUPP);
1376477180fbSGarrett Wollman 
1377477180fbSGarrett Wollman 		/* Don't let users screw up protocols' entries. */
1378477180fbSGarrett Wollman 		if (ifr->ifr_addr.sa_family != AF_LINK)
1379f13ad206SJonathan Lemon 			return (EINVAL);
1380477180fbSGarrett Wollman 
1381477180fbSGarrett Wollman 		if (cmd == SIOCADDMULTI) {
1382477180fbSGarrett Wollman 			struct ifmultiaddr *ifma;
1383477180fbSGarrett Wollman 			error = if_addmulti(ifp, &ifr->ifr_addr, &ifma);
1384477180fbSGarrett Wollman 		} else {
1385477180fbSGarrett Wollman 			error = if_delmulti(ifp, &ifr->ifr_addr);
1386477180fbSGarrett Wollman 		}
1387e39a0280SGary Palmer 		if (error == 0)
138898b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
1389f13ad206SJonathan Lemon 		break;
1390df8bae1dSRodney W. Grimes 
139141b3e8e5SJun-ichiro itojun Hagino 	case SIOCSIFPHYADDR:
139241b3e8e5SJun-ichiro itojun Hagino 	case SIOCDIFPHYADDR:
139341b3e8e5SJun-ichiro itojun Hagino #ifdef INET6
139441b3e8e5SJun-ichiro itojun Hagino 	case SIOCSIFPHYADDR_IN6:
139541b3e8e5SJun-ichiro itojun Hagino #endif
139633841545SHajimu UMEMOTO 	case SIOCSLIFPHYADDR:
1397a912e453SPeter Wemm         case SIOCSIFMEDIA:
1398d7189ec6SJoerg Wunsch 	case SIOCSIFGENERIC:
139944731cabSJohn Baldwin 		error = suser(td);
1400a912e453SPeter Wemm 		if (error)
1401a912e453SPeter Wemm 			return (error);
1402f13ad206SJonathan Lemon 		if (ifp->if_ioctl == NULL)
1403a912e453SPeter Wemm 			return (EOPNOTSUPP);
1404a912e453SPeter Wemm 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1405a912e453SPeter Wemm 		if (error == 0)
140698b9590eSPoul-Henning Kamp 			getmicrotime(&ifp->if_lastchange);
1407f13ad206SJonathan Lemon 		break;
1408a912e453SPeter Wemm 
1409413dd0baSPoul-Henning Kamp 	case SIOCGIFSTATUS:
1410413dd0baSPoul-Henning Kamp 		ifs = (struct ifstat *)data;
1411413dd0baSPoul-Henning Kamp 		ifs->ascii[0] = '\0';
1412413dd0baSPoul-Henning Kamp 
141333841545SHajimu UMEMOTO 	case SIOCGIFPSRCADDR:
141433841545SHajimu UMEMOTO 	case SIOCGIFPDSTADDR:
141533841545SHajimu UMEMOTO 	case SIOCGLIFPHYADDR:
1416a912e453SPeter Wemm 	case SIOCGIFMEDIA:
1417d7189ec6SJoerg Wunsch 	case SIOCGIFGENERIC:
1418a912e453SPeter Wemm 		if (ifp->if_ioctl == 0)
1419a912e453SPeter Wemm 			return (EOPNOTSUPP);
1420f13ad206SJonathan Lemon 		error = (*ifp->if_ioctl)(ifp, cmd, data);
1421f13ad206SJonathan Lemon 		break;
1422a912e453SPeter Wemm 
1423b106252cSBill Paul 	case SIOCSIFLLADDR:
142444731cabSJohn Baldwin 		error = suser(td);
1425b106252cSBill Paul 		if (error)
1426b106252cSBill Paul 			return (error);
1427f13ad206SJonathan Lemon 		error = if_setlladdr(ifp,
142866ce51ceSArchie Cobbs 		    ifr->ifr_addr.sa_data, ifr->ifr_addr.sa_len);
1429f13ad206SJonathan Lemon 		break;
143066ce51ceSArchie Cobbs 
1431df8bae1dSRodney W. Grimes 	default:
1432f13ad206SJonathan Lemon 		error = ENOIOCTL;
1433f13ad206SJonathan Lemon 		break;
1434f13ad206SJonathan Lemon 	}
1435f13ad206SJonathan Lemon 	return (error);
1436f13ad206SJonathan Lemon }
1437f13ad206SJonathan Lemon 
1438f13ad206SJonathan Lemon /*
1439f13ad206SJonathan Lemon  * Interface ioctls.
1440f13ad206SJonathan Lemon  */
1441f13ad206SJonathan Lemon int
1442f13ad206SJonathan Lemon ifioctl(so, cmd, data, td)
1443f13ad206SJonathan Lemon 	struct socket *so;
1444f13ad206SJonathan Lemon 	u_long cmd;
1445f13ad206SJonathan Lemon 	caddr_t data;
1446f13ad206SJonathan Lemon 	struct thread *td;
1447f13ad206SJonathan Lemon {
1448f13ad206SJonathan Lemon 	struct ifnet *ifp;
1449f13ad206SJonathan Lemon 	struct ifreq *ifr;
1450f13ad206SJonathan Lemon 	int error;
145162f76486SMaxim Sobolev 	int oif_flags;
1452f13ad206SJonathan Lemon 
1453f13ad206SJonathan Lemon 	switch (cmd) {
1454f13ad206SJonathan Lemon 	case SIOCGIFCONF:
1455f13ad206SJonathan Lemon 	case OSIOCGIFCONF:
1456f13ad206SJonathan Lemon 		return (ifconf(cmd, data));
1457f13ad206SJonathan Lemon 	}
1458f13ad206SJonathan Lemon 	ifr = (struct ifreq *)data;
1459f13ad206SJonathan Lemon 
1460f13ad206SJonathan Lemon 	switch (cmd) {
1461f13ad206SJonathan Lemon 	case SIOCIFCREATE:
1462f13ad206SJonathan Lemon 	case SIOCIFDESTROY:
146344731cabSJohn Baldwin 		if ((error = suser(td)) != 0)
1464f13ad206SJonathan Lemon 			return (error);
1465f13ad206SJonathan Lemon 		return ((cmd == SIOCIFCREATE) ?
1466f13ad206SJonathan Lemon 			if_clone_create(ifr->ifr_name, sizeof(ifr->ifr_name)) :
1467f13ad206SJonathan Lemon 			if_clone_destroy(ifr->ifr_name));
1468f13ad206SJonathan Lemon 
1469f13ad206SJonathan Lemon 	case SIOCIFGCLONERS:
1470f13ad206SJonathan Lemon 		return (if_clone_list((struct if_clonereq *)data));
1471f13ad206SJonathan Lemon 	}
1472f13ad206SJonathan Lemon 
1473f13ad206SJonathan Lemon 	ifp = ifunit(ifr->ifr_name);
1474f13ad206SJonathan Lemon 	if (ifp == 0)
1475f13ad206SJonathan Lemon 		return (ENXIO);
1476f13ad206SJonathan Lemon 
1477f13ad206SJonathan Lemon 	error = ifhwioctl(cmd, ifp, data, td);
1478f13ad206SJonathan Lemon 	if (error != ENOIOCTL)
1479f13ad206SJonathan Lemon 		return (error);
1480f13ad206SJonathan Lemon 
148182cd038dSYoshinobu Inoue 	oif_flags = ifp->if_flags;
1482df8bae1dSRodney W. Grimes 	if (so->so_proto == 0)
1483df8bae1dSRodney W. Grimes 		return (EOPNOTSUPP);
1484df8bae1dSRodney W. Grimes #ifndef COMPAT_43
148582cd038dSYoshinobu Inoue 	error = ((*so->so_proto->pr_usrreqs->pru_control)(so, cmd,
14862c37256eSGarrett Wollman 								 data,
1487b40ce416SJulian Elischer 								 ifp, td));
1488df8bae1dSRodney W. Grimes #else
1489df8bae1dSRodney W. Grimes 	{
1490df8bae1dSRodney W. Grimes 		int ocmd = cmd;
1491df8bae1dSRodney W. Grimes 
1492df8bae1dSRodney W. Grimes 		switch (cmd) {
1493df8bae1dSRodney W. Grimes 
1494df8bae1dSRodney W. Grimes 		case SIOCSIFDSTADDR:
1495df8bae1dSRodney W. Grimes 		case SIOCSIFADDR:
1496df8bae1dSRodney W. Grimes 		case SIOCSIFBRDADDR:
1497df8bae1dSRodney W. Grimes 		case SIOCSIFNETMASK:
1498df8bae1dSRodney W. Grimes #if BYTE_ORDER != BIG_ENDIAN
1499df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_family == 0 &&
1500df8bae1dSRodney W. Grimes 			    ifr->ifr_addr.sa_len < 16) {
1501df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_family = ifr->ifr_addr.sa_len;
1502df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
1503df8bae1dSRodney W. Grimes 			}
1504df8bae1dSRodney W. Grimes #else
1505df8bae1dSRodney W. Grimes 			if (ifr->ifr_addr.sa_len == 0)
1506df8bae1dSRodney W. Grimes 				ifr->ifr_addr.sa_len = 16;
1507df8bae1dSRodney W. Grimes #endif
1508df8bae1dSRodney W. Grimes 			break;
1509df8bae1dSRodney W. Grimes 
1510df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
1511df8bae1dSRodney W. Grimes 			cmd = SIOCGIFADDR;
1512df8bae1dSRodney W. Grimes 			break;
1513df8bae1dSRodney W. Grimes 
1514df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
1515df8bae1dSRodney W. Grimes 			cmd = SIOCGIFDSTADDR;
1516df8bae1dSRodney W. Grimes 			break;
1517df8bae1dSRodney W. Grimes 
1518df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
1519df8bae1dSRodney W. Grimes 			cmd = SIOCGIFBRDADDR;
1520df8bae1dSRodney W. Grimes 			break;
1521df8bae1dSRodney W. Grimes 
1522df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
1523df8bae1dSRodney W. Grimes 			cmd = SIOCGIFNETMASK;
1524df8bae1dSRodney W. Grimes 		}
15252c37256eSGarrett Wollman 		error =  ((*so->so_proto->pr_usrreqs->pru_control)(so,
15262c37256eSGarrett Wollman 								   cmd,
15272c37256eSGarrett Wollman 								   data,
1528b40ce416SJulian Elischer 								   ifp, td));
1529df8bae1dSRodney W. Grimes 		switch (ocmd) {
1530df8bae1dSRodney W. Grimes 
1531df8bae1dSRodney W. Grimes 		case OSIOCGIFADDR:
1532df8bae1dSRodney W. Grimes 		case OSIOCGIFDSTADDR:
1533df8bae1dSRodney W. Grimes 		case OSIOCGIFBRDADDR:
1534df8bae1dSRodney W. Grimes 		case OSIOCGIFNETMASK:
1535df8bae1dSRodney W. Grimes 			*(u_short *)&ifr->ifr_addr = ifr->ifr_addr.sa_family;
153682cd038dSYoshinobu Inoue 
153782cd038dSYoshinobu Inoue 		}
153882cd038dSYoshinobu Inoue 	}
153982cd038dSYoshinobu Inoue #endif /* COMPAT_43 */
154082cd038dSYoshinobu Inoue 
154182cd038dSYoshinobu Inoue 	if ((oif_flags ^ ifp->if_flags) & IFF_UP) {
154282cd038dSYoshinobu Inoue #ifdef INET6
154388ff5695SSUZUKI Shinsuke 		DELAY(100);/* XXX: temporary workaround for fxp issue*/
154482cd038dSYoshinobu Inoue 		if (ifp->if_flags & IFF_UP) {
154582cd038dSYoshinobu Inoue 			int s = splimp();
154682cd038dSYoshinobu Inoue 			in6_if_up(ifp);
154782cd038dSYoshinobu Inoue 			splx(s);
154882cd038dSYoshinobu Inoue 		}
154982cd038dSYoshinobu Inoue #endif
1550df8bae1dSRodney W. Grimes 	}
1551df8bae1dSRodney W. Grimes 	return (error);
1552df8bae1dSRodney W. Grimes }
1553df8bae1dSRodney W. Grimes 
1554df8bae1dSRodney W. Grimes /*
1555963e4c2aSGarrett Wollman  * Set/clear promiscuous mode on interface ifp based on the truth value
1556963e4c2aSGarrett Wollman  * of pswitch.  The calls are reference counted so that only the first
1557963e4c2aSGarrett Wollman  * "on" request actually has an effect, as does the final "off" request.
1558963e4c2aSGarrett Wollman  * Results are undefined if the "off" and "on" requests are not matched.
1559963e4c2aSGarrett Wollman  */
1560963e4c2aSGarrett Wollman int
1561963e4c2aSGarrett Wollman ifpromisc(ifp, pswitch)
1562963e4c2aSGarrett Wollman 	struct ifnet *ifp;
1563963e4c2aSGarrett Wollman 	int pswitch;
1564963e4c2aSGarrett Wollman {
1565963e4c2aSGarrett Wollman 	struct ifreq ifr;
15664a26224cSGarrett Wollman 	int error;
15674f3c11a6SBill Fenner 	int oldflags, oldpcount;
1568963e4c2aSGarrett Wollman 
15694f3c11a6SBill Fenner 	oldpcount = ifp->if_pcount;
15702c514a31SBrian Somers 	oldflags = ifp->if_flags;
1571ffb079beSMaxim Sobolev 	if (ifp->if_flags & IFF_PPROMISC) {
1572ffb079beSMaxim Sobolev 		/* Do nothing if device is in permanently promiscuous mode */
1573ffb079beSMaxim Sobolev 		ifp->if_pcount += pswitch ? 1 : -1;
1574ffb079beSMaxim Sobolev 		return (0);
1575ffb079beSMaxim Sobolev 	}
1576963e4c2aSGarrett Wollman 	if (pswitch) {
1577963e4c2aSGarrett Wollman 		/*
1578963e4c2aSGarrett Wollman 		 * If the device is not configured up, we cannot put it in
1579963e4c2aSGarrett Wollman 		 * promiscuous mode.
1580963e4c2aSGarrett Wollman 		 */
1581963e4c2aSGarrett Wollman 		if ((ifp->if_flags & IFF_UP) == 0)
1582963e4c2aSGarrett Wollman 			return (ENETDOWN);
1583963e4c2aSGarrett Wollman 		if (ifp->if_pcount++ != 0)
1584963e4c2aSGarrett Wollman 			return (0);
1585963e4c2aSGarrett Wollman 		ifp->if_flags |= IFF_PROMISC;
1586963e4c2aSGarrett Wollman 	} else {
1587963e4c2aSGarrett Wollman 		if (--ifp->if_pcount > 0)
1588963e4c2aSGarrett Wollman 			return (0);
1589963e4c2aSGarrett Wollman 		ifp->if_flags &= ~IFF_PROMISC;
1590963e4c2aSGarrett Wollman 	}
159162f76486SMaxim Sobolev 	ifr.ifr_flags = ifp->if_flags & 0xffff;
159262f76486SMaxim Sobolev 	ifr.ifr_flagshigh = ifp->if_flags >> 16;
15934a26224cSGarrett Wollman 	error = (*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
15944f3c11a6SBill Fenner 	if (error == 0) {
15954f3c11a6SBill Fenner 		log(LOG_INFO, "%s%d: promiscuous mode %s\n",
15964f3c11a6SBill Fenner 		    ifp->if_name, ifp->if_unit,
15974f3c11a6SBill Fenner 		    (ifp->if_flags & IFF_PROMISC) ? "enabled" : "disabled");
15984a26224cSGarrett Wollman 		rt_ifmsg(ifp);
15994f3c11a6SBill Fenner 	} else {
16004f3c11a6SBill Fenner 		ifp->if_pcount = oldpcount;
16012c514a31SBrian Somers 		ifp->if_flags = oldflags;
16024f3c11a6SBill Fenner 	}
16034a26224cSGarrett Wollman 	return error;
1604963e4c2aSGarrett Wollman }
1605963e4c2aSGarrett Wollman 
1606963e4c2aSGarrett Wollman /*
1607df8bae1dSRodney W. Grimes  * Return interface configuration
1608df8bae1dSRodney W. Grimes  * of system.  List may be used
1609df8bae1dSRodney W. Grimes  * in later ioctl's (above) to get
1610df8bae1dSRodney W. Grimes  * other information.
1611df8bae1dSRodney W. Grimes  */
1612df8bae1dSRodney W. Grimes /*ARGSUSED*/
16133bda9f9bSPoul-Henning Kamp static int
1614df8bae1dSRodney W. Grimes ifconf(cmd, data)
1615ecbb00a2SDoug Rabson 	u_long cmd;
1616df8bae1dSRodney W. Grimes 	caddr_t data;
1617df8bae1dSRodney W. Grimes {
16180b59d917SJonathan Lemon 	struct ifconf *ifc = (struct ifconf *)data;
16190b59d917SJonathan Lemon 	struct ifnet *ifp;
16200b59d917SJonathan Lemon 	struct ifaddr *ifa;
1621df8bae1dSRodney W. Grimes 	struct ifreq ifr, *ifrp;
1622df8bae1dSRodney W. Grimes 	int space = ifc->ifc_len, error = 0;
1623df8bae1dSRodney W. Grimes 
1624df8bae1dSRodney W. Grimes 	ifrp = ifc->ifc_req;
16250b59d917SJonathan Lemon 	TAILQ_FOREACH(ifp, &ifnet, if_link) {
16261ce9bf88SPoul-Henning Kamp 		char workbuf[64];
162775c13541SPoul-Henning Kamp 		int ifnlen, addrs;
16282624cf89SGarrett Wollman 
16292defe5cdSJonathan Lemon 		if (space < sizeof(ifr))
16300b59d917SJonathan Lemon 			break;
16312127f260SArchie Cobbs 		ifnlen = snprintf(workbuf, sizeof(workbuf),
16322127f260SArchie Cobbs 		    "%s%d", ifp->if_name, ifp->if_unit);
16331ce9bf88SPoul-Henning Kamp 		if(ifnlen + 1 > sizeof ifr.ifr_name) {
16342624cf89SGarrett Wollman 			error = ENAMETOOLONG;
1635b3f1e629SGuido van Rooij 			break;
16362624cf89SGarrett Wollman 		} else {
16371ce9bf88SPoul-Henning Kamp 			strcpy(ifr.ifr_name, workbuf);
16382624cf89SGarrett Wollman 		}
16392624cf89SGarrett Wollman 
164075c13541SPoul-Henning Kamp 		addrs = 0;
16412defe5cdSJonathan Lemon 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
16422defe5cdSJonathan Lemon 			struct sockaddr *sa = ifa->ifa_addr;
16432defe5cdSJonathan Lemon 
16442defe5cdSJonathan Lemon 			if (space < sizeof(ifr))
16452defe5cdSJonathan Lemon 				break;
1646a854ed98SJohn Baldwin 			if (jailed(curthread->td_ucred) &&
1647a854ed98SJohn Baldwin 			    prison_if(curthread->td_ucred, sa))
164875c13541SPoul-Henning Kamp 				continue;
164975c13541SPoul-Henning Kamp 			addrs++;
1650df8bae1dSRodney W. Grimes #ifdef COMPAT_43
1651df8bae1dSRodney W. Grimes 			if (cmd == OSIOCGIFCONF) {
1652df8bae1dSRodney W. Grimes 				struct osockaddr *osa =
1653df8bae1dSRodney W. Grimes 					 (struct osockaddr *)&ifr.ifr_addr;
1654df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
1655df8bae1dSRodney W. Grimes 				osa->sa_family = sa->sa_family;
1656df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1657df8bae1dSRodney W. Grimes 						sizeof (ifr));
1658df8bae1dSRodney W. Grimes 				ifrp++;
1659df8bae1dSRodney W. Grimes 			} else
1660df8bae1dSRodney W. Grimes #endif
1661df8bae1dSRodney W. Grimes 			if (sa->sa_len <= sizeof(*sa)) {
1662df8bae1dSRodney W. Grimes 				ifr.ifr_addr = *sa;
1663df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1664df8bae1dSRodney W. Grimes 						sizeof (ifr));
1665df8bae1dSRodney W. Grimes 				ifrp++;
1666df8bae1dSRodney W. Grimes 			} else {
1667d91a068eSGuido van Rooij 				if (space < sizeof (ifr) + sa->sa_len -
1668d91a068eSGuido van Rooij 					    sizeof(*sa))
1669b3f1e629SGuido van Rooij 					break;
1670df8bae1dSRodney W. Grimes 				space -= sa->sa_len - sizeof(*sa);
1671df8bae1dSRodney W. Grimes 				error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
1672df8bae1dSRodney W. Grimes 						sizeof (ifr.ifr_name));
1673df8bae1dSRodney W. Grimes 				if (error == 0)
1674df8bae1dSRodney W. Grimes 				    error = copyout((caddr_t)sa,
1675df8bae1dSRodney W. Grimes 				      (caddr_t)&ifrp->ifr_addr, sa->sa_len);
1676df8bae1dSRodney W. Grimes 				ifrp = (struct ifreq *)
1677df8bae1dSRodney W. Grimes 					(sa->sa_len + (caddr_t)&ifrp->ifr_addr);
1678df8bae1dSRodney W. Grimes 			}
1679df8bae1dSRodney W. Grimes 			if (error)
1680df8bae1dSRodney W. Grimes 				break;
1681df8bae1dSRodney W. Grimes 			space -= sizeof (ifr);
1682df8bae1dSRodney W. Grimes 		}
1683b3f1e629SGuido van Rooij 		if (error)
1684b3f1e629SGuido van Rooij 			break;
168575c13541SPoul-Henning Kamp 		if (!addrs) {
168675c13541SPoul-Henning Kamp 			bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr));
168775c13541SPoul-Henning Kamp 			error = copyout((caddr_t)&ifr, (caddr_t)ifrp,
168875c13541SPoul-Henning Kamp 			    sizeof (ifr));
168975c13541SPoul-Henning Kamp 			if (error)
169075c13541SPoul-Henning Kamp 				break;
1691b3f1e629SGuido van Rooij 			space -= sizeof (ifr);
1692b3f1e629SGuido van Rooij 			ifrp++;
169375c13541SPoul-Henning Kamp 		}
1694df8bae1dSRodney W. Grimes 	}
1695df8bae1dSRodney W. Grimes 	ifc->ifc_len -= space;
1696df8bae1dSRodney W. Grimes 	return (error);
1697df8bae1dSRodney W. Grimes }
1698df8bae1dSRodney W. Grimes 
16991158dfb7SGarrett Wollman /*
17001158dfb7SGarrett Wollman  * Just like if_promisc(), but for all-multicast-reception mode.
17011158dfb7SGarrett Wollman  */
17021158dfb7SGarrett Wollman int
17031158dfb7SGarrett Wollman if_allmulti(ifp, onswitch)
17041158dfb7SGarrett Wollman 	struct ifnet *ifp;
17051158dfb7SGarrett Wollman 	int onswitch;
17061158dfb7SGarrett Wollman {
17071158dfb7SGarrett Wollman 	int error = 0;
17081158dfb7SGarrett Wollman 	int s = splimp();
1709ee0a4f7eSSUZUKI Shinsuke 	struct ifreq ifr;
17101158dfb7SGarrett Wollman 
17111158dfb7SGarrett Wollman 	if (onswitch) {
17121158dfb7SGarrett Wollman 		if (ifp->if_amcount++ == 0) {
17131158dfb7SGarrett Wollman 			ifp->if_flags |= IFF_ALLMULTI;
171462f76486SMaxim Sobolev 			ifr.ifr_flags = ifp->if_flags & 0xffff;
171562f76486SMaxim Sobolev 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
1716ee0a4f7eSSUZUKI Shinsuke 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
17171158dfb7SGarrett Wollman 		}
17181158dfb7SGarrett Wollman 	} else {
17191158dfb7SGarrett Wollman 		if (ifp->if_amcount > 1) {
17201158dfb7SGarrett Wollman 			ifp->if_amcount--;
17211158dfb7SGarrett Wollman 		} else {
17221158dfb7SGarrett Wollman 			ifp->if_amcount = 0;
17231158dfb7SGarrett Wollman 			ifp->if_flags &= ~IFF_ALLMULTI;
172462f76486SMaxim Sobolev 			ifr.ifr_flags = ifp->if_flags & 0xffff;;
172562f76486SMaxim Sobolev 			ifr.ifr_flagshigh = ifp->if_flags >> 16;
1726ee0a4f7eSSUZUKI Shinsuke 			error = ifp->if_ioctl(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
17271158dfb7SGarrett Wollman 		}
17281158dfb7SGarrett Wollman 	}
17291158dfb7SGarrett Wollman 	splx(s);
17304a26224cSGarrett Wollman 
17314a26224cSGarrett Wollman 	if (error == 0)
17324a26224cSGarrett Wollman 		rt_ifmsg(ifp);
17331158dfb7SGarrett Wollman 	return error;
17341158dfb7SGarrett Wollman }
17351158dfb7SGarrett Wollman 
17361158dfb7SGarrett Wollman /*
17371158dfb7SGarrett Wollman  * Add a multicast listenership to the interface in question.
17381158dfb7SGarrett Wollman  * The link layer provides a routine which converts
17391158dfb7SGarrett Wollman  */
17401158dfb7SGarrett Wollman int
1741373f88edSGarrett Wollman if_addmulti(ifp, sa, retifma)
17421158dfb7SGarrett Wollman 	struct ifnet *ifp;	/* interface to manipulate */
17431158dfb7SGarrett Wollman 	struct sockaddr *sa;	/* address to add */
1744b2053118SGarrett Wollman 	struct ifmultiaddr **retifma;
17451158dfb7SGarrett Wollman {
17461158dfb7SGarrett Wollman 	struct sockaddr *llsa, *dupsa;
17471158dfb7SGarrett Wollman 	int error, s;
17481158dfb7SGarrett Wollman 	struct ifmultiaddr *ifma;
17491158dfb7SGarrett Wollman 
175057af7922SJulian Elischer 	/*
175157af7922SJulian Elischer 	 * If the matching multicast address already exists
175257af7922SJulian Elischer 	 * then don't add a new one, just add a reference
175357af7922SJulian Elischer 	 */
17546817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
175557af7922SJulian Elischer 		if (equal(sa, ifma->ifma_addr)) {
17561158dfb7SGarrett Wollman 			ifma->ifma_refcount++;
175757af7922SJulian Elischer 			if (retifma)
175857af7922SJulian Elischer 				*retifma = ifma;
17591158dfb7SGarrett Wollman 			return 0;
17601158dfb7SGarrett Wollman 		}
176157af7922SJulian Elischer 	}
17621158dfb7SGarrett Wollman 
17631158dfb7SGarrett Wollman 	/*
17641158dfb7SGarrett Wollman 	 * Give the link layer a chance to accept/reject it, and also
17651158dfb7SGarrett Wollman 	 * find out which AF_LINK address this maps to, if it isn't one
17661158dfb7SGarrett Wollman 	 * already.
17671158dfb7SGarrett Wollman 	 */
17681158dfb7SGarrett Wollman 	if (ifp->if_resolvemulti) {
17691158dfb7SGarrett Wollman 		error = ifp->if_resolvemulti(ifp, &llsa, sa);
17701158dfb7SGarrett Wollman 		if (error) return error;
17711158dfb7SGarrett Wollman 	} else {
17721158dfb7SGarrett Wollman 		llsa = 0;
17731158dfb7SGarrett Wollman 	}
17741158dfb7SGarrett Wollman 
17751158dfb7SGarrett Wollman 	MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma, M_IFMADDR, M_WAITOK);
17761158dfb7SGarrett Wollman 	MALLOC(dupsa, struct sockaddr *, sa->sa_len, M_IFMADDR, M_WAITOK);
17771158dfb7SGarrett Wollman 	bcopy(sa, dupsa, sa->sa_len);
17781158dfb7SGarrett Wollman 
17791158dfb7SGarrett Wollman 	ifma->ifma_addr = dupsa;
17801158dfb7SGarrett Wollman 	ifma->ifma_lladdr = llsa;
17811158dfb7SGarrett Wollman 	ifma->ifma_ifp = ifp;
17821158dfb7SGarrett Wollman 	ifma->ifma_refcount = 1;
1783373f88edSGarrett Wollman 	ifma->ifma_protospec = 0;
1784477180fbSGarrett Wollman 	rt_newmaddrmsg(RTM_NEWMADDR, ifma);
1785373f88edSGarrett Wollman 
17861158dfb7SGarrett Wollman 	/*
17871158dfb7SGarrett Wollman 	 * Some network interfaces can scan the address list at
17881158dfb7SGarrett Wollman 	 * interrupt time; lock them out.
17891158dfb7SGarrett Wollman 	 */
17901158dfb7SGarrett Wollman 	s = splimp();
17916817526dSPoul-Henning Kamp 	TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
17921158dfb7SGarrett Wollman 	splx(s);
179313990766SJonathan Mini 	if (retifma != NULL)
1794373f88edSGarrett Wollman 		*retifma = ifma;
17951158dfb7SGarrett Wollman 
17961158dfb7SGarrett Wollman 	if (llsa != 0) {
17976817526dSPoul-Henning Kamp 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
17981158dfb7SGarrett Wollman 			if (equal(ifma->ifma_addr, llsa))
17991158dfb7SGarrett Wollman 				break;
18001158dfb7SGarrett Wollman 		}
18011158dfb7SGarrett Wollman 		if (ifma) {
18021158dfb7SGarrett Wollman 			ifma->ifma_refcount++;
18031158dfb7SGarrett Wollman 		} else {
18041158dfb7SGarrett Wollman 			MALLOC(ifma, struct ifmultiaddr *, sizeof *ifma,
18051158dfb7SGarrett Wollman 			       M_IFMADDR, M_WAITOK);
1806477180fbSGarrett Wollman 			MALLOC(dupsa, struct sockaddr *, llsa->sa_len,
1807477180fbSGarrett Wollman 			       M_IFMADDR, M_WAITOK);
1808477180fbSGarrett Wollman 			bcopy(llsa, dupsa, llsa->sa_len);
1809477180fbSGarrett Wollman 			ifma->ifma_addr = dupsa;
18101158dfb7SGarrett Wollman 			ifma->ifma_ifp = ifp;
18111158dfb7SGarrett Wollman 			ifma->ifma_refcount = 1;
18121158dfb7SGarrett Wollman 			s = splimp();
18136817526dSPoul-Henning Kamp 			TAILQ_INSERT_HEAD(&ifp->if_multiaddrs, ifma, ifma_link);
18141158dfb7SGarrett Wollman 			splx(s);
18151158dfb7SGarrett Wollman 		}
181657af7922SJulian Elischer 	}
18171158dfb7SGarrett Wollman 	/*
18181158dfb7SGarrett Wollman 	 * We are certain we have added something, so call down to the
18191158dfb7SGarrett Wollman 	 * interface to let them know about it.
18201158dfb7SGarrett Wollman 	 */
18211158dfb7SGarrett Wollman 	s = splimp();
18221158dfb7SGarrett Wollman 	ifp->if_ioctl(ifp, SIOCADDMULTI, 0);
18231158dfb7SGarrett Wollman 	splx(s);
18241158dfb7SGarrett Wollman 
18251158dfb7SGarrett Wollman 	return 0;
18261158dfb7SGarrett Wollman }
18271158dfb7SGarrett Wollman 
18281158dfb7SGarrett Wollman /*
18291158dfb7SGarrett Wollman  * Remove a reference to a multicast address on this interface.  Yell
18301158dfb7SGarrett Wollman  * if the request does not match an existing membership.
18311158dfb7SGarrett Wollman  */
18321158dfb7SGarrett Wollman int
18331158dfb7SGarrett Wollman if_delmulti(ifp, sa)
18341158dfb7SGarrett Wollman 	struct ifnet *ifp;
18351158dfb7SGarrett Wollman 	struct sockaddr *sa;
18361158dfb7SGarrett Wollman {
18371158dfb7SGarrett Wollman 	struct ifmultiaddr *ifma;
18381158dfb7SGarrett Wollman 	int s;
18391158dfb7SGarrett Wollman 
18406817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
18411158dfb7SGarrett Wollman 		if (equal(sa, ifma->ifma_addr))
18421158dfb7SGarrett Wollman 			break;
18431158dfb7SGarrett Wollman 	if (ifma == 0)
18441158dfb7SGarrett Wollman 		return ENOENT;
18451158dfb7SGarrett Wollman 
18461158dfb7SGarrett Wollman 	if (ifma->ifma_refcount > 1) {
18471158dfb7SGarrett Wollman 		ifma->ifma_refcount--;
18481158dfb7SGarrett Wollman 		return 0;
18491158dfb7SGarrett Wollman 	}
18501158dfb7SGarrett Wollman 
1851477180fbSGarrett Wollman 	rt_newmaddrmsg(RTM_DELMADDR, ifma);
18521158dfb7SGarrett Wollman 	sa = ifma->ifma_lladdr;
18531158dfb7SGarrett Wollman 	s = splimp();
18546817526dSPoul-Henning Kamp 	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
1855ccb7cc8dSYaroslav Tykhiy 	/*
1856ccb7cc8dSYaroslav Tykhiy 	 * Make sure the interface driver is notified
1857ccb7cc8dSYaroslav Tykhiy 	 * in the case of a link layer mcast group being left.
1858ccb7cc8dSYaroslav Tykhiy 	 */
1859ccb7cc8dSYaroslav Tykhiy 	if (ifma->ifma_addr->sa_family == AF_LINK && sa == 0)
1860ccb7cc8dSYaroslav Tykhiy 		ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
18611158dfb7SGarrett Wollman 	splx(s);
18621158dfb7SGarrett Wollman 	free(ifma->ifma_addr, M_IFMADDR);
18631158dfb7SGarrett Wollman 	free(ifma, M_IFMADDR);
18641158dfb7SGarrett Wollman 	if (sa == 0)
18651158dfb7SGarrett Wollman 		return 0;
18661158dfb7SGarrett Wollman 
18671158dfb7SGarrett Wollman 	/*
18681158dfb7SGarrett Wollman 	 * Now look for the link-layer address which corresponds to
18691158dfb7SGarrett Wollman 	 * this network address.  It had been squirreled away in
18701158dfb7SGarrett Wollman 	 * ifma->ifma_lladdr for this purpose (so we don't have
18711158dfb7SGarrett Wollman 	 * to call ifp->if_resolvemulti() again), and we saved that
18721158dfb7SGarrett Wollman 	 * value in sa above.  If some nasty deleted the
18731158dfb7SGarrett Wollman 	 * link-layer address out from underneath us, we can deal because
18741158dfb7SGarrett Wollman 	 * the address we stored was is not the same as the one which was
18751158dfb7SGarrett Wollman 	 * in the record for the link-layer address.  (So we don't complain
18761158dfb7SGarrett Wollman 	 * in that case.)
18771158dfb7SGarrett Wollman 	 */
18786817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
18791158dfb7SGarrett Wollman 		if (equal(sa, ifma->ifma_addr))
18801158dfb7SGarrett Wollman 			break;
18811158dfb7SGarrett Wollman 	if (ifma == 0)
18821158dfb7SGarrett Wollman 		return 0;
18831158dfb7SGarrett Wollman 
18841158dfb7SGarrett Wollman 	if (ifma->ifma_refcount > 1) {
18851158dfb7SGarrett Wollman 		ifma->ifma_refcount--;
18861158dfb7SGarrett Wollman 		return 0;
18871158dfb7SGarrett Wollman 	}
18881158dfb7SGarrett Wollman 
18891158dfb7SGarrett Wollman 	s = splimp();
18906817526dSPoul-Henning Kamp 	TAILQ_REMOVE(&ifp->if_multiaddrs, ifma, ifma_link);
1891c7323482SBill Paul 	ifp->if_ioctl(ifp, SIOCDELMULTI, 0);
18921158dfb7SGarrett Wollman 	splx(s);
18931158dfb7SGarrett Wollman 	free(ifma->ifma_addr, M_IFMADDR);
18941158dfb7SGarrett Wollman 	free(sa, M_IFMADDR);
18951158dfb7SGarrett Wollman 	free(ifma, M_IFMADDR);
18961158dfb7SGarrett Wollman 
18971158dfb7SGarrett Wollman 	return 0;
18981158dfb7SGarrett Wollman }
18991158dfb7SGarrett Wollman 
190066ce51ceSArchie Cobbs /*
190166ce51ceSArchie Cobbs  * Set the link layer address on an interface.
190266ce51ceSArchie Cobbs  *
190366ce51ceSArchie Cobbs  * At this time we only support certain types of interfaces,
190466ce51ceSArchie Cobbs  * and we don't allow the length of the address to change.
190566ce51ceSArchie Cobbs  */
190666ce51ceSArchie Cobbs int
190766ce51ceSArchie Cobbs if_setlladdr(struct ifnet *ifp, const u_char *lladdr, int len)
190866ce51ceSArchie Cobbs {
190966ce51ceSArchie Cobbs 	struct sockaddr_dl *sdl;
191066ce51ceSArchie Cobbs 	struct ifaddr *ifa;
1911d637e989SPeter Wemm 	struct ifreq ifr;
191266ce51ceSArchie Cobbs 
1913f9132cebSJonathan Lemon 	ifa = ifaddr_byindex(ifp->if_index);
191466ce51ceSArchie Cobbs 	if (ifa == NULL)
191566ce51ceSArchie Cobbs 		return (EINVAL);
191666ce51ceSArchie Cobbs 	sdl = (struct sockaddr_dl *)ifa->ifa_addr;
191766ce51ceSArchie Cobbs 	if (sdl == NULL)
191866ce51ceSArchie Cobbs 		return (EINVAL);
191966ce51ceSArchie Cobbs 	if (len != sdl->sdl_alen)	/* don't allow length to change */
192066ce51ceSArchie Cobbs 		return (EINVAL);
192166ce51ceSArchie Cobbs 	switch (ifp->if_type) {
192266ce51ceSArchie Cobbs 	case IFT_ETHER:			/* these types use struct arpcom */
192366ce51ceSArchie Cobbs 	case IFT_FDDI:
192466ce51ceSArchie Cobbs 	case IFT_XETHER:
192566ce51ceSArchie Cobbs 	case IFT_ISO88025:
1926b7bffa71SYaroslav Tykhiy 	case IFT_L2VLAN:
192766ce51ceSArchie Cobbs 		bcopy(lladdr, ((struct arpcom *)ifp->if_softc)->ac_enaddr, len);
192866ce51ceSArchie Cobbs 		bcopy(lladdr, LLADDR(sdl), len);
192966ce51ceSArchie Cobbs 		break;
193066ce51ceSArchie Cobbs 	default:
193166ce51ceSArchie Cobbs 		return (ENODEV);
193266ce51ceSArchie Cobbs 	}
193366ce51ceSArchie Cobbs 	/*
193466ce51ceSArchie Cobbs 	 * If the interface is already up, we need
193566ce51ceSArchie Cobbs 	 * to re-init it in order to reprogram its
193666ce51ceSArchie Cobbs 	 * address filter.
193766ce51ceSArchie Cobbs 	 */
193866ce51ceSArchie Cobbs 	if ((ifp->if_flags & IFF_UP) != 0) {
193966ce51ceSArchie Cobbs 		ifp->if_flags &= ~IFF_UP;
194062f76486SMaxim Sobolev 		ifr.ifr_flags = ifp->if_flags & 0xffff;
194162f76486SMaxim Sobolev 		ifr.ifr_flagshigh = ifp->if_flags >> 16;
1942ee0a4f7eSSUZUKI Shinsuke 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
194366ce51ceSArchie Cobbs 		ifp->if_flags |= IFF_UP;
194462f76486SMaxim Sobolev 		ifr.ifr_flags = ifp->if_flags & 0xffff;
194562f76486SMaxim Sobolev 		ifr.ifr_flagshigh = ifp->if_flags >> 16;
1946ee0a4f7eSSUZUKI Shinsuke 		(*ifp->if_ioctl)(ifp, SIOCSIFFLAGS, (caddr_t)&ifr);
1947b2c08f43SLuigi Rizzo #ifdef INET
1948b2c08f43SLuigi Rizzo 		/*
1949b2c08f43SLuigi Rizzo 		 * Also send gratuitous ARPs to notify other nodes about
1950b2c08f43SLuigi Rizzo 		 * the address change.
1951b2c08f43SLuigi Rizzo 		 */
1952b2c08f43SLuigi Rizzo 		TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
1953b2c08f43SLuigi Rizzo 			if (ifa->ifa_addr != NULL &&
1954b2c08f43SLuigi Rizzo 			    ifa->ifa_addr->sa_family == AF_INET)
1955c0933269SPeter Wemm 				arp_ifinit(ifp, ifa);
1956b2c08f43SLuigi Rizzo 		}
1957b2c08f43SLuigi Rizzo #endif
195866ce51ceSArchie Cobbs 	}
195966ce51ceSArchie Cobbs 	return (0);
196066ce51ceSArchie Cobbs }
196166ce51ceSArchie Cobbs 
1962373f88edSGarrett Wollman struct ifmultiaddr *
1963373f88edSGarrett Wollman ifmaof_ifpforaddr(sa, ifp)
1964373f88edSGarrett Wollman 	struct sockaddr *sa;
1965373f88edSGarrett Wollman 	struct ifnet *ifp;
1966373f88edSGarrett Wollman {
1967373f88edSGarrett Wollman 	struct ifmultiaddr *ifma;
1968373f88edSGarrett Wollman 
19696817526dSPoul-Henning Kamp 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link)
1970373f88edSGarrett Wollman 		if (equal(ifma->ifma_addr, sa))
1971373f88edSGarrett Wollman 			break;
1972373f88edSGarrett Wollman 
1973373f88edSGarrett Wollman 	return ifma;
1974373f88edSGarrett Wollman }
1975373f88edSGarrett Wollman 
1976fa882e87SBrooks Davis int
1977fa882e87SBrooks Davis if_printf(struct ifnet *ifp, const char * fmt, ...)
1978fa882e87SBrooks Davis {
1979fa882e87SBrooks Davis 	va_list ap;
1980fa882e87SBrooks Davis 	int retval;
1981fa882e87SBrooks Davis 
1982fa882e87SBrooks Davis 	retval = printf("%s%d: ", ifp->if_name, ifp->if_unit);
1983fa882e87SBrooks Davis 	va_start(ap, fmt);
1984fa882e87SBrooks Davis 	retval += vprintf(fmt, ap);
1985fa882e87SBrooks Davis 	va_end(ap);
1986fa882e87SBrooks Davis 	return (retval);
1987fa882e87SBrooks Davis }
1988fa882e87SBrooks Davis 
1989602d513cSGarrett Wollman SYSCTL_NODE(_net, PF_LINK, link, CTLFLAG_RW, 0, "Link layers");
19902c37256eSGarrett Wollman SYSCTL_NODE(_net_link, 0, generic, CTLFLAG_RW, 0, "Generic link-management");
1991