]> git.ipfire.org Git - thirdparty/wireguard-go.git/log
thirdparty/wireguard-go.git
3 years agoconn: remove the final alloc per packet receive
Josh Bleecher Snyder [Tue, 22 Mar 2022 18:23:56 +0000 (11:23 -0700)] 
conn: remove the final alloc per packet receive

This does bind_std only; other platforms remain.

The remaining alloc per iteration in the Throughput benchmark
comes from the tuntest package, and should not appear in regular use.

name           old time/op      new time/op      delta
Latency-10         25.2µs ± 1%      25.0µs ± 0%   -0.58%  (p=0.006 n=10+10)
Throughput-10      2.44µs ± 3%      2.41µs ± 2%     ~     (p=0.140 n=10+8)

name           old alloc/op     new alloc/op     delta
Latency-10           854B ± 5%        741B ± 3%  -13.22%  (p=0.000 n=10+10)
Throughput-10        265B ±34%        267B ±39%     ~     (p=0.670 n=10+10)

name           old allocs/op    new allocs/op    delta
Latency-10           16.0 ± 0%        14.0 ± 0%  -12.50%  (p=0.000 n=10+10)
Throughput-10        2.00 ± 0%        1.00 ± 0%  -50.00%  (p=0.000 n=10+10)

name           old packet-loss  new packet-loss  delta
Throughput-10        0.01 ±82%       0.01 ±282%     ~     (p=0.321 n=9+8)

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agoconn: use netip for std bind
Jason A. Donenfeld [Fri, 18 Mar 2022 04:23:02 +0000 (22:23 -0600)] 
conn: use netip for std bind

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agoversion: bump snapshot 0.0.20220316
Jason A. Donenfeld [Thu, 17 Mar 2022 03:32:14 +0000 (21:32 -0600)] 
version: bump snapshot

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agotun/netstack: bump mod
Jason A. Donenfeld [Wed, 16 Mar 2022 23:58:35 +0000 (17:58 -0600)] 
tun/netstack: bump mod

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agomod: bump packages and remove compat netip
Jason A. Donenfeld [Wed, 16 Mar 2022 23:51:47 +0000 (17:51 -0600)] 
mod: bump packages and remove compat netip

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agoall: use any in place of interface{}
Josh Bleecher Snyder [Wed, 16 Mar 2022 23:40:24 +0000 (16:40 -0700)] 
all: use any in place of interface{}

Enabled by using Go 1.18. A bit less verbose.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
3 years agoall: update to Go 1.18
Josh Bleecher Snyder [Wed, 16 Mar 2022 23:09:48 +0000 (16:09 -0700)] 
all: update to Go 1.18

Bump go.mod and README.

Switch to upstream net/netip.

Use strings.Cut.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
3 years agotun/netstack: check error returned by SetDeadline()
Alexander Neumann [Fri, 4 Mar 2022 09:38:10 +0000 (10:38 +0100)] 
tun/netstack: check error returned by SetDeadline()

Signed-off-by: Alexander Neumann <alexander.neumann@redteam-pentesting.de>
[Jason: don't wrap deadline error.]
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agotun/netstack: update to latest wireguard-go
Alexander Neumann [Fri, 4 Mar 2022 09:36:15 +0000 (10:36 +0100)] 
tun/netstack: update to latest wireguard-go

This commit fixes all callsites of netip.AddrFromSlice(), which has
changed its signature and now returns two values.

Signed-off-by: Alexander Neumann <alexander.neumann@redteam-pentesting.de>
[Jason: remove error handling from AddrFromSlice.]
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agotun/netstack: simplify read timeout on ping socket
Jason A. Donenfeld [Wed, 2 Feb 2022 22:30:31 +0000 (23:30 +0100)] 
tun/netstack: simplify read timeout on ping socket

I'm not 100% sure this is correct, but it certainly is a lot simpler.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agotun/netstack: implement ICMP ping
Thomas H. Ptacek [Mon, 31 Jan 2022 22:55:36 +0000 (16:55 -0600)] 
tun/netstack: implement ICMP ping

Provide a PacketConn interface for netstack's ICMP endpoint; netstack
currently only provides EchoRequest/EchoResponse ICMP support, so this
code exposes only an interface for doing ping.

Signed-off-by: Thomas Ptacek <thomas@sockpuppet.org>
[Jason: rework structure, match std go interfaces, add example code]
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agoversion: bump snapshot 0.0.20220117
Jason A. Donenfeld [Mon, 17 Jan 2022 16:37:42 +0000 (17:37 +0100)] 
version: bump snapshot

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agoipc: bsd: try again if kqueue returns EINTR
Jason A. Donenfeld [Fri, 14 Jan 2022 15:10:43 +0000 (16:10 +0100)] 
ipc: bsd: try again if kqueue returns EINTR

Reported-by: J. Michael McAtee <mmcatee@jumptrading.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agoglobal: apply gofumpt
Jason A. Donenfeld [Thu, 9 Dec 2021 16:55:50 +0000 (17:55 +0100)] 
global: apply gofumpt

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agodevice: handle peer post config on blank line
Jason A. Donenfeld [Mon, 29 Nov 2021 17:31:54 +0000 (12:31 -0500)] 
device: handle peer post config on blank line

We missed a function exit point. This was exacerbated by e3134bf
("device: defer state machine transitions until configuration is
complete"), but the bug existed prior. Minus provided the following
useful reproducer script:

    #!/usr/bin/env bash

    set -eux

    make wireguard-go || exit 125

    ip netns del test-ns || true
    ip netns add test-ns
    ip link add test-kernel type wireguard
    wg set test-kernel listen-port 0 private-key <(echo "QMCfZcp1KU27kEkpcMCgASEjDnDZDYsfMLHPed7+538=") peer "eDPZJMdfnb8ZcA/VSUnLZvLB2k8HVH12ufCGa7Z7rHI=" allowed-ips 10.51.234.10/32
    ip link set test-kernel netns test-ns up
    ip -n test-ns addr add 10.51.234.1/24 dev test-kernel
    port=$(ip netns exec test-ns wg show test-kernel listen-port)

    ip link del test-go || true
    ./wireguard-go test-go
    wg set test-go private-key <(echo "WBM7qimR3vFk1QtWNfH+F4ggy/hmO+5hfIHKxxI4nF4=") peer "+nj9Dkqpl4phsHo2dQliGm5aEiWJJgBtYKbh7XjeNjg=" allowed-ips 0.0.0.0/0 endpoint 127.0.0.1:$port
    ip addr add 10.51.234.10/24 dev test-go
    ip link set test-go up

    ping -c2 -W1 10.51.234.1

Reported-by: minus <minus@mnus.de>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agodevice: reduce peer lock critical section in UAPI
Josh Bleecher Snyder [Thu, 18 Nov 2021 23:37:24 +0000 (15:37 -0800)] 
device: reduce peer lock critical section in UAPI

The deferred RUnlock calls weren't executing until all peers
had been processed. Add an anonymous function so that each
peer may be unlocked as soon as it is completed.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agodevice: remove code using unsafe
Josh Bleecher Snyder [Mon, 8 Nov 2021 19:15:06 +0000 (11:15 -0800)] 
device: remove code using unsafe

There is no performance impact.

name                             old time/op  new time/op  delta
TrieIPv4Peers100Addresses1000-8  78.6ns ± 1%  79.4ns ± 3%    ~     (p=0.604 n=10+9)
TrieIPv4Peers10Addresses10-8     29.1ns ± 2%  28.8ns ± 1%  -1.12%  (p=0.014 n=10+9)
TrieIPv6Peers100Addresses1000-8  78.9ns ± 1%  78.6ns ± 1%    ~     (p=0.492 n=10+10)
TrieIPv6Peers10Addresses10-8     29.3ns ± 2%  28.6ns ± 2%  -2.16%  (p=0.000 n=10+10)

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agoglobal: use netip where possible now
Jason A. Donenfeld [Fri, 5 Nov 2021 00:52:54 +0000 (01:52 +0100)] 
global: use netip where possible now

There are more places where we'll need to add it later, when Go 1.18
comes out with support for it in the "net" package. Also, allowedips
still uses slices internally, which might be suboptimal.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agodevice: only propagate roaming value before peer is referenced elsewhere
Jason A. Donenfeld [Tue, 16 Nov 2021 20:13:55 +0000 (21:13 +0100)] 
device: only propagate roaming value before peer is referenced elsewhere

A peer.endpoint never becomes nil after being not-nil, so creation is
the only time we actually need to set this. This prevents a race from
when the variable is actually used elsewhere, and allows us to avoid an
expensive atomic.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agodevice: align 64-bit atomic member in Device
Jason A. Donenfeld [Tue, 16 Nov 2021 20:07:15 +0000 (21:07 +0100)] 
device: align 64-bit atomic member in Device

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agodevice: start peers before running handshake test
Jason A. Donenfeld [Tue, 16 Nov 2021 20:04:54 +0000 (21:04 +0100)] 
device: start peers before running handshake test

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agoMakefile: don't use test -v because it hides failures in scrollback
Jason A. Donenfeld [Tue, 16 Nov 2021 19:59:40 +0000 (20:59 +0100)] 
Makefile: don't use test -v because it hides failures in scrollback

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agodevice: fix nil pointer dereference in uapi read
David Anderson [Tue, 16 Nov 2021 19:27:44 +0000 (11:27 -0800)] 
device: fix nil pointer dereference in uapi read

Signed-off-by: David Anderson <danderson@tailscale.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agodevice: make new peers inherit broken mobile semantics
Jason A. Donenfeld [Thu, 11 Nov 2021 02:13:20 +0000 (03:13 +0100)] 
device: make new peers inherit broken mobile semantics

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agodevice: defer state machine transitions until configuration is complete
Jason A. Donenfeld [Thu, 11 Nov 2021 02:12:37 +0000 (03:12 +0100)] 
device: defer state machine transitions until configuration is complete

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agodevice: do not consume handshake messages if not running
Jason A. Donenfeld [Thu, 11 Nov 2021 02:11:29 +0000 (03:11 +0100)] 
device: do not consume handshake messages if not running

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agotun: move wintun to its own repo
Jason A. Donenfeld [Thu, 4 Nov 2021 11:53:44 +0000 (12:53 +0100)] 
tun: move wintun to its own repo

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agonamedpipe: rename from winpipe to keep in sync with CL299009
Jason A. Donenfeld [Sat, 30 Oct 2021 00:39:56 +0000 (02:39 +0200)] 
namedpipe: rename from winpipe to keep in sync with CL299009

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agodevice: timers: use pre-seeded per-thread unlocked fastrandn for jitter
Jason A. Donenfeld [Thu, 28 Oct 2021 11:47:50 +0000 (13:47 +0200)] 
device: timers: use pre-seeded per-thread unlocked fastrandn for jitter

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agodevice: timers: seed unsafe rng before use for jitter
Jason A. Donenfeld [Thu, 28 Oct 2021 11:34:21 +0000 (13:34 +0200)] 
device: timers: seed unsafe rng before use for jitter

Forgetting to seed the unsafe rng, the jitter before followed a fixed
pattern, which didn't help when a fleet of computers all boot at once.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agowintun: align 64-bit argument on ARM32
Jason A. Donenfeld [Tue, 26 Oct 2021 12:51:44 +0000 (14:51 +0200)] 
wintun: align 64-bit argument on ARM32

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agoREADME: raise minimum Go to 1.17
Jason A. Donenfeld [Mon, 25 Oct 2021 15:53:11 +0000 (17:53 +0200)] 
README: raise minimum Go to 1.17

Suggested-by: Adam Bliss <abliss@gmail.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agotun/netstack: update gvisor
Mikael Magnusson [Thu, 21 Oct 2021 22:04:20 +0000 (00:04 +0200)] 
tun/netstack: update gvisor

Update gvisor to v0.0.0-20211020211948-f76a604701b6, which requires some
changes to tun.go:

WriteRawPacket: Add function with not implemented error.

CreateNetTUN: Replace stack.AddAddress with stack.AddProtocolAddress, and
fix IPv6 address in error message.

Signed-off-by: Mikael Magnusson <mikma@users.sourceforge.net>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agoipc, rwcancel: compile on js/wasm
Brad Fitzpatrick [Wed, 20 Oct 2021 15:56:39 +0000 (08:56 -0700)] 
ipc, rwcancel: compile on js/wasm

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
3 years agowintun: allow retrieving DLL version
Jason A. Donenfeld [Wed, 20 Oct 2021 18:13:44 +0000 (12:13 -0600)] 
wintun: allow retrieving DLL version

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agoversion: bump snapshot 0.0.20211016
Jason A. Donenfeld [Sun, 17 Oct 2021 05:27:13 +0000 (23:27 -0600)] 
version: bump snapshot

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agowintun: remove memmod option for dll loading
Jason A. Donenfeld [Sun, 17 Oct 2021 04:49:38 +0000 (22:49 -0600)] 
wintun: remove memmod option for dll loading

Only wireguard-windows used this, and it's moving to wgnt exclusively.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agoglobal: remove old-style build tags
Jason A. Donenfeld [Tue, 12 Oct 2021 18:02:10 +0000 (12:02 -0600)] 
global: remove old-style build tags

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agoglobal: add newer-style build tags
Jason A. Donenfeld [Tue, 12 Oct 2021 17:46:53 +0000 (11:46 -0600)] 
global: add newer-style build tags

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agowintun: use new swdevice-based API for upcoming Wintun 0.14
Jason A. Donenfeld [Tue, 12 Oct 2021 06:26:46 +0000 (00:26 -0600)] 
wintun: use new swdevice-based API for upcoming Wintun 0.14

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agoconn,wintun: use unsafe.Slice instead of unsafeSlice
Jason A. Donenfeld [Mon, 11 Oct 2021 20:57:53 +0000 (14:57 -0600)] 
conn,wintun: use unsafe.Slice instead of unsafeSlice

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agomemmod: import from wireguard-windows
Jason A. Donenfeld [Mon, 11 Oct 2021 20:53:36 +0000 (14:53 -0600)] 
memmod: import from wireguard-windows

We'll eventually be getting rid of it here, but keep it sync'd up for
now.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agorwcancel: use unix.Poll again but bump x/sys so it uses ppoll under the hood
Jason A. Donenfeld [Mon, 27 Sep 2021 20:18:38 +0000 (14:18 -0600)] 
rwcancel: use unix.Poll again but bump x/sys so it uses ppoll under the hood

This reverts commit fcc601dbf0f6b626ec1d47a880cbe64f9c8fe385 but then
bumps go.mod.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agorwcancel: use ppoll on Linux for Android
Jason A. Donenfeld [Sun, 26 Sep 2021 23:15:58 +0000 (17:15 -0600)] 
rwcancel: use ppoll on Linux for Android

This is a temporary measure while we wait for
https://go-review.googlesource.com/c/sys/+/352310 to land.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agotun: make operateonfd.go build tags more specific
Tobias Klauser [Thu, 23 Sep 2021 10:07:19 +0000 (12:07 +0200)] 
tun: make operateonfd.go build tags more specific

(*NativeTun).operateOnFd is only used on darwin and freebsd. Adjust the
build tags accordingly.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agotun: avoid leaking sock fd in CreateTUN error cases
Tobias Klauser [Thu, 23 Sep 2021 10:05:13 +0000 (12:05 +0200)] 
tun: avoid leaking sock fd in CreateTUN error cases

At these points, the socket file descriptor is not yet wrapped in an
*os.File, so it needs to be closed explicitly on error.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agoglobal: add new go 1.17 build comments
Jason A. Donenfeld [Sun, 5 Sep 2021 14:00:43 +0000 (16:00 +0200)] 
global: add new go 1.17 build comments

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agomemmod: register exception handler tables
Jason A. Donenfeld [Thu, 5 Aug 2021 12:56:48 +0000 (14:56 +0200)] 
memmod: register exception handler tables

Otherwise recent WDK binaries fail on ARM64, where an exception handler
is used for trapping an illegal instruction when ARMv8.1 atomics are
being tested for functionality.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agomemmod: fix protected delayed load the right way
Jason A. Donenfeld [Wed, 28 Jul 2021 23:27:40 +0000 (01:27 +0200)] 
memmod: fix protected delayed load the right way

The reason this was failing before is that dloadsup.h's
DloadObtainSection was doing a linear search of sections to find which
header corresponds with the IMAGE_DELAYLOAD_DESCRIPTOR section, and we
were stupidly overwriting the VirtualSize field, so the linear search
wound up matching the .text section, which then it found to not be
marked writable and failed with FAST_FAIL_DLOAD_PROTECTION_FAILURE.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
3 years agomemmod: disable protected delayed load for now
Jason A. Donenfeld [Wed, 28 Jul 2021 23:01:46 +0000 (01:01 +0200)] 
memmod: disable protected delayed load for now

Probably a bad idea, but we don't currently support it, and those huge
windows.NewCallback trampolines make juicer targets anyway.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agoipc: allow admins but require high integrity label
Jason A. Donenfeld [Wed, 23 Jun 2021 23:19:29 +0000 (01:19 +0200)] 
ipc: allow admins but require high integrity label

Might be more reasonable.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: zero out allowedip node pointers when removing
Jason A. Donenfeld [Fri, 4 Jun 2021 14:33:28 +0000 (16:33 +0200)] 
device: zero out allowedip node pointers when removing

This should make it a bit easier for the garbage collector.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: limit allowedip fuzzer a to 4 times through
Jason A. Donenfeld [Thu, 3 Jun 2021 16:22:50 +0000 (18:22 +0200)] 
device: limit allowedip fuzzer a to 4 times through

Trying this for every peer winds up being very slow and precludes it
from acceptable runtime in the CI, so reduce this to 4.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: simplify allowedips lookup signature
Jason A. Donenfeld [Thu, 3 Jun 2021 14:12:29 +0000 (16:12 +0200)] 
device: simplify allowedips lookup signature

The inliner should handle this for us.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: remove nodes by peer in O(1) instead of O(n)
Jason A. Donenfeld [Thu, 3 Jun 2021 13:40:09 +0000 (15:40 +0200)] 
device: remove nodes by peer in O(1) instead of O(n)

Now that we have parent pointers hooked up, we can simply go right to
the node and remove it in place, rather than having to recursively walk
the entire trie.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: remove recursion from insertion and connect parent pointers
Jason A. Donenfeld [Thu, 3 Jun 2021 12:50:28 +0000 (14:50 +0200)] 
device: remove recursion from insertion and connect parent pointers

This makes the insertion algorithm a bit more efficient, while also now
taking on the additional task of connecting up parent pointers. This
will be handy in the following commit.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: reduce size of trie struct
Jason A. Donenfeld [Thu, 3 Jun 2021 11:51:03 +0000 (13:51 +0200)] 
device: reduce size of trie struct

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agogo.mod: bump golang.org/x/sys again
Josh Bleecher Snyder [Sat, 3 Apr 2021 17:15:59 +0000 (10:15 -0700)] 
go.mod: bump golang.org/x/sys again

To pick up https://go-review.googlesource.com/c/sys/+/307129.

Signed-off-by: Josh Bleecher Snyder <josharian@gmail.com>
4 years agodevice: allow reducing queue constants on iOS
Jason A. Donenfeld [Fri, 21 May 2021 22:57:42 +0000 (00:57 +0200)] 
device: allow reducing queue constants on iOS

Heavier network extensions might require the wireguard-go component to
use less ram, so let users of this reduce these as needed.

At some point we'll put this behind a configuration method of sorts, but
for now, just expose the consts as vars.

Requested-by: Josh Bleecher Snyder <josh@tailscale.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun: linux: account for interface removal from outside
Jason A. Donenfeld [Thu, 20 May 2021 16:26:01 +0000 (18:26 +0200)] 
tun: linux: account for interface removal from outside

On Linux we can run `ip link del wg0`, in which case the fd becomes
stale, and we should exit. Since this is an intentional action, don't
treat it as an error.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agoconn: linux: protect read fds
Jason A. Donenfeld [Thu, 20 May 2021 16:09:55 +0000 (18:09 +0200)] 
conn: linux: protect read fds

The -1 protection was removed and the wrong error was returned, causing
us to read from a bogus fd. As well, remove the useless closures that
aren't doing anything, since this is all synchronized anyway.

Fixes: 10533c3 ("all: make conn.Bind.Open return a slice of receive functions")
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agorwcancel: use ordinary os.ErrClosed instead of custom error
Jason A. Donenfeld [Thu, 20 May 2021 15:56:36 +0000 (17:56 +0200)] 
rwcancel: use ordinary os.ErrClosed instead of custom error

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agorwcancel: use poll instead of select
Jason A. Donenfeld [Thu, 20 May 2021 15:42:34 +0000 (17:42 +0200)] 
rwcancel: use poll instead of select

Suggested-by: Lennart Poettering <lennart@poettering.net>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: optimize Peer.String even more
Jason A. Donenfeld [Thu, 13 May 2021 23:07:55 +0000 (01:07 +0200)] 
device: optimize Peer.String even more

This reduces the allocation, branches, and amount of base64 encoding.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: optimize Peer.String
Josh Bleecher Snyder [Thu, 13 May 2021 22:30:18 +0000 (15:30 -0700)] 
device: optimize Peer.String

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agoconn: windows: set count=0 on retry
Jason A. Donenfeld [Tue, 11 May 2021 14:47:17 +0000 (16:47 +0200)] 
conn: windows: set count=0 on retry

When retrying, if count is not 0, we forget to dequeue another request,
and so the ring fills up and errors out.

Reported-by: Sascha Dierberg <dierberg@dresearch-fe.de>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agomain: replace crlf on windows in fmt test
Jason A. Donenfeld [Mon, 10 May 2021 20:23:32 +0000 (22:23 +0200)] 
main: replace crlf on windows in fmt test

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agomain: check that code is formatted in unit test
Jason A. Donenfeld [Fri, 7 May 2021 10:56:10 +0000 (12:56 +0200)] 
main: check that code is formatted in unit test

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun: format
Jason A. Donenfeld [Fri, 7 May 2021 10:21:27 +0000 (12:21 +0200)] 
tun: format

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: add ID to repeated routines
Jason A. Donenfeld [Fri, 7 May 2021 10:21:21 +0000 (12:21 +0200)] 
device: add ID to repeated routines

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: remove unusual ... in messages
Jason A. Donenfeld [Fri, 7 May 2021 10:17:41 +0000 (12:17 +0200)] 
device: remove unusual ... in messages

We dont use ... in any other present progressive messages except these.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: avoid verbose log line during ordinary shutdown sequence
Jason A. Donenfeld [Fri, 7 May 2021 07:39:06 +0000 (09:39 +0200)] 
device: avoid verbose log line during ordinary shutdown sequence

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun: windows: set event before waiting
Jason A. Donenfeld [Fri, 7 May 2021 07:26:24 +0000 (09:26 +0200)] 
tun: windows: set event before waiting

In 097af6e ("tun: windows: protect reads from closing") we made sure no
functions are running when End() is called, to avoid a UaF. But we still
need to kick that event somehow, so that Read() is allowed to exit, in
order to release the lock. So this commit calls SetEvent, while moving
the closing boolean to be atomic so it can be modified without locks,
and then moves to a WaitGroup for the RCU-like pattern.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun: windows: rearrange struct to avoid alignment trap on 32bit
Jason A. Donenfeld [Fri, 7 May 2021 07:17:35 +0000 (09:17 +0200)] 
tun: windows: rearrange struct to avoid alignment trap on 32bit

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun: windows: check alignment in unit test
Jason A. Donenfeld [Fri, 7 May 2021 07:10:41 +0000 (09:10 +0200)] 
tun: windows: check alignment in unit test

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: log all errors received by RoutineReceiveIncoming
Josh Bleecher Snyder [Tue, 27 Apr 2021 17:15:15 +0000 (10:15 -0700)] 
device: log all errors received by RoutineReceiveIncoming

When debugging, it's useful to know why a receive func exited.

We were already logging that, but only in the "death spiral" case.
Move the logging up, to capture it always.
Reduce the verbosity, since it is not an error case any more.
Put the receive func name in the log line.

Signed-off-by: Josh Bleecher Snyder <josharian@gmail.com>
4 years agotun/netstack: update go mod and remove GSO argument
Jason A. Donenfeld [Thu, 6 May 2021 09:03:04 +0000 (11:03 +0200)] 
tun/netstack: update go mod and remove GSO argument

Reported-by: John Xiong <xiaoyang1258@yeah.net>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun: windows: send MTU update when forced MTU changes
Jason A. Donenfeld [Wed, 5 May 2021 09:42:45 +0000 (11:42 +0200)] 
tun: windows: send MTU update when forced MTU changes

Otherwise the padding doesn't get updated.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun: windows: protect reads from closing
Jason A. Donenfeld [Tue, 27 Apr 2021 02:22:45 +0000 (22:22 -0400)] 
tun: windows: protect reads from closing

The code previously used the old errors channel for checking, rather
than the simpler boolean, which caused issues on shutdown, since the
errors channel was meaningless. However, looking at this exposed a more
basic problem: Close() and all the other functions that check the closed
boolean can race. So protect with a basic RW lock, to ensure that
Close() waits for all pending operations to complete.

Reported-by: Joshua Sjoding <joshua.sjoding@scjalliance.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agoconn: windows: do not error out when receiving UDP jumbogram
Jason A. Donenfeld [Tue, 27 Apr 2021 02:07:03 +0000 (22:07 -0400)] 
conn: windows: do not error out when receiving UDP jumbogram

If we receive a large UDP packet, don't return an error to receive.go,
which then terminates the receive loop. Instead, simply retry.

Considering Winsock's general finickiness, we might consider other
places where an attacker on the wire can generate error conditions like
this.

Reported-by: Sascha Dierberg <sascha.dierberg@gmail.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agoversion: bump snapshot 0.0.20210424
Jason A. Donenfeld [Sat, 24 Apr 2021 17:07:27 +0000 (13:07 -0400)] 
version: bump snapshot

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun: freebsd: avoid OOB writes
Jason A. Donenfeld [Mon, 19 Apr 2021 21:10:23 +0000 (15:10 -0600)] 
tun: freebsd: avoid OOB writes

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun: freebsd: become controlling process when reopening tun FD
Jason A. Donenfeld [Mon, 19 Apr 2021 21:01:36 +0000 (15:01 -0600)] 
tun: freebsd: become controlling process when reopening tun FD

When we pass the TUN FD to the child, we have to call TUNSIFPID;
otherwise when we close the device, we get a splat in dmesg.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun: freebsd: restructure and cleanup
Jason A. Donenfeld [Mon, 19 Apr 2021 20:54:59 +0000 (14:54 -0600)] 
tun: freebsd: restructure and cleanup

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun: freebsd: remove horrific hack for getting tunnel name
Jason A. Donenfeld [Mon, 19 Apr 2021 02:26:32 +0000 (20:26 -0600)] 
tun: freebsd: remove horrific hack for getting tunnel name

As of FreeBSD 12.1, there's TUNGIFNAME.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun: freebsd: set IFF_MULTICAST for routing daemons
Jason A. Donenfeld [Mon, 19 Apr 2021 02:09:04 +0000 (20:09 -0600)] 
tun: freebsd: set IFF_MULTICAST for routing daemons

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agomain: print kernel warning on OpenBSD and FreeBSD too
Jason A. Donenfeld [Fri, 16 Apr 2021 05:32:44 +0000 (23:32 -0600)] 
main: print kernel warning on OpenBSD and FreeBSD too

More kernels!

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: don't defer unlocking from loop
Jason A. Donenfeld [Mon, 12 Apr 2021 22:19:35 +0000 (16:19 -0600)] 
device: don't defer unlocking from loop

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agoconn: reconstruct v4 vs v6 receive function based on symtab
Jason A. Donenfeld [Fri, 9 Apr 2021 23:21:35 +0000 (17:21 -0600)] 
conn: reconstruct v4 vs v6 receive function based on symtab

This is kind of gross but it's better than the alternatives.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: allocate new buffer in receive death spiral
Kristupas Antanavičius [Mon, 12 Apr 2021 11:50:58 +0000 (14:50 +0300)] 
device: allocate new buffer in receive death spiral

Note: this bug is "hidden" by avoiding "death spiral" code path by
6228659 ("device: handle broader range of errors in RoutineReceiveIncoming").

If the code reached "death spiral" mechanism, there would be multiple
double frees happening. This results in a deadlock on iOS, because the
pools are fixed size and goroutine might stop until somebody makes
space in the pool.

This was almost 100% repro on the new ARM Macbooks:

- Build with 'ios' tag for Mac. This will enable bounded pools.
- Somehow call device.IpcSet at least couple of times (update config)
- device.BindUpdate() would be triggered
- RoutineReceiveIncoming would enter "death spiral".
- RoutineReceiveIncoming would stall on double free (pool is already
  full)
- The stuck routine would deadlock 'device.closeBindLocked()' function
  on line 'netc.stopping.Wait()'

Signed-off-by: Kristupas Antanavičius <kristupas.antanavicius@nordsec.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agoconn: windows: reset ring to starting position after free
Jason A. Donenfeld [Sat, 10 Apr 2021 00:08:48 +0000 (18:08 -0600)] 
conn: windows: reset ring to starting position after free

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agoconn: windows: compare head and tail properly
Jason A. Donenfeld [Fri, 9 Apr 2021 00:17:59 +0000 (18:17 -0600)] 
conn: windows: compare head and tail properly

By not comparing these with the modulo, the ring became nearly never
full, resulting in completion queue buffers filling up prematurely.

Reported-by: Joshua Sjoding <joshua.sjoding@scjalliance.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agowinrio: test that IOCP-based RIO is supported
Jason A. Donenfeld [Tue, 6 Apr 2021 17:45:10 +0000 (11:45 -0600)] 
winrio: test that IOCP-based RIO is supported

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agoall: make conn.Bind.Open return a slice of receive functions
Josh Bleecher Snyder [Wed, 31 Mar 2021 20:55:18 +0000 (13:55 -0700)] 
all: make conn.Bind.Open return a slice of receive functions

Instead of hard-coding exactly two sources from which
to receive packets (an IPv4 source and an IPv6 source),
allow the conn.Bind to specify a set of sources.

Beneficial consequences:

* If there's no IPv6 support on a system,
  conn.Bind.Open can choose not to return a receive function for it,
  which is simpler than tracking that state in the bind.
  This simplification removes existing data races from both
  conn.StdNetBind and bindtest.ChannelBind.
* If there are more than two sources on a system,
  the conn.Bind no longer needs to add a separate muxing layer.

Signed-off-by: Josh Bleecher Snyder <josharian@gmail.com>
4 years agoconn: winrio: pass key parameter into struct
Jason A. Donenfeld [Fri, 2 Apr 2021 16:36:41 +0000 (10:36 -0600)] 
conn: winrio: pass key parameter into struct

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: handle broader range of errors in RoutineReceiveIncoming
Josh Bleecher Snyder [Tue, 30 Mar 2021 19:36:59 +0000 (12:36 -0700)] 
device: handle broader range of errors in RoutineReceiveIncoming

RoutineReceiveIncoming exits immediately on net.ErrClosed,
but not on other errors. However, for errors that are known
to be permanent, such as syscall.EAFNOSUPPORT,
we may as well exit immediately instead of retrying.

This considerably speeds up the package device tests right now,
because the Bind sometimes (incorrectly) returns syscall.EAFNOSUPPORT
instead of net.ErrClosed.

Signed-off-by: Josh Bleecher Snyder <josharian@gmail.com>
4 years agoconn: document retry loop in StdNetBind.Open
Josh Bleecher Snyder [Mon, 29 Mar 2021 20:27:21 +0000 (13:27 -0700)] 
conn: document retry loop in StdNetBind.Open

It's not obvious on a first read what the loop is doing.

Signed-off-by: Josh Bleecher Snyder <josharian@gmail.com>
4 years agoconn: use local ipvN vars in StdNetBind.Open
Josh Bleecher Snyder [Mon, 29 Mar 2021 20:21:06 +0000 (13:21 -0700)] 
conn: use local ipvN vars in StdNetBind.Open

This makes it clearer that they are fresh on each attempt,
and avoids the bookkeeping required to clearing them on failure.

Also, remove an unnecessary err != nil.

Signed-off-by: Josh Bleecher Snyder <josharian@gmail.com>
4 years agoconn: unify code in StdNetBind.Send
Josh Bleecher Snyder [Mon, 29 Mar 2021 20:11:11 +0000 (13:11 -0700)] 
conn: unify code in StdNetBind.Send

The sending code is identical for ipv4 and ipv6;
select the conn, then use it.

Signed-off-by: Josh Bleecher Snyder <josharian@gmail.com>
4 years agodevice: rename unsafeCloseBind to closeBindLocked
Josh Bleecher Snyder [Mon, 29 Mar 2021 19:36:09 +0000 (12:36 -0700)] 
device: rename unsafeCloseBind to closeBindLocked

And document a bit.
This name is more idiomatic.

Signed-off-by: Josh Bleecher Snyder <josharian@gmail.com>