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 $
17 * $DragonFly: src/sys/net/tun/if_tun.c,v 1.37 2008/06/05 18:06:32 swildner Exp $
20 #include "opt_atalk.h"
22 #include "opt_inet6.h"
25 #include <sys/param.h>
27 #include <sys/systm.h>
29 #include <sys/socket.h>
31 #include <sys/device.h>
32 #include <sys/filio.h>
33 #include <sys/sockio.h>
34 #include <sys/thread2.h>
35 #include <sys/ttycom.h>
37 #include <sys/signalvar.h>
38 #include <sys/filedesc.h>
39 #include <sys/kernel.h>
40 #include <sys/sysctl.h>
42 #include <sys/vnode.h>
43 #include <sys/malloc.h>
46 #include <net/if_types.h>
47 #include <net/ifq_var.h>
48 #include <net/netisr.h>
49 #include <net/route.h>
52 #include <netinet/in.h>
57 #include "if_tunvar.h"
60 static MALLOC_DEFINE(M_TUN
, "tun", "Tunnel Interface");
62 static void tunattach (void *);
63 PSEUDO_SET(tunattach
, if_tun
);
65 static void tuncreate (cdev_t dev
);
67 #define TUNDEBUG if (tundebug) if_printf
68 static int tundebug
= 0;
69 SYSCTL_INT(_debug
, OID_AUTO
, if_tun_debug
, CTLFLAG_RW
, &tundebug
, 0, "");
71 static int tunoutput (struct ifnet
*, struct mbuf
*, struct sockaddr
*,
73 static int tunifioctl (struct ifnet
*, u_long
, caddr_t
, struct ucred
*);
74 static int tuninit (struct ifnet
*);
75 static void tunstart(struct ifnet
*);
77 static d_open_t tunopen
;
78 static d_close_t tunclose
;
79 static d_read_t tunread
;
80 static d_write_t tunwrite
;
81 static d_ioctl_t tunioctl
;
82 static d_poll_t tunpoll
;
85 static struct dev_ops tun_ops
= {
86 { "tun", CDEV_MAJOR
, 0 },
96 tunattach(void *dummy
)
98 dev_ops_add(&tun_ops
, 0, 0);
102 tuncreate(cdev_t dev
)
104 struct tun_softc
*sc
;
107 dev
= make_dev(&tun_ops
, minor(dev
),
108 UID_UUCP
, GID_DIALER
, 0600, "tun%d", lminor(dev
));
110 MALLOC(sc
, struct tun_softc
*, sizeof(*sc
), M_TUN
, M_WAITOK
| M_ZERO
);
111 sc
->tun_flags
= TUN_INITED
;
114 if_initname(ifp
, "tun", lminor(dev
));
115 ifp
->if_mtu
= TUNMTU
;
116 ifp
->if_ioctl
= tunifioctl
;
117 ifp
->if_output
= tunoutput
;
118 ifp
->if_start
= tunstart
;
119 ifp
->if_flags
= IFF_POINTOPOINT
| IFF_MULTICAST
;
120 ifp
->if_type
= IFT_PPP
;
121 ifq_set_maxlen(&ifp
->if_snd
, ifqmaxlen
);
122 ifq_set_ready(&ifp
->if_snd
);
124 if_attach(ifp
, NULL
);
125 bpfattach(ifp
, DLT_NULL
, sizeof(u_int
));
130 * tunnel open - must be superuser & the device must be
134 tunopen(struct dev_open_args
*ap
)
136 cdev_t dev
= ap
->a_head
.a_dev
;
138 struct tun_softc
*tp
;
141 if ((error
= suser_cred(ap
->a_cred
, 0)) != 0)
149 if (tp
->tun_flags
& TUN_OPEN
)
151 tp
->tun_pid
= curproc
->p_pid
;
153 tp
->tun_flags
|= TUN_OPEN
;
154 TUNDEBUG(ifp
, "open\n");
159 * tunclose - close the device - mark i/f down & delete
163 tunclose(struct dev_close_args
*ap
)
165 cdev_t dev
= ap
->a_head
.a_dev
;
166 struct tun_softc
*tp
;
172 tp
->tun_flags
&= ~TUN_OPEN
;
175 /* Junk all pending output. */
176 lwkt_serialize_enter(ifp
->if_serializer
);
177 ifq_purge(&ifp
->if_snd
);
178 lwkt_serialize_exit(ifp
->if_serializer
);
180 if (ifp
->if_flags
& IFF_UP
) {
181 lwkt_serialize_enter(ifp
->if_serializer
);
183 lwkt_serialize_exit(ifp
->if_serializer
);
185 ifp
->if_flags
&= ~IFF_RUNNING
;
186 if_purgeaddrs_nolink(ifp
);
188 funsetown(tp
->tun_sigio
);
189 selwakeup(&tp
->tun_rsel
);
191 TUNDEBUG(ifp
, "closed\n");
196 tuninit(struct ifnet
*ifp
)
198 struct tun_softc
*tp
= ifp
->if_softc
;
199 struct ifaddr_container
*ifac
;
202 TUNDEBUG(ifp
, "tuninit\n");
204 ifp
->if_flags
|= IFF_UP
| IFF_RUNNING
;
205 getmicrotime(&ifp
->if_lastchange
);
207 TAILQ_FOREACH(ifac
, &ifp
->if_addrheads
[mycpuid
], ifa_link
) {
208 struct ifaddr
*ifa
= ifac
->ifa
;
210 if (ifa
->ifa_addr
== NULL
) {
212 /* XXX: Should maybe return straight off? */
215 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
216 struct sockaddr_in
*si
;
218 si
= (struct sockaddr_in
*)ifa
->ifa_addr
;
219 if (si
->sin_addr
.s_addr
)
220 tp
->tun_flags
|= TUN_IASET
;
229 * Process an ioctl request.
234 tunifioctl(struct ifnet
*ifp
, u_long cmd
, caddr_t data
, struct ucred
*cr
)
236 struct ifreq
*ifr
= (struct ifreq
*)data
;
237 struct tun_softc
*tp
= ifp
->if_softc
;
243 ifs
= (struct ifstat
*)data
;
245 ksprintf(ifs
->ascii
+ strlen(ifs
->ascii
),
246 "\tOpened by PID %d\n", tp
->tun_pid
);
249 error
= tuninit(ifp
);
250 TUNDEBUG(ifp
, "address set, error=%d\n", error
);
253 error
= tuninit(ifp
);
254 TUNDEBUG(ifp
, "destination address set, error=%d\n", error
);
257 ifp
->if_mtu
= ifr
->ifr_mtu
;
258 TUNDEBUG(ifp
, "mtu set\n");
271 * tunoutput - queue packets from higher level ready to put out.
276 tunoutput_serialized(struct ifnet
*ifp
, struct mbuf
*m0
, struct sockaddr
*dst
,
279 struct tun_softc
*tp
= ifp
->if_softc
;
281 struct altq_pktattr pktattr
;
283 TUNDEBUG(ifp
, "tunoutput\n");
285 if ((tp
->tun_flags
& TUN_READY
) != TUN_READY
) {
286 TUNDEBUG(ifp
, "not ready 0%o\n", tp
->tun_flags
);
292 * if the queueing discipline needs packet classification,
293 * do it before prepending link headers.
295 ifq_classify(&ifp
->if_snd
, m0
, dst
->sa_family
, &pktattr
);
297 /* BPF write needs to be handled specially */
298 if (dst
->sa_family
== AF_UNSPEC
) {
299 dst
->sa_family
= *(mtod(m0
, int *));
300 m0
->m_len
-= sizeof(int);
301 m0
->m_pkthdr
.len
-= sizeof(int);
302 m0
->m_data
+= sizeof(int);
307 * We need to prepend the address family as
310 uint32_t af
= dst
->sa_family
;
312 bpf_ptap(ifp
->if_bpf
, m0
, &af
, sizeof(af
));
315 /* prepend sockaddr? this may abort if the mbuf allocation fails */
316 if (tp
->tun_flags
& TUN_LMODE
) {
317 /* allocate space for sockaddr */
318 M_PREPEND(m0
, dst
->sa_len
, MB_DONTWAIT
);
320 /* if allocation failed drop packet */
322 IF_DROP(&ifp
->if_snd
);
326 bcopy(dst
, m0
->m_data
, dst
->sa_len
);
330 if (tp
->tun_flags
& TUN_IFHEAD
) {
331 /* Prepend the address family */
332 M_PREPEND(m0
, 4, MB_DONTWAIT
);
334 /* if allocation failed drop packet */
336 IF_DROP(&ifp
->if_snd
);
340 *(u_int32_t
*)m0
->m_data
= htonl(dst
->sa_family
);
343 if (dst
->sa_family
!= AF_INET
)
351 error
= ifq_handoff(ifp
, m0
, &pktattr
);
353 ifp
->if_collisions
++;
356 if (tp
->tun_flags
& TUN_RWAIT
) {
357 tp
->tun_flags
&= ~TUN_RWAIT
;
361 if (tp
->tun_flags
& TUN_ASYNC
&& tp
->tun_sigio
)
362 pgsigio(tp
->tun_sigio
, SIGIO
, 0);
363 selwakeup(&tp
->tun_rsel
);
370 tunoutput(struct ifnet
*ifp
, struct mbuf
*m0
, struct sockaddr
*dst
,
375 lwkt_serialize_enter(ifp
->if_serializer
);
376 error
= tunoutput_serialized(ifp
, m0
, dst
, rt
);
377 lwkt_serialize_exit(ifp
->if_serializer
);
383 * the ops interface is now pretty minimal.
386 tunioctl(struct dev_ioctl_args
*ap
)
388 cdev_t dev
= ap
->a_head
.a_dev
;
389 struct tun_softc
*tp
= dev
->si_drv1
;
390 struct tuninfo
*tunp
;
394 tunp
= (struct tuninfo
*)ap
->a_data
;
395 if (tunp
->mtu
< IF_MINMTU
)
397 tp
->tun_if
.if_mtu
= tunp
->mtu
;
398 tp
->tun_if
.if_type
= tunp
->type
;
399 tp
->tun_if
.if_baudrate
= tunp
->baudrate
;
402 tunp
= (struct tuninfo
*)ap
->a_data
;
403 tunp
->mtu
= tp
->tun_if
.if_mtu
;
404 tunp
->type
= tp
->tun_if
.if_type
;
405 tunp
->baudrate
= tp
->tun_if
.if_baudrate
;
408 tundebug
= *(int *)ap
->a_data
;
411 *(int *)ap
->a_data
= tundebug
;
414 if (*(int *)ap
->a_data
) {
415 tp
->tun_flags
|= TUN_LMODE
;
416 tp
->tun_flags
&= ~TUN_IFHEAD
;
418 tp
->tun_flags
&= ~TUN_LMODE
;
421 if (*(int *)ap
->a_data
) {
422 tp
->tun_flags
|= TUN_IFHEAD
;
423 tp
->tun_flags
&= ~TUN_LMODE
;
425 tp
->tun_flags
&= ~TUN_IFHEAD
;
428 *(int *)ap
->a_data
= (tp
->tun_flags
& TUN_IFHEAD
) ? 1 : 0;
431 /* deny this if UP */
432 if (tp
->tun_if
.if_flags
& IFF_UP
)
435 switch (*(int *)ap
->a_data
& ~IFF_MULTICAST
) {
436 case IFF_POINTOPOINT
:
438 tp
->tun_if
.if_flags
&= ~(IFF_BROADCAST
|IFF_POINTOPOINT
);
439 tp
->tun_if
.if_flags
|= *(int *)ap
->a_data
;
446 tp
->tun_pid
= curproc
->p_pid
;
449 if (*(int *)ap
->a_data
)
450 tp
->tun_flags
|= TUN_ASYNC
;
452 tp
->tun_flags
&= ~TUN_ASYNC
;
455 lwkt_serialize_enter(tp
->tun_if
.if_serializer
);
456 if (!ifq_is_empty(&tp
->tun_if
.if_snd
)) {
459 mb
= ifq_poll(&tp
->tun_if
.if_snd
);
460 for( *(int *)ap
->a_data
= 0; mb
!= 0; mb
= mb
->m_next
)
461 *(int *)ap
->a_data
+= mb
->m_len
;
463 *(int *)ap
->a_data
= 0;
465 lwkt_serialize_exit(tp
->tun_if
.if_serializer
);
468 return (fsetown(*(int *)ap
->a_data
, &tp
->tun_sigio
));
471 *(int *)ap
->a_data
= fgetown(tp
->tun_sigio
);
474 /* This is deprecated, FIOSETOWN should be used instead. */
476 return (fsetown(-(*(int *)ap
->a_data
), &tp
->tun_sigio
));
478 /* This is deprecated, FIOGETOWN should be used instead. */
480 *(int *)ap
->a_data
= -fgetown(tp
->tun_sigio
);
490 * The ops read interface - reads a packet at a time, or at
491 * least as much of a packet as can be read.
494 tunread(struct dev_read_args
*ap
)
496 cdev_t dev
= ap
->a_head
.a_dev
;
497 struct uio
*uio
= ap
->a_uio
;
498 struct tun_softc
*tp
= dev
->si_drv1
;
499 struct ifnet
*ifp
= &tp
->tun_if
;
503 TUNDEBUG(ifp
, "read\n");
504 if ((tp
->tun_flags
& TUN_READY
) != TUN_READY
) {
505 TUNDEBUG(ifp
, "not ready 0%o\n", tp
->tun_flags
);
509 tp
->tun_flags
&= ~TUN_RWAIT
;
511 lwkt_serialize_enter(ifp
->if_serializer
);
513 while ((m0
= ifq_dequeue(&ifp
->if_snd
, NULL
)) == NULL
) {
514 if (ap
->a_ioflag
& IO_NDELAY
) {
515 lwkt_serialize_exit(ifp
->if_serializer
);
518 tp
->tun_flags
|= TUN_RWAIT
;
519 lwkt_serialize_exit(ifp
->if_serializer
);
520 if ((error
= tsleep(tp
, PCATCH
, "tunread", 0)) != 0)
522 lwkt_serialize_enter(ifp
->if_serializer
);
525 lwkt_serialize_exit(ifp
->if_serializer
);
527 while (m0
&& uio
->uio_resid
> 0 && error
== 0) {
528 len
= min(uio
->uio_resid
, m0
->m_len
);
530 error
= uiomove(mtod(m0
, caddr_t
), len
, uio
);
535 TUNDEBUG(ifp
, "Dropping mbuf\n");
542 * the ops write interface - an atomic write is a packet - or else!
545 tunwrite(struct dev_write_args
*ap
)
547 cdev_t dev
= ap
->a_head
.a_dev
;
548 struct uio
*uio
= ap
->a_uio
;
549 struct tun_softc
*tp
= dev
->si_drv1
;
550 struct ifnet
*ifp
= &tp
->tun_if
;
551 struct mbuf
*top
, **mp
, *m
;
552 int error
=0, tlen
, mlen
;
556 TUNDEBUG(ifp
, "tunwrite\n");
558 if (uio
->uio_resid
== 0)
561 if (uio
->uio_resid
< 0 || uio
->uio_resid
> TUNMRU
) {
562 TUNDEBUG(ifp
, "len=%d!\n", uio
->uio_resid
);
565 tlen
= uio
->uio_resid
;
567 /* get a header mbuf */
568 MGETHDR(m
, MB_DONTWAIT
, MT_DATA
);
575 while (error
== 0 && uio
->uio_resid
> 0) {
576 m
->m_len
= min(mlen
, uio
->uio_resid
);
577 error
= uiomove(mtod (m
, caddr_t
), m
->m_len
, uio
);
580 if (uio
->uio_resid
> 0) {
581 MGET (m
, MB_DONTWAIT
, MT_DATA
);
596 top
->m_pkthdr
.len
= tlen
;
597 top
->m_pkthdr
.rcvif
= ifp
;
600 if (tp
->tun_flags
& TUN_IFHEAD
) {
602 * Conveniently, we already have a 4-byte address
603 * family prepended to our packet !
604 * Inconveniently, it's in the wrong byte order !
606 if ((top
= m_pullup(top
, sizeof(family
))) == NULL
)
608 *mtod(top
, u_int32_t
*) =
609 ntohl(*mtod(top
, u_int32_t
*));
610 bpf_mtap(ifp
->if_bpf
, top
);
611 *mtod(top
, u_int32_t
*) =
612 htonl(*mtod(top
, u_int32_t
*));
615 * We need to prepend the address family as
618 static const uint32_t af
= AF_INET
;
620 bpf_ptap(ifp
->if_bpf
, top
, &af
, sizeof(af
));
624 if (tp
->tun_flags
& TUN_IFHEAD
) {
625 if (top
->m_len
< sizeof(family
) &&
626 (top
= m_pullup(top
, sizeof(family
))) == NULL
)
628 family
= ntohl(*mtod(top
, u_int32_t
*));
629 m_adj(top
, sizeof(family
));
633 ifp
->if_ibytes
+= top
->m_pkthdr
.len
;
659 return (EAFNOSUPPORT
);
662 netisr_dispatch(isr
, top
);
667 * tunpoll - the poll interface, this is only useful on reads
668 * really. The write detect always returns true, write never blocks
669 * anyway, it either accepts the packet or drops it.
672 tunpoll(struct dev_poll_args
*ap
)
674 cdev_t dev
= ap
->a_head
.a_dev
;
675 struct tun_softc
*tp
= dev
->si_drv1
;
676 struct ifnet
*ifp
= &tp
->tun_if
;
679 TUNDEBUG(ifp
, "tunpoll\n");
681 lwkt_serialize_enter(ifp
->if_serializer
);
683 if (ap
->a_events
& (POLLIN
| POLLRDNORM
)) {
684 if (!ifq_is_empty(&ifp
->if_snd
)) {
685 TUNDEBUG(ifp
, "tunpoll q=%d\n", ifp
->if_snd
.ifq_len
);
686 revents
|= ap
->a_events
& (POLLIN
| POLLRDNORM
);
688 TUNDEBUG(ifp
, "tunpoll waiting\n");
689 selrecord(curthread
, &tp
->tun_rsel
);
692 if (ap
->a_events
& (POLLOUT
| POLLWRNORM
))
693 revents
|= ap
->a_events
& (POLLOUT
| POLLWRNORM
);
695 lwkt_serialize_exit(ifp
->if_serializer
);
696 ap
->a_events
= revents
;
701 * Start packet transmission on the interface.
702 * when the interface queue is rate-limited by ALTQ,
703 * if_start is needed to drain packets from the queue in order
704 * to notify readers when outgoing packets become ready.
707 tunstart(struct ifnet
*ifp
)
709 struct tun_softc
*tp
= ifp
->if_softc
;
712 if (!ifq_is_enabled(&ifp
->if_snd
))
715 m
= ifq_poll(&ifp
->if_snd
);
717 if (tp
->tun_flags
& TUN_RWAIT
) {
718 tp
->tun_flags
&= ~TUN_RWAIT
;
721 if (tp
->tun_flags
& TUN_ASYNC
&& tp
->tun_sigio
)
722 pgsigio(tp
->tun_sigio
, SIGIO
, 0);
723 selwakeup(&tp
->tun_rsel
);