]> git.ipfire.org Git - thirdparty/wireguard-go.git/log
thirdparty/wireguard-go.git
4 years agodevice: add Device.ListenPort and Device.SetListenPort js/sample-api
Josh Bleecher Snyder [Tue, 22 Dec 2020 19:12:54 +0000 (11:12 -0800)] 
device: add Device.ListenPort and Device.SetListenPort

This is a sample commit for a possible way to make
a Go API that lives alongside UAPI.

The general idea is to add Device and Peer methods
corresponding to UAPI directives, including a way to
look up a peer from a device based on a public key,
as in UAPI.

The UAPI code then deals with parsing and generating textual
input/output, and calls the Go methods to do the work.

This commit also contains a bug fix for a racy access of device.net.port
I will send an independently commit that fixes those directly in UAPI.
This commit is NOT meant to be merged as-is.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: add UAPI helper methods
Jason A. Donenfeld [Tue, 22 Dec 2020 13:30:57 +0000 (14:30 +0100)] 
device: add UAPI helper methods

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun: make customization of WintunPool and WintunGUID more obvious
Jason A. Donenfeld [Tue, 22 Dec 2020 13:09:16 +0000 (14:09 +0100)] 
tun: make customization of WintunPool and WintunGUID more obvious

Persnickety consumers can now do:

    func init() {
        tun.WintunPool, _ = wintun.Pool("Flurp")
        tun.WintunStaticRequestedGUID, _ = windows.GUIDFromString("{5ae2716f-0b3e-4dc4-a8b5-48eba11a6e16}")
    }

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agoconn: do not SO_REUSEADDR on linux
Jason A. Donenfeld [Fri, 18 Dec 2020 15:41:49 +0000 (16:41 +0100)] 
conn: do not SO_REUSEADDR on linux

SO_REUSEADDR does not make sense for unicast UDP sockets.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: add missing colon to error line
Jason A. Donenfeld [Fri, 18 Dec 2020 10:52:13 +0000 (11:52 +0100)] 
device: add missing colon to error line

People are actually hitting this condition, so make it uniform. Also,
change a printf into a println, to match the other conventions.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: fix error shadowing before log print
Brad Fitzpatrick [Mon, 21 Sep 2020 22:17:16 +0000 (15:17 -0700)] 
device: fix error shadowing before log print

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
4 years agodevice: fix data race in peer.timersActive
Josh Bleecher Snyder [Wed, 16 Dec 2020 01:44:21 +0000 (17:44 -0800)] 
device: fix data race in peer.timersActive

Found by the race detector and existing tests.

To avoid introducing a lock into this hot path,
calculate and cache whether any peers exist.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: fix races from changing private_key
Josh Bleecher Snyder [Tue, 15 Dec 2020 23:02:13 +0000 (15:02 -0800)] 
device: fix races from changing private_key

Access keypair.sendNonce atomically.
Eliminate one unnecessary initialization to zero.

Mutate handshake.lastSentHandshake with the mutex held.

Co-authored-by: David Anderson <danderson@tailscale.com>
Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: always name *Queue*Element variables elem
Josh Bleecher Snyder [Wed, 16 Dec 2020 00:00:52 +0000 (16:00 -0800)] 
device: always name *Queue*Element variables elem

They're called elem in most places.
Rename a few local variables to make it consistent.
This makes it easier to grep the code for things like elem.Drop.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: use channel close to shut down and drain outbound channel
Josh Bleecher Snyder [Tue, 15 Dec 2020 23:54:48 +0000 (15:54 -0800)] 
device: use channel close to shut down and drain outbound channel

This is a similar treatment to the handling of the encryption
channel found a few commits ago: Use the closing of the channel
to manage goroutine lifetime and shutdown.
It is considerably simpler because there is only a single writer.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: fix persistent_keepalive_interval data races
Josh Bleecher Snyder [Mon, 14 Dec 2020 23:28:52 +0000 (15:28 -0800)] 
device: fix persistent_keepalive_interval data races

Co-authored-by: David Anderson <danderson@tailscale.com>
Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: use channel close to shut down and drain encryption channel
Josh Bleecher Snyder [Mon, 14 Dec 2020 23:07:23 +0000 (15:07 -0800)] 
device: use channel close to shut down and drain encryption channel

The new test introduced in this commit used to deadlock about 1% of the time.

I believe that the deadlock occurs as follows:

* The test completes, calling device.Close.
* device.Close closes device.signals.stop.
* RoutineEncryption stops.
* The deferred function in RoutineEncryption drains device.queue.encryption.
* RoutineEncryption exits.
* A peer's RoutineNonce processes an element queued in peer.queue.nonce.
* RoutineNonce puts that element into the outbound and encryption queues.
* RoutineSequentialSender reads that elements from the outbound queue.
* It waits for that element to get Unlocked by RoutineEncryption.
* RoutineEncryption has already exited, so RoutineSequentialSender blocks forever.
* device.RemoveAllPeers calls peer.Stop on all peers.
* peer.Stop waits for peer.routines.stopping, which blocks forever.

