2019-04-28 16:14:09 +00:00
|
|
|
package tuntap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-05-28 23:35:52 +00:00
|
|
|
"time"
|
2019-04-28 16:14:09 +00:00
|
|
|
|
2019-05-24 01:27:52 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
2019-05-02 22:37:49 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/util"
|
2019-04-28 16:14:09 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil"
|
2019-05-29 19:16:17 +00:00
|
|
|
"golang.org/x/net/icmp"
|
|
|
|
"golang.org/x/net/ipv6"
|
2019-04-28 16:14:09 +00:00
|
|
|
)
|
|
|
|
|
2019-07-17 09:12:10 +00:00
|
|
|
const tunConnTimeout = 2 * time.Minute
|
|
|
|
|
2019-04-28 16:14:09 +00:00
|
|
|
type tunConn struct {
|
2019-05-28 23:35:52 +00:00
|
|
|
tun *TunAdapter
|
|
|
|
conn *yggdrasil.Conn
|
|
|
|
addr address.Address
|
|
|
|
snet address.Subnet
|
|
|
|
send chan []byte
|
|
|
|
stop chan struct{}
|
|
|
|
alive chan struct{}
|
2019-04-28 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *tunConn) close() {
|
2019-05-24 01:27:52 +00:00
|
|
|
s.tun.mutex.Lock()
|
2019-05-28 23:35:52 +00:00
|
|
|
defer s.tun.mutex.Unlock()
|
2019-05-24 01:27:52 +00:00
|
|
|
s._close_nomutex()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *tunConn) _close_nomutex() {
|
2019-05-28 23:35:52 +00:00
|
|
|
s.conn.Close()
|
2019-05-24 01:27:52 +00:00
|
|
|
delete(s.tun.addrToConn, s.addr)
|
|
|
|
delete(s.tun.subnetToConn, s.snet)
|
2019-05-28 23:35:52 +00:00
|
|
|
func() {
|
|
|
|
defer func() { recover() }()
|
|
|
|
close(s.stop) // Closes reader/writer goroutines
|
|
|
|
}()
|
|
|
|
func() {
|
|
|
|
defer func() { recover() }()
|
|
|
|
close(s.alive) // Closes timeout goroutine
|
|
|
|
}()
|
2019-04-28 16:14:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *tunConn) reader() error {
|
|
|
|
select {
|
|
|
|
case _, ok := <-s.stop:
|
|
|
|
if !ok {
|
|
|
|
return errors.New("session was already closed")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
2019-07-17 09:12:10 +00:00
|
|
|
s.tun.log.Debugln("Starting conn reader for", s)
|
2019-07-17 10:13:53 +00:00
|
|
|
defer s.tun.log.Debugln("Stopping conn reader for", s)
|
2019-04-28 16:14:09 +00:00
|
|
|
var n int
|
|
|
|
var err error
|
|
|
|
read := make(chan bool)
|
|
|
|
b := make([]byte, 65535)
|
2019-07-17 09:12:10 +00:00
|
|
|
go func() {
|
|
|
|
s.tun.log.Debugln("Starting conn reader helper for", s)
|
2019-07-17 10:13:53 +00:00
|
|
|
defer s.tun.log.Debugln("Stopping conn reader helper for", s)
|
2019-07-17 09:12:10 +00:00
|
|
|
for {
|
|
|
|
s.conn.SetReadDeadline(time.Now().Add(tunConnTimeout))
|
2019-04-28 16:14:09 +00:00
|
|
|
if n, err = s.conn.Read(b); err != nil {
|
|
|
|
s.tun.log.Errorln(s.conn.String(), "TUN/TAP conn read error:", err)
|
2019-07-17 09:12:10 +00:00
|
|
|
if e, eok := err.(yggdrasil.ConnError); eok {
|
2019-07-17 10:13:53 +00:00
|
|
|
s.tun.log.Debugln("Conn reader helper", s, "error:", e)
|
2019-07-17 09:12:10 +00:00
|
|
|
switch {
|
|
|
|
case e.Temporary():
|
2019-07-17 10:13:53 +00:00
|
|
|
fallthrough
|
|
|
|
case e.Timeout():
|
2019-07-17 09:12:10 +00:00
|
|
|
read <- false
|
|
|
|
continue
|
2019-07-17 10:13:53 +00:00
|
|
|
case e.Closed():
|
2019-07-17 09:12:10 +00:00
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
s.close()
|
|
|
|
return
|
|
|
|
}
|
2019-05-30 11:44:47 +00:00
|
|
|
}
|
2019-07-17 09:12:10 +00:00
|
|
|
read <- false
|
2019-04-28 16:14:09 +00:00
|
|
|
}
|
|
|
|
read <- true
|
2019-07-17 09:12:10 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
for {
|
2019-04-28 16:14:09 +00:00
|
|
|
select {
|
2019-07-17 11:07:16 +00:00
|
|
|
case r, ok := <-read:
|
2019-05-30 11:44:47 +00:00
|
|
|
if r && n > 0 {
|
2019-05-15 23:01:26 +00:00
|
|
|
bs := append(util.GetBytes(), b[:n]...)
|
|
|
|
select {
|
|
|
|
case s.tun.send <- bs:
|
|
|
|
default:
|
|
|
|
util.PutBytes(bs)
|
|
|
|
}
|
2019-04-28 16:14:09 +00:00
|
|
|
}
|
2019-07-17 11:07:16 +00:00
|
|
|
if ok {
|
|
|
|
s.stillAlive() // TODO? Only stay alive if we read >0 bytes?
|
|
|
|
}
|
2019-04-28 16:14:09 +00:00
|
|
|
case <-s.stop:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *tunConn) writer() error {
|
|
|
|
select {
|
|
|
|
case _, ok := <-s.stop:
|
|
|
|
if !ok {
|
|
|
|
return errors.New("session was already closed")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
2019-07-17 09:12:10 +00:00
|
|
|
s.tun.log.Debugln("Starting conn writer for", s)
|
2019-07-17 10:13:53 +00:00
|
|
|
defer s.tun.log.Debugln("Stopping conn writer for", s)
|
2019-04-28 16:14:09 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.stop:
|
|
|
|
return nil
|
|
|
|
case b, ok := <-s.send:
|
|
|
|
if !ok {
|
|
|
|
return errors.New("send closed")
|
|
|
|
}
|
2019-05-24 01:27:52 +00:00
|
|
|
// TODO write timeout and close
|
2019-04-28 16:14:09 +00:00
|
|
|
if _, err := s.conn.Write(b); err != nil {
|
2019-05-29 19:16:17 +00:00
|
|
|
e, eok := err.(yggdrasil.ConnError)
|
|
|
|
if !eok {
|
|
|
|
s.tun.log.Errorln(s.conn.String(), "TUN/TAP generic write error:", err)
|
|
|
|
} else if ispackettoobig, maxsize := e.PacketTooBig(); ispackettoobig {
|
|
|
|
// TODO: This currently isn't aware of IPv4 for CKR
|
|
|
|
ptb := &icmp.PacketTooBig{
|
|
|
|
MTU: int(maxsize),
|
|
|
|
Data: b[:900],
|
|
|
|
}
|
|
|
|
if packet, err := CreateICMPv6(b[8:24], b[24:40], ipv6.ICMPTypePacketTooBig, 0, ptb); err == nil {
|
|
|
|
s.tun.send <- packet
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.tun.log.Errorln(s.conn.String(), "TUN/TAP conn write error:", err)
|
|
|
|
}
|
2019-04-28 16:14:09 +00:00
|
|
|
}
|
2019-05-02 22:37:49 +00:00
|
|
|
util.PutBytes(b)
|
2019-05-28 23:35:52 +00:00
|
|
|
s.stillAlive()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *tunConn) stillAlive() {
|
|
|
|
select {
|
|
|
|
case s.alive <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *tunConn) checkForTimeouts() error {
|
2019-07-17 09:12:10 +00:00
|
|
|
timer := time.NewTimer(tunConnTimeout)
|
2019-05-28 23:35:52 +00:00
|
|
|
defer util.TimerStop(timer)
|
|
|
|
defer s.close()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case _, ok := <-s.alive:
|
|
|
|
if !ok {
|
|
|
|
return errors.New("connection closed")
|
|
|
|
}
|
|
|
|
util.TimerStop(timer)
|
2019-07-17 09:12:10 +00:00
|
|
|
timer.Reset(tunConnTimeout)
|
2019-05-28 23:35:52 +00:00
|
|
|
case <-timer.C:
|
|
|
|
return errors.New("timed out")
|
2019-04-28 16:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|