1 /* $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $ */
4 * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
5 * Nottingham University 1987.
7 * This source may be freely distributed, however I would be interested
8 * in any changes that are made.
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 it's
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
16 * $FreeBSD: src/sys/net/if_tun.c,v 1.74.2.8 2002/02/13 00:43:11 dillon Exp $
20 #include "opt_inet6.h"
22 #include <sys/param.h>
25 #include <sys/systm.h>
27 #include <sys/socket.h>
29 #include <sys/device.h>
30 #include <sys/filio.h>
31 #include <sys/sockio.h>
32 #include <sys/ttycom.h>
33 #include <sys/signalvar.h>
34 #include <sys/filedesc.h>
35 #include <sys/kernel.h>
36 #include <sys/sysctl.h>
38 #include <sys/vnode.h>
39 #include <sys/malloc.h>
40 #include <sys/mplock2.h>
41 #include <sys/devfs.h>
42 #include <sys/queue.h>
46 #include <net/if_types.h>
47 #include <net/if_clone.h>
48 #include <net/ifq_var.h>
49 #include <net/netisr.h>
50 #include <net/route.h>
53 #include <netinet/in.h>
56 #include "if_tunvar.h"
60 #define TUNDEBUG if (tundebug) if_printf
63 static int tunmodevent(module_t
, int, void *);
66 static struct tun_softc
*tuncreate(cdev_t
, int);
67 static void tundestroy(struct tun_softc
*sc
);
70 static int tun_clone_create(struct if_clone
*, int,
72 static int tun_clone_destroy(struct ifnet
*);
74 /* network interface */
75 static int tunifinit(struct ifnet
*);
76 static void tunifstart(struct ifnet
*, struct ifaltq_subque
*);
77 static int tunifoutput(struct ifnet
*, struct mbuf
*,
78 struct sockaddr
*, struct rtentry
*rt
);
79 static int tunifioctl(struct ifnet
*, u_long
,
80 caddr_t
, struct ucred
*);
82 /* character device */
83 static d_open_t tunopen
;
84 static d_close_t tunclose
;
85 static d_read_t tunread
;
86 static d_write_t tunwrite
;
87 static d_ioctl_t tunioctl
;
88 static d_kqfilter_t tunkqfilter
;
89 static d_clone_t tunclone
;
91 static struct dev_ops tun_ops
= {
98 .d_kqfilter
= tunkqfilter
102 static void tun_filter_detach(struct knote
*);
103 static int tun_filter_read(struct knote
*, long);
104 static int tun_filter_write(struct knote
*, long);
106 static struct filterops tun_read_filtops
= {
112 static struct filterops tun_write_filtops
= {
119 static int tundebug
= 0; /* debug flag */
120 static int tunrefcnt
= 0; /* module reference counter */
122 static MALLOC_DEFINE(M_TUN
, TUN
, "Tunnel Interface");
124 static DEVFS_DEFINE_CLONE_BITMAP(tun
);
126 struct if_clone tun_cloner
= IF_CLONE_INITIALIZER(
127 TUN
, tun_clone_create
, tun_clone_destroy
, 0, IF_MAXUNIT
);
129 static SLIST_HEAD(,tun_softc
) tun_listhead
=
130 SLIST_HEAD_INITIALIZER(&tun_listhead
);
132 SYSCTL_INT(_debug
, OID_AUTO
, if_tun_debug
, CTLFLAG_RW
, &tundebug
, 0,
133 "Enable debug output");
134 SYSCTL_DECL(_net_link
);
135 SYSCTL_NODE(_net_link
, OID_AUTO
, tun
, CTLFLAG_RW
, 0,
136 "IP tunnel software network interface");
137 SYSCTL_INT(_net_link_tun
, OID_AUTO
, debug
, CTLFLAG_RW
, &tundebug
, 0,
138 "Enable debug output");
139 SYSCTL_INT(_net_link_tun
, OID_AUTO
, refcnt
, CTLFLAG_RD
, &tunrefcnt
, 0,
140 "Number of opened devices");
142 DEV_MODULE(if_tun
, tunmodevent
, NULL
);
145 * tunmodevent - module event handler
148 tunmodevent(module_t mod
, int type
, void *data
)
150 static cdev_t dev
= NULL
;
151 struct tun_softc
*sc
, *sc_tmp
;
155 dev
= make_autoclone_dev(&tun_ops
, &DEVFS_CLONE_BITMAP(tun
),
156 tunclone
, UID_UUCP
, GID_DIALER
,
159 SLIST_INIT(&tun_listhead
);
160 if_clone_attach(&tun_cloner
);
167 if_clone_detach(&tun_cloner
);
169 SLIST_FOREACH_MUTABLE(sc
, &tun_listhead
, tun_link
, sc_tmp
)
172 dev_ops_remove_all(&tun_ops
);
173 destroy_autoclone_dev(dev
, &DEVFS_CLONE_BITMAP(tun
));
184 tunclone(struct dev_clone_args
*ap
)
186 char ifname
[IFNAMSIZ
];
189 unit
= devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(tun
), 0);
190 ksnprintf(ifname
, IFNAMSIZ
, "%s%d", TUN
, unit
);
191 ap
->a_dev
= make_only_dev(&tun_ops
, unit
, UID_UUCP
, GID_DIALER
,
195 * Use the if_clone framework to create cloned device/interface,
196 * so the two clone methods (autoclone device /dev/tun; ifconfig
197 * clone) are consistent and can be mix used.
199 * Need to pass the cdev_t because the device created by
200 * 'make_only_dev()' doesn't appear in '/dev' yet so that it can't
201 * be found by 'devfs_find_device_by_name()' in 'tun_clone_create()'.
203 return (if_clone_create(ifname
, IFNAMSIZ
, NULL
, (caddr_t
)ap
->a_dev
));
206 static struct tun_softc
*
207 tuncreate(cdev_t dev
, int flags
)
209 struct tun_softc
*sc
;
211 int unit
= minor(dev
);
213 sc
= kmalloc(sizeof(*sc
), M_TUN
, M_WAITOK
| M_ZERO
);
216 sc
->tun_flags
= TUN_INITED
;
217 sc
->tun_flags
|= flags
;
219 reference_dev(dev
); /* device association */
221 ifp
= sc
->tun_ifp
= if_alloc(IFT_PPP
);
223 kprintf("%s: failed to if_alloc() interface for %s%d",
224 __func__
, TUN
, unit
);
228 if_initname(ifp
, TUN
, unit
);
229 ifp
->if_mtu
= TUNMTU
;
230 ifp
->if_ioctl
= tunifioctl
;
231 ifp
->if_output
= tunifoutput
;
232 ifp
->if_start
= tunifstart
;
233 ifp
->if_flags
= IFF_POINTOPOINT
| IFF_MULTICAST
;
235 ifq_set_maxlen(&ifp
->if_snd
, ifqmaxlen
);
236 ifq_set_ready(&ifp
->if_snd
);
238 if_attach(ifp
, NULL
);
239 bpfattach(ifp
, DLT_NULL
, sizeof(uint32_t));
241 SLIST_INSERT_HEAD(&tun_listhead
, sc
, tun_link
);
242 TUNDEBUG(ifp
, "created, minor = %#x, flags = 0x%x\n",
243 unit
, sc
->tun_flags
);
248 tundestroy(struct tun_softc
*sc
)
250 cdev_t dev
= sc
->tun_dev
;
251 struct ifnet
*ifp
= sc
->tun_ifp
;
252 int unit
= minor(dev
);
254 TUNDEBUG(ifp
, "destroyed, minor = %#x. Module refcnt = %d\n",
263 release_dev(dev
); /* device disassociation */
265 /* Also destroy the cloned device */
267 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(tun
), unit
);
269 SLIST_REMOVE(&tun_listhead
, sc
, tun_softc
, tun_link
);
274 * tunnel open - must be superuser & the device must be configured in
277 tunopen(struct dev_open_args
*ap
)
279 cdev_t dev
= ap
->a_head
.a_dev
;
280 struct tun_softc
*sc
;
283 if ((error
= caps_priv_check(ap
->a_cred
, SYSCAP_RESTRICTEDROOT
)) != 0)
287 if (sc
== NULL
&& (sc
= tuncreate(dev
, 0)) == NULL
)
289 if (sc
->tun_flags
& TUN_OPEN
)
292 sc
->tun_pid
= curproc
->p_pid
;
293 sc
->tun_flags
|= TUN_OPEN
;
296 TUNDEBUG(sc
->tun_ifp
, "opened, minor = %#x. Module refcnt = %d\n",
297 minor(dev
), tunrefcnt
);
302 * close the device - mark interface down & delete routing info
305 tunclose(struct dev_close_args
*ap
)
307 cdev_t dev
= ap
->a_head
.a_dev
;
308 struct tun_softc
*sc
= dev
->si_drv1
;
310 int unit
= minor(dev
);
311 char ifname
[IFNAMSIZ
];
314 ("try closing the already destroyed %s%d", TUN
, unit
));
317 sc
->tun_flags
&= ~TUN_OPEN
;
320 /* Junk all pending output. */
321 ifq_purge_all(&ifp
->if_snd
);
323 if (ifp
->if_flags
& IFF_UP
)
325 ifp
->if_flags
&= ~IFF_RUNNING
;
327 if ((sc
->tun_flags
& TUN_MANUALMAKE
) == 0) {
328 if_purgeaddrs_nolink(ifp
);
330 EVENTHANDLER_INVOKE(ifnet_detach_event
, ifp
);
332 /* Announce the departure of the interface. */
333 rt_ifannouncemsg(ifp
, IFAN_DEPARTURE
);
336 funsetown(&sc
->tun_sigio
);
337 KNOTE(&sc
->tun_rkq
.ki_note
, 0);
342 if_printf(ifp
, ". Module refcnt = %d is out of sync! "
343 "Force refcnt to be 0.\n", tunrefcnt
);
346 TUNDEBUG(ifp
, "closed, minor = %#x. Module refcnt = %d\n",
349 /* Only auto-destroy if the interface was not manually created. */
350 if ((sc
->tun_flags
& TUN_MANUALMAKE
) == 0) {
351 ksnprintf(ifname
, IFNAMSIZ
, "%s%d", TUN
, unit
);
352 if_clone_destroy(ifname
);
360 * Interface clone support
362 * Create and destroy tun device/interface via ifconfig(8).
365 static struct tun_softc
*
368 struct tun_softc
*sc
;
370 SLIST_FOREACH(sc
, &tun_listhead
, tun_link
) {
371 if (minor(sc
->tun_dev
) == unit
)
378 tun_clone_create(struct if_clone
*ifc __unused
, int unit
,
379 caddr_t params __unused
, caddr_t data
)
381 struct tun_softc
*sc
;
382 cdev_t dev
= (cdev_t
)data
;
385 if (tunfind(unit
) != NULL
)
389 if (!devfs_clone_bitmap_chk(&DEVFS_CLONE_BITMAP(tun
), unit
)) {
390 devfs_clone_bitmap_set(&DEVFS_CLONE_BITMAP(tun
), unit
);
391 dev
= make_dev(&tun_ops
, unit
, UID_UUCP
, GID_DIALER
,
392 0600, "%s%d", TUN
, unit
);
394 dev
= devfs_find_device_by_name("%s%d", TUN
, unit
);
398 flags
= TUN_MANUALMAKE
;
403 if ((sc
= tuncreate(dev
, flags
)) == NULL
)
406 sc
->tun_flags
|= TUN_CLONE
;
408 TUNDEBUG(sc
->tun_ifp
, "clone created, minor = %#x, flags = 0x%x\n",
409 minor(sc
->tun_dev
), sc
->tun_flags
);
415 tun_clone_destroy(struct ifnet
*ifp
)
417 struct tun_softc
*sc
= ifp
->if_softc
;
419 if (sc
->tun_flags
& TUN_OPEN
)
421 if ((sc
->tun_flags
& TUN_CLONE
) == 0)
424 TUNDEBUG(ifp
, "clone destroyed, minor = %#x, flags = 0x%x\n",
425 minor(sc
->tun_dev
), sc
->tun_flags
);
433 * Network interface functions
437 tunifinit(struct ifnet
*ifp
)
440 struct tun_softc
*sc
= ifp
->if_softc
;
442 struct ifaddr_container
*ifac
;
445 TUNDEBUG(ifp
, "initialize\n");
447 ifp
->if_flags
|= IFF_UP
| IFF_RUNNING
;
448 getmicrotime(&ifp
->if_lastchange
);
450 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
451 struct ifaddr
*ifa
= ifac
->ifa
;
453 if (ifa
->ifa_addr
== NULL
) {
455 /* XXX: Should maybe return straight off? */
458 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
459 struct sockaddr_in
*si
;
461 si
= (struct sockaddr_in
*)ifa
->ifa_addr
;
462 if (si
->sin_addr
.s_addr
)
463 sc
->tun_flags
|= TUN_IASET
;
472 * Process an ioctl request.
477 tunifioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
479 struct ifreq
*ifr
= (struct ifreq
*)data
;
480 struct tun_softc
*sc
= ifp
->if_softc
;
486 ifs
= (struct ifstat
*)data
;
488 ksprintf(ifs
->ascii
+ strlen(ifs
->ascii
),
489 "\tOpened by PID %d\n", sc
->tun_pid
);
492 error
= tunifinit(ifp
);
493 TUNDEBUG(ifp
, "address set, error=%d\n", error
);
496 error
= tunifinit(ifp
);
497 TUNDEBUG(ifp
, "destination address set, error=%d\n", error
);
500 ifp
->if_mtu
= ifr
->ifr_mtu
;
501 TUNDEBUG(ifp
, "mtu set\n");
514 * Start packet transmission on the interface.
515 * when the interface queue is rate-limited by ALTQ,
516 * if_start is needed to drain packets from the queue in order
517 * to notify readers when outgoing packets become ready.
520 tunifstart(struct ifnet
*ifp
, struct ifaltq_subque
*ifsq
)
522 struct tun_softc
*sc
= ifp
->if_softc
;
525 ASSERT_ALTQ_SQ_DEFAULT(ifp
, ifsq
);
527 if (!ifq_is_enabled(&ifp
->if_snd
))
532 if (sc
->tun_flags
& TUN_RWAIT
) {
533 sc
->tun_flags
&= ~TUN_RWAIT
;
536 if (sc
->tun_flags
& TUN_ASYNC
&& sc
->tun_sigio
)
537 pgsigio(sc
->tun_sigio
, SIGIO
, 0);
538 ifsq_deserialize_hw(ifsq
);
539 KNOTE(&sc
->tun_rkq
.ki_note
, 0);
540 ifsq_serialize_hw(ifsq
);
545 * tunifoutput - queue packets from higher level ready to put out.
550 tunifoutput_serialized(struct ifnet
*ifp
, struct mbuf
*m0
,
551 struct sockaddr
*dst
, struct rtentry
*rt
)
553 struct tun_softc
*sc
= ifp
->if_softc
;
555 struct altq_pktattr pktattr
;
557 TUNDEBUG(ifp
, "output\n");
559 if ((sc
->tun_flags
& TUN_READY
) != TUN_READY
) {
560 TUNDEBUG(ifp
, "not ready, flags = 0x%x\n", sc
->tun_flags
);
566 * if the queueing discipline needs packet classification,
567 * do it before prepending link headers.
569 ifq_classify(&ifp
->if_snd
, m0
, dst
->sa_family
, &pktattr
);
571 /* BPF write needs to be handled specially */
572 if (dst
->sa_family
== AF_UNSPEC
) {
573 dst
->sa_family
= *(mtod(m0
, int *));
574 m0
->m_len
-= sizeof(int);
575 m0
->m_pkthdr
.len
-= sizeof(int);
576 m0
->m_data
+= sizeof(int);
583 * We need to prepend the address family as
586 uint32_t af
= dst
->sa_family
;
588 bpf_ptap(ifp
->if_bpf
, m0
, &af
, sizeof(af
));
593 /* prepend sockaddr? this may abort if the mbuf allocation fails */
594 if (sc
->tun_flags
& TUN_LMODE
) {
595 /* allocate space for sockaddr */
596 M_PREPEND(m0
, dst
->sa_len
, M_NOWAIT
);
598 /* if allocation failed drop packet */
600 IFNET_STAT_INC(ifp
, oerrors
, 1);
603 bcopy(dst
, m0
->m_data
, dst
->sa_len
);
607 if (sc
->tun_flags
& TUN_IFHEAD
) {
608 /* Prepend the address family */
609 M_PREPEND(m0
, 4, M_NOWAIT
);
611 /* if allocation failed drop packet */
613 IFNET_STAT_INC(ifp
, oerrors
, 1);
616 *(u_int32_t
*)m0
->m_data
= htonl(dst
->sa_family
);
620 if (dst
->sa_family
!= AF_INET
)
624 return (EAFNOSUPPORT
);
628 error
= ifq_handoff(ifp
, m0
, &pktattr
);
630 IFNET_STAT_INC(ifp
, collisions
, 1);
632 IFNET_STAT_INC(ifp
, opackets
, 1);
633 if (sc
->tun_flags
& TUN_RWAIT
) {
634 sc
->tun_flags
&= ~TUN_RWAIT
;
638 if (sc
->tun_flags
& TUN_ASYNC
&& sc
->tun_sigio
)
639 pgsigio(sc
->tun_sigio
, SIGIO
, 0);
641 ifnet_deserialize_all(ifp
);
642 KNOTE(&sc
->tun_rkq
.ki_note
, 0);
643 ifnet_serialize_all(ifp
);
649 tunifoutput(struct ifnet
*ifp
, struct mbuf
*m0
, struct sockaddr
*dst
,
654 ifnet_serialize_all(ifp
);
655 error
= tunifoutput_serialized(ifp
, m0
, dst
, rt
);
656 ifnet_deserialize_all(ifp
);
663 * the ops interface is now pretty minimal.
666 tunioctl(struct dev_ioctl_args
*ap
)
668 cdev_t dev
= ap
->a_head
.a_dev
;
669 caddr_t data
= ap
->a_data
;
670 struct tun_softc
*sc
= dev
->si_drv1
;
671 struct ifnet
*ifp
= sc
->tun_ifp
;
673 struct tuninfo
*tunp
;
678 tunp
= (struct tuninfo
*)data
;
679 if (ifp
->if_type
!= tunp
->type
)
681 if (tunp
->mtu
< IF_MINMTU
)
683 ifp
->if_mtu
= tunp
->mtu
;
684 ifp
->if_baudrate
= tunp
->baudrate
;
688 tunp
= (struct tuninfo
*)data
;
689 tunp
->mtu
= ifp
->if_mtu
;
690 tunp
->type
= ifp
->if_type
;
691 tunp
->baudrate
= ifp
->if_baudrate
;
695 ifr
= (struct ifreq
*)data
;
696 strlcpy(ifr
->ifr_name
, ifp
->if_xname
, IFNAMSIZ
);
700 tundebug
= *(int *)data
;
704 *(int *)data
= tundebug
;
709 sc
->tun_flags
|= TUN_LMODE
;
710 sc
->tun_flags
&= ~TUN_IFHEAD
;
712 sc
->tun_flags
&= ~TUN_LMODE
;
718 sc
->tun_flags
|= TUN_IFHEAD
;
719 sc
->tun_flags
&= ~TUN_LMODE
;
721 sc
->tun_flags
&= ~TUN_IFHEAD
;
726 *(int *)data
= (sc
->tun_flags
& TUN_IFHEAD
) ? 1 : 0;
730 /* deny this if UP */
731 if (ifp
->if_flags
& IFF_UP
)
734 switch (*(int *)data
& ~IFF_MULTICAST
) {
735 case IFF_POINTOPOINT
:
737 ifp
->if_flags
&= ~(IFF_BROADCAST
| IFF_POINTOPOINT
);
738 ifp
->if_flags
|= *(int *)data
;
746 sc
->tun_pid
= curproc
->p_pid
;
751 sc
->tun_flags
|= TUN_ASYNC
;
753 sc
->tun_flags
&= ~TUN_ASYNC
;
757 *(int *)data
= ifsq_poll_pktlen(
758 ifq_get_subq_default(&ifp
->if_snd
));
762 error
= fsetown(*(int *)data
, &sc
->tun_sigio
);
766 *(int *)data
= fgetown(&sc
->tun_sigio
);
769 /* This is deprecated, FIOSETOWN should be used instead. */
771 error
= fsetown(-(*(int *)data
), &sc
->tun_sigio
);
774 /* This is deprecated, FIOGETOWN should be used instead. */
776 *(int *)data
= -fgetown(&sc
->tun_sigio
);
788 * The ops read interface - reads a packet at a time, or at
789 * least as much of a packet as can be read.
792 tunread(struct dev_read_args
*ap
)
794 cdev_t dev
= ap
->a_head
.a_dev
;
795 struct uio
*uio
= ap
->a_uio
;
796 struct tun_softc
*sc
= dev
->si_drv1
;
797 struct ifnet
*ifp
= sc
->tun_ifp
;
798 struct ifaltq_subque
*ifsq
= ifq_get_subq_default(&ifp
->if_snd
);
802 TUNDEBUG(ifp
, "read\n");
803 if ((sc
->tun_flags
& TUN_READY
) != TUN_READY
) {
804 TUNDEBUG(ifp
, "not ready, flags = 0x%x\n", sc
->tun_flags
);
808 sc
->tun_flags
&= ~TUN_RWAIT
;
810 ifnet_serialize_all(ifp
);
812 while ((m0
= ifsq_dequeue(ifsq
)) == NULL
) {
813 if (ap
->a_ioflag
& IO_NDELAY
) {
814 ifnet_deserialize_all(ifp
);
815 return (EWOULDBLOCK
);
817 sc
->tun_flags
|= TUN_RWAIT
;
818 ifnet_deserialize_all(ifp
);
819 if ((error
= tsleep(sc
, PCATCH
, "tunread", 0)) != 0)
821 ifnet_serialize_all(ifp
);
824 ifnet_deserialize_all(ifp
);
826 while (m0
&& uio
->uio_resid
> 0 && error
== 0) {
827 len
= (int)szmin(uio
->uio_resid
, m0
->m_len
);
829 error
= uiomove(mtod(m0
, caddr_t
), (size_t)len
, uio
);
834 TUNDEBUG(ifp
, "dropping mbuf\n");
841 * the ops write interface - an atomic write is a packet - or else!
844 tunwrite(struct dev_write_args
*ap
)
846 cdev_t dev
= ap
->a_head
.a_dev
;
847 struct uio
*uio
= ap
->a_uio
;
848 struct tun_softc
*sc
= dev
->si_drv1
;
849 struct ifnet
*ifp
= sc
->tun_ifp
;
850 struct mbuf
*top
, **mp
, *m
;
852 uint32_t family
, mru
;
856 TUNDEBUG(ifp
, "tunwrite\n");
858 if (uio
->uio_resid
== 0)
862 if (sc
->tun_flags
& TUN_IFHEAD
)
863 mru
+= sizeof(family
);
864 if (uio
->uio_resid
> mru
) {
865 TUNDEBUG(ifp
, "len = %zd!\n", uio
->uio_resid
);
869 /* get a header mbuf */
870 MGETHDR(m
, M_WAITOK
, MT_DATA
);
872 tlen
= uio
->uio_resid
;
875 while (error
== 0 && uio
->uio_resid
> 0) {
876 m
->m_len
= (int)szmin(MHLEN
, uio
->uio_resid
);
877 error
= uiomove(mtod(m
, caddr_t
), (size_t)m
->m_len
, uio
);
880 if (uio
->uio_resid
> 0)
881 MGET(m
, M_WAITOK
, MT_DATA
);
886 IFNET_STAT_INC(ifp
, ierrors
, 1);
890 top
->m_pkthdr
.len
= (int)tlen
;
891 top
->m_pkthdr
.rcvif
= ifp
;
897 if (sc
->tun_flags
& TUN_IFHEAD
) {
899 * Conveniently, we already have a 4-byte
900 * address family prepended to our packet !
901 * Inconveniently, it's in the wrong byte
904 if ((top
= m_pullup(top
, sizeof(family
)))
909 *mtod(top
, u_int32_t
*) =
910 ntohl(*mtod(top
, u_int32_t
*));
911 bpf_mtap(ifp
->if_bpf
, top
);
912 *mtod(top
, u_int32_t
*) =
913 htonl(*mtod(top
, u_int32_t
*));
916 * We need to prepend the address family as
919 static const uint32_t af
= AF_INET
;
921 bpf_ptap(ifp
->if_bpf
, top
, &af
, sizeof(af
));
928 if (sc
->tun_flags
& TUN_IFHEAD
) {
929 if (top
->m_len
< sizeof(family
) &&
930 (top
= m_pullup(top
, sizeof(family
))) == NULL
)
932 family
= ntohl(*mtod(top
, u_int32_t
*));
933 m_adj(top
, sizeof(family
));
938 IFNET_STAT_INC(ifp
, ibytes
, top
->m_pkthdr
.len
);
939 IFNET_STAT_INC(ifp
, ipackets
, 1);
954 return (EAFNOSUPPORT
);
957 netisr_queue(isr
, top
);
963 * tunkqfilter - support for the kevent() system call.
966 tunkqfilter(struct dev_kqfilter_args
*ap
)
968 cdev_t dev
= ap
->a_head
.a_dev
;
969 struct tun_softc
*sc
= dev
->si_drv1
;
970 struct ifnet
*ifp
= sc
->tun_ifp
;
971 struct knote
*kn
= ap
->a_kn
;
975 ifnet_serialize_all(ifp
);
977 switch (kn
->kn_filter
) {
979 kn
->kn_fop
= &tun_read_filtops
;
980 kn
->kn_hook
= (caddr_t
)sc
;
983 kn
->kn_fop
= &tun_write_filtops
;
984 kn
->kn_hook
= (caddr_t
)sc
;
987 ifnet_deserialize_all(ifp
);
988 ap
->a_result
= EOPNOTSUPP
;
992 klist
= &sc
->tun_rkq
.ki_note
;
993 knote_insert(klist
, kn
);
994 ifnet_deserialize_all(ifp
);
1000 tun_filter_detach(struct knote
*kn
)
1002 struct tun_softc
*sc
= (struct tun_softc
*)kn
->kn_hook
;
1003 struct klist
*klist
= &sc
->tun_rkq
.ki_note
;
1005 knote_remove(klist
, kn
);
1009 tun_filter_write(struct knote
*kn
, long hint
)
1011 /* Always ready for a write */
1016 tun_filter_read(struct knote
*kn
, long hint
)
1018 struct tun_softc
*sc
= (struct tun_softc
*)kn
->kn_hook
;
1019 struct ifnet
*ifp
= sc
->tun_ifp
;
1022 ifnet_serialize_all(ifp
);
1023 if (!ifsq_is_empty(ifq_get_subq_default(&ifp
->if_snd
))) {
1024 if ((kn
->kn_sfflags
& NOTE_HUPONLY
) == 0)
1027 ifnet_deserialize_all(ifp
);