]> git.saurik.com Git - apple/xnu.git/blob - bsd/net/if_tun.c
833251c07ce469d9032fb21cb29ee4e00c1a7734
[apple/xnu.git] / bsd / net / if_tun.c
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /* $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $ */
23
24 /*
25 * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
26 * Nottingham University 1987.
27 *
28 * This source may be freely distributed, however I would be interested
29 * in any changes that are made.
30 *
31 * This driver takes packets off the IP i/f and hands them up to a
32 * user process to have its wicked way with. This driver has it's
33 * roots in a similar driver written by Phil Cockcroft (formerly) at
34 * UCL. This driver is based much more on read/write/poll mode of
35 * operation though.
36 */
37
38 #include "tun.h"
39 #if NTUN > 0
40
41 #include "opt_devfs.h"
42 #include "opt_inet.h"
43
44 #include <sys/param.h>
45 #include <sys/proc.h>
46 #include <sys/systm.h>
47 #include <sys/mbuf.h>
48 #include <sys/socket.h>
49 #include <sys/filio.h>
50 #include <sys/sockio.h>
51 #include <sys/ttycom.h>
52 #include <sys/poll.h>
53 #include <sys/signalvar.h>
54 #include <sys/filedesc.h>
55 #include <sys/kernel.h>
56 #include <sys/sysctl.h>
57 #if DEVFS
58 #include <sys/devfsext.h>
59 #endif /*DEVFS*/
60 #include <sys/conf.h>
61 #include <sys/uio.h>
62 #include <sys/vnode.h>
63
64 #include <net/if.h>
65 #include <net/if_types.h>
66 #include <net/netisr.h>
67 #include <net/route.h>
68
69 #if INET
70 #include <netinet/in.h>
71 #include <netinet/in_var.h>
72 #endif
73
74 #if INET6
75 #include <netinet/ip6.h>
76 #include <netinet6/ip6_var.h>
77 #include <netinet6/in6_ifattach.h>
78 #endif /* INET6 */
79
80 #if NS
81 #include <netns/ns.h>
82 #include <netns/ns_if.h>
83 #endif
84
85 #include "bpfilter.h"
86 #if NBPFILTER > 0
87 #include <net/bpf.h>
88 #endif
89
90 #include <net/if_tunvar.h>
91 #include <net/if_tun.h>
92
93 static void tunattach __P((void *));
94 PSEUDO_SET(tunattach, if_tun);
95
96 #define TUNDEBUG if (tundebug) printf
97 static int tundebug = 0;
98 SYSCTL_INT(_debug, OID_AUTO, if_tun_debug, CTLFLAG_RW, &tundebug, 0, "");
99
100 static struct tun_softc tunctl[NTUN];
101
102 static int tunoutput __P((struct ifnet *, struct mbuf *, struct sockaddr *,
103 struct rtentry *rt));
104 static int tunifioctl __P((struct ifnet *, u_long, caddr_t));
105 static int tuninit __P((int, int, u_char));
106
107 static d_open_t tunopen;
108 static d_close_t tunclose;
109 static d_read_t tunread;
110 static d_write_t tunwrite;
111 static d_ioctl_t tunioctl;
112 static d_poll_t tunpoll;
113
114 #define CDEV_MAJOR 52
115 static struct cdevsw tun_cdevsw = {
116 tunopen, tunclose, tunread, tunwrite,
117 tunioctl, nullstop, noreset, nodevtotty,
118 tunpoll, nommap, nostrategy, "tun", NULL, -1
119 };
120
121
122 static int tun_devsw_installed;
123 #if DEVFS
124 static void *tun_devfs_token[NTUN];
125 #endif
126
127 #define minor_val(n) ((((n) & ~0xff) << 8) | ((n) & 0xff))
128 #define dev_val(n) (((n) >> 8) | ((n) & 0xff))
129
130 static void
131 tunattach(dummy)
132 void *dummy;
133 {
134 register int i;
135 struct ifnet *ifp;
136 dev_t dev;
137
138 if ( tun_devsw_installed )
139 return;
140 dev = makedev(CDEV_MAJOR, 0);
141 cdevsw_add(&dev, &tun_cdevsw, NULL);
142 tun_devsw_installed = 1;
143 for ( i = 0; i < NTUN; i++ ) {
144 #if DEVFS
145 tun_devfs_token[i] = devfs_add_devswf(&tun_cdevsw, minor_val(i),
146 DV_CHR, UID_UUCP,
147 GID_DIALER, 0600,
148 "tun%d", i);
149 #endif
150 tunctl[i].tun_flags = TUN_INITED;
151
152 ifp = &tunctl[i].tun_if;
153 ifp->if_unit = i;
154 ifp->if_name = "tun";
155 ifp->if_family = APPLE_IF_FAM_TUN;
156 ifp->if_mtu = TUNMTU;
157 ifp->if_ioctl = tunifioctl;
158 ifp->if_output = tunoutput;
159 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
160 ifp->if_type = IFT_PPP; /* necessary init value for IPv6 lladdr auto conf */
161 ifp->if_snd.ifq_maxlen = ifqmaxlen;
162 if_attach(ifp);
163 #if NBPFILTER > 0
164 bpfattach(ifp, DLT_NULL, sizeof(u_int));
165 #endif
166 }
167 }
168
169 /*
170 * tunnel open - must be superuser & the device must be
171 * configured in
172 */
173 static int
174 tunopen(dev, flag, mode, p)
175 dev_t dev;
176 int flag, mode;
177 struct proc *p;
178 {
179 struct ifnet *ifp;
180 struct tun_softc *tp;
181 register int unit, error;
182
183 error = suser(p->p_ucred, &p->p_acflag);
184 if (error)
185 return (error);
186
187 if ((unit = dev_val(minor(dev))) >= NTUN)
188 return (ENXIO);
189 tp = &tunctl[unit];
190 if (tp->tun_flags & TUN_OPEN)
191 return EBUSY;
192 ifp = &tp->tun_if;
193 tp->tun_flags |= TUN_OPEN;
194 TUNDEBUG("%s%d: open\n", ifp->if_name, ifp->if_unit);
195 return (0);
196 }
197
198 /*
199 * tunclose - close the device - mark i/f down & delete
200 * routing info
201 */
202 static int
203 tunclose(dev, foo, bar, p)
204 dev_t dev;
205 int foo;
206 int bar;
207 struct proc *p;
208 {
209 register int unit = dev_val(minor(dev)), s;
210 struct tun_softc *tp = &tunctl[unit];
211 struct ifnet *ifp = &tp->tun_if;
212 struct mbuf *m;
213
214 tp->tun_flags &= ~TUN_OPEN;
215
216 /*
217 * junk all pending output
218 */
219 do {
220 s = splimp();
221 IF_DEQUEUE(&ifp->if_snd, m);
222 splx(s);
223 if (m)
224 m_freem(m);
225 } while (m);
226
227 if (ifp->if_flags & IFF_UP) {
228 s = splimp();
229 if_down(ifp);
230 if (ifp->if_flags & IFF_RUNNING) {
231 /* find internet addresses and delete routes */
232 register struct ifaddr *ifa;
233 for (ifa = ifp->if_addrhead.tqh_first; ifa;
234 ifa = ifa->ifa_link.tqe_next) {
235 switch (ifa->ifa_addr->sa_family) {
236 #if INET
237 case AF_INET:
238 #endif
239 #if INET6
240 case AF_INET6:
241 #endif
242 rtinit(ifa, (int)RTM_DELETE,
243 tp->tun_flags & TUN_DSTADDR ? RTF_HOST : 0);
244 break;
245 }
246 }
247 }
248 splx(s);
249 }
250 ifp->if_flags &= ~IFF_RUNNING;
251 funsetown(tp->tun_sigio);
252 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
253 selwakeup(&tp->tun_rsel);
254 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
255
256 TUNDEBUG ("%s%d: closed\n", ifp->if_name, ifp->if_unit);
257 return (0);
258 }
259
260 static int
261 tuninit(unit, cmd, af)
262 int unit;
263 int cmd;
264 u_char af;
265 {
266 struct tun_softc *tp = &tunctl[unit];
267 struct ifnet *ifp = &tp->tun_if;
268 register struct ifaddr *ifa;
269
270 TUNDEBUG("%s%d: tuninit\n", ifp->if_name, ifp->if_unit);
271
272 ifp->if_flags |= IFF_UP | IFF_RUNNING;
273 getmicrotime(&ifp->if_lastchange);
274
275 for (ifa = ifp->if_addrhead.tqh_first; ifa;
276 ifa = ifa->ifa_link.tqe_next) {
277 #if INET
278 if (ifa->ifa_addr->sa_family == AF_INET) {
279 struct sockaddr_in *si;
280
281 si = (struct sockaddr_in *)ifa->ifa_addr;
282 if (si && si->sin_addr.s_addr)
283 tp->tun_flags |= TUN_IASET;
284
285 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
286 if (si && si->sin_addr.s_addr)
287 tp->tun_flags |= TUN_DSTADDR;
288 }
289 #endif
290 }
291 return 0;
292 }
293
294 /*
295 * Process an ioctl request.
296 */
297 int
298 tunifioctl(ifp, cmd, data)
299 struct ifnet *ifp;
300 u_long cmd;
301 caddr_t data;
302 {
303 register struct ifreq *ifr = (struct ifreq *)data;
304 int error = 0, s;
305
306 s = splimp();
307 switch(cmd) {
308 case SIOCSIFADDR:
309 tuninit(ifp->if_unit);
310 TUNDEBUG("%s%d: address set\n",
311 ifp->if_name, ifp->if_unit);
312 break;
313 case SIOCSIFDSTADDR:
314 #if 0
315 #if defined(INET6) && defined(__FreeBSD__) && __FreeBSD__ >= 3
316 if (found_first_ifid == 0)
317 in6_ifattach_noifid(ifp);
318 #endif /* defined(INET6) && defined(__FreeBSD__) && __FreeBSD__ >= 3 */
319 #endif
320 tuninit(ifp->if_unit, cmd, ifr->ifr_addr.sa_family);
321 break;
322 case SIOCSIFMTU:
323 ifp->if_mtu = ifr->ifr_mtu;
324 TUNDEBUG("%s%d: mtu set\n",
325 ifp->if_name, ifp->if_unit);
326 break;
327 case SIOCADDMULTI:
328 case SIOCDELMULTI:
329 break;
330
331 case SIOCSIFFLAGS:
332 if ((ifp->if_flags & IFF_UP) != 0)
333 ifp->if_flags |= IFF_RUNNING;
334 else if ((ifp->if_flags & IFF_UP) == 0)
335 ifp->if_flags &= ~IFF_RUNNING;
336 break;
337
338 default:
339 error = EINVAL;
340 }
341 splx(s);
342 return (error);
343 }
344
345 /*
346 * tunoutput - queue packets from higher level ready to put out.
347 */
348 /* Packet data format between tun and ppp is changed to enable checking of
349 * Address Family of sending packet. When INET6 is defined, 4byte AF field
350 * is appended to packet data as following.
351 *
352 * 0 1 2 3 4 5 6 7 8 .....
353 * ------------------------------
354 * | af | packet data .....
355 * ------------------------------
356 * ^^^^^^^^^^^^^
357 * Newly added part. The size is sizeof(u_long).
358 *
359 * However, this is not adopted for tun -> ppp AF_INET packet for
360 * backword compatibility, because the ppp process may be an existing
361 * ip only supporting one.
362 * Also in ppp->tun case, when af value is unknown, (af > 255) is checked and
363 * if it is true, AF_INET is assumed. (the 4byte may be the head of
364 * AF_INET packet. Despite the byte order, the value must always be
365 * greater than 255, because of ip_len field or (ip_v and ip_hl)
366 * field. (Idea from Mr. Noritoshi Demize)
367 */
368 int
369 tunoutput(ifp, m0, dst, rt)
370 struct ifnet *ifp;
371 struct mbuf *m0;
372 struct sockaddr *dst;
373 struct rtentry *rt;
374 {
375 struct tun_softc *tp = &tunctl[ifp->if_unit];
376 int s;
377
378 TUNDEBUG ("%s%d: tunoutput\n", ifp->if_name, ifp->if_unit);
379
380 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
381 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
382 ifp->if_unit, tp->tun_flags);
383 m_freem (m0);
384 return EHOSTDOWN;
385 }
386
387 #if NBPFILTER > 0
388 /* BPF write needs to be handled specially */
389 if (dst->sa_family == AF_UNSPEC) {
390 dst->sa_family = *(mtod(m0, int *));
391 m0->m_len -= sizeof(int);
392 m0->m_pkthdr.len -= sizeof(int);
393 m0->m_data += sizeof(int);
394 }
395
396 if (ifp->if_bpf) {
397 /*
398 * We need to prepend the address family as
399 * a four byte field. Cons up a dummy header
400 * to pacify bpf. This is safe because bpf
401 * will only read from the mbuf (i.e., it won't
402 * try to free it or keep a pointer to it).
403 */
404 struct mbuf m;
405 u_int af = dst->sa_family;
406
407 m.m_next = m0;
408 m.m_len = 4;
409 m.m_data = (char *)&af;
410
411 bpf_mtap(ifp, &m);
412 }
413 #endif
414
415 switch(dst->sa_family) {
416 #if defined(INET) || defined(INET6)
417 #if INET6
418 case AF_INET6:
419 M_PREPEND(m0, sizeof(u_long) /* af field passed to upper */,
420 M_DONTWAIT);
421 if (m0 == 0)
422 return (ENOBUFS);
423 *mtod(m0, u_long *) = (u_long)dst->sa_family;
424 /* FALLTHROUGH */
425 #endif /* INET6 */
426 #if INET
427 case AF_INET:
428 #endif /* INET */
429 #endif /* INET || INET6 */
430 s = splimp();
431 if (IF_QFULL(&ifp->if_snd)) {
432 IF_DROP(&ifp->if_snd);
433 m_freem(m0);
434 splx(s);
435 ifp->if_collisions++;
436 return (ENOBUFS);
437 }
438 ifp->if_obytes += m0->m_pkthdr.len;
439 IF_ENQUEUE(&ifp->if_snd, m0);
440 splx(s);
441 ifp->if_opackets++;
442 break;
443 default:
444 m_freem(m0);
445 return EAFNOSUPPORT;
446 }
447
448 if (tp->tun_flags & TUN_RWAIT) {
449 tp->tun_flags &= ~TUN_RWAIT;
450 wakeup((caddr_t)tp);
451 }
452 if (tp->tun_flags & TUN_ASYNC && tp->tun_sigio)
453 pgsigio(tp->tun_sigio, SIGIO, 0);
454 thread_funnel_switch(NETWORK_FUNNEL, KERNEL_FUNNEL);
455 selwakeup(&tp->tun_rsel);
456 thread_funnel_switch(KERNEL_FUNNEL, NETWORK_FUNNEL);
457 return 0;
458 }
459
460 /*
461 * the cdevsw interface is now pretty minimal.
462 */
463 static int
464 tunioctl(dev, cmd, data, flag, p)
465 dev_t dev;
466 u_long cmd;
467 caddr_t data;
468 int flag;
469 struct proc *p;
470 {
471 int unit = dev_val(minor(dev)), s;
472 struct tun_softc *tp = &tunctl[unit];
473 struct tuninfo *tunp;
474
475 switch (cmd) {
476 case TUNSIFINFO:
477 tunp = (struct tuninfo *)data;
478 tp->tun_if.if_mtu = tunp->mtu;
479 tp->tun_if.if_type = tunp->type;
480 tp->tun_if.if_baudrate = tunp->baudrate;
481 break;
482 case TUNGIFINFO:
483 tunp = (struct tuninfo *)data;
484 tunp->mtu = tp->tun_if.if_mtu;
485 tunp->type = tp->tun_if.if_type;
486 tunp->baudrate = tp->tun_if.if_baudrate;
487 break;
488 case TUNSDEBUG:
489 tundebug = *(int *)data;
490 break;
491 case TUNGDEBUG:
492 *(int *)data = tundebug;
493 break;
494 case FIONBIO:
495 break;
496 case FIOASYNC:
497 if (*(int *)data)
498 tp->tun_flags |= TUN_ASYNC;
499 else
500 tp->tun_flags &= ~TUN_ASYNC;
501 break;
502 case FIONREAD:
503 s = splimp();
504 if (tp->tun_if.if_snd.ifq_head) {
505 struct mbuf *mb = tp->tun_if.if_snd.ifq_head;
506 for( *(int *)data = 0; mb != 0; mb = mb->m_next)
507 *(int *)data += mb->m_len;
508 } else
509 *(int *)data = 0;
510 splx(s);
511 break;
512 case FIOSETOWN:
513 return (fsetown(*(int *)data, &tp->tun_sigio));
514
515 case FIOGETOWN:
516 *(int *)data = fgetown(tp->tun_sigio);
517 return (0);
518
519 /* This is deprecated, FIOSETOWN should be used instead. */
520 case TIOCSPGRP:
521 return (fsetown(-(*(int *)data), &tp->tun_sigio));
522
523 /* This is deprecated, FIOGETOWN should be used instead. */
524 case TIOCGPGRP:
525 *(int *)data = -fgetown(tp->tun_sigio);
526 return (0);
527
528 default:
529 return (ENOTTY);
530 }
531 return (0);
532 }
533
534 /*
535 * The cdevsw read interface - reads a packet at a time, or at
536 * least as much of a packet as can be read.
537 */
538 static int
539 tunread(dev, uio, flag)
540 dev_t dev;
541 struct uio *uio;
542 int flag;
543 {
544 int unit = dev_val(minor(dev));
545 struct tun_softc *tp = &tunctl[unit];
546 struct ifnet *ifp = &tp->tun_if;
547 struct mbuf *m, *m0;
548 int error=0, len, s;
549
550 TUNDEBUG ("%s%d: read\n", ifp->if_name, ifp->if_unit);
551 if ((tp->tun_flags & TUN_READY) != TUN_READY) {
552 TUNDEBUG ("%s%d: not ready 0%o\n", ifp->if_name,
553 ifp->if_unit, tp->tun_flags);
554 return EHOSTDOWN;
555 }
556
557 tp->tun_flags &= ~TUN_RWAIT;
558
559 s = splimp();
560 do {
561 IF_DEQUEUE(&ifp->if_snd, m0);
562 if (m0 == 0) {
563 if (flag & IO_NDELAY) {
564 splx(s);
565 return EWOULDBLOCK;
566 }
567 tp->tun_flags |= TUN_RWAIT;
568 if( error = tsleep((caddr_t)tp, PCATCH | (PZERO + 1),
569 "tunread", 0)) {
570 splx(s);
571 return error;
572 }
573 }
574 } while (m0 == 0);
575 splx(s);
576
577 while (m0 && uio->uio_resid > 0 && error == 0) {
578 len = min(uio->uio_resid, m0->m_len);
579 if (len == 0)
580 break;
581 error = uiomove(mtod(m0, caddr_t), len, uio);
582 MFREE(m0, m);
583 m0 = m;
584 }
585
586 if (m0) {
587 TUNDEBUG("Dropping mbuf\n");
588 m_freem(m0);
589 }
590 return error;
591 }
592
593 /*
594 * the cdevsw write interface - an atomic write is a packet - or else!
595 */
596 /* See top of tunoutput() about interface change between ppp process and
597 * tun. */
598 static int
599 tunwrite(dev, uio, flag)
600 dev_t dev;
601 struct uio *uio;
602 int flag;
603 {
604 int unit = dev_val(minor(dev));
605 struct ifnet *ifp = &tunctl[unit].tun_if;
606 struct mbuf *top, **mp, *m;
607 int error=0, s, tlen, mlen;
608 u_long af;
609 u_int netisr_af;
610 struct ifqueue *afintrq = NULL;
611
612 TUNDEBUG("%s%d: tunwrite\n", ifp->if_name, ifp->if_unit);
613
614 if (uio->uio_resid < 0 || uio->uio_resid > TUNMRU) {
615 TUNDEBUG("%s%d: len=%d!\n", ifp->if_name, ifp->if_unit,
616 uio->uio_resid);
617 return EIO;
618 }
619 tlen = uio->uio_resid;
620
621 /* get a header mbuf */
622 MGETHDR(m, M_DONTWAIT, MT_DATA);
623 if (m == NULL)
624 return ENOBUFS;
625 if (tlen > MHLEN) {
626 MCLGET(m, M_DONTWAIT);
627 if ((m->m_flags & M_EXT) == 0) {
628 m_free(m);
629 return ENOBUFS;
630 }
631 mlen = m->m_ext.ext_size;
632 } else
633 mlen = MHLEN;
634
635 top = 0;
636 mp = &top;
637 while (error == 0 && uio->uio_resid > 0) {
638 m->m_len = min(mlen, uio->uio_resid);
639 error = uiomove(mtod (m, caddr_t), m->m_len, uio);
640 *mp = m;
641 mp = &m->m_next;
642 if (uio->uio_resid > 0) {
643 MGET (m, M_DONTWAIT, MT_DATA);
644 if (m == 0) {
645 error = ENOBUFS;
646 break;
647 }
648 mlen = MLEN;
649 }
650 }
651 /* Change for checking Address Family of sending packet. */
652 af = *mtod(top, u_long *);
653 switch (af) {
654 #if INET
655 case AF_INET:
656 netisr_af = NETISR_IP;
657 afintrq = &ipintrq;
658 break;
659 #endif /* INET */
660 #if INET6
661 case AF_INET6:
662 netisr_af = NETISR_IPV6;
663 afintrq = &ip6intrq;
664 break;
665 #endif /* INET6 */
666 default:
667 if (af > 255) { /* see description at the top of tunoutput */
668 af = AF_INET;
669 netisr_af = NETISR_IP;
670 afintrq = &ipintrq;
671 goto af_decided;
672 }
673 error = EAFNOSUPPORT;
674 break;
675 }
676 m_adj(top, sizeof(u_long)); /* remove af field passed from upper */
677 tlen -= sizeof(u_long);
678 af_decided:
679 if (error) {
680 if (top)
681 m_freem (top);
682 return error;
683 }
684
685 top->m_pkthdr.len = tlen;
686 top->m_pkthdr.rcvif = ifp;
687
688 #if NBPFILTER > 0
689 if (ifp->if_bpf) {
690 /*
691 * We need to prepend the address family as
692 * a four byte field. Cons up a dummy header
693 * to pacify bpf. This is safe because bpf
694 * will only read from the mbuf (i.e., it won't
695 * try to free it or keep a pointer to it).
696 */
697 struct mbuf m;
698
699 m.m_next = top;
700 m.m_len = 4;
701 m.m_data = (char *)&af;
702
703 bpf_mtap(ifp, &m);
704 }
705 #endif
706
707 /* just for safety */
708 if (!afintrq)
709 return EAFNOSUPPORT;
710
711 s = splimp();
712 if (IF_QFULL (afintrq)) {
713 IF_DROP(afintrq);
714 splx(s);
715 ifp->if_collisions++;
716 m_freem(top);
717 return ENOBUFS;
718 }
719 IF_ENQUEUE(afintrq, top);
720 splx(s);
721 ifp->if_ibytes += tlen;
722 ifp->if_ipackets++;
723 schednetisr(netisr_af);
724 return error;
725 }
726
727 /*
728 * tunpoll - the poll interface, this is only useful on reads
729 * really. The write detect always returns true, write never blocks
730 * anyway, it either accepts the packet or drops it.
731 */
732 static int
733 tunpoll(dev, events, p)
734 dev_t dev;
735 int events;
736 struct proc *p;
737 {
738 int unit = dev_val(minor(dev)), s;
739 struct tun_softc *tp = &tunctl[unit];
740 struct ifnet *ifp = &tp->tun_if;
741 int revents = 0;
742
743 s = splimp();
744 TUNDEBUG("%s%d: tunpoll\n", ifp->if_name, ifp->if_unit);
745
746 if (events & (POLLIN | POLLRDNORM))
747 if (ifp->if_snd.ifq_len > 0) {
748 TUNDEBUG("%s%d: tunpoll q=%d\n", ifp->if_name,
749 ifp->if_unit, ifp->if_snd.ifq_len);
750 revents |= events & (POLLIN | POLLRDNORM);
751 } else {
752 TUNDEBUG("%s%d: tunpoll waiting\n", ifp->if_name,
753 ifp->if_unit);
754 selrecord(p, &tp->tun_rsel);
755 }
756
757 if (events & (POLLOUT | POLLWRNORM))
758 revents |= events & (POLLOUT | POLLWRNORM);
759
760 splx(s);
761 return (revents);
762 }
763
764
765 #endif /* NTUN */