]>
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 * 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 selwakeup(&tp
->tun_rsel
);
253 selthreadclear(&tp
->tun_rsel
);
255 TUNDEBUG ("%s%d: closed\n", ifp
->if_name
, ifp
->if_unit
);
260 tuninit(unit
, cmd
, af
)
265 struct tun_softc
*tp
= &tunctl
[unit
];
266 struct ifnet
*ifp
= &tp
->tun_if
;
267 register struct ifaddr
*ifa
;
269 TUNDEBUG("%s%d: tuninit\n", ifp
->if_name
, ifp
->if_unit
);
271 ifp
->if_flags
|= IFF_UP
| IFF_RUNNING
;
272 getmicrotime(&ifp
->if_lastchange
);
274 for (ifa
= ifp
->if_addrhead
.tqh_first
; ifa
;
275 ifa
= ifa
->ifa_link
.tqe_next
) {
277 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
278 struct sockaddr_in
*si
;
280 si
= (struct sockaddr_in
*)ifa
->ifa_addr
;
281 if (si
&& si
->sin_addr
.s_addr
)
282 tp
->tun_flags
|= TUN_IASET
;
284 si
= (struct sockaddr_in
*)ifa
->ifa_dstaddr
;
285 if (si
&& si
->sin_addr
.s_addr
)
286 tp
->tun_flags
|= TUN_DSTADDR
;
294 * Process an ioctl request.
297 tunifioctl(ifp
, cmd
, data
)
302 register struct ifreq
*ifr
= (struct ifreq
*)data
;
308 tuninit(ifp
->if_unit
);
309 TUNDEBUG("%s%d: address set\n",
310 ifp
->if_name
, ifp
->if_unit
);
314 #if defined(INET6) && defined(__FreeBSD__) && __FreeBSD__ >= 3
315 if (found_first_ifid
== 0)
316 in6_ifattach_noifid(ifp
);
317 #endif /* defined(INET6) && defined(__FreeBSD__) && __FreeBSD__ >= 3 */
319 tuninit(ifp
->if_unit
, cmd
, ifr
->ifr_addr
.sa_family
);
322 ifp
->if_mtu
= ifr
->ifr_mtu
;
323 TUNDEBUG("%s%d: mtu set\n",
324 ifp
->if_name
, ifp
->if_unit
);
331 if ((ifp
->if_flags
& IFF_UP
) != 0)
332 ifp
->if_flags
|= IFF_RUNNING
;
333 else if ((ifp
->if_flags
& IFF_UP
) == 0)
334 ifp
->if_flags
&= ~IFF_RUNNING
;
345 * tunoutput - queue packets from higher level ready to put out.
347 /* Packet data format between tun and ppp is changed to enable checking of
348 * Address Family of sending packet. When INET6 is defined, 4byte AF field
349 * is appended to packet data as following.
351 * 0 1 2 3 4 5 6 7 8 .....
352 * ------------------------------
353 * | af | packet data .....
354 * ------------------------------
356 * Newly added part. The size is sizeof(u_long).
358 * However, this is not adopted for tun -> ppp AF_INET packet for
359 * backword compatibility, because the ppp process may be an existing
360 * ip only supporting one.
361 * Also in ppp->tun case, when af value is unknown, (af > 255) is checked and
362 * if it is true, AF_INET is assumed. (the 4byte may be the head of
363 * AF_INET packet. Despite the byte order, the value must always be
364 * greater than 255, because of ip_len field or (ip_v and ip_hl)
365 * field. (Idea from Mr. Noritoshi Demize)
368 tunoutput(ifp
, m0
, dst
, rt
)
371 struct sockaddr
*dst
;
374 struct tun_softc
*tp
= &tunctl
[ifp
->if_unit
];
377 TUNDEBUG ("%s%d: tunoutput\n", ifp
->if_name
, ifp
->if_unit
);
379 if ((tp
->tun_flags
& TUN_READY
) != TUN_READY
) {
380 TUNDEBUG ("%s%d: not ready 0%o\n", ifp
->if_name
,
381 ifp
->if_unit
, tp
->tun_flags
);
387 /* BPF write needs to be handled specially */
388 if (dst
->sa_family
== AF_UNSPEC
) {
389 dst
->sa_family
= *(mtod(m0
, int *));
390 m0
->m_len
-= sizeof(int);
391 m0
->m_pkthdr
.len
-= sizeof(int);
392 m0
->m_data
+= sizeof(int);
397 * We need to prepend the address family as
398 * a four byte field. Cons up a dummy header
399 * to pacify bpf. This is safe because bpf
400 * will only read from the mbuf (i.e., it won't
401 * try to free it or keep a pointer to it).
404 u_int af
= dst
->sa_family
;
408 m
.m_data
= (char *)&af
;
414 switch(dst
->sa_family
) {
415 #if defined(INET) || defined(INET6)
418 M_PREPEND(m0
, sizeof(u_long
) /* af field passed to upper */,
422 *mtod(m0
, u_long
*) = (u_long
)dst
->sa_family
;
428 #endif /* INET || INET6 */
430 if (IF_QFULL(&ifp
->if_snd
)) {
431 IF_DROP(&ifp
->if_snd
);
434 ifp
->if_collisions
++;
437 ifp
->if_obytes
+= m0
->m_pkthdr
.len
;
438 IF_ENQUEUE(&ifp
->if_snd
, m0
);
447 if (tp
->tun_flags
& TUN_RWAIT
) {
448 tp
->tun_flags
&= ~TUN_RWAIT
;
451 if (tp
->tun_flags
& TUN_ASYNC
&& tp
->tun_sigio
)
452 pgsigio(tp
->tun_sigio
, SIGIO
, 0);
453 selwakeup(&tp
->tun_rsel
);
458 * the cdevsw interface is now pretty minimal.
461 tunioctl(dev
, cmd
, data
, flag
, p
)
468 int unit
= dev_val(minor(dev
)), s
;
469 struct tun_softc
*tp
= &tunctl
[unit
];
470 struct tuninfo
*tunp
;
474 tunp
= (struct tuninfo
*)data
;
475 tp
->tun_if
.if_mtu
= tunp
->mtu
;
476 tp
->tun_if
.if_type
= tunp
->type
;
477 tp
->tun_if
.if_baudrate
= tunp
->baudrate
;
480 tunp
= (struct tuninfo
*)data
;
481 tunp
->mtu
= tp
->tun_if
.if_mtu
;
482 tunp
->type
= tp
->tun_if
.if_type
;
483 tunp
->baudrate
= tp
->tun_if
.if_baudrate
;
486 tundebug
= *(int *)data
;
489 *(int *)data
= tundebug
;
495 tp
->tun_flags
|= TUN_ASYNC
;
497 tp
->tun_flags
&= ~TUN_ASYNC
;
501 if (tp
->tun_if
.if_snd
.ifq_head
) {
502 struct mbuf
*mb
= tp
->tun_if
.if_snd
.ifq_head
;
503 for( *(int *)data
= 0; mb
!= 0; mb
= mb
->m_next
)
504 *(int *)data
+= mb
->m_len
;
510 return (fsetown(*(int *)data
, &tp
->tun_sigio
));
513 *(int *)data
= fgetown(tp
->tun_sigio
);
516 /* This is deprecated, FIOSETOWN should be used instead. */
518 return (fsetown(-(*(int *)data
), &tp
->tun_sigio
));
520 /* This is deprecated, FIOGETOWN should be used instead. */
522 *(int *)data
= -fgetown(tp
->tun_sigio
);
532 * The cdevsw read interface - reads a packet at a time, or at
533 * least as much of a packet as can be read.
536 tunread(dev
, uio
, flag
)
541 int unit
= dev_val(minor(dev
));
542 struct tun_softc
*tp
= &tunctl
[unit
];
543 struct ifnet
*ifp
= &tp
->tun_if
;
547 TUNDEBUG ("%s%d: read\n", ifp
->if_name
, ifp
->if_unit
);
548 if ((tp
->tun_flags
& TUN_READY
) != TUN_READY
) {
549 TUNDEBUG ("%s%d: not ready 0%o\n", ifp
->if_name
,
550 ifp
->if_unit
, tp
->tun_flags
);
554 tp
->tun_flags
&= ~TUN_RWAIT
;
558 IF_DEQUEUE(&ifp
->if_snd
, m0
);
560 if (flag
& IO_NDELAY
) {
564 tp
->tun_flags
|= TUN_RWAIT
;
565 if( error
= tsleep((caddr_t
)tp
, PCATCH
| (PZERO
+ 1),
574 while (m0
&& uio
->uio_resid
> 0 && error
== 0) {
575 len
= min(uio
->uio_resid
, m0
->m_len
);
578 error
= uiomove(mtod(m0
, caddr_t
), len
, uio
);
584 TUNDEBUG("Dropping mbuf\n");
591 * the cdevsw write interface - an atomic write is a packet - or else!
593 /* See top of tunoutput() about interface change between ppp process and
596 tunwrite(dev
, uio
, flag
)
601 int unit
= dev_val(minor(dev
));
602 struct ifnet
*ifp
= &tunctl
[unit
].tun_if
;
603 struct mbuf
*top
, **mp
, *m
;
604 int error
=0, s
, tlen
, mlen
;
607 struct ifqueue
*afintrq
= NULL
;
609 TUNDEBUG("%s%d: tunwrite\n", ifp
->if_name
, ifp
->if_unit
);
611 if (uio
->uio_resid
< 0 || uio
->uio_resid
> TUNMRU
) {
612 TUNDEBUG("%s%d: len=%d!\n", ifp
->if_name
, ifp
->if_unit
,
616 tlen
= uio
->uio_resid
;
618 /* get a header mbuf */
619 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
623 MCLGET(m
, M_DONTWAIT
);
624 if ((m
->m_flags
& M_EXT
) == 0) {
628 mlen
= m
->m_ext
.ext_size
;
634 while (error
== 0 && uio
->uio_resid
> 0) {
635 m
->m_len
= min(mlen
, uio
->uio_resid
);
636 error
= uiomove(mtod (m
, caddr_t
), m
->m_len
, uio
);
639 if (uio
->uio_resid
> 0) {
640 MGET (m
, M_DONTWAIT
, MT_DATA
);
648 /* Change for checking Address Family of sending packet. */
649 af
= *mtod(top
, u_long
*);
653 netisr_af
= NETISR_IP
;
659 netisr_af
= NETISR_IPV6
;
664 if (af
> 255) { /* see description at the top of tunoutput */
666 netisr_af
= NETISR_IP
;
670 error
= EAFNOSUPPORT
;
673 m_adj(top
, sizeof(u_long
)); /* remove af field passed from upper */
674 tlen
-= sizeof(u_long
);
682 top
->m_pkthdr
.len
= tlen
;
683 top
->m_pkthdr
.rcvif
= ifp
;
688 * We need to prepend the address family as
689 * a four byte field. Cons up a dummy header
690 * to pacify bpf. This is safe because bpf
691 * will only read from the mbuf (i.e., it won't
692 * try to free it or keep a pointer to it).
698 m
.m_data
= (char *)&af
;
704 /* just for safety */
709 if (IF_QFULL (afintrq
)) {
712 ifp
->if_collisions
++;
716 IF_ENQUEUE(afintrq
, top
);
718 ifp
->if_ibytes
+= tlen
;
720 schednetisr(netisr_af
);
725 * tunpoll - the poll interface, this is only useful on reads
726 * really. The write detect always returns true, write never blocks
727 * anyway, it either accepts the packet or drops it.
730 tunpoll(dev
, events
, wql
, p
)
736 int unit
= dev_val(minor(dev
)), s
;
737 struct tun_softc
*tp
= &tunctl
[unit
];
738 struct ifnet
*ifp
= &tp
->tun_if
;
742 TUNDEBUG("%s%d: tunpoll\n", ifp
->if_name
, ifp
->if_unit
);
744 if (events
& (POLLIN
| POLLRDNORM
))
745 if (ifp
->if_snd
.ifq_len
> 0) {
746 TUNDEBUG("%s%d: tunpoll q=%d\n", ifp
->if_name
,
747 ifp
->if_unit
, ifp
->if_snd
.ifq_len
);
748 revents
|= events
& (POLLIN
| POLLRDNORM
);
750 TUNDEBUG("%s%d: tunpoll waiting\n", ifp
->if_name
,
752 selrecord(p
, &tp
->tun_rsel
, wql
);
755 if (events
& (POLLOUT
| POLLWRNORM
))
756 revents
|= events
& (POLLOUT
| POLLWRNORM
);