2019-04-28 16:14:09 +00:00
|
|
|
package tuntap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/address"
|
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
|
2019-10-22 00:44:06 +00:00
|
|
|
"github.com/yggdrasil-network/yggdrasil-go/src/yggdrasil"
|
2019-08-25 23:08:43 +00:00
|
|
|
|
2020-12-06 19:47:25 +00:00
|
|
|
"golang.org/x/net/icmp"
|
|
|
|
"golang.org/x/net/ipv6"
|
|
|
|
|
2019-08-25 23:08:43 +00:00
|
|
|
"github.com/Arceliar/phony"
|
2019-04-28 16:14:09 +00:00
|
|
|
)
|
|
|
|
|
2019-11-22 16:43:50 +00:00
|
|
|
const TUN_OFFSET_BYTES = 4
|
|
|
|
|
2019-08-25 23:08:43 +00:00
|
|
|
type tunWriter struct {
|
|
|
|
phony.Inbox
|
|
|
|
tun *TunAdapter
|
2020-05-02 11:44:51 +00:00
|
|
|
buf [TUN_OFFSET_BYTES + 65536]byte
|
2019-08-25 23:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *tunWriter) writeFrom(from phony.Actor, b []byte) {
|
2019-08-28 00:43:54 +00:00
|
|
|
w.Act(from, func() {
|
2019-08-25 23:08:43 +00:00
|
|
|
w._write(b)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-22 16:43:50 +00:00
|
|
|
// write is pretty loose with the memory safety rules, e.g. it assumes it can
|
|
|
|
// read w.tun.iface.IsTap() safely
|
2019-08-25 23:08:43 +00:00
|
|
|
func (w *tunWriter) _write(b []byte) {
|
|
|
|
var written int
|
2019-04-28 16:14:09 +00:00
|
|
|
var err error
|
2019-08-25 23:08:43 +00:00
|
|
|
n := len(b)
|
|
|
|
if n == 0 {
|
|
|
|
return
|
|
|
|
}
|
2020-05-02 11:44:51 +00:00
|
|
|
temp := append(w.buf[:TUN_OFFSET_BYTES], b...)
|
2019-11-28 18:00:00 +00:00
|
|
|
written, err = w.tun.iface.Write(temp, TUN_OFFSET_BYTES)
|
2019-08-25 23:08:43 +00:00
|
|
|
if err != nil {
|
2019-08-28 00:43:54 +00:00
|
|
|
w.tun.Act(w, func() {
|
2019-08-25 23:53:11 +00:00
|
|
|
if !w.tun.isOpen {
|
2019-11-22 16:43:50 +00:00
|
|
|
w.tun.log.Errorln("TUN iface write error:", err)
|
2019-08-25 23:53:11 +00:00
|
|
|
}
|
|
|
|
})
|
2019-08-25 23:08:43 +00:00
|
|
|
}
|
2019-11-24 21:01:20 +00:00
|
|
|
if written != n+TUN_OFFSET_BYTES {
|
2019-11-24 21:37:37 +00:00
|
|
|
// FIXME some platforms return the wrong number of bytes written, causing error spam
|
|
|
|
//w.tun.log.Errorln("TUN iface write mismatch:", written, "bytes written vs", n+TUN_OFFSET_BYTES, "bytes given")
|
2019-04-28 16:14:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-25 23:08:43 +00:00
|
|
|
type tunReader struct {
|
|
|
|
phony.Inbox
|
|
|
|
tun *TunAdapter
|
2020-05-02 11:44:51 +00:00
|
|
|
buf [TUN_OFFSET_BYTES + 65536]byte
|
2019-08-25 23:08:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *tunReader) _read() {
|
|
|
|
// Get a slice to store the packet in
|
2019-11-22 16:43:50 +00:00
|
|
|
// Wait for a packet to be delivered to us through the TUN adapter
|
2020-05-02 11:44:51 +00:00
|
|
|
n, err := r.tun.iface.Read(r.buf[:], TUN_OFFSET_BYTES)
|
2019-11-22 16:43:50 +00:00
|
|
|
if n <= TUN_OFFSET_BYTES || err != nil {
|
|
|
|
r.tun.log.Errorln("Error reading TUN:", err)
|
2019-11-24 19:42:56 +00:00
|
|
|
ferr := r.tun.iface.Flush()
|
|
|
|
if ferr != nil {
|
|
|
|
r.tun.log.Errorln("Unable to flush packets:", ferr)
|
2019-11-22 16:43:50 +00:00
|
|
|
}
|
2019-08-25 23:08:43 +00:00
|
|
|
} else {
|
2020-05-02 11:44:51 +00:00
|
|
|
bs := make([]byte, n, n+crypto.BoxOverhead) // extra capacity for later...
|
|
|
|
copy(bs, r.buf[TUN_OFFSET_BYTES:n+TUN_OFFSET_BYTES])
|
|
|
|
r.tun.handlePacketFrom(r, bs, err)
|
2019-08-25 23:08:43 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
// Now read again
|
2019-08-28 00:43:54 +00:00
|
|
|
r.Act(nil, r._read)
|
2019-08-25 23:08:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tun *TunAdapter) handlePacketFrom(from phony.Actor, packet []byte, err error) {
|
2019-08-28 00:43:54 +00:00
|
|
|
tun.Act(from, func() {
|
2019-08-25 23:08:43 +00:00
|
|
|
tun._handlePacket(packet, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// does the work of reading a packet and sending it to the correct tunConn
|
|
|
|
func (tun *TunAdapter) _handlePacket(recvd []byte, err error) {
|
|
|
|
if err != nil {
|
2019-11-22 16:43:50 +00:00
|
|
|
tun.log.Errorln("TUN iface read error:", err)
|
2019-08-25 23:08:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Offset the buffer from now on so that we can ignore ethernet frames if
|
|
|
|
// they are present
|
2019-11-22 16:43:50 +00:00
|
|
|
bs := recvd[:]
|
2019-09-26 23:11:58 +00:00
|
|
|
// Check if the packet is long enough to detect if it's an ICMP packet or not
|
|
|
|
if len(bs) < 7 {
|
2019-11-22 16:43:50 +00:00
|
|
|
tun.log.Traceln("TUN iface read undersized unknown packet, length:", len(bs))
|
2019-09-26 23:11:58 +00:00
|
|
|
return
|
|
|
|
}
|
2019-08-25 23:08:43 +00:00
|
|
|
// From the IP header, work out what our source and destination addresses
|
|
|
|
// and node IDs are. We will need these in order to work out where to send
|
|
|
|
// the packet
|
|
|
|
var dstAddr address.Address
|
|
|
|
var dstSnet address.Subnet
|
|
|
|
var addrlen int
|
|
|
|
n := len(bs)
|
|
|
|
// Check the IP protocol - if it doesn't match then we drop the packet and
|
|
|
|
// do nothing with it
|
|
|
|
if bs[0]&0xf0 == 0x60 {
|
|
|
|
// Check if we have a fully-sized IPv6 header
|
|
|
|
if len(bs) < 40 {
|
2019-11-22 16:43:50 +00:00
|
|
|
tun.log.Traceln("TUN iface read undersized ipv6 packet, length:", len(bs))
|
2019-08-25 23:08:43 +00:00
|
|
|
return
|
2019-08-04 19:50:19 +00:00
|
|
|
}
|
2019-08-25 23:08:43 +00:00
|
|
|
// Check the packet size
|
|
|
|
if n-tun_IPv6_HEADER_LENGTH != 256*int(bs[4])+int(bs[5]) {
|
|
|
|
return
|
2019-07-29 04:33:04 +00:00
|
|
|
}
|
2019-08-25 23:08:43 +00:00
|
|
|
// IPv6 address
|
|
|
|
addrlen = 16
|
|
|
|
copy(dstAddr[:addrlen], bs[24:])
|
|
|
|
copy(dstSnet[:addrlen/2], bs[24:])
|
|
|
|
} else if bs[0]&0xf0 == 0x40 {
|
|
|
|
// Check if we have a fully-sized IPv4 header
|
|
|
|
if len(bs) < 20 {
|
2019-11-22 16:43:50 +00:00
|
|
|
tun.log.Traceln("TUN iface read undersized ipv4 packet, length:", len(bs))
|
2019-08-25 23:08:43 +00:00
|
|
|
return
|
2019-08-20 23:10:08 +00:00
|
|
|
}
|
2019-08-25 23:08:43 +00:00
|
|
|
// Check the packet size
|
|
|
|
if n != 256*int(bs[2])+int(bs[3]) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// IPv4 address
|
|
|
|
addrlen = 4
|
|
|
|
copy(dstAddr[:addrlen], bs[16:])
|
|
|
|
} else {
|
|
|
|
// Unknown address length or protocol, so drop the packet and ignore it
|
|
|
|
tun.log.Traceln("Unknown packet type, dropping")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if tun.ckr.isEnabled() {
|
2019-08-20 23:10:08 +00:00
|
|
|
if addrlen != 16 || (!dstAddr.IsValid() && !dstSnet.IsValid()) {
|
2019-08-25 23:08:43 +00:00
|
|
|
if key, err := tun.ckr.getPublicKeyForAddress(dstAddr, addrlen); err == nil {
|
|
|
|
// A public key was found, get the node ID for the search
|
|
|
|
dstNodeID := crypto.GetNodeID(&key)
|
|
|
|
dstAddr = *address.AddrForNodeID(dstNodeID)
|
|
|
|
dstSnet = *address.SubnetForNodeID(dstNodeID)
|
|
|
|
addrlen = 16
|
|
|
|
}
|
2019-07-29 04:33:04 +00:00
|
|
|
}
|
2019-08-25 23:08:43 +00:00
|
|
|
}
|
|
|
|
if addrlen != 16 || (!dstAddr.IsValid() && !dstSnet.IsValid()) {
|
|
|
|
// Couldn't find this node's ygg IP
|
2020-12-06 19:47:25 +00:00
|
|
|
dlen := len(bs)
|
|
|
|
if dlen > 900 {
|
|
|
|
dlen = 900
|
|
|
|
}
|
|
|
|
ptb := &icmp.DstUnreach{
|
|
|
|
Data: bs[:dlen],
|
|
|
|
}
|
|
|
|
if packet, err := CreateICMPv6(bs[8:24], bs[24:40], ipv6.ICMPTypeDestinationUnreachable, 0, ptb); err == nil {
|
|
|
|
tun.writer.writeFrom(nil, packet)
|
|
|
|
}
|
2019-08-25 23:08:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Do we have an active connection for this node address?
|
2019-10-22 00:44:06 +00:00
|
|
|
var dstString string
|
2019-08-25 23:08:43 +00:00
|
|
|
session, isIn := tun.addrToConn[dstAddr]
|
|
|
|
if !isIn || session == nil {
|
|
|
|
session, isIn = tun.subnetToConn[dstSnet]
|
2019-07-29 04:33:04 +00:00
|
|
|
if !isIn || session == nil {
|
2019-08-25 23:08:43 +00:00
|
|
|
// Neither an address nor a subnet mapping matched, therefore populate
|
|
|
|
// the node ID and mask to commence a search
|
|
|
|
if dstAddr.IsValid() {
|
2019-10-22 00:44:06 +00:00
|
|
|
dstString = dstAddr.GetNodeIDLengthString()
|
2019-08-25 23:08:43 +00:00
|
|
|
} else {
|
2019-10-22 00:44:06 +00:00
|
|
|
dstString = dstSnet.GetNodeIDLengthString()
|
2019-07-28 01:09:43 +00:00
|
|
|
}
|
2019-07-29 04:33:04 +00:00
|
|
|
}
|
2019-08-25 23:08:43 +00:00
|
|
|
}
|
|
|
|
// If we don't have a connection then we should open one
|
|
|
|
if !isIn || session == nil {
|
|
|
|
// Check we haven't been given empty node ID, really this shouldn't ever
|
|
|
|
// happen but just to be sure...
|
2019-10-22 00:44:06 +00:00
|
|
|
if dstString == "" {
|
|
|
|
panic("Given empty dstString - this shouldn't happen")
|
2019-08-25 23:08:43 +00:00
|
|
|
}
|
2019-10-22 00:44:06 +00:00
|
|
|
_, known := tun.dials[dstString]
|
|
|
|
tun.dials[dstString] = append(tun.dials[dstString], bs)
|
|
|
|
for len(tun.dials[dstString]) > 32 {
|
|
|
|
tun.dials[dstString] = tun.dials[dstString][1:]
|
2019-08-25 23:53:11 +00:00
|
|
|
}
|
|
|
|
if !known {
|
|
|
|
go func() {
|
2019-10-22 00:44:06 +00:00
|
|
|
conn, err := tun.dialer.Dial("nodeid", dstString)
|
2019-08-28 00:43:54 +00:00
|
|
|
tun.Act(nil, func() {
|
2019-10-22 00:44:06 +00:00
|
|
|
packets := tun.dials[dstString]
|
|
|
|
delete(tun.dials, dstString)
|
2019-08-26 05:38:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// We've been given a connection so prepare the session wrapper
|
|
|
|
var tc *tunConn
|
2019-10-22 00:44:06 +00:00
|
|
|
if tc, err = tun._wrap(conn.(*yggdrasil.Conn)); err != nil {
|
2019-08-26 05:38:14 +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 iface wrap:", err)
|
2019-08-26 05:38:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, packet := range packets {
|
|
|
|
tc.writeFrom(nil, packet)
|
|
|
|
}
|
|
|
|
})
|
2019-08-25 23:53:11 +00:00
|
|
|
}()
|
|
|
|
}
|
2019-08-25 23:08:43 +00:00
|
|
|
}
|
|
|
|
// If we have a connection now, try writing to it
|
|
|
|
if isIn && session != nil {
|
2019-08-25 23:53:11 +00:00
|
|
|
session.writeFrom(tun, bs)
|
2019-04-28 16:14:09 +00:00
|
|
|
}
|
|
|
|
}
|