if_tun.c revision 1.73 1 1.73 peter /* $NetBSD: if_tun.c,v 1.73 2004/12/04 18:31:43 peter Exp $ */
2 1.14 cgd
3 1.1 cgd /*
4 1.8 deraadt * Copyright (c) 1988, Julian Onions <jpo (at) cs.nott.ac.uk>
5 1.8 deraadt * Nottingham University 1987.
6 1.1 cgd *
7 1.1 cgd * This source may be freely distributed, however I would be interested
8 1.1 cgd * in any changes that are made.
9 1.6 deraadt *
10 1.1 cgd * This driver takes packets off the IP i/f and hands them up to a
11 1.21 scottr * user process to have its wicked way with. This driver has its
12 1.1 cgd * roots in a similar driver written by Phil Cockcroft (formerly) at
13 1.27 mycroft * UCL. This driver is based much more on read/write/poll mode of
14 1.1 cgd * operation though.
15 1.1 cgd */
16 1.48 lukem
17 1.48 lukem #include <sys/cdefs.h>
18 1.73 peter __KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.73 2004/12/04 18:31:43 peter Exp $");
19 1.1 cgd
20 1.8 deraadt #include "tun.h"
21 1.33 jonathan
22 1.33 jonathan #include "opt_inet.h"
23 1.34 jonathan #include "opt_ns.h"
24 1.8 deraadt
25 1.6 deraadt #include <sys/param.h>
26 1.8 deraadt #include <sys/proc.h>
27 1.6 deraadt #include <sys/systm.h>
28 1.6 deraadt #include <sys/mbuf.h>
29 1.6 deraadt #include <sys/buf.h>
30 1.6 deraadt #include <sys/protosw.h>
31 1.6 deraadt #include <sys/socket.h>
32 1.6 deraadt #include <sys/ioctl.h>
33 1.6 deraadt #include <sys/errno.h>
34 1.6 deraadt #include <sys/syslog.h>
35 1.6 deraadt #include <sys/select.h>
36 1.27 mycroft #include <sys/poll.h>
37 1.8 deraadt #include <sys/file.h>
38 1.22 christos #include <sys/signalvar.h>
39 1.23 christos #include <sys/conf.h>
40 1.9 deraadt
41 1.9 deraadt #include <machine/cpu.h>
42 1.6 deraadt
43 1.6 deraadt #include <net/if.h>
44 1.69 tron #include <net/if_types.h>
45 1.6 deraadt #include <net/netisr.h>
46 1.6 deraadt #include <net/route.h>
47 1.1 cgd
48 1.30 is
49 1.1 cgd #ifdef INET
50 1.6 deraadt #include <netinet/in.h>
51 1.6 deraadt #include <netinet/in_systm.h>
52 1.6 deraadt #include <netinet/in_var.h>
53 1.6 deraadt #include <netinet/ip.h>
54 1.30 is #include <netinet/if_inarp.h>
55 1.1 cgd #endif
56 1.1 cgd
57 1.1 cgd #ifdef NS
58 1.6 deraadt #include <netns/ns.h>
59 1.6 deraadt #include <netns/ns_if.h>
60 1.1 cgd #endif
61 1.1 cgd
62 1.8 deraadt #include "bpfilter.h"
63 1.8 deraadt #if NBPFILTER > 0
64 1.8 deraadt #include <sys/time.h>
65 1.8 deraadt #include <net/bpf.h>
66 1.8 deraadt #endif
67 1.8 deraadt
68 1.8 deraadt #include <net/if_tun.h>
69 1.8 deraadt
70 1.29 christos #define TUNDEBUG if (tundebug) printf
71 1.6 deraadt int tundebug = 0;
72 1.1 cgd
73 1.6 deraadt extern int ifqmaxlen;
74 1.22 christos void tunattach __P((int));
75 1.46 atatat LIST_HEAD(, tun_softc) tun_softc_list;
76 1.70 pk LIST_HEAD(, tun_softc) tunz_softc_list;
77 1.46 atatat static struct simplelock tun_softc_lock;
78 1.6 deraadt
79 1.22 christos int tun_ioctl __P((struct ifnet *, u_long, caddr_t));
80 1.22 christos int tun_output __P((struct ifnet *, struct mbuf *, struct sockaddr *,
81 1.22 christos struct rtentry *rt));
82 1.46 atatat int tun_clone_create __P((struct if_clone *, int));
83 1.73 peter int tun_clone_destroy __P((struct ifnet *));
84 1.6 deraadt
85 1.46 atatat struct if_clone tun_cloner =
86 1.46 atatat IF_CLONE_INITIALIZER("tun", tun_clone_create, tun_clone_destroy);
87 1.46 atatat
88 1.46 atatat static void tunattach0 __P((struct tun_softc *));
89 1.26 pk static void tuninit __P((struct tun_softc *));
90 1.50 itojun #ifdef ALTQ
91 1.50 itojun static void tunstart __P((struct ifnet *));
92 1.50 itojun #endif
93 1.46 atatat static struct tun_softc *tun_find_unit __P((dev_t));
94 1.70 pk static struct tun_softc *tun_find_zunit __P((int));
95 1.53 gehenna
96 1.53 gehenna dev_type_open(tunopen);
97 1.53 gehenna dev_type_close(tunclose);
98 1.53 gehenna dev_type_read(tunread);
99 1.53 gehenna dev_type_write(tunwrite);
100 1.53 gehenna dev_type_ioctl(tunioctl);
101 1.53 gehenna dev_type_poll(tunpoll);
102 1.56 jdolecek dev_type_kqfilter(tunkqfilter);
103 1.53 gehenna
104 1.53 gehenna const struct cdevsw tun_cdevsw = {
105 1.53 gehenna tunopen, tunclose, tunread, tunwrite, tunioctl,
106 1.56 jdolecek nostop, notty, tunpoll, nommap, tunkqfilter,
107 1.53 gehenna };
108 1.6 deraadt
109 1.8 deraadt void
110 1.8 deraadt tunattach(unused)
111 1.8 deraadt int unused;
112 1.8 deraadt {
113 1.46 atatat
114 1.46 atatat simple_lock_init(&tun_softc_lock);
115 1.46 atatat LIST_INIT(&tun_softc_list);
116 1.70 pk LIST_INIT(&tunz_softc_list);
117 1.46 atatat if_clone_attach(&tun_cloner);
118 1.46 atatat }
119 1.46 atatat
120 1.70 pk /*
121 1.70 pk * Find driver instance from dev_t.
122 1.70 pk * Call at splnet().
123 1.70 pk * Returns with tp locked (if found).
124 1.70 pk */
125 1.70 pk static struct tun_softc *
126 1.70 pk tun_find_unit(dev)
127 1.70 pk dev_t dev;
128 1.70 pk {
129 1.70 pk struct tun_softc *tp;
130 1.70 pk int unit = minor(dev);
131 1.70 pk
132 1.70 pk simple_lock(&tun_softc_lock);
133 1.70 pk LIST_FOREACH(tp, &tun_softc_list, tun_list)
134 1.70 pk if (unit == tp->tun_unit)
135 1.70 pk break;
136 1.70 pk if (tp)
137 1.70 pk simple_lock(&tp->tun_lock);
138 1.70 pk simple_unlock(&tun_softc_lock);
139 1.70 pk
140 1.70 pk return (tp);
141 1.70 pk }
142 1.70 pk
143 1.70 pk /*
144 1.70 pk * Find zombie driver instance by unit number.
145 1.70 pk * Call at splnet().
146 1.70 pk * Remove tp from list and return it unlocked (if found).
147 1.70 pk */
148 1.70 pk static struct tun_softc *
149 1.70 pk tun_find_zunit(unit)
150 1.70 pk int unit;
151 1.70 pk {
152 1.70 pk struct tun_softc *tp;
153 1.70 pk
154 1.70 pk simple_lock(&tun_softc_lock);
155 1.70 pk LIST_FOREACH(tp, &tunz_softc_list, tun_list)
156 1.70 pk if (unit == tp->tun_unit)
157 1.70 pk break;
158 1.70 pk if (tp)
159 1.70 pk LIST_REMOVE(tp, tun_list);
160 1.70 pk simple_unlock(&tun_softc_lock);
161 1.70 pk #ifdef DIAGNOSTIC
162 1.70 pk if (tp != NULL && (tp->tun_flags & (TUN_INITED|TUN_OPEN)) != TUN_OPEN)
163 1.70 pk printf("tun%d: inconsistent flags: %x\n", unit, tp->tun_flags);
164 1.70 pk #endif
165 1.70 pk
166 1.70 pk return (tp);
167 1.70 pk }
168 1.70 pk
169 1.46 atatat int
170 1.46 atatat tun_clone_create(ifc, unit)
171 1.46 atatat struct if_clone *ifc;
172 1.46 atatat int unit;
173 1.46 atatat {
174 1.70 pk struct tun_softc *tp;
175 1.46 atatat
176 1.70 pk if ((tp = tun_find_zunit(unit)) == NULL) {
177 1.70 pk /* Allocate a new instance */
178 1.70 pk tp = malloc(sizeof(struct tun_softc), M_DEVBUF, M_WAITOK);
179 1.70 pk (void)memset(tp, 0, sizeof(struct tun_softc));
180 1.46 atatat
181 1.70 pk tp->tun_unit = unit;
182 1.70 pk simple_lock_init(&tp->tun_lock);
183 1.70 pk } else {
184 1.70 pk /* Revive tunnel instance; clear ifp part */
185 1.70 pk (void)memset(&tp->tun_if, 0, sizeof(struct ifnet));
186 1.70 pk }
187 1.46 atatat
188 1.70 pk (void)snprintf(tp->tun_if.if_xname, sizeof(tp->tun_if.if_xname),
189 1.70 pk "%s%d", ifc->ifc_name, unit);
190 1.70 pk tunattach0(tp);
191 1.70 pk tp->tun_flags |= TUN_INITED;
192 1.46 atatat
193 1.46 atatat simple_lock(&tun_softc_lock);
194 1.70 pk LIST_INSERT_HEAD(&tun_softc_list, tp, tun_list);
195 1.46 atatat simple_unlock(&tun_softc_lock);
196 1.46 atatat
197 1.46 atatat return (0);
198 1.46 atatat }
199 1.46 atatat
200 1.46 atatat void
201 1.70 pk tunattach0(tp)
202 1.70 pk struct tun_softc *tp;
203 1.46 atatat {
204 1.70 pk struct ifnet *ifp;
205 1.46 atatat
206 1.70 pk ifp = &tp->tun_if;
207 1.70 pk ifp->if_softc = tp;
208 1.46 atatat ifp->if_mtu = TUNMTU;
209 1.46 atatat ifp->if_ioctl = tun_ioctl;
210 1.46 atatat ifp->if_output = tun_output;
211 1.50 itojun #ifdef ALTQ
212 1.50 itojun ifp->if_start = tunstart;
213 1.50 itojun #endif
214 1.46 atatat ifp->if_flags = IFF_POINTOPOINT;
215 1.69 tron ifp->if_type = IFT_TUNNEL;
216 1.46 atatat ifp->if_snd.ifq_maxlen = ifqmaxlen;
217 1.46 atatat ifp->if_collisions = 0;
218 1.46 atatat ifp->if_ierrors = 0;
219 1.46 atatat ifp->if_oerrors = 0;
220 1.46 atatat ifp->if_ipackets = 0;
221 1.46 atatat ifp->if_opackets = 0;
222 1.58 jdolecek ifp->if_ibytes = 0;
223 1.58 jdolecek ifp->if_obytes = 0;
224 1.46 atatat ifp->if_dlt = DLT_NULL;
225 1.50 itojun IFQ_SET_READY(&ifp->if_snd);
226 1.46 atatat if_attach(ifp);
227 1.46 atatat if_alloc_sadl(ifp);
228 1.46 atatat #if NBPFILTER > 0
229 1.46 atatat bpfattach(ifp, DLT_NULL, sizeof(u_int32_t));
230 1.46 atatat #endif
231 1.46 atatat }
232 1.46 atatat
233 1.73 peter int
234 1.46 atatat tun_clone_destroy(ifp)
235 1.8 deraadt struct ifnet *ifp;
236 1.46 atatat {
237 1.46 atatat struct tun_softc *tp = (void *)ifp;
238 1.70 pk int s, zombie = 0;
239 1.8 deraadt
240 1.70 pk s = splnet();
241 1.46 atatat simple_lock(&tun_softc_lock);
242 1.46 atatat simple_lock(&tp->tun_lock);
243 1.46 atatat LIST_REMOVE(tp, tun_list);
244 1.70 pk if (tp->tun_flags & TUN_OPEN) {
245 1.70 pk /* Hang on to storage until last close */
246 1.70 pk zombie = 1;
247 1.70 pk tp->tun_flags &= ~TUN_INITED;
248 1.70 pk LIST_INSERT_HEAD(&tunz_softc_list, tp, tun_list);
249 1.70 pk }
250 1.46 atatat simple_unlock(&tun_softc_lock);
251 1.46 atatat
252 1.70 pk IF_PURGE(&ifp->if_snd);
253 1.70 pk ifp->if_flags &= ~IFF_RUNNING;
254 1.70 pk
255 1.46 atatat if (tp->tun_flags & TUN_RWAIT) {
256 1.46 atatat tp->tun_flags &= ~TUN_RWAIT;
257 1.46 atatat wakeup((caddr_t)tp);
258 1.46 atatat }
259 1.64 jdolecek if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
260 1.66 christos fownsignal(tp->tun_pgid, SIGIO, POLL_HUP, 0, NULL);
261 1.64 jdolecek
262 1.46 atatat selwakeup(&tp->tun_rsel);
263 1.8 deraadt
264 1.70 pk simple_unlock(&tp->tun_lock);
265 1.70 pk splx(s);
266 1.70 pk
267 1.8 deraadt #if NBPFILTER > 0
268 1.46 atatat bpfdetach(ifp);
269 1.8 deraadt #endif
270 1.46 atatat if_detach(ifp);
271 1.46 atatat
272 1.70 pk if (!zombie)
273 1.70 pk free(tp, M_DEVBUF);
274 1.73 peter
275 1.73 peter return (0);
276 1.8 deraadt }
277 1.8 deraadt
278 1.6 deraadt /*
279 1.6 deraadt * tunnel open - must be superuser & the device must be
280 1.6 deraadt * configured in
281 1.6 deraadt */
282 1.6 deraadt int
283 1.63 fvdl tunopen(dev, flag, mode, p)
284 1.6 deraadt dev_t dev;
285 1.6 deraadt int flag, mode;
286 1.63 fvdl struct proc *p;
287 1.6 deraadt {
288 1.6 deraadt struct ifnet *ifp;
289 1.8 deraadt struct tun_softc *tp;
290 1.70 pk int s, error;
291 1.1 cgd
292 1.22 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
293 1.5 deraadt return (error);
294 1.5 deraadt
295 1.46 atatat if (NTUN < 1)
296 1.6 deraadt return (ENXIO);
297 1.46 atatat
298 1.70 pk s = splnet();
299 1.46 atatat tp = tun_find_unit(dev);
300 1.52 atatat
301 1.70 pk if (tp == NULL) {
302 1.52 atatat (void)tun_clone_create(&tun_cloner, minor(dev));
303 1.52 atatat tp = tun_find_unit(dev);
304 1.70 pk if (tp == NULL) {
305 1.70 pk error = ENXIO;
306 1.70 pk goto out_nolock;
307 1.70 pk }
308 1.52 atatat }
309 1.46 atatat
310 1.46 atatat if (tp->tun_flags & TUN_OPEN) {
311 1.70 pk error = EBUSY;
312 1.70 pk goto out;
313 1.46 atatat }
314 1.46 atatat
315 1.6 deraadt ifp = &tp->tun_if;
316 1.6 deraadt tp->tun_flags |= TUN_OPEN;
317 1.24 thorpej TUNDEBUG("%s: open\n", ifp->if_xname);
318 1.70 pk out:
319 1.46 atatat simple_unlock(&tp->tun_lock);
320 1.70 pk out_nolock:
321 1.70 pk splx(s);
322 1.70 pk return (error);
323 1.1 cgd }
324 1.1 cgd
325 1.6 deraadt /*
326 1.6 deraadt * tunclose - close the device - mark i/f down & delete
327 1.6 deraadt * routing info
328 1.6 deraadt */
329 1.6 deraadt int
330 1.63 fvdl tunclose(dev, flag, mode, p)
331 1.6 deraadt dev_t dev;
332 1.6 deraadt int flag;
333 1.22 christos int mode;
334 1.63 fvdl struct proc *p;
335 1.6 deraadt {
336 1.46 atatat int s;
337 1.46 atatat struct tun_softc *tp;
338 1.46 atatat struct ifnet *ifp;
339 1.6 deraadt
340 1.70 pk s = splnet();
341 1.70 pk if ((tp = tun_find_zunit(minor(dev))) != NULL) {
342 1.70 pk /* interface was "destroyed" before the close */
343 1.70 pk free(tp, M_DEVBUF);
344 1.70 pk goto out_nolock;
345 1.70 pk }
346 1.46 atatat
347 1.70 pk if ((tp = tun_find_unit(dev)) == NULL)
348 1.70 pk goto out_nolock;
349 1.46 atatat
350 1.46 atatat ifp = &tp->tun_if;
351 1.46 atatat
352 1.10 andrew tp->tun_flags &= ~TUN_OPEN;
353 1.6 deraadt
354 1.6 deraadt /*
355 1.6 deraadt * junk all pending output
356 1.6 deraadt */
357 1.50 itojun IFQ_PURGE(&ifp->if_snd);
358 1.6 deraadt
359 1.6 deraadt if (ifp->if_flags & IFF_UP) {
360 1.6 deraadt if_down(ifp);
361 1.8 deraadt if (ifp->if_flags & IFF_RUNNING) {
362 1.17 mycroft /* find internet addresses and delete routes */
363 1.39 augustss struct ifaddr *ifa;
364 1.47 matt TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
365 1.38 itojun #ifdef INET
366 1.18 mycroft if (ifa->ifa_addr->sa_family == AF_INET) {
367 1.18 mycroft rtinit(ifa, (int)RTM_DELETE,
368 1.26 pk tp->tun_flags & TUN_DSTADDR
369 1.26 pk ? RTF_HOST
370 1.26 pk : 0);
371 1.18 mycroft }
372 1.38 itojun #endif
373 1.11 deraadt }
374 1.8 deraadt }
375 1.6 deraadt }
376 1.64 jdolecek tp->tun_pgid = 0;
377 1.56 jdolecek selnotify(&tp->tun_rsel, 0);
378 1.70 pk
379 1.24 thorpej TUNDEBUG ("%s: closed\n", ifp->if_xname);
380 1.46 atatat simple_unlock(&tp->tun_lock);
381 1.70 pk out_nolock:
382 1.70 pk splx(s);
383 1.6 deraadt return (0);
384 1.1 cgd }
385 1.1 cgd
386 1.70 pk /*
387 1.70 pk * Call at splnet() with tp locked.
388 1.70 pk */
389 1.26 pk static void
390 1.24 thorpej tuninit(tp)
391 1.24 thorpej struct tun_softc *tp;
392 1.6 deraadt {
393 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
394 1.46 atatat struct ifaddr *ifa;
395 1.8 deraadt
396 1.24 thorpej TUNDEBUG("%s: tuninit\n", ifp->if_xname);
397 1.6 deraadt
398 1.6 deraadt ifp->if_flags |= IFF_UP | IFF_RUNNING;
399 1.8 deraadt
400 1.26 pk tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
401 1.47 matt TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
402 1.38 itojun #ifdef INET
403 1.11 deraadt if (ifa->ifa_addr->sa_family == AF_INET) {
404 1.17 mycroft struct sockaddr_in *sin;
405 1.11 deraadt
406 1.17 mycroft sin = satosin(ifa->ifa_addr);
407 1.17 mycroft if (sin && sin->sin_addr.s_addr)
408 1.17 mycroft tp->tun_flags |= TUN_IASET;
409 1.17 mycroft
410 1.26 pk if (ifp->if_flags & IFF_POINTOPOINT) {
411 1.26 pk sin = satosin(ifa->ifa_dstaddr);
412 1.26 pk if (sin && sin->sin_addr.s_addr)
413 1.26 pk tp->tun_flags |= TUN_DSTADDR;
414 1.26 pk }
415 1.11 deraadt }
416 1.38 itojun #endif
417 1.18 mycroft }
418 1.8 deraadt
419 1.26 pk return;
420 1.1 cgd }
421 1.1 cgd
422 1.1 cgd /*
423 1.1 cgd * Process an ioctl request.
424 1.1 cgd */
425 1.1 cgd int
426 1.22 christos tun_ioctl(ifp, cmd, data)
427 1.6 deraadt struct ifnet *ifp;
428 1.25 mycroft u_long cmd;
429 1.6 deraadt caddr_t data;
430 1.6 deraadt {
431 1.6 deraadt int error = 0, s;
432 1.46 atatat struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc);
433 1.46 atatat
434 1.70 pk s = splnet();
435 1.46 atatat simple_lock(&tp->tun_lock);
436 1.6 deraadt
437 1.61 itojun switch (cmd) {
438 1.6 deraadt case SIOCSIFADDR:
439 1.70 pk tuninit(tp);
440 1.24 thorpej TUNDEBUG("%s: address set\n", ifp->if_xname);
441 1.6 deraadt break;
442 1.6 deraadt case SIOCSIFDSTADDR:
443 1.70 pk tuninit(tp);
444 1.24 thorpej TUNDEBUG("%s: destination address set\n", ifp->if_xname);
445 1.6 deraadt break;
446 1.26 pk case SIOCSIFBRDADDR:
447 1.26 pk TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
448 1.26 pk break;
449 1.31 matt case SIOCSIFMTU: {
450 1.31 matt struct ifreq *ifr = (struct ifreq *) data;
451 1.31 matt if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) {
452 1.31 matt error = EINVAL;
453 1.31 matt break;
454 1.31 matt }
455 1.31 matt TUNDEBUG("%s: interface mtu set\n", ifp->if_xname);
456 1.31 matt ifp->if_mtu = ifr->ifr_mtu;
457 1.32 matt break;
458 1.32 matt }
459 1.32 matt case SIOCADDMULTI:
460 1.32 matt case SIOCDELMULTI: {
461 1.32 matt struct ifreq *ifr = (struct ifreq *) data;
462 1.32 matt if (ifr == 0) {
463 1.32 matt error = EAFNOSUPPORT; /* XXX */
464 1.32 matt break;
465 1.32 matt }
466 1.32 matt switch (ifr->ifr_addr.sa_family) {
467 1.32 matt #ifdef INET
468 1.32 matt case AF_INET:
469 1.32 matt break;
470 1.32 matt #endif
471 1.32 matt default:
472 1.32 matt error = EAFNOSUPPORT;
473 1.32 matt break;
474 1.32 matt }
475 1.31 matt break;
476 1.31 matt }
477 1.38 itojun case SIOCSIFFLAGS:
478 1.38 itojun break;
479 1.6 deraadt default:
480 1.6 deraadt error = EINVAL;
481 1.6 deraadt }
482 1.70 pk
483 1.70 pk simple_unlock(&tp->tun_lock);
484 1.6 deraadt splx(s);
485 1.6 deraadt return (error);
486 1.1 cgd }
487 1.1 cgd
488 1.1 cgd /*
489 1.22 christos * tun_output - queue packets from higher level ready to put out.
490 1.1 cgd */
491 1.6 deraadt int
492 1.22 christos tun_output(ifp, m0, dst, rt)
493 1.6 deraadt struct ifnet *ifp;
494 1.6 deraadt struct mbuf *m0;
495 1.6 deraadt struct sockaddr *dst;
496 1.12 deraadt struct rtentry *rt;
497 1.6 deraadt {
498 1.24 thorpej struct tun_softc *tp = ifp->if_softc;
499 1.38 itojun #ifdef INET
500 1.6 deraadt int s;
501 1.51 itojun int error;
502 1.38 itojun #endif
503 1.58 jdolecek int mlen;
504 1.50 itojun ALTQ_DECL(struct altq_pktattr pktattr;)
505 1.6 deraadt
506 1.70 pk s = splnet();
507 1.46 atatat simple_lock(&tp->tun_lock);
508 1.24 thorpej TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
509 1.1 cgd
510 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
511 1.24 thorpej TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
512 1.24 thorpej tp->tun_flags);
513 1.8 deraadt m_freem (m0);
514 1.70 pk error = EHOSTDOWN;
515 1.70 pk goto out;
516 1.8 deraadt }
517 1.8 deraadt
518 1.50 itojun /*
519 1.50 itojun * if the queueing discipline needs packet classification,
520 1.50 itojun * do it before prepending link headers.
521 1.50 itojun */
522 1.50 itojun IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
523 1.70 pk
524 1.8 deraadt #if NBPFILTER > 0
525 1.72 christos if (ifp->if_bpf)
526 1.72 christos bpf_mtap_af(ifp->if_bpf, dst->sa_family, m0);
527 1.8 deraadt #endif
528 1.8 deraadt
529 1.6 deraadt switch(dst->sa_family) {
530 1.1 cgd #ifdef INET
531 1.6 deraadt case AF_INET:
532 1.26 pk if (tp->tun_flags & TUN_PREPADDR) {
533 1.26 pk /* Simple link-layer header */
534 1.26 pk M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
535 1.26 pk if (m0 == NULL) {
536 1.26 pk IF_DROP(&ifp->if_snd);
537 1.70 pk error = ENOBUFS;
538 1.70 pk goto out;
539 1.26 pk }
540 1.26 pk bcopy(dst, mtod(m0, char *), dst->sa_len);
541 1.26 pk }
542 1.36 sommerfe /* FALLTHROUGH */
543 1.36 sommerfe case AF_UNSPEC:
544 1.50 itojun IFQ_ENQUEUE(&ifp->if_snd, m0, &pktattr, error);
545 1.50 itojun if (error) {
546 1.6 deraadt ifp->if_collisions++;
547 1.70 pk error = EAFNOSUPPORT;
548 1.70 pk goto out;
549 1.6 deraadt }
550 1.58 jdolecek mlen = m0->m_pkthdr.len;
551 1.6 deraadt ifp->if_opackets++;
552 1.58 jdolecek ifp->if_obytes += mlen;
553 1.6 deraadt break;
554 1.1 cgd #endif
555 1.6 deraadt default:
556 1.6 deraadt m_freem(m0);
557 1.70 pk error = EAFNOSUPPORT;
558 1.70 pk goto out;
559 1.6 deraadt }
560 1.6 deraadt
561 1.6 deraadt if (tp->tun_flags & TUN_RWAIT) {
562 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
563 1.6 deraadt wakeup((caddr_t)tp);
564 1.6 deraadt }
565 1.64 jdolecek if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
566 1.66 christos fownsignal(tp->tun_pgid, SIGIO, POLL_IN, POLLIN|POLLRDNORM,
567 1.66 christos NULL);
568 1.64 jdolecek
569 1.56 jdolecek selnotify(&tp->tun_rsel, 0);
570 1.70 pk out:
571 1.46 atatat simple_unlock(&tp->tun_lock);
572 1.70 pk splx(s);
573 1.26 pk return (0);
574 1.1 cgd }
575 1.1 cgd
576 1.1 cgd /*
577 1.1 cgd * the cdevsw interface is now pretty minimal.
578 1.1 cgd */
579 1.6 deraadt int
580 1.63 fvdl tunioctl(dev, cmd, data, flag, p)
581 1.6 deraadt dev_t dev;
582 1.15 cgd u_long cmd;
583 1.6 deraadt caddr_t data;
584 1.6 deraadt int flag;
585 1.63 fvdl struct proc *p;
586 1.6 deraadt {
587 1.46 atatat struct tun_softc *tp;
588 1.70 pk int s, error = 0;
589 1.46 atatat
590 1.70 pk s = splnet();
591 1.46 atatat tp = tun_find_unit(dev);
592 1.46 atatat
593 1.46 atatat /* interface was "destroyed" already */
594 1.70 pk if (tp == NULL) {
595 1.70 pk error = ENXIO;
596 1.70 pk goto out_nolock;
597 1.70 pk }
598 1.6 deraadt
599 1.6 deraadt switch (cmd) {
600 1.6 deraadt case TUNSDEBUG:
601 1.6 deraadt tundebug = *(int *)data;
602 1.6 deraadt break;
603 1.26 pk
604 1.6 deraadt case TUNGDEBUG:
605 1.6 deraadt *(int *)data = tundebug;
606 1.6 deraadt break;
607 1.26 pk
608 1.26 pk case TUNSIFMODE:
609 1.31 matt switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
610 1.26 pk case IFF_POINTOPOINT:
611 1.26 pk case IFF_BROADCAST:
612 1.26 pk if (tp->tun_if.if_flags & IFF_UP) {
613 1.70 pk error = EBUSY;
614 1.70 pk goto out;
615 1.26 pk }
616 1.26 pk tp->tun_if.if_flags &=
617 1.31 matt ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
618 1.26 pk tp->tun_if.if_flags |= *(int *)data;
619 1.26 pk break;
620 1.26 pk default:
621 1.70 pk error = EINVAL;
622 1.70 pk goto out;
623 1.26 pk }
624 1.26 pk break;
625 1.26 pk
626 1.26 pk case TUNSLMODE:
627 1.26 pk if (*(int *)data)
628 1.26 pk tp->tun_flags |= TUN_PREPADDR;
629 1.26 pk else
630 1.26 pk tp->tun_flags &= ~TUN_PREPADDR;
631 1.26 pk break;
632 1.26 pk
633 1.6 deraadt case FIONBIO:
634 1.6 deraadt if (*(int *)data)
635 1.6 deraadt tp->tun_flags |= TUN_NBIO;
636 1.6 deraadt else
637 1.6 deraadt tp->tun_flags &= ~TUN_NBIO;
638 1.6 deraadt break;
639 1.26 pk
640 1.6 deraadt case FIOASYNC:
641 1.6 deraadt if (*(int *)data)
642 1.6 deraadt tp->tun_flags |= TUN_ASYNC;
643 1.6 deraadt else
644 1.6 deraadt tp->tun_flags &= ~TUN_ASYNC;
645 1.6 deraadt break;
646 1.26 pk
647 1.6 deraadt case FIONREAD:
648 1.6 deraadt if (tp->tun_if.if_snd.ifq_head)
649 1.19 pk *(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
650 1.70 pk else
651 1.6 deraadt *(int *)data = 0;
652 1.6 deraadt break;
653 1.26 pk
654 1.6 deraadt case TIOCSPGRP:
655 1.64 jdolecek case FIOSETOWN:
656 1.64 jdolecek error = fsetown(p, &tp->tun_pgid, cmd, data);
657 1.6 deraadt break;
658 1.26 pk
659 1.6 deraadt case TIOCGPGRP:
660 1.64 jdolecek case FIOGETOWN:
661 1.64 jdolecek error = fgetown(p, tp->tun_pgid, cmd, data);
662 1.6 deraadt break;
663 1.26 pk
664 1.6 deraadt default:
665 1.70 pk error = ENOTTY;
666 1.6 deraadt }
667 1.70 pk
668 1.70 pk out:
669 1.46 atatat simple_unlock(&tp->tun_lock);
670 1.70 pk out_nolock:
671 1.70 pk splx(s);
672 1.64 jdolecek return (error);
673 1.1 cgd }
674 1.1 cgd
675 1.1 cgd /*
676 1.6 deraadt * The cdevsw read interface - reads a packet at a time, or at
677 1.6 deraadt * least as much of a packet as can be read.
678 1.1 cgd */
679 1.6 deraadt int
680 1.22 christos tunread(dev, uio, ioflag)
681 1.6 deraadt dev_t dev;
682 1.6 deraadt struct uio *uio;
683 1.22 christos int ioflag;
684 1.6 deraadt {
685 1.46 atatat struct tun_softc *tp;
686 1.46 atatat struct ifnet *ifp;
687 1.6 deraadt struct mbuf *m, *m0;
688 1.70 pk int error = 0, len, s, index;
689 1.46 atatat
690 1.70 pk s = splnet();
691 1.46 atatat tp = tun_find_unit(dev);
692 1.46 atatat
693 1.46 atatat /* interface was "destroyed" already */
694 1.70 pk if (tp == NULL) {
695 1.70 pk error = ENXIO;
696 1.70 pk goto out_nolock;
697 1.70 pk }
698 1.46 atatat
699 1.46 atatat index = tp->tun_if.if_index;
700 1.46 atatat ifp = &tp->tun_if;
701 1.6 deraadt
702 1.24 thorpej TUNDEBUG ("%s: read\n", ifp->if_xname);
703 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
704 1.26 pk TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
705 1.70 pk error = EHOSTDOWN;
706 1.70 pk goto out;
707 1.8 deraadt }
708 1.8 deraadt
709 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
710 1.6 deraadt
711 1.6 deraadt do {
712 1.50 itojun IFQ_DEQUEUE(&ifp->if_snd, m0);
713 1.6 deraadt if (m0 == 0) {
714 1.6 deraadt if (tp->tun_flags & TUN_NBIO) {
715 1.70 pk error = EWOULDBLOCK;
716 1.70 pk goto out;
717 1.6 deraadt }
718 1.6 deraadt tp->tun_flags |= TUN_RWAIT;
719 1.70 pk if (ltsleep((caddr_t)tp, PZERO|PCATCH|PNORELOCK,
720 1.70 pk "tunread", 0, &tp->tun_lock) != 0) {
721 1.70 pk error = EINTR;
722 1.70 pk goto out_nolock;
723 1.46 atatat } else {
724 1.46 atatat /*
725 1.46 atatat * Maybe the interface was destroyed while
726 1.46 atatat * we were sleeping, so let's ensure that
727 1.46 atatat * we're looking at the same (valid) tun
728 1.46 atatat * interface before looping.
729 1.46 atatat */
730 1.46 atatat tp = tun_find_unit(dev);
731 1.70 pk if (tp == NULL) {
732 1.70 pk error = ENXIO;
733 1.70 pk goto out_nolock;
734 1.70 pk }
735 1.70 pk if (tp->tun_if.if_index != index) {
736 1.70 pk error = ENXIO;
737 1.70 pk goto out;
738 1.46 atatat }
739 1.26 pk }
740 1.6 deraadt }
741 1.6 deraadt } while (m0 == 0);
742 1.70 pk
743 1.70 pk simple_unlock(&tp->tun_lock);
744 1.6 deraadt splx(s);
745 1.6 deraadt
746 1.70 pk /* Copy the mbuf chain */
747 1.6 deraadt while (m0 && uio->uio_resid > 0 && error == 0) {
748 1.13 deraadt len = min(uio->uio_resid, m0->m_len);
749 1.45 itojun if (len != 0)
750 1.45 itojun error = uiomove(mtod(m0, caddr_t), len, uio);
751 1.6 deraadt MFREE(m0, m);
752 1.6 deraadt m0 = m;
753 1.6 deraadt }
754 1.6 deraadt
755 1.6 deraadt if (m0) {
756 1.6 deraadt TUNDEBUG("Dropping mbuf\n");
757 1.6 deraadt m_freem(m0);
758 1.6 deraadt }
759 1.19 pk if (error)
760 1.19 pk ifp->if_ierrors++;
761 1.70 pk
762 1.70 pk return (error);
763 1.70 pk
764 1.70 pk out:
765 1.46 atatat simple_unlock(&tp->tun_lock);
766 1.70 pk out_nolock:
767 1.70 pk splx(s);
768 1.26 pk return (error);
769 1.1 cgd }
770 1.1 cgd
771 1.1 cgd /*
772 1.1 cgd * the cdevsw write interface - an atomic write is a packet - or else!
773 1.1 cgd */
774 1.6 deraadt int
775 1.22 christos tunwrite(dev, uio, ioflag)
776 1.8 deraadt dev_t dev;
777 1.6 deraadt struct uio *uio;
778 1.22 christos int ioflag;
779 1.6 deraadt {
780 1.46 atatat struct tun_softc *tp;
781 1.46 atatat struct ifnet *ifp;
782 1.6 deraadt struct mbuf *top, **mp, *m;
783 1.26 pk struct ifqueue *ifq;
784 1.26 pk struct sockaddr dst;
785 1.70 pk int isr, error = 0, s, tlen, mlen;
786 1.6 deraadt
787 1.70 pk s = splnet();
788 1.46 atatat tp = tun_find_unit(dev);
789 1.46 atatat
790 1.46 atatat /* interface was "destroyed" already */
791 1.70 pk if (tp == NULL) {
792 1.70 pk error = ENXIO;
793 1.70 pk goto out_nolock;
794 1.70 pk }
795 1.70 pk
796 1.70 pk /* Unlock until we've got the data */
797 1.70 pk simple_unlock(&tp->tun_lock);
798 1.70 pk splx(s);
799 1.46 atatat
800 1.46 atatat ifp = &tp->tun_if;
801 1.46 atatat
802 1.24 thorpej TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
803 1.6 deraadt
804 1.26 pk if (tp->tun_flags & TUN_PREPADDR) {
805 1.46 atatat if (uio->uio_resid < sizeof(dst)) {
806 1.70 pk error = EIO;
807 1.70 pk goto out0;
808 1.46 atatat }
809 1.26 pk error = uiomove((caddr_t)&dst, sizeof(dst), uio);
810 1.26 pk if (dst.sa_len > sizeof(dst)) {
811 1.26 pk /* Duh.. */
812 1.26 pk char discard;
813 1.26 pk int n = dst.sa_len - sizeof(dst);
814 1.26 pk while (n--)
815 1.46 atatat if ((error = uiomove(&discard, 1, uio)) != 0) {
816 1.70 pk goto out0;
817 1.46 atatat }
818 1.26 pk }
819 1.26 pk } else {
820 1.26 pk #ifdef INET
821 1.26 pk dst.sa_family = AF_INET;
822 1.26 pk #endif
823 1.26 pk }
824 1.26 pk
825 1.54 simonb if (uio->uio_resid > TUNMTU) {
826 1.37 mjacob TUNDEBUG("%s: len=%lu!\n", ifp->if_xname,
827 1.37 mjacob (unsigned long)uio->uio_resid);
828 1.70 pk error = EIO;
829 1.70 pk goto out0;
830 1.6 deraadt }
831 1.26 pk
832 1.26 pk switch (dst.sa_family) {
833 1.26 pk #ifdef INET
834 1.26 pk case AF_INET:
835 1.26 pk ifq = &ipintrq;
836 1.26 pk isr = NETISR_IP;
837 1.26 pk break;
838 1.26 pk #endif
839 1.26 pk default:
840 1.70 pk error = EAFNOSUPPORT;
841 1.70 pk goto out0;
842 1.26 pk }
843 1.26 pk
844 1.8 deraadt tlen = uio->uio_resid;
845 1.8 deraadt
846 1.8 deraadt /* get a header mbuf */
847 1.8 deraadt MGETHDR(m, M_DONTWAIT, MT_DATA);
848 1.46 atatat if (m == NULL) {
849 1.70 pk error = ENOBUFS;
850 1.70 pk goto out0;
851 1.46 atatat }
852 1.8 deraadt mlen = MHLEN;
853 1.8 deraadt
854 1.68 tron top = NULL;
855 1.6 deraadt mp = ⊤
856 1.6 deraadt while (error == 0 && uio->uio_resid > 0) {
857 1.13 deraadt m->m_len = min(mlen, uio->uio_resid);
858 1.68 tron error = uiomove(mtod(m, caddr_t), m->m_len, uio);
859 1.6 deraadt *mp = m;
860 1.6 deraadt mp = &m->m_next;
861 1.68 tron if (error == 0 && uio->uio_resid > 0) {
862 1.68 tron MGET(m, M_DONTWAIT, MT_DATA);
863 1.68 tron if (m == NULL) {
864 1.8 deraadt error = ENOBUFS;
865 1.8 deraadt break;
866 1.8 deraadt }
867 1.8 deraadt mlen = MLEN;
868 1.8 deraadt }
869 1.6 deraadt }
870 1.6 deraadt if (error) {
871 1.68 tron if (top != NULL)
872 1.8 deraadt m_freem (top);
873 1.19 pk ifp->if_ierrors++;
874 1.70 pk goto out0;
875 1.6 deraadt }
876 1.6 deraadt
877 1.8 deraadt top->m_pkthdr.len = tlen;
878 1.8 deraadt top->m_pkthdr.rcvif = ifp;
879 1.8 deraadt
880 1.8 deraadt #if NBPFILTER > 0
881 1.72 christos if (ifp->if_bpf)
882 1.72 christos bpf_mtap_af(ifp->if_bpf, AF_INET, top);
883 1.8 deraadt #endif
884 1.6 deraadt
885 1.43 thorpej s = splnet();
886 1.70 pk simple_lock(&tp->tun_lock);
887 1.70 pk if ((tp->tun_flags & TUN_INITED) == 0) {
888 1.70 pk /* Interface was destroyed */
889 1.70 pk error = ENXIO;
890 1.70 pk goto out;
891 1.70 pk }
892 1.26 pk if (IF_QFULL(ifq)) {
893 1.26 pk IF_DROP(ifq);
894 1.6 deraadt ifp->if_collisions++;
895 1.6 deraadt m_freem(top);
896 1.70 pk error = ENOBUFS;
897 1.70 pk goto out;
898 1.6 deraadt }
899 1.70 pk
900 1.26 pk IF_ENQUEUE(ifq, top);
901 1.6 deraadt ifp->if_ipackets++;
902 1.58 jdolecek ifp->if_ibytes += tlen;
903 1.26 pk schednetisr(isr);
904 1.70 pk out:
905 1.46 atatat simple_unlock(&tp->tun_lock);
906 1.70 pk out_nolock:
907 1.70 pk splx(s);
908 1.70 pk out0:
909 1.26 pk return (error);
910 1.1 cgd }
911 1.1 cgd
912 1.50 itojun #ifdef ALTQ
913 1.50 itojun /*
914 1.50 itojun * Start packet transmission on the interface.
915 1.50 itojun * when the interface queue is rate-limited by ALTQ or TBR,
916 1.50 itojun * if_start is needed to drain packets from the queue in order
917 1.50 itojun * to notify readers when outgoing packets become ready.
918 1.70 pk *
919 1.70 pk * Should be called at splnet.
920 1.50 itojun */
921 1.50 itojun static void
922 1.50 itojun tunstart(ifp)
923 1.50 itojun struct ifnet *ifp;
924 1.50 itojun {
925 1.50 itojun struct tun_softc *tp = ifp->if_softc;
926 1.50 itojun
927 1.50 itojun if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd))
928 1.50 itojun return;
929 1.50 itojun
930 1.70 pk simple_lock(&tp->tun_lock);
931 1.70 pk if (!IF_IS_EMPTY(&ifp->if_snd)) {
932 1.50 itojun if (tp->tun_flags & TUN_RWAIT) {
933 1.50 itojun tp->tun_flags &= ~TUN_RWAIT;
934 1.50 itojun wakeup((caddr_t)tp);
935 1.50 itojun }
936 1.64 jdolecek if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
937 1.67 cl fownsignal(tp->tun_pgid, SIGIO, POLL_OUT,
938 1.67 cl POLLOUT|POLLWRNORM, NULL);
939 1.70 pk
940 1.50 itojun selwakeup(&tp->tun_rsel);
941 1.50 itojun }
942 1.70 pk simple_unlock(&tp->tun_lock);
943 1.50 itojun }
944 1.50 itojun #endif /* ALTQ */
945 1.1 cgd /*
946 1.27 mycroft * tunpoll - the poll interface, this is only useful on reads
947 1.6 deraadt * really. The write detect always returns true, write never blocks
948 1.6 deraadt * anyway, it either accepts the packet or drops it.
949 1.6 deraadt */
950 1.6 deraadt int
951 1.63 fvdl tunpoll(dev, events, p)
952 1.6 deraadt dev_t dev;
953 1.27 mycroft int events;
954 1.63 fvdl struct proc *p;
955 1.6 deraadt {
956 1.46 atatat struct tun_softc *tp;
957 1.46 atatat struct ifnet *ifp;
958 1.46 atatat int s, revents = 0;
959 1.46 atatat
960 1.70 pk s = splnet();
961 1.46 atatat tp = tun_find_unit(dev);
962 1.46 atatat
963 1.46 atatat /* interface was "destroyed" already */
964 1.46 atatat if (tp == NULL)
965 1.70 pk goto out_nolock;
966 1.46 atatat
967 1.46 atatat ifp = &tp->tun_if;
968 1.6 deraadt
969 1.27 mycroft TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
970 1.6 deraadt
971 1.35 veego if (events & (POLLIN | POLLRDNORM)) {
972 1.70 pk if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
973 1.27 mycroft TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
974 1.24 thorpej ifp->if_snd.ifq_len);
975 1.27 mycroft revents |= events & (POLLIN | POLLRDNORM);
976 1.27 mycroft } else {
977 1.27 mycroft TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
978 1.63 fvdl selrecord(p, &tp->tun_rsel);
979 1.6 deraadt }
980 1.35 veego }
981 1.27 mycroft
982 1.27 mycroft if (events & (POLLOUT | POLLWRNORM))
983 1.27 mycroft revents |= events & (POLLOUT | POLLWRNORM);
984 1.27 mycroft
985 1.70 pk simple_unlock(&tp->tun_lock);
986 1.70 pk out_nolock:
987 1.6 deraadt splx(s);
988 1.27 mycroft return (revents);
989 1.56 jdolecek }
990 1.56 jdolecek
991 1.56 jdolecek static void
992 1.56 jdolecek filt_tunrdetach(struct knote *kn)
993 1.56 jdolecek {
994 1.56 jdolecek struct tun_softc *tp = kn->kn_hook;
995 1.56 jdolecek int s;
996 1.56 jdolecek
997 1.56 jdolecek s = splnet();
998 1.57 christos SLIST_REMOVE(&tp->tun_rsel.sel_klist, kn, knote, kn_selnext);
999 1.56 jdolecek splx(s);
1000 1.56 jdolecek }
1001 1.56 jdolecek
1002 1.56 jdolecek static int
1003 1.56 jdolecek filt_tunread(struct knote *kn, long hint)
1004 1.56 jdolecek {
1005 1.56 jdolecek struct tun_softc *tp = kn->kn_hook;
1006 1.56 jdolecek struct ifnet *ifp = &tp->tun_if;
1007 1.56 jdolecek struct mbuf *m;
1008 1.56 jdolecek int s;
1009 1.56 jdolecek
1010 1.56 jdolecek s = splnet();
1011 1.56 jdolecek IF_POLL(&ifp->if_snd, m);
1012 1.56 jdolecek if (m == NULL) {
1013 1.56 jdolecek splx(s);
1014 1.56 jdolecek return (0);
1015 1.56 jdolecek }
1016 1.56 jdolecek
1017 1.56 jdolecek for (kn->kn_data = 0; m != NULL; m = m->m_next)
1018 1.56 jdolecek kn->kn_data += m->m_len;
1019 1.56 jdolecek
1020 1.56 jdolecek splx(s);
1021 1.56 jdolecek return (1);
1022 1.56 jdolecek }
1023 1.56 jdolecek
1024 1.56 jdolecek static const struct filterops tunread_filtops =
1025 1.56 jdolecek { 1, NULL, filt_tunrdetach, filt_tunread };
1026 1.56 jdolecek
1027 1.56 jdolecek static const struct filterops tun_seltrue_filtops =
1028 1.56 jdolecek { 1, NULL, filt_tunrdetach, filt_seltrue };
1029 1.56 jdolecek
1030 1.56 jdolecek int
1031 1.56 jdolecek tunkqfilter(dev_t dev, struct knote *kn)
1032 1.56 jdolecek {
1033 1.70 pk struct tun_softc *tp;
1034 1.56 jdolecek struct klist *klist;
1035 1.70 pk int rv = 0, s;
1036 1.70 pk
1037 1.70 pk s = splnet();
1038 1.70 pk tp = tun_find_unit(dev);
1039 1.70 pk if (tp == NULL)
1040 1.70 pk goto out_nolock;
1041 1.56 jdolecek
1042 1.56 jdolecek switch (kn->kn_filter) {
1043 1.56 jdolecek case EVFILT_READ:
1044 1.57 christos klist = &tp->tun_rsel.sel_klist;
1045 1.56 jdolecek kn->kn_fop = &tunread_filtops;
1046 1.56 jdolecek break;
1047 1.56 jdolecek
1048 1.56 jdolecek case EVFILT_WRITE:
1049 1.57 christos klist = &tp->tun_rsel.sel_klist;
1050 1.56 jdolecek kn->kn_fop = &tun_seltrue_filtops;
1051 1.56 jdolecek break;
1052 1.56 jdolecek
1053 1.56 jdolecek default:
1054 1.70 pk rv = 1;
1055 1.70 pk goto out;
1056 1.56 jdolecek }
1057 1.56 jdolecek
1058 1.56 jdolecek kn->kn_hook = tp;
1059 1.56 jdolecek
1060 1.56 jdolecek SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1061 1.70 pk
1062 1.70 pk out:
1063 1.70 pk simple_unlock(&tp->tun_lock);
1064 1.70 pk out_nolock:
1065 1.56 jdolecek splx(s);
1066 1.70 pk return (rv);
1067 1.1 cgd }
1068