From 7adeb6e88196a594ec3d36ffc1c658340ffcd7bc Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Sun, 20 Apr 2014 05:29:11 +0200 Subject: Make ctx global --- src/async.c | 36 +- src/async.h | 6 +- src/buffer.h | 28 +- src/capabilities.c | 30 +- src/config.c | 180 ++++----- src/config.h | 36 +- src/config.y | 43 +- src/fastd.c | 455 +++++++++++----------- src/fastd.h | 98 ++--- src/handshake.c | 78 ++-- src/handshake.h | 26 +- src/log.c | 28 +- src/log.h | 38 +- src/method.h | 24 +- src/methods/cipher_test/cipher_test.c | 42 +- src/methods/common.c | 18 +- src/methods/common.h | 36 +- src/methods/composed_gmac/composed_gmac.c | 42 +- src/methods/generic_gmac/generic_gmac.c | 44 +-- src/methods/generic_poly1305/generic_poly1305.c | 58 +-- src/methods/null/null.c | 20 +- src/methods/xsalsa20_poly1305/xsalsa20_poly1305.c | 76 ++-- src/options.c | 110 +++--- src/peer.c | 284 +++++++------- src/peer.h | 58 +-- src/peer_hashtable.c | 44 +-- src/peer_hashtable.h | 10 +- src/poll.c | 186 ++++----- src/poll.h | 16 +- src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c | 94 ++--- src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.h | 26 +- src/protocols/ec25519_fhmqvc/handshake.c | 236 +++++------ src/protocols/ec25519_fhmqvc/handshake.h | 8 +- src/protocols/ec25519_fhmqvc/state.c | 56 +-- src/protocols/ec25519_fhmqvc/util.c | 8 +- src/random.c | 6 +- src/receive.c | 66 ++-- src/resolve.c | 24 +- src/send.c | 46 +-- src/shell.c | 28 +- src/shell.h | 4 +- src/socket.c | 86 ++-- src/tuntap.c | 172 ++++---- 43 files changed, 1506 insertions(+), 1504 deletions(-) diff --git a/src/async.c b/src/async.c index 0c37842..144a82c 100644 --- a/src/async.c +++ b/src/async.c @@ -28,26 +28,26 @@ #include "fastd.h" -void fastd_async_init(fastd_context_t *ctx) { - fastd_open_pipe(ctx, &ctx->async_rfd, &ctx->async_wfd); +void fastd_async_init(void) { + fastd_open_pipe(&ctx.async_rfd, &ctx.async_wfd); } -static void handle_resolve_return(fastd_context_t *ctx) { +static void handle_resolve_return(void) { fastd_async_resolve_return_t resolve_return; - while (read(ctx->async_rfd, &resolve_return, sizeof(resolve_return)) < 0) { + while (read(ctx.async_rfd, &resolve_return, sizeof(resolve_return)) < 0) { if (errno != EINTR) - exit_errno(ctx, "handle_resolve_return: read"); + exit_errno("handle_resolve_return: read"); } fastd_peer_address_t addresses[resolve_return.n_addr]; - while (read(ctx->async_rfd, &addresses, sizeof(addresses)) < 0) { + while (read(ctx.async_rfd, &addresses, sizeof(addresses)) < 0) { if (errno != EINTR) - exit_errno(ctx, "handle_resolve_return: read"); + exit_errno("handle_resolve_return: read"); } size_t i; - for (i = 0; i < VECTOR_LEN(ctx->peers); i++) { - fastd_peer_t *peer = VECTOR_INDEX(ctx->peers, i); + for (i = 0; i < VECTOR_LEN(ctx.peers); i++) { + fastd_peer_t *peer = VECTOR_INDEX(ctx.peers, i); if (!peer->config) continue; @@ -61,7 +61,7 @@ static void handle_resolve_return(fastd_context_t *ctx) { if (!remote) continue; - fastd_peer_handle_resolve(ctx, peer, remote, resolve_return.n_addr, addresses); + fastd_peer_handle_resolve(peer, remote, resolve_return.n_addr, addresses); break; } @@ -69,30 +69,30 @@ static void handle_resolve_return(fastd_context_t *ctx) { fastd_remote_unref(resolve_return.remote); } -void fastd_async_handle(fastd_context_t *ctx) { +void fastd_async_handle(void) { fastd_async_type_t type; - while (read(ctx->async_rfd, &type, sizeof(type)) < 0) { + while (read(ctx.async_rfd, &type, sizeof(type)) < 0) { if (errno != EINTR) - exit_errno(ctx, "fastd_async_handle: read"); + exit_errno("fastd_async_handle: read"); } switch (type) { case ASYNC_TYPE_RESOLVE_RETURN: - handle_resolve_return(ctx); + handle_resolve_return(); break; default: - exit_bug(ctx, "fastd_async_handle: unknown type"); + exit_bug("fastd_async_handle: unknown type"); } } -void fastd_async_enqueue(fastd_context_t *ctx, fastd_async_type_t type, const void *data, size_t len) { +void fastd_async_enqueue(fastd_async_type_t type, const void *data, size_t len) { struct iovec vec[2] = { { .iov_base = &type, .iov_len = sizeof(type) }, { .iov_base = (void *)data, .iov_len = len }, }; - if (writev(ctx->async_wfd, vec, 2) < 0) - pr_error_errno(ctx, "fastd_async_enqueue"); + if (writev(ctx.async_wfd, vec, 2) < 0) + pr_error_errno("fastd_async_enqueue"); } diff --git a/src/async.h b/src/async.h index a983ee9..7e1921c 100644 --- a/src/async.h +++ b/src/async.h @@ -37,6 +37,6 @@ struct fastd_async_resolve_return { }; -void fastd_async_init(fastd_context_t *ctx); -void fastd_async_handle(fastd_context_t *ctx); -void fastd_async_enqueue(fastd_context_t *ctx, fastd_async_type_t type, const void *data, size_t len); +void fastd_async_init(void); +void fastd_async_handle(void); +void fastd_async_enqueue(fastd_async_type_t type, const void *data, size_t len); diff --git a/src/buffer.h b/src/buffer.h index d403785..489e509 100644 --- a/src/buffer.h +++ b/src/buffer.h @@ -38,18 +38,18 @@ struct fastd_buffer { }; -static inline fastd_buffer_t fastd_buffer_alloc(const fastd_context_t *ctx, size_t len, size_t head_space, size_t tail_space) { +static inline fastd_buffer_t fastd_buffer_alloc(const size_t len, size_t head_space, size_t tail_space) { size_t base_len = head_space+len+tail_space; void *ptr; int err = posix_memalign(&ptr, 16, base_len); if (err) - exit_error(ctx, "posix_memalign: %s", strerror(err)); + exit_error("posix_memalign: %s", strerror(err)); return (fastd_buffer_t){ .base = ptr, .base_len = base_len, .data = ptr+head_space, .len = len }; } -static inline fastd_buffer_t fastd_buffer_dup(const fastd_context_t *ctx, fastd_buffer_t buffer, size_t head_space, size_t tail_space) { - fastd_buffer_t new_buffer = fastd_buffer_alloc(ctx, buffer.len, head_space, tail_space); +static inline fastd_buffer_t fastd_buffer_dup(const fastd_buffer_t buffer, size_t head_space, size_t tail_space) { + fastd_buffer_t new_buffer = fastd_buffer_alloc(buffer.len, head_space, tail_space); memcpy(new_buffer.data, buffer.data, buffer.len); return new_buffer; } @@ -59,34 +59,34 @@ static inline void fastd_buffer_free(fastd_buffer_t buffer) { } -static inline void fastd_buffer_pull_head(const fastd_context_t *ctx, fastd_buffer_t *buffer, size_t len) { +static inline void fastd_buffer_pull_head(fastd_buffer_t *buffer, size_t len) { if (len > (size_t)(buffer->data - buffer->base)) - exit_bug(ctx, "tried to pull buffer across base"); + exit_bug("tried to pull buffer across base"); buffer->data -= len; buffer->len += len; } -static inline void fastd_buffer_pull_head_zero(const fastd_context_t *ctx, fastd_buffer_t *buffer, size_t len) { - fastd_buffer_pull_head(ctx, buffer, len); +static inline void fastd_buffer_pull_head_zero(fastd_buffer_t *buffer, size_t len) { + fastd_buffer_pull_head(buffer, len); memset(buffer->data, 0, len); } -static inline void fastd_buffer_pull_head_from(const fastd_context_t *ctx, fastd_buffer_t *buffer, const void *data, size_t len) { - fastd_buffer_pull_head(ctx, buffer, len); +static inline void fastd_buffer_pull_head_from(fastd_buffer_t *buffer, const void *data, size_t len) { + fastd_buffer_pull_head(buffer, len); memcpy(buffer->data, data, len); } -static inline void fastd_buffer_push_head(const fastd_context_t *ctx, fastd_buffer_t *buffer, size_t len) { +static inline void fastd_buffer_push_head(fastd_buffer_t *buffer, size_t len) { if (buffer->len < len) - exit_bug(ctx, "tried to push buffer across tail"); + exit_bug("tried to push buffer across tail"); buffer->data += len; buffer->len -= len; } -static inline void fastd_buffer_push_head_to(const fastd_context_t *ctx, fastd_buffer_t *buffer, void *data, size_t len) { +static inline void fastd_buffer_push_head_to(fastd_buffer_t *buffer, void *data, size_t len) { memcpy(data, buffer->data, len); - fastd_buffer_push_head(ctx, buffer, len); + fastd_buffer_push_head(buffer, len); } diff --git a/src/capabilities.c b/src/capabilities.c index 9c9d334..897ca3c 100644 --- a/src/capabilities.c +++ b/src/capabilities.c @@ -31,7 +31,7 @@ #include -static void try_cap(fastd_context_t *ctx, cap_value_t cap) { +static void try_cap(cap_value_t cap) { char *name = cap_to_name(cap); if (!name) return; @@ -42,51 +42,51 @@ static void try_cap(fastd_context_t *ctx, cap_value_t cap) { cap_flag_value_t val; if (cap_get_flag(caps, cap, CAP_EFFECTIVE, &val) < 0) { - pr_debug_errno(ctx, "cap_get_flag"); + pr_debug_errno("cap_get_flag"); goto end_free; } if (val == CAP_SET) goto end_free; - pr_verbose(ctx, "Trying to acquire %s", name); + pr_verbose("Trying to acquire %s", name); if (cap_set_flag(caps, CAP_EFFECTIVE, 1, &cap, CAP_SET) < 0) { - pr_debug_errno(ctx, "cap_set_flags"); + pr_debug_errno("cap_set_flags"); goto end_free; } if (cap_set_proc(caps) < 0) { - pr_debug_errno(ctx, "cap_set_proc"); + pr_debug_errno("cap_set_proc"); goto end_free; } - pr_verbose(ctx, "Acquired capability %s.", name); + pr_verbose("Acquired capability %s.", name); end_free: cap_free(caps); cap_free(name); } -void fastd_cap_init(fastd_context_t *ctx) { +void fastd_cap_init(void) { /* interface creation */ - try_cap(ctx, CAP_NET_ADMIN); + try_cap(CAP_NET_ADMIN); /* privileged binds */ - try_cap(ctx, CAP_NET_BIND_SERVICE); + try_cap(CAP_NET_BIND_SERVICE); /* for device binds */ - try_cap(ctx, CAP_NET_RAW); + try_cap(CAP_NET_RAW); } -void fastd_cap_drop(fastd_context_t *ctx) { +void fastd_cap_drop(void) { cap_t caps = cap_init(); if (cap_set_proc(caps) < 0) { - pr_debug_errno(ctx, "cap_set_proc"); + pr_debug_errno("cap_set_proc"); } else { - pr_verbose(ctx, "Dropped capabilities."); + pr_verbose("Dropped capabilities."); } cap_free(caps); @@ -96,10 +96,10 @@ void fastd_cap_drop(fastd_context_t *ctx) { #else /* WITH_CAPABILITIES */ -void fastd_cap_init(fastd_context_t *ctx UNUSED) { +void fastd_cap_init(void) { } -void fastd_cap_drop(fastd_context_t *ctx UNUSED) { +void fastd_cap_drop(void) { } #endif /* WITH_CAPABILITIES */ diff --git a/src/config.c b/src/config.c index fbddd7f..75c4709 100644 --- a/src/config.c +++ b/src/config.c @@ -84,19 +84,19 @@ static void default_config(void) { conf.macs = fastd_mac_config_alloc(); } -void fastd_config_protocol(fastd_context_t *ctx UNUSED, const char *name) { +void fastd_config_protocol(const char *name) { if (!strcmp(name, "ec25519-fhmqvc")) conf.protocol = &fastd_protocol_ec25519_fhmqvc; else - exit_error(ctx, "config error: protocol `%s' not supported", name); + exit_error("config error: protocol `%s' not supported", name); } -void fastd_config_method(fastd_context_t *ctx, const char *name) { +void fastd_config_method(const char *name) { fastd_string_stack_t **method; for (method = &conf.method_list; *method; method = &(*method)->next) { if (!strcmp((*method)->str, name)) { - pr_debug(ctx, "duplicate method name `%s', ignoring", name); + pr_debug("duplicate method name `%s', ignoring", name); return; } } @@ -104,20 +104,20 @@ void fastd_config_method(fastd_context_t *ctx, const char *name) { *method = fastd_string_stack_dup(name); } -void fastd_config_cipher(fastd_context_t *ctx, const char *name, const char *impl) { +void fastd_config_cipher(const char *name, const char *impl) { if (!fastd_cipher_config(conf.ciphers, name, impl)) - exit_error(ctx, "config error: implementation `%s' is not supported for cipher `%s' (or cipher `%s' is not supported)", impl, name, name); + exit_error("config error: implementation `%s' is not supported for cipher `%s' (or cipher `%s' is not supported)", impl, name, name); } -void fastd_config_mac(fastd_context_t *ctx, const char *name, const char *impl) { +void fastd_config_mac(const char *name, const char *impl) { if (!fastd_mac_config(conf.macs, name, impl)) - exit_error(ctx, "config error: implementation `%s' is not supported for MAC `%s' (or MAC `%s' is not supported)", impl, name, name); + exit_error("config error: implementation `%s' is not supported for MAC `%s' (or MAC `%s' is not supported)", impl, name, name); } -void fastd_config_bind_address(fastd_context_t *ctx UNUSED, const fastd_peer_address_t *address, const char *bindtodev, bool default_v4, bool default_v6) { +void fastd_config_bind_address(const fastd_peer_address_t *address, const char *bindtodev, bool default_v4, bool default_v6) { #ifndef USE_BINDTODEVICE if (bindtodev && !fastd_peer_address_is_v6_ll(address)) - exit_error(ctx, "config error: device bind configuration not supported on this system"); + exit_error("config error: device bind configuration not supported on this system"); #endif #ifndef USE_MULTIAF_BIND @@ -125,8 +125,8 @@ void fastd_config_bind_address(fastd_context_t *ctx UNUSED, const fastd_peer_add fastd_peer_address_t addr4 = { .in = { .sin_family = AF_INET, .sin_port = address->in.sin_port } }; fastd_peer_address_t addr6 = { .in6 = { .sin6_family = AF_INET6, .sin6_port = address->in.sin_port } }; - fastd_config_bind_address(ctx, &addr4, bindtodev, default_v4, default_v6); - fastd_config_bind_address(ctx, &addr6, bindtodev, default_v4, default_v6); + fastd_config_bind_address(&addr4, bindtodev, default_v4, default_v6); + fastd_config_bind_address(&addr6, bindtodev, default_v4, default_v6); return; } #endif @@ -148,7 +148,7 @@ void fastd_config_bind_address(fastd_context_t *ctx UNUSED, const fastd_peer_add conf.bind_addr_default_v6 = addr; } -void fastd_config_peer_group_push(fastd_context_t *ctx UNUSED, const char *name) { +void fastd_config_peer_group_push(const char *name) { fastd_peer_group_config_t *group = calloc(1, sizeof(fastd_peer_group_config_t)); group->name = strdup(name); group->max_connections = -1; @@ -161,7 +161,7 @@ void fastd_config_peer_group_push(fastd_context_t *ctx UNUSED, const char *name) conf.peer_group = group; } -void fastd_config_peer_group_pop(fastd_context_t *ctx UNUSED) { +void fastd_config_peer_group_pop(void) { conf.peer_group = conf.peer_group->parent; } @@ -190,7 +190,7 @@ static bool has_peer_group_peer_dirs(const fastd_peer_group_config_t *group) { return false; } -void fastd_config_add_log_file(fastd_context_t *ctx, const char *name, fastd_loglevel_t level) { +void fastd_config_add_log_file(const char *name, fastd_loglevel_t level) { char *name2 = strdup(name); char *name3 = strdup(name); @@ -215,12 +215,12 @@ void fastd_config_add_log_file(fastd_context_t *ctx, const char *name, fastd_log conf.log_files = file; if(chdir(oldcwd)) - pr_error(ctx, "can't chdir to `%s': %s", oldcwd, strerror(errno)); + pr_error("can't chdir to `%s': %s", oldcwd, strerror(errno)); free(logdir); } else { - pr_error(ctx, "change from directory `%s' to `%s' failed: %s", oldcwd, dir, strerror(errno)); + pr_error("change from directory `%s' to `%s' failed: %s", oldcwd, dir, strerror(errno)); } free(oldcwd); @@ -228,7 +228,7 @@ void fastd_config_add_log_file(fastd_context_t *ctx, const char *name, fastd_log free(name3); } -static void read_peer_dir(fastd_context_t *ctx, const char *dir) { +static void read_peer_dir(const char *dir) { DIR *dirh = opendir("."); if (dirh) { @@ -237,7 +237,7 @@ static void read_peer_dir(fastd_context_t *ctx, const char *dir) { struct dirent *result = readdir(dirh); if (!result) { if (errno) - pr_error_errno(ctx, "readdir"); + pr_error_errno("readdir"); break; } @@ -246,57 +246,57 @@ static void read_peer_dir(fastd_context_t *ctx, const char *dir) { continue; if (result->d_name[strlen(result->d_name)-1] == '~') { - pr_verbose(ctx, "ignoring file `%s' as it seems to be a backup file", result->d_name); + pr_verbose("ignoring file `%s' as it seems to be a backup file", result->d_name); continue; } struct stat statbuf; if (stat(result->d_name, &statbuf)) { - pr_warn(ctx, "ignoring file `%s': stat failed: %s", result->d_name, strerror(errno)); + pr_warn("ignoring file `%s': stat failed: %s", result->d_name, strerror(errno)); continue; } if ((statbuf.st_mode & S_IFMT) != S_IFREG) { - pr_info(ctx, "ignoring file `%s': no regular file", result->d_name); + pr_info("ignoring file `%s': no regular file", result->d_name); continue; } - fastd_peer_config_new(ctx); + fastd_peer_config_new(); conf.peers->name = strdup(result->d_name); conf.peers->config_source_dir = dir; - if (!fastd_read_config(ctx, result->d_name, true, 0)) { - pr_warn(ctx, "peer config `%s' will be ignored", result->d_name); - fastd_peer_config_delete(ctx); + if (!fastd_read_config(result->d_name, true, 0)) { + pr_warn("peer config `%s' will be ignored", result->d_name); + fastd_peer_config_delete(); } } if (closedir(dirh) < 0) - pr_error_errno(ctx, "closedir"); + pr_error_errno("closedir"); } else { - pr_error(ctx, "opendir for `%s' failed: %s", dir, strerror(errno)); + pr_error("opendir for `%s' failed: %s", dir, strerror(errno)); } } -static void read_peer_dirs(fastd_context_t *ctx) { +static void read_peer_dirs(void) { char *oldcwd = get_current_dir_name(); fastd_string_stack_t *dir; for (dir = conf.peer_group->peer_dirs; dir; dir = dir->next) { if (!chdir(dir->str)) - read_peer_dir(ctx, dir->str); + read_peer_dir(dir->str); else - pr_error(ctx, "change from directory `%s' to `%s' failed: %s", oldcwd, dir->str, strerror(errno)); + pr_error("change from directory `%s' to `%s' failed: %s", oldcwd, dir->str, strerror(errno)); } if (chdir(oldcwd)) - pr_error(ctx, "can't chdir to `%s': %s", oldcwd, strerror(errno)); + pr_error("can't chdir to `%s': %s", oldcwd, strerror(errno)); free(oldcwd); } -void fastd_add_peer_dir(fastd_context_t *ctx, const char *dir) { +void fastd_add_peer_dir(const char *dir) { char *oldcwd = get_current_dir_name(); if (!chdir(dir)) { @@ -305,18 +305,18 @@ void fastd_add_peer_dir(fastd_context_t *ctx, const char *dir) { free(newdir); if(chdir(oldcwd)) - pr_error(ctx, "can't chdir to `%s': %s", oldcwd, strerror(errno)); + pr_error("can't chdir to `%s': %s", oldcwd, strerror(errno)); } else { - pr_error(ctx, "change from directory `%s' to `%s' failed: %s", oldcwd, dir, strerror(errno)); + pr_error("change from directory `%s' to `%s' failed: %s", oldcwd, dir, strerror(errno)); } free(oldcwd); } -bool fastd_read_config(fastd_context_t *ctx, const char *filename, bool peer_config, int depth) { +bool fastd_read_config(const char *filename, bool peer_config, int depth) { if (depth >= MAX_CONFIG_DEPTH) - exit_error(ctx, "maximum config include depth exceeded"); + exit_error("maximum config include depth exceeded"); bool ret = true; char *oldcwd = get_current_dir_name(); @@ -335,7 +335,7 @@ bool fastd_read_config(fastd_context_t *ctx, const char *filename, bool peer_con else { file = fopen(filename, "r"); if (!file) { - pr_error(ctx, "can't open config file `%s': %s", filename, strerror(errno)); + pr_error("can't open config file `%s': %s", filename, strerror(errno)); ret = false; goto end_free; } @@ -348,7 +348,7 @@ bool fastd_read_config(fastd_context_t *ctx, const char *filename, bool peer_con dir = dirname(filename2); if (chdir(dir)) { - pr_error(ctx, "change from directory `%s' to `%s' failed", oldcwd, dir); + pr_error("change from directory `%s' to `%s' failed", oldcwd, dir); ret = false; goto end_free; } @@ -363,13 +363,13 @@ bool fastd_read_config(fastd_context_t *ctx, const char *filename, bool peer_con else token = conf.peer_group->parent ? START_PEER_GROUP_CONFIG : START_CONFIG; - int parse_ret = fastd_config_push_parse(ps, token, &token_val, &loc, ctx, filename, depth+1); + int parse_ret = fastd_config_push_parse(ps, token, &token_val, &loc, filename, depth+1); while(parse_ret == YYPUSH_MORE) { token = fastd_lex(&token_val, &loc, lex); if (token < 0) { - pr_error(ctx, "config error: %s at %s:%i:%i", token_val.error, filename, loc.first_line, loc.first_column); + pr_error("config error: %s at %s:%i:%i", token_val.error, filename, loc.first_line, loc.first_column); ret = false; goto end_free; } @@ -379,7 +379,7 @@ bool fastd_read_config(fastd_context_t *ctx, const char *filename, bool peer_con strings = token_val.str; } - parse_ret = fastd_config_push_parse(ps, token, &token_val, &loc, ctx, filename, depth+1); + parse_ret = fastd_config_push_parse(ps, token, &token_val, &loc, filename, depth+1); } if (parse_ret) @@ -392,7 +392,7 @@ bool fastd_read_config(fastd_context_t *ctx, const char *filename, bool peer_con fastd_config_pstate_delete(ps); if(chdir(oldcwd)) - pr_error(ctx, "can't chdir to `%s': %s", oldcwd, strerror(errno)); + pr_error("can't chdir to `%s': %s", oldcwd, strerror(errno)); free(filename2); free(oldcwd); @@ -403,7 +403,7 @@ bool fastd_read_config(fastd_context_t *ctx, const char *filename, bool peer_con return ret; } -static void assess_peers(fastd_context_t *ctx) { +static void assess_peers(void) { conf.has_floating = false; fastd_peer_config_t *peer; @@ -412,12 +412,12 @@ static void assess_peers(fastd_context_t *ctx) { conf.has_floating = true; if (peer->dynamic_float_deprecated) - pr_warn(ctx, "peer `%s' uses deprecated float syntax, please update your configuration", peer->name); + pr_warn("peer `%s' uses deprecated float syntax, please update your configuration", peer->name); } } -static void configure_user(fastd_context_t *ctx) { +static void configure_user(void) { conf.uid = getuid(); conf.gid = getgid(); @@ -433,10 +433,10 @@ static void configure_user(fastd_context_t *ctx) { } while(error == ERANGE); if (error) - exit_errno(ctx, "getpwnam_r"); + exit_errno("getpwnam_r"); if (!pwdr) - exit_error(ctx, "config error: unable to find user `%s'.", conf.user); + exit_error("config error: unable to find user `%s'.", conf.user); conf.uid = pwdr->pw_uid; conf.gid = pwdr->pw_gid; @@ -454,10 +454,10 @@ static void configure_user(fastd_context_t *ctx) { } while(error == ERANGE); if (error) - exit_errno(ctx, "getgrnam_r"); + exit_errno("getgrnam_r"); if (!grpr) - exit_error(ctx, "config error: unable to find group `%s'.", conf.group); + exit_error("config error: unable to find group `%s'.", conf.group); conf.gid = grpr->gr_gid; } @@ -469,7 +469,7 @@ static void configure_user(fastd_context_t *ctx) { conf.groups = calloc(ngroups, sizeof(gid_t)); if (getgrouplist(conf.user, conf.gid, conf.groups, &ngroups) < 0) - exit_errno(ctx, "getgrouplist"); + exit_errno("getgrouplist"); conf.n_groups = ngroups; } @@ -500,7 +500,7 @@ static void configure_method_parameters(void) { conf.min_decrypt_head_space = alignto(conf.min_decrypt_head_space, 16) + 8; } -static void configure_methods(fastd_context_t *ctx) { +static void configure_methods(void) { size_t n_methods = 0, i; fastd_string_stack_t *method_name; for (method_name = conf.method_list; method_name; method_name = method_name->next) @@ -511,7 +511,7 @@ static void configure_methods(fastd_context_t *ctx) { for (i = 0, method_name = conf.method_list; method_name; i++, method_name = method_name->next) { conf.methods[i].name = method_name->str; if (!fastd_method_create_by_name(method_name->str, &conf.methods[i].provider, &conf.methods[i].method)) - exit_error(ctx, "config error: method `%s' not supported", method_name->str); + exit_error("config error: method `%s' not supported", method_name->str); } configure_method_parameters(); @@ -526,81 +526,81 @@ static void destroy_methods(void) { free(conf.methods); } -void fastd_configure(fastd_context_t *ctx, int argc, char *const argv[]) { +void fastd_configure(int argc, char *const argv[]) { default_config(); - fastd_config_handle_options(ctx, argc, argv); + fastd_config_handle_options(argc, argv); if (!conf.log_stderr_level && !conf.log_syslog_level && !conf.log_files) conf.log_stderr_level = FASTD_DEFAULT_LOG_LEVEL; } -static void config_check_base(fastd_context_t *ctx) { +static void config_check_base(void) { if (conf.ifname) { if (strchr(conf.ifname, '/')) - exit_error(ctx, "config error: invalid interface name"); + exit_error("config error: invalid interface name"); } if (conf.mode == MODE_TUN) { if (conf.peers->next) - exit_error(ctx, "config error: in TUN mode exactly one peer must be configured"); + exit_error("config error: in TUN mode exactly one peer must be configured"); if (conf.peer_group->children) - exit_error(ctx, "config error: in TUN mode peer groups can't be used"); + exit_error("config error: in TUN mode peer groups can't be used"); if (has_peer_group_peer_dirs(conf.peer_group)) - exit_error(ctx, "config error: in TUN mode peer directories can't be used"); + exit_error("config error: in TUN mode peer directories can't be used"); } #ifndef USE_PMTU if (conf.pmtu.set) - exit_error(ctx, "config error: setting pmtu is not supported on this system"); + exit_error("config error: setting pmtu is not supported on this system"); #endif #ifndef USE_PACKET_MARK if (conf.packet_mark) - exit_error(ctx, "config error: setting a packet mark is not supported on this system"); + exit_error("config error: setting a packet mark is not supported on this system"); #endif } -void fastd_config_check(fastd_context_t *ctx) { - config_check_base(ctx); +void fastd_config_check(void) { + config_check_base(); if (conf.mode == MODE_TUN) { if (!conf.peers) - exit_error(ctx, "config error: in TUN mode exactly one peer must be configured"); + exit_error("config error: in TUN mode exactly one peer must be configured"); } if (!conf.peers && !has_peer_group_peer_dirs(conf.peer_group)) - exit_error(ctx, "config error: neither fixed peers nor peer dirs have been configured"); + exit_error("config error: neither fixed peers nor peer dirs have been configured"); if (!conf.method_list) { - pr_warn(ctx, "no encryption method configured, falling back to method `null' (unencrypted)"); - fastd_config_method(ctx, "null"); + pr_warn("no encryption method configured, falling back to method `null' (unencrypted)"); + fastd_config_method("null"); } - configure_user(ctx); - configure_methods(ctx); + configure_user(); + configure_methods(); } -void fastd_config_verify(fastd_context_t *ctx) { - config_check_base(ctx); - configure_methods(ctx); +void fastd_config_verify(void) { + config_check_base(); + configure_methods(); fastd_peer_config_t *peer; for (peer = conf.peers; peer; peer = peer->next) - conf.protocol->peer_verify(ctx, peer); + conf.protocol->peer_verify(peer); } -static void peer_dirs_read_peer_group(fastd_context_t *ctx) { - read_peer_dirs(ctx); +static void peer_dirs_read_peer_group(void) { + read_peer_dirs(); fastd_peer_group_config_t *group; for (group = conf.peer_group->children; group; group = group->next) { conf.peer_group = group; - peer_dirs_read_peer_group(ctx); + peer_dirs_read_peer_group(); } } -static void peer_dirs_handle_old_peers(fastd_context_t *ctx, fastd_peer_config_t **old_peers, fastd_peer_config_t **new_peers) { +static void peer_dirs_handle_old_peers(fastd_peer_config_t **old_peers, fastd_peer_config_t **new_peers) { fastd_peer_config_t **peer, **next, **new_peer, **new_next; for (peer = old_peers; *peer; peer = next) { next = &(*peer)->next; @@ -615,7 +615,7 @@ static void peer_dirs_handle_old_peers(fastd_context_t *ctx, fastd_peer_config_t if (((*peer)->config_source_dir == (*new_peer)->config_source_dir) && strequal((*peer)->name, (*new_peer)->name)) { if (fastd_peer_config_equal(*peer, *new_peer)) { - pr_verbose(ctx, "peer `%s' unchanged", (*peer)->name); + pr_verbose("peer `%s' unchanged", (*peer)->name); fastd_peer_config_t *free_peer = *new_peer; *new_peer = *new_next; @@ -623,7 +623,7 @@ static void peer_dirs_handle_old_peers(fastd_context_t *ctx, fastd_peer_config_t peer = NULL; } else { - pr_verbose(ctx, "peer `%s' changed, resetting", (*peer)->name); + pr_verbose("peer `%s' changed, resetting", (*peer)->name); new_peer = NULL; } @@ -633,18 +633,18 @@ static void peer_dirs_handle_old_peers(fastd_context_t *ctx, fastd_peer_config_t /* no new peer was found, or the old one has changed */ if (peer && (!new_peer || !*new_peer)) { - pr_verbose(ctx, "removing peer `%s'", (*peer)->name); + pr_verbose("removing peer `%s'", (*peer)->name); fastd_peer_config_t *free_peer = *peer; *peer = *next; next = peer; - fastd_peer_config_purge(ctx, free_peer); + fastd_peer_config_purge(free_peer); } } } -static void peer_dirs_handle_new_peers(fastd_context_t *ctx UNUSED, fastd_peer_config_t **peers, fastd_peer_config_t *new_peers) { +static void peer_dirs_handle_new_peers(fastd_peer_config_t **peers, fastd_peer_config_t *new_peers) { fastd_peer_config_t *peer; for (peer = new_peers; peer; peer = peer->next) { if (peer->next) @@ -656,24 +656,24 @@ static void peer_dirs_handle_new_peers(fastd_context_t *ctx UNUSED, fastd_peer_c } } -void fastd_config_load_peer_dirs(fastd_context_t *ctx) { +void fastd_config_load_peer_dirs(void) { fastd_peer_config_t *old_peers = conf.peers; conf.peers = NULL; - peer_dirs_read_peer_group(ctx); + peer_dirs_read_peer_group(); fastd_peer_config_t *new_peers = conf.peers; conf.peers = old_peers; - peer_dirs_handle_old_peers(ctx, &conf.peers, &new_peers); - peer_dirs_handle_new_peers(ctx, &conf.peers, new_peers); + peer_dirs_handle_old_peers(&conf.peers, &new_peers); + peer_dirs_handle_new_peers(&conf.peers, new_peers); - assess_peers(ctx); + assess_peers(); } -void fastd_config_release(fastd_context_t *ctx) { +void fastd_config_release(void) { while (conf.peers) - fastd_peer_config_delete(ctx); + fastd_peer_config_delete(); while (conf.log_files) { fastd_log_file_t *next = conf.log_files->next; diff --git a/src/config.h b/src/config.h index 60663f4..18e516a 100644 --- a/src/config.h +++ b/src/config.h @@ -29,21 +29,21 @@ #include "fastd.h" -void fastd_config_protocol(fastd_context_t *ctx, const char *name); -void fastd_config_method(fastd_context_t *ctx, const char *name); -void fastd_config_cipher(fastd_context_t *ctx, const char *name, const char *impl); -void fastd_config_mac(fastd_context_t *ctx, const char *name, const char *impl); -void fastd_config_add_log_file(fastd_context_t *ctx, const char *name, fastd_loglevel_t level); -void fastd_config_bind_address(fastd_context_t *ctx, const fastd_peer_address_t *address, const char *bindtodev, bool default_v4, bool default_v6); -void fastd_config_peer_group_push(fastd_context_t *ctx, const char *name); -void fastd_config_peer_group_pop(fastd_context_t *ctx); -void fastd_config_release(fastd_context_t *ctx); -void fastd_config_handle_options(fastd_context_t *ctx, int argc, char *const argv[]); -void fastd_config_verify(fastd_context_t *ctx); - -void fastd_configure(fastd_context_t *ctx, int argc, char *const argv[]); -void fastd_config_check(fastd_context_t *ctx); -void fastd_config_load_peer_dirs(fastd_context_t *ctx); - -void fastd_add_peer_dir(fastd_context_t *ctx, const char *dir); -bool fastd_read_config(fastd_context_t *ctx, const char *filename, bool peer_config, int depth); +void fastd_config_protocol(const char *name); +void fastd_config_method(const char *name); +void fastd_config_cipher(const char *name, const char *impl); +void fastd_config_mac(const char *name, const char *impl); +void fastd_config_add_log_file(const char *name, fastd_loglevel_t level); +void fastd_config_bind_address(const fastd_peer_address_t *address, const char *bindtodev, bool default_v4, bool default_v6); +void fastd_config_peer_group_push(const char *name); +void fastd_config_peer_group_pop(void); +void fastd_config_release(void); +void fastd_config_handle_options(int argc, char *const argv[]); +void fastd_config_verify(void); + +void fastd_configure(int argc, char *const argv[]); +void fastd_config_check(void); +void fastd_config_load_peer_dirs(void); + +void fastd_add_peer_dir(const char *dir); +bool fastd_read_config(const char *filename, bool peer_config, int depth); diff --git a/src/config.y b/src/config.y index 6545288..78f2f70 100644 --- a/src/config.y +++ b/src/config.y @@ -28,7 +28,6 @@ %define api.push-pull push %name-prefix "fastd_config_" %locations -%parse-param {fastd_context_t *ctx} %parse-param {const char *filename} %parse-param {int depth} @@ -139,7 +138,7 @@ #include - void fastd_config_error(YYLTYPE *loc, fastd_context_t *ctx, const char *filename, int depth, const char *s); + void fastd_config_error(YYLTYPE *loc, const char *filename, int depth, const char *s); } @@ -242,11 +241,11 @@ secure_handshakes: ; cipher: TOK_STRING TOK_USE TOK_STRING { - fastd_config_cipher(ctx, $1->str, $3->str); + fastd_config_cipher($1->str, $3->str); } mac: TOK_STRING TOK_USE TOK_STRING { - fastd_config_mac(ctx, $1->str, $3->str); + fastd_config_mac($1->str, $3->str); } log: TOK_LEVEL log_level { @@ -265,7 +264,7 @@ log: TOK_LEVEL log_level { conf.log_syslog_level = $5; } | TOK_TO TOK_STRING maybe_log_level { - fastd_config_add_log_file(ctx, $2->str, $3); + fastd_config_add_log_file($2->str, $3); } ; @@ -295,11 +294,11 @@ interface: TOK_STRING { free(conf.ifname); conf.ifname = strdup($1->str); } ; bind: bind_address maybe_bind_interface maybe_bind_default { - fastd_config_bind_address(ctx, &$1, $2 ? $2->str : NULL, $3 == AF_UNSPEC || $3 == AF_INET, $3 == AF_UNSPEC || $3 == AF_INET6); + fastd_config_bind_address(&$1, $2 ? $2->str : NULL, $3 == AF_UNSPEC || $3 == AF_INET, $3 == AF_UNSPEC || $3 == AF_INET6); } | TOK_ADDR6_SCOPED maybe_port maybe_bind_default { fastd_peer_address_t addr = { .in6 = { .sin6_family = AF_INET6, .sin6_addr = $1.addr, .sin6_port = htons($2) } }; - fastd_config_bind_address(ctx, &addr, $1.ifname, $3 == AF_UNSPEC || $3 == AF_INET, $3 == AF_UNSPEC || $3 == AF_INET6); + fastd_config_bind_address(&addr, $1.ifname, $3 == AF_UNSPEC || $3 == AF_INET, $3 == AF_UNSPEC || $3 == AF_INET6); } ; @@ -351,7 +350,7 @@ packet_mark: TOK_UINT { mtu: TOK_UINT { if ($1 < 576 || $1 > 65535) { - fastd_config_error(&@$, ctx, filename, depth, "invalid MTU"); + fastd_config_error(&@$, filename, depth, "invalid MTU"); YYERROR; } @@ -367,12 +366,12 @@ mode: TOK_TAP { conf.mode = MODE_TAP; } ; protocol: TOK_STRING { - fastd_config_protocol(ctx, $1->str); + fastd_config_protocol($1->str); } ; method: TOK_STRING { - fastd_config_method(ctx, $1->str); + fastd_config_method($1->str); } ; @@ -420,7 +419,7 @@ on_verify: sync_def_async TOK_STRING { ; peer: TOK_STRING { - fastd_peer_config_new(ctx); + fastd_peer_config_new(); conf.peers->name = strdup($1->str); } ; @@ -508,26 +507,26 @@ peer_key: TOK_STRING { ; peer_include: TOK_STRING { - if (!fastd_read_config(ctx, $1->str, true, depth)) + if (!fastd_read_config($1->str, true, depth)) YYERROR; } ; peer_group: TOK_STRING { - fastd_config_peer_group_push(ctx, $1->str); + fastd_config_peer_group_push($1->str); } ; peer_group_after: { - fastd_config_peer_group_pop(ctx); + fastd_config_peer_group_pop(); } ; peer_limit: TOK_UINT { if ($1 > INT_MAX) { - fastd_config_error(&@$, ctx, filename, depth, "invalid peer limit"); + fastd_config_error(&@$, filename, depth, "invalid peer limit"); YYERROR; } @@ -540,18 +539,18 @@ forward: boolean { conf.forward = $1; } include: TOK_PEER TOK_STRING maybe_as { - fastd_peer_config_new(ctx); + fastd_peer_config_new(); if ($3) conf.peers->name = strdup($3->str); - if (!fastd_read_config(ctx, $2->str, true, depth)) + if (!fastd_read_config($2->str, true, depth)) YYERROR; } | TOK_PEERS TOK_FROM TOK_STRING { - fastd_add_peer_dir(ctx, $3->str); + fastd_add_peer_dir($3->str); } | TOK_STRING { - if (!fastd_read_config(ctx, $1->str, false, depth)) + if (!fastd_read_config($1->str, false, depth)) YYERROR; } ; @@ -601,7 +600,7 @@ colon_or_port: ':' port: colon_or_port TOK_UINT { if ($2 < 1 || $2 > 65535) { - fastd_config_error(&@$, ctx, filename, depth, "invalid port"); + fastd_config_error(&@$, filename, depth, "invalid port"); YYERROR; } $$ = $2; @@ -609,6 +608,6 @@ port: colon_or_port TOK_UINT { ; %% -void fastd_config_error(YYLTYPE *loc, fastd_context_t *ctx, const char *filename, int depth UNUSED, const char *s) { - pr_error(ctx, "config error: %s at %s:%i:%i", s, filename, loc->first_line, loc->first_column); +void fastd_config_error(YYLTYPE *loc, const char *filename, int depth UNUSED, const char *s) { + pr_error("config error: %s at %s:%i:%i", s, filename, loc->first_line, loc->first_column); } diff --git a/src/fastd.c b/src/fastd.c index 47daada..2649ef2 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -58,6 +58,9 @@ #endif +fastd_context_t ctx; + + static volatile bool sighup = false; static volatile bool terminate = false; static volatile bool dump = false; @@ -79,7 +82,7 @@ static void on_sigchld(int signo UNUSED) { while (waitpid(-1, NULL, WNOHANG) > 0) {} } -static void init_signals(fastd_context_t *ctx) { +static void init_signals(void) { struct sigaction action; action.sa_flags = 0; @@ -90,56 +93,56 @@ static void init_signals(fastd_context_t *ctx) { action.sa_handler = on_sighup; if (sigaction(SIGHUP, &action, NULL)) - exit_errno(ctx, "sigaction"); + exit_errno("sigaction"); action.sa_handler = on_terminate; if (sigaction(SIGTERM, &action, NULL)) - exit_errno(ctx, "sigaction"); + exit_errno("sigaction"); if (sigaction(SIGQUIT, &action, NULL)) - exit_errno(ctx, "sigaction"); + exit_errno("sigaction"); if (sigaction(SIGINT, &action, NULL)) - exit_errno(ctx, "sigaction"); + exit_errno("sigaction"); action.sa_handler = on_sigusr1; if (sigaction(SIGUSR1, &action, NULL)) - exit_errno(ctx, "sigaction"); + exit_errno("sigaction"); action.sa_handler = on_sigchld; if (sigaction(SIGCHLD, &action, NULL)) - exit_errno(ctx, "sigaction"); + exit_errno("sigaction"); action.sa_handler = SIG_IGN; if (sigaction(SIGPIPE, &action, NULL)) - exit_errno(ctx, "sigaction"); + exit_errno("sigaction"); if (sigaction(SIGTTIN, &action, NULL)) - exit_errno(ctx, "sigaction"); + exit_errno("sigaction"); if (sigaction(SIGTTOU, &action, NULL)) - exit_errno(ctx, "sigaction"); + exit_errno("sigaction"); } -void fastd_open_pipe(fastd_context_t *ctx, int *readfd, int *writefd) { +void fastd_open_pipe(int *readfd, int *writefd) { int pipefd[2]; if (pipe(pipefd)) - exit_errno(ctx, "pipe"); + exit_errno("pipe"); - fastd_setfd(ctx, pipefd[0], FD_CLOEXEC, 0); - fastd_setfd(ctx, pipefd[1], FD_CLOEXEC, 0); + fastd_setfd(pipefd[0], FD_CLOEXEC, 0); + fastd_setfd(pipefd[1], FD_CLOEXEC, 0); *readfd = pipefd[0]; *writefd = pipefd[1]; } -static void init_log(fastd_context_t *ctx) { +static void init_log(void) { uid_t uid = geteuid(); gid_t gid = getegid(); if (conf.user || conf.group) { if (setegid(conf.gid) < 0) - pr_debug_errno(ctx, "setegid"); + pr_debug_errno("setegid"); if (seteuid(conf.uid) < 0) - pr_debug_errno(ctx, "seteuid"); + pr_debug_errno("seteuid"); } if (conf.log_syslog_level > LL_UNSPEC) @@ -152,88 +155,88 @@ static void init_log(fastd_context_t *ctx) { file->config = config; file->fd = open(config->filename, O_WRONLY|O_APPEND|O_CREAT, 0600); - file->next = ctx->log_files; - ctx->log_files = file; + file->next = ctx.log_files; + ctx.log_files = file; } - ctx->log_initialized = true; + ctx.log_initialized = true; if (seteuid(uid) < 0) - pr_debug_errno(ctx, "seteuid"); + pr_debug_errno("seteuid"); if (setegid(gid) < 0) - pr_debug_errno(ctx, "setegid"); + pr_debug_errno("setegid"); } -static void close_log(fastd_context_t *ctx) { - while (ctx->log_files) { - fastd_log_fd_t *next = ctx->log_files->next; +static void close_log(void) { + while (ctx.log_files) { + fastd_log_fd_t *next = ctx.log_files->next; - close(ctx->log_files->fd); - free(ctx->log_files); + close(ctx.log_files->fd); + free(ctx.log_files); - ctx->log_files = next; + ctx.log_files = next; } closelog(); } -static void init_sockets(fastd_context_t *ctx) { - ctx->socks = malloc(conf.n_bind_addrs * sizeof(fastd_socket_t)); +static void init_sockets(void) { + ctx.socks = malloc(conf.n_bind_addrs * sizeof(fastd_socket_t)); unsigned i; fastd_bind_address_t *addr = conf.bind_addrs; for (i = 0; i < conf.n_bind_addrs; i++) { - ctx->socks[i] = (fastd_socket_t){ .fd = -2, .addr = addr }; + ctx.socks[i] = (fastd_socket_t){ .fd = -2, .addr = addr }; if (addr == conf.bind_addr_default_v4) - ctx->sock_default_v4 = &ctx->socks[i]; + ctx.sock_default_v4 = &ctx.socks[i]; if (addr == conf.bind_addr_default_v6) - ctx->sock_default_v6 = &ctx->socks[i]; + ctx.sock_default_v6 = &ctx.socks[i]; addr = addr->next; } - ctx->n_socks = conf.n_bind_addrs; + ctx.n_socks = conf.n_bind_addrs; } -void fastd_setfd(const fastd_context_t *ctx, int fd, int set, int unset) { +void fastd_setfd(const int fd, int set, int unset) { int flags = fcntl(fd, F_GETFD); if (flags < 0) - exit_errno(ctx, "Getting file descriptor flags failed: fcntl"); + exit_errno("Getting file descriptor flags failed: fcntl"); if (fcntl(fd, F_SETFD, (flags|set) & (~unset)) < 0) - exit_errno(ctx, "Setting file descriptor flags failed: fcntl"); + exit_errno("Setting file descriptor flags failed: fcntl"); } -void fastd_setfl(const fastd_context_t *ctx, int fd, int set, int unset) { +void fastd_setfl(const int fd, int set, int unset) { int flags = fcntl(fd, F_GETFL); if (flags < 0) - exit_errno(ctx, "Getting file status flags failed: fcntl"); + exit_errno("Getting file status flags failed: fcntl"); if (fcntl(fd, F_SETFL, (flags|set) & (~unset)) < 0) - exit_errno(ctx, "Setting file status flags failed: fcntl"); + exit_errno("Setting file status flags failed: fcntl"); } -static void close_sockets(fastd_context_t *ctx) { +static void close_sockets(void) { unsigned i; - for (i = 0; i < ctx->n_socks; i++) - fastd_socket_close(ctx, &ctx->socks[i]); + for (i = 0; i < ctx.n_socks; i++) + fastd_socket_close(&ctx.socks[i]); - free(ctx->socks); + free(ctx.socks); } -static inline void handle_forward(fastd_context_t *ctx, fastd_peer_t *source_peer, fastd_buffer_t buffer) { - fastd_eth_addr_t dest_addr = fastd_get_dest_address(ctx, buffer); +static inline void handle_forward(fastd_peer_t *source_peer, fastd_buffer_t buffer) { + fastd_eth_addr_t dest_addr = fastd_get_dest_address(buffer); if (fastd_eth_addr_is_unicast(dest_addr)) { - fastd_peer_t *dest_peer = fastd_peer_find_by_eth_addr(ctx, dest_addr); + fastd_peer_t *dest_peer = fastd_peer_find_by_eth_addr(dest_addr); if (dest_peer) { if (dest_peer != source_peer) - conf.protocol->send(ctx, dest_peer, buffer); + conf.protocol->send(dest_peer, buffer); else fastd_buffer_free(buffer); @@ -241,50 +244,50 @@ static inline void handle_forward(fastd_context_t *ctx, fastd_peer_t *source_pee } } - fastd_send_all(ctx, source_peer, buffer); + fastd_send_all(source_peer, buffer); } -void fastd_handle_receive(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer) { +void fastd_handle_receive(fastd_peer_t *peer, fastd_buffer_t buffer) { if (conf.mode == MODE_TAP) { if (buffer.len < ETH_HLEN) { - pr_debug(ctx, "received truncated packet"); + pr_debug("received truncated packet"); fastd_buffer_free(buffer); return; } - fastd_eth_addr_t src_addr = fastd_get_source_address(ctx, buffer); + fastd_eth_addr_t src_addr = fastd_get_source_address(buffer); if (fastd_eth_addr_is_unicast(src_addr)) - fastd_peer_eth_addr_add(ctx, peer, src_addr); + fastd_peer_eth_addr_add(peer, src_addr); } - ctx->rx.packets++; - ctx->rx.bytes += buffer.len; + ctx.rx.packets++; + ctx.rx.bytes += buffer.len; - fastd_tuntap_write(ctx, buffer); + fastd_tuntap_write(buffer); if (conf.mode == MODE_TAP && conf.forward) { - handle_forward(ctx, peer, buffer); + handle_forward(peer, buffer); return; } fastd_buffer_free(buffer); } -static inline void on_pre_up(fastd_context_t *ctx) { - fastd_shell_command_exec(ctx, &conf.on_pre_up, NULL, NULL, NULL); +static inline void on_pre_up(void) { + fastd_shell_command_exec(&conf.on_pre_up, NULL, NULL, NULL); } -static inline void on_up(fastd_context_t *ctx) { - fastd_shell_command_exec(ctx, &conf.on_up, NULL, NULL, NULL); +static inline void on_up(void) { + fastd_shell_command_exec(&conf.on_up, NULL, NULL, NULL); } -static inline void on_down(fastd_context_t *ctx) { - fastd_shell_command_exec(ctx, &conf.on_down, NULL, NULL, NULL); +static inline void on_down(void) { + fastd_shell_command_exec(&conf.on_down, NULL, NULL, NULL); } -static inline void on_post_down(fastd_context_t *ctx) { - fastd_shell_command_exec(ctx, &conf.on_post_down, NULL, NULL, NULL); +static inline void on_post_down(void) { + fastd_shell_command_exec(&conf.on_post_down, NULL, NULL, NULL); } static fastd_peer_group_t* init_peer_group(const fastd_peer_group_config_t *config, fastd_peer_group_t *parent) { @@ -304,8 +307,8 @@ static fastd_peer_group_t* init_peer_group(const fastd_peer_group_config_t *conf return ret; } -static void init_peer_groups(fastd_context_t *ctx) { - ctx->peer_group = init_peer_group(conf.peer_group, NULL); +static void init_peer_groups(void) { + ctx.peer_group = init_peer_group(conf.peer_group, NULL); } static void free_peer_group(fastd_peer_group_t *group) { @@ -319,38 +322,38 @@ static void free_peer_group(fastd_peer_group_t *group) { free(group); } -static void delete_peer_groups(fastd_context_t *ctx) { - free_peer_group(ctx->peer_group); +static void delete_peer_groups(void) { + free_peer_group(ctx.peer_group); } -static void init_peers(fastd_context_t *ctx) { +static void init_peers(void) { fastd_peer_config_t *peer_conf; for (peer_conf = conf.peers; peer_conf; peer_conf = peer_conf->next) - conf.protocol->peer_configure(ctx, peer_conf); + conf.protocol->peer_configure(peer_conf); for (peer_conf = conf.peers; peer_conf; peer_conf = peer_conf->next) { - bool enable = conf.protocol->peer_check(ctx, peer_conf); + bool enable = conf.protocol->peer_check(peer_conf); if (enable && !peer_conf->enabled) - fastd_peer_add(ctx, peer_conf); + fastd_peer_add(peer_conf); peer_conf->enabled = enable; } size_t i; - for (i = 0; i < VECTOR_LEN(ctx->peers);) { - fastd_peer_t *peer = VECTOR_INDEX(ctx->peers, i); + for (i = 0; i < VECTOR_LEN(ctx.peers);) { + fastd_peer_t *peer = VECTOR_INDEX(ctx.peers, i); if (peer->config) { if (!peer->config->enabled) { - pr_info(ctx, "previously enabled peer %P disabled, deleting.", peer); - fastd_peer_delete(ctx, peer); + pr_info("previously enabled peer %P disabled, deleting.", peer); + fastd_peer_delete(peer); continue; } } else { - if (!conf.protocol->peer_check_temporary(ctx, peer)) { - fastd_peer_delete(ctx, peer); + if (!conf.protocol->peer_check_temporary(peer)) { + fastd_peer_delete(peer); continue; } } @@ -359,93 +362,93 @@ static void init_peers(fastd_context_t *ctx) { } } -static void delete_peers(fastd_context_t *ctx) { - while (VECTOR_LEN(ctx->peers)) - fastd_peer_delete(ctx, VECTOR_INDEX(ctx->peers, VECTOR_LEN(ctx->peers)-1)); +static void delete_peers(void) { + while (VECTOR_LEN(ctx.peers)) + fastd_peer_delete(VECTOR_INDEX(ctx.peers, VECTOR_LEN(ctx.peers)-1)); } -static void dump_state(fastd_context_t *ctx) { - pr_info(ctx, "TX stats: %U packet(s), %U byte(s); dropped: %U packet(s), %U byte(s); error: %U packet(s), %U byte(s)", - ctx->tx.packets, ctx->tx.bytes, ctx->tx_dropped.packets, ctx->tx_dropped.bytes, ctx->tx_error.packets, ctx->tx_error.bytes); - pr_info(ctx, "RX stats: %U packet(s), %U byte(s)", ctx->rx.packets, ctx->rx.bytes); +static void dump_state(void) { + pr_info("TX stats: %U packet(s), %U byte(s); dropped: %U packet(s), %U byte(s); error: %U packet(s), %U byte(s)", + ctx.tx.packets, ctx.tx.bytes, ctx.tx_dropped.packets, ctx.tx_dropped.bytes, ctx.tx_error.packets, ctx.tx_error.bytes); + pr_info("RX stats: %U packet(s), %U byte(s)", ctx.rx.packets, ctx.rx.bytes); - pr_info(ctx, "dumping peers:"); + pr_info("dumping peers:"); size_t i; - for (i = 0; i < VECTOR_LEN(ctx->peers);) { - fastd_peer_t *peer = VECTOR_INDEX(ctx->peers, i); + for (i = 0; i < VECTOR_LEN(ctx.peers);) { + fastd_peer_t *peer = VECTOR_INDEX(ctx.peers, i); if (!fastd_peer_is_established(peer)) { - pr_info(ctx, "peer %P not connected, address: %I", peer, &peer->address); + pr_info("peer %P not connected, address: %I", peer, &peer->address); continue; } if (conf.mode == MODE_TAP) { unsigned int eth_addresses = 0; size_t i; - for (i = 0; i < VECTOR_LEN(ctx->eth_addrs); i++) { - if (VECTOR_INDEX(ctx->eth_addrs, i).peer == peer) + for (i = 0; i < VECTOR_LEN(ctx.eth_addrs); i++) { + if (VECTOR_INDEX(ctx.eth_addrs, i).peer == peer) eth_addresses++; } - pr_info(ctx, "peer %P connected, address: %I, associated MAC addresses: %u", peer, &peer->address, eth_addresses); + pr_info("peer %P connected, address: %I, associated MAC addresses: %u", peer, &peer->address, eth_addresses); } else { - pr_info(ctx, "peer %P connected, address: %I", peer, &peer->address); + pr_info("peer %P connected, address: %I", peer, &peer->address); } } - pr_info(ctx, "dump finished."); + pr_info("dump finished."); } -static inline void no_valid_address_debug(fastd_context_t *ctx, const fastd_peer_t *peer) { - pr_debug(ctx, "not sending a handshake to %P (no valid address resolved)", peer); +static inline void no_valid_address_debug(const fastd_peer_t *peer) { + pr_debug("not sending a handshake to %P (no valid address resolved)", peer); } -static void send_handshake(fastd_context_t *ctx, fastd_peer_t *peer) { +static void send_handshake(fastd_peer_t *peer) { if (!fastd_peer_is_established(peer)) { if (!peer->next_remote->n_addresses) { - no_valid_address_debug(ctx, peer); + no_valid_address_debug(peer); return; } - fastd_peer_claim_address(ctx, peer, NULL, NULL, &peer->next_remote->addresses[peer->next_remote->current_address]); - fastd_peer_reset_socket(ctx, peer); + fastd_peer_claim_address(peer, NULL, NULL, &peer->next_remote->addresses[peer->next_remote->current_address]); + fastd_peer_reset_socket(peer); } if (!peer->sock) return; if (peer->address.sa.sa_family == AF_UNSPEC) { - no_valid_address_debug(ctx, peer); + no_valid_address_debug(peer); return; } - if (!fastd_timed_out(ctx, &peer->last_handshake_timeout) + if (!fastd_timed_out(&peer->last_handshake_timeout) && fastd_peer_address_equal(&peer->address, &peer->last_handshake_address)) { - pr_debug(ctx, "not sending a handshake to %P as we sent one a short time ago", peer); + pr_debug("not sending a handshake to %P as we sent one a short time ago", peer); return; } - pr_debug(ctx, "sending handshake to %P[%I]...", peer, &peer->address); - peer->last_handshake_timeout = fastd_in_seconds(ctx, conf.min_handshake_interval); + pr_debug("sending handshake to %P[%I]...", peer, &peer->address); + peer->last_handshake_timeout = fastd_in_seconds(conf.min_handshake_interval); peer->last_handshake_address = peer->address; - conf.protocol->handshake_init(ctx, peer->sock, &peer->local_address, &peer->address, peer); + conf.protocol->handshake_init(peer->sock, &peer->local_address, &peer->address, peer); } -static void handle_handshake_queue(fastd_context_t *ctx) { - if (!ctx->handshake_queue.next) +static void handle_handshake_queue(void) { + if (!ctx.handshake_queue.next) return; - fastd_peer_t *peer = container_of(ctx->handshake_queue.next, fastd_peer_t, handshake_entry); - if (!fastd_timed_out(ctx, &peer->next_handshake)) + fastd_peer_t *peer = container_of(ctx.handshake_queue.next, fastd_peer_t, handshake_entry); + if (!fastd_timed_out(&peer->next_handshake)) return; - fastd_peer_schedule_handshake_default(ctx, peer); + fastd_peer_schedule_handshake_default(peer); - if (!fastd_peer_may_connect(ctx, peer)) { + if (!fastd_peer_may_connect(peer)) { if (peer->next_remote != NULL) { - pr_debug(ctx, "temporarily disabling handshakes with %P", peer); + pr_debug("temporarily disabling handshakes with %P", peer); peer->next_remote = NULL; } @@ -453,7 +456,7 @@ static void handle_handshake_queue(fastd_context_t *ctx) { } if (peer->next_remote || fastd_peer_is_established(peer)) { - send_handshake(ctx, peer); + send_handshake(peer); if (fastd_peer_is_established(peer)) return; @@ -470,27 +473,27 @@ static void handle_handshake_queue(fastd_context_t *ctx) { peer->next_remote->current_address = 0; if (fastd_remote_is_dynamic(peer->next_remote)) - fastd_resolve_peer(ctx, peer, peer->next_remote); + fastd_resolve_peer(peer, peer->next_remote); } -static void enable_temporaries(fastd_context_t *ctx) { +static void enable_temporaries(void) { size_t i; - for (i = 0; i < VECTOR_LEN(ctx->peers_temp); i++) - fastd_peer_enable_temporary(ctx, VECTOR_INDEX(ctx->peers_temp, i)); + for (i = 0; i < VECTOR_LEN(ctx.peers_temp); i++) + fastd_peer_enable_temporary(VECTOR_INDEX(ctx.peers_temp, i)); - VECTOR_RESIZE(ctx->peers_temp, 0); + VECTOR_RESIZE(ctx.peers_temp, 0); } -static bool maintain_peer(fastd_context_t *ctx, fastd_peer_t *peer) { +static bool maintain_peer(fastd_peer_t *peer) { if (fastd_peer_is_temporary(peer) || fastd_peer_is_established(peer)) { /* check for peer timeout */ - if (fastd_timed_out(ctx, &peer->timeout)) { + if (fastd_timed_out(&peer->timeout)) { if (fastd_peer_is_temporary(peer)) { - fastd_peer_delete(ctx, peer); + fastd_peer_delete(peer); return false; } else { - fastd_peer_reset(ctx, peer); + fastd_peer_reset(peer); return true; } } @@ -499,34 +502,34 @@ static bool maintain_peer(fastd_context_t *ctx, fastd_peer_t *peer) { if (!fastd_peer_is_established(peer)) return true; - if (!fastd_timed_out(ctx, &peer->keepalive_timeout)) + if (!fastd_timed_out(&peer->keepalive_timeout)) return true; - pr_debug2(ctx, "sending keepalive to %P", peer); - conf.protocol->send(ctx, peer, fastd_buffer_alloc(ctx, 0, conf.min_encrypt_head_space, conf.min_encrypt_tail_space)); + pr_debug2("sending keepalive to %P", peer); + conf.protocol->send(peer, fastd_buffer_alloc(0, conf.min_encrypt_head_space, conf.min_encrypt_tail_space)); } return true; } -static void maintenance(fastd_context_t *ctx) { - fastd_socket_handle_binds(ctx); +static void maintenance(void) { + fastd_socket_handle_binds(); size_t i; - for (i = 0; i < VECTOR_LEN(ctx->peers);) { - fastd_peer_t *peer = VECTOR_INDEX(ctx->peers, i); + for (i = 0; i < VECTOR_LEN(ctx.peers);) { + fastd_peer_t *peer = VECTOR_INDEX(ctx.peers, i); - if (maintain_peer(ctx, peer)) + if (maintain_peer(peer)) i++; } - fastd_peer_eth_addr_cleanup(ctx); + fastd_peer_eth_addr_cleanup(); - ctx->next_maintenance.tv_sec += conf.maintenance_interval; + ctx.next_maintenance.tv_sec += conf.maintenance_interval; } -static void close_fds(fastd_context_t *ctx) { +static void close_fds(void) { struct rlimit rl; int fd, maxfd; @@ -543,12 +546,12 @@ static void close_fds(fastd_context_t *ctx) { } if (errno != EBADF) - pr_error_errno(ctx, "close"); + pr_error_errno("close"); } } } -static void write_pid(fastd_context_t *ctx, pid_t pid) { +static void write_pid(pid_t pid) { if (!conf.pid_file) return; @@ -557,98 +560,98 @@ static void write_pid(fastd_context_t *ctx, pid_t pid) { if (conf.user || conf.group) { if (setegid(conf.gid) < 0) - pr_debug_errno(ctx, "setegid"); + pr_debug_errno("setegid"); if (seteuid(conf.uid) < 0) - pr_debug_errno(ctx, "seteuid"); + pr_debug_errno("seteuid"); } int fd = open(conf.pid_file, O_WRONLY|O_CREAT|O_TRUNC, 0666); if (fd < 0) { - pr_error_errno(ctx, "can't write PID file: open"); + pr_error_errno("can't write PID file: open"); goto end; } if (dprintf(fd, "%i", pid) < 0) - pr_error_errno(ctx, "can't write PID file: dprintf"); + pr_error_errno("can't write PID file: dprintf"); if (close(fd) < 0) - pr_warn_errno(ctx, "close"); + pr_warn_errno("close"); end: if (seteuid(uid) < 0) - pr_debug_errno(ctx, "seteuid"); + pr_debug_errno("seteuid"); if (setegid(gid) < 0) - pr_debug_errno(ctx, "setegid"); + pr_debug_errno("setegid"); } -static void set_user(fastd_context_t *ctx) { +static void set_user(void) { if (conf.user || conf.group) { if (setgid(conf.gid) < 0) - exit_errno(ctx, "setgid"); + exit_errno("setgid"); if (setuid(conf.uid) < 0) - exit_errno(ctx, "setuid"); + exit_errno("setuid"); - pr_info(ctx, "Changed to UID %i, GID %i.", conf.uid, conf.gid); + pr_info("Changed to UID %i, GID %i.", conf.uid, conf.gid); } } -static void set_groups(fastd_context_t *ctx) { +static void set_groups(void) { if (conf.groups) { if (setgroups(conf.n_groups, conf.groups) < 0) { if (errno != EPERM) - pr_debug_errno(ctx, "setgroups"); + pr_debug_errno("setgroups"); } } else if (conf.user || conf.group) { if (setgroups(1, &conf.gid) < 0) { if (errno != EPERM) - pr_debug_errno(ctx, "setgroups"); + pr_debug_errno("setgroups"); } } } -static void drop_caps(fastd_context_t *ctx) { - set_user(ctx); - fastd_cap_drop(ctx); +static void drop_caps(void) { + set_user(); + fastd_cap_drop(); } /* will double fork and forward potential exit codes from the child to the parent */ -static int daemonize(fastd_context_t *ctx) { +static int daemonize(void) { static const uint8_t ERROR_STATUS = 1; uint8_t status = 1; int parent_rpipe, parent_wpipe; - fastd_open_pipe(ctx, &parent_rpipe, &parent_wpipe); + fastd_open_pipe(&parent_rpipe, &parent_wpipe); pid_t fork1 = fork(); if (fork1 < 0) { - exit_errno(ctx, "fork"); + exit_errno("fork"); } else if (fork1 == 0) { /* child 1 */ if (close(parent_rpipe) < 0) - pr_error_errno(ctx, "close"); + pr_error_errno("close"); if (setsid() < 0) - pr_error_errno(ctx, "setsid"); + pr_error_errno("setsid"); int child_rpipe, child_wpipe; - fastd_open_pipe(ctx, &child_rpipe, &child_wpipe); + fastd_open_pipe(&child_rpipe, &child_wpipe); pid_t fork2 = fork(); if (fork2 < 0) { write(parent_wpipe, &ERROR_STATUS, 1); - exit_errno(ctx, "fork"); + exit_errno("fork"); } else if (fork2 == 0) { /* child 2 */ if (close(child_rpipe) < 0 || close(parent_wpipe) < 0) { write(child_wpipe, &ERROR_STATUS, 1); - pr_error_errno(ctx, "close"); + pr_error_errno("close"); } return child_wpipe; @@ -668,7 +671,7 @@ static int daemonize(fastd_context_t *ctx) { if (ret < 0) { write(child_wpipe, &ERROR_STATUS, 1); - pr_error_errno(ctx, "waitpid"); + pr_error_errno("waitpid"); } if (WIFEXITED(child_status)) { @@ -679,7 +682,7 @@ static int daemonize(fastd_context_t *ctx) { else { write(parent_wpipe, &ERROR_STATUS, 1); if (WIFSIGNALED(child_status)) - exit_error(ctx, "child exited with signal %i", WTERMSIG(child_status)); + exit_error("child exited with signal %i", WTERMSIG(child_status)); exit(1); } } @@ -692,10 +695,10 @@ static int daemonize(fastd_context_t *ctx) { action.sa_handler = SIG_IGN; if (sigaction(SIGCHLD, &action, NULL)) - exit_errno(ctx, "sigaction"); + exit_errno("sigaction"); if (read(parent_rpipe, &status, 1) < 0) - exit_errno(ctx, "read"); + exit_errno("read"); exit(status); } @@ -704,7 +707,7 @@ static int daemonize(fastd_context_t *ctx) { } #ifdef USE_SYSTEMD -static void notify_systemd(fastd_context_t *ctx, const char *notify_socket) { +static void notify_systemd(const char *notify_socket) { int fd; struct sockaddr_un sa = {}; @@ -722,20 +725,20 @@ static void notify_systemd(fastd_context_t *ctx, const char *notify_socket) { sa.sun_path[0] = 0; if (connect(fd, (struct sockaddr*)&sa, offsetof(struct sockaddr_un, sun_path) + strnlen(notify_socket, sizeof(sa.sun_path))) < 0) { - pr_debug_errno(ctx, "unable to connect to notify socket: connect"); + pr_debug_errno("unable to connect to notify socket: connect"); close(fd); return; } dprintf(fd, "READY=1\nMAINPID=%lu", (unsigned long) getpid()); - pr_debug(ctx, "sent startup notification to systemd"); + pr_debug("sent startup notification to systemd"); close(fd); } #endif int main(int argc, char *argv[]) { - fastd_context_t ctx = {}; + memset(&ctx, 0, sizeof(ctx)); int status_fd = -1; #ifdef USE_SYSTEMD @@ -749,38 +752,38 @@ int main(int argc, char *argv[]) { } #endif - close_fds(&ctx); + close_fds(); - fastd_random_bytes(&ctx, &ctx.randseed, sizeof(ctx.randseed), false); + fastd_random_bytes(&ctx.randseed, sizeof(ctx.randseed), false); - fastd_configure(&ctx, argc, argv); + fastd_configure(argc, argv); if (conf.verify_config) { - fastd_config_verify(&ctx); + fastd_config_verify(); exit(0); } if (conf.generate_key) { - conf.protocol->generate_key(&ctx); + conf.protocol->generate_key(); exit(0); } - conf.protocol_config = conf.protocol->init(&ctx); + conf.protocol_config = conf.protocol->init(); if (conf.show_key) { conf.protocol->show_key(); exit(0); } - init_signals(&ctx); + init_signals(); if (conf.daemon) - status_fd = daemonize(&ctx); + status_fd = daemonize(); if (chdir("/")) - pr_error(&ctx, "can't chdir to `/': %s", strerror(errno)); + pr_error("can't chdir to `/': %s", strerror(errno)); - init_log(&ctx); + init_log(); #ifdef HAVE_LIBSODIUM sodium_init(); @@ -792,39 +795,39 @@ int main(int argc, char *argv[]) { OPENSSL_config(NULL); #endif - fastd_config_check(&ctx); + fastd_config_check(); - fastd_update_time(&ctx); + fastd_update_time(); - ctx.next_maintenance = fastd_in_seconds(&ctx, conf.maintenance_interval); + ctx.next_maintenance = fastd_in_seconds(conf.maintenance_interval); ctx.unknown_handshakes[0].timeout = ctx.now; - pr_info(&ctx, "fastd " FASTD_VERSION " starting"); + pr_info("fastd " FASTD_VERSION " starting"); - fastd_cap_init(&ctx); + fastd_cap_init(); /* change groups early as the can be relevant for file access (for PID file & log files) */ - set_groups(&ctx); + set_groups(); - init_sockets(&ctx); - fastd_async_init(&ctx); - fastd_poll_init(&ctx); + init_sockets(); + fastd_async_init(); + fastd_poll_init(); - if (!fastd_socket_handle_binds(&ctx)) - exit_error(&ctx, "unable to bind default socket"); + if (!fastd_socket_handle_binds()) + exit_error("unable to bind default socket"); - on_pre_up(&ctx); + on_pre_up(); - fastd_tuntap_open(&ctx); + fastd_tuntap_open(); - init_peer_groups(&ctx); + init_peer_groups(); - write_pid(&ctx, getpid()); + write_pid(getpid()); #ifdef USE_SYSTEMD if (notify_socket) { - notify_systemd(&ctx, notify_socket); + notify_systemd(notify_socket); free(notify_socket); } #endif @@ -832,40 +835,40 @@ int main(int argc, char *argv[]) { if (status_fd >= 0) { static const uint8_t STATUS = 0; if (write(status_fd, &STATUS, 1) < 0) - exit_errno(&ctx, "status: write"); + exit_errno("status: write"); if (close(status_fd)) - exit_errno(&ctx, "status: close"); + exit_errno("status: close"); } if (conf.drop_caps == DROP_CAPS_EARLY) - drop_caps(&ctx); + drop_caps(); - on_up(&ctx); + on_up(); if (conf.drop_caps == DROP_CAPS_ON) - drop_caps(&ctx); + drop_caps(); else if (conf.drop_caps == DROP_CAPS_OFF) - set_user(&ctx); + set_user(); - fastd_config_load_peer_dirs(&ctx); + fastd_config_load_peer_dirs(); VECTOR_ALLOC(ctx.eth_addrs, 0); VECTOR_ALLOC(ctx.peers, 0); VECTOR_ALLOC(ctx.peers_temp, 0); - fastd_peer_hashtable_init(&ctx); + fastd_peer_hashtable_init(); - init_peers(&ctx); + init_peers(); while (!terminate) { - handle_handshake_queue(&ctx); + handle_handshake_queue(); - fastd_poll_handle(&ctx); + fastd_poll_handle(); - enable_temporaries(&ctx); + enable_temporaries(); - if (fastd_timed_out(&ctx, &ctx.next_maintenance)) - maintenance(&ctx); + if (fastd_timed_out(&ctx.next_maintenance)) + maintenance(); sigset_t set, oldset; sigemptyset(&set); @@ -874,35 +877,35 @@ int main(int argc, char *argv[]) { if (sighup) { sighup = false; - pr_info(&ctx, "reconfigure triggered"); + pr_info("reconfigure triggered"); - close_log(&ctx); - init_log(&ctx); + close_log(); + init_log(); - fastd_config_load_peer_dirs(&ctx); - init_peers(&ctx); + fastd_config_load_peer_dirs(); + init_peers(); } if (dump) { dump = false; - dump_state(&ctx); + dump_state(); } pthread_sigmask(SIG_SETMASK, &oldset, NULL); } - on_down(&ctx); + on_down(); - delete_peers(&ctx); - delete_peer_groups(&ctx); + delete_peers(); + delete_peer_groups(); - fastd_tuntap_close(&ctx); - close_sockets(&ctx); - fastd_poll_free(&ctx); + fastd_tuntap_close(); + close_sockets(); + fastd_poll_free(); - on_post_down(&ctx); + on_post_down(); - fastd_peer_hashtable_free(&ctx); + fastd_peer_hashtable_free(); VECTOR_FREE(ctx.peers_temp); VECTOR_FREE(ctx.peers); @@ -917,8 +920,8 @@ int main(int argc, char *argv[]) { ERR_free_strings(); #endif - close_log(&ctx); - fastd_config_release(&ctx); + close_log(); + fastd_config_release(); return 0; } diff --git a/src/fastd.h b/src/fastd.h index 5cbd4d7..4913a35 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -52,26 +52,26 @@ struct __attribute__((__packed__)) fastd_eth_addr { struct fastd_protocol { const char *name; - fastd_protocol_config_t* (*init)(fastd_context_t *ctx); - void (*peer_verify)(fastd_context_t *ctx, fastd_peer_config_t *peer_conf); - void (*peer_configure)(fastd_context_t *ctx, fastd_peer_config_t *peer_conf); - bool (*peer_check)(fastd_context_t *ctx, fastd_peer_config_t *peer_conf); - bool (*peer_check_temporary)(fastd_context_t *ctx, fastd_peer_t *peer); + fastd_protocol_config_t* (*init)(void); + void (*peer_verify)(fastd_peer_config_t *peer_conf); + void (*peer_configure)(fastd_peer_config_t *peer_conf); + bool (*peer_check)(fastd_peer_config_t *peer_conf); + bool (*peer_check_temporary)(fastd_peer_t *peer); - void (*handshake_init)(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer); - void (*handshake_handle)(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, const fastd_handshake_t *handshake, const fastd_method_info_t *method); + void (*handshake_init)(const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer); + void (*handshake_handle)(fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, const fastd_handshake_t *handshake, const fastd_method_info_t *method); - void (*handle_recv)(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer); - void (*send)(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer); + void (*handle_recv)(fastd_peer_t *peer, fastd_buffer_t buffer); + void (*send)(fastd_peer_t *peer, fastd_buffer_t buffer); - void (*init_peer_state)(fastd_context_t *ctx, fastd_peer_t *peer); - void (*reset_peer_state)(fastd_context_t *ctx, fastd_peer_t *peer); - void (*free_peer_state)(fastd_context_t *ctx, fastd_peer_t *peer); + void (*init_peer_state)(fastd_peer_t *peer); + void (*reset_peer_state)(fastd_peer_t *peer); + void (*free_peer_state)(fastd_peer_t *peer); - void (*generate_key)(fastd_context_t *ctx); + void (*generate_key)(void); void (*show_key)(void); void (*set_shell_env)(const fastd_peer_t *peer); - bool (*describe_peer)(const fastd_context_t *ctx, const fastd_peer_t *peer, char *buf, size_t len); + bool (*describe_peer)(const fastd_peer_t *peer, char *buf, size_t len); }; union fastd_peer_address { @@ -228,8 +228,6 @@ struct fastd_config { bool verify_config; }; -extern fastd_config_t conf; - struct fastd_context { bool log_initialized; fastd_log_fd_t *log_files; @@ -288,36 +286,40 @@ struct fastd_string_stack { }; -void fastd_send(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer, size_t stat_size); -void fastd_send_all(fastd_context_t *ctx, fastd_peer_t *source_peer, fastd_buffer_t buffer); -void fastd_send_handshake(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer); -void fastd_receive(fastd_context_t *ctx, fastd_socket_t *sock); +extern fastd_context_t ctx; +extern fastd_config_t conf; + + +void fastd_send(const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer, size_t stat_size); +void fastd_send_all(fastd_peer_t *source_peer, fastd_buffer_t buffer); +void fastd_send_handshake(const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer); +void fastd_receive(fastd_socket_t *sock); -void fastd_handle_receive(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer); +void fastd_handle_receive(fastd_peer_t *peer, fastd_buffer_t buffer); -bool fastd_socket_handle_binds(fastd_context_t *ctx); -fastd_socket_t* fastd_socket_open(fastd_context_t *ctx, fastd_peer_t *peer, int af); -void fastd_socket_close(fastd_context_t *ctx, fastd_socket_t *sock); -void fastd_socket_error(fastd_context_t *ctx, fastd_socket_t *sock); +bool fastd_socket_handle_binds(void); +fastd_socket_t* fastd_socket_open(fastd_peer_t *peer, int af); +void fastd_socket_close(fastd_socket_t *sock); +void fastd_socket_error(fastd_socket_t *sock); -void fastd_open_pipe(fastd_context_t *ctx, int *readfd, int *writefd); -void fastd_setfd(const fastd_context_t *ctx, int fd, int set, int unset); -void fastd_setfl(const fastd_context_t *ctx, int fd, int set, int unset); +void fastd_open_pipe(int *readfd, int *writefd); +void fastd_setfd(const int fd, int set, int unset); +void fastd_setfl(const int fd, int set, int unset); -void fastd_resolve_peer(fastd_context_t *ctx, fastd_peer_t *peer, fastd_remote_t *remote); +void fastd_resolve_peer(fastd_peer_t *peer, fastd_remote_t *remote); -void fastd_tuntap_open(fastd_context_t *ctx); -fastd_buffer_t fastd_tuntap_read(fastd_context_t *ctx); -void fastd_tuntap_write(fastd_context_t *ctx, fastd_buffer_t buffer); -void fastd_tuntap_close(fastd_context_t *ctx); +void fastd_tuntap_open(void); +fastd_buffer_t fastd_tuntap_read(void); +void fastd_tuntap_write(fastd_buffer_t buffer); +void fastd_tuntap_close(void); -void fastd_cap_init(fastd_context_t *ctx); -void fastd_cap_drop(fastd_context_t *ctx); +void fastd_cap_init(void); +void fastd_cap_drop(void); -void fastd_random_bytes(fastd_context_t *ctx, void *buffer, size_t len, bool secure); +void fastd_random_bytes(void *buffer, size_t len, bool secure); -static inline int fastd_rand(fastd_context_t *ctx, int min, int max) { - unsigned int r = (unsigned int)rand_r(&ctx->randseed); +static inline int fastd_rand(int min, int max) { + unsigned int r = (unsigned int)rand_r(&ctx.randseed); return (r%(max-min) + min); } @@ -338,19 +340,19 @@ static inline size_t alignto(size_t l, size_t a) { } -static inline size_t fastd_max_inner_packet(const fastd_context_t *ctx) { +static inline size_t fastd_max_inner_packet(void) { switch (conf.mode) { case MODE_TAP: return conf.mtu+ETH_HLEN; case MODE_TUN: return conf.mtu; default: - exit_bug(ctx, "invalid mode"); + exit_bug("invalid mode"); } } -static inline size_t fastd_max_outer_packet(const fastd_context_t *ctx) { - return PACKET_TYPE_LEN + fastd_max_inner_packet(ctx) + conf.max_overhead; +static inline size_t fastd_max_outer_packet(void) { + return PACKET_TYPE_LEN + fastd_max_inner_packet() + conf.max_overhead; } static inline bool fastd_peer_address_is_v6_ll(const fastd_peer_address_t *addr) { @@ -401,18 +403,18 @@ static inline int timespec_diff(const struct timespec *tp1, const struct timespe return ((tp1->tv_sec - tp2->tv_sec))*1000 + (tp1->tv_nsec - tp2->tv_nsec)/1e6; } -static inline bool fastd_timed_out(const fastd_context_t *ctx, const struct timespec *timeout) { - return !timespec_after(timeout, &ctx->now); +static inline bool fastd_timed_out(const struct timespec *timeout) { + return !timespec_after(timeout, &ctx.now); } -static inline struct timespec fastd_in_seconds(const fastd_context_t *ctx, int seconds) { - struct timespec ret = ctx->now; +static inline struct timespec fastd_in_seconds(const int seconds) { + struct timespec ret = ctx.now; ret.tv_sec += seconds; return ret; } -static inline void fastd_update_time(fastd_context_t *ctx) { - clock_gettime(CLOCK_MONOTONIC, &ctx->now); +static inline void fastd_update_time(void) { + clock_gettime(CLOCK_MONOTONIC, &ctx.now); } static inline bool strequal(const char *str1, const char *str2) { diff --git a/src/handshake.c b/src/handshake.c index f6a256d..2cb6cc3 100644 --- a/src/handshake.c +++ b/src/handshake.c @@ -97,7 +97,7 @@ static fastd_string_stack_t* parse_string_list(const uint8_t *data, size_t len) return ret; } -static fastd_buffer_t new_handshake(fastd_context_t *ctx, uint8_t type, const fastd_method_info_t *method, bool with_method_list, size_t tail_space) { +static fastd_buffer_t new_handshake(uint8_t type, const fastd_method_info_t *method, bool with_method_list, size_t tail_space) { size_t version_len = strlen(FASTD_VERSION); size_t protocol_len = strlen(conf.protocol->name); size_t method_len = method ? strlen(method->name) : 0; @@ -108,7 +108,7 @@ static fastd_buffer_t new_handshake(fastd_context_t *ctx, uint8_t type, const fa if (with_method_list) method_list = create_method_list(&method_list_len); - fastd_buffer_t buffer = fastd_buffer_alloc(ctx, sizeof(fastd_handshake_packet_t), 1, + fastd_buffer_t buffer = fastd_buffer_alloc(sizeof(fastd_handshake_packet_t), 1, 3*5 + /* handshake type, mode, reply code */ 6 + /* MTU */ 4+version_len + /* version name */ @@ -121,35 +121,35 @@ static fastd_buffer_t new_handshake(fastd_context_t *ctx, uint8_t type, const fa packet->rsv = 0; packet->tlv_len = 0; - fastd_handshake_add_uint8(ctx, &buffer, RECORD_HANDSHAKE_TYPE, type); - fastd_handshake_add_uint8(ctx, &buffer, RECORD_MODE, conf.mode); - fastd_handshake_add_uint16(ctx, &buffer, RECORD_MTU, conf.mtu); + fastd_handshake_add_uint8(&buffer, RECORD_HANDSHAKE_TYPE, type); + fastd_handshake_add_uint8(&buffer, RECORD_MODE, conf.mode); + fastd_handshake_add_uint16(&buffer, RECORD_MTU, conf.mtu); - fastd_handshake_add(ctx, &buffer, RECORD_VERSION_NAME, version_len, FASTD_VERSION); - fastd_handshake_add(ctx, &buffer, RECORD_PROTOCOL_NAME, protocol_len, conf.protocol->name); + fastd_handshake_add(&buffer, RECORD_VERSION_NAME, version_len, FASTD_VERSION); + fastd_handshake_add(&buffer, RECORD_PROTOCOL_NAME, protocol_len, conf.protocol->name); if (method && (!with_method_list || !conf.secure_handshakes)) - fastd_handshake_add(ctx, &buffer, RECORD_METHOD_NAME, method_len, method->name); + fastd_handshake_add(&buffer, RECORD_METHOD_NAME, method_len, method->name); if (with_method_list) { - fastd_handshake_add(ctx, &buffer, RECORD_METHOD_LIST, method_list_len, method_list); + fastd_handshake_add(&buffer, RECORD_METHOD_LIST, method_list_len, method_list); free(method_list); } return buffer; } -fastd_buffer_t fastd_handshake_new_init(fastd_context_t *ctx, size_t tail_space) { - return new_handshake(ctx, 1, NULL, !conf.secure_handshakes, tail_space); +fastd_buffer_t fastd_handshake_new_init(size_t tail_space) { + return new_handshake(1, NULL, !conf.secure_handshakes, tail_space); } -fastd_buffer_t fastd_handshake_new_reply(fastd_context_t *ctx, const fastd_handshake_t *handshake, const fastd_method_info_t *method, bool with_method_list, size_t tail_space) { - fastd_buffer_t buffer = new_handshake(ctx, handshake->type+1, method, with_method_list, tail_space); - fastd_handshake_add_uint8(ctx, &buffer, RECORD_REPLY_CODE, 0); +fastd_buffer_t fastd_handshake_new_reply(const fastd_handshake_t *handshake, const fastd_method_info_t *method, bool with_method_list, size_t tail_space) { + fastd_buffer_t buffer = new_handshake(handshake->type+1, method, with_method_list, tail_space); + fastd_handshake_add_uint8(&buffer, RECORD_REPLY_CODE, 0); return buffer; } -static void print_error(fastd_context_t *ctx, const char *prefix, const fastd_peer_address_t *remote_addr, uint8_t reply_code, uint8_t error_detail) { +static void print_error(const char *prefix, const fastd_peer_address_t *remote_addr, uint8_t reply_code, uint8_t error_detail) { const char *error_field_str; if (error_detail >= RECORD_MAX) @@ -162,32 +162,32 @@ static void print_error(fastd_context_t *ctx, const char *prefix, const fastd_pe break; case REPLY_MANDATORY_MISSING: - pr_warn(ctx, "Handshake with %I failed: %s error: mandatory field `%s' missing", remote_addr, prefix, error_field_str); + pr_warn("Handshake with %I failed: %s error: mandatory field `%s' missing", remote_addr, prefix, error_field_str); break; case REPLY_UNACCEPTABLE_VALUE: - pr_warn(ctx, "Handshake with %I failed: %s error: unacceptable value for field `%s'", remote_addr, prefix, error_field_str); + pr_warn("Handshake with %I failed: %s error: unacceptable value for field `%s'", remote_addr, prefix, error_field_str); break; default: - pr_warn(ctx, "Handshake with %I failed: %s error: unknown code %i", remote_addr, prefix, reply_code); + pr_warn("Handshake with %I failed: %s error: unknown code %i", remote_addr, prefix, reply_code); } } -static void send_error(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, const fastd_handshake_t *handshake, uint8_t reply_code, uint8_t error_detail) { - print_error(ctx, "sending", remote_addr, reply_code, error_detail); +static void send_error(fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, const fastd_handshake_t *handshake, uint8_t reply_code, uint8_t error_detail) { + print_error("sending", remote_addr, reply_code, error_detail); - fastd_buffer_t buffer = fastd_buffer_alloc(ctx, sizeof(fastd_handshake_packet_t), 0, 3*5 /* enough space for handshake type, reply code and error detail */); + fastd_buffer_t buffer = fastd_buffer_alloc(sizeof(fastd_handshake_packet_t), 0, 3*5 /* enough space for handshake type, reply code and error detail */); fastd_handshake_packet_t *reply = buffer.data; reply->rsv = 0; reply->tlv_len = 0; - fastd_handshake_add_uint8(ctx, &buffer, RECORD_HANDSHAKE_TYPE, handshake->type+1); - fastd_handshake_add_uint8(ctx, &buffer, RECORD_REPLY_CODE, reply_code); - fastd_handshake_add_uint8(ctx, &buffer, RECORD_ERROR_DETAIL, error_detail); + fastd_handshake_add_uint8(&buffer, RECORD_HANDSHAKE_TYPE, handshake->type+1); + fastd_handshake_add_uint8(&buffer, RECORD_REPLY_CODE, reply_code); + fastd_handshake_add_uint8(&buffer, RECORD_ERROR_DETAIL, error_detail); - fastd_send_handshake(ctx, sock, local_addr, remote_addr, peer, buffer); + fastd_send_handshake(sock, local_addr, remote_addr, peer, buffer); } static inline fastd_handshake_t parse_tlvs(const fastd_buffer_t *buffer) { @@ -232,27 +232,27 @@ static inline fastd_handshake_t parse_tlvs(const fastd_buffer_t *buffer) { return handshake; } -static inline void print_error_reply(fastd_context_t *ctx, const fastd_peer_address_t *remote_addr, const fastd_handshake_t *handshake) { +static inline void print_error_reply(const fastd_peer_address_t *remote_addr, const fastd_handshake_t *handshake) { uint8_t reply_code = AS_UINT8(handshake->records[RECORD_REPLY_CODE]); uint8_t error_detail = RECORD_MAX; if (handshake->records[RECORD_ERROR_DETAIL].length == 1) error_detail = AS_UINT8(handshake->records[RECORD_ERROR_DETAIL]); - print_error(ctx, "received", remote_addr, reply_code, error_detail); + print_error("received", remote_addr, reply_code, error_detail); } -static inline bool check_records(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, const fastd_handshake_t *handshake) { +static inline bool check_records(fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, const fastd_handshake_t *handshake) { if (handshake->records[RECORD_PROTOCOL_NAME].data) { if (!record_equal(conf.protocol->name, &handshake->records[RECORD_PROTOCOL_NAME])) { - send_error(ctx, sock, local_addr, remote_addr, peer, handshake, REPLY_UNACCEPTABLE_VALUE, RECORD_PROTOCOL_NAME); + send_error(sock, local_addr, remote_addr, peer, handshake, REPLY_UNACCEPTABLE_VALUE, RECORD_PROTOCOL_NAME); return false; } } if (handshake->records[RECORD_MODE].data) { if (handshake->records[RECORD_MODE].length != 1 || AS_UINT8(handshake->records[RECORD_MODE]) != conf.mode) { - send_error(ctx, sock, local_addr, remote_addr, peer, handshake, REPLY_UNACCEPTABLE_VALUE, RECORD_MODE); + send_error(sock, local_addr, remote_addr, peer, handshake, REPLY_UNACCEPTABLE_VALUE, RECORD_MODE); return false; } } @@ -260,7 +260,7 @@ static inline bool check_records(fastd_context_t *ctx, fastd_socket_t *sock, con if (!conf.secure_handshakes || handshake->type > 1) { if (handshake->records[RECORD_MTU].length == 2) { if (AS_UINT16(handshake->records[RECORD_MTU]) != conf.mtu) { - pr_warn(ctx, "MTU configuration differs with peer %I: local MTU is %u, remote MTU is %u", + pr_warn("MTU configuration differs with peer %I: local MTU is %u, remote MTU is %u", remote_addr, conf.mtu, AS_UINT16(handshake->records[RECORD_MTU])); } } @@ -268,12 +268,12 @@ static inline bool check_records(fastd_context_t *ctx, fastd_socket_t *sock, con if (handshake->type > 1) { if (handshake->records[RECORD_REPLY_CODE].length != 1) { - pr_warn(ctx, "received handshake reply without reply code from %I", remote_addr); + pr_warn("received handshake reply without reply code from %I", remote_addr); return false; } if (AS_UINT8(handshake->records[RECORD_REPLY_CODE]) != REPLY_SUCCESS) { - print_error_reply(ctx, remote_addr, handshake); + print_error_reply(remote_addr, handshake); return false; } } @@ -314,25 +314,25 @@ static inline const fastd_method_info_t* get_method(const fastd_handshake_t *han return get_method_by_name((const char*)handshake->records[RECORD_METHOD_NAME].data, handshake->records[RECORD_METHOD_NAME].length); } -void fastd_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer) { +void fastd_handshake_handle(fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer) { char *peer_version = NULL; const fastd_method_info_t *method = NULL; fastd_handshake_t handshake = parse_tlvs(&buffer); if (!handshake.tlv_data) { - pr_warn(ctx, "received a short handshake from %I", remote_addr); + pr_warn("received a short handshake from %I", remote_addr); goto end_free; } if (handshake.records[RECORD_HANDSHAKE_TYPE].length != 1) { - pr_debug(ctx, "received handshake without handshake type from %I", remote_addr); + pr_debug("received handshake without handshake type from %I", remote_addr); goto end_free; } handshake.type = AS_UINT8(handshake.records[RECORD_HANDSHAKE_TYPE]); - if (!check_records(ctx, sock, local_addr, remote_addr, peer, &handshake)) + if (!check_records(sock, local_addr, remote_addr, peer, &handshake)) goto end_free; if (!conf.secure_handshakes || handshake.type > 1) { @@ -343,11 +343,11 @@ void fastd_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock, const fa } if (handshake.type > 1 && !method) { - send_error(ctx, sock, local_addr, remote_addr, peer, &handshake, REPLY_UNACCEPTABLE_VALUE, RECORD_METHOD_LIST); + send_error(sock, local_addr, remote_addr, peer, &handshake, REPLY_UNACCEPTABLE_VALUE, RECORD_METHOD_LIST); goto end_free; } - conf.protocol->handshake_handle(ctx, sock, local_addr, remote_addr, peer, &handshake, method); + conf.protocol->handshake_handle(sock, local_addr, remote_addr, peer, &handshake, method); end_free: free(peer_version); diff --git a/src/handshake.h b/src/handshake.h index 5674b83..a401c2a 100644 --- a/src/handshake.h +++ b/src/handshake.h @@ -77,10 +77,10 @@ struct fastd_handshake { }; -fastd_buffer_t fastd_handshake_new_init(fastd_context_t *ctx, size_t tail_space); -fastd_buffer_t fastd_handshake_new_reply(fastd_context_t *ctx, const fastd_handshake_t *handshake, const fastd_method_info_t *method, bool with_method_list, size_t tail_space); +fastd_buffer_t fastd_handshake_new_init(size_t tail_space); +fastd_buffer_t fastd_handshake_new_reply(const fastd_handshake_t *handshake, const fastd_method_info_t *method, bool with_method_list, size_t tail_space); -void fastd_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer); +void fastd_handshake_handle(fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer); static inline void* fastd_handshake_tlv_data(const fastd_buffer_t *buffer) { @@ -93,11 +93,11 @@ static inline uint16_t fastd_handshake_tlv_len(const fastd_buffer_t *buffer) { return ntohs(packet->tlv_len); } -static inline uint8_t* fastd_handshake_extend(fastd_context_t *ctx, fastd_buffer_t *buffer, fastd_handshake_record_type_t type, size_t len) { +static inline uint8_t* fastd_handshake_extend(fastd_buffer_t *buffer, fastd_handshake_record_type_t type, size_t len) { uint8_t *dst = buffer->data + buffer->len; if (buffer->data + buffer->len + 4 + len > buffer->base + buffer->base_len) - exit_bug(ctx, "not enough buffer allocated for handshake"); + exit_bug("not enough buffer allocated for handshake"); buffer->len += 4 + len; @@ -112,27 +112,27 @@ static inline uint8_t* fastd_handshake_extend(fastd_context_t *ctx, fastd_buffer return dst+4; } -static inline void fastd_handshake_add(fastd_context_t *ctx, fastd_buffer_t *buffer, fastd_handshake_record_type_t type, size_t len, const void *data) { - uint8_t *dst = fastd_handshake_extend(ctx, buffer, type, len); +static inline void fastd_handshake_add(fastd_buffer_t *buffer, fastd_handshake_record_type_t type, size_t len, const void *data) { + uint8_t *dst = fastd_handshake_extend(buffer, type, len); memcpy(dst, data, len); } -static inline uint8_t* fastd_handshake_add_zero(fastd_context_t *ctx, fastd_buffer_t *buffer, fastd_handshake_record_type_t type, size_t len) { - uint8_t *dst = fastd_handshake_extend(ctx, buffer, type, len); +static inline uint8_t* fastd_handshake_add_zero(fastd_buffer_t *buffer, fastd_handshake_record_type_t type, size_t len) { + uint8_t *dst = fastd_handshake_extend(buffer, type, len); memset(dst, 0, len); return dst; } -static inline void fastd_handshake_add_uint8(fastd_context_t *ctx, fastd_buffer_t *buffer, fastd_handshake_record_type_t type, uint8_t value) { - uint8_t *dst = fastd_handshake_extend(ctx, buffer, type, 1); +static inline void fastd_handshake_add_uint8(fastd_buffer_t *buffer, fastd_handshake_record_type_t type, uint8_t value) { + uint8_t *dst = fastd_handshake_extend(buffer, type, 1); dst[0] = value; } -static inline void fastd_handshake_add_uint16(fastd_context_t *ctx, fastd_buffer_t *buffer, fastd_handshake_record_type_t type, uint16_t value) { - uint8_t *dst = fastd_handshake_extend(ctx, buffer, type, 2); +static inline void fastd_handshake_add_uint16(fastd_buffer_t *buffer, fastd_handshake_record_type_t type, uint16_t value) { + uint8_t *dst = fastd_handshake_extend(buffer, type, 2); dst[0] = value; dst[1] = value >> 8; diff --git a/src/log.c b/src/log.c index aa04517..9bcbd14 100644 --- a/src/log.c +++ b/src/log.c @@ -44,7 +44,7 @@ static inline size_t snprintf_safe(char *buffer, size_t size, const char *format return min_size_t(ret, size); } -static size_t snprint_peer_address(const fastd_context_t *ctx, char *buffer, size_t size, const fastd_peer_address_t *address, const char *iface, bool bind_address) { +static size_t snprint_peer_address(char *buffer, size_t size, const fastd_peer_address_t *address, const char *iface, bool bind_address) { char addr_buf[INET6_ADDRSTRLEN] = ""; switch (address->sa.sa_family) { @@ -79,24 +79,24 @@ static size_t snprint_peer_address(const fastd_context_t *ctx, char *buffer, siz return 0; default: - exit_bug(ctx, "unsupported address family"); + exit_bug("unsupported address family"); } } -static size_t snprint_peer_str(const fastd_context_t *ctx, char *buffer, size_t size, const fastd_peer_t *peer) { +static size_t snprint_peer_str(char *buffer, size_t size, const fastd_peer_t *peer) { if (peer->config && peer->config->name) { return snprintf_safe(buffer, size, "<%s>", peer->config->name); } else { char buf[65]; - if (conf.protocol->describe_peer(ctx, peer, buf, sizeof(buf))) + if (conf.protocol->describe_peer(peer, buf, sizeof(buf))) return snprintf_safe(buffer, size, "{%s}", buf); else return snprintf_safe(buffer, size, "(null)"); } } -static int fastd_vsnprintf(const fastd_context_t *ctx, char *buffer, size_t size, const char *format, va_list ap) { +static int fastd_vsnprintf(char *buffer, size_t size, const char *format, va_list ap) { char *buffer_start = buffer; char *buffer_end = buffer+size; @@ -159,7 +159,7 @@ static int fastd_vsnprintf(const fastd_context_t *ctx, char *buffer, size_t size p = va_arg(ap, const fastd_peer_t*); if (p) - buffer += snprint_peer_str(ctx, buffer, buffer_end-buffer, (const fastd_peer_t*)p); + buffer += snprint_peer_str(buffer, buffer_end-buffer, (const fastd_peer_t*)p); else buffer += snprintf_safe(buffer, buffer_end-buffer, "(null)"); break; @@ -172,13 +172,13 @@ static int fastd_vsnprintf(const fastd_context_t *ctx, char *buffer, size_t size iface = (*format == 'L') ? va_arg(ap, const char*) : NULL; if (p) - buffer += snprint_peer_address(ctx, buffer, buffer_end-buffer, (const fastd_peer_address_t*)p, iface, *format != 'I'); + buffer += snprint_peer_address(buffer, buffer_end-buffer, (const fastd_peer_address_t*)p, iface, *format != 'I'); else buffer += snprintf_safe(buffer, buffer_end-buffer, "(null)"); break; default: - pr_warn(ctx, "fastd_vsnprintf: unknown format conversion specifier '%c'", *format); + pr_warn("fastd_vsnprintf: unknown format conversion specifier '%c'", *format); *buffer_start = 0; return -1; } @@ -228,18 +228,18 @@ static inline int get_syslog_level(fastd_loglevel_t log_level) { } } -void fastd_logf(const fastd_context_t *ctx, fastd_loglevel_t level, const char *format, ...) { +void fastd_logf(fastd_loglevel_t level, const char *format, ...) { char buffer[1024]; char timestr[100] = ""; va_list ap; va_start(ap, format); - fastd_vsnprintf(ctx, buffer, sizeof(buffer), format, ap); + fastd_vsnprintf(buffer, sizeof(buffer), format, ap); va_end(ap); buffer[sizeof(buffer)-1] = 0; - if (!ctx->log_initialized || level <= conf.log_stderr_level || conf.log_files) { + if (!ctx.log_initialized || level <= conf.log_stderr_level || conf.log_files) { time_t t; struct tm tm; @@ -250,15 +250,15 @@ void fastd_logf(const fastd_context_t *ctx, fastd_loglevel_t level, const char * } } - if (!ctx->log_initialized || level <= conf.log_stderr_level) + if (!ctx.log_initialized || level <= conf.log_stderr_level) fprintf(stderr, "%s%s%s\n", timestr, get_log_prefix(level), buffer); - if (ctx->log_initialized) { + if (ctx.log_initialized) { if (level <= conf.log_syslog_level) syslog(get_syslog_level(level), "%s", buffer); fastd_log_fd_t *file; - for (file = ctx->log_files; file; file = file->next) { + for (file = ctx.log_files; file; file = file->next) { if (level <= file->config->level) dprintf(file->fd, "%s%s%s\n", timestr, get_log_prefix(level), buffer); } diff --git a/src/log.h b/src/log.h index 6e47590..0fe6b67 100644 --- a/src/log.h +++ b/src/log.h @@ -35,22 +35,22 @@ #define FASTD_DEFAULT_LOG_LEVEL LL_VERBOSE -void fastd_logf(const fastd_context_t *ctx, fastd_loglevel_t level, const char *format, ...); - -#define pr_fatal(ctx, args...) fastd_logf(ctx, LL_FATAL, args) -#define pr_error(ctx, args...) fastd_logf(ctx, LL_ERROR, args) -#define pr_warn(ctx, args...) fastd_logf(ctx, LL_WARN, args) -#define pr_info(ctx, args...) fastd_logf(ctx, LL_INFO, args) -#define pr_verbose(ctx, args...) fastd_logf(ctx, LL_VERBOSE, args) -#define pr_debug(ctx, args...) fastd_logf(ctx, LL_DEBUG, args) -#define pr_debug2(ctx, args...) fastd_logf(ctx, LL_DEBUG2, args) - -#define pr_error_errno(ctx, message) pr_error(ctx, "%s: %s", message, strerror(errno)) -#define pr_warn_errno(ctx, message) pr_warn(ctx, "%s: %s", message, strerror(errno)) -#define pr_debug_errno(ctx, message) pr_debug(ctx, "%s: %s", message, strerror(errno)) -#define pr_debug2_errno(ctx, message) pr_debug2(ctx, "%s: %s", message, strerror(errno)) - -#define exit_fatal(ctx, args...) do { pr_fatal(ctx, args); abort(); } while(0) -#define exit_bug(ctx, message) exit_fatal(ctx, "BUG: %s", message) -#define exit_error(ctx, args...) do { pr_error(ctx, args); exit(1); } while(0) -#define exit_errno(ctx, message) exit_error(ctx, "%s: %s", message, strerror(errno)) +void fastd_logf(const fastd_loglevel_t level, const char *format, ...); + +#define pr_fatal(args...) fastd_logf(LL_FATAL, args) +#define pr_error(args...) fastd_logf(LL_ERROR, args) +#define pr_warn(args...) fastd_logf(LL_WARN, args) +#define pr_info(args...) fastd_logf(LL_INFO, args) +#define pr_verbose(args...) fastd_logf(LL_VERBOSE, args) +#define pr_debug(args...) fastd_logf(LL_DEBUG, args) +#define pr_debug2(args...) fastd_logf(LL_DEBUG2, args) + +#define pr_error_errno(message) pr_error("%s: %s", message, strerror(errno)) +#define pr_warn_errno(message) pr_warn("%s: %s", message, strerror(errno)) +#define pr_debug_errno(message) pr_debug("%s: %s", message, strerror(errno)) +#define pr_debug2_errno(message) pr_debug2("%s: %s", message, strerror(errno)) + +#define exit_fatal(args...) do { pr_fatal(args); abort(); } while(0) +#define exit_bug(message) exit_fatal("BUG: %s", message) +#define exit_error(args...) do { pr_error(args); exit(1); } while(0) +#define exit_errno(message) exit_error("%s: %s", message, strerror(errno)) diff --git a/src/method.h b/src/method.h index b5ff2f5..d7f3304 100644 --- a/src/method.h +++ b/src/method.h @@ -45,18 +45,18 @@ struct fastd_method_provider { bool (*create_by_name)(const char *name, fastd_method_t **method); void (*destroy)(fastd_method_t *method); - size_t (*key_length)(fastd_context_t *ctx, const fastd_method_t *method); - - fastd_method_session_state_t* (*session_init)(fastd_context_t *ctx, const fastd_method_t *method, const uint8_t *secret, bool initiator); - fastd_method_session_state_t* (*session_init_compat)(fastd_context_t *ctx, const fastd_method_t *method, const uint8_t *secret, size_t length, bool initiator); - bool (*session_is_valid)(fastd_context_t *ctx, fastd_method_session_state_t *session); - bool (*session_is_initiator)(fastd_context_t *ctx, fastd_method_session_state_t *session); - bool (*session_want_refresh)(fastd_context_t *ctx, fastd_method_session_state_t *session); - void (*session_superseded)(fastd_context_t *ctx, fastd_method_session_state_t *session); - void (*session_free)(fastd_context_t *ctx, fastd_method_session_state_t *session); - - bool (*encrypt)(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in); - bool (*decrypt)(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in); + size_t (*key_length)(const fastd_method_t *method); + + fastd_method_session_state_t* (*session_init)(const fastd_method_t *method, const uint8_t *secret, bool initiator); + fastd_method_session_state_t* (*session_init_compat)(const fastd_method_t *method, const uint8_t *secret, size_t length, bool initiator); + bool (*session_is_valid)(fastd_method_session_state_t *session); + bool (*session_is_initiator)(fastd_method_session_state_t *session); + bool (*session_want_refresh)(fastd_method_session_state_t *session); + void (*session_superseded)(fastd_method_session_state_t *session); + void (*session_free)(fastd_method_session_state_t *session); + + bool (*encrypt)(fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in); + bool (*decrypt)(fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in); }; diff --git a/src/methods/cipher_test/cipher_test.c b/src/methods/cipher_test/cipher_test.c index 0cec4b0..52bca0f 100644 --- a/src/methods/cipher_test/cipher_test.c +++ b/src/methods/cipher_test/cipher_test.c @@ -70,49 +70,49 @@ static void method_destroy(fastd_method_t *method) { free(method); } -static size_t method_key_length(fastd_context_t *ctx UNUSED, const fastd_method_t *method) { +static size_t method_key_length(const fastd_method_t *method) { return method->cipher_info->key_length; } -static fastd_method_session_state_t* method_session_init(fastd_context_t *ctx, const fastd_method_t *method, const uint8_t *secret, bool initiator) { +static fastd_method_session_state_t* method_session_init(const fastd_method_t *method, const uint8_t *secret, bool initiator) { fastd_method_session_state_t *session = malloc(sizeof(fastd_method_session_state_t)); - fastd_method_common_init(ctx, &session->common, initiator); + fastd_method_common_init(&session->common, initiator); session->method = method; session->cipher = fastd_cipher_get(method->cipher_info); session->cipher_state = session->cipher->init(secret); - pr_warn(ctx, "using cipher-test method; this method must be used for testing and benchmarks only"); + pr_warn("using cipher-test method; this method must be used for testing and benchmarks only"); return session; } -static bool method_session_is_valid(fastd_context_t *ctx, fastd_method_session_state_t *session) { - return (session && fastd_method_session_common_is_valid(ctx, &session->common)); +static bool method_session_is_valid(fastd_method_session_state_t *session) { + return (session && fastd_method_session_common_is_valid(&session->common)); } -static bool method_session_is_initiator(fastd_context_t *ctx UNUSED, fastd_method_session_state_t *session) { +static bool method_session_is_initiator(fastd_method_session_state_t *session) { return fastd_method_session_common_is_initiator(&session->common); } -static bool method_session_want_refresh(fastd_context_t *ctx, fastd_method_session_state_t *session) { - return fastd_method_session_common_want_refresh(ctx, &session->common); +static bool method_session_want_refresh(fastd_method_session_state_t *session) { + return fastd_method_session_common_want_refresh(&session->common); } -static void method_session_superseded(fastd_context_t *ctx, fastd_method_session_state_t *session) { - fastd_method_session_common_superseded(ctx, &session->common); +static void method_session_superseded(fastd_method_session_state_t *session) { + fastd_method_session_common_superseded(&session->common); } -static void method_session_free(fastd_context_t *ctx UNUSED, fastd_method_session_state_t *session) { +static void method_session_free(fastd_method_session_state_t *session) { if (session) { session->cipher->free(session->cipher_state); free(session); } } -static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer UNUSED, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { +static bool method_encrypt(fastd_peer_t *peer UNUSED, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { size_t tail_len = alignto(in.len, sizeof(fastd_block128_t))-in.len; - *out = fastd_buffer_alloc(ctx, in.len, alignto(COMMON_HEADBYTES, 16), sizeof(fastd_block128_t)+tail_len); + *out = fastd_buffer_alloc(in.len, alignto(COMMON_HEADBYTES, 16), sizeof(fastd_block128_t)+tail_len); if (tail_len) memset(in.data+in.len, 0, tail_len); @@ -134,23 +134,23 @@ static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer UNUSED, fast fastd_buffer_free(in); - fastd_method_put_common_header(ctx, out, session->common.send_nonce, 0); + fastd_method_put_common_header(out, session->common.send_nonce, 0); fastd_method_increment_nonce(&session->common); return true; } -static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { +static bool method_decrypt(fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { if (in.len < COMMON_HEADBYTES) return false; - if (!method_session_is_valid(ctx, session)) + if (!method_session_is_valid(session)) return false; uint8_t in_nonce[COMMON_NONCEBYTES]; uint8_t flags; int64_t age; - if (!fastd_method_handle_common_header(ctx, &session->common, &in, in_nonce, &flags, &age)) + if (!fastd_method_handle_common_header(&session->common, &in, in_nonce, &flags, &age)) return false; if (flags) @@ -160,7 +160,7 @@ static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho fastd_method_expand_nonce(nonce, in_nonce, sizeof(nonce)); size_t tail_len = alignto(in.len, sizeof(fastd_block128_t))-in.len; - *out = fastd_buffer_alloc(ctx, in.len, 0, tail_len); + *out = fastd_buffer_alloc(in.len, 0, tail_len); int n_blocks = block_count(in.len, sizeof(fastd_block128_t)); @@ -174,9 +174,9 @@ static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho return false; } - if (!fastd_method_reorder_check(ctx, peer, &session->common, in_nonce, age)) { + if (!fastd_method_reorder_check(peer, &session->common, in_nonce, age)) { fastd_buffer_free(*out); - *out = fastd_buffer_alloc(ctx, 0, 0, 0); + *out = fastd_buffer_alloc(0, 0, 0); } fastd_buffer_free(in); diff --git a/src/methods/common.c b/src/methods/common.c index 166d3ab..17722cf 100644 --- a/src/methods/common.c +++ b/src/methods/common.c @@ -27,11 +27,11 @@ #include "common.h" -void fastd_method_common_init(fastd_context_t *ctx, fastd_method_common_t *session, bool initiator) { +void fastd_method_common_init(fastd_method_common_t *session, bool initiator) { memset(session, 0, sizeof(*session)); - session->valid_till = fastd_in_seconds(ctx, conf.key_valid); - session->refresh_after = fastd_in_seconds(ctx, conf.key_refresh - fastd_rand(ctx, 0, conf.key_refresh_splay)); + session->valid_till = fastd_in_seconds(conf.key_valid); + session->refresh_after = fastd_in_seconds(conf.key_refresh - fastd_rand(0, conf.key_refresh_splay)); if (initiator) { session->send_nonce[COMMON_NONCEBYTES-1] = 3; @@ -42,7 +42,7 @@ void fastd_method_common_init(fastd_context_t *ctx, fastd_method_common_t *sessi } } -bool fastd_method_is_nonce_valid(fastd_context_t *ctx, const fastd_method_common_t *session, const uint8_t nonce[COMMON_NONCEBYTES], int64_t *age) { +bool fastd_method_is_nonce_valid(const fastd_method_common_t *session, const uint8_t nonce[COMMON_NONCEBYTES], int64_t *age) { if ((nonce[0] & 1) != (session->receive_nonce[0] & 1)) return false; @@ -57,7 +57,7 @@ bool fastd_method_is_nonce_valid(fastd_context_t *ctx, const fastd_method_common *age >>= 1; if (*age >= 0) { - if (fastd_timed_out(ctx, &session->reorder_timeout)) + if (fastd_timed_out(&session->reorder_timeout)) return false; if (*age > 64) @@ -67,7 +67,7 @@ bool fastd_method_is_nonce_valid(fastd_context_t *ctx, const fastd_method_common return true; } -bool fastd_method_reorder_check(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_common_t *session, const uint8_t nonce[COMMON_NONCEBYTES], int64_t age) { +bool fastd_method_reorder_check(fastd_peer_t *peer, fastd_method_common_t *session, const uint8_t nonce[COMMON_NONCEBYTES], int64_t age) { if (age < 0) { size_t shift = age < (-64) ? 64 : ((size_t)-age); @@ -79,15 +79,15 @@ bool fastd_method_reorder_check(fastd_context_t *ctx, fastd_peer_t *peer, fastd_ session->receive_reorder_seen |= (1 << (shift-1)); memcpy(session->receive_nonce, nonce, COMMON_NONCEBYTES); - session->reorder_timeout = fastd_in_seconds(ctx, conf.reorder_time); + session->reorder_timeout = fastd_in_seconds(conf.reorder_time); return true; } else if (age == 0 || session->receive_reorder_seen & (1 << (age-1))) { - pr_debug(ctx, "dropping duplicate packet from %P (age %u)", peer, (unsigned)age); + pr_debug("dropping duplicate packet from %P (age %u)", peer, (unsigned)age); return false; } else { - pr_debug2(ctx, "accepting reordered packet from %P (age %u)", peer, (unsigned)age); + pr_debug2("accepting reordered packet from %P (age %u)", peer, (unsigned)age); session->receive_reorder_seen |= (1 << (age-1)); return true; } diff --git a/src/methods/common.h b/src/methods/common.h index 510ad3f..ea4a13a 100644 --- a/src/methods/common.h +++ b/src/methods/common.h @@ -46,34 +46,34 @@ typedef struct fastd_method_common { } fastd_method_common_t; -void fastd_method_common_init(fastd_context_t *ctx, fastd_method_common_t *session, bool initiator); -bool fastd_method_is_nonce_valid(fastd_context_t *ctx, const fastd_method_common_t *session, const uint8_t nonce[COMMON_NONCEBYTES], int64_t *age); -bool fastd_method_reorder_check(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_common_t *session, const uint8_t nonce[COMMON_NONCEBYTES], int64_t age); +void fastd_method_common_init(fastd_method_common_t *session, bool initiator); +bool fastd_method_is_nonce_valid(const fastd_method_common_t *session, const uint8_t nonce[COMMON_NONCEBYTES], int64_t *age); +bool fastd_method_reorder_check(fastd_peer_t *peer, fastd_method_common_t *session, const uint8_t nonce[COMMON_NONCEBYTES], int64_t age); -static inline bool fastd_method_session_common_is_valid(fastd_context_t *ctx, const fastd_method_common_t *session) { +static inline bool fastd_method_session_common_is_valid(const fastd_method_common_t *session) { if (session->send_nonce[0] == 0xff && session->send_nonce[1] == 0xff) return false; - return (!fastd_timed_out(ctx, &session->valid_till)); + return (!fastd_timed_out(&session->valid_till)); } static inline bool fastd_method_session_common_is_initiator(const fastd_method_common_t *session) { return (session->send_nonce[COMMON_NONCEBYTES-1] & 1); } -static inline bool fastd_method_session_common_want_refresh(fastd_context_t *ctx, const fastd_method_common_t *session) { +static inline bool fastd_method_session_common_want_refresh(const fastd_method_common_t *session) { if (session->send_nonce[0] == 0xff) return true; - if (fastd_method_session_common_is_initiator(session) && fastd_timed_out(ctx, &session->refresh_after)) + if (fastd_method_session_common_is_initiator(session) && fastd_timed_out(&session->refresh_after)) return true; return false; } -static inline void fastd_method_session_common_superseded(fastd_context_t *ctx, fastd_method_common_t *session) { - struct timespec valid_max = fastd_in_seconds(ctx, conf.key_valid_old); +static inline void fastd_method_session_common_superseded(fastd_method_common_t *session) { + struct timespec valid_max = fastd_in_seconds(conf.key_valid_old); if (timespec_after(&session->valid_till, &valid_max)) session->valid_till = valid_max; @@ -91,19 +91,19 @@ static inline void fastd_method_increment_nonce(fastd_method_common_t *session) } } -static inline void fastd_method_put_common_header(fastd_context_t *ctx, fastd_buffer_t *buffer, const uint8_t nonce[COMMON_NONCEBYTES], uint8_t flags) { - fastd_buffer_pull_head_from(ctx, buffer, nonce, COMMON_NONCEBYTES); - fastd_buffer_pull_head_from(ctx, buffer, &flags, 1); +static inline void fastd_method_put_common_header(fastd_buffer_t *buffer, const uint8_t nonce[COMMON_NONCEBYTES], uint8_t flags) { + fastd_buffer_pull_head_from(buffer, nonce, COMMON_NONCEBYTES); + fastd_buffer_pull_head_from(buffer, &flags, 1); } -static inline void fastd_method_take_common_header(fastd_context_t *ctx, fastd_buffer_t *buffer, uint8_t nonce[COMMON_NONCEBYTES], uint8_t *flags) { - fastd_buffer_push_head_to(ctx, buffer, flags, 1); - fastd_buffer_push_head_to(ctx, buffer, nonce, COMMON_NONCEBYTES); +static inline void fastd_method_take_common_header(fastd_buffer_t *buffer, uint8_t nonce[COMMON_NONCEBYTES], uint8_t *flags) { + fastd_buffer_push_head_to(buffer, flags, 1); + fastd_buffer_push_head_to(buffer, nonce, COMMON_NONCEBYTES); } -static inline bool fastd_method_handle_common_header(fastd_context_t *ctx, const fastd_method_common_t *session, fastd_buffer_t *buffer, uint8_t nonce[COMMON_NONCEBYTES], uint8_t *flags, int64_t *age) { - fastd_method_take_common_header(ctx, buffer, nonce, flags); - return fastd_method_is_nonce_valid(ctx, session, nonce, age); +static inline bool fastd_method_handle_common_header(const fastd_method_common_t *session, fastd_buffer_t *buffer, uint8_t nonce[COMMON_NONCEBYTES], uint8_t *flags, int64_t *age) { + fastd_method_take_common_header(buffer, nonce, flags); + return fastd_method_is_nonce_valid(session, nonce, age); } diff --git a/src/methods/composed_gmac/composed_gmac.c b/src/methods/composed_gmac/composed_gmac.c index c848bbf..887fb5c 100644 --- a/src/methods/composed_gmac/composed_gmac.c +++ b/src/methods/composed_gmac/composed_gmac.c @@ -112,14 +112,14 @@ static void method_destroy(fastd_method_t *method) { } -static size_t method_key_length(fastd_context_t *ctx UNUSED, const fastd_method_t *method) { +static size_t method_key_length(const fastd_method_t *method) { return method->cipher_info->key_length + method->gmac_cipher_info->key_length; } -static fastd_method_session_state_t* method_session_init(fastd_context_t *ctx, const fastd_method_t *method, const uint8_t *secret, bool initiator) { +static fastd_method_session_state_t* method_session_init(const fastd_method_t *method, const uint8_t *secret, bool initiator) { fastd_method_session_state_t *session = malloc(sizeof(fastd_method_session_state_t)); - fastd_method_common_init(ctx, &session->common, initiator); + fastd_method_common_init(&session->common, initiator); session->method = method; session->cipher = fastd_cipher_get(method->cipher_info); @@ -148,23 +148,23 @@ static fastd_method_session_state_t* method_session_init(fastd_context_t *ctx, c return session; } -static bool method_session_is_valid(fastd_context_t *ctx, fastd_method_session_state_t *session) { - return (session && fastd_method_session_common_is_valid(ctx, &session->common)); +static bool method_session_is_valid(fastd_method_session_state_t *session) { + return (session && fastd_method_session_common_is_valid(&session->common)); } -static bool method_session_is_initiator(fastd_context_t *ctx UNUSED, fastd_method_session_state_t *session) { +static bool method_session_is_initiator(fastd_method_session_state_t *session) { return fastd_method_session_common_is_initiator(&session->common); } -static bool method_session_want_refresh(fastd_context_t *ctx, fastd_method_session_state_t *session) { - return fastd_method_session_common_want_refresh(ctx, &session->common); +static bool method_session_want_refresh(fastd_method_session_state_t *session) { + return fastd_method_session_common_want_refresh(&session->common); } -static void method_session_superseded(fastd_context_t *ctx, fastd_method_session_state_t *session) { - fastd_method_session_common_superseded(ctx, &session->common); +static void method_session_superseded(fastd_method_session_state_t *session) { + fastd_method_session_common_superseded(&session->common); } -static void method_session_free(fastd_context_t *ctx UNUSED, fastd_method_session_state_t *session) { +static void method_session_free(fastd_method_session_state_t *session) { if (session) { session->cipher->free(session->cipher_state); session->gmac_cipher->free(session->gmac_cipher_state); @@ -183,9 +183,9 @@ static inline void put_size(fastd_block128_t *out, size_t len) { out->b[7] = len << 3; } -static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer UNUSED, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { +static bool method_encrypt(fastd_peer_t *peer UNUSED, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { size_t tail_len = alignto(in.len, sizeof(fastd_block128_t))-in.len; - *out = fastd_buffer_alloc(ctx, sizeof(fastd_block128_t)+in.len, alignto(COMMON_HEADBYTES, 16), sizeof(fastd_block128_t)+tail_len); + *out = fastd_buffer_alloc(sizeof(fastd_block128_t)+in.len, alignto(COMMON_HEADBYTES, 16), sizeof(fastd_block128_t)+tail_len); if (tail_len) memset(in.data+in.len, 0, tail_len); @@ -226,23 +226,23 @@ static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer UNUSED, fast fastd_buffer_free(in); - fastd_method_put_common_header(ctx, out, session->common.send_nonce, 0); + fastd_method_put_common_header(out, session->common.send_nonce, 0); fastd_method_increment_nonce(&session->common); return true; } -static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { +static bool method_decrypt(fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { if (in.len < COMMON_HEADBYTES+sizeof(fastd_block128_t)) return false; - if (!method_session_is_valid(ctx, session)) + if (!method_session_is_valid(session)) return false; uint8_t in_nonce[COMMON_NONCEBYTES]; uint8_t flags; int64_t age; - if (!fastd_method_handle_common_header(ctx, &session->common, &in, in_nonce, &flags, &age)) + if (!fastd_method_handle_common_header(&session->common, &in, in_nonce, &flags, &age)) return false; if (flags) @@ -255,7 +255,7 @@ static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho fastd_method_expand_nonce(gmac_nonce, in_nonce, sizeof(gmac_nonce)); size_t tail_len = alignto(in.len, sizeof(fastd_block128_t))-in.len; - *out = fastd_buffer_alloc(ctx, in.len, 0, tail_len); + *out = fastd_buffer_alloc(in.len, 0, tail_len); int n_blocks = block_count(in.len, sizeof(fastd_block128_t)); @@ -282,11 +282,11 @@ static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho return false; } - fastd_buffer_push_head(ctx, out, sizeof(fastd_block128_t)); + fastd_buffer_push_head(out, sizeof(fastd_block128_t)); - if (!fastd_method_reorder_check(ctx, peer, &session->common, in_nonce, age)) { + if (!fastd_method_reorder_check(peer, &session->common, in_nonce, age)) { fastd_buffer_free(*out); - *out = fastd_buffer_alloc(ctx, 0, 0, 0); + *out = fastd_buffer_alloc(0, 0, 0); } fastd_buffer_free(in); diff --git a/src/methods/generic_gmac/generic_gmac.c b/src/methods/generic_gmac/generic_gmac.c index 88c1880..6b84a95 100644 --- a/src/methods/generic_gmac/generic_gmac.c +++ b/src/methods/generic_gmac/generic_gmac.c @@ -89,14 +89,14 @@ static void method_destroy(fastd_method_t *method) { free(method); } -static size_t method_key_length(fastd_context_t *ctx UNUSED, const fastd_method_t *method) { +static size_t method_key_length(const fastd_method_t *method) { return method->cipher_info->key_length; } -static fastd_method_session_state_t* method_session_init(fastd_context_t *ctx, const fastd_method_t *method, const uint8_t *secret, bool initiator) { +static fastd_method_session_state_t* method_session_init(const fastd_method_t *method, const uint8_t *secret, bool initiator) { fastd_method_session_state_t *session = malloc(sizeof(fastd_method_session_state_t)); - fastd_method_common_init(ctx, &session->common, initiator); + fastd_method_common_init(&session->common, initiator); session->method = method; session->cipher = fastd_cipher_get(method->cipher_info); @@ -121,23 +121,23 @@ static fastd_method_session_state_t* method_session_init(fastd_context_t *ctx, c return session; } -static bool method_session_is_valid(fastd_context_t *ctx, fastd_method_session_state_t *session) { - return (session && fastd_method_session_common_is_valid(ctx, &session->common)); +static bool method_session_is_valid(fastd_method_session_state_t *session) { + return (session && fastd_method_session_common_is_valid(&session->common)); } -static bool method_session_is_initiator(fastd_context_t *ctx UNUSED, fastd_method_session_state_t *session) { +static bool method_session_is_initiator(fastd_method_session_state_t *session) { return fastd_method_session_common_is_initiator(&session->common); } -static bool method_session_want_refresh(fastd_context_t *ctx, fastd_method_session_state_t *session) { - return fastd_method_session_common_want_refresh(ctx, &session->common); +static bool method_session_want_refresh(fastd_method_session_state_t *session) { + return fastd_method_session_common_want_refresh(&session->common); } -static void method_session_superseded(fastd_context_t *ctx, fastd_method_session_state_t *session) { - fastd_method_session_common_superseded(ctx, &session->common); +static void method_session_superseded(fastd_method_session_state_t *session) { + fastd_method_session_common_superseded(&session->common); } -static void method_session_free(fastd_context_t *ctx UNUSED, fastd_method_session_state_t *session) { +static void method_session_free(fastd_method_session_state_t *session) { if (session) { session->cipher->free(session->cipher_state); session->ghash->free(session->ghash_state); @@ -155,11 +155,11 @@ static inline void put_size(fastd_block128_t *out, size_t len) { out->b[15] = len << 3; } -static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer UNUSED, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { - fastd_buffer_pull_head_zero(ctx, &in, sizeof(fastd_block128_t)); +static bool method_encrypt(fastd_peer_t *peer UNUSED, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { + fastd_buffer_pull_head_zero(&in, sizeof(fastd_block128_t)); size_t tail_len = alignto(in.len, sizeof(fastd_block128_t))-in.len; - *out = fastd_buffer_alloc(ctx, in.len, alignto(COMMON_HEADBYTES, 16), sizeof(fastd_block128_t)+tail_len); + *out = fastd_buffer_alloc(in.len, alignto(COMMON_HEADBYTES, 16), sizeof(fastd_block128_t)+tail_len); if (tail_len) memset(in.data+in.len, 0, tail_len); @@ -193,23 +193,23 @@ static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer UNUSED, fast fastd_buffer_free(in); - fastd_method_put_common_header(ctx, out, session->common.send_nonce, 0); + fastd_method_put_common_header(out, session->common.send_nonce, 0); fastd_method_increment_nonce(&session->common); return true; } -static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { +static bool method_decrypt(fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { if (in.len < COMMON_HEADBYTES+sizeof(fastd_block128_t)) return false; - if (!method_session_is_valid(ctx, session)) + if (!method_session_is_valid(session)) return false; uint8_t in_nonce[COMMON_NONCEBYTES]; uint8_t flags; int64_t age; - if (!fastd_method_handle_common_header(ctx, &session->common, &in, in_nonce, &flags, &age)) + if (!fastd_method_handle_common_header(&session->common, &in, in_nonce, &flags, &age)) return false; if (flags) @@ -219,7 +219,7 @@ static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho fastd_method_expand_nonce(nonce, in_nonce, sizeof(nonce)); size_t tail_len = alignto(in.len, sizeof(fastd_block128_t))-in.len; - *out = fastd_buffer_alloc(ctx, in.len, 0, tail_len); + *out = fastd_buffer_alloc(in.len, 0, tail_len); int n_blocks = block_count(in.len, sizeof(fastd_block128_t)); @@ -245,11 +245,11 @@ static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho fastd_buffer_free(in); - fastd_buffer_push_head(ctx, out, sizeof(fastd_block128_t)); + fastd_buffer_push_head(out, sizeof(fastd_block128_t)); - if (!fastd_method_reorder_check(ctx, peer, &session->common, in_nonce, age)) { + if (!fastd_method_reorder_check(peer, &session->common, in_nonce, age)) { fastd_buffer_free(*out); - *out = fastd_buffer_alloc(ctx, 0, 0, 0); + *out = fastd_buffer_alloc(0, 0, 0); } return true; diff --git a/src/methods/generic_poly1305/generic_poly1305.c b/src/methods/generic_poly1305/generic_poly1305.c index a8eb3d1..0e2641c 100644 --- a/src/methods/generic_poly1305/generic_poly1305.c +++ b/src/methods/generic_poly1305/generic_poly1305.c @@ -79,14 +79,14 @@ static void method_destroy(fastd_method_t *method) { free(method); } -static size_t method_key_length(fastd_context_t *ctx UNUSED, const fastd_method_t *method) { +static size_t method_key_length(const fastd_method_t *method) { return method->cipher_info->key_length; } -static fastd_method_session_state_t* method_session_init(fastd_context_t *ctx, const fastd_method_t *method, const uint8_t *secret, bool initiator) { +static fastd_method_session_state_t* method_session_init(const fastd_method_t *method, const uint8_t *secret, bool initiator) { fastd_method_session_state_t *session = malloc(sizeof(fastd_method_session_state_t)); - fastd_method_common_init(ctx, &session->common, initiator); + fastd_method_common_init(&session->common, initiator); session->method = method; session->cipher = fastd_cipher_get(session->method->cipher_info); session->cipher_state = session->cipher->init(secret); @@ -94,34 +94,34 @@ static fastd_method_session_state_t* method_session_init(fastd_context_t *ctx, c return session; } -static bool method_session_is_valid(fastd_context_t *ctx, fastd_method_session_state_t *session) { - return (session && fastd_method_session_common_is_valid(ctx, &session->common)); +static bool method_session_is_valid(fastd_method_session_state_t *session) { + return (session && fastd_method_session_common_is_valid(&session->common)); } -static bool method_session_is_initiator(fastd_context_t *ctx UNUSED, fastd_method_session_state_t *session) { +static bool method_session_is_initiator(fastd_method_session_state_t *session) { return fastd_method_session_common_is_initiator(&session->common); } -static bool method_session_want_refresh(fastd_context_t *ctx, fastd_method_session_state_t *session) { - return fastd_method_session_common_want_refresh(ctx, &session->common); +static bool method_session_want_refresh(fastd_method_session_state_t *session) { + return fastd_method_session_common_want_refresh(&session->common); } -static void method_session_superseded(fastd_context_t *ctx, fastd_method_session_state_t *session) { - fastd_method_session_common_superseded(ctx, &session->common); +static void method_session_superseded(fastd_method_session_state_t *session) { + fastd_method_session_common_superseded(&session->common); } -static void method_session_free(fastd_context_t *ctx UNUSED, fastd_method_session_state_t *session) { +static void method_session_free(fastd_method_session_state_t *session) { if (session) { session->cipher->free(session->cipher_state); free(session); } } -static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer UNUSED, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { - fastd_buffer_pull_head_zero(ctx, &in, KEYBYTES); +static bool method_encrypt(fastd_peer_t *peer UNUSED, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { + fastd_buffer_pull_head_zero(&in, KEYBYTES); size_t tail_len = alignto(in.len, sizeof(fastd_block128_t))-in.len; - *out = fastd_buffer_alloc(ctx, in.len, alignto(COMMON_HEADBYTES, 16), sizeof(fastd_block128_t)+tail_len); + *out = fastd_buffer_alloc(in.len, alignto(COMMON_HEADBYTES, 16), sizeof(fastd_block128_t)+tail_len); if (tail_len) memset(in.data+in.len, 0, tail_len); @@ -144,28 +144,28 @@ static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer UNUSED, fast crypto_onetimeauth_poly1305(tag, outblocks->b+KEYBYTES, in.len - KEYBYTES, outblocks->b); - fastd_buffer_push_head(ctx, out, KEYBYTES); - fastd_buffer_pull_head_from(ctx, out, tag, TAGBYTES); + fastd_buffer_push_head(out, KEYBYTES); + fastd_buffer_pull_head_from(out, tag, TAGBYTES); fastd_buffer_free(in); - fastd_method_put_common_header(ctx, out, session->common.send_nonce, 0); + fastd_method_put_common_header(out, session->common.send_nonce, 0); fastd_method_increment_nonce(&session->common); return true; } -static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { +static bool method_decrypt(fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { if (in.len < COMMON_HEADBYTES+TAGBYTES) return false; - if (!method_session_is_valid(ctx, session)) + if (!method_session_is_valid(session)) return false; uint8_t in_nonce[COMMON_NONCEBYTES]; uint8_t flags; int64_t age; - if (!fastd_method_handle_common_header(ctx, &session->common, &in, in_nonce, &flags, &age)) + if (!fastd_method_handle_common_header(&session->common, &in, in_nonce, &flags, &age)) return false; if (flags) @@ -175,11 +175,11 @@ static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho fastd_method_expand_nonce(nonce, in_nonce, sizeof(nonce)); uint8_t tag[TAGBYTES] __attribute__((aligned(8))); - fastd_buffer_push_head_to(ctx, &in, tag, TAGBYTES); - fastd_buffer_pull_head_zero(ctx, &in, KEYBYTES); + fastd_buffer_push_head_to(&in, tag, TAGBYTES); + fastd_buffer_pull_head_zero(&in, KEYBYTES); size_t tail_len = alignto(in.len, sizeof(fastd_block128_t))-in.len; - *out = fastd_buffer_alloc(ctx, in.len, 0, tail_len); + *out = fastd_buffer_alloc(in.len, 0, tail_len); int n_blocks = block_count(in.len, sizeof(fastd_block128_t)); fastd_block128_t *inblocks = in.data; @@ -198,20 +198,20 @@ static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho fastd_buffer_free(*out); /* restore input buffer */ - fastd_buffer_push_head(ctx, &in, KEYBYTES); - fastd_buffer_pull_head_from(ctx, &in, tag, TAGBYTES); - fastd_method_put_common_header(ctx, &in, in_nonce, 0); + fastd_buffer_push_head(&in, KEYBYTES); + fastd_buffer_pull_head_from(&in, tag, TAGBYTES); + fastd_method_put_common_header(&in, in_nonce, 0); return false; } fastd_buffer_free(in); - fastd_buffer_push_head(ctx, out, KEYBYTES); + fastd_buffer_push_head(out, KEYBYTES); - if (!fastd_method_reorder_check(ctx, peer, &session->common, in_nonce, age)) { + if (!fastd_method_reorder_check(peer, &session->common, in_nonce, age)) { fastd_buffer_free(*out); - *out = fastd_buffer_alloc(ctx, 0, 0, 0); + *out = fastd_buffer_alloc(0, 0, 0); } return true; diff --git a/src/methods/null/null.c b/src/methods/null/null.c index 9c7dc43..76fd961 100644 --- a/src/methods/null/null.c +++ b/src/methods/null/null.c @@ -40,11 +40,11 @@ static bool method_create_by_name(const char *name, fastd_method_t **method UNUS static void method_destroy(fastd_method_t *method UNUSED) { } -static size_t method_key_length(fastd_context_t *ctx UNUSED, const fastd_method_t *method UNUSED) { +static size_t method_key_length(const fastd_method_t *method UNUSED) { return 0; } -static fastd_method_session_state_t* method_session_init(fastd_context_t *ctx UNUSED, const fastd_method_t *method UNUSED, const uint8_t *secret UNUSED, bool initiator) { +static fastd_method_session_state_t* method_session_init(const fastd_method_t *method UNUSED, const uint8_t *secret UNUSED, bool initiator) { fastd_method_session_state_t *session = malloc(sizeof(fastd_method_session_state_t)); session->valid = true; @@ -53,31 +53,31 @@ static fastd_method_session_state_t* method_session_init(fastd_context_t *ctx UN return session; } -static fastd_method_session_state_t* method_session_init_compat(fastd_context_t *ctx, const fastd_method_t *method, const uint8_t *secret, size_t length UNUSED, bool initiator) { - return method_session_init(ctx, method, secret, initiator); +static fastd_method_session_state_t* method_session_init_compat(const fastd_method_t *method, const uint8_t *secret, size_t length UNUSED, bool initiator) { + return method_session_init(method, secret, initiator); } -static bool method_session_is_valid(fastd_context_t *ctx UNUSED, fastd_method_session_state_t *session) { +static bool method_session_is_valid(fastd_method_session_state_t *session) { return (session && session->valid); } -static bool method_session_is_initiator(fastd_context_t *ctx UNUSED, fastd_method_session_state_t *session) { +static bool method_session_is_initiator(fastd_method_session_state_t *session) { return (session->initiator); } -static bool method_session_want_refresh(fastd_context_t *ctx UNUSED, fastd_method_session_state_t *session UNUSED) { +static bool method_session_want_refresh(fastd_method_session_state_t *session UNUSED) { return false; } -static void method_session_superseded(fastd_context_t *ctx UNUSED, fastd_method_session_state_t *session) { +static void method_session_superseded(fastd_method_session_state_t *session) { session->valid = false; } -static void method_session_free(fastd_context_t *ctx UNUSED, fastd_method_session_state_t *session) { +static void method_session_free(fastd_method_session_state_t *session) { free(session); } -static bool method_passthrough(fastd_context_t *ctx UNUSED, fastd_peer_t *peer UNUSED, fastd_method_session_state_t *session UNUSED, fastd_buffer_t *out, fastd_buffer_t in) { +static bool method_passthrough(fastd_peer_t *peer UNUSED, fastd_method_session_state_t *session UNUSED, fastd_buffer_t *out, fastd_buffer_t in) { *out = in; return true; } diff --git a/src/methods/xsalsa20_poly1305/xsalsa20_poly1305.c b/src/methods/xsalsa20_poly1305/xsalsa20_poly1305.c index e11826f..f06e0f8 100644 --- a/src/methods/xsalsa20_poly1305/xsalsa20_poly1305.c +++ b/src/methods/xsalsa20_poly1305/xsalsa20_poly1305.c @@ -45,44 +45,44 @@ static bool method_create_by_name(const char *name, fastd_method_t **method UNUS static void method_destroy(fastd_method_t *method UNUSED) { } -static size_t method_key_length(fastd_context_t *ctx UNUSED, const fastd_method_t *method UNUSED) { +static size_t method_key_length(const fastd_method_t *method UNUSED) { return crypto_secretbox_xsalsa20poly1305_KEYBYTES; } -static fastd_method_session_state_t* method_session_init(fastd_context_t *ctx, const fastd_method_t *method UNUSED, const uint8_t *secret, bool initiator) { +static fastd_method_session_state_t* method_session_init(const fastd_method_t *method UNUSED, const uint8_t *secret, bool initiator) { fastd_method_session_state_t *session = malloc(sizeof(fastd_method_session_state_t)); - fastd_method_common_init(ctx, &session->common, initiator); + fastd_method_common_init(&session->common, initiator); memcpy(session->key, secret, crypto_secretbox_xsalsa20poly1305_KEYBYTES); return session; } -static fastd_method_session_state_t* method_session_init_compat(fastd_context_t *ctx, const fastd_method_t *method, const uint8_t *secret, size_t length, bool initiator) { +static fastd_method_session_state_t* method_session_init_compat(const fastd_method_t *method, const uint8_t *secret, size_t length, bool initiator) { if (length < crypto_secretbox_xsalsa20poly1305_KEYBYTES) - exit_bug(ctx, "xsalsa20-poly1305: tried to init with short secret"); + exit_bug("xsalsa20-poly1305: tried to init with short secret"); - return method_session_init(ctx, method, secret, initiator); + return method_session_init(method, secret, initiator); } -static bool method_session_is_valid(fastd_context_t *ctx, fastd_method_session_state_t *session) { - return (session && fastd_method_session_common_is_valid(ctx, &session->common)); +static bool method_session_is_valid(fastd_method_session_state_t *session) { + return (session && fastd_method_session_common_is_valid(&session->common)); } -static bool method_session_is_initiator(fastd_context_t *ctx UNUSED, fastd_method_session_state_t *session) { +static bool method_session_is_initiator(fastd_method_session_state_t *session) { return fastd_method_session_common_is_initiator(&session->common); } -static bool method_session_want_refresh(fastd_context_t *ctx, fastd_method_session_state_t *session) { - return fastd_method_session_common_want_refresh(ctx, &session->common); +static bool method_session_want_refresh(fastd_method_session_state_t *session) { + return fastd_method_session_common_want_refresh(&session->common); } -static void method_session_superseded(fastd_context_t *ctx, fastd_method_session_state_t *session) { - fastd_method_session_common_superseded(ctx, &session->common); +static void method_session_superseded(fastd_method_session_state_t *session) { + fastd_method_session_common_superseded(&session->common); } -static void method_session_free(fastd_context_t *ctx UNUSED, fastd_method_session_state_t *session) { +static void method_session_free(fastd_method_session_state_t *session) { if(session) { secure_memzero(session, sizeof(fastd_method_session_state_t)); free(session); @@ -96,30 +96,30 @@ static inline void memcpy_nonce(uint8_t *dst, const uint8_t *src) { dst[i] = src[COMMON_NONCEBYTES-i-1]; } -static inline void put_header(fastd_context_t *ctx, fastd_buffer_t *buffer, const uint8_t nonce[COMMON_NONCEBYTES], uint8_t flags) { - fastd_buffer_pull_head_from(ctx, buffer, &flags, 1); +static inline void put_header(fastd_buffer_t *buffer, const uint8_t nonce[COMMON_NONCEBYTES], uint8_t flags) { + fastd_buffer_pull_head_from(buffer, &flags, 1); - fastd_buffer_pull_head(ctx, buffer, COMMON_NONCEBYTES); + fastd_buffer_pull_head(buffer, COMMON_NONCEBYTES); memcpy_nonce(buffer->data, nonce); } -static inline void take_header(fastd_context_t *ctx, fastd_buffer_t *buffer, uint8_t nonce[COMMON_NONCEBYTES], uint8_t *flags) { +static inline void take_header(fastd_buffer_t *buffer, uint8_t nonce[COMMON_NONCEBYTES], uint8_t *flags) { memcpy_nonce(nonce, buffer->data ); - fastd_buffer_push_head(ctx, buffer, COMMON_NONCEBYTES); + fastd_buffer_push_head(buffer, COMMON_NONCEBYTES); - fastd_buffer_push_head_to(ctx, buffer, flags, 1); + fastd_buffer_push_head_to(buffer, flags, 1); } -static inline bool handle_header(fastd_context_t *ctx, const fastd_method_common_t *session, fastd_buffer_t *buffer, uint8_t nonce[COMMON_NONCEBYTES], uint8_t *flags, int64_t *age) { - take_header(ctx, buffer, nonce, flags); - return fastd_method_is_nonce_valid(ctx, session, nonce, age); +static inline bool handle_header(const fastd_method_common_t *session, fastd_buffer_t *buffer, uint8_t nonce[COMMON_NONCEBYTES], uint8_t *flags, int64_t *age) { + take_header(buffer, nonce, flags); + return fastd_method_is_nonce_valid(session, nonce, age); } -static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer UNUSED, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { - fastd_buffer_pull_head_zero(ctx, &in, crypto_secretbox_xsalsa20poly1305_ZEROBYTES); +static bool method_encrypt(fastd_peer_t *peer UNUSED, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { + fastd_buffer_pull_head_zero(&in, crypto_secretbox_xsalsa20poly1305_ZEROBYTES); - *out = fastd_buffer_alloc(ctx, in.len, 0, 0); + *out = fastd_buffer_alloc(in.len, 0, 0); uint8_t nonce[crypto_secretbox_xsalsa20poly1305_NONCEBYTES] __attribute__((aligned(8))) = {}; memcpy_nonce(nonce, session->common.send_nonce); @@ -128,24 +128,24 @@ static bool method_encrypt(fastd_context_t *ctx, fastd_peer_t *peer UNUSED, fast fastd_buffer_free(in); - fastd_buffer_push_head(ctx, out, crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES); - put_header(ctx, out, session->common.send_nonce, 0); + fastd_buffer_push_head(out, crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES); + put_header(out, session->common.send_nonce, 0); fastd_method_increment_nonce(&session->common); return true; } -static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { +static bool method_decrypt(fastd_peer_t *peer, fastd_method_session_state_t *session, fastd_buffer_t *out, fastd_buffer_t in) { if (in.len < COMMON_HEADBYTES) return false; - if (!method_session_is_valid(ctx, session)) + if (!method_session_is_valid(session)) return false; uint8_t in_nonce[COMMON_NONCEBYTES]; uint8_t flags; int64_t age; - if (!handle_header(ctx, &session->common, &in, in_nonce, &flags, &age)) + if (!handle_header(&session->common, &in, in_nonce, &flags, &age)) return false; if (flags) @@ -154,27 +154,27 @@ static bool method_decrypt(fastd_context_t *ctx, fastd_peer_t *peer, fastd_metho uint8_t nonce[crypto_secretbox_xsalsa20poly1305_NONCEBYTES] __attribute__((aligned(8))) = {}; memcpy_nonce(nonce, in_nonce); - fastd_buffer_pull_head_zero(ctx, &in, crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES); + fastd_buffer_pull_head_zero(&in, crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES); - *out = fastd_buffer_alloc(ctx, in.len, 0, 0); + *out = fastd_buffer_alloc(in.len, 0, 0); if (crypto_secretbox_xsalsa20poly1305_open(out->data, in.data, in.len, nonce, session->key) != 0) { fastd_buffer_free(*out); /* restore input buffer */ - fastd_buffer_push_head(ctx, &in, crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES); - put_header(ctx, &in, in_nonce, 0); + fastd_buffer_push_head(&in, crypto_secretbox_xsalsa20poly1305_BOXZEROBYTES); + put_header(&in, in_nonce, 0); return false; } fastd_buffer_free(in); - if (!fastd_method_reorder_check(ctx, peer, &session->common, in_nonce, age)) { + if (!fastd_method_reorder_check(peer, &session->common, in_nonce, age)) { fastd_buffer_free(*out); - *out = fastd_buffer_alloc(ctx, crypto_secretbox_xsalsa20poly1305_ZEROBYTES, 0, 0); + *out = fastd_buffer_alloc(crypto_secretbox_xsalsa20poly1305_ZEROBYTES, 0, 0); } - fastd_buffer_push_head(ctx, out, crypto_secretbox_xsalsa20poly1305_ZEROBYTES); + fastd_buffer_push_head(out, crypto_secretbox_xsalsa20poly1305_ZEROBYTES); return true; } diff --git a/src/options.c b/src/options.c index 0817ed7..fef7cc1 100644 --- a/src/options.c +++ b/src/options.c @@ -48,7 +48,7 @@ static void print_usage(const char *options, const char *message) { puts(message); } -static void usage(fastd_context_t *ctx UNUSED) { +static void usage(void) { puts("fastd (Fast and Secure Tunnelling Daemon) " FASTD_VERSION " usage:\n"); #define OR ", " @@ -64,49 +64,49 @@ static void usage(fastd_context_t *ctx UNUSED) { exit(0); } -static void version(fastd_context_t *ctx UNUSED) { +static void version(void) { puts("fastd " FASTD_VERSION); exit(0); } -static void option_daemon(fastd_context_t *ctx UNUSED) { +static void option_daemon(void) { conf.daemon = true; } -static void option_pid_file(fastd_context_t *ctx UNUSED, const char *arg) { +static void option_pid_file(const char *arg) { free(conf.pid_file); conf.pid_file = strdup(arg); } -static void option_config(fastd_context_t *ctx, const char *arg) { +static void option_config(const char *arg) { if (!strcmp(arg, "-")) arg = NULL; - if (!fastd_read_config(ctx, arg, false, 0)) + if (!fastd_read_config(arg, false, 0)) exit(1); } -static void option_config_peer(fastd_context_t *ctx, const char *arg) { - fastd_peer_config_new(ctx); +static void option_config_peer(const char *arg) { + fastd_peer_config_new(); - if(!fastd_read_config(ctx, arg, true, 0)) + if(!fastd_read_config(arg, true, 0)) exit(1); } -static void option_config_peer_dir(fastd_context_t *ctx, const char *arg) { - fastd_add_peer_dir(ctx, arg); +static void option_config_peer_dir(const char *arg) { + fastd_add_peer_dir(arg); } #ifdef WITH_CMDLINE_USER -static void option_user(fastd_context_t *ctx UNUSED, const char *arg) { +static void option_user(const char *arg) { free(conf.user); conf.user = strdup(arg); } -static void option_group(fastd_context_t *ctx UNUSED, const char *arg) { +static void option_group(const char *arg) { free(conf.group); conf.group = strdup(arg); } @@ -115,7 +115,7 @@ static void option_group(fastd_context_t *ctx UNUSED, const char *arg) { #ifdef WITH_CMDLINE_LOGGING -static int parse_log_level(fastd_context_t *ctx, const char *arg) { +static int parse_log_level(const char *arg) { if (!strcmp(arg, "fatal")) return LL_FATAL; else if (!strcmp(arg, "error")) @@ -131,27 +131,27 @@ static int parse_log_level(fastd_context_t *ctx, const char *arg) { else if (!strcmp(arg, "debug2")) return LL_DEBUG2; else - exit_error(ctx, "invalid log level `%s'", arg); + exit_error("invalid log level `%s'", arg); } -static void option_log_level(fastd_context_t *ctx, const char *arg) { - conf.log_stderr_level = parse_log_level(ctx, arg); +static void option_log_level(const char *arg) { + conf.log_stderr_level = parse_log_level(arg); } -static void option_syslog_level(fastd_context_t *ctx, const char *arg) { - conf.log_syslog_level = parse_log_level(ctx, arg); +static void option_syslog_level(const char *arg) { + conf.log_syslog_level = parse_log_level(arg); } -static void option_syslog_ident(fastd_context_t *ctx UNUSED, const char *arg) { +static void option_syslog_ident(const char *arg) { free(conf.log_syslog_ident); conf.log_syslog_ident = strdup(arg); } -static void option_hide_ip_addresses(fastd_context_t *ctx UNUSED) { +static void option_hide_ip_addresses(void) { conf.hide_ip_addresses = true; } -static void option_hide_mac_addresses(fastd_context_t *ctx UNUSED) { +static void option_hide_mac_addresses(void) { conf.hide_mac_addresses = true; } @@ -159,31 +159,31 @@ static void option_hide_mac_addresses(fastd_context_t *ctx UNUSED) { #ifdef WITH_CMDLINE_OPERATION -static void option_mode(fastd_context_t *ctx, const char *arg) { +static void option_mode(const char *arg) { if (!strcmp(arg, "tap")) conf.mode = MODE_TAP; else if (!strcmp(arg, "tun")) conf.mode = MODE_TUN; else - exit_error(ctx, "invalid mode `%s'", arg); + exit_error("invalid mode `%s'", arg); } -static void option_interface(fastd_context_t *ctx UNUSED, const char *arg) { +static void option_interface(const char *arg) { free(conf.ifname); conf.ifname = strdup(arg); } -static void option_mtu(fastd_context_t *ctx, const char *arg) { +static void option_mtu(const char *arg) { char *endptr; long mtu = strtol(arg, &endptr, 10); if (*endptr || mtu < 576 || mtu > 65535) - exit_error(ctx, "invalid mtu `%s'", arg); + exit_error("invalid mtu `%s'", arg); conf.mtu = mtu; } -static void option_bind(fastd_context_t *ctx, const char *arg) { +static void option_bind(const char *arg) { long l; char *charptr; char *endptr; @@ -193,7 +193,7 @@ static void option_bind(fastd_context_t *ctx, const char *arg) { if (arg[0] == '[') { charptr = strrchr(arg, ']'); if (!charptr || (charptr[1] != ':' && charptr[1] != '\0')) - exit_error(ctx, "invalid bind address `%s'", arg); + exit_error("invalid bind address `%s'", arg); addrstr = strndup(arg+1, charptr-arg-1); @@ -215,7 +215,7 @@ static void option_bind(fastd_context_t *ctx, const char *arg) { if (charptr) { l = strtol(charptr+1, &endptr, 10); if (*endptr || l < 1 || l > 65535) - exit_error(ctx, "invalid bind port `%s'", charptr+1); + exit_error("invalid bind port `%s'", charptr+1); } else { l = 0; @@ -234,7 +234,7 @@ static void option_bind(fastd_context_t *ctx, const char *arg) { } if (inet_pton(AF_INET6, addrstr, &addr.in6.sin6_addr) != 1) - exit_error(ctx, "invalid bind address `[%s]'", addrstr); + exit_error("invalid bind address `[%s]'", addrstr); } else if (strcmp(addrstr, "any") == 0) { addr.in.sin_family = AF_UNSPEC; @@ -245,23 +245,23 @@ static void option_bind(fastd_context_t *ctx, const char *arg) { addr.in.sin_port = htons(l); if (inet_pton(AF_INET, addrstr, &addr.in.sin_addr) != 1) - exit_error(ctx, "invalid bind address `%s'", addrstr); + exit_error("invalid bind address `%s'", addrstr); } free(addrstr); - fastd_config_bind_address(ctx, &addr, ifname, false, false); + fastd_config_bind_address(&addr, ifname, false, false); } -static void option_protocol(fastd_context_t *ctx, const char *arg) { - fastd_config_protocol(ctx, arg); +static void option_protocol(const char *arg) { + fastd_config_protocol(arg); } -static void option_method(fastd_context_t *ctx, const char *arg) { - fastd_config_method(ctx, arg); +static void option_method(const char *arg) { + fastd_config_method(arg); } -static void option_forward(fastd_context_t *ctx UNUSED) { +static void option_forward(void) { conf.forward = true; } @@ -269,55 +269,55 @@ static void option_forward(fastd_context_t *ctx UNUSED) { #ifdef WITH_CMDLINE_COMMANDS -static void option_on_pre_up(fastd_context_t *ctx UNUSED, const char *arg) { +static void option_on_pre_up(const char *arg) { fastd_shell_command_set(&conf.on_pre_up, arg, true); } -static void option_on_up(fastd_context_t *ctx UNUSED, const char *arg) { +static void option_on_up(const char *arg) { fastd_shell_command_set(&conf.on_up, arg, true); } -static void option_on_down(fastd_context_t *ctx UNUSED, const char *arg) { +static void option_on_down(const char *arg) { fastd_shell_command_set(&conf.on_down, arg, true); } -static void option_on_post_down(fastd_context_t *ctx UNUSED, const char *arg) { +static void option_on_post_down(const char *arg) { fastd_shell_command_set(&conf.on_post_down, arg, true); } -static void option_on_connect(fastd_context_t *ctx UNUSED, const char *arg) { +static void option_on_connect(const char *arg) { fastd_shell_command_set(&conf.on_connect, arg, false); } -static void option_on_establish(fastd_context_t *ctx UNUSED, const char *arg) { +static void option_on_establish(const char *arg) { fastd_shell_command_set(&conf.on_establish, arg, false); } -static void option_on_disestablish(fastd_context_t *ctx UNUSED, const char *arg) { +static void option_on_disestablish(const char *arg) { fastd_shell_command_set(&conf.on_disestablish, arg, false); } -static void option_on_verify(fastd_context_t *ctx UNUSED, const char *arg) { +static void option_on_verify(const char *arg) { fastd_shell_command_set(&conf.on_verify, arg, false); } #endif -static void option_verify_config(fastd_context_t *ctx UNUSED) { +static void option_verify_config(void) { conf.verify_config = true; } -static void option_generate_key(fastd_context_t *ctx UNUSED) { +static void option_generate_key(void) { conf.generate_key = true; conf.show_key = false; } -static void option_show_key(fastd_context_t *ctx UNUSED) { +static void option_show_key(void) { conf.generate_key = false; conf.show_key = true; } -static void option_machine_readable(fastd_context_t *ctx UNUSED) { +static void option_machine_readable(void) { conf.machine_readable = true; } @@ -341,7 +341,7 @@ static bool config_match(const char *opt, ...) { return match; } -void fastd_config_handle_options(fastd_context_t *ctx, int argc, char *const argv[]) { +void fastd_config_handle_options(int argc, char *const argv[]) { int i = 1; while (i < argc) { @@ -351,7 +351,7 @@ void fastd_config_handle_options(fastd_context_t *ctx, int argc, char *const arg ({ \ if(config_match(argv[i], options, NULL)) { \ i++; \ - func(ctx); \ + func(); \ continue; \ } \ }) @@ -360,8 +360,8 @@ void fastd_config_handle_options(fastd_context_t *ctx, int argc, char *const arg if(config_match(argv[i], options, NULL)) { \ i+=2; \ if (i > argc) \ - exit_error(ctx, "command line error: option `%s' needs an argument; see --help for usage", argv[i-2]); \ - func(ctx, argv[i-1]); \ + exit_error("command line error: option `%s' needs an argument; see --help for usage", argv[i-2]); \ + func(argv[i-1]); \ continue; \ } \ }) @@ -371,6 +371,6 @@ void fastd_config_handle_options(fastd_context_t *ctx, int argc, char *const arg #undef OPTION #undef OPTION_ARG - exit_error(ctx, "command line error: unknown option `%s'; see --help for usage", argv[i]); + exit_error("command line error: unknown option `%s'; see --help for usage", argv[i]); } } diff --git a/src/peer.c b/src/peer.c index 9193441..faba121 100644 --- a/src/peer.c +++ b/src/peer.c @@ -32,28 +32,28 @@ #include -static inline void on_establish(fastd_context_t *ctx, const fastd_peer_t *peer) { - fastd_shell_command_exec(ctx, &conf.on_establish, peer, &peer->local_address, &peer->address); +static inline void on_establish(const fastd_peer_t *peer) { + fastd_shell_command_exec(&conf.on_establish, peer, &peer->local_address, &peer->address); } -static inline void on_disestablish(fastd_context_t *ctx, const fastd_peer_t *peer) { - fastd_shell_command_exec(ctx, &conf.on_disestablish, peer, &peer->local_address, &peer->address); +static inline void on_disestablish(const fastd_peer_t *peer) { + fastd_shell_command_exec(&conf.on_disestablish, peer, &peer->local_address, &peer->address); } -static inline void free_socket(fastd_context_t *ctx, fastd_peer_t *peer) { +static inline void free_socket(fastd_peer_t *peer) { if (peer->sock) { if (fastd_peer_is_socket_dynamic(peer)) { if (peer->sock->peer != peer) - exit_bug(ctx, "dynamic peer socket mismatch"); + exit_bug("dynamic peer socket mismatch"); - fastd_socket_close(ctx, peer->sock); + fastd_socket_close(peer->sock); free(peer->sock); peer->sock = NULL; size_t i; - for (i = 0; i < VECTOR_LEN(ctx->peers); i++) { - if (VECTOR_INDEX(ctx->peers, i) == peer) { - fastd_poll_set_fd_peer(ctx, i); + for (i = 0; i < VECTOR_LEN(ctx.peers); i++) { + if (VECTOR_INDEX(ctx.peers, i) == peer) { + fastd_poll_set_fd_peer(i); break; } } @@ -73,50 +73,50 @@ static inline bool has_group_config_constraints(const fastd_peer_group_config_t return false; } -void fastd_peer_reset_socket(fastd_context_t *ctx, fastd_peer_t *peer) { +void fastd_peer_reset_socket(fastd_peer_t *peer) { if (peer->address.sa.sa_family == AF_UNSPEC) { - free_socket(ctx, peer); + free_socket(peer); return; } if (!fastd_peer_is_socket_dynamic(peer)) return; - pr_debug(ctx, "resetting socket for peer %P", peer); + pr_debug("resetting socket for peer %P", peer); - free_socket(ctx, peer); + free_socket(peer); switch (peer->address.sa.sa_family) { case AF_INET: - if (ctx->sock_default_v4) - peer->sock = ctx->sock_default_v4; + if (ctx.sock_default_v4) + peer->sock = ctx.sock_default_v4; else - peer->sock = fastd_socket_open(ctx, peer, AF_INET); + peer->sock = fastd_socket_open(peer, AF_INET); break; case AF_INET6: - if (ctx->sock_default_v6) - peer->sock = ctx->sock_default_v6; + if (ctx.sock_default_v6) + peer->sock = ctx.sock_default_v6; else - peer->sock = fastd_socket_open(ctx, peer, AF_INET6); + peer->sock = fastd_socket_open(peer, AF_INET6); } if (!peer->sock || !fastd_peer_is_socket_dynamic(peer)) return; size_t i; - for (i = 0; i < VECTOR_LEN(ctx->peers); i++) { - if (VECTOR_INDEX(ctx->peers, i) == peer) { - fastd_poll_set_fd_peer(ctx, i); + for (i = 0; i < VECTOR_LEN(ctx.peers); i++) { + if (VECTOR_INDEX(ctx.peers, i) == peer) { + fastd_poll_set_fd_peer(i); break; } } } -void fastd_peer_schedule_handshake(fastd_context_t *ctx, fastd_peer_t *peer, int delay) { - fastd_peer_unschedule_handshake(ctx, peer); +void fastd_peer_schedule_handshake(fastd_peer_t *peer, int delay) { + fastd_peer_unschedule_handshake(peer); - peer->next_handshake = ctx->now; + peer->next_handshake = ctx.now; peer->next_handshake.tv_sec += delay/1000; peer->next_handshake.tv_nsec += (delay%1000)*1e6; @@ -127,7 +127,7 @@ void fastd_peer_schedule_handshake(fastd_context_t *ctx, fastd_peer_t *peer, int } fastd_dlist_head_t *list; - for (list = &ctx->handshake_queue; list->next; list = list->next) { + for (list = &ctx.handshake_queue; list->next; list = list->next) { fastd_peer_t *entry = container_of(list->next, fastd_peer_t, handshake_entry); if (timespec_after(&entry->next_handshake, &peer->next_handshake)) @@ -167,43 +167,43 @@ static bool is_peer_in_group(fastd_peer_t *peer, fastd_peer_group_t *group) { return is_group_in(peer->group, group); } -static void reset_peer(fastd_context_t *ctx, fastd_peer_t *peer) { +static void reset_peer(fastd_peer_t *peer) { if (fastd_peer_is_established(peer)) - on_disestablish(ctx, peer); + on_disestablish(peer); - free_socket(ctx, peer); + free_socket(peer); memset(&peer->local_address, 0, sizeof(peer->local_address)); - conf.protocol->reset_peer_state(ctx, peer); + conf.protocol->reset_peer_state(peer); size_t i, deleted = 0; - for (i = 0; i < VECTOR_LEN(ctx->eth_addrs); i++) { - if (VECTOR_INDEX(ctx->eth_addrs, i).peer == peer) { + for (i = 0; i < VECTOR_LEN(ctx.eth_addrs); i++) { + if (VECTOR_INDEX(ctx.eth_addrs, i).peer == peer) { deleted++; } else if (deleted) { - VECTOR_INDEX(ctx->eth_addrs, i-deleted) = VECTOR_INDEX(ctx->eth_addrs, i); + VECTOR_INDEX(ctx.eth_addrs, i-deleted) = VECTOR_INDEX(ctx.eth_addrs, i); } } - VECTOR_RESIZE(ctx->eth_addrs, VECTOR_LEN(ctx->eth_addrs)-deleted); + VECTOR_RESIZE(ctx.eth_addrs, VECTOR_LEN(ctx.eth_addrs)-deleted); - fastd_peer_unschedule_handshake(ctx, peer); + fastd_peer_unschedule_handshake(peer); } -static void init_handshake(fastd_context_t *ctx, fastd_peer_t *peer) { +static void init_handshake(fastd_peer_t *peer) { unsigned delay = 0; if (has_group_config_constraints(peer->group->conf)) - delay = fastd_rand(ctx, 0, 3000); + delay = fastd_rand(0, 3000); if (!fastd_peer_is_established(peer)) peer->state = STATE_HANDSHAKE; - fastd_peer_schedule_handshake(ctx, peer, delay); + fastd_peer_schedule_handshake(peer, delay); } -void fastd_peer_handle_resolve(fastd_context_t *ctx, fastd_peer_t *peer, fastd_remote_t *remote, size_t n_addresses, const fastd_peer_address_t *addresses) { +void fastd_peer_handle_resolve(fastd_peer_t *peer, fastd_remote_t *remote, size_t n_addresses, const fastd_peer_address_t *addresses) { remote->resolving = false; free(remote->addresses); @@ -214,10 +214,10 @@ void fastd_peer_handle_resolve(fastd_context_t *ctx, fastd_peer_t *peer, fastd_r remote->current_address = 0; if (peer->state == STATE_RESOLVING) - init_handshake(ctx, peer); + init_handshake(peer); } -static void setup_peer(fastd_context_t *ctx, fastd_peer_t *peer) { +static void setup_peer(fastd_peer_t *peer) { peer->address.sa.sa_family = AF_UNSPEC; peer->local_address.sa.sa_family = AF_UNSPEC; @@ -225,60 +225,60 @@ static void setup_peer(fastd_context_t *ctx, fastd_peer_t *peer) { fastd_remote_t *remote; for (remote = peer->remotes; remote; remote = remote->next) { - remote->last_resolve_timeout = ctx->now; + remote->last_resolve_timeout = ctx.now; remote->resolving = false; } peer->next_remote = peer->remotes; - peer->last_handshake_timeout = ctx->now; + peer->last_handshake_timeout = ctx.now; peer->last_handshake_address.sa.sa_family = AF_UNSPEC; - peer->last_handshake_response_timeout = ctx->now; + peer->last_handshake_response_timeout = ctx.now; peer->last_handshake_response_address.sa.sa_family = AF_UNSPEC; - peer->establish_handshake_timeout = ctx->now; + peer->establish_handshake_timeout = ctx.now; if (!peer->protocol_state) - conf.protocol->init_peer_state(ctx, peer); + conf.protocol->init_peer_state(peer); if (peer->next_remote) { peer->next_remote->current_address = 0; if (fastd_remote_is_dynamic(peer->next_remote)) { peer->state = STATE_RESOLVING; - fastd_resolve_peer(ctx, peer, peer->next_remote); + fastd_resolve_peer(peer, peer->next_remote); } else { - init_handshake(ctx, peer); + init_handshake(peer); } } } -static void delete_peer(fastd_context_t *ctx, fastd_peer_t *peer) { - pr_debug(ctx, "deleting peer %P", peer); +static void delete_peer(fastd_peer_t *peer) { + pr_debug("deleting peer %P", peer); size_t i; - for (i = 0; i < VECTOR_LEN(ctx->peers); i++) { - if (VECTOR_INDEX(ctx->peers, i) == peer) { - VECTOR_DELETE(ctx->peers, i); - fastd_poll_delete_peer(ctx, i); + for (i = 0; i < VECTOR_LEN(ctx.peers); i++) { + if (VECTOR_INDEX(ctx.peers, i) == peer) { + VECTOR_DELETE(ctx.peers, i); + fastd_poll_delete_peer(i); break; } } - if (i == VECTOR_LEN(ctx->peers)) { - for (i = 0; i < VECTOR_LEN(ctx->peers_temp); i++) { - if (VECTOR_INDEX(ctx->peers_temp, i) == peer) { - VECTOR_DELETE(ctx->peers_temp, i); + if (i == VECTOR_LEN(ctx.peers)) { + for (i = 0; i < VECTOR_LEN(ctx.peers_temp); i++) { + if (VECTOR_INDEX(ctx.peers_temp, i) == peer) { + VECTOR_DELETE(ctx.peers_temp, i); break; } } } - fastd_peer_hashtable_remove(ctx, peer); + fastd_peer_hashtable_remove(peer); - conf.protocol->free_peer_state(ctx, peer); + conf.protocol->free_peer_state(peer); if (!peer->config) free(peer->protocol_config); @@ -294,7 +294,7 @@ static void delete_peer(fastd_context_t *ctx, fastd_peer_t *peer) { } -fastd_peer_config_t* fastd_peer_config_new(fastd_context_t *ctx UNUSED) { +fastd_peer_config_t* fastd_peer_config_new(void) { fastd_peer_config_t *peer = calloc(1, sizeof(fastd_peer_config_t)); peer->group = conf.peer_group; @@ -320,19 +320,19 @@ void fastd_peer_config_free(fastd_peer_config_t *peer) { free(peer); } -void fastd_peer_config_delete(fastd_context_t *ctx UNUSED) { +void fastd_peer_config_delete(void) { fastd_peer_config_t *peer = conf.peers, *next = peer->next; fastd_peer_config_free(peer); conf.peers = next; } -void fastd_peer_config_purge(fastd_context_t *ctx, fastd_peer_config_t *config) { +void fastd_peer_config_purge(fastd_peer_config_t *config) { size_t i; - for (i = 0; i < VECTOR_LEN(ctx->peers); i++) { - fastd_peer_t *peer = VECTOR_INDEX(ctx->peers, i); + for (i = 0; i < VECTOR_LEN(ctx.peers); i++) { + fastd_peer_t *peer = VECTOR_INDEX(ctx.peers, i); if (peer->config == config) { - fastd_peer_delete(ctx, peer); + fastd_peer_delete(peer); break; } } @@ -394,15 +394,15 @@ void fastd_peer_address_widen(fastd_peer_address_t *addr) { } -static inline void reset_peer_address(fastd_context_t *ctx, fastd_peer_t *peer) { +static inline void reset_peer_address(fastd_peer_t *peer) { if (fastd_peer_is_established(peer)) - fastd_peer_reset(ctx, peer); + fastd_peer_reset(peer); - fastd_peer_hashtable_remove(ctx, peer); + fastd_peer_hashtable_remove(peer); memset(&peer->address, 0, sizeof(fastd_peer_address_t)); } -bool fastd_peer_owns_address(fastd_context_t *ctx UNUSED, const fastd_peer_t *peer, const fastd_peer_address_t *addr) { +bool fastd_peer_owns_address(const fastd_peer_t *peer, const fastd_peer_address_t *addr) { if (fastd_peer_is_floating(peer)) return false; @@ -418,7 +418,7 @@ bool fastd_peer_owns_address(fastd_context_t *ctx UNUSED, const fastd_peer_t *pe return false; } -bool fastd_peer_matches_address(fastd_context_t *ctx UNUSED, const fastd_peer_t *peer, const fastd_peer_address_t *addr) { +bool fastd_peer_matches_address(const fastd_peer_t *peer, const fastd_peer_address_t *addr) { if (fastd_peer_is_floating(peer)) return true; @@ -434,21 +434,21 @@ bool fastd_peer_matches_address(fastd_context_t *ctx UNUSED, const fastd_peer_t return false; } -bool fastd_peer_claim_address(fastd_context_t *ctx, fastd_peer_t *new_peer, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr) { +bool fastd_peer_claim_address(fastd_peer_t *new_peer, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr) { if (remote_addr->sa.sa_family == AF_UNSPEC) { if (fastd_peer_is_established(new_peer)) - fastd_peer_reset(ctx, new_peer); + fastd_peer_reset(new_peer); } else { size_t i; - for (i = 0; i < VECTOR_LEN(ctx->peers); i++) { - fastd_peer_t *peer = VECTOR_INDEX(ctx->peers, i); + for (i = 0; i < VECTOR_LEN(ctx.peers); i++) { + fastd_peer_t *peer = VECTOR_INDEX(ctx.peers, i); if (peer == new_peer) continue; - if (fastd_peer_owns_address(ctx, peer, remote_addr)) { - reset_peer_address(ctx, new_peer); + if (fastd_peer_owns_address(peer, remote_addr)) { + reset_peer_address(new_peer); return false; } @@ -456,21 +456,21 @@ bool fastd_peer_claim_address(fastd_context_t *ctx, fastd_peer_t *new_peer, fast if (fastd_peer_is_established(peer)) return false; - reset_peer_address(ctx, peer); + reset_peer_address(peer); break; } } } - fastd_peer_hashtable_remove(ctx, new_peer); + fastd_peer_hashtable_remove(new_peer); new_peer->address = *remote_addr; if (remote_addr->sa.sa_family != AF_UNSPEC) - fastd_peer_hashtable_insert(ctx, new_peer); + fastd_peer_hashtable_insert(new_peer); if (sock && sock->addr && sock != new_peer->sock) { - free_socket(ctx, new_peer); + free_socket(new_peer); new_peer->sock = sock; } @@ -512,23 +512,23 @@ bool fastd_peer_config_equal(const fastd_peer_config_t *peer1, const fastd_peer_ return true; } -void fastd_peer_reset(fastd_context_t *ctx, fastd_peer_t *peer) { - pr_debug(ctx, "resetting peer %P", peer); +void fastd_peer_reset(fastd_peer_t *peer) { + pr_debug("resetting peer %P", peer); - reset_peer(ctx, peer); - setup_peer(ctx, peer); + reset_peer(peer); + setup_peer(peer); } -void fastd_peer_delete(fastd_context_t *ctx, fastd_peer_t *peer) { - reset_peer(ctx, peer); - delete_peer(ctx, peer); +void fastd_peer_delete(fastd_peer_t *peer) { + reset_peer(peer); + delete_peer(peer); } -static inline unsigned count_established_group_peers(fastd_context_t *ctx, fastd_peer_group_t *group) { +static inline unsigned count_established_group_peers(fastd_peer_group_t *group) { unsigned ret = 0; size_t i; - for (i = 0; i < VECTOR_LEN(ctx->peers); i++) { - fastd_peer_t *peer = VECTOR_INDEX(ctx->peers, i); + for (i = 0; i < VECTOR_LEN(ctx.peers); i++) { + fastd_peer_t *peer = VECTOR_INDEX(ctx.peers, i); if (fastd_peer_is_established(peer) && is_peer_in_group(peer, group)) ret++; @@ -537,7 +537,7 @@ static inline unsigned count_established_group_peers(fastd_context_t *ctx, fastd return ret; } -bool fastd_peer_may_connect(fastd_context_t *ctx, fastd_peer_t *peer) { +bool fastd_peer_may_connect(fastd_peer_t *peer) { if (fastd_peer_is_established(peer)) return true; @@ -547,18 +547,18 @@ bool fastd_peer_may_connect(fastd_context_t *ctx, fastd_peer_t *peer) { if (group->conf->max_connections < 0) continue; - if (count_established_group_peers(ctx, group) >= (unsigned)group->conf->max_connections) + if (count_established_group_peers(group) >= (unsigned)group->conf->max_connections) return false; } return true; } -fastd_peer_t* fastd_peer_add(fastd_context_t *ctx, fastd_peer_config_t *peer_conf) { +fastd_peer_t* fastd_peer_add(fastd_peer_config_t *peer_conf) { fastd_peer_t *peer = calloc(1, sizeof(fastd_peer_t)); peer->config = peer_conf; - peer->group = find_peer_group(ctx->peer_group, peer_conf->group); + peer->group = find_peer_group(ctx.peer_group, peer_conf->group); peer->protocol_config = peer_conf->protocol_config; fastd_remote_t **remote = &peer->remotes; @@ -579,74 +579,74 @@ fastd_peer_t* fastd_peer_add(fastd_context_t *ctx, fastd_peer_config_t *peer_con remote_config = remote_config->next; } - pr_verbose(ctx, "adding peer %P (group `%s')", peer, peer->group->conf->name); + pr_verbose("adding peer %P (group `%s')", peer, peer->group->conf->name); - setup_peer(ctx, peer); + setup_peer(peer); - VECTOR_ADD(ctx->peers, peer); - fastd_poll_add_peer(ctx); + VECTOR_ADD(ctx.peers, peer); + fastd_poll_add_peer(); return peer; } -fastd_peer_t* fastd_peer_add_temporary(fastd_context_t *ctx) { +fastd_peer_t* fastd_peer_add_temporary(void) { if (!fastd_shell_command_isset(&conf.on_verify)) - exit_bug(ctx, "tried to add temporary peer without on-verify command"); + exit_bug("tried to add temporary peer without on-verify command"); fastd_peer_t *peer = calloc(1, sizeof(fastd_peer_t)); - peer->group = ctx->peer_group; - fastd_peer_seen(ctx, peer); + peer->group = ctx.peer_group; + fastd_peer_seen(peer); - pr_debug(ctx, "adding temporary peer"); + pr_debug("adding temporary peer"); - setup_peer(ctx, peer); + setup_peer(peer); - VECTOR_ADD(ctx->peers_temp, peer); + VECTOR_ADD(ctx.peers_temp, peer); return peer; } -bool fastd_peer_verify_temporary(fastd_context_t *ctx, fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr) { +bool fastd_peer_verify_temporary(fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr) { if (!fastd_shell_command_isset(&conf.on_verify)) - exit_bug(ctx, "tried to verify temporary peer without on-verify command"); + exit_bug("tried to verify temporary peer without on-verify command"); /* TODO: async not supported yet */ int ret; - if (!fastd_shell_command_exec_sync(ctx, &conf.on_verify, peer, local_addr, peer_addr, &ret)) + if (!fastd_shell_command_exec_sync(&conf.on_verify, peer, local_addr, peer_addr, &ret)) return false; if (WIFSIGNALED(ret)) { - pr_error(ctx, "verify command exited with signal %i", WTERMSIG(ret)); + pr_error("verify command exited with signal %i", WTERMSIG(ret)); return false; } else if (WEXITSTATUS(ret)) { - pr_debug(ctx, "verify command exited with status %i", WEXITSTATUS(ret)); + pr_debug("verify command exited with status %i", WEXITSTATUS(ret)); return false; } return true; } -void fastd_peer_enable_temporary(fastd_context_t *ctx, fastd_peer_t *peer) { +void fastd_peer_enable_temporary(fastd_peer_t *peer) { if (peer->config) - exit_bug(ctx, "trying to re-enable non-temporary peer"); + exit_bug("trying to re-enable non-temporary peer"); - VECTOR_ADD(ctx->peers, peer); - fastd_poll_add_peer(ctx); + VECTOR_ADD(ctx.peers, peer); + fastd_poll_add_peer(); } -void fastd_peer_set_established(fastd_context_t *ctx, fastd_peer_t *peer) { +void fastd_peer_set_established(fastd_peer_t *peer) { if (fastd_peer_is_established(peer)) return; peer->state = STATE_ESTABLISHED; - on_establish(ctx, peer); - pr_info(ctx, "connection with %P established.", peer); + on_establish(peer); + pr_info("connection with %P established.", peer); } -fastd_eth_addr_t fastd_get_source_address(const fastd_context_t *ctx, fastd_buffer_t buffer) { +fastd_eth_addr_t fastd_get_source_address(const fastd_buffer_t buffer) { fastd_eth_addr_t ret; switch (conf.mode) { @@ -654,18 +654,18 @@ fastd_eth_addr_t fastd_get_source_address(const fastd_context_t *ctx, fastd_buff memcpy(&ret, buffer.data+offsetof(struct ethhdr, h_source), ETH_ALEN); return ret; default: - exit_bug(ctx, "invalid mode"); + exit_bug("invalid mode"); } } -fastd_eth_addr_t fastd_get_dest_address(const fastd_context_t *ctx, fastd_buffer_t buffer) { +fastd_eth_addr_t fastd_get_dest_address(const fastd_buffer_t buffer) { fastd_eth_addr_t ret; switch (conf.mode) { case MODE_TAP: memcpy(&ret, buffer.data+offsetof(struct ethhdr, h_dest), ETH_ALEN); return ret; default: - exit_bug(ctx, "invalid mode"); + exit_bug("invalid mode"); } } @@ -693,8 +693,8 @@ static inline int fastd_eth_addr_cmp(const fastd_eth_addr_t *addr1, const fastd_ return memcmp(addr1->data, addr2->data, ETH_ALEN); } -static inline fastd_peer_eth_addr_t* peer_get_by_addr(fastd_context_t *ctx, fastd_eth_addr_t addr) { - fastd_eth_addr_t *ret = bsearch(&addr, &VECTOR_INDEX(ctx->eth_addrs, 0).addr, VECTOR_LEN(ctx->eth_addrs), sizeof(fastd_peer_eth_addr_t), +static inline fastd_peer_eth_addr_t* peer_get_by_addr(fastd_eth_addr_t addr) { + fastd_eth_addr_t *ret = bsearch(&addr, &VECTOR_INDEX(ctx.eth_addrs, 0).addr, VECTOR_LEN(ctx.eth_addrs), sizeof(fastd_peer_eth_addr_t), (int (*)(const void *, const void *))fastd_eth_addr_cmp); if (ret) @@ -703,19 +703,19 @@ static inline fastd_peer_eth_addr_t* peer_get_by_addr(fastd_context_t *ctx, fast return NULL; } -void fastd_peer_eth_addr_add(fastd_context_t *ctx, fastd_peer_t *peer, fastd_eth_addr_t addr) { - int min = 0, max = VECTOR_LEN(ctx->eth_addrs); +void fastd_peer_eth_addr_add(fastd_peer_t *peer, fastd_eth_addr_t addr) { + int min = 0, max = VECTOR_LEN(ctx.eth_addrs); if (!fastd_peer_is_established(peer)) - exit_bug(ctx, "tried to learn ethernet address on non-established peer"); + exit_bug("tried to learn ethernet address on non-established peer"); while (max > min) { int cur = (min+max)/2; - int cmp = fastd_eth_addr_cmp(&addr, &VECTOR_INDEX(ctx->eth_addrs, cur).addr); + int cmp = fastd_eth_addr_cmp(&addr, &VECTOR_INDEX(ctx.eth_addrs, cur).addr); if (cmp == 0) { - VECTOR_INDEX(ctx->eth_addrs, cur).peer = peer; - VECTOR_INDEX(ctx->eth_addrs, cur).timeout = fastd_in_seconds(ctx, conf.eth_addr_stale_time); + VECTOR_INDEX(ctx.eth_addrs, cur).peer = peer; + VECTOR_INDEX(ctx.eth_addrs, cur).timeout = fastd_in_seconds(conf.eth_addr_stale_time); return; /* We're done here. */ } else if (cmp < 0) { @@ -726,30 +726,30 @@ void fastd_peer_eth_addr_add(fastd_context_t *ctx, fastd_peer_t *peer, fastd_eth } } - VECTOR_INSERT(ctx->eth_addrs, ((fastd_peer_eth_addr_t) {addr, peer, fastd_in_seconds(ctx, conf.eth_addr_stale_time)}), min); + VECTOR_INSERT(ctx.eth_addrs, ((fastd_peer_eth_addr_t) {addr, peer, fastd_in_seconds(conf.eth_addr_stale_time)}), min); - pr_debug(ctx, "learned new MAC address %E on peer %P", &addr, peer); + pr_debug("learned new MAC address %E on peer %P", &addr, peer); } -void fastd_peer_eth_addr_cleanup(fastd_context_t *ctx) { +void fastd_peer_eth_addr_cleanup(void) { size_t i, deleted = 0; - for (i = 0; i < VECTOR_LEN(ctx->eth_addrs); i++) { - if (fastd_timed_out(ctx, &VECTOR_INDEX(ctx->eth_addrs, i).timeout)) { + for (i = 0; i < VECTOR_LEN(ctx.eth_addrs); i++) { + if (fastd_timed_out(&VECTOR_INDEX(ctx.eth_addrs, i).timeout)) { deleted++; - pr_debug(ctx, "MAC address %E not seen for more than %u seconds, removing", - &VECTOR_INDEX(ctx->eth_addrs, i).addr, conf.eth_addr_stale_time); + pr_debug("MAC address %E not seen for more than %u seconds, removing", + &VECTOR_INDEX(ctx.eth_addrs, i).addr, conf.eth_addr_stale_time); } else if (deleted) { - VECTOR_INDEX(ctx->eth_addrs, i-deleted) = VECTOR_INDEX(ctx->eth_addrs, i); + VECTOR_INDEX(ctx.eth_addrs, i-deleted) = VECTOR_INDEX(ctx.eth_addrs, i); } } - VECTOR_RESIZE(ctx->eth_addrs, VECTOR_LEN(ctx->eth_addrs)-deleted); + VECTOR_RESIZE(ctx.eth_addrs, VECTOR_LEN(ctx.eth_addrs)-deleted); } -fastd_peer_t* fastd_peer_find_by_eth_addr(fastd_context_t *ctx, const fastd_eth_addr_t addr) { - fastd_peer_eth_addr_t *peer_eth_addr = peer_get_by_addr(ctx, addr); +fastd_peer_t* fastd_peer_find_by_eth_addr(const fastd_eth_addr_t addr) { + fastd_peer_eth_addr_t *peer_eth_addr = peer_get_by_addr(addr); if (peer_eth_addr) return peer_eth_addr->peer; diff --git a/src/peer.h b/src/peer.h index 8d1afa2..1fa4822 100644 --- a/src/peer.h +++ b/src/peer.h @@ -122,41 +122,41 @@ static inline uint16_t fastd_peer_address_get_port(const fastd_peer_address_t *a } } -fastd_peer_config_t* fastd_peer_config_new(fastd_context_t *ctx); +fastd_peer_config_t* fastd_peer_config_new(void); void fastd_peer_config_free(fastd_peer_config_t *peer); -void fastd_peer_config_delete(fastd_context_t *ctx); -void fastd_peer_config_purge(fastd_context_t *ctx, fastd_peer_config_t *config); +void fastd_peer_config_delete(void); +void fastd_peer_config_purge(fastd_peer_config_t *config); bool fastd_peer_config_equal(const fastd_peer_config_t *peer1, const fastd_peer_config_t *peer2); -void fastd_peer_reset(fastd_context_t *ctx, fastd_peer_t *peer); -void fastd_peer_delete(fastd_context_t *ctx, fastd_peer_t *peer); -fastd_peer_t* fastd_peer_add(fastd_context_t *ctx, fastd_peer_config_t *peer_conf); -fastd_peer_t* fastd_peer_add_temporary(fastd_context_t *ctx); -bool fastd_peer_verify_temporary(fastd_context_t *ctx, fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr); -void fastd_peer_enable_temporary(fastd_context_t *ctx, fastd_peer_t *peer); -void fastd_peer_set_established(fastd_context_t *ctx, fastd_peer_t *peer); -bool fastd_peer_may_connect(fastd_context_t *ctx, fastd_peer_t *peer); -void fastd_peer_handle_resolve(fastd_context_t *ctx, fastd_peer_t *peer, fastd_remote_t *remote, size_t n_addresses, const fastd_peer_address_t *addresses); -bool fastd_peer_owns_address(fastd_context_t *ctx, const fastd_peer_t *peer, const fastd_peer_address_t *addr); -bool fastd_peer_matches_address(fastd_context_t *ctx, const fastd_peer_t *peer, const fastd_peer_address_t *addr); -bool fastd_peer_claim_address(fastd_context_t *ctx, fastd_peer_t *peer, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr); -void fastd_peer_reset_socket(fastd_context_t *ctx, fastd_peer_t *peer); -void fastd_peer_schedule_handshake(fastd_context_t *ctx, fastd_peer_t *peer, int delay); - -static inline void fastd_peer_schedule_handshake_default(fastd_context_t *ctx, fastd_peer_t *peer) { - fastd_peer_schedule_handshake(ctx, peer, fastd_rand(ctx, 17500, 22500)); +void fastd_peer_reset(fastd_peer_t *peer); +void fastd_peer_delete(fastd_peer_t *peer); +fastd_peer_t* fastd_peer_add(fastd_peer_config_t *peer_conf); +fastd_peer_t* fastd_peer_add_temporary(void); +bool fastd_peer_verify_temporary(fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr); +void fastd_peer_enable_temporary(fastd_peer_t *peer); +void fastd_peer_set_established(fastd_peer_t *peer); +bool fastd_peer_may_connect(fastd_peer_t *peer); +void fastd_peer_handle_resolve(fastd_peer_t *peer, fastd_remote_t *remote, size_t n_addresses, const fastd_peer_address_t *addresses); +bool fastd_peer_owns_address(const fastd_peer_t *peer, const fastd_peer_address_t *addr); +bool fastd_peer_matches_address(const fastd_peer_t *peer, const fastd_peer_address_t *addr); +bool fastd_peer_claim_address(fastd_peer_t *peer, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr); +void fastd_peer_reset_socket(fastd_peer_t *peer); +void fastd_peer_schedule_handshake(fastd_peer_t *peer, int delay); + +static inline void fastd_peer_schedule_handshake_default(fastd_peer_t *peer) { + fastd_peer_schedule_handshake(peer, fastd_rand(17500, 22500)); } -static inline void fastd_peer_unschedule_handshake(fastd_context_t *ctx UNUSED, fastd_peer_t *peer) { +static inline void fastd_peer_unschedule_handshake(fastd_peer_t *peer) { fastd_dlist_remove(&peer->handshake_entry); } -static inline bool fastd_peer_handshake_scheduled(fastd_context_t *ctx UNUSED, fastd_peer_t *peer) { +static inline bool fastd_peer_handshake_scheduled(fastd_peer_t *peer) { return fastd_dlist_linked(&peer->handshake_entry); } -fastd_eth_addr_t fastd_get_source_address(const fastd_context_t *ctx, fastd_buffer_t buffer); -fastd_eth_addr_t fastd_get_dest_address(const fastd_context_t *ctx, fastd_buffer_t buffer); +fastd_eth_addr_t fastd_get_source_address(const fastd_buffer_t buffer); +fastd_eth_addr_t fastd_get_dest_address(const fastd_buffer_t buffer); static inline bool fastd_peer_config_is_floating(const fastd_peer_config_t *config) { return (!config->remotes || config->floating); @@ -197,8 +197,8 @@ static inline bool fastd_remote_is_dynamic(const fastd_remote_t *remote) { return remote->config->hostname; } -static inline void fastd_peer_seen(fastd_context_t *ctx, fastd_peer_t *peer) { - peer->timeout = fastd_in_seconds(ctx, conf.peer_stale_time); +static inline void fastd_peer_seen(fastd_peer_t *peer) { + peer->timeout = fastd_in_seconds(conf.peer_stale_time); } static inline bool fastd_peer_is_socket_dynamic(const fastd_peer_t *peer) { @@ -209,6 +209,6 @@ static inline bool fastd_eth_addr_is_unicast(fastd_eth_addr_t addr) { return ((addr.data[0] & 1) == 0); } -void fastd_peer_eth_addr_add(fastd_context_t *ctx, fastd_peer_t *peer, fastd_eth_addr_t addr); -void fastd_peer_eth_addr_cleanup(fastd_context_t *ctx); -fastd_peer_t* fastd_peer_find_by_eth_addr(fastd_context_t *ctx, fastd_eth_addr_t addr); +void fastd_peer_eth_addr_add(fastd_peer_t *peer, fastd_eth_addr_t addr); +void fastd_peer_eth_addr_cleanup(void); +fastd_peer_t* fastd_peer_find_by_eth_addr(fastd_eth_addr_t addr); diff --git a/src/peer_hashtable.c b/src/peer_hashtable.c index f6cc11e..003a16c 100644 --- a/src/peer_hashtable.c +++ b/src/peer_hashtable.c @@ -33,26 +33,26 @@ #define PEER_ADDR_HT_SIZE 64 -void fastd_peer_hashtable_init(fastd_context_t *ctx) { - fastd_random_bytes(ctx, &ctx->peer_addr_ht_seed, sizeof(ctx->peer_addr_ht_seed), false); +void fastd_peer_hashtable_init(void) { + fastd_random_bytes(&ctx.peer_addr_ht_seed, sizeof(ctx.peer_addr_ht_seed), false); - ctx->peer_addr_ht = malloc(sizeof(*ctx->peer_addr_ht) * PEER_ADDR_HT_SIZE); + ctx.peer_addr_ht = malloc(sizeof(*ctx.peer_addr_ht) * PEER_ADDR_HT_SIZE); size_t i; for (i = 0; i < PEER_ADDR_HT_SIZE; i++) - VECTOR_ALLOC(ctx->peer_addr_ht[i], 0); + VECTOR_ALLOC(ctx.peer_addr_ht[i], 0); } -void fastd_peer_hashtable_free(fastd_context_t *ctx) { +void fastd_peer_hashtable_free(void) { size_t i; for (i = 0; i < PEER_ADDR_HT_SIZE; i++) - VECTOR_FREE(ctx->peer_addr_ht[i]); + VECTOR_FREE(ctx.peer_addr_ht[i]); - free(ctx->peer_addr_ht); + free(ctx.peer_addr_ht); } -static size_t peer_address_bucket(fastd_context_t *ctx, const fastd_peer_address_t *addr) { - uint32_t hash = ctx->peer_addr_ht_seed; +static size_t peer_address_bucket(const fastd_peer_address_t *addr) { + uint32_t hash = ctx.peer_addr_ht_seed; switch(addr->sa.sa_family) { case AF_INET: @@ -64,7 +64,7 @@ static size_t peer_address_bucket(fastd_context_t *ctx, const fastd_peer_address break; default: - exit_bug(ctx, "peer_address_bucket: unknown address family"); + exit_bug("peer_address_bucket: unknown address family"); } fastd_hash_final(&hash); @@ -72,32 +72,32 @@ static size_t peer_address_bucket(fastd_context_t *ctx, const fastd_peer_address return hash % PEER_ADDR_HT_SIZE; } -void fastd_peer_hashtable_insert(fastd_context_t *ctx, fastd_peer_t *peer) { - size_t b = peer_address_bucket(ctx, &peer->address); - VECTOR_ADD(ctx->peer_addr_ht[b], peer); +void fastd_peer_hashtable_insert(fastd_peer_t *peer) { + size_t b = peer_address_bucket(&peer->address); + VECTOR_ADD(ctx.peer_addr_ht[b], peer); } -void fastd_peer_hashtable_remove(fastd_context_t *ctx, fastd_peer_t *peer) { +void fastd_peer_hashtable_remove(fastd_peer_t *peer) { if (!peer->address.sa.sa_family) return; - size_t b = peer_address_bucket(ctx, &peer->address); + size_t b = peer_address_bucket(&peer->address); size_t i; - for (i = 0; i < VECTOR_LEN(ctx->peer_addr_ht[b]); i++) { - if (VECTOR_INDEX(ctx->peer_addr_ht[b], i) == peer) { - VECTOR_DELETE(ctx->peer_addr_ht[b], i); + for (i = 0; i < VECTOR_LEN(ctx.peer_addr_ht[b]); i++) { + if (VECTOR_INDEX(ctx.peer_addr_ht[b], i) == peer) { + VECTOR_DELETE(ctx.peer_addr_ht[b], i); break; } } } -fastd_peer_t *fastd_peer_hashtable_lookup(fastd_context_t *ctx, const fastd_peer_address_t *addr) { - size_t b = peer_address_bucket(ctx, addr); +fastd_peer_t *fastd_peer_hashtable_lookup(const fastd_peer_address_t *addr) { + size_t b = peer_address_bucket(addr); size_t i; - for (i = 0; i < VECTOR_LEN(ctx->peer_addr_ht[b]); i++) { - fastd_peer_t *peer = VECTOR_INDEX(ctx->peer_addr_ht[b], i); + for (i = 0; i < VECTOR_LEN(ctx.peer_addr_ht[b]); i++) { + fastd_peer_t *peer = VECTOR_INDEX(ctx.peer_addr_ht[b], i); if (fastd_peer_address_equal(&peer->address, addr)) return peer; diff --git a/src/peer_hashtable.h b/src/peer_hashtable.h index 5952ae4..6c026bc 100644 --- a/src/peer_hashtable.h +++ b/src/peer_hashtable.h @@ -30,9 +30,9 @@ #include "types.h" -void fastd_peer_hashtable_init(fastd_context_t *ctx); -void fastd_peer_hashtable_free(fastd_context_t *ctx); +void fastd_peer_hashtable_init(void); +void fastd_peer_hashtable_free(void); -void fastd_peer_hashtable_insert(fastd_context_t *ctx, fastd_peer_t *peer); -void fastd_peer_hashtable_remove(fastd_context_t *ctx, fastd_peer_t *peer); -fastd_peer_t *fastd_peer_hashtable_lookup(fastd_context_t *ctx, const fastd_peer_address_t *addr); +void fastd_peer_hashtable_insert(fastd_peer_t *peer); +void fastd_peer_hashtable_remove(fastd_peer_t *peer); +fastd_peer_t *fastd_peer_hashtable_lookup(const fastd_peer_address_t *addr); diff --git a/src/poll.c b/src/poll.c index 7fd6b6b..79b847a 100644 --- a/src/poll.c +++ b/src/poll.c @@ -35,48 +35,48 @@ #endif -static inline bool handle_tap(fastd_context_t *ctx, fastd_buffer_t buffer) { +static inline bool handle_tap(fastd_buffer_t buffer) { if (conf.mode != MODE_TAP) return false; if (buffer.len < ETH_HLEN) { - pr_debug(ctx, "truncated packet on tap interface"); + pr_debug("truncated packet on tap interface"); fastd_buffer_free(buffer); return true; } - fastd_eth_addr_t dest_addr = fastd_get_dest_address(ctx, buffer); + fastd_eth_addr_t dest_addr = fastd_get_dest_address(buffer); if (!fastd_eth_addr_is_unicast(dest_addr)) return false; - fastd_peer_t *peer = fastd_peer_find_by_eth_addr(ctx, dest_addr); + fastd_peer_t *peer = fastd_peer_find_by_eth_addr(dest_addr); if (!peer) return false; - conf.protocol->send(ctx, peer, buffer); + conf.protocol->send(peer, buffer); return true; } -static void handle_tuntap(fastd_context_t *ctx) { - fastd_buffer_t buffer = fastd_tuntap_read(ctx); +static void handle_tuntap(void) { + fastd_buffer_t buffer = fastd_tuntap_read(); if (!buffer.len) return; - if (handle_tap(ctx, buffer)) + if (handle_tap(buffer)) return; /* TUN mode or multicast packet */ - fastd_send_all(ctx, NULL, buffer); + fastd_send_all(NULL, buffer); } -static inline int handshake_timeout(fastd_context_t *ctx) { - if (!ctx->handshake_queue.next) +static inline int handshake_timeout(void) { + if (!ctx.handshake_queue.next) return -1; - fastd_peer_t *peer = container_of(ctx->handshake_queue.next, fastd_peer_t, handshake_entry); + fastd_peer_t *peer = container_of(ctx.handshake_queue.next, fastd_peer_t, handshake_entry); - int diff_msec = timespec_diff(&peer->next_handshake, &ctx->now); + int diff_msec = timespec_diff(&peer->next_handshake, &ctx.now); if (diff_msec < 0) return 0; else @@ -90,45 +90,45 @@ static inline int handshake_timeout(fastd_context_t *ctx) { #include -void fastd_poll_init(fastd_context_t *ctx) { - ctx->epoll_fd = epoll_create1(EPOLL_CLOEXEC); - if (ctx->epoll_fd < 0) - exit_errno(ctx, "epoll_create1"); +void fastd_poll_init(void) { + ctx.epoll_fd = epoll_create1(EPOLL_CLOEXEC); + if (ctx.epoll_fd < 0) + exit_errno("epoll_create1"); struct epoll_event event = { .events = EPOLLIN, - .data.ptr = &ctx->async_rfd, + .data.ptr = &ctx.async_rfd, }; - if (epoll_ctl(ctx->epoll_fd, EPOLL_CTL_ADD, ctx->async_rfd, &event) < 0) - exit_errno(ctx, "epoll_ctl"); + if (epoll_ctl(ctx.epoll_fd, EPOLL_CTL_ADD, ctx.async_rfd, &event) < 0) + exit_errno("epoll_ctl"); } -void fastd_poll_free(fastd_context_t *ctx) { - if (close(ctx->epoll_fd)) - pr_warn_errno(ctx, "closing EPOLL: close"); +void fastd_poll_free(void) { + if (close(ctx.epoll_fd)) + pr_warn_errno("closing EPOLL: close"); } -void fastd_poll_set_fd_tuntap(fastd_context_t *ctx) { +void fastd_poll_set_fd_tuntap(void) { struct epoll_event event = { .events = EPOLLIN, - .data.ptr = &ctx->tunfd, + .data.ptr = &ctx.tunfd, }; - if (epoll_ctl(ctx->epoll_fd, EPOLL_CTL_ADD, ctx->tunfd, &event) < 0) - exit_errno(ctx, "epoll_ctl"); + if (epoll_ctl(ctx.epoll_fd, EPOLL_CTL_ADD, ctx.tunfd, &event) < 0) + exit_errno("epoll_ctl"); } -void fastd_poll_set_fd_sock(fastd_context_t *ctx, size_t i) { +void fastd_poll_set_fd_sock(size_t i) { struct epoll_event event = { .events = EPOLLIN, - .data.ptr = &ctx->socks[i], + .data.ptr = &ctx.socks[i], }; - if (epoll_ctl(ctx->epoll_fd, EPOLL_CTL_ADD, ctx->socks[i].fd, &event) < 0) - exit_errno(ctx, "epoll_ctl"); + if (epoll_ctl(ctx.epoll_fd, EPOLL_CTL_ADD, ctx.socks[i].fd, &event) < 0) + exit_errno("epoll_ctl"); } -void fastd_poll_set_fd_peer(fastd_context_t *ctx, size_t i) { - fastd_peer_t *peer = VECTOR_INDEX(ctx->peers, i); +void fastd_poll_set_fd_peer(size_t i) { + fastd_peer_t *peer = VECTOR_INDEX(ctx.peers, i); if (!peer->sock || !fastd_peer_is_socket_dynamic(peer)) return; @@ -137,59 +137,59 @@ void fastd_poll_set_fd_peer(fastd_context_t *ctx, size_t i) { .events = EPOLLIN, .data.ptr = peer->sock, }; - if (epoll_ctl(ctx->epoll_fd, EPOLL_CTL_ADD, peer->sock->fd, &event) < 0) - exit_errno(ctx, "epoll_ctl"); + if (epoll_ctl(ctx.epoll_fd, EPOLL_CTL_ADD, peer->sock->fd, &event) < 0) + exit_errno("epoll_ctl"); } -void fastd_poll_add_peer(fastd_context_t *ctx UNUSED) { +void fastd_poll_add_peer(void) { } -void fastd_poll_delete_peer(fastd_context_t *ctx UNUSED, size_t i UNUSED) { +void fastd_poll_delete_peer(size_t i UNUSED) { } -void fastd_poll_handle(fastd_context_t *ctx) { - int maintenance_timeout = timespec_diff(&ctx->next_maintenance, &ctx->now); +void fastd_poll_handle(void) { + int maintenance_timeout = timespec_diff(&ctx.next_maintenance, &ctx.now); if (maintenance_timeout < 0) maintenance_timeout = 0; - int timeout = handshake_timeout(ctx); + int timeout = handshake_timeout(); if (timeout < 0 || timeout > maintenance_timeout) timeout = maintenance_timeout; - fastd_update_time(ctx); + fastd_update_time(); struct epoll_event events[16]; - int ret = epoll_wait(ctx->epoll_fd, events, 16, timeout); + int ret = epoll_wait(ctx.epoll_fd, events, 16, timeout); if (ret < 0) { if (errno == EINTR) return; - exit_errno(ctx, "epoll_wait"); + exit_errno("epoll_wait"); } size_t i; for (i = 0; i < (size_t)ret; i++) { - if (events[i].data.ptr == &ctx->tunfd) { + if (events[i].data.ptr == &ctx.tunfd) { if (events[i].events & EPOLLIN) - handle_tuntap(ctx); + handle_tuntap(); } - else if (events[i].data.ptr == &ctx->async_rfd) { + else if (events[i].data.ptr == &ctx.async_rfd) { if (events[i].events & EPOLLIN) - fastd_async_handle(ctx); + fastd_async_handle(); } else { fastd_socket_t *sock = events[i].data.ptr; if (events[i].events & (EPOLLERR|EPOLLHUP)) { if (sock->peer) - fastd_peer_reset_socket(ctx, sock->peer); + fastd_peer_reset_socket(sock->peer); else - fastd_socket_error(ctx, sock); + fastd_socket_error(sock); } else if (events[i].events & EPOLLIN) { - fastd_receive(ctx, sock); + fastd_receive(sock); } } } @@ -197,24 +197,24 @@ void fastd_poll_handle(fastd_context_t *ctx) { #else -void fastd_poll_init(fastd_context_t *ctx) { - VECTOR_ALLOC(ctx->pollfds, 2 + ctx->n_socks); +void fastd_poll_init(void) { + VECTOR_ALLOC(ctx.pollfds, 2 + ctx.n_socks); - VECTOR_INDEX(ctx->pollfds, 0) = (struct pollfd) { + VECTOR_INDEX(ctx.pollfds, 0) = (struct pollfd) { .fd = -1, .events = POLLIN, .revents = 0, }; - VECTOR_INDEX(ctx->pollfds, 1) = (struct pollfd) { - .fd = ctx->async_rfd, + VECTOR_INDEX(ctx.pollfds, 1) = (struct pollfd) { + .fd = ctx.async_rfd, .events = POLLIN, .revents = 0, }; size_t i; - for (i = 0; i < ctx->n_socks; i++) { - VECTOR_INDEX(ctx->pollfds, 2+i) = (struct pollfd) { + for (i = 0; i < ctx.n_socks; i++) { + VECTOR_INDEX(ctx.pollfds, 2+i) = (struct pollfd) { .fd = -1, .events = POLLIN, .revents = 0, @@ -222,87 +222,87 @@ void fastd_poll_init(fastd_context_t *ctx) { } } -void fastd_poll_free(fastd_context_t *ctx) { - VECTOR_FREE(ctx->pollfds); +void fastd_poll_free(void) { + VECTOR_FREE(ctx.pollfds); } -void fastd_poll_set_fd_tuntap(fastd_context_t *ctx) { - VECTOR_INDEX(ctx->pollfds, 0).fd = ctx->tunfd; +void fastd_poll_set_fd_tuntap(void) { + VECTOR_INDEX(ctx.pollfds, 0).fd = ctx.tunfd; } -void fastd_poll_set_fd_sock(fastd_context_t *ctx, size_t i) { - VECTOR_INDEX(ctx->pollfds, 2+i).fd = ctx->socks[i].fd; +void fastd_poll_set_fd_sock(size_t i) { + VECTOR_INDEX(ctx.pollfds, 2+i).fd = ctx.socks[i].fd; } -void fastd_poll_set_fd_peer(fastd_context_t *ctx, size_t i) { - fastd_peer_t *peer = VECTOR_INDEX(ctx->peers, i); +void fastd_poll_set_fd_peer(size_t i) { + fastd_peer_t *peer = VECTOR_INDEX(ctx.peers, i); if (!peer->sock || !fastd_peer_is_socket_dynamic(peer)) - VECTOR_INDEX(ctx->pollfds, 2+ctx->n_socks+i).fd = -1; + VECTOR_INDEX(ctx.pollfds, 2+ctx.n_socks+i).fd = -1; else - VECTOR_INDEX(ctx->pollfds, 2+ctx->n_socks+i).fd = peer->sock->fd; + VECTOR_INDEX(ctx.pollfds, 2+ctx.n_socks+i).fd = peer->sock->fd; } -void fastd_poll_add_peer(fastd_context_t *ctx) { +void fastd_poll_add_peer(void) { struct pollfd pollfd = { .fd = -1, .events = POLLIN, .revents = 0, }; - VECTOR_ADD(ctx->pollfds, pollfd); + VECTOR_ADD(ctx.pollfds, pollfd); } -void fastd_poll_delete_peer(fastd_context_t *ctx, size_t i) { - VECTOR_DELETE(ctx->pollfds, 2+ctx->n_socks+i); +void fastd_poll_delete_peer(size_t i) { + VECTOR_DELETE(ctx.pollfds, 2+ctx.n_socks+i); } -void fastd_poll_handle(fastd_context_t *ctx) { - int maintenance_timeout = timespec_diff(&ctx->next_maintenance, &ctx->now); +void fastd_poll_handle(void) { + int maintenance_timeout = timespec_diff(&ctx.next_maintenance, &ctx.now); if (maintenance_timeout < 0) maintenance_timeout = 0; - int timeout = handshake_timeout(ctx); + int timeout = handshake_timeout(); if (timeout < 0 || timeout > maintenance_timeout) timeout = maintenance_timeout; - int ret = poll(VECTOR_DATA(ctx->pollfds), VECTOR_LEN(ctx->pollfds), timeout); + int ret = poll(VECTOR_DATA(ctx.pollfds), VECTOR_LEN(ctx.pollfds), timeout); if (ret < 0) { if (errno == EINTR) return; - exit_errno(ctx, "poll"); + exit_errno("poll"); } - fastd_update_time(ctx); + fastd_update_time(); - if (VECTOR_INDEX(ctx->pollfds, 0).revents & POLLIN) - handle_tuntap(ctx); - if (VECTOR_INDEX(ctx->pollfds, 1).revents & POLLIN) - fastd_async_handle(ctx); + if (VECTOR_INDEX(ctx.pollfds, 0).revents & POLLIN) + handle_tuntap(); + if (VECTOR_INDEX(ctx.pollfds, 1).revents & POLLIN) + fastd_async_handle(); size_t i; - for (i = 0; i < ctx->n_socks; i++) { - if (VECTOR_INDEX(ctx->pollfds, 2+i).revents & (POLLERR|POLLHUP|POLLNVAL)) { - fastd_socket_error(ctx, &ctx->socks[i]); - VECTOR_INDEX(ctx->pollfds, 2+i).fd = -1; + for (i = 0; i < ctx.n_socks; i++) { + if (VECTOR_INDEX(ctx.pollfds, 2+i).revents & (POLLERR|POLLHUP|POLLNVAL)) { + fastd_socket_error(&ctx.socks[i]); + VECTOR_INDEX(ctx.pollfds, 2+i).fd = -1; } - else if (VECTOR_INDEX(ctx->pollfds, 2+i).revents & POLLIN) { - fastd_receive(ctx, &ctx->socks[i]); + else if (VECTOR_INDEX(ctx.pollfds, 2+i).revents & POLLIN) { + fastd_receive(&ctx.socks[i]); } } - for (i = 0; i < VECTOR_LEN(ctx->peers); i++) { - fastd_peer_t *peer = VECTOR_INDEX(ctx->peers, i); + for (i = 0; i < VECTOR_LEN(ctx.peers); i++) { + fastd_peer_t *peer = VECTOR_INDEX(ctx.peers, i); - if (VECTOR_INDEX(ctx->pollfds, 2+ctx->n_socks+i).revents & (POLLERR|POLLHUP|POLLNVAL)) { - fastd_peer_reset_socket(ctx, peer); + if (VECTOR_INDEX(ctx.pollfds, 2+ctx.n_socks+i).revents & (POLLERR|POLLHUP|POLLNVAL)) { + fastd_peer_reset_socket(peer); } - else if (VECTOR_INDEX(ctx->pollfds, 2+ctx->n_socks+i).revents & POLLIN) { - fastd_receive(ctx, peer->sock); + else if (VECTOR_INDEX(ctx.pollfds, 2+ctx.n_socks+i).revents & POLLIN) { + fastd_receive(peer->sock); } } } diff --git a/src/poll.h b/src/poll.h index 4049824..dcf5d6c 100644 --- a/src/poll.h +++ b/src/poll.h @@ -30,14 +30,14 @@ #include "types.h" -void fastd_poll_init(fastd_context_t *ctx); -void fastd_poll_free(fastd_context_t *ctx); +void fastd_poll_init(void); +void fastd_poll_free(void); -void fastd_poll_set_fd_tuntap(fastd_context_t *ctx); -void fastd_poll_set_fd_sock(fastd_context_t *ctx, size_t i); -void fastd_poll_set_fd_peer(fastd_context_t *ctx, size_t i); +void fastd_poll_set_fd_tuntap(void); +void fastd_poll_set_fd_sock(size_t i); +void fastd_poll_set_fd_peer(size_t i); -void fastd_poll_add_peer(fastd_context_t *ctx); -void fastd_poll_delete_peer(fastd_context_t *ctx, size_t i); +void fastd_poll_add_peer(void); +void fastd_poll_delete_peer(size_t i); -void fastd_poll_handle(fastd_context_t *ctx); +void fastd_poll_handle(void); diff --git a/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c b/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c index 698746b..9d32f3b 100644 --- a/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c +++ b/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.c @@ -38,25 +38,25 @@ static inline bool read_key(uint8_t key[32], const char *hexkey) { return true; } -static inline void check_session_refresh(fastd_context_t *ctx, fastd_peer_t *peer) { +static inline void check_session_refresh(fastd_peer_t *peer) { protocol_session_t *session = &peer->protocol_state->session; - if (!session->refreshing && session->method->provider->session_want_refresh(ctx, session->method_state)) { - pr_verbose(ctx, "refreshing session with %P", peer); + if (!session->refreshing && session->method->provider->session_want_refresh(session->method_state)) { + pr_verbose("refreshing session with %P", peer); session->handshakes_cleaned = true; session->refreshing = true; - fastd_peer_schedule_handshake(ctx, peer, 0); + fastd_peer_schedule_handshake(peer, 0); } } -static fastd_protocol_config_t* protocol_init(fastd_context_t *ctx) { +static fastd_protocol_config_t* protocol_init(void) { fastd_protocol_config_t *protocol_config = malloc(sizeof(fastd_protocol_config_t)); if (!conf.secret) - exit_error(ctx, "no secret key configured"); + exit_error("no secret key configured"); if (!read_key(protocol_config->key.secret.p, conf.secret)) - exit_error(ctx, "invalid secret key"); + exit_error("invalid secret key"); ecc_25519_work_t work; ecc_25519_scalarmult_base(&work, &protocol_config->key.secret); @@ -65,27 +65,27 @@ static fastd_protocol_config_t* protocol_init(fastd_context_t *ctx) { return protocol_config; } -static void protocol_peer_verify(fastd_context_t *ctx, fastd_peer_config_t *peer_conf) { +static void protocol_peer_verify(fastd_peer_config_t *peer_conf) { if (!peer_conf->key) - exit_error(ctx, "no key configured for peer `%s'", peer_conf->name); + exit_error("no key configured for peer `%s'", peer_conf->name); aligned_int256_t key; if (!read_key(key.u8, peer_conf->key)) - exit_error(ctx, "invalid key configured for peer `%s'", peer_conf->name); + exit_error("invalid key configured for peer `%s'", peer_conf->name); } -static void protocol_peer_configure(fastd_context_t *ctx, fastd_peer_config_t *peer_conf) { +static void protocol_peer_configure(fastd_peer_config_t *peer_conf) { if (peer_conf->protocol_config) return; if (!peer_conf->key) { - pr_warn(ctx, "no key configured for `%s', disabling peer", peer_conf->name); + pr_warn("no key configured for `%s', disabling peer", peer_conf->name); return; } aligned_int256_t key; if (!read_key(key.u8, peer_conf->key)) { - pr_warn(ctx, "invalid key configured for `%s', disabling peer", peer_conf->name); + pr_warn("invalid key configured for `%s', disabling peer", peer_conf->name); return; } @@ -93,62 +93,62 @@ static void protocol_peer_configure(fastd_context_t *ctx, fastd_peer_config_t *p peer_conf->protocol_config->public_key = key; if (memcmp(&peer_conf->protocol_config->public_key, &conf.protocol_config->key.public, 32) == 0) - pr_debug(ctx, "found own key as `%s', ignoring peer", peer_conf->name); + pr_debug("found own key as `%s', ignoring peer", peer_conf->name); } -static inline bool check_session(fastd_context_t *ctx, fastd_peer_t *peer) { - if (is_session_valid(ctx, &peer->protocol_state->session)) +static inline bool check_session(fastd_peer_t *peer) { + if (is_session_valid(&peer->protocol_state->session)) return true; - pr_verbose(ctx, "active session with %P timed out", peer); - fastd_peer_reset(ctx, peer); + pr_verbose("active session with %P timed out", peer); + fastd_peer_reset(peer); return false; } -static void protocol_handle_recv(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer) { - if (!peer->protocol_state || !check_session(ctx, peer)) +static void protocol_handle_recv(fastd_peer_t *peer, fastd_buffer_t buffer) { + if (!peer->protocol_state || !check_session(peer)) goto fail; fastd_buffer_t recv_buffer; bool ok = false; - if (is_session_valid(ctx, &peer->protocol_state->old_session)) { - if (peer->protocol_state->old_session.method->provider->decrypt(ctx, peer, peer->protocol_state->old_session.method_state, &recv_buffer, buffer)) + if (is_session_valid(&peer->protocol_state->old_session)) { + if (peer->protocol_state->old_session.method->provider->decrypt(peer, peer->protocol_state->old_session.method_state, &recv_buffer, buffer)) ok = true; } if (!ok) { - if (peer->protocol_state->session.method->provider->decrypt(ctx, peer, peer->protocol_state->session.method_state, &recv_buffer, buffer)) { + if (peer->protocol_state->session.method->provider->decrypt(peer, peer->protocol_state->session.method_state, &recv_buffer, buffer)) { ok = true; if (peer->protocol_state->old_session.method) { - pr_debug(ctx, "invalidating old session with %P", peer); - peer->protocol_state->old_session.method->provider->session_free(ctx, peer->protocol_state->old_session.method_state); + pr_debug("invalidating old session with %P", peer); + peer->protocol_state->old_session.method->provider->session_free(peer->protocol_state->old_session.method_state); peer->protocol_state->old_session = (protocol_session_t){}; } if (!peer->protocol_state->session.handshakes_cleaned) { - pr_debug(ctx, "cleaning left handshakes with %P", peer); - fastd_peer_unschedule_handshake(ctx, peer); + pr_debug("cleaning left handshakes with %P", peer); + fastd_peer_unschedule_handshake(peer); peer->protocol_state->session.handshakes_cleaned = true; - if (peer->protocol_state->session.method->provider->session_is_initiator(ctx, peer->protocol_state->session.method_state)) - fastd_protocol_ec25519_fhmqvc_send_empty(ctx, peer, &peer->protocol_state->session); + if (peer->protocol_state->session.method->provider->session_is_initiator(peer->protocol_state->session.method_state)) + fastd_protocol_ec25519_fhmqvc_send_empty(peer, &peer->protocol_state->session); } - check_session_refresh(ctx, peer); + check_session_refresh(peer); } } if (!ok) { - pr_verbose(ctx, "verification failed for packet received from %P", peer); + pr_verbose("verification failed for packet received from %P", peer); goto fail; } - fastd_peer_seen(ctx, peer); + fastd_peer_seen(peer); if (recv_buffer.len) - fastd_handle_receive(ctx, peer, recv_buffer); + fastd_handle_receive(peer, recv_buffer); else fastd_buffer_free(recv_buffer); @@ -158,39 +158,39 @@ static void protocol_handle_recv(fastd_context_t *ctx, fastd_peer_t *peer, fastd fastd_buffer_free(buffer); } -static void session_send(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer, protocol_session_t *session) { +static void session_send(fastd_peer_t *peer, fastd_buffer_t buffer, protocol_session_t *session) { size_t stat_size = buffer.len; fastd_buffer_t send_buffer; - if (!session->method->provider->encrypt(ctx, peer, session->method_state, &send_buffer, buffer)) { + if (!session->method->provider->encrypt(peer, session->method_state, &send_buffer, buffer)) { fastd_buffer_free(buffer); - pr_error(ctx, "failed to encrypt packet for %P", peer); + pr_error("failed to encrypt packet for %P", peer); return; } - fastd_send(ctx, peer->sock, &peer->local_address, &peer->address, peer, send_buffer, stat_size); - peer->keepalive_timeout = fastd_in_seconds(ctx, conf.keepalive_timeout); + fastd_send(peer->sock, &peer->local_address, &peer->address, peer, send_buffer, stat_size); + peer->keepalive_timeout = fastd_in_seconds(conf.keepalive_timeout); } -static void protocol_send(fastd_context_t *ctx, fastd_peer_t *peer, fastd_buffer_t buffer) { - if (!peer->protocol_state || !fastd_peer_is_established(peer) || !check_session(ctx, peer)) { +static void protocol_send(fastd_peer_t *peer, fastd_buffer_t buffer) { + if (!peer->protocol_state || !fastd_peer_is_established(peer) || !check_session(peer)) { fastd_buffer_free(buffer); return; } - check_session_refresh(ctx, peer); + check_session_refresh(peer); - if (peer->protocol_state->session.method->provider->session_is_initiator(ctx, peer->protocol_state->session.method_state) && is_session_valid(ctx, &peer->protocol_state->old_session)) { - pr_debug2(ctx, "sending packet for old session to %P", peer); - session_send(ctx, peer, buffer, &peer->protocol_state->old_session); + if (peer->protocol_state->session.method->provider->session_is_initiator(peer->protocol_state->session.method_state) && is_session_valid(&peer->protocol_state->old_session)) { + pr_debug2("sending packet for old session to %P", peer); + session_send(peer, buffer, &peer->protocol_state->old_session); } else { - session_send(ctx, peer, buffer, &peer->protocol_state->session); + session_send(peer, buffer, &peer->protocol_state->session); } } -void fastd_protocol_ec25519_fhmqvc_send_empty(fastd_context_t *ctx, fastd_peer_t *peer, protocol_session_t *session) { - session_send(ctx, peer, fastd_buffer_alloc(ctx, 0, alignto(session->method->provider->min_encrypt_head_space, 8), session->method->provider->min_encrypt_tail_space), session); +void fastd_protocol_ec25519_fhmqvc_send_empty(fastd_peer_t *peer, protocol_session_t *session) { + session_send(peer, fastd_buffer_alloc(0, alignto(session->method->provider->min_encrypt_head_space, 8), session->method->provider->min_encrypt_tail_space), session); } const fastd_protocol_t fastd_protocol_ec25519_fhmqvc = { diff --git a/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.h b/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.h index be316db..468a7b2 100644 --- a/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.h +++ b/src/protocols/ec25519_fhmqvc/ec25519_fhmqvc.h @@ -76,23 +76,23 @@ struct fastd_protocol_peer_state { }; -bool fastd_protocol_ec25519_fhmqvc_peer_check(fastd_context_t *ctx, fastd_peer_config_t *peer_conf); -bool fastd_protocol_ec25519_fhmqvc_peer_check_temporary(fastd_context_t *ctx, fastd_peer_t *peer); +bool fastd_protocol_ec25519_fhmqvc_peer_check(fastd_peer_config_t *peer_conf); +bool fastd_protocol_ec25519_fhmqvc_peer_check_temporary(fastd_peer_t *peer); -void fastd_protocol_ec25519_fhmqvc_maintenance(fastd_context_t *ctx); -void fastd_protocol_ec25519_fhmqvc_init_peer_state(fastd_context_t *ctx, fastd_peer_t *peer); -void fastd_protocol_ec25519_fhmqvc_reset_peer_state(fastd_context_t *ctx, fastd_peer_t *peer); -void fastd_protocol_ec25519_fhmqvc_free_peer_state(fastd_context_t *ctx, fastd_peer_t *peer); +void fastd_protocol_ec25519_fhmqvc_maintenance(void); +void fastd_protocol_ec25519_fhmqvc_init_peer_state(fastd_peer_t *peer); +void fastd_protocol_ec25519_fhmqvc_reset_peer_state(fastd_peer_t *peer); +void fastd_protocol_ec25519_fhmqvc_free_peer_state(fastd_peer_t *peer); -void fastd_protocol_ec25519_fhmqvc_handshake_init(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer); -void fastd_protocol_ec25519_fhmqvc_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, const fastd_handshake_t *handshake, const fastd_method_info_t *method); +void fastd_protocol_ec25519_fhmqvc_handshake_init(const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer); +void fastd_protocol_ec25519_fhmqvc_handshake_handle(fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, const fastd_handshake_t *handshake, const fastd_method_info_t *method); -void fastd_protocol_ec25519_fhmqvc_send_empty(fastd_context_t *ctx, fastd_peer_t *peer, protocol_session_t *session); +void fastd_protocol_ec25519_fhmqvc_send_empty(fastd_peer_t *peer, protocol_session_t *session); -void fastd_protocol_ec25519_fhmqvc_generate_key(fastd_context_t *ctx); +void fastd_protocol_ec25519_fhmqvc_generate_key(void); void fastd_protocol_ec25519_fhmqvc_show_key(void); void fastd_protocol_ec25519_fhmqvc_set_shell_env(const fastd_peer_t *peer); -bool fastd_protocol_ec25519_fhmqvc_describe_peer(const fastd_context_t *ctx UNUSED, const fastd_peer_t *peer, char *buf, size_t len); +bool fastd_protocol_ec25519_fhmqvc_describe_peer(const fastd_peer_t *peer, char *buf, size_t len); static inline void hexdump(char out[65], const unsigned char d[32]) { @@ -102,6 +102,6 @@ static inline void hexdump(char out[65], const unsigned char d[32]) { } -static inline bool is_session_valid(fastd_context_t *ctx, const protocol_session_t *session) { - return (session->method && session->method->provider->session_is_valid(ctx, session->method_state)); +static inline bool is_session_valid(const protocol_session_t *session) { + return (session->method && session->method->provider->session_is_valid(session->method_state)); } diff --git a/src/protocols/ec25519_fhmqvc/handshake.c b/src/protocols/ec25519_fhmqvc/handshake.c index dda798b..df53157 100644 --- a/src/protocols/ec25519_fhmqvc/handshake.c +++ b/src/protocols/ec25519_fhmqvc/handshake.c @@ -68,46 +68,46 @@ static void derive_key(fastd_sha256_t *out, size_t blocks, const uint32_t *salt, fastd_hkdf_sha256_expand(out, blocks, &prk, info, sizeof(info)); } -static inline void supersede_session(fastd_context_t *ctx, fastd_peer_t *peer, const fastd_method_info_t *method) { - if (is_session_valid(ctx, &peer->protocol_state->session) && !is_session_valid(ctx, &peer->protocol_state->old_session)) { +static inline void supersede_session(fastd_peer_t *peer, const fastd_method_info_t *method) { + if (is_session_valid(&peer->protocol_state->session) && !is_session_valid(&peer->protocol_state->old_session)) { if (peer->protocol_state->old_session.method) - peer->protocol_state->old_session.method->provider->session_free(ctx, peer->protocol_state->old_session.method_state); + peer->protocol_state->old_session.method->provider->session_free(peer->protocol_state->old_session.method_state); peer->protocol_state->old_session = peer->protocol_state->session; } else { if (peer->protocol_state->session.method) - peer->protocol_state->session.method->provider->session_free(ctx, peer->protocol_state->session.method_state); + peer->protocol_state->session.method->provider->session_free(peer->protocol_state->session.method_state); } if (peer->protocol_state->old_session.method) { if (peer->protocol_state->old_session.method != method) { - pr_debug(ctx, "method of %P has changed, terminating old session", peer); - peer->protocol_state->old_session.method->provider->session_free(ctx, peer->protocol_state->old_session.method_state); + pr_debug("method of %P has changed, terminating old session", peer); + peer->protocol_state->old_session.method->provider->session_free(peer->protocol_state->old_session.method_state); peer->protocol_state->old_session = (protocol_session_t){}; } else { - peer->protocol_state->old_session.method->provider->session_superseded(ctx, peer->protocol_state->old_session.method_state); + peer->protocol_state->old_session.method->provider->session_superseded(peer->protocol_state->old_session.method_state); } } } -static inline bool new_session(fastd_context_t *ctx, fastd_peer_t *peer, const fastd_method_info_t *method, bool initiator, +static inline bool new_session(fastd_peer_t *peer, const fastd_method_info_t *method, bool initiator, const aligned_int256_t *A, const aligned_int256_t *B, const aligned_int256_t *X, const aligned_int256_t *Y, const aligned_int256_t *sigma, const uint32_t *salt, uint64_t serial) { - supersede_session(ctx, peer, method); + supersede_session(peer, method); if (salt) { - size_t blocks = block_count(method->provider->key_length(ctx, method->method), sizeof(fastd_sha256_t)); + size_t blocks = block_count(method->provider->key_length(method->method), sizeof(fastd_sha256_t)); fastd_sha256_t secret[blocks]; derive_key(secret, blocks, salt, method->name, A, B, X, Y, sigma); - peer->protocol_state->session.method_state = method->provider->session_init(ctx, method->method, (const uint8_t*)secret, initiator); + peer->protocol_state->session.method_state = method->provider->session_init(method->method, (const uint8_t*)secret, initiator); } else { fastd_sha256_t hash; fastd_sha256_blocks(&hash, X->u32, Y->u32, A->u32, B->u32, sigma->u32, NULL); - peer->protocol_state->session.method_state = method->provider->session_init_compat(ctx, method->method, hash.b, HASHBYTES, initiator); + peer->protocol_state->session.method_state = method->provider->session_init_compat(method->method, hash.b, HASHBYTES, initiator); } if (!peer->protocol_state->session.method_state) @@ -121,44 +121,44 @@ static inline bool new_session(fastd_context_t *ctx, fastd_peer_t *peer, const f return true; } -static bool establish(fastd_context_t *ctx, fastd_peer_t *peer, const fastd_method_info_t *method, fastd_socket_t *sock, +static bool establish(fastd_peer_t *peer, const fastd_method_info_t *method, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, bool initiator, const aligned_int256_t *A, const aligned_int256_t *B, const aligned_int256_t *X, const aligned_int256_t *Y, const aligned_int256_t *sigma, const uint32_t *salt, uint64_t serial) { if (serial <= peer->protocol_state->last_serial) { - pr_debug(ctx, "ignoring handshake from %P[%I] because of handshake key reuse", peer, remote_addr); + pr_debug("ignoring handshake from %P[%I] because of handshake key reuse", peer, remote_addr); return false; } if (!salt && !method->provider->session_init_compat) { - pr_warn(ctx, "can't establish compat session with %P[%I] (method without compat support)", peer, remote_addr); + pr_warn("can't establish compat session with %P[%I] (method without compat support)", peer, remote_addr); return false; } - pr_verbose(ctx, "%I authorized as %P", remote_addr, peer); + pr_verbose("%I authorized as %P", remote_addr, peer); - if (!fastd_peer_claim_address(ctx, peer, sock, local_addr, remote_addr)) { - pr_warn(ctx, "can't set address %I which is used by a fixed peer", remote_addr); - fastd_peer_reset(ctx, peer); + if (!fastd_peer_claim_address(peer, sock, local_addr, remote_addr)) { + pr_warn("can't set address %I which is used by a fixed peer", remote_addr); + fastd_peer_reset(peer); return false; } - if (!new_session(ctx, peer, method, initiator, A, B, X, Y, sigma, salt, serial)) { - pr_error(ctx, "failed to initialize method session for %P (method `%s'%s)", peer, method->name, salt ? "" : " (compat mode)"); - fastd_peer_reset(ctx, peer); + if (!new_session(peer, method, initiator, A, B, X, Y, sigma, salt, serial)) { + pr_error("failed to initialize method session for %P (method `%s'%s)", peer, method->name, salt ? "" : " (compat mode)"); + fastd_peer_reset(peer); return false; } - peer->establish_handshake_timeout = fastd_in_seconds(ctx, conf.min_handshake_interval); - fastd_peer_seen(ctx, peer); - fastd_peer_set_established(ctx, peer); + peer->establish_handshake_timeout = fastd_in_seconds(conf.min_handshake_interval); + fastd_peer_seen(peer); + fastd_peer_set_established(peer); - pr_verbose(ctx, "new session with %P established using method `%s'%s.", peer, method->name, salt ? "" : " (compat mode)"); + pr_verbose("new session with %P established using method `%s'%s.", peer, method->name, salt ? "" : " (compat mode)"); if (initiator) - fastd_peer_schedule_handshake_default(ctx, peer); + fastd_peer_schedule_handshake_default(peer); else - fastd_protocol_ec25519_fhmqvc_send_empty(ctx, peer, &peer->protocol_state->session); + fastd_protocol_ec25519_fhmqvc_send_empty(peer, &peer->protocol_state->session); return true; } @@ -260,7 +260,7 @@ static bool update_shared_handshake_key(const fastd_peer_t *peer, const handshak return true; } -static void clear_shared_handshake_key(fastd_context_t *ctx UNUSED, const fastd_peer_t *peer) { +static void clear_shared_handshake_key(const fastd_peer_t *peer) { memset(&peer->protocol_state->sigma, 0, sizeof(peer->protocol_state->sigma)); memset(&peer->protocol_state->shared_handshake_key, 0, sizeof(peer->protocol_state->shared_handshake_key)); memset(&peer->protocol_state->shared_handshake_key_compat, 0, sizeof(peer->protocol_state->shared_handshake_key_compat)); @@ -269,37 +269,37 @@ static void clear_shared_handshake_key(fastd_context_t *ctx UNUSED, const fastd_ memset(&peer->protocol_state->peer_handshake_key, 0, sizeof(peer->protocol_state->peer_handshake_key)); } -static void respond_handshake(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, +static void respond_handshake(const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, const handshake_key_t *handshake_key, const aligned_int256_t *peer_handshake_key, const fastd_handshake_t *handshake, const fastd_method_info_t *method) { - pr_debug(ctx, "responding handshake with %P[%I]...", peer, remote_addr); + pr_debug("responding handshake with %P[%I]...", peer, remote_addr); if (!update_shared_handshake_key(peer, handshake_key, peer_handshake_key)) return; - fastd_buffer_t buffer = fastd_handshake_new_reply(ctx, handshake, method, true, 4*(4+PUBLICKEYBYTES) + 2*(4+HASHBYTES)); + fastd_buffer_t buffer = fastd_handshake_new_reply(handshake, method, true, 4*(4+PUBLICKEYBYTES) + 2*(4+HASHBYTES)); - fastd_handshake_add(ctx, &buffer, RECORD_SENDER_KEY, PUBLICKEYBYTES, &conf.protocol_config->key.public); - fastd_handshake_add(ctx, &buffer, RECORD_RECEIPIENT_KEY, PUBLICKEYBYTES, &peer->protocol_config->public_key); - fastd_handshake_add(ctx, &buffer, RECORD_SENDER_HANDSHAKE_KEY, PUBLICKEYBYTES, &handshake_key->key.public); - fastd_handshake_add(ctx, &buffer, RECORD_RECEIPIENT_HANDSHAKE_KEY, PUBLICKEYBYTES, peer_handshake_key); + fastd_handshake_add(&buffer, RECORD_SENDER_KEY, PUBLICKEYBYTES, &conf.protocol_config->key.public); + fastd_handshake_add(&buffer, RECORD_RECEIPIENT_KEY, PUBLICKEYBYTES, &peer->protocol_config->public_key); + fastd_handshake_add(&buffer, RECORD_SENDER_HANDSHAKE_KEY, PUBLICKEYBYTES, &handshake_key->key.public); + fastd_handshake_add(&buffer, RECORD_RECEIPIENT_HANDSHAKE_KEY, PUBLICKEYBYTES, peer_handshake_key); fastd_sha256_t hmacbuf; if (!conf.secure_handshakes) { fastd_hmacsha256_blocks(&hmacbuf, peer->protocol_state->shared_handshake_key_compat.w, conf.protocol_config->key.public.u32, handshake_key->key.public.u32, NULL); - fastd_handshake_add(ctx, &buffer, RECORD_T, HASHBYTES, hmacbuf.b); + fastd_handshake_add(&buffer, RECORD_T, HASHBYTES, hmacbuf.b); } - uint8_t *mac = fastd_handshake_add_zero(ctx, &buffer, RECORD_TLV_MAC, HASHBYTES); + uint8_t *mac = fastd_handshake_add_zero(&buffer, RECORD_TLV_MAC, HASHBYTES); fastd_hmacsha256(&hmacbuf, peer->protocol_state->shared_handshake_key.w, fastd_handshake_tlv_data(&buffer), fastd_handshake_tlv_len(&buffer)); memcpy(mac, hmacbuf.b, HASHBYTES); - fastd_send_handshake(ctx, sock, local_addr, remote_addr, peer, buffer); + fastd_send_handshake(sock, local_addr, remote_addr, peer, buffer); } -static void finish_handshake(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, const handshake_key_t *handshake_key, const aligned_int256_t *peer_handshake_key, +static void finish_handshake(fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, const handshake_key_t *handshake_key, const aligned_int256_t *peer_handshake_key, const fastd_handshake_t *handshake, const fastd_method_info_t *method) { - pr_debug(ctx, "finishing handshake with %P[%I]...", peer, remote_addr); + pr_debug("finishing handshake with %P[%I]...", peer, remote_addr); bool compat = !secure_handshake(handshake); @@ -328,40 +328,40 @@ static void finish_handshake(fastd_context_t *ctx, fastd_socket_t *sock, const f } if (!valid) { - pr_warn(ctx, "received invalid protocol handshake response from %P[%I]", peer, remote_addr); + pr_warn("received invalid protocol handshake response from %P[%I]", peer, remote_addr); return; } - if (!establish(ctx, peer, method, sock, local_addr, remote_addr, true, &handshake_key->key.public, peer_handshake_key, &conf.protocol_config->key.public, + if (!establish(peer, method, sock, local_addr, remote_addr, true, &handshake_key->key.public, peer_handshake_key, &conf.protocol_config->key.public, &peer->protocol_config->public_key, &sigma, compat ? NULL : shared_handshake_key.w, handshake_key->serial)) return; - fastd_buffer_t buffer = fastd_handshake_new_reply(ctx, handshake, method, false, 4*(4+PUBLICKEYBYTES) + 2*(4+HASHBYTES)); + fastd_buffer_t buffer = fastd_handshake_new_reply(handshake, method, false, 4*(4+PUBLICKEYBYTES) + 2*(4+HASHBYTES)); - fastd_handshake_add(ctx, &buffer, RECORD_SENDER_KEY, PUBLICKEYBYTES, &conf.protocol_config->key.public); - fastd_handshake_add(ctx, &buffer, RECORD_RECEIPIENT_KEY, PUBLICKEYBYTES, &peer->protocol_config->public_key); - fastd_handshake_add(ctx, &buffer, RECORD_SENDER_HANDSHAKE_KEY, PUBLICKEYBYTES, &handshake_key->key.public); - fastd_handshake_add(ctx, &buffer, RECORD_RECEIPIENT_HANDSHAKE_KEY, PUBLICKEYBYTES, peer_handshake_key); + fastd_handshake_add(&buffer, RECORD_SENDER_KEY, PUBLICKEYBYTES, &conf.protocol_config->key.public); + fastd_handshake_add(&buffer, RECORD_RECEIPIENT_KEY, PUBLICKEYBYTES, &peer->protocol_config->public_key); + fastd_handshake_add(&buffer, RECORD_SENDER_HANDSHAKE_KEY, PUBLICKEYBYTES, &handshake_key->key.public); + fastd_handshake_add(&buffer, RECORD_RECEIPIENT_HANDSHAKE_KEY, PUBLICKEYBYTES, peer_handshake_key); if (!compat) { fastd_sha256_t hmacbuf; - uint8_t *mac = fastd_handshake_add_zero(ctx, &buffer, RECORD_TLV_MAC, HASHBYTES); + uint8_t *mac = fastd_handshake_add_zero(&buffer, RECORD_TLV_MAC, HASHBYTES); fastd_hmacsha256(&hmacbuf, shared_handshake_key.w, fastd_handshake_tlv_data(&buffer), fastd_handshake_tlv_len(&buffer)); memcpy(mac, hmacbuf.b, HASHBYTES); } else { fastd_sha256_t hmacbuf; fastd_hmacsha256_blocks(&hmacbuf, shared_handshake_key_compat.w, conf.protocol_config->key.public.u32, handshake_key->key.public.u32, NULL); - fastd_handshake_add(ctx, &buffer, RECORD_T, HASHBYTES, hmacbuf.b); + fastd_handshake_add(&buffer, RECORD_T, HASHBYTES, hmacbuf.b); } - fastd_send_handshake(ctx, sock, local_addr, remote_addr, peer, buffer); + fastd_send_handshake(sock, local_addr, remote_addr, peer, buffer); } -static void handle_finish_handshake(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, +static void handle_finish_handshake(fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, const handshake_key_t *handshake_key, const aligned_int256_t *peer_handshake_key, const fastd_handshake_t *handshake, const fastd_method_info_t *method) { - pr_debug(ctx, "handling handshake finish with %P[%I]...", peer, remote_addr); + pr_debug("handling handshake finish with %P[%I]...", peer, remote_addr); bool compat = !secure_handshake(handshake); @@ -381,27 +381,27 @@ static void handle_finish_handshake(fastd_context_t *ctx, fastd_socket_t *sock, } if (!valid) { - pr_warn(ctx, "received invalid protocol handshake finish from %P[%I]", peer, remote_addr); + pr_warn("received invalid protocol handshake finish from %P[%I]", peer, remote_addr); return; } - establish(ctx, peer, method, sock, local_addr, remote_addr, false, peer_handshake_key, &handshake_key->key.public, &peer->protocol_config->public_key, + establish(peer, method, sock, local_addr, remote_addr, false, peer_handshake_key, &handshake_key->key.public, &peer->protocol_config->public_key, &conf.protocol_config->key.public, &peer->protocol_state->sigma, compat ? NULL : peer->protocol_state->shared_handshake_key.w, handshake_key->serial); - clear_shared_handshake_key(ctx, peer); + clear_shared_handshake_key(peer); } -static fastd_peer_t* find_sender_key(fastd_context_t *ctx, const fastd_peer_address_t *address, const unsigned char key[32]) { +static fastd_peer_t* find_sender_key(const fastd_peer_address_t *address, const unsigned char key[32]) { errno = 0; fastd_peer_t *ret = NULL; size_t i; - for (i = 0; i < VECTOR_LEN(ctx->peers); i++) { - fastd_peer_t *peer = VECTOR_INDEX(ctx->peers, i); + for (i = 0; i < VECTOR_LEN(ctx.peers); i++) { + fastd_peer_t *peer = VECTOR_INDEX(ctx.peers, i); if (memcmp(&peer->protocol_config->public_key, key, PUBLICKEYBYTES) == 0) { - if (!fastd_peer_matches_address(ctx, peer, address)) { + if (!fastd_peer_matches_address(peer, address)) { errno = EPERM; return NULL; } @@ -410,7 +410,7 @@ static fastd_peer_t* find_sender_key(fastd_context_t *ctx, const fastd_peer_addr continue; } - if (fastd_peer_owns_address(ctx, peer, address)) { + if (fastd_peer_owns_address(peer, address)) { errno = EPERM; return NULL; } @@ -422,23 +422,23 @@ static fastd_peer_t* find_sender_key(fastd_context_t *ctx, const fastd_peer_addr return ret; } -static fastd_peer_t* match_sender_key(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *address, fastd_peer_t *peer, const unsigned char key[32]) { +static fastd_peer_t* match_sender_key(const fastd_socket_t *sock, const fastd_peer_address_t *address, fastd_peer_t *peer, const unsigned char key[32]) { errno = 0; if (sock->peer && peer != sock->peer) - exit_bug(ctx, "packet without correct peer set on dynamic socket"); + exit_bug("packet without correct peer set on dynamic socket"); if (peer) { if (memcmp(&peer->protocol_config->public_key, key, PUBLICKEYBYTES) == 0) return peer; - if (fastd_peer_owns_address(ctx, peer, address)) { + if (fastd_peer_owns_address(peer, address)) { errno = EPERM; return NULL; } } - return find_sender_key(ctx, address, key); + return find_sender_key(address, key); } static size_t key_count(const unsigned char key[32]) { @@ -456,7 +456,7 @@ static size_t key_count(const unsigned char key[32]) { return ret; } -bool fastd_protocol_ec25519_fhmqvc_peer_check(fastd_context_t *ctx, fastd_peer_config_t *peer_conf) { +bool fastd_protocol_ec25519_fhmqvc_peer_check(fastd_peer_config_t *peer_conf) { if (!peer_conf->protocol_config) return false; @@ -466,18 +466,18 @@ bool fastd_protocol_ec25519_fhmqvc_peer_check(fastd_context_t *ctx, fastd_peer_c if (key_count(peer_conf->protocol_config->public_key.u8) > 1) { char buf[65]; hexdump(buf, peer_conf->protocol_config->public_key.u8); - pr_warn(ctx, "more than one peer is configured with key %s, disabling %s", buf, peer_conf->name); + pr_warn("more than one peer is configured with key %s, disabling %s", buf, peer_conf->name); return false; } return true; } -bool fastd_protocol_ec25519_fhmqvc_peer_check_temporary(fastd_context_t *ctx, fastd_peer_t *peer) { +bool fastd_protocol_ec25519_fhmqvc_peer_check_temporary(fastd_peer_t *peer) { if (key_count(peer->protocol_config->public_key.u8)) { char buf[65]; hexdump(buf, peer->protocol_config->public_key.u8); - pr_info(ctx, "key %s is configured now, deleting temporary peer.", buf); + pr_info("key %s is configured now, deleting temporary peer.", buf); return false; } @@ -488,18 +488,18 @@ static inline bool allow_unknown(void) { return fastd_shell_command_isset(&conf.on_verify); } -static inline fastd_peer_t* add_temporary(fastd_context_t *ctx, const fastd_peer_address_t *addr, const unsigned char key[32]) { +static inline fastd_peer_t* add_temporary(const fastd_peer_address_t *addr, const unsigned char key[32]) { if (!allow_unknown()) { - pr_debug(ctx, "ignoring handshake from %I (unknown key)", addr); + pr_debug("ignoring handshake from %I (unknown key)", addr); return NULL; } if (key_count(key)) { - pr_debug(ctx, "ignoring handshake from %I (disabled key)", addr); + pr_debug("ignoring handshake from %I (disabled key)", addr); return NULL; } - fastd_peer_t *peer = fastd_peer_add_temporary(ctx); + fastd_peer_t *peer = fastd_peer_add_temporary(); peer->protocol_config = malloc(sizeof(fastd_protocol_peer_config_t)); memcpy(&peer->protocol_config->public_key, key, PUBLICKEYBYTES); @@ -511,79 +511,79 @@ static inline fastd_peer_t* add_temporary(fastd_context_t *ctx, const fastd_peer } -void fastd_protocol_ec25519_fhmqvc_handshake_init(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer) { - fastd_protocol_ec25519_fhmqvc_maintenance(ctx); +void fastd_protocol_ec25519_fhmqvc_handshake_init(const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer) { + fastd_protocol_ec25519_fhmqvc_maintenance(); - fastd_buffer_t buffer = fastd_handshake_new_init(ctx, 3*(4+PUBLICKEYBYTES) /* sender key, receipient key, handshake key */); + fastd_buffer_t buffer = fastd_handshake_new_init(3*(4+PUBLICKEYBYTES) /* sender key, receipient key, handshake key */); - fastd_handshake_add(ctx, &buffer, RECORD_SENDER_KEY, PUBLICKEYBYTES, &conf.protocol_config->key.public); + fastd_handshake_add(&buffer, RECORD_SENDER_KEY, PUBLICKEYBYTES, &conf.protocol_config->key.public); if (peer) - fastd_handshake_add(ctx, &buffer, RECORD_RECEIPIENT_KEY, PUBLICKEYBYTES, &peer->protocol_config->public_key); + fastd_handshake_add(&buffer, RECORD_RECEIPIENT_KEY, PUBLICKEYBYTES, &peer->protocol_config->public_key); else - pr_debug(ctx, "sending handshake to unknown peer %I", remote_addr); + pr_debug("sending handshake to unknown peer %I", remote_addr); - fastd_handshake_add(ctx, &buffer, RECORD_SENDER_HANDSHAKE_KEY, PUBLICKEYBYTES, &ctx->protocol_state->handshake_key.key.public); + fastd_handshake_add(&buffer, RECORD_SENDER_HANDSHAKE_KEY, PUBLICKEYBYTES, &ctx.protocol_state->handshake_key.key.public); if (!peer || !fastd_peer_is_established(peer)) - fastd_shell_command_exec(ctx, &conf.on_connect, peer, (local_addr && local_addr->sa.sa_family) ? local_addr : sock->bound_addr, remote_addr); + fastd_shell_command_exec(&conf.on_connect, peer, (local_addr && local_addr->sa.sa_family) ? local_addr : sock->bound_addr, remote_addr); - fastd_send_handshake(ctx, sock, local_addr, remote_addr, peer, buffer); + fastd_send_handshake(sock, local_addr, remote_addr, peer, buffer); } -void fastd_protocol_ec25519_fhmqvc_handshake_handle(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, +void fastd_protocol_ec25519_fhmqvc_handshake_handle(fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, const fastd_handshake_t *handshake, const fastd_method_info_t *method) { - fastd_protocol_ec25519_fhmqvc_maintenance(ctx); + fastd_protocol_ec25519_fhmqvc_maintenance(); if (!has_field(handshake, RECORD_SENDER_KEY, PUBLICKEYBYTES)) { - pr_debug(ctx, "received handshake without sender key from %I", remote_addr); + pr_debug("received handshake without sender key from %I", remote_addr); return; } - peer = match_sender_key(ctx, sock, remote_addr, peer, handshake->records[RECORD_SENDER_KEY].data); + peer = match_sender_key(sock, remote_addr, peer, handshake->records[RECORD_SENDER_KEY].data); if (!peer) { switch (errno) { case EPERM: - pr_debug(ctx, "ignoring handshake from %I (incorrect source address)", remote_addr); + pr_debug("ignoring handshake from %I (incorrect source address)", remote_addr); return; case ENOENT: - peer = add_temporary(ctx, remote_addr, handshake->records[RECORD_SENDER_KEY].data); + peer = add_temporary(remote_addr, handshake->records[RECORD_SENDER_KEY].data); if (peer) break; return; default: - exit_bug(ctx, "match_sender_key: unknown error"); + exit_bug("match_sender_key: unknown error"); } } - if (fastd_peer_is_temporary(peer) && !fastd_peer_verify_temporary(ctx, peer, local_addr, remote_addr)) { - pr_debug(ctx, "ignoring handshake from %P[%I] (verification failed)", peer, remote_addr); - fastd_peer_delete(ctx, peer); + if (fastd_peer_is_temporary(peer) && !fastd_peer_verify_temporary(peer, local_addr, remote_addr)) { + pr_debug("ignoring handshake from %P[%I] (verification failed)", peer, remote_addr); + fastd_peer_delete(peer); return; } - if (!fastd_peer_may_connect(ctx, peer)) { - pr_debug(ctx, "ignoring handshake from %P[%I] because of local constraints", peer, remote_addr); + if (!fastd_peer_may_connect(peer)) { + pr_debug("ignoring handshake from %P[%I] because of local constraints", peer, remote_addr); return; } - if (!fastd_timed_out(ctx, &peer->establish_handshake_timeout)) { - pr_debug(ctx, "received repeated handshakes from %P[%I], ignoring", peer, remote_addr); + if (!fastd_timed_out(&peer->establish_handshake_timeout)) { + pr_debug("received repeated handshakes from %P[%I], ignoring", peer, remote_addr); return; } if (has_field(handshake, RECORD_RECEIPIENT_KEY, PUBLICKEYBYTES)) { if (memcmp(&conf.protocol_config->key.public, handshake->records[RECORD_RECEIPIENT_KEY].data, PUBLICKEYBYTES) != 0) { - pr_debug(ctx, "received protocol handshake with wrong receipient key from %P[%I]", peer, remote_addr); + pr_debug("received protocol handshake with wrong receipient key from %P[%I]", peer, remote_addr); return; } } if (!has_field(handshake, RECORD_SENDER_HANDSHAKE_KEY, PUBLICKEYBYTES)) { - pr_debug(ctx, "received handshake without sender handshake key from %P[%I]", peer, remote_addr); + pr_debug("received handshake without sender handshake key from %P[%I]", peer, remote_addr); return; } @@ -591,65 +591,65 @@ void fastd_protocol_ec25519_fhmqvc_handshake_handle(fastd_context_t *ctx, fastd_ memcpy(&peer_handshake_key, handshake->records[RECORD_SENDER_HANDSHAKE_KEY].data, PUBLICKEYBYTES); if (handshake->type == 1) { - if (!fastd_timed_out(ctx, &peer->last_handshake_response_timeout) + if (!fastd_timed_out(&peer->last_handshake_response_timeout) && fastd_peer_address_equal(remote_addr, &peer->last_handshake_response_address)) { - pr_debug(ctx, "not responding to repeated handshake from %P[%I]", peer, remote_addr); + pr_debug("not responding to repeated handshake from %P[%I]", peer, remote_addr); return; } - pr_verbose(ctx, "received handshake from %P[%I]%s%s", peer, remote_addr, handshake->peer_version ? " using fastd " : "", handshake->peer_version ?: ""); + pr_verbose("received handshake from %P[%I]%s%s", peer, remote_addr, handshake->peer_version ? " using fastd " : "", handshake->peer_version ?: ""); - peer->last_handshake_response_timeout = fastd_in_seconds(ctx, conf.min_handshake_interval); + peer->last_handshake_response_timeout = fastd_in_seconds(conf.min_handshake_interval); peer->last_handshake_response_address = *remote_addr; - respond_handshake(ctx, sock, local_addr, remote_addr, peer, &ctx->protocol_state->handshake_key, &peer_handshake_key, handshake, method); + respond_handshake(sock, local_addr, remote_addr, peer, &ctx.protocol_state->handshake_key, &peer_handshake_key, handshake, method); return; } if (!has_field(handshake, RECORD_RECEIPIENT_KEY, PUBLICKEYBYTES)) { - pr_debug(ctx, "received handshake reply without receipient key from %P[%I]", peer, remote_addr); + pr_debug("received handshake reply without receipient key from %P[%I]", peer, remote_addr); return; } if (!has_field(handshake, RECORD_RECEIPIENT_HANDSHAKE_KEY, PUBLICKEYBYTES)) { - pr_debug(ctx, "received handshake reply without receipient handshake key from %P[%I]", peer, remote_addr); + pr_debug("received handshake reply without receipient handshake key from %P[%I]", peer, remote_addr); return; } if (!secure_handshake(handshake)) { if (conf.secure_handshakes || !has_field(handshake, RECORD_T, HASHBYTES)) { - pr_debug(ctx, "received handshake reply without HMAC from %P[%I]", peer, remote_addr); + pr_debug("received handshake reply without HMAC from %P[%I]", peer, remote_addr); return; } } handshake_key_t *handshake_key; - if (is_handshake_key_valid(ctx, &ctx->protocol_state->handshake_key) && - memcmp(&ctx->protocol_state->handshake_key.key.public, handshake->records[RECORD_RECEIPIENT_HANDSHAKE_KEY].data, PUBLICKEYBYTES) == 0) { - handshake_key = &ctx->protocol_state->handshake_key; + if (is_handshake_key_valid(&ctx.protocol_state->handshake_key) && + memcmp(&ctx.protocol_state->handshake_key.key.public, handshake->records[RECORD_RECEIPIENT_HANDSHAKE_KEY].data, PUBLICKEYBYTES) == 0) { + handshake_key = &ctx.protocol_state->handshake_key; } - else if (is_handshake_key_valid(ctx, &ctx->protocol_state->prev_handshake_key) && - memcmp(&ctx->protocol_state->prev_handshake_key.key.public, handshake->records[RECORD_RECEIPIENT_HANDSHAKE_KEY].data, PUBLICKEYBYTES) == 0) { - handshake_key = &ctx->protocol_state->prev_handshake_key; + else if (is_handshake_key_valid(&ctx.protocol_state->prev_handshake_key) && + memcmp(&ctx.protocol_state->prev_handshake_key.key.public, handshake->records[RECORD_RECEIPIENT_HANDSHAKE_KEY].data, PUBLICKEYBYTES) == 0) { + handshake_key = &ctx.protocol_state->prev_handshake_key; } else { - pr_debug(ctx, "received handshake reply with unexpected receipient handshake key from %P[%I]", peer, remote_addr); + pr_debug("received handshake reply with unexpected receipient handshake key from %P[%I]", peer, remote_addr); return; } switch (handshake->type) { case 2: - pr_verbose(ctx, "received handshake response from %P[%I]%s%s", peer, remote_addr, handshake->peer_version ? " using fastd " : "", handshake->peer_version ?: ""); + pr_verbose("received handshake response from %P[%I]%s%s", peer, remote_addr, handshake->peer_version ? " using fastd " : "", handshake->peer_version ?: ""); - finish_handshake(ctx, sock, local_addr, remote_addr, peer, handshake_key, &peer_handshake_key, handshake, method); + finish_handshake(sock, local_addr, remote_addr, peer, handshake_key, &peer_handshake_key, handshake, method); break; case 3: - pr_debug(ctx, "received handshake finish from %P[%I]%s%s", peer, remote_addr, handshake->peer_version ? " using fastd " : "", handshake->peer_version ?: ""); + pr_debug("received handshake finish from %P[%I]%s%s", peer, remote_addr, handshake->peer_version ? " using fastd " : "", handshake->peer_version ?: ""); - handle_finish_handshake(ctx, sock, local_addr, remote_addr, peer, handshake_key, &peer_handshake_key, handshake, method); + handle_finish_handshake(sock, local_addr, remote_addr, peer, handshake_key, &peer_handshake_key, handshake, method); break; default: - pr_debug(ctx, "received handshake reply with unknown type %u from %P[%I]", handshake->type, peer, remote_addr); + pr_debug("received handshake reply with unknown type %u from %P[%I]", handshake->type, peer, remote_addr); } } diff --git a/src/protocols/ec25519_fhmqvc/handshake.h b/src/protocols/ec25519_fhmqvc/handshake.h index 867b0fa..1578913 100644 --- a/src/protocols/ec25519_fhmqvc/handshake.h +++ b/src/protocols/ec25519_fhmqvc/handshake.h @@ -43,10 +43,10 @@ struct fastd_protocol_state { }; -static inline bool is_handshake_key_valid(fastd_context_t *ctx, const handshake_key_t *handshake_key) { - return !fastd_timed_out(ctx, &handshake_key->valid_till); +static inline bool is_handshake_key_valid(const handshake_key_t *handshake_key) { + return !fastd_timed_out(&handshake_key->valid_till); } -static inline bool is_handshake_key_preferred(fastd_context_t *ctx, const handshake_key_t *handshake_key) { - return !fastd_timed_out(ctx, &handshake_key->preferred_till); +static inline bool is_handshake_key_preferred(const handshake_key_t *handshake_key) { + return !fastd_timed_out(&handshake_key->preferred_till); } diff --git a/src/protocols/ec25519_fhmqvc/state.c b/src/protocols/ec25519_fhmqvc/state.c index fe0dcde..7f3b847 100644 --- a/src/protocols/ec25519_fhmqvc/state.c +++ b/src/protocols/ec25519_fhmqvc/state.c @@ -28,17 +28,17 @@ #include "../../crypto.h" -static void init_protocol_state(fastd_context_t *ctx) { - if (!ctx->protocol_state) { - ctx->protocol_state = calloc(1, sizeof(fastd_protocol_state_t)); +static void init_protocol_state(void) { + if (!ctx.protocol_state) { + ctx.protocol_state = calloc(1, sizeof(fastd_protocol_state_t)); - ctx->protocol_state->prev_handshake_key.preferred_till = ctx->now; - ctx->protocol_state->handshake_key.preferred_till = ctx->now; + ctx.protocol_state->prev_handshake_key.preferred_till = ctx.now; + ctx.protocol_state->handshake_key.preferred_till = ctx.now; } } -static void new_handshake_key(fastd_context_t *ctx, keypair_t *key) { - fastd_random_bytes(ctx, key->secret.p, 32, false); +static void new_handshake_key(keypair_t *key) { + fastd_random_bytes(key->secret.p, 32, false); ecc_25519_gf_sanitize_secret(&key->secret, &key->secret); ecc_25519_work_t work; @@ -46,51 +46,51 @@ static void new_handshake_key(fastd_context_t *ctx, keypair_t *key) { ecc_25519_store_packed(&key->public.int256, &work); } -void fastd_protocol_ec25519_fhmqvc_maintenance(fastd_context_t *ctx) { - init_protocol_state(ctx); +void fastd_protocol_ec25519_fhmqvc_maintenance(void) { + init_protocol_state(); - if (!is_handshake_key_preferred(ctx, &ctx->protocol_state->handshake_key)) { - pr_debug(ctx, "generating new handshake key"); + if (!is_handshake_key_preferred(&ctx.protocol_state->handshake_key)) { + pr_debug("generating new handshake key"); - ctx->protocol_state->prev_handshake_key = ctx->protocol_state->handshake_key; + ctx.protocol_state->prev_handshake_key = ctx.protocol_state->handshake_key; - ctx->protocol_state->handshake_key.serial++; + ctx.protocol_state->handshake_key.serial++; - new_handshake_key(ctx, &ctx->protocol_state->handshake_key.key); + new_handshake_key(&ctx.protocol_state->handshake_key.key); - ctx->protocol_state->handshake_key.preferred_till = fastd_in_seconds(ctx, 15); - ctx->protocol_state->handshake_key.valid_till = fastd_in_seconds(ctx, 30); + ctx.protocol_state->handshake_key.preferred_till = fastd_in_seconds(15); + ctx.protocol_state->handshake_key.valid_till = fastd_in_seconds(30); } } -void fastd_protocol_ec25519_fhmqvc_init_peer_state(fastd_context_t *ctx, fastd_peer_t *peer) { - init_protocol_state(ctx); +void fastd_protocol_ec25519_fhmqvc_init_peer_state(fastd_peer_t *peer) { + init_protocol_state(); if (peer->protocol_state) - exit_bug(ctx, "tried to reinit peer state"); + exit_bug("tried to reinit peer state"); peer->protocol_state = calloc(1, sizeof(fastd_protocol_peer_state_t)); - peer->protocol_state->last_serial = ctx->protocol_state->handshake_key.serial; + peer->protocol_state->last_serial = ctx.protocol_state->handshake_key.serial; } -static void reset_session(fastd_context_t *ctx, protocol_session_t *session) { +static void reset_session(protocol_session_t *session) { if (session->method) - session->method->provider->session_free(ctx, session->method_state); + session->method->provider->session_free(session->method_state); secure_memzero(session, sizeof(protocol_session_t)); } -void fastd_protocol_ec25519_fhmqvc_reset_peer_state(fastd_context_t *ctx, fastd_peer_t *peer) { +void fastd_protocol_ec25519_fhmqvc_reset_peer_state(fastd_peer_t *peer) { if (!peer->protocol_state) return; - reset_session(ctx, &peer->protocol_state->old_session); - reset_session(ctx, &peer->protocol_state->session); + reset_session(&peer->protocol_state->old_session); + reset_session(&peer->protocol_state->session); } -void fastd_protocol_ec25519_fhmqvc_free_peer_state(fastd_context_t *ctx, fastd_peer_t *peer) { +void fastd_protocol_ec25519_fhmqvc_free_peer_state(fastd_peer_t *peer) { if (peer->protocol_state) { - reset_session(ctx, &peer->protocol_state->old_session); - reset_session(ctx, &peer->protocol_state->session); + reset_session(&peer->protocol_state->old_session); + reset_session(&peer->protocol_state->session); free(peer->protocol_state); } diff --git a/src/protocols/ec25519_fhmqvc/util.c b/src/protocols/ec25519_fhmqvc/util.c index a72ebbf..5bd23b3 100644 --- a/src/protocols/ec25519_fhmqvc/util.c +++ b/src/protocols/ec25519_fhmqvc/util.c @@ -34,14 +34,14 @@ static inline void print_hexdump(const char *desc, unsigned char d[32]) { printf("%s%s\n", desc, buf); } -void fastd_protocol_ec25519_fhmqvc_generate_key(fastd_context_t *ctx) { +void fastd_protocol_ec25519_fhmqvc_generate_key(void) { ecc_int256_t secret_key; ecc_int256_t public_key; if (!conf.machine_readable) - pr_info(ctx, "Reading 32 bytes from /dev/random..."); + pr_info("Reading 32 bytes from /dev/random..."); - fastd_random_bytes(ctx, secret_key.p, 32, true); + fastd_random_bytes(secret_key.p, 32, true); ecc_25519_gf_sanitize_secret(&secret_key, &secret_key); ecc_25519_work_t work; @@ -79,7 +79,7 @@ void fastd_protocol_ec25519_fhmqvc_set_shell_env(const fastd_peer_t *peer) { } } -bool fastd_protocol_ec25519_fhmqvc_describe_peer(const fastd_context_t *ctx UNUSED, const fastd_peer_t *peer, char *buf, size_t len) { +bool fastd_protocol_ec25519_fhmqvc_describe_peer(const fastd_peer_t *peer, char *buf, size_t len) { if (peer && peer->protocol_config) { char dumpbuf[65]; diff --git a/src/random.c b/src/random.c index 9c7359f..ce7183b 100644 --- a/src/random.c +++ b/src/random.c @@ -30,7 +30,7 @@ #include -void fastd_random_bytes(fastd_context_t *ctx, void *buffer, size_t len, bool secure) { +void fastd_random_bytes(void *buffer, size_t len, bool secure) { int fd; size_t read_bytes = 0; @@ -40,7 +40,7 @@ void fastd_random_bytes(fastd_context_t *ctx, void *buffer, size_t len, bool sec fd = open("/dev/urandom", O_RDONLY); if (fd < 0) - exit_errno(ctx, "unable to open random device"); + exit_errno("unable to open random device"); while (read_bytes < len) { ssize_t ret = read(fd, ((char*)buffer)+read_bytes, len-read_bytes); @@ -49,7 +49,7 @@ void fastd_random_bytes(fastd_context_t *ctx, void *buffer, size_t len, bool sec if (errno == EINTR) continue; - exit_errno(ctx, "unable to read from random device"); + exit_errno("unable to read from random device"); } read_bytes += ret; diff --git a/src/receive.c b/src/receive.c index 49f05fe..a8ec1ce 100644 --- a/src/receive.c +++ b/src/receive.c @@ -77,57 +77,57 @@ static inline void handle_socket_control(struct msghdr *message, const fastd_soc } } -static bool backoff_unknown(fastd_context_t *ctx, const fastd_peer_address_t *addr) { +static bool backoff_unknown(const fastd_peer_address_t *addr) { size_t i; - for (i = 0; i < array_size(ctx->unknown_handshakes); i++) { - const fastd_handshake_timeout_t *t = &ctx->unknown_handshakes[(ctx->unknown_handshake_pos + i) % array_size(ctx->unknown_handshakes)]; + for (i = 0; i < array_size(ctx.unknown_handshakes); i++) { + const fastd_handshake_timeout_t *t = &ctx.unknown_handshakes[(ctx.unknown_handshake_pos + i) % array_size(ctx.unknown_handshakes)]; - if (fastd_timed_out(ctx, &t->timeout)) + if (fastd_timed_out(&t->timeout)) break; if (fastd_peer_address_equal(addr, &t->address)) { - pr_debug2(ctx, "sent a handshake to unknown address %I a short time ago, not sending again", addr); + pr_debug2("sent a handshake to unknown address %I a short time ago, not sending again", addr); return true; } } - if (ctx->unknown_handshake_pos == 0) - ctx->unknown_handshake_pos = array_size(ctx->unknown_handshakes)-1; + if (ctx.unknown_handshake_pos == 0) + ctx.unknown_handshake_pos = array_size(ctx.unknown_handshakes)-1; else - ctx->unknown_handshake_pos--; + ctx.unknown_handshake_pos--; - fastd_handshake_timeout_t *t = &ctx->unknown_handshakes[ctx->unknown_handshake_pos]; + fastd_handshake_timeout_t *t = &ctx.unknown_handshakes[ctx.unknown_handshake_pos]; t->address = *addr; - t->timeout = fastd_in_seconds(ctx, conf.min_handshake_interval); + t->timeout = fastd_in_seconds(conf.min_handshake_interval); return false; } -static inline void handle_socket_receive_known(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer) { - if (!fastd_peer_may_connect(ctx, peer)) { +static inline void handle_socket_receive_known(fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer) { + if (!fastd_peer_may_connect(peer)) { fastd_buffer_free(buffer); return; } const uint8_t *packet_type = buffer.data; - fastd_buffer_push_head(ctx, &buffer, 1); + fastd_buffer_push_head(&buffer, 1); switch (*packet_type) { case PACKET_DATA: if (!fastd_peer_is_established(peer) || !fastd_peer_address_equal(&peer->local_address, local_addr)) { fastd_buffer_free(buffer); - if (!backoff_unknown(ctx, remote_addr)) - conf.protocol->handshake_init(ctx, sock, local_addr, remote_addr, NULL); + if (!backoff_unknown(remote_addr)) + conf.protocol->handshake_init(sock, local_addr, remote_addr, NULL); return; } - conf.protocol->handle_recv(ctx, peer, buffer); + conf.protocol->handle_recv(peer, buffer); break; case PACKET_HANDSHAKE: - fastd_handshake_handle(ctx, sock, local_addr, remote_addr, peer, buffer); + fastd_handshake_handle(sock, local_addr, remote_addr, peer, buffer); } } @@ -135,24 +135,24 @@ static inline bool allow_unknown_peers(void) { return conf.has_floating || fastd_shell_command_isset(&conf.on_verify); } -static inline void handle_socket_receive_unknown(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_buffer_t buffer) { +static inline void handle_socket_receive_unknown(fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_buffer_t buffer) { const uint8_t *packet_type = buffer.data; - fastd_buffer_push_head(ctx, &buffer, 1); + fastd_buffer_push_head(&buffer, 1); switch (*packet_type) { case PACKET_DATA: fastd_buffer_free(buffer); - if (!backoff_unknown(ctx, remote_addr)) - conf.protocol->handshake_init(ctx, sock, local_addr, remote_addr, NULL); + if (!backoff_unknown(remote_addr)) + conf.protocol->handshake_init(sock, local_addr, remote_addr, NULL); break; case PACKET_HANDSHAKE: - fastd_handshake_handle(ctx, sock, local_addr, remote_addr, NULL, buffer); + fastd_handshake_handle(sock, local_addr, remote_addr, NULL, buffer); } } -static inline void handle_socket_receive(fastd_context_t *ctx, fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_buffer_t buffer) { +static inline void handle_socket_receive(fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_buffer_t buffer) { fastd_peer_t *peer = NULL; if (sock->peer) { @@ -164,24 +164,24 @@ static inline void handle_socket_receive(fastd_context_t *ctx, fastd_socket_t *s peer = sock->peer; } else { - peer = fastd_peer_hashtable_lookup(ctx, remote_addr); + peer = fastd_peer_hashtable_lookup(remote_addr); } if (peer) { - handle_socket_receive_known(ctx, sock, local_addr, remote_addr, peer, buffer); + handle_socket_receive_known(sock, local_addr, remote_addr, peer, buffer); } else if (allow_unknown_peers()) { - handle_socket_receive_unknown(ctx, sock, local_addr, remote_addr, buffer); + handle_socket_receive_unknown(sock, local_addr, remote_addr, buffer); } else { - pr_debug(ctx, "received packet from unknown peer %I", remote_addr); + pr_debug("received packet from unknown peer %I", remote_addr); fastd_buffer_free(buffer); } } -void fastd_receive(fastd_context_t *ctx, fastd_socket_t *sock) { - size_t max_len = fastd_max_outer_packet(ctx); - fastd_buffer_t buffer = fastd_buffer_alloc(ctx, max_len, conf.min_decrypt_head_space, conf.min_decrypt_tail_space); +void fastd_receive(fastd_socket_t *sock) { + size_t max_len = fastd_max_outer_packet(); + fastd_buffer_t buffer = fastd_buffer_alloc(max_len, conf.min_decrypt_head_space, conf.min_decrypt_tail_space); fastd_peer_address_t local_addr; fastd_peer_address_t recvaddr; struct iovec buffer_vec = { .iov_base = buffer.data, .iov_len = buffer.len }; @@ -199,7 +199,7 @@ void fastd_receive(fastd_context_t *ctx, fastd_socket_t *sock) { ssize_t len = recvmsg(sock->fd, &message, 0); if (len <= 0) { if (len < 0 && errno != EINTR) - pr_warn_errno(ctx, "recvmsg"); + pr_warn_errno("recvmsg"); fastd_buffer_free(buffer); return; @@ -211,7 +211,7 @@ void fastd_receive(fastd_context_t *ctx, fastd_socket_t *sock) { #ifdef USE_PKTINFO if (!local_addr.sa.sa_family) { - pr_error(ctx, "received packet without packet info"); + pr_error("received packet without packet info"); fastd_buffer_free(buffer); return; } @@ -219,5 +219,5 @@ void fastd_receive(fastd_context_t *ctx, fastd_socket_t *sock) { fastd_peer_address_simplify(&recvaddr); - handle_socket_receive(ctx, sock, &local_addr, &recvaddr, buffer); + handle_socket_receive(sock, &local_addr, &recvaddr, buffer); } diff --git a/src/resolve.c b/src/resolve.c index c67fd53..e791687 100644 --- a/src/resolve.c +++ b/src/resolve.c @@ -33,7 +33,6 @@ typedef struct resolv_arg { - fastd_context_t *ctx; fastd_remote_t *remote; char *hostname; fastd_peer_address_t constraints; @@ -63,7 +62,7 @@ static void* resolve_peer(void *varg) { gai_ret = getaddrinfo(arg->hostname, portstr, &hints, &res); if (gai_ret || !res) { - pr_verbose(arg->ctx, "resolving host `%s' failed: %s", arg->hostname, gai_strerror(gai_ret)); + pr_verbose("resolving host `%s' failed: %s", arg->hostname, gai_strerror(gai_ret)); } else { for (res2 = res; res2; res2 = res2->ai_next) @@ -78,7 +77,7 @@ static void* resolve_peer(void *varg) { n_addr = 0; for (res2 = res; res2; res2 = res2->ai_next) { if (res2->ai_addrlen > sizeof(fastd_peer_address_t) || (res2->ai_addr->sa_family != AF_INET && res2->ai_addr->sa_family != AF_INET6)) { - pr_warn(arg->ctx, "resolving host `%s': unsupported address returned", arg->hostname); + pr_warn("resolving host `%s': unsupported address returned", arg->hostname); continue; } @@ -90,12 +89,12 @@ static void* resolve_peer(void *varg) { } if (n_addr) - pr_verbose(arg->ctx, "resolved host `%s' successfully", arg->hostname); + pr_verbose("resolved host `%s' successfully", arg->hostname); } ret->n_addr = n_addr; - fastd_async_enqueue(arg->ctx, ASYNC_TYPE_RESOLVE_RETURN, ret, sizeof(fastd_async_resolve_return_t) + n_addr*sizeof(fastd_peer_address_t)); + fastd_async_enqueue(ASYNC_TYPE_RESOLVE_RETURN, ret, sizeof(fastd_async_resolve_return_t) + n_addr*sizeof(fastd_peer_address_t)); freeaddrinfo(res); free(arg->hostname); @@ -104,36 +103,35 @@ static void* resolve_peer(void *varg) { return NULL; } -void fastd_resolve_peer(fastd_context_t *ctx, fastd_peer_t *peer, fastd_remote_t *remote) { +void fastd_resolve_peer(fastd_peer_t *peer, fastd_remote_t *remote) { if (!peer->config) - exit_bug(ctx, "trying to resolve temporary peer"); + exit_bug("trying to resolve temporary peer"); if (remote->resolving) { - pr_debug(ctx, "not resolving %P as there is already a resolve running", peer); + pr_debug("not resolving %P as there is already a resolve running", peer); return; } - if (!fastd_timed_out(ctx, &remote->last_resolve_timeout)) { + if (!fastd_timed_out(&remote->last_resolve_timeout)) { /* last resolve was just a few seconds ago */ return; } - pr_verbose(ctx, "resolving host `%s' for peer %P...", remote->config->hostname, peer); + pr_verbose("resolving host `%s' for peer %P...", remote->config->hostname, peer); fastd_remote_ref(remote); - remote->last_resolve_timeout = fastd_in_seconds(ctx, conf.min_resolve_interval); + remote->last_resolve_timeout = fastd_in_seconds(conf.min_resolve_interval); remote->resolving = true; resolv_arg_t *arg = malloc(sizeof(resolv_arg_t)); - arg->ctx = ctx; arg->remote = remote; arg->hostname = strdup(remote->config->hostname); arg->constraints = remote->config->address; pthread_t thread; if (pthread_create(&thread, NULL, resolve_peer, arg) != 0) { - pr_error_errno(ctx, "unable to create resolver thread"); + pr_error_errno("unable to create resolver thread"); free(arg->hostname); free(arg); diff --git a/src/send.c b/src/send.c index 025eff9..3e346ec 100644 --- a/src/send.c +++ b/src/send.c @@ -73,9 +73,9 @@ static inline void count_stat(fastd_stats_t *stats, size_t stat_size) { } } -static void send_type(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, uint8_t packet_type, fastd_buffer_t buffer, size_t stat_size) { +static void send_type(const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, uint8_t packet_type, fastd_buffer_t buffer, size_t stat_size) { if (!sock) - exit_bug(ctx, "send: sock == NULL"); + exit_bug("send: sock == NULL"); struct msghdr msg = {}; uint8_t cbuf[1024] __attribute__((aligned(8))) = {}; @@ -93,7 +93,7 @@ static void send_type(fastd_context_t *ctx, const fastd_socket_t *sock, const fa break; default: - exit_bug(ctx, "unsupported address family"); + exit_bug("unsupported address family"); } if (sock->bound_addr->sa.sa_family == AF_INET6) { @@ -125,10 +125,10 @@ static void send_type(fastd_context_t *ctx, const fastd_socket_t *sock, const fa } while (ret < 0 && errno == EINTR); if (ret < 0 && errno == EINVAL && msg.msg_controllen) { - pr_debug2(ctx, "sendmsg failed, trying again without pktinfo"); + pr_debug2("sendmsg failed, trying again without pktinfo"); - if (peer && !fastd_peer_handshake_scheduled(ctx, peer)) - fastd_peer_schedule_handshake_default(ctx, peer); + if (peer && !fastd_peer_handshake_scheduled(peer)) + fastd_peer_schedule_handshake_default(peer); msg.msg_control = NULL; msg.msg_controllen = 0; @@ -145,51 +145,51 @@ static void send_type(fastd_context_t *ctx, const fastd_socket_t *sock, const fa #if EAGAIN != EWOULDBLOCK case EWOULDBLOCK: #endif - pr_debug2_errno(ctx, "sendmsg"); - count_stat(&ctx->tx_dropped, stat_size); + pr_debug2_errno("sendmsg"); + count_stat(&ctx.tx_dropped, stat_size); break; case ENETDOWN: case ENETUNREACH: case EHOSTUNREACH: - pr_debug_errno(ctx, "sendmsg"); - count_stat(&ctx->tx_error, stat_size); + pr_debug_errno("sendmsg"); + count_stat(&ctx.tx_error, stat_size); break; default: - pr_warn_errno(ctx, "sendmsg"); - count_stat(&ctx->tx_error, stat_size); + pr_warn_errno("sendmsg"); + count_stat(&ctx.tx_error, stat_size); } } else { - count_stat(&ctx->tx, stat_size); + count_stat(&ctx.tx, stat_size); } fastd_buffer_free(buffer); } -void fastd_send(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer, size_t stat_size) { - send_type(ctx, sock, local_addr, remote_addr, peer, PACKET_DATA, buffer, stat_size); +void fastd_send(const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer, size_t stat_size) { + send_type(sock, local_addr, remote_addr, peer, PACKET_DATA, buffer, stat_size); } -void fastd_send_handshake(fastd_context_t *ctx, const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer) { - send_type(ctx, sock, local_addr, remote_addr, peer, PACKET_HANDSHAKE, buffer, 0); +void fastd_send_handshake(const fastd_socket_t *sock, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *remote_addr, fastd_peer_t *peer, fastd_buffer_t buffer) { + send_type(sock, local_addr, remote_addr, peer, PACKET_HANDSHAKE, buffer, 0); } -void fastd_send_all(fastd_context_t *ctx, fastd_peer_t *source_peer, fastd_buffer_t buffer) { +void fastd_send_all(fastd_peer_t *source_peer, fastd_buffer_t buffer) { size_t i; - for (i = 0; i < VECTOR_LEN(ctx->peers); i++) { - fastd_peer_t *dest_peer = VECTOR_INDEX(ctx->peers, i); + for (i = 0; i < VECTOR_LEN(ctx.peers); i++) { + fastd_peer_t *dest_peer = VECTOR_INDEX(ctx.peers, i); if (dest_peer == source_peer || !fastd_peer_is_established(dest_peer)) continue; /* optimization, primarily for TUN mode: don't duplicate the buffer for the last (or only) peer */ - if (i == VECTOR_LEN(ctx->peers)-1) { - conf.protocol->send(ctx, dest_peer, buffer); + if (i == VECTOR_LEN(ctx.peers)-1) { + conf.protocol->send(dest_peer, buffer); return; } - conf.protocol->send(ctx, dest_peer, fastd_buffer_dup(ctx, buffer, conf.min_encrypt_head_space, conf.min_encrypt_tail_space)); + conf.protocol->send(dest_peer, fastd_buffer_dup(buffer, conf.min_encrypt_head_space, conf.min_encrypt_tail_space)); } fastd_buffer_free(buffer); diff --git a/src/shell.c b/src/shell.c index 786dd2a..e35e986 100644 --- a/src/shell.c +++ b/src/shell.c @@ -32,15 +32,15 @@ #include -static void shell_command_setenv(fastd_context_t *ctx, pid_t pid, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr) { +static void shell_command_setenv(pid_t pid, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr) { /* both INET6_ADDRSTRLEN and IFNAMESIZE already include space for the zero termination, so there is no need to add space for the '%' here. */ char buf[INET6_ADDRSTRLEN+IF_NAMESIZE]; snprintf(buf, sizeof(buf), "%u", (unsigned)pid); setenv("FASTD_PID", buf, 1); - if (ctx->ifname) { - setenv("INTERFACE", ctx->ifname, 1); + if (ctx.ifname) { + setenv("INTERFACE", ctx.ifname, 1); } else if (conf.ifname) { char ifname[IF_NAMESIZE]; @@ -125,12 +125,12 @@ static void shell_command_setenv(fastd_context_t *ctx, pid_t pid, const fastd_pe conf.protocol->set_shell_env(peer); } -static bool shell_command_do_exec(fastd_context_t *ctx, const fastd_shell_command_t *command, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr, pid_t *pid_ret) { +static bool shell_command_do_exec(const fastd_shell_command_t *command, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr, pid_t *pid_ret) { pid_t parent = getpid(); pid_t pid = fork(); if (pid < 0) { - pr_error_errno(ctx, "shell_command_do_exec: fork"); + pr_error_errno("shell_command_do_exec: fork"); return false; } else if (pid > 0) { @@ -145,7 +145,7 @@ static bool shell_command_do_exec(fastd_context_t *ctx, const fastd_shell_comman if (chdir(command->dir)) _exit(126); - shell_command_setenv(ctx, parent, peer, local_addr, peer_addr); + shell_command_setenv(parent, peer, local_addr, peer_addr); /* unblock SIGCHLD */ sigset_t set; @@ -157,7 +157,7 @@ static bool shell_command_do_exec(fastd_context_t *ctx, const fastd_shell_comman _exit(127); } -bool fastd_shell_command_exec_sync(fastd_context_t *ctx, const fastd_shell_command_t *command, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr, int *ret) { +bool fastd_shell_command_exec_sync(const fastd_shell_command_t *command, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr, int *ret) { if (!fastd_shell_command_isset(command)) return true; @@ -168,7 +168,7 @@ bool fastd_shell_command_exec_sync(fastd_context_t *ctx, const fastd_shell_comma pthread_sigmask(SIG_BLOCK, &set, &oldset); pid_t pid; - if (!shell_command_do_exec(ctx, command, peer, local_addr, peer_addr, &pid)) + if (!shell_command_do_exec(command, peer, local_addr, peer_addr, &pid)) return false; int status; @@ -177,7 +177,7 @@ bool fastd_shell_command_exec_sync(fastd_context_t *ctx, const fastd_shell_comma pthread_sigmask(SIG_SETMASK, &oldset, NULL); if (err <= 0) { - pr_error_errno(ctx, "fastd_shell_command_exec_sync: waitpid"); + pr_error_errno("fastd_shell_command_exec_sync: waitpid"); return false; } @@ -186,21 +186,21 @@ bool fastd_shell_command_exec_sync(fastd_context_t *ctx, const fastd_shell_comma } else { if (WIFSIGNALED(status)) - pr_warn(ctx, "command exited with signal %i", WTERMSIG(status)); + pr_warn("command exited with signal %i", WTERMSIG(status)); else if (WEXITSTATUS(status)) - pr_warn(ctx, "command exited with status %i", WEXITSTATUS(status)); + pr_warn("command exited with status %i", WEXITSTATUS(status)); } return true; } -void fastd_shell_command_exec(fastd_context_t *ctx, const fastd_shell_command_t *command, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr) { +void fastd_shell_command_exec(const fastd_shell_command_t *command, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr) { if (!fastd_shell_command_isset(command)) return; if (command->sync) - fastd_shell_command_exec_sync(ctx, command, peer, local_addr, peer_addr, NULL); + fastd_shell_command_exec_sync(command, peer, local_addr, peer_addr, NULL); else - shell_command_do_exec(ctx, command, peer, local_addr, peer_addr, NULL); + shell_command_do_exec(command, peer, local_addr, peer_addr, NULL); } diff --git a/src/shell.h b/src/shell.h index 21a451b..30de630 100644 --- a/src/shell.h +++ b/src/shell.h @@ -58,5 +58,5 @@ static inline bool fastd_shell_command_isset(const fastd_shell_command_t *comman return command->command; } -bool fastd_shell_command_exec_sync(fastd_context_t *ctx, const fastd_shell_command_t *command, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr, int *ret); -void fastd_shell_command_exec(fastd_context_t *ctx, const fastd_shell_command_t *command, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr); +bool fastd_shell_command_exec_sync(const fastd_shell_command_t *command, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr, int *ret); +void fastd_shell_command_exec(const fastd_shell_command_t *command, const fastd_peer_t *peer, const fastd_peer_address_t *local_addr, const fastd_peer_address_t *peer_addr); diff --git a/src/socket.c b/src/socket.c index 70ff17a..77f73b4 100644 --- a/src/socket.c +++ b/src/socket.c @@ -30,7 +30,7 @@ #include -static int bind_socket(fastd_context_t *ctx, const fastd_bind_address_t *addr, bool warn) { +static int bind_socket(const fastd_bind_address_t *addr, bool warn) { int fd = -1; int af = AF_UNSPEC; @@ -42,7 +42,7 @@ static int bind_socket(fastd_context_t *ctx, const fastd_bind_address_t *addr, b int val = (addr->addr.sa.sa_family == AF_INET6); if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val))) { if (warn) - pr_warn_errno(ctx, "setsockopt"); + pr_warn_errno("setsockopt"); goto error; } } @@ -50,7 +50,7 @@ static int bind_socket(fastd_context_t *ctx, const fastd_bind_address_t *addr, b if (fd < 0 && addr->addr.sa.sa_family != AF_INET6) { fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); if (fd < 0) - exit_errno(ctx, "unable to create socket"); + exit_errno("unable to create socket"); else af = AF_INET; } @@ -58,26 +58,26 @@ static int bind_socket(fastd_context_t *ctx, const fastd_bind_address_t *addr, b if (fd < 0) goto error; - fastd_setfd(ctx, fd, FD_CLOEXEC, 0); - fastd_setfl(ctx, fd, O_NONBLOCK, 0); + fastd_setfd(fd, FD_CLOEXEC, 0); + fastd_setfl(fd, O_NONBLOCK, 0); int one = 1; #ifdef USE_PKTINFO if (setsockopt(fd, IPPROTO_IP, IP_PKTINFO, &one, sizeof(one))) { - pr_error_errno(ctx, "setsockopt: unable to set IP_PKTINFO"); + pr_error_errno("setsockopt: unable to set IP_PKTINFO"); goto error; } #endif #ifdef USE_FREEBIND if (setsockopt(fd, IPPROTO_IP, IP_FREEBIND, &one, sizeof(one))) - pr_warn_errno(ctx, "setsockopt: unable to set IP_FREEBIND"); + pr_warn_errno("setsockopt: unable to set IP_FREEBIND"); #endif if (af == AF_INET6) { if (setsockopt(fd, IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one))) { - pr_error_errno(ctx, "setsockopt: unable to set IPV6_RECVPKTINFO"); + pr_error_errno("setsockopt: unable to set IPV6_RECVPKTINFO"); goto error; } } @@ -86,7 +86,7 @@ static int bind_socket(fastd_context_t *ctx, const fastd_bind_address_t *addr, b if (addr->bindtodev && !fastd_peer_address_is_v6_ll(&addr->addr)) { if (setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, addr->bindtodev, strlen(addr->bindtodev))) { if (warn) - pr_warn_errno(ctx, "setsockopt: unable to bind to device"); + pr_warn_errno("setsockopt: unable to bind to device"); goto error; } } @@ -96,7 +96,7 @@ static int bind_socket(fastd_context_t *ctx, const fastd_bind_address_t *addr, b if (conf.pmtu.set) { int pmtu = conf.pmtu.state ? IP_PMTUDISC_DO : IP_PMTUDISC_DONT; if (setsockopt(fd, IPPROTO_IP, IP_MTU_DISCOVER, &pmtu, sizeof(pmtu))) { - pr_error_errno(ctx, "setsockopt: unable to set PMTU discovery"); + pr_error_errno("setsockopt: unable to set PMTU discovery"); goto error; } } @@ -105,7 +105,7 @@ static int bind_socket(fastd_context_t *ctx, const fastd_bind_address_t *addr, b #ifdef USE_PACKET_MARK if (conf.packet_mark) { if (setsockopt(fd, SOL_SOCKET, SO_MARK, &conf.packet_mark, sizeof(conf.packet_mark))) { - pr_error_errno(ctx, "setsockopt: unable to set packet mark"); + pr_error_errno("setsockopt: unable to set packet mark"); goto error; } } @@ -122,7 +122,7 @@ static int bind_socket(fastd_context_t *ctx, const fastd_bind_address_t *addr, b if (!bind_address.in6.sin6_scope_id) { if (warn) - pr_warn_errno(ctx, "if_nametoindex"); + pr_warn_errno("if_nametoindex"); goto error; } } @@ -139,7 +139,7 @@ static int bind_socket(fastd_context_t *ctx, const fastd_bind_address_t *addr, b if (bind(fd, &bind_address.sa, bind_address.sa.sa_family == AF_INET6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in))) { if (warn) - pr_warn_errno(ctx, "bind"); + pr_warn_errno("bind"); goto error; } @@ -148,30 +148,30 @@ static int bind_socket(fastd_context_t *ctx, const fastd_bind_address_t *addr, b error: if (fd >= 0) { if (close(fd)) - pr_error_errno(ctx, "close"); + pr_error_errno("close"); } if (warn) { if (addr->bindtodev) - pr_warn(ctx, fastd_peer_address_is_v6_ll(&addr->addr) ? "unable to bind to %L" : "unable to bind to %B on `%s'", &addr->addr, addr->bindtodev); + pr_warn(fastd_peer_address_is_v6_ll(&addr->addr) ? "unable to bind to %L" : "unable to bind to %B on `%s'", &addr->addr, addr->bindtodev); else - pr_warn(ctx, "unable to bind to %B", &addr->addr); + pr_warn("unable to bind to %B", &addr->addr); } return -1; } -static bool set_bound_address(fastd_context_t *ctx, fastd_socket_t *sock) { +static bool set_bound_address(fastd_socket_t *sock) { fastd_peer_address_t addr = {}; socklen_t len = sizeof(addr); if (getsockname(sock->fd, &addr.sa, &len) < 0) { - pr_error_errno(ctx, "getsockname"); + pr_error_errno("getsockname"); return false; } if (len > sizeof(addr)) { - pr_error(ctx, "getsockname: got strange long address"); + pr_error("getsockname: got strange long address"); return false; } @@ -181,44 +181,44 @@ static bool set_bound_address(fastd_context_t *ctx, fastd_socket_t *sock) { return true; } -bool fastd_socket_handle_binds(fastd_context_t *ctx) { +bool fastd_socket_handle_binds(void) { unsigned i; - for (i = 0; i < ctx->n_socks; i++) { - if (ctx->socks[i].fd >= 0) + for (i = 0; i < ctx.n_socks; i++) { + if (ctx.socks[i].fd >= 0) continue; - ctx->socks[i].fd = bind_socket(ctx, ctx->socks[i].addr, ctx->socks[i].fd < -1); + ctx.socks[i].fd = bind_socket(ctx.socks[i].addr, ctx.socks[i].fd < -1); - if (ctx->socks[i].fd >= 0) { - if (!set_bound_address(ctx, &ctx->socks[i])) { - fastd_socket_close(ctx, &ctx->socks[i]); + if (ctx.socks[i].fd >= 0) { + if (!set_bound_address(&ctx.socks[i])) { + fastd_socket_close(&ctx.socks[i]); continue; } - fastd_poll_set_fd_sock(ctx, i); + fastd_poll_set_fd_sock(i); - fastd_peer_address_t bound_addr = *ctx->socks[i].bound_addr; - if (!ctx->socks[i].addr->addr.sa.sa_family) + fastd_peer_address_t bound_addr = *ctx.socks[i].bound_addr; + if (!ctx.socks[i].addr->addr.sa.sa_family) bound_addr.sa.sa_family = AF_UNSPEC; - if (ctx->socks[i].addr->bindtodev && !fastd_peer_address_is_v6_ll(&bound_addr)) - pr_info(ctx, "successfully bound to %B on `%s'", &bound_addr, ctx->socks[i].addr->bindtodev); + if (ctx.socks[i].addr->bindtodev && !fastd_peer_address_is_v6_ll(&bound_addr)) + pr_info("successfully bound to %B on `%s'", &bound_addr, ctx.socks[i].addr->bindtodev); else - pr_info(ctx, "successfully bound to %B", &bound_addr); + pr_info("successfully bound to %B", &bound_addr); } } - if ((ctx->sock_default_v4 && ctx->sock_default_v4->fd < 0) || (ctx->sock_default_v6 && ctx->sock_default_v6->fd < 0)) + if ((ctx.sock_default_v4 && ctx.sock_default_v4->fd < 0) || (ctx.sock_default_v6 && ctx.sock_default_v6->fd < 0)) return false; return true; } -fastd_socket_t* fastd_socket_open(fastd_context_t *ctx, fastd_peer_t *peer, int af) { +fastd_socket_t* fastd_socket_open(fastd_peer_t *peer, int af) { const fastd_bind_address_t any_address = { .addr.sa.sa_family = af }; - int fd = bind_socket(ctx, &any_address, true); + int fd = bind_socket(&any_address, true); if (fd < 0) return NULL; @@ -229,8 +229,8 @@ fastd_socket_t* fastd_socket_open(fastd_context_t *ctx, fastd_peer_t *peer, int sock->bound_addr = NULL; sock->peer = peer; - if (!set_bound_address(ctx, sock)) { - fastd_socket_close(ctx, sock); + if (!set_bound_address(sock)) { + fastd_socket_close(sock); free(sock); return NULL; } @@ -238,10 +238,10 @@ fastd_socket_t* fastd_socket_open(fastd_context_t *ctx, fastd_peer_t *peer, int return sock; } -void fastd_socket_close(fastd_context_t *ctx, fastd_socket_t *sock) { +void fastd_socket_close(fastd_socket_t *sock) { if (sock->fd >= 0) { if(close(sock->fd)) - pr_error_errno(ctx, "closing socket: close"); + pr_error_errno("closing socket: close"); sock->fd = -2; } @@ -252,11 +252,11 @@ void fastd_socket_close(fastd_context_t *ctx, fastd_socket_t *sock) { } } -void fastd_socket_error(fastd_context_t *ctx, fastd_socket_t *sock) { +void fastd_socket_error(fastd_socket_t *sock) { if (sock->addr->bindtodev) - pr_warn(ctx, "socket bind %I on `%s' lost", &sock->addr->addr, sock->addr->bindtodev); + pr_warn("socket bind %I on `%s' lost", &sock->addr->addr, sock->addr->bindtodev); else - pr_warn(ctx, "socket bind %I lost", &sock->addr->addr); + pr_warn("socket bind %I lost", &sock->addr->addr); - fastd_socket_close(ctx, sock); + fastd_socket_close(sock); } diff --git a/src/tuntap.c b/src/tuntap.c index 0fc17a3..e5c0471 100644 --- a/src/tuntap.c +++ b/src/tuntap.c @@ -56,13 +56,13 @@ static const bool multiaf_tun = true; #if defined(__linux__) -void fastd_tuntap_open(fastd_context_t *ctx) { +void fastd_tuntap_open(void) { struct ifreq ifr = {}; - pr_debug(ctx, "initializing tun/tap device..."); + pr_debug("initializing tun/tap device..."); - if ((ctx->tunfd = open("/dev/net/tun", O_RDWR|O_CLOEXEC|O_NONBLOCK)) < 0) - exit_errno(ctx, "could not open tun/tap device file"); + if ((ctx.tunfd = open("/dev/net/tun", O_RDWR|O_CLOEXEC|O_NONBLOCK)) < 0) + exit_errno("could not open tun/tap device file"); if (conf.ifname) strncpy(ifr.ifr_name, conf.ifname, IFNAMSIZ-1); @@ -77,87 +77,87 @@ void fastd_tuntap_open(fastd_context_t *ctx) { break; default: - exit_bug(ctx, "invalid mode"); + exit_bug("invalid mode"); } ifr.ifr_flags |= IFF_NO_PI; - if (ioctl(ctx->tunfd, TUNSETIFF, &ifr) < 0) - exit_errno(ctx, "TUNSETIFF ioctl failed"); + if (ioctl(ctx.tunfd, TUNSETIFF, &ifr) < 0) + exit_errno("TUNSETIFF ioctl failed"); - ctx->ifname = strndup(ifr.ifr_name, IFNAMSIZ-1); + ctx.ifname = strndup(ifr.ifr_name, IFNAMSIZ-1); int ctl_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); if (ctl_sock < 0) - exit_errno(ctx, "socket"); + exit_errno("socket"); if (ioctl(ctl_sock, SIOCGIFMTU, &ifr) < 0) - exit_errno(ctx, "SIOCGIFMTU ioctl failed"); + exit_errno("SIOCGIFMTU ioctl failed"); if (ifr.ifr_mtu != conf.mtu) { ifr.ifr_mtu = conf.mtu; if (ioctl(ctl_sock, SIOCSIFMTU, &ifr) < 0) - exit_errno(ctx, "SIOCSIFMTU ioctl failed"); + exit_errno("SIOCSIFMTU ioctl failed"); } if (close(ctl_sock)) - pr_error_errno(ctx, "close"); + pr_error_errno("close"); - fastd_poll_set_fd_tuntap(ctx); + fastd_poll_set_fd_tuntap(); - pr_debug(ctx, "tun/tap device initialized."); + pr_debug("tun/tap device initialized."); } #elif defined(__FreeBSD__) || defined(__OpenBSD__) -static void set_tun_mtu(fastd_context_t *ctx) { +static void set_tun_mtu(void) { struct tuninfo tuninfo; - if (ioctl(ctx->tunfd, TUNGIFINFO, &tuninfo) < 0) - exit_errno(ctx, "TUNGIFINFO ioctl failed"); + if (ioctl(ctx.tunfd, TUNGIFINFO, &tuninfo) < 0) + exit_errno("TUNGIFINFO ioctl failed"); tuninfo.mtu = conf.mtu; - if (ioctl(ctx->tunfd, TUNSIFINFO, &tuninfo) < 0) - exit_errno(ctx, "TUNSIFINFO ioctl failed"); + if (ioctl(ctx.tunfd, TUNSIFINFO, &tuninfo) < 0) + exit_errno("TUNSIFINFO ioctl failed"); } #ifdef __FreeBSD__ -static void set_tap_mtu(fastd_context_t *ctx) { +static void set_tap_mtu(void) { struct tapinfo tapinfo; - if (ioctl(ctx->tunfd, TAPGIFINFO, &tapinfo) < 0) - exit_errno(ctx, "TAPGIFINFO ioctl failed"); + if (ioctl(ctx.tunfd, TAPGIFINFO, &tapinfo) < 0) + exit_errno("TAPGIFINFO ioctl failed"); tapinfo.mtu = conf.mtu; - if (ioctl(ctx->tunfd, TAPSIFINFO, &tapinfo) < 0) - exit_errno(ctx, "TAPSIFINFO ioctl failed"); + if (ioctl(ctx.tunfd, TAPSIFINFO, &tapinfo) < 0) + exit_errno("TAPSIFINFO ioctl failed"); } -static void setup_tun(fastd_context_t *ctx) { +static void setup_tun(void) { int one = 1; - if (ioctl(ctx->tunfd, TUNSIFHEAD, &one) < 0) - exit_errno(ctx, "TUNSIFHEAD ioctl failed"); + if (ioctl(ctx.tunfd, TUNSIFHEAD, &one) < 0) + exit_errno("TUNSIFHEAD ioctl failed"); - set_tun_mtu(ctx); + set_tun_mtu(); } -static void setup_tap(fastd_context_t *ctx) { +static void setup_tap(void) { struct ifreq ifr = {}; - if (ioctl(ctx->tunfd, TAPGIFNAME, &ifr) < 0) - exit_errno(ctx, "TAPGIFNAME ioctl failed"); + if (ioctl(ctx.tunfd, TAPGIFNAME, &ifr) < 0) + exit_errno("TAPGIFNAME ioctl failed"); - free(ctx->ifname); - ctx->ifname = strndup(ifr.ifr_name, IFNAMSIZ-1); + free(ctx.ifname); + ctx.ifname = strndup(ifr.ifr_name, IFNAMSIZ-1); - set_tap_mtu(ctx); + set_tap_mtu(); } -void fastd_tuntap_open(fastd_context_t *ctx) { - pr_debug(ctx, "initializing tun/tap device..."); +void fastd_tuntap_open(void) { + pr_debug("initializing tun/tap device..."); char ifname[5+IFNAMSIZ] = "/dev/"; const char *type; @@ -172,12 +172,12 @@ void fastd_tuntap_open(fastd_context_t *ctx) { break; default: - exit_bug(ctx, "invalid mode"); + exit_bug("invalid mode"); } if (conf.ifname) { if (strncmp(conf.ifname, type, 3) != 0) - exit_error(ctx, "`%s' doesn't seem to be a %s device", conf.ifname, type); + exit_error("`%s' doesn't seem to be a %s device", conf.ifname, type); strncat(ifname, conf.ifname, IFNAMSIZ-1); } @@ -185,42 +185,42 @@ void fastd_tuntap_open(fastd_context_t *ctx) { strncat(ifname, type, IFNAMSIZ-1); } - if ((ctx->tunfd = open(ifname, O_RDWR|O_CLOEXEC|O_NONBLOCK)) < 0) - exit_errno(ctx, "could not open tun/tap device file"); + if ((ctx.tunfd = open(ifname, O_RDWR|O_CLOEXEC|O_NONBLOCK)) < 0) + exit_errno("could not open tun/tap device file"); - if (!(ctx->ifname = fdevname_r(ctx->tunfd, malloc(IFNAMSIZ), IFNAMSIZ))) - exit_errno(ctx, "could not get tun/tap interface name"); + if (!(ctx.ifname = fdevname_r(ctx.tunfd, malloc(IFNAMSIZ), IFNAMSIZ))) + exit_errno("could not get tun/tap interface name"); switch (conf.mode) { case MODE_TAP: - setup_tap(ctx); + setup_tap(); break; case MODE_TUN: - setup_tun(ctx); + setup_tun(); break; default: - exit_bug(ctx, "invalid mode"); + exit_bug("invalid mode"); } - fastd_poll_set_fd_tuntap(ctx); + fastd_poll_set_fd_tuntap(); - pr_debug(ctx, "tun/tap device initialized."); + pr_debug("tun/tap device initialized."); } #else /* __OpenBSD__ */ -static void set_link0(fastd_context_t *ctx, bool set) { +static void set_link0(bool set) { struct ifreq ifr = {}; int ctl_sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); if (ctl_sock < 0) - exit_errno(ctx, "socket"); + exit_errno("socket"); - strncpy(ifr.ifr_name, ctx->ifname, IFNAMSIZ-1); + strncpy(ifr.ifr_name, ctx.ifname, IFNAMSIZ-1); if (ioctl(ctl_sock, SIOCGIFFLAGS, &ifr) < 0) - exit_errno(ctx, "SIOCGIFFLAGS ioctl failed"); + exit_errno("SIOCGIFFLAGS ioctl failed"); if (set) ifr.ifr_flags |= IFF_LINK0; @@ -228,54 +228,54 @@ static void set_link0(fastd_context_t *ctx, bool set) { ifr.ifr_flags &= ~IFF_LINK0; if (ioctl(ctl_sock, SIOCSIFFLAGS, &ifr) < 0) - exit_errno(ctx, "SIOCSIFFLAGS ioctl failed"); + exit_errno("SIOCSIFFLAGS ioctl failed"); if (close(ctl_sock)) - pr_error_errno(ctx, "close"); + pr_error_errno("close"); } -static void setup_tun(fastd_context_t *ctx) { - set_link0(ctx, false); - set_tun_mtu(ctx); +static void setup_tun(void) { + set_link0(false); + set_tun_mtu(); } -static void setup_tap(fastd_context_t *ctx) { - set_link0(ctx, true); - set_tun_mtu(ctx); +static void setup_tap(void) { + set_link0(true); + set_tun_mtu(); } -void fastd_tuntap_open(fastd_context_t *ctx) { +void fastd_tuntap_open(void) { char ifname[5+IFNAMSIZ] = "/dev/"; if (!conf.ifname) - exit_error(ctx, "config error: no interface name given."); + exit_error("config error: no interface name given."); else if (strncmp(conf.ifname, "tun", 3) != 0) - exit_error(ctx, "config error: `%s' doesn't seem to be a tun device", conf.ifname); + exit_error("config error: `%s' doesn't seem to be a tun device", conf.ifname); else strncat(ifname, conf.ifname, IFNAMSIZ-1); - pr_debug(ctx, "initializing tun device..."); + pr_debug("initializing tun device..."); - if ((ctx->tunfd = open(ifname, O_RDWR|O_CLOEXEC|O_NONBLOCK)) < 0) - exit_errno(ctx, "could not open tun device file"); + if ((ctx.tunfd = open(ifname, O_RDWR|O_CLOEXEC|O_NONBLOCK)) < 0) + exit_errno("could not open tun device file"); - ctx->ifname = strndup(conf.ifname, IFNAMSIZ-1); + ctx.ifname = strndup(conf.ifname, IFNAMSIZ-1); switch (conf.mode) { case MODE_TAP: - setup_tap(ctx); + setup_tap(); break; case MODE_TUN: - setup_tun(ctx); + setup_tun(); break; default: - exit_bug(ctx, "invalid mode"); + exit_bug("invalid mode"); } - fastd_poll_set_fd_tuntap(ctx); + fastd_poll_set_fd_tuntap(); - pr_debug(ctx, "tun device initialized."); + pr_debug("tun device initialized."); } #endif @@ -287,34 +287,34 @@ void fastd_tuntap_open(fastd_context_t *ctx) { #endif -fastd_buffer_t fastd_tuntap_read(fastd_context_t *ctx) { - size_t max_len = fastd_max_inner_packet(ctx); +fastd_buffer_t fastd_tuntap_read(void) { + size_t max_len = fastd_max_inner_packet(); fastd_buffer_t buffer; if (multiaf_tun && conf.mode == MODE_TUN) - buffer = fastd_buffer_alloc(ctx, max_len+4, conf.min_encrypt_head_space+12, conf.min_encrypt_tail_space); + buffer = fastd_buffer_alloc(max_len+4, conf.min_encrypt_head_space+12, conf.min_encrypt_tail_space); else - buffer = fastd_buffer_alloc(ctx, max_len, conf.min_encrypt_head_space, conf.min_encrypt_tail_space); + buffer = fastd_buffer_alloc(max_len, conf.min_encrypt_head_space, conf.min_encrypt_tail_space); - ssize_t len = read(ctx->tunfd, buffer.data, max_len); + ssize_t len = read(ctx.tunfd, buffer.data, max_len); if (len < 0) { if (errno == EINTR) { fastd_buffer_free(buffer); return (fastd_buffer_t){}; } - exit_errno(ctx, "read"); + exit_errno("read"); } buffer.len = len; if (multiaf_tun && conf.mode == MODE_TUN) - fastd_buffer_push_head(ctx, &buffer, 4); + fastd_buffer_push_head(&buffer, 4); return buffer; } -void fastd_tuntap_write(fastd_context_t *ctx, fastd_buffer_t buffer) { +void fastd_tuntap_write(fastd_buffer_t buffer) { if (multiaf_tun && conf.mode == MODE_TUN) { uint8_t version = *((uint8_t*)buffer.data) >> 4; uint32_t af; @@ -329,19 +329,19 @@ void fastd_tuntap_write(fastd_context_t *ctx, fastd_buffer_t buffer) { break; default: - pr_warn(ctx, "fastd_tuntap_write: unknown IP version %u", version); + pr_warn("fastd_tuntap_write: unknown IP version %u", version); return; } - fastd_buffer_pull_head(ctx, &buffer, 4); + fastd_buffer_pull_head(&buffer, 4); memcpy(buffer.data, &af, 4); } - if (write(ctx->tunfd, buffer.data, buffer.len) < 0) - pr_warn_errno(ctx, "write"); + if (write(ctx.tunfd, buffer.data, buffer.len) < 0) + pr_warn_errno("write"); } -void fastd_tuntap_close(fastd_context_t *ctx) { - if (close(ctx->tunfd)) - pr_warn_errno(ctx, "closing tun/tap: close"); +void fastd_tuntap_close(void) { + if (close(ctx.tunfd)) + pr_warn_errno("closing tun/tap: close"); } -- cgit v1.2.3