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"
|
2019-08-28 18:31:04 +00:00
|
|
|
|
2019-08-25 23:53:11 +00:00
|
|
|
//"sync"
|
2018-07-07 11:08:52 +00:00
|
|
|
|
2019-08-25 23:08:43 +00:00
|
|
|
"github.com/Arceliar/phony"
|
2019-03-28 00:30:25 +00:00
|
|
|
"github.com/gologme/log"
|
2019-11-22 16:43:50 +00:00
|
|
|
"golang.zx2c4.com/wireguard/tun"
|
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
|
2017-12-29 04:16:20 +00:00
|
|
|
|
2019-11-22 16:43:50 +00:00
|
|
|
// TunAdapter represents a running TUN interface and extends the
|
|
|
|
// yggdrasil.Adapter type. In order to use the TUN 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-11-21 00:02:39 +00:00
|
|
|
core *yggdrasil.Core
|
2019-08-25 23:53:11 +00:00
|
|
|
writer tunWriter
|
|
|
|
reader tunReader
|
|
|
|
config *config.NodeState
|
|
|
|
log *log.Logger
|
|
|
|
reconfigure chan chan error
|
|
|
|
listener *yggdrasil.Listener
|
|
|
|
dialer *yggdrasil.Dialer
|
|
|
|
addr address.Address
|
|
|
|
subnet address.Subnet
|
|
|
|
ckr cryptokey
|
|
|
|
icmpv6 ICMPv6
|
|
|
|
mtu int
|
2019-11-22 16:43:50 +00:00
|
|
|
iface tun.Device
|
2019-08-25 23:53:11 +00:00
|
|
|
phony.Inbox // Currently only used for _handlePacket from the reader, TODO: all the stuff that currently needs a mutex below
|
|
|
|
//mutex sync.RWMutex // Protects the below
|
2019-04-28 16:14:09 +00:00
|
|
|
addrToConn map[address.Address]*tunConn
|
|
|
|
subnetToConn map[address.Subnet]*tunConn
|
2019-10-22 00:44:06 +00:00
|
|
|
dials map[string][][]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
|
|
|
}
|
|
|
|
|
2019-10-23 09:44:58 +00:00
|
|
|
type TunOptions struct {
|
|
|
|
Listener *yggdrasil.Listener
|
|
|
|
Dialer *yggdrasil.Dialer
|
|
|
|
}
|
|
|
|
|
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().
|
2019-11-22 16:43:50 +00:00
|
|
|
func getSupportedMTU(mtu int) int {
|
2019-11-19 14:20:11 +00:00
|
|
|
if mtu < 1280 {
|
|
|
|
return 1280
|
|
|
|
}
|
2019-11-22 16:43:50 +00:00
|
|
|
if mtu > MaximumMTU() {
|
|
|
|
return MaximumMTU()
|
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 {
|
2019-11-22 16:43:50 +00:00
|
|
|
if name, err := tun.iface.Name(); err == nil {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
return ""
|
2019-03-28 00:30:25 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-11-22 16:43:50 +00:00
|
|
|
return getSupportedMTU(tun.mtu)
|
2019-03-28 00:30:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 16:43:50 +00:00
|
|
|
// DefaultName gets the default TUN interface name for your platform.
|
2019-03-29 18:05:17 +00:00
|
|
|
func DefaultName() string {
|
|
|
|
return defaults.GetDefaults().DefaultIfName
|
|
|
|
}
|
|
|
|
|
2019-11-22 16:43:50 +00:00
|
|
|
// DefaultMTU gets the default TUN interface MTU for your platform. This can
|
2019-03-29 18:18:31 +00:00
|
|
|
// 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 {
|
2019-11-22 16:43:50 +00:00
|
|
|
return defaults.GetDefaults().DefaultIfMTU
|
2019-03-29 18:05:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 16:43:50 +00:00
|
|
|
// MaximumMTU returns the maximum supported TUN interface MTU for your
|
2019-03-29 18:18:31 +00:00
|
|
|
// platform. This can be as high as 65535, depending on platform, but is never
|
|
|
|
// lower than 1280.
|
2019-11-22 16:43:50 +00:00
|
|
|
func MaximumMTU() int {
|
|
|
|
return defaults.GetDefaults().MaximumIfMTU
|
2019-03-29 18:05:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 16:43:50 +00:00
|
|
|
// Init initialises the TUN module. You must have acquired a Listener from
|
2019-04-20 15:32:27 +00:00
|
|
|
// the Yggdrasil core before this point and it must not be in use elsewhere.
|
2019-10-23 09:44:58 +00:00
|
|
|
func (tun *TunAdapter) Init(core *yggdrasil.Core, config *config.NodeState, log *log.Logger, options interface{}) error {
|
|
|
|
tunoptions, ok := options.(TunOptions)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("invalid options supplied to TunAdapter module")
|
|
|
|
}
|
2019-11-21 00:02:39 +00:00
|
|
|
tun.core = core
|
2019-04-20 15:32:27 +00:00
|
|
|
tun.config = config
|
|
|
|
tun.log = log
|
2019-10-23 09:44:58 +00:00
|
|
|
tun.listener = tunoptions.Listener
|
|
|
|
tun.dialer = tunoptions.Dialer
|
2019-04-28 16:14:09 +00:00
|
|
|
tun.addrToConn = make(map[address.Address]*tunConn)
|
|
|
|
tun.subnetToConn = make(map[address.Subnet]*tunConn)
|
2019-10-22 00:44:06 +00:00
|
|
|
tun.dials = make(map[string][][]byte)
|
2019-08-25 23:08:43 +00:00
|
|
|
tun.writer.tun = tun
|
|
|
|
tun.reader.tun = tun
|
2019-10-23 09:44:58 +00:00
|
|
|
return nil
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 16:43:50 +00:00
|
|
|
// Start the setup process for the TUN adapter. If successful, starts the
|
2019-08-25 23:53:11 +00:00
|
|
|
// reader actor to handle packets on that interface.
|
2019-04-20 15:32:27 +00:00
|
|
|
func (tun *TunAdapter) Start() error {
|
2019-08-25 23:53:11 +00:00
|
|
|
var err error
|
2019-08-28 00:43:54 +00:00
|
|
|
phony.Block(tun, func() {
|
2019-08-25 23:53:11 +00:00
|
|
|
err = tun._start()
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tun *TunAdapter) _start() error {
|
2019-10-24 22:37:39 +00:00
|
|
|
if tun.isOpen {
|
2019-11-22 16:43:50 +00:00
|
|
|
return errors.New("TUN module is already started")
|
2019-10-24 22:37:39 +00:00
|
|
|
}
|
2019-07-27 14:00:09 +00:00
|
|
|
current := tun.config.GetCurrent()
|
2019-04-20 15:32:27 +00:00
|
|
|
if tun.config == nil || tun.listener == nil || tun.dialer == nil {
|
2019-11-22 16:43:50 +00:00
|
|
|
return errors.New("no configuration available to TUN")
|
2019-03-28 00:30:25 +00:00
|
|
|
}
|
2019-04-20 15:32:27 +00:00
|
|
|
var boxPub crypto.BoxPubKey
|
2019-07-06 19:08:32 +00:00
|
|
|
boxPubHex, err := hex.DecodeString(current.EncryptionPublicKey)
|
2019-04-20 15:32:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
copy(boxPub[:], boxPubHex)
|
|
|
|
nodeID := crypto.GetNodeID(&boxPub)
|
|
|
|
tun.addr = *address.AddrForNodeID(nodeID)
|
|
|
|
tun.subnet = *address.SubnetForNodeID(nodeID)
|
2019-04-22 01:56:12 +00:00
|
|
|
addr := fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(address.GetPrefix())-1)
|
2019-11-20 22:11:52 +00:00
|
|
|
if current.IfName == "none" || current.IfName == "dummy" {
|
2019-11-22 16:43:50 +00:00
|
|
|
tun.log.Debugln("Not starting TUN as ifname is none or dummy")
|
2019-01-02 18:05:54 +00:00
|
|
|
return nil
|
2018-05-27 21:35:30 +00:00
|
|
|
}
|
2019-11-22 16:43:50 +00:00
|
|
|
if err := tun.setup(current.IfName, addr, current.IfMTU); err != nil {
|
2019-11-20 22:11:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if tun.MTU() != current.IfMTU {
|
2019-11-22 16:43:50 +00:00
|
|
|
tun.log.Warnf("Warning: Interface MTU %d automatically adjusted to %d (supported range is 1280-%d)", current.IfMTU, tun.MTU(), MaximumMTU())
|
2019-11-20 22:11:52 +00:00
|
|
|
}
|
2019-11-21 00:02:39 +00:00
|
|
|
tun.core.SetMaximumSessionMTU(uint16(tun.MTU()))
|
2018-12-16 23:01:59 +00:00
|
|
|
tun.isOpen = true
|
2019-04-20 15:32:27 +00:00
|
|
|
go tun.handler()
|
2019-08-28 00:43:54 +00:00
|
|
|
tun.reader.Act(nil, tun.reader._read) // Start the reader
|
2019-05-17 21:09:20 +00:00
|
|
|
tun.ckr.init(tun)
|
2018-05-27 21:35:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-23 09:44:58 +00:00
|
|
|
// IsStarted returns true if the module has been started.
|
|
|
|
func (tun *TunAdapter) IsStarted() bool {
|
2019-10-24 22:37:39 +00:00
|
|
|
var isOpen bool
|
|
|
|
phony.Block(tun, func() {
|
|
|
|
isOpen = tun.isOpen
|
|
|
|
})
|
|
|
|
return isOpen
|
2019-10-23 09:44:58 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 16:43:50 +00:00
|
|
|
// Start the setup process for the TUN adapter. If successful, starts the
|
2019-07-06 10:52:30 +00:00
|
|
|
// read/write goroutines to handle packets on that interface.
|
|
|
|
func (tun *TunAdapter) Stop() error {
|
2019-08-25 23:53:11 +00:00
|
|
|
var err error
|
2019-08-28 00:43:54 +00:00
|
|
|
phony.Block(tun, func() {
|
2019-08-25 23:53:11 +00:00
|
|
|
err = tun._stop()
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tun *TunAdapter) _stop() error {
|
2019-07-06 10:52:30 +00:00
|
|
|
tun.isOpen = false
|
2019-11-22 16:43:50 +00:00
|
|
|
// by TUN, e.g. readers/writers, sessions
|
2019-09-01 18:06:25 +00:00
|
|
|
if tun.iface != nil {
|
|
|
|
// Just in case we failed to start up the iface for some reason, this can apparently happen on Windows
|
|
|
|
tun.iface.Close()
|
|
|
|
}
|
2019-07-06 10:52:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-22 16:43:50 +00:00
|
|
|
// UpdateConfig updates the TUN module with the provided config.NodeConfig
|
2019-05-17 21:29:52 +00:00
|
|
|
// and then signals the various module goroutines to reconfigure themselves if
|
|
|
|
// needed.
|
|
|
|
func (tun *TunAdapter) UpdateConfig(config *config.NodeConfig) {
|
2019-11-22 16:43:50 +00:00
|
|
|
tun.log.Debugln("Reloading TUN configuration...")
|
2019-05-17 21:29:52 +00:00
|
|
|
|
2019-08-28 18:31:04 +00:00
|
|
|
// Replace the active configuration with the supplied one
|
2019-05-17 21:29:52 +00:00
|
|
|
tun.config.Replace(*config)
|
|
|
|
|
2019-11-22 16:43:50 +00:00
|
|
|
// If the MTU has changed in the TUN module then this is where we would
|
2019-11-21 09:54:36 +00:00
|
|
|
// tell the router so that updated session pings can be sent. However, we
|
|
|
|
// don't currently update the MTU of the adapter once it has been created so
|
|
|
|
// this doesn't actually happen in the real world yet.
|
|
|
|
// tun.core.SetMaximumSessionMTU(...)
|
|
|
|
|
2019-08-28 18:31:04 +00:00
|
|
|
// Notify children about the configuration change
|
|
|
|
tun.Act(nil, tun.ckr.configure)
|
2019-05-17 21:29:52 +00:00
|
|
|
}
|
|
|
|
|
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-11-22 16:43:50 +00:00
|
|
|
tun.log.Errorln("TUN connection accept error:", err)
|
2019-04-20 15:32:27 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-08-28 00:43:54 +00:00
|
|
|
phony.Block(tun, func() {
|
2019-10-19 20:10:28 +00:00
|
|
|
if _, err := tun._wrap(conn.(*yggdrasil.Conn)); err != nil {
|
2019-08-25 23:53:11 +00:00
|
|
|
// Something went wrong when storing the connection, typically that
|
|
|
|
// something already exists for this address or subnet
|
2019-11-22 16:43:50 +00:00
|
|
|
tun.log.Debugln("TUN handler wrap:", err)
|
2019-08-25 23:53:11 +00:00
|
|
|
}
|
|
|
|
})
|
2019-04-20 15:32:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 23:53:11 +00:00
|
|
|
func (tun *TunAdapter) _wrap(conn *yggdrasil.Conn) (c *tunConn, err error) {
|
2019-04-28 16:14:09 +00:00
|
|
|
// Prepare a session wrapper for the given connection
|
|
|
|
s := tunConn{
|
2019-08-25 22:00:02 +00:00
|
|
|
tun: tun,
|
|
|
|
conn: conn,
|
|
|
|
stop: make(chan struct{}),
|
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-10-19 20:10:28 +00:00
|
|
|
remoteNodeID := conn.RemoteAddr().(*crypto.NodeID)
|
|
|
|
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
|
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-08-25 23:53:11 +00:00
|
|
|
atc._close_from_tun()
|
2019-04-28 16:14:09 +00:00
|
|
|
err = errors.New("replaced connection for address")
|
|
|
|
} else if sok {
|
2019-08-25 23:53:11 +00:00
|
|
|
stc._close_from_tun()
|
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-08-25 22:00:02 +00:00
|
|
|
// Set the read callback and start the timeout
|
2019-08-24 16:38:47 +00:00
|
|
|
conn.SetReadCallback(func(bs []byte) {
|
2019-08-28 00:43:54 +00:00
|
|
|
s.Act(conn, func() {
|
2019-08-24 16:38:47 +00:00
|
|
|
s._read(bs)
|
|
|
|
})
|
|
|
|
})
|
2019-08-28 00:43:54 +00:00
|
|
|
s.Act(nil, s.stillAlive)
|
2019-04-28 16:14:09 +00:00
|
|
|
// Return
|
|
|
|
return c, err
|
2017-12-29 04:16:20 +00:00
|
|
|
}
|