if_tun.c revision 1.17 1 1.17 mycroft /* $NetBSD: if_tun.c,v 1.17 1995/06/12 01:09:20 mycroft 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.1 cgd * user process to have it's wicked way with. This driver has it's
12 1.1 cgd * roots in a similar driver written by Phil Cockcroft (formerly) at
13 1.1 cgd * UCL. This driver is based much more on read/write/select mode of
14 1.1 cgd * operation though.
15 1.1 cgd */
16 1.1 cgd
17 1.8 deraadt #include "tun.h"
18 1.8 deraadt #if NTUN > 0
19 1.8 deraadt
20 1.6 deraadt #include <sys/param.h>
21 1.8 deraadt #include <sys/proc.h>
22 1.6 deraadt #include <sys/systm.h>
23 1.6 deraadt #include <sys/mbuf.h>
24 1.6 deraadt #include <sys/buf.h>
25 1.6 deraadt #include <sys/protosw.h>
26 1.6 deraadt #include <sys/socket.h>
27 1.6 deraadt #include <sys/ioctl.h>
28 1.6 deraadt #include <sys/errno.h>
29 1.6 deraadt #include <sys/syslog.h>
30 1.6 deraadt #include <sys/select.h>
31 1.8 deraadt #include <sys/file.h>
32 1.9 deraadt
33 1.9 deraadt #include <machine/cpu.h>
34 1.6 deraadt
35 1.6 deraadt #include <net/if.h>
36 1.6 deraadt #include <net/netisr.h>
37 1.6 deraadt #include <net/route.h>
38 1.1 cgd
39 1.1 cgd #ifdef INET
40 1.6 deraadt #include <netinet/in.h>
41 1.6 deraadt #include <netinet/in_systm.h>
42 1.6 deraadt #include <netinet/in_var.h>
43 1.6 deraadt #include <netinet/ip.h>
44 1.6 deraadt #include <netinet/if_ether.h>
45 1.1 cgd #endif
46 1.1 cgd
47 1.1 cgd #ifdef NS
48 1.6 deraadt #include <netns/ns.h>
49 1.6 deraadt #include <netns/ns_if.h>
50 1.1 cgd #endif
51 1.1 cgd
52 1.8 deraadt #include "bpfilter.h"
53 1.8 deraadt #if NBPFILTER > 0
54 1.8 deraadt #include <sys/time.h>
55 1.8 deraadt #include <net/bpf.h>
56 1.8 deraadt #endif
57 1.8 deraadt
58 1.8 deraadt #include <net/if_tun.h>
59 1.8 deraadt
60 1.6 deraadt #define TUNDEBUG if (tundebug) printf
61 1.6 deraadt int tundebug = 0;
62 1.1 cgd
63 1.6 deraadt struct tun_softc tunctl[NTUN];
64 1.6 deraadt extern int ifqmaxlen;
65 1.6 deraadt
66 1.8 deraadt int tunopen __P((dev_t, int, int, struct proc *));
67 1.6 deraadt int tunclose __P((dev_t, int));
68 1.12 deraadt int tunoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
69 1.12 deraadt struct rtentry *rt));
70 1.6 deraadt int tunread __P((dev_t, struct uio *));
71 1.6 deraadt int tunwrite __P((dev_t, struct uio *));
72 1.15 cgd int tuncioctl __P((dev_t, u_long, caddr_t, int, struct proc *));
73 1.15 cgd int tunioctl __P((struct ifnet *, u_long, caddr_t));
74 1.8 deraadt int tunselect __P((dev_t, int));
75 1.8 deraadt void tunattach __P((int));
76 1.6 deraadt
77 1.6 deraadt static int tuninit __P((int));
78 1.6 deraadt
79 1.8 deraadt void
80 1.8 deraadt tunattach(unused)
81 1.8 deraadt int unused;
82 1.8 deraadt {
83 1.8 deraadt register int i;
84 1.8 deraadt struct ifnet *ifp;
85 1.8 deraadt struct sockaddr_in *sin;
86 1.8 deraadt
87 1.8 deraadt for (i = 0; i < NTUN; i++) {
88 1.8 deraadt tunctl[i].tun_flags = TUN_INITED;
89 1.8 deraadt
90 1.8 deraadt ifp = &tunctl[i].tun_if;
91 1.8 deraadt ifp->if_unit = i;
92 1.8 deraadt ifp->if_name = "tun";
93 1.8 deraadt ifp->if_mtu = TUNMTU;
94 1.8 deraadt ifp->if_ioctl = tunioctl;
95 1.8 deraadt ifp->if_output = tunoutput;
96 1.8 deraadt ifp->if_flags = IFF_POINTOPOINT;
97 1.8 deraadt ifp->if_snd.ifq_maxlen = ifqmaxlen;
98 1.8 deraadt ifp->if_collisions = 0;
99 1.8 deraadt ifp->if_ierrors = 0;
100 1.8 deraadt ifp->if_oerrors = 0;
101 1.8 deraadt ifp->if_ipackets = 0;
102 1.8 deraadt ifp->if_opackets = 0;
103 1.8 deraadt if_attach(ifp);
104 1.8 deraadt #if NBPFILTER > 0
105 1.16 cgd bpfattach(&tunctl[i].tun_bpf, ifp, DLT_NULL, sizeof(u_int32_t));
106 1.8 deraadt #endif
107 1.8 deraadt }
108 1.8 deraadt }
109 1.8 deraadt
110 1.6 deraadt /*
111 1.6 deraadt * tunnel open - must be superuser & the device must be
112 1.6 deraadt * configured in
113 1.6 deraadt */
114 1.6 deraadt int
115 1.6 deraadt tunopen(dev, flag, mode, p)
116 1.6 deraadt dev_t dev;
117 1.6 deraadt int flag, mode;
118 1.6 deraadt struct proc *p;
119 1.6 deraadt {
120 1.6 deraadt struct ifnet *ifp;
121 1.8 deraadt struct tun_softc *tp;
122 1.6 deraadt register int unit, error;
123 1.1 cgd
124 1.5 deraadt if (error = suser(p->p_ucred, &p->p_acflag))
125 1.5 deraadt return (error);
126 1.5 deraadt
127 1.6 deraadt if ((unit = minor(dev)) >= NTUN)
128 1.6 deraadt return (ENXIO);
129 1.6 deraadt tp = &tunctl[unit];
130 1.6 deraadt if (tp->tun_flags & TUN_OPEN)
131 1.6 deraadt return ENXIO;
132 1.6 deraadt ifp = &tp->tun_if;
133 1.6 deraadt tp->tun_flags |= TUN_OPEN;
134 1.6 deraadt TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
135 1.6 deraadt return (0);
136 1.1 cgd }
137 1.1 cgd
138 1.6 deraadt /*
139 1.6 deraadt * tunclose - close the device - mark i/f down & delete
140 1.6 deraadt * routing info
141 1.6 deraadt */
142 1.6 deraadt int
143 1.6 deraadt tunclose(dev, flag)
144 1.6 deraadt dev_t dev;
145 1.6 deraadt int flag;
146 1.6 deraadt {
147 1.8 deraadt register int unit = minor(dev), s;
148 1.8 deraadt struct tun_softc *tp = &tunctl[unit];
149 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
150 1.6 deraadt struct mbuf *m;
151 1.6 deraadt
152 1.10 andrew tp->tun_flags &= ~TUN_OPEN;
153 1.6 deraadt
154 1.6 deraadt /*
155 1.6 deraadt * junk all pending output
156 1.6 deraadt */
157 1.6 deraadt do {
158 1.6 deraadt s = splimp();
159 1.6 deraadt IF_DEQUEUE(&ifp->if_snd, m);
160 1.6 deraadt splx(s);
161 1.6 deraadt if (m)
162 1.6 deraadt m_freem(m);
163 1.6 deraadt } while (m);
164 1.6 deraadt
165 1.6 deraadt if (ifp->if_flags & IFF_UP) {
166 1.6 deraadt s = splimp();
167 1.6 deraadt if_down(ifp);
168 1.8 deraadt if (ifp->if_flags & IFF_RUNNING) {
169 1.17 mycroft /* find internet addresses and delete routes */
170 1.17 mycroft register struct ifaddr *ifa;
171 1.17 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
172 1.17 mycroft ifa = ifa->ifa_list.le_next) {
173 1.17 mycroft if (ifa->ifa_addr->sa_family == AF_INET)
174 1.17 mycroft rtinit(ifa, (int)RTM_DELETE,
175 1.17 mycroft p->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
176 1.11 deraadt }
177 1.8 deraadt }
178 1.6 deraadt splx(s);
179 1.6 deraadt }
180 1.6 deraadt tp->tun_pgrp = 0;
181 1.7 deraadt selwakeup(&tp->tun_rsel);
182 1.6 deraadt
183 1.6 deraadt TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
184 1.6 deraadt return (0);
185 1.1 cgd }
186 1.1 cgd
187 1.6 deraadt static int
188 1.6 deraadt tuninit(unit)
189 1.6 deraadt int unit;
190 1.6 deraadt {
191 1.8 deraadt struct tun_softc *tp = &tunctl[unit];
192 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
193 1.8 deraadt register struct ifaddr *ifa;
194 1.8 deraadt
195 1.8 deraadt TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
196 1.6 deraadt
197 1.6 deraadt ifp->if_flags |= IFF_UP | IFF_RUNNING;
198 1.8 deraadt
199 1.17 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa != 0;
200 1.17 mycroft ifa = ifa->ifa_list.le_next) {
201 1.11 deraadt if (ifa->ifa_addr->sa_family == AF_INET) {
202 1.17 mycroft struct sockaddr_in *sin;
203 1.11 deraadt
204 1.17 mycroft sin = satosin(ifa->ifa_addr);
205 1.17 mycroft if (sin && sin->sin_addr.s_addr)
206 1.17 mycroft tp->tun_flags |= TUN_IASET;
207 1.17 mycroft
208 1.17 mycroft sin = satosin(ifa->ifa_dstaddr);
209 1.17 mycroft if (sin && sin->sin_addr.s_addr)
210 1.17 mycroft tp->tun_flags |= TUN_DSTADDR;
211 1.11 deraadt }
212 1.8 deraadt
213 1.6 deraadt return 0;
214 1.1 cgd }
215 1.1 cgd
216 1.1 cgd /*
217 1.1 cgd * Process an ioctl request.
218 1.1 cgd */
219 1.1 cgd int
220 1.12 deraadt tunioctl(ifp, cmd, data)
221 1.6 deraadt struct ifnet *ifp;
222 1.15 cgd u_long cmd;
223 1.6 deraadt caddr_t data;
224 1.6 deraadt {
225 1.8 deraadt struct tun_softc *tp = &tunctl[ifp->if_unit];
226 1.6 deraadt int error = 0, s;
227 1.6 deraadt
228 1.6 deraadt s = splimp();
229 1.6 deraadt switch(cmd) {
230 1.6 deraadt case SIOCSIFADDR:
231 1.6 deraadt tuninit(ifp->if_unit);
232 1.11 deraadt TUNDEBUG("%s%d: address set\n",
233 1.11 deraadt ifp->if_name, ifp->if_unit);
234 1.6 deraadt break;
235 1.6 deraadt case SIOCSIFDSTADDR:
236 1.11 deraadt tuninit(ifp->if_unit);
237 1.11 deraadt TUNDEBUG("%s%d: destination address set\n",
238 1.11 deraadt ifp->if_name, ifp->if_unit);
239 1.6 deraadt break;
240 1.6 deraadt default:
241 1.6 deraadt error = EINVAL;
242 1.6 deraadt }
243 1.6 deraadt splx(s);
244 1.6 deraadt return (error);
245 1.1 cgd }
246 1.1 cgd
247 1.1 cgd /*
248 1.1 cgd * tunoutput - queue packets from higher level ready to put out.
249 1.1 cgd */
250 1.6 deraadt int
251 1.12 deraadt tunoutput(ifp, m0, dst, rt)
252 1.6 deraadt struct ifnet *ifp;
253 1.6 deraadt struct mbuf *m0;
254 1.6 deraadt struct sockaddr *dst;
255 1.12 deraadt struct rtentry *rt;
256 1.6 deraadt {
257 1.8 deraadt struct tun_softc *tp = &tunctl[ifp->if_unit];
258 1.6 deraadt struct proc *p;
259 1.6 deraadt int s;
260 1.6 deraadt
261 1.6 deraadt TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
262 1.1 cgd
263 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
264 1.8 deraadt TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
265 1.8 deraadt ifp->if_unit, tp->tun_flags);
266 1.8 deraadt m_freem (m0);
267 1.8 deraadt return EHOSTDOWN;
268 1.8 deraadt }
269 1.8 deraadt
270 1.8 deraadt #if NBPFILTER > 0
271 1.8 deraadt if (tp->tun_bpf) {
272 1.8 deraadt /*
273 1.8 deraadt * We need to prepend the address family as
274 1.8 deraadt * a four byte field. Cons up a dummy header
275 1.8 deraadt * to pacify bpf. This is safe because bpf
276 1.8 deraadt * will only read from the mbuf (i.e., it won't
277 1.8 deraadt * try to free it or keep a pointer to it).
278 1.8 deraadt */
279 1.8 deraadt struct mbuf m;
280 1.16 cgd u_int32_t af = dst->sa_family;
281 1.8 deraadt
282 1.8 deraadt m.m_next = m0;
283 1.16 cgd m.m_len = sizeof(af);
284 1.8 deraadt m.m_data = (char *)⁡
285 1.8 deraadt
286 1.8 deraadt bpf_mtap(tp->tun_bpf, &m);
287 1.8 deraadt }
288 1.8 deraadt #endif
289 1.8 deraadt
290 1.6 deraadt switch(dst->sa_family) {
291 1.1 cgd #ifdef INET
292 1.6 deraadt case AF_INET:
293 1.6 deraadt s = splimp();
294 1.6 deraadt if (IF_QFULL(&ifp->if_snd)) {
295 1.6 deraadt IF_DROP(&ifp->if_snd);
296 1.6 deraadt m_freem(m0);
297 1.6 deraadt splx(s);
298 1.6 deraadt ifp->if_collisions++;
299 1.6 deraadt return (ENOBUFS);
300 1.6 deraadt }
301 1.6 deraadt IF_ENQUEUE(&ifp->if_snd, m0);
302 1.6 deraadt splx(s);
303 1.6 deraadt ifp->if_opackets++;
304 1.6 deraadt break;
305 1.1 cgd #endif
306 1.6 deraadt default:
307 1.6 deraadt m_freem(m0);
308 1.6 deraadt return EAFNOSUPPORT;
309 1.6 deraadt }
310 1.6 deraadt
311 1.6 deraadt if (tp->tun_flags & TUN_RWAIT) {
312 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
313 1.6 deraadt wakeup((caddr_t)tp);
314 1.6 deraadt }
315 1.6 deraadt if (tp->tun_flags & TUN_ASYNC && tp->tun_pgrp) {
316 1.6 deraadt if (tp->tun_pgrp > 0)
317 1.6 deraadt gsignal(tp->tun_pgrp, SIGIO);
318 1.6 deraadt else if (p = pfind(-tp->tun_pgrp))
319 1.6 deraadt psignal(p, SIGIO);
320 1.6 deraadt }
321 1.6 deraadt selwakeup(&tp->tun_rsel);
322 1.6 deraadt return 0;
323 1.1 cgd }
324 1.1 cgd
325 1.1 cgd /*
326 1.1 cgd * the cdevsw interface is now pretty minimal.
327 1.1 cgd */
328 1.6 deraadt int
329 1.12 deraadt tuncioctl(dev, cmd, data, flag, p)
330 1.6 deraadt dev_t dev;
331 1.15 cgd u_long cmd;
332 1.6 deraadt caddr_t data;
333 1.6 deraadt int flag;
334 1.12 deraadt struct proc *p;
335 1.6 deraadt {
336 1.6 deraadt int unit = minor(dev), s;
337 1.8 deraadt struct tun_softc *tp = &tunctl[unit];
338 1.6 deraadt
339 1.6 deraadt switch (cmd) {
340 1.6 deraadt case TUNSDEBUG:
341 1.6 deraadt tundebug = *(int *)data;
342 1.6 deraadt break;
343 1.6 deraadt case TUNGDEBUG:
344 1.6 deraadt *(int *)data = tundebug;
345 1.6 deraadt break;
346 1.6 deraadt case FIONBIO:
347 1.6 deraadt if (*(int *)data)
348 1.6 deraadt tp->tun_flags |= TUN_NBIO;
349 1.6 deraadt else
350 1.6 deraadt tp->tun_flags &= ~TUN_NBIO;
351 1.6 deraadt break;
352 1.6 deraadt case FIOASYNC:
353 1.6 deraadt if (*(int *)data)
354 1.6 deraadt tp->tun_flags |= TUN_ASYNC;
355 1.6 deraadt else
356 1.6 deraadt tp->tun_flags &= ~TUN_ASYNC;
357 1.6 deraadt break;
358 1.6 deraadt case FIONREAD:
359 1.6 deraadt s = splimp();
360 1.6 deraadt if (tp->tun_if.if_snd.ifq_head)
361 1.6 deraadt *(int *)data = tp->tun_if.if_snd.ifq_head->m_len;
362 1.6 deraadt else
363 1.6 deraadt *(int *)data = 0;
364 1.6 deraadt splx(s);
365 1.6 deraadt break;
366 1.6 deraadt case TIOCSPGRP:
367 1.6 deraadt tp->tun_pgrp = *(int *)data;
368 1.6 deraadt break;
369 1.6 deraadt case TIOCGPGRP:
370 1.6 deraadt *(int *)data = tp->tun_pgrp;
371 1.6 deraadt break;
372 1.6 deraadt default:
373 1.6 deraadt return (ENOTTY);
374 1.6 deraadt }
375 1.6 deraadt return (0);
376 1.1 cgd }
377 1.1 cgd
378 1.1 cgd /*
379 1.6 deraadt * The cdevsw read interface - reads a packet at a time, or at
380 1.6 deraadt * least as much of a packet as can be read.
381 1.1 cgd */
382 1.6 deraadt int
383 1.6 deraadt tunread(dev, uio)
384 1.6 deraadt dev_t dev;
385 1.6 deraadt struct uio *uio;
386 1.6 deraadt {
387 1.8 deraadt int unit = minor(dev);
388 1.8 deraadt struct tun_softc *tp = &tunctl[unit];
389 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
390 1.6 deraadt struct mbuf *m, *m0;
391 1.6 deraadt int error=0, len, s;
392 1.6 deraadt
393 1.6 deraadt TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
394 1.8 deraadt if ((tp->tun_flags & TUN_READY) != TUN_READY) {
395 1.8 deraadt TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
396 1.8 deraadt ifp->if_unit, tp->tun_flags);
397 1.8 deraadt return EHOSTDOWN;
398 1.8 deraadt }
399 1.8 deraadt
400 1.6 deraadt tp->tun_flags &= ~TUN_RWAIT;
401 1.6 deraadt
402 1.6 deraadt s = splimp();
403 1.6 deraadt do {
404 1.6 deraadt IF_DEQUEUE(&ifp->if_snd, m0);
405 1.6 deraadt if (m0 == 0) {
406 1.6 deraadt if (tp->tun_flags & TUN_NBIO) {
407 1.6 deraadt splx(s);
408 1.6 deraadt return EWOULDBLOCK;
409 1.6 deraadt }
410 1.6 deraadt tp->tun_flags |= TUN_RWAIT;
411 1.6 deraadt tsleep((caddr_t)tp, PZERO + 1, "tunread", 0);
412 1.6 deraadt }
413 1.6 deraadt } while (m0 == 0);
414 1.6 deraadt splx(s);
415 1.6 deraadt
416 1.6 deraadt while (m0 && uio->uio_resid > 0 && error == 0) {
417 1.13 deraadt len = min(uio->uio_resid, m0->m_len);
418 1.6 deraadt if (len == 0)
419 1.6 deraadt break;
420 1.8 deraadt error = uiomove(mtod(m0, caddr_t), len, uio);
421 1.6 deraadt MFREE(m0, m);
422 1.6 deraadt m0 = m;
423 1.6 deraadt }
424 1.6 deraadt
425 1.6 deraadt if (m0) {
426 1.6 deraadt TUNDEBUG("Dropping mbuf\n");
427 1.6 deraadt m_freem(m0);
428 1.6 deraadt }
429 1.6 deraadt return error;
430 1.1 cgd }
431 1.1 cgd
432 1.1 cgd /*
433 1.1 cgd * the cdevsw write interface - an atomic write is a packet - or else!
434 1.1 cgd */
435 1.6 deraadt int
436 1.6 deraadt tunwrite(dev, uio)
437 1.8 deraadt dev_t dev;
438 1.6 deraadt struct uio *uio;
439 1.6 deraadt {
440 1.6 deraadt int unit = minor (dev);
441 1.8 deraadt struct ifnet *ifp = &tunctl[unit].tun_if;
442 1.6 deraadt struct mbuf *top, **mp, *m;
443 1.8 deraadt int error=0, s, tlen, mlen;
444 1.6 deraadt
445 1.6 deraadt TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
446 1.6 deraadt
447 1.6 deraadt if (uio->uio_resid < 0 || uio->uio_resid > TUNMTU) {
448 1.6 deraadt TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
449 1.6 deraadt uio->uio_resid);
450 1.6 deraadt return EIO;
451 1.6 deraadt }
452 1.8 deraadt tlen = uio->uio_resid;
453 1.8 deraadt
454 1.8 deraadt /* get a header mbuf */
455 1.8 deraadt MGETHDR(m, M_DONTWAIT, MT_DATA);
456 1.8 deraadt if (m == NULL)
457 1.8 deraadt return ENOBUFS;
458 1.8 deraadt mlen = MHLEN;
459 1.8 deraadt
460 1.6 deraadt top = 0;
461 1.6 deraadt mp = ⊤
462 1.6 deraadt while (error == 0 && uio->uio_resid > 0) {
463 1.13 deraadt m->m_len = min(mlen, uio->uio_resid);
464 1.8 deraadt error = uiomove(mtod (m, caddr_t), m->m_len, uio);
465 1.6 deraadt *mp = m;
466 1.6 deraadt mp = &m->m_next;
467 1.8 deraadt if (uio->uio_resid > 0) {
468 1.8 deraadt MGET (m, M_DONTWAIT, MT_DATA);
469 1.8 deraadt if (m == 0) {
470 1.8 deraadt error = ENOBUFS;
471 1.8 deraadt break;
472 1.8 deraadt }
473 1.8 deraadt mlen = MLEN;
474 1.8 deraadt }
475 1.6 deraadt }
476 1.6 deraadt if (error) {
477 1.6 deraadt if (top)
478 1.8 deraadt m_freem (top);
479 1.6 deraadt return error;
480 1.6 deraadt }
481 1.6 deraadt
482 1.8 deraadt top->m_pkthdr.len = tlen;
483 1.8 deraadt top->m_pkthdr.rcvif = ifp;
484 1.8 deraadt
485 1.8 deraadt #if NBPFILTER > 0
486 1.8 deraadt if (tunctl[unit].tun_bpf) {
487 1.8 deraadt /*
488 1.8 deraadt * We need to prepend the address family as
489 1.8 deraadt * a four byte field. Cons up a dummy header
490 1.8 deraadt * to pacify bpf. This is safe because bpf
491 1.8 deraadt * will only read from the mbuf (i.e., it won't
492 1.8 deraadt * try to free it or keep a pointer to it).
493 1.8 deraadt */
494 1.8 deraadt struct mbuf m;
495 1.16 cgd u_int32_t af = AF_INET;
496 1.8 deraadt
497 1.8 deraadt m.m_next = top;
498 1.16 cgd m.m_len = sizeof(af);
499 1.8 deraadt m.m_data = (char *)⁡
500 1.8 deraadt
501 1.8 deraadt bpf_mtap(tunctl[unit].tun_bpf, &m);
502 1.6 deraadt }
503 1.8 deraadt #endif
504 1.6 deraadt
505 1.6 deraadt s = splimp();
506 1.6 deraadt if (IF_QFULL (&ipintrq)) {
507 1.6 deraadt IF_DROP(&ipintrq);
508 1.6 deraadt splx(s);
509 1.6 deraadt ifp->if_collisions++;
510 1.6 deraadt m_freem(top);
511 1.6 deraadt return ENOBUFS;
512 1.6 deraadt }
513 1.6 deraadt IF_ENQUEUE(&ipintrq, top);
514 1.6 deraadt splx(s);
515 1.6 deraadt ifp->if_ipackets++;
516 1.6 deraadt schednetisr(NETISR_IP);
517 1.6 deraadt return error;
518 1.1 cgd }
519 1.1 cgd
520 1.1 cgd /*
521 1.6 deraadt * tunselect - the select interface, this is only useful on reads
522 1.6 deraadt * really. The write detect always returns true, write never blocks
523 1.6 deraadt * anyway, it either accepts the packet or drops it.
524 1.6 deraadt */
525 1.6 deraadt int
526 1.8 deraadt tunselect(dev, rw)
527 1.6 deraadt dev_t dev;
528 1.6 deraadt int rw;
529 1.6 deraadt {
530 1.6 deraadt int unit = minor(dev), s;
531 1.8 deraadt struct tun_softc *tp = &tunctl[unit];
532 1.6 deraadt struct ifnet *ifp = &tp->tun_if;
533 1.6 deraadt
534 1.6 deraadt s = splimp();
535 1.6 deraadt TUNDEBUG("%s%d: tunselect\n", ifp->if_name, ifp->if_unit);
536 1.6 deraadt
537 1.6 deraadt switch (rw) {
538 1.6 deraadt case FREAD:
539 1.6 deraadt if (ifp->if_snd.ifq_len > 0) {
540 1.6 deraadt splx(s);
541 1.6 deraadt TUNDEBUG("%s%d: tunselect q=%d\n", ifp->if_name,
542 1.6 deraadt ifp->if_unit, ifp->if_snd.ifq_len);
543 1.6 deraadt return 1;
544 1.6 deraadt }
545 1.8 deraadt selrecord(curproc, &tp->tun_rsel);
546 1.6 deraadt break;
547 1.6 deraadt case FWRITE:
548 1.6 deraadt splx(s);
549 1.6 deraadt return 1;
550 1.6 deraadt }
551 1.6 deraadt splx(s);
552 1.6 deraadt TUNDEBUG("%s%d: tunselect waiting\n", ifp->if_name, ifp->if_unit);
553 1.6 deraadt return 0;
554 1.1 cgd }
555 1.8 deraadt
556 1.8 deraadt #endif /* NTUN */
557