Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LINUX_VERSION_CODE inspired cleanup #30

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 0 additions & 62 deletions src/wg.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,65 +32,3 @@ struct wg_message_handshake_response {
u8 encrypted_nothing[wg_noise_encrypted_len(0)];
struct wg_message_macs macs;
};

struct wg_message_handshake_cookie {
struct wg_message_header header;
__le32 receiver_index;
u8 nonce[WG_COOKIE_NONCE_LEN];
u8 encrypted_cookie[wg_noise_encrypted_len(WG_COOKIE_LEN)];
};

#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0) && LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) && (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0) || LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 320))
#include <linux/completion.h>
#include <linux/random.h>
#include <linux/errno.h>
struct rng_initializer {
struct completion done;
struct random_ready_callback cb;
};
static inline void rng_initialized_callback(struct random_ready_callback *cb)
{
complete(&container_of(cb, struct rng_initializer, cb)->done);
}
static inline int wait_for_random_bytes(void)
{
static bool rng_is_initialized = false;
int ret;
if (unlikely(!rng_is_initialized)) {
struct rng_initializer rng = {
.done = COMPLETION_INITIALIZER(rng.done),
.cb = { .owner = THIS_MODULE, .func = rng_initialized_callback }
};
ret = add_random_ready_callback(&rng.cb);
if (!ret) {
ret = wait_for_completion_interruptible(&rng.done);
if (ret) {
del_random_ready_callback(&rng.cb);
return ret;
}
} else if (ret != -EALREADY)
return ret;
rng_is_initialized = true;
}
return 0;
}
#elif LINUX_VERSION_CODE < KERNEL_VERSION(4, 2, 0)
/* This is a disaster. Without this API, we really have no way of
* knowing if it's initialized. We just return that it has and hope
* for the best... */
static inline int wait_for_random_bytes(void)
{
return 0;
}
#endif

#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 13, 0) && (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 10, 0) || LINUX_VERSION_CODE < KERNEL_VERSION(4, 9, 320))
static inline int get_random_bytes_wait(void *buf, int nbytes)
{
int ret = wait_for_random_bytes();
if (unlikely(ret))
return ret;
get_random_bytes(buf, nbytes);
return 0;
}
#endif
38 changes: 12 additions & 26 deletions src/xt_WGOBFS_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
#define WG_MIN_LEN 32
#define MIN_RND_LEN 4

#if LINUX_VERSION_CODE < KERNEL_VERSION(3,7,0)
#define xt_action_param xt_target_param
#endif

#if LINUX_VERSION_CODE < KERNEL_VERSION(5,3,0)
static inline int
skb_ensure_writable(struct sk_buff *skb, unsigned int write_len)
{
return !skb_make_writable(skb, write_len);
}
#endif

enum chacha_output_lengths {
MAX_RND_LEN = 32,
MAX_RND_WORDS = MAX_RND_LEN / sizeof(u32),
Expand Down Expand Up @@ -164,11 +176,7 @@ static int prepare_skb_for_insert(struct sk_buff *skb, int ntail)
return -1;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,3,0)
if (unlikely(skb_ensure_writable(skb, skb->len)))
#else
if (unlikely(!skb_make_writable(skb, skb->len)))
#endif
return -1;

skb_put(skb, ntail);
Expand Down Expand Up @@ -312,11 +320,7 @@ static unsigned int xt_unobfs(struct sk_buff *skb,
int data_len;
int rnd_len;

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,3,0)
if (unlikely(skb_ensure_writable(skb, skb->len)))
#else
if (unlikely(!skb_make_writable(skb, skb->len)))
#endif
return NF_DROP;

udph = udp_hdr(skb);
Expand Down Expand Up @@ -349,13 +353,8 @@ static unsigned int xt_unobfs(struct sk_buff *skb,
return XT_CONTINUE;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,7,0)
static unsigned int
xt_wg_obfs_target(struct sk_buff *skb, const struct xt_action_param *par)
#else
static unsigned int
xt_wg_obfs_target(struct sk_buff *skb, const struct xt_target_param *par)
#endif
{
const struct xt_wg_obfs_info *info = par->targinfo;
struct iphdr *iph;
Expand All @@ -373,7 +372,6 @@ xt_wg_obfs_target(struct sk_buff *skb, const struct xt_target_param *par)
return XT_CONTINUE;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35)
static int xt_wg_obfs_checkentry(const struct xt_tgchk_param *par)
{
if (strcmp(par->table, "mangle")) {
Expand All @@ -384,18 +382,6 @@ static int xt_wg_obfs_checkentry(const struct xt_tgchk_param *par)

return 0;
}
#else
static bool xt_wg_obfs_checkentry(const struct xt_tgchk_param *par)
{
if (strcmp(par->table, "mangle")) {
printk(KERN_WARNING
"WGOBFS: can only be called from mangle table\n");
return false;
}

return true;
}
#endif

static struct xt_target xt_wg_obfs = {
.name = "WGOBFS",
Expand Down