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