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