Rather than attempt to add even more ordering to the already complex
centralized shutdown orchestration, this commit moves towards a
data-flow-oriented shutdown.

The device.queue.encryption gets closed when there will be no more writes to it.
All device.queue.encryption readers always read until the channel is closed and then exit.
We thus guarantee that any element that enters the encryption queue also exits it.
This removes the need for central control of the lifetime of RoutineEncryption,
removes the need to drain the encryption queue on shutdown, and simplifies RoutineEncryption.

This commit also fixes a data race. When RoutineSequentialSender
drains its queue on shutdown, it needs to lock the elem before operating on it,
just as the main body does.

The new test in this commit passed 50k iterations with the race detector enabled
and 150k iterations with the race detector disabled, with no failures.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: simplify copying counter to nonce
Josh Bleecher Snyder [Tue, 15 Dec 2020 02:30:13 +0000 (18:30 -0800)] 
device: simplify copying counter to nonce

Since we already have it packed into a uint64
in a known byte order, write it back out again
the same byte order instead of copying byte by byte.

This should also generate more efficient code,
because the compiler can do a single uint64 write,
instead of eight bounds checks and eight byte writes.

Due to a missed optimization, it actually generates a mishmash
of smaller writes: 1 byte, 4 bytes, 2 bytes, 1 byte.
This is https://golang.org/issue/41663.
The code is still better than before, and will get better yet
once that compiler bug gets fixed.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: add a helper to generate uapi configs
Josh Bleecher Snyder [Mon, 14 Dec 2020 22:12:56 +0000 (14:12 -0800)] 
device: add a helper to generate uapi configs

This makes it easier to work with configs in tests.
It'll see heavier use over upcoming commits;
this commit only adds the infrastructure.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: use defer to simplify peer.NewTimer
Josh Bleecher Snyder [Mon, 14 Dec 2020 23:30:10 +0000 (15:30 -0800)] 
device: use defer to simplify peer.NewTimer

This also makes the lifetime of modifyingLock more prominent.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: accept any io.Reader in device.IpcSetOperation
Josh Bleecher Snyder [Mon, 14 Dec 2020 21:30:38 +0000 (13:30 -0800)] 
device: accept any io.Reader in device.IpcSetOperation

Any io.Reader will do, and there are no performance concerns here.
This is technically backwards incompatible,
but it is very unlikely to break any existing code.
It is compatible with the existing uses in wireguard-{windows,android,apple}
and also will allow us to slightly simplify it if desired.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: increase timeout in tests
Josh Bleecher Snyder [Mon, 14 Dec 2020 22:11:33 +0000 (14:11 -0800)] 
device: increase timeout in tests

When running many concurrent test processing using
https://godoc.org/golang.org/x/tools/cmd/stress
the processing sometimes cannot complete a ping in under 300ms.
Increase the timeout to 5s to reduce the rate of false positives.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: prevent spurious errors while closing a device
Josh Bleecher Snyder [Mon, 14 Dec 2020 21:34:03 +0000 (13:34 -0800)] 
device: prevent spurious errors while closing a device

When closing a device, packets that are in flight
can make it to SendBuffer, which then returns an error.
Those errors add noise but no light;
they do not reflect an actual problem.

Adding the synchronization required to prevent
this from occurring is currently expensive and error-prone.
Instead, quietly drop such packets instead of
returning an error.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: remove starting waitgroups
Josh Bleecher Snyder [Thu, 10 Dec 2020 19:25:08 +0000 (11:25 -0800)] 
device: remove starting waitgroups

In each case, the starting waitgroup did nothing but ensure
that the goroutine has launched.

Nothing downstream depends on the order in which goroutines launch,
and if the Go runtime scheduler is so broken that goroutines
don't get launched reasonably promptly, we have much deeper problems.

Given all that, simplify the code.

Passed a race-enabled stress test 25,000 times without failure.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: make test setup more robust
Josh Bleecher Snyder [Wed, 9 Dec 2020 03:23:56 +0000 (19:23 -0800)] 
device: make test setup more robust

Picking two free ports to use for a test is difficult.
The free port we selected might no longer be free when we reach
for it a second time.

On my machine, this failure mode led to failures approximately
once per thousand test runs.

Since failures are rare, and threading through and checking for
all possible errors is complicated, fix this with a big hammer:
Retry if either device fails to come up.

Also, if you accidentally pick the same port twice, delightful confusion ensues.
The handshake failures manifest as crypto errors, which look scary.
Again, fix with retries.

