2020-05-13 14:16:17 +01:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Package tstun provides a TUN struct implementing the tun.Device interface
|
|
|
|
// with additional features as required by wgengine.
|
|
|
|
package tstun
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
2020-06-08 23:19:26 +01:00
|
|
|
"sync"
|
2020-05-13 14:16:17 +01:00
|
|
|
"sync/atomic"
|
2020-06-25 22:19:12 +01:00
|
|
|
"time"
|
2020-05-13 14:16:17 +01:00
|
|
|
|
|
|
|
"github.com/tailscale/wireguard-go/device"
|
|
|
|
"github.com/tailscale/wireguard-go/tun"
|
2020-12-20 00:43:25 +00:00
|
|
|
"inet.af/netaddr"
|
2020-11-10 00:16:04 +00:00
|
|
|
"tailscale.com/net/packet"
|
2021-03-21 04:45:47 +00:00
|
|
|
"tailscale.com/types/ipproto"
|
2020-05-13 14:16:17 +01:00
|
|
|
"tailscale.com/types/logger"
|
|
|
|
"tailscale.com/wgengine/filter"
|
|
|
|
)
|
|
|
|
|
2020-06-08 23:19:26 +01:00
|
|
|
const maxBufferSize = device.MaxMessageSize
|
|
|
|
|
|
|
|
// PacketStartOffset is the minimal amount of leading space that must exist
|
|
|
|
// before &packet[offset] in a packet passed to Read, Write, or InjectInboundDirect.
|
|
|
|
// This is necessary to avoid reallocation in wireguard-go internals.
|
|
|
|
const PacketStartOffset = device.MessageTransportHeaderSize
|
2020-05-13 14:16:17 +01:00
|
|
|
|
|
|
|
// MaxPacketSize is the maximum size (in bytes)
|
|
|
|
// of a packet that can be injected into a tstun.TUN.
|
|
|
|
const MaxPacketSize = device.MaxContentSize
|
|
|
|
|
|
|
|
var (
|
2020-05-26 23:14:19 +01:00
|
|
|
// ErrClosed is returned when attempting an operation on a closed TUN.
|
|
|
|
ErrClosed = errors.New("device closed")
|
|
|
|
// ErrFiltered is returned when the acted-on packet is rejected by a filter.
|
|
|
|
ErrFiltered = errors.New("packet dropped by filter")
|
2020-05-13 14:16:17 +01:00
|
|
|
)
|
|
|
|
|
2020-06-08 23:19:26 +01:00
|
|
|
var (
|
|
|
|
errPacketTooBig = errors.New("packet too big")
|
|
|
|
errOffsetTooBig = errors.New("offset larger than buffer length")
|
|
|
|
errOffsetTooSmall = errors.New("offset smaller than PacketStartOffset")
|
|
|
|
)
|
|
|
|
|
2020-11-10 07:49:09 +00:00
|
|
|
// parsedPacketPool holds a pool of Parsed structs for use in filtering.
|
2020-07-24 16:29:36 +01:00
|
|
|
// This is needed because escape analysis cannot see that parsed packets
|
|
|
|
// do not escape through {Pre,Post}Filter{In,Out}.
|
2020-11-10 07:49:09 +00:00
|
|
|
var parsedPacketPool = sync.Pool{New: func() interface{} { return new(packet.Parsed) }}
|
2020-07-24 16:29:36 +01:00
|
|
|
|
2020-06-08 23:19:26 +01:00
|
|
|
// FilterFunc is a packet-filtering function with access to the TUN device.
|
|
|
|
// It must not hold onto the packet struct, as its backing storage will be reused.
|
2020-11-10 07:49:09 +00:00
|
|
|
type FilterFunc func(*packet.Parsed, *TUN) filter.Response
|
2020-05-26 23:14:19 +01:00
|
|
|
|
2020-05-13 14:16:17 +01:00
|
|
|
// TUN wraps a tun.Device from wireguard-go,
|
|
|
|
// augmenting it with filtering and packet injection.
|
|
|
|
// All the added work happens in Read and Write:
|
|
|
|
// the other methods delegate to the underlying tdev.
|
|
|
|
type TUN struct {
|
|
|
|
logf logger.Logf
|
|
|
|
// tdev is the underlying TUN device.
|
|
|
|
tdev tun.Device
|
|
|
|
|
2020-09-18 16:03:10 +01:00
|
|
|
closeOnce sync.Once
|
|
|
|
|
2020-09-18 16:18:38 +01:00
|
|
|
lastActivityAtomic int64 // unix seconds of last send or receive
|
2020-06-25 22:19:12 +01:00
|
|
|
|
2020-12-20 00:43:25 +00:00
|
|
|
destIPActivity atomic.Value // of map[netaddr.IP]func()
|
2020-07-23 23:15:28 +01:00
|
|
|
|
2020-05-13 14:16:17 +01:00
|
|
|
// buffer stores the oldest unconsumed packet from tdev.
|
2020-06-08 23:19:26 +01:00
|
|
|
// It is made a static buffer in order to avoid allocations.
|
|
|
|
buffer [maxBufferSize]byte
|
2020-05-13 14:16:17 +01:00
|
|
|
// bufferConsumed synchronizes access to buffer (shared by Read and poll).
|
|
|
|
bufferConsumed chan struct{}
|
|
|
|
|
|
|
|
// closed signals poll (by closing) when the device is closed.
|
|
|
|
closed chan struct{}
|
|
|
|
// errors is the error queue populated by poll.
|
|
|
|
errors chan error
|
|
|
|
// outbound is the queue by which packets leave the TUN device.
|
2020-05-26 23:14:19 +01:00
|
|
|
//
|
2020-05-13 14:16:17 +01:00
|
|
|
// The directions are relative to the network, not the device:
|
|
|
|
// inbound packets arrive via UDP and are written into the TUN device;
|
|
|
|
// outbound packets are read from the TUN device and sent out via UDP.
|
|
|
|
// This queue is needed because although inbound writes are synchronous,
|
|
|
|
// the other direction must wait on a Wireguard goroutine to poll it.
|
2020-05-26 23:14:19 +01:00
|
|
|
//
|
|
|
|
// Empty reads are skipped by Wireguard, so it is always legal
|
|
|
|
// to discard an empty packet instead of sending it through t.outbound.
|
2020-05-13 14:16:17 +01:00
|
|
|
outbound chan []byte
|
|
|
|
|
|
|
|
// fitler stores the currently active package filter
|
|
|
|
filter atomic.Value // of *filter.Filter
|
|
|
|
// filterFlags control the verbosity of logging packet drops/accepts.
|
|
|
|
filterFlags filter.RunFlags
|
2020-06-05 16:19:03 +01:00
|
|
|
|
2020-06-08 23:19:26 +01:00
|
|
|
// PreFilterIn is the inbound filter function that runs before the main filter
|
|
|
|
// and therefore sees the packets that may be later dropped by it.
|
|
|
|
PreFilterIn FilterFunc
|
|
|
|
// PostFilterIn is the inbound filter function that runs after the main filter.
|
|
|
|
PostFilterIn FilterFunc
|
|
|
|
// PreFilterOut is the outbound filter function that runs before the main filter
|
|
|
|
// and therefore sees the packets that may be later dropped by it.
|
|
|
|
PreFilterOut FilterFunc
|
|
|
|
// PostFilterOut is the outbound filter function that runs after the main filter.
|
|
|
|
PostFilterOut FilterFunc
|
|
|
|
|
2021-03-23 22:16:15 +00:00
|
|
|
// OnTSMPPongReceived, if non-nil, is called whenever a TSMP pong arrives.
|
|
|
|
OnTSMPPongReceived func(data [8]byte)
|
|
|
|
|
2020-06-08 23:19:26 +01:00
|
|
|
// disableFilter disables all filtering when set. This should only be used in tests.
|
|
|
|
disableFilter bool
|
2020-05-13 14:16:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func WrapTUN(logf logger.Logf, tdev tun.Device) *TUN {
|
|
|
|
tun := &TUN{
|
2020-07-15 16:48:33 +01:00
|
|
|
logf: logger.WithPrefix(logf, "tstun: "),
|
2020-05-13 14:16:17 +01:00
|
|
|
tdev: tdev,
|
|
|
|
// bufferConsumed is conceptually a condition variable:
|
|
|
|
// a goroutine should not block when setting it, even with no listeners.
|
|
|
|
bufferConsumed: make(chan struct{}, 1),
|
|
|
|
closed: make(chan struct{}),
|
|
|
|
errors: make(chan error),
|
|
|
|
outbound: make(chan []byte),
|
2020-06-08 23:19:26 +01:00
|
|
|
// TODO(dmytro): (highly rate-limited) hexdumps should happen on unknown packets.
|
|
|
|
filterFlags: filter.LogAccepts | filter.LogDrops,
|
2020-05-13 14:16:17 +01:00
|
|
|
}
|
2020-06-08 23:19:26 +01:00
|
|
|
|
2020-05-13 14:16:17 +01:00
|
|
|
go tun.poll()
|
|
|
|
// The buffer starts out consumed.
|
|
|
|
tun.bufferConsumed <- struct{}{}
|
|
|
|
|
|
|
|
return tun
|
|
|
|
}
|
|
|
|
|
2020-07-23 23:15:28 +01:00
|
|
|
// SetDestIPActivityFuncs sets a map of funcs to run per packet
|
|
|
|
// destination (the map keys).
|
|
|
|
//
|
|
|
|
// The map ownership passes to the TUN. It must be non-nil.
|
2020-12-20 00:43:25 +00:00
|
|
|
func (t *TUN) SetDestIPActivityFuncs(m map[netaddr.IP]func()) {
|
|
|
|
t.destIPActivity.Store(m)
|
2020-07-23 23:15:28 +01:00
|
|
|
}
|
|
|
|
|
2020-05-13 14:16:17 +01:00
|
|
|
func (t *TUN) Close() error {
|
2020-09-18 16:03:10 +01:00
|
|
|
var err error
|
|
|
|
t.closeOnce.Do(func() {
|
2020-05-13 14:16:17 +01:00
|
|
|
// Other channels need not be closed: poll will exit gracefully after this.
|
|
|
|
close(t.closed)
|
|
|
|
|
2020-09-18 16:03:10 +01:00
|
|
|
err = t.tdev.Close()
|
|
|
|
})
|
|
|
|
return err
|
2020-05-13 14:16:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUN) Events() chan tun.Event {
|
|
|
|
return t.tdev.Events()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUN) File() *os.File {
|
|
|
|
return t.tdev.File()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUN) Flush() error {
|
|
|
|
return t.tdev.Flush()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUN) MTU() (int, error) {
|
|
|
|
return t.tdev.MTU()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUN) Name() (string, error) {
|
|
|
|
return t.tdev.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
// poll polls t.tdev.Read, placing the oldest unconsumed packet into t.buffer.
|
|
|
|
// This is needed because t.tdev.Read in general may block (it does on Windows),
|
|
|
|
// so packets may be stuck in t.outbound if t.Read called t.tdev.Read directly.
|
|
|
|
func (t *TUN) poll() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.closed:
|
|
|
|
return
|
|
|
|
case <-t.bufferConsumed:
|
|
|
|
// continue
|
|
|
|
}
|
|
|
|
|
2020-06-08 23:19:26 +01:00
|
|
|
// Read may use memory in t.buffer before PacketStartOffset for mandatory headers.
|
2020-05-13 14:16:17 +01:00
|
|
|
// This is the rationale behind the tun.TUN.{Read,Write} interfaces
|
|
|
|
// and the reason t.buffer has size MaxMessageSize and not MaxContentSize.
|
2020-06-08 23:19:26 +01:00
|
|
|
n, err := t.tdev.Read(t.buffer[:], PacketStartOffset)
|
2020-05-13 14:16:17 +01:00
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case <-t.closed:
|
|
|
|
return
|
|
|
|
case t.errors <- err:
|
|
|
|
// In principle, read errors are not fatal (but wireguard-go disagrees).
|
|
|
|
t.bufferConsumed <- struct{}{}
|
|
|
|
}
|
2020-05-26 23:14:19 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wireguard will skip an empty read,
|
|
|
|
// so we might as well do it here to avoid the send through t.outbound.
|
|
|
|
if n == 0 {
|
|
|
|
t.bufferConsumed <- struct{}{}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-t.closed:
|
|
|
|
return
|
2020-06-08 23:19:26 +01:00
|
|
|
case t.outbound <- t.buffer[PacketStartOffset : PacketStartOffset+n]:
|
2020-05-26 23:14:19 +01:00
|
|
|
// continue
|
2020-05-13 14:16:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-11 19:51:59 +00:00
|
|
|
var magicDNSIPPort = netaddr.MustParseIPPort("100.100.100.100:0")
|
|
|
|
|
2020-11-10 07:49:09 +00:00
|
|
|
func (t *TUN) filterOut(p *packet.Parsed) filter.Response {
|
2021-02-11 19:51:59 +00:00
|
|
|
// Fake ICMP echo responses to MagicDNS (100.100.100.100).
|
|
|
|
if p.IsEchoRequest() && p.Dst == magicDNSIPPort {
|
|
|
|
header := p.ICMP4Header()
|
|
|
|
header.ToResponse()
|
|
|
|
outp := packet.Generate(&header, p.Payload())
|
|
|
|
t.InjectInboundCopy(outp)
|
|
|
|
return filter.DropSilently // don't pass on to OS; already handled
|
|
|
|
}
|
2020-06-08 23:19:26 +01:00
|
|
|
|
|
|
|
if t.PreFilterOut != nil {
|
2021-01-12 20:03:41 +00:00
|
|
|
if res := t.PreFilterOut(p, t); res.IsDrop() {
|
|
|
|
return res
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 14:16:17 +01:00
|
|
|
filt, _ := t.filter.Load().(*filter.Filter)
|
|
|
|
|
|
|
|
if filt == nil {
|
|
|
|
return filter.Drop
|
|
|
|
}
|
|
|
|
|
2020-06-08 23:19:26 +01:00
|
|
|
if filt.RunOut(p, t.filterFlags) != filter.Accept {
|
|
|
|
return filter.Drop
|
2020-05-13 14:16:17 +01:00
|
|
|
}
|
2020-06-04 23:42:44 +01:00
|
|
|
|
2020-06-08 23:19:26 +01:00
|
|
|
if t.PostFilterOut != nil {
|
2021-01-12 20:03:41 +00:00
|
|
|
if res := t.PostFilterOut(p, t); res.IsDrop() {
|
|
|
|
return res
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filter.Accept
|
2020-05-13 14:16:17 +01:00
|
|
|
}
|
|
|
|
|
2020-06-25 22:19:12 +01:00
|
|
|
// noteActivity records that there was a read or write at the current time.
|
|
|
|
func (t *TUN) noteActivity() {
|
|
|
|
atomic.StoreInt64(&t.lastActivityAtomic, time.Now().Unix())
|
|
|
|
}
|
|
|
|
|
|
|
|
// IdleDuration reports how long it's been since the last read or write to this device.
|
|
|
|
//
|
|
|
|
// Its value is only accurate to roughly second granularity.
|
|
|
|
// If there's never been activity, the duration is since 1970.
|
|
|
|
func (t *TUN) IdleDuration() time.Duration {
|
|
|
|
sec := atomic.LoadInt64(&t.lastActivityAtomic)
|
|
|
|
return time.Since(time.Unix(sec, 0))
|
|
|
|
}
|
|
|
|
|
2020-05-13 14:16:17 +01:00
|
|
|
func (t *TUN) Read(buf []byte, offset int) (int, error) {
|
|
|
|
var n int
|
|
|
|
|
2021-01-26 21:25:42 +00:00
|
|
|
wasInjectedPacket := false
|
|
|
|
|
2020-05-13 14:16:17 +01:00
|
|
|
select {
|
|
|
|
case <-t.closed:
|
|
|
|
return 0, io.EOF
|
|
|
|
case err := <-t.errors:
|
|
|
|
return 0, err
|
2021-01-12 20:03:41 +00:00
|
|
|
case pkt := <-t.outbound:
|
|
|
|
n = copy(buf[offset:], pkt)
|
2020-05-13 14:16:17 +01:00
|
|
|
// t.buffer has a fixed location in memory,
|
|
|
|
// so this is the easiest way to tell when it has been consumed.
|
2021-01-12 20:03:41 +00:00
|
|
|
// &pkt[0] can be used because empty packets do not reach t.outbound.
|
|
|
|
if &pkt[0] == &t.buffer[PacketStartOffset] {
|
2020-05-13 14:16:17 +01:00
|
|
|
t.bufferConsumed <- struct{}{}
|
2020-06-08 23:19:26 +01:00
|
|
|
} else {
|
|
|
|
// If the packet is not from t.buffer, then it is an injected packet.
|
2021-01-26 21:25:42 +00:00
|
|
|
wasInjectedPacket = true
|
2020-05-13 14:16:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 07:49:09 +00:00
|
|
|
p := parsedPacketPool.Get().(*packet.Parsed)
|
2020-07-23 23:15:28 +01:00
|
|
|
defer parsedPacketPool.Put(p)
|
|
|
|
p.Decode(buf[offset : offset+n])
|
|
|
|
|
2020-12-20 00:43:25 +00:00
|
|
|
if m, ok := t.destIPActivity.Load().(map[netaddr.IP]func()); ok {
|
|
|
|
if fn := m[p.Dst.IP]; fn != nil {
|
|
|
|
fn()
|
2020-07-23 23:15:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-26 21:25:42 +00:00
|
|
|
// For injected packets, we return early to bypass filtering.
|
|
|
|
if wasInjectedPacket {
|
|
|
|
t.noteActivity()
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
2020-06-08 23:19:26 +01:00
|
|
|
if !t.disableFilter {
|
2020-07-23 23:15:28 +01:00
|
|
|
response := t.filterOut(p)
|
2020-06-05 16:19:03 +01:00
|
|
|
if response != filter.Accept {
|
|
|
|
// Wireguard considers read errors fatal; pretend nothing was read
|
|
|
|
return 0, nil
|
|
|
|
}
|
2020-05-13 14:16:17 +01:00
|
|
|
}
|
|
|
|
|
2020-06-25 22:19:12 +01:00
|
|
|
t.noteActivity()
|
2020-05-13 14:16:17 +01:00
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUN) filterIn(buf []byte) filter.Response {
|
2020-11-10 07:49:09 +00:00
|
|
|
p := parsedPacketPool.Get().(*packet.Parsed)
|
2020-07-24 16:29:36 +01:00
|
|
|
defer parsedPacketPool.Put(p)
|
2020-06-08 23:19:26 +01:00
|
|
|
p.Decode(buf)
|
|
|
|
|
2021-03-23 22:16:15 +00:00
|
|
|
if p.IPProto == ipproto.TSMP {
|
|
|
|
if pingReq, ok := p.AsTSMPPing(); ok {
|
|
|
|
t.noteActivity()
|
|
|
|
t.injectOutboundPong(p, pingReq)
|
|
|
|
return filter.DropSilently
|
|
|
|
} else if data, ok := p.AsTSMPPong(); ok {
|
|
|
|
if f := t.OnTSMPPongReceived; f != nil {
|
|
|
|
f(data)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 23:19:26 +01:00
|
|
|
if t.PreFilterIn != nil {
|
2021-01-12 20:03:41 +00:00
|
|
|
if res := t.PreFilterIn(p, t); res.IsDrop() {
|
|
|
|
return res
|
2020-06-08 23:19:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-13 14:16:17 +01:00
|
|
|
filt, _ := t.filter.Load().(*filter.Filter)
|
|
|
|
|
|
|
|
if filt == nil {
|
|
|
|
return filter.Drop
|
|
|
|
}
|
|
|
|
|
2020-06-08 23:19:26 +01:00
|
|
|
if filt.RunIn(p, t.filterFlags) != filter.Accept {
|
2021-01-12 20:03:41 +00:00
|
|
|
|
|
|
|
// Tell them, via TSMP, we're dropping them due to the ACL.
|
|
|
|
// Their host networking stack can translate this into ICMP
|
|
|
|
// or whatnot as required. But notably, their GUI or tailscale CLI
|
|
|
|
// can show them a rejection history with reasons.
|
2021-03-21 04:45:47 +00:00
|
|
|
if p.IPVersion == 4 && p.IPProto == ipproto.TCP && p.TCPFlags&packet.TCPSyn != 0 {
|
2021-01-12 20:03:41 +00:00
|
|
|
rj := packet.TailscaleRejectedHeader{
|
|
|
|
IPSrc: p.Dst.IP,
|
|
|
|
IPDst: p.Src.IP,
|
|
|
|
Src: p.Src,
|
|
|
|
Dst: p.Dst,
|
|
|
|
Proto: p.IPProto,
|
|
|
|
Reason: packet.RejectedDueToACLs,
|
|
|
|
}
|
|
|
|
if filt.ShieldsUp() {
|
|
|
|
rj.Reason = packet.RejectedDueToShieldsUp
|
|
|
|
}
|
|
|
|
pkt := packet.Generate(rj, nil)
|
|
|
|
t.InjectOutbound(pkt)
|
|
|
|
|
|
|
|
// TODO(bradfitz): also send a TCP RST, after the TSMP message.
|
|
|
|
}
|
|
|
|
|
2020-06-08 23:19:26 +01:00
|
|
|
return filter.Drop
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.PostFilterIn != nil {
|
2021-01-22 21:22:32 +00:00
|
|
|
if res := t.PostFilterIn(p, t); res.IsDrop() {
|
|
|
|
return res
|
2020-05-13 14:16:17 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-04 23:42:44 +01:00
|
|
|
|
2020-06-08 23:19:26 +01:00
|
|
|
return filter.Accept
|
2020-05-13 14:16:17 +01:00
|
|
|
}
|
|
|
|
|
2021-01-12 20:03:41 +00:00
|
|
|
// Write accepts an incoming packet. The packet begins at buf[offset:],
|
|
|
|
// like wireguard-go/tun.Device.Write.
|
2020-05-13 14:16:17 +01:00
|
|
|
func (t *TUN) Write(buf []byte, offset int) (int, error) {
|
2020-06-08 23:19:26 +01:00
|
|
|
if !t.disableFilter {
|
2021-01-12 20:03:41 +00:00
|
|
|
res := t.filterIn(buf[offset:])
|
|
|
|
if res == filter.DropSilently {
|
|
|
|
return len(buf), nil
|
|
|
|
}
|
|
|
|
if res != filter.Accept {
|
2020-06-05 16:19:03 +01:00
|
|
|
return 0, ErrFiltered
|
|
|
|
}
|
2020-05-13 14:16:17 +01:00
|
|
|
}
|
|
|
|
|
2020-06-25 22:19:12 +01:00
|
|
|
t.noteActivity()
|
2020-05-13 14:16:17 +01:00
|
|
|
return t.tdev.Write(buf, offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUN) GetFilter() *filter.Filter {
|
|
|
|
filt, _ := t.filter.Load().(*filter.Filter)
|
|
|
|
return filt
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TUN) SetFilter(filt *filter.Filter) {
|
|
|
|
t.filter.Store(filt)
|
|
|
|
}
|
|
|
|
|
2020-06-08 23:19:26 +01:00
|
|
|
// InjectInboundDirect makes the TUN device behave as if a packet
|
2020-05-13 14:16:17 +01:00
|
|
|
// with the given contents was received from the network.
|
|
|
|
// It blocks and does not take ownership of the packet.
|
2020-06-08 23:19:26 +01:00
|
|
|
// The injected packet will not pass through inbound filters.
|
|
|
|
//
|
|
|
|
// The packet contents are to start at &buf[offset].
|
|
|
|
// offset must be greater or equal to PacketStartOffset.
|
|
|
|
// The space before &buf[offset] will be used by Wireguard.
|
|
|
|
func (t *TUN) InjectInboundDirect(buf []byte, offset int) error {
|
|
|
|
if len(buf) > MaxPacketSize {
|
|
|
|
return errPacketTooBig
|
|
|
|
}
|
|
|
|
if len(buf) < offset {
|
|
|
|
return errOffsetTooBig
|
|
|
|
}
|
|
|
|
if offset < PacketStartOffset {
|
|
|
|
return errOffsetTooSmall
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write to the underlying device to skip filters.
|
|
|
|
_, err := t.tdev.Write(buf, offset)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// InjectInboundCopy takes a packet without leading space,
|
2020-09-25 20:24:44 +01:00
|
|
|
// reallocates it to conform to the InjectInboundDirect interface
|
2020-06-08 23:19:26 +01:00
|
|
|
// and calls InjectInboundDirect on it. Injecting a nil packet is a no-op.
|
|
|
|
func (t *TUN) InjectInboundCopy(packet []byte) error {
|
|
|
|
// We duplicate this check from InjectInboundDirect here
|
|
|
|
// to avoid wasting an allocation on an oversized packet.
|
2020-05-13 14:16:17 +01:00
|
|
|
if len(packet) > MaxPacketSize {
|
2020-05-26 23:14:19 +01:00
|
|
|
return errPacketTooBig
|
|
|
|
}
|
|
|
|
if len(packet) == 0 {
|
|
|
|
return nil
|
2020-05-13 14:16:17 +01:00
|
|
|
}
|
2020-06-08 23:19:26 +01:00
|
|
|
|
|
|
|
buf := make([]byte, PacketStartOffset+len(packet))
|
|
|
|
copy(buf[PacketStartOffset:], packet)
|
|
|
|
|
|
|
|
return t.InjectInboundDirect(buf, PacketStartOffset)
|
2020-05-13 14:16:17 +01:00
|
|
|
}
|
|
|
|
|
2021-03-23 22:16:15 +00:00
|
|
|
func (t *TUN) injectOutboundPong(pp *packet.Parsed, req packet.TSMPPingRequest) {
|
|
|
|
pong := packet.TSMPPongReply{
|
|
|
|
Data: req.Data,
|
|
|
|
}
|
|
|
|
switch pp.IPVersion {
|
|
|
|
case 4:
|
|
|
|
h4 := pp.IP4Header()
|
|
|
|
h4.ToResponse()
|
|
|
|
pong.IPHeader = h4
|
|
|
|
case 6:
|
|
|
|
h6 := pp.IP6Header()
|
|
|
|
h6.ToResponse()
|
|
|
|
pong.IPHeader = h6
|
|
|
|
default:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.InjectOutbound(packet.Generate(pong, nil))
|
|
|
|
}
|
|
|
|
|
2020-05-13 14:16:17 +01:00
|
|
|
// InjectOutbound makes the TUN device behave as if a packet
|
|
|
|
// with the given contents was sent to the network.
|
|
|
|
// It does not block, but takes ownership of the packet.
|
2020-06-08 23:19:26 +01:00
|
|
|
// The injected packet will not pass through outbound filters.
|
2020-05-26 23:14:19 +01:00
|
|
|
// Injecting an empty packet is a no-op.
|
2020-05-13 14:16:17 +01:00
|
|
|
func (t *TUN) InjectOutbound(packet []byte) error {
|
|
|
|
if len(packet) > MaxPacketSize {
|
2020-05-26 23:14:19 +01:00
|
|
|
return errPacketTooBig
|
|
|
|
}
|
|
|
|
if len(packet) == 0 {
|
|
|
|
return nil
|
2020-05-13 14:16:17 +01:00
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-t.closed:
|
|
|
|
return ErrClosed
|
|
|
|
case t.outbound <- packet:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2020-05-15 08:06:30 +01:00
|
|
|
|
|
|
|
// Unwrap returns the underlying TUN device.
|
|
|
|
func (t *TUN) Unwrap() tun.Device {
|
|
|
|
return t.tdev
|
|
|
|
}
|