@ -6,6 +6,7 @@
package tstun
package tstun
import (
import (
"bytes"
"fmt"
"fmt"
"net"
"net"
"net/netip"
"net/netip"
@ -20,10 +21,13 @@ import (
"gvisor.dev/gvisor/pkg/tcpip/checksum"
"gvisor.dev/gvisor/pkg/tcpip/checksum"
"gvisor.dev/gvisor/pkg/tcpip/header"
"gvisor.dev/gvisor/pkg/tcpip/header"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv4"
"gvisor.dev/gvisor/pkg/tcpip/network/ipv6"
"gvisor.dev/gvisor/pkg/tcpip/transport/udp"
"gvisor.dev/gvisor/pkg/tcpip/transport/udp"
"tailscale.com/net/netaddr"
"tailscale.com/net/netaddr"
"tailscale.com/net/packet"
"tailscale.com/net/packet"
"tailscale.com/syncs"
"tailscale.com/types/ipproto"
"tailscale.com/types/ipproto"
"tailscale.com/types/logger"
"tailscale.com/util/multierr"
"tailscale.com/util/multierr"
)
)
@ -35,13 +39,13 @@ var ourMAC = net.HardwareAddr{0x30, 0x2D, 0x66, 0xEC, 0x7A, 0x93}
func init ( ) { createTAP = createTAPLinux }
func init ( ) { createTAP = createTAPLinux }
func createTAPLinux ( tapName, bridgeName string ) ( tun . Device , error ) {
func createTAPLinux ( logf logger . Logf , tapName, bridgeName string ) ( tun . Device , error ) {
fd , err := unix . Open ( "/dev/net/tun" , unix . O_RDWR , 0 )
fd , err := unix . Open ( "/dev/net/tun" , unix . O_RDWR , 0 )
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
dev , err := openDevice ( fd, tapName , bridgeName )
dev , err := openDevice ( logf, fd, tapName , bridgeName )
if err != nil {
if err != nil {
unix . Close ( fd )
unix . Close ( fd )
return nil , err
return nil , err
@ -50,7 +54,7 @@ func createTAPLinux(tapName, bridgeName string) (tun.Device, error) {
return dev , nil
return dev , nil
}
}
func openDevice ( fd int , tapName , bridgeName string ) ( tun . Device , error ) {
func openDevice ( logf logger . Logf , fd int , tapName , bridgeName string ) ( tun . Device , error ) {
ifr , err := unix . NewIfreq ( tapName )
ifr , err := unix . NewIfreq ( tapName )
if err != nil {
if err != nil {
return nil , err
return nil , err
@ -71,7 +75,7 @@ func openDevice(fd int, tapName, bridgeName string) (tun.Device, error) {
}
}
}
}
return newTAPDevice ( fd, tapName )
return newTAPDevice ( logf, fd, tapName )
}
}
type etherType [ 2 ] byte
type etherType [ 2 ] byte
@ -91,7 +95,7 @@ const (
// handleTAPFrame handles receiving a raw TAP ethernet frame and reports whether
// handleTAPFrame handles receiving a raw TAP ethernet frame and reports whether
// it's been handled (that is, whether it should NOT be passed to wireguard).
// it's been handled (that is, whether it should NOT be passed to wireguard).
func ( t * Wrapper ) handleTAPFrame ( ethBuf [ ] byte ) bool {
func ( t * tapDevice ) handleTAPFrame ( ethBuf [ ] byte ) bool {
if len ( ethBuf ) < ethernetFrameSize {
if len ( ethBuf ) < ethernetFrameSize {
// Corrupt. Ignore.
// Corrupt. Ignore.
@ -164,8 +168,7 @@ func (t *Wrapper) handleTAPFrame(ethBuf []byte) bool {
copy ( res . HardwareAddressTarget ( ) , req . HardwareAddressSender ( ) )
copy ( res . HardwareAddressTarget ( ) , req . HardwareAddressSender ( ) )
copy ( res . ProtocolAddressTarget ( ) , req . ProtocolAddressSender ( ) )
copy ( res . ProtocolAddressTarget ( ) , req . ProtocolAddressSender ( ) )
// TODO(raggi): reduce allocs!
n , err := t . WriteEthernet ( buf )
n , err := t . tdev . Write ( [ ] [ ] byte { buf } , 0 )
if tapDebug {
if tapDebug {
t . logf ( "tap: wrote ARP reply %v, %v" , n , err )
t . logf ( "tap: wrote ARP reply %v, %v" , n , err )
}
}
@ -182,7 +185,7 @@ const routerIP = "100.70.145.1" // must be in same netmask (currently hack at
// handleDHCPRequest handles receiving a raw TAP ethernet frame and reports whether
// handleDHCPRequest handles receiving a raw TAP ethernet frame and reports whether
// it's been handled as a DHCP request. That is, it reports whether the frame should
// it's been handled as a DHCP request. That is, it reports whether the frame should
// be ignored by the caller and not passed on.
// be ignored by the caller and not passed on.
func ( t * Wrapper ) handleDHCPRequest ( ethBuf [ ] byte ) bool {
func ( t * tapDevice ) handleDHCPRequest ( ethBuf [ ] byte ) bool {
const udpHeader = 8
const udpHeader = 8
if len ( ethBuf ) < ethernetFrameSize + ipv4HeaderLen + udpHeader {
if len ( ethBuf ) < ethernetFrameSize + ipv4HeaderLen + udpHeader {
if tapDebug {
if tapDebug {
@ -207,7 +210,7 @@ func (t *Wrapper) handleDHCPRequest(ethBuf []byte) bool {
if p . IPProto != ipproto . UDP || p . Src . Port ( ) != 68 || p . Dst . Port ( ) != 67 {
if p . IPProto != ipproto . UDP || p . Src . Port ( ) != 68 || p . Dst . Port ( ) != 67 {
// Not a DHCP request.
// Not a DHCP request.
if tapDebug {
if tapDebug {
t . logf ( "tap: DHCP wrong meta ")
t . logf ( "tap: DHCP wrong meta : %+v ", p )
}
}
return passOnPacket
return passOnPacket
}
}
@ -250,8 +253,7 @@ func (t *Wrapper) handleDHCPRequest(ethBuf []byte) bool {
netip . AddrPortFrom ( netaddr . IPv4 ( 255 , 255 , 255 , 255 ) , 68 ) , // dst
netip . AddrPortFrom ( netaddr . IPv4 ( 255 , 255 , 255 , 255 ) , 68 ) , // dst
)
)
// TODO(raggi): reduce allocs!
n , err := t . WriteEthernet ( pkt )
n , err := t . tdev . Write ( [ ] [ ] byte { pkt } , 0 )
if tapDebug {
if tapDebug {
t . logf ( "tap: wrote DHCP OFFER %v, %v" , n , err )
t . logf ( "tap: wrote DHCP OFFER %v, %v" , n , err )
}
}
@ -278,8 +280,7 @@ func (t *Wrapper) handleDHCPRequest(ethBuf []byte) bool {
netip . AddrPortFrom ( netaddr . IPv4 ( 100 , 100 , 100 , 100 ) , 67 ) , // src
netip . AddrPortFrom ( netaddr . IPv4 ( 100 , 100 , 100 , 100 ) , 67 ) , // src
netip . AddrPortFrom ( netaddr . IPv4 ( 255 , 255 , 255 , 255 ) , 68 ) , // dst
netip . AddrPortFrom ( netaddr . IPv4 ( 255 , 255 , 255 , 255 ) , 68 ) , // dst
)
)
// TODO(raggi): reduce allocs!
n , err := t . WriteEthernet ( pkt )
n , err := t . tdev . Write ( [ ] [ ] byte { pkt } , 0 )
if tapDebug {
if tapDebug {
t . logf ( "tap: wrote DHCP ACK %v, %v" , n , err )
t . logf ( "tap: wrote DHCP ACK %v, %v" , n , err )
}
}
@ -291,6 +292,16 @@ func (t *Wrapper) handleDHCPRequest(ethBuf []byte) bool {
return consumePacket
return consumePacket
}
}
func writeEthernetFrame ( buf [ ] byte , srcMAC , dstMAC net . HardwareAddr , proto tcpip . NetworkProtocolNumber ) {
// Ethernet header
eth := header . Ethernet ( buf )
eth . Encode ( & header . EthernetFields {
SrcAddr : tcpip . LinkAddress ( srcMAC ) ,
DstAddr : tcpip . LinkAddress ( dstMAC ) ,
Type : proto ,
} )
}
func packLayer2UDP ( payload [ ] byte , srcMAC , dstMAC net . HardwareAddr , src , dst netip . AddrPort ) [ ] byte {
func packLayer2UDP ( payload [ ] byte , srcMAC , dstMAC net . HardwareAddr , src , dst netip . AddrPort ) [ ] byte {
buf := make ( [ ] byte , header . EthernetMinimumSize + header . UDPMinimumSize + header . IPv4MinimumSize + len ( payload ) )
buf := make ( [ ] byte , header . EthernetMinimumSize + header . UDPMinimumSize + header . IPv4MinimumSize + len ( payload ) )
payloadStart := len ( buf ) - len ( payload )
payloadStart := len ( buf ) - len ( payload )
@ -300,12 +311,7 @@ func packLayer2UDP(payload []byte, srcMAC, dstMAC net.HardwareAddr, src, dst net
dstB := dst . Addr ( ) . As4 ( )
dstB := dst . Addr ( ) . As4 ( )
dstIP := tcpip . AddrFromSlice ( dstB [ : ] )
dstIP := tcpip . AddrFromSlice ( dstB [ : ] )
// Ethernet header
// Ethernet header
eth := header . Ethernet ( buf )
writeEthernetFrame ( buf , srcMAC , dstMAC , ipv4 . ProtocolNumber )
eth . Encode ( & header . EthernetFields {
SrcAddr : tcpip . LinkAddress ( srcMAC ) ,
DstAddr : tcpip . LinkAddress ( dstMAC ) ,
Type : ipv4 . ProtocolNumber ,
} )
// IP header
// IP header
ipbuf := buf [ header . EthernetMinimumSize : ]
ipbuf := buf [ header . EthernetMinimumSize : ]
ip := header . IPv4 ( ipbuf )
ip := header . IPv4 ( ipbuf )
@ -342,17 +348,18 @@ func run(prog string, args ...string) error {
return nil
return nil
}
}
func ( t * Wrapper ) destMAC ( ) [ 6 ] byte {
func ( t * tapDevice ) destMAC ( ) [ 6 ] byte {
return t . destMACAtomic . Load ( )
return t . destMACAtomic . Load ( )
}
}
func newTAPDevice ( fd int , tapName string ) ( tun . Device , error ) {
func newTAPDevice ( logf logger . Logf , fd int , tapName string ) ( tun . Device , error ) {
err := unix . SetNonblock ( fd , true )
err := unix . SetNonblock ( fd , true )
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
file := os . NewFile ( uintptr ( fd ) , "/dev/tap" )
file := os . NewFile ( uintptr ( fd ) , "/dev/tap" )
d := & tapDevice {
d := & tapDevice {
logf : logf ,
file : file ,
file : file ,
events : make ( chan tun . Event ) ,
events : make ( chan tun . Event ) ,
name : tapName ,
name : tapName ,
@ -360,20 +367,14 @@ func newTAPDevice(fd int, tapName string) (tun.Device, error) {
return d , nil
return d , nil
}
}
var (
_ setWrapperer = & tapDevice { }
)
type tapDevice struct {
type tapDevice struct {
file * os . File
file * os . File
logf func ( format string , args ... any )
events chan tun . Event
events chan tun . Event
name string
name string
wrapper * Wrapper
closeOnce sync . Once
closeOnce sync . Once
}
func ( t * tapDevice ) setWrapper ( wrapper * Wrapper ) {
destMACAtomic syncs . AtomicValue [ [ 6 ] byte ]
t . wrapper = wrapper
}
}
func ( t * tapDevice ) File ( ) * os . File {
func ( t * tapDevice ) File ( ) * os . File {
@ -384,36 +385,63 @@ func (t *tapDevice) Name() (string, error) {
return t . name , nil
return t . name , nil
}
}
// Read reads an IP packet from the TAP device. It strips the ethernet frame header.
func ( t * tapDevice ) Read ( buffs [ ] [ ] byte , sizes [ ] int , offset int ) ( int , error ) {
func ( t * tapDevice ) Read ( buffs [ ] [ ] byte , sizes [ ] int , offset int ) ( int , error ) {
n , err := t . ReadEthernet ( buffs , sizes , offset )
if err != nil || n == 0 {
return n , err
}
// Strip the ethernet frame header.
copy ( buffs [ 0 ] [ offset : ] , buffs [ 0 ] [ offset + ethernetFrameSize : offset + sizes [ 0 ] ] )
sizes [ 0 ] -= ethernetFrameSize
return 1 , nil
}
// ReadEthernet reads a raw ethernet frame from the TAP device.
func ( t * tapDevice ) ReadEthernet ( buffs [ ] [ ] byte , sizes [ ] int , offset int ) ( int , error ) {
n , err := t . file . Read ( buffs [ 0 ] [ offset : ] )
n , err := t . file . Read ( buffs [ 0 ] [ offset : ] )
if err != nil {
if err != nil {
return 0 , err
return 0 , err
}
}
if t . handleTAPFrame ( buffs [ 0 ] [ offset : offset + n ] ) {
return 0 , nil
}
sizes [ 0 ] = n
sizes [ 0 ] = n
return 1 , nil
return 1 , nil
}
}
// WriteEthernet writes a raw ethernet frame to the TAP device.
func ( t * tapDevice ) WriteEthernet ( buf [ ] byte ) ( int , error ) {
return t . file . Write ( buf )
}
// ethBufPool holds a pool of bytes.Buffers for use in [tapDevice.Write].
var ethBufPool = syncs . Pool [ * bytes . Buffer ] { New : func ( ) * bytes . Buffer { return new ( bytes . Buffer ) } }
// Write writes a raw IP packet to the TAP device. It adds the ethernet frame header.
func ( t * tapDevice ) Write ( buffs [ ] [ ] byte , offset int ) ( int , error ) {
func ( t * tapDevice ) Write ( buffs [ ] [ ] byte , offset int ) ( int , error ) {
errs := make ( [ ] error , 0 )
errs := make ( [ ] error , 0 )
wrote := 0
wrote := 0
m := t . destMAC ( )
dstMac := net . HardwareAddr ( m [ : ] )
buf := ethBufPool . Get ( )
defer ethBufPool . Put ( buf )
for _ , buff := range buffs {
for _ , buff := range buffs {
if offset < ethernetFrameSize {
buf . Reset ( )
errs = append ( errs , fmt . Errorf ( "[unexpected] weird offset %d for TAP write" , offset ) )
buf . Grow ( header . EthernetMinimumSize + len ( buff ) - offset )
return 0 , multierr . New ( errs ... )
}
var ebuf [ 14 ] byte
eth := buff [ offset - ethernetFrameSize : ]
switch buff [ offset ] >> 4 {
dst := t . wrapper . destMAC ( )
case 4 :
copy ( eth [ : 6 ] , dst [ : ] )
writeEthernetFrame ( ebuf [ : ] , ourMAC , dstMac , ipv4 . ProtocolNumber )
copy ( eth [ 6 : 12 ] , ourMAC [ : ] )
case 6 :
et := etherTypeIPv4
writeEthernetFrame ( ebuf [ : ] , ourMAC , dstMac , ipv6 . ProtocolNumber )
if buff [ offset ] >> 4 == 6 {
default:
et = etherTypeIPv6
continue
}
}
eth [ 12 ] , eth [ 13 ] = et [ 0 ] , et [ 1 ]
buf . Write ( ebuf [ : ] )
if tapDebug {
buf . Write ( buff [ offset : ] )
t . wrapper . logf ( "tap: tapWrite off=%v % x" , offset , buff )
_ , err := t . WriteEthernet ( buf . Bytes ( ) )
}
_ , err := t . file . Write ( buff [ offset - ethernetFrameSize : ] )
if err != nil {
if err != nil {
errs = append ( errs , err )
errs = append ( errs , err )
} else {
} else {
@ -428,8 +456,7 @@ func (t *tapDevice) MTU() (int, error) {
if err != nil {
if err != nil {
return 0 , err
return 0 , err
}
}
err = unix . IoctlIfreq ( int ( t . file . Fd ( ) ) , unix . SIOCGIFMTU , ifr )
if err := unix . IoctlIfreq ( int ( t . file . Fd ( ) ) , unix . SIOCGIFMTU , ifr ) ; err != nil {
if err != nil {
return 0 , err
return 0 , err
}
}
return int ( ifr . Uint32 ( ) ) , nil
return int ( ifr . Uint32 ( ) ) , nil