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
|
|
|
|
|
2019-04-22 23:04:22 +00:00
|
|
|
// TODO: Crypto-key routing support
|
2019-04-22 22:12:13 +00:00
|
|
|
// TODO: Set MTU of session properly
|
2019-04-22 23:04:22 +00:00
|
|
|
// TODO: Reject packets that exceed session MTU with ICMPv6 for PMTU Discovery
|
|
|
|
// TODO: Connection timeouts (call Conn.Close() when we want to time out)
|
2019-04-28 16:14:09 +00:00
|
|
|
// TODO: Don't block in reader on writes that are pending searches
|
2019-04-22 22:12:13 +00:00
|
|
|
|
2018-06-12 22:50:08 +00:00
|
|
|
import (
|
2019-04-20 15:32:27 +00:00
|
|
|
"encoding/hex"
|
2018-11-10 15:46:10 +00:00
|
|
|
"errors"
|
2019-04-22 01:56:12 +00:00
|
|
|
"fmt"
|
2019-01-14 14:25:52 +00:00
|
|
|
"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/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"
|
2019-04-20 15:32:27 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
2018-12-08 01:56:04 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/defaults"
|
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
|
|
|
|
2019-03-29 18:18:31 +00: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 00:30:25 +00:00
|
|
|
type TunAdapter struct {
|
2019-04-22 19:06:39 +00:00
|
|
|
config *config.NodeState
|
|
|
|
log *log.Logger
|
|
|
|
reconfigure chan chan error
|
|
|
|
listener *yggdrasil.Listener
|
|
|
|
dialer *yggdrasil.Dialer
|
|
|
|
addr address.Address
|
|
|
|
subnet address.Subnet
|
2019-05-17 21:09:20 +00:00
|
|
|
ckr cryptokey
|
2019-04-22 19:06:39 +00:00
|
|
|
icmpv6 ICMPv6
|
|
|
|
mtu int
|
|
|
|
iface *water.Interface
|
2019-04-28 16:14:09 +00:00
|
|
|
send chan []byte
|
|
|
|
mutex sync.RWMutex // Protects the below
|
|
|
|
addrToConn map[address.Address]*tunConn
|
|
|
|
subnetToConn map[address.Subnet]*tunConn
|
2019-06-28 23:42:31 +00:00
|
|
|
dials map[crypto.NodeID][][]byte // Buffer of packets to send after dialing finishes
|
2019-04-22 19:06:39 +00:00
|
|
|
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-29 18:18:31 +00:00
|
|
|
// Name returns the name of the adapter, e.g. "tun0". On Windows, this may
|
|
|
|
// return a canonical adapter name instead.
|
2019-03-28 00:30:25 +00:00
|
|
|
func (tun *TunAdapter) Name() string {
|
|
|
|
return tun.iface.Name()
|
|
|
|
}
|
|
|
|
|
2019-03-29 18:18:31 +00: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 00:30:25 +00:00
|
|
|
func (tun *TunAdapter) MTU() int {
|
|
|
|
return getSupportedMTU(tun.mtu)
|
|
|
|
}
|
|
|
|
|
2019-03-29 18:18:31 +00: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 00:30:25 +00:00
|
|
|
func (tun *TunAdapter) IsTAP() bool {
|
|
|
|
return tun.iface.IsTAP()
|
|
|
|
}
|
|
|
|
|
2019-03-29 18:18:31 +00:00
|
|
|
// DefaultName gets the default TUN/TAP interface name for your platform.
|
2019-03-29 18:05:17 +00:00
|
|
|
func DefaultName() string {
|
|
|
|
return defaults.GetDefaults().DefaultIfName
|
|
|
|
}
|
|
|
|
|
2019-03-29 18:18:31 +00: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 18:05:17 +00:00
|
|
|
func DefaultMTU() int {
|
|
|
|
return defaults.GetDefaults().DefaultIfMTU
|
|
|
|
}
|
|
|
|
|
2019-03-29 18:18:31 +00: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 18:05:17 +00:00
|
|
|
func DefaultIsTAP() bool {
|
|
|
|
return defaults.GetDefaults().DefaultIfTAPMode
|
|
|
|
}
|
|
|
|
|
2019-03-29 18:18:31 +00: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 18:05:17 +00:00
|
|
|
func MaximumMTU() int {
|
|
|
|
return defaults.GetDefaults().MaximumIfMTU
|
|
|
|
}
|
|
|
|
|
2019-04-20 15:32:27 +00:00
|
|
|
// Init initialises the TUN/TAP module. You must have acquired a Listener from
|
|
|
|
// the Yggdrasil core before this point and it must not be in use elsewhere.
|
|
|
|
func (tun *TunAdapter) Init(config *config.NodeState, log *log.Logger, listener *yggdrasil.Listener, dialer *yggdrasil.Dialer) {
|
|
|
|
tun.config = config
|
|
|
|
tun.log = log
|
|
|
|
tun.listener = listener
|
|
|
|
tun.dialer = dialer
|
2019-04-28 16:14:09 +00:00
|
|
|
tun.addrToConn = make(map[address.Address]*tunConn)
|
|
|
|
tun.subnetToConn = make(map[address.Subnet]*tunConn)
|
2019-06-28 23:42:31 +00:00
|
|
|
tun.dials = make(map[crypto.NodeID][][]byte)
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 18:18:31 +00:00
|
|
|
// Start the setup process for the TUN/TAP adapter. If successful, starts the
|
|
|
|
// read/write goroutines to handle packets on that interface.
|
2019-04-20 15:32:27 +00:00
|
|
|
func (tun *TunAdapter) Start() error {
|
2019-05-17 21:09:20 +00:00
|
|
|
tun.config.Mutex.RLock()
|
|
|
|
defer tun.config.Mutex.RUnlock()
|
2019-04-20 15:32:27 +00:00
|
|
|
if tun.config == nil || tun.listener == nil || tun.dialer == nil {
|
2019-03-28 00:30:25 +00:00
|
|
|
return errors.New("No configuration available to TUN/TAP")
|
|
|
|
}
|
2019-04-20 15:32:27 +00:00
|
|
|
var boxPub crypto.BoxPubKey
|
|
|
|
boxPubHex, err := hex.DecodeString(tun.config.Current.EncryptionPublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
copy(boxPub[:], boxPubHex)
|
|
|
|
nodeID := crypto.GetNodeID(&boxPub)
|
|
|
|
tun.addr = *address.AddrForNodeID(nodeID)
|
|
|
|
tun.subnet = *address.SubnetForNodeID(nodeID)
|
|
|
|
tun.mtu = tun.config.Current.IfMTU
|
|
|
|
ifname := tun.config.Current.IfName
|
|
|
|
iftapmode := tun.config.Current.IfTAPMode
|
2019-04-22 01:56:12 +00:00
|
|
|
addr := fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(address.GetPrefix())-1)
|
2019-01-02 18:05:54 +00:00
|
|
|
if ifname != "none" {
|
2019-04-22 01:56:12 +00:00
|
|
|
if err := tun.setup(ifname, iftapmode, addr, tun.mtu); err != nil {
|
2019-01-02 18:05:54 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-06-02 22:29:06 +00:00
|
|
|
}
|
2019-01-02 18:05:54 +00:00
|
|
|
if ifname == "none" || ifname == "dummy" {
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.log.Debugln("Not starting TUN/TAP as ifname is none or dummy")
|
2019-01-02 18:05:54 +00:00
|
|
|
return nil
|
2018-05-27 21:35:30 +00:00
|
|
|
}
|
2018-12-16 23:01:59 +00:00
|
|
|
tun.mutex.Lock()
|
|
|
|
tun.isOpen = true
|
2019-04-28 16:14:09 +00:00
|
|
|
tun.send = make(chan []byte, 32) // TODO: is this a sensible value?
|
2019-05-17 21:29:52 +00:00
|
|
|
tun.reconfigure = make(chan chan error)
|
2018-12-16 23:01:59 +00:00
|
|
|
tun.mutex.Unlock()
|
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)
|
|
|
|
}
|
2019-04-28 16:14:09 +00:00
|
|
|
tun.send <- request
|
2018-11-11 04:39:15 +00:00
|
|
|
time.Sleep(time.Second)
|
2018-11-10 17:32:03 +00:00
|
|
|
}
|
2018-11-11 04:39:15 +00:00
|
|
|
}()
|
|
|
|
}
|
2019-04-20 15:32:27 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
e := <-tun.reconfigure
|
|
|
|
e <- nil
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go tun.handler()
|
2019-04-28 16:14:09 +00:00
|
|
|
go tun.reader()
|
|
|
|
go tun.writer()
|
2019-04-23 10:37:32 +00:00
|
|
|
tun.icmpv6.Init(tun)
|
2019-05-17 21:09:20 +00:00
|
|
|
tun.ckr.init(tun)
|
2018-05-27 21:35:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-05-17 21:29:52 +00:00
|
|
|
// UpdateConfig updates the TUN/TAP module with the provided config.NodeConfig
|
|
|
|
// and then signals the various module goroutines to reconfigure themselves if
|
|
|
|
// needed.
|
|
|
|
func (tun *TunAdapter) UpdateConfig(config *config.NodeConfig) {
|
|
|
|
tun.log.Debugln("Reloading TUN/TAP configuration...")
|
|
|
|
|
|
|
|
tun.config.Replace(*config)
|
|
|
|
|
|
|
|
errors := 0
|
|
|
|
|
|
|
|
components := []chan chan error{
|
|
|
|
tun.reconfigure,
|
|
|
|
tun.ckr.reconfigure,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, component := range components {
|
|
|
|
response := make(chan error)
|
|
|
|
component <- response
|
|
|
|
if err := <-response; err != nil {
|
|
|
|
tun.log.Errorln(err)
|
|
|
|
errors++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors > 0 {
|
|
|
|
tun.log.Warnln(errors, "TUN/TAP module(s) reported errors during configuration reload")
|
|
|
|
} else {
|
|
|
|
tun.log.Infoln("TUN/TAP configuration reloaded successfully")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-20 15:32:27 +00:00
|
|
|
func (tun *TunAdapter) handler() error {
|
|
|
|
for {
|
|
|
|
// Accept the incoming connection
|
|
|
|
conn, err := tun.listener.Accept()
|
|
|
|
if err != nil {
|
2019-04-21 11:28:46 +00:00
|
|
|
tun.log.Errorln("TUN/TAP connection accept error:", err)
|
2019-04-20 15:32:27 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-04-28 16:14:09 +00:00
|
|
|
if _, err := tun.wrap(conn); err != nil {
|
|
|
|
// Something went wrong when storing the connection, typically that
|
|
|
|
// something already exists for this address or subnet
|
|
|
|
tun.log.Debugln("TUN/TAP handler wrap:", err)
|
|
|
|
}
|
2019-04-20 15:32:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-28 16:14:09 +00:00
|
|
|
func (tun *TunAdapter) wrap(conn *yggdrasil.Conn) (c *tunConn, err error) {
|
|
|
|
// Prepare a session wrapper for the given connection
|
|
|
|
s := tunConn{
|
2019-05-28 23:35:52 +00:00
|
|
|
tun: tun,
|
|
|
|
conn: conn,
|
|
|
|
send: make(chan []byte, 32), // TODO: is this a sensible value?
|
|
|
|
stop: make(chan struct{}),
|
|
|
|
alive: make(chan struct{}, 1),
|
2019-04-28 16:14:09 +00:00
|
|
|
}
|
2019-06-29 00:11:28 +00:00
|
|
|
c = &s
|
2019-04-28 16:14:09 +00:00
|
|
|
// Get the remote address and subnet of the other side
|
2019-04-21 11:00:31 +00:00
|
|
|
remoteNodeID := conn.RemoteAddr()
|
2019-05-24 01:27:52 +00:00
|
|
|
s.addr = *address.AddrForNodeID(&remoteNodeID)
|
|
|
|
s.snet = *address.SubnetForNodeID(&remoteNodeID)
|
2019-04-28 16:14:09 +00:00
|
|
|
// Work out if this is already a destination we already know about
|
2018-12-16 23:01:59 +00:00
|
|
|
tun.mutex.Lock()
|
2019-04-28 16:14:09 +00:00
|
|
|
defer tun.mutex.Unlock()
|
2019-05-24 01:27:52 +00:00
|
|
|
atc, aok := tun.addrToConn[s.addr]
|
|
|
|
stc, sok := tun.subnetToConn[s.snet]
|
2019-04-28 16:14:09 +00:00
|
|
|
// If we know about a connection for this destination already then assume it
|
|
|
|
// is no longer valid and close it
|
|
|
|
if aok {
|
2019-05-24 01:27:52 +00:00
|
|
|
atc._close_nomutex()
|
2019-04-28 16:14:09 +00:00
|
|
|
err = errors.New("replaced connection for address")
|
|
|
|
} else if sok {
|
2019-05-24 01:27:52 +00:00
|
|
|
stc._close_nomutex()
|
2019-04-28 16:14:09 +00:00
|
|
|
err = errors.New("replaced connection for subnet")
|
2018-02-15 22:29:13 +00:00
|
|
|
}
|
2019-04-28 16:14:09 +00:00
|
|
|
// Save the session wrapper so that we can look it up quickly next time
|
|
|
|
// we receive a packet through the interface for this address
|
2019-05-24 01:27:52 +00:00
|
|
|
tun.addrToConn[s.addr] = &s
|
|
|
|
tun.subnetToConn[s.snet] = &s
|
2019-04-28 16:14:09 +00:00
|
|
|
// Start the connection goroutines
|
|
|
|
go s.reader()
|
|
|
|
go s.writer()
|
2019-05-28 23:35:52 +00:00
|
|
|
go s.checkForTimeouts()
|
2019-04-28 16:14:09 +00:00
|
|
|
// Return
|
|
|
|
return c, err
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|