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