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