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