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