]> git.ipfire.org Git - thirdparty/wireguard-go.git/log
thirdparty/wireguard-go.git
4 years agodevice: remove unused fields from DummyDatagram and DummyBind
Josh Bleecher Snyder [Tue, 19 Jan 2021 21:42:45 +0000 (13:42 -0800)] 
device: remove unused fields from DummyDatagram and DummyBind

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: remove unused trie test code
Josh Bleecher Snyder [Tue, 19 Jan 2021 21:41:47 +0000 (13:41 -0800)] 
device: remove unused trie test code

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agoconn: remove _ method receiver
Josh Bleecher Snyder [Tue, 19 Jan 2021 21:41:07 +0000 (13:41 -0800)] 
conn: remove _ method receiver

Minor style fix.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agotun: fix fmt.Errorf format strings
Josh Bleecher Snyder [Tue, 19 Jan 2021 21:39:48 +0000 (13:39 -0800)] 
tun: fix fmt.Errorf format strings

Type tcpip.Error is not an error.

I've filed https://github.com/google/gvisor/issues/5314
to fix this upstream.

Until that is fixed, use %v instead of %w,
to keep vet happy.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: remove unnecessary zeroing
Josh Bleecher Snyder [Tue, 19 Jan 2021 16:33:12 +0000 (08:33 -0800)] 
device: remove unnecessary zeroing

Newly allocated objects are already zeroed.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: remove QueueInboundElement.dropped
Josh Bleecher Snyder [Sun, 17 Jan 2021 17:49:39 +0000 (09:49 -0800)] 
device: remove QueueInboundElement.dropped

Now that we block when enqueueing to the decryption queue,
there is only one case in which we "drop" a inbound element,
when decryption fails.

We can use a simple, obvious, sync-free sentinel for that, elem.packet == nil.
Also, we can return the message buffer to the pool slightly later,
which further simplifies the code.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: remove QueueOutboundElement.dropped
Josh Bleecher Snyder [Sun, 17 Jan 2021 17:40:18 +0000 (09:40 -0800)] 
device: remove QueueOutboundElement.dropped

If we block when enqueuing encryption elements to the queue,
then we never drop them.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agotun/wintun/memmod: gofmt
Josh Bleecher Snyder [Sat, 16 Jan 2021 01:25:34 +0000 (17:25 -0800)] 
tun/wintun/memmod: gofmt

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agotun/wintun/memmod: fix format verb
Josh Bleecher Snyder [Sat, 16 Jan 2021 01:25:11 +0000 (17:25 -0800)] 
tun/wintun/memmod: fix format verb

Caught by 'go vet'.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: check returned errors from NewPeer in TestNoiseHandshake
Josh Bleecher Snyder [Wed, 13 Jan 2021 01:27:27 +0000 (17:27 -0800)] 
device: check returned errors from NewPeer in TestNoiseHandshake

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: remove selects from encrypt/decrypt/inbound/outbound enqueuing
Josh Bleecher Snyder [Tue, 12 Jan 2021 01:21:16 +0000 (17:21 -0800)] 
device: remove selects from encrypt/decrypt/inbound/outbound enqueuing

Block instead. Backpressure here is fine, probably preferable.
This reduces code complexity.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: put handshake buffer in pool in FlushPacketQueues
Josh Bleecher Snyder [Tue, 12 Jan 2021 23:04:32 +0000 (15:04 -0800)] 
device: put handshake buffer in pool in FlushPacketQueues

This appears to have been an oversight.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: use channel close to shut down and drain decryption channel
Josh Bleecher Snyder [Tue, 12 Jan 2021 01:34:02 +0000 (17:34 -0800)] 
device: use channel close to shut down and drain decryption channel

This is similar to commit e1fa1cc5560020e67d33aa7e74674853671cf0a0,
but for the decryption channel.

It is an alternative fix to f9f655567930a4cd78d40fa4ba0d58503335ae6a.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agotun: add tcpip stack tunnel abstraction
Jason A. Donenfeld [Mon, 11 Jan 2021 15:28:12 +0000 (16:28 +0100)] 
tun: add tcpip stack tunnel abstraction

This allows people to initiate connections over WireGuard without any
underlying operating system support.

I'm not crazy about the trash it adds to go.sum, but the code this
actually adds to the binaries seems contained to the gvisor repo.

For the TCP/IP implementation, it uses gvisor. And it borrows some
internals from the Go standard library's resolver in order to bring Dial
and DialContext to tun_net, along with the LookupHost helper function.
This allows for things like HTTP2-over-TLS to work quite well:

    package main

    import (
        "io"
        "log"
        "net"
        "net/http"

        "golang.zx2c4.com/wireguard/device"
        "golang.zx2c4.com/wireguard/tun"
    )

    func main() {
        tun, tnet, err := tun.CreateNetTUN([]net.IP{net.ParseIP("192.168.4.29")}, []net.IP{net.ParseIP("8.8.8.8"), net.ParseIP("8.8.4.4")}, 1420)
        if err != nil {
            log.Panic(err)
        }
        dev := device.NewDevice(tun, &device.Logger{log.Default(), log.Default(), log.Default()})
        dev.IpcSet(`private_key=a8dac1d8a70a751f0f699fb14ba1cff7b79cf4fbd8f09f44c6e6a90d0369604f
    public_key=25123c5dcd3328ff645e4f2a3fce0d754400d3887a0cb7c56f0267e20fbf3c5b
    endpoint=163.172.161.0:12912
    allowed_ip=0.0.0.0/0
    `)
        dev.Up()

        client := http.Client{
            Transport: &http.Transport{
                DialContext: tnet.DialContext,
            },
        }
        resp, err := client.Get("https://www.zx2c4.com/ip")
        if err != nil {
            log.Panic(err)
        }
        body, err := io.ReadAll(resp.Body)
        if err != nil {
            log.Panic(err)
        }
        log.Println(string(body))
    }

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: receive: do not exit immediately on transient UDP receive errors
Jason A. Donenfeld [Fri, 8 Jan 2021 13:25:37 +0000 (14:25 +0100)] 
device: receive: do not exit immediately on transient UDP receive errors

