]>
git.saurik.com Git - apple/xnu.git/blob - bsd/net/if_tun.c
833251c07ce469d9032fb21cb29ee4e00c1a7734
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>
82 #include <netns/ns_if.h>
90 #include <net/if_tunvar.h>
91 #include <net/if_tun.h>
93 static void tunattach
__P((void *));
94 PSEUDO_SET(tunattach
, if_tun
);
96 #define TUNDEBUG if (tundebug) printf
97 static int tundebug
= 0;
98 SYSCTL_INT(_debug
, OID_AUTO
, if_tun_debug
, CTLFLAG_RW
, &tundebug
, 0, "");
100 static struct tun_softc tunctl
[NTUN
];
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
));
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
;
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
122 static int tun_devsw_installed
;
124 static void *tun_devfs_token
[NTUN
];
127 #define minor_val(n) ((((n) & ~0xff) << 8) | ((n) & 0xff))
128 #define dev_val(n) (((n) >> 8) | ((n) & 0xff))
138 if ( tun_devsw_installed
)
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
++ ) {
145 tun_devfs_token
[i
] = devfs_add_devswf(&tun_cdevsw
, minor_val(i
),
150 tunctl
[i
].tun_flags
= TUN_INITED
;
152 ifp
= &tunctl
[i
].tun_if
;
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
;
164 bpfattach(ifp
, DLT_NULL
, sizeof(u_int
));
170 * tunnel open - must be superuser & the device must be
174 tunopen(dev
, flag
, mode
, p
)
180 struct tun_softc
*tp
;
181 register int unit
, error
;
183 error
= suser(p
->p_ucred
, &p
->p_acflag
);
187 if ((unit
= dev_val(minor(dev
))) >= NTUN
)
190 if (tp
->tun_flags
& TUN_OPEN
)
193 tp
->tun_flags
|= TUN_OPEN
;
194 TUNDEBUG("%s%d: open\n", ifp
->if_name
, ifp
->if_unit
);
199 * tunclose - close the device - mark i/f down & delete
203 tunclose(dev
, foo
, bar
, p
)
209 register int unit
= dev_val(minor(dev
)), s
;
210 struct tun_softc
*tp
= &tunctl
[unit
];
211 struct ifnet
*ifp
= &tp
->tun_if
;
214 tp
->tun_flags
&= ~TUN_OPEN
;
217 * junk all pending output
221 IF_DEQUEUE(&ifp
->if_snd
, m
);
227 if (ifp
->if_flags
& IFF_UP
) {
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
) {
242 rtinit(ifa
, (int)RTM_DELETE
,
243 tp
->tun_flags
& TUN_DSTADDR
? RTF_HOST
: 0);
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
);
256 TUNDEBUG ("%s%d: closed\n", ifp
->if_name
, ifp
->if_unit
);
261 tuninit(unit
, cmd
, af
)
266 struct tun_softc
*tp
= &tunctl
[unit
];
267 struct ifnet
*ifp
= &tp
->tun_if
;
268 register struct ifaddr
*ifa
;
270 TUNDEBUG("%s%d: tuninit\n", ifp
->if_name
, ifp
->if_unit
);
272 ifp
->if_flags
|= IFF_UP
| IFF_RUNNING
;
273 getmicrotime(&ifp
->if_lastchange
);
275 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
276 ifa
= ifa
->ifa_link
.tqe_next
) {
278 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
279 struct sockaddr_in
*si
;
281 si
= (struct sockaddr_in
*)ifa
->ifa_addr
;
282 if (si
&& si
->sin_addr
.s_addr
)
283 tp
->tun_flags
|= TUN_IASET
;
285 si
= (struct sockaddr_in
*)ifa
->ifa_dstaddr
;
286 if (si
&& si
->sin_addr
.s_addr
)
287 tp
->tun_flags
|= TUN_DSTADDR
;
295 * Process an ioctl request.
298 tunifioctl(ifp
, cmd
, data
)
303 register struct ifreq
*ifr
= (struct ifreq
*)data
;
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 thread_funnel_switch(NETWORK_FUNNEL
, KERNEL_FUNNEL
);
455 selwakeup(&tp
->tun_rsel
);
456 thread_funnel_switch(KERNEL_FUNNEL
, NETWORK_FUNNEL
);
461 * the cdevsw interface is now pretty minimal.
464 tunioctl(dev
, cmd
, data
, flag
, p
)
471 int unit
= dev_val(minor(dev
)), s
;
472 struct tun_softc
*tp
= &tunctl
[unit
];
473 struct tuninfo
*tunp
;
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
;
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
;
489 tundebug
= *(int *)data
;
492 *(int *)data
= tundebug
;
498 tp
->tun_flags
|= TUN_ASYNC
;
500 tp
->tun_flags
&= ~TUN_ASYNC
;
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
;
513 return (fsetown(*(int *)data
, &tp
->tun_sigio
));
516 *(int *)data
= fgetown(tp
->tun_sigio
);
519 /* This is deprecated, FIOSETOWN should be used instead. */
521 return (fsetown(-(*(int *)data
), &tp
->tun_sigio
));
523 /* This is deprecated, FIOGETOWN should be used instead. */
525 *(int *)data
= -fgetown(tp
->tun_sigio
);
535 * The cdevsw read interface - reads a packet at a time, or at
536 * least as much of a packet as can be read.
539 tunread(dev
, uio
, flag
)
544 int unit
= dev_val(minor(dev
));
545 struct tun_softc
*tp
= &tunctl
[unit
];
546 struct ifnet
*ifp
= &tp
->tun_if
;
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
);
557 tp
->tun_flags
&= ~TUN_RWAIT
;
561 IF_DEQUEUE(&ifp
->if_snd
, m0
);
563 if (flag
& IO_NDELAY
) {
567 tp
->tun_flags
|= TUN_RWAIT
;
568 if( error
= tsleep((caddr_t
)tp
, PCATCH
| (PZERO
+ 1),
577 while (m0
&& uio
->uio_resid
> 0 && error
== 0) {
578 len
= min(uio
->uio_resid
, m0
->m_len
);
581 error
= uiomove(mtod(m0
, caddr_t
), len
, uio
);
587 TUNDEBUG("Dropping mbuf\n");
594 * the cdevsw write interface - an atomic write is a packet - or else!
596 /* See top of tunoutput() about interface change between ppp process and
599 tunwrite(dev
, uio
, flag
)
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
;
610 struct ifqueue
*afintrq
= NULL
;
612 TUNDEBUG("%s%d: tunwrite\n", ifp
->if_name
, ifp
->if_unit
);
614 if (uio
->uio_resid
< 0 || uio
->uio_resid
> TUNMRU
) {
615 TUNDEBUG("%s%d: len=%d!\n", ifp
->if_name
, ifp
->if_unit
,
619 tlen
= uio
->uio_resid
;
621 /* get a header mbuf */
622 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
626 MCLGET(m
, M_DONTWAIT
);
627 if ((m
->m_flags
& M_EXT
) == 0) {
631 mlen
= m
->m_ext
.ext_size
;
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
);
642 if (uio
->uio_resid
> 0) {
643 MGET (m
, M_DONTWAIT
, MT_DATA
);
651 /* Change for checking Address Family of sending packet. */
652 af
= *mtod(top
, u_long
*);
656 netisr_af
= NETISR_IP
;
662 netisr_af
= NETISR_IPV6
;
667 if (af
> 255) { /* see description at the top of tunoutput */
669 netisr_af
= NETISR_IP
;
673 error
= EAFNOSUPPORT
;
676 m_adj(top
, sizeof(u_long
)); /* remove af field passed from upper */
677 tlen
-= sizeof(u_long
);
685 top
->m_pkthdr
.len
= tlen
;
686 top
->m_pkthdr
.rcvif
= ifp
;
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).
701 m
.m_data
= (char *)&af
;
707 /* just for safety */
712 if (IF_QFULL (afintrq
)) {
715 ifp
->if_collisions
++;
719 IF_ENQUEUE(afintrq
, top
);
721 ifp
->if_ibytes
+= tlen
;
723 schednetisr(netisr_af
);
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.
733 tunpoll(dev
, events
, p
)
738 int unit
= dev_val(minor(dev
)), s
;
739 struct tun_softc
*tp
= &tunctl
[unit
];
740 struct ifnet
*ifp
= &tp
->tun_if
;
744 TUNDEBUG("%s%d: tunpoll\n", ifp
->if_name
, ifp
->if_unit
);
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
);
752 TUNDEBUG("%s%d: tunpoll waiting\n", ifp
->if_name
,
754 selrecord(p
, &tp
->tun_rsel
);
757 if (events
& (POLLOUT
| POLLWRNORM
))
758 revents
|= events
& (POLLOUT
| POLLWRNORM
);