]>
git.saurik.com Git - apple/xnu.git/blob - bsd/net/if_tun.c
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $ */
28 * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
29 * Nottingham University 1987.
31 * This source may be freely distributed, however I would be interested
32 * in any changes that are made.
34 * This driver takes packets off the IP i/f and hands them up to a
35 * user process to have its wicked way with. This driver has it's
36 * roots in a similar driver written by Phil Cockcroft (formerly) at
37 * UCL. This driver is based much more on read/write/poll mode of
44 #include "opt_devfs.h"
47 #include <sys/param.h>
49 #include <sys/systm.h>
51 #include <sys/socket.h>
52 #include <sys/filio.h>
53 #include <sys/sockio.h>
54 #include <sys/ttycom.h>
56 #include <sys/signalvar.h>
57 #include <sys/filedesc.h>
58 #include <sys/kernel.h>
59 #include <sys/sysctl.h>
61 #include <sys/devfsext.h>
65 #include <sys/vnode.h>
68 #include <net/if_types.h>
69 #include <net/netisr.h>
70 #include <net/route.h>
73 #include <netinet/in.h>
74 #include <netinet/in_var.h>
78 #include <netinet/ip6.h>
79 #include <netinet6/ip6_var.h>
80 #include <netinet6/in6_ifattach.h>
88 #include <net/if_tunvar.h>
89 #include <net/if_tun.h>
91 static void tunattach
__P((void *));
92 PSEUDO_SET(tunattach
, if_tun
);
94 #define TUNDEBUG if (tundebug) printf
95 static int tundebug
= 0;
96 SYSCTL_INT(_debug
, OID_AUTO
, if_tun_debug
, CTLFLAG_RW
, &tundebug
, 0, "");
98 static struct tun_softc tunctl
[NTUN
];
100 static int tunoutput
__P((struct ifnet
*, struct mbuf
*, struct sockaddr
*,
101 struct rtentry
*rt
));
102 static int tunifioctl
__P((struct ifnet
*, u_long
, caddr_t
));
103 static int tuninit
__P((int, int, u_char
));
105 static d_open_t tunopen
;
106 static d_close_t tunclose
;
107 static d_read_t tunread
;
108 static d_write_t tunwrite
;
109 static d_ioctl_t tunioctl
;
110 static d_poll_t tunpoll
;
112 #define CDEV_MAJOR 52
113 static struct cdevsw tun_cdevsw
= {
114 tunopen
, tunclose
, tunread
, tunwrite
,
115 tunioctl
, nullstop
, noreset
, nodevtotty
,
116 tunpoll
, nommap
, nostrategy
, "tun", NULL
, -1
120 static int tun_devsw_installed
;
122 static void *tun_devfs_token
[NTUN
];
125 #define minor_val(n) ((((n) & ~0xff) << 8) | ((n) & 0xff))
126 #define dev_val(n) (((n) >> 8) | ((n) & 0xff))
136 if ( tun_devsw_installed
)
138 dev
= makedev(CDEV_MAJOR
, 0);
139 cdevsw_add(&dev
, &tun_cdevsw
, NULL
);
140 tun_devsw_installed
= 1;
141 for ( i
= 0; i
< NTUN
; i
++ ) {
143 tun_devfs_token
[i
] = devfs_add_devswf(&tun_cdevsw
, minor_val(i
),
148 tunctl
[i
].tun_flags
= TUN_INITED
;
150 ifp
= &tunctl
[i
].tun_if
;
152 ifp
->if_name
= "tun";
153 ifp
->if_family
= APPLE_IF_FAM_TUN
;
154 ifp
->if_mtu
= TUNMTU
;
155 ifp
->if_ioctl
= tunifioctl
;
156 ifp
->if_output
= tunoutput
;
157 ifp
->if_flags
= IFF_POINTOPOINT
| IFF_MULTICAST
;
158 ifp
->if_type
= IFT_PPP
; /* necessary init value for IPv6 lladdr auto conf */
159 ifp
->if_snd
.ifq_maxlen
= ifqmaxlen
;
162 bpfattach(ifp
, DLT_NULL
, sizeof(u_int
));
168 * tunnel open - must be superuser & the device must be
172 tunopen(dev
, flag
, mode
, p
)
178 struct tun_softc
*tp
;
179 register int unit
, error
;
181 error
= suser(p
->p_ucred
, &p
->p_acflag
);
185 if ((unit
= dev_val(minor(dev
))) >= NTUN
)
188 if (tp
->tun_flags
& TUN_OPEN
)
191 tp
->tun_flags
|= TUN_OPEN
;
192 TUNDEBUG("%s%d: open\n", ifp
->if_name
, ifp
->if_unit
);
197 * tunclose - close the device - mark i/f down & delete
201 tunclose(dev
, foo
, bar
, p
)
207 register int unit
= dev_val(minor(dev
)), s
;
208 struct tun_softc
*tp
= &tunctl
[unit
];
209 struct ifnet
*ifp
= &tp
->tun_if
;
212 tp
->tun_flags
&= ~TUN_OPEN
;
215 * junk all pending output
219 IF_DEQUEUE(&ifp
->if_snd
, m
);
225 if (ifp
->if_flags
& IFF_UP
) {
228 if (ifp
->if_flags
& IFF_RUNNING
) {
229 /* find internet addresses and delete routes */
230 register struct ifaddr
*ifa
;
231 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
232 ifa
= ifa
->ifa_link
.tqe_next
) {
233 switch (ifa
->ifa_addr
->sa_family
) {
240 rtinit(ifa
, (int)RTM_DELETE
,
241 tp
->tun_flags
& TUN_DSTADDR
? RTF_HOST
: 0);
248 ifp
->if_flags
&= ~IFF_RUNNING
;
249 funsetown(tp
->tun_sigio
);
250 selwakeup(&tp
->tun_rsel
);
251 selthreadclear(&tp
->tun_rsel
);
253 TUNDEBUG ("%s%d: closed\n", ifp
->if_name
, ifp
->if_unit
);
258 tuninit(unit
, cmd
, af
)
263 struct tun_softc
*tp
= &tunctl
[unit
];
264 struct ifnet
*ifp
= &tp
->tun_if
;
265 register struct ifaddr
*ifa
;
267 TUNDEBUG("%s%d: tuninit\n", ifp
->if_name
, ifp
->if_unit
);
269 ifp
->if_flags
|= IFF_UP
| IFF_RUNNING
;
270 getmicrotime(&ifp
->if_lastchange
);
272 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
273 ifa
= ifa
->ifa_link
.tqe_next
) {
275 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
276 struct sockaddr_in
*si
;
278 si
= (struct sockaddr_in
*)ifa
->ifa_addr
;
279 if (si
&& si
->sin_addr
.s_addr
)
280 tp
->tun_flags
|= TUN_IASET
;
282 si
= (struct sockaddr_in
*)ifa
->ifa_dstaddr
;
283 if (si
&& si
->sin_addr
.s_addr
)
284 tp
->tun_flags
|= TUN_DSTADDR
;
292 * Process an ioctl request.
295 tunifioctl(ifp
, cmd
, data
)
300 register struct ifreq
*ifr
= (struct ifreq
*)data
;
306 ifs
= (struct ifstat
*)data
;
308 sprintf(ifs
->ascii
+ strlen(ifs
->ascii
),
309 "\tOpened by PID %d\n", tp
->tun_pid
);
312 tuninit(ifp
->if_unit
);
313 TUNDEBUG("%s%d: address set\n",
314 ifp
->if_name
, ifp
->if_unit
);
318 #if defined(INET6) && defined(__FreeBSD__) && __FreeBSD__ >= 3
319 if (found_first_ifid
== 0)
320 in6_ifattach_noifid(ifp
);
321 #endif /* defined(INET6) && defined(__FreeBSD__) && __FreeBSD__ >= 3 */
323 tuninit(ifp
->if_unit
, cmd
, ifr
->ifr_addr
.sa_family
);
326 ifp
->if_mtu
= ifr
->ifr_mtu
;
327 TUNDEBUG("%s%d: mtu set\n",
328 ifp
->if_name
, ifp
->if_unit
);
335 if ((ifp
->if_flags
& IFF_UP
) != 0)
336 ifp
->if_flags
|= IFF_RUNNING
;
337 else if ((ifp
->if_flags
& IFF_UP
) == 0)
338 ifp
->if_flags
&= ~IFF_RUNNING
;
349 * tunoutput - queue packets from higher level ready to put out.
351 /* Packet data format between tun and ppp is changed to enable checking of
352 * Address Family of sending packet. When INET6 is defined, 4byte AF field
353 * is appended to packet data as following.
355 * 0 1 2 3 4 5 6 7 8 .....
356 * ------------------------------
357 * | af | packet data .....
358 * ------------------------------
360 * Newly added part. The size is sizeof(u_long).
362 * However, this is not adopted for tun -> ppp AF_INET packet for
363 * backword compatibility, because the ppp process may be an existing
364 * ip only supporting one.
365 * Also in ppp->tun case, when af value is unknown, (af > 255) is checked and
366 * if it is true, AF_INET is assumed. (the 4byte may be the head of
367 * AF_INET packet. Despite the byte order, the value must always be
368 * greater than 255, because of ip_len field or (ip_v and ip_hl)
369 * field. (Idea from Mr. Noritoshi Demize)
372 tunoutput(ifp
, m0
, dst
, rt
)
375 struct sockaddr
*dst
;
378 struct tun_softc
*tp
= &tunctl
[ifp
->if_unit
];
381 TUNDEBUG ("%s%d: tunoutput\n", ifp
->if_name
, ifp
->if_unit
);
383 if ((tp
->tun_flags
& TUN_READY
) != TUN_READY
) {
384 TUNDEBUG ("%s%d: not ready 0%o\n", ifp
->if_name
,
385 ifp
->if_unit
, tp
->tun_flags
);
391 /* BPF write needs to be handled specially */
392 if (dst
->sa_family
== AF_UNSPEC
) {
393 dst
->sa_family
= *(mtod(m0
, int *));
394 m0
->m_len
-= sizeof(int);
395 m0
->m_pkthdr
.len
-= sizeof(int);
396 m0
->m_data
+= sizeof(int);
401 * We need to prepend the address family as
402 * a four byte field. Cons up a dummy header
403 * to pacify bpf. This is safe because bpf
404 * will only read from the mbuf (i.e., it won't
405 * try to free it or keep a pointer to it).
408 u_int af
= dst
->sa_family
;
412 m
.m_data
= (char *)&af
;
418 switch(dst
->sa_family
) {
419 #if defined(INET) || defined(INET6)
422 M_PREPEND(m0
, sizeof(u_long
) /* af field passed to upper */,
426 *mtod(m0
, u_long
*) = (u_long
)dst
->sa_family
;
432 #endif /* INET || INET6 */
434 if (IF_QFULL(&ifp
->if_snd
)) {
435 IF_DROP(&ifp
->if_snd
);
438 ifp
->if_collisions
++;
441 ifp
->if_obytes
+= m0
->m_pkthdr
.len
;
442 IF_ENQUEUE(&ifp
->if_snd
, m0
);
451 if (tp
->tun_flags
& TUN_RWAIT
) {
452 tp
->tun_flags
&= ~TUN_RWAIT
;
455 if (tp
->tun_flags
& TUN_ASYNC
&& tp
->tun_sigio
)
456 pgsigio(tp
->tun_sigio
, SIGIO
, 0);
457 selwakeup(&tp
->tun_rsel
);
462 * the cdevsw interface is now pretty minimal.
465 tunioctl(dev
, cmd
, data
, flag
, p
)
472 int unit
= dev_val(minor(dev
)), s
;
473 struct tun_softc
*tp
= &tunctl
[unit
];
474 struct tuninfo
*tunp
;
478 tunp
= (struct tuninfo
*)data
;
479 tp
->tun_if
.if_mtu
= tunp
->mtu
;
480 tp
->tun_if
.if_type
= tunp
->type
;
481 tp
->tun_if
.if_baudrate
= tunp
->baudrate
;
484 tunp
= (struct tuninfo
*)data
;
485 tunp
->mtu
= tp
->tun_if
.if_mtu
;
486 tunp
->type
= tp
->tun_if
.if_type
;
487 tunp
->baudrate
= tp
->tun_if
.if_baudrate
;
490 tundebug
= *(int *)data
;
493 *(int *)data
= tundebug
;
499 tp
->tun_flags
|= TUN_ASYNC
;
501 tp
->tun_flags
&= ~TUN_ASYNC
;
505 if (tp
->tun_if
.if_snd
.ifq_head
) {
506 struct mbuf
*mb
= tp
->tun_if
.if_snd
.ifq_head
;
507 for( *(int *)data
= 0; mb
!= 0; mb
= mb
->m_next
)
508 *(int *)data
+= mb
->m_len
;
514 return (fsetown(*(int *)data
, &tp
->tun_sigio
));
517 *(int *)data
= fgetown(tp
->tun_sigio
);
520 /* This is deprecated, FIOSETOWN should be used instead. */
522 return (fsetown(-(*(int *)data
), &tp
->tun_sigio
));
524 /* This is deprecated, FIOGETOWN should be used instead. */
526 *(int *)data
= -fgetown(tp
->tun_sigio
);
536 * The cdevsw read interface - reads a packet at a time, or at
537 * least as much of a packet as can be read.
540 tunread(dev
, uio
, flag
)
545 int unit
= dev_val(minor(dev
));
546 struct tun_softc
*tp
= &tunctl
[unit
];
547 struct ifnet
*ifp
= &tp
->tun_if
;
551 TUNDEBUG ("%s%d: read\n", ifp
->if_name
, ifp
->if_unit
);
552 if ((tp
->tun_flags
& TUN_READY
) != TUN_READY
) {
553 TUNDEBUG ("%s%d: not ready 0%o\n", ifp
->if_name
,
554 ifp
->if_unit
, tp
->tun_flags
);
558 tp
->tun_flags
&= ~TUN_RWAIT
;
562 IF_DEQUEUE(&ifp
->if_snd
, m0
);
564 if (flag
& IO_NDELAY
) {
568 tp
->tun_flags
|= TUN_RWAIT
;
569 if( error
= tsleep((caddr_t
)tp
, PCATCH
| (PZERO
+ 1),
578 while (m0
&& uio
->uio_resid
> 0 && error
== 0) {
579 len
= min(uio
->uio_resid
, m0
->m_len
);
582 error
= uiomove(mtod(m0
, caddr_t
), len
, uio
);
588 TUNDEBUG("Dropping mbuf\n");
595 * the cdevsw write interface - an atomic write is a packet - or else!
597 /* See top of tunoutput() about interface change between ppp process and
600 tunwrite(dev
, uio
, flag
)
605 int unit
= dev_val(minor(dev
));
606 struct ifnet
*ifp
= &tunctl
[unit
].tun_if
;
607 struct mbuf
*top
, **mp
, *m
;
608 int error
=0, s
, tlen
, mlen
;
611 struct ifqueue
*afintrq
= NULL
;
613 TUNDEBUG("%s%d: tunwrite\n", ifp
->if_name
, ifp
->if_unit
);
615 if (uio
->uio_resid
< 0 || uio
->uio_resid
> TUNMRU
) {
616 TUNDEBUG("%s%d: len=%d!\n", ifp
->if_name
, ifp
->if_unit
,
620 tlen
= uio
->uio_resid
;
622 /* get a header mbuf */
623 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
627 MCLGET(m
, M_DONTWAIT
);
628 if ((m
->m_flags
& M_EXT
) == 0) {
632 mlen
= m
->m_ext
.ext_size
;
638 while (error
== 0 && uio
->uio_resid
> 0) {
639 m
->m_len
= min(mlen
, uio
->uio_resid
);
640 error
= uiomove(mtod (m
, caddr_t
), m
->m_len
, uio
);
643 if (uio
->uio_resid
> 0) {
644 MGET (m
, M_DONTWAIT
, MT_DATA
);
652 /* Change for checking Address Family of sending packet. */
653 af
= *mtod(top
, u_long
*);
657 netisr_af
= NETISR_IP
;
663 netisr_af
= NETISR_IPV6
;
668 if (af
> 255) { /* see description at the top of tunoutput */
670 netisr_af
= NETISR_IP
;
674 error
= EAFNOSUPPORT
;
677 m_adj(top
, sizeof(u_long
)); /* remove af field passed from upper */
678 tlen
-= sizeof(u_long
);
686 top
->m_pkthdr
.len
= tlen
;
687 top
->m_pkthdr
.rcvif
= ifp
;
692 * We need to prepend the address family as
693 * a four byte field. Cons up a dummy header
694 * to pacify bpf. This is safe because bpf
695 * will only read from the mbuf (i.e., it won't
696 * try to free it or keep a pointer to it).
702 m
.m_data
= (char *)&af
;
708 /* just for safety */
713 if (IF_QFULL (afintrq
)) {
716 ifp
->if_collisions
++;
720 IF_ENQUEUE(afintrq
, top
);
722 ifp
->if_ibytes
+= tlen
;
724 schednetisr(netisr_af
);
729 * tunpoll - the poll interface, this is only useful on reads
730 * really. The write detect always returns true, write never blocks
731 * anyway, it either accepts the packet or drops it.
734 tunpoll(dev
, events
, wql
, p
)
740 int unit
= dev_val(minor(dev
)), s
;
741 struct tun_softc
*tp
= &tunctl
[unit
];
742 struct ifnet
*ifp
= &tp
->tun_if
;
746 TUNDEBUG("%s%d: tunpoll\n", ifp
->if_name
, ifp
->if_unit
);
748 if (events
& (POLLIN
| POLLRDNORM
))
749 if (ifp
->if_snd
.ifq_len
> 0) {
750 TUNDEBUG("%s%d: tunpoll q=%d\n", ifp
->if_name
,
751 ifp
->if_unit
, ifp
->if_snd
.ifq_len
);
752 revents
|= events
& (POLLIN
| POLLRDNORM
);
754 TUNDEBUG("%s%d: tunpoll waiting\n", ifp
->if_name
,
756 selrecord(p
, &tp
->tun_rsel
, wql
);
759 if (events
& (POLLOUT
| POLLWRNORM
))
760 revents
|= events
& (POLLOUT
| POLLWRNORM
);