mirror of
https://github.com/slackhq/nebula.git
synced 2026-11-16 21:16:52 +01:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1519dbbf8e | ||
|
|
d576827ade |
No files matched your search
@@ -457,6 +457,9 @@ firewall:
|
|||||||
# Rules are comprised of a protocol, port, and one or more of host, group, or CIDR
|
# Rules are comprised of a protocol, port, and one or more of host, group, or CIDR
|
||||||
# Logical evaluation is roughly: port AND proto AND (ca_sha OR ca_name) AND (host OR group OR groups OR cidr) AND (local cidr)
|
# Logical evaluation is roughly: port AND proto AND (ca_sha OR ca_name) AND (host OR group OR groups OR cidr) AND (local cidr)
|
||||||
# - port: Takes `0` or `any` as any, a single number `80`, a range `200-901`, or `fragment` to match second and further fragments of fragmented packets (since there is no port available).
|
# - port: Takes `0` or `any` as any, a single number `80`, a range `200-901`, or `fragment` to match second and further fragments of fragmented packets (since there is no port available).
|
||||||
|
# For IPv6, Nebula can't tell which protocol a second or further fragment belongs to. Any rule that allows them (port `any` or `fragment`,
|
||||||
|
# and every icmp rule, since icmp ignores ports) allows them for every protocol, not just the rule's. The first fragment, which carries
|
||||||
|
# the ports, is always checked against its real protocol and port, so this never opens a port the rules don't allow.
|
||||||
# proto: `any`, `tcp`, `udp`, or `icmp`
|
# proto: `any`, `tcp`, `udp`, or `icmp`
|
||||||
# a port specification is ignored if proto is `icmp`
|
# a port specification is ignored if proto is `icmp`
|
||||||
# host: `any` or a literal hostname, ie `test-host`
|
# host: `any` or a literal hostname, ie `test-host`
|
||||||
|
|||||||
+35
-4
@@ -11,6 +11,8 @@ import (
|
|||||||
// ErrIPv6CouldNotFindPayload is returned when the ipv6 extension header chain is truncated before a terminal
|
// ErrIPv6CouldNotFindPayload is returned when the ipv6 extension header chain is truncated before a terminal
|
||||||
// upper layer protocol is reached.
|
// upper layer protocol is reached.
|
||||||
var ErrIPv6CouldNotFindPayload = errors.New("could not find payload in ipv6 packet")
|
var ErrIPv6CouldNotFindPayload = errors.New("could not find payload in ipv6 packet")
|
||||||
|
var ErrIPv6ExtensionHeaderAfterFragment = errors.New("extension header after fragment header in packet")
|
||||||
|
var ErrIPv6HopByHopHeadersMustBeFirst = errors.New("hop-by-hop header must be first extension header")
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// MaxIPv4RejectPacketSize is the largest IPv4 reject packet:
|
// MaxIPv4RejectPacketSize is the largest IPv4 reject packet:
|
||||||
@@ -28,6 +30,13 @@ const (
|
|||||||
|
|
||||||
MaxRejectPacketSize = maxIPv6RejectPacketSize
|
MaxRejectPacketSize = maxIPv6RejectPacketSize
|
||||||
|
|
||||||
|
IPv6HeaderHopByHop = 0
|
||||||
|
IPv6HeaderRouting = 43
|
||||||
|
IPv6HeaderFragment = 44
|
||||||
|
IPv6NoNextHeader = 59
|
||||||
|
IPv6HeaderDestination = 60
|
||||||
|
IPv6HeaderAuthentication = 51
|
||||||
|
|
||||||
IPProtocolICMP = 1
|
IPProtocolICMP = 1
|
||||||
IPProtocolICMPv6 = 58
|
IPProtocolICMPv6 = 58
|
||||||
IPProtocolTCP = 6
|
IPProtocolTCP = 6
|
||||||
@@ -350,6 +359,10 @@ func ipv6CreateRejectTCPPacket(packet []byte, out []byte, offset int) []byte {
|
|||||||
// Everything else, including Mobility (135), HIP (139), Shim6 (140), experimental 253/254, and real upper layer protocols like SCTP or GRE, is terminal.
|
// Everything else, including Mobility (135), HIP (139), Shim6 (140), experimental 253/254, and real upper layer protocols like SCTP or GRE, is terminal.
|
||||||
// For a non-first fragment the returned protocol is the fragmented protocol and offset points at the fragment header, there is no transport header to locate.
|
// For a non-first fragment the returned protocol is the fragmented protocol and offset points at the fragment header, there is no transport header to locate.
|
||||||
// Returns ErrIPv6CouldNotFindPayload if packet is smaller than an ipv6 header, or the chain is truncated, or if more than 8 extension headers are present.
|
// Returns ErrIPv6CouldNotFindPayload if packet is smaller than an ipv6 header, or the chain is truncated, or if more than 8 extension headers are present.
|
||||||
|
// Returns ErrIPv6HopByHopHeadersMustBeFirst if a hop-by-hop header appears anywhere but directly after the ipv6 header (RFC 8200 section 4.1).
|
||||||
|
// Returns ErrIPv6ExtensionHeaderAfterFragment if any walked extension header, including a second fragment header, follows the fragment header of a
|
||||||
|
// first fragment. The upper layer header must then begin at byte 0 of the fragmentable part, beyond the reach of any non-first fragment,
|
||||||
|
// whose offset is at least 8, so overlapping fragments cannot rewrite the ports or protocol the firewall read from the first fragment.
|
||||||
func IPv6FindUpperProtocol(packet []byte) (nextHeader uint8, offset int, isFragment bool, anyFragment bool, err error) {
|
func IPv6FindUpperProtocol(packet []byte) (nextHeader uint8, offset int, isFragment bool, anyFragment bool, err error) {
|
||||||
const maxIPv6ExtHeaders = 8
|
const maxIPv6ExtHeaders = 8
|
||||||
if len(packet) < ipv6.HeaderLen {
|
if len(packet) < ipv6.HeaderLen {
|
||||||
@@ -360,26 +373,44 @@ func IPv6FindUpperProtocol(packet []byte) (nextHeader uint8, offset int, isFragm
|
|||||||
|
|
||||||
for range maxIPv6ExtHeaders {
|
for range maxIPv6ExtHeaders {
|
||||||
switch nextHeader {
|
switch nextHeader {
|
||||||
case 0, 43, 60: // Hop-by-Hop, Routing, Destination
|
case IPv6HeaderHopByHop, IPv6HeaderRouting, IPv6HeaderDestination:
|
||||||
|
if anyFragment {
|
||||||
|
return 0, 0, false, false, ErrIPv6ExtensionHeaderAfterFragment
|
||||||
|
}
|
||||||
if len(packet) < offset+2 {
|
if len(packet) < offset+2 {
|
||||||
return 0, 0, false, false, ErrIPv6CouldNotFindPayload
|
return 0, 0, false, false, ErrIPv6CouldNotFindPayload
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if nextHeader == IPv6HeaderHopByHop && offset != ipv6.HeaderLen {
|
||||||
|
//hop-by-hop must appear first, and only once, if it exists.
|
||||||
|
return 0, 0, false, false, ErrIPv6HopByHopHeadersMustBeFirst
|
||||||
|
}
|
||||||
|
|
||||||
nextHeader = packet[offset]
|
nextHeader = packet[offset]
|
||||||
offset += (int(packet[offset+1]) + 1) << 3
|
offset += (int(packet[offset+1]) + 1) << 3
|
||||||
|
|
||||||
case 44: // Fragment
|
case IPv6HeaderFragment:
|
||||||
|
if anyFragment {
|
||||||
|
return 0, 0, false, false, ErrIPv6ExtensionHeaderAfterFragment
|
||||||
|
}
|
||||||
if len(packet) < offset+8 {
|
if len(packet) < offset+8 {
|
||||||
return 0, 0, false, false, ErrIPv6CouldNotFindPayload
|
return 0, 0, false, false, ErrIPv6CouldNotFindPayload
|
||||||
}
|
}
|
||||||
|
|
||||||
anyFragment = true
|
anyFragment = true
|
||||||
// Non-first fragments carry no transport header, report the fragmented protocol and stop
|
// Non-first fragments carry no transport header, report the fragmented protocol and stop.
|
||||||
|
// Per RFC 8200 section 4.5 the receiver only uses the offset zero fragment's next header, so the value
|
||||||
|
// returned here can't be verified.
|
||||||
if packet[offset+2] != 0 || packet[offset+3]&0xf8 != 0 {
|
if packet[offset+2] != 0 || packet[offset+3]&0xf8 != 0 {
|
||||||
return packet[offset], offset, true, anyFragment, nil
|
return packet[offset], offset, true, anyFragment, nil
|
||||||
}
|
}
|
||||||
nextHeader = packet[offset]
|
nextHeader = packet[offset]
|
||||||
offset += 8
|
offset += 8
|
||||||
|
|
||||||
case 51: // AH
|
case IPv6HeaderAuthentication: // AH
|
||||||
|
if anyFragment {
|
||||||
|
return 0, 0, false, false, ErrIPv6ExtensionHeaderAfterFragment
|
||||||
|
}
|
||||||
if len(packet) < offset+2 {
|
if len(packet) < offset+2 {
|
||||||
return 0, 0, false, false, ErrIPv6CouldNotFindPayload
|
return 0, 0, false, false, ErrIPv6CouldNotFindPayload
|
||||||
}
|
}
|
||||||
|
|||||||
+28
-11
@@ -522,21 +522,29 @@ func Test_IPv6FindUpperProtocol(t *testing.T) {
|
|||||||
dst := net.ParseIP("fd00::2")
|
dst := net.ParseIP("fd00::2")
|
||||||
|
|
||||||
// 8 byte extension/transport stand-ins, first byte is the next header, second is the length field
|
// 8 byte extension/transport stand-ins, first byte is the next header, second is the length field
|
||||||
extToTCP := []byte{6, 0, 0, 0, 0, 0, 0, 0} // len 0 -> 8 bytes, next = TCP
|
extToTCP := []byte{6, 0, 0, 0, 0, 0, 0, 0} // len 0 -> 8 bytes, next = TCP
|
||||||
extToUDP := []byte{17, 0, 0, 0, 0, 0, 0, 0} // len 0 -> 8 bytes, next = UDP
|
extToUDP := []byte{17, 0, 0, 0, 0, 0, 0, 0} // len 0 -> 8 bytes, next = UDP
|
||||||
extToRouting := []byte{43, 0, 0, 0, 0, 0, 0, 0} // len 0 -> 8 bytes, next = Routing
|
extToRouting := []byte{43, 0, 0, 0, 0, 0, 0, 0} // len 0 -> 8 bytes, next = Routing
|
||||||
ahToUDP := []byte{17, 0, 0, 0, 0, 0, 0, 0} // AH len 0 -> (0+2)<<2 = 8 bytes, next = UDP
|
ahToUDP := []byte{17, 0, 0, 0, 0, 0, 0, 0} // AH len 0 -> (0+2)<<2 = 8 bytes, next = UDP
|
||||||
firstFragToUDP := []byte{17, 0, 0, 1, 0, 0, 0, 1} // frag offset 0, M=1, next = UDP
|
firstFragToUDP := []byte{17, 0, 0, 1, 0, 0, 0, 1} // frag offset 0, M=1, next = UDP
|
||||||
nonFirstFrag := []byte{17, 0, 0, 9, 0, 0, 0, 1} // frag offset non-zero, next = UDP
|
nonFirstFrag := []byte{17, 0, 0, 9, 0, 0, 0, 1} // frag offset non-zero, next = UDP
|
||||||
transport := []byte{0, 80, 1, 187, 0, 0, 0, 0} // stand-in bytes, IPv6FindUpperProtocol never reads ports
|
transport := []byte{0, 80, 1, 187, 0, 0, 0, 0} // stand-in bytes, IPv6FindUpperProtocol never reads ports
|
||||||
extToHbH := []byte{0, 0, 0, 0, 0, 0, 0, 0} // len 0 -> 8 bytes, next = Hop-by-Hop
|
extToHbH := []byte{0, 0, 0, 0, 0, 0, 0, 0} // len 0 -> 8 bytes, next = Hop-by-Hop
|
||||||
|
extToDst := []byte{60, 0, 0, 0, 0, 0, 0, 0} // len 0 -> 8 bytes, next = Destination
|
||||||
|
extToFrag := []byte{44, 0, 0, 0, 0, 0, 0, 0} // len 0 -> 8 bytes, next = Fragment
|
||||||
|
firstFragToDst := []byte{60, 0, 0, 1, 0, 0, 0, 1} // frag offset 0, M=1, next = Destination
|
||||||
|
firstFragToAH := []byte{51, 0, 0, 1, 0, 0, 0, 1} // frag offset 0, M=1, next = AH
|
||||||
|
firstFragToFrag := []byte{44, 0, 0, 1, 0, 0, 0, 1} // frag offset 0, M=1, next = Fragment
|
||||||
|
firstFragToESP := []byte{50, 0, 0, 1, 0, 0, 0, 1} // frag offset 0, M=1, next = ESP
|
||||||
|
atomicFragToDst := []byte{60, 0, 0, 0, 0, 0, 0, 1} // frag offset 0, M=0, next = Destination
|
||||||
|
nonFirstFragToDst := []byte{60, 0, 0, 9, 0, 0, 0, 1} // frag offset non-zero, next = Destination
|
||||||
|
|
||||||
// 8 walked headers spend the budget before the 9th slot's terminal is ever classified. This is the
|
// 8 walked headers spend the budget before the 9th slot's terminal is ever classified. This is the
|
||||||
// report 4040546 shape: without failing closed the walk hands back offset 104, and a caller reads ports
|
// report 4040546 shape: without failing closed the walk hands back offset 104, and a caller reads ports
|
||||||
// from the attacker's bytes there.
|
// from the attacker's bytes there. Destination is the header that may legally repeat.
|
||||||
var budgetChain []byte
|
var budgetChain []byte
|
||||||
for range 7 {
|
for range 7 {
|
||||||
budgetChain = append(budgetChain, extToHbH...)
|
budgetChain = append(budgetChain, extToDst...)
|
||||||
}
|
}
|
||||||
budgetChain = append(budgetChain, extToTCP...)
|
budgetChain = append(budgetChain, extToTCP...)
|
||||||
budgetChain = append(budgetChain, transport...)
|
budgetChain = append(budgetChain, transport...)
|
||||||
@@ -559,11 +567,20 @@ func Test_IPv6FindUpperProtocol(t *testing.T) {
|
|||||||
{"ah then udp", 51, append(ahToUDP, transport...), 17, ipv6.HeaderLen + 8, false, false, nil},
|
{"ah then udp", 51, append(ahToUDP, transport...), 17, ipv6.HeaderLen + 8, false, false, nil},
|
||||||
{"first fragment walks to transport", 44, append(firstFragToUDP, transport...), 17, ipv6.HeaderLen + 8, false, true, nil},
|
{"first fragment walks to transport", 44, append(firstFragToUDP, transport...), 17, ipv6.HeaderLen + 8, false, true, nil},
|
||||||
{"non-first fragment stops", 44, append(nonFirstFrag, transport...), 17, ipv6.HeaderLen, true, true, nil},
|
{"non-first fragment stops", 44, append(nonFirstFrag, transport...), 17, ipv6.HeaderLen, true, true, nil},
|
||||||
|
{"non-first fragment reports its claimed protocol", 44, append(nonFirstFragToDst, transport...), 60, ipv6.HeaderLen, true, true, nil},
|
||||||
|
{"routing then first fragment", 43, append(append(extToFrag, firstFragToUDP...), transport...), 17, ipv6.HeaderLen + 16, false, true, nil},
|
||||||
|
{"first fragment of a non-transport protocol is terminal", 44, append(firstFragToESP, transport...), 50, ipv6.HeaderLen + 8, false, true, nil},
|
||||||
|
{"destination after first fragment", 44, append(append(firstFragToDst, extToTCP...), transport...), 0, 0, false, false, ErrIPv6ExtensionHeaderAfterFragment},
|
||||||
|
{"ah after first fragment", 44, append(append(firstFragToAH, ahToUDP...), transport...), 0, 0, false, false, ErrIPv6ExtensionHeaderAfterFragment},
|
||||||
|
{"fragment after first fragment", 44, append(append(firstFragToFrag, nonFirstFrag...), transport...), 0, 0, false, false, ErrIPv6ExtensionHeaderAfterFragment},
|
||||||
|
{"destination after atomic fragment", 44, append(append(atomicFragToDst, extToTCP...), transport...), 0, 0, false, false, ErrIPv6ExtensionHeaderAfterFragment},
|
||||||
|
{"hop-by-hop after destination", 60, append(append(extToHbH, extToTCP...), transport...), 0, 0, false, false, ErrIPv6HopByHopHeadersMustBeFirst},
|
||||||
|
{"hop-by-hop twice", 0, append(append(extToHbH, extToTCP...), transport...), 0, 0, false, false, ErrIPv6HopByHopHeadersMustBeFirst},
|
||||||
{"unknown protocol is terminal", 132, transport, 132, ipv6.HeaderLen, false, false, nil}, // SCTP
|
{"unknown protocol is terminal", 132, transport, 132, ipv6.HeaderLen, false, false, nil}, // SCTP
|
||||||
{"truncated extension header", 0, nil, 0, 0, false, false, ErrIPv6CouldNotFindPayload},
|
{"truncated extension header", 0, nil, 0, 0, false, false, ErrIPv6CouldNotFindPayload},
|
||||||
// Destination Options with a declared length (255+1)*8 = 2048 that runs past the 48 byte buffer, next = SCTP
|
// Destination Options with a declared length (255+1)*8 = 2048 that runs past the 48 byte buffer, next = SCTP
|
||||||
{"extension length past buffer", 0, []byte{132, 255, 0, 0, 0, 0, 0, 0}, 0, 0, false, false, ErrIPv6CouldNotFindPayload},
|
{"extension length past buffer", 0, []byte{132, 255, 0, 0, 0, 0, 0, 0}, 0, 0, false, false, ErrIPv6CouldNotFindPayload},
|
||||||
{"budget exhausted fails closed", 0, budgetChain, 0, 0, false, false, ErrIPv6CouldNotFindPayload},
|
{"budget exhausted fails closed", 60, budgetChain, 0, 0, false, false, ErrIPv6CouldNotFindPayload},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
|||||||
@@ -710,6 +710,26 @@ func Test_newPacket_v6ExtHeaderPastBuffer(t *testing.T) {
|
|||||||
require.ErrorIs(t, newPacket(pkt, true, p), iputil.ErrIPv6CouldNotFindPayload)
|
require.ErrorIs(t, newPacket(pkt, true, p), iputil.ErrIPv6CouldNotFindPayload)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test_newPacket_v6ExtHeaderAfterFragment rejects a first fragment whose transport header sits behind another extension
|
||||||
|
// header. The ports would then start 8 or more bytes into the fragmentable part, where a non-first fragment, filtered
|
||||||
|
// only as `port: fragment`, could overlap them on a receiver that splices overlapping fragments.
|
||||||
|
func Test_newPacket_v6ExtHeaderAfterFragment(t *testing.T) {
|
||||||
|
p := &firewall.ParsedPacket{}
|
||||||
|
|
||||||
|
pkt := make([]byte, 60)
|
||||||
|
pkt[0] = 0x60 // version 6
|
||||||
|
pkt[6] = byte(layers.IPProtocolIPv6Fragment) // NextHeader -> Fragment
|
||||||
|
pkt[7] = 64 // hop limit
|
||||||
|
pkt[40] = byte(layers.IPProtocolIPv6Destination) // Fragment NextHeader -> Destination Options
|
||||||
|
pkt[43] = 0x01 // offset 0, M=1: a first fragment
|
||||||
|
pkt[48] = byte(iputil.IPProtocolTCP) // Destination Options NextHeader -> TCP
|
||||||
|
pkt[49] = 0 // HdrExtLen 0 -> 8 bytes, TCP begins at 56
|
||||||
|
binary.BigEndian.PutUint16(pkt[58:60], 443) // TCP dst port, reachable by a fragment at offset 8
|
||||||
|
|
||||||
|
require.ErrorIs(t, newPacket(pkt, true, p), iputil.ErrIPv6ExtensionHeaderAfterFragment)
|
||||||
|
require.ErrorIs(t, newPacket(pkt, false, p), iputil.ErrIPv6ExtensionHeaderAfterFragment)
|
||||||
|
}
|
||||||
|
|
||||||
// Test_newPacket_v6ExtHeaderConfusion is a regression test for parseV6 walking any unrecognized
|
// Test_newPacket_v6ExtHeaderConfusion is a regression test for parseV6 walking any unrecognized
|
||||||
// Next Header as if it were an ipv6 extension header. A real upper layer protocol Nebula doesn't
|
// Next Header as if it were an ipv6 extension header. A real upper layer protocol Nebula doesn't
|
||||||
// dissect (SCTP here) is not walkable, so applying the (len+1)*8 formula marched into the SCTP
|
// dissect (SCTP here) is not walkable, so applying the (len+1)*8 formula marched into the SCTP
|
||||||
|
|||||||
+1
-67
@@ -607,72 +607,6 @@ func (t *tun) Read(to []byte) (int, error) {
|
|||||||
return n - 4, nil
|
return n - 4, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// tunReadBatch is the most packets one tunQueue.Read returns.
|
|
||||||
const tunReadBatch = 64
|
|
||||||
|
|
||||||
// tunReadArena is tunQueue's receive buffer. Draining stops once less than defaultBatchBufSize of it
|
|
||||||
// is left, so every readv has room for the largest packet utun can return at any device MTU.
|
|
||||||
const tunReadArena = 4 * defaultBatchBufSize
|
|
||||||
|
|
||||||
// tunQueue is the darwin tun's Queue. Read drains every packet already queued on the utun, up to
|
|
||||||
// tunReadBatch, so the caller encrypts and sends them as one batch rather than one per wakeup.
|
|
||||||
type tunQueue struct {
|
|
||||||
t *tun
|
|
||||||
buf []byte
|
|
||||||
pkts [tunReadBatch]tio.Packet
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read waits for the utun to become readable, then reads packets until it would block. An error after
|
|
||||||
// at least one packet is dropped in favor of returning those packets; a persistent one recurs on the
|
|
||||||
// next Read.
|
|
||||||
func (q *tunQueue) Read() ([]tio.Packet, error) {
|
|
||||||
rc, err := q.t.f.SyscallConn()
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var head [4]byte
|
|
||||||
n, off := 0, 0
|
|
||||||
var callErr error
|
|
||||||
err = rc.Read(func(fd uintptr) bool {
|
|
||||||
for n < tunReadBatch && len(q.buf)-off >= defaultBatchBufSize {
|
|
||||||
iovecs := [2]unix.Iovec{
|
|
||||||
{Base: &head[0], Len: 4},
|
|
||||||
{Base: &q.buf[off], Len: uint64(len(q.buf) - off)},
|
|
||||||
}
|
|
||||||
l, e := tunReadv(int(fd), iovecs[:])
|
|
||||||
if e != nil {
|
|
||||||
if errno, ok := e.(syscall.Errno); ok && errno.Temporary() {
|
|
||||||
// Park on the poller only while there is nothing to hand back.
|
|
||||||
return n > 0
|
|
||||||
}
|
|
||||||
callErr = e
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
if l < 4 {
|
|
||||||
// A datagram too short to carry the AF prefix, or end of file; stop rather than spin on it.
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
end := off + l - 4
|
|
||||||
q.pkts[n] = tio.Packet{Bytes: q.buf[off:end:end]}
|
|
||||||
n++
|
|
||||||
off = end
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
if n > 0 {
|
|
||||||
return q.pkts[:n], nil
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return nil, callErr
|
|
||||||
}
|
|
||||||
|
|
||||||
func (q *tunQueue) Write(p []byte) (int, error) { return q.t.Write(p) }
|
|
||||||
|
|
||||||
func (q *tunQueue) Close() error { return q.t.Close() }
|
|
||||||
|
|
||||||
// Write pushes one IP packet onto the utun device. Safe for concurrent use:
|
// Write pushes one IP packet onto the utun device. Safe for concurrent use:
|
||||||
// the AF prefix and iovecs are per-call stack state, and the fd write itself
|
// the AF prefix and iovecs are per-call stack state, and the fd write itself
|
||||||
// serializes on the runtime's fd mutex (see the Queue contract in tio.go).
|
// serializes on the runtime's fd mutex (see the Queue contract in tio.go).
|
||||||
@@ -732,5 +666,5 @@ func (t *tun) Name() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) Queues(int) ([]tio.Queue, error) {
|
func (t *tun) Queues(int) ([]tio.Queue, error) {
|
||||||
return []tio.Queue{&tunQueue{t: t, buf: make([]byte, tunReadArena)}}, nil
|
return []tio.Queue{tio.NewSingleQueue(t, defaultBatchBufSize)}, nil
|
||||||
}
|
}
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
//go:build !ios && !e2e_testing
|
|
||||||
|
|
||||||
package overlay
|
|
||||||
|
|
||||||
import (
|
|
||||||
"os"
|
|
||||||
"syscall"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/slackhq/nebula/test"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
|
||||||
|
|
||||||
// newSocketpairTun returns a tun on one end of a connected AF_UNIX datagram socketpair, which keeps
|
|
||||||
// datagram boundaries the way a utun control socket does, and the other end's fd.
|
|
||||||
// sndbuf is the tun end's send buffer, which caps the largest datagram it accepts.
|
|
||||||
func newSocketpairTun(t *testing.T, sndbuf int) (*tun, int) {
|
|
||||||
t.Helper()
|
|
||||||
fds, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_DGRAM, 0)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NoError(t, unix.SetsockoptInt(fds[0], unix.SOL_SOCKET, unix.SO_SNDBUF, sndbuf))
|
|
||||||
require.NoError(t, unix.SetsockoptInt(fds[1], unix.SOL_SOCKET, unix.SO_RCVBUF, 1<<20))
|
|
||||||
// A dropped datagram fails the read-back instead of hanging it.
|
|
||||||
require.NoError(t, unix.SetsockoptTimeval(fds[1], unix.SOL_SOCKET, unix.SO_RCVTIMEO, &unix.Timeval{Sec: 2}))
|
|
||||||
require.NoError(t, unix.SetNonblock(fds[0], true))
|
|
||||||
f := os.NewFile(uintptr(fds[0]), "socketpair")
|
|
||||||
t.Cleanup(func() {
|
|
||||||
_ = f.Close()
|
|
||||||
_ = unix.Close(fds[1])
|
|
||||||
})
|
|
||||||
return &tun{f: f, l: test.NewLogger()}, fds[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
// testTunPkt returns packet i of size bytes, alternating IPv4 and IPv6, and the same packet as a utun
|
|
||||||
// datagram, behind its AF prefix.
|
|
||||||
func testTunPkt(i, size int) (pkt, wire []byte) {
|
|
||||||
pkt = make([]byte, size)
|
|
||||||
pkt[0] = 0x45
|
|
||||||
af := byte(syscall.AF_INET)
|
|
||||||
if i%2 == 1 {
|
|
||||||
pkt[0] = 0x60
|
|
||||||
af = syscall.AF_INET6
|
|
||||||
}
|
|
||||||
pkt[1] = byte(i)
|
|
||||||
pkt[2] = byte(i >> 8)
|
|
||||||
return pkt, append([]byte{0, 0, 0, af}, pkt...)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestTunQueueReadDrains pins that Read hands back every queued packet, AF prefix stripped and in
|
|
||||||
// order, capped at tunReadBatch per call, and doesn't wait for more once the queue is empty.
|
|
||||||
func TestTunQueueReadDrains(t *testing.T) {
|
|
||||||
tn, w := newSocketpairTun(t, 1<<20)
|
|
||||||
// An AF_UNIX datagram send is bounded by the receiver's buffer, here the tun's end.
|
|
||||||
rc, err := tn.f.SyscallConn()
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.NoError(t, rc.Control(func(fd uintptr) {
|
|
||||||
require.NoError(t, unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_RCVBUF, 1<<20))
|
|
||||||
}))
|
|
||||||
q := &tunQueue{t: tn, buf: make([]byte, tunReadArena)}
|
|
||||||
total := tunReadBatch + 3
|
|
||||||
var want [][]byte
|
|
||||||
for i := range total {
|
|
||||||
pkt, wire := testTunPkt(i, 60+i)
|
|
||||||
_, err := unix.Write(w, wire)
|
|
||||||
require.NoError(t, err)
|
|
||||||
want = append(want, pkt)
|
|
||||||
}
|
|
||||||
|
|
||||||
var got [][]byte
|
|
||||||
for _, size := range []int{tunReadBatch, 3} {
|
|
||||||
pkts, err := q.Read()
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Len(t, pkts, size)
|
|
||||||
for _, p := range pkts {
|
|
||||||
got = append(got, p.Clone().Bytes)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assert.Equal(t, want, got)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestTunQueueReadClipsPackets pins that each returned packet's capacity ends at its own bytes, so
|
|
||||||
// appending to one can't overwrite the next.
|
|
||||||
func TestTunQueueReadClipsPackets(t *testing.T) {
|
|
||||||
tn, w := newSocketpairTun(t, 1<<20)
|
|
||||||
q := &tunQueue{t: tn, buf: make([]byte, tunReadArena)}
|
|
||||||
for i := range 2 {
|
|
||||||
_, wire := testTunPkt(i, 100)
|
|
||||||
_, err := unix.Write(w, wire)
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
|
||||||
pkts, err := q.Read()
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Len(t, pkts, 2)
|
|
||||||
assert.Equal(t, len(pkts[0].Bytes), cap(pkts[0].Bytes))
|
|
||||||
}
|
|
||||||
Reference in new issue
Block a user