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