From 2c7b22db920239ca4845158a82e1688192602c95 Mon Sep 17 00:00:00 2001 From: Arceliar Date: Sun, 16 May 2021 13:01:54 -0500 Subject: [PATCH] allow for multiple traffic types inside the session at the tuntap level, only implement typeSessionTraffic for now --- src/tuntap/iface.go | 20 +++++++++++++++++--- src/tuntap/tun.go | 14 +++++++------- src/tuntap/types.go | 14 ++++++++++++++ 3 files changed, 38 insertions(+), 10 deletions(-) create mode 100644 src/tuntap/types.go diff --git a/src/tuntap/iface.go b/src/tuntap/iface.go index d2680d0d..ff91cf17 100644 --- a/src/tuntap/iface.go +++ b/src/tuntap/iface.go @@ -50,6 +50,8 @@ func (tun *TunAdapter) read() { if srcAddr != tun.addr && srcSubnet != tun.subnet { continue // Wrong soruce address } + bs = buf[begin-1 : end] + bs[0] = typeSessionTraffic if dstAddr.IsValid() { tun.store.sendToAddress(dstAddr, bs) } else if dstSubnet.IsValid() { @@ -61,12 +63,24 @@ func (tun *TunAdapter) read() { func (tun *TunAdapter) write() { var buf [TUN_OFFSET_BYTES + 65535]byte for { - bs := buf[TUN_OFFSET_BYTES:] + bs := buf[TUN_OFFSET_BYTES-1:] n, from, err := tun.core.ReadFrom(bs) if err != nil { return } - bs = bs[:n] + if n == 0 { + continue + } + switch bs[0] { + case typeSessionTraffic: + // This is what we want to handle here + default: + continue + } + bs = bs[1:n] + if len(bs) == 0 { + continue + } if bs[0]&0xf0 != 0x60 { continue // not IPv6 } @@ -99,7 +113,7 @@ func (tun *TunAdapter) write() { if srcAddr != info.address && srcSubnet != info.subnet { continue // bad remote address/subnet } - bs = buf[:TUN_OFFSET_BYTES+n] + bs = buf[:TUN_OFFSET_BYTES+len(bs)] n, err = tun.iface.Write(bs, TUN_OFFSET_BYTES) if err != nil { tun.Act(nil, func() { diff --git a/src/tuntap/tun.go b/src/tuntap/tun.go index 6d41f59b..4492784d 100644 --- a/src/tuntap/tun.go +++ b/src/tuntap/tun.go @@ -150,8 +150,8 @@ func (tun *TunAdapter) _start() error { return nil } mtu := current.IfMTU - if tun.core.MTU() < uint64(mtu) { - mtu = MTU(tun.core.MTU()) + if tun.maxSessionMTU() < mtu { + mtu = tun.maxSessionMTU() } if err := tun.setup(current.IfName, addr, mtu); err != nil { return err @@ -216,11 +216,6 @@ func (tun *TunAdapter) oobHandler(fromKey, toKey ed25519.PublicKey, data []byte) } } -const ( - typeKeyLookup = 1 - typeKeyResponse = 2 -) - func (tun *TunAdapter) sendKeyLookup(partial ed25519.PublicKey) { sig := ed25519.Sign(tun.core.PrivateKey(), partial[:]) bs := append([]byte{typeKeyLookup}, sig...) @@ -232,3 +227,8 @@ func (tun *TunAdapter) sendKeyResponse(dest ed25519.PublicKey) { bs := append([]byte{typeKeyResponse}, sig...) tun.core.SendOutOfBand(dest, bs) } + +func (tun *TunAdapter) maxSessionMTU() MTU { + const sessionTypeOverhead = 1 + return MTU(tun.core.MTU() - sessionTypeOverhead) +} diff --git a/src/tuntap/types.go b/src/tuntap/types.go new file mode 100644 index 00000000..b503e647 --- /dev/null +++ b/src/tuntap/types.go @@ -0,0 +1,14 @@ +package tuntap + +// Out-of-band packet types +const ( + typeKeyDummy = iota + typeKeyLookup + typeKeyResponse +) + +// In-band packet types +const ( + typeSessionDummy = iota + typeSessionTraffic +)