net: WireGuard secure network tunnel
Subsystems (5)
Jason A. Donenfeld
maintainerPATCH 1/1 net: WireGuard secure network tunnel
WireGuard is a layer 3 secure networking tunnel made specifically for the kernel, that aims to be much simpler and easier to audit than IPsec. Extensive documentation and description of the protocol and considerations, along with formal proofs of the cryptography, are available at:
* https://www.wireguard.com/
* https://www.wireguard.com/papers/wireguard.pdf
This commit implements WireGuard as a simple network device driver, accessible in the usual RTNL way used by virtual network drivers. It makes use of the udp_tunnel APIs, GRO, GSO, NAPI, and the usual set of networking subsystem APIs. It has a somewhat novel multicore queueing system designed for maximum throughput and minimal latency of encryption operations, but it is implemented modestly using workqueues and NAPI. Configuration is done via generic Netlink, and following a review from the Netlink maintainer a year ago, several high profile userspace have already implemented the API.
This commit also comes with several different tests, both in-kernel tests and out-of-kernel tests based on network namespaces, taking profit of the fact that sockets used by WireGuard intentionally stay in the namespace the WireGuard interface was originally created, exactly like the semantics of userspace tun devices. See wireguard.com/netns/ for pictures and examples.
The source code is fairly short, but rather than combining everything into a single file, WireGuard is developed as cleanly separable files, making auditing and comprehension easier. Things are laid out as follows:
* noise.[ch], cookie.[ch], messages.h: These implement the bulk of the cryptographic aspects of the protocol, and are mostly data-only in nature, taking in buffers of bytes and spitting out buffers of bytes. They also handle reference counting for their various shared pieces of data, like keys and key lists.
* ratelimiter.[ch]: Used as an integral part of cookie.[ch] for ratelimiting certain types of cryptographic operations in accordance with particular WireGuard semantics.
* allowedips.[ch], peerlookup.[ch]: The main lookup structures of WireGuard, the former being trie-like with particular semantics, an integral part of the design of the protocol, and the latter just being nice helper functions around the various hashtables we use.
* device.[ch]: Implementation of functions for the netdevice and for rtnl, responsible for maintaining the life of a given interface and wiring it up to the rest of WireGuard.
* peer.[ch]: Each interface has a list of peers, with helper functions available here for creation, destruction, and reference counting.
* socket.[ch]: Implementation of functions related to udp_socket and the general set of kernel socket APIs, for sending and receiving ciphertext UDP packets, and taking care of WireGuard-specific sticky socket routing semantics for the automatic roaming.
* netlink.[ch]: Userspace API entry point for configuring WireGuard peers and devices. The API has been implemented by several userspace tools and network management utility, and the WireGuard project distributes the basic wg(8) tool.
* queueing.[ch]: Shared function on the rx and tx path for handling the various queues used in the multicore algorithms.
* send.c: Handles encrypting outgoing packets in parallel on multiple cores, before sending them in order on a single core, via workqueues and ring buffers. Also handles sending handshake and cookie messages as part of the protocol, in parallel.
* receive.c: Handles decrypting incoming packets in parallel on multiple cores, before passing them off in order to be ingested via the rest of the networking subsystem with GRO via the typical NAPI poll function. Also handles receiving handshake and cookie messages as part of the protocol, in parallel.
* timers.[ch]: Uses the timer wheel to implement protocol particular event timeouts, and gives a set of very simple event-driven entry point functions for callers.
* main.c, version.h: Initialization and deinitialization of the module.
* selftest/*.h: Runtime unit tests for some of the most security sensitive functions.
* tools/testing/selftests/wireguard/netns.sh: Aforementioned testing script using network namespaces.
This commit aims to be as self-contained as possible, implementing WireGuard as a standalone module not needing much special handling or coordination from the network subsystem. I expect for future optimizations to the network stack to positively improve WireGuard, and vice-versa, but for the time being, this exists as intentionally standalone.
We introduce a menu option for CONFIG_WIREGUARD, as well as providing a verbose debug log and self-tests via CONFIG_WIREGUARD_DEBUG.
---
Note: This benefits from [1], which is currently in Herbert's tree, but
will be in Linus' for 5.5 pretty shortly. In the meanwhile, this code
here still does work fine.
[1] https://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6.git/commit/?id=8394bfec51e0e565556101bcc4e2fe7551104cd8
MAINTAINERS | 8 + drivers/net/Kconfig | 41 + drivers/net/Makefile | 1 + drivers/net/wireguard/Makefile | 18 + drivers/net/wireguard/allowedips.c | 381 +++++++++ drivers/net/wireguard/allowedips.h | 59 ++ drivers/net/wireguard/cookie.c | 236 ++++++ drivers/net/wireguard/cookie.h | 59 ++ drivers/net/wireguard/device.c | 458 ++++++++++ drivers/net/wireguard/device.h | 65 ++ drivers/net/wireguard/main.c | 64 ++ drivers/net/wireguard/messages.h | 128 +++ drivers/net/wireguard/netlink.c | 642 ++++++++++++++ drivers/net/wireguard/netlink.h | 12 + drivers/net/wireguard/noise.c | 828 +++++++++++++++++++ drivers/net/wireguard/noise.h | 137 +++ drivers/net/wireguard/peer.c | 240 ++++++ drivers/net/wireguard/peer.h | 83 ++ drivers/net/wireguard/peerlookup.c | 221 +++++ drivers/net/wireguard/peerlookup.h | 64 ++ drivers/net/wireguard/queueing.c | 53 ++ drivers/net/wireguard/queueing.h | 197 +++++ drivers/net/wireguard/ratelimiter.c | 223 +++++ drivers/net/wireguard/ratelimiter.h | 19 + drivers/net/wireguard/receive.c | 595 +++++++++++++ drivers/net/wireguard/selftest/allowedips.c | 683 +++++++++++++++ drivers/net/wireguard/selftest/counter.c | 104 +++ drivers/net/wireguard/selftest/ratelimiter.c | 226 +++++ drivers/net/wireguard/send.c | 424 ++++++++++ drivers/net/wireguard/socket.c | 436 ++++++++++ drivers/net/wireguard/socket.h | 44 + drivers/net/wireguard/timers.c | 243 ++++++ drivers/net/wireguard/timers.h | 31 + drivers/net/wireguard/version.h | 1 + include/uapi/linux/wireguard.h | 196 +++++ tools/testing/selftests/wireguard/netns.sh | 537 ++++++++++++ 36 files changed, 7757 insertions(+) create mode 100644 drivers/net/wireguard/Makefile create mode 100644 drivers/net/wireguard/allowedips.c create mode 100644 drivers/net/wireguard/allowedips.h create mode 100644 drivers/net/wireguard/cookie.c create mode 100644 drivers/net/wireguard/cookie.h create mode 100644 drivers/net/wireguard/device.c create mode 100644 drivers/net/wireguard/device.h create mode 100644 drivers/net/wireguard/main.c create mode 100644 drivers/net/wireguard/messages.h create mode 100644 drivers/net/wireguard/netlink.c create mode 100644 drivers/net/wireguard/netlink.h create mode 100644 drivers/net/wireguard/noise.c create mode 100644 drivers/net/wireguard/noise.h create mode 100644 drivers/net/wireguard/peer.c create mode 100644 drivers/net/wireguard/peer.h create mode 100644 drivers/net/wireguard/peerlookup.c create mode 100644 drivers/net/wireguard/peerlookup.h create mode 100644 drivers/net/wireguard/queueing.c create mode 100644 drivers/net/wireguard/queueing.h create mode 100644 drivers/net/wireguard/ratelimiter.c create mode 100644 drivers/net/wireguard/ratelimiter.h create mode 100644 drivers/net/wireguard/receive.c create mode 100644 drivers/net/wireguard/selftest/allowedips.c create mode 100644 drivers/net/wireguard/selftest/counter.c create mode 100644 drivers/net/wireguard/selftest/ratelimiter.c create mode 100644 drivers/net/wireguard/send.c create mode 100644 drivers/net/wireguard/socket.c create mode 100644 drivers/net/wireguard/socket.h create mode 100644 drivers/net/wireguard/timers.c create mode 100644 drivers/net/wireguard/timers.h create mode 100644 drivers/net/wireguard/version.h create mode 100644 include/uapi/linux/wireguard.h create mode 100755 tools/testing/selftests/wireguard/netns.sh
MAINTAINERS +8 -0
@@ -17679,6 +17679,14 @@ L: [email protected] S: Maintained F: drivers/gpio/gpio-ws16c48.c +WIREGUARD SECURE NETWORK TUNNEL +M: Jason A. Donenfeld <[email protected]> +S: Maintained +F: drivers/net/wireguard/ +F: tools/testing/selftests/wireguard/ +L: [email protected] +L: [email protected] + WISTRON LAPTOP BUTTON DRIVER M: Miloslav Trmac <[email protected]> S: Maintained
drivers/net/Kconfig +41 -0
47 lines not on this page · raw message
drivers/net/Makefile +1 -0
@@ -10,6 +10,7 @@ obj-$(CONFIG_BONDING) += bonding/ obj-$(CONFIG_IPVLAN) += ipvlan/ obj-$(CONFIG_IPVTAP) += ipvlan/ obj-$(CONFIG_DUMMY) += dummy.o +obj-$(CONFIG_WIREGUARD) += wireguard/ obj-$(CONFIG_EQUALIZER) += eql.o obj-$(CONFIG_IFB) += ifb.o obj-$(CONFIG_MACSEC) += macsec.o
drivers/net/wireguard/Makefile new file +18 -0
@@ -0,0 +1,18 @@ +ccflags-y := -O3 +ccflags-y += -D'pr_fmt(fmt)=KBUILD_MODNAME ": " fmt' +ccflags-$(CONFIG_WIREGUARD_DEBUG) += -DDEBUG +wireguard-y := main.o +wireguard-y += noise.o +wireguard-y += device.o +wireguard-y += peer.o +wireguard-y += timers.o +wireguard-y += queueing.o +wireguard-y += send.o +wireguard-y += receive.o +wireguard-y += socket.o +wireguard-y += peerlookup.o +wireguard-y += allowedips.o +wireguard-y += ratelimiter.o +wireguard-y += cookie.o +wireguard-y += netlink.o +obj-$(CONFIG_WIREGUARD) := wireguard.o
drivers/net/wireguard/allowedips.c new file +381 -0
381 lines not on this page · raw message
drivers/net/wireguard/allowedips.h new file +59 -0
59 lines not on this page · raw message
drivers/net/wireguard/cookie.c new file +236 -0
236 lines not on this page · raw message
drivers/net/wireguard/cookie.h new file +59 -0
59 lines not on this page · raw message
drivers/net/wireguard/device.c new file +458 -0
458 lines not on this page · raw message
drivers/net/wireguard/device.h new file +65 -0
65 lines not on this page · raw message
drivers/net/wireguard/main.c new file +64 -0
64 lines not on this page · raw message
drivers/net/wireguard/messages.h new file +128 -0
128 lines not on this page · raw message
drivers/net/wireguard/netlink.c new file +642 -0
642 lines not on this page · raw message
drivers/net/wireguard/netlink.h new file +12 -0
@@ -0,0 +1,12 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved. +*/ + +#ifndef _WG_NETLINK_H +#define _WG_NETLINK_H + +int wg_genetlink_init (void ); +void wg_genetlink_uninit (void ); + +#endif /* _WG_NETLINK_H */
drivers/net/wireguard/noise.c new file +828 -0
810 lines not on this page · raw message
drivers/net/wireguard/noise.h new file +137 -0
137 lines not on this page · raw message
drivers/net/wireguard/peer.c new file +240 -0
240 lines not on this page · raw message
drivers/net/wireguard/peer.h new file +83 -0
83 lines not on this page · raw message
drivers/net/wireguard/peerlookup.c new file +221 -0
221 lines not on this page · raw message
drivers/net/wireguard/peerlookup.h new file +64 -0
64 lines not on this page · raw message
drivers/net/wireguard/queueing.c new file +53 -0
53 lines not on this page · raw message
drivers/net/wireguard/queueing.h new file +197 -0
197 lines not on this page · raw message
drivers/net/wireguard/ratelimiter.c new file +223 -0
223 lines not on this page · raw message
drivers/net/wireguard/ratelimiter.h new file +19 -0
Lines of this file are not shown. View the raw message.
drivers/net/wireguard/receive.c new file +595 -0
595 lines not on this page · raw message
drivers/net/wireguard/selftest/allowedips.c new file +683 -0
683 lines not on this page · raw message
drivers/net/wireguard/selftest/counter.c new file +104 -0
104 lines not on this page · raw message
drivers/net/wireguard/selftest/ratelimiter.c new file +226 -0
226 lines not on this page · raw message
drivers/net/wireguard/send.c new file +424 -0
424 lines not on this page · raw message
drivers/net/wireguard/socket.c new file +436 -0
436 lines not on this page · raw message
drivers/net/wireguard/socket.h new file +44 -0
44 lines not on this page · raw message
drivers/net/wireguard/timers.c new file +243 -0
243 lines not on this page · raw message
drivers/net/wireguard/timers.h new file +31 -0
Lines of this file are not shown. View the raw message.
drivers/net/wireguard/version.h new file +1 -0
Lines of this file are not shown. View the raw message.
include/uapi/linux/wireguard.h new file +196 -0
196 lines not on this page · raw message
tools/testing/selftests/wireguard/netns.sh new file +537 -0
537 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.24.0
Part of the diff is not shown here. View the raw message.
David Miller
maintainerFrom: "Jason A. Donenfeld" <[email protected]>
Date: Wed, 27 Nov 2019 12:26:43 +0100
drivers/net/wireguard/device.c line 167
Quoted 2 lines
drivers/net/wireguard/device.c new file +2
-0+ do { + next = skb->next;
I've been trying desperately to remove all direct references to the SKB list implementation details so that we can convert it over to list_head. This means no direct references to skb->next nor skb->prev.
Please rearrange this using appropriate helpers and abstractions from skbuff.h
Jason A. Donenfeld
maintainerHey Dave,
Quoted 11 lines · David Miller
From: "Jason A. Donenfeld" <[email protected]>
Date: Wed, 27 Nov 2019 12:26:43 +0100drivers/net/wireguard/device.c line 167
Quoted 2 lines
drivers/net/wireguard/device.c new file +2
-0+ do { + next = skb->next;I've been trying desperately to remove all direct references to the SKB list implementation details so that we can convert it over to
list_head. This means no direct references toskb->nextnorskb->prev.Please rearrange this using appropriate helpers and abstractions from
skbuff.h
I'm not a huge fan of doing manual skb surgery either. The annoying thing here is that skb_gso_segment returns a list of skbs that's terminated by the last one's next pointer being NULL. I assume it's this way so that the GSO code doesn't have to pass a head around. I went to see what other drivers are doing to deal with the return value of skb_gso_segment, and found that every place without fail does pretty much the same thing as me, whether it's wifi drivers, ethernet drivers, qdiscs, ipsec, etc. Here's (one of) IPsec's usage, for example:
segs = skb_gso_segment(skb, 0);
kfree_skb(skb);
if (IS_ERR(segs))
return PTR_ERR(segs);
if (segs == NULL)
return -EINVAL;
do {
struct sk_buff *nskb = segs->next;
int err;
skb_mark_not_on_list(segs);
err = xfrm_output2(net, sk, segs);
if (unlikely(err)) {
kfree_skb_list(nskb);
return err;
}
segs = nskb;
} while (segs);Given that so much code does the same skb surgery, this seems like it would be a good opportunity for actually adding the right helper / abstraction around this. If that sounds good to you, I'll send a commit adding something like the below, along with fixing up a couple of the more straight-forward existing places to use the new helper:
#define skb_walk_null_list_safe(first, skb, next) \
for (skb = first, next = skb->next; skb; \
skb = next, next = skb ? skb->next : NULL)Does this sound good to you? If so, would you like this as lead-up commits to WireGuard, or just a new independent series all together?
Regards,
Jason
David Miller
maintainerFrom: "Jason A. Donenfeld" <[email protected]>
Date: Fri, 29 Nov 2019 04:32:05 +0100
Quoted 4 lines
I'm not a huge fan of doing manual skb surgery either. The annoying thing here is that
skb_gso_segmentreturns a list of skbs that's terminated by the last one's next pointer being NULL. I assume it's this way so that the GSO code doesn't have to pass a head around.
Sorry, I missed that this was processing a GSO list which doesn't use double linked list semantics.
So ignore my feedback on this one :-)
Jason A. Donenfeld
maintainerQuoted 9 lines · David Miller
From: "Jason A. Donenfeld" <[email protected]>
Date: Fri, 29 Nov 2019 04:32:05 +0100Quoted 4 lines
I'm not a huge fan of doing manual skb surgery either. The annoying thing here is that
skb_gso_segmentreturns a list of skbs that's terminated by the last one's next pointer being NULL. I assume it's this way so that the GSO code doesn't have to pass a head around.
Quoted 4 lines
Sorry, I missed that this was processing a GSO list which doesn't use double linked list semantics.
So ignore my feedback on this one :-)
Okay, no problem. I'll submit some global tree-wide cleanup patches in the way of a helper macro sometime after all the wireguard dust settles, then, and we can assess that separately.