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
|
|
|
}
|
|
|
|
|
2019-07-17 20:42:17 +00:00
|
|
|
func (s *tunConn) reader() (err error) {
|
2019-04-28 16:14:09 +00:00
|
|
|
select {
|
|
|
|
case _, ok := <-s.stop:
|
|
|
|
if !ok {
|
|
|
|
return errors.New("session was already closed")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
2019-07-17 20:42:17 +00:00
|
|
|
s.tun.log.Debugln("Starting conn reader for", s.conn.String())
|
|
|
|
defer s.tun.log.Debugln("Stopping conn reader for", s.conn.String())
|
2019-07-17 09:12:10 +00:00
|
|
|
for {
|
2019-04-28 16:14:09 +00:00
|
|
|
select {
|
|
|
|
case <-s.stop:
|
|
|
|
return nil
|
2019-07-17 20:42:17 +00:00
|
|
|
default:
|
|
|
|
}
|
2019-08-04 20:59:51 +00:00
|
|
|
var bs []byte
|
|
|
|
if bs, err = s.conn.ReadNoCopy(); err != nil {
|
2019-07-17 20:42:17 +00:00
|
|
|
if e, eok := err.(yggdrasil.ConnError); eok && !e.Temporary() {
|
2019-07-22 21:41:55 +00:00
|
|
|
if e.Closed() {
|
|
|
|
s.tun.log.Debugln(s.conn.String(), "TUN/TAP conn read debug:", err)
|
|
|
|
} else {
|
|
|
|
s.tun.log.Errorln(s.conn.String(), "TUN/TAP conn read error:", err)
|
|
|
|
}
|
2019-07-17 20:42:17 +00:00
|
|
|
return e
|
|
|
|
}
|
2019-08-04 20:59:51 +00:00
|
|
|
} else if len(bs) > 0 {
|
|
|
|
s.tun.send <- bs
|
2019-07-17 20:42:17 +00:00
|
|
|
s.stillAlive()
|
2019-08-04 19:50:19 +00:00
|
|
|
} else {
|
2019-08-04 20:59:51 +00:00
|
|
|
util.PutBytes(bs)
|
2019-04-28 16:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *tunConn) writer() error {
|
|
|
|
select {
|
|
|
|
case _, ok := <-s.stop:
|
|
|
|
if !ok {
|
|
|
|
return errors.New("session was already closed")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
}
|
2019-07-17 20:42:17 +00:00
|
|
|
s.tun.log.Debugln("Starting conn writer for", s.conn.String())
|
|
|
|
defer s.tun.log.Debugln("Stopping conn writer for", s.conn.String())
|
2019-04-28 16:14:09 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.stop:
|
|
|
|
return nil
|
2019-08-04 20:59:51 +00:00
|
|
|
case bs, ok := <-s.send:
|
2019-04-28 16:14:09 +00:00
|
|
|
if !ok {
|
|
|
|
return errors.New("send closed")
|
|
|
|
}
|
2019-08-07 00:25:55 +00:00
|
|
|
msg := yggdrasil.FlowKeyMessage{
|
|
|
|
FlowKey: util.GetFlowKey(bs),
|
|
|
|
Message: bs,
|
|
|
|
}
|
|
|
|
if err := s.conn.WriteNoCopy(msg); err != nil {
|
2019-07-22 21:41:55 +00:00
|
|
|
if e, eok := err.(yggdrasil.ConnError); !eok {
|
|
|
|
if e.Closed() {
|
|
|
|
s.tun.log.Debugln(s.conn.String(), "TUN/TAP generic write debug:", err)
|
|
|
|
} else {
|
|
|
|
s.tun.log.Errorln(s.conn.String(), "TUN/TAP generic write error:", err)
|
|
|
|
}
|
2019-07-27 14:00:09 +00:00
|
|
|
} else if e.PacketTooBig() {
|
2019-05-29 19:16:17 +00:00
|
|
|
// TODO: This currently isn't aware of IPv4 for CKR
|
|
|
|
ptb := &icmp.PacketTooBig{
|
2019-07-27 14:00:09 +00:00
|
|
|
MTU: int(e.PacketMaximumSize()),
|
2019-08-04 20:59:51 +00:00
|
|
|
Data: bs[:900],
|
2019-05-29 19:16:17 +00:00
|
|
|
}
|
2019-08-04 20:59:51 +00:00
|
|
|
if packet, err := CreateICMPv6(bs[8:24], bs[24:40], ipv6.ICMPTypePacketTooBig, 0, ptb); err == nil {
|
2019-05-29 19:16:17 +00:00
|
|
|
s.tun.send <- packet
|
|
|
|
}
|
|
|
|
} else {
|
2019-07-22 21:41:55 +00:00
|
|
|
if e.Closed() {
|
|
|
|
s.tun.log.Debugln(s.conn.String(), "TUN/TAP conn write debug:", err)
|
|
|
|
} else {
|
|
|
|
s.tun.log.Errorln(s.conn.String(), "TUN/TAP conn write error:", err)
|
|
|
|
}
|
2019-05-29 19:16:17 +00:00
|
|
|
}
|
2019-07-17 22:23:19 +00:00
|
|
|
} else {
|
|
|
|
s.stillAlive()
|
2019-04-28 16:14:09 +00:00
|
|
|
}
|
2019-07-17 18:43:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *tunConn) stillAlive() {
|
2019-07-17 23:02:16 +00:00
|
|
|
defer func() { recover() }()
|
2019-07-17 18:43:29 +00:00
|
|
|
select {
|
|
|
|
case s.alive <- struct{}{}:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *tunConn) checkForTimeouts() error {
|
|
|
|
timer := time.NewTimer(tunConnTimeout)
|
|
|
|
defer util.TimerStop(timer)
|
|
|
|
defer s.close()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case _, ok := <-s.alive:
|
|
|
|
if !ok {
|
|
|
|
return errors.New("connection closed")
|
|
|
|
}
|
|
|
|
util.TimerStop(timer)
|
|
|
|
timer.Reset(tunConnTimeout)
|
|
|
|
case <-timer.C:
|
|
|
|
return errors.New("timed out")
|
2019-04-28 16:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|