To make these retries easier to implement, use testing.T.Cleanup
instead of defer to close devices. This requires Go 1.14.
Update go.mod accordingly. Go 1.13 is no longer supported anyway.

With these fixes, 'go test -race' ran 100,000 times without failure.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agowintun: do not load dll in init()
Jason A. Donenfeld [Wed, 9 Dec 2020 00:46:55 +0000 (01:46 +0100)] 
wintun: do not load dll in init()

This prevents linking to wintun.dll until it's actually needed, which
should improve startup time.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun/tuntest: make genICMPv4 allocate less
Josh Bleecher Snyder [Thu, 13 Aug 2020 22:39:09 +0000 (15:39 -0700)] 
tun/tuntest: make genICMPv4 allocate less

It doesn't really matter, because it is only used in tests,
but it does remove some noise from pprof profiles.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: avoid copying lock in tests
Josh Bleecher Snyder [Sat, 5 Dec 2020 00:05:51 +0000 (16:05 -0800)] 
device: avoid copying lock in tests

This doesn't cause any practical problems as it is,
but vet (rightly) flags this code as copying a mutex.
It is easy to fix, so do so.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: clear pointers when returning elems to pools
Josh Bleecher Snyder [Fri, 4 Dec 2020 23:36:21 +0000 (15:36 -0800)] 
device: clear pointers when returning elems to pools

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: use labeled for loop instead of goto
Josh Bleecher Snyder [Fri, 4 Dec 2020 22:26:51 +0000 (14:26 -0800)] 
device: use labeled for loop instead of goto

Minor code cleanup; no functional changes.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agomemmod: fix import loading function usage
Jason A. Donenfeld [Fri, 27 Nov 2020 12:13:45 +0000 (13:13 +0100)] 
memmod: fix import loading function usage

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agowintun: log when reboot is suggested by Windows
Simon Rozman [Wed, 25 Nov 2020 11:54:26 +0000 (12:54 +0100)] 
wintun: log when reboot is suggested by Windows

Which really shouldn't happen. But it is a useful information for
troubleshooting.

Signed-off-by: Simon Rozman <simon@rozman.si>
4 years agowintun: keep original error when Wintun session start fails
Simon Rozman [Wed, 25 Nov 2020 11:37:02 +0000 (12:37 +0100)] 
wintun: keep original error when Wintun session start fails

Signed-off-by: Simon Rozman <simon@rozman.si>
4 years agoversion: bump snapshot 0.0.20201118
Jason A. Donenfeld [Wed, 18 Nov 2020 13:24:17 +0000 (14:24 +0100)] 
version: bump snapshot

4 years agomod: bump
Jason A. Donenfeld [Wed, 18 Nov 2020 13:24:00 +0000 (14:24 +0100)] 
mod: bump

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: add write queue mutex for peer
Haichao Liu [Wed, 18 Nov 2020 12:53:22 +0000 (20:53 +0800)] 
device: add write queue mutex for peer

fix panic: send on closed channel when remove peer

Signed-off-by: Haichao Liu <liuhaichao@bytedance.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agowintun: load from filesystem by default
Jason A. Donenfeld [Wed, 11 Nov 2020 17:51:44 +0000 (18:51 +0100)] 
wintun: load from filesystem by default

We let people loading this from resources opt in via:

    go build -tags load_wintun_from_rsrc

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agoglobal: switch to using %w instead of %v for Errorf
Jason A. Donenfeld [Sat, 7 Nov 2020 20:56:32 +0000 (21:56 +0100)] 
global: switch to using %w instead of %v for Errorf

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agomod: update deps
Jason A. Donenfeld [Sat, 7 Nov 2020 14:22:18 +0000 (15:22 +0100)] 
mod: update deps

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agowintun: ring management moved to wintun.dll
Simon Rozman [Sat, 24 Oct 2020 20:40:46 +0000 (22:40 +0200)] 
wintun: ring management moved to wintun.dll

Signed-off-by: Simon Rozman <simon@rozman.si>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agowintun: load wintun.dll from RCDATA resource
Simon Rozman [Fri, 6 Nov 2020 04:24:50 +0000 (05:24 +0100)] 
wintun: load wintun.dll from RCDATA resource

Signed-off-by: Simon Rozman <simon@rozman.si>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agowintun: migrate to wintun.dll API
Simon Rozman [Wed, 22 Jul 2020 07:15:49 +0000 (09:15 +0200)] 
wintun: migrate to wintun.dll API

Rather than having every application using Wintun driver reinvent the
wheel, the Wintun device/adapter/interface management has been moved
from wireguard-go to wintun.dll deployed with Wintun itself.