Some users report seeing lines like:

> Routine: receive incoming IPv4 - stopped

Popping up unexpectedly. Let's sleep and try again before failing, and
also log the error, and perhaps we'll eventually understand this
situation better in future versions.

Because we have to distinguish between the socket being closed
explicitly and whatever error this is, we bump the module to require Go
1.16.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agoconn: linux: do not allow ReceiveIPvX to race with Close
Jason A. Donenfeld [Thu, 7 Jan 2021 16:00:21 +0000 (17:00 +0100)] 
conn: linux: do not allow ReceiveIPvX to race with Close

If Close is called after ReceiveIPvX, then ReceiveIPvX will block on an
invalid or potentially reused fd.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: receive: drain decryption queue before exiting RoutineDecryption
Jason A. Donenfeld [Thu, 7 Jan 2021 14:56:52 +0000 (15:56 +0100)] 
device: receive: drain decryption queue before exiting RoutineDecryption

It's possible for RoutineSequentialReceiver to try to lock an elem after
RoutineDecryption has exited. Before this meant we didn't then unlock
the elem, so the whole program deadlocked.

As well, it looks like the flush code (which is now potentially
unnecessary?) wasn't properly dropping the buffers for the
not-already-dropped case.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agodevice: add latency and throughput benchmarks
Josh Bleecher Snyder [Wed, 6 Jan 2021 02:14:59 +0000 (18:14 -0800)] 
device: add latency and throughput benchmarks

These obviously don't perfectly capture real world performance,
in which syscalls and network links have a significant impact.
Nevertheless, they capture some of the internal performance factors,
and they're easy and convenient to work with.

Hat tip to Avery Pennarun for help designing the throughput benchmark.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: use LogLevelError for benchmarking
Josh Bleecher Snyder [Wed, 6 Jan 2021 00:04:38 +0000 (16:04 -0800)] 
device: use LogLevelError for benchmarking

This keeps the output minimal and focused on the benchmark results.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: make test infrastructure usable with benchmarks
Josh Bleecher Snyder [Tue, 5 Jan 2021 23:03:24 +0000 (15:03 -0800)] 
device: make test infrastructure usable with benchmarks

Switch from *testing.T to testing.TB.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agomemmod: apply explicit build tags to _32 and _64 files
Jason A. Donenfeld [Wed, 23 Dec 2020 16:49:36 +0000 (17:49 +0100)] 
memmod: apply explicit build tags to _32 and _64 files

Since _32 and _64 aren't valid goarchs, they don't match _GOOS_GOARCH,
and so the existing tags wind up not being restricted to windows-only.
This fixes the problem by adding windows to the tags explicitly. We
could also fix it by calling the files _32_windows or _64_windows, but
that changes the convention with the other single-arch files.

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

Persnickety consumers can now do:

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

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
4 years agoall: use ++ to increment
Josh Bleecher Snyder [Tue, 22 Dec 2020 19:38:24 +0000 (11:38 -0800)] 
all: use ++ to increment

Make the code slightly more idiomatic. No functional changes.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: remove unnecessary zeroing
Josh Bleecher Snyder [Thu, 17 Dec 2020 23:20:35 +0000 (15:20 -0800)] 
device: remove unnecessary zeroing

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: call wg.Add outside the goroutine
Josh Bleecher Snyder [Sat, 19 Dec 2020 00:32:46 +0000 (16:32 -0800)] 
device: call wg.Add outside the goroutine

One of the first rules of WaitGroups is that you call wg.Add
outside of a goroutine, not inside it. Fix this embarrassing mistake.

This prevents an extremely rare race condition (2 per 100,000 runs)
which could occur when attempting to start a new peer
concurrently with shutting down a device.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: remove QueueInboundElement leak with stopped peers
Josh Bleecher Snyder [Tue, 22 Dec 2020 17:52:53 +0000 (09:52 -0800)] 
device: remove QueueInboundElement leak with stopped peers

This is particularly problematic on mobile,
where there is a fixed number of elements.
If most of them leak, it'll impact performance;
if all of them leak, the device will permanently deadlock.

I have a test that detects element leaks, which is how I found this one.
There are some remaining leaks that I have not yet tracked down,
but this is the most prominent by far.

I will commit the test when it passes reliably.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: simplify UAPI helper methods
Josh Bleecher Snyder [Tue, 22 Dec 2020 18:08:25 +0000 (10:08 -0800)] 
device: simplify UAPI helper methods

bufio is not required.

strings.Builder is cheaper than bytes.Buffer for constructing strings.

io.Writer is more flexible than io.StringWriter,
and just as cheap (when used with io.WriteString).

Run gofmt.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
4 years agodevice: fix alignment of peer stats member
Jason A. Donenfeld [Tue, 22 Dec 2020 20:34:21 +0000 (21:34 +0100)] 
device: fix alignment of peer stats member

This was shifted by 2 bytes when making persistent keepalive into a u32.
Fix it by placing it after the aligned region.

Fixes: e739ff7 ("device: fix persistent_keepalive_interval data races")
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.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 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>