mirror of
https://github.com/tailscale/tailscale.git
synced 2025-03-28 12:02:23 +00:00
net/tstun: simplify code
Calculate whether the packet is injected directly, rather than via an else branch. Unify the exit paths. It is easier here than duplicating them. Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
This commit is contained in:
parent
cc23049cd2
commit
a4cc7b6d54
@ -354,15 +354,13 @@ func (t *Wrapper) Read(buf []byte, offset int) (int, error) {
|
|||||||
defer allowSendOnClosedChannel() // for send to t.bufferConsumed
|
defer allowSendOnClosedChannel() // for send to t.bufferConsumed
|
||||||
pkt := res.data
|
pkt := res.data
|
||||||
n := copy(buf[offset:], pkt)
|
n := copy(buf[offset:], pkt)
|
||||||
wasInjectedPacket := false
|
// t.buffer has a fixed location in memory.
|
||||||
// t.buffer has a fixed location in memory,
|
// If the packet is not from t.buffer, then it is an injected packet.
|
||||||
// so this is the easiest way to tell when it has been consumed.
|
|
||||||
// &pkt[0] can be used because empty packets do not reach t.outbound.
|
// &pkt[0] can be used because empty packets do not reach t.outbound.
|
||||||
if &pkt[0] == &t.buffer[PacketStartOffset] {
|
isInjectedPacket := &pkt[0] != &t.buffer[PacketStartOffset]
|
||||||
|
if !isInjectedPacket {
|
||||||
|
// We are done with t.buffer. Let poll re-use it.
|
||||||
t.bufferConsumed <- struct{}{}
|
t.bufferConsumed <- struct{}{}
|
||||||
} else {
|
|
||||||
// If the packet is not from t.buffer, then it is an injected packet.
|
|
||||||
wasInjectedPacket = true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
p := parsedPacketPool.Get().(*packet.Parsed)
|
p := parsedPacketPool.Get().(*packet.Parsed)
|
||||||
@ -375,13 +373,8 @@ func (t *Wrapper) Read(buf []byte, offset int) (int, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// For injected packets, we return early to bypass filtering.
|
// Do not filter injected packets.
|
||||||
if wasInjectedPacket {
|
if !isInjectedPacket && !t.disableFilter {
|
||||||
t.noteActivity()
|
|
||||||
return n, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if !t.disableFilter {
|
|
||||||
response := t.filterOut(p)
|
response := t.filterOut(p)
|
||||||
if response != filter.Accept {
|
if response != filter.Accept {
|
||||||
// Wireguard considers read errors fatal; pretend nothing was read
|
// Wireguard considers read errors fatal; pretend nothing was read
|
||||||
|
Loading…
x
Reference in New Issue
Block a user