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