if_tun.c revision 1.70 1 1.70 pk /* $NetBSD: if_tun.c,v 1.70 2004/05/14 13:23:12 pk 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.70 pk __KERNEL_RCSID(0, "$NetBSD: if_tun.c,v 1.70 2004/05/14 13:23:12 pk 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.46 atatat void 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.46 atatat void
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.8 deraadt }
275 1.8 deraadt
276 1.6 deraadt /*
277 1.6 deraadt * tunnel open - must be superuser & the device must be
278 1.6 deraadt * configured in
279 1.6 deraadt */
280 1.6 deraadt int
281 1.63 fvdl tunopen(dev, flag, mode, p)
282 1.6 deraadt dev_t dev;
283 1.6 deraadt int flag, mode;
284 1.63 fvdl struct proc *p;
285 1.6 deraadt {
286 1.6 deraadt struct ifnet *ifp;
287 1.8 deraadt struct tun_softc *tp;
288 1.70 pk int s, error;
289 1.1 cgd
290 1.22 christos if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
291 1.5 deraadt return (error);
292 1.5 deraadt
293 1.46 atatat if (NTUN < 1)
294 1.6 deraadt return (ENXIO);
295 1.46 atatat
296 1.70 pk s = splnet();
297 1.46 atatat tp = tun_find_unit(dev);
298 1.52 atatat
299 1.70 pk if (tp == NULL) {
300 1.52 atatat (void)tun_clone_create(&tun_cloner, minor(dev));
301 1.52 atatat tp = tun_find_unit(dev);
302 1.70 pk if (tp == NULL) {
303 1.70 pk error = ENXIO;
304 1.70 pk goto out_nolock;
305 1.70 pk }
306 1.52 atatat }
307 1.46 atatat
308 1.46 atatat if (tp->tun_flags & TUN_OPEN) {
309 1.70 pk error = EBUSY;
310 1.70 pk goto out;
311 1.46 atatat }
312 1.46 atatat
313 1.6 deraadt ifp = &tp->tun_if;
314 1.6 deraadt tp->tun_flags |= TUN_OPEN;
315 1.24 thorpej TUNDEBUG("%s: open\n", ifp->if_xname);
316 1.70 pk out:
317 1.46 atatat simple_unlock(&tp->tun_lock);
318 1.70 pk out_nolock:
319 1.70 pk splx(s);
320 1.70 pk return (error);
321 1.1 cgd }
322 1.1 cgd
323 1.6 deraadt /*
324 1.6 deraadt * tunclose - close the device - mark i/f down & delete
325 1.6 deraadt * routing info
326 1.6 deraadt */
327 1.6 deraadt int
328 1.63 fvdl tunclose(dev, flag, mode, p)
329 1.6 deraadt dev_t dev;
330 1.6 deraadt int flag;
331 1.22 christos int mode;
332 1.63 fvdl struct proc *p;
333 1.6 deraadt {
334 1.46 atatat int s;
335 1.46 atatat struct tun_softc *tp;
336 1.46 atatat struct ifnet *ifp;
337 1.6 deraadt
338 1.70 pk s = splnet();
339 1.70 pk if ((tp = tun_find_zunit(minor(dev))) != NULL) {
340 1.70 pk /* interface was "destroyed" before the close */
341 1.70 pk free(tp, M_DEVBUF);
342 1.70 pk goto out_nolock;
343 1.70 pk }
344 1.46 atatat
345 1.70 pk if ((tp = tun_find_unit(dev)) == NULL)
346 1.70 pk goto out_nolock;
347 1.46 atatat
348 1.46 atatat ifp = &tp->tun_if;
349 1.46 atatat
350 1.10 andrew tp->tun_flags &= ~TUN_OPEN;
351 1.6 deraadt
352 1.6 deraadt /*
353 1.6 deraadt * junk all pending output
354 1.6 deraadt */
355 1.50 itojun IFQ_PURGE(&ifp->if_snd);
356 1.6 deraadt
357 1.6 deraadt if (ifp->if_flags & IFF_UP) {
358 1.6 deraadt if_down(ifp);
359 1.8 deraadt if (ifp->if_flags & IFF_RUNNING) {
360 1.17 mycroft /* find internet addresses and delete routes */
361 1.39 augustss struct ifaddr *ifa;
362 1.47 matt TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
363 1.38 itojun #ifdef INET
364 1.18 mycroft if (ifa->ifa_addr->sa_family == AF_INET) {
365 1.18 mycroft rtinit(ifa, (int)RTM_DELETE,
366 1.26 pk tp->tun_flags & TUN_DSTADDR
367 1.26 pk ? RTF_HOST
368 1.26 pk : 0);
369 1.18 mycroft }
370 1.38 itojun #endif
371 1.11 deraadt }
372 1.8 deraadt }
373 1.6 deraadt }
374 1.64 jdolecek tp->tun_pgid = 0;
375 1.56 jdolecek selnotify(&tp->tun_rsel, 0);
376 1.70 pk
377 1.24 thorpej TUNDEBUG ("%s: closed\n", ifp->if_xname);
378 1.46 atatat simple_unlock(&tp->tun_lock);
379 1.70 pk out_nolock:
380 1.70 pk splx(s);
381 1.6 deraadt return (0);
382 1.1 cgd }
383 1.1 cgd
384 1.70 pk /*
385 1.70 pk * Call at splnet() with tp locked.
386 1.70 pk */
387 1.26 pk static void
388 1.24 thorpej tuninit(tp)
389 1.24 thorpej struct tun_softc *tp;
390 1.6 deraadt {
391 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
392 1.46 atatat struct ifaddr *ifa;
393 1.8 deraadt
394 1.24 thorpej TUNDEBUG("%s: tuninit\n", ifp->if_xname);
395 1.6 deraadt
396 1.6 deraadt ifp->if_flags |= IFF_UP | IFF_RUNNING;
397 1.8 deraadt
398 1.26 pk tp->tun_flags &= ~(TUN_IASET|TUN_DSTADDR);
399 1.47 matt TAILQ_FOREACH(ifa, &ifp->if_addrlist, ifa_list) {
400 1.38 itojun #ifdef INET
401 1.11 deraadt if (ifa->ifa_addr->sa_family == AF_INET) {
402 1.17 mycroft struct sockaddr_in *sin;
403 1.11 deraadt
404 1.17 mycroft sin = satosin(ifa->ifa_addr);
405 1.17 mycroft if (sin && sin->sin_addr.s_addr)
406 1.17 mycroft tp->tun_flags |= TUN_IASET;
407 1.17 mycroft
408 1.26 pk if (ifp->if_flags & IFF_POINTOPOINT) {
409 1.26 pk sin = satosin(ifa->ifa_dstaddr);
410 1.26 pk if (sin && sin->sin_addr.s_addr)
411 1.26 pk tp->tun_flags |= TUN_DSTADDR;
412 1.26 pk }
413 1.11 deraadt }
414 1.38 itojun #endif
415 1.18 mycroft }
416 1.8 deraadt
417 1.26 pk return;
418 1.1 cgd }
419 1.1 cgd
420 1.1 cgd /*
421 1.1 cgd * Process an ioctl request.
422 1.1 cgd */
423 1.1 cgd int
424 1.22 christos tun_ioctl(ifp, cmd, data)
425 1.6 deraadt struct ifnet *ifp;
426 1.25 mycroft u_long cmd;
427 1.6 deraadt caddr_t data;
428 1.6 deraadt {
429 1.6 deraadt int error = 0, s;
430 1.46 atatat struct tun_softc *tp = (struct tun_softc *)(ifp->if_softc);
431 1.46 atatat
432 1.70 pk s = splnet();
433 1.46 atatat simple_lock(&tp->tun_lock);
434 1.6 deraadt
435 1.61 itojun switch (cmd) {
436 1.6 deraadt case SIOCSIFADDR:
437 1.70 pk tuninit(tp);
438 1.24 thorpej TUNDEBUG("%s: address set\n", ifp->if_xname);
439 1.6 deraadt break;
440 1.6 deraadt case SIOCSIFDSTADDR:
441 1.70 pk tuninit(tp);
442 1.24 thorpej TUNDEBUG("%s: destination address set\n", ifp->if_xname);
443 1.6 deraadt break;
444 1.26 pk case SIOCSIFBRDADDR:
445 1.26 pk TUNDEBUG("%s: broadcast address set\n", ifp->if_xname);
446 1.26 pk break;
447 1.31 matt case SIOCSIFMTU: {
448 1.31 matt struct ifreq *ifr = (struct ifreq *) data;
449 1.31 matt if (ifr->ifr_mtu > TUNMTU || ifr->ifr_mtu < 576) {
450 1.31 matt error = EINVAL;
451 1.31 matt break;
452 1.31 matt }
453 1.31 matt TUNDEBUG("%s: interface mtu set\n", ifp->if_xname);
454 1.31 matt ifp->if_mtu = ifr->ifr_mtu;
455 1.32 matt break;
456 1.32 matt }
457 1.32 matt case SIOCADDMULTI:
458 1.32 matt case SIOCDELMULTI: {
459 1.32 matt struct ifreq *ifr = (struct ifreq *) data;
460 1.32 matt if (ifr == 0) {
461 1.32 matt error = EAFNOSUPPORT; /* XXX */
462 1.32 matt break;
463 1.32 matt }
464 1.32 matt switch (ifr->ifr_addr.sa_family) {
465 1.32 matt #ifdef INET
466 1.32 matt case AF_INET:
467 1.32 matt break;
468 1.32 matt #endif
469 1.32 matt default:
470 1.32 matt error = EAFNOSUPPORT;
471 1.32 matt break;
472 1.32 matt }
473 1.31 matt break;
474 1.31 matt }
475 1.38 itojun case SIOCSIFFLAGS:
476 1.38 itojun break;
477 1.6 deraadt default:
478 1.6 deraadt error = EINVAL;
479 1.6 deraadt }
480 1.70 pk
481 1.70 pk simple_unlock(&tp->tun_lock);
482 1.6 deraadt splx(s);
483 1.6 deraadt return (error);
484 1.1 cgd }
485 1.1 cgd
486 1.1 cgd /*
487 1.22 christos * tun_output - queue packets from higher level ready to put out.
488 1.1 cgd */
489 1.6 deraadt int
490 1.22 christos tun_output(ifp, m0, dst, rt)
491 1.6 deraadt struct ifnet *ifp;
492 1.6 deraadt struct mbuf *m0;
493 1.6 deraadt struct sockaddr *dst;
494 1.12 deraadt struct rtentry *rt;
495 1.6 deraadt {
496 1.24 thorpej struct tun_softc *tp = ifp->if_softc;
497 1.38 itojun #ifdef INET
498 1.6 deraadt int s;
499 1.51 itojun int error;
500 1.38 itojun #endif
501 1.58 jdolecek int mlen;
502 1.50 itojun ALTQ_DECL(struct altq_pktattr pktattr;)
503 1.6 deraadt
504 1.70 pk s = splnet();
505 1.46 atatat simple_lock(&tp->tun_lock);
506 1.24 thorpej TUNDEBUG ("%s: tun_output\n", ifp->if_xname);
507 1.1 cgd
508 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
509 1.24 thorpej TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname,
510 1.24 thorpej tp->tun_flags);
511 1.8 deraadt m_freem (m0);
512 1.70 pk error = EHOSTDOWN;
513 1.70 pk goto out;
514 1.8 deraadt }
515 1.8 deraadt
516 1.50 itojun /*
517 1.50 itojun * if the queueing discipline needs packet classification,
518 1.50 itojun * do it before prepending link headers.
519 1.50 itojun */
520 1.50 itojun IFQ_CLASSIFY(&ifp->if_snd, m0, dst->sa_family, &pktattr);
521 1.70 pk
522 1.8 deraadt #if NBPFILTER > 0
523 1.40 thorpej if (ifp->if_bpf) {
524 1.8 deraadt /*
525 1.8 deraadt * We need to prepend the address family as
526 1.8 deraadt * a four byte field. Cons up a dummy header
527 1.8 deraadt * to pacify bpf. This is safe because bpf
528 1.8 deraadt * will only read from the mbuf (i.e., it won't
529 1.8 deraadt * try to free it or keep a pointer to it).
530 1.8 deraadt */
531 1.8 deraadt struct mbuf m;
532 1.16 cgd u_int32_t af = dst->sa_family;
533 1.8 deraadt
534 1.60 itojun m.m_flags = 0;
535 1.8 deraadt m.m_next = m0;
536 1.16 cgd m.m_len = sizeof(af);
537 1.8 deraadt m.m_data = (char *)⁡
538 1.8 deraadt
539 1.40 thorpej bpf_mtap(ifp->if_bpf, &m);
540 1.8 deraadt }
541 1.8 deraadt #endif
542 1.8 deraadt
543 1.6 deraadt switch(dst->sa_family) {
544 1.1 cgd #ifdef INET
545 1.6 deraadt case AF_INET:
546 1.26 pk if (tp->tun_flags & TUN_PREPADDR) {
547 1.26 pk /* Simple link-layer header */
548 1.26 pk M_PREPEND(m0, dst->sa_len, M_DONTWAIT);
549 1.26 pk if (m0 == NULL) {
550 1.26 pk IF_DROP(&ifp->if_snd);
551 1.70 pk error = ENOBUFS;
552 1.70 pk goto out;
553 1.26 pk }
554 1.26 pk bcopy(dst, mtod(m0, char *), dst->sa_len);
555 1.26 pk }
556 1.36 sommerfe /* FALLTHROUGH */
557 1.36 sommerfe case AF_UNSPEC:
558 1.50 itojun IFQ_ENQUEUE(&ifp->if_snd, m0, &pktattr, error);
559 1.50 itojun if (error) {
560 1.6 deraadt ifp->if_collisions++;
561 1.70 pk error = EAFNOSUPPORT;
562 1.70 pk goto out;
563 1.6 deraadt }
564 1.58 jdolecek mlen = m0->m_pkthdr.len;
565 1.6 deraadt ifp->if_opackets++;
566 1.58 jdolecek ifp->if_obytes += mlen;
567 1.6 deraadt break;
568 1.1 cgd #endif
569 1.6 deraadt default:
570 1.6 deraadt m_freem(m0);
571 1.70 pk error = EAFNOSUPPORT;
572 1.70 pk goto out;
573 1.6 deraadt }
574 1.6 deraadt
575 1.6 deraadt if (tp->tun_flags & TUN_RWAIT) {
576 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
577 1.6 deraadt wakeup((caddr_t)tp);
578 1.6 deraadt }
579 1.64 jdolecek if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
580 1.66 christos fownsignal(tp->tun_pgid, SIGIO, POLL_IN, POLLIN|POLLRDNORM,
581 1.66 christos NULL);
582 1.64 jdolecek
583 1.56 jdolecek selnotify(&tp->tun_rsel, 0);
584 1.70 pk out:
585 1.46 atatat simple_unlock(&tp->tun_lock);
586 1.70 pk splx(s);
587 1.26 pk return (0);
588 1.1 cgd }
589 1.1 cgd
590 1.1 cgd /*
591 1.1 cgd * the cdevsw interface is now pretty minimal.
592 1.1 cgd */
593 1.6 deraadt int
594 1.63 fvdl tunioctl(dev, cmd, data, flag, p)
595 1.6 deraadt dev_t dev;
596 1.15 cgd u_long cmd;
597 1.6 deraadt caddr_t data;
598 1.6 deraadt int flag;
599 1.63 fvdl struct proc *p;
600 1.6 deraadt {
601 1.46 atatat struct tun_softc *tp;
602 1.70 pk int s, error = 0;
603 1.46 atatat
604 1.70 pk s = splnet();
605 1.46 atatat tp = tun_find_unit(dev);
606 1.46 atatat
607 1.46 atatat /* interface was "destroyed" already */
608 1.70 pk if (tp == NULL) {
609 1.70 pk error = ENXIO;
610 1.70 pk goto out_nolock;
611 1.70 pk }
612 1.6 deraadt
613 1.6 deraadt switch (cmd) {
614 1.6 deraadt case TUNSDEBUG:
615 1.6 deraadt tundebug = *(int *)data;
616 1.6 deraadt break;
617 1.26 pk
618 1.6 deraadt case TUNGDEBUG:
619 1.6 deraadt *(int *)data = tundebug;
620 1.6 deraadt break;
621 1.26 pk
622 1.26 pk case TUNSIFMODE:
623 1.31 matt switch (*(int *)data & (IFF_POINTOPOINT|IFF_BROADCAST)) {
624 1.26 pk case IFF_POINTOPOINT:
625 1.26 pk case IFF_BROADCAST:
626 1.26 pk if (tp->tun_if.if_flags & IFF_UP) {
627 1.70 pk error = EBUSY;
628 1.70 pk goto out;
629 1.26 pk }
630 1.26 pk tp->tun_if.if_flags &=
631 1.31 matt ~(IFF_BROADCAST|IFF_POINTOPOINT|IFF_MULTICAST);
632 1.26 pk tp->tun_if.if_flags |= *(int *)data;
633 1.26 pk break;
634 1.26 pk default:
635 1.70 pk error = EINVAL;
636 1.70 pk goto out;
637 1.26 pk }
638 1.26 pk break;
639 1.26 pk
640 1.26 pk case TUNSLMODE:
641 1.26 pk if (*(int *)data)
642 1.26 pk tp->tun_flags |= TUN_PREPADDR;
643 1.26 pk else
644 1.26 pk tp->tun_flags &= ~TUN_PREPADDR;
645 1.26 pk break;
646 1.26 pk
647 1.6 deraadt case FIONBIO:
648 1.6 deraadt if (*(int *)data)
649 1.6 deraadt tp->tun_flags |= TUN_NBIO;
650 1.6 deraadt else
651 1.6 deraadt tp->tun_flags &= ~TUN_NBIO;
652 1.6 deraadt break;
653 1.26 pk
654 1.6 deraadt case FIOASYNC:
655 1.6 deraadt if (*(int *)data)
656 1.6 deraadt tp->tun_flags |= TUN_ASYNC;
657 1.6 deraadt else
658 1.6 deraadt tp->tun_flags &= ~TUN_ASYNC;
659 1.6 deraadt break;
660 1.26 pk
661 1.6 deraadt case FIONREAD:
662 1.6 deraadt if (tp->tun_if.if_snd.ifq_head)
663 1.19 pk *(int *)data = tp->tun_if.if_snd.ifq_head->m_pkthdr.len;
664 1.70 pk else
665 1.6 deraadt *(int *)data = 0;
666 1.6 deraadt break;
667 1.26 pk
668 1.6 deraadt case TIOCSPGRP:
669 1.64 jdolecek case FIOSETOWN:
670 1.64 jdolecek error = fsetown(p, &tp->tun_pgid, cmd, data);
671 1.6 deraadt break;
672 1.26 pk
673 1.6 deraadt case TIOCGPGRP:
674 1.64 jdolecek case FIOGETOWN:
675 1.64 jdolecek error = fgetown(p, tp->tun_pgid, cmd, data);
676 1.6 deraadt break;
677 1.26 pk
678 1.6 deraadt default:
679 1.70 pk error = ENOTTY;
680 1.6 deraadt }
681 1.70 pk
682 1.70 pk out:
683 1.46 atatat simple_unlock(&tp->tun_lock);
684 1.70 pk out_nolock:
685 1.70 pk splx(s);
686 1.64 jdolecek return (error);
687 1.1 cgd }
688 1.1 cgd
689 1.1 cgd /*
690 1.6 deraadt * The cdevsw read interface - reads a packet at a time, or at
691 1.6 deraadt * least as much of a packet as can be read.
692 1.1 cgd */
693 1.6 deraadt int
694 1.22 christos tunread(dev, uio, ioflag)
695 1.6 deraadt dev_t dev;
696 1.6 deraadt struct uio *uio;
697 1.22 christos int ioflag;
698 1.6 deraadt {
699 1.46 atatat struct tun_softc *tp;
700 1.46 atatat struct ifnet *ifp;
701 1.6 deraadt struct mbuf *m, *m0;
702 1.70 pk int error = 0, len, s, index;
703 1.46 atatat
704 1.70 pk s = splnet();
705 1.46 atatat tp = tun_find_unit(dev);
706 1.46 atatat
707 1.46 atatat /* interface was "destroyed" already */
708 1.70 pk if (tp == NULL) {
709 1.70 pk error = ENXIO;
710 1.70 pk goto out_nolock;
711 1.70 pk }
712 1.46 atatat
713 1.46 atatat index = tp->tun_if.if_index;
714 1.46 atatat ifp = &tp->tun_if;
715 1.6 deraadt
716 1.24 thorpej TUNDEBUG ("%s: read\n", ifp->if_xname);
717 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
718 1.26 pk TUNDEBUG ("%s: not ready 0%o\n", ifp->if_xname, tp->tun_flags);
719 1.70 pk error = EHOSTDOWN;
720 1.70 pk goto out;
721 1.8 deraadt }
722 1.8 deraadt
723 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
724 1.6 deraadt
725 1.6 deraadt do {
726 1.50 itojun IFQ_DEQUEUE(&ifp->if_snd, m0);
727 1.6 deraadt if (m0 == 0) {
728 1.6 deraadt if (tp->tun_flags & TUN_NBIO) {
729 1.70 pk error = EWOULDBLOCK;
730 1.70 pk goto out;
731 1.6 deraadt }
732 1.6 deraadt tp->tun_flags |= TUN_RWAIT;
733 1.70 pk if (ltsleep((caddr_t)tp, PZERO|PCATCH|PNORELOCK,
734 1.70 pk "tunread", 0, &tp->tun_lock) != 0) {
735 1.70 pk error = EINTR;
736 1.70 pk goto out_nolock;
737 1.46 atatat } else {
738 1.46 atatat /*
739 1.46 atatat * Maybe the interface was destroyed while
740 1.46 atatat * we were sleeping, so let's ensure that
741 1.46 atatat * we're looking at the same (valid) tun
742 1.46 atatat * interface before looping.
743 1.46 atatat */
744 1.46 atatat tp = tun_find_unit(dev);
745 1.70 pk if (tp == NULL) {
746 1.70 pk error = ENXIO;
747 1.70 pk goto out_nolock;
748 1.70 pk }
749 1.70 pk if (tp->tun_if.if_index != index) {
750 1.70 pk error = ENXIO;
751 1.70 pk goto out;
752 1.46 atatat }
753 1.26 pk }
754 1.6 deraadt }
755 1.6 deraadt } while (m0 == 0);
756 1.70 pk
757 1.70 pk simple_unlock(&tp->tun_lock);
758 1.6 deraadt splx(s);
759 1.6 deraadt
760 1.70 pk /* Copy the mbuf chain */
761 1.6 deraadt while (m0 && uio->uio_resid > 0 && error == 0) {
762 1.13 deraadt len = min(uio->uio_resid, m0->m_len);
763 1.45 itojun if (len != 0)
764 1.45 itojun error = uiomove(mtod(m0, caddr_t), len, uio);
765 1.6 deraadt MFREE(m0, m);
766 1.6 deraadt m0 = m;
767 1.6 deraadt }
768 1.6 deraadt
769 1.6 deraadt if (m0) {
770 1.6 deraadt TUNDEBUG("Dropping mbuf\n");
771 1.6 deraadt m_freem(m0);
772 1.6 deraadt }
773 1.19 pk if (error)
774 1.19 pk ifp->if_ierrors++;
775 1.70 pk
776 1.70 pk return (error);
777 1.70 pk
778 1.70 pk out:
779 1.46 atatat simple_unlock(&tp->tun_lock);
780 1.70 pk out_nolock:
781 1.70 pk splx(s);
782 1.26 pk return (error);
783 1.1 cgd }
784 1.1 cgd
785 1.1 cgd /*
786 1.1 cgd * the cdevsw write interface - an atomic write is a packet - or else!
787 1.1 cgd */
788 1.6 deraadt int
789 1.22 christos tunwrite(dev, uio, ioflag)
790 1.8 deraadt dev_t dev;
791 1.6 deraadt struct uio *uio;
792 1.22 christos int ioflag;
793 1.6 deraadt {
794 1.46 atatat struct tun_softc *tp;
795 1.46 atatat struct ifnet *ifp;
796 1.6 deraadt struct mbuf *top, **mp, *m;
797 1.26 pk struct ifqueue *ifq;
798 1.26 pk struct sockaddr dst;
799 1.70 pk int isr, error = 0, s, tlen, mlen;
800 1.6 deraadt
801 1.70 pk s = splnet();
802 1.46 atatat tp = tun_find_unit(dev);
803 1.46 atatat
804 1.46 atatat /* interface was "destroyed" already */
805 1.70 pk if (tp == NULL) {
806 1.70 pk error = ENXIO;
807 1.70 pk goto out_nolock;
808 1.70 pk }
809 1.70 pk
810 1.70 pk /* Unlock until we've got the data */
811 1.70 pk simple_unlock(&tp->tun_lock);
812 1.70 pk splx(s);
813 1.46 atatat
814 1.46 atatat ifp = &tp->tun_if;
815 1.46 atatat
816 1.24 thorpej TUNDEBUG("%s: tunwrite\n", ifp->if_xname);
817 1.6 deraadt
818 1.26 pk if (tp->tun_flags & TUN_PREPADDR) {
819 1.46 atatat if (uio->uio_resid < sizeof(dst)) {
820 1.70 pk error = EIO;
821 1.70 pk goto out0;
822 1.46 atatat }
823 1.26 pk error = uiomove((caddr_t)&dst, sizeof(dst), uio);
824 1.26 pk if (dst.sa_len > sizeof(dst)) {
825 1.26 pk /* Duh.. */
826 1.26 pk char discard;
827 1.26 pk int n = dst.sa_len - sizeof(dst);
828 1.26 pk while (n--)
829 1.46 atatat if ((error = uiomove(&discard, 1, uio)) != 0) {
830 1.70 pk goto out0;
831 1.46 atatat }
832 1.26 pk }
833 1.26 pk } else {
834 1.26 pk #ifdef INET
835 1.26 pk dst.sa_family = AF_INET;
836 1.26 pk #endif
837 1.26 pk }
838 1.26 pk
839 1.54 simonb if (uio->uio_resid > TUNMTU) {
840 1.37 mjacob TUNDEBUG("%s: len=%lu!\n", ifp->if_xname,
841 1.37 mjacob (unsigned long)uio->uio_resid);
842 1.70 pk error = EIO;
843 1.70 pk goto out0;
844 1.6 deraadt }
845 1.26 pk
846 1.26 pk switch (dst.sa_family) {
847 1.26 pk #ifdef INET
848 1.26 pk case AF_INET:
849 1.26 pk ifq = &ipintrq;
850 1.26 pk isr = NETISR_IP;
851 1.26 pk break;
852 1.26 pk #endif
853 1.26 pk default:
854 1.70 pk error = EAFNOSUPPORT;
855 1.70 pk goto out0;
856 1.26 pk }
857 1.26 pk
858 1.8 deraadt tlen = uio->uio_resid;
859 1.8 deraadt
860 1.8 deraadt /* get a header mbuf */
861 1.8 deraadt MGETHDR(m, M_DONTWAIT, MT_DATA);
862 1.46 atatat if (m == NULL) {
863 1.70 pk error = ENOBUFS;
864 1.70 pk goto out0;
865 1.46 atatat }
866 1.8 deraadt mlen = MHLEN;
867 1.8 deraadt
868 1.68 tron top = NULL;
869 1.6 deraadt mp = ⊤
870 1.6 deraadt while (error == 0 && uio->uio_resid > 0) {
871 1.13 deraadt m->m_len = min(mlen, uio->uio_resid);
872 1.68 tron error = uiomove(mtod(m, caddr_t), m->m_len, uio);
873 1.6 deraadt *mp = m;
874 1.6 deraadt mp = &m->m_next;
875 1.68 tron if (error == 0 && uio->uio_resid > 0) {
876 1.68 tron MGET(m, M_DONTWAIT, MT_DATA);
877 1.68 tron if (m == NULL) {
878 1.8 deraadt error = ENOBUFS;
879 1.8 deraadt break;
880 1.8 deraadt }
881 1.8 deraadt mlen = MLEN;
882 1.8 deraadt }
883 1.6 deraadt }
884 1.6 deraadt if (error) {
885 1.68 tron if (top != NULL)
886 1.8 deraadt m_freem (top);
887 1.19 pk ifp->if_ierrors++;
888 1.70 pk goto out0;
889 1.6 deraadt }
890 1.6 deraadt
891 1.8 deraadt top->m_pkthdr.len = tlen;
892 1.8 deraadt top->m_pkthdr.rcvif = ifp;
893 1.8 deraadt
894 1.8 deraadt #if NBPFILTER > 0
895 1.40 thorpej if (ifp->if_bpf) {
896 1.8 deraadt /*
897 1.8 deraadt * We need to prepend the address family as
898 1.8 deraadt * a four byte field. Cons up a dummy header
899 1.8 deraadt * to pacify bpf. This is safe because bpf
900 1.8 deraadt * will only read from the mbuf (i.e., it won't
901 1.8 deraadt * try to free it or keep a pointer to it).
902 1.8 deraadt */
903 1.8 deraadt struct mbuf m;
904 1.16 cgd u_int32_t af = AF_INET;
905 1.8 deraadt
906 1.60 itojun m.m_flags = 0;
907 1.8 deraadt m.m_next = top;
908 1.16 cgd m.m_len = sizeof(af);
909 1.8 deraadt m.m_data = (char *)⁡
910 1.8 deraadt
911 1.40 thorpej bpf_mtap(ifp->if_bpf, &m);
912 1.6 deraadt }
913 1.8 deraadt #endif
914 1.6 deraadt
915 1.43 thorpej s = splnet();
916 1.70 pk simple_lock(&tp->tun_lock);
917 1.70 pk if ((tp->tun_flags & TUN_INITED) == 0) {
918 1.70 pk /* Interface was destroyed */
919 1.70 pk error = ENXIO;
920 1.70 pk goto out;
921 1.70 pk }
922 1.26 pk if (IF_QFULL(ifq)) {
923 1.26 pk IF_DROP(ifq);
924 1.6 deraadt ifp->if_collisions++;
925 1.6 deraadt m_freem(top);
926 1.70 pk error = ENOBUFS;
927 1.70 pk goto out;
928 1.6 deraadt }
929 1.70 pk
930 1.26 pk IF_ENQUEUE(ifq, top);
931 1.6 deraadt ifp->if_ipackets++;
932 1.58 jdolecek ifp->if_ibytes += tlen;
933 1.26 pk schednetisr(isr);
934 1.70 pk out:
935 1.46 atatat simple_unlock(&tp->tun_lock);
936 1.70 pk out_nolock:
937 1.70 pk splx(s);
938 1.70 pk out0:
939 1.26 pk return (error);
940 1.1 cgd }
941 1.1 cgd
942 1.50 itojun #ifdef ALTQ
943 1.50 itojun /*
944 1.50 itojun * Start packet transmission on the interface.
945 1.50 itojun * when the interface queue is rate-limited by ALTQ or TBR,
946 1.50 itojun * if_start is needed to drain packets from the queue in order
947 1.50 itojun * to notify readers when outgoing packets become ready.
948 1.70 pk *
949 1.70 pk * Should be called at splnet.
950 1.50 itojun */
951 1.50 itojun static void
952 1.50 itojun tunstart(ifp)
953 1.50 itojun struct ifnet *ifp;
954 1.50 itojun {
955 1.50 itojun struct tun_softc *tp = ifp->if_softc;
956 1.50 itojun
957 1.50 itojun if (!ALTQ_IS_ENABLED(&ifp->if_snd) && !TBR_IS_ENABLED(&ifp->if_snd))
958 1.50 itojun return;
959 1.50 itojun
960 1.70 pk simple_lock(&tp->tun_lock);
961 1.70 pk if (!IF_IS_EMPTY(&ifp->if_snd)) {
962 1.50 itojun if (tp->tun_flags & TUN_RWAIT) {
963 1.50 itojun tp->tun_flags &= ~TUN_RWAIT;
964 1.50 itojun wakeup((caddr_t)tp);
965 1.50 itojun }
966 1.64 jdolecek if (tp->tun_flags & TUN_ASYNC && tp->tun_pgid)
967 1.67 cl fownsignal(tp->tun_pgid, SIGIO, POLL_OUT,
968 1.67 cl POLLOUT|POLLWRNORM, NULL);
969 1.70 pk
970 1.50 itojun selwakeup(&tp->tun_rsel);
971 1.50 itojun }
972 1.70 pk simple_unlock(&tp->tun_lock);
973 1.50 itojun }
974 1.50 itojun #endif /* ALTQ */
975 1.1 cgd /*
976 1.27 mycroft * tunpoll - the poll interface, this is only useful on reads
977 1.6 deraadt * really. The write detect always returns true, write never blocks
978 1.6 deraadt * anyway, it either accepts the packet or drops it.
979 1.6 deraadt */
980 1.6 deraadt int
981 1.63 fvdl tunpoll(dev, events, p)
982 1.6 deraadt dev_t dev;
983 1.27 mycroft int events;
984 1.63 fvdl struct proc *p;
985 1.6 deraadt {
986 1.46 atatat struct tun_softc *tp;
987 1.46 atatat struct ifnet *ifp;
988 1.46 atatat int s, revents = 0;
989 1.46 atatat
990 1.70 pk s = splnet();
991 1.46 atatat tp = tun_find_unit(dev);
992 1.46 atatat
993 1.46 atatat /* interface was "destroyed" already */
994 1.46 atatat if (tp == NULL)
995 1.70 pk goto out_nolock;
996 1.46 atatat
997 1.46 atatat ifp = &tp->tun_if;
998 1.6 deraadt
999 1.27 mycroft TUNDEBUG("%s: tunpoll\n", ifp->if_xname);
1000 1.6 deraadt
1001 1.35 veego if (events & (POLLIN | POLLRDNORM)) {
1002 1.70 pk if (!IFQ_IS_EMPTY(&ifp->if_snd)) {
1003 1.27 mycroft TUNDEBUG("%s: tunpoll q=%d\n", ifp->if_xname,
1004 1.24 thorpej ifp->if_snd.ifq_len);
1005 1.27 mycroft revents |= events & (POLLIN | POLLRDNORM);
1006 1.27 mycroft } else {
1007 1.27 mycroft TUNDEBUG("%s: tunpoll waiting\n", ifp->if_xname);
1008 1.63 fvdl selrecord(p, &tp->tun_rsel);
1009 1.6 deraadt }
1010 1.35 veego }
1011 1.27 mycroft
1012 1.27 mycroft if (events & (POLLOUT | POLLWRNORM))
1013 1.27 mycroft revents |= events & (POLLOUT | POLLWRNORM);
1014 1.27 mycroft
1015 1.70 pk simple_unlock(&tp->tun_lock);
1016 1.70 pk out_nolock:
1017 1.6 deraadt splx(s);
1018 1.27 mycroft return (revents);
1019 1.56 jdolecek }
1020 1.56 jdolecek
1021 1.56 jdolecek static void
1022 1.56 jdolecek filt_tunrdetach(struct knote *kn)
1023 1.56 jdolecek {
1024 1.56 jdolecek struct tun_softc *tp = kn->kn_hook;
1025 1.56 jdolecek int s;
1026 1.56 jdolecek
1027 1.56 jdolecek s = splnet();
1028 1.57 christos SLIST_REMOVE(&tp->tun_rsel.sel_klist, kn, knote, kn_selnext);
1029 1.56 jdolecek splx(s);
1030 1.56 jdolecek }
1031 1.56 jdolecek
1032 1.56 jdolecek static int
1033 1.56 jdolecek filt_tunread(struct knote *kn, long hint)
1034 1.56 jdolecek {
1035 1.56 jdolecek struct tun_softc *tp = kn->kn_hook;
1036 1.56 jdolecek struct ifnet *ifp = &tp->tun_if;
1037 1.56 jdolecek struct mbuf *m;
1038 1.56 jdolecek int s;
1039 1.56 jdolecek
1040 1.56 jdolecek s = splnet();
1041 1.56 jdolecek IF_POLL(&ifp->if_snd, m);
1042 1.56 jdolecek if (m == NULL) {
1043 1.56 jdolecek splx(s);
1044 1.56 jdolecek return (0);
1045 1.56 jdolecek }
1046 1.56 jdolecek
1047 1.56 jdolecek for (kn->kn_data = 0; m != NULL; m = m->m_next)
1048 1.56 jdolecek kn->kn_data += m->m_len;
1049 1.56 jdolecek
1050 1.56 jdolecek splx(s);
1051 1.56 jdolecek return (1);
1052 1.56 jdolecek }
1053 1.56 jdolecek
1054 1.56 jdolecek static const struct filterops tunread_filtops =
1055 1.56 jdolecek { 1, NULL, filt_tunrdetach, filt_tunread };
1056 1.56 jdolecek
1057 1.56 jdolecek static const struct filterops tun_seltrue_filtops =
1058 1.56 jdolecek { 1, NULL, filt_tunrdetach, filt_seltrue };
1059 1.56 jdolecek
1060 1.56 jdolecek int
1061 1.56 jdolecek tunkqfilter(dev_t dev, struct knote *kn)
1062 1.56 jdolecek {
1063 1.70 pk struct tun_softc *tp;
1064 1.56 jdolecek struct klist *klist;
1065 1.70 pk int rv = 0, s;
1066 1.70 pk
1067 1.70 pk s = splnet();
1068 1.70 pk tp = tun_find_unit(dev);
1069 1.70 pk if (tp == NULL)
1070 1.70 pk goto out_nolock;
1071 1.56 jdolecek
1072 1.56 jdolecek switch (kn->kn_filter) {
1073 1.56 jdolecek case EVFILT_READ:
1074 1.57 christos klist = &tp->tun_rsel.sel_klist;
1075 1.56 jdolecek kn->kn_fop = &tunread_filtops;
1076 1.56 jdolecek break;
1077 1.56 jdolecek
1078 1.56 jdolecek case EVFILT_WRITE:
1079 1.57 christos klist = &tp->tun_rsel.sel_klist;
1080 1.56 jdolecek kn->kn_fop = &tun_seltrue_filtops;
1081 1.56 jdolecek break;
1082 1.56 jdolecek
1083 1.56 jdolecek default:
1084 1.70 pk rv = 1;
1085 1.70 pk goto out;
1086 1.56 jdolecek }
1087 1.56 jdolecek
1088 1.56 jdolecek kn->kn_hook = tp;
1089 1.56 jdolecek
1090 1.56 jdolecek SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1091 1.70 pk
1092 1.70 pk out:
1093 1.70 pk simple_unlock(&tp->tun_lock);
1094 1.70 pk out_nolock:
1095 1.56 jdolecek splx(s);
1096 1.70 pk return (rv);
1097 1.1 cgd }
1098