Signed-off-by: Simon Rozman <simon@rozman.si>
4 years agodevice: format a few things
Jason A. Donenfeld [Fri, 6 Nov 2020 17:01:09 +0000 (18:01 +0100)] 
device: format a few things

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun: use SockaddrCtl from golang.org/x/sys/unix on macOS
Tobias Klauser [Tue, 27 Oct 2020 13:39:36 +0000 (14:39 +0100)] 
tun: use SockaddrCtl from golang.org/x/sys/unix on macOS

Direct syscalls using unix.Syscall(unix.SYS_*, ...) are discouraged on
macOS and might not be supported in future versions. Switch to use
unix.Connect with unix.SockaddrCtl instead.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun: use Ioctl{Get,Set}IfreqMTU from golang.org/x/sys/unix on macOS
Tobias Klauser [Tue, 27 Oct 2020 13:39:35 +0000 (14:39 +0100)] 
tun: use Ioctl{Get,Set}IfreqMTU from golang.org/x/sys/unix on macOS

Direct syscalls using unix.Syscall(unix.SYS_*, ...) are discouraged on
macOS and might not be supported in future versions. Switch to use
unix.Ioctl{Get,Set}IfreqMTU to get and set an interface's MTU.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun: use IoctlCtlInfo from golang.org/x/sys/unix on macOS
Tobias Klauser [Tue, 27 Oct 2020 13:39:34 +0000 (14:39 +0100)] 
tun: use IoctlCtlInfo from golang.org/x/sys/unix on macOS

Direct syscalls using unix.Syscall(unix.SYS_*, ...) are discouraged on
macOS and might not be supported in future versions. Switch to use
unix.IoctlCtlInfo to get the kernel control info.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun: use GetsockoptString in (*NativeTun).Name on macOS
Tobias Klauser [Tue, 27 Oct 2020 13:39:33 +0000 (14:39 +0100)] 
tun: use GetsockoptString in (*NativeTun).Name on macOS

Direct syscalls using unix.Syscall(unix.SYS_*, ...) are discouraged on
macOS and might not be supported in future versions. Instead, use the
existing unix.GetsockoptString wrapper to get the interface name.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agogo.mod: bump golang.org/x/sys to latest version
Tobias Klauser [Tue, 27 Oct 2020 13:39:31 +0000 (14:39 +0100)] 
go.mod: bump golang.org/x/sys to latest version

This adds the fixes for golang/go#41868 which are needed to build
wireguard without direct syscalls on macOS.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agotun/wintun/registry: fix Go 1.15 race/checkptr failure
Brad Fitzpatrick [Wed, 21 Oct 2020 04:13:15 +0000 (21:13 -0700)] 
tun/wintun/registry: fix Go 1.15 race/checkptr failure

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
[Jason: ran go mod tidy.]
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agoMakefile: Add test target
Frank Werner [Wed, 15 Jul 2020 14:22:18 +0000 (16:22 +0200)] 
Makefile: Add test target

Signed-off-by: Frank Werner <mail@hb9fxq.ch>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agoreplay: minor API changes to more idiomatic Go
Riobard Zhan [Wed, 9 Sep 2020 18:06:44 +0000 (02:06 +0800)] 
replay: minor API changes to more idiomatic Go

Signed-off-by: Riobard Zhan <me@riobard.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agoreplay: clean up internals and better documentation
Riobard Zhan [Wed, 9 Sep 2020 17:55:24 +0000 (01:55 +0800)] 
replay: clean up internals and better documentation

Signed-off-by: Riobard Zhan <me@riobard.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: remove global for roaming escape hatch
Jason A. Donenfeld [Wed, 7 Oct 2020 08:17:48 +0000 (10:17 +0200)] 
device: remove global for roaming escape hatch

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agoreplay: divide by bits-per-byte
Jason A. Donenfeld [Mon, 7 Sep 2020 16:51:49 +0000 (18:51 +0200)] 
replay: divide by bits-per-byte

Bits / Bytes-per-Word misses the step of also dividing by Bits-per-Byte,
which we need in order for this to make sense.

Reported-by: Riobard Zhan <me@riobard.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
5 years agodevice: get free port when testing
Sina Siadat [Thu, 30 Jul 2020 16:20:49 +0000 (20:50 +0430)] 
device: get free port when testing

Signed-off-by: Sina Siadat <siadat@gmail.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
5 years agodevice: remove bindsocketshim.go
David Crawshaw [Sat, 4 Jul 2020 10:26:46 +0000 (20:26 +1000)] 
device: remove bindsocketshim.go

Both wireguard-windows and wireguard-android access Bind
directly for these methods now.

Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
5 years agodevice: remove some unnecessary unsafe
Brad Fitzpatrick [Mon, 22 Jun 2020 19:58:01 +0000 (12:58 -0700)] 
device: remove some unnecessary unsafe

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
5 years agodevice: use RTMGRP_IPV4_ROUTE to specify multicast groups mask
Tobias Klauser [Tue, 7 Jul 2020 11:15:13 +0000 (13:15 +0200)] 
device: use RTMGRP_IPV4_ROUTE to specify multicast groups mask

