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