]>
git.saurik.com Git - apple/xnu.git/blob - bsd/net/if_tun.c
8ed3afd27f11d66c491004be2ba2bd2a31025912
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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.
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
20 * @APPLE_LICENSE_HEADER_END@
22 /* $NetBSD: if_tun.c,v 1.14 1994/06/29 06:36:25 cgd Exp $ */
25 * Copyright (c) 1988, Julian Onions <jpo@cs.nott.ac.uk>
26 * Nottingham University 1987.
28 * This source may be freely distributed, however I would be interested
29 * in any changes that are made.
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
41 #include "opt_devfs.h"
44 #include <sys/param.h>
46 #include <sys/systm.h>
48 #include <sys/socket.h>
49 #include <sys/filio.h>
50 #include <sys/sockio.h>
51 #include <sys/ttycom.h>
53 #include <sys/signalvar.h>
54 #include <sys/filedesc.h>
55 #include <sys/kernel.h>
56 #include <sys/sysctl.h>
58 #include <sys/devfsext.h>
62 #include <sys/vnode.h>
65 #include <net/if_types.h>
66 #include <net/netisr.h>
67 #include <net/route.h>
70 #include <netinet/in.h>
71 #include <netinet/in_var.h>
75 #include <netinet/ip6.h>
76 #include <netinet6/ip6_var.h>
77 #include <netinet6/in6_ifattach.h>
85 #include <net/if_tunvar.h>
86 #include <net/if_tun.h>
88 static void tunattach
__P((void *));
89 PSEUDO_SET(tunattach
, if_tun
);
91 #define TUNDEBUG if (tundebug) printf
92 static int tundebug
= 0;
93 SYSCTL_INT(_debug
, OID_AUTO
, if_tun_debug
, CTLFLAG_RW
, &tundebug
, 0, "");
95 static struct tun_softc tunctl
[NTUN
];
97 static int tunoutput
__P((struct ifnet
*, struct mbuf
*, struct sockaddr
*,
99 static int tunifioctl
__P((struct ifnet
*, u_long
, caddr_t
));
100 static int tuninit
__P((int, int, u_char
));
102 static d_open_t tunopen
;
103 static d_close_t tunclose
;
104 static d_read_t tunread
;
105 static d_write_t tunwrite
;
106 static d_ioctl_t tunioctl
;
107 static d_poll_t tunpoll
;
109 #define CDEV_MAJOR 52
110 static struct cdevsw tun_cdevsw
= {
111 tunopen
, tunclose
, tunread
, tunwrite
,
112 tunioctl
, nullstop
, noreset
, nodevtotty
,
113 tunpoll
, nommap
, nostrategy
, "tun", NULL
, -1
117 static int tun_devsw_installed
;
119 static void *tun_devfs_token
[NTUN
];
122 #define minor_val(n) ((((n) & ~0xff) << 8) | ((n) & 0xff))
123 #define dev_val(n) (((n) >> 8) | ((n) & 0xff))
133 if ( tun_devsw_installed
)
135 dev
= makedev(CDEV_MAJOR
, 0);
136 cdevsw_add(&dev
, &tun_cdevsw
, NULL
);
137 tun_devsw_installed
= 1;
138 for ( i
= 0; i
< NTUN
; i
++ ) {
140 tun_devfs_token
[i
] = devfs_add_devswf(&tun_cdevsw
, minor_val(i
),
145 tunctl
[i
].tun_flags
= TUN_INITED
;
147 ifp
= &tunctl
[i
].tun_if
;
149 ifp
->if_name
= "tun";
150 ifp
->if_family
= APPLE_IF_FAM_TUN
;
151 ifp
->if_mtu
= TUNMTU
;
152 ifp
->if_ioctl
= tunifioctl
;
153 ifp
->if_output
= tunoutput
;
154 ifp
->if_flags
= IFF_POINTOPOINT
| IFF_MULTICAST
;
155 ifp
->if_type
= IFT_PPP
; /* necessary init value for IPv6 lladdr auto conf */
156 ifp
->if_snd
.ifq_maxlen
= ifqmaxlen
;
159 bpfattach(ifp
, DLT_NULL
, sizeof(u_int
));
165 * tunnel open - must be superuser & the device must be
169 tunopen(dev
, flag
, mode
, p
)
175 struct tun_softc
*tp
;
176 register int unit
, error
;
178 error
= suser(p
->p_ucred
, &p
->p_acflag
);
182 if ((unit
= dev_val(minor(dev
))) >= NTUN
)
185 if (tp
->tun_flags
& TUN_OPEN
)
188 tp
->tun_flags
|= TUN_OPEN
;
189 TUNDEBUG("%s%d: open\n", ifp
->if_name
, ifp
->if_unit
);
194 * tunclose - close the device - mark i/f down & delete
198 tunclose(dev
, foo
, bar
, p
)
204 register int unit
= dev_val(minor(dev
)), s
;
205 struct tun_softc
*tp
= &tunctl
[unit
];
206 struct ifnet
*ifp
= &tp
->tun_if
;
209 tp
->tun_flags
&= ~TUN_OPEN
;
212 * junk all pending output
216 IF_DEQUEUE(&ifp
->if_snd
, m
);
222 if (ifp
->if_flags
& IFF_UP
) {
225 if (ifp
->if_flags
& IFF_RUNNING
) {
226 /* find internet addresses and delete routes */
227 register struct ifaddr
*ifa
;
228 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
229 ifa
= ifa
->ifa_link
.tqe_next
) {
230 switch (ifa
->ifa_addr
->sa_family
) {
237 rtinit(ifa
, (int)RTM_DELETE
,
238 tp
->tun_flags
& TUN_DSTADDR
? RTF_HOST
: 0);
245 ifp
->if_flags
&= ~IFF_RUNNING
;
246 funsetown(tp
->tun_sigio
);
247 selwakeup(&tp
->tun_rsel
);
248 selthreadclear(&tp
->tun_rsel
);
250 TUNDEBUG ("%s%d: closed\n", ifp
->if_name
, ifp
->if_unit
);
255 tuninit(unit
, cmd
, af
)
260 struct tun_softc
*tp
= &tunctl
[unit
];
261 struct ifnet
*ifp
= &tp
->tun_if
;
262 register struct ifaddr
*ifa
;
264 TUNDEBUG("%s%d: tuninit\n", ifp
->if_name
, ifp
->if_unit
);
266 ifp
->if_flags
|= IFF_UP
| IFF_RUNNING
;
267 getmicrotime(&ifp
->if_lastchange
);
269 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
270 ifa
= ifa
->ifa_link
.tqe_next
) {
272 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
273 struct sockaddr_in
*si
;
275 si
= (struct sockaddr_in
*)ifa
->ifa_addr
;
276 if (si
&& si
->sin_addr
.s_addr
)
277 tp
->tun_flags
|= TUN_IASET
;
279 si
= (struct sockaddr_in
*)ifa
->ifa_dstaddr
;
280 if (si
&& si
->sin_addr
.s_addr
)
281 tp
->tun_flags
|= TUN_DSTADDR
;
289 * Process an ioctl request.
292 tunifioctl(ifp
, cmd
, data
)
297 register struct ifreq
*ifr
= (struct ifreq
*)data
;
303 ifs
= (struct ifstat
*)data
;
305 sprintf(ifs
->ascii
+ strlen(ifs
->ascii
),
306 "\tOpened by PID %d\n", tp
->tun_pid
);
309 tuninit(ifp
->if_unit
);
310 TUNDEBUG("%s%d: address set\n",
311 ifp
->if_name
, ifp
->if_unit
);
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 */
320 tuninit(ifp
->if_unit
, cmd
, ifr
->ifr_addr
.sa_family
);
323 ifp
->if_mtu
= ifr
->ifr_mtu
;
324 TUNDEBUG("%s%d: mtu set\n",
325 ifp
->if_name
, ifp
->if_unit
);
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
;
346 * tunoutput - queue packets from higher level ready to put out.
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.
352 * 0 1 2 3 4 5 6 7 8 .....
353 * ------------------------------
354 * | af | packet data .....
355 * ------------------------------
357 * Newly added part. The size is sizeof(u_long).
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)
369 tunoutput(ifp
, m0
, dst
, rt
)
372 struct sockaddr
*dst
;
375 struct tun_softc
*tp
= &tunctl
[ifp
->if_unit
];
378 TUNDEBUG ("%s%d: tunoutput\n", ifp
->if_name
, ifp
->if_unit
);
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
);
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);
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).
405 u_int af
= dst
->sa_family
;
409 m
.m_data
= (char *)&af
;
415 switch(dst
->sa_family
) {
416 #if defined(INET) || defined(INET6)
419 M_PREPEND(m0
, sizeof(u_long
) /* af field passed to upper */,
423 *mtod(m0
, u_long
*) = (u_long
)dst
->sa_family
;
429 #endif /* INET || INET6 */
431 if (IF_QFULL(&ifp
->if_snd
)) {
432 IF_DROP(&ifp
->if_snd
);
435 ifp
->if_collisions
++;
438 ifp
->if_obytes
+= m0
->m_pkthdr
.len
;
439 IF_ENQUEUE(&ifp
->if_snd
, m0
);
448 if (tp
->tun_flags
& TUN_RWAIT
) {
449 tp
->tun_flags
&= ~TUN_RWAIT
;
452 if (tp
->tun_flags
& TUN_ASYNC
&& tp
->tun_sigio
)
453 pgsigio(tp
->tun_sigio
, SIGIO
, 0);
454 selwakeup(&tp
->tun_rsel
);
459 * the cdevsw interface is now pretty minimal.
462 tunioctl(dev
, cmd
, data
, flag
, p
)
469 int unit
= dev_val(minor(dev
)), s
;
470 struct tun_softc
*tp
= &tunctl
[unit
];
471 struct tuninfo
*tunp
;
475 tunp
= (struct tuninfo
*)data
;
476 tp
->tun_if
.if_mtu
= tunp
->mtu
;
477 tp
->tun_if
.if_type
= tunp
->type
;
478 tp
->tun_if
.if_baudrate
= tunp
->baudrate
;
481 tunp
= (struct tuninfo
*)data
;
482 tunp
->mtu
= tp
->tun_if
.if_mtu
;
483 tunp
->type
= tp
->tun_if
.if_type
;
484 tunp
->baudrate
= tp
->tun_if
.if_baudrate
;
487 tundebug
= *(int *)data
;
490 *(int *)data
= tundebug
;
496 tp
->tun_flags
|= TUN_ASYNC
;
498 tp
->tun_flags
&= ~TUN_ASYNC
;
502 if (tp
->tun_if
.if_snd
.ifq_head
) {
503 struct mbuf
*mb
= tp
->tun_if
.if_snd
.ifq_head
;
504 for( *(int *)data
= 0; mb
!= 0; mb
= mb
->m_next
)
505 *(int *)data
+= mb
->m_len
;
511 return (fsetown(*(int *)data
, &tp
->tun_sigio
));
514 *(int *)data
= fgetown(tp
->tun_sigio
);
517 /* This is deprecated, FIOSETOWN should be used instead. */
519 return (fsetown(-(*(int *)data
), &tp
->tun_sigio
));
521 /* This is deprecated, FIOGETOWN should be used instead. */
523 *(int *)data
= -fgetown(tp
->tun_sigio
);
533 * The cdevsw read interface - reads a packet at a time, or at
534 * least as much of a packet as can be read.
537 tunread(dev
, uio
, flag
)
542 int unit
= dev_val(minor(dev
));
543 struct tun_softc
*tp
= &tunctl
[unit
];
544 struct ifnet
*ifp
= &tp
->tun_if
;
548 TUNDEBUG ("%s%d: read\n", ifp
->if_name
, ifp
->if_unit
);
549 if ((tp
->tun_flags
& TUN_READY
) != TUN_READY
) {
550 TUNDEBUG ("%s%d: not ready 0%o\n", ifp
->if_name
,
551 ifp
->if_unit
, tp
->tun_flags
);
555 tp
->tun_flags
&= ~TUN_RWAIT
;
559 IF_DEQUEUE(&ifp
->if_snd
, m0
);
561 if (flag
& IO_NDELAY
) {
565 tp
->tun_flags
|= TUN_RWAIT
;
566 if( error
= tsleep((caddr_t
)tp
, PCATCH
| (PZERO
+ 1),
575 while (m0
&& uio
->uio_resid
> 0 && error
== 0) {
576 len
= min(uio
->uio_resid
, m0
->m_len
);
579 error
= uiomove(mtod(m0
, caddr_t
), len
, uio
);
585 TUNDEBUG("Dropping mbuf\n");
592 * the cdevsw write interface - an atomic write is a packet - or else!
594 /* See top of tunoutput() about interface change between ppp process and
597 tunwrite(dev
, uio
, flag
)
602 int unit
= dev_val(minor(dev
));
603 struct ifnet
*ifp
= &tunctl
[unit
].tun_if
;
604 struct mbuf
*top
, **mp
, *m
;
605 int error
=0, s
, tlen
, mlen
;
608 struct ifqueue
*afintrq
= NULL
;
610 TUNDEBUG("%s%d: tunwrite\n", ifp
->if_name
, ifp
->if_unit
);
612 if (uio
->uio_resid
< 0 || uio
->uio_resid
> TUNMRU
) {
613 TUNDEBUG("%s%d: len=%d!\n", ifp
->if_name
, ifp
->if_unit
,
617 tlen
= uio
->uio_resid
;
619 /* get a header mbuf */
620 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
624 MCLGET(m
, M_DONTWAIT
);
625 if ((m
->m_flags
& M_EXT
) == 0) {
629 mlen
= m
->m_ext
.ext_size
;
635 while (error
== 0 && uio
->uio_resid
> 0) {
636 m
->m_len
= min(mlen
, uio
->uio_resid
);
637 error
= uiomove(mtod (m
, caddr_t
), m
->m_len
, uio
);
640 if (uio
->uio_resid
> 0) {
641 MGET (m
, M_DONTWAIT
, MT_DATA
);
649 /* Change for checking Address Family of sending packet. */
650 af
= *mtod(top
, u_long
*);
654 netisr_af
= NETISR_IP
;
660 netisr_af
= NETISR_IPV6
;
665 if (af
> 255) { /* see description at the top of tunoutput */
667 netisr_af
= NETISR_IP
;
671 error
= EAFNOSUPPORT
;
674 m_adj(top
, sizeof(u_long
)); /* remove af field passed from upper */
675 tlen
-= sizeof(u_long
);
683 top
->m_pkthdr
.len
= tlen
;
684 top
->m_pkthdr
.rcvif
= ifp
;
689 * We need to prepend the address family as
690 * a four byte field. Cons up a dummy header
691 * to pacify bpf. This is safe because bpf
692 * will only read from the mbuf (i.e., it won't
693 * try to free it or keep a pointer to it).
699 m
.m_data
= (char *)&af
;
705 /* just for safety */
710 if (IF_QFULL (afintrq
)) {
713 ifp
->if_collisions
++;
717 IF_ENQUEUE(afintrq
, top
);
719 ifp
->if_ibytes
+= tlen
;
721 schednetisr(netisr_af
);
726 * tunpoll - the poll interface, this is only useful on reads
727 * really. The write detect always returns true, write never blocks
728 * anyway, it either accepts the packet or drops it.
731 tunpoll(dev
, events
, wql
, p
)
737 int unit
= dev_val(minor(dev
)), s
;
738 struct tun_softc
*tp
= &tunctl
[unit
];
739 struct ifnet
*ifp
= &tp
->tun_if
;
743 TUNDEBUG("%s%d: tunpoll\n", ifp
->if_name
, ifp
->if_unit
);
745 if (events
& (POLLIN
| POLLRDNORM
))
746 if (ifp
->if_snd
.ifq_len
> 0) {
747 TUNDEBUG("%s%d: tunpoll q=%d\n", ifp
->if_name
,
748 ifp
->if_unit
, ifp
->if_snd
.ifq_len
);
749 revents
|= events
& (POLLIN
| POLLRDNORM
);
751 TUNDEBUG("%s%d: tunpoll waiting\n", ifp
->if_name
,
753 selrecord(p
, &tp
->tun_rsel
, wql
);
756 if (events
& (POLLOUT
| POLLWRNORM
))
757 revents
|= events
& (POLLOUT
| POLLWRNORM
);