mirror of
https://github.com/slackhq/nebula.git
synced 2026-11-16 05:46:52 +01:00
Compare commits
23
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
21a7f1b711 | ||
|
|
64bec56404 | ||
|
|
81f991f1fd | ||
|
|
21075ba32e | ||
|
|
37032a22bf | ||
|
|
7cfa47d569 | ||
|
|
e20569ce51 | ||
|
|
9651ffdb0d | ||
|
|
3a45c01656 | ||
|
|
019cc46a7f | ||
|
|
cf3f315cd3 | ||
|
|
3fc627e08d | ||
|
|
abff1a4555 | ||
|
|
227134a1d5 | ||
|
|
232bacfdd0 | ||
|
|
4e1ef9cd73 | ||
|
|
614e0ea3aa | ||
|
|
2999fc0b6a | ||
|
|
f474b534e1 | ||
|
|
0dc8a28d1f | ||
|
|
89178f45ba | ||
|
|
6a72e1c304 | ||
|
|
e50f8128f4 |
No files matched your search
Executable
+142
@@ -0,0 +1,142 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Backport a merged PR to a release branch.
|
||||
#
|
||||
# Cherry-picks the merge commit of a merged PR onto release-1.11 and opens a
|
||||
# backport PR, mirroring the shape of #1842.
|
||||
#
|
||||
# Usage: ./backport.sh <pr-number> [target-version] [--continue]
|
||||
# pr-number The merged PR to backport
|
||||
# target-version Release version to target (default: 1.11)
|
||||
# --continue Resume after resolving cherry-pick conflicts: assumes the
|
||||
# fixes are staged, runs `git cherry-pick --continue`, and
|
||||
# proceeds to push and open the backport PR.
|
||||
#
|
||||
# Requires: gh (authenticated), git, jq.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
PR_NUMBER=${1:?usage: backport.sh <pr-number> [target-version] [--continue]}
|
||||
shift
|
||||
CONTINUE=0
|
||||
TARGET_VERSION=""
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
--continue) CONTINUE=1 ;;
|
||||
*) TARGET_VERSION=$arg ;;
|
||||
esac
|
||||
done
|
||||
|
||||
for cmd in gh git jq; do
|
||||
command -v "$cmd" >/dev/null || { echo "error: $cmd is required" >&2; exit 1; }
|
||||
done
|
||||
|
||||
# Pull the source PR's metadata. Refuse to backport a PR that never merged.
|
||||
pr_json=$(gh pr view "$PR_NUMBER" --json title,body,mergedAt,mergeCommit,milestone)
|
||||
PR_TITLE=$(jq -r '.title' <<<"$pr_json")
|
||||
PR_BODY=$(jq -r '.body' <<<"$pr_json")
|
||||
MERGE_SHA=$(jq -r '.mergeCommit.oid // empty' <<<"$pr_json")
|
||||
PR_MILESTONE=$(jq -r '.milestone.title // empty' <<<"$pr_json")
|
||||
|
||||
if [ "$(jq -r '.mergedAt // empty' <<<"$pr_json")" = "" ] || [ -z "$MERGE_SHA" ]; then
|
||||
echo "error: PR #${PR_NUMBER} is not merged (no merge commit to cherry-pick)" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Default the target to one minor below the PR's milestone: a PR landing in
|
||||
# v1.12.0 backports to release-1.11. An explicit target-version arg overrides.
|
||||
if [ -z "$TARGET_VERSION" ]; then
|
||||
if [[ $PR_MILESTONE =~ ^v?([0-9]+)\.([0-9]+) ]]; then
|
||||
TARGET_VERSION="${BASH_REMATCH[1]}.$(( BASH_REMATCH[2] - 1 ))"
|
||||
else
|
||||
echo "error: PR #${PR_NUMBER} has no v<major>.<minor>.* milestone to infer the target from." >&2
|
||||
echo "Pass the target version explicitly, e.g. $0 ${PR_NUMBER} 1.11" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
TARGET_BRANCH="release-${TARGET_VERSION}"
|
||||
|
||||
# Slug from the PR title: lower-case, non-alphanumerics to dashes, trimmed.
|
||||
slug=$(printf '%s' "$PR_TITLE" \
|
||||
| tr '[:upper:]' '[:lower:]' \
|
||||
| sed -E 's/[^a-z0-9]+/-/g; s/^-+//; s/-+$//' \
|
||||
| cut -c1-50 \
|
||||
| sed -E 's/-+$//')
|
||||
BRANCH="backport-${TARGET_VERSION//./-}-${slug}"
|
||||
|
||||
if [ "$CONTINUE" -eq 1 ]; then
|
||||
# Resuming: the branch already exists and a cherry-pick is mid-conflict.
|
||||
current=$(git rev-parse --abbrev-ref HEAD)
|
||||
if [ "$current" != "$BRANCH" ]; then
|
||||
echo "error: --continue expects to be on ${BRANCH}, but HEAD is ${current}" >&2
|
||||
exit 1
|
||||
fi
|
||||
echo "Resuming backport of #${PR_NUMBER} on ${BRANCH}"
|
||||
# Conflicts assumed resolved and staged; core.editor=true keeps the commit message.
|
||||
git -c core.editor=true cherry-pick --continue
|
||||
else
|
||||
# A fresh run switches branches and cherry-picks, so tracked changes must be
|
||||
# clean. Untracked files are fine, and --continue is exempt (its resolved
|
||||
# conflicts are meant to be staged).
|
||||
if [ -n "$(git status --porcelain --untracked-files=no)" ]; then
|
||||
echo "error: working tree has uncommitted changes to tracked files; commit or stash them first." >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Backporting #${PR_NUMBER} (${MERGE_SHA}) onto ${TARGET_BRANCH} as ${BRANCH}"
|
||||
|
||||
git fetch origin
|
||||
git checkout -b "$BRANCH" "origin/${TARGET_BRANCH}"
|
||||
|
||||
# -m 1 handles a real merge commit; plain cherry-pick handles a squash merge.
|
||||
if ! { git cherry-pick -x -m 1 "$MERGE_SHA" 2>/dev/null || git cherry-pick -x "$MERGE_SHA"; }; then
|
||||
echo "error: ${MERGE_SHA} did not cherry-pick cleanly onto ${TARGET_BRANCH}." >&2
|
||||
echo "Resolve the conflicts, 'git add' them, then re-run:" >&2
|
||||
echo " $0 ${PR_NUMBER} ${TARGET_VERSION} --continue" >&2
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
title="backport v${TARGET_VERSION}: ${PR_TITLE}"
|
||||
body=$(printf 'Backport from #%s to release v%s\n\n---\n\n%s' \
|
||||
"$PR_NUMBER" "$TARGET_VERSION" "$PR_BODY")
|
||||
|
||||
# Highest open milestone matching v<version>.* (e.g. v1.11.1), if any.
|
||||
MILESTONE=$(gh api "repos/{owner}/{repo}/milestones?state=open" \
|
||||
--jq ".[].title | select(startswith(\"v${TARGET_VERSION}.\"))" \
|
||||
| sort -V | tail -n1)
|
||||
milestone_args=()
|
||||
if [ -n "$MILESTONE" ]; then
|
||||
milestone_args=(--milestone "$MILESTONE")
|
||||
else
|
||||
echo "warning: no open milestone matching v${TARGET_VERSION}.* found; PR will have no milestone" >&2
|
||||
fi
|
||||
|
||||
echo
|
||||
echo "About to run:"
|
||||
printf ' git push -u --force-with-lease origin %q\n' "$BRANCH"
|
||||
printf ' gh pr create --base %q --head %q --title %q --body %q' \
|
||||
"$TARGET_BRANCH" "$BRANCH" "$title" "$body"
|
||||
[ -n "$MILESTONE" ] && printf ' --milestone %q' "$MILESTONE"
|
||||
printf '\n'
|
||||
echo
|
||||
read -r -p "Open this pull request? [y/N] " reply
|
||||
case "$reply" in
|
||||
[yY] | [yY][eE][sS]) ;;
|
||||
*) echo "Aborted. The cherry-pick is on local branch ${BRANCH}; push it manually if desired."; exit 0 ;;
|
||||
esac
|
||||
|
||||
git push -u --force-with-lease origin "$BRANCH"
|
||||
|
||||
gh pr create \
|
||||
--base "$TARGET_BRANCH" \
|
||||
--head "$BRANCH" \
|
||||
--title "$title" \
|
||||
--body "$body" \
|
||||
${milestone_args[@]+"${milestone_args[@]}"}
|
||||
|
||||
# The backport now exists, so drop the label that flagged this PR for one.
|
||||
gh pr edit "$PR_NUMBER" --remove-label needs-backport
|
||||
|
||||
# Switch back to the original branch we were on before doing the backport
|
||||
git checkout -
|
||||
@@ -98,7 +98,7 @@ jobs:
|
||||
# WSL2 + Ubuntu so the smoke can run a real linux peer with its own
|
||||
# netns. iputils-ping is needed for the in-WSL ping check. WSL1 has no
|
||||
# real kernel and would lack /dev/net/tun, so we have to force WSL2.
|
||||
- uses: Vampire/setup-wsl@v3
|
||||
- uses: Vampire/setup-wsl@v7
|
||||
with:
|
||||
distribution: Ubuntu-24.04
|
||||
additional-packages: iputils-ping iproute2
|
||||
|
||||
@@ -51,15 +51,19 @@ wsl -d $Distro -- bash -c "rm -rf $WslDir && mkdir -p $WslDir" | Out-Null
|
||||
$DevName = 'nebula-smoke'
|
||||
$Ip1 = '192.168.241.1'
|
||||
$Ip2 = '192.168.241.2'
|
||||
# Dual stack on purpose: a v4-only overlay never exercises the v6 side of tun.mtu.
|
||||
$Ip6_1 = 'fd42:4242:241::1'
|
||||
$Ip6_2 = 'fd42:4242:241::2'
|
||||
$Mtu = 1300
|
||||
$Port = 4242
|
||||
|
||||
& $NebulaCert ca -name 'smoke-ca' -out-crt "$WorkDir\ca.crt" -out-key "$WorkDir\ca.key"
|
||||
if ($LASTEXITCODE -ne 0) { throw "nebula-cert ca failed (exit $LASTEXITCODE)" }
|
||||
|
||||
& $NebulaCert sign -name 'lighthouse' -networks "$Ip1/24" -ca-crt "$WorkDir\ca.crt" -ca-key "$WorkDir\ca.key" -out-crt "$WorkDir\lighthouse.crt" -out-key "$WorkDir\lighthouse.key"
|
||||
& $NebulaCert sign -name 'lighthouse' -networks "$Ip1/24,$Ip6_1/64" -ca-crt "$WorkDir\ca.crt" -ca-key "$WorkDir\ca.key" -out-crt "$WorkDir\lighthouse.crt" -out-key "$WorkDir\lighthouse.key"
|
||||
if ($LASTEXITCODE -ne 0) { throw "nebula-cert sign lighthouse failed (exit $LASTEXITCODE)" }
|
||||
|
||||
& $NebulaCert sign -name 'peer' -networks "$Ip2/24" -ca-crt "$WorkDir\ca.crt" -ca-key "$WorkDir\ca.key" -out-crt "$WorkDir\peer.crt" -out-key "$WorkDir\peer.key"
|
||||
& $NebulaCert sign -name 'peer' -networks "$Ip2/24,$Ip6_2/64" -ca-crt "$WorkDir\ca.crt" -ca-key "$WorkDir\ca.key" -out-crt "$WorkDir\peer.crt" -out-key "$WorkDir\peer.key"
|
||||
if ($LASTEXITCODE -ne 0) { throw "nebula-cert sign peer failed (exit $LASTEXITCODE)" }
|
||||
|
||||
# Windows lighthouse config.
|
||||
@@ -82,7 +86,7 @@ tun:
|
||||
drop_local_broadcast: false
|
||||
drop_multicast: false
|
||||
tx_queue: 500
|
||||
mtu: 1300
|
||||
mtu: $Mtu
|
||||
network_category: private
|
||||
logging:
|
||||
level: info
|
||||
@@ -126,7 +130,7 @@ tun:
|
||||
drop_local_broadcast: false
|
||||
drop_multicast: false
|
||||
tx_queue: 500
|
||||
mtu: 1300
|
||||
mtu: $Mtu
|
||||
logging:
|
||||
level: info
|
||||
format: text
|
||||
@@ -169,7 +173,7 @@ Write-Host '=== WSL diagnostic ==='
|
||||
wsl --version 2>&1 | Out-Host
|
||||
wsl --list --verbose 2>&1 | Out-Host
|
||||
wsl -d $Distro -u root -- uname -a | Out-Host
|
||||
wsl -d $Distro -u root -- bash -c "modprobe tun 2>&1 || true; mkdir -p /dev/net; [ -c /dev/net/tun ] || mknod /dev/net/tun c 10 200; chmod 600 /dev/net/tun; ls -l /dev/net/tun"
|
||||
wsl -d $Distro -u root -- bash -c "modprobe tun 2>&1 || true; mkdir -p /dev/net; [ -c /dev/net/tun ] || mknod /dev/net/tun c 10 200; chmod 600 /dev/net/tun; { echo 0 > /proc/sys/net/ipv6/conf/all/disable_ipv6; echo 0 > /proc/sys/net/ipv6/conf/default/disable_ipv6; } 2>/dev/null || true; ls -l /dev/net/tun"
|
||||
if ($LASTEXITCODE -ne 0) { throw "failed to prepare /dev/net/tun in WSL (TUN support missing?)" }
|
||||
|
||||
# Deliberately no New-NetFirewallRule calls here -- nebula's windows_bypass_wdf
|
||||
@@ -214,6 +218,16 @@ try {
|
||||
}
|
||||
Write-Host "OK: $DevName NetworkCategory=Private"
|
||||
|
||||
# v6 silently kept the adapter default of 65535 while v4 was correct.
|
||||
foreach ($family in @('IPv4', 'IPv6')) {
|
||||
Wait-Until -TimeoutSec 30 -What "$DevName $family NlMtu=$Mtu" -Predicate {
|
||||
if ($lhProc.HasExited) { throw "lighthouse exited (code $($lhProc.ExitCode)) before $family mtu was set" }
|
||||
$rows = @(Get-NetIPInterface -InterfaceAlias $DevName -AddressFamily $family -ErrorAction SilentlyContinue)
|
||||
$rows.Count -gt 0 -and -not ($rows | Where-Object { $_.NlMtu -ne $Mtu })
|
||||
}
|
||||
Write-Host "OK: $DevName $family NlMtu=$Mtu"
|
||||
}
|
||||
|
||||
Wait-Until -TimeoutSec 30 -What "WSL nebula1 with $Ip2" -Predicate {
|
||||
if ($peerProc.HasExited) { throw "peer exited (code $($peerProc.ExitCode)) before tun was ready" }
|
||||
$r = wsl -d $Distro -u root -- bash -c "ip -o addr show nebula1 2>/dev/null | grep -q 'inet $Ip2' && echo yes"
|
||||
@@ -221,6 +235,13 @@ try {
|
||||
}
|
||||
Write-Host "OK: WSL nebula1 has $Ip2"
|
||||
|
||||
Wait-Until -TimeoutSec 30 -What "WSL nebula1 with $Ip6_2" -Predicate {
|
||||
if ($peerProc.HasExited) { throw "peer exited (code $($peerProc.ExitCode)) before the v6 address was up" }
|
||||
$r = wsl -d $Distro -u root -- bash -c "ip -o addr show nebula1 2>/dev/null | grep -q 'inet6 $Ip6_2' && echo yes"
|
||||
("$r").Trim() -eq 'yes'
|
||||
}
|
||||
Write-Host "OK: WSL nebula1 has $Ip6_2"
|
||||
|
||||
Wait-Until -TimeoutSec 30 -What "ping from WSL peer to windows lighthouse ($Ip1)" -Predicate {
|
||||
if ($peerProc.HasExited) { throw "peer exited (code $($peerProc.ExitCode)) before ping succeeded" }
|
||||
$r = wsl -d $Distro -u root -- bash -c "ping -c1 -W1 $Ip1 >/dev/null 2>&1 && echo OK"
|
||||
@@ -234,6 +255,28 @@ try {
|
||||
}
|
||||
Write-Host "OK: windows lighthouse -> WSL peer"
|
||||
|
||||
# Otherwise the v6 networks only prove the interface exists, not that it forwards.
|
||||
Wait-Until -TimeoutSec 30 -What "v6 ping from WSL peer to windows lighthouse ($Ip6_1)" -Predicate {
|
||||
if ($peerProc.HasExited) { throw "peer exited (code $($peerProc.ExitCode)) before the v6 ping succeeded" }
|
||||
$r = wsl -d $Distro -u root -- bash -c "ping -6 -c1 -W1 $Ip6_1 >/dev/null 2>&1 && echo OK"
|
||||
("$r").Trim() -eq 'OK'
|
||||
}
|
||||
Write-Host "OK: WSL peer -> windows lighthouse over v6"
|
||||
|
||||
# The NlMtu check above proves the value was written, not that windows honors it. A payload this size only
|
||||
# crosses the tunnel as fragments cut at NlMtu on the way out of the adapter. Left at the adapter default it
|
||||
# reaches nebula whole, overflows the udp.MTU (9001) write buffer and is dropped, so the ping never answers.
|
||||
# Anything smaller would ride out as one oversized datagram and survive on IP fragmentation of the underlay.
|
||||
$BigPayload = 9000
|
||||
foreach ($target in @(@{ Family = 'v4'; Ip = $Ip2 }, @{ Family = 'v6'; Ip = $Ip6_2 })) {
|
||||
Wait-Until -TimeoutSec 30 -What "$($target.Family) ping with a $BigPayload byte payload from windows lighthouse to WSL peer ($($target.Ip))" -Predicate {
|
||||
if ($peerProc.HasExited) { throw "peer exited (code $($peerProc.ExitCode)) before the large $($target.Family) ping succeeded" }
|
||||
$null = & ping.exe -n 1 -w 1000 -l $BigPayload $target.Ip
|
||||
$LASTEXITCODE -eq 0
|
||||
}
|
||||
Write-Host "OK: windows lighthouse -> WSL peer, $BigPayload byte $($target.Family) payload"
|
||||
}
|
||||
|
||||
Write-Host ''
|
||||
Write-Host 'All smoke checks passed.'
|
||||
}
|
||||
|
||||
+14
-1
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [1.11.1] - 2026-08-21
|
||||
|
||||
See the [v1.11.1](https://github.com/slackhq/nebula/milestone/30?closed=1) milestone for a complete list of changes.
|
||||
|
||||
### Changed
|
||||
|
||||
- IPv6 packets whose next header is a protocol Nebula does not parse (SCTP, GRE, IP-in-IP, etc.) are now
|
||||
@@ -15,11 +19,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||
their true protocol, so only a `proto: any` rule allows them. If you carry one of these protocols over the
|
||||
overlay, confirm a `proto: any` rule covers it before upgrading, it may have been passing only through this
|
||||
bypass. (#1840)
|
||||
- Drop the dependency on `github.com/cyberdelia/go-metrics-graphite`, which has been unmaintained for over ten
|
||||
years, by inlining the small amount of code Nebula used. (#1832)
|
||||
|
||||
### Fixed
|
||||
|
||||
- The ICMPv6 type was read from the wrong byte when classifying IPv6 packets, so the echo identifier used
|
||||
for conntrack was never picked up. (#1840)
|
||||
- Enforce outbound message counter limits so a tunnel is rehandshaked before the counter can wrap, preventing
|
||||
nonce reuse. This is unreachable in practice, but is enforced as a defense-in-depth measure. (#1841)
|
||||
- Prevent `nebula-cert ca` from running out of memory on 32bit systems when generating encrypted private keys. (#1834)
|
||||
- Tolerate `ErrDumpInterrupted` when listing tun addresses on Linux, so a transient interrupted netlink dump
|
||||
no longer aborts startup. (#1835)
|
||||
|
||||
## [1.11.0] - 2026-07-23
|
||||
|
||||
@@ -884,7 +895,9 @@ created.)
|
||||
|
||||
- Initial public release.
|
||||
|
||||
[Unreleased]: https://github.com/slackhq/nebula/compare/v1.10.3...HEAD
|
||||
[Unreleased]: https://github.com/slackhq/nebula/compare/v1.11.1...HEAD
|
||||
[1.11.1]: https://github.com/slackhq/nebula/releases/tag/v1.11.1
|
||||
[1.11.0]: https://github.com/slackhq/nebula/releases/tag/v1.11.0
|
||||
[1.10.3]: https://github.com/slackhq/nebula/releases/tag/v1.10.3
|
||||
[1.10.2]: https://github.com/slackhq/nebula/releases/tag/v1.10.2
|
||||
[1.10.1]: https://github.com/slackhq/nebula/releases/tag/v1.10.1
|
||||
|
||||
@@ -242,11 +242,14 @@ test-cov-html:
|
||||
go test -coverprofile=coverage.out
|
||||
go tool cover -html=coverage.out
|
||||
|
||||
# The package builds only compile. The final line links an android binary so a linker-only failure
|
||||
# cannot pass CI.
|
||||
build-test-mobile:
|
||||
GOARCH=amd64 GOOS=ios go build $(shell go list ./... | grep -v '/cmd/\|/examples/')
|
||||
GOARCH=arm64 GOOS=ios go build $(shell go list ./... | grep -v '/cmd/\|/examples/')
|
||||
GOARCH=amd64 GOOS=android go build $(shell go list ./... | grep -v '/cmd/\|/examples/')
|
||||
GOARCH=arm64 GOOS=android go build $(shell go list ./... | grep -v '/cmd/\|/examples/')
|
||||
GOARCH=arm64 GOOS=android go build -o /dev/null ${NEBULA_CMD_PATH}
|
||||
|
||||
bench:
|
||||
go test -bench=.
|
||||
|
||||
@@ -79,13 +79,7 @@ func (b *Bits) clearRange(startPos, count uint64) uint64 {
|
||||
// handle the potential partial word before pos becomes u64 aligned
|
||||
word := pos >> 6
|
||||
bit := pos & 63
|
||||
take := uint64(64) - bit
|
||||
if take > remaining {
|
||||
take = remaining
|
||||
}
|
||||
if take > b.length-pos {
|
||||
take = b.length - pos
|
||||
}
|
||||
take := min(min(uint64(64)-bit, remaining), b.length-pos)
|
||||
var mask uint64
|
||||
if take == 64 {
|
||||
mask = math.MaxUint64
|
||||
@@ -189,10 +183,7 @@ func (b *Bits) Update(l *slog.Logger, i uint64) bool {
|
||||
func (b *Bits) updateSlow(l *slog.Logger, i uint64) bool {
|
||||
// If i is a jump, adjust the window, record lost, update current, and return true
|
||||
if i > b.current {
|
||||
end := i
|
||||
if end > b.current+b.length {
|
||||
end = b.current + b.length
|
||||
}
|
||||
end := min(i, b.current+b.length)
|
||||
count := end - b.current
|
||||
startPos := (b.current + 1) & b.lengthMask
|
||||
|
||||
|
||||
+1
-1
@@ -120,7 +120,7 @@ func TestCertificate_SignP256_AlwaysNormalized(t *testing.T) {
|
||||
pub := elliptic.Marshal(elliptic.P256(), priv.PublicKey.X, priv.PublicKey.Y)
|
||||
rawPriv := priv.D.FillBytes(make([]byte, 32))
|
||||
|
||||
for i := 0; i < 1000; i++ {
|
||||
for i := range 1000 {
|
||||
if i&1 == 1 {
|
||||
tbs.Version = Version1
|
||||
} else {
|
||||
|
||||
@@ -151,7 +151,7 @@ func ca(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) error
|
||||
|
||||
var groups []string
|
||||
if *cf.groups != "" {
|
||||
for _, rg := range strings.Split(*cf.groups, ",") {
|
||||
for rg := range strings.SplitSeq(*cf.groups, ",") {
|
||||
g := strings.TrimSpace(rg)
|
||||
if g != "" {
|
||||
groups = append(groups, g)
|
||||
@@ -171,7 +171,7 @@ func ca(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) error
|
||||
}
|
||||
|
||||
if *cf.networks != "" {
|
||||
for _, rs := range strings.Split(*cf.networks, ",") {
|
||||
for rs := range strings.SplitSeq(*cf.networks, ",") {
|
||||
rs := strings.Trim(rs, " ")
|
||||
if rs != "" {
|
||||
n, err := netip.ParsePrefix(rs)
|
||||
@@ -193,7 +193,7 @@ func ca(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) error
|
||||
}
|
||||
|
||||
if *cf.unsafeNetworks != "" {
|
||||
for _, rs := range strings.Split(*cf.unsafeNetworks, ",") {
|
||||
for rs := range strings.SplitSeq(*cf.unsafeNetworks, ",") {
|
||||
rs := strings.Trim(rs, " ")
|
||||
if rs != "" {
|
||||
n, err := netip.ParsePrefix(rs)
|
||||
@@ -221,7 +221,7 @@ func ca(args []string, out io.Writer, errOut io.Writer, pr PasswordReader) error
|
||||
if !isP11 && *cf.encryption {
|
||||
passphrase = []byte(os.Getenv("NEBULA_CA_PASSPHRASE"))
|
||||
if len(passphrase) == 0 {
|
||||
for i := 0; i < 5; i++ {
|
||||
for range 5 {
|
||||
errOut.Write([]byte("Enter passphrase: "))
|
||||
passphrase, err = pr.ReadPassword()
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package main
|
||||
|
||||
|
||||
@@ -146,7 +146,7 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader)
|
||||
passphrase = []byte(os.Getenv("NEBULA_CA_PASSPHRASE"))
|
||||
if len(passphrase) == 0 {
|
||||
// ask for a passphrase until we get one
|
||||
for i := 0; i < 5; i++ {
|
||||
for range 5 {
|
||||
errOut.Write([]byte("Enter passphrase: "))
|
||||
passphrase, err = pr.ReadPassword()
|
||||
|
||||
@@ -203,7 +203,7 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader)
|
||||
}
|
||||
|
||||
if *sf.networks != "" {
|
||||
for _, rs := range strings.Split(*sf.networks, ",") {
|
||||
for rs := range strings.SplitSeq(*sf.networks, ",") {
|
||||
rs := strings.Trim(rs, " ")
|
||||
if rs != "" {
|
||||
n, err := netip.ParsePrefix(rs)
|
||||
@@ -228,7 +228,7 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader)
|
||||
}
|
||||
|
||||
if *sf.unsafeNetworks != "" {
|
||||
for _, rs := range strings.Split(*sf.unsafeNetworks, ",") {
|
||||
for rs := range strings.SplitSeq(*sf.unsafeNetworks, ",") {
|
||||
rs := strings.Trim(rs, " ")
|
||||
if rs != "" {
|
||||
n, err := netip.ParsePrefix(rs)
|
||||
@@ -247,7 +247,7 @@ func signCert(args []string, out io.Writer, errOut io.Writer, pr PasswordReader)
|
||||
|
||||
var groups []string
|
||||
if *sf.groups != "" {
|
||||
for _, rg := range strings.Split(*sf.groups, ",") {
|
||||
for rg := range strings.SplitSeq(*sf.groups, ",") {
|
||||
g := strings.TrimSpace(rg)
|
||||
if g != "" {
|
||||
groups = append(groups, g)
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package main
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package main
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build !linux
|
||||
// +build !linux
|
||||
|
||||
package main
|
||||
|
||||
|
||||
+6
-9
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"maps"
|
||||
"math"
|
||||
"os"
|
||||
"os/signal"
|
||||
@@ -154,9 +155,7 @@ func (c *C) ReloadConfig() {
|
||||
defer c.reloadLock.Unlock()
|
||||
|
||||
c.oldSettings = make(map[string]any)
|
||||
for k, v := range c.Settings {
|
||||
c.oldSettings[k] = v
|
||||
}
|
||||
maps.Copy(c.oldSettings, c.Settings)
|
||||
|
||||
err := c.Load(c.path)
|
||||
if err != nil {
|
||||
@@ -177,9 +176,7 @@ func (c *C) ReloadConfigString(raw string) error {
|
||||
defer c.reloadLock.Unlock()
|
||||
|
||||
c.oldSettings = make(map[string]any)
|
||||
for k, v := range c.Settings {
|
||||
c.oldSettings[k] = v
|
||||
}
|
||||
maps.Copy(c.oldSettings, c.Settings)
|
||||
|
||||
err := c.LoadString(raw)
|
||||
if err != nil {
|
||||
@@ -216,7 +213,7 @@ func (c *C) GetStringSlice(k string, d []string) []string {
|
||||
}
|
||||
|
||||
v := make([]string, len(rv))
|
||||
for i := 0; i < len(v); i++ {
|
||||
for i := range v {
|
||||
v[i] = fmt.Sprintf("%v", rv[i])
|
||||
}
|
||||
|
||||
@@ -310,8 +307,8 @@ func (c *C) IsSet(k string) bool {
|
||||
}
|
||||
|
||||
func (c *C) get(k string, v any) any {
|
||||
parts := strings.Split(k, ".")
|
||||
for _, p := range parts {
|
||||
parts := strings.SplitSeq(k, ".")
|
||||
for p := range parts {
|
||||
m, ok := v.(map[string]any)
|
||||
if !ok {
|
||||
return nil
|
||||
|
||||
@@ -97,7 +97,7 @@ func TestConnectionState_NextMessageCounter(t *testing.T) {
|
||||
assert.Equal(t, RejectAfterMessages, cs.messageCounter.Load())
|
||||
|
||||
// Continued send attempts stay refused and the counter never wraps
|
||||
for i := 0; i < 10; i++ {
|
||||
for range 10 {
|
||||
_, ok = cs.NextMessageCounter()
|
||||
assert.False(t, ok)
|
||||
}
|
||||
|
||||
+2
-1
@@ -203,7 +203,8 @@ func (c *Control) RebindUDPServer() {
|
||||
}
|
||||
|
||||
// A failure here means we are likely still pinned to the interface we came up on, so the rest of this is
|
||||
// unlikely to help. Say so instead of silently carrying on as if we rebound.
|
||||
// unlikely to help. Say so instead of silently carrying on as if we rebound. Only the first writer is rebound;
|
||||
// darwin's Rebind clears every listen routine's listener.
|
||||
if err := c.f.outside.Rebind(); err != nil {
|
||||
c.l.Error("Failed to rebind udp socket", "error", err)
|
||||
}
|
||||
|
||||
@@ -247,7 +247,7 @@ func TestControl_ConcurrentStopAndStart(t *testing.T) {
|
||||
c, _, _ := newReadyControl(t)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < 2; i++ {
|
||||
for range 2 {
|
||||
wg.Go(func() { c.Stop() })
|
||||
}
|
||||
wg.Go(func() { _ = c.Start() })
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build e2e_testing
|
||||
// +build e2e_testing
|
||||
|
||||
package e2e
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build e2e_testing
|
||||
// +build e2e_testing
|
||||
|
||||
package e2e
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build e2e_testing
|
||||
// +build e2e_testing
|
||||
|
||||
package e2e
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build e2e_testing
|
||||
// +build e2e_testing
|
||||
|
||||
package e2e
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build e2e_testing
|
||||
// +build e2e_testing
|
||||
|
||||
package e2e
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build e2e_testing
|
||||
// +build e2e_testing
|
||||
|
||||
package e2e
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build e2e_testing
|
||||
// +build e2e_testing
|
||||
|
||||
package e2e
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build e2e_testing
|
||||
// +build e2e_testing
|
||||
|
||||
package router
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build e2e_testing
|
||||
// +build e2e_testing
|
||||
|
||||
package router
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build e2e_testing
|
||||
// +build e2e_testing
|
||||
|
||||
package e2e
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build e2e_testing
|
||||
// +build e2e_testing
|
||||
|
||||
package e2e
|
||||
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
package nebula
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"sync"
|
||||
|
||||
"github.com/slackhq/nebula/udp"
|
||||
)
|
||||
|
||||
// establishedRemotes tells the udp conns that implement udp.EstablishedPeerConn which addresses are the current
|
||||
// direct remote of at least one established tunnel. A hostinfo holds a reference on its remote while it is in the
|
||||
// main hostmap, and SetRemote moves the reference; a relayed tunnel has no direct remote and holds none.
|
||||
type establishedRemotes struct {
|
||||
mu sync.Mutex
|
||||
conns []udp.EstablishedPeerConn
|
||||
refs map[netip.AddrPort]int
|
||||
}
|
||||
|
||||
// newEstablishedRemotes returns nil when no conn wants to know, which every method accepts.
|
||||
func newEstablishedRemotes(conns []udp.Conn) *establishedRemotes {
|
||||
var pc []udp.EstablishedPeerConn
|
||||
for _, c := range conns {
|
||||
if p, ok := c.(udp.EstablishedPeerConn); ok {
|
||||
pc = append(pc, p)
|
||||
}
|
||||
}
|
||||
if len(pc) == 0 {
|
||||
return nil
|
||||
}
|
||||
return &establishedRemotes{conns: pc, refs: map[netip.AddrPort]int{}}
|
||||
}
|
||||
|
||||
// add makes hostinfo hold a reference on its remote, as it joins the main hostmap. A hostinfo holds references in
|
||||
// one establishedRemotes at a time, so an add while it is in another is ignored.
|
||||
func (e *establishedRemotes) add(hostinfo *HostInfo) {
|
||||
if e == nil {
|
||||
return
|
||||
}
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
if !hostinfo.establishedIn.CompareAndSwap(nil, e) && hostinfo.establishedIn.Load() != e {
|
||||
return
|
||||
}
|
||||
e.syncLocked(hostinfo)
|
||||
}
|
||||
|
||||
// remove drops hostinfo's reference, as it leaves the main hostmap.
|
||||
func (e *establishedRemotes) remove(hostinfo *HostInfo) {
|
||||
if e == nil {
|
||||
return
|
||||
}
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
if hostinfo.establishedIn.CompareAndSwap(e, nil) {
|
||||
e.syncLocked(hostinfo)
|
||||
}
|
||||
}
|
||||
|
||||
// syncLocked moves hostinfo's reference to the remote it should hold now: its current remote while it is
|
||||
// established, none otherwise. Reading both under mu makes the last call win however add, remove and SetRemote
|
||||
// interleave. Only the establishedRemotes that hostinfo is in, or has just left, may call it, since
|
||||
// hostinfo.establishedRemote is a reference in that one's refs.
|
||||
func (e *establishedRemotes) syncLocked(hostinfo *HostInfo) {
|
||||
var want netip.AddrPort
|
||||
if hostinfo.establishedIn.Load() == e {
|
||||
want = hostinfo.GetRemote()
|
||||
}
|
||||
held := hostinfo.establishedRemote
|
||||
if want == held {
|
||||
return
|
||||
}
|
||||
if held.IsValid() {
|
||||
e.refs[held]--
|
||||
if e.refs[held] == 0 {
|
||||
delete(e.refs, held)
|
||||
for _, c := range e.conns {
|
||||
c.SetEstablishedPeer(held, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
if want.IsValid() {
|
||||
e.refs[want]++
|
||||
if e.refs[want] == 1 {
|
||||
for _, c := range e.conns {
|
||||
c.SetEstablishedPeer(want, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
hostinfo.establishedRemote = want
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
package nebula
|
||||
|
||||
import (
|
||||
"net/netip"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/slackhq/nebula/test"
|
||||
"github.com/slackhq/nebula/udp"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// establishedPeerConn records which destinations the hostmap has told it are established peers, and fails the test
|
||||
// on a call that doesn't change anything, which would mean a reference count went wrong.
|
||||
type establishedPeerConn struct {
|
||||
udp.NoopConn
|
||||
t *testing.T
|
||||
mu sync.Mutex
|
||||
peers map[netip.AddrPort]bool
|
||||
}
|
||||
|
||||
func (c *establishedPeerConn) SetEstablishedPeer(dst netip.AddrPort, established bool) {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
assert.NotEqual(c.t, established, c.peers[dst], "SetEstablishedPeer(%v, %v) repeated", dst, established)
|
||||
if established {
|
||||
c.peers[dst] = true
|
||||
} else {
|
||||
delete(c.peers, dst)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *establishedPeerConn) established() []netip.AddrPort {
|
||||
c.mu.Lock()
|
||||
defer c.mu.Unlock()
|
||||
var out []netip.AddrPort
|
||||
for dst := range c.peers {
|
||||
out = append(out, dst)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// newEstablishedTestHostMap returns a hostmap whose conns are a recording conn and one that doesn't track peers.
|
||||
func newEstablishedTestHostMap(t *testing.T) (*HostMap, *establishedPeerConn) {
|
||||
hm := newHostMap(test.NewLogger())
|
||||
hm.preferredRanges.Store(&[]netip.Prefix{netip.MustParsePrefix("192.168.0.0/16")})
|
||||
c := &establishedPeerConn{t: t, peers: map[netip.AddrPort]bool{}}
|
||||
hm.established = newEstablishedRemotes([]udp.Conn{c, udp.NoopConn{}})
|
||||
require.NotNil(t, hm.established)
|
||||
return hm, c
|
||||
}
|
||||
|
||||
func newEstablishedTestHostInfo(vpnAddr netip.Addr, index uint32) *HostInfo {
|
||||
return &HostInfo{
|
||||
vpnAddrs: []netip.Addr{vpnAddr},
|
||||
localIndexId: index,
|
||||
remotes: NewRemoteList([]netip.Addr{vpnAddr}, nil),
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewEstablishedRemotesWithoutTrackingConns(t *testing.T) {
|
||||
assert.Nil(t, newEstablishedRemotes([]udp.Conn{udp.NoopConn{}}))
|
||||
}
|
||||
|
||||
// A conn hears about a remote while at least one hostinfo in the main hostmap has it as its direct remote: from
|
||||
// the add, through roaming and SetRemoteIfPreferred, until the last such hostinfo is deleted or retired by
|
||||
// MaxHostInfosPerVpnIp. Pending and relayed hostinfos never count.
|
||||
func TestHostMap_EstablishedRemotes(t *testing.T) {
|
||||
hm, c := newEstablishedTestHostMap(t)
|
||||
f := &Interface{}
|
||||
a := netip.MustParseAddr("10.0.0.1")
|
||||
r1 := netip.MustParseAddrPort("203.0.113.1:4242")
|
||||
r2 := netip.MustParseAddrPort("203.0.113.2:4242")
|
||||
r3 := netip.MustParseAddrPort("192.168.1.3:4242")
|
||||
|
||||
// A pending hostinfo, as a handshake sets its remote, isn't established.
|
||||
h1 := newEstablishedTestHostInfo(a, 1)
|
||||
h1.SetRemote(r1)
|
||||
assert.Empty(t, c.established())
|
||||
|
||||
// Handshake completion adds it to the main hostmap.
|
||||
hm.unlockedAddHostInfo(h1, f)
|
||||
assert.ElementsMatch(t, []netip.AddrPort{r1}, c.established())
|
||||
|
||||
// A relayed tunnel has no direct remote.
|
||||
relayed := newEstablishedTestHostInfo(netip.MustParseAddr("10.0.0.2"), 2)
|
||||
hm.unlockedAddHostInfo(relayed, f)
|
||||
assert.ElementsMatch(t, []netip.AddrPort{r1}, c.established())
|
||||
|
||||
// A rehandshake's new hostinfo shares the remote, which stays established until both are gone.
|
||||
h3 := newEstablishedTestHostInfo(a, 3)
|
||||
h3.SetRemote(r1)
|
||||
hm.unlockedAddHostInfo(h3, f)
|
||||
hm.DeleteHostInfo(h1)
|
||||
assert.ElementsMatch(t, []netip.AddrPort{r1}, c.established())
|
||||
|
||||
// Roaming moves the reference.
|
||||
h3.SetRemote(r2)
|
||||
assert.ElementsMatch(t, []netip.AddrPort{r2}, c.established())
|
||||
require.True(t, h3.SetRemoteIfPreferred(hm, ViaSender{UdpAddr: r3}))
|
||||
assert.ElementsMatch(t, []netip.AddrPort{r3}, c.established())
|
||||
|
||||
// A relayed tunnel that hears from its peer directly roams onto a direct remote.
|
||||
relayed.SetRemote(r1)
|
||||
assert.ElementsMatch(t, []netip.AddrPort{r1, r3}, c.established())
|
||||
hm.DeleteHostInfo(relayed)
|
||||
|
||||
// A deleted hostinfo lets go, and a SetRemote after that changes nothing.
|
||||
hm.DeleteHostInfo(h3)
|
||||
assert.Empty(t, c.established())
|
||||
h3.SetRemote(r2)
|
||||
assert.Empty(t, c.established())
|
||||
hm.DeleteHostInfo(h3)
|
||||
|
||||
// Adding past MaxHostInfosPerVpnIp retires the oldest hostinfo and its remote.
|
||||
var hs []*HostInfo
|
||||
for i := range MaxHostInfosPerVpnIp + 1 {
|
||||
h := newEstablishedTestHostInfo(a, uint32(10+i))
|
||||
h.SetRemote(netip.AddrPortFrom(netip.MustParseAddr("198.51.100.1"), uint16(1000+i)))
|
||||
hm.unlockedAddHostInfo(h, f)
|
||||
hs = append(hs, h)
|
||||
}
|
||||
assert.Len(t, c.established(), MaxHostInfosPerVpnIp)
|
||||
assert.NotContains(t, c.established(), hs[0].GetRemote())
|
||||
}
|
||||
|
||||
// Roaming on the receive path races tunnel teardown on the connection manager; whatever the order, a deleted
|
||||
// hostinfo holds no reference.
|
||||
func TestHostMap_EstablishedRemotesRoamRacesDelete(t *testing.T) {
|
||||
hm, c := newEstablishedTestHostMap(t)
|
||||
f := &Interface{}
|
||||
a := netip.MustParseAddr("10.0.0.1")
|
||||
for i := range 200 {
|
||||
h := newEstablishedTestHostInfo(a, uint32(i+1))
|
||||
h.SetRemote(netip.MustParseAddrPort("203.0.113.1:4242"))
|
||||
hm.Lock()
|
||||
hm.unlockedAddHostInfo(h, f)
|
||||
hm.Unlock()
|
||||
var wg sync.WaitGroup
|
||||
wg.Go(func() { h.SetRemote(netip.AddrPortFrom(netip.MustParseAddr("203.0.113.2"), uint16(i+1))) })
|
||||
wg.Go(func() { hm.DeleteHostInfo(h) })
|
||||
wg.Wait()
|
||||
require.Empty(t, c.established(), "round %d", i)
|
||||
}
|
||||
}
|
||||
|
||||
// A hostinfo holds its reference in the first tracking hostmap it joins; another's add and delete of it change
|
||||
// nothing, so neither hostmap's counts go wrong.
|
||||
func TestHostMap_EstablishedRemotesSingleOwner(t *testing.T) {
|
||||
hm1, c1 := newEstablishedTestHostMap(t)
|
||||
hm2, c2 := newEstablishedTestHostMap(t)
|
||||
f := &Interface{}
|
||||
r := netip.MustParseAddrPort("203.0.113.1:4242")
|
||||
h := newEstablishedTestHostInfo(netip.MustParseAddr("10.0.0.1"), 1)
|
||||
h.SetRemote(r)
|
||||
|
||||
hm1.unlockedAddHostInfo(h, f)
|
||||
hm2.unlockedAddHostInfo(h, f)
|
||||
assert.ElementsMatch(t, []netip.AddrPort{r}, c1.established())
|
||||
assert.Empty(t, c2.established())
|
||||
|
||||
hm2.DeleteHostInfo(h)
|
||||
assert.Equal(t, map[netip.AddrPort]int{r: 1}, hm1.established.refs)
|
||||
assert.Empty(t, hm2.established.refs)
|
||||
assert.ElementsMatch(t, []netip.AddrPort{r}, c1.established())
|
||||
|
||||
hm1.DeleteHostInfo(h)
|
||||
assert.Empty(t, hm1.established.refs)
|
||||
assert.Empty(t, hm2.established.refs)
|
||||
assert.Empty(t, c1.established())
|
||||
assert.Empty(t, c2.established())
|
||||
}
|
||||
@@ -22,7 +22,7 @@ func newFixedTicker(t *testing.T, l *slog.Logger, cacheLen int) *ConntrackCacheT
|
||||
l: l,
|
||||
cache: make(ConntrackCache, cacheLen),
|
||||
}
|
||||
for i := 0; i < cacheLen; i++ {
|
||||
for i := range cacheLen {
|
||||
c.cache[Packet{LocalPort: uint16(i) + 1}] = struct{}{}
|
||||
}
|
||||
c.cacheTick.Store(1) // cacheV starts at 0, so Get() takes the reset path
|
||||
|
||||
@@ -5,33 +5,33 @@ go 1.26.0
|
||||
require (
|
||||
dario.cat/mergo v1.0.2
|
||||
filippo.io/bigmod v0.1.0
|
||||
github.com/DefinedNet/netlink v1.3.2-dn.1
|
||||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be
|
||||
github.com/armon/go-radix v1.0.0
|
||||
github.com/flynn/noise v1.1.0
|
||||
github.com/gaissmai/bart v0.28.0
|
||||
github.com/gaissmai/bart v0.29.0
|
||||
github.com/gogo/protobuf v1.3.2
|
||||
github.com/google/gopacket v1.1.19
|
||||
github.com/kardianos/service v1.3.0
|
||||
github.com/miekg/dns v1.1.72
|
||||
github.com/miekg/dns v1.1.73
|
||||
github.com/miekg/pkcs11 v1.1.2
|
||||
github.com/nbrownus/go-metrics-prometheus v0.0.0-20210712211119-974a6260965f
|
||||
github.com/prometheus/client_golang v1.24.1
|
||||
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475
|
||||
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
|
||||
github.com/stefanberger/go-pkcs11uri v0.0.0-20230803200340-78284954bff6
|
||||
github.com/stretchr/testify v1.12.0
|
||||
github.com/vishvananda/netlink v1.3.1
|
||||
github.com/stretchr/testify v1.12.1
|
||||
go.uber.org/goleak v1.3.0
|
||||
go.yaml.in/yaml/v3 v3.0.5
|
||||
golang.org/x/crypto v0.54.0
|
||||
golang.org/x/net v0.57.0
|
||||
golang.org/x/sync v0.22.0
|
||||
golang.org/x/sys v0.47.0
|
||||
golang.org/x/term v0.45.0
|
||||
golang.org/x/crypto v0.57.0
|
||||
golang.org/x/net v0.59.0
|
||||
golang.org/x/sync v0.23.0
|
||||
golang.org/x/sys v0.48.0
|
||||
golang.org/x/term v0.46.0
|
||||
golang.zx2c4.com/wintun v0.0.0-20230126152724-0fa3db229ce2
|
||||
golang.zx2c4.com/wireguard v0.0.0-20230325221338-052af4a8072b
|
||||
golang.zx2c4.com/wireguard/windows v1.0.1
|
||||
google.golang.org/protobuf v1.36.11
|
||||
google.golang.org/protobuf v1.36.12
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
gvisor.dev/gvisor v0.0.0-20240423190808-9d7a357edefe
|
||||
)
|
||||
@@ -45,7 +45,5 @@ require (
|
||||
github.com/prometheus/common v0.70.1 // indirect
|
||||
github.com/prometheus/procfs v0.21.1 // indirect
|
||||
github.com/vishvananda/netns v0.0.5 // indirect
|
||||
golang.org/x/mod v0.36.0 // indirect
|
||||
golang.org/x/time v0.5.0 // indirect
|
||||
golang.org/x/tools v0.45.0 // indirect
|
||||
)
|
||||
@@ -3,6 +3,8 @@ dario.cat/mergo v1.0.2 h1:85+piFYR1tMbRrLcDwR18y4UKJ3aH1Tbzi24VRW1TK8=
|
||||
dario.cat/mergo v1.0.2/go.mod h1:E/hbnu0NxMFBjpMIE34DRGLWqDy0g5FuKDhCb31ngxA=
|
||||
filippo.io/bigmod v0.1.0 h1:UNzDk7y9ADKST+axd9skUpBQeW7fG2KrTZyOE4uGQy8=
|
||||
filippo.io/bigmod v0.1.0/go.mod h1:OjOXDNlClLblvXdwgFFOQFJEocLhhtai8vGLy0JCZlI=
|
||||
github.com/DefinedNet/netlink v1.3.2-dn.1 h1:Md2PsUE1Xuy1Zxs/Bm6356n1kt5bq2HspXlPJ6pEspg=
|
||||
github.com/DefinedNet/netlink v1.3.2-dn.1/go.mod h1:o4YSn8Jw7zoDcYXDmV77TRd4eS5sSLuE+vu2zAN67C8=
|
||||
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||
@@ -23,8 +25,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/flynn/noise v1.1.0 h1:KjPQoQCEFdZDiP03phOvGi11+SVVhBG2wOWAorLsstg=
|
||||
github.com/flynn/noise v1.1.0/go.mod h1:xbMo+0i6+IGbYdJhF31t2eR1BIU0CYc12+BNAKwUTag=
|
||||
github.com/gaissmai/bart v0.28.0 h1:89yZLo8NmyqD0RYgJ3QO9HhqqGGw+oWhf90cZm69Lko=
|
||||
github.com/gaissmai/bart v0.28.0/go.mod h1:GREWQfTLRWz/c5FTOsIw+KkscuFkIV5t8Rp7Nd1Td5c=
|
||||
github.com/gaissmai/bart v0.29.0 h1:wO6HGE8g9YE0Wm0bCpYxwRzfQ4+fbJKOhL64e5ACGCI=
|
||||
github.com/gaissmai/bart v0.29.0/go.mod h1:GREWQfTLRWz/c5FTOsIw+KkscuFkIV5t8Rp7Nd1Td5c=
|
||||
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
|
||||
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
|
||||
@@ -81,8 +83,8 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
|
||||
github.com/miekg/dns v1.1.72 h1:vhmr+TF2A3tuoGNkLDFK9zi36F2LS+hKTRW0Uf8kbzI=
|
||||
github.com/miekg/dns v1.1.72/go.mod h1:+EuEPhdHOsfk6Wk5TT2CzssZdqkmFhf8r+aVyDEToIs=
|
||||
github.com/miekg/dns v1.1.73 h1:uhT8nJxmTrPJYClxVxTCX+CVn6qnzSiybRk72Z6DgrE=
|
||||
github.com/miekg/dns v1.1.73/go.mod h1:RW2Obtfd5NZHvOFe3zYG0W8koWOQtAzyHaLo8vASBuQ=
|
||||
github.com/miekg/pkcs11 v1.1.2 h1:/VxmeAX5qU6Q3EwafypogwWbYryHFmF2RpkJmw3m4MQ=
|
||||
github.com/miekg/pkcs11 v1.1.2/go.mod h1:XsNlhZGX73bx86s2hdc/FuaLm2CPZJemRLMA+WTFxgs=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
@@ -136,10 +138,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.12.0 h1:K6Mr6jO9JICuend/5xzTM03ydSV3vdNRYAdPSukj8uI=
|
||||
github.com/stretchr/testify v1.12.0/go.mod h1:bOYBZb5qJ00vPzWfIqBUZPaxK8jWiXc6d3ErP4Ca9Gw=
|
||||
github.com/vishvananda/netlink v1.3.1 h1:3AEMt62VKqz90r0tmNhog0r/PpWKmrEShJU0wJW6bV0=
|
||||
github.com/vishvananda/netlink v1.3.1/go.mod h1:ARtKouGSTGchR8aMwmkzC0qiNPrrWO5JS/XMVl45+b4=
|
||||
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
|
||||
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
|
||||
github.com/vishvananda/netns v0.0.5 h1:DfiHV+j8bA32MFM7bfEunvT8IAqQ/NzSJHtcmW5zdEY=
|
||||
github.com/vishvananda/netns v0.0.5/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
@@ -155,14 +155,12 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
||||
golang.org/x/crypto v0.54.0 h1:YLIA59K4fiNzHzjnZt2tUJQjQtUWfWbeHBqKtk3eScw=
|
||||
golang.org/x/crypto v0.54.0/go.mod h1:KWL8ny2AZdGR2cWmzeHrp2azQPGogOv+HeQaVEXC2dk=
|
||||
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
|
||||
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
|
||||
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
|
||||
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.36.0 h1:JJjpVx6myfUsUdAzZuOSTTmRE0PfZeNWzzvKrP7amb4=
|
||||
golang.org/x/mod v0.36.0/go.mod h1:moc6ELqsWcOw5Ef3xVprK5ul/MvtVvkIXLziUOICjUQ=
|
||||
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||
@@ -173,8 +171,8 @@ golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLL
|
||||
golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.57.0 h1:K5+3DljvIuDG9/Jv9rvyMywYNFCQ9RSUY6OOTTkT+tE=
|
||||
golang.org/x/net v0.57.0/go.mod h1:KpXc8iv+r3XplLAG/f7Jsf9RPszJzdR0f58q9vGOuEU=
|
||||
golang.org/x/net v0.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues=
|
||||
golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
|
||||
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
|
||||
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
@@ -182,8 +180,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
|
||||
golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||
golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk=
|
||||
golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
||||
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
@@ -197,13 +195,11 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
|
||||
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
||||
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.45.0 h1:NwWyBmoJCbfTHpxrWoZ9C6/VxOf7ic219I8xZZFdrf0=
|
||||
golang.org/x/term v0.45.0/go.mod h1:9aqxs0blBcrm/n0L9QW0aRVD+ktan8ssZromtqJC43w=
|
||||
golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE=
|
||||
golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
@@ -214,8 +210,6 @@ golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtn
|
||||
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
|
||||
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/tools v0.45.0 h1:18qN3FAooORvApf5XjCXgsuayZOEtXf6JK18I3+ONa8=
|
||||
golang.org/x/tools v0.45.0/go.mod h1:LuUGqqaXcXMEFEruIVJVm5mgDD8vww/z/SR1gQ4uE/0=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
@@ -234,8 +228,8 @@ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miE
|
||||
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
|
||||
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
|
||||
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
|
||||
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
||||
+17
-59
@@ -6,7 +6,6 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/netip"
|
||||
"slices"
|
||||
"sync"
|
||||
@@ -18,7 +17,6 @@ import (
|
||||
"github.com/slackhq/nebula/cert"
|
||||
"github.com/slackhq/nebula/config"
|
||||
"github.com/slackhq/nebula/header"
|
||||
"github.com/slackhq/nebula/logging"
|
||||
)
|
||||
|
||||
const defaultPromoteEvery = 1000 // Count of packets sent before we try moving a tunnel to a preferred underlay ip address
|
||||
@@ -71,7 +69,9 @@ type HostMap struct {
|
||||
Hosts map[netip.Addr]*HostInfo
|
||||
moreHosts map[netip.Addr][]*HostInfo
|
||||
preferredRanges atomic.Pointer[[]netip.Prefix]
|
||||
l *slog.Logger
|
||||
// established hears about each hostinfo that joins or leaves the hostmap; nil when no conn wants to know.
|
||||
established *establishedRemotes
|
||||
l *slog.Logger
|
||||
}
|
||||
|
||||
// For synchronization, treat the pointed-to Relay struct as immutable. To edit the Relay
|
||||
@@ -278,6 +278,11 @@ type HostInfo struct {
|
||||
// This value will be behind against actual tunnel utilization in the hot path.
|
||||
// This should only be used by the ConnectionManagers ticker routine.
|
||||
lastUsed time.Time
|
||||
|
||||
// establishedIn is set while the hostinfo is in a main hostmap whose conns track established remotes, and
|
||||
// establishedRemote is the remote it holds a reference on there, guarded by establishedIn's mu.
|
||||
establishedIn atomic.Pointer[establishedRemotes]
|
||||
establishedRemote netip.AddrPort
|
||||
}
|
||||
|
||||
type ViaSender struct {
|
||||
@@ -518,6 +523,7 @@ func (hm *HostMap) unlockedDeleteHostInfo(hostinfo *HostInfo) bool {
|
||||
if len(hm.Indexes) == 0 {
|
||||
hm.Indexes = map[uint32]*HostInfo{}
|
||||
}
|
||||
hm.established.remove(hostinfo)
|
||||
|
||||
if hm.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||
hm.l.Debug("Hostmap hostInfo deleted",
|
||||
@@ -664,6 +670,7 @@ func (hm *HostMap) unlockedAddHostInfo(hostinfo *HostInfo, f *Interface) {
|
||||
|
||||
hm.Indexes[hostinfo.localIndexId] = hostinfo
|
||||
hm.RemoteIndexes[hostinfo.remoteIndexId] = hostinfo
|
||||
hm.established.add(hostinfo)
|
||||
|
||||
hostinfo.markOut(f.rebindEpoch.Load())
|
||||
if f.connectionManager != nil { // f.connectionManager is only nil in some unit tests
|
||||
@@ -844,6 +851,13 @@ func (i *HostInfo) SetRemote(remote netip.AddrPort) {
|
||||
if i.GetRemote() != remote {
|
||||
i.remote.Store(&remote)
|
||||
i.remotes.LearnRemote(i.vpnAddrs[0], remote)
|
||||
if e := i.establishedIn.Load(); e != nil {
|
||||
e.mu.Lock()
|
||||
if i.establishedIn.Load() == e {
|
||||
e.syncLocked(i)
|
||||
}
|
||||
e.mu.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -930,59 +944,3 @@ func (i *HostInfo) logger(l *slog.Logger) *slog.Logger {
|
||||
|
||||
return li
|
||||
}
|
||||
|
||||
// Utility functions
|
||||
|
||||
func localAddrs(l *slog.Logger, allowList *LocalAllowList) []netip.Addr {
|
||||
//FIXME: This function is pretty garbage
|
||||
var finalAddrs []netip.Addr
|
||||
ifaces, _ := net.Interfaces()
|
||||
for _, i := range ifaces {
|
||||
allow := allowList.AllowName(i.Name)
|
||||
if l.Enabled(context.Background(), logging.LevelTrace) {
|
||||
l.Log(context.Background(), logging.LevelTrace, "localAllowList.AllowName",
|
||||
"interfaceName", i.Name,
|
||||
"allow", allow,
|
||||
)
|
||||
}
|
||||
|
||||
if !allow {
|
||||
continue
|
||||
}
|
||||
addrs, _ := i.Addrs()
|
||||
for _, rawAddr := range addrs {
|
||||
var addr netip.Addr
|
||||
switch v := rawAddr.(type) {
|
||||
case *net.IPNet:
|
||||
//continue
|
||||
addr, _ = netip.AddrFromSlice(v.IP)
|
||||
case *net.IPAddr:
|
||||
addr, _ = netip.AddrFromSlice(v.IP)
|
||||
}
|
||||
|
||||
if !addr.IsValid() {
|
||||
if l.Enabled(context.Background(), slog.LevelDebug) {
|
||||
l.Debug("addr was invalid", "localAddr", rawAddr)
|
||||
}
|
||||
continue
|
||||
}
|
||||
addr = addr.Unmap()
|
||||
|
||||
if addr.IsLoopback() == false && addr.IsLinkLocalUnicast() == false {
|
||||
isAllowed := allowList.Allow(addr)
|
||||
if l.Enabled(context.Background(), logging.LevelTrace) {
|
||||
l.Log(context.Background(), logging.LevelTrace, "localAllowList.Allow",
|
||||
"localAddr", addr,
|
||||
"allowed", isAllowed,
|
||||
)
|
||||
}
|
||||
if !isAllowed {
|
||||
continue
|
||||
}
|
||||
|
||||
finalAddrs = append(finalAddrs, addr)
|
||||
}
|
||||
}
|
||||
}
|
||||
return finalAddrs
|
||||
}
|
||||
@@ -432,6 +432,11 @@ func (f *Interface) pinThisThread(i int) {
|
||||
}
|
||||
|
||||
func (f *Interface) listenIn(queue tio.Queue, i int) {
|
||||
if sendPipelinePlatform {
|
||||
listenInPipelined(f, queue, i)
|
||||
return
|
||||
}
|
||||
|
||||
// Pinning this thread (and goroutine) to a single CPU keeps every sendmmsg from this goroutine going through the
|
||||
// same TX ring on the nic, so the wire sees per-flow order. Skip entirely when tun.pin_threads is false.
|
||||
if f.pinThreads {
|
||||
@@ -478,6 +483,11 @@ func (f *Interface) listenIn(queue tio.Queue, i int) {
|
||||
func (f *Interface) flushSendBatch(sb *batch.SendBatch, q int) {
|
||||
queued := sb.Len()
|
||||
written, err := sb.Flush()
|
||||
f.accountSendBatch(queued, written, err, q)
|
||||
}
|
||||
|
||||
// accountSendBatch is flushSendBatch's accounting, also for the writes a send pipeline makes.
|
||||
func (f *Interface) accountSendBatch(queued, written int, err error, q int) {
|
||||
if err != nil {
|
||||
f.l.Error("Failed to write outgoing batch", "error", err, "writer", q)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
//go:build darwin || send_pipeline
|
||||
|
||||
package nebula
|
||||
|
||||
import (
|
||||
"github.com/slackhq/nebula/firewall"
|
||||
"github.com/slackhq/nebula/overlay/batch"
|
||||
"github.com/slackhq/nebula/overlay/tio"
|
||||
"github.com/slackhq/nebula/udp"
|
||||
)
|
||||
|
||||
// sendPipelinePlatform lets listenIn take the pipelined send path. It is on for darwin, and for any platform built
|
||||
// with -tags send_pipeline so the pipeline's tests (and the e2e suite through it) run on linux too.
|
||||
const sendPipelinePlatform = true
|
||||
|
||||
// listenInPipelined is f.listenIn with the underlay writes moved to a sender goroutine (batch.SendPipeline). This
|
||||
// goroutine still reads the tun, runs the firewall and encrypts, straight into the pipeline's batch. The sender
|
||||
// writes whatever has built up each time its previous write returns, so under load the packets read while one
|
||||
// write is in the kernel leave together in the next one, and the next tun read overlaps the write.
|
||||
func listenInPipelined(f *Interface, queue tio.Queue, i int) {
|
||||
if f.pinThreads {
|
||||
f.pinThisThread(i)
|
||||
}
|
||||
|
||||
rejectBuf := make([]byte, mtu)
|
||||
arenaSize := batch.SendBatchCap * (udp.MTU + 32)
|
||||
onWrite := func(queued, written int, err error) {
|
||||
f.accountSendBatch(queued, written, err, i)
|
||||
}
|
||||
p := batch.NewSendPipeline(f.writers[i], batch.SendBatchCap, arenaSize, udp.MaxWriteBatch, onWrite)
|
||||
defer p.Close()
|
||||
fwPacket := &firewall.ParsedPacket{}
|
||||
nb := make([]byte, 12, 12)
|
||||
|
||||
conntrackCache := firewall.NewConntrackCacheTicker(f.ctx, f.l, f.conntrackCacheTimeout)
|
||||
|
||||
for {
|
||||
pkts, err := queue.Read()
|
||||
if err != nil {
|
||||
// Same shutdown noise handling as listenOut
|
||||
if !f.closed.Load() && f.ctx.Err() == nil {
|
||||
f.l.Error("Error while reading outbound packet, closing", "error", err, "reader", i)
|
||||
f.onFatal(err)
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
for j, pkt := range pkts {
|
||||
// One Begin/End per packet holds the batch only briefly, so a sender that finishes a write mid-read
|
||||
// takes what is ready then. An idle sender is woken once, at the end of the read, to take it whole.
|
||||
sb := p.Begin()
|
||||
f.consumeInsidePacket(pkt, fwPacket, nb, sb, rejectBuf, i, conntrackCache.Get())
|
||||
p.End(j == len(pkts)-1)
|
||||
}
|
||||
}
|
||||
|
||||
f.l.Debug("overlay reader is done", "reader", i)
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
//go:build darwin || send_pipeline
|
||||
|
||||
package nebula
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/netip"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/slackhq/nebula/overlay/tio"
|
||||
"github.com/slackhq/nebula/test"
|
||||
"github.com/slackhq/nebula/udp"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"go.uber.org/goleak"
|
||||
)
|
||||
|
||||
// scriptedQueue hands out reads from a script, then fails every Read with err.
|
||||
type scriptedQueue struct {
|
||||
reads [][]tio.Packet
|
||||
err error
|
||||
}
|
||||
|
||||
func (q *scriptedQueue) Read() ([]tio.Packet, error) {
|
||||
if len(q.reads) == 0 {
|
||||
return nil, q.err
|
||||
}
|
||||
r := q.reads[0]
|
||||
q.reads = q.reads[1:]
|
||||
return r, nil
|
||||
}
|
||||
func (q *scriptedQueue) Write(b []byte) (int, error) { return len(b), nil }
|
||||
func (q *scriptedQueue) Close() error { return nil }
|
||||
|
||||
type countingConn struct {
|
||||
udp.NoopConn
|
||||
mu sync.Mutex
|
||||
batches int
|
||||
}
|
||||
|
||||
func (c *countingConn) WriteBatch(bufs [][]byte, _ []netip.AddrPort) (int, error) {
|
||||
c.mu.Lock()
|
||||
c.batches++
|
||||
c.mu.Unlock()
|
||||
return len(bufs), nil
|
||||
}
|
||||
|
||||
// TestListenInPipelinedStops pins that the pipelined reader returns when its queue fails, stopping its sender, and
|
||||
// reports the error as fatal only when the interface isn't shutting down.
|
||||
func TestListenInPipelinedStops(t *testing.T) {
|
||||
for _, closing := range []bool{false, true} {
|
||||
t.Run(map[bool]string{false: "fatal", true: "shutdown"}[closing], func(t *testing.T) {
|
||||
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
|
||||
readErr := errors.New("tun gone")
|
||||
conn := &countingConn{}
|
||||
// Packets too short to parse are dropped before the firewall, so this reads without a hostmap.
|
||||
junk := tio.Packet{Bytes: []byte{0x45, 0}}
|
||||
q := &scriptedQueue{reads: [][]tio.Packet{{junk}, {junk, junk, junk}, {}}, err: readErr}
|
||||
shutdown := make(chan struct{})
|
||||
f := &Interface{
|
||||
ctx: context.Background(),
|
||||
l: test.NewLogger(),
|
||||
writers: []udp.Conn{conn},
|
||||
triggerShutdown: func() { close(shutdown) },
|
||||
}
|
||||
f.closed.Store(closing)
|
||||
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
f.listenIn(q, 0)
|
||||
close(done)
|
||||
}()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("listenIn did not return after its queue failed")
|
||||
}
|
||||
if closing {
|
||||
assert.Nil(t, f.fatalErr.Load())
|
||||
} else {
|
||||
<-shutdown
|
||||
assert.Equal(t, readErr, *f.fatalErr.Load())
|
||||
}
|
||||
assert.Zero(t, conn.batches, "dropped packets must not be written")
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// Package msgx calls xnu's private batched socket calls, sendmsg_x and recvmsg_x, which move many datagrams per
|
||||
// syscall. They are not public API, so a kernel or sandbox may refuse them; callers fall back to one datagram per
|
||||
// syscall when Unsupported reports that.
|
||||
package msgx
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// Batch is the most messages the callers hand one sendmsg_x or recvmsg_x call.
|
||||
const Batch = 64
|
||||
|
||||
// Hdr mirrors xnu's struct msghdr_x (bsd/sys/socket_private.h), which the SDK doesn't ship. recvmsg_x reports each
|
||||
// message's length in Datalen rather than in its return value.
|
||||
type Hdr struct {
|
||||
Name *byte
|
||||
Namelen uint32
|
||||
Iov *unix.Iovec
|
||||
Iovlen int32
|
||||
Control *byte
|
||||
Controllen uint32
|
||||
Flags int32
|
||||
Datalen uint64
|
||||
}
|
||||
|
||||
// Send passes hdrs to sendmsg_x on fd and returns how many messages the kernel took. EINTR is retried.
|
||||
func Send(fd uintptr, hdrs []Hdr) (int, syscall.Errno) {
|
||||
return call(unix.SYS_SENDMSG_X, fd, hdrs)
|
||||
}
|
||||
|
||||
// Recv passes hdrs to recvmsg_x on fd and returns how many messages it filled. EINTR is retried.
|
||||
func Recv(fd uintptr, hdrs []Hdr) (int, syscall.Errno) {
|
||||
return call(unix.SYS_RECVMSG_X, fd, hdrs)
|
||||
}
|
||||
|
||||
func call(trap, fd uintptr, hdrs []Hdr) (int, syscall.Errno) {
|
||||
if len(hdrs) == 0 {
|
||||
return 0, syscall.EINVAL
|
||||
}
|
||||
for {
|
||||
r, _, errno := unix.Syscall6(trap, fd, uintptr(unsafe.Pointer(&hdrs[0])), uintptr(len(hdrs)), 0, 0, 0)
|
||||
switch errno {
|
||||
case 0:
|
||||
return int(r), 0
|
||||
case syscall.EINTR:
|
||||
default:
|
||||
return 0, errno
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Unsupported reports whether errno means the kernel or a sandbox refuses the private calls.
|
||||
func Unsupported(errno syscall.Errno) bool {
|
||||
return errno == syscall.ENOSYS || errno == syscall.EPERM || errno == syscall.EOPNOTSUPP
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package msgx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"syscall"
|
||||
"testing"
|
||||
"unsafe"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// TestHdrLayout pins Hdr to struct msghdr_x's LP64 layout: 56 bytes, Datalen last at 48.
|
||||
func TestHdrLayout(t *testing.T) {
|
||||
var h Hdr
|
||||
assert.EqualValues(t, 56, unsafe.Sizeof(h))
|
||||
assert.EqualValues(t, 48, unsafe.Offsetof(h.Datalen))
|
||||
}
|
||||
|
||||
// TestSendRecvRoundTrip sends a batch through sendmsg_x in one call and reads it back through recvmsg_x in one
|
||||
// call, each message whole, in order, with Datalen set.
|
||||
func TestSendRecvRoundTrip(t *testing.T) {
|
||||
fds, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_DGRAM, 0)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
_ = unix.Close(fds[0])
|
||||
_ = unix.Close(fds[1])
|
||||
})
|
||||
// A dropped datagram fails the receive instead of hanging it.
|
||||
require.NoError(t, unix.SetsockoptTimeval(fds[1], unix.SOL_SOCKET, unix.SO_RCVTIMEO, &unix.Timeval{Sec: 2}))
|
||||
|
||||
const n = 5
|
||||
var want [n][]byte
|
||||
var iovs [n]unix.Iovec
|
||||
hdrs := make([]Hdr, n)
|
||||
for i := range n {
|
||||
want[i] = bytes.Repeat([]byte{byte(i + 1)}, 10+i)
|
||||
iovs[i] = unix.Iovec{Base: &want[i][0]}
|
||||
iovs[i].SetLen(len(want[i]))
|
||||
hdrs[i] = Hdr{Iov: &iovs[i], Iovlen: 1}
|
||||
}
|
||||
sent, errno := Send(uintptr(fds[0]), hdrs)
|
||||
require.Zero(t, errno)
|
||||
require.Equal(t, n, sent)
|
||||
|
||||
var bufs [n + 1][64]byte
|
||||
var riovs [n + 1]unix.Iovec
|
||||
rhdrs := make([]Hdr, n+1)
|
||||
for i := range rhdrs {
|
||||
riovs[i] = unix.Iovec{Base: &bufs[i][0]}
|
||||
riovs[i].SetLen(len(bufs[i]))
|
||||
rhdrs[i] = Hdr{Iov: &riovs[i], Iovlen: 1}
|
||||
}
|
||||
got, errno := Recv(uintptr(fds[1]), rhdrs)
|
||||
require.Zero(t, errno)
|
||||
require.Equal(t, n, got)
|
||||
for i := range n {
|
||||
l := int(rhdrs[i].Datalen)
|
||||
assert.Equal(t, want[i], bufs[i][:l], "message %d", i)
|
||||
}
|
||||
}
|
||||
|
||||
// TestEmptyBatch pins that an empty batch is refused before it reaches the kernel.
|
||||
func TestEmptyBatch(t *testing.T) {
|
||||
_, errno := Send(0, nil)
|
||||
assert.Equal(t, syscall.EINVAL, errno)
|
||||
_, errno = Recv(0, nil)
|
||||
assert.Equal(t, syscall.EINVAL, errno)
|
||||
}
|
||||
|
||||
func TestUnsupported(t *testing.T) {
|
||||
for _, e := range []syscall.Errno{syscall.ENOSYS, syscall.EPERM, syscall.EOPNOTSUPP} {
|
||||
assert.True(t, Unsupported(e), "%v", e)
|
||||
}
|
||||
for _, e := range []syscall.Errno{0, syscall.EAGAIN, syscall.ENOBUFS, syscall.EINVAL} {
|
||||
assert.False(t, Unsupported(e), "%v", e)
|
||||
}
|
||||
}
|
||||
+3
-13
@@ -45,8 +45,7 @@ func setTransportChecksum4(packet []byte) {
|
||||
if ihl < ipv4.HeaderLen || end < ihl || end > len(packet) {
|
||||
return
|
||||
}
|
||||
// The checksum covers the whole datagram, which a fragment (MF set or a
|
||||
// non-zero offset) does not carry.
|
||||
// do not attempt to set checksums for fragments
|
||||
if binary.BigEndian.Uint16(packet[6:8])&0x3fff != 0 {
|
||||
return
|
||||
}
|
||||
@@ -68,10 +67,7 @@ func setTransportChecksum6(packet []byte) {
|
||||
return
|
||||
}
|
||||
|
||||
// The checksum covers the whole datagram, which a fragment does not carry.
|
||||
// An unknown extension header hides where the transport header starts. A
|
||||
// chain longer than the walk's budget ends it early, at an offset that was
|
||||
// never checked against the packet.
|
||||
// we must find the transport header in order to write a checksum
|
||||
proto, offset, _, anyFragment, err := IPv6FindUpperProtocol(packet[:end])
|
||||
if err != nil || anyFragment || offset >= end {
|
||||
return
|
||||
@@ -109,8 +105,7 @@ func transportExtent(transport []byte, proto uint8) ([]byte, bool) {
|
||||
// writeTransportChecksum stores the checksum of transport, taken over the
|
||||
// pseudo-header sum csum, in the header's checksum field. A UDP checksum that
|
||||
// computes to zero goes on the wire as 0xffff: zero means no checksum was
|
||||
// computed (RFC 768), and over IPv6 the checksum is mandatory (RFC 8200
|
||||
// section 8.1).
|
||||
// computed (RFC 768), and over IPv6 the checksum is mandatory (RFC 8200 section 8.1).
|
||||
func writeTransportChecksum(transport []byte, proto uint8, csum uint32) {
|
||||
var at, minLen int
|
||||
switch proto {
|
||||
@@ -133,11 +128,6 @@ func writeTransportChecksum(transport []byte, proto uint8, csum uint32) {
|
||||
binary.BigEndian.PutUint16(transport[at:], sum)
|
||||
}
|
||||
|
||||
// fold reduces a pseudo-header sum to the 16 bit seed Checksum takes. Carrying
|
||||
// the high half back into the low half is what keeps the reduction lossless, so
|
||||
// the seed sums exactly as the wider value would; 0xffff is its fixed point.
|
||||
// Every term of that sum comes from a 16 bit field, so it stays far below the
|
||||
// width at which the accumulator would wrap.
|
||||
func fold(csum uint32) uint16 {
|
||||
for csum > 0xffff {
|
||||
csum = (csum >> 16) + (csum & 0xffff)
|
||||
|
||||
+13
-16
@@ -345,14 +345,11 @@ func ipv6CreateRejectTCPPacket(packet []byte, out []byte, offset int) []byte {
|
||||
return out
|
||||
}
|
||||
|
||||
// IPv6FindUpperProtocol walks the ipv6 extension header chain and returns the upper layer protocol, the
|
||||
// offset it begins at, and whether the packet is a non-first fragment. Only the RFC 8200 and IANA extension
|
||||
// headers below are walked. Everything else, including Mobility (135), HIP (139), Shim6 (140), experimental
|
||||
// 253/254, and real upper layer protocols like SCTP or GRE, is terminal. Walking those as extension headers
|
||||
// is a firewall bypass, so they fail closed. 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 before a
|
||||
// terminal protocol is reached.
|
||||
// IPv6FindUpperProtocol walks the ipv6 extension header chain and returns the upper layer protocol, the offset it begins at, and whether the packet is a non-first fragment.
|
||||
// Only the RFC 8200 and IANA extension headers below are walked.
|
||||
// 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.
|
||||
// Returns ErrIPv6CouldNotFindPayload if packet is smaller than an ipv6 header, or the chain is truncated, or if more than 8 extension headers are present.
|
||||
func IPv6FindUpperProtocol(packet []byte) (nextHeader uint8, offset int, isFragment bool, anyFragment bool, err error) {
|
||||
const maxIPv6ExtHeaders = 8
|
||||
if len(packet) < ipv6.HeaderLen {
|
||||
@@ -365,14 +362,14 @@ func IPv6FindUpperProtocol(packet []byte) (nextHeader uint8, offset int, isFragm
|
||||
switch nextHeader {
|
||||
case 0, 43, 60: // Hop-by-Hop, Routing, Destination
|
||||
if len(packet) < offset+2 {
|
||||
return nextHeader, offset, isFragment, anyFragment, ErrIPv6CouldNotFindPayload
|
||||
return 0, 0, false, false, ErrIPv6CouldNotFindPayload
|
||||
}
|
||||
nextHeader = packet[offset]
|
||||
offset += (int(packet[offset+1]) + 1) << 3
|
||||
|
||||
case 44: // Fragment
|
||||
if len(packet) < offset+8 {
|
||||
return nextHeader, offset, isFragment, anyFragment, ErrIPv6CouldNotFindPayload
|
||||
return 0, 0, false, false, ErrIPv6CouldNotFindPayload
|
||||
}
|
||||
anyFragment = true
|
||||
// Non-first fragments carry no transport header, report the fragmented protocol and stop
|
||||
@@ -384,21 +381,21 @@ func IPv6FindUpperProtocol(packet []byte) (nextHeader uint8, offset int, isFragm
|
||||
|
||||
case 51: // AH
|
||||
if len(packet) < offset+2 {
|
||||
return nextHeader, offset, isFragment, anyFragment, ErrIPv6CouldNotFindPayload
|
||||
return 0, 0, false, false, ErrIPv6CouldNotFindPayload
|
||||
}
|
||||
nextHeader = packet[offset]
|
||||
offset += (int(packet[offset+1]) + 2) << 2
|
||||
|
||||
default:
|
||||
// A prior extension header can declare a length that advances offset past the packet. The terminal
|
||||
// protocol's header isn't actually here, so treat the chain as truncated rather than classifying it.
|
||||
if offset > len(packet) {
|
||||
return nextHeader, offset, isFragment, anyFragment, ErrIPv6CouldNotFindPayload
|
||||
// A prior extension header can declare a length that advances offset past the packet, reject this.
|
||||
return 0, 0, false, false, ErrIPv6CouldNotFindPayload
|
||||
}
|
||||
return nextHeader, offset, isFragment, anyFragment, nil
|
||||
return nextHeader, offset, isFragment, anyFragment, nil //success!
|
||||
}
|
||||
}
|
||||
return nextHeader, offset, isFragment, anyFragment, nil
|
||||
// Fail closed
|
||||
return 0, 0, false, false, ErrIPv6CouldNotFindPayload
|
||||
}
|
||||
|
||||
func CreateICMPEchoResponse(packet, out []byte) []byte {
|
||||
|
||||
+17
-5
@@ -529,6 +529,17 @@ func Test_IPv6FindUpperProtocol(t *testing.T) {
|
||||
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
|
||||
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
|
||||
|
||||
// 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
|
||||
// from the attacker's bytes there.
|
||||
var budgetChain []byte
|
||||
for range 7 {
|
||||
budgetChain = append(budgetChain, extToHbH...)
|
||||
}
|
||||
budgetChain = append(budgetChain, extToTCP...)
|
||||
budgetChain = append(budgetChain, transport...)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -549,9 +560,10 @@ func Test_IPv6FindUpperProtocol(t *testing.T) {
|
||||
{"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},
|
||||
{"unknown protocol is terminal", 132, transport, 132, ipv6.HeaderLen, false, false, nil}, // SCTP
|
||||
{"truncated extension header", 0, nil, 0, ipv6.HeaderLen, 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
|
||||
{"extension length past buffer", 60, []byte{132, 255, 0, 0, 0, 0, 0, 0}, 132, ipv6.HeaderLen + 2048, 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},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -559,10 +571,10 @@ func Test_IPv6FindUpperProtocol(t *testing.T) {
|
||||
packet := makeIPv6Packet(src, dst, tt.nextHeader, tt.payload)
|
||||
proto, offset, isFragment, anyFragment, err := IPv6FindUpperProtocol(packet)
|
||||
if tt.wantErr != nil {
|
||||
assert.ErrorIs(t, err, tt.wantErr)
|
||||
return
|
||||
require.ErrorIs(t, err, tt.wantErr)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tt.wantProto, proto)
|
||||
assert.Equal(t, tt.wantOffset, offset)
|
||||
assert.Equal(t, tt.wantFragment, isFragment)
|
||||
|
||||
+43
-2
@@ -19,6 +19,7 @@ import (
|
||||
"github.com/slackhq/nebula/config"
|
||||
"github.com/slackhq/nebula/header"
|
||||
"github.com/slackhq/nebula/logging"
|
||||
"github.com/slackhq/nebula/nicfinder"
|
||||
"github.com/slackhq/nebula/udp"
|
||||
"github.com/slackhq/nebula/util"
|
||||
)
|
||||
@@ -34,12 +35,18 @@ type LightHouse struct {
|
||||
|
||||
myVpnNetworks []netip.Prefix
|
||||
myVpnNetworksTable *bart.Lite
|
||||
punchy *Punchy
|
||||
// myVpnAddrsTable contains our overlay host addrs, as opposed to the overlay networks
|
||||
myVpnAddrsTable *bart.Lite
|
||||
punchy *Punchy
|
||||
|
||||
// localAddrsFn enumerates the underlay addresses we advertise. It is a field so tests can supply simulated
|
||||
// addresses rather than whatever this machine's NICs happen to be. Set it before Start.
|
||||
localAddrsFn func(*LocalAllowList) []netip.Addr
|
||||
|
||||
// lastLocalAddrsErr is the previous localAddrsFn failure. Enumeration runs on every update, so an
|
||||
// unchanged failure is demoted to Debug rather than warning every lighthouse.interval forever.
|
||||
lastLocalAddrsErr atomic.Pointer[string]
|
||||
|
||||
// Local cache of answers from light houses
|
||||
// map of vpn addr to answers
|
||||
addrMap map[netip.Addr]*RemoteList
|
||||
@@ -104,6 +111,7 @@ func NewLightHouseFromConfig(ctx context.Context, l *slog.Logger, c *config.C, c
|
||||
amLighthouse: amLighthouse,
|
||||
myVpnNetworks: cs.myVpnNetworks,
|
||||
myVpnNetworksTable: cs.myVpnNetworksTable,
|
||||
myVpnAddrsTable: cs.myVpnAddrsTable,
|
||||
addrMap: make(map[netip.Addr]*RemoteList),
|
||||
nebulaPort: nebulaPort,
|
||||
punchy: p,
|
||||
@@ -112,7 +120,9 @@ func NewLightHouseFromConfig(ctx context.Context, l *slog.Logger, c *config.C, c
|
||||
l: l,
|
||||
}
|
||||
h.localAddrsFn = func(al *LocalAllowList) []netip.Addr {
|
||||
return localAddrs(h.l, al)
|
||||
addrs, err := nicfinder.Find(ctx, h.l, al)
|
||||
h.logLocalAddrsErr(err)
|
||||
return addrs
|
||||
}
|
||||
|
||||
lighthouses := make([]netip.Addr, 0)
|
||||
@@ -913,6 +923,26 @@ func (lh *LightHouse) TriggerUpdate() {
|
||||
}
|
||||
}
|
||||
|
||||
// logLocalAddrsErr reports a localAddrs failure at Warn the first time it is seen and at Debug while
|
||||
// it persists unchanged, so a permanent failure does not warn on every update forever.
|
||||
func (lh *LightHouse) logLocalAddrsErr(err error) {
|
||||
if err == nil {
|
||||
lh.lastLocalAddrsErr.Store(nil)
|
||||
return
|
||||
}
|
||||
|
||||
msg := err.Error()
|
||||
prev := lh.lastLocalAddrsErr.Swap(&msg)
|
||||
if prev != nil && *prev == msg {
|
||||
if lh.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||
lh.l.Debug("Failed to collect local addresses to advertise", "error", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
lh.l.Warn("Failed to collect local addresses to advertise", "error", err)
|
||||
}
|
||||
|
||||
func (lh *LightHouse) SendUpdate() {
|
||||
var v4 []*V4AddrPort
|
||||
var v6 []*V6AddrPort
|
||||
@@ -1158,6 +1188,17 @@ func (lhh *LightHouseHandler) handleHostQuery(n *NebulaMeta, fromVpnAddrs []neti
|
||||
return
|
||||
}
|
||||
|
||||
// Don't respond to requests for us.
|
||||
if lhh.lh.myVpnAddrsTable.Contains(queryVpnAddr) {
|
||||
if lhh.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||
lhh.l.Debug("Ignoring HostQuery for one of my own addresses",
|
||||
"fromVpnAddrs", fromVpnAddrs,
|
||||
"queryVpnAddr", queryVpnAddr,
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
found, ln, err := lhh.lh.queryAndPrepMessage(queryVpnAddr, func(c *cache) (int, error) {
|
||||
n = lhh.resetMeta()
|
||||
n.Type = NebulaMeta_HostQueryReply
|
||||
|
||||
+111
-54
@@ -1,7 +1,9 @@
|
||||
package nebula
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"testing"
|
||||
@@ -27,15 +29,27 @@ func TestOldIPv4Only(t *testing.T) {
|
||||
assert.Equal(t, binary.BigEndian.Uint32(bp[:]), m.GetAddr())
|
||||
}
|
||||
|
||||
func testCertState(networks ...netip.Prefix) *CertState {
|
||||
cs := &CertState{
|
||||
myVpnNetworks: networks,
|
||||
myVpnNetworksTable: new(bart.Lite),
|
||||
myVpnAddrs: make([]netip.Addr, 0, len(networks)),
|
||||
myVpnAddrsTable: new(bart.Lite),
|
||||
}
|
||||
|
||||
for _, n := range networks {
|
||||
cs.myVpnNetworksTable.Insert(n)
|
||||
cs.myVpnAddrs = append(cs.myVpnAddrs, n.Addr())
|
||||
cs.myVpnAddrsTable.Insert(netip.PrefixFrom(n.Addr(), n.Addr().BitLen()))
|
||||
}
|
||||
|
||||
return cs
|
||||
}
|
||||
|
||||
func Test_lhStaticMapping(t *testing.T) {
|
||||
l := test.NewLogger()
|
||||
myVpnNet := netip.MustParsePrefix("10.128.0.1/16")
|
||||
nt := new(bart.Lite)
|
||||
nt.Insert(myVpnNet)
|
||||
cs := &CertState{
|
||||
myVpnNetworks: []netip.Prefix{myVpnNet},
|
||||
myVpnNetworksTable: nt,
|
||||
}
|
||||
cs := testCertState(myVpnNet)
|
||||
lh1 := "10.128.0.2"
|
||||
|
||||
c := config.NewC(l)
|
||||
@@ -55,12 +69,7 @@ func Test_lhStaticMapping(t *testing.T) {
|
||||
func TestReloadLighthouseInterval(t *testing.T) {
|
||||
l := test.NewLogger()
|
||||
myVpnNet := netip.MustParsePrefix("10.128.0.1/16")
|
||||
nt := new(bart.Lite)
|
||||
nt.Insert(myVpnNet)
|
||||
cs := &CertState{
|
||||
myVpnNetworks: []netip.Prefix{myVpnNet},
|
||||
myVpnNetworksTable: nt,
|
||||
}
|
||||
cs := testCertState(myVpnNet)
|
||||
lh1 := "10.128.0.2"
|
||||
|
||||
c := config.NewC(l)
|
||||
@@ -90,12 +99,7 @@ func TestReloadLighthouseInterval(t *testing.T) {
|
||||
func BenchmarkLighthouseHandleRequest(b *testing.B) {
|
||||
l := test.NewLogger()
|
||||
myVpnNet := netip.MustParsePrefix("10.128.0.1/0")
|
||||
nt := new(bart.Lite)
|
||||
nt.Insert(myVpnNet)
|
||||
cs := &CertState{
|
||||
myVpnNetworks: []netip.Prefix{myVpnNet},
|
||||
myVpnNetworksTable: nt,
|
||||
}
|
||||
cs := testCertState(myVpnNet)
|
||||
|
||||
c := config.NewC(l)
|
||||
lh, err := NewLightHouseFromConfig(b.Context(), l, c, cs, nil, nil)
|
||||
@@ -195,12 +199,7 @@ func TestLighthouse_Memory(t *testing.T) {
|
||||
c.Settings["listen"] = map[string]any{"port": 4242}
|
||||
|
||||
myVpnNet := netip.MustParsePrefix("10.128.0.1/24")
|
||||
nt := new(bart.Lite)
|
||||
nt.Insert(myVpnNet)
|
||||
cs := &CertState{
|
||||
myVpnNetworks: []netip.Prefix{myVpnNet},
|
||||
myVpnNetworksTable: nt,
|
||||
}
|
||||
cs := testCertState(myVpnNet)
|
||||
lh, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil)
|
||||
lh.ifce = &mockEncWriter{}
|
||||
require.NoError(t, err)
|
||||
@@ -280,12 +279,7 @@ func TestLighthouse_reload(t *testing.T) {
|
||||
c.Settings["listen"] = map[string]any{"port": 4242}
|
||||
|
||||
myVpnNet := netip.MustParsePrefix("10.128.0.1/24")
|
||||
nt := new(bart.Lite)
|
||||
nt.Insert(myVpnNet)
|
||||
cs := &CertState{
|
||||
myVpnNetworks: []netip.Prefix{myVpnNet},
|
||||
myVpnNetworksTable: nt,
|
||||
}
|
||||
cs := testCertState(myVpnNet)
|
||||
|
||||
lh, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil)
|
||||
require.NoError(t, err)
|
||||
@@ -315,12 +309,7 @@ func TestLighthouse_reloadStaticHostMap(t *testing.T) {
|
||||
}
|
||||
|
||||
myVpnNet := netip.MustParsePrefix("10.128.0.1/24")
|
||||
nt := new(bart.Lite)
|
||||
nt.Insert(myVpnNet)
|
||||
cs := &CertState{
|
||||
myVpnNetworks: []netip.Prefix{myVpnNet},
|
||||
myVpnNetworksTable: nt,
|
||||
}
|
||||
cs := testCertState(myVpnNet)
|
||||
|
||||
lh, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil)
|
||||
require.NoError(t, err)
|
||||
@@ -429,7 +418,9 @@ func TestLighthouse_reloadStaticHostMap(t *testing.T) {
|
||||
assert.Equal(t, []netip.AddrPort{netip.MustParseAddrPort("3.3.3.3:4242")}, rl.CopyAddrs([]netip.Prefix{}))
|
||||
}
|
||||
|
||||
func newLHHostRequest(fromAddr netip.AddrPort, myVpnIp, queryVpnIp netip.Addr, lhh *LightHouseHandler) testLhReply {
|
||||
// sendLHHostRequest delivers a HostQuery to lhh and hands back the writer that
|
||||
// captured what it emitted. Pass a nil filter to see every message.
|
||||
func sendLHHostRequest(fromAddr netip.AddrPort, myVpnIp, queryVpnIp netip.Addr, lhh *LightHouseHandler, filter *NebulaMeta_MessageType) *testEncWriter {
|
||||
req := &NebulaMeta{
|
||||
Type: NebulaMeta_HostQuery,
|
||||
Details: &NebulaMetaDetails{},
|
||||
@@ -447,12 +438,59 @@ func newLHHostRequest(fromAddr netip.AddrPort, myVpnIp, queryVpnIp netip.Addr, l
|
||||
panic(err)
|
||||
}
|
||||
|
||||
filter := NebulaMeta_HostQueryReply
|
||||
w := &testEncWriter{
|
||||
metaFilter: &filter,
|
||||
}
|
||||
w := &testEncWriter{metaFilter: filter}
|
||||
lhh.HandleRequest(fromAddr, []netip.Addr{myVpnIp}, b, w)
|
||||
return w.lastReply
|
||||
return w
|
||||
}
|
||||
|
||||
func newLHHostRequest(fromAddr netip.AddrPort, myVpnIp, queryVpnIp netip.Addr, lhh *LightHouseHandler) testLhReply {
|
||||
filter := NebulaMeta_HostQueryReply
|
||||
return sendLHHostRequest(fromAddr, myVpnIp, queryVpnIp, lhh, &filter).lastReply
|
||||
}
|
||||
|
||||
func TestLighthouse_IgnoresHostQueryForItself(t *testing.T) {
|
||||
// Validate that we don't answer host queries for our own address.
|
||||
l := test.NewLogger()
|
||||
|
||||
myVpnNet := netip.MustParsePrefix("10.128.0.1/24")
|
||||
myVpnIp := myVpnNet.Addr()
|
||||
|
||||
c := config.NewC(l)
|
||||
c.Settings["lighthouse"] = map[string]any{"am_lighthouse": true}
|
||||
c.Settings["listen"] = map[string]any{"port": 4242}
|
||||
// Add a static_host_map entry for ourselves, so our address
|
||||
// is in the addrMap.
|
||||
c.Settings["static_host_map"] = map[string]any{
|
||||
myVpnIp.String(): []any{"192.168.100.1:4242"},
|
||||
}
|
||||
|
||||
lh, err := NewLightHouseFromConfig(t.Context(), l, c, testCertState(myVpnNet), nil, nil)
|
||||
require.NoError(t, err)
|
||||
lh.ifce = &mockEncWriter{}
|
||||
lhh := lh.NewRequestHandler()
|
||||
|
||||
peerVpnIp := netip.MustParseAddr("10.128.0.2")
|
||||
peerUdpAddr := netip.MustParseAddrPort("10.0.0.2:4242")
|
||||
otherVpnIp := netip.MustParseAddr("10.128.0.3")
|
||||
otherUdpAddr := netip.MustParseAddrPort("10.0.0.3:4242")
|
||||
|
||||
newLHHostUpdate(peerUdpAddr, peerVpnIp, []netip.AddrPort{peerUdpAddr}, lhh)
|
||||
newLHHostUpdate(otherUdpAddr, otherVpnIp, []netip.AddrPort{otherUdpAddr}, lhh)
|
||||
|
||||
// Control: a query about a real peer is still answered, and still ends with
|
||||
// the punch notification aimed at the host that was asked about.
|
||||
w := sendLHHostRequest(peerUdpAddr, peerVpnIp, otherVpnIp, lhh, nil)
|
||||
require.NotNil(t, w.lastReply.msg)
|
||||
assert.Equal(t, NebulaMeta_HostPunchNotification, w.lastReply.msg.Type)
|
||||
assert.Equal(t, otherVpnIp, w.lastReply.vpnIp)
|
||||
|
||||
// Now validate that we don't send to ourselves.
|
||||
found, _, err := lh.queryAndPrepMessage(myVpnIp, func(*cache) (int, error) { return 0, nil })
|
||||
require.NoError(t, err)
|
||||
require.True(t, found, "the lighthouse should hold a cache entry for its own address")
|
||||
|
||||
w = sendLHHostRequest(peerUdpAddr, peerVpnIp, myVpnIp, lhh, nil)
|
||||
assert.Nil(t, w.lastReply.msg, "a query about our own address must produce no reply and no punch notification")
|
||||
}
|
||||
|
||||
func newLHHostUpdate(fromAddr netip.AddrPort, vpnIp netip.Addr, addrs []netip.AddrPort, lhh *LightHouseHandler) {
|
||||
@@ -642,12 +680,7 @@ func TestLighthouse_Dont_Delete_Static_Hosts(t *testing.T) {
|
||||
}
|
||||
|
||||
myVpnNet := netip.MustParsePrefix("10.128.0.1/24")
|
||||
nt := new(bart.Lite)
|
||||
nt.Insert(myVpnNet)
|
||||
cs := &CertState{
|
||||
myVpnNetworks: []netip.Prefix{myVpnNet},
|
||||
myVpnNetworksTable: nt,
|
||||
}
|
||||
cs := testCertState(myVpnNet)
|
||||
lh, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil)
|
||||
require.NoError(t, err)
|
||||
lh.ifce = &mockEncWriter{}
|
||||
@@ -708,12 +741,7 @@ func TestLighthouse_DeletesWork(t *testing.T) {
|
||||
}
|
||||
|
||||
myVpnNet := netip.MustParsePrefix("10.128.0.1/24")
|
||||
nt := new(bart.Lite)
|
||||
nt.Insert(myVpnNet)
|
||||
cs := &CertState{
|
||||
myVpnNetworks: []netip.Prefix{myVpnNet},
|
||||
myVpnNetworksTable: nt,
|
||||
}
|
||||
cs := testCertState(myVpnNet)
|
||||
lh, err := NewLightHouseFromConfig(t.Context(), l, c, cs, nil, nil)
|
||||
require.NoError(t, err)
|
||||
lh.ifce = &mockEncWriter{}
|
||||
@@ -738,3 +766,32 @@ func TestLighthouse_DeletesWork(t *testing.T) {
|
||||
out = lh.Query(testHost)
|
||||
assert.Nil(t, out)
|
||||
}
|
||||
|
||||
func TestLightHouse_logLocalAddrsErr(t *testing.T) {
|
||||
out := &bytes.Buffer{}
|
||||
lh := &LightHouse{l: test.NewLoggerWithOutput(out)}
|
||||
|
||||
// The first sighting of a failure warns.
|
||||
lh.logLocalAddrsErr(errors.New("permission denied"))
|
||||
assert.Contains(t, out.String(), "level=WARN")
|
||||
assert.Contains(t, out.String(), "permission denied")
|
||||
|
||||
// Repeating unchanged does not warn again, which is what keeps a permanent failure from warning
|
||||
// on every lighthouse.interval for the life of the process.
|
||||
out.Reset()
|
||||
lh.logLocalAddrsErr(errors.New("permission denied"))
|
||||
assert.NotContains(t, out.String(), "level=WARN")
|
||||
|
||||
// A different failure is a new event and warns.
|
||||
out.Reset()
|
||||
lh.logLocalAddrsErr(errors.New("something else"))
|
||||
assert.Contains(t, out.String(), "level=WARN")
|
||||
assert.Contains(t, out.String(), "something else")
|
||||
|
||||
// Recovering resets, so the same failure returning later warns again.
|
||||
out.Reset()
|
||||
lh.logLocalAddrsErr(nil)
|
||||
assert.Empty(t, out.String())
|
||||
lh.logLocalAddrsErr(errors.New("something else"))
|
||||
assert.Contains(t, out.String(), "level=WARN")
|
||||
}
|
||||
@@ -32,9 +32,12 @@ const _ = noiseutil.RejectHeadroom - 4*maxRoutines
|
||||
|
||||
func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, deviceFactory overlay.DeviceFactory) (retcon *Control, reterr error) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
// Automatically cancel the context if Main returns an error, to signal all created goroutines to quit.
|
||||
// The goroutines started below stop only when this context does, and only a caller holding the
|
||||
// Control can arrange that. Cancel whenever we are not handing one back, which covers an error
|
||||
// and a config test alike: a config test used to leave the lighthouse query worker, and a
|
||||
// hostname resolver per dns named static host, running for the life of the process.
|
||||
defer func() {
|
||||
if reterr != nil {
|
||||
if retcon == nil {
|
||||
cancel()
|
||||
}
|
||||
}()
|
||||
@@ -210,6 +213,7 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
||||
}
|
||||
|
||||
hostMap := NewHostMapFromConfig(l, c)
|
||||
hostMap.established = newEstablishedRemotes(udpConns)
|
||||
punchy := NewPunchyFromConfig(l, c, udpConns[0])
|
||||
connManager := newConnectionManagerFromConfig(l, c, hostMap, punchy)
|
||||
lightHouse, err := NewLightHouseFromConfig(ctx, l, c, pki.getCertState(), udpConns[0], punchy)
|
||||
|
||||
@@ -1,12 +1,21 @@
|
||||
package nebula
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/slackhq/nebula/cert"
|
||||
cert_test "github.com/slackhq/nebula/cert_test"
|
||||
"github.com/slackhq/nebula/config"
|
||||
"github.com/slackhq/nebula/test"
|
||||
"github.com/slackhq/nebula/util"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/goleak"
|
||||
)
|
||||
|
||||
func TestParseCpuAffinity(t *testing.T) {
|
||||
@@ -49,3 +58,68 @@ func TestParseCpuAffinity(t *testing.T) {
|
||||
assert.Nil(t, parseCpuAffinity(newConfig([]any{1 << 20}), l, 1))
|
||||
}
|
||||
}
|
||||
|
||||
// TestMain_ConfigTestReleasesItsGoroutines pins the rule that Main only leaves goroutines running
|
||||
// when it hands back a Control to stop them with.
|
||||
//
|
||||
// A config test gets no Control, so anything it started had nothing to stop it: the lighthouse
|
||||
// query worker, and a hostname resolver per dns named static host, ran for the life of the
|
||||
// process. That matters to every embedder that validates a config in process rather than by
|
||||
// exec'ing, dnclient and the apple clients included, because they do it on each config load and
|
||||
// the leak accumulates.
|
||||
func TestMain_ConfigTestReleasesItsGoroutines(t *testing.T) {
|
||||
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
|
||||
|
||||
l := test.NewLogger()
|
||||
dir := t.TempDir()
|
||||
|
||||
before := time.Now().Add(-time.Hour)
|
||||
after := time.Now().Add(time.Hour)
|
||||
ca, _, caKey, caPEM := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, before, after, nil, nil, nil)
|
||||
networks := []netip.Prefix{netip.MustParsePrefix("10.0.0.1/24")}
|
||||
_, _, keyPEM, certPEM := cert_test.NewTestCert(
|
||||
cert.Version2, cert.Curve_CURVE25519, ca, caKey, "config-test", before, after, networks, nil, nil)
|
||||
|
||||
caPath := filepath.Join(dir, "ca.pem")
|
||||
certPath := filepath.Join(dir, "cert.pem")
|
||||
keyPath := filepath.Join(dir, "key.pem")
|
||||
require.NoError(t, os.WriteFile(caPath, caPEM, 0o600))
|
||||
require.NoError(t, os.WriteFile(certPath, certPEM, 0o600))
|
||||
require.NoError(t, os.WriteFile(keyPath, keyPEM, 0o600))
|
||||
|
||||
// A static host by address, not by name: the query worker is the goroutine under test and a
|
||||
// hostname would drag a real dns lookup into a unit test.
|
||||
configBody := fmt.Sprintf(`
|
||||
pki:
|
||||
ca: %s
|
||||
cert: %s
|
||||
key: %s
|
||||
static_host_map:
|
||||
"10.0.0.2": ["192.0.2.1:4242"]
|
||||
lighthouse:
|
||||
hosts:
|
||||
- "10.0.0.2"
|
||||
listen:
|
||||
host: 127.0.0.1
|
||||
port: 0
|
||||
tun:
|
||||
disabled: true
|
||||
firewall:
|
||||
outbound:
|
||||
- port: any
|
||||
proto: any
|
||||
host: any
|
||||
inbound:
|
||||
- port: any
|
||||
proto: any
|
||||
host: any
|
||||
`, caPath, certPath, keyPath)
|
||||
require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yml"), []byte(configBody), 0o600))
|
||||
|
||||
c := config.NewC(l)
|
||||
require.NoError(t, c.Load(dir))
|
||||
|
||||
ctrl, err := Main(c, true, "config-test", l, nil)
|
||||
require.NoError(t, err)
|
||||
require.Nil(t, ctrl, "a config test hands back nothing to stop, so it must stop itself")
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
//go:build !android
|
||||
|
||||
package nicfinder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/netip"
|
||||
)
|
||||
|
||||
func localAddrs(ctx context.Context, l *slog.Logger, filter Filter) ([]netip.Addr, error) {
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to enumerate local interfaces: %w", err)
|
||||
}
|
||||
|
||||
var out []netip.Addr
|
||||
var errs []error
|
||||
|
||||
for _, i := range ifaces {
|
||||
if !allowName(ctx, l, filter, i.Name) {
|
||||
continue
|
||||
}
|
||||
addrs, err := i.Addrs()
|
||||
if err != nil {
|
||||
errs = append(errs, fmt.Errorf("failed to get addresses for %s: %w", i.Name, err))
|
||||
continue
|
||||
}
|
||||
for _, rawAddr := range addrs {
|
||||
if addr, ok := allowedAddr(ctx, l, filter, rawAddr); ok {
|
||||
out = append(out, addr)
|
||||
}
|
||||
}
|
||||
}
|
||||
return out, errors.Join(errs...)
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
//go:build android
|
||||
|
||||
package nicfinder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/netip"
|
||||
"os"
|
||||
|
||||
"github.com/DefinedNet/netlink"
|
||||
"github.com/DefinedNet/netlink/nl"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// Android denies bind() on netlink route sockets and RTM_GETLINK, so net.Interfaces fails with EACCES.
|
||||
// Enumerate with an RTM_GETADDR dump on an unbound socket instead.
|
||||
|
||||
// localInterfaces returns every interface that has an address, with its addresses. It lists
|
||||
// addresses over a netlink socket that is never bound and names each index with SIOCGIFNAME, the
|
||||
// same operations Bionic's getifaddrs uses for app UIDs, which the sandbox permits.
|
||||
func localAddrs(ctx context.Context, l *slog.Logger, filter Filter) ([]netip.Addr, error) {
|
||||
var errs []error
|
||||
addrs, err := netlinkAddrList()
|
||||
if err != nil {
|
||||
if !errors.Is(err, netlink.ErrDumpInterrupted) {
|
||||
return nil, fmt.Errorf("failed to enumerate local interfaces: %w", err)
|
||||
}
|
||||
// The address table kept changing under the dump and netlink gave up retrying. What it
|
||||
// returned is still worth advertising.
|
||||
errs = append(errs, fmt.Errorf("local addresses may be incomplete: %w", err))
|
||||
}
|
||||
|
||||
// Any socket will do for SIOCGIFNAME.
|
||||
sfd, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM|unix.SOCK_CLOEXEC, 0)
|
||||
if err != nil {
|
||||
return nil, os.NewSyscallError("socket", err)
|
||||
}
|
||||
defer unix.Close(sfd)
|
||||
|
||||
var out []netip.Addr
|
||||
nameAllowed := make(map[int]bool) // by interface index, once named
|
||||
for _, a := range addrs {
|
||||
allow, named := nameAllowed[a.LinkIndex]
|
||||
if (named && !allow) || a.IPNet == nil {
|
||||
continue
|
||||
}
|
||||
addr, ok := allowedAddr(ctx, l, filter, a.IPNet)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if !named {
|
||||
name, err := interfaceNameByIndex(sfd, a.LinkIndex)
|
||||
// ENODEV means the interface went away after the dump. Any other failure is reported
|
||||
// and the interface skipped, so the rest are still returned.
|
||||
if err != nil && !errors.Is(err, unix.ENODEV) {
|
||||
errs = append(errs, fmt.Errorf("failed to resolve name of interface %d: %w", a.LinkIndex, err))
|
||||
}
|
||||
allow = err == nil && allowName(ctx, l, filter, name)
|
||||
nameAllowed[a.LinkIndex] = allow
|
||||
}
|
||||
if allow {
|
||||
out = append(out, addr)
|
||||
}
|
||||
}
|
||||
return out, errors.Join(errs...)
|
||||
}
|
||||
|
||||
// netlinkAddrList is netlink.AddrList over a socket that is never bound. The kernel autobinds it
|
||||
// on the first send, which the sandbox permits.
|
||||
func netlinkAddrList() ([]netlink.Addr, error) {
|
||||
fd, err := unix.Socket(unix.AF_NETLINK, unix.SOCK_RAW|unix.SOCK_CLOEXEC, unix.NETLINK_ROUTE)
|
||||
if err != nil {
|
||||
return nil, os.NewSyscallError("socket", err)
|
||||
}
|
||||
s, err := nl.NewNetlinkSocketFromFd(fd)
|
||||
if err != nil {
|
||||
unix.Close(fd)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
h := netlink.NewHandleFromSockets(
|
||||
map[int]*nl.SocketHandle{unix.NETLINK_ROUTE: {Socket: s}},
|
||||
netlink.HandleOptions{RetryInterrupted: true},
|
||||
)
|
||||
defer h.Close()
|
||||
if err := h.SetSocketTimeout(netlink.GetSocketTimeout()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return h.AddrList(nil, netlink.FAMILY_ALL)
|
||||
}
|
||||
|
||||
// interfaceNameByIndex resolves an interface index with SIOCGIFNAME.
|
||||
func interfaceNameByIndex(fd int, index int) (string, error) {
|
||||
var ifr unix.Ifreq
|
||||
ifr.SetUint32(uint32(index)) // ifr_ifindex is in the ifr_ifru union
|
||||
if err := unix.IoctlIfreq(fd, unix.SIOCGIFNAME, &ifr); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return ifr.Name(), nil
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package nicfinder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/netip"
|
||||
|
||||
"github.com/slackhq/nebula/logging"
|
||||
)
|
||||
|
||||
// Filter decides which interfaces, by name, and which of their addresses Find returns.
|
||||
type Filter interface {
|
||||
AllowName(string) bool
|
||||
Allow(netip.Addr) bool
|
||||
}
|
||||
|
||||
// Find returns the addresses of this host's network interfaces that allowList accepts, with
|
||||
// loopback and link-local addresses left out. When only some interfaces could be read it returns
|
||||
// their addresses alongside an error for the rest. allowList must not be nil; it is called as
|
||||
// given, so a nil pointer of a type whose methods accept a nil receiver is fine.
|
||||
func Find(ctx context.Context, l *slog.Logger, allowList Filter) ([]netip.Addr, error) {
|
||||
return localAddrs(ctx, l, allowList)
|
||||
}
|
||||
|
||||
// allowName asks filter about an interface name and traces the answer.
|
||||
func allowName(ctx context.Context, l *slog.Logger, filter Filter, name string) bool {
|
||||
allow := filter.AllowName(name)
|
||||
if l.Enabled(ctx, logging.LevelTrace) {
|
||||
l.Log(ctx, logging.LevelTrace, "localAllowList.AllowName", "interfaceName", name, "allow", allow)
|
||||
}
|
||||
return allow
|
||||
}
|
||||
|
||||
// allowedAddr converts one address a platform reports and says whether to advertise it: it must
|
||||
// be a *net.IPNet or *net.IPAddr carrying an IP, not be loopback or link-local, and be accepted by
|
||||
// filter, whose answer is traced. IPv4 comes back unmapped.
|
||||
func allowedAddr(ctx context.Context, l *slog.Logger, filter Filter, rawAddr net.Addr) (netip.Addr, bool) {
|
||||
var ip net.IP
|
||||
switch v := rawAddr.(type) {
|
||||
case *net.IPNet:
|
||||
ip = v.IP
|
||||
case *net.IPAddr:
|
||||
ip = v.IP
|
||||
}
|
||||
addr, ok := netip.AddrFromSlice(ip)
|
||||
if !ok {
|
||||
return netip.Addr{}, false
|
||||
}
|
||||
addr = addr.Unmap()
|
||||
if addr.IsLoopback() || addr.IsLinkLocalUnicast() {
|
||||
return netip.Addr{}, false
|
||||
}
|
||||
isAllowed := filter.Allow(addr)
|
||||
if l.Enabled(ctx, logging.LevelTrace) {
|
||||
l.Log(ctx, logging.LevelTrace, "localAllowList.Allow", "localAddr", addr, "allowed", isAllowed)
|
||||
}
|
||||
return addr, isAllowed
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package nicfinder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/netip"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/slackhq/nebula/test"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type allowAll struct{}
|
||||
|
||||
func (allowAll) Allow(netip.Addr) bool { return true }
|
||||
func (allowAll) AllowName(string) bool { return true }
|
||||
|
||||
type testAllowList struct {
|
||||
bannedName *regexp.Regexp
|
||||
bannedAddr netip.Addr
|
||||
}
|
||||
|
||||
func (l *testAllowList) Allow(addr netip.Addr) bool {
|
||||
return addr != l.bannedAddr
|
||||
}
|
||||
|
||||
func (l *testAllowList) AllowName(n string) bool {
|
||||
return !l.bannedName.MatchString(n)
|
||||
}
|
||||
|
||||
func newTestAllowList(bannedName string) *testAllowList {
|
||||
return &testAllowList{
|
||||
bannedName: regexp.MustCompile(bannedName),
|
||||
}
|
||||
}
|
||||
|
||||
func TestAllowedAddr(t *testing.T) {
|
||||
ctx, l := context.Background(), test.NewLogger()
|
||||
allowed := func(filter Filter, addrs []net.Addr) []netip.Addr {
|
||||
var out []netip.Addr
|
||||
for _, rawAddr := range addrs {
|
||||
if addr, ok := allowedAddr(ctx, l, filter, rawAddr); ok {
|
||||
out = append(out, addr)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
addrs := []net.Addr{
|
||||
&net.IPNet{IP: net.ParseIP("127.0.0.1"), Mask: net.CIDRMask(8, 32)},
|
||||
&net.IPNet{IP: net.ParseIP("::1"), Mask: net.CIDRMask(128, 128)},
|
||||
&net.IPNet{IP: net.ParseIP("10.0.0.5"), Mask: net.CIDRMask(24, 32)},
|
||||
&net.IPNet{IP: net.ParseIP("169.254.10.10"), Mask: net.CIDRMask(16, 32)},
|
||||
&net.IPNet{IP: net.ParseIP("fe80::1"), Mask: net.CIDRMask(64, 128)},
|
||||
&net.IPAddr{IP: net.ParseIP("fd00::5")},
|
||||
&net.IPNet{},
|
||||
otherAddr{},
|
||||
nil,
|
||||
}
|
||||
|
||||
// Loopback, link local and anything without an IP are dropped, and IPv4 comes back unmapped
|
||||
// even though net.ParseIP hands over a 16 byte slice.
|
||||
assert.Equal(t, []netip.Addr{
|
||||
netip.MustParseAddr("10.0.0.5"),
|
||||
netip.MustParseAddr("fd00::5"),
|
||||
}, allowed(allowAll{}, addrs))
|
||||
|
||||
// The filter sees the unmapped form and can drop a single address.
|
||||
al := newTestAllowList("none")
|
||||
al.bannedAddr = netip.MustParseAddr("10.0.0.5")
|
||||
assert.Equal(t, []netip.Addr{netip.MustParseAddr("fd00::5")}, allowed(al, addrs))
|
||||
}
|
||||
|
||||
// otherAddr is a net.Addr that is neither *net.IPNet nor *net.IPAddr. netlink.Addr is one such
|
||||
// type: it satisfies net.Addr through an embedded *net.IPNet, and passing it directly once
|
||||
// silently produced no addresses.
|
||||
type otherAddr struct{}
|
||||
|
||||
func (otherAddr) Network() string { return "other" }
|
||||
func (otherAddr) String() string { return "other" }
|
||||
|
||||
// hostFilter counts AllowName calls and can reject every name or every address.
|
||||
type hostFilter struct {
|
||||
rejectNames bool
|
||||
rejectAddrs bool
|
||||
asked map[string]int
|
||||
}
|
||||
|
||||
func (f *hostFilter) Allow(netip.Addr) bool { return !f.rejectAddrs }
|
||||
func (f *hostFilter) AllowName(name string) bool {
|
||||
f.asked[name]++
|
||||
return !f.rejectNames
|
||||
}
|
||||
|
||||
// Runs this platform's localAddrs against the host and checks it honors the whole filter.
|
||||
func TestLocalAddrsAppliesFilter(t *testing.T) {
|
||||
ctx, l := context.Background(), test.NewLogger()
|
||||
|
||||
f := &hostFilter{asked: map[string]int{}}
|
||||
all, err := localAddrs(ctx, l, f)
|
||||
if len(f.asked) == 0 {
|
||||
t.Skipf("no interfaces to enumerate here: %v", err)
|
||||
}
|
||||
for name, n := range f.asked {
|
||||
assert.Equal(t, 1, n, "AllowName asked %d times for %s", n, name)
|
||||
}
|
||||
// Loopback and link-local never come back, whatever the filter says.
|
||||
for _, addr := range all {
|
||||
assert.False(t, addr.IsLoopback() || addr.IsLinkLocalUnicast(), "%s", addr)
|
||||
}
|
||||
|
||||
none, _ := localAddrs(ctx, l, &hostFilter{rejectNames: true, asked: map[string]int{}})
|
||||
assert.Empty(t, none, "addresses got past a filter that rejects every interface name")
|
||||
|
||||
none, _ = localAddrs(ctx, l, &hostFilter{rejectAddrs: true, asked: map[string]int{}})
|
||||
assert.Empty(t, none, "addresses got past a filter that rejects every address")
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build boringcrypto
|
||||
// +build boringcrypto
|
||||
|
||||
package noiseutil
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build boringcrypto
|
||||
// +build boringcrypto
|
||||
|
||||
package noiseutil
|
||||
|
||||
|
||||
+1
-1
@@ -354,7 +354,7 @@ func parseV6(data []byte, incoming bool, fp *firewall.ParsedPacket) error {
|
||||
// and cannot drift into misreading an unknown protocol (SCTP, GRE, etc.) as a forged transport.
|
||||
proto, offset, isFragment, anyFragment, err := iputil.IPv6FindUpperProtocol(data)
|
||||
if err != nil {
|
||||
return ErrIPv6PacketTooShort
|
||||
return err
|
||||
}
|
||||
|
||||
fp.Protocol = proto
|
||||
|
||||
+7
-7
@@ -117,7 +117,7 @@ func Test_newPacket_v6(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
err = newPacket(buffer.Bytes(), true, p)
|
||||
require.ErrorIs(t, err, ErrIPv6PacketTooShort)
|
||||
require.ErrorIs(t, err, iputil.ErrIPv6CouldNotFindPayload)
|
||||
|
||||
// A v6 packet with a hop-by-hop extension
|
||||
// ICMPv6 Payload (Echo Request)
|
||||
@@ -151,12 +151,12 @@ func Test_newPacket_v6(t *testing.T) {
|
||||
// A full IPv6 header and 1 byte in the first extension, but missing
|
||||
// the length byte.
|
||||
err = newPacket(buffer.Bytes()[:41], true, p)
|
||||
require.ErrorIs(t, err, ErrIPv6PacketTooShort)
|
||||
require.ErrorIs(t, err, iputil.ErrIPv6CouldNotFindPayload)
|
||||
|
||||
// A full IPv6 header plus 1 full extension, but only 1 byte of the
|
||||
// next layer, missing length byte
|
||||
err = newPacket(buffer.Bytes()[:49], true, p)
|
||||
require.ErrorIs(t, err, ErrIPv6PacketTooShort)
|
||||
require.ErrorIs(t, err, iputil.ErrIPv6CouldNotFindPayload)
|
||||
err = nil
|
||||
|
||||
// A good ICMP packet
|
||||
@@ -354,12 +354,12 @@ func Test_newPacket_v6(t *testing.T) {
|
||||
|
||||
// Ensure buffer bounds checking during processing, a truncated AH header can't reach the payload
|
||||
err = newPacket(b[:41], true, p)
|
||||
require.ErrorIs(t, err, ErrIPv6PacketTooShort)
|
||||
require.ErrorIs(t, err, iputil.ErrIPv6CouldNotFindPayload)
|
||||
|
||||
// Invalid AH header
|
||||
b = buffer.Bytes()
|
||||
err = newPacket(b, true, p)
|
||||
require.ErrorIs(t, err, ErrIPv6PacketTooShort)
|
||||
require.ErrorIs(t, err, iputil.ErrIPv6CouldNotFindPayload)
|
||||
}
|
||||
|
||||
func Test_newPacket_ipv6Fragment(t *testing.T) {
|
||||
@@ -466,7 +466,7 @@ func Test_newPacket_ipv6Fragment(t *testing.T) {
|
||||
|
||||
// Too short of a fragment packet
|
||||
err = newPacket(secondFrag[:len(secondFrag)-10], false, p)
|
||||
require.ErrorIs(t, err, ErrIPv6PacketTooShort)
|
||||
require.ErrorIs(t, err, iputil.ErrIPv6CouldNotFindPayload)
|
||||
}
|
||||
|
||||
func BenchmarkParseV6(b *testing.B) {
|
||||
@@ -707,7 +707,7 @@ func Test_newPacket_v6ExtHeaderPastBuffer(t *testing.T) {
|
||||
pkt[40] = byte(layers.IPProtocolSCTP) // Dest Options next header = SCTP
|
||||
pkt[41] = 255 // declared length (255+1)*8 = 2048, past the 48 byte buffer
|
||||
|
||||
require.ErrorIs(t, newPacket(pkt, true, p), ErrIPv6PacketTooShort)
|
||||
require.ErrorIs(t, newPacket(pkt, true, p), iputil.ErrIPv6CouldNotFindPayload)
|
||||
}
|
||||
|
||||
// Test_newPacket_v6ExtHeaderConfusion is a regression test for parseV6 walking any unrecognized
|
||||
|
||||
@@ -127,7 +127,7 @@ func TestPseudoSumIPv6MatchesReference(t *testing.T) {
|
||||
func TestIPv4HdrChecksumMatchesReference(t *testing.T) {
|
||||
rng := rand.New(rand.NewSource(0x1791))
|
||||
for _, hdrLen := range []int{20, 24, 40, 60} {
|
||||
for trial := 0; trial < 200; trial++ {
|
||||
for trial := range 200 {
|
||||
hdr := make([]byte, hdrLen)
|
||||
rng.Read(hdr)
|
||||
hdr[0] = 0x40 | byte(hdrLen/4)
|
||||
@@ -156,7 +156,7 @@ func TestIPv4HdrChecksumMatchesReference(t *testing.T) {
|
||||
// the way a receiver does (pseudo-header + L4 must sum to all-ones).
|
||||
func TestChecksumSeedReceiverAcceptance(t *testing.T) {
|
||||
rng := rand.New(rand.NewSource(0x1826))
|
||||
for trial := 0; trial < 200; trial++ {
|
||||
for trial := range 200 {
|
||||
src := [4]byte{byte(rng.Intn(256)), byte(rng.Intn(256)), byte(rng.Intn(256)), byte(rng.Intn(256))}
|
||||
dst := [4]byte{byte(rng.Intn(256)), byte(rng.Intn(256)), byte(rng.Intn(256)), byte(rng.Intn(256))}
|
||||
payLen := rng.Intn(1500)
|
||||
|
||||
@@ -435,3 +435,65 @@ func testPP(pkt []byte) *firewall.ParsedPacket {
|
||||
}
|
||||
return pp
|
||||
}
|
||||
|
||||
// fakeTunBatchWriter records each WriteBatch call and each plain Write separately.
|
||||
type fakeTunBatchWriter struct {
|
||||
writes [][]byte
|
||||
batches [][][]byte
|
||||
}
|
||||
|
||||
func (w *fakeTunBatchWriter) Write(p []byte) (int, error) {
|
||||
w.writes = append(w.writes, bytes.Clone(p))
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
func (w *fakeTunBatchWriter) WriteBatch(pkts [][]byte) error {
|
||||
var b [][]byte
|
||||
for _, p := range pkts {
|
||||
b = append(b, bytes.Clone(p))
|
||||
}
|
||||
w.batches = append(w.batches, b)
|
||||
return nil
|
||||
}
|
||||
|
||||
// TestPassthroughUsesWriteBatch pins that a writer implementing tio.BatchWriter gets the whole
|
||||
// passthrough lane in one WriteBatch per Flush, in order, and no plain Writes; an empty Flush
|
||||
// makes no call.
|
||||
func TestPassthroughUsesWriteBatch(t *testing.T) {
|
||||
w := &fakeTunBatchWriter{}
|
||||
m := newTestMultiCoalescer(t, w)
|
||||
k := &keySeq{epoch: 1}
|
||||
|
||||
var want [][]byte
|
||||
for i := range 5 {
|
||||
icmp := make([]byte, 28)
|
||||
icmp[0] = 0x45
|
||||
icmp[3] = 28
|
||||
icmp[9] = 1
|
||||
icmp[20] = byte(i)
|
||||
want = append(want, icmp)
|
||||
if err := m.Commit(icmp, k.next(), testPP(icmp)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if err := m.Flush(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := m.Flush(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(w.writes) != 0 {
|
||||
t.Fatalf("got %d plain Writes, want 0", len(w.writes))
|
||||
}
|
||||
if len(w.batches) != 1 {
|
||||
t.Fatalf("got %d WriteBatch calls, want 1", len(w.batches))
|
||||
}
|
||||
for i, p := range w.batches[0] {
|
||||
if !bytes.Equal(p, want[i]) {
|
||||
t.Fatalf("packet %d: got % x, want % x", i, p, want[i])
|
||||
}
|
||||
}
|
||||
if len(w.batches[0]) != len(want) {
|
||||
t.Fatalf("got %d packets, want %d", len(w.batches[0]), len(want))
|
||||
}
|
||||
}
|
||||
@@ -2,18 +2,23 @@ package batch
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/slackhq/nebula/overlay/tio"
|
||||
)
|
||||
|
||||
// Passthrough is MultiCoalescer's verbatim lane: no batching, packets are written at Flush in the
|
||||
// order enqueued.
|
||||
// Passthrough is MultiCoalescer's verbatim lane: no coalescing, packets are written at Flush in the
|
||||
// order enqueued, in one WriteBatch when out implements tio.BatchWriter.
|
||||
type Passthrough struct {
|
||||
out io.Writer
|
||||
bw tio.BatchWriter // nil: Flush writes one packet per Write
|
||||
slots [][]byte
|
||||
}
|
||||
|
||||
func NewPassthrough(w io.Writer) *Passthrough {
|
||||
bw, _ := w.(tio.BatchWriter)
|
||||
return &Passthrough{
|
||||
out: w,
|
||||
bw: bw,
|
||||
slots: make([][]byte, 0, 128),
|
||||
}
|
||||
}
|
||||
@@ -26,10 +31,16 @@ func (p *Passthrough) enqueue(pkt []byte) error {
|
||||
|
||||
func (p *Passthrough) Flush() error {
|
||||
var firstErr error
|
||||
for _, s := range p.slots {
|
||||
_, err := p.out.Write(s)
|
||||
if err != nil && firstErr == nil {
|
||||
firstErr = err
|
||||
if p.bw != nil {
|
||||
if len(p.slots) > 0 {
|
||||
firstErr = p.bw.WriteBatch(p.slots)
|
||||
}
|
||||
} else {
|
||||
for _, s := range p.slots {
|
||||
_, err := p.out.Write(s)
|
||||
if err != nil && firstErr == nil {
|
||||
firstErr = err
|
||||
}
|
||||
}
|
||||
}
|
||||
clear(p.slots)
|
||||
|
||||
@@ -0,0 +1,156 @@
|
||||
//go:build darwin || send_pipeline
|
||||
|
||||
package batch
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// SendPipeline splits encrypt from send. The producer (a tun reader) encrypts packets into one SendBatch while a
|
||||
// sender goroutine writes the other, and they swap whenever the sender is free. The sender takes everything the
|
||||
// producer committed since its last take, so packets that arrive during a write leave together in the next
|
||||
// WriteBatch, and the reader returns to the tun while the kernel is still sending.
|
||||
//
|
||||
// The producer holds the batch for one packet at a time (Begin, reserve and commit, End) so the sender can take the
|
||||
// batch between any two packets, never while a reserved slot is still being written.
|
||||
//
|
||||
// Ownership: the producer touches its batch only between Begin and End, and the sender swaps batches only under
|
||||
// the same lock, so a slot the producer reserved is never sent half written. A batch goes back to the producer
|
||||
// only after its WriteBatch returned and it was emptied, so no slot is reused before its send completes.
|
||||
//
|
||||
// Memory is bounded at two batches. When the producer fills its batch while the sender is still writing the other,
|
||||
// Begin waits for the swap. That slows the tun reader instead of dropping, as the unpipelined path does.
|
||||
//
|
||||
// Order: a single sender writes batches in the order they were filled, each in commit order, so every peer's
|
||||
// packets leave in the order their message counters were taken.
|
||||
type SendPipeline struct {
|
||||
out batchWriter
|
||||
cap int
|
||||
maxWrite int
|
||||
onWrite func(queued, written int, err error)
|
||||
|
||||
mu sync.Mutex
|
||||
cond sync.Cond
|
||||
cur *SendBatch // the producer's batch, guarded by mu
|
||||
gen uint64 // swaps so far, guarded by mu
|
||||
|
||||
ready chan struct{} // cap 1: cur may have packets for the sender
|
||||
stop chan struct{}
|
||||
done chan struct{}
|
||||
once sync.Once
|
||||
}
|
||||
|
||||
// NewSendPipeline starts a pipeline over two batches of batchCap slots and arenaSize arena bytes each. It writes
|
||||
// to out at most maxWrite packets per WriteBatch. If onWrite is not nil, it is called on the sender goroutine
|
||||
// after every WriteBatch with how many packets that call was given, how many went out, and its error. Close stops
|
||||
// the pipeline.
|
||||
func NewSendPipeline(out batchWriter, batchCap, arenaSize, maxWrite int, onWrite func(queued, written int, err error)) *SendPipeline {
|
||||
batchCap = max(batchCap, 1)
|
||||
p := &SendPipeline{
|
||||
out: out,
|
||||
cap: batchCap,
|
||||
maxWrite: max(maxWrite, 1),
|
||||
onWrite: onWrite,
|
||||
cur: NewSendBatch(out, batchCap, arenaSize),
|
||||
ready: make(chan struct{}, 1),
|
||||
stop: make(chan struct{}),
|
||||
done: make(chan struct{}),
|
||||
}
|
||||
p.cond.L = &p.mu
|
||||
go p.run(NewSendBatch(out, batchCap, arenaSize))
|
||||
return p
|
||||
}
|
||||
|
||||
// Begin returns a batch with room for at least one more packet. The batch stays the producer's until End. If the
|
||||
// batch is full, Begin first waits for the sender to swap it out. Producer only.
|
||||
func (p *SendPipeline) Begin() *SendBatch {
|
||||
p.mu.Lock()
|
||||
if p.cur.Len() >= p.cap {
|
||||
g := p.gen
|
||||
p.wake()
|
||||
for p.gen == g {
|
||||
p.cond.Wait()
|
||||
}
|
||||
}
|
||||
return p.cur
|
||||
}
|
||||
|
||||
// End gives up the batch Begin returned. With wake set it also tells the sender the batch has packets for it, which
|
||||
// the producer does once a run of packets is in (the end of a tun read) rather than per packet, so the sender takes
|
||||
// the run whole when it is idle. A sender still writing takes the batch when it finishes, woken or not.
|
||||
func (p *SendPipeline) End(wake bool) {
|
||||
n := p.cur.Len()
|
||||
p.mu.Unlock()
|
||||
if wake && n > 0 {
|
||||
p.wake()
|
||||
}
|
||||
}
|
||||
|
||||
func (p *SendPipeline) wake() {
|
||||
select {
|
||||
case p.ready <- struct{}{}:
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
// Close writes whatever the producer left in its batch, stops the sender, and waits for it. The producer calls it,
|
||||
// outside Begin/End, once it is done. Safe to call more than once.
|
||||
func (p *SendPipeline) Close() {
|
||||
p.once.Do(func() { close(p.stop) })
|
||||
<-p.done
|
||||
}
|
||||
|
||||
func (p *SendPipeline) run(mine *SendBatch) {
|
||||
defer close(p.done)
|
||||
for {
|
||||
stopping := false
|
||||
select {
|
||||
case <-p.ready:
|
||||
case <-p.stop:
|
||||
stopping = true
|
||||
}
|
||||
// Keep taking whatever the producer committed during the last write, without waiting to be woken, so a
|
||||
// busy sender goes straight from one write to the next and each write carries everything that built up.
|
||||
// Once stopping, the producer is done and this writes what it left.
|
||||
for p.take(&mine) {
|
||||
p.write(mine)
|
||||
}
|
||||
if stopping {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// take swaps the producer's batch for the empty *mine if the producer's has packets, and reports whether it did.
|
||||
func (p *SendPipeline) take(mine **SendBatch) bool {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
if p.cur.Len() == 0 {
|
||||
return false
|
||||
}
|
||||
*mine, p.cur = p.cur, *mine
|
||||
p.gen++
|
||||
p.cond.Broadcast()
|
||||
return true
|
||||
}
|
||||
|
||||
// write sends b's packets, maxWrite at a time, then empties b.
|
||||
func (p *SendPipeline) write(b *SendBatch) {
|
||||
for off := 0; off < len(b.bufs); {
|
||||
n := min(p.maxWrite, len(b.bufs)-off)
|
||||
written, err := p.out.WriteBatch(b.bufs[off:off+n], b.dsts[off:off+n])
|
||||
if p.onWrite != nil {
|
||||
p.onWrite(n, written, err)
|
||||
}
|
||||
off += n
|
||||
}
|
||||
b.recycle()
|
||||
}
|
||||
|
||||
// recycle empties b without writing it. SendPipeline uses it because it writes a batch's packets itself.
|
||||
func (b *SendBatch) recycle() {
|
||||
clear(b.bufs)
|
||||
b.bufs = b.bufs[:0]
|
||||
b.dsts = b.dsts[:0]
|
||||
b.arena.Reset()
|
||||
}
|
||||
@@ -0,0 +1,400 @@
|
||||
//go:build darwin || send_pipeline
|
||||
|
||||
package batch
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"net/netip"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.uber.org/goleak"
|
||||
)
|
||||
|
||||
// pipeWriter records every WriteBatch a SendPipeline makes. It checks each packet's bytes against the sequence
|
||||
// number they carry while the call is in progress, and can hold a call until the test releases it.
|
||||
type pipeWriter struct {
|
||||
t *testing.T
|
||||
|
||||
mu sync.Mutex
|
||||
seqs []uint32
|
||||
dsts []netip.AddrPort
|
||||
calls []int
|
||||
inCall [][]byte // the bufs of the call in progress, for overlap checks
|
||||
|
||||
gate chan struct{} // if not nil, each call waits for a receive from it
|
||||
entered chan int // if not nil, each call sends its size here on entry
|
||||
short int // each call reports this many fewer written
|
||||
err error
|
||||
}
|
||||
|
||||
func (w *pipeWriter) WriteBatch(bufs [][]byte, addrs []netip.AddrPort) (int, error) {
|
||||
w.mu.Lock()
|
||||
w.inCall = bufs
|
||||
w.mu.Unlock()
|
||||
if w.entered != nil {
|
||||
w.entered <- len(bufs)
|
||||
}
|
||||
if w.gate != nil {
|
||||
<-w.gate
|
||||
}
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
for i, b := range bufs {
|
||||
seq := binary.BigEndian.Uint32(b)
|
||||
if !checkPkt(b, seq) {
|
||||
w.t.Errorf("packet %d changed while being sent", seq)
|
||||
}
|
||||
w.seqs = append(w.seqs, seq)
|
||||
w.dsts = append(w.dsts, addrs[i])
|
||||
}
|
||||
w.calls = append(w.calls, len(bufs))
|
||||
w.inCall = nil
|
||||
return max(len(bufs)-w.short, 0), w.err
|
||||
}
|
||||
|
||||
func (w *pipeWriter) sent() ([]uint32, []netip.AddrPort, []int) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
return append([]uint32(nil), w.seqs...), append([]netip.AddrPort(nil), w.dsts...), append([]int(nil), w.calls...)
|
||||
}
|
||||
|
||||
// fillPkt writes seq and a pattern derived from it into b, as the encrypt step writes a slot.
|
||||
func fillPkt(b []byte, seq uint32) {
|
||||
binary.BigEndian.PutUint32(b, seq)
|
||||
for i := 4; i < len(b); i++ {
|
||||
b[i] = byte(seq) ^ byte(i)
|
||||
}
|
||||
}
|
||||
|
||||
func checkPkt(b []byte, seq uint32) bool {
|
||||
if len(b) < 4 || binary.BigEndian.Uint32(b) != seq {
|
||||
return false
|
||||
}
|
||||
for i := 4; i < len(b); i++ {
|
||||
if b[i] != byte(seq)^byte(i) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func pktSize(seq uint32) int { return 16 + int(seq%97) }
|
||||
|
||||
var testDsts = []netip.AddrPort{
|
||||
netip.MustParseAddrPort("10.0.0.1:4242"),
|
||||
netip.MustParseAddrPort("10.0.0.2:4242"),
|
||||
netip.MustParseAddrPort("[fd00::3]:4242"),
|
||||
}
|
||||
|
||||
// commit adds packet seq to p the way listenIn does: Begin, reserve, encrypt, commit, End.
|
||||
func commit(p *SendPipeline, seq uint32, last bool) {
|
||||
sb := p.Begin()
|
||||
slot := sb.Reserve(pktSize(seq))
|
||||
fillPkt(slot, seq)
|
||||
sb.Commit(slot, testDsts[seq%uint32(len(testDsts))])
|
||||
p.End(last)
|
||||
}
|
||||
|
||||
func TestSendPipelineOrder(t *testing.T) {
|
||||
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
|
||||
w := &pipeWriter{t: t}
|
||||
// A small arena makes the batches grow theirs, and a small maxWrite splits takes across several writes.
|
||||
p := NewSendPipeline(w, 16, 64, 5, nil)
|
||||
|
||||
const n = 20000
|
||||
seq := uint32(0)
|
||||
for seq < n {
|
||||
// Reads of 1 to 40 packets, with an occasional pause so the sender goes idle and is woken.
|
||||
read := 1 + int(seq*7%40)
|
||||
for j := 0; j < read && seq < n; j++ {
|
||||
commit(p, seq, j == read-1 || seq == n-1)
|
||||
seq++
|
||||
}
|
||||
if seq%1000 < 40 {
|
||||
time.Sleep(50 * time.Microsecond)
|
||||
}
|
||||
}
|
||||
p.Close()
|
||||
|
||||
seqs, dsts, calls := w.sent()
|
||||
require.Len(t, seqs, n)
|
||||
for i, s := range seqs {
|
||||
require.Equal(t, uint32(i), s, "packet %d out of order", i)
|
||||
require.Equal(t, testDsts[s%uint32(len(testDsts))], dsts[i])
|
||||
}
|
||||
for _, c := range calls {
|
||||
assert.LessOrEqual(t, c, 5, "WriteBatch got more than maxWrite")
|
||||
assert.Positive(t, c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendPipelineConcurrentPeersKeepOrder(t *testing.T) {
|
||||
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
|
||||
// A slow writer makes the sender take many packets per write, interleaving the three peers in each batch.
|
||||
w := &pipeWriter{t: t}
|
||||
slow := &slowWriter{pipeWriter: w, d: 20 * time.Microsecond}
|
||||
p := NewSendPipeline(slow, 128, 128*64, 128, nil)
|
||||
const n = 5000
|
||||
for i := uint32(0); i < n; i++ {
|
||||
commit(p, i, i%8 == 7 || i == n-1)
|
||||
}
|
||||
p.Close()
|
||||
|
||||
seqs, dsts, _ := w.sent()
|
||||
require.Len(t, seqs, n)
|
||||
last := map[netip.AddrPort]int64{}
|
||||
for i, s := range seqs {
|
||||
prev, ok := last[dsts[i]]
|
||||
if ok {
|
||||
require.Greater(t, int64(s), prev, "peer %v went backwards", dsts[i])
|
||||
}
|
||||
last[dsts[i]] = int64(s)
|
||||
}
|
||||
}
|
||||
|
||||
type slowWriter struct {
|
||||
*pipeWriter
|
||||
d time.Duration
|
||||
}
|
||||
|
||||
func (s *slowWriter) WriteBatch(bufs [][]byte, addrs []netip.AddrPort) (int, error) {
|
||||
time.Sleep(s.d)
|
||||
return s.pipeWriter.WriteBatch(bufs, addrs)
|
||||
}
|
||||
|
||||
// TestSendPipelineBackpressureAndReuse holds the sender inside a write and checks that the producer can fill
|
||||
// exactly one more batch, that none of its slots overlap the packets being written, and that the next Begin waits
|
||||
// until the write returns.
|
||||
func TestSendPipelineBackpressureAndReuse(t *testing.T) {
|
||||
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
|
||||
const batchCap = 8
|
||||
w := &pipeWriter{t: t, gate: make(chan struct{}), entered: make(chan int, 16)}
|
||||
p := NewSendPipeline(w, batchCap, 64, 128, nil)
|
||||
|
||||
seq := uint32(0)
|
||||
for ; seq < batchCap; seq++ {
|
||||
commit(p, seq, seq == batchCap-1)
|
||||
}
|
||||
require.Equal(t, batchCap, <-w.entered, "sender should take the first batch whole")
|
||||
|
||||
w.mu.Lock()
|
||||
inFlight := append([][]byte(nil), w.inCall...)
|
||||
w.mu.Unlock()
|
||||
|
||||
// The second batch fills while the first is being written, in slots that don't touch the first's bytes.
|
||||
for ; seq < 2*batchCap; seq++ {
|
||||
sb := p.Begin()
|
||||
slot := sb.Reserve(pktSize(seq))
|
||||
for _, b := range inFlight {
|
||||
assert.False(t, overlaps(slot, b), "slot for packet %d overlaps packet %d, still being sent", seq, binary.BigEndian.Uint32(b))
|
||||
}
|
||||
fillPkt(slot, seq)
|
||||
sb.Commit(slot, testDsts[0])
|
||||
p.End(true)
|
||||
}
|
||||
|
||||
// Both batches are now spoken for, so the next Begin must wait for the write to return.
|
||||
begun := make(chan struct{})
|
||||
go func() {
|
||||
commit(p, seq, true)
|
||||
close(begun)
|
||||
}()
|
||||
select {
|
||||
case <-begun:
|
||||
t.Fatal("Begin returned with both batches in use: the pipeline is not bounded")
|
||||
case <-time.After(50 * time.Millisecond):
|
||||
}
|
||||
for i, b := range inFlight {
|
||||
assert.True(t, checkPkt(b, uint32(i)), "packet %d was overwritten before its send completed", i)
|
||||
}
|
||||
|
||||
w.gate <- struct{}{} // first write returns; the sender swaps and starts on the second batch
|
||||
assert.Equal(t, batchCap, <-w.entered)
|
||||
select {
|
||||
case <-begun:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("Begin did not return after the sender freed a batch")
|
||||
}
|
||||
w.gate <- struct{}{}
|
||||
go func() {
|
||||
for range w.entered {
|
||||
w.gate <- struct{}{}
|
||||
}
|
||||
}()
|
||||
p.Close()
|
||||
close(w.entered)
|
||||
|
||||
seqs, _, calls := w.sent()
|
||||
require.Len(t, seqs, 2*batchCap+1)
|
||||
for i, s := range seqs {
|
||||
assert.Equal(t, uint32(i), s)
|
||||
}
|
||||
assert.Equal(t, []int{batchCap, batchCap, 1}, calls)
|
||||
}
|
||||
|
||||
func overlaps(a, b []byte) bool {
|
||||
if len(a) == 0 || len(b) == 0 {
|
||||
return false
|
||||
}
|
||||
a0, b0 := uintptr(unsafe.Pointer(&a[0])), uintptr(unsafe.Pointer(&b[0]))
|
||||
return a0 < b0+uintptr(len(b)) && b0 < a0+uintptr(len(a))
|
||||
}
|
||||
|
||||
// TestSendPipelineBatchesBuildUp checks the point of the pipeline: while one write is in the kernel, everything the
|
||||
// producer commits leaves together in the next write.
|
||||
func TestSendPipelineBatchesBuildUp(t *testing.T) {
|
||||
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
|
||||
w := &pipeWriter{t: t, gate: make(chan struct{}), entered: make(chan int, 16)}
|
||||
p := NewSendPipeline(w, 128, 128*64, 128, nil)
|
||||
|
||||
commit(p, 0, true)
|
||||
require.Equal(t, 1, <-w.entered)
|
||||
// Ten one-packet reads while the first write is held.
|
||||
for i := uint32(1); i <= 10; i++ {
|
||||
commit(p, i, true)
|
||||
}
|
||||
w.gate <- struct{}{}
|
||||
require.Equal(t, 10, <-w.entered, "packets queued during a write should go out in one WriteBatch")
|
||||
w.gate <- struct{}{}
|
||||
p.Close()
|
||||
|
||||
_, _, calls := w.sent()
|
||||
assert.Equal(t, []int{1, 10}, calls)
|
||||
}
|
||||
|
||||
func TestSendPipelineCloseFlushes(t *testing.T) {
|
||||
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
|
||||
w := &pipeWriter{t: t}
|
||||
p := NewSendPipeline(w, 32, 32*64, 128, nil)
|
||||
// End(false) never wakes the sender, so only Close's final take sends these.
|
||||
for i := uint32(0); i < 5; i++ {
|
||||
commit(p, i, false)
|
||||
}
|
||||
p.Close()
|
||||
p.Close() // idempotent
|
||||
seqs, _, _ := w.sent()
|
||||
assert.Equal(t, []uint32{0, 1, 2, 3, 4}, seqs)
|
||||
}
|
||||
|
||||
func TestSendPipelineCloseEmpty(t *testing.T) {
|
||||
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
|
||||
w := &pipeWriter{t: t}
|
||||
p := NewSendPipeline(w, 4, 64, 128, nil)
|
||||
sb := p.Begin()
|
||||
p.End(true) // nothing committed: no write
|
||||
assert.Zero(t, sb.Len())
|
||||
p.Close()
|
||||
_, _, calls := w.sent()
|
||||
assert.Empty(t, calls)
|
||||
}
|
||||
|
||||
// TestSendPipelineCloseWaitsForWrite checks that Close returns only after a write in progress has returned, so the
|
||||
// caller can't free anything the sender still uses.
|
||||
func TestSendPipelineCloseWaitsForWrite(t *testing.T) {
|
||||
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
|
||||
w := &pipeWriter{t: t, gate: make(chan struct{}), entered: make(chan int, 4)}
|
||||
p := NewSendPipeline(w, 4, 64, 128, nil)
|
||||
commit(p, 0, true)
|
||||
<-w.entered
|
||||
commit(p, 1, false)
|
||||
|
||||
closed := make(chan struct{})
|
||||
go func() {
|
||||
p.Close()
|
||||
close(closed)
|
||||
}()
|
||||
select {
|
||||
case <-closed:
|
||||
t.Fatal("Close returned during a write")
|
||||
case <-time.After(50 * time.Millisecond):
|
||||
}
|
||||
w.gate <- struct{}{}
|
||||
<-w.entered // Close's final take writes packet 1
|
||||
w.gate <- struct{}{}
|
||||
select {
|
||||
case <-closed:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("Close did not return")
|
||||
}
|
||||
seqs, _, _ := w.sent()
|
||||
assert.Equal(t, []uint32{0, 1}, seqs)
|
||||
}
|
||||
|
||||
func TestSendPipelineOnWriteAndDrops(t *testing.T) {
|
||||
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
|
||||
werr := errors.New("boom")
|
||||
w := &pipeWriter{t: t, short: 1, err: werr}
|
||||
type call struct {
|
||||
queued, written int
|
||||
err error
|
||||
}
|
||||
var got []call
|
||||
p := NewSendPipeline(w, 16, 16*64, 4, func(q, n int, err error) {
|
||||
got = append(got, call{q, n, err}) // sender goroutine only
|
||||
})
|
||||
for i := uint32(0); i < 10; i++ {
|
||||
commit(p, i, false)
|
||||
}
|
||||
p.Close()
|
||||
|
||||
assert.Equal(t, []call{{4, 3, werr}, {4, 3, werr}, {2, 1, werr}}, got)
|
||||
}
|
||||
|
||||
// TestSendPipelineStressRace runs a producer against a sender whose writes take varying time, for the race
|
||||
// detector, and checks nothing is lost, reordered or overwritten.
|
||||
func TestSendPipelineStressRace(t *testing.T) {
|
||||
defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
|
||||
w := &pipeWriter{t: t}
|
||||
jitter := &jitterWriter{pipeWriter: w}
|
||||
p := NewSendPipeline(jitter, 32, 256, 16, nil)
|
||||
const n = 50000
|
||||
for i := uint32(0); i < n; i++ {
|
||||
commit(p, i, i%3 == 0)
|
||||
}
|
||||
p.Close()
|
||||
seqs, _, _ := w.sent()
|
||||
require.Len(t, seqs, n)
|
||||
for i, s := range seqs {
|
||||
if uint32(i) != s {
|
||||
t.Fatalf("packet %d arrived at %d", s, i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type jitterWriter struct {
|
||||
*pipeWriter
|
||||
i int
|
||||
}
|
||||
|
||||
func (j *jitterWriter) WriteBatch(bufs [][]byte, addrs []netip.AddrPort) (int, error) {
|
||||
j.i++
|
||||
if j.i%7 == 0 {
|
||||
time.Sleep(time.Duration(j.i%5) * 10 * time.Microsecond)
|
||||
}
|
||||
return j.pipeWriter.WriteBatch(bufs, addrs)
|
||||
}
|
||||
|
||||
type discardWriter struct{}
|
||||
|
||||
func (discardWriter) WriteBatch(bufs [][]byte, _ []netip.AddrPort) (int, error) {
|
||||
return len(bufs), nil
|
||||
}
|
||||
|
||||
// BenchmarkSendPipeline measures a packet through Begin, reserve, commit and End with a writer that costs nothing;
|
||||
// its allocs/op is the pipeline's steady-state heap cost per packet.
|
||||
func BenchmarkSendPipeline(b *testing.B) {
|
||||
p := NewSendPipeline(discardWriter{}, SendBatchCap, SendBatchCap*1500, 128, nil)
|
||||
defer p.Close()
|
||||
b.ReportAllocs()
|
||||
for i := range b.N {
|
||||
sb := p.Begin()
|
||||
sb.Commit(sb.Reserve(1400), testDsts[0])
|
||||
p.End(i%8 == 7)
|
||||
}
|
||||
}
|
||||
@@ -540,7 +540,7 @@ func TestCoalescerCapBySegments(t *testing.T) {
|
||||
c := newTestTCPCoalescer(t, w)
|
||||
pay := make([]byte, 512)
|
||||
seq := uint32(1000)
|
||||
for i := 0; i < tcpCoalesceMaxSegs+5; i++ {
|
||||
for range tcpCoalesceMaxSegs + 5 {
|
||||
if err := c.Commit(buildTCPv4(seq, tcpAck, pay)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ func TestSendBatchReserveCommitFlush(t *testing.T) {
|
||||
b := NewSendBatch(fw, 4, 32)
|
||||
|
||||
ap := netip.MustParseAddrPort("10.0.0.1:4242")
|
||||
for i := 0; i < 4; i++ {
|
||||
for i := range 4 {
|
||||
slot := b.Reserve(32)
|
||||
if cap(slot) != 32 {
|
||||
t.Fatalf("slot %d: cap=%d want 32", i, cap(slot))
|
||||
@@ -72,7 +72,7 @@ func TestSendBatchSlotsDoNotOverlap(t *testing.T) {
|
||||
b := NewSendBatch(fw, 3, 8)
|
||||
ap := netip.MustParseAddrPort("10.0.0.1:80")
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
for i := range 3 {
|
||||
s := b.Reserve(8)
|
||||
pkt := append(s[:0], byte(0xA0+i), byte(0xB0+i))
|
||||
b.Commit(pkt, ap)
|
||||
|
||||
@@ -129,7 +129,7 @@ func TestUDPCoalescerCoalescesEqualSized(t *testing.T) {
|
||||
w := &fakeTunWriter{gsoEnabled: true}
|
||||
c := newTestUDPCoalescer(t, w)
|
||||
pay := make([]byte, 1200)
|
||||
for i := 0; i < 3; i++ {
|
||||
for range 3 {
|
||||
if err := c.Commit(buildUDPv4(1000, 53, pay)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -259,7 +259,7 @@ func TestUDPCoalescerCapsAtMaxSegs(t *testing.T) {
|
||||
w := &fakeTunWriter{gsoEnabled: true}
|
||||
c := newTestUDPCoalescer(t, w)
|
||||
pay := make([]byte, 100)
|
||||
for i := 0; i < udpCoalesceMaxSegs+5; i++ {
|
||||
for range udpCoalesceMaxSegs + 5 {
|
||||
if err := c.Commit(buildUDPv4(1000, 53, pay)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -317,7 +317,7 @@ func TestUDPCoalescerIPv6Coalesces(t *testing.T) {
|
||||
w := &fakeTunWriter{gsoEnabled: true}
|
||||
c := newTestUDPCoalescer(t, w)
|
||||
pay := make([]byte, 1200)
|
||||
for i := 0; i < 3; i++ {
|
||||
for range 3 {
|
||||
if err := c.Commit(buildUDPv6(1000, 53, pay)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -151,7 +151,7 @@ func TestChecksumTailPaths(t *testing.T) {
|
||||
offsets := []int{0, 1, 3, 7, 15} // mix of aligned and odd starts
|
||||
|
||||
for k := 0; k <= maxK; k++ {
|
||||
for tail := 0; tail < 64; tail++ {
|
||||
for tail := range 64 {
|
||||
length := 64*k + tail
|
||||
for _, seed := range seeds {
|
||||
for _, off := range offsets {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build !e2e_testing
|
||||
// +build !e2e_testing
|
||||
|
||||
package overlay
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build !e2e_testing
|
||||
// +build !e2e_testing
|
||||
|
||||
package overlay
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build linux && !android
|
||||
// +build linux,!android
|
||||
|
||||
package tio
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build linux && !android
|
||||
// +build linux,!android
|
||||
|
||||
package tio
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build linux && !android
|
||||
// +build linux,!android
|
||||
|
||||
package tio
|
||||
|
||||
|
||||
@@ -145,3 +145,14 @@ func SupportsGSO(w io.Writer, want GSOProto) (GSOWriter, bool) {
|
||||
return gw, false
|
||||
}
|
||||
}
|
||||
|
||||
// BatchWriter is implemented by Queues that can emit several already-segmented packets in one call.
|
||||
// Backends without a batched write do not implement it and callers loop over Write.
|
||||
//
|
||||
// pkts are borrowed: WriteBatch must not retain them past its return.
|
||||
// A returned error doesn't say which packets were delivered, so callers must not retry the batch.
|
||||
// Safe for concurrent use, like Write.
|
||||
type BatchWriter interface {
|
||||
io.Writer
|
||||
WriteBatch(pkts [][]byte) error
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build linux && !android
|
||||
// +build linux,!android
|
||||
|
||||
package tio
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build linux && !android
|
||||
// +build linux,!android
|
||||
|
||||
package tio
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build linux && !android && !e2e_testing
|
||||
// +build linux,!android,!e2e_testing
|
||||
|
||||
package tio
|
||||
|
||||
@@ -117,17 +116,15 @@ func TestPoll_ConcurrentWrite_NoRace(t *testing.T) {
|
||||
}()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for w := 0; w < writers; w++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
for i := 0; i < perWriter; i++ {
|
||||
for range writers {
|
||||
wg.Go(func() {
|
||||
for range perWriter {
|
||||
if _, werr := p.Write(payload); werr != nil {
|
||||
t.Errorf("write: %v", werr)
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build linux && !android
|
||||
// +build linux,!android
|
||||
|
||||
package tio
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build linux && !android && !e2e_testing
|
||||
// +build linux,!android,!e2e_testing
|
||||
|
||||
package tio
|
||||
|
||||
@@ -137,7 +136,7 @@ func buildTSOv4(t *testing.T, payLen, mss int) ([]byte, virtio.Hdr) {
|
||||
binary.BigEndian.PutUint16(pkt[34:36], 65535) // window
|
||||
|
||||
// payload
|
||||
for i := 0; i < payLen; i++ {
|
||||
for i := range payLen {
|
||||
pkt[ipLen+tcpLen+i] = byte(i & 0xff)
|
||||
}
|
||||
return pkt, virtio.NewHeader(
|
||||
@@ -257,7 +256,7 @@ func TestSegmentTCPv6(t *testing.T) {
|
||||
pkt[53] = 0x19 // FIN | ACK | PSH — exercise FIN clearing too
|
||||
binary.BigEndian.PutUint16(pkt[54:56], 65535)
|
||||
|
||||
for i := 0; i < payLen; i++ {
|
||||
for i := range payLen {
|
||||
pkt[ipLen+tcpLen+i] = byte(i)
|
||||
}
|
||||
|
||||
@@ -361,7 +360,7 @@ func buildUSOv4(t *testing.T, payLen, gsoSize int) ([]byte, virtio.Hdr) {
|
||||
binary.BigEndian.PutUint16(pkt[22:24], 53) // dport
|
||||
binary.BigEndian.PutUint16(pkt[24:26], uint16(udpLen+payLen)) // superpacket length
|
||||
|
||||
for i := 0; i < payLen; i++ {
|
||||
for i := range payLen {
|
||||
pkt[ipLen+udpLen+i] = byte(i & 0xff)
|
||||
}
|
||||
|
||||
@@ -473,7 +472,7 @@ func TestSegmentUDPv6(t *testing.T) {
|
||||
// Superpacket-wide length, as the kernel supplies it; see buildUSOv4.
|
||||
binary.BigEndian.PutUint16(pkt[44:46], uint16(udpLen+payLen))
|
||||
|
||||
for i := 0; i < payLen; i++ {
|
||||
for i := range payLen {
|
||||
pkt[ipLen+udpLen+i] = byte(i)
|
||||
}
|
||||
|
||||
@@ -772,7 +771,7 @@ func buildTSOv6(payLen, gso int) []byte {
|
||||
pkt[53] = 0x10 // ACK only
|
||||
binary.BigEndian.PutUint16(pkt[54:56], 65535)
|
||||
|
||||
for i := 0; i < payLen; i++ {
|
||||
for i := range payLen {
|
||||
pkt[ipLen+tcpLen+i] = byte(i)
|
||||
}
|
||||
return pkt
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build linux && !android
|
||||
// +build linux,!android
|
||||
|
||||
package virtio
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build linux && !android
|
||||
// +build linux,!android
|
||||
|
||||
// Package virtio implements the pure validation, header-correction, and
|
||||
// per-segment slicing logic for kernel-supplied TSO/USO superpackets on
|
||||
@@ -254,12 +253,9 @@ func SegmentTCP(pkt []byte, hdrLenU, csumStartU, gsoSizeU uint16, yield func(seg
|
||||
var savedHdr [maxSegHdrLen]byte
|
||||
copy(savedHdr[:headerLen], pkt[:headerLen])
|
||||
|
||||
for i := 0; i < numSeg; i++ {
|
||||
for i := range numSeg {
|
||||
segStart := i * gsoSize
|
||||
segEnd := segStart + gsoSize
|
||||
if segEnd > payLen {
|
||||
segEnd = payLen
|
||||
}
|
||||
segEnd := min(segStart+gsoSize, payLen)
|
||||
segPayLen := segEnd - segStart
|
||||
segLen := headerLen + segPayLen
|
||||
headerOff := i * gsoSize
|
||||
@@ -359,12 +355,9 @@ func SegmentUDP(pkt []byte, hdrLenU, csumStartU, gsoSizeU uint16, yield func(seg
|
||||
var savedHdr [maxSegHdrLen]byte
|
||||
copy(savedHdr[:headerLen], pkt[:headerLen])
|
||||
|
||||
for i := 0; i < numSeg; i++ {
|
||||
for i := range numSeg {
|
||||
segStart := i * gsoSize
|
||||
segEnd := segStart + gsoSize
|
||||
if segEnd > payLen {
|
||||
segEnd = payLen
|
||||
}
|
||||
segEnd := min(segStart+gsoSize, payLen)
|
||||
segPayLen := segEnd - segStart
|
||||
segLen := headerLen + segPayLen
|
||||
headerOff := i * gsoSize
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build linux && !android
|
||||
// +build linux,!android
|
||||
|
||||
package virtio
|
||||
|
||||
@@ -58,7 +57,7 @@ func buildTCPv4Super(payLen int) (pkt []byte, hdrLen, csumStart uint16) {
|
||||
pkt[33] = 0x18 // ACK | PSH
|
||||
binary.BigEndian.PutUint16(pkt[34:36], 65535) // window
|
||||
|
||||
for i := 0; i < payLen; i++ {
|
||||
for i := range payLen {
|
||||
pkt[ipLen+tcpLen+i] = byte(i & 0xff)
|
||||
}
|
||||
return pkt, ipLen + tcpLen, ipLen
|
||||
@@ -82,7 +81,7 @@ func buildUDPv4Super(payLen int) (pkt []byte, hdrLen, csumStart uint16) {
|
||||
binary.BigEndian.PutUint16(pkt[20:22], 12345) // sport
|
||||
binary.BigEndian.PutUint16(pkt[22:24], 53) // dport
|
||||
|
||||
for i := 0; i < payLen; i++ {
|
||||
for i := range payLen {
|
||||
pkt[ipLen+udpLen+i] = byte(i & 0xff)
|
||||
}
|
||||
return pkt, ipLen + udpLen, ipLen
|
||||
@@ -188,7 +187,7 @@ func TestSegmentTCPHeaderNotCorrupted(t *testing.T) {
|
||||
// Payload bytes must be the original contiguous slice.
|
||||
segPayLen := len(seg) - int(hdrLen)
|
||||
wantPay := make([]byte, segPayLen)
|
||||
for k := 0; k < segPayLen; k++ {
|
||||
for k := range segPayLen {
|
||||
wantPay[k] = byte((off + k) & 0xff)
|
||||
}
|
||||
if !bytes.Equal(seg[hdrLen:], wantPay) {
|
||||
@@ -317,7 +316,7 @@ func TestSegmentUDPHeaderNotCorrupted(t *testing.T) {
|
||||
}
|
||||
|
||||
wantPay := make([]byte, segPayLen)
|
||||
for k := 0; k < segPayLen; k++ {
|
||||
for k := range segPayLen {
|
||||
wantPay[k] = byte((off + k) & 0xff)
|
||||
}
|
||||
if !bytes.Equal(seg[hdrLen:], wantPay) {
|
||||
@@ -365,7 +364,7 @@ func buildUDPv4Single(payload []byte) (pkt []byte, hdr Hdr) {
|
||||
// 0xffff, because all-zero is the reserved "no checksum" encoding that IPv6 rejects outright.
|
||||
func TestFinishChecksumUDPZeroStoresAllOnes(t *testing.T) {
|
||||
var payload []byte
|
||||
for i := 0; i < 0x10000; i++ {
|
||||
for i := range 0x10000 {
|
||||
p := []byte{byte(i >> 8), byte(i)}
|
||||
pkt, hdr := buildUDPv4Single(p)
|
||||
cs, co := int(hdr.CsumStart), int(hdr.CsumOffset)
|
||||
@@ -544,7 +543,7 @@ func TestBaseSumsMatchZeroingReference(t *testing.T) {
|
||||
|
||||
t.Run("ipv4", func(t *testing.T) {
|
||||
for ihl := ipv4HeaderMinLen; ihl <= ipv4HeaderMaxLen; ihl += 4 {
|
||||
for iter := 0; iter < 5000; iter++ {
|
||||
for range 5000 {
|
||||
pkt := make([]byte, ihl)
|
||||
for i := range pkt {
|
||||
pkt[i] = randByte(&state)
|
||||
@@ -574,7 +573,7 @@ func TestBaseSumsMatchZeroingReference(t *testing.T) {
|
||||
for dataOff := 5; dataOff <= 15; dataOff++ {
|
||||
tcpLen := dataOff * 4
|
||||
headerLen := csumStart + tcpLen
|
||||
for iter := 0; iter < 5000; iter++ {
|
||||
for range 5000 {
|
||||
pkt := make([]byte, headerLen+64)
|
||||
for i := range pkt {
|
||||
pkt[i] = randByte(&state)
|
||||
|
||||
+2
-2
@@ -93,14 +93,14 @@ func prefixToMask(prefix netip.Prefix) netip.Addr {
|
||||
}
|
||||
|
||||
func flipBytes(b []byte) []byte {
|
||||
for i := 0; i < len(b); i++ {
|
||||
for i := range b {
|
||||
b[i] ^= 0xFF
|
||||
}
|
||||
return b
|
||||
}
|
||||
func orBytes(a []byte, b []byte) []byte {
|
||||
ret := make([]byte, len(a))
|
||||
for i := 0; i < len(a); i++ {
|
||||
for i := range a {
|
||||
ret[i] = a[i] | b[i]
|
||||
}
|
||||
return ret
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build !e2e_testing
|
||||
// +build !e2e_testing
|
||||
|
||||
package overlay
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build !e2e_testing
|
||||
// +build !e2e_testing
|
||||
|
||||
package overlay
|
||||
|
||||
|
||||
+465
-13
@@ -1,5 +1,4 @@
|
||||
//go:build !ios && !e2e_testing
|
||||
// +build !ios,!e2e_testing
|
||||
|
||||
package overlay
|
||||
|
||||
@@ -9,12 +8,14 @@ import (
|
||||
"log/slog"
|
||||
"net/netip"
|
||||
"os"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
|
||||
"github.com/gaissmai/bart"
|
||||
"github.com/slackhq/nebula/config"
|
||||
"github.com/slackhq/nebula/internal/msgx"
|
||||
"github.com/slackhq/nebula/overlay/tio"
|
||||
"github.com/slackhq/nebula/routing"
|
||||
"github.com/slackhq/nebula/util"
|
||||
@@ -30,9 +31,27 @@ type tun struct {
|
||||
Routes atomic.Pointer[[]Route]
|
||||
routeTree atomic.Pointer[bart.Table[routing.Gateways]]
|
||||
linkAddr *netroute.LinkAddr
|
||||
l *slog.Logger
|
||||
// hostOwned means the fd arrived from the OS, which has already configured addressing, mtu
|
||||
// and routes for it. NEPacketTunnelProvider on darwin does this.
|
||||
hostOwned bool
|
||||
l *slog.Logger
|
||||
|
||||
// batchMu guards batch, WriteBatch's sendmsg_x scratch.
|
||||
batchMu sync.Mutex
|
||||
batch tunBatch
|
||||
}
|
||||
|
||||
// tunBatch is WriteBatch's scratch. The kernel reads every entry in place.
|
||||
type tunBatch struct {
|
||||
heads [msgx.Batch][4]byte
|
||||
iovs [msgx.Batch][2]unix.Iovec
|
||||
hdrs [msgx.Batch]msgx.Hdr
|
||||
pkts [msgx.Batch][]byte
|
||||
}
|
||||
|
||||
// noTunSendX is set once sendmsg_x is refused on the tun; WriteBatch then writes one packet at a time.
|
||||
var noTunSendX atomic.Bool
|
||||
|
||||
type ifReq struct {
|
||||
Name [unix.IFNAMSIZ]byte
|
||||
Flags uint16
|
||||
@@ -150,8 +169,48 @@ func (t *tun) deviceBytes() (o [16]byte) {
|
||||
return
|
||||
}
|
||||
|
||||
func newTunFromFd(_ *config.C, _ *slog.Logger, _ int, _ []netip.Prefix) (*tun, error) {
|
||||
return nil, fmt.Errorf("newTunFromFd not supported in Darwin")
|
||||
// newTunFromFd adopts a utun the host already created and configured, which is how a darwin
|
||||
// network extension is handed its device. Everything about moving packets is shared with newTun,
|
||||
// only the setup differs: the host owns addressing and routing here.
|
||||
func newTunFromFd(c *config.C, l *slog.Logger, deviceFd int, vpnNetworks []netip.Prefix) (*tun, error) {
|
||||
if err := unix.SetNonblock(deviceFd, true); err != nil {
|
||||
// We own the fd from the moment it is handed to us
|
||||
_ = unix.Close(deviceFd)
|
||||
return nil, fmt.Errorf("failed to set the tun fd to non-blocking mode: %w", err)
|
||||
}
|
||||
|
||||
file := os.NewFile(uintptr(deviceFd), "/dev/tun")
|
||||
t := &tun{
|
||||
f: file,
|
||||
Device: utunNameFromFd(deviceFd),
|
||||
vpnNetworks: vpnNetworks,
|
||||
DefaultMTU: c.GetInt("tun.mtu", DefaultMTU),
|
||||
hostOwned: true,
|
||||
l: l,
|
||||
}
|
||||
|
||||
if err := t.reload(c, true); err != nil {
|
||||
_ = file.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.RegisterReloadCallback(func(c *config.C) {
|
||||
if err := t.reload(c, false); err != nil {
|
||||
util.LogWithContextIfNeeded("failed to reload tun device", err, t.l)
|
||||
}
|
||||
})
|
||||
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// utunNameFromFd asks the socket what interface it is, for logs. A blank name is not worth
|
||||
// failing a tunnel over, so an error just leaves it empty.
|
||||
func utunNameFromFd(fd int) string {
|
||||
name, err := unix.GetsockoptString(fd, unix.AF_SYS_CONTROL, _UTUN_OPT_IFNAME)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
func (t *tun) Close() error {
|
||||
@@ -162,6 +221,12 @@ func (t *tun) Close() error {
|
||||
}
|
||||
|
||||
func (t *tun) Activate() error {
|
||||
// The host handed us a configured device. Its addresses, mtu and routes come from the network
|
||||
// settings it applied, and a sandboxed extension cannot change them anyway.
|
||||
if t.hostOwned {
|
||||
return nil
|
||||
}
|
||||
|
||||
devName := t.deviceBytes()
|
||||
|
||||
s, err := unix.Socket(
|
||||
@@ -375,6 +440,11 @@ func getLinkAddr(name string) (*netroute.LinkAddr, error) {
|
||||
}
|
||||
|
||||
func (t *tun) addRoutes(logErrors bool) error {
|
||||
// The route tree is still ours, the system routing table is not
|
||||
if t.hostOwned {
|
||||
return nil
|
||||
}
|
||||
|
||||
routes := *t.Routes.Load()
|
||||
|
||||
for _, r := range routes {
|
||||
@@ -404,6 +474,10 @@ func (t *tun) addRoutes(logErrors bool) error {
|
||||
}
|
||||
|
||||
func (t *tun) removeRoutes(routes []Route) error {
|
||||
if t.hostOwned {
|
||||
return nil
|
||||
}
|
||||
|
||||
for _, r := range routes {
|
||||
if !r.Install {
|
||||
continue
|
||||
@@ -558,16 +632,12 @@ func (t *tun) Write(from []byte) (int, error) {
|
||||
return 0, syscall.EIO
|
||||
}
|
||||
|
||||
ipVer := from[0] >> 4
|
||||
var head [4]byte
|
||||
switch ipVer {
|
||||
case 4:
|
||||
head[3] = syscall.AF_INET
|
||||
case 6:
|
||||
head[3] = syscall.AF_INET6
|
||||
default:
|
||||
return 0, fmt.Errorf("unable to determine IP version from packet")
|
||||
af, err := tunAF(from)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
head[3] = af
|
||||
|
||||
// Grab rc as a local so the compiler can devirtualize the call and keep the closure on the stack.
|
||||
rc, err := t.f.SyscallConn()
|
||||
@@ -600,6 +670,129 @@ func (t *tun) Write(from []byte) (int, error) {
|
||||
return n - 4, nil
|
||||
}
|
||||
|
||||
// tunAF returns the utun address-family prefix byte for an IP packet.
|
||||
func tunAF(pkt []byte) (byte, error) {
|
||||
switch pkt[0] >> 4 {
|
||||
case 4:
|
||||
return syscall.AF_INET, nil
|
||||
case 6:
|
||||
return syscall.AF_INET6, nil
|
||||
default:
|
||||
return 0, fmt.Errorf("unable to determine IP version from packet")
|
||||
}
|
||||
}
|
||||
|
||||
// WriteBatch writes pkts to the utun device with sendmsg_x, up to msgx.Batch packets per syscall. utun is a
|
||||
// connected datagram socket, so xnu takes the batched send path, though it still hands utun each packet on its own:
|
||||
// this saves syscalls, not per-packet kernel work. Safe for concurrent use.
|
||||
//
|
||||
// An empty packet or one with no IP version is skipped and reported, as Write would. A short count goes on with the
|
||||
// rest; EMSGSIZE, which sendmsg_x returns only when the first packet is past the send buffer, sends that one through
|
||||
// Write, which reports it. Any other error means the kernel took an unknown prefix and dropped the rest, so it is
|
||||
// reported rather than retried, which could deliver packets twice. Under mbuf exhaustion sendmsg_x reports packets it
|
||||
// freed unsent as a short count, so they are lost where writev would have delivered them.
|
||||
func (t *tun) WriteBatch(pkts [][]byte) error {
|
||||
if noTunSendX.Load() {
|
||||
return t.writeEach(pkts)
|
||||
}
|
||||
|
||||
t.batchMu.Lock()
|
||||
defer t.batchMu.Unlock()
|
||||
b := &t.batch
|
||||
var firstErr error
|
||||
keep := func(err error) {
|
||||
if err != nil && firstErr == nil {
|
||||
firstErr = err
|
||||
}
|
||||
}
|
||||
for len(pkts) > 0 {
|
||||
n := 0
|
||||
for len(pkts) > 0 && n < msgx.Batch {
|
||||
p := pkts[0]
|
||||
pkts = pkts[1:]
|
||||
if len(p) == 0 {
|
||||
keep(syscall.EIO)
|
||||
continue
|
||||
}
|
||||
af, err := tunAF(p)
|
||||
if err != nil {
|
||||
keep(err)
|
||||
continue
|
||||
}
|
||||
b.heads[n] = [4]byte{3: af}
|
||||
b.iovs[n] = [2]unix.Iovec{{Base: &b.heads[n][0], Len: 4}, {Base: &p[0], Len: uint64(len(p))}}
|
||||
b.hdrs[n] = msgx.Hdr{Iov: &b.iovs[n][0], Iovlen: 2}
|
||||
b.pkts[n] = p
|
||||
n++
|
||||
}
|
||||
|
||||
for off := 0; off < n; {
|
||||
sent, err := t.sendmsgX(b.hdrs[off:n])
|
||||
switch {
|
||||
case err == nil && sent > 0:
|
||||
off += sent
|
||||
case err == nil:
|
||||
keep(t.writeEach(b.pkts[off:n]))
|
||||
off = n
|
||||
case err == unix.EMSGSIZE:
|
||||
keep(t.writeEach(b.pkts[off : off+1]))
|
||||
off++
|
||||
case isUnsupported(err):
|
||||
if noTunSendX.CompareAndSwap(false, true) {
|
||||
t.l.Warn("sendmsg_x unavailable on the tun device, writing one packet per syscall", "error", err)
|
||||
}
|
||||
keep(t.writeEach(b.pkts[off:n]))
|
||||
keep(t.writeEach(pkts))
|
||||
pkts, off = nil, n
|
||||
default:
|
||||
keep(err)
|
||||
off = n
|
||||
}
|
||||
}
|
||||
clear(b.iovs[:n])
|
||||
clear(b.pkts[:n])
|
||||
}
|
||||
return firstErr
|
||||
}
|
||||
|
||||
// sendmsgX hands hdrs to sendmsg_x, waiting while the socket is full, and returns how many the kernel took.
|
||||
func (t *tun) sendmsgX(hdrs []msgx.Hdr) (int, error) {
|
||||
rc, err := t.f.SyscallConn()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
var sent int
|
||||
var errno syscall.Errno
|
||||
err = rc.Write(func(fd uintptr) bool {
|
||||
sent, errno = msgx.Send(fd, hdrs)
|
||||
// sendmsg_x reports EAGAIN only when it took nothing; a partial batch returns its count.
|
||||
return errno != syscall.EAGAIN
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if errno != 0 {
|
||||
return 0, errno
|
||||
}
|
||||
return sent, nil
|
||||
}
|
||||
|
||||
func isUnsupported(err error) bool {
|
||||
errno, ok := err.(syscall.Errno)
|
||||
return ok && msgx.Unsupported(errno)
|
||||
}
|
||||
|
||||
// writeEach writes pkts one at a time with Write.
|
||||
func (t *tun) writeEach(pkts [][]byte) error {
|
||||
var firstErr error
|
||||
for _, p := range pkts {
|
||||
if _, err := t.Write(p); err != nil && firstErr == nil {
|
||||
firstErr = err
|
||||
}
|
||||
}
|
||||
return firstErr
|
||||
}
|
||||
|
||||
func (t *tun) Networks() []netip.Prefix {
|
||||
return t.vpnNetworks
|
||||
}
|
||||
@@ -609,5 +802,264 @@ func (t *tun) Name() string {
|
||||
}
|
||||
|
||||
func (t *tun) Queues(int) ([]tio.Queue, error) {
|
||||
return []tio.Queue{tio.NewSingleQueue(t, defaultBatchBufSize)}, nil
|
||||
return []tio.Queue{newTunQueue(t)}, nil
|
||||
}
|
||||
|
||||
// tunReadBatch is the most packets one tunQueue.Read returns.
|
||||
const tunReadBatch = msgx.Batch
|
||||
|
||||
// tunRecvXSlot is each recvmsg_x entry's packet buffer. recvmsg_x lays its buffers out before it knows the packet
|
||||
// sizes, so each gets room for the largest IP packet; the kernel only touches what it writes.
|
||||
const tunRecvXSlot = defaultBatchBufSize
|
||||
|
||||
// tunRecvXStride is how far apart the slots start: 64 byte aligned, and one cache line more than 64KiB so the packet
|
||||
// starts don't all land in the same cache sets.
|
||||
const tunRecvXStride = 65536 + 64
|
||||
|
||||
// tunQueue is the darwin tun's Queue. Read hands back 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. It reads them with one recvmsg_x when
|
||||
// it can and one readv per packet otherwise.
|
||||
type tunQueue struct {
|
||||
t *tun
|
||||
// buf holds the packets Read returns: recvmsg_x's slots, or, for readv, one arena. readv stops once less than
|
||||
// defaultBatchBufSize of it is left, so every readv has room for the largest packet utun can return.
|
||||
buf []byte
|
||||
pkts [tunReadBatch]tio.Packet
|
||||
// x is the recvmsg_x scratch, nil once recvmsg_x is off for good.
|
||||
x *tunRecvX
|
||||
}
|
||||
|
||||
// tunRecvX is the recvmsg_x scratch. Entry i scatters its AF prefix into heads[i] and its packet into slot i of the
|
||||
// queue's buf; the kernel reads and writes every entry in place.
|
||||
type tunRecvX struct {
|
||||
heads [tunReadBatch][4]byte
|
||||
iovs [tunReadBatch][2]unix.Iovec
|
||||
hdrs [tunReadBatch]msgx.Hdr
|
||||
}
|
||||
|
||||
// newTunQueue returns t's Queue, raising t's pending packet limit first so a read can find more than one.
|
||||
func newTunQueue(t *tun) *tunQueue {
|
||||
t.raisePendingPackets()
|
||||
q := &tunQueue{t: t, buf: make([]byte, tunReadBatch*tunRecvXStride), x: &tunRecvX{}}
|
||||
x := q.x
|
||||
for i := range x.hdrs {
|
||||
x.iovs[i] = [2]unix.Iovec{
|
||||
{Base: &x.heads[i][0], Len: 4},
|
||||
{Base: &q.buf[i*tunRecvXStride], Len: tunRecvXSlot},
|
||||
}
|
||||
x.hdrs[i] = msgx.Hdr{Iov: &x.iovs[i][0], Iovlen: 2}
|
||||
}
|
||||
return q
|
||||
}
|
||||
|
||||
// Read waits for the utun to become readable, then returns the packets queued on it.
|
||||
func (q *tunQueue) Read() ([]tio.Packet, error) {
|
||||
if q.x != nil {
|
||||
if pkts, err, ok := q.readX(); ok {
|
||||
return pkts, err
|
||||
}
|
||||
}
|
||||
return q.readv()
|
||||
}
|
||||
|
||||
// tunRecvmsgX is msgx.Recv; tests swap it to inject results.
|
||||
var tunRecvmsgX = msgx.Recv
|
||||
|
||||
// readX reads up to tunReadBatch packets with one recvmsg_x. ok is false when the caller should read with readv
|
||||
// instead: recvmsg_x failed this time, or has been turned off for good. A batch that fails tunRecvXCheck is dropped:
|
||||
// its packets are off the socket, but their lengths can't be trusted.
|
||||
func (q *tunQueue) readX() (pkts []tio.Packet, err error, ok bool) {
|
||||
rc, err := q.t.f.SyscallConn()
|
||||
if err != nil {
|
||||
return nil, err, true
|
||||
}
|
||||
|
||||
x := q.x
|
||||
var n int
|
||||
var errno syscall.Errno
|
||||
err = rc.Read(func(fd uintptr) bool {
|
||||
for i := range x.hdrs {
|
||||
x.hdrs[i].Flags = 0
|
||||
x.hdrs[i].Datalen = 0
|
||||
}
|
||||
n, errno = tunRecvmsgX(fd, x.hdrs[:])
|
||||
return errno != unix.EAGAIN
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err, true
|
||||
}
|
||||
if tunRecvXRefused(errno) {
|
||||
q.disableX("unavailable", errno)
|
||||
return nil, nil, false
|
||||
}
|
||||
if errno != 0 {
|
||||
// recvmsg_x can fail where readv wouldn't (a kernel allocation, say), so let readv read this wakeup and
|
||||
// report the error if it is real.
|
||||
return nil, nil, false
|
||||
}
|
||||
if cerr := tunRecvXCheck(n, x.hdrs[:], x.heads[:], tunRecvXSlot); cerr != nil {
|
||||
q.disableX("returned an unexpected batch", cerr)
|
||||
return nil, nil, false
|
||||
}
|
||||
for i := range n {
|
||||
off := i * tunRecvXStride
|
||||
end := off + int(x.hdrs[i].Datalen) - 4
|
||||
q.pkts[i] = tio.Packet{Bytes: q.buf[off:end:end]}
|
||||
}
|
||||
// n is 0 at end of file, as readv's short read is.
|
||||
return q.pkts[:n], nil, true
|
||||
}
|
||||
|
||||
// tunRecvXRefused reports whether a recvmsg_x errno means it will never work here: msgx.Unsupported's refusals, and
|
||||
// EINVAL and EMSGSIZE, which with the headers newTunQueue builds come only from xnu rejecting their layout.
|
||||
func tunRecvXRefused(errno syscall.Errno) bool {
|
||||
return msgx.Unsupported(errno) || errno == unix.EINVAL || errno == unix.EMSGSIZE
|
||||
}
|
||||
|
||||
// disableX turns recvmsg_x off for q for good. readv keeps reading into buf.
|
||||
func (q *tunQueue) disableX(why string, err error) {
|
||||
q.x = nil
|
||||
q.t.l.Warn("recvmsg_x "+why+" on the tun device, reading one packet per syscall", "error", err)
|
||||
}
|
||||
|
||||
// tunRecvXCheck rejects a recvmsg_x batch of n entries that intact msghdr_x entries can't produce, which would mean
|
||||
// the private layout changed under us: more entries than headers, one past its 4 byte head plus slot byte buffer or
|
||||
// too short to hold the head, or an AF prefix utun never writes. A slot holds the largest IP packet, so nothing is
|
||||
// ever truncated; xnu's default recvmsg_x path doesn't report MSG_TRUNC per entry anyway, only its SO_DONTTRUNC path
|
||||
// does, and that is checked too.
|
||||
func tunRecvXCheck(n int, hdrs []msgx.Hdr, heads [][4]byte, slot int) error {
|
||||
if n < 0 || n > len(hdrs) || n > len(heads) {
|
||||
return fmt.Errorf("returned %d packets for %d headers", n, len(hdrs))
|
||||
}
|
||||
for i := range n {
|
||||
h := &hdrs[i]
|
||||
if h.Flags&unix.MSG_TRUNC != 0 {
|
||||
return fmt.Errorf("packet %d came back truncated, flags %#x", i, h.Flags)
|
||||
}
|
||||
if h.Datalen < 4 || h.Datalen > uint64(4+slot) {
|
||||
return fmt.Errorf("packet %d is %d bytes, outside the 4 to %d the buffers allow", i, h.Datalen, 4+slot)
|
||||
}
|
||||
switch heads[i] {
|
||||
case [4]byte{3: syscall.AF_INET}, [4]byte{3: syscall.AF_INET6}:
|
||||
default:
|
||||
return fmt.Errorf("packet %d has AF prefix % x", i, heads[i])
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// readv reads packets one readv at a time until the utun 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) readv() ([]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) WriteBatch(pkts [][]byte) error { return q.t.WriteBatch(pkts) }
|
||||
|
||||
func (q *tunQueue) Close() error { return q.t.Close() }
|
||||
|
||||
// xnu lets one packet wait on a utun control socket by default (if_utun.c utun_ctl_bind): utun_start stops moving
|
||||
// packets from the interface's send queue to the socket once one is waiting, and resumes only when a read wakes the
|
||||
// interface's starter thread. So every read finds one packet. tunPendingPackets lets a whole read batch wait
|
||||
// instead (_UTUN_OPT_MAX_PENDING_PACKETS, bsd/net/if_utun.h, set at SYSPROTO_CONTROL level).
|
||||
const (
|
||||
tunPendingPackets = tunReadBatch
|
||||
_UTUN_OPT_MAX_PENDING_PACKETS = 16
|
||||
_SYSPROTO_CONTROL = 2
|
||||
)
|
||||
|
||||
// tunSetsockoptInt and tunGetsockoptInt are setsockopt and getsockopt; tests swap them.
|
||||
var (
|
||||
tunSetsockoptInt = unix.SetsockoptInt
|
||||
tunGetsockoptInt = unix.GetsockoptInt
|
||||
)
|
||||
|
||||
// raisePendingPackets lets up to tunPendingPackets packets wait on the utun control socket, first growing its
|
||||
// receive buffer to hold that many packets and their 4 byte AF prefix: utun drops a packet the buffer has no room
|
||||
// for, while one over the limit waits in the interface's queue. It sizes for the larger of tun.mtu and the device's
|
||||
// current MTU. Failures are logged and leave xnu's default; the tun works either way.
|
||||
func (t *tun) raisePendingPackets() {
|
||||
if t.f == nil {
|
||||
return
|
||||
}
|
||||
rc, err := t.f.SyscallConn()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
mtu := max(t.DefaultMTU, t.currentMTU())
|
||||
_ = rc.Control(func(fd uintptr) {
|
||||
pending := tunPendingPackets
|
||||
need := pending * (mtu + 4)
|
||||
if have, err := tunGetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_RCVBUF); err == nil && have < need {
|
||||
if err := tunSetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_RCVBUF, need); err != nil {
|
||||
pending = max(have/(mtu+4), 1)
|
||||
t.l.Warn("could not grow the tun control socket's receive buffer, capping pending packets",
|
||||
"error", err, "rcvbuf", have, "wanted", need, "pending", pending)
|
||||
}
|
||||
}
|
||||
if err := tunSetsockoptInt(int(fd), _SYSPROTO_CONTROL, _UTUN_OPT_MAX_PENDING_PACKETS, pending); err != nil {
|
||||
t.l.Warn("could not raise the tun's pending packet limit, reads will return one packet each",
|
||||
"error", err, "pending", pending)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// currentMTU is the utun's MTU as the kernel has it now, or 0 if that can't be read. A host-owned device's MTU
|
||||
// comes from the host, not tun.mtu.
|
||||
func (t *tun) currentMTU() int {
|
||||
if t.Device == "" {
|
||||
return 0
|
||||
}
|
||||
s, err := unix.Socket(unix.AF_INET, unix.SOCK_DGRAM, unix.IPPROTO_IP)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
defer unix.Close(s)
|
||||
ifr, err := unix.IoctlGetIfreqMTU(s, t.Device)
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return int(ifr.MTU)
|
||||
}
|
||||
@@ -0,0 +1,637 @@
|
||||
//go:build !ios && !e2e_testing
|
||||
|
||||
package overlay
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/slackhq/nebula/internal/msgx"
|
||||
"github.com/slackhq/nebula/test"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// newSocketpairTun returns a tun writing to one end of a connected AF_UNIX datagram socketpair, which
|
||||
// takes sendmsg_x the way a utun control socket does, and the other end's fd for reading back.
|
||||
// sndbuf is the writer'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 datagram the utun
|
||||
// device should receive for it.
|
||||
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...)
|
||||
}
|
||||
|
||||
// readTunBack asserts r receives exactly want, in order.
|
||||
func readTunBack(t *testing.T, r int, want [][]byte) {
|
||||
t.Helper()
|
||||
buf := make([]byte, 4096)
|
||||
for i, w := range want {
|
||||
n, _, err := unix.Recvfrom(r, buf, 0)
|
||||
require.NoError(t, err, "datagram %d", i)
|
||||
if !bytes.Equal(w, buf[:n]) {
|
||||
t.Fatalf("datagram %d: got % x, want % x", i, buf[:n], w)
|
||||
}
|
||||
}
|
||||
_, _, err := unix.Recvfrom(r, buf, unix.MSG_DONTWAIT)
|
||||
assert.ErrorIs(t, err, unix.EAGAIN, "extra datagrams delivered")
|
||||
}
|
||||
|
||||
// forEachTunWritePath runs fn once over sendmsg_x and once over the per-packet writev fallback.
|
||||
func forEachTunWritePath(t *testing.T, fn func(t *testing.T, fallback bool)) {
|
||||
for _, fallback := range []bool{false, true} {
|
||||
name := "sendmsg_x"
|
||||
if fallback {
|
||||
name = "writev"
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
noTunSendX.Store(fallback)
|
||||
t.Cleanup(func() { noTunSendX.Store(false) })
|
||||
fn(t, fallback)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestTunWriteBatch pins that WriteBatch delivers every packet whole, in order and behind the right
|
||||
// utun AF prefix, across several sendmsg_x calls, and returns nil.
|
||||
func TestTunWriteBatch(t *testing.T) {
|
||||
forEachTunWritePath(t, func(t *testing.T, fallback bool) {
|
||||
tn, r := newSocketpairTun(t, 1<<20)
|
||||
|
||||
var pkts, want [][]byte
|
||||
for i := range 3*msgx.Batch + 5 {
|
||||
p, w := testTunPkt(i, 60+i)
|
||||
pkts = append(pkts, p)
|
||||
want = append(want, w)
|
||||
}
|
||||
require.NoError(t, tn.WriteBatch(pkts))
|
||||
assert.Equal(t, fallback, noTunSendX.Load())
|
||||
readTunBack(t, r, want)
|
||||
})
|
||||
}
|
||||
|
||||
// TestTunWriteBatchSkipsInvalid pins that an empty packet or one with no IP version is skipped
|
||||
// without shifting the packets after it, and that the first such error is the one reported.
|
||||
func TestTunWriteBatchSkipsInvalid(t *testing.T) {
|
||||
forEachTunWritePath(t, func(t *testing.T, fallback bool) {
|
||||
tn, r := newSocketpairTun(t, 1<<20)
|
||||
|
||||
var pkts, want [][]byte
|
||||
for i := range 20 {
|
||||
p, w := testTunPkt(i, 60)
|
||||
pkts = append(pkts, p)
|
||||
want = append(want, w)
|
||||
}
|
||||
pkts = append(pkts[:10], append([][]byte{{0x10, 1, 2}, {}}, pkts[10:]...)...)
|
||||
|
||||
err := tn.WriteBatch(pkts)
|
||||
assert.ErrorContains(t, err, "IP version")
|
||||
readTunBack(t, r, want)
|
||||
})
|
||||
}
|
||||
|
||||
// TestTunWriteBatchOversize pins that a packet larger than the socket's send buffer drops only
|
||||
// itself and is reported, whether sendmsg_x reaches it mid-call (a short count) or first (EMSGSIZE
|
||||
// for the whole call). The send buffer is 1024 bytes and the oversize packet 2000.
|
||||
func TestTunWriteBatchOversize(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
at int
|
||||
}{
|
||||
{"mid-call", msgx.Batch + 6},
|
||||
{"first-in-call", msgx.Batch},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
forEachTunWritePath(t, func(t *testing.T, fallback bool) {
|
||||
tn, r := newSocketpairTun(t, 1024)
|
||||
|
||||
var pkts, want [][]byte
|
||||
for i := range 2*msgx.Batch + 10 {
|
||||
size := 100
|
||||
if i == tc.at {
|
||||
size = 2000
|
||||
}
|
||||
p, w := testTunPkt(i, size)
|
||||
pkts = append(pkts, p)
|
||||
if i != tc.at {
|
||||
want = append(want, w)
|
||||
}
|
||||
}
|
||||
err := tn.WriteBatch(pkts)
|
||||
assert.ErrorIs(t, err, unix.EMSGSIZE)
|
||||
assert.Equal(t, fallback, noTunSendX.Load())
|
||||
readTunBack(t, r, want)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// forEachTunReadPath runs fn once over recvmsg_x and once over the per-packet readv drain.
|
||||
func forEachTunReadPath(t *testing.T, fn func(t *testing.T, newQ func(*tun) *tunQueue)) {
|
||||
for _, recvX := range []bool{true, false} {
|
||||
name := "recvmsg_x"
|
||||
if !recvX {
|
||||
name = "readv"
|
||||
}
|
||||
t.Run(name, func(t *testing.T) {
|
||||
fn(t, func(tn *tun) *tunQueue {
|
||||
q := newTunQueue(tn)
|
||||
if !recvX {
|
||||
q.x = nil
|
||||
}
|
||||
return q
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// writeTunPkts writes n test packets of size bytes to the peer end w and returns the packets the tun should hand back
|
||||
// for them.
|
||||
func writeTunPkts(t *testing.T, w, n, size int) [][]byte {
|
||||
t.Helper()
|
||||
var want [][]byte
|
||||
for i := range n {
|
||||
pkt, wire := testTunPkt(i, size+i)
|
||||
_, err := unix.Write(w, wire)
|
||||
require.NoError(t, err)
|
||||
want = append(want, pkt)
|
||||
}
|
||||
return want
|
||||
}
|
||||
|
||||
// newReadTun is newSocketpairTun with the tun's receive buffer big enough for every test burst, since an AF_UNIX
|
||||
// datagram send is bounded by the receiver's buffer, and the writer's send buffer big enough for the largest IP packet.
|
||||
func newReadTun(t *testing.T) (*tun, int) {
|
||||
t.Helper()
|
||||
tn, w := newSocketpairTun(t, 1<<20)
|
||||
require.NoError(t, unix.SetsockoptInt(w, unix.SOL_SOCKET, unix.SO_SNDBUF, 1<<18))
|
||||
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))
|
||||
}))
|
||||
return tn, w
|
||||
}
|
||||
|
||||
// readTunPkts calls q.Read once per entry of sizes, requires each to return that many packets, and returns copies of
|
||||
// them all.
|
||||
func readTunPkts(t *testing.T, q *tunQueue, sizes ...int) [][]byte {
|
||||
t.Helper()
|
||||
var got [][]byte
|
||||
for _, size := range sizes {
|
||||
pkts, err := q.Read()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, pkts, size)
|
||||
for _, p := range pkts {
|
||||
got = append(got, p.Clone().Bytes)
|
||||
}
|
||||
}
|
||||
return got
|
||||
}
|
||||
|
||||
// stubTunRecvmsgX swaps tunRecvmsgX for fn until the test ends and counts its calls. fn gets the real msgx.Recv to
|
||||
// delegate to.
|
||||
func stubTunRecvmsgX(t *testing.T, fn func(sys func(uintptr, []msgx.Hdr) (int, syscall.Errno), fd uintptr, hdrs []msgx.Hdr) (int, syscall.Errno)) *atomic.Int64 {
|
||||
t.Helper()
|
||||
sys := tunRecvmsgX
|
||||
var calls atomic.Int64
|
||||
tunRecvmsgX = func(fd uintptr, hdrs []msgx.Hdr) (int, syscall.Errno) {
|
||||
calls.Add(1)
|
||||
return fn(sys, fd, hdrs)
|
||||
}
|
||||
t.Cleanup(func() { tunRecvmsgX = sys })
|
||||
return &calls
|
||||
}
|
||||
|
||||
// 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) {
|
||||
forEachTunReadPath(t, func(t *testing.T, newQ func(*tun) *tunQueue) {
|
||||
tn, w := newReadTun(t)
|
||||
q := newQ(tn)
|
||||
want := writeTunPkts(t, w, tunReadBatch+3, 60)
|
||||
assert.Equal(t, want, readTunPkts(t, q, tunReadBatch, 3))
|
||||
})
|
||||
}
|
||||
|
||||
// TestTunQueueReadUsesRecvX pins that a burst is read with one recvmsg_x, not one call per packet.
|
||||
func TestTunQueueReadUsesRecvX(t *testing.T) {
|
||||
calls := stubTunRecvmsgX(t, func(sys func(uintptr, []msgx.Hdr) (int, syscall.Errno), fd uintptr, hdrs []msgx.Hdr) (int, syscall.Errno) {
|
||||
return sys(fd, hdrs)
|
||||
})
|
||||
tn, w := newReadTun(t)
|
||||
q := newTunQueue(tn)
|
||||
want := writeTunPkts(t, w, 10, 60)
|
||||
assert.Equal(t, want, readTunPkts(t, q, 10))
|
||||
assert.EqualValues(t, 1, calls.Load())
|
||||
assert.NotNil(t, q.x)
|
||||
}
|
||||
|
||||
// TestTunQueueReadLargePackets pins that packets up to the largest IP packet come back whole, which on the recvmsg_x
|
||||
// path means each lands in its own slot and none spills into the next.
|
||||
func TestTunQueueReadLargePackets(t *testing.T) {
|
||||
forEachTunReadPath(t, func(t *testing.T, newQ func(*tun) *tunQueue) {
|
||||
tn, w := newReadTun(t)
|
||||
q := newQ(tn)
|
||||
var want [][]byte
|
||||
for i, size := range []int{65535, 1, 9001, 65535} {
|
||||
pkt, wire := testTunPkt(i, max(size, 3))
|
||||
pkt, wire = pkt[:size], wire[:4+size]
|
||||
_, err := unix.Write(w, wire)
|
||||
require.NoError(t, err)
|
||||
want = append(want, pkt)
|
||||
}
|
||||
assert.Equal(t, want, readTunPkts(t, q, 4))
|
||||
})
|
||||
}
|
||||
|
||||
// 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) {
|
||||
forEachTunReadPath(t, func(t *testing.T, newQ func(*tun) *tunQueue) {
|
||||
tn, w := newSocketpairTun(t, 1<<20)
|
||||
q := newQ(tn)
|
||||
writeTunPkts(t, w, 2, 100)
|
||||
pkts, err := q.Read()
|
||||
require.NoError(t, err)
|
||||
require.Len(t, pkts, 2)
|
||||
assert.Equal(t, len(pkts[0].Bytes), cap(pkts[0].Bytes))
|
||||
assert.Equal(t, len(pkts[1].Bytes), cap(pkts[1].Bytes))
|
||||
})
|
||||
}
|
||||
|
||||
// TestTunQueueReadDoesNotAllocate pins both read paths to zero allocations per Read once the queue exists.
|
||||
func TestTunQueueReadDoesNotAllocate(t *testing.T) {
|
||||
forEachTunReadPath(t, func(t *testing.T, newQ func(*tun) *tunQueue) {
|
||||
tn, w := newReadTun(t)
|
||||
q := newQ(tn)
|
||||
_, wire := testTunPkt(0, 1300)
|
||||
allocs := testing.AllocsPerRun(1000, func() {
|
||||
if _, err := unix.Write(w, wire); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if pkts, err := q.Read(); err != nil || len(pkts) != 1 {
|
||||
t.Fatalf("read %d packets: %v", len(pkts), err)
|
||||
}
|
||||
})
|
||||
assert.Zero(t, allocs)
|
||||
})
|
||||
}
|
||||
|
||||
// TestTunQueueReadXRefused pins that recvmsg_x being refused turns it off for good, and that the same Read still
|
||||
// hands back the queued packets through readv.
|
||||
func TestTunQueueReadXRefused(t *testing.T) {
|
||||
for _, errno := range []syscall.Errno{unix.ENOSYS, unix.EPERM, unix.EOPNOTSUPP, unix.EINVAL} {
|
||||
t.Run(errno.Error(), func(t *testing.T) {
|
||||
calls := stubTunRecvmsgX(t, func(func(uintptr, []msgx.Hdr) (int, syscall.Errno), uintptr, []msgx.Hdr) (int, syscall.Errno) {
|
||||
return 0, errno
|
||||
})
|
||||
tn, w := newReadTun(t)
|
||||
q := newTunQueue(tn)
|
||||
want := writeTunPkts(t, w, 5, 60)
|
||||
assert.Equal(t, want, readTunPkts(t, q, 5))
|
||||
assert.Nil(t, q.x)
|
||||
assert.EqualValues(t, 1, calls.Load())
|
||||
|
||||
want = writeTunPkts(t, w, 2, 60)
|
||||
assert.Equal(t, want, readTunPkts(t, q, 2))
|
||||
assert.EqualValues(t, 1, calls.Load(), "recvmsg_x was tried again after it was refused")
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestTunQueueReadXTransientError pins that an error from recvmsg_x that doesn't mean it is refused leaves this Read
|
||||
// to readv but keeps recvmsg_x on for the next.
|
||||
func TestTunQueueReadXTransientError(t *testing.T) {
|
||||
var failed atomic.Bool
|
||||
calls := stubTunRecvmsgX(t, func(sys func(uintptr, []msgx.Hdr) (int, syscall.Errno), fd uintptr, hdrs []msgx.Hdr) (int, syscall.Errno) {
|
||||
if !failed.Swap(true) {
|
||||
return 0, unix.ENOBUFS
|
||||
}
|
||||
return sys(fd, hdrs)
|
||||
})
|
||||
tn, w := newReadTun(t)
|
||||
q := newTunQueue(tn)
|
||||
want := writeTunPkts(t, w, 3, 60)
|
||||
assert.Equal(t, want, readTunPkts(t, q, 3))
|
||||
assert.NotNil(t, q.x)
|
||||
assert.EqualValues(t, 1, calls.Load())
|
||||
|
||||
want = writeTunPkts(t, w, 2, 60)
|
||||
assert.Equal(t, want, readTunPkts(t, q, 2))
|
||||
assert.EqualValues(t, 2, calls.Load(), "recvmsg_x wasn't tried again after a transient error")
|
||||
}
|
||||
|
||||
// TestTunQueueReadXParks pins that EAGAIN from recvmsg_x parks the Read until the utun is readable, rather than
|
||||
// falling back or returning an empty batch.
|
||||
func TestTunQueueReadXParks(t *testing.T) {
|
||||
var failed atomic.Bool
|
||||
calls := stubTunRecvmsgX(t, func(sys func(uintptr, []msgx.Hdr) (int, syscall.Errno), fd uintptr, hdrs []msgx.Hdr) (int, syscall.Errno) {
|
||||
if !failed.Swap(true) {
|
||||
return 0, unix.EAGAIN
|
||||
}
|
||||
return sys(fd, hdrs)
|
||||
})
|
||||
tn, w := newReadTun(t)
|
||||
q := newTunQueue(tn)
|
||||
// A real EAGAIN means nothing is queued, so the packet arrives only once Read has parked. One packet, so the
|
||||
// wakeup can't split it across Reads.
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
_, wire := testTunPkt(0, 60)
|
||||
_, _ = unix.Write(w, wire)
|
||||
}()
|
||||
t.Cleanup(func() { <-done })
|
||||
pkt, _ := testTunPkt(0, 60)
|
||||
assert.Equal(t, [][]byte{pkt}, readTunPkts(t, q, 1))
|
||||
assert.NotNil(t, q.x)
|
||||
// A spurious wakeup costs another EAGAIN, so only a floor.
|
||||
assert.GreaterOrEqual(t, calls.Load(), int64(2))
|
||||
}
|
||||
|
||||
// TestTunQueueReadXBadBatch pins that a recvmsg_x batch no intact header could produce turns recvmsg_x off for good
|
||||
// and hands the Read to readv. The stub reports a bad batch without taking any packets, so readv then reads them all.
|
||||
func TestTunQueueReadXBadBatch(t *testing.T) {
|
||||
v4 := [4]byte{3: syscall.AF_INET}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
mess func(x *tunRecvX) int
|
||||
}{
|
||||
{"too many", func(x *tunRecvX) int {
|
||||
for i := range x.hdrs {
|
||||
x.hdrs[i].Datalen, x.heads[i] = 64, v4
|
||||
}
|
||||
return len(x.hdrs) + 1
|
||||
}},
|
||||
{"past the buffer", func(x *tunRecvX) int {
|
||||
x.hdrs[0].Datalen, x.heads[0] = 4+tunRecvXSlot+1, v4
|
||||
return 1
|
||||
}},
|
||||
{"bad AF prefix", func(x *tunRecvX) int {
|
||||
x.hdrs[0].Datalen, x.heads[0] = 64, [4]byte{syscall.AF_INET}
|
||||
return 1
|
||||
}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var q *tunQueue
|
||||
stubTunRecvmsgX(t, func(func(uintptr, []msgx.Hdr) (int, syscall.Errno), uintptr, []msgx.Hdr) (int, syscall.Errno) {
|
||||
return tc.mess(q.x), 0
|
||||
})
|
||||
tn, w := newReadTun(t)
|
||||
q = newTunQueue(tn)
|
||||
want := writeTunPkts(t, w, 4, 60)
|
||||
assert.Equal(t, want, readTunPkts(t, q, 4))
|
||||
assert.Nil(t, q.x)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestTunRecvXCheck pins which recvmsg_x batches tunRecvXCheck accepts.
|
||||
func TestTunRecvXCheck(t *testing.T) {
|
||||
const slot = 1000
|
||||
v4, v6 := [4]byte{3: syscall.AF_INET}, [4]byte{3: syscall.AF_INET6}
|
||||
good := func() ([]msgx.Hdr, [][4]byte) {
|
||||
hdrs := make([]msgx.Hdr, 4)
|
||||
heads := make([][4]byte, 4)
|
||||
for i := range hdrs {
|
||||
hdrs[i].Datalen = uint64(4 + 20 + i)
|
||||
heads[i] = v4
|
||||
if i%2 == 1 {
|
||||
heads[i] = v6
|
||||
}
|
||||
}
|
||||
return hdrs, heads
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
n int
|
||||
mess func(hdrs []msgx.Hdr, heads [][4]byte)
|
||||
ok bool
|
||||
}{
|
||||
{"all", 4, nil, true},
|
||||
{"none", 0, nil, true},
|
||||
{"prefix", 2, nil, true},
|
||||
{"garbage past n is ignored", 2, func(h []msgx.Hdr, hd [][4]byte) { h[3].Datalen, hd[3] = 1<<40, [4]byte{9} }, true},
|
||||
{"just the AF prefix", 1, func(h []msgx.Hdr, _ [][4]byte) { h[0].Datalen = 4 }, true},
|
||||
{"fills the slot", 1, func(h []msgx.Hdr, _ [][4]byte) { h[0].Datalen = 4 + slot }, true},
|
||||
{"more than headers", 5, nil, false},
|
||||
{"negative", -1, nil, false},
|
||||
{"past the slot", 2, func(h []msgx.Hdr, _ [][4]byte) { h[1].Datalen = 4 + slot + 1 }, false},
|
||||
{"shorter than the AF prefix", 1, func(h []msgx.Hdr, _ [][4]byte) { h[0].Datalen = 3 }, false},
|
||||
{"truncated", 3, func(h []msgx.Hdr, _ [][4]byte) { h[2].Flags = unix.MSG_TRUNC }, false},
|
||||
{"zero AF prefix", 1, func(_ []msgx.Hdr, hd [][4]byte) { hd[0] = [4]byte{} }, false},
|
||||
{"host order AF prefix", 1, func(_ []msgx.Hdr, hd [][4]byte) { hd[0] = [4]byte{syscall.AF_INET} }, false},
|
||||
{"other AF", 2, func(_ []msgx.Hdr, hd [][4]byte) { hd[1] = [4]byte{3: syscall.AF_UNIX} }, false},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
hdrs, heads := good()
|
||||
if tc.mess != nil {
|
||||
tc.mess(hdrs, heads)
|
||||
}
|
||||
err := tunRecvXCheck(tc.n, hdrs, heads, slot)
|
||||
if tc.ok {
|
||||
assert.NoError(t, err)
|
||||
} else {
|
||||
assert.Error(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestTunRecvXRefused pins which recvmsg_x errnos turn it off for good.
|
||||
func TestTunRecvXRefused(t *testing.T) {
|
||||
for _, errno := range []syscall.Errno{unix.ENOSYS, unix.EPERM, unix.EOPNOTSUPP, unix.EINVAL, unix.EMSGSIZE} {
|
||||
assert.True(t, tunRecvXRefused(errno), "%v", errno)
|
||||
}
|
||||
for _, errno := range []syscall.Errno{0, unix.EAGAIN, unix.EINTR, unix.ENOBUFS, unix.ENOMEM, unix.EBADF, unix.ENOTCONN} {
|
||||
assert.False(t, tunRecvXRefused(errno), "%v", errno)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTunRaisePendingPackets pins that the pending limit is set at SYSPROTO_CONTROL level after the receive buffer is
|
||||
// grown to hold that many packets of the tun's MTU, and capped to what the buffer holds when it can't grow.
|
||||
func TestTunRaisePendingPackets(t *testing.T) {
|
||||
type call struct{ level, opt, value int }
|
||||
stub := func(t *testing.T, rcvbuf int, failLevel, failOpt int) *[]call {
|
||||
set, get := tunSetsockoptInt, tunGetsockoptInt
|
||||
var calls []call
|
||||
tunSetsockoptInt = func(fd, level, opt, value int) error {
|
||||
calls = append(calls, call{level, opt, value})
|
||||
if level == failLevel && opt == failOpt {
|
||||
return unix.ENOBUFS
|
||||
}
|
||||
return nil
|
||||
}
|
||||
tunGetsockoptInt = func(fd, level, opt int) (int, error) { return rcvbuf, nil }
|
||||
t.Cleanup(func() { tunSetsockoptInt, tunGetsockoptInt = set, get })
|
||||
return &calls
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
rcvbuf int
|
||||
mtu int
|
||||
failLevel int
|
||||
failOpt int
|
||||
want []call
|
||||
}{
|
||||
{"grows rcvbuf first", 1 << 16, 9000, -1, -1, []call{
|
||||
{unix.SOL_SOCKET, unix.SO_RCVBUF, 64 * 9004},
|
||||
{2, 16, 64},
|
||||
}},
|
||||
{"rcvbuf already big enough", 1 << 20, 1300, -1, -1, []call{{2, 16, 64}}},
|
||||
{"rcvbuf refused caps pending", 10 * 1304, 1300, unix.SOL_SOCKET, unix.SO_RCVBUF, []call{
|
||||
{unix.SOL_SOCKET, unix.SO_RCVBUF, 64 * 1304},
|
||||
{2, 16, 10},
|
||||
}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
calls := stub(t, tc.rcvbuf, tc.failLevel, tc.failOpt)
|
||||
tn, _ := newSocketpairTun(t, 1<<20)
|
||||
tn.DefaultMTU = tc.mtu
|
||||
_, err := tn.Queues(1)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, tc.want, *calls)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTun_WriteFraming(t *testing.T) {
|
||||
r, w, err := os.Pipe()
|
||||
require.NoError(t, err)
|
||||
defer r.Close()
|
||||
defer w.Close()
|
||||
tn := &tun{f: w}
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
pkt []byte
|
||||
af byte
|
||||
}{
|
||||
// Literal families rather than syscall.AF_*, so a wrong constant in Write can't also fix the expectation.
|
||||
{"ipv4", []byte{0x45, 1, 2, 3, 4, 5}, 2},
|
||||
{"ipv6", []byte{0x60, 1, 2, 3, 4, 5}, 30},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
n, err := tn.Write(tc.pkt)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(tc.pkt), n)
|
||||
|
||||
got := make([]byte, 64)
|
||||
n, err = r.Read(got)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, append([]byte{0, 0, 0, tc.af}, tc.pkt...), got[:n])
|
||||
})
|
||||
}
|
||||
|
||||
_, err = tn.Write([]byte{0x10, 1, 2, 3})
|
||||
require.Error(t, err)
|
||||
_, err = tn.Write([]byte{})
|
||||
require.ErrorIs(t, err, syscall.EIO)
|
||||
|
||||
// The rejected writes must not have put anything on the fd, so the next read sees only this packet.
|
||||
pkt := []byte{0x45, 9, 9}
|
||||
_, err = tn.Write(pkt)
|
||||
require.NoError(t, err)
|
||||
got := make([]byte, 64)
|
||||
n, err := r.Read(got)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, append([]byte{0, 0, 0, 2}, pkt...), got[:n])
|
||||
}
|
||||
|
||||
func TestTun_ReadStripsHeader(t *testing.T) {
|
||||
r, w, err := os.Pipe()
|
||||
require.NoError(t, err)
|
||||
defer r.Close()
|
||||
defer w.Close()
|
||||
tn := &tun{f: r}
|
||||
|
||||
pkt := []byte{0x45, 1, 2, 3, 4, 5}
|
||||
_, err = w.Write(append([]byte{0, 0, 0, syscall.AF_INET}, pkt...))
|
||||
require.NoError(t, err)
|
||||
|
||||
got := make([]byte, 64)
|
||||
n, err := tn.Read(got)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, pkt, got[:n])
|
||||
}
|
||||
|
||||
func TestTun_ReadShort(t *testing.T) {
|
||||
r, w, err := os.Pipe()
|
||||
require.NoError(t, err)
|
||||
defer r.Close()
|
||||
defer w.Close()
|
||||
tn := &tun{f: r}
|
||||
|
||||
// Fewer bytes than the protocol header is not a packet.
|
||||
_, err = w.Write([]byte{0, 0})
|
||||
require.NoError(t, err)
|
||||
n, err := tn.Read(make([]byte, 64))
|
||||
require.NoError(t, err)
|
||||
require.Zero(t, n)
|
||||
|
||||
// The next packet reads cleanly only if the short read consumed exactly what was written.
|
||||
pkt := []byte{0x45, 1, 2, 3}
|
||||
_, err = w.Write(append([]byte{0, 0, 0, syscall.AF_INET}, pkt...))
|
||||
require.NoError(t, err)
|
||||
got := make([]byte, 64)
|
||||
n, err = tn.Read(got)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, pkt, got[:n])
|
||||
}
|
||||
|
||||
// A heap allocation per packet rarely shows in a short throughput run, only across GC pauses, so
|
||||
// this pins Read and Write to zero allocations directly.
|
||||
func TestTun_ReadWriteDoNotAllocate(t *testing.T) {
|
||||
r, w, err := os.Pipe()
|
||||
require.NoError(t, err)
|
||||
defer r.Close()
|
||||
defer w.Close()
|
||||
tw := &tun{f: w}
|
||||
tr := &tun{f: r}
|
||||
|
||||
pkts := [][]byte{bytes.Repeat([]byte{0x45}, 1300), bytes.Repeat([]byte{0x60}, 1300)}
|
||||
buf := make([]byte, 1500)
|
||||
i := 0
|
||||
allocs := testing.AllocsPerRun(1000, func() {
|
||||
i++
|
||||
if _, err := tw.Write(pkts[i%len(pkts)]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := tr.Read(buf); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
})
|
||||
require.Zero(t, allocs)
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build !e2e_testing
|
||||
// +build !e2e_testing
|
||||
|
||||
package overlay
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build ios && !e2e_testing
|
||||
// +build ios,!e2e_testing
|
||||
|
||||
package overlay
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build !android && !e2e_testing
|
||||
// +build !android,!e2e_testing
|
||||
|
||||
package overlay
|
||||
|
||||
@@ -16,12 +15,12 @@ import (
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/DefinedNet/netlink"
|
||||
"github.com/gaissmai/bart"
|
||||
"github.com/slackhq/nebula/config"
|
||||
"github.com/slackhq/nebula/overlay/tio"
|
||||
"github.com/slackhq/nebula/routing"
|
||||
"github.com/slackhq/nebula/util"
|
||||
"github.com/vishvananda/netlink"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
@@ -548,7 +547,7 @@ func (t *tun) setDefaultRoute(cidr netip.Prefix) error {
|
||||
if err != nil {
|
||||
t.l.Warn("Failed to set default route MTU, retrying", "error", err, "cidr", cidr)
|
||||
//retry twice more -- on some systems there appears to be a race condition where if we set routes too soon, netlink says `invalid argument`
|
||||
for i := 0; i < 2; i++ {
|
||||
for range 2 {
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
err = netlink.RouteReplace(&nr)
|
||||
if err == nil {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build !e2e_testing
|
||||
// +build !e2e_testing
|
||||
|
||||
package overlay
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build !e2e_testing
|
||||
// +build !e2e_testing
|
||||
|
||||
package overlay
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package overlay
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build !e2e_testing
|
||||
// +build !e2e_testing
|
||||
|
||||
package overlay
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
//go:build e2e_testing
|
||||
// +build e2e_testing
|
||||
|
||||
package overlay
|
||||
|
||||
|
||||
+29
-1
@@ -1,5 +1,4 @@
|
||||
//go:build !e2e_testing
|
||||
// +build !e2e_testing
|
||||
|
||||
package overlay
|
||||
|
||||
@@ -11,6 +10,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"slices"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"unsafe"
|
||||
@@ -182,6 +182,7 @@ func (t *winTun) addRoutes(logErrors bool) error {
|
||||
luid := winipcfg.LUID(t.tun.LUID())
|
||||
routes := *t.Routes.Load()
|
||||
foundDefault4 := false
|
||||
carriesV6 := slices.ContainsFunc(t.vpnNetworks, func(p netip.Prefix) bool { return p.Addr().Is6() })
|
||||
|
||||
for _, r := range routes {
|
||||
if len(r.Via) == 0 || !r.Install {
|
||||
@@ -189,6 +190,9 @@ func (t *winTun) addRoutes(logErrors bool) error {
|
||||
continue
|
||||
}
|
||||
|
||||
// A v6 unsafe_route is legal under a v4-only cert; uninstalled ones put nothing on the adapter.
|
||||
carriesV6 = carriesV6 || r.Cidr.Addr().Is6()
|
||||
|
||||
// Add our unsafe route as an on-link route to the nebula tun device.
|
||||
err := luid.AddRoute(r.Cidr, unspecifiedNextHop(r.Cidr), uint32(r.Metric))
|
||||
if err != nil {
|
||||
@@ -210,6 +214,11 @@ func (t *winTun) addRoutes(logErrors bool) error {
|
||||
}
|
||||
}
|
||||
|
||||
return t.setMTU(luid, foundDefault4, carriesV6)
|
||||
}
|
||||
|
||||
// setMTU applies tun.mtu per address family. The default route metric rides along on the v4 handle.
|
||||
func (t *winTun) setMTU(luid winipcfg.LUID, foundDefault4, carriesV6 bool) error {
|
||||
ipif, err := luid.IPInterface(windows.AF_INET)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get ip interface: %w", err)
|
||||
@@ -224,6 +233,25 @@ func (t *winTun) addRoutes(logErrors bool) error {
|
||||
if err := ipif.Set(); err != nil {
|
||||
return fmt.Errorf("failed to set ip interface: %w", err)
|
||||
}
|
||||
|
||||
// Windows tracks NLMTU per family and wintun sets neither, so v6 keeps the adapter default of 65535.
|
||||
// Gated so a v4-only overlay under 1280 boots; a v6 one deliberately does not, as linux also refuses.
|
||||
if !carriesV6 {
|
||||
return nil
|
||||
}
|
||||
|
||||
ipif6, err := luid.IPInterface(windows.AF_INET6)
|
||||
if err != nil {
|
||||
// No v6 on the adapter means there is no NLMTU to get wrong. A failed Set below is not the same thing.
|
||||
t.l.Info("Skipping ipv6 MTU, no ipv6 interface on this adapter", "error", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
ipif6.NLMTU = uint32(t.MTU)
|
||||
if err := ipif6.Set(); err != nil {
|
||||
return fmt.Errorf("failed to set ipv6 interface: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -108,7 +108,7 @@ func TestUserDeviceReadersConcurrentRace(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
run := func(idx int) {
|
||||
defer wg.Done()
|
||||
for i := 0; i < iterations; i++ {
|
||||
for range iterations {
|
||||
pkts, err := readers[idx].Read()
|
||||
if err != nil {
|
||||
errs <- err
|
||||
@@ -136,7 +136,7 @@ func TestUserDeviceReadersConcurrentRace(t *testing.T) {
|
||||
// waiting reader's private buffer, so reusing buf between writes is safe.
|
||||
go func() {
|
||||
buf := make([]byte, 32)
|
||||
for i := 0; i < 2*iterations; i++ {
|
||||
for i := range 2 * iterations {
|
||||
for j := range buf {
|
||||
buf[j] = byte(i + j)
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ func TestPacketsAreBalancedEqually(t *testing.T) {
|
||||
gw3count := 0
|
||||
|
||||
iterationCount := uint16(65535)
|
||||
for i := uint16(0); i < iterationCount; i++ {
|
||||
for i := range iterationCount {
|
||||
packet := firewall.Packet{
|
||||
LocalAddr: netip.MustParseAddr("192.168.1.1"),
|
||||
RemoteAddr: netip.MustParseAddr("10.0.0.1"),
|
||||
@@ -74,7 +74,7 @@ func TestPacketsAreBalancedByPriority(t *testing.T) {
|
||||
gw2count := 0
|
||||
|
||||
iterationCount := uint16(65535)
|
||||
for i := uint16(0); i < iterationCount; i++ {
|
||||
for i := range iterationCount {
|
||||
packet := firewall.Packet{
|
||||
LocalAddr: netip.MustParseAddr("192.168.1.1"),
|
||||
RemoteAddr: netip.MustParseAddr("10.0.0.1"),
|
||||
@@ -115,7 +115,7 @@ func TestBalancePacketDistributsRandomlyAndReturnsFalseIfBucketsNotCalculated(t
|
||||
gw1count := 0
|
||||
gw2count := 0
|
||||
|
||||
for i := uint16(0); i < iterationCount; i++ {
|
||||
for i := range iterationCount {
|
||||
packet := firewall.Packet{
|
||||
LocalAddr: netip.MustParseAddr("192.168.1.1"),
|
||||
RemoteAddr: netip.MustParseAddr("10.0.0.1"),
|
||||
|
||||
+5
-4
@@ -3,6 +3,7 @@ package routing
|
||||
import (
|
||||
"fmt"
|
||||
"net/netip"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -13,14 +14,14 @@ const (
|
||||
type Gateways []Gateway
|
||||
|
||||
func (g Gateways) String() string {
|
||||
str := ""
|
||||
var str strings.Builder
|
||||
for i, gw := range g {
|
||||
str += gw.String()
|
||||
str.WriteString(gw.String())
|
||||
if i < len(g)-1 {
|
||||
str += ", "
|
||||
str.WriteString(", ")
|
||||
}
|
||||
}
|
||||
return str
|
||||
return str.String()
|
||||
}
|
||||
|
||||
type Gateway struct {
|
||||
|
||||
+4
-8
@@ -1,21 +1,19 @@
|
||||
package nebula
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestScheduler_PooledReuse(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
ctx := t.Context()
|
||||
|
||||
s := NewScheduler[int](16)
|
||||
delivered := make(chan int, 256)
|
||||
go s.Run(ctx, func(item int) { delivered <- item })
|
||||
|
||||
const N = 100
|
||||
for i := 0; i < N; i++ {
|
||||
for i := range N {
|
||||
s.Schedule(ctx, i, time.Millisecond)
|
||||
}
|
||||
|
||||
@@ -34,8 +32,7 @@ func TestScheduler_PooledReuse(t *testing.T) {
|
||||
// BenchmarkScheduler_Schedule reports allocations per Schedule call.
|
||||
// In steady state the Scheduler's sync.Pool means we should see zero allocs per op once the pool warms up.
|
||||
func BenchmarkScheduler_Schedule(b *testing.B) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
ctx := b.Context()
|
||||
|
||||
s := NewScheduler[int](b.N)
|
||||
go s.Run(ctx, func(int) {})
|
||||
@@ -51,8 +48,7 @@ func BenchmarkScheduler_Schedule(b *testing.B) {
|
||||
// What we'd pay per Schedule if Punchy called time.AfterFunc directly without the pooled Scheduler.
|
||||
// Allocates a *time.Timer plus a closure each call.
|
||||
func BenchmarkBareAfterFunc(b *testing.B) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
ctx := b.Context()
|
||||
|
||||
queue := make(chan int, b.N)
|
||||
go func() {
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
//go:build !darwin && !send_pipeline
|
||||
|
||||
package nebula
|
||||
|
||||
import "github.com/slackhq/nebula/overlay/tio"
|
||||
|
||||
// sendPipelinePlatform is false off darwin: the pipelined send path (interface_pipeline.go) is darwin only for now,
|
||||
// and listenIn writes its own batches everywhere else. Being a constant, it removes the branch at compile time.
|
||||
// Build with -tags send_pipeline to run the pipeline, and its tests, on another platform.
|
||||
const sendPipelinePlatform = false
|
||||
|
||||
// listenInPipelined is a plain function rather than a method so this unreachable stub leaves no trace in the binary.
|
||||
func listenInPipelined(*Interface, tio.Queue, int) {}
|
||||
+2
-2
@@ -25,7 +25,7 @@ func AssertDeepCopyEqual(t *testing.T, a any, b any) {
|
||||
}
|
||||
|
||||
func traverseDeepCopy(t *testing.T, v1 reflect.Value, v2 reflect.Value, name string) bool {
|
||||
if v1.Type() == v2.Type() && v1.Type() == reflect.TypeOf(netip.Addr{}) {
|
||||
if v1.Type() == v2.Type() && v1.Type() == reflect.TypeFor[netip.Addr]() {
|
||||
// Ignore netip.Addr types since they reuse an interned global value
|
||||
return false
|
||||
}
|
||||
@@ -72,7 +72,7 @@ func traverseDeepCopy(t *testing.T, v1 reflect.Value, v2 reflect.Value, name str
|
||||
}
|
||||
return traverseDeepCopy(t, v1.Elem(), v2.Elem(), name)
|
||||
|
||||
case reflect.Ptr:
|
||||
case reflect.Pointer:
|
||||
local := reflect.ValueOf(time.Local).Pointer()
|
||||
if local == v1.Pointer() && local == v2.Pointer() {
|
||||
return true
|
||||
|
||||
@@ -48,6 +48,14 @@ type Conn interface {
|
||||
Close() error
|
||||
}
|
||||
|
||||
// EstablishedPeerConn is a Conn that treats the remotes of established tunnels differently from other
|
||||
// destinations, as darwin's gives busy ones connected sockets.
|
||||
type EstablishedPeerConn interface {
|
||||
// SetEstablishedPeer reports whether dst is the current remote of at least one established tunnel. Calls for
|
||||
// one dst must not race each other.
|
||||
SetEstablishedPeer(dst netip.AddrPort, established bool)
|
||||
}
|
||||
|
||||
type NoopConn struct{}
|
||||
|
||||
func (NoopConn) Rebind() error {
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
//go:build !e2e_testing
|
||||
|
||||
package udp
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/netip"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// benchConnSocks opens a wildcard listener with ListenOut running and cfg applied, and a loopback peer that never
|
||||
// reads, so its sends cost only the sender.
|
||||
func benchConnSocks(b *testing.B, s Settings, cfg func(*connSockConfig)) (*StdConn, netip.AddrPort) {
|
||||
b.Helper()
|
||||
c, err := NewListener(slog.New(slog.DiscardHandler), s)
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
b.Cleanup(func() { _ = c.Close() })
|
||||
u := c.(*StdConn)
|
||||
if cfg != nil {
|
||||
cfg(&u.socks.cfg)
|
||||
}
|
||||
go func() { _ = u.ListenOut(func(netip.AddrPort, []byte) {}, func() {}) }()
|
||||
for !u.listening.Load() {
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
pc, err := net.ListenPacket("udp4", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
b.Fatal(err)
|
||||
}
|
||||
b.Cleanup(func() { _ = pc.Close() })
|
||||
return u, pc.LocalAddr().(*net.UDPAddr).AddrPort()
|
||||
}
|
||||
|
||||
// BenchmarkConnSockWriteTo measures one WriteTo of a full-size datagram down each path: the listener with
|
||||
// connected sockets off, to a destination that is no tunnel's remote while another is, to an established peer
|
||||
// that never gets busy, and on a connected socket.
|
||||
func BenchmarkConnSockWriteTo(b *testing.B) {
|
||||
pkt := make([]byte, 1400)
|
||||
cases := []struct {
|
||||
name string
|
||||
cfg func(*connSockConfig)
|
||||
setup func(b *testing.B, u *StdConn, dst netip.AddrPort)
|
||||
}{
|
||||
{name: "listener/off", cfg: func(c *connSockConfig) { c.max = 0 }},
|
||||
{name: "listener/ineligible", setup: func(b *testing.B, u *StdConn, dst netip.AddrPort) {
|
||||
u.SetEstablishedPeer(netip.AddrPortFrom(dst.Addr(), dst.Port()+1), true)
|
||||
}},
|
||||
{name: "listener/eligible-not-busy", cfg: func(c *connSockConfig) { c.run = 1 << 30 },
|
||||
setup: func(b *testing.B, u *StdConn, dst netip.AddrPort) { u.SetEstablishedPeer(dst, true) }},
|
||||
{name: "connected", setup: func(b *testing.B, u *StdConn, dst netip.AddrPort) {
|
||||
u.SetEstablishedPeer(dst, true)
|
||||
for range u.socks.cfg.run {
|
||||
_ = u.WriteTo(pkt, dst)
|
||||
}
|
||||
if u.connSockFor(dst) == nil {
|
||||
b.Fatal("no connected socket")
|
||||
}
|
||||
}},
|
||||
}
|
||||
for _, c := range cases {
|
||||
b.Run(c.name, func(b *testing.B) {
|
||||
u, dst := benchConnSocks(b, Settings{Listen: netip.MustParseAddrPort("0.0.0.0:0")}, c.cfg)
|
||||
if c.setup != nil {
|
||||
c.setup(b, u, dst)
|
||||
}
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for range b.N {
|
||||
_ = u.WriteTo(pkt, dst)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// BenchmarkConnSockDispatch measures what WriteTo adds before the listener's sendto, without the syscall: a v4
|
||||
// listener refuses a v6 destination before sending, with connected sockets off or with another peer established.
|
||||
func BenchmarkConnSockDispatch(b *testing.B) {
|
||||
pkt := make([]byte, 1400)
|
||||
dst := netip.MustParseAddrPort("[::1]:4242")
|
||||
for _, c := range []struct {
|
||||
name string
|
||||
cfg func(*connSockConfig)
|
||||
establish bool
|
||||
}{
|
||||
{name: "off", cfg: func(c *connSockConfig) { c.max = 0 }},
|
||||
{name: "ineligible", establish: true},
|
||||
} {
|
||||
b.Run(c.name, func(b *testing.B) {
|
||||
u, peer := benchConnSocks(b, Settings{Listen: netip.MustParseAddrPort("127.0.0.1:0"), Multi: true}, c.cfg)
|
||||
if c.establish {
|
||||
u.SetEstablishedPeer(peer, true)
|
||||
}
|
||||
b.ReportAllocs()
|
||||
b.ResetTimer()
|
||||
for range b.N {
|
||||
if u.WriteTo(pkt, dst) == nil {
|
||||
b.Fatal("sent a v6 datagram on a v4 listener")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,839 @@
|
||||
//go:build !e2e_testing
|
||||
|
||||
package udp
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"maps"
|
||||
"net"
|
||||
"net/netip"
|
||||
"os"
|
||||
"slices"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/slackhq/nebula/internal/msgx"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// A connected socket is a UDP socket connected to one busy peer, on the listener's port and bound to the source
|
||||
// address the listener's route to that peer picks, so the peer sees the same address and port from either. A send
|
||||
// on a connected socket skips the per-datagram address and MAC policy checks, and the EndpointSecurity send check
|
||||
// that xprotectd can hold every unconnected sendto on, and it is the only UDP send that reports the interface
|
||||
// queue's flow control, as ENOBUFS. A connected socket wins xnu's lookup for its peer's datagrams; it never sees
|
||||
// anyone else's.
|
||||
//
|
||||
// A connected socket sets SO_REUSEPORT and binds a specific address beside a wildcard listener, which sets
|
||||
// SO_REUSEPORT only for listen routines. xnu refuses a wildcard SO_REUSEPORT bind beside a holder without the
|
||||
// option, and refuses another uid's bind to an address a socket holds specifically, so no other user can join the
|
||||
// port while connected sockets open and close: uid nobody got EADDRINUSE on every bind against a root listener
|
||||
// with connected sockets in the 09-25 probes. A connected socket is closed, never disconnected, because xnu widens
|
||||
// a disconnected socket's local address to the wildcard.
|
||||
//
|
||||
// Only the current remote of an established tunnel may get one: Nebula names those with SetEstablishedPeer, so an
|
||||
// unauthenticated sender can't earn a connected socket by drawing replies such as recv_error or handshake
|
||||
// responses.
|
||||
type connSock struct {
|
||||
dst netip.AddrPort
|
||||
peer *connSockPeer
|
||||
conn *net.UDPConn
|
||||
rc syscall.RawConn
|
||||
// used is the monotonic time in nanoseconds of the connected socket's last send or receive.
|
||||
used atomic.Int64
|
||||
// stallAt is the monotonic time of the first ENOBUFS or EAGAIN since a send last went through, or 0, and
|
||||
// dropping is set once that stall has dropped a datagram, so each stall logs its drops once.
|
||||
stallAt atomic.Int64
|
||||
dropping atomic.Bool
|
||||
// parkMu serializes the writes that wait for a full send buffer, since the write deadline bounding each wait
|
||||
// is the descriptor's, not the writer's.
|
||||
parkMu sync.Mutex
|
||||
// sys writes bufs on the descriptor and returns how many went out: sendFd, or a stand-in for the interface in
|
||||
// tests.
|
||||
sys func(fd int, bufs [][]byte, x *sendScratch) (int, error)
|
||||
}
|
||||
|
||||
// sendScratch is the msghdr_x array one sendmsg_x reads.
|
||||
type sendScratch struct {
|
||||
hdrs [msgx.Batch]msgx.Hdr
|
||||
iovs [msgx.Batch]unix.Iovec
|
||||
}
|
||||
|
||||
// noSendX is set once sendmsg_x is refused; connected sockets then write one datagram per syscall.
|
||||
var noSendX atomic.Bool
|
||||
|
||||
// sendFd writes bufs on fd, a lone datagram with write and more with one sendmsg_x, which xnu runs through the stack
|
||||
// as a batch on a connected socket. It returns how many went out, or none and the errno. sendmsg_x reports EAGAIN,
|
||||
// ENOBUFS and EMSGSIZE only when it took nothing, and any other error without the count it took, which a connected
|
||||
// socket gets only on its first datagram, as its peer's unreachable.
|
||||
func sendFd(fd int, bufs [][]byte, x *sendScratch) (int, error) {
|
||||
if len(bufs) == 1 {
|
||||
if _, err := syscall.Write(fd, bufs[0]); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return 1, nil
|
||||
}
|
||||
n := min(len(bufs), msgx.Batch)
|
||||
for i, b := range bufs[:n] {
|
||||
x.iovs[i] = unix.Iovec{Base: unsafe.SliceData(b)}
|
||||
x.iovs[i].SetLen(len(b))
|
||||
x.hdrs[i] = msgx.Hdr{Iov: &x.iovs[i], Iovlen: 1}
|
||||
}
|
||||
sent, errno := msgx.Send(uintptr(fd), x.hdrs[:n])
|
||||
clear(x.iovs[:n])
|
||||
if errno != 0 {
|
||||
return 0, errno
|
||||
}
|
||||
if sent == 0 {
|
||||
return 0, syscall.EAGAIN
|
||||
}
|
||||
return sent, nil
|
||||
}
|
||||
|
||||
// errStallSpent is a write that found the connected socket's stall budget spent, so its datagram is dropped.
|
||||
var errStallSpent = errors.New("connected socket stall budget spent")
|
||||
|
||||
// connSockWriter carries one write through RawConn.Write. They are pooled with fn bound once, since a closure
|
||||
// passed through the RawConn interface escapes and would allocate on every send.
|
||||
type connSockWriter struct {
|
||||
s *connSock
|
||||
// bufs holds a copy of the datagrams' slice headers rather than the caller's slice, which would escape
|
||||
// through the pool and cost WriteTo's one-datagram array an allocation per send.
|
||||
bufs [msgx.Batch][]byte
|
||||
nbufs int
|
||||
park bool
|
||||
n int
|
||||
err error
|
||||
fn func(fd uintptr) bool
|
||||
x sendScratch
|
||||
}
|
||||
|
||||
var connSockWriters = sync.Pool{New: func() any {
|
||||
w := &connSockWriter{}
|
||||
w.fn = w.write
|
||||
return w
|
||||
}}
|
||||
|
||||
func (w *connSockWriter) write(fd uintptr) bool {
|
||||
for {
|
||||
w.n, w.err = w.s.sys(int(fd), w.bufs[:w.nbufs], &w.x)
|
||||
switch {
|
||||
case w.err == syscall.EINTR:
|
||||
case w.err == syscall.EAGAIN && w.park:
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// try writes bufs on s once and returns how many went out, or none and the write's errno,
|
||||
// os.ErrDeadlineExceeded, or net.ErrClosed. With park set, EAGAIN waits in the netpoller until the socket is
|
||||
// writable or the write deadline passes; without it, EAGAIN is returned.
|
||||
func (s *connSock) try(bufs [][]byte, park bool) (int, error) {
|
||||
w := connSockWriters.Get().(*connSockWriter)
|
||||
w.s, w.nbufs, w.park = s, copy(w.bufs[:], bufs), park
|
||||
rerr := s.rc.Write(w.fn)
|
||||
n, err := w.n, w.err
|
||||
clear(w.bufs[:w.nbufs])
|
||||
w.s, w.nbufs, w.n, w.err = nil, 0, 0, nil
|
||||
connSockWriters.Put(w)
|
||||
switch {
|
||||
case rerr == nil:
|
||||
return n, err
|
||||
case errors.Is(rerr, os.ErrDeadlineExceeded):
|
||||
return 0, os.ErrDeadlineExceeded
|
||||
default:
|
||||
return 0, net.ErrClosed
|
||||
}
|
||||
}
|
||||
|
||||
// connSockConfig holds the knobs, which tests change.
|
||||
type connSockConfig struct {
|
||||
// max is how many connected sockets the listen routines may have open at once; 0 turns connected sockets off.
|
||||
max int
|
||||
// run is how many datagrams to one destination within one window earn it a connected socket. darwin's tun
|
||||
// hands over one packet per read, so a peer's sends arrive one WriteTo or one-datagram batch at a time and
|
||||
// busyness has to be counted across calls.
|
||||
run int
|
||||
window time.Duration
|
||||
// idle closes a connected socket after it has neither sent nor received for this long.
|
||||
idle time.Duration
|
||||
// backoff keeps a destination whose connected socket failed on the listener for this long.
|
||||
backoff time.Duration
|
||||
// openEvery is how often the open bucket earns back an open once max are spent, which bounds connected socket
|
||||
// churn however fast senders earn connected sockets and errors close them.
|
||||
openEvery time.Duration
|
||||
// enobufsMax is how long a connected socket's sends may wait out ENOBUFS without one going through before they
|
||||
// drop instead, so a suspended interface costs one write per datagram rather than stalling the tun reader.
|
||||
enobufsMax time.Duration
|
||||
}
|
||||
|
||||
func defaultConnSockConfig() connSockConfig {
|
||||
return connSockConfig{
|
||||
max: 8,
|
||||
run: 64,
|
||||
window: time.Second,
|
||||
idle: 30 * time.Second,
|
||||
backoff: 30 * time.Second,
|
||||
openEvery: time.Second,
|
||||
enobufsMax: 10 * time.Millisecond,
|
||||
}
|
||||
}
|
||||
|
||||
// connSockGroups holds the connSocks that the listen routines bound to one address share, so the limits, the
|
||||
// connected sockets and Rebind cover every routine, and a peer has at most one connected socket.
|
||||
var connSockGroups = struct {
|
||||
sync.Mutex
|
||||
m map[netip.AddrPort]*connSocks
|
||||
}{m: map[netip.AddrPort]*connSocks{}}
|
||||
|
||||
// joinConnSocks returns the connSocks u shares with the other listen routines bound to la, and makes it for the
|
||||
// first.
|
||||
func joinConnSocks(u *StdConn, la netip.AddrPort, s Settings) *connSocks {
|
||||
g := &connSockGroups
|
||||
g.Lock()
|
||||
defer g.Unlock()
|
||||
if cs := g.m[la]; s.Multi && cs != nil {
|
||||
cs.mu.Lock()
|
||||
cs.members = append(cs.members, u)
|
||||
cs.mu.Unlock()
|
||||
return cs
|
||||
}
|
||||
cs := &connSocks{cfg: defaultConnSockConfig(), key: la, members: []*StdConn{u}, clock: monoNow, sleep: time.Sleep}
|
||||
if cs.cfg.max > 0 && !u.listenHost.IsUnspecified() && !s.Multi {
|
||||
// A connected socket on a specific listen.host binds the listener's own address, which xnu allows only when
|
||||
// both sockets set SO_REUSEPORT, and a listener with it lets another uid bind a dual-stack wildcard on the
|
||||
// port.
|
||||
cs.off.Store(true)
|
||||
u.l.Debug("connected sockets: off for a specific listen.host", "addr", s.Listen)
|
||||
}
|
||||
if s.Multi {
|
||||
g.m[la] = cs
|
||||
}
|
||||
return cs
|
||||
}
|
||||
|
||||
type connSocks struct {
|
||||
cfg connSockConfig
|
||||
// key is the listeners' bound address, which connSockGroups files this under.
|
||||
key netip.AddrPort
|
||||
// off keeps connected sockets from opening, when the listener's address can't be shared with them.
|
||||
off atomic.Bool
|
||||
// table maps each destination that is the remote of an established tunnel, and that a connected socket could
|
||||
// reach, to its peer; it is nil when there are none, so a send with nothing to consider costs one load. Every
|
||||
// send reads it without a lock, and mu guards copying it. An open that holds mu and still finds its peer here
|
||||
// can't outlive the tunnel.
|
||||
table atomic.Pointer[map[netip.AddrPort]*connSockPeer]
|
||||
// reserved counts the connected sockets that are open, opening or closing, against cfg.max; mu guards changing it.
|
||||
reserved atomic.Int32
|
||||
mu sync.Mutex
|
||||
closed bool
|
||||
// members are the listen routines' listeners that share these connected sockets.
|
||||
members []*StdConn
|
||||
// pending holds each destination whose connected socket is opening or closing, and not on its peer. xnu
|
||||
// refuses to connect a 4-tuple that is still bound, so a destination in pending can't open another.
|
||||
pending map[netip.AddrPort]struct{}
|
||||
// rebinds counts Rebinds, so an open that copied the listener's interface before one is discarded.
|
||||
rebinds uint64
|
||||
// closers tracks the goroutines that close detached connected sockets.
|
||||
closers sync.WaitGroup
|
||||
// done stops the reaper; it is made when ListenOut starts the connected sockets.
|
||||
done chan struct{}
|
||||
// opens bounds connected socket opens to cfg.max at once and one per cfg.openEvery after.
|
||||
opens gcra
|
||||
// logs bounds connected socket log lines at info or warn; see connSockLogBurst.
|
||||
logs gcra
|
||||
// clock returns monotonic nanoseconds and sleep waits out ENOBUFS: monoNow and time.Sleep, or stand-ins in
|
||||
// tests.
|
||||
clock func() int64
|
||||
sleep func(time.Duration)
|
||||
// dialHook and closeHook, when set, run just before an open's dials and a close's Close, outside mu; tests
|
||||
// use them to hold an open or close in flight.
|
||||
dialHook, closeHook func(dst netip.AddrPort)
|
||||
}
|
||||
|
||||
// connSockPeer is what connSocks keeps about an established tunnel's remote: its connected socket, how busy it is,
|
||||
// and how long a failed connected socket keeps it on the listener.
|
||||
type connSockPeer struct {
|
||||
sock atomic.Pointer[connSock]
|
||||
// windowMu guards start, the monotonic time the current window began, and n, the datagrams sent in it, so a
|
||||
// window's reset and its counts can't interleave with another sender's.
|
||||
windowMu sync.Mutex
|
||||
start int64
|
||||
n int
|
||||
// retryAt is the monotonic time before which a peer whose connected socket failed stays on the listener;
|
||||
// connSocks.mu guards it.
|
||||
retryAt int64
|
||||
}
|
||||
|
||||
// busy counts n more datagrams to p and reports whether it has now sent cfg.run within one window. A busy peer's
|
||||
// count starts over, so a peer that can't get a connected socket retries at most once per cfg.run sends.
|
||||
func (p *connSockPeer) busy(now int64, n int, cfg *connSockConfig) bool {
|
||||
p.windowMu.Lock()
|
||||
defer p.windowMu.Unlock()
|
||||
if time.Duration(now-p.start) > cfg.window {
|
||||
p.start, p.n = now, 0
|
||||
}
|
||||
p.n += n
|
||||
if p.n < cfg.run {
|
||||
return false
|
||||
}
|
||||
p.n = 0
|
||||
return true
|
||||
}
|
||||
|
||||
// connSockLogBurst and connSockLogEvery size the bucket that lets connected socket opens, closes and failures log
|
||||
// at info or warn; past it they log at debug, so churn can't flood the log.
|
||||
const (
|
||||
connSockLogBurst = 32
|
||||
connSockLogEvery = 10 * time.Second
|
||||
)
|
||||
|
||||
var monoStart = time.Now()
|
||||
|
||||
func monoNow() int64 { return int64(time.Since(monoStart)) }
|
||||
|
||||
// gcra is a token bucket kept as the monotonic time it is next full (the generic cell rate algorithm), so it needs
|
||||
// no refill tick.
|
||||
type gcra struct{ tat int64 }
|
||||
|
||||
// take spends a token from a bucket of burst that earns one back per every, and reports whether there was one to
|
||||
// spend.
|
||||
func (g *gcra) take(now int64, burst int, every time.Duration) bool {
|
||||
e := int64(every)
|
||||
tat := max(g.tat, now)
|
||||
if tat-now > int64(burst-1)*e {
|
||||
return false
|
||||
}
|
||||
g.tat = tat + e
|
||||
return true
|
||||
}
|
||||
|
||||
// logLevelLocked returns level while the log bucket has a token, and debug once it is spent.
|
||||
func (cs *connSocks) logLevelLocked(level slog.Level) slog.Level {
|
||||
if cs.logs.take(cs.clock(), connSockLogBurst, connSockLogEvery) {
|
||||
return level
|
||||
}
|
||||
return slog.LevelDebug
|
||||
}
|
||||
|
||||
// startConnSocks starts the idle reaper.
|
||||
func (u *StdConn) startConnSocks() {
|
||||
cs := u.socks
|
||||
cs.mu.Lock()
|
||||
defer cs.mu.Unlock()
|
||||
if cs.closed || cs.done != nil || cs.cfg.max <= 0 || cs.off.Load() {
|
||||
return
|
||||
}
|
||||
cs.done = make(chan struct{})
|
||||
go u.reapConnSocks(cs.done)
|
||||
}
|
||||
|
||||
// stopConnSocks closes every connected socket and keeps new ones from opening, once any listen routine's listener
|
||||
// closes, as they do together at shutdown. It returns once their descriptors are closed.
|
||||
func (u *StdConn) stopConnSocks() {
|
||||
cs := u.socks
|
||||
cs.mu.Lock()
|
||||
cs.members = slices.DeleteFunc(cs.members, func(m *StdConn) bool { return m == u })
|
||||
if !cs.closed {
|
||||
cs.closed = true
|
||||
if cs.done != nil {
|
||||
close(cs.done)
|
||||
}
|
||||
u.detachAllLocked("close")
|
||||
}
|
||||
cs.mu.Unlock()
|
||||
connSockGroups.Lock()
|
||||
if connSockGroups.m[cs.key] == cs {
|
||||
delete(connSockGroups.m, cs.key)
|
||||
}
|
||||
connSockGroups.Unlock()
|
||||
cs.closers.Wait()
|
||||
}
|
||||
|
||||
// siblings returns the other listen routines' listeners.
|
||||
func (cs *connSocks) siblings(u *StdConn) []*StdConn {
|
||||
cs.mu.Lock()
|
||||
defer cs.mu.Unlock()
|
||||
return slices.DeleteFunc(slices.Clone(cs.members), func(m *StdConn) bool { return m == u })
|
||||
}
|
||||
|
||||
// connSockFor returns dst's connected socket, or nil.
|
||||
func (u *StdConn) connSockFor(dst netip.AddrPort) *connSock {
|
||||
if p := u.socks.peer(dst); p != nil {
|
||||
return p.sock.Load()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cs *connSocks) peer(dst netip.AddrPort) *connSockPeer {
|
||||
if t := cs.table.Load(); t != nil {
|
||||
return (*t)[dst]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetEstablishedPeer makes dst eligible for a connected socket while it is the remote of an established tunnel,
|
||||
// and closes the one it has once it no longer is. Nebula calls it for every listen routine under the hostmap's
|
||||
// write lock, so repeats are no-ops and it makes no syscalls: the descriptor is closed by another goroutine.
|
||||
func (u *StdConn) SetEstablishedPeer(dst netip.AddrPort, established bool) {
|
||||
cs := u.socks
|
||||
if cs.cfg.max <= 0 || cs.off.Load() || !u.connSockable(dst) {
|
||||
return
|
||||
}
|
||||
cs.mu.Lock()
|
||||
defer cs.mu.Unlock()
|
||||
t := cs.table.Load()
|
||||
p := cs.peer(dst)
|
||||
if cs.closed || (p != nil) == established {
|
||||
return
|
||||
}
|
||||
next := map[netip.AddrPort]*connSockPeer{}
|
||||
if t != nil {
|
||||
next = maps.Clone(*t)
|
||||
}
|
||||
if established {
|
||||
p = &connSockPeer{start: cs.clock()}
|
||||
next[dst] = p
|
||||
} else {
|
||||
delete(next, dst)
|
||||
if s := p.sock.Load(); s != nil {
|
||||
u.detachLocked(s, "tunnel gone", false)
|
||||
}
|
||||
}
|
||||
cs.storeTableLocked(next)
|
||||
}
|
||||
|
||||
// storeTableLocked publishes t, or nil for an empty one.
|
||||
func (cs *connSocks) storeTableLocked(t map[netip.AddrPort]*connSockPeer) {
|
||||
if len(t) == 0 {
|
||||
cs.table.Store(nil)
|
||||
return
|
||||
}
|
||||
cs.table.Store(&t)
|
||||
}
|
||||
|
||||
// connSockable reports whether a connected socket to dst could send what the listener would. A v4 listener can't
|
||||
// reach a v6 destination at all, and a connected socket bound to a specific listen.host must share its family.
|
||||
func (u *StdConn) connSockable(dst netip.AddrPort) bool {
|
||||
if !dst.IsValid() || dst.Port() == 0 {
|
||||
return false
|
||||
}
|
||||
if u.isV4 {
|
||||
return dst.Addr().Is4()
|
||||
}
|
||||
return u.listenHost.IsUnspecified() || u.listenHost.Unmap().Is4() == dst.Addr().Unmap().Is4()
|
||||
}
|
||||
|
||||
// connSockRun sends bufs, all to p's destination, on p's connected socket, opening one if they make p busy. It
|
||||
// returns how many went out; when that is short of len(bufs), err is the failure of bufs[sent], or nil to leave
|
||||
// bufs[sent:] to the listener.
|
||||
func (u *StdConn) connSockRun(p *connSockPeer, dst netip.AddrPort, bufs [][]byte) (sent int, err error) {
|
||||
s := p.sock.Load()
|
||||
if s == nil {
|
||||
if s = u.maybeOpenConnSock(p, dst, len(bufs)); s == nil {
|
||||
return 0, nil
|
||||
}
|
||||
}
|
||||
sent, errno, closed := u.connSockWrite(s, bufs)
|
||||
switch {
|
||||
case sent == len(bufs):
|
||||
case connSockDead(errno):
|
||||
u.closeConnSock(s, errno.Error(), true)
|
||||
case !closed:
|
||||
return sent, &net.OpError{Op: "write", Err: errno}
|
||||
}
|
||||
return sent, nil
|
||||
}
|
||||
|
||||
// maybeOpenConnSock counts n datagrams to p, and opens a connected socket to it once it is busy and nothing rules
|
||||
// one out.
|
||||
func (u *StdConn) maybeOpenConnSock(p *connSockPeer, dst netip.AddrPort, n int) *connSock {
|
||||
cs := u.socks
|
||||
if n <= 0 || !u.listening.Load() || int(cs.reserved.Load()) >= cs.cfg.max || !p.busy(cs.clock(), n, &cs.cfg) {
|
||||
return nil
|
||||
}
|
||||
cs.mu.Lock()
|
||||
if cs.closed || cs.peer(dst) != p {
|
||||
cs.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
if s := p.sock.Load(); s != nil {
|
||||
cs.mu.Unlock()
|
||||
return s
|
||||
}
|
||||
now := cs.clock()
|
||||
if _, ok := cs.pending[dst]; ok || int(cs.reserved.Load()) >= cs.cfg.max || now < p.retryAt ||
|
||||
!cs.opens.take(now, cs.cfg.max, cs.cfg.openEvery) {
|
||||
cs.mu.Unlock()
|
||||
return nil
|
||||
}
|
||||
if cs.pending == nil {
|
||||
cs.pending = map[netip.AddrPort]struct{}{}
|
||||
}
|
||||
cs.pending[dst] = struct{}{}
|
||||
cs.reserved.Add(1)
|
||||
rebinds := cs.rebinds
|
||||
cs.mu.Unlock()
|
||||
|
||||
if cs.dialHook != nil {
|
||||
cs.dialHook(dst)
|
||||
}
|
||||
s, err := u.dialConnSock(p, dst)
|
||||
|
||||
cs.mu.Lock()
|
||||
if err != nil {
|
||||
delete(cs.pending, dst)
|
||||
cs.reserved.Add(-1)
|
||||
p.retryAt = cs.clock() + int64(cs.cfg.backoff)
|
||||
level := cs.logLevelLocked(slog.LevelWarn)
|
||||
cs.mu.Unlock()
|
||||
u.l.Log(context.Background(), level, "connected socket: open failed, staying on the listener", "udpAddr", dst, "error", err)
|
||||
return nil
|
||||
}
|
||||
if cs.closed || cs.rebinds != rebinds || cs.peer(dst) != p {
|
||||
// The tunnel went away, or a Rebind or shutdown ran, while the socket was opening; it stays in pending until
|
||||
// it is closed.
|
||||
cs.mu.Unlock()
|
||||
u.finishClose(s)
|
||||
u.l.Debug("connected socket: discarded, its tunnel or route changed while it opened", "udpAddr", dst)
|
||||
return nil
|
||||
}
|
||||
delete(cs.pending, dst)
|
||||
p.sock.Store(s)
|
||||
go u.readConnSock(s)
|
||||
level := cs.logLevelLocked(slog.LevelInfo)
|
||||
cs.mu.Unlock()
|
||||
u.l.Log(context.Background(), level, "connected socket: opened", "udpAddr", dst, "local", s.conn.LocalAddr(), "open", cs.reserved.Load())
|
||||
return s
|
||||
}
|
||||
|
||||
// dialConnSock opens a socket on the listener's port with SO_REUSEPORT, bound to the configured listen.host or,
|
||||
// for a wildcard listener, to the source address the route to dst picks, and connected to dst. It carries the
|
||||
// listener's IP_BOUND_IF, so both route the same way.
|
||||
func (u *StdConn) dialConnSock(p *connSockPeer, dst netip.AddrPort) (*connSock, error) {
|
||||
raddr := net.UDPAddrFromAddrPort(netip.AddrPortFrom(dst.Addr().Unmap(), dst.Port()))
|
||||
network := "udp6"
|
||||
if raddr.IP.To4() != nil {
|
||||
network = "udp4"
|
||||
}
|
||||
ifindex, err := u.boundIf()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
local := &net.UDPAddr{IP: u.listenHost.AsSlice(), Zone: u.listenHost.Zone()}
|
||||
if u.listenHost.IsUnspecified() {
|
||||
// A connected socket gets the source address xnu would pick for an unconnected send to dst.
|
||||
d := net.Dialer{Control: connSockControl(false, ifindex)}
|
||||
probe, err := d.Dial(network, raddr.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
local = probe.LocalAddr().(*net.UDPAddr)
|
||||
_ = probe.Close()
|
||||
}
|
||||
local.Port = int(u.port)
|
||||
d := net.Dialer{LocalAddr: local, Control: connSockControl(true, ifindex)}
|
||||
c, err := d.Dial(network, raddr.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
uc := c.(*net.UDPConn)
|
||||
rc, err := uc.SyscallConn()
|
||||
if err != nil {
|
||||
_ = uc.Close()
|
||||
return nil, err
|
||||
}
|
||||
s := &connSock{dst: dst, peer: p, conn: uc, rc: rc, sys: sendFd}
|
||||
s.used.Store(u.socks.clock())
|
||||
return s, nil
|
||||
}
|
||||
|
||||
// connSockControl sets SO_REUSEPORT when reuse is set, and IP_BOUND_IF or IPV6_BOUND_IF when ifindex isn't 0,
|
||||
// before the socket binds.
|
||||
func connSockControl(reuse bool, ifindex int) func(network, address string, c syscall.RawConn) error {
|
||||
return func(network, _ string, c syscall.RawConn) error {
|
||||
var serr error
|
||||
err := c.Control(func(fd uintptr) {
|
||||
if reuse {
|
||||
serr = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
|
||||
}
|
||||
if serr == nil && ifindex != 0 {
|
||||
if network == "udp4" {
|
||||
serr = unix.SetsockoptInt(int(fd), unix.IPPROTO_IP, unix.IP_BOUND_IF, ifindex)
|
||||
} else {
|
||||
serr = unix.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, ifindex)
|
||||
}
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return serr
|
||||
}
|
||||
}
|
||||
|
||||
// boundIf returns the interface index the listener is scoped to, or 0. An error keeps the peer on the listener,
|
||||
// since a connected socket opened without the listener's scope could route out an interface the listener can't.
|
||||
func (u *StdConn) boundIf() (int, error) {
|
||||
level, opt := unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF
|
||||
if u.isV4 {
|
||||
level, opt = unix.IPPROTO_IP, unix.IP_BOUND_IF
|
||||
}
|
||||
v, err := unix.GetsockoptInt(int(u.sysFd), level, opt)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("reading the listener's bound interface: %w", err)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// closeConnSock closes s if it is still open. With backoff set, the peer stays on the listener until the backoff
|
||||
// runs out.
|
||||
func (u *StdConn) closeConnSock(s *connSock, why string, backoff bool) {
|
||||
u.socks.mu.Lock()
|
||||
u.detachLocked(s, why, backoff)
|
||||
u.socks.mu.Unlock()
|
||||
}
|
||||
|
||||
// detachLocked takes s off its peer, if it is still there, and closes it on another goroutine, since callers such
|
||||
// as SetEstablishedPeer hold locks that no socket syscall should run under. s.dst stays in pending until the close
|
||||
// completes.
|
||||
func (u *StdConn) detachLocked(s *connSock, why string, backoff bool) {
|
||||
cs := u.socks
|
||||
if !s.peer.sock.CompareAndSwap(s, nil) {
|
||||
return
|
||||
}
|
||||
if backoff {
|
||||
s.peer.retryAt = cs.clock() + int64(cs.cfg.backoff)
|
||||
}
|
||||
if cs.pending == nil {
|
||||
cs.pending = map[netip.AddrPort]struct{}{}
|
||||
}
|
||||
cs.pending[s.dst] = struct{}{}
|
||||
level := cs.logLevelLocked(slog.LevelInfo)
|
||||
cs.closers.Add(1)
|
||||
go func() {
|
||||
defer cs.closers.Done()
|
||||
open := u.finishClose(s)
|
||||
u.l.Log(context.Background(), level, "connected socket: closed", "udpAddr", s.dst, "reason", why, "open", open)
|
||||
}()
|
||||
}
|
||||
|
||||
// finishClose closes s, whose destination is in pending, then lets the destination open again, and returns how
|
||||
// many connected sockets are left. Closing waits only for a write already in a syscall: a write parked on a full
|
||||
// buffer is woken, and one waiting out ENOBUFS holds no reference on the descriptor.
|
||||
func (u *StdConn) finishClose(s *connSock) int32 {
|
||||
cs := u.socks
|
||||
if cs.closeHook != nil {
|
||||
cs.closeHook(s.dst)
|
||||
}
|
||||
_ = s.conn.Close()
|
||||
cs.mu.Lock()
|
||||
defer cs.mu.Unlock()
|
||||
delete(cs.pending, s.dst)
|
||||
return cs.reserved.Add(-1)
|
||||
}
|
||||
|
||||
// detachAllLocked detaches every connected socket, for a network change or shutdown.
|
||||
func (u *StdConn) detachAllLocked(why string) {
|
||||
if t := u.socks.table.Load(); t != nil {
|
||||
for _, p := range *t {
|
||||
if s := p.sock.Load(); s != nil {
|
||||
u.detachLocked(s, why, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// closeConnSocks detaches every connected socket and discards the ones still opening, for a network change.
|
||||
func (u *StdConn) closeConnSocks(why string) {
|
||||
u.socks.mu.Lock()
|
||||
u.socks.rebinds++
|
||||
u.detachAllLocked(why)
|
||||
u.socks.mu.Unlock()
|
||||
}
|
||||
|
||||
func (u *StdConn) reapConnSocks(done <-chan struct{}) {
|
||||
cs := u.socks
|
||||
idle := cs.cfg.idle
|
||||
tick := time.NewTicker(max(min(idle/4, 5*time.Second), time.Millisecond))
|
||||
defer tick.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-done:
|
||||
return
|
||||
case <-tick.C:
|
||||
}
|
||||
t := cs.table.Load()
|
||||
if t == nil {
|
||||
continue
|
||||
}
|
||||
now := cs.clock()
|
||||
for _, p := range *t {
|
||||
if s := p.sock.Load(); s != nil && time.Duration(now-s.used.Load()) > idle {
|
||||
u.closeConnSock(s, "idle", false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// connSockDead reports whether a connected socket send or receive error means the connected path to the peer is
|
||||
// gone, so its traffic belongs back on the listener. A connected UDP socket reports a peer's ICMP unreachable as
|
||||
// ECONNREFUSED on its next call, which the unconnected listener never sees.
|
||||
func connSockDead(errno syscall.Errno) bool {
|
||||
switch errno {
|
||||
case unix.ECONNREFUSED, unix.EHOSTUNREACH, unix.EHOSTDOWN, unix.ENETUNREACH, unix.ENETDOWN,
|
||||
unix.EADDRNOTAVAIL, unix.EPIPE, unix.ENOTCONN, unix.EDESTADDRREQ:
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// enobufsWait is how long a connected socket send sleeps when the interface queue is flow controlled. The kernel
|
||||
// posts no wakeup when the advisory lifts, so this polls; 50us kept a gigabit link full without spinning a core in
|
||||
// the 09-25 probes.
|
||||
const enobufsWait = 50 * time.Microsecond
|
||||
|
||||
// connSockWrite writes bufs on s, up to msgx.Batch datagrams per sendmsg_x. A full socket buffer parks the caller in the netpoller
|
||||
// until the socket is writable, and a flow-controlled interface makes it wait out ENOBUFS, so a busy link slows
|
||||
// the tun reader instead of dropping. Once the connected socket has waited cfg.enobufsMax without a send going
|
||||
// through, either one drops the datagram instead, and a dropped datagram counts as sent, as the listener's
|
||||
// unreported interface drops do; it never falls back to the listener, which would reorder it. closed reports that
|
||||
// the connected socket was closed before bufs[sent] went out; any other error stops the run and is returned with
|
||||
// how many went out.
|
||||
func (u *StdConn) connSockWrite(s *connSock, bufs [][]byte) (sent int, errno syscall.Errno, closed bool) {
|
||||
cs := u.socks
|
||||
defer func() {
|
||||
if sent > 0 {
|
||||
s.used.Store(cs.clock())
|
||||
}
|
||||
}()
|
||||
probe := false
|
||||
for sent < len(bufs) {
|
||||
n := 1
|
||||
if !noSendX.Load() && !probe {
|
||||
n = len(bufs) - sent
|
||||
}
|
||||
k, err := s.try(bufs[sent:sent+n], false)
|
||||
if errno, ok := err.(syscall.Errno); ok && n > 1 && msgx.Unsupported(errno) {
|
||||
// EPERM may be the datagram's own fault, so sendmsg_x is off for good only once a plain write of the same
|
||||
// datagram goes through.
|
||||
probe = true
|
||||
continue
|
||||
}
|
||||
if probe && err == nil && noSendX.CompareAndSwap(false, true) {
|
||||
u.l.Warn("sendmsg_x unavailable, writing one datagram per syscall")
|
||||
}
|
||||
probe = false
|
||||
if err == syscall.EAGAIN || err == os.ErrDeadlineExceeded {
|
||||
// os.ErrDeadlineExceeded here is another writer's parked deadline, passed but not yet cleared.
|
||||
k, err = u.parkedWrite(s, bufs[sent:sent+1])
|
||||
}
|
||||
switch err {
|
||||
case nil:
|
||||
sent += k
|
||||
if s.stallAt.Load() != 0 {
|
||||
s.stallAt.Store(0)
|
||||
s.dropping.Store(false)
|
||||
}
|
||||
continue
|
||||
case errStallSpent:
|
||||
u.dropStalled(s)
|
||||
sent++
|
||||
continue
|
||||
}
|
||||
errno, ok := err.(syscall.Errno)
|
||||
if !ok {
|
||||
return sent, 0, true
|
||||
}
|
||||
if errno != syscall.ENOBUFS {
|
||||
return sent, errno, false
|
||||
}
|
||||
// The wait holds no reference on the descriptor, so a close doesn't wait for it.
|
||||
if s.stalledFor(cs.clock()) < cs.cfg.enobufsMax {
|
||||
cs.sleep(enobufsWait)
|
||||
} else {
|
||||
u.dropStalled(s)
|
||||
sent++
|
||||
}
|
||||
}
|
||||
return sent, 0, false
|
||||
}
|
||||
|
||||
// parkedWrite writes b after EAGAIN, parked in the netpoller for what is left of s's stall budget, and returns
|
||||
// errStallSpent once none is. golang/go#73919: a darwin UDP write parked on EAGAIN can wait forever for a
|
||||
// writability event that never comes, so the wait always has a deadline.
|
||||
func (u *StdConn) parkedWrite(s *connSock, b [][]byte) (int, error) {
|
||||
cs := u.socks
|
||||
s.parkMu.Lock()
|
||||
defer s.parkMu.Unlock()
|
||||
left := cs.cfg.enobufsMax - s.stalledFor(cs.clock())
|
||||
if left <= 0 {
|
||||
return 0, errStallSpent
|
||||
}
|
||||
if err := s.conn.SetWriteDeadline(time.Now().Add(left)); err != nil {
|
||||
return 0, net.ErrClosed
|
||||
}
|
||||
n, err := s.try(b, true)
|
||||
_ = s.conn.SetWriteDeadline(time.Time{})
|
||||
if err == os.ErrDeadlineExceeded {
|
||||
return 0, errStallSpent
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
// dropStalled logs the first datagram a stall drops.
|
||||
func (u *StdConn) dropStalled(s *connSock) {
|
||||
if !s.dropping.Swap(true) {
|
||||
u.l.Debug("connected socket: sends stalled, dropping datagrams until one goes through", "udpAddr", s.dst)
|
||||
}
|
||||
}
|
||||
|
||||
// stalledFor records an ENOBUFS or EAGAIN on s at now and returns how long its sends have stalled: since the first
|
||||
// ENOBUFS or EAGAIN after a send last went through. Every writer measures from that one time, so concurrent writers
|
||||
// spend the budget no faster than one, and a send going through ends the stall for every writer, including one
|
||||
// asleep in it.
|
||||
func (s *connSock) stalledFor(now int64) time.Duration {
|
||||
if s.stallAt.CompareAndSwap(0, now) {
|
||||
return 0
|
||||
}
|
||||
at := s.stallAt.Load()
|
||||
if at == 0 {
|
||||
return 0
|
||||
}
|
||||
return time.Duration(now - at)
|
||||
}
|
||||
|
||||
// readConnSock hands a connected socket's batches to ListenOut until the connected socket is closed. Any receive
|
||||
// error closes it: a connected socket reports its peer's unreachables here, and retrying an unexpected error could
|
||||
// spin.
|
||||
func (u *StdConn) readConnSock(s *connSock) {
|
||||
r := u.newBatchReader(s.conn, s.rc)
|
||||
for {
|
||||
b, err := u.read(r)
|
||||
if err != nil {
|
||||
if errors.Is(err, net.ErrClosed) {
|
||||
return
|
||||
}
|
||||
why := err.Error()
|
||||
var errno syscall.Errno
|
||||
if errors.As(err, &errno) && connSockDead(errno) {
|
||||
why = errno.Error()
|
||||
} else {
|
||||
u.l.Warn("connected socket: unexpected receive error", "udpAddr", s.dst, "error", err)
|
||||
}
|
||||
u.closeConnSock(s, why, true)
|
||||
return
|
||||
}
|
||||
s.used.Store(u.socks.clock())
|
||||
if !u.deliver(b) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
Loaded 100 of 119 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user