Use the RTMGRP_IPV4_ROUTE const from x/sys/unix instead of using the
corresponding RTNLGRP_IPV4_ROUTE const to create the multicast groups
mask.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
5 years agodevice: wait for routines to stop before removing peers
Dmytro Shynkevych [Wed, 24 Jun 2020 05:35:41 +0000 (01:35 -0400)] 
device: wait for routines to stop before removing peers

Peers are currently removed after Device's goroutines are signaled to stop,
but without waiting for them to actually do so, which is racy.

For example, RoutineHandshake may be in Peer.SendKeepalive
when the corresponding peer is removed, which closes its nonce channel.
This causes a send on a closed channel, as observed in tailscale/tailscale#487.

This patch seems to be the correct synchronizing action:
Peer's goroutines are receivers and handle channel closure gracefully,
so Device's goroutines are the ones that should be fully stopped first.

Signed-Off-By: Dmytro Shynkevych <dmytro@tailscale.com>
5 years agodevice: export Bind and remove socketfd shims for android
David Crawshaw [Mon, 22 Jun 2020 00:42:28 +0000 (10:42 +1000)] 
device: export Bind and remove socketfd shims for android

Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
5 years agoipc: add comment about socketDirectory linker override on android
David Crawshaw [Mon, 22 Jun 2020 00:41:19 +0000 (10:41 +1000)] 
ipc: add comment about socketDirectory linker override on android

Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
5 years agoconn: add comments saying what uses these interfaces
David Crawshaw [Mon, 22 Jun 2020 00:40:59 +0000 (10:40 +1000)] 
conn: add comments saying what uses these interfaces

Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
5 years agodevice: do not include sticky sockets on android
Jason A. Donenfeld [Sun, 7 Jun 2020 07:50:20 +0000 (01:50 -0600)] 
device: do not include sticky sockets on android

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
5 years agoconn: unbreak boundif on android
Jason A. Donenfeld [Sun, 7 Jun 2020 07:41:08 +0000 (01:41 -0600)] 
conn: unbreak boundif on android

Another thing never tested ever.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
5 years agoconn: remove useless comment
Jason A. Donenfeld [Sun, 7 Jun 2020 07:37:01 +0000 (01:37 -0600)] 
conn: remove useless comment

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
5 years agoconn: fix windows situation with boundif
Jason A. Donenfeld [Sun, 7 Jun 2020 07:24:06 +0000 (01:24 -0600)] 
conn: fix windows situation with boundif

This was evidently never tested before committing.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
5 years agoreplay: account for fqcodel reordering
Jason A. Donenfeld [Tue, 19 May 2020 23:46:29 +0000 (17:46 -0600)] 
replay: account for fqcodel reordering

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
5 years agodevice: rework padding calculation and don't shadow paddedSize
Jason A. Donenfeld [Mon, 18 May 2020 20:32:31 +0000 (14:32 -0600)] 
device: rework padding calculation and don't shadow paddedSize

Reported-by: Jayakumar S <jayakumar82.s@gmail.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
5 years agotai64n: make the test deterministic
Dmytro Shynkevych [Tue, 5 May 2020 22:37:54 +0000 (18:37 -0400)] 
tai64n: make the test deterministic

In the presence of preemption, the current test may fail transiently.
This uses static test data instead to ensure consistent behavior.

Signed-off-by: Dmytro Shynkevych <dmytro@tailscale.com>
5 years agomain: now that we're upstreamed, relax Linux warning
Jason A. Donenfeld [Sat, 2 May 2020 08:14:53 +0000 (02:14 -0600)] 
main: now that we're upstreamed, relax Linux warning

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
5 years agoREADME: specify go 1.13
Jason A. Donenfeld [Sat, 2 May 2020 08:08:52 +0000 (02:08 -0600)] 
README: specify go 1.13

Due to the use of the new errors module, we now require at least 1.13
instead of 1.12.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
5 years agoglobal: update header comments and modules
Jason A. Donenfeld [Sat, 2 May 2020 08:08:26 +0000 (02:08 -0600)] 
global: update header comments and modules

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
5 years agoipc: deduplicate some unix-specific code
David Crawshaw [Sat, 2 May 2020 06:28:33 +0000 (16:28 +1000)] 
ipc: deduplicate some unix-specific code

Cleans up and splits out UAPIOpen to its own file.

Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
[zx2c4: changed const to var for socketDirectory]
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
5 years agoipc: remove unnecessary error check
David Crawshaw [Sat, 2 May 2020 06:18:17 +0000 (16:18 +1000)] 
ipc: remove unnecessary error check

