2019-03-28 03:30:25 +03:00
|
|
|
package tuntap
|
2017-12-29 07:16:20 +03:00
|
|
|
|
|
|
|
// This manages the tun driver to send/recv packets to/from applications
|
|
|
|
|
2018-06-13 01:50:08 +03:00
|
|
|
import (
|
2018-11-10 21:33:52 +03:00
|
|
|
"bytes"
|
2018-11-10 18:46:10 +03:00
|
|
|
"errors"
|
2019-01-14 17:25:52 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2018-12-17 02:01:59 +03:00
|
|
|
"sync"
|
2018-11-10 20:32:03 +03:00
|
|
|
"time"
|
2018-07-07 14:08:52 +03:00
|
|
|
|
2019-03-28 03:30:25 +03:00
|
|
|
"github.com/gologme/log"
|
2019-03-28 12:50:13 +03:00
|
|
|
"golang.org/x/net/icmp"
|
|
|
|
"golang.org/x/net/ipv6"
|
2019-03-28 03:30:25 +03:00
|
|
|
|
2018-06-13 01:50:08 +03:00
|
|
|
"github.com/songgao/packets/ethernet"
|
|
|
|
"github.com/yggdrasil-network/water"
|
2018-12-08 04:56:04 +03:00
|
|
|
|
2018-12-15 05:49:18 +03:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
2019-03-28 03:30:25 +03:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
2018-12-08 04:56:04 +03:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
|
2018-12-15 05:49:18 +03:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/util"
|
2019-03-28 03:30:25 +03:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil"
|
2018-06-13 01:50:08 +03:00
|
|
|
)
|
2017-12-29 07:16:20 +03:00
|
|
|
|
2018-05-28 01:31:34 +03:00
|
|
|
const tun_IPv6_HEADER_LENGTH = 40
|
|
|
|
const tun_ETHER_HEADER_LENGTH = 14
|
2017-12-29 07:16:20 +03:00
|
|
|
|
2019-03-29 21:18:31 +03:00
|
|
|
// TunAdapter represents a running TUN/TAP interface and extends the
|
|
|
|
// yggdrasil.Adapter type. In order to use the TUN/TAP adapter with Yggdrasil,
|
|
|
|
// you should pass this object to the yggdrasil.SetRouterAdapter() function
|
|
|
|
// before calling yggdrasil.Start().
|
2019-03-28 03:30:25 +03:00
|
|
|
type TunAdapter struct {
|
|
|
|
yggdrasil.Adapter
|
|
|
|
addr address.Address
|
|
|
|
subnet address.Subnet
|
|
|
|
icmpv6 ICMPv6
|
2018-02-12 21:19:31 +03:00
|
|
|
mtu int
|
2018-03-04 22:57:34 +03:00
|
|
|
iface *water.Interface
|
2018-12-17 02:01:59 +03:00
|
|
|
mutex sync.RWMutex // Protects the below
|
|
|
|
isOpen bool
|
2017-12-29 07:16:20 +03:00
|
|
|
}
|
|
|
|
|
2018-06-13 00:45:53 +03:00
|
|
|
// Gets the maximum supported MTU for the platform based on the defaults in
|
2018-07-07 14:08:52 +03:00
|
|
|
// defaults.GetDefaults().
|
2018-03-03 15:30:54 +03:00
|
|
|
func getSupportedMTU(mtu int) int {
|
2018-07-07 14:08:52 +03:00
|
|
|
if mtu > defaults.GetDefaults().MaximumIfMTU {
|
|
|
|
return defaults.GetDefaults().MaximumIfMTU
|
2018-03-03 14:47:14 +03:00
|
|
|
}
|
|
|
|
return mtu
|
|
|
|
}
|
|
|
|
|
2019-03-29 21:18:31 +03:00
|
|
|
// Name returns the name of the adapter, e.g. "tun0". On Windows, this may
|
|
|
|
// return a canonical adapter name instead.
|
2019-03-28 03:30:25 +03:00
|
|
|
func (tun *TunAdapter) Name() string {
|
|
|
|
return tun.iface.Name()
|
|
|
|
}
|
|
|
|
|
2019-03-29 21:18:31 +03:00
|
|
|
// MTU gets the adapter's MTU. This can range between 1280 and 65535, although
|
|
|
|
// the maximum value is determined by your platform. The returned value will
|
|
|
|
// never exceed that of MaximumMTU().
|
2019-03-28 03:30:25 +03:00
|
|
|
func (tun *TunAdapter) MTU() int {
|
|
|
|
return getSupportedMTU(tun.mtu)
|
|
|
|
}
|
|
|
|
|
2019-03-29 21:18:31 +03:00
|
|
|
// IsTAP returns true if the adapter is a TAP adapter (Layer 2) or false if it
|
|
|
|
// is a TUN adapter (Layer 3).
|
2019-03-28 03:30:25 +03:00
|
|
|
func (tun *TunAdapter) IsTAP() bool {
|
|
|
|
return tun.iface.IsTAP()
|
|
|
|
}
|
|
|
|
|
2019-03-29 21:18:31 +03:00
|
|
|
// DefaultName gets the default TUN/TAP interface name for your platform.
|
2019-03-29 21:05:17 +03:00
|
|
|
func DefaultName() string {
|
|
|
|
return defaults.GetDefaults().DefaultIfName
|
|
|
|
}
|
|
|
|
|
2019-03-29 21:18:31 +03:00
|
|
|
// DefaultMTU gets the default TUN/TAP interface MTU for your platform. This can
|
|
|
|
// be as high as MaximumMTU(), depending on platform, but is never lower than 1280.
|
2019-03-29 21:05:17 +03:00
|
|
|
func DefaultMTU() int {
|
|
|
|
return defaults.GetDefaults().DefaultIfMTU
|
|
|
|
}
|
|
|
|
|
2019-03-29 21:18:31 +03:00
|
|
|
// DefaultIsTAP returns true if the default adapter mode for the current
|
|
|
|
// platform is TAP (Layer 2) and returns false for TUN (Layer 3).
|
2019-03-29 21:05:17 +03:00
|
|
|
func DefaultIsTAP() bool {
|
|
|
|
return defaults.GetDefaults().DefaultIfTAPMode
|
|
|
|
}
|
|
|
|
|
2019-03-29 21:18:31 +03:00
|
|
|
// MaximumMTU returns the maximum supported TUN/TAP interface MTU for your
|
|
|
|
// platform. This can be as high as 65535, depending on platform, but is never
|
|
|
|
// lower than 1280.
|
2019-03-29 21:05:17 +03:00
|
|
|
func MaximumMTU() int {
|
|
|
|
return defaults.GetDefaults().MaximumIfMTU
|
|
|
|
}
|
|
|
|
|
2019-03-29 21:18:31 +03:00
|
|
|
// Init initialises the TUN/TAP adapter.
|
2019-03-28 12:50:13 +03:00
|
|
|
func (tun *TunAdapter) Init(config *config.NodeState, log *log.Logger, send chan<- []byte, recv <-chan []byte, reject <-chan yggdrasil.RejectedPacket) {
|
|
|
|
tun.Adapter.Init(config, log, send, recv, reject)
|
2019-03-28 03:30:25 +03:00
|
|
|
tun.icmpv6.Init(tun)
|
2019-01-14 17:25:52 +03:00
|
|
|
go func() {
|
|
|
|
for {
|
2019-03-28 03:30:25 +03:00
|
|
|
e := <-tun.Reconfigure
|
2019-04-01 22:10:14 +03:00
|
|
|
tun.Config.Mutex.RLock()
|
|
|
|
updated := tun.Config.Current.IfName != tun.Config.Previous.IfName ||
|
|
|
|
tun.Config.Current.IfTAPMode != tun.Config.Previous.IfTAPMode ||
|
|
|
|
tun.Config.Current.IfMTU != tun.Config.Previous.IfMTU
|
|
|
|
tun.Config.Mutex.RUnlock()
|
2019-01-15 11:51:19 +03:00
|
|
|
if updated {
|
2019-04-01 22:10:14 +03:00
|
|
|
tun.Log.Warnln("Reconfiguring TUN/TAP is not supported yet")
|
2019-01-15 11:51:19 +03:00
|
|
|
e <- nil
|
|
|
|
} else {
|
|
|
|
e <- nil
|
2019-01-14 17:25:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2017-12-29 07:16:20 +03:00
|
|
|
}
|
|
|
|
|
2019-03-29 21:18:31 +03:00
|
|
|
// Start the setup process for the TUN/TAP adapter. If successful, starts the
|
|
|
|
// read/write goroutines to handle packets on that interface.
|
2019-03-28 03:30:25 +03:00
|
|
|
func (tun *TunAdapter) Start(a address.Address, s address.Subnet) error {
|
|
|
|
tun.addr = a
|
|
|
|
tun.subnet = s
|
2019-04-01 22:10:14 +03:00
|
|
|
if tun.Config == nil {
|
2019-03-28 03:30:25 +03:00
|
|
|
return errors.New("No configuration available to TUN/TAP")
|
|
|
|
}
|
2019-04-01 22:10:14 +03:00
|
|
|
tun.Config.Mutex.RLock()
|
|
|
|
ifname := tun.Config.Current.IfName
|
|
|
|
iftapmode := tun.Config.Current.IfTAPMode
|
2019-03-28 03:30:25 +03:00
|
|
|
addr := fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(address.GetPrefix())-1)
|
2019-04-01 22:10:14 +03:00
|
|
|
mtu := tun.Config.Current.IfMTU
|
|
|
|
tun.Config.Mutex.RUnlock()
|
2019-01-02 21:05:54 +03:00
|
|
|
if ifname != "none" {
|
|
|
|
if err := tun.setup(ifname, iftapmode, addr, mtu); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-03 01:29:06 +03:00
|
|
|
}
|
2019-01-02 21:05:54 +03:00
|
|
|
if ifname == "none" || ifname == "dummy" {
|
2019-04-01 22:10:14 +03:00
|
|
|
tun.Log.Debugln("Not starting TUN/TAP as ifname is none or dummy")
|
2019-01-02 21:05:54 +03:00
|
|
|
return nil
|
2018-05-28 00:35:30 +03:00
|
|
|
}
|
2018-12-17 02:01:59 +03:00
|
|
|
tun.mutex.Lock()
|
|
|
|
tun.isOpen = true
|
|
|
|
tun.mutex.Unlock()
|
2019-03-28 12:12:00 +03:00
|
|
|
go func() {
|
2019-04-01 22:10:14 +03:00
|
|
|
tun.Log.Debugln("Starting TUN/TAP reader goroutine")
|
|
|
|
tun.Log.Errorln("WARNING: tun.read() exited with error:", tun.read())
|
2019-03-28 12:12:00 +03:00
|
|
|
}()
|
|
|
|
go func() {
|
2019-04-01 22:10:14 +03:00
|
|
|
tun.Log.Debugln("Starting TUN/TAP writer goroutine")
|
|
|
|
tun.Log.Errorln("WARNING: tun.write() exited with error:", tun.write())
|
2019-03-28 12:12:00 +03:00
|
|
|
}()
|
2018-11-11 07:39:15 +03:00
|
|
|
if iftapmode {
|
|
|
|
go func() {
|
|
|
|
for {
|
2019-03-28 03:30:25 +03:00
|
|
|
if _, ok := tun.icmpv6.peermacs[tun.addr]; ok {
|
2018-11-11 07:39:15 +03:00
|
|
|
break
|
|
|
|
}
|
2019-03-28 03:30:25 +03:00
|
|
|
request, err := tun.icmpv6.CreateNDPL2(tun.addr)
|
2018-11-11 07:39:15 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if _, err := tun.iface.Write(request); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
2018-11-10 20:32:03 +03:00
|
|
|
}
|
2018-11-11 07:39:15 +03:00
|
|
|
}()
|
|
|
|
}
|
2018-05-28 00:35:30 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-13 00:45:53 +03:00
|
|
|
// Writes a packet to the TUN/TAP adapter. If the adapter is running in TAP
|
|
|
|
// mode then additional ethernet encapsulation is added for the benefit of the
|
|
|
|
// host operating system.
|
2019-03-30 03:09:35 +03:00
|
|
|
func (tun *TunAdapter) write() error {
|
2018-01-05 01:37:51 +03:00
|
|
|
for {
|
2019-03-28 12:50:13 +03:00
|
|
|
select {
|
|
|
|
case reject := <-tun.Reject:
|
|
|
|
switch reject.Reason {
|
|
|
|
case yggdrasil.PacketTooBig:
|
|
|
|
if mtu, ok := reject.Detail.(int); ok {
|
|
|
|
// Create the Packet Too Big response
|
|
|
|
ptb := &icmp.PacketTooBig{
|
|
|
|
MTU: int(mtu),
|
|
|
|
Data: reject.Packet,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the ICMPv6 response from it
|
|
|
|
icmpv6Buf, err := CreateICMPv6(
|
|
|
|
reject.Packet[8:24], reject.Packet[24:40],
|
|
|
|
ipv6.ICMPTypePacketTooBig, 0, ptb)
|
|
|
|
|
|
|
|
// Send the ICMPv6 response back to the TUN/TAP adapter
|
|
|
|
if err == nil {
|
|
|
|
tun.iface.Write(icmpv6Buf)
|
|
|
|
}
|
2018-11-10 18:46:10 +03:00
|
|
|
}
|
2019-03-28 12:50:13 +03:00
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
continue
|
2018-11-10 18:46:10 +03:00
|
|
|
}
|
2019-03-28 12:50:13 +03:00
|
|
|
case data := <-tun.Recv:
|
|
|
|
if tun.iface == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if tun.iface.IsTAP() {
|
|
|
|
var destAddr address.Address
|
|
|
|
if data[0]&0xf0 == 0x60 {
|
|
|
|
if len(data) < 40 {
|
|
|
|
//panic("Tried to send a packet shorter than an IPv6 header...")
|
|
|
|
util.PutBytes(data)
|
|
|
|
continue
|
2018-11-10 20:32:03 +03:00
|
|
|
}
|
2019-03-28 12:50:13 +03:00
|
|
|
copy(destAddr[:16], data[24:])
|
|
|
|
} else if data[0]&0xf0 == 0x40 {
|
|
|
|
if len(data) < 20 {
|
|
|
|
//panic("Tried to send a packet shorter than an IPv4 header...")
|
|
|
|
util.PutBytes(data)
|
|
|
|
continue
|
2018-11-10 20:32:03 +03:00
|
|
|
}
|
2019-03-28 12:50:13 +03:00
|
|
|
copy(destAddr[:4], data[16:])
|
|
|
|
} else {
|
|
|
|
return errors.New("Invalid address family")
|
|
|
|
}
|
|
|
|
sendndp := func(destAddr address.Address) {
|
|
|
|
neigh, known := tun.icmpv6.peermacs[destAddr]
|
|
|
|
known = known && (time.Since(neigh.lastsolicitation).Seconds() < 30)
|
|
|
|
if !known {
|
|
|
|
request, err := tun.icmpv6.CreateNDPL2(destAddr)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if _, err := tun.iface.Write(request); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
tun.icmpv6.peermacs[destAddr] = neighbor{
|
|
|
|
lastsolicitation: time.Now(),
|
|
|
|
}
|
2018-11-10 20:32:03 +03:00
|
|
|
}
|
|
|
|
}
|
2019-03-28 12:50:13 +03:00
|
|
|
var peermac macAddress
|
|
|
|
var peerknown bool
|
|
|
|
if data[0]&0xf0 == 0x40 {
|
2019-03-28 03:30:25 +03:00
|
|
|
destAddr = tun.addr
|
2019-03-28 12:50:13 +03:00
|
|
|
} else if data[0]&0xf0 == 0x60 {
|
|
|
|
if !bytes.Equal(tun.addr[:16], destAddr[:16]) && !bytes.Equal(tun.subnet[:8], destAddr[:8]) {
|
|
|
|
destAddr = tun.addr
|
|
|
|
}
|
2018-11-10 21:33:52 +03:00
|
|
|
}
|
2019-03-28 12:50:13 +03:00
|
|
|
if neighbor, ok := tun.icmpv6.peermacs[destAddr]; ok && neighbor.learned {
|
|
|
|
peermac = neighbor.mac
|
|
|
|
peerknown = true
|
|
|
|
} else if neighbor, ok := tun.icmpv6.peermacs[tun.addr]; ok && neighbor.learned {
|
|
|
|
peermac = neighbor.mac
|
|
|
|
peerknown = true
|
|
|
|
sendndp(destAddr)
|
|
|
|
} else {
|
|
|
|
sendndp(tun.addr)
|
2018-11-10 21:33:52 +03:00
|
|
|
}
|
2019-03-28 12:50:13 +03:00
|
|
|
if peerknown {
|
|
|
|
var proto ethernet.Ethertype
|
|
|
|
switch {
|
|
|
|
case data[0]&0xf0 == 0x60:
|
|
|
|
proto = ethernet.IPv6
|
|
|
|
case data[0]&0xf0 == 0x40:
|
|
|
|
proto = ethernet.IPv4
|
|
|
|
}
|
|
|
|
var frame ethernet.Frame
|
|
|
|
frame.Prepare(
|
|
|
|
peermac[:6], // Destination MAC address
|
|
|
|
tun.icmpv6.mymac[:6], // Source MAC address
|
|
|
|
ethernet.NotTagged, // VLAN tagging
|
|
|
|
proto, // Ethertype
|
|
|
|
len(data)) // Payload length
|
|
|
|
copy(frame[tun_ETHER_HEADER_LENGTH:], data[:])
|
|
|
|
if _, err := tun.iface.Write(frame); err != nil {
|
|
|
|
tun.mutex.RLock()
|
|
|
|
open := tun.isOpen
|
|
|
|
tun.mutex.RUnlock()
|
|
|
|
if !open {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err := tun.iface.Write(data); err != nil {
|
2018-12-17 02:01:59 +03:00
|
|
|
tun.mutex.RLock()
|
|
|
|
open := tun.isOpen
|
|
|
|
tun.mutex.RUnlock()
|
|
|
|
if !open {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-11-10 18:46:10 +03:00
|
|
|
}
|
2018-01-24 13:59:01 +03:00
|
|
|
}
|
2019-03-28 12:50:13 +03:00
|
|
|
util.PutBytes(data)
|
2018-01-05 01:37:51 +03:00
|
|
|
}
|
|
|
|
}
|
2017-12-29 07:16:20 +03:00
|
|
|
}
|
|
|
|
|
2018-06-13 00:45:53 +03:00
|
|
|
// Reads any packets that are waiting on the TUN/TAP adapter. If the adapter
|
|
|
|
// is running in TAP mode then the ethernet headers will automatically be
|
|
|
|
// processed and stripped if necessary. If an ICMPv6 packet is found, then
|
|
|
|
// the relevant helper functions in icmpv6.go are called.
|
2019-03-30 03:09:35 +03:00
|
|
|
func (tun *TunAdapter) read() error {
|
2018-01-25 20:44:56 +03:00
|
|
|
mtu := tun.mtu
|
|
|
|
if tun.iface.IsTAP() {
|
2018-05-28 01:31:34 +03:00
|
|
|
mtu += tun_ETHER_HEADER_LENGTH
|
2018-01-25 20:44:56 +03:00
|
|
|
}
|
|
|
|
buf := make([]byte, mtu)
|
2018-01-05 01:37:51 +03:00
|
|
|
for {
|
|
|
|
n, err := tun.iface.Read(buf)
|
|
|
|
if err != nil {
|
2018-12-17 02:01:59 +03:00
|
|
|
tun.mutex.RLock()
|
|
|
|
open := tun.isOpen
|
|
|
|
tun.mutex.RUnlock()
|
|
|
|
if !open {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-05 01:37:51 +03:00
|
|
|
}
|
2018-01-24 13:59:01 +03:00
|
|
|
o := 0
|
|
|
|
if tun.iface.IsTAP() {
|
2018-05-28 01:31:34 +03:00
|
|
|
o = tun_ETHER_HEADER_LENGTH
|
2018-01-24 13:59:01 +03:00
|
|
|
}
|
2018-11-07 01:35:28 +03:00
|
|
|
switch {
|
|
|
|
case buf[o]&0xf0 == 0x60 && n == 256*int(buf[o+4])+int(buf[o+5])+tun_IPv6_HEADER_LENGTH+o:
|
|
|
|
case buf[o]&0xf0 == 0x40 && n == 256*int(buf[o+2])+int(buf[o+3])+o:
|
|
|
|
default:
|
|
|
|
continue
|
2018-01-05 01:37:51 +03:00
|
|
|
}
|
2018-02-12 21:19:31 +03:00
|
|
|
if buf[o+6] == 58 {
|
2018-12-27 01:45:21 +03:00
|
|
|
if tun.iface.IsTAP() {
|
|
|
|
// Found an ICMPv6 packet
|
|
|
|
b := make([]byte, n)
|
|
|
|
copy(b, buf)
|
2019-03-28 03:30:25 +03:00
|
|
|
go tun.icmpv6.ParsePacket(b)
|
2018-12-27 01:45:21 +03:00
|
|
|
}
|
2018-02-12 21:19:31 +03:00
|
|
|
}
|
2018-12-15 05:49:18 +03:00
|
|
|
packet := append(util.GetBytes(), buf[o:n]...)
|
2019-03-28 03:30:25 +03:00
|
|
|
tun.Send <- packet
|
2018-01-05 01:37:51 +03:00
|
|
|
}
|
2017-12-29 07:16:20 +03:00
|
|
|
}
|
|
|
|
|
2018-06-13 00:45:53 +03:00
|
|
|
// Closes the TUN/TAP adapter. This is only usually called when the Yggdrasil
|
|
|
|
// process stops. Typically this operation will happen quickly, but on macOS
|
2018-06-13 01:50:08 +03:00
|
|
|
// it can block until a read operation is completed.
|
2019-03-28 03:30:25 +03:00
|
|
|
func (tun *TunAdapter) Close() error {
|
2018-12-17 02:01:59 +03:00
|
|
|
tun.mutex.Lock()
|
|
|
|
tun.isOpen = false
|
|
|
|
tun.mutex.Unlock()
|
2018-02-16 01:29:13 +03:00
|
|
|
if tun.iface == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-05 01:37:51 +03:00
|
|
|
return tun.iface.Close()
|
2017-12-29 07:16:20 +03:00
|
|
|
}
|