diff --git a/kernel/linux/.gitignore b/kernel/linux/.gitignore index 364d0659e..d0efb06dc 100644 --- a/kernel/linux/.gitignore +++ b/kernel/linux/.gitignore @@ -7,3 +7,4 @@ modules.order Module.symvers /out/ +/src/compat/generated/ diff --git a/kernel/linux/Makefile b/kernel/linux/Makefile index 61a4e0f61..9e18d3e7c 100644 --- a/kernel/linux/Makefile +++ b/kernel/linux/Makefile @@ -8,13 +8,20 @@ KDIR ?= /lib/modules/$(shell uname -r)/build SRC := $(CURDIR)/src +AUTOCONF := $(SRC)/compat/generated/autoconf.h all: module -module: +# The probes are cheap, so always rerun them; probe.sh leaves the header +# untouched (and so doesn't trigger a rebuild) when the results don't change. +probe: + scripts/probe.sh $(KDIR) $(AUTOCONF) + +module: probe $(MAKE) -C $(KDIR) M=$(SRC) TSWG_DEBUG=$(TSWG_DEBUG) modules clean: $(MAKE) -C $(KDIR) M=$(SRC) clean + rm -rf $(SRC)/compat/generated -.PHONY: all module clean +.PHONY: all probe module clean diff --git a/kernel/linux/scripts/probe.sh b/kernel/linux/scripts/probe.sh new file mode 100755 index 000000000..3af1627ea --- /dev/null +++ b/kernel/linux/scripts/probe.sh @@ -0,0 +1,50 @@ +#!/bin/sh +# SPDX-License-Identifier: GPL-2.0 +# +# probe.sh compiles each src/compat/probes/HAVE_*.c against the kernel build +# directory KDIR and writes a header defining the HAVE_* macros whose probes +# compiled. It only rewrites the output file when its contents change, so +# that unchanged probe results don't force a rebuild. +# +# usage: probe.sh KDIR OUTPUT_HEADER + +set -eu + +if [ $# -ne 2 ]; then + echo "usage: $0 KDIR OUTPUT_HEADER" >&2 + exit 2 +fi +kdir=$1 +out=$2 +top=$(cd "$(dirname "$0")/.." && pwd) +probes=$top/src/compat/probes + +tmp=$(mktemp -d) +trap 'rm -rf "$tmp"' EXIT +cp "$probes"/HAVE_*.c "$tmp"/ +printf 'obj-m :=\nccflags-y := -Werror\n' > "$tmp/Kbuild" + +# Unset MAKEFLAGS so a parent make's jobserver and flags don't leak into the +# per-probe builds. +for c in "$tmp"/HAVE_*.c; do + basename "$c" .c +done | MAKEFLAGS= xargs -P "$(nproc)" -I{} sh -c \ + 'make -s -C "$1" M="$2" "$3.o" >"$2/$3.log" 2>&1 || true' \ + sh "$kdir" "$tmp" {} + +{ + echo "/* Generated by scripts/probe.sh for $kdir. Do not edit. */" + for c in "$tmp"/HAVE_*.c; do + name=$(basename "$c" .c) + if [ -f "$tmp/$name.o" ]; then + echo "#define $name 1" + else + echo "/* #undef $name */" + fi + done +} > "$tmp/autoconf.h" + +mkdir -p "$(dirname "$out")" +if ! cmp -s "$tmp/autoconf.h" "$out"; then + cp "$tmp/autoconf.h" "$out" +fi diff --git a/kernel/linux/src/Kbuild b/kernel/linux/src/Kbuild index caffffb25..c712f266c 100644 --- a/kernel/linux/src/Kbuild +++ b/kernel/linux/src/Kbuild @@ -2,6 +2,7 @@ ccflags-y := -D'pr_fmt(fmt)=KBUILD_MODNAME ": " fmt' ccflags-y += -I$(src)/include +ccflags-y += -include $(src)/compat/compat.h ccflags-$(TSWG_DEBUG) += -DDEBUG tswg-y := main.o tswg-y += noise.o diff --git a/kernel/linux/src/compat/compat.h b/kernel/linux/src/compat/compat.h new file mode 100644 index 000000000..ad7b4618c --- /dev/null +++ b/kernel/linux/src/compat/compat.h @@ -0,0 +1,139 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Compatibility shims that let the fork, which tracks the newest upstream + * WireGuard, build against older kernels. Kbuild force-includes this file + * before every source file. + * + * Each shim is keyed on a HAVE_* macro from generated/autoconf.h, which + * scripts/probe.sh produces by compile-testing src/compat/probes/HAVE_*.c + * against the target kernel. Shims that wrap a kernel function under its + * own name include the kernel header first, so the macro doesn't rewrite + * the kernel's own declaration. + */ + +#ifndef _TSWG_COMPAT_H +#define _TSWG_COMPAT_H + +#include "generated/autoconf.h" + +#include +#include + +#ifndef HAVE_UDP_GET_LEN_SHORT +#include +static inline unsigned int udp_get_len_short(const struct udphdr *uh) +{ + return ntohs(uh->len); +} +#endif + +#if !defined(HAVE_SETUP_UDP_TUNNEL_SOCK_SK) || \ + !defined(HAVE_UDP_TUNNEL_SOCK_RELEASE_SK) || \ + !defined(HAVE_UDP_TUNNEL_XMIT_IPCB_FLAGS) || \ + !defined(HAVE_UDP_TUNNEL6_XMIT_IPCB_FLAGS) +#include +#endif + +#ifndef HAVE_SETUP_UDP_TUNNEL_SOCK_SK +#define setup_udp_tunnel_sock(net, sk, cfg) \ + setup_udp_tunnel_sock(net, (sk)->sk_socket, cfg) +#endif + +#ifndef HAVE_UDP_TUNNEL_SOCK_RELEASE_SK +#define udp_tunnel_sock_release(sk) udp_tunnel_sock_release((sk)->sk_socket) +#endif + +/* The fork always passes 0 for the (ip|ip6)cb_flags argument. */ +#ifndef HAVE_UDP_TUNNEL_XMIT_IPCB_FLAGS +#define udp_tunnel_xmit_skb(rt, sk, skb, src, dst, tos, ttl, df, sport, \ + dport, xnet, nocheck, ipcb_flags) \ + udp_tunnel_xmit_skb(rt, sk, skb, src, dst, tos, ttl, df, sport, \ + dport, xnet, nocheck) +#endif + +#ifndef HAVE_UDP_TUNNEL6_XMIT_IPCB_FLAGS +#define udp_tunnel6_xmit_skb(dst, sk, skb, dev, saddr, daddr, prio, ttl, \ + label, sport, dport, nocheck, ip6cb_flags) \ + udp_tunnel6_xmit_skb(dst, sk, skb, dev, saddr, daddr, prio, ttl, \ + label, sport, dport, nocheck) +#endif + +#ifndef HAVE_BLAKE2S_CTX +#include +#define blake2s_ctx blake2s_state +static inline void tswg_blake2s(const u8 *key, size_t keylen, + const u8 *in, size_t inlen, + u8 *out, size_t outlen) +{ + blake2s(out, in, key, outlen, inlen, keylen); +} +#define blake2s tswg_blake2s +#endif + +#ifndef HAVE_WQ_PERCPU +/* Before WQ_PERCPU existed, workqueues without WQ_UNBOUND were per-CPU. */ +#define WQ_PERCPU 0 +#endif + +#ifndef HAVE_NETIF_THREADED_ENABLE +#include +/* + * Older upstream WireGuard didn't enable threaded NAPI, so neither do we on + * kernels that predate netif_threaded_enable(). + */ +static inline void netif_threaded_enable(struct net_device *dev) +{ +} +#endif + +#ifndef HAVE_RTNL_NEWLINK_PARAMS +#include +/* + * A minimal stand-in so wg_newlink can keep its upstream signature; see the + * old-style wrapper in device.c. + */ +struct rtnl_newlink_params { + struct net *src_net; +}; +#endif + +#ifndef HAVE_RTNL_NEWLINK_LINK_NET +static inline struct net *rtnl_newlink_link_net(struct rtnl_newlink_params *p) +{ + return p->src_net; +} +#endif + +#ifndef HAVE_SOCKADDR_INET +#include +struct sockaddr_inet { + unsigned short sa_family; + char sa_data[sizeof(struct sockaddr_in6) - + sizeof(unsigned short)]; +}; +#endif + +#include +#ifndef kmalloc_obj +#define kmalloc_obj(P) kmalloc(sizeof(P), GFP_KERNEL) +#endif +#ifndef kzalloc_obj +#define kzalloc_obj(P) kzalloc(sizeof(P), GFP_KERNEL) +#endif +#ifndef kzalloc_objs +#define kzalloc_objs(P, COUNT) kcalloc(COUNT, sizeof(P), GFP_KERNEL) +#endif +#ifndef kvmalloc_obj +#define kvmalloc_obj(P) kvmalloc(sizeof(P), GFP_KERNEL) +#endif +#ifndef kvzalloc_objs +#define kvzalloc_objs(P, COUNT) kvcalloc(COUNT, sizeof(P), GFP_KERNEL) +#endif + +#include +#ifndef timer_container_of +#define timer_container_of(var, callback_timer, timer_fieldname) \ + from_timer(var, callback_timer, timer_fieldname) +#endif + +#endif /* _TSWG_COMPAT_H */ diff --git a/kernel/linux/src/compat/probes/HAVE_BLAKE2S_CTX.c b/kernel/linux/src/compat/probes/HAVE_BLAKE2S_CTX.c new file mode 100644 index 000000000..f3d3a5257 --- /dev/null +++ b/kernel/linux/src/compat/probes/HAVE_BLAKE2S_CTX.c @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: GPL-2.0 +// Probe for struct blake2s_ctx and the (key, in, out) blake2s() argument order. +#include + +static void __maybe_unused probe(const u8 *in, u8 *out) +{ + struct blake2s_ctx ctx; + + blake2s_init(&ctx, BLAKE2S_HASH_SIZE); + blake2s(NULL, 0, in, 1, out, BLAKE2S_HASH_SIZE); +} diff --git a/kernel/linux/src/compat/probes/HAVE_NETDEV_LLTX.c b/kernel/linux/src/compat/probes/HAVE_NETDEV_LLTX.c new file mode 100644 index 000000000..fe2de2936 --- /dev/null +++ b/kernel/linux/src/compat/probes/HAVE_NETDEV_LLTX.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 +// Probe for the net_device lltx bit. +#include + +static void __maybe_unused probe(struct net_device *dev) +{ + dev->lltx = true; +} diff --git a/kernel/linux/src/compat/probes/HAVE_NETIF_THREADED_ENABLE.c b/kernel/linux/src/compat/probes/HAVE_NETIF_THREADED_ENABLE.c new file mode 100644 index 000000000..faf7cc0b1 --- /dev/null +++ b/kernel/linux/src/compat/probes/HAVE_NETIF_THREADED_ENABLE.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 +// Probe for netif_threaded_enable(). +#include + +static void __maybe_unused probe(struct net_device *dev) +{ + netif_threaded_enable(dev); +} diff --git a/kernel/linux/src/compat/probes/HAVE_PERNET_EXIT_RTNL.c b/kernel/linux/src/compat/probes/HAVE_PERNET_EXIT_RTNL.c new file mode 100644 index 000000000..cc7175268 --- /dev/null +++ b/kernel/linux/src/compat/probes/HAVE_PERNET_EXIT_RTNL.c @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: GPL-2.0 +// Probe for pernet_operations.exit_rtnl. +#include + +static void probe_exit_rtnl(struct net *net, struct list_head *dev_kill_list) +{ +} + +static struct pernet_operations probe_ops __maybe_unused = { + .exit_rtnl = probe_exit_rtnl, +}; diff --git a/kernel/linux/src/compat/probes/HAVE_RTNL_NEWLINK_LINK_NET.c b/kernel/linux/src/compat/probes/HAVE_RTNL_NEWLINK_LINK_NET.c new file mode 100644 index 000000000..72e72db0c --- /dev/null +++ b/kernel/linux/src/compat/probes/HAVE_RTNL_NEWLINK_LINK_NET.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 +// Probe for rtnl_newlink_link_net(). +#include + +static __maybe_unused struct net *probe(struct rtnl_newlink_params *params) +{ + return rtnl_newlink_link_net(params); +} diff --git a/kernel/linux/src/compat/probes/HAVE_RTNL_NEWLINK_PARAMS.c b/kernel/linux/src/compat/probes/HAVE_RTNL_NEWLINK_PARAMS.c new file mode 100644 index 000000000..5542c264b --- /dev/null +++ b/kernel/linux/src/compat/probes/HAVE_RTNL_NEWLINK_PARAMS.c @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 +// Probe for struct rtnl_newlink_params in rtnl_link_ops.newlink. +#include + +static int probe_newlink(struct net_device *dev, struct rtnl_newlink_params *params, + struct netlink_ext_ack *extack) +{ + return 0; +} + +static struct rtnl_link_ops probe_ops __maybe_unused = { + .newlink = probe_newlink, +}; diff --git a/kernel/linux/src/compat/probes/HAVE_SETUP_UDP_TUNNEL_SOCK_SK.c b/kernel/linux/src/compat/probes/HAVE_SETUP_UDP_TUNNEL_SOCK_SK.c new file mode 100644 index 000000000..9c97a977c --- /dev/null +++ b/kernel/linux/src/compat/probes/HAVE_SETUP_UDP_TUNNEL_SOCK_SK.c @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: GPL-2.0 +// Probe for setup_udp_tunnel_sock() taking a struct sock (7.4). +#include + +static void __maybe_unused probe(struct net *net, struct sock *sk, + struct udp_tunnel_sock_cfg *cfg) +{ + setup_udp_tunnel_sock(net, sk, cfg); +} diff --git a/kernel/linux/src/compat/probes/HAVE_SOCKADDR_INET.c b/kernel/linux/src/compat/probes/HAVE_SOCKADDR_INET.c new file mode 100644 index 000000000..4cad282f9 --- /dev/null +++ b/kernel/linux/src/compat/probes/HAVE_SOCKADDR_INET.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 +// Probe for struct sockaddr_inet. +#include + +static unsigned short __maybe_unused probe(struct sockaddr_inet *sa) +{ + return sa->sa_family; +} diff --git a/kernel/linux/src/compat/probes/HAVE_UDP_GET_LEN_SHORT.c b/kernel/linux/src/compat/probes/HAVE_UDP_GET_LEN_SHORT.c new file mode 100644 index 000000000..e0f683fb9 --- /dev/null +++ b/kernel/linux/src/compat/probes/HAVE_UDP_GET_LEN_SHORT.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 +// Probe for udp_get_len_short() (7.4). +#include + +static unsigned int __maybe_unused probe(const struct udphdr *uh) +{ + return udp_get_len_short(uh); +} diff --git a/kernel/linux/src/compat/probes/HAVE_UDP_TUNNEL6_XMIT_IPCB_FLAGS.c b/kernel/linux/src/compat/probes/HAVE_UDP_TUNNEL6_XMIT_IPCB_FLAGS.c new file mode 100644 index 000000000..a2abce47c --- /dev/null +++ b/kernel/linux/src/compat/probes/HAVE_UDP_TUNNEL6_XMIT_IPCB_FLAGS.c @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: GPL-2.0 +// Probe for the ip6cb_flags argument to udp_tunnel6_xmit_skb(). +#include + +static void __maybe_unused probe(struct dst_entry *dst, struct sock *sk, + struct sk_buff *skb, struct net_device *dev, + struct in6_addr *a) +{ + udp_tunnel6_xmit_skb(dst, sk, skb, dev, a, a, 0, 0, 0, 0, 0, false, 0); +} diff --git a/kernel/linux/src/compat/probes/HAVE_UDP_TUNNEL_SOCK_RELEASE_SK.c b/kernel/linux/src/compat/probes/HAVE_UDP_TUNNEL_SOCK_RELEASE_SK.c new file mode 100644 index 000000000..44facd2f8 --- /dev/null +++ b/kernel/linux/src/compat/probes/HAVE_UDP_TUNNEL_SOCK_RELEASE_SK.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 +// Probe for udp_tunnel_sock_release() taking a struct sock (7.4). +#include + +static void __maybe_unused probe(struct sock *sk) +{ + udp_tunnel_sock_release(sk); +} diff --git a/kernel/linux/src/compat/probes/HAVE_UDP_TUNNEL_XMIT_IPCB_FLAGS.c b/kernel/linux/src/compat/probes/HAVE_UDP_TUNNEL_XMIT_IPCB_FLAGS.c new file mode 100644 index 000000000..8ebbe8f57 --- /dev/null +++ b/kernel/linux/src/compat/probes/HAVE_UDP_TUNNEL_XMIT_IPCB_FLAGS.c @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: GPL-2.0 +// Probe for the ipcb_flags argument to udp_tunnel_xmit_skb(). +#include + +static void __maybe_unused probe(struct rtable *rt, struct sock *sk, + struct sk_buff *skb) +{ + udp_tunnel_xmit_skb(rt, sk, skb, 0, 0, 0, 0, 0, 0, 0, false, false, 0); +} diff --git a/kernel/linux/src/compat/probes/HAVE_WQ_PERCPU.c b/kernel/linux/src/compat/probes/HAVE_WQ_PERCPU.c new file mode 100644 index 000000000..ef8d717ec --- /dev/null +++ b/kernel/linux/src/compat/probes/HAVE_WQ_PERCPU.c @@ -0,0 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0 +// Probe for the WQ_PERCPU workqueue flag. +#include + +static int __maybe_unused probe(void) +{ + return WQ_PERCPU; +} diff --git a/kernel/linux/src/device.c b/kernel/linux/src/device.c index 67b07ee2d..ff291382b 100644 --- a/kernel/linux/src/device.c +++ b/kernel/linux/src/device.c @@ -237,6 +237,8 @@ static const struct net_device_ops netdev_ops = { .ndo_open = wg_open, .ndo_stop = wg_stop, .ndo_start_xmit = wg_xmit, + /* TSWG_COMPAT: older kernels don't fold tstats in dev_get_stats(). */ + .ndo_get_stats64 = dev_get_tstats64, }; static void wg_destruct(struct net_device *dev) @@ -289,7 +291,11 @@ static void wg_setup(struct net_device *dev) dev->type = ARPHRD_NONE; dev->flags = IFF_POINTOPOINT | IFF_NOARP; dev->priv_flags |= IFF_NO_QUEUE; +#ifdef HAVE_NETDEV_LLTX /* TSWG_COMPAT */ dev->lltx = true; +#else + dev->features |= NETIF_F_LLTX; +#endif dev->features |= WG_NETDEV_FEATURES; dev->hw_features |= WG_NETDEV_FEATURES; dev->hw_enc_features |= WG_NETDEV_FEATURES; @@ -404,6 +410,18 @@ err_free_peer_hashtable: return ret; } +#ifndef HAVE_RTNL_NEWLINK_PARAMS /* TSWG_COMPAT */ +static int wg_newlink_compat(struct net *src_net, struct net_device *dev, + struct nlattr *tb[], struct nlattr *data[], + struct netlink_ext_ack *extack) +{ + struct rtnl_newlink_params params = { .src_net = src_net }; + + return wg_newlink(dev, ¶ms, extack); +} +#define wg_newlink wg_newlink_compat +#endif + static struct rtnl_link_ops link_ops __read_mostly = { .kind = KBUILD_MODNAME, .priv_size = sizeof(struct wg_device), @@ -430,9 +448,22 @@ static void __net_exit wg_netns_exit_rtnl(struct net *net, struct list_head *dev } } +#ifdef HAVE_PERNET_EXIT_RTNL /* TSWG_COMPAT */ static struct pernet_operations pernet_ops __read_mostly = { .exit_rtnl = wg_netns_exit_rtnl }; +#else +static void wg_netns_pre_exit(struct net *net) +{ + rtnl_lock(); + wg_netns_exit_rtnl(net, NULL); + rtnl_unlock(); +} + +static struct pernet_operations pernet_ops __read_mostly = { + .pre_exit = wg_netns_pre_exit +}; +#endif int __init wg_device_init(void) {