os.MkdirAll never returns an os.IsExist error.

Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
5 years agodevice: use atomic access for unlocked keypair.next
Jason A. Donenfeld [Sat, 2 May 2020 07:30:23 +0000 (01:30 -0600)] 
device: use atomic access for unlocked keypair.next

Go's GC semantics might not always guarantee the safety of this, and the
race detector gets upset too, so instead we wrap this all in atomic
accessors.

Reported-by: David Anderson <danderson@tailscale.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
5 years agowintun: make remaining HWID comparisons case insensitive
Simon Rozman [Sat, 2 May 2020 06:49:35 +0000 (08:49 +0200)] 
wintun: make remaining HWID comparisons case insensitive

c85e4a410f27986a2967a49c0155633c716bf3ca introduced preliminary HWID
checking to speed up Wintun adapter enumeration. However, all HWID are
case insensitive by Windows convention.

Furthermore, a device might have multiple HWIDs. When DevInfo's
DeviceRegistryProperty(SPDRP_HARDWAREID) method returns []string, all
strings returned should be checked against given hardware ID.

This issue was discovered when researching Wintun and wireguard-go on
Windows 10 ARM64. The Wintun adapter was created using devcon.exe
utility with "wintun" hardware ID, causing wireguard-go fail to
enumerate the adapter properly.

Signed-off-by: Simon Rozman <simon@rozman.si>
5 years agosetupapi: extend struct size constant definitions for arm(64)
Simon Rozman [Fri, 1 May 2020 04:57:23 +0000 (06:57 +0200)] 
setupapi: extend struct size constant definitions for arm(64)

Signed-off-by: Simon Rozman <simon@rozman.si>
5 years agodevice: add debug logs describing handshake rejection
Avery Pennarun [Wed, 16 Oct 2019 02:39:44 +0000 (22:39 -0400)] 
device: add debug logs describing handshake rejection

Useful in testing when bad network stacks repeat or
batch large numbers of packets.

Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
5 years agotun: return a better error message if /dev/net/tun doesn't exist
Brad Fitzpatrick [Wed, 18 Mar 2020 20:23:00 +0000 (13:23 -0700)] 
tun: return a better error message if /dev/net/tun doesn't exist

It was just returning "no such file or directory" (the String of the
syscall.Errno returned by CreateTUN).

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
5 years agodevice: return generic error from Ipc{Get,Set}Operation.
David Anderson [Wed, 1 Apr 2020 16:27:02 +0000 (09:27 -0700)] 
device: return generic error from Ipc{Get,Set}Operation.

This makes uapi.go's public API conform to Go style in terms
of error types.

Signed-off-by: David Anderson <danderson@tailscale.com>
5 years agotun: NetlinkListener: don't send EventDown before sending EventUp
Avery Pennarun [Wed, 6 Nov 2019 08:28:02 +0000 (00:28 -0800)] 
tun: NetlinkListener: don't send EventDown before sending EventUp

This works around a startup race condition when competing with
HackListener, which is trying to do the same job. If HackListener
detects that the tundev is running while there is still an event in the
netlink queue that says it isn't running, then the device receives a
string of events like
EventUp (HackListener)
EventDown (NetlinkListener)
EventUp (NetlinkListener)
Unfortunately, after the first EventDown, the device stops itself,
thinking incorrectly that the administrator has downed its tundev.

The device is ignoring the initial EventDown anyway, so just don't emit
it.

Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
5 years agodevice: give handshake state a type
David Crawshaw [Thu, 5 Mar 2020 01:58:39 +0000 (20:58 -0500)] 
device: give handshake state a type

And unexport handshake constants.

Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
5 years agotuntest: split out testing package
David Crawshaw [Tue, 7 Jan 2020 15:43:17 +0000 (07:43 -0800)] 
tuntest: split out testing package

This code is useful to other packages writing tests.

Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
5 years agotun: fix data race on name field
Brad Fitzpatrick [Fri, 28 Feb 2020 17:10:16 +0000 (09:10 -0800)] 
tun: fix data race on name field

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
5 years agotun: remove unused isUp method
Brad Fitzpatrick [Fri, 28 Feb 2020 16:53:29 +0000 (08:53 -0800)] 
tun: remove unused isUp method

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
5 years agoconn: introduce new package that splits out the Bind and Endpoint types
David Crawshaw [Thu, 7 Nov 2019 16:13:05 +0000 (11:13 -0500)] 
conn: introduce new package that splits out the Bind and Endpoint types

The sticky socket code stays in the device package for now,
as it reaches deeply into the peer list.

This is the first step in an effort to split some code out of
the very busy device package.

Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
5 years agowintun: split error message for create vs open namespace.
Avery Pennarun [Wed, 23 Oct 2019 04:08:52 +0000 (00:08 -0400)] 
wintun: split error message for create vs open namespace.

