if_tap.c revision 1.1.4.4 1 1.1.4.4 skrll /* $NetBSD: if_tap.c,v 1.1.4.4 2005/02/04 11:47:44 skrll Exp $ */
2 1.1.4.2 skrll
3 1.1.4.2 skrll /*
4 1.1.4.2 skrll * Copyright (c) 2003, 2004 The NetBSD Foundation.
5 1.1.4.2 skrll * All rights reserved.
6 1.1.4.2 skrll *
7 1.1.4.2 skrll * This code is derived from software contributed to the NetBSD Foundation
8 1.1.4.2 skrll * by Quentin Garnier.
9 1.1.4.2 skrll *
10 1.1.4.2 skrll * Redistribution and use in source and binary forms, with or without
11 1.1.4.2 skrll * modification, are permitted provided that the following conditions
12 1.1.4.2 skrll * are met:
13 1.1.4.2 skrll * 1. Redistributions of source code must retain the above copyright
14 1.1.4.2 skrll * notice, this list of conditions and the following disclaimer.
15 1.1.4.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.4.2 skrll * notice, this list of conditions and the following disclaimer in the
17 1.1.4.2 skrll * documentation and/or other materials provided with the distribution.
18 1.1.4.2 skrll * 3. All advertising materials mentioning features or use of this software
19 1.1.4.2 skrll * must display the following acknowledgement:
20 1.1.4.2 skrll * This product includes software developed by the NetBSD
21 1.1.4.2 skrll * Foundation, Inc. and its contributors.
22 1.1.4.2 skrll * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1.4.2 skrll * contributors may be used to endorse or promote products derived
24 1.1.4.2 skrll * from this software without specific prior written permission.
25 1.1.4.2 skrll *
26 1.1.4.2 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1.4.2 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1.4.2 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1.4.2 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1.4.2 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1.4.2 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1.4.2 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1.4.2 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1.4.2 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1.4.2 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1.4.2 skrll * POSSIBILITY OF SUCH DAMAGE.
37 1.1.4.2 skrll */
38 1.1.4.2 skrll
39 1.1.4.2 skrll /*
40 1.1.4.2 skrll * tap(4) is a virtual Ethernet interface. It appears as a real Ethernet
41 1.1.4.2 skrll * device to the system, but can also be accessed by userland through a
42 1.1.4.2 skrll * character device interface, which allows reading and injecting frames.
43 1.1.4.2 skrll */
44 1.1.4.2 skrll
45 1.1.4.2 skrll #include <sys/cdefs.h>
46 1.1.4.4 skrll __KERNEL_RCSID(0, "$NetBSD: if_tap.c,v 1.1.4.4 2005/02/04 11:47:44 skrll Exp $");
47 1.1.4.2 skrll
48 1.1.4.3 skrll #if defined(_KERNEL_OPT)
49 1.1.4.2 skrll #include "bpfilter.h"
50 1.1.4.3 skrll #endif
51 1.1.4.2 skrll
52 1.1.4.2 skrll #include <sys/param.h>
53 1.1.4.2 skrll #include <sys/systm.h>
54 1.1.4.2 skrll #include <sys/kernel.h>
55 1.1.4.2 skrll #include <sys/malloc.h>
56 1.1.4.2 skrll #include <sys/conf.h>
57 1.1.4.2 skrll #include <sys/device.h>
58 1.1.4.2 skrll #include <sys/file.h>
59 1.1.4.2 skrll #include <sys/filedesc.h>
60 1.1.4.2 skrll #include <sys/ksyms.h>
61 1.1.4.2 skrll #include <sys/poll.h>
62 1.1.4.2 skrll #include <sys/select.h>
63 1.1.4.2 skrll #include <sys/sockio.h>
64 1.1.4.2 skrll #include <sys/sysctl.h>
65 1.1.4.2 skrll
66 1.1.4.2 skrll #include <net/if.h>
67 1.1.4.2 skrll #include <net/if_dl.h>
68 1.1.4.2 skrll #include <net/if_ether.h>
69 1.1.4.2 skrll #include <net/if_media.h>
70 1.1.4.2 skrll #include <net/if_tap.h>
71 1.1.4.2 skrll #if NBPFILTER > 0
72 1.1.4.2 skrll #include <net/bpf.h>
73 1.1.4.2 skrll #endif
74 1.1.4.2 skrll
75 1.1.4.2 skrll /*
76 1.1.4.2 skrll * sysctl node management
77 1.1.4.2 skrll *
78 1.1.4.2 skrll * It's not really possible to use a SYSCTL_SETUP block with
79 1.1.4.2 skrll * current LKM implementation, so it is easier to just define
80 1.1.4.2 skrll * our own function.
81 1.1.4.2 skrll *
82 1.1.4.2 skrll * The handler function is a "helper" in Andrew Brown's sysctl
83 1.1.4.2 skrll * framework terminology. It is used as a gateway for sysctl
84 1.1.4.2 skrll * requests over the nodes.
85 1.1.4.2 skrll *
86 1.1.4.2 skrll * tap_log allows the module to log creations of nodes and
87 1.1.4.2 skrll * destroy them all at once using sysctl_teardown.
88 1.1.4.2 skrll */
89 1.1.4.2 skrll static int tap_node;
90 1.1.4.2 skrll static int tap_sysctl_handler(SYSCTLFN_PROTO);
91 1.1.4.3 skrll SYSCTL_SETUP_PROTO(sysctl_tap_setup);
92 1.1.4.2 skrll
93 1.1.4.2 skrll /*
94 1.1.4.2 skrll * Since we're an Ethernet device, we need the 3 following
95 1.1.4.2 skrll * components: a leading struct device, a struct ethercom,
96 1.1.4.2 skrll * and also a struct ifmedia since we don't attach a PHY to
97 1.1.4.2 skrll * ourselves. We could emulate one, but there's no real
98 1.1.4.2 skrll * point.
99 1.1.4.2 skrll */
100 1.1.4.2 skrll
101 1.1.4.2 skrll struct tap_softc {
102 1.1.4.2 skrll struct device sc_dev;
103 1.1.4.2 skrll struct ifmedia sc_im;
104 1.1.4.2 skrll struct ethercom sc_ec;
105 1.1.4.2 skrll int sc_flags;
106 1.1.4.2 skrll #define TAP_INUSE 0x00000001 /* tap device can only be opened once */
107 1.1.4.2 skrll #define TAP_ASYNCIO 0x00000002 /* user is using async I/O (SIGIO) on the device */
108 1.1.4.2 skrll #define TAP_NBIO 0x00000004 /* user wants calls to avoid blocking */
109 1.1.4.2 skrll #define TAP_GOING 0x00000008 /* interface is being destroyed */
110 1.1.4.2 skrll struct selinfo sc_rsel;
111 1.1.4.2 skrll pid_t sc_pgid; /* For async. IO */
112 1.1.4.2 skrll struct lock sc_rdlock;
113 1.1.4.2 skrll struct simplelock sc_kqlock;
114 1.1.4.2 skrll };
115 1.1.4.2 skrll
116 1.1.4.2 skrll /* autoconf(9) glue */
117 1.1.4.2 skrll
118 1.1.4.2 skrll void tapattach(int);
119 1.1.4.2 skrll
120 1.1.4.2 skrll static int tap_match(struct device *, struct cfdata *, void *);
121 1.1.4.2 skrll static void tap_attach(struct device *, struct device *, void *);
122 1.1.4.2 skrll static int tap_detach(struct device*, int);
123 1.1.4.2 skrll
124 1.1.4.2 skrll /* Ethernet address helper functions */
125 1.1.4.2 skrll
126 1.1.4.2 skrll static char *tap_ether_sprintf(char *, const u_char *);
127 1.1.4.2 skrll static int tap_ether_aton(u_char *, char *);
128 1.1.4.2 skrll
129 1.1.4.2 skrll CFATTACH_DECL(tap, sizeof(struct tap_softc),
130 1.1.4.2 skrll tap_match, tap_attach, tap_detach, NULL);
131 1.1.4.2 skrll extern struct cfdriver tap_cd;
132 1.1.4.2 skrll
133 1.1.4.2 skrll /* Real device access routines */
134 1.1.4.2 skrll static int tap_dev_close(struct tap_softc *);
135 1.1.4.2 skrll static int tap_dev_read(int, struct uio *, int);
136 1.1.4.2 skrll static int tap_dev_write(int, struct uio *, int);
137 1.1.4.3 skrll static int tap_dev_ioctl(int, u_long, caddr_t, struct lwp *);
138 1.1.4.3 skrll static int tap_dev_poll(int, int, struct lwp *);
139 1.1.4.2 skrll static int tap_dev_kqfilter(int, struct knote *);
140 1.1.4.2 skrll
141 1.1.4.2 skrll /* Fileops access routines */
142 1.1.4.3 skrll static int tap_fops_close(struct file *, struct lwp *);
143 1.1.4.2 skrll static int tap_fops_read(struct file *, off_t *, struct uio *,
144 1.1.4.2 skrll struct ucred *, int);
145 1.1.4.2 skrll static int tap_fops_write(struct file *, off_t *, struct uio *,
146 1.1.4.2 skrll struct ucred *, int);
147 1.1.4.2 skrll static int tap_fops_ioctl(struct file *, u_long, void *,
148 1.1.4.3 skrll struct lwp *);
149 1.1.4.3 skrll static int tap_fops_poll(struct file *, int, struct lwp *);
150 1.1.4.2 skrll static int tap_fops_kqfilter(struct file *, struct knote *);
151 1.1.4.2 skrll
152 1.1.4.2 skrll static const struct fileops tap_fileops = {
153 1.1.4.2 skrll tap_fops_read,
154 1.1.4.2 skrll tap_fops_write,
155 1.1.4.2 skrll tap_fops_ioctl,
156 1.1.4.2 skrll fnullop_fcntl,
157 1.1.4.2 skrll tap_fops_poll,
158 1.1.4.2 skrll fbadop_stat,
159 1.1.4.2 skrll tap_fops_close,
160 1.1.4.2 skrll tap_fops_kqfilter,
161 1.1.4.2 skrll };
162 1.1.4.2 skrll
163 1.1.4.2 skrll /* Helper for cloning open() */
164 1.1.4.3 skrll static int tap_dev_cloner(struct lwp *);
165 1.1.4.2 skrll
166 1.1.4.2 skrll /* Character device routines */
167 1.1.4.3 skrll static int tap_cdev_open(dev_t, int, int, struct lwp *);
168 1.1.4.3 skrll static int tap_cdev_close(dev_t, int, int, struct lwp *);
169 1.1.4.2 skrll static int tap_cdev_read(dev_t, struct uio *, int);
170 1.1.4.2 skrll static int tap_cdev_write(dev_t, struct uio *, int);
171 1.1.4.3 skrll static int tap_cdev_ioctl(dev_t, u_long, caddr_t, int, struct lwp *);
172 1.1.4.3 skrll static int tap_cdev_poll(dev_t, int, struct lwp *);
173 1.1.4.2 skrll static int tap_cdev_kqfilter(dev_t, struct knote *);
174 1.1.4.2 skrll
175 1.1.4.2 skrll const struct cdevsw tap_cdevsw = {
176 1.1.4.2 skrll tap_cdev_open, tap_cdev_close,
177 1.1.4.2 skrll tap_cdev_read, tap_cdev_write,
178 1.1.4.2 skrll tap_cdev_ioctl, nostop, notty,
179 1.1.4.2 skrll tap_cdev_poll, nommap,
180 1.1.4.2 skrll tap_cdev_kqfilter,
181 1.1.4.2 skrll };
182 1.1.4.2 skrll
183 1.1.4.2 skrll #define TAP_CLONER 0xfffff /* Maximal minor value */
184 1.1.4.2 skrll
185 1.1.4.2 skrll /* kqueue-related routines */
186 1.1.4.2 skrll static void tap_kqdetach(struct knote *);
187 1.1.4.2 skrll static int tap_kqread(struct knote *, long);
188 1.1.4.2 skrll
189 1.1.4.2 skrll /*
190 1.1.4.2 skrll * Those are needed by the if_media interface.
191 1.1.4.2 skrll */
192 1.1.4.2 skrll
193 1.1.4.2 skrll static int tap_mediachange(struct ifnet *);
194 1.1.4.2 skrll static void tap_mediastatus(struct ifnet *, struct ifmediareq *);
195 1.1.4.2 skrll
196 1.1.4.2 skrll /*
197 1.1.4.2 skrll * Those are needed by the ifnet interface, and would typically be
198 1.1.4.2 skrll * there for any network interface driver.
199 1.1.4.2 skrll * Some other routines are optional: watchdog and drain.
200 1.1.4.2 skrll */
201 1.1.4.2 skrll
202 1.1.4.2 skrll static void tap_start(struct ifnet *);
203 1.1.4.2 skrll static void tap_stop(struct ifnet *, int);
204 1.1.4.2 skrll static int tap_init(struct ifnet *);
205 1.1.4.2 skrll static int tap_ioctl(struct ifnet *, u_long, caddr_t);
206 1.1.4.2 skrll
207 1.1.4.2 skrll /* This is an internal function to keep tap_ioctl readable */
208 1.1.4.2 skrll static int tap_lifaddr(struct ifnet *, u_long, struct ifaliasreq *);
209 1.1.4.2 skrll
210 1.1.4.2 skrll /*
211 1.1.4.2 skrll * tap is a clonable interface, although it is highly unrealistic for
212 1.1.4.2 skrll * an Ethernet device.
213 1.1.4.2 skrll *
214 1.1.4.2 skrll * Here are the bits needed for a clonable interface.
215 1.1.4.2 skrll */
216 1.1.4.2 skrll static int tap_clone_create(struct if_clone *, int);
217 1.1.4.2 skrll static int tap_clone_destroy(struct ifnet *);
218 1.1.4.2 skrll
219 1.1.4.2 skrll struct if_clone tap_cloners = IF_CLONE_INITIALIZER("tap",
220 1.1.4.2 skrll tap_clone_create,
221 1.1.4.2 skrll tap_clone_destroy);
222 1.1.4.2 skrll
223 1.1.4.2 skrll /* Helper functionis shared by the two cloning code paths */
224 1.1.4.2 skrll static struct tap_softc * tap_clone_creator(int);
225 1.1.4.2 skrll static int tap_clone_destroyer(struct device *);
226 1.1.4.2 skrll
227 1.1.4.2 skrll void
228 1.1.4.2 skrll tapattach(int n)
229 1.1.4.2 skrll {
230 1.1.4.2 skrll int error;
231 1.1.4.2 skrll
232 1.1.4.2 skrll error = config_cfattach_attach(tap_cd.cd_name, &tap_ca);
233 1.1.4.2 skrll if (error) {
234 1.1.4.2 skrll aprint_error("%s: unable to register cfattach\n",
235 1.1.4.2 skrll tap_cd.cd_name);
236 1.1.4.2 skrll (void)config_cfdriver_detach(&tap_cd);
237 1.1.4.2 skrll return;
238 1.1.4.2 skrll }
239 1.1.4.2 skrll
240 1.1.4.2 skrll if_clone_attach(&tap_cloners);
241 1.1.4.2 skrll }
242 1.1.4.2 skrll
243 1.1.4.2 skrll /* Pretty much useless for a pseudo-device */
244 1.1.4.2 skrll static int
245 1.1.4.2 skrll tap_match(struct device *self, struct cfdata *cfdata, void *arg)
246 1.1.4.2 skrll {
247 1.1.4.2 skrll return (1);
248 1.1.4.2 skrll }
249 1.1.4.2 skrll
250 1.1.4.2 skrll void
251 1.1.4.2 skrll tap_attach(struct device *parent, struct device *self, void *aux)
252 1.1.4.2 skrll {
253 1.1.4.2 skrll struct tap_softc *sc = (struct tap_softc *)self;
254 1.1.4.2 skrll struct ifnet *ifp;
255 1.1.4.2 skrll u_int8_t enaddr[ETHER_ADDR_LEN] =
256 1.1.4.2 skrll { 0xf0, 0x0b, 0xa4, 0xff, 0xff, 0xff };
257 1.1.4.2 skrll char enaddrstr[18];
258 1.1.4.2 skrll uint32_t ui;
259 1.1.4.2 skrll int error;
260 1.1.4.2 skrll struct sysctlnode *node;
261 1.1.4.2 skrll
262 1.1.4.2 skrll aprint_normal("%s: faking Ethernet device\n",
263 1.1.4.2 skrll self->dv_xname);
264 1.1.4.2 skrll
265 1.1.4.2 skrll /*
266 1.1.4.2 skrll * In order to obtain unique initial Ethernet address on a host,
267 1.1.4.2 skrll * do some randomisation using mono_time. It's not meant for anything
268 1.1.4.2 skrll * but avoiding hard-coding an address.
269 1.1.4.2 skrll */
270 1.1.4.2 skrll ui = (mono_time.tv_sec ^ mono_time.tv_usec) & 0xffffff;
271 1.1.4.2 skrll memcpy(enaddr+3, (u_int8_t *)&ui, 3);
272 1.1.4.2 skrll
273 1.1.4.2 skrll aprint_normal("%s: Ethernet address %s\n", sc->sc_dev.dv_xname,
274 1.1.4.2 skrll tap_ether_sprintf(enaddrstr, enaddr));
275 1.1.4.2 skrll
276 1.1.4.2 skrll /*
277 1.1.4.2 skrll * Why 1000baseT? Why not? You can add more.
278 1.1.4.2 skrll *
279 1.1.4.2 skrll * Note that there are 3 steps: init, one or several additions to
280 1.1.4.2 skrll * list of supported media, and in the end, the selection of one
281 1.1.4.2 skrll * of them.
282 1.1.4.2 skrll */
283 1.1.4.2 skrll ifmedia_init(&sc->sc_im, 0, tap_mediachange, tap_mediastatus);
284 1.1.4.2 skrll ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_1000_T, 0, NULL);
285 1.1.4.2 skrll ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_1000_T|IFM_FDX, 0, NULL);
286 1.1.4.2 skrll ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_100_TX, 0, NULL);
287 1.1.4.2 skrll ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_100_TX|IFM_FDX, 0, NULL);
288 1.1.4.2 skrll ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_10_T, 0, NULL);
289 1.1.4.2 skrll ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_10_T|IFM_FDX, 0, NULL);
290 1.1.4.2 skrll ifmedia_add(&sc->sc_im, IFM_ETHER|IFM_AUTO, 0, NULL);
291 1.1.4.2 skrll ifmedia_set(&sc->sc_im, IFM_ETHER|IFM_AUTO);
292 1.1.4.2 skrll
293 1.1.4.2 skrll /*
294 1.1.4.2 skrll * One should note that an interface must do multicast in order
295 1.1.4.2 skrll * to support IPv6.
296 1.1.4.2 skrll */
297 1.1.4.2 skrll ifp = &sc->sc_ec.ec_if;
298 1.1.4.2 skrll strcpy(ifp->if_xname, sc->sc_dev.dv_xname);
299 1.1.4.2 skrll ifp->if_softc = sc;
300 1.1.4.2 skrll ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
301 1.1.4.2 skrll ifp->if_ioctl = tap_ioctl;
302 1.1.4.2 skrll ifp->if_start = tap_start;
303 1.1.4.2 skrll ifp->if_stop = tap_stop;
304 1.1.4.2 skrll ifp->if_init = tap_init;
305 1.1.4.2 skrll IFQ_SET_READY(&ifp->if_snd);
306 1.1.4.2 skrll
307 1.1.4.2 skrll sc->sc_ec.ec_capabilities = ETHERCAP_VLAN_MTU | ETHERCAP_JUMBO_MTU;
308 1.1.4.2 skrll
309 1.1.4.2 skrll /* Those steps are mandatory for an Ethernet driver, the fisrt call
310 1.1.4.2 skrll * being common to all network interface drivers. */
311 1.1.4.2 skrll if_attach(ifp);
312 1.1.4.2 skrll ether_ifattach(ifp, enaddr);
313 1.1.4.2 skrll
314 1.1.4.2 skrll sc->sc_flags = 0;
315 1.1.4.2 skrll
316 1.1.4.2 skrll /*
317 1.1.4.2 skrll * Add a sysctl node for that interface.
318 1.1.4.2 skrll *
319 1.1.4.2 skrll * The pointer transmitted is not a string, but instead a pointer to
320 1.1.4.2 skrll * the softc structure, which we can use to build the string value on
321 1.1.4.2 skrll * the fly in the helper function of the node. See the comments for
322 1.1.4.2 skrll * tap_sysctl_handler for details.
323 1.1.4.2 skrll */
324 1.1.4.2 skrll if ((error = sysctl_createv(NULL, 0, NULL,
325 1.1.4.2 skrll &node, CTLFLAG_READWRITE,
326 1.1.4.2 skrll CTLTYPE_STRING, sc->sc_dev.dv_xname, NULL,
327 1.1.4.2 skrll tap_sysctl_handler, 0, sc, 18,
328 1.1.4.3 skrll CTL_NET, AF_LINK, tap_node, sc->sc_dev.dv_unit, CTL_EOL)) != 0)
329 1.1.4.2 skrll aprint_error("%s: sysctl_createv returned %d, ignoring\n",
330 1.1.4.2 skrll sc->sc_dev.dv_xname, error);
331 1.1.4.2 skrll
332 1.1.4.2 skrll /*
333 1.1.4.2 skrll * Initialize the two locks for the device.
334 1.1.4.2 skrll *
335 1.1.4.2 skrll * We need a lock here because even though the tap device can be
336 1.1.4.2 skrll * opened only once, the file descriptor might be passed to another
337 1.1.4.2 skrll * process, say a fork(2)ed child.
338 1.1.4.2 skrll *
339 1.1.4.2 skrll * The Giant saves us from most of the hassle, but since the read
340 1.1.4.2 skrll * operation can sleep, we don't want two processes to wake up at
341 1.1.4.2 skrll * the same moment and both try and dequeue a single packet.
342 1.1.4.2 skrll *
343 1.1.4.2 skrll * The queue for event listeners (used by kqueue(9), see below) has
344 1.1.4.2 skrll * to be protected, too, but we don't need the same level of
345 1.1.4.2 skrll * complexity for that lock, so a simple spinning lock is fine.
346 1.1.4.2 skrll */
347 1.1.4.2 skrll lockinit(&sc->sc_rdlock, PSOCK|PCATCH, "tapl", 0, LK_SLEEPFAIL);
348 1.1.4.2 skrll simple_lock_init(&sc->sc_kqlock);
349 1.1.4.2 skrll }
350 1.1.4.2 skrll
351 1.1.4.2 skrll /*
352 1.1.4.2 skrll * When detaching, we do the inverse of what is done in the attach
353 1.1.4.2 skrll * routine, in reversed order.
354 1.1.4.2 skrll */
355 1.1.4.2 skrll static int
356 1.1.4.2 skrll tap_detach(struct device* self, int flags)
357 1.1.4.2 skrll {
358 1.1.4.2 skrll struct tap_softc *sc = (struct tap_softc *)self;
359 1.1.4.2 skrll struct ifnet *ifp = &sc->sc_ec.ec_if;
360 1.1.4.2 skrll int error, s;
361 1.1.4.2 skrll
362 1.1.4.2 skrll /*
363 1.1.4.2 skrll * Some processes might be sleeping on "tap", so we have to make
364 1.1.4.2 skrll * them release their hold on the device.
365 1.1.4.2 skrll *
366 1.1.4.2 skrll * The LK_DRAIN operation will wait for every locked process to
367 1.1.4.2 skrll * release their hold.
368 1.1.4.2 skrll */
369 1.1.4.2 skrll sc->sc_flags |= TAP_GOING;
370 1.1.4.2 skrll s = splnet();
371 1.1.4.2 skrll tap_stop(ifp, 1);
372 1.1.4.2 skrll if_down(ifp);
373 1.1.4.2 skrll splx(s);
374 1.1.4.2 skrll lockmgr(&sc->sc_rdlock, LK_DRAIN, NULL);
375 1.1.4.2 skrll
376 1.1.4.2 skrll /*
377 1.1.4.2 skrll * Destroying a single leaf is a very straightforward operation using
378 1.1.4.2 skrll * sysctl_destroyv. One should be sure to always end the path with
379 1.1.4.2 skrll * CTL_EOL.
380 1.1.4.2 skrll */
381 1.1.4.3 skrll if ((error = sysctl_destroyv(NULL, CTL_NET, AF_LINK, tap_node,
382 1.1.4.2 skrll sc->sc_dev.dv_unit, CTL_EOL)) != 0)
383 1.1.4.2 skrll aprint_error("%s: sysctl_destroyv returned %d, ignoring\n",
384 1.1.4.2 skrll sc->sc_dev.dv_xname, error);
385 1.1.4.2 skrll ether_ifdetach(ifp);
386 1.1.4.2 skrll if_detach(ifp);
387 1.1.4.2 skrll ifmedia_delete_instance(&sc->sc_im, IFM_INST_ANY);
388 1.1.4.2 skrll
389 1.1.4.2 skrll return (0);
390 1.1.4.2 skrll }
391 1.1.4.2 skrll
392 1.1.4.2 skrll /*
393 1.1.4.2 skrll * This function is called by the ifmedia layer to notify the driver
394 1.1.4.2 skrll * that the user requested a media change. A real driver would
395 1.1.4.2 skrll * reconfigure the hardware.
396 1.1.4.2 skrll */
397 1.1.4.2 skrll static int
398 1.1.4.2 skrll tap_mediachange(struct ifnet *ifp)
399 1.1.4.2 skrll {
400 1.1.4.2 skrll return (0);
401 1.1.4.2 skrll }
402 1.1.4.2 skrll
403 1.1.4.2 skrll /*
404 1.1.4.2 skrll * Here the user asks for the currently used media.
405 1.1.4.2 skrll */
406 1.1.4.2 skrll static void
407 1.1.4.2 skrll tap_mediastatus(struct ifnet *ifp, struct ifmediareq *imr)
408 1.1.4.2 skrll {
409 1.1.4.2 skrll struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
410 1.1.4.2 skrll imr->ifm_active = sc->sc_im.ifm_cur->ifm_media;
411 1.1.4.2 skrll }
412 1.1.4.2 skrll
413 1.1.4.2 skrll /*
414 1.1.4.2 skrll * This is the function where we SEND packets.
415 1.1.4.2 skrll *
416 1.1.4.2 skrll * There is no 'receive' equivalent. A typical driver will get
417 1.1.4.2 skrll * interrupts from the hardware, and from there will inject new packets
418 1.1.4.2 skrll * into the network stack.
419 1.1.4.2 skrll *
420 1.1.4.2 skrll * Once handled, a packet must be freed. A real driver might not be able
421 1.1.4.2 skrll * to fit all the pending packets into the hardware, and is allowed to
422 1.1.4.2 skrll * return before having sent all the packets. It should then use the
423 1.1.4.2 skrll * if_flags flag IFF_OACTIVE to notify the upper layer.
424 1.1.4.2 skrll *
425 1.1.4.2 skrll * There are also other flags one should check, such as IFF_PAUSE.
426 1.1.4.2 skrll *
427 1.1.4.2 skrll * It is our duty to make packets available to BPF listeners.
428 1.1.4.2 skrll *
429 1.1.4.2 skrll * You should be aware that this function is called by the Ethernet layer
430 1.1.4.2 skrll * at splnet().
431 1.1.4.2 skrll *
432 1.1.4.2 skrll * When the device is opened, we have to pass the packet(s) to the
433 1.1.4.2 skrll * userland. For that we stay in OACTIVE mode while the userland gets
434 1.1.4.2 skrll * the packets, and we send a signal to the processes waiting to read.
435 1.1.4.2 skrll *
436 1.1.4.2 skrll * wakeup(sc) is the counterpart to the tsleep call in
437 1.1.4.2 skrll * tap_dev_read, while selnotify() is used for kevent(2) and
438 1.1.4.2 skrll * poll(2) (which includes select(2)) listeners.
439 1.1.4.2 skrll */
440 1.1.4.2 skrll static void
441 1.1.4.2 skrll tap_start(struct ifnet *ifp)
442 1.1.4.2 skrll {
443 1.1.4.2 skrll struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
444 1.1.4.2 skrll struct mbuf *m0;
445 1.1.4.2 skrll
446 1.1.4.2 skrll if ((sc->sc_flags & TAP_INUSE) == 0) {
447 1.1.4.2 skrll /* Simply drop packets */
448 1.1.4.2 skrll for(;;) {
449 1.1.4.2 skrll IFQ_DEQUEUE(&ifp->if_snd, m0);
450 1.1.4.2 skrll if (m0 == NULL)
451 1.1.4.2 skrll return;
452 1.1.4.2 skrll
453 1.1.4.2 skrll ifp->if_opackets++;
454 1.1.4.2 skrll #if NBPFILTER > 0
455 1.1.4.2 skrll if (ifp->if_bpf)
456 1.1.4.2 skrll bpf_mtap(ifp->if_bpf, m0);
457 1.1.4.2 skrll #endif
458 1.1.4.2 skrll
459 1.1.4.2 skrll m_freem(m0);
460 1.1.4.2 skrll }
461 1.1.4.2 skrll } else if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
462 1.1.4.2 skrll ifp->if_flags |= IFF_OACTIVE;
463 1.1.4.2 skrll wakeup(sc);
464 1.1.4.2 skrll selnotify(&sc->sc_rsel, 1);
465 1.1.4.2 skrll if (sc->sc_flags & TAP_ASYNCIO)
466 1.1.4.2 skrll fownsignal(sc->sc_pgid, SIGIO, POLL_IN,
467 1.1.4.2 skrll POLLIN|POLLRDNORM, NULL);
468 1.1.4.2 skrll }
469 1.1.4.2 skrll }
470 1.1.4.2 skrll
471 1.1.4.2 skrll /*
472 1.1.4.2 skrll * A typical driver will only contain the following handlers for
473 1.1.4.2 skrll * ioctl calls, except SIOCSIFPHYADDR.
474 1.1.4.2 skrll * The latter is a hack I used to set the Ethernet address of the
475 1.1.4.2 skrll * faked device.
476 1.1.4.2 skrll *
477 1.1.4.2 skrll * Note that both ifmedia_ioctl() and ether_ioctl() have to be
478 1.1.4.2 skrll * called under splnet().
479 1.1.4.2 skrll */
480 1.1.4.2 skrll static int
481 1.1.4.2 skrll tap_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
482 1.1.4.2 skrll {
483 1.1.4.2 skrll struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
484 1.1.4.2 skrll struct ifreq *ifr = (struct ifreq *)data;
485 1.1.4.2 skrll int s, error;
486 1.1.4.2 skrll
487 1.1.4.2 skrll s = splnet();
488 1.1.4.2 skrll
489 1.1.4.2 skrll switch (cmd) {
490 1.1.4.2 skrll case SIOCSIFMEDIA:
491 1.1.4.2 skrll case SIOCGIFMEDIA:
492 1.1.4.2 skrll error = ifmedia_ioctl(ifp, ifr, &sc->sc_im, cmd);
493 1.1.4.2 skrll break;
494 1.1.4.2 skrll case SIOCSIFPHYADDR:
495 1.1.4.2 skrll error = tap_lifaddr(ifp, cmd, (struct ifaliasreq *)data);
496 1.1.4.2 skrll break;
497 1.1.4.2 skrll default:
498 1.1.4.2 skrll error = ether_ioctl(ifp, cmd, data);
499 1.1.4.2 skrll if (error == ENETRESET)
500 1.1.4.2 skrll error = 0;
501 1.1.4.2 skrll break;
502 1.1.4.2 skrll }
503 1.1.4.2 skrll
504 1.1.4.2 skrll splx(s);
505 1.1.4.2 skrll
506 1.1.4.2 skrll return (error);
507 1.1.4.2 skrll }
508 1.1.4.2 skrll
509 1.1.4.2 skrll /*
510 1.1.4.2 skrll * Helper function to set Ethernet address. This shouldn't be done there,
511 1.1.4.2 skrll * and should actually be available to all Ethernet drivers, real or not.
512 1.1.4.2 skrll */
513 1.1.4.2 skrll static int
514 1.1.4.2 skrll tap_lifaddr(struct ifnet *ifp, u_long cmd, struct ifaliasreq *ifra)
515 1.1.4.2 skrll {
516 1.1.4.2 skrll struct sockaddr *sa = (struct sockaddr *)&ifra->ifra_addr;
517 1.1.4.2 skrll
518 1.1.4.2 skrll if (sa->sa_family != AF_LINK)
519 1.1.4.2 skrll return (EINVAL);
520 1.1.4.2 skrll
521 1.1.4.2 skrll memcpy(LLADDR(ifp->if_sadl), sa->sa_data, ETHER_ADDR_LEN);
522 1.1.4.2 skrll
523 1.1.4.2 skrll return (0);
524 1.1.4.2 skrll }
525 1.1.4.2 skrll
526 1.1.4.2 skrll /*
527 1.1.4.2 skrll * _init() would typically be called when an interface goes up,
528 1.1.4.2 skrll * meaning it should configure itself into the state in which it
529 1.1.4.2 skrll * can send packets.
530 1.1.4.2 skrll */
531 1.1.4.2 skrll static int
532 1.1.4.2 skrll tap_init(struct ifnet *ifp)
533 1.1.4.2 skrll {
534 1.1.4.2 skrll ifp->if_flags |= IFF_RUNNING;
535 1.1.4.2 skrll
536 1.1.4.2 skrll tap_start(ifp);
537 1.1.4.2 skrll
538 1.1.4.2 skrll return (0);
539 1.1.4.2 skrll }
540 1.1.4.2 skrll
541 1.1.4.2 skrll /*
542 1.1.4.2 skrll * _stop() is called when an interface goes down. It is our
543 1.1.4.2 skrll * responsability to validate that state by clearing the
544 1.1.4.2 skrll * IFF_RUNNING flag.
545 1.1.4.2 skrll *
546 1.1.4.2 skrll * We have to wake up all the sleeping processes to have the pending
547 1.1.4.2 skrll * read requests cancelled.
548 1.1.4.2 skrll */
549 1.1.4.2 skrll static void
550 1.1.4.2 skrll tap_stop(struct ifnet *ifp, int disable)
551 1.1.4.2 skrll {
552 1.1.4.2 skrll struct tap_softc *sc = (struct tap_softc *)ifp->if_softc;
553 1.1.4.2 skrll
554 1.1.4.2 skrll ifp->if_flags &= ~IFF_RUNNING;
555 1.1.4.2 skrll wakeup(sc);
556 1.1.4.2 skrll selnotify(&sc->sc_rsel, 1);
557 1.1.4.2 skrll if (sc->sc_flags & TAP_ASYNCIO)
558 1.1.4.2 skrll fownsignal(sc->sc_pgid, SIGIO, POLL_HUP, 0, NULL);
559 1.1.4.2 skrll }
560 1.1.4.2 skrll
561 1.1.4.2 skrll /*
562 1.1.4.2 skrll * The 'create' command of ifconfig can be used to create
563 1.1.4.2 skrll * any numbered instance of a given device. Thus we have to
564 1.1.4.2 skrll * make sure we have enough room in cd_devs to create the
565 1.1.4.2 skrll * user-specified instance. config_attach_pseudo will do this
566 1.1.4.2 skrll * for us.
567 1.1.4.2 skrll */
568 1.1.4.2 skrll static int
569 1.1.4.2 skrll tap_clone_create(struct if_clone *ifc, int unit)
570 1.1.4.2 skrll {
571 1.1.4.2 skrll if (tap_clone_creator(unit) == NULL) {
572 1.1.4.2 skrll aprint_error("%s%d: unable to attach an instance\n",
573 1.1.4.2 skrll tap_cd.cd_name, unit);
574 1.1.4.2 skrll return (ENXIO);
575 1.1.4.2 skrll }
576 1.1.4.2 skrll
577 1.1.4.2 skrll return (0);
578 1.1.4.2 skrll }
579 1.1.4.2 skrll
580 1.1.4.2 skrll /*
581 1.1.4.2 skrll * tap(4) can be cloned by two ways:
582 1.1.4.2 skrll * using 'ifconfig tap0 create', which will use the network
583 1.1.4.2 skrll * interface cloning API, and call tap_clone_create above.
584 1.1.4.2 skrll * opening the cloning device node, whose minor number is TAP_CLONER.
585 1.1.4.2 skrll * See below for an explanation on how this part work.
586 1.1.4.2 skrll *
587 1.1.4.2 skrll * config_attach_pseudo can be called with unit = DVUNIT_ANY to have
588 1.1.4.2 skrll * autoconf(9) choose a unit number for us. This is what happens when
589 1.1.4.2 skrll * the cloner is openend, while the ifcloner interface creates a device
590 1.1.4.2 skrll * with a specific unit number.
591 1.1.4.2 skrll */
592 1.1.4.2 skrll static struct tap_softc *
593 1.1.4.2 skrll tap_clone_creator(int unit)
594 1.1.4.2 skrll {
595 1.1.4.2 skrll struct cfdata *cf;
596 1.1.4.2 skrll
597 1.1.4.2 skrll cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
598 1.1.4.2 skrll cf->cf_name = tap_cd.cd_name;
599 1.1.4.2 skrll cf->cf_atname = tap_ca.ca_name;
600 1.1.4.2 skrll cf->cf_unit = unit;
601 1.1.4.2 skrll cf->cf_fstate = FSTATE_STAR;
602 1.1.4.2 skrll
603 1.1.4.2 skrll return (struct tap_softc *)config_attach_pseudo(cf);
604 1.1.4.2 skrll }
605 1.1.4.2 skrll
606 1.1.4.2 skrll /*
607 1.1.4.2 skrll * The clean design of if_clone and autoconf(9) makes that part
608 1.1.4.2 skrll * really straightforward. The second argument of config_detach
609 1.1.4.2 skrll * means neither QUIET nor FORCED.
610 1.1.4.2 skrll */
611 1.1.4.2 skrll static int
612 1.1.4.2 skrll tap_clone_destroy(struct ifnet *ifp)
613 1.1.4.2 skrll {
614 1.1.4.2 skrll return tap_clone_destroyer((struct device *)ifp->if_softc);
615 1.1.4.2 skrll }
616 1.1.4.2 skrll
617 1.1.4.2 skrll static int
618 1.1.4.2 skrll tap_clone_destroyer(struct device *dev)
619 1.1.4.2 skrll {
620 1.1.4.2 skrll struct cfdata *cf = dev->dv_cfdata;
621 1.1.4.2 skrll int error;
622 1.1.4.2 skrll
623 1.1.4.2 skrll if ((error = config_detach(dev, 0)) != 0)
624 1.1.4.2 skrll aprint_error("%s: unable to detach instance\n",
625 1.1.4.2 skrll dev->dv_xname);
626 1.1.4.2 skrll free(cf, M_DEVBUF);
627 1.1.4.2 skrll
628 1.1.4.2 skrll return (error);
629 1.1.4.2 skrll }
630 1.1.4.2 skrll
631 1.1.4.2 skrll /*
632 1.1.4.2 skrll * tap(4) is a bit of an hybrid device. It can be used in two different
633 1.1.4.2 skrll * ways:
634 1.1.4.2 skrll * 1. ifconfig tapN create, then use /dev/tapN to read/write off it.
635 1.1.4.2 skrll * 2. open /dev/tap, get a new interface created and read/write off it.
636 1.1.4.2 skrll * That interface is destroyed when the process that had it created exits.
637 1.1.4.2 skrll *
638 1.1.4.2 skrll * The first way is managed by the cdevsw structure, and you access interfaces
639 1.1.4.2 skrll * through a (major, minor) mapping: tap4 is obtained by the minor number
640 1.1.4.2 skrll * 4. The entry points for the cdevsw interface are prefixed by tap_cdev_.
641 1.1.4.2 skrll *
642 1.1.4.2 skrll * The second way is the so-called "cloning" device. It's a special minor
643 1.1.4.2 skrll * number (chosen as the maximal number, to allow as much tap devices as
644 1.1.4.2 skrll * possible). The user first opens the cloner (e.g., /dev/tap), and that
645 1.1.4.2 skrll * call ends in tap_cdev_open. The actual place where it is handled is
646 1.1.4.2 skrll * tap_dev_cloner.
647 1.1.4.2 skrll *
648 1.1.4.2 skrll * An tap device cannot be opened more than once at a time, so the cdevsw
649 1.1.4.2 skrll * part of open() does nothing but noting that the interface is being used and
650 1.1.4.2 skrll * hence ready to actually handle packets.
651 1.1.4.2 skrll */
652 1.1.4.2 skrll
653 1.1.4.2 skrll static int
654 1.1.4.3 skrll tap_cdev_open(dev_t dev, int flags, int fmt, struct lwp *l)
655 1.1.4.2 skrll {
656 1.1.4.2 skrll struct tap_softc *sc;
657 1.1.4.2 skrll
658 1.1.4.2 skrll if (minor(dev) == TAP_CLONER)
659 1.1.4.3 skrll return tap_dev_cloner(l);
660 1.1.4.2 skrll
661 1.1.4.2 skrll sc = (struct tap_softc *)device_lookup(&tap_cd, minor(dev));
662 1.1.4.2 skrll if (sc == NULL)
663 1.1.4.2 skrll return (ENXIO);
664 1.1.4.2 skrll
665 1.1.4.2 skrll /* The device can only be opened once */
666 1.1.4.2 skrll if (sc->sc_flags & TAP_INUSE)
667 1.1.4.2 skrll return (EBUSY);
668 1.1.4.2 skrll sc->sc_flags |= TAP_INUSE;
669 1.1.4.2 skrll return (0);
670 1.1.4.2 skrll }
671 1.1.4.2 skrll
672 1.1.4.2 skrll /*
673 1.1.4.2 skrll * There are several kinds of cloning devices, and the most simple is the one
674 1.1.4.2 skrll * tap(4) uses. What it does is change the file descriptor with a new one,
675 1.1.4.2 skrll * with its own fileops structure (which maps to the various read, write,
676 1.1.4.2 skrll * ioctl functions). It starts allocating a new file descriptor with falloc,
677 1.1.4.2 skrll * then actually creates the new tap devices.
678 1.1.4.2 skrll *
679 1.1.4.2 skrll * Once those two steps are successful, we can re-wire the existing file
680 1.1.4.2 skrll * descriptor to its new self. This is done with fdclone(): it fills the fp
681 1.1.4.2 skrll * structure as needed (notably f_data gets filled with the fifth parameter
682 1.1.4.2 skrll * passed, the unit of the tap device which will allows us identifying the
683 1.1.4.2 skrll * device later), and returns EMOVEFD.
684 1.1.4.2 skrll *
685 1.1.4.2 skrll * That magic value is interpreted by sys_open() which then replaces the
686 1.1.4.2 skrll * current file descriptor by the new one (through a magic member of struct
687 1.1.4.2 skrll * proc, p_dupfd).
688 1.1.4.2 skrll *
689 1.1.4.2 skrll * The tap device is flagged as being busy since it otherwise could be
690 1.1.4.2 skrll * externally accessed through the corresponding device node with the cdevsw
691 1.1.4.2 skrll * interface.
692 1.1.4.2 skrll */
693 1.1.4.2 skrll
694 1.1.4.2 skrll static int
695 1.1.4.3 skrll tap_dev_cloner(struct lwp *l)
696 1.1.4.2 skrll {
697 1.1.4.2 skrll struct tap_softc *sc;
698 1.1.4.2 skrll struct file *fp;
699 1.1.4.2 skrll int error, fd;
700 1.1.4.2 skrll
701 1.1.4.3 skrll if ((error = falloc(l->l_proc, &fp, &fd)) != 0)
702 1.1.4.2 skrll return (error);
703 1.1.4.2 skrll
704 1.1.4.2 skrll if ((sc = tap_clone_creator(DVUNIT_ANY)) == NULL) {
705 1.1.4.3 skrll FILE_UNUSE(fp, l);
706 1.1.4.2 skrll ffree(fp);
707 1.1.4.2 skrll return (ENXIO);
708 1.1.4.2 skrll }
709 1.1.4.2 skrll
710 1.1.4.2 skrll sc->sc_flags |= TAP_INUSE;
711 1.1.4.2 skrll
712 1.1.4.3 skrll return fdclone(l, fp, fd, &tap_fileops, (void *)(intptr_t)sc->sc_dev.dv_unit);
713 1.1.4.2 skrll }
714 1.1.4.2 skrll
715 1.1.4.2 skrll /*
716 1.1.4.2 skrll * While all other operations (read, write, ioctl, poll and kqfilter) are
717 1.1.4.2 skrll * really the same whether we are in cdevsw or fileops mode, the close()
718 1.1.4.2 skrll * function is slightly different in the two cases.
719 1.1.4.2 skrll *
720 1.1.4.2 skrll * As for the other, the core of it is shared in tap_dev_close. What
721 1.1.4.2 skrll * it does is sufficient for the cdevsw interface, but the cloning interface
722 1.1.4.2 skrll * needs another thing: the interface is destroyed when the processes that
723 1.1.4.2 skrll * created it closes it.
724 1.1.4.2 skrll */
725 1.1.4.2 skrll static int
726 1.1.4.3 skrll tap_cdev_close(dev_t dev, int flags, int fmt, struct lwp *l)
727 1.1.4.2 skrll {
728 1.1.4.2 skrll struct tap_softc *sc =
729 1.1.4.2 skrll (struct tap_softc *)device_lookup(&tap_cd, minor(dev));
730 1.1.4.2 skrll
731 1.1.4.2 skrll if (sc == NULL)
732 1.1.4.2 skrll return (ENXIO);
733 1.1.4.2 skrll
734 1.1.4.2 skrll return tap_dev_close(sc);
735 1.1.4.2 skrll }
736 1.1.4.2 skrll
737 1.1.4.2 skrll /*
738 1.1.4.2 skrll * It might happen that the administrator used ifconfig to externally destroy
739 1.1.4.2 skrll * the interface. In that case, tap_fops_close will be called while
740 1.1.4.2 skrll * tap_detach is already happening. If we called it again from here, we
741 1.1.4.2 skrll * would dead lock. TAP_GOING ensures that this situation doesn't happen.
742 1.1.4.2 skrll */
743 1.1.4.2 skrll static int
744 1.1.4.3 skrll tap_fops_close(struct file *fp, struct lwp *l)
745 1.1.4.2 skrll {
746 1.1.4.2 skrll int unit = (intptr_t)fp->f_data;
747 1.1.4.2 skrll struct tap_softc *sc;
748 1.1.4.2 skrll int error;
749 1.1.4.2 skrll
750 1.1.4.2 skrll sc = (struct tap_softc *)device_lookup(&tap_cd, unit);
751 1.1.4.2 skrll if (sc == NULL)
752 1.1.4.2 skrll return (ENXIO);
753 1.1.4.2 skrll
754 1.1.4.2 skrll /* tap_dev_close currently always succeeds, but it might not
755 1.1.4.2 skrll * always be the case. */
756 1.1.4.2 skrll if ((error = tap_dev_close(sc)) != 0)
757 1.1.4.2 skrll return (error);
758 1.1.4.2 skrll
759 1.1.4.2 skrll /* Destroy the device now that it is no longer useful,
760 1.1.4.2 skrll * unless it's already being destroyed. */
761 1.1.4.2 skrll if ((sc->sc_flags & TAP_GOING) != 0)
762 1.1.4.2 skrll return (0);
763 1.1.4.2 skrll
764 1.1.4.2 skrll return tap_clone_destroyer((struct device *)sc);
765 1.1.4.2 skrll }
766 1.1.4.2 skrll
767 1.1.4.2 skrll static int
768 1.1.4.2 skrll tap_dev_close(struct tap_softc *sc)
769 1.1.4.2 skrll {
770 1.1.4.2 skrll struct ifnet *ifp;
771 1.1.4.2 skrll int s;
772 1.1.4.2 skrll
773 1.1.4.2 skrll s = splnet();
774 1.1.4.2 skrll /* Let tap_start handle packets again */
775 1.1.4.2 skrll ifp = &sc->sc_ec.ec_if;
776 1.1.4.2 skrll ifp->if_flags &= ~IFF_OACTIVE;
777 1.1.4.2 skrll
778 1.1.4.2 skrll /* Purge output queue */
779 1.1.4.2 skrll if (!(IFQ_IS_EMPTY(&ifp->if_snd))) {
780 1.1.4.2 skrll struct mbuf *m;
781 1.1.4.2 skrll
782 1.1.4.2 skrll for (;;) {
783 1.1.4.2 skrll IFQ_DEQUEUE(&ifp->if_snd, m);
784 1.1.4.2 skrll if (m == NULL)
785 1.1.4.2 skrll break;
786 1.1.4.2 skrll
787 1.1.4.2 skrll ifp->if_opackets++;
788 1.1.4.2 skrll #if NBPFILTER > 0
789 1.1.4.2 skrll if (ifp->if_bpf)
790 1.1.4.2 skrll bpf_mtap(ifp->if_bpf, m);
791 1.1.4.2 skrll #endif
792 1.1.4.2 skrll }
793 1.1.4.2 skrll }
794 1.1.4.2 skrll splx(s);
795 1.1.4.2 skrll
796 1.1.4.2 skrll sc->sc_flags &= ~(TAP_INUSE | TAP_ASYNCIO);
797 1.1.4.2 skrll
798 1.1.4.2 skrll return (0);
799 1.1.4.2 skrll }
800 1.1.4.2 skrll
801 1.1.4.2 skrll static int
802 1.1.4.2 skrll tap_cdev_read(dev_t dev, struct uio *uio, int flags)
803 1.1.4.2 skrll {
804 1.1.4.2 skrll return tap_dev_read(minor(dev), uio, flags);
805 1.1.4.2 skrll }
806 1.1.4.2 skrll
807 1.1.4.2 skrll static int
808 1.1.4.2 skrll tap_fops_read(struct file *fp, off_t *offp, struct uio *uio,
809 1.1.4.2 skrll struct ucred *cred, int flags)
810 1.1.4.2 skrll {
811 1.1.4.2 skrll return tap_dev_read((intptr_t)fp->f_data, uio, flags);
812 1.1.4.2 skrll }
813 1.1.4.2 skrll
814 1.1.4.2 skrll static int
815 1.1.4.2 skrll tap_dev_read(int unit, struct uio *uio, int flags)
816 1.1.4.2 skrll {
817 1.1.4.2 skrll struct tap_softc *sc =
818 1.1.4.2 skrll (struct tap_softc *)device_lookup(&tap_cd, unit);
819 1.1.4.2 skrll struct ifnet *ifp;
820 1.1.4.2 skrll struct mbuf *m, *n;
821 1.1.4.2 skrll int error = 0, s;
822 1.1.4.2 skrll
823 1.1.4.2 skrll if (sc == NULL)
824 1.1.4.2 skrll return (ENXIO);
825 1.1.4.2 skrll
826 1.1.4.2 skrll ifp = &sc->sc_ec.ec_if;
827 1.1.4.2 skrll if ((ifp->if_flags & IFF_UP) == 0)
828 1.1.4.2 skrll return (EHOSTDOWN);
829 1.1.4.2 skrll
830 1.1.4.2 skrll /*
831 1.1.4.2 skrll * In the TAP_NBIO case, we have to make sure we won't be sleeping
832 1.1.4.2 skrll */
833 1.1.4.2 skrll if ((sc->sc_flags & TAP_NBIO) &&
834 1.1.4.2 skrll lockstatus(&sc->sc_rdlock) == LK_EXCLUSIVE)
835 1.1.4.2 skrll return (EWOULDBLOCK);
836 1.1.4.2 skrll error = lockmgr(&sc->sc_rdlock, LK_EXCLUSIVE, NULL);
837 1.1.4.2 skrll if (error != 0)
838 1.1.4.2 skrll return (error);
839 1.1.4.2 skrll
840 1.1.4.2 skrll s = splnet();
841 1.1.4.2 skrll if (IFQ_IS_EMPTY(&ifp->if_snd)) {
842 1.1.4.2 skrll ifp->if_flags &= ~IFF_OACTIVE;
843 1.1.4.2 skrll splx(s);
844 1.1.4.2 skrll /*
845 1.1.4.2 skrll * We must release the lock before sleeping, and re-acquire it
846 1.1.4.2 skrll * after.
847 1.1.4.2 skrll */
848 1.1.4.2 skrll (void)lockmgr(&sc->sc_rdlock, LK_RELEASE, NULL);
849 1.1.4.2 skrll if (sc->sc_flags & TAP_NBIO)
850 1.1.4.2 skrll error = EWOULDBLOCK;
851 1.1.4.2 skrll else
852 1.1.4.2 skrll error = tsleep(sc, PSOCK|PCATCH, "tap", 0);
853 1.1.4.2 skrll
854 1.1.4.2 skrll if (error != 0)
855 1.1.4.2 skrll return (error);
856 1.1.4.2 skrll /* The device might have been downed */
857 1.1.4.2 skrll if ((ifp->if_flags & IFF_UP) == 0)
858 1.1.4.2 skrll return (EHOSTDOWN);
859 1.1.4.2 skrll if ((sc->sc_flags & TAP_NBIO) &&
860 1.1.4.2 skrll lockstatus(&sc->sc_rdlock) == LK_EXCLUSIVE)
861 1.1.4.2 skrll return (EWOULDBLOCK);
862 1.1.4.2 skrll error = lockmgr(&sc->sc_rdlock, LK_EXCLUSIVE, NULL);
863 1.1.4.2 skrll if (error != 0)
864 1.1.4.2 skrll return (error);
865 1.1.4.2 skrll s = splnet();
866 1.1.4.2 skrll }
867 1.1.4.2 skrll
868 1.1.4.2 skrll IFQ_DEQUEUE(&ifp->if_snd, m);
869 1.1.4.2 skrll ifp->if_flags &= ~IFF_OACTIVE;
870 1.1.4.2 skrll splx(s);
871 1.1.4.2 skrll if (m == NULL) {
872 1.1.4.2 skrll error = 0;
873 1.1.4.2 skrll goto out;
874 1.1.4.2 skrll }
875 1.1.4.2 skrll
876 1.1.4.2 skrll ifp->if_opackets++;
877 1.1.4.2 skrll #if NBPFILTER > 0
878 1.1.4.2 skrll if (ifp->if_bpf)
879 1.1.4.2 skrll bpf_mtap(ifp->if_bpf, m);
880 1.1.4.2 skrll #endif
881 1.1.4.2 skrll
882 1.1.4.2 skrll /*
883 1.1.4.2 skrll * One read is one packet.
884 1.1.4.2 skrll */
885 1.1.4.2 skrll do {
886 1.1.4.2 skrll error = uiomove(mtod(m, caddr_t),
887 1.1.4.2 skrll min(m->m_len, uio->uio_resid), uio);
888 1.1.4.2 skrll MFREE(m, n);
889 1.1.4.2 skrll m = n;
890 1.1.4.2 skrll } while (m != NULL && uio->uio_resid > 0 && error == 0);
891 1.1.4.2 skrll
892 1.1.4.2 skrll if (m != NULL)
893 1.1.4.2 skrll m_freem(m);
894 1.1.4.2 skrll
895 1.1.4.2 skrll out:
896 1.1.4.2 skrll (void)lockmgr(&sc->sc_rdlock, LK_RELEASE, NULL);
897 1.1.4.2 skrll return (error);
898 1.1.4.2 skrll }
899 1.1.4.2 skrll
900 1.1.4.2 skrll static int
901 1.1.4.2 skrll tap_cdev_write(dev_t dev, struct uio *uio, int flags)
902 1.1.4.2 skrll {
903 1.1.4.2 skrll return tap_dev_write(minor(dev), uio, flags);
904 1.1.4.2 skrll }
905 1.1.4.2 skrll
906 1.1.4.2 skrll static int
907 1.1.4.2 skrll tap_fops_write(struct file *fp, off_t *offp, struct uio *uio,
908 1.1.4.2 skrll struct ucred *cred, int flags)
909 1.1.4.2 skrll {
910 1.1.4.2 skrll return tap_dev_write((intptr_t)fp->f_data, uio, flags);
911 1.1.4.2 skrll }
912 1.1.4.2 skrll
913 1.1.4.2 skrll static int
914 1.1.4.2 skrll tap_dev_write(int unit, struct uio *uio, int flags)
915 1.1.4.2 skrll {
916 1.1.4.2 skrll struct tap_softc *sc =
917 1.1.4.2 skrll (struct tap_softc *)device_lookup(&tap_cd, unit);
918 1.1.4.2 skrll struct ifnet *ifp;
919 1.1.4.2 skrll struct mbuf *m, **mp;
920 1.1.4.2 skrll int error = 0;
921 1.1.4.2 skrll
922 1.1.4.2 skrll if (sc == NULL)
923 1.1.4.2 skrll return (ENXIO);
924 1.1.4.2 skrll
925 1.1.4.2 skrll ifp = &sc->sc_ec.ec_if;
926 1.1.4.2 skrll
927 1.1.4.2 skrll /* One write, one packet, that's the rule */
928 1.1.4.2 skrll MGETHDR(m, M_DONTWAIT, MT_DATA);
929 1.1.4.2 skrll if (m == NULL) {
930 1.1.4.2 skrll ifp->if_ierrors++;
931 1.1.4.2 skrll return (ENOBUFS);
932 1.1.4.2 skrll }
933 1.1.4.2 skrll m->m_pkthdr.len = uio->uio_resid;
934 1.1.4.2 skrll
935 1.1.4.2 skrll mp = &m;
936 1.1.4.2 skrll while (error == 0 && uio->uio_resid > 0) {
937 1.1.4.2 skrll if (*mp != m) {
938 1.1.4.2 skrll MGET(*mp, M_DONTWAIT, MT_DATA);
939 1.1.4.2 skrll if (*mp == NULL) {
940 1.1.4.2 skrll error = ENOBUFS;
941 1.1.4.2 skrll break;
942 1.1.4.2 skrll }
943 1.1.4.2 skrll }
944 1.1.4.2 skrll (*mp)->m_len = min(MHLEN, uio->uio_resid);
945 1.1.4.2 skrll error = uiomove(mtod(*mp, caddr_t), (*mp)->m_len, uio);
946 1.1.4.2 skrll mp = &(*mp)->m_next;
947 1.1.4.2 skrll }
948 1.1.4.2 skrll if (error) {
949 1.1.4.2 skrll ifp->if_ierrors++;
950 1.1.4.2 skrll m_freem(m);
951 1.1.4.2 skrll return (error);
952 1.1.4.2 skrll }
953 1.1.4.2 skrll
954 1.1.4.2 skrll ifp->if_ipackets++;
955 1.1.4.2 skrll m->m_pkthdr.rcvif = ifp;
956 1.1.4.2 skrll
957 1.1.4.2 skrll #if NBPFILTER > 0
958 1.1.4.2 skrll if (ifp->if_bpf)
959 1.1.4.2 skrll bpf_mtap(ifp->if_bpf, m);
960 1.1.4.2 skrll #endif
961 1.1.4.2 skrll (*ifp->if_input)(ifp, m);
962 1.1.4.2 skrll
963 1.1.4.2 skrll return (0);
964 1.1.4.2 skrll }
965 1.1.4.2 skrll
966 1.1.4.2 skrll static int
967 1.1.4.2 skrll tap_cdev_ioctl(dev_t dev, u_long cmd, caddr_t data, int flags,
968 1.1.4.3 skrll struct lwp *l)
969 1.1.4.2 skrll {
970 1.1.4.3 skrll return tap_dev_ioctl(minor(dev), cmd, data, l);
971 1.1.4.2 skrll }
972 1.1.4.2 skrll
973 1.1.4.2 skrll static int
974 1.1.4.3 skrll tap_fops_ioctl(struct file *fp, u_long cmd, void *data, struct lwp *l)
975 1.1.4.2 skrll {
976 1.1.4.3 skrll return tap_dev_ioctl((intptr_t)fp->f_data, cmd, (caddr_t)data, l);
977 1.1.4.2 skrll }
978 1.1.4.2 skrll
979 1.1.4.2 skrll static int
980 1.1.4.3 skrll tap_dev_ioctl(int unit, u_long cmd, caddr_t data, struct lwp *l)
981 1.1.4.2 skrll {
982 1.1.4.2 skrll struct tap_softc *sc =
983 1.1.4.2 skrll (struct tap_softc *)device_lookup(&tap_cd, unit);
984 1.1.4.2 skrll int error = 0;
985 1.1.4.2 skrll
986 1.1.4.2 skrll if (sc == NULL)
987 1.1.4.2 skrll return (ENXIO);
988 1.1.4.2 skrll
989 1.1.4.2 skrll switch (cmd) {
990 1.1.4.2 skrll case FIONREAD:
991 1.1.4.2 skrll {
992 1.1.4.2 skrll struct ifnet *ifp = &sc->sc_ec.ec_if;
993 1.1.4.2 skrll struct mbuf *m;
994 1.1.4.2 skrll int s;
995 1.1.4.2 skrll
996 1.1.4.2 skrll s = splnet();
997 1.1.4.2 skrll IFQ_POLL(&ifp->if_snd, m);
998 1.1.4.2 skrll
999 1.1.4.2 skrll if (m == NULL)
1000 1.1.4.2 skrll *(int *)data = 0;
1001 1.1.4.2 skrll else
1002 1.1.4.2 skrll *(int *)data = m->m_pkthdr.len;
1003 1.1.4.2 skrll splx(s);
1004 1.1.4.2 skrll } break;
1005 1.1.4.2 skrll case TIOCSPGRP:
1006 1.1.4.2 skrll case FIOSETOWN:
1007 1.1.4.3 skrll error = fsetown(l->l_proc, &sc->sc_pgid, cmd, data);
1008 1.1.4.2 skrll break;
1009 1.1.4.2 skrll case TIOCGPGRP:
1010 1.1.4.2 skrll case FIOGETOWN:
1011 1.1.4.3 skrll error = fgetown(l->l_proc, sc->sc_pgid, cmd, data);
1012 1.1.4.2 skrll break;
1013 1.1.4.2 skrll case FIOASYNC:
1014 1.1.4.2 skrll if (*(int *)data)
1015 1.1.4.2 skrll sc->sc_flags |= TAP_ASYNCIO;
1016 1.1.4.2 skrll else
1017 1.1.4.2 skrll sc->sc_flags &= ~TAP_ASYNCIO;
1018 1.1.4.2 skrll break;
1019 1.1.4.2 skrll case FIONBIO:
1020 1.1.4.2 skrll if (*(int *)data)
1021 1.1.4.2 skrll sc->sc_flags |= TAP_NBIO;
1022 1.1.4.2 skrll else
1023 1.1.4.2 skrll sc->sc_flags &= ~TAP_NBIO;
1024 1.1.4.2 skrll break;
1025 1.1.4.2 skrll case TAPGIFNAME:
1026 1.1.4.2 skrll {
1027 1.1.4.2 skrll struct ifreq *ifr = (struct ifreq *)data;
1028 1.1.4.2 skrll struct ifnet *ifp = &sc->sc_ec.ec_if;
1029 1.1.4.2 skrll
1030 1.1.4.2 skrll strlcpy(ifr->ifr_name, ifp->if_xname, IFNAMSIZ);
1031 1.1.4.2 skrll } break;
1032 1.1.4.2 skrll default:
1033 1.1.4.2 skrll error = ENOTTY;
1034 1.1.4.2 skrll break;
1035 1.1.4.2 skrll }
1036 1.1.4.2 skrll
1037 1.1.4.2 skrll return (0);
1038 1.1.4.2 skrll }
1039 1.1.4.2 skrll
1040 1.1.4.2 skrll static int
1041 1.1.4.3 skrll tap_cdev_poll(dev_t dev, int events, struct lwp *l)
1042 1.1.4.2 skrll {
1043 1.1.4.3 skrll return tap_dev_poll(minor(dev), events, l);
1044 1.1.4.2 skrll }
1045 1.1.4.2 skrll
1046 1.1.4.2 skrll static int
1047 1.1.4.3 skrll tap_fops_poll(struct file *fp, int events, struct lwp *l)
1048 1.1.4.2 skrll {
1049 1.1.4.3 skrll return tap_dev_poll((intptr_t)fp->f_data, events, l);
1050 1.1.4.2 skrll }
1051 1.1.4.2 skrll
1052 1.1.4.2 skrll static int
1053 1.1.4.3 skrll tap_dev_poll(int unit, int events, struct lwp *l)
1054 1.1.4.2 skrll {
1055 1.1.4.2 skrll struct tap_softc *sc =
1056 1.1.4.2 skrll (struct tap_softc *)device_lookup(&tap_cd, unit);
1057 1.1.4.2 skrll int revents = 0;
1058 1.1.4.2 skrll
1059 1.1.4.2 skrll if (sc == NULL)
1060 1.1.4.2 skrll return (ENXIO);
1061 1.1.4.2 skrll
1062 1.1.4.2 skrll if (events & (POLLIN|POLLRDNORM)) {
1063 1.1.4.2 skrll struct ifnet *ifp = &sc->sc_ec.ec_if;
1064 1.1.4.2 skrll struct mbuf *m;
1065 1.1.4.2 skrll int s;
1066 1.1.4.2 skrll
1067 1.1.4.2 skrll s = splnet();
1068 1.1.4.2 skrll IFQ_POLL(&ifp->if_snd, m);
1069 1.1.4.2 skrll splx(s);
1070 1.1.4.2 skrll
1071 1.1.4.2 skrll if (m != NULL)
1072 1.1.4.2 skrll revents |= events & (POLLIN|POLLRDNORM);
1073 1.1.4.2 skrll else {
1074 1.1.4.4 skrll simple_lock(&sc->sc_kqlock);
1075 1.1.4.3 skrll selrecord(l, &sc->sc_rsel);
1076 1.1.4.2 skrll simple_unlock(&sc->sc_kqlock);
1077 1.1.4.2 skrll }
1078 1.1.4.2 skrll }
1079 1.1.4.2 skrll revents |= events & (POLLOUT|POLLWRNORM);
1080 1.1.4.2 skrll
1081 1.1.4.2 skrll return (revents);
1082 1.1.4.2 skrll }
1083 1.1.4.2 skrll
1084 1.1.4.2 skrll static struct filterops tap_read_filterops = { 1, NULL, tap_kqdetach,
1085 1.1.4.2 skrll tap_kqread };
1086 1.1.4.2 skrll static struct filterops tap_seltrue_filterops = { 1, NULL, tap_kqdetach,
1087 1.1.4.2 skrll filt_seltrue };
1088 1.1.4.2 skrll
1089 1.1.4.2 skrll static int
1090 1.1.4.2 skrll tap_cdev_kqfilter(dev_t dev, struct knote *kn)
1091 1.1.4.2 skrll {
1092 1.1.4.2 skrll return tap_dev_kqfilter(minor(dev), kn);
1093 1.1.4.2 skrll }
1094 1.1.4.2 skrll
1095 1.1.4.2 skrll static int
1096 1.1.4.2 skrll tap_fops_kqfilter(struct file *fp, struct knote *kn)
1097 1.1.4.2 skrll {
1098 1.1.4.2 skrll return tap_dev_kqfilter((intptr_t)fp->f_data, kn);
1099 1.1.4.2 skrll }
1100 1.1.4.2 skrll
1101 1.1.4.2 skrll static int
1102 1.1.4.2 skrll tap_dev_kqfilter(int unit, struct knote *kn)
1103 1.1.4.2 skrll {
1104 1.1.4.2 skrll struct tap_softc *sc =
1105 1.1.4.2 skrll (struct tap_softc *)device_lookup(&tap_cd, unit);
1106 1.1.4.2 skrll
1107 1.1.4.2 skrll if (sc == NULL)
1108 1.1.4.2 skrll return (ENXIO);
1109 1.1.4.2 skrll
1110 1.1.4.2 skrll switch(kn->kn_filter) {
1111 1.1.4.2 skrll case EVFILT_READ:
1112 1.1.4.2 skrll kn->kn_fop = &tap_read_filterops;
1113 1.1.4.2 skrll break;
1114 1.1.4.2 skrll case EVFILT_WRITE:
1115 1.1.4.2 skrll kn->kn_fop = &tap_seltrue_filterops;
1116 1.1.4.2 skrll break;
1117 1.1.4.2 skrll default:
1118 1.1.4.2 skrll return (1);
1119 1.1.4.2 skrll }
1120 1.1.4.2 skrll
1121 1.1.4.2 skrll kn->kn_hook = sc;
1122 1.1.4.4 skrll simple_lock(&sc->sc_kqlock);
1123 1.1.4.2 skrll SLIST_INSERT_HEAD(&sc->sc_rsel.sel_klist, kn, kn_selnext);
1124 1.1.4.2 skrll simple_unlock(&sc->sc_kqlock);
1125 1.1.4.2 skrll return (0);
1126 1.1.4.2 skrll }
1127 1.1.4.2 skrll
1128 1.1.4.2 skrll static void
1129 1.1.4.2 skrll tap_kqdetach(struct knote *kn)
1130 1.1.4.2 skrll {
1131 1.1.4.2 skrll struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
1132 1.1.4.2 skrll
1133 1.1.4.4 skrll simple_lock(&sc->sc_kqlock);
1134 1.1.4.2 skrll SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
1135 1.1.4.2 skrll simple_unlock(&sc->sc_kqlock);
1136 1.1.4.2 skrll }
1137 1.1.4.2 skrll
1138 1.1.4.2 skrll static int
1139 1.1.4.2 skrll tap_kqread(struct knote *kn, long hint)
1140 1.1.4.2 skrll {
1141 1.1.4.2 skrll struct tap_softc *sc = (struct tap_softc *)kn->kn_hook;
1142 1.1.4.2 skrll struct ifnet *ifp = &sc->sc_ec.ec_if;
1143 1.1.4.2 skrll struct mbuf *m;
1144 1.1.4.2 skrll int s;
1145 1.1.4.2 skrll
1146 1.1.4.2 skrll s = splnet();
1147 1.1.4.2 skrll IFQ_POLL(&ifp->if_snd, m);
1148 1.1.4.2 skrll
1149 1.1.4.2 skrll if (m == NULL)
1150 1.1.4.2 skrll kn->kn_data = 0;
1151 1.1.4.2 skrll else
1152 1.1.4.2 skrll kn->kn_data = m->m_pkthdr.len;
1153 1.1.4.2 skrll splx(s);
1154 1.1.4.2 skrll return (kn->kn_data != 0 ? 1 : 0);
1155 1.1.4.2 skrll }
1156 1.1.4.2 skrll
1157 1.1.4.2 skrll /*
1158 1.1.4.2 skrll * sysctl management routines
1159 1.1.4.2 skrll * You can set the address of an interface through:
1160 1.1.4.2 skrll * net.link.tap.tap<number>
1161 1.1.4.2 skrll *
1162 1.1.4.2 skrll * Note the consistent use of tap_log in order to use
1163 1.1.4.2 skrll * sysctl_teardown at unload time.
1164 1.1.4.2 skrll *
1165 1.1.4.2 skrll * In the kernel you will find a lot of SYSCTL_SETUP blocks. Those
1166 1.1.4.2 skrll * blocks register a function in a special section of the kernel
1167 1.1.4.2 skrll * (called a link set) which is used at init_sysctl() time to cycle
1168 1.1.4.2 skrll * through all those functions to create the kernel's sysctl tree.
1169 1.1.4.2 skrll *
1170 1.1.4.2 skrll * It is not (currently) possible to use link sets in a LKM, so the
1171 1.1.4.2 skrll * easiest is to simply call our own setup routine at load time.
1172 1.1.4.2 skrll *
1173 1.1.4.2 skrll * In the SYSCTL_SETUP blocks you find in the kernel, nodes have the
1174 1.1.4.2 skrll * CTLFLAG_PERMANENT flag, meaning they cannot be removed. Once the
1175 1.1.4.2 skrll * whole kernel sysctl tree is built, it is not possible to add any
1176 1.1.4.2 skrll * permanent node.
1177 1.1.4.2 skrll *
1178 1.1.4.2 skrll * It should be noted that we're not saving the sysctlnode pointer
1179 1.1.4.2 skrll * we are returned when creating the "tap" node. That structure
1180 1.1.4.2 skrll * cannot be trusted once out of the calling function, as it might
1181 1.1.4.2 skrll * get reused. So we just save the MIB number, and always give the
1182 1.1.4.2 skrll * full path starting from the root for later calls to sysctl_createv
1183 1.1.4.2 skrll * and sysctl_destroyv.
1184 1.1.4.2 skrll */
1185 1.1.4.2 skrll SYSCTL_SETUP(sysctl_tap_setup, "sysctl net.link.tap subtree setup")
1186 1.1.4.2 skrll {
1187 1.1.4.2 skrll struct sysctlnode *node;
1188 1.1.4.2 skrll int error = 0;
1189 1.1.4.2 skrll
1190 1.1.4.2 skrll if ((error = sysctl_createv(clog, 0, NULL, NULL,
1191 1.1.4.2 skrll CTLFLAG_PERMANENT,
1192 1.1.4.2 skrll CTLTYPE_NODE, "net", NULL,
1193 1.1.4.2 skrll NULL, 0, NULL, 0,
1194 1.1.4.2 skrll CTL_NET, CTL_EOL)) != 0)
1195 1.1.4.2 skrll return;
1196 1.1.4.2 skrll
1197 1.1.4.2 skrll if ((error = sysctl_createv(clog, 0, NULL, NULL,
1198 1.1.4.2 skrll CTLFLAG_PERMANENT,
1199 1.1.4.2 skrll CTLTYPE_NODE, "link", NULL,
1200 1.1.4.2 skrll NULL, 0, NULL, 0,
1201 1.1.4.3 skrll CTL_NET, AF_LINK, CTL_EOL)) != 0)
1202 1.1.4.2 skrll return;
1203 1.1.4.2 skrll
1204 1.1.4.2 skrll /*
1205 1.1.4.2 skrll * The first four parameters of sysctl_createv are for management.
1206 1.1.4.2 skrll *
1207 1.1.4.2 skrll * The four that follows, here starting with a '0' for the flags,
1208 1.1.4.2 skrll * describe the node.
1209 1.1.4.2 skrll *
1210 1.1.4.2 skrll * The next series of four set its value, through various possible
1211 1.1.4.2 skrll * means.
1212 1.1.4.2 skrll *
1213 1.1.4.2 skrll * Last but not least, the path to the node is described. That path
1214 1.1.4.2 skrll * is relative to the given root (third argument). Here we're
1215 1.1.4.2 skrll * starting from the root.
1216 1.1.4.2 skrll */
1217 1.1.4.2 skrll if ((error = sysctl_createv(clog, 0, NULL, &node,
1218 1.1.4.2 skrll CTLFLAG_PERMANENT,
1219 1.1.4.2 skrll CTLTYPE_NODE, "tap", NULL,
1220 1.1.4.2 skrll NULL, 0, NULL, 0,
1221 1.1.4.3 skrll CTL_NET, AF_LINK, CTL_CREATE, CTL_EOL)) != 0)
1222 1.1.4.2 skrll return;
1223 1.1.4.2 skrll tap_node = node->sysctl_num;
1224 1.1.4.2 skrll }
1225 1.1.4.2 skrll
1226 1.1.4.2 skrll /*
1227 1.1.4.2 skrll * The helper functions make Andrew Brown's interface really
1228 1.1.4.2 skrll * shine. It makes possible to create value on the fly whether
1229 1.1.4.2 skrll * the sysctl value is read or written.
1230 1.1.4.2 skrll *
1231 1.1.4.2 skrll * As shown as an example in the man page, the first step is to
1232 1.1.4.2 skrll * create a copy of the node to have sysctl_lookup work on it.
1233 1.1.4.2 skrll *
1234 1.1.4.2 skrll * Here, we have more work to do than just a copy, since we have
1235 1.1.4.2 skrll * to create the string. The first step is to collect the actual
1236 1.1.4.2 skrll * value of the node, which is a convenient pointer to the softc
1237 1.1.4.2 skrll * of the interface. From there we create the string and use it
1238 1.1.4.2 skrll * as the value, but only for the *copy* of the node.
1239 1.1.4.2 skrll *
1240 1.1.4.2 skrll * Then we let sysctl_lookup do the magic, which consists in
1241 1.1.4.2 skrll * setting oldp and newp as required by the operation. When the
1242 1.1.4.2 skrll * value is read, that means that the string will be copied to
1243 1.1.4.2 skrll * the user, and when it is written, the new value will be copied
1244 1.1.4.2 skrll * over in the addr array.
1245 1.1.4.2 skrll *
1246 1.1.4.2 skrll * If newp is NULL, the user was reading the value, so we don't
1247 1.1.4.2 skrll * have anything else to do. If a new value was written, we
1248 1.1.4.2 skrll * have to check it.
1249 1.1.4.2 skrll *
1250 1.1.4.2 skrll * If it is incorrect, we can return an error and leave 'node' as
1251 1.1.4.2 skrll * it is: since it is a copy of the actual node, the change will
1252 1.1.4.2 skrll * be forgotten.
1253 1.1.4.2 skrll *
1254 1.1.4.2 skrll * Upon a correct input, we commit the change to the ifnet
1255 1.1.4.2 skrll * structure of our interface.
1256 1.1.4.2 skrll */
1257 1.1.4.2 skrll static int
1258 1.1.4.2 skrll tap_sysctl_handler(SYSCTLFN_ARGS)
1259 1.1.4.2 skrll {
1260 1.1.4.2 skrll struct sysctlnode node;
1261 1.1.4.2 skrll struct tap_softc *sc;
1262 1.1.4.2 skrll struct ifnet *ifp;
1263 1.1.4.2 skrll int error;
1264 1.1.4.2 skrll size_t len;
1265 1.1.4.2 skrll char addr[18];
1266 1.1.4.2 skrll
1267 1.1.4.2 skrll node = *rnode;
1268 1.1.4.2 skrll sc = node.sysctl_data;
1269 1.1.4.2 skrll ifp = &sc->sc_ec.ec_if;
1270 1.1.4.2 skrll (void)tap_ether_sprintf(addr, LLADDR(ifp->if_sadl));
1271 1.1.4.2 skrll node.sysctl_data = addr;
1272 1.1.4.2 skrll error = sysctl_lookup(SYSCTLFN_CALL(&node));
1273 1.1.4.2 skrll if (error || newp == NULL)
1274 1.1.4.2 skrll return (error);
1275 1.1.4.2 skrll
1276 1.1.4.2 skrll len = strlen(addr);
1277 1.1.4.2 skrll if (len < 11 || len > 17)
1278 1.1.4.2 skrll return (EINVAL);
1279 1.1.4.2 skrll
1280 1.1.4.2 skrll /* Commit change */
1281 1.1.4.2 skrll if (tap_ether_aton(LLADDR(ifp->if_sadl), addr) != 0)
1282 1.1.4.2 skrll return (EINVAL);
1283 1.1.4.2 skrll return (error);
1284 1.1.4.2 skrll }
1285 1.1.4.2 skrll
1286 1.1.4.2 skrll /*
1287 1.1.4.2 skrll * ether_aton implementation, not using a static buffer.
1288 1.1.4.2 skrll */
1289 1.1.4.2 skrll static int
1290 1.1.4.2 skrll tap_ether_aton(u_char *dest, char *str)
1291 1.1.4.2 skrll {
1292 1.1.4.2 skrll int i;
1293 1.1.4.2 skrll char *cp = str;
1294 1.1.4.2 skrll u_char val[6];
1295 1.1.4.2 skrll
1296 1.1.4.2 skrll #define set_value \
1297 1.1.4.2 skrll if (*cp > '9' && *cp < 'a') \
1298 1.1.4.2 skrll *cp -= 'A' - 10; \
1299 1.1.4.2 skrll else if (*cp > '9') \
1300 1.1.4.2 skrll *cp -= 'a' - 10; \
1301 1.1.4.2 skrll else \
1302 1.1.4.2 skrll *cp -= '0'
1303 1.1.4.2 skrll
1304 1.1.4.2 skrll for (i = 0; i < 6; i++, cp++) {
1305 1.1.4.2 skrll if (!isxdigit(*cp))
1306 1.1.4.2 skrll return (1);
1307 1.1.4.2 skrll set_value;
1308 1.1.4.2 skrll val[i] = *cp++;
1309 1.1.4.2 skrll if (isxdigit(*cp)) {
1310 1.1.4.2 skrll set_value;
1311 1.1.4.2 skrll val[i] *= 16;
1312 1.1.4.2 skrll val[i] += *cp++;
1313 1.1.4.2 skrll }
1314 1.1.4.2 skrll if (*cp == ':' || i == 5)
1315 1.1.4.2 skrll continue;
1316 1.1.4.2 skrll else
1317 1.1.4.2 skrll return (1);
1318 1.1.4.2 skrll }
1319 1.1.4.2 skrll memcpy(dest, val, 6);
1320 1.1.4.2 skrll return (0);
1321 1.1.4.2 skrll }
1322 1.1.4.2 skrll
1323 1.1.4.2 skrll /*
1324 1.1.4.2 skrll * ether_sprintf made thread-safer.
1325 1.1.4.2 skrll *
1326 1.1.4.2 skrll * Copied over from sys/net/if_ethersubr.c, with a change to avoid the use
1327 1.1.4.2 skrll * of a static buffer.
1328 1.1.4.2 skrll */
1329 1.1.4.2 skrll
1330 1.1.4.2 skrll /*
1331 1.1.4.2 skrll * Copyright (c) 1982, 1989, 1993
1332 1.1.4.2 skrll * The Regents of the University of California. All rights reserved.
1333 1.1.4.2 skrll *
1334 1.1.4.2 skrll * Redistribution and use in source and binary forms, with or without
1335 1.1.4.2 skrll * modification, are permitted provided that the following conditions
1336 1.1.4.2 skrll * are met:
1337 1.1.4.2 skrll * 1. Redistributions of source code must retain the above copyright
1338 1.1.4.2 skrll * notice, this list of conditions and the following disclaimer.
1339 1.1.4.2 skrll * 2. Redistributions in binary form must reproduce the above copyright
1340 1.1.4.2 skrll * notice, this list of conditions and the following disclaimer in the
1341 1.1.4.2 skrll * documentation and/or other materials provided with the distribution.
1342 1.1.4.2 skrll * 3. Neither the name of the University nor the names of its contributors
1343 1.1.4.2 skrll * may be used to endorse or promote products derived from this software
1344 1.1.4.2 skrll * without specific prior written permission.
1345 1.1.4.2 skrll *
1346 1.1.4.2 skrll * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1347 1.1.4.2 skrll * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1348 1.1.4.2 skrll * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1349 1.1.4.2 skrll * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1350 1.1.4.2 skrll * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1351 1.1.4.2 skrll * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1352 1.1.4.2 skrll * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1353 1.1.4.2 skrll * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1354 1.1.4.2 skrll * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1355 1.1.4.2 skrll * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1356 1.1.4.2 skrll * SUCH DAMAGE.
1357 1.1.4.2 skrll *
1358 1.1.4.2 skrll * @(#)if_ethersubr.c 8.2 (Berkeley) 4/4/96
1359 1.1.4.2 skrll */
1360 1.1.4.2 skrll
1361 1.1.4.2 skrll static char digits[] = "0123456789abcdef";
1362 1.1.4.2 skrll static char *
1363 1.1.4.2 skrll tap_ether_sprintf(char *dest, const u_char *ap)
1364 1.1.4.2 skrll {
1365 1.1.4.2 skrll char *cp = dest;
1366 1.1.4.2 skrll int i;
1367 1.1.4.2 skrll
1368 1.1.4.2 skrll for (i = 0; i < 6; i++) {
1369 1.1.4.2 skrll *cp++ = digits[*ap >> 4];
1370 1.1.4.2 skrll *cp++ = digits[*ap++ & 0xf];
1371 1.1.4.2 skrll *cp++ = ':';
1372 1.1.4.2 skrll }
1373 1.1.4.2 skrll *--cp = 0;
1374 1.1.4.2 skrll return (dest);
1375 1.1.4.2 skrll }
1376