2019-03-28 00:30:25 +00:00
|
|
|
package tuntap
|
2017-12-29 04:16:20 +00:00
|
|
|
|
|
|
|
// This manages the tun driver to send/recv packets to/from applications
|
|
|
|
|
2018-06-12 22:50:08 +00:00
|
|
|
import (
|
2018-11-10 18:33:52 +00:00
|
|
|
"bytes"
|
2018-11-10 15:46:10 +00:00
|
|
|
"errors"
|
2019-01-14 14:25:52 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2018-12-16 23:01:59 +00:00
|
|
|
"sync"
|
2018-11-10 17:32:03 +00:00
|
|
|
"time"
|
2018-07-07 11:08:52 +00:00
|
|
|
|
2019-03-28 00:30:25 +00:00
|
|
|
"github.com/gologme/log"
|
|
|
|
|
2018-06-12 22:50:08 +00:00
|
|
|
"github.com/songgao/packets/ethernet"
|
|
|
|
"github.com/yggdrasil-network/water"
|
2018-12-08 01:56:04 +00:00
|
|
|
|
2018-12-15 02:49:18 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
2019-03-28 00:30:25 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/config"
|
2018-12-08 01:56:04 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
|
2018-12-15 02:49:18 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/util"
|
2019-03-28 00:30:25 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil"
|
2018-06-12 22:50:08 +00:00
|
|
|
)
|
2017-12-29 04:16:20 +00:00
|
|
|
|
2018-05-27 22:31:34 +00:00
|
|
|
const tun_IPv6_HEADER_LENGTH = 40
|
|
|
|
const tun_ETHER_HEADER_LENGTH = 14
|
2017-12-29 04:16:20 +00:00
|
|
|
|
2018-06-12 21:45:53 +00:00
|
|
|
// Represents a running TUN/TAP interface.
|
2019-03-28 00:30:25 +00:00
|
|
|
type TunAdapter struct {
|
|
|
|
yggdrasil.Adapter
|
|
|
|
addr address.Address
|
|
|
|
subnet address.Subnet
|
|
|
|
log *log.Logger
|
|
|
|
config *config.NodeState
|
|
|
|
icmpv6 ICMPv6
|
2018-02-12 18:19:31 +00:00
|
|
|
mtu int
|
2018-03-04 19:57:34 +00:00
|
|
|
iface *water.Interface
|
2018-12-16 23:01:59 +00:00
|
|
|
mutex sync.RWMutex // Protects the below
|
|
|
|
isOpen bool
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 21:45:53 +00:00
|
|
|
// Gets the maximum supported MTU for the platform based on the defaults in
|
2018-07-07 11:08:52 +00:00
|
|
|
// defaults.GetDefaults().
|
2018-03-03 12:30:54 +00:00
|
|
|
func getSupportedMTU(mtu int) int {
|
2018-07-07 11:08:52 +00:00
|
|
|
if mtu > defaults.GetDefaults().MaximumIfMTU {
|
|
|
|
return defaults.GetDefaults().MaximumIfMTU
|
2018-03-03 11:47:14 +00:00
|
|
|
}
|
|
|
|
return mtu
|
|
|
|
}
|
|
|
|
|
2019-03-28 00:30:25 +00:00
|
|
|
// Get the adapter name
|
|
|
|
func (tun *TunAdapter) Name() string {
|
|
|
|
return tun.iface.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the adapter MTU
|
|
|
|
func (tun *TunAdapter) MTU() int {
|
|
|
|
return getSupportedMTU(tun.mtu)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the adapter mode
|
|
|
|
func (tun *TunAdapter) IsTAP() bool {
|
|
|
|
return tun.iface.IsTAP()
|
|
|
|
}
|
|
|
|
|
2018-06-12 21:45:53 +00:00
|
|
|
// Initialises the TUN/TAP adapter.
|
2019-03-28 00:30:25 +00:00
|
|
|
func (tun *TunAdapter) Init(config *config.NodeState, log *log.Logger, send chan<- []byte, recv <-chan []byte) {
|
|
|
|
tun.config = config
|
|
|
|
tun.log = log
|
|
|
|
tun.Adapter.Init(config, log, send, recv)
|
|
|
|
tun.icmpv6.Init(tun)
|
2019-01-14 14:25:52 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
2019-03-28 00:30:25 +00:00
|
|
|
e := <-tun.Reconfigure
|
|
|
|
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 08:51:19 +00:00
|
|
|
if updated {
|
2019-03-28 00:30:25 +00:00
|
|
|
tun.log.Warnln("Reconfiguring TUN/TAP is not supported yet")
|
2019-01-15 08:51:19 +00:00
|
|
|
e <- nil
|
|
|
|
} else {
|
|
|
|
e <- nil
|
2019-01-14 14:25:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 21:45:53 +00:00
|
|
|
// Starts the setup process for the TUN/TAP adapter, and if successful, starts
|
|
|
|
// the read/write goroutines to handle packets on that interface.
|
2019-03-28 00:30:25 +00:00
|
|
|
func (tun *TunAdapter) Start(a address.Address, s address.Subnet) error {
|
|
|
|
tun.addr = a
|
|
|
|
tun.subnet = s
|
|
|
|
if tun.config == nil {
|
|
|
|
return errors.New("No configuration available to TUN/TAP")
|
|
|
|
}
|
|
|
|
tun.config.Mutex.RLock()
|
|
|
|
ifname := tun.config.Current.IfName
|
|
|
|
iftapmode := tun.config.Current.IfTAPMode
|
|
|
|
addr := fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(address.GetPrefix())-1)
|
|
|
|
mtu := tun.config.Current.IfMTU
|
|
|
|
tun.config.Mutex.RUnlock()
|
2019-01-02 18:05:54 +00:00
|
|
|
if ifname != "none" {
|
|
|
|
if err := tun.setup(ifname, iftapmode, addr, mtu); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-06-02 22:29:06 +00:00
|
|
|
}
|
2019-01-02 18:05:54 +00:00
|
|
|
if ifname == "none" || ifname == "dummy" {
|
|
|
|
return nil
|
2018-05-27 21:35:30 +00:00
|
|
|
}
|
2018-12-16 23:01:59 +00:00
|
|
|
tun.mutex.Lock()
|
|
|
|
tun.isOpen = true
|
|
|
|
tun.mutex.Unlock()
|
2019-03-28 00:30:25 +00:00
|
|
|
go func() { tun.log.Errorln("WARNING: tun.read() exited with error:", tun.Read()) }()
|
|
|
|
go func() { tun.log.Errorln("WARNING: tun.write() exited with error:", tun.Write()) }()
|
2018-11-11 04:39:15 +00:00
|
|
|
if iftapmode {
|
|
|
|
go func() {
|
|
|
|
for {
|
2019-03-28 00:30:25 +00:00
|
|
|
if _, ok := tun.icmpv6.peermacs[tun.addr]; ok {
|
2018-11-11 04:39:15 +00:00
|
|
|
break
|
|
|
|
}
|
2019-03-28 00:30:25 +00:00
|
|
|
request, err := tun.icmpv6.CreateNDPL2(tun.addr)
|
2018-11-11 04:39:15 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if _, err := tun.iface.Write(request); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
2018-11-10 17:32:03 +00:00
|
|
|
}
|
2018-11-11 04:39:15 +00:00
|
|
|
}()
|
|
|
|
}
|
2018-05-27 21:35:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-12 21:45:53 +00: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-28 00:30:25 +00:00
|
|
|
func (tun *TunAdapter) Write() error {
|
2018-01-04 22:37:51 +00:00
|
|
|
for {
|
2019-03-28 00:30:25 +00:00
|
|
|
data := <-tun.Recv
|
2018-02-15 22:29:13 +00:00
|
|
|
if tun.iface == nil {
|
|
|
|
continue
|
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
if tun.iface.IsTAP() {
|
2018-12-15 02:49:18 +00:00
|
|
|
var destAddr address.Address
|
2018-11-10 15:46:10 +00:00
|
|
|
if data[0]&0xf0 == 0x60 {
|
|
|
|
if len(data) < 40 {
|
|
|
|
panic("Tried to send a packet shorter than an IPv6 header...")
|
|
|
|
}
|
|
|
|
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...")
|
|
|
|
}
|
|
|
|
copy(destAddr[:4], data[16:])
|
|
|
|
} else {
|
|
|
|
return errors.New("Invalid address family")
|
|
|
|
}
|
2018-12-15 02:49:18 +00:00
|
|
|
sendndp := func(destAddr address.Address) {
|
2018-11-10 17:32:03 +00:00
|
|
|
neigh, known := tun.icmpv6.peermacs[destAddr]
|
|
|
|
known = known && (time.Since(neigh.lastsolicitation).Seconds() < 30)
|
|
|
|
if !known {
|
2019-03-28 00:30:25 +00:00
|
|
|
request, err := tun.icmpv6.CreateNDPL2(destAddr)
|
2018-11-10 17:32:03 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if _, err := tun.iface.Write(request); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
tun.icmpv6.peermacs[destAddr] = neighbor{
|
|
|
|
lastsolicitation: time.Now(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var peermac macAddress
|
|
|
|
var peerknown bool
|
2018-11-10 18:33:52 +00:00
|
|
|
if data[0]&0xf0 == 0x40 {
|
2019-03-28 00:30:25 +00:00
|
|
|
destAddr = tun.addr
|
2018-11-10 18:33:52 +00:00
|
|
|
} else if data[0]&0xf0 == 0x60 {
|
2019-03-28 00:30:25 +00:00
|
|
|
if !bytes.Equal(tun.addr[:16], destAddr[:16]) && !bytes.Equal(tun.subnet[:8], destAddr[:8]) {
|
|
|
|
destAddr = tun.addr
|
2018-11-10 18:33:52 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-10 17:32:03 +00:00
|
|
|
if neighbor, ok := tun.icmpv6.peermacs[destAddr]; ok && neighbor.learned {
|
|
|
|
peermac = neighbor.mac
|
|
|
|
peerknown = true
|
2019-03-28 00:30:25 +00:00
|
|
|
} else if neighbor, ok := tun.icmpv6.peermacs[tun.addr]; ok && neighbor.learned {
|
2018-11-10 17:32:03 +00:00
|
|
|
peermac = neighbor.mac
|
|
|
|
peerknown = true
|
|
|
|
sendndp(destAddr)
|
|
|
|
} else {
|
2019-03-28 00:30:25 +00:00
|
|
|
sendndp(tun.addr)
|
2018-11-10 17:32:03 +00:00
|
|
|
}
|
|
|
|
if peerknown {
|
2018-11-10 18:33:52 +00:00
|
|
|
var proto ethernet.Ethertype
|
|
|
|
switch {
|
|
|
|
case data[0]&0xf0 == 0x60:
|
|
|
|
proto = ethernet.IPv6
|
|
|
|
case data[0]&0xf0 == 0x40:
|
|
|
|
proto = ethernet.IPv4
|
|
|
|
}
|
2018-11-10 15:46:10 +00:00
|
|
|
var frame ethernet.Frame
|
|
|
|
frame.Prepare(
|
|
|
|
peermac[:6], // Destination MAC address
|
|
|
|
tun.icmpv6.mymac[:6], // Source MAC address
|
|
|
|
ethernet.NotTagged, // VLAN tagging
|
2018-11-10 18:33:52 +00:00
|
|
|
proto, // Ethertype
|
2018-11-10 15:46:10 +00:00
|
|
|
len(data)) // Payload length
|
|
|
|
copy(frame[tun_ETHER_HEADER_LENGTH:], data[:])
|
|
|
|
if _, err := tun.iface.Write(frame); err != nil {
|
2018-12-16 23:01:59 +00:00
|
|
|
tun.mutex.RLock()
|
|
|
|
open := tun.isOpen
|
|
|
|
tun.mutex.RUnlock()
|
|
|
|
if !open {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-11-10 15:46:10 +00:00
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, err := tun.iface.Write(data); err != nil {
|
2018-12-16 23:01:59 +00:00
|
|
|
tun.mutex.RLock()
|
|
|
|
open := tun.isOpen
|
|
|
|
tun.mutex.RUnlock()
|
|
|
|
if !open {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
}
|
2018-01-04 22:37:51 +00:00
|
|
|
}
|
2018-12-15 02:49:18 +00:00
|
|
|
util.PutBytes(data)
|
2018-01-04 22:37:51 +00:00
|
|
|
}
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 21:45:53 +00: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-28 00:30:25 +00:00
|
|
|
func (tun *TunAdapter) Read() error {
|
2018-01-25 17:44:56 +00:00
|
|
|
mtu := tun.mtu
|
|
|
|
if tun.iface.IsTAP() {
|
2018-05-27 22:31:34 +00:00
|
|
|
mtu += tun_ETHER_HEADER_LENGTH
|
2018-01-25 17:44:56 +00:00
|
|
|
}
|
|
|
|
buf := make([]byte, mtu)
|
2018-01-04 22:37:51 +00:00
|
|
|
for {
|
|
|
|
n, err := tun.iface.Read(buf)
|
|
|
|
if err != nil {
|
2018-12-16 23:01:59 +00:00
|
|
|
tun.mutex.RLock()
|
|
|
|
open := tun.isOpen
|
|
|
|
tun.mutex.RUnlock()
|
|
|
|
if !open {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
// panic(err)
|
|
|
|
return err
|
|
|
|
}
|
2018-01-04 22:37:51 +00:00
|
|
|
}
|
2018-01-24 10:59:01 +00:00
|
|
|
o := 0
|
|
|
|
if tun.iface.IsTAP() {
|
2018-05-27 22:31:34 +00:00
|
|
|
o = tun_ETHER_HEADER_LENGTH
|
2018-01-24 10:59:01 +00:00
|
|
|
}
|
2018-11-06 22:35:28 +00: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-04 22:37:51 +00:00
|
|
|
}
|
2018-02-12 18:19:31 +00:00
|
|
|
if buf[o+6] == 58 {
|
2018-12-26 22:45:21 +00:00
|
|
|
if tun.iface.IsTAP() {
|
|
|
|
// Found an ICMPv6 packet
|
|
|
|
b := make([]byte, n)
|
|
|
|
copy(b, buf)
|
2019-03-28 00:30:25 +00:00
|
|
|
go tun.icmpv6.ParsePacket(b)
|
2018-12-26 22:45:21 +00:00
|
|
|
}
|
2018-02-12 18:19:31 +00:00
|
|
|
}
|
2018-12-15 02:49:18 +00:00
|
|
|
packet := append(util.GetBytes(), buf[o:n]...)
|
2019-03-28 00:30:25 +00:00
|
|
|
tun.Send <- packet
|
2018-01-04 22:37:51 +00:00
|
|
|
}
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 21:45:53 +00: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-12 22:50:08 +00:00
|
|
|
// it can block until a read operation is completed.
|
2019-03-28 00:30:25 +00:00
|
|
|
func (tun *TunAdapter) Close() error {
|
2018-12-16 23:01:59 +00:00
|
|
|
tun.mutex.Lock()
|
|
|
|
tun.isOpen = false
|
|
|
|
tun.mutex.Unlock()
|
2018-02-15 22:29:13 +00:00
|
|
|
if tun.iface == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2018-01-04 22:37:51 +00:00
|
|
|
return tun.iface.Close()
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|