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