Signed-off-by: Avery Pennarun <apenwarr@tailscale.com>
5 years agodevice: add test to ensure Peer fields are safe for atomic access on 32-bit
David Anderson [Sun, 1 Mar 2020 08:39:24 +0000 (00:39 -0800)] 
device: add test to ensure Peer fields are safe for atomic access on 32-bit

Adds a test that will fail consistently on 32-bit platforms if the
struct ever changes again to violate the rules. This is likely not
needed because unaligned access crashes reliably, but this will reliably
fail even if tests accidentally pass due to lucky alignment.

Signed-Off-By: David Anderson <danderson@tailscale.com>
5 years agorwcancel: no-op builds for windows and darwin
David Crawshaw [Wed, 19 Feb 2020 15:09:24 +0000 (10:09 -0500)] 
rwcancel: no-op builds for windows and darwin

This lets us include the package on those platforms in a
followup commit where we split out a conn package from device.
It also lets us run `go test ./...` when developing on macOS.

Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
5 years agoratelimiter: use a fake clock in tests and style cleanups
David Crawshaw [Sun, 8 Dec 2019 23:22:31 +0000 (18:22 -0500)] 
ratelimiter: use a fake clock in tests and style cleanups

The existing test would occasionally flake out with:

--- FAIL: TestRatelimiter (0.12s)
    ratelimiter_test.go:99: Test failed for 127.0.0.1 , on: 7 ( not having refilled enough ) expected: false got: true
FAIL
FAIL    golang.zx2c4.com/wireguard/ratelimiter  0.171s

The fake clock also means the tests run much faster, so
testing this package with -count=1000 now takes < 100ms.

While here, several style cleanups. The most significant one
is unembeding the sync.Mutex fields in the rate limiter objects.
Embedded as they were, the lock methods were accessible
outside the ratelimiter package. As they aren't needed externally,
keep them internal to make them easier to reason about.

Passes `go test -race -count=10000 ./ratelimiter`

Signed-off-by: David Crawshaw <crawshaw@tailscale.com>
5 years agoversion: bump snapshot 0.0.20200320
Jason A. Donenfeld [Fri, 20 Mar 2020 18:00:53 +0000 (12:00 -0600)] 
version: bump snapshot

5 years agonoise: unify zero checking of ecdh
Jason A. Donenfeld [Wed, 18 Mar 2020 05:06:56 +0000 (23:06 -0600)] 
noise: unify zero checking of ecdh

5 years agoglobal: use RTMGRP_* consts from x/sys/unix
Tobias Klauser [Wed, 4 Mar 2020 16:21:54 +0000 (17:21 +0100)] 
global: use RTMGRP_* consts from x/sys/unix

Update the golang.org/x/sys/unix dependency and use the newly introduced
RTMGRP_* consts instead of using the corresponding RTNLGRP_* const to
create a mask.

Signed-off-by: Tobias Klauser <tklauser@distanz.ch>
5 years agosend: account for zero mtu
Jason A. Donenfeld [Thu, 13 Feb 2020 15:20:56 +0000 (16:20 +0100)] 
send: account for zero mtu

Don't divide by zero.

5 years agodevice: fix private key removal logic
Jason A. Donenfeld [Tue, 4 Feb 2020 17:08:51 +0000 (18:08 +0100)] 
device: fix private key removal logic

5 years agouapi: allow unsetting device private key with /dev/null
Jason A. Donenfeld [Tue, 4 Feb 2020 17:03:31 +0000 (18:03 +0100)] 
uapi: allow unsetting device private key with /dev/null

5 years agoversion: bump snapshot 0.0.20200121
Jason A. Donenfeld [Tue, 21 Jan 2020 15:27:19 +0000 (16:27 +0100)] 
version: bump snapshot

5 years agotun: darwin: ignore ENOMEM errors
Jason A. Donenfeld [Wed, 15 Jan 2020 18:39:37 +0000 (13:39 -0500)] 
tun: darwin: ignore ENOMEM errors

Coauthored-by: Andrej Mihajlov <and@mullvad.net>
5 years agotun: windows: serialize write calls
Jason A. Donenfeld [Tue, 7 Jan 2020 16:40:45 +0000 (11:40 -0500)] 
tun: windows: serialize write calls

5 years agoREADME: update repo urls
Jason A. Donenfeld [Mon, 30 Dec 2019 10:46:34 +0000 (11:46 +0100)] 
README: update repo urls

5 years agodevice: SendmsgN mutates the input sockaddr
Jason A. Donenfeld [Wed, 27 Nov 2019 12:38:45 +0000 (13:38 +0100)] 
device: SendmsgN mutates the input sockaddr

So we take a new granular lock to prevent concurrent writes from
racing.

WARNING: DATA RACE
Write at 0x00c0011f2740 by goroutine 27:
  golang.org/x/sys/unix.(*SockaddrInet4).sockaddr()
      /go/pkg/mod/golang.org/x/sys@v0.0.0-20191105231009-c1f44814a5cd/unix/syscall_linux.go:384
+0x114
  golang.org/x/sys/unix.SendmsgN()
      /go/pkg/mod/golang.org/x/sys@v0.0.0-20191105231009-c1f44814a5cd/unix/syscall_linux.go:1304
+0x288
  golang.zx2c4.com/wireguard/device.send4()
      /go/pkg/mod/golang.zx2c4.com/wireguard@v0.0.20191012/device/conn_linux.go:485
+0x11f
  golang.zx2c4.com/wireguard/device.(*nativeBind).Send()
      /go/pkg/mod/golang.zx2c4.com/wireguard@v0.0.20191012/device/conn_linux.go:268
+0x1d6
  golang.zx2c4.com/wireguard/device.(*Peer).SendBuffer()
      /go/pkg/mod/golang.zx2c4.com/wireguard@v0.0.20191012/device/peer.go:151
+0x285
  golang.zx2c4.com/wireguard/device.(*Peer).SendHandshakeInitiation()
      /go/pkg/mod/golang.zx2c4.com/wireguard@v0.0.20191012/device/send.go:163
+0x692
  golang.zx2c4.com/wireguard/device.(*Device).RoutineReadFromTUN()
      /go/pkg/mod/golang.zx2c4.com/wireguard@v0.0.20191012/device/send.go:318
+0x4b8

Previous write at 0x00c0011f2740 by goroutine 386:
  golang.org/x/sys/unix.(*SockaddrInet4).sockaddr()
      /go/pkg/mod/golang.org/x/sys@v0.0.0-20191105231009-c1f44814a5cd/unix/syscall_linux.go:384
+0x114
  golang.org/x/sys/unix.SendmsgN()
      /go/pkg/mod/golang.org/x/sys@v0.0.0-20191105231009-c1f44814a5cd/unix/syscall_linux.go:1304
+0x288
  golang.zx2c4.com/wireguard/device.send4()
      /go/pkg/mod/golang.zx2c4.com/wireguard@v0.0.20191012/device/conn_linux.go:485
+0x11f
  golang.zx2c4.com/wireguard/device.(*nativeBind).Send()
      /go/pkg/mod/golang.zx2c4.com/wireguard@v0.0.20191012/device/conn_linux.go:268
+0x1d6
  golang.zx2c4.com/wireguard/device.(*Peer).SendBuffer()
      /go/pkg/mod/golang.zx2c4.com/wireguard@v0.0.20191012/device/peer.go:151
+0x285
  golang.zx2c4.com/wireguard/device.(*Peer).SendHandshakeInitiation()
      /go/pkg/mod/golang.zx2c4.com/wireguard@v0.0.20191012/device/send.go:163
+0x692
  golang.zx2c4.com/wireguard/device.expiredRetransmitHandshake()
      /go/pkg/mod/golang.zx2c4.com/wireguard@v0.0.20191012/device/timers.go:110
+0x40c
  golang.zx2c4.com/wireguard/device.(*Peer).NewTimer.func1()
      /go/pkg/mod/golang.zx2c4.com/wireguard@v0.0.20191012/device/timers.go:42
+0xd8

Goroutine 27 (running) created at:
  golang.zx2c4.com/wireguard/device.NewDevice()
      /go/pkg/mod/golang.zx2c4.com/wireguard@v0.0.20191012/device/device.go:322
+0x5e8
  main.main()
      /go/src/x/main.go:102 +0x58e

Goroutine 386 (finished) created at:
  time.goFunc()
      /usr/local/go/src/time/sleep.go:168 +0x51

Reported-by: Ben Burkert <ben@benburkert.com>
5 years agowintun: manage ring memory manually
Jason A. Donenfeld [Thu, 21 Nov 2019 13:48:21 +0000 (14:48 +0100)] 
wintun: manage ring memory manually

It's large and Go's garbage collector doesn't deal with it especially
well.

5 years agoconstants: recalculate rekey max based on a one minute flood
Jason A. Donenfeld [Wed, 30 Oct 2019 13:29:32 +0000 (14:29 +0100)] 
constants: recalculate rekey max based on a one minute flood

Discussed-with: Mathias Hall-Andersen <mathias@hall-andersen.dk>

5 years agoglobal: fix a few typos courtesy of codespell
Jonathan Tooker [Mon, 21 Oct 2019 20:52:26 +0000 (15:52 -0500)] 
global: fix a few typos courtesy of codespell

Signed-off-by: Jonathan Tooker <jonathan.tooker@netprotect.com>
5 years agodevice: allow blackholing sockets
Jason A. Donenfeld [Mon, 21 Oct 2019 11:29:57 +0000 (13:29 +0200)] 
device: allow blackholing sockets