diff options
Diffstat (limited to 'src/irmd/main.c')
| -rw-r--r-- | src/irmd/main.c | 4171 |
1 files changed, 2479 insertions, 1692 deletions
diff --git a/src/irmd/main.c b/src/irmd/main.c index 1749b651..19be4ab9 100644 --- a/src/irmd/main.c +++ b/src/irmd/main.c @@ -1,5 +1,5 @@ /* - * Ouroboros - Copyright (C) 2016 - 2022 + * Ouroboros - Copyright (C) 2016 - 2026 * * The IPC Resource Manager * @@ -22,46 +22,59 @@ #if defined(__linux__) || defined(__CYGWIN__) #define _DEFAULT_SOURCE +#define _GNU_SOURCE #else -#define _POSIX_C_SOURCE 200809L +#define _DEFAULT_SOURCE +#define _BSD_SOURCE #endif #include "config.h" #define OUROBOROS_PREFIX "irmd" -#include <ouroboros/hash.h> +#include <ouroboros/bitmap.h> +#include <ouroboros/crypt.h> #include <ouroboros/errno.h> -#include <ouroboros/sockets.h> -#include <ouroboros/list.h> -#include <ouroboros/utils.h> +#include <ouroboros/flow.h> +#include <ouroboros/fqueue.h> +#include <ouroboros/hash.h> #include <ouroboros/irm.h> +#include <ouroboros/list.h> #include <ouroboros/lockfile.h> -#include <ouroboros/shm_rbuff.h> -#include <ouroboros/shm_rdrbuff.h> -#include <ouroboros/bitmap.h> -#include <ouroboros/qos.h> -#include <ouroboros/time_utils.h> -#include <ouroboros/tpm.h> #include <ouroboros/logs.h> -#include <ouroboros/version.h> +#include <ouroboros/protobuf.h> #include <ouroboros/pthread.h> +#include <ouroboros/random.h> +#include <ouroboros/rib.h> +#include <ouroboros/ssm_pool.h> +#include <ouroboros/sockets.h> +#include <ouroboros/time.h> +#include <ouroboros/tpm.h> +#include <ouroboros/utils.h> +#include <ouroboros/version.h> -#include "utils.h" -#include "registry.h" -#include "irm_flow.h" -#include "proc_table.h" +#include "irmd.h" #include "ipcp.h" +#include "oap.h" +#include "reg/reg.h" +#include "configfile.h" -#include <sys/socket.h> -#include <sys/un.h> +#include <dirent.h> +#include <grp.h> +#include <pwd.h> #include <signal.h> +#include <spawn.h> #include <stdlib.h> #include <string.h> #include <limits.h> +#include <sys/socket.h> #include <sys/stat.h> #include <sys/wait.h> -#include <spawn.h> +#include <sys/un.h> +#ifdef __APPLE__ +#include <sys/types.h> +#include <unistd.h> +#endif #ifdef HAVE_LIBGCRYPT #include <gcrypt.h> @@ -69,33 +82,22 @@ #define IRMD_CLEANUP_TIMER ((IRMD_FLOW_TIMEOUT / 20) * MILLION) /* ns */ #define SHM_SAN_HOLDOFF 1000 /* ms */ -#define IPCP_HASH_LEN(e) hash_len(e->dir_hash_algo) +#define IPCP_HASH_LEN(p) hash_len((p)->dir_hash_algo) #define BIND_TIMEOUT 10 /* ms */ +#define TIMESYNC_SLACK 100 /* ms */ +#define OAP_SEEN_TIMER 20 /* s */ #define DEALLOC_TIME 300 /* s */ - -enum init_state { - IPCP_NULL = 0, - IPCP_BOOT, - IPCP_LIVE -}; - -struct ipcp_entry { - struct list_head next; - - char * name; - pid_t pid; - enum ipcp_type type; - enum hash_algo dir_hash_algo; - char * layer; - - enum init_state state; - pthread_cond_t cond; - pthread_mutex_t lock; -}; +#define REKEY_BATCH 64 /* flows re-keyed per timer pass */ +#define REKEY_RESP_TIMEO 20 /* s; give-up on a re-key RESPONSE */ +#define DIRECT_MPL 20 /* ms */ +/* bytes; in-process, bounded only by PUP/GSPP. */ +#define DIRECT_MTU 65000 enum irm_state { IRMD_NULL = 0, - IRMD_RUNNING + IRMD_INIT, + IRMD_RUNNING, + IRMD_SHUTDOWN }; struct cmd { @@ -106,24 +108,38 @@ struct cmd { int fd; }; -struct { - struct list_head registry; /* registered names known */ - size_t n_names; /* number of names */ +/* In-flight Tier-2 re-key, owned solely by the re-key worker thread. */ +struct rekey_ctx { + struct list_head next; - struct list_head ipcps; /* list of ipcps in system */ - size_t n_ipcps; /* number of ipcps */ + int flow_id; + void * ctx; /* OAP client ctx (opaque) */ + struct timespec deadline; /* reap if no RESPONSE by then */ +}; - struct list_head proc_table; /* processes */ - struct list_head prog_table; /* programs known */ - struct list_head spawned_pids; /* child processes */ - pthread_rwlock_t reg_lock; /* lock for registration info */ +enum rekey_evt_type { + REKEY_INIT = 0, /* start an exchange for flow_id */ + REKEY_REQ, /* a REQUEST arrived for flow_id */ + REKEY_RESP, /* a RESPONSE arrived for flow_id */ + REKEY_DIRECT /* in-process re-key, direct flow */ +}; - struct bmp * flow_ids; /* flow_ids for flows */ - struct list_head irm_flows; /* flow information */ - pthread_rwlock_t flows_lock; /* lock for flows */ +struct rekey_evt { + struct list_head next; + + enum rekey_evt_type type; + int flow_id; + pid_t n_1_pid; /* INIT: flow's lower IPCP */ + buffer_t buf; /* RESP: owned RESPONSE payload */ +}; +struct { + bool log_stdout; /* log to stdout */ +#ifdef HAVE_TOML + char * cfg_file; /* configuration file path */ +#endif struct lockfile * lf; /* single irmd per system */ - struct shm_rdrbuff * rdrb; /* rdrbuff for packets */ + struct ssm_pool * gspp; /* pool for packets */ int sockfd; /* UNIX socket */ @@ -138,6 +154,13 @@ struct { pthread_t irm_sanitize; /* clean up irmd resources */ pthread_t acceptor; /* accept new commands */ + + struct { + pthread_t worker; /* Tier-2 re-key orchestrator */ + struct list_head inbox; /* re-key events for worker */ + pthread_cond_t cond; /* inbox signal condvar */ + pthread_mutex_t mtx; /* inbox lock */ + } rk; } irmd; static enum irm_state irmd_get_state(void) @@ -162,488 +185,261 @@ static void irmd_set_state(enum irm_state state) pthread_rwlock_unlock(&irmd.state_lock); } -static void clear_irm_flow(struct irm_flow * f) { - ssize_t idx; - - assert(f); - - if (f->len != 0) { - free(f->data); - f->len = 0; - } - - while ((idx = shm_rbuff_read(f->n_rb)) >= 0) - shm_rdrbuff_remove(irmd.rdrb, idx); - - while ((idx = shm_rbuff_read(f->n_1_rb)) >= 0) - shm_rdrbuff_remove(irmd.rdrb, idx); -} - -static struct irm_flow * get_irm_flow(int flow_id) +static pid_t spawn_program(char ** argv) { - struct list_head * pos = NULL; + pid_t pid; + struct stat s; - list_for_each(pos, &irmd.irm_flows) { - struct irm_flow * e = list_entry(pos, struct irm_flow, next); - if (e->flow_id == flow_id) - return e; + if (stat(argv[0], &s) != 0) { + log_warn("Program %s does not exist.", argv[0]); + return -1; } - return NULL; -} - -static struct irm_flow * get_irm_flow_n(pid_t n_pid) -{ - struct list_head * pos = NULL; - - list_for_each(pos, &irmd.irm_flows) { - struct irm_flow * e = list_entry(pos, struct irm_flow, next); - if (e->n_pid == n_pid && - irm_flow_get_state(e) == FLOW_ALLOC_PENDING) - return e; + if (!(s.st_mode & S_IXUSR)) { + log_warn("Program %s is not executable.", argv[0]); + return -1; } - return NULL; -} - -static struct ipcp_entry * ipcp_entry_create(const char * name, - enum ipcp_type type) -{ - struct ipcp_entry * e; - pthread_condattr_t cattr; - - e = malloc(sizeof(*e)); - if (e == NULL) - goto fail_malloc; - - e->layer = NULL; - e->type = type; - e->state = IPCP_BOOT; - e->name = strdup(name); - if (e->name == NULL) - goto fail_name; - - if (pthread_condattr_init(&cattr)) - goto fail_cattr; -#ifndef __APPLE__ - pthread_condattr_setclock(&cattr, PTHREAD_COND_CLOCK); -#endif - if (pthread_cond_init(&e->cond, &cattr)) - goto fail_cond; - - if (pthread_mutex_init(&e->lock, NULL)) - goto fail_mutex; - - - list_head_init(&e->next); - - pthread_condattr_destroy(&cattr); - - return e; - - fail_mutex: - pthread_cond_destroy(&e->cond); - fail_cond: - pthread_condattr_destroy(&cattr); - fail_cattr: - free(e->name); - fail_name: - free(e); - fail_malloc: - return NULL; -} - -static void ipcp_entry_destroy(struct ipcp_entry * e) -{ - assert(e); - - pthread_mutex_lock(&e->lock); - - while (e->state == IPCP_BOOT) - pthread_cond_wait(&e->cond, &e->lock); - - pthread_mutex_unlock(&e->lock); + if (posix_spawn(&pid, argv[0], NULL, NULL, argv, NULL)) { + log_err("Failed to spawn new process for %s.", argv[0]); + return -1; + } - free(e->name); - free(e->layer); - free(e); -} + log_info("Instantiated %s as process %d.", argv[0], pid); -static void ipcp_entry_set_state(struct ipcp_entry * e, - enum init_state state) -{ - pthread_mutex_lock(&e->lock); - e->state = state; - pthread_cond_broadcast(&e->cond); - pthread_mutex_unlock(&e->lock); + return pid; } -static int ipcp_entry_wait_boot(struct ipcp_entry * e) +static pid_t spawn_ipcp(struct ipcp_info * info) { - int ret = 0; - struct timespec dl; - struct timespec to = {SOCKET_TIMEOUT / 1000, - (SOCKET_TIMEOUT % 1000) * MILLION}; - - clock_gettime(PTHREAD_COND_CLOCK, &dl); - ts_add(&dl, &to, &dl); - - pthread_mutex_lock(&e->lock); - - while (e->state == IPCP_BOOT && ret != ETIMEDOUT) - ret = pthread_cond_timedwait(&e->cond, &e->lock, &dl); - - if (ret == ETIMEDOUT) { - kill(e->pid, SIGTERM); - e->state = IPCP_NULL; - pthread_cond_signal(&e->cond); + char * exec_name = NULL; + char irmd_pid[10]; + char full_name[256]; + char * argv[5]; + pid_t pid; + + switch(info->type) { + case IPCP_UNICAST: + exec_name = IPCP_UNICAST_EXEC; + break; + case IPCP_BROADCAST: + exec_name = IPCP_BROADCAST_EXEC; + break; + case IPCP_UDP4: + exec_name = IPCP_UDP4_EXEC; + break; + case IPCP_UDP6: + exec_name = IPCP_UDP6_EXEC; + break; + case IPCP_ETH_LLC: + exec_name = IPCP_ETH_LLC_EXEC; + break; + case IPCP_ETH_DIX: + exec_name = IPCP_ETH_DIX_EXEC; + break; + case IPCP_LOCAL: + exec_name = IPCP_LOCAL_EXEC; + break; + default: + assert(false); } - if (e->state != IPCP_LIVE) { - pthread_mutex_unlock(&e->lock); + if (exec_name == NULL) { + log_err("IPCP type not installed."); return -1; } - pthread_mutex_unlock(&e->lock); + sprintf(irmd_pid, "%u", getpid()); - return 0; -} - -static struct ipcp_entry * get_ipcp_entry_by_pid(pid_t pid) -{ - struct list_head * p; + strcpy(full_name, INSTALL_PREFIX"/"INSTALL_SBINDIR"/"); + strcat(full_name, exec_name); - list_for_each(p, &irmd.ipcps) { - struct ipcp_entry * e = list_entry(p, struct ipcp_entry, next); - if (e->pid == pid) - return e; - } - - return NULL; -} + /* log_file to be placed at the end */ + argv[0] = full_name; + argv[1] = irmd_pid; + argv[2] = (char *) info->name; + if (log_syslog) + argv[3] = "1"; + else + argv[3] = NULL; -static struct ipcp_entry * get_ipcp_entry_by_name(const char * name) -{ - struct list_head * p; + argv[4] = NULL; - list_for_each(p, &irmd.ipcps) { - struct ipcp_entry * e = list_entry(p, struct ipcp_entry, next); - if (strcmp(name, e->name) == 0) - return e; + pid = spawn_program(argv); + if (pid < 0) { + log_err("Failed to spawn IPCP %s.", info->name); + return -1; } - return NULL; -} - -static struct ipcp_entry * get_ipcp_entry_by_layer(const char * layer) -{ - struct list_head * p; - - list_for_each(p, &irmd.ipcps) { - struct ipcp_entry * e = list_entry(p, struct ipcp_entry, next); - if (strcmp(layer, e->layer) == 0) - return e; - } + info->pid = pid; + info->state = IPCP_INIT; - return NULL; + return 0; } -static struct ipcp_entry * get_ipcp_by_dst_name(const char * name, - pid_t src) +static int kill_ipcp(pid_t pid) { - struct list_head * p; - struct list_head * h; - uint8_t * hash; - pid_t pid; - size_t len; + int status; - pthread_rwlock_rdlock(&irmd.reg_lock); - - list_for_each_safe(p, h, &irmd.ipcps) { - struct ipcp_entry * e = list_entry(p, struct ipcp_entry, next); - if (e->layer == NULL || e->pid == src) - continue; - - len = IPCP_HASH_LEN(e); - - hash = malloc(len); - if (hash == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - return NULL; - } - - str_hash(e->dir_hash_algo, hash, name); - - pid = e->pid; - - pthread_rwlock_unlock(&irmd.reg_lock); - - if (ipcp_query(pid, hash, len) == 0) { - free(hash); - return e; - } - - free(hash); - - pthread_rwlock_rdlock(&irmd.reg_lock); + if (kill(pid, SIGTERM) < 0) { + log_err("Failed to destroy IPCP: %s.", strerror(errno)); + return -1; } - pthread_rwlock_unlock(&irmd.reg_lock); + waitpid(pid, &status, 0); - return NULL; + return 0; } -static pid_t create_ipcp(const char * name, - enum ipcp_type type) +int create_ipcp(struct ipcp_info * info) { - struct pid_el * ppid; - struct ipcp_entry * entry; - struct list_head * p; - pid_t pid; - - pthread_rwlock_rdlock(&irmd.reg_lock); - - entry = get_ipcp_entry_by_name(name); - if (entry != NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - log_err("IPCP by that name already exists."); - return -EPERM; - } - - pthread_rwlock_unlock(&irmd.reg_lock); + struct timespec abstime; + struct timespec timeo = TIMESPEC_INIT_MS(SOCKET_TIMEOUT); + int status; - ppid = malloc(sizeof(*ppid)); - if (ppid == NULL) - goto fail_ppid; + assert(info->pid == 0); - entry = ipcp_entry_create(name, type); - if (entry == NULL) { - log_err("Failed to create IPCP entry."); - goto fail_ipcp_entry; - } + clock_gettime(PTHREAD_COND_CLOCK, &abstime); + ts_add(&abstime, &timeo, &abstime); - pid = ipcp_create(name, type); - if (pid == -1) { + if (spawn_ipcp(info) < 0) { log_err("Failed to create IPCP."); goto fail_ipcp; } - entry->pid = pid; - - pthread_rwlock_wrlock(&irmd.reg_lock); - - list_for_each(p, &irmd.ipcps) { - if (list_entry(p, struct ipcp_entry, next)->type > type) - break; + if (reg_create_ipcp(info) < 0) { + log_err("Failed to create IPCP entry."); + goto fail_reg_ipcp; } - list_add_tail(&entry->next, p); - ++irmd.n_ipcps; - - ppid->pid = entry->pid; - list_add(&ppid->next, &irmd.spawned_pids); - - pthread_rwlock_unlock(&irmd.reg_lock); - - /* IRMd maintenance will clean up if booting fails. */ - if (ipcp_entry_wait_boot(entry)) { - log_err("IPCP %d failed to boot.", pid); - return -1; + if (reg_wait_ipcp_boot(info, &abstime)) { + log_err("IPCP %d failed to boot.", info->pid); + goto fail_boot; } - log_info("Created IPCP %d.", pid); + log_info("Created IPCP %d.", info->pid); - return pid; + return 0; - fail_ipcp: - ipcp_entry_destroy(entry); - fail_ipcp_entry: - free(ppid); - fail_ppid: + fail_boot: + waitpid(info->pid, &status, 0); + reg_destroy_proc(info->pid); return -1; -} - -static int create_ipcp_r(pid_t pid, - int result) -{ - struct list_head * p; - - pthread_rwlock_rdlock(&irmd.reg_lock); - - list_for_each(p, &irmd.ipcps) { - struct ipcp_entry * e = list_entry(p, struct ipcp_entry, next); - if (e->pid == pid) { - ipcp_entry_set_state(e, result ? IPCP_NULL : IPCP_LIVE); - break; - } - } - - pthread_rwlock_unlock(&irmd.reg_lock); - return 0; + fail_reg_ipcp: + kill_ipcp(info->pid); + fail_ipcp: + return -1; } -static void clear_spawned_process(pid_t pid) +static int create_ipcp_r(struct ipcp_info * info) { - struct list_head * p; - struct list_head * h; - - list_for_each_safe(p, h, &(irmd.spawned_pids)) { - struct pid_el * a = list_entry(p, struct pid_el, next); - if (a->pid == pid) { - list_del(&a->next); - free(a); - } - } + return reg_respond_ipcp(info); } static int destroy_ipcp(pid_t pid) { - struct list_head * p; - struct list_head * h; - - pthread_rwlock_wrlock(&irmd.reg_lock); - - list_for_each_safe(p, h, &irmd.ipcps) { - struct ipcp_entry * e = list_entry(p, struct ipcp_entry, next); - if (e->pid == pid) { - clear_spawned_process(pid); - if (ipcp_destroy(pid)) - log_err("Could not destroy IPCP."); - list_del(&e->next); - ipcp_entry_destroy(e); - --irmd.n_ipcps; - log_info("Destroyed IPCP %d.", pid); - } + if (kill_ipcp(pid)) { + log_err("Could not destroy IPCP."); + goto fail; } - pthread_rwlock_unlock(&irmd.reg_lock); + if (reg_destroy_proc(pid)) { + log_err("Failed to remove IPCP from registry."); + goto fail; + } return 0; + fail: + return -1; } -static int bootstrap_ipcp(pid_t pid, - ipcp_config_msg_t * conf) +int bootstrap_ipcp(pid_t pid, + struct ipcp_config * conf) { - struct ipcp_entry * entry; - struct layer_info info; + struct ipcp_info info; + struct layer_info layer; - pthread_rwlock_wrlock(&irmd.reg_lock); + info.pid = pid; - entry = get_ipcp_entry_by_pid(pid); - if (entry == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - log_err("No such IPCP."); - return -1; + if (reg_get_ipcp(&info, NULL) < 0) { + log_err("Could not find IPCP %d.", pid); + goto fail; } - if (entry->type != (enum ipcp_type) conf->ipcp_type) { - pthread_rwlock_unlock(&irmd.reg_lock); - log_err("Configuration does not match IPCP type."); - return -1; - } + if (conf->type == IPCP_UDP4 || conf->type == IPCP_UDP6) + conf->layer_info.dir_hash_algo = (enum pol_dir_hash) HASH_MD5; - if (ipcp_bootstrap(entry->pid, conf, &info)) { - pthread_rwlock_unlock(&irmd.reg_lock); + if (ipcp_bootstrap(pid, conf, &layer)) { log_err("Could not bootstrap IPCP."); - return -1; - } - - entry->layer = strdup(info.layer_name); - if (entry->layer == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - log_warn("Failed to set name of layer."); - return -ENOMEM; + goto fail; } - entry->dir_hash_algo = info.dir_hash_algo; + info.state = IPCP_BOOT; - pthread_rwlock_unlock(&irmd.reg_lock); + if (reg_set_layer_for_ipcp(&info, &layer) < 0) { + log_err("Failed to set layer info for IPCP."); + goto fail; + } - log_info("Bootstrapped IPCP %d in layer %s.", - pid, conf->layer_info->layer_name); + log_info("Bootstrapped IPCP %d.", pid); return 0; + fail: + return -1; } -static int enroll_ipcp(pid_t pid, - char * dst) +int enroll_ipcp(pid_t pid, + const char * dst) { - struct ipcp_entry * entry = NULL; - struct layer_info info; - - pthread_rwlock_wrlock(&irmd.reg_lock); + struct layer_info layer; + struct ipcp_info info; - entry = get_ipcp_entry_by_pid(pid); - if (entry == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - log_err("No such IPCP."); - return -1; - } + info.pid = pid; - if (entry->layer != NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - log_err("IPCP in wrong state"); - return -1; + if (reg_get_ipcp(&info, NULL) < 0) { + log_err("Could not find IPCP."); + goto fail; } - pthread_rwlock_unlock(&irmd.reg_lock); - - if (ipcp_enroll(pid, dst, &info) < 0) { + if (ipcp_enroll(pid, dst, &layer) < 0) { log_err("Could not enroll IPCP %d.", pid); - return -1; + goto fail; } - pthread_rwlock_wrlock(&irmd.reg_lock); - - entry = get_ipcp_entry_by_pid(pid); - if (entry == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - log_err("No such IPCP."); - return -1; - } + info.state = IPCP_BOOT; - entry->layer = strdup(info.layer_name); - if (entry->layer == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - log_err("Failed to strdup layer_name."); - return -ENOMEM; + if (reg_set_layer_for_ipcp(&info, &layer) < 0) { + log_err("Failed to set layer info for IPCP."); + goto fail; } - entry->dir_hash_algo = info.dir_hash_algo; - - pthread_rwlock_unlock(&irmd.reg_lock); - - log_info("Enrolled IPCP %d in layer %s.", - pid, info.layer_name); + log_info("Enrolled IPCP %d in layer %s.", pid, layer.name); return 0; + fail: + return -1; } -static int connect_ipcp(pid_t pid, - const char * dst, - const char * component, - qosspec_t qs) +int connect_ipcp(pid_t pid, + const char * dst, + const char * component, + qosspec_t qs) { - struct ipcp_entry * entry = NULL; + struct ipcp_info info; - pthread_rwlock_rdlock(&irmd.reg_lock); + info.pid = pid; - entry = get_ipcp_entry_by_pid(pid); - if (entry == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); + if (reg_get_ipcp(&info, NULL) < 0) { log_err("No such IPCP."); return -EIPCP; } - if (entry->type != IPCP_UNICAST && entry->type != IPCP_BROADCAST) { - pthread_rwlock_unlock(&irmd.reg_lock); + if (info.type != IPCP_UNICAST && info.type != IPCP_BROADCAST) { log_err("Cannot establish connections for this IPCP type."); return -EIPCP; } - pthread_rwlock_unlock(&irmd.reg_lock); - log_dbg("Connecting %s to %s.", component, dst); if (ipcp_connect(pid, dst, component, qs)) { @@ -661,25 +457,20 @@ static int disconnect_ipcp(pid_t pid, const char * dst, const char * component) { - struct ipcp_entry * entry = NULL; + struct ipcp_info info; - pthread_rwlock_rdlock(&irmd.reg_lock); + info.pid = pid; - entry = get_ipcp_entry_by_pid(pid); - if (entry == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); + if (reg_get_ipcp(&info, NULL) < 0) { log_err("No such IPCP."); return -EIPCP; } - if (entry->type != IPCP_UNICAST) { - pthread_rwlock_unlock(&irmd.reg_lock); + if (info.type != IPCP_UNICAST && info.type != IPCP_BROADCAST) { log_err("Cannot tear down connections for this IPCP type."); return -EIPCP; } - pthread_rwlock_unlock(&irmd.reg_lock); - if (ipcp_disconnect(pid, dst, component)) { log_err("Could not disconnect IPCP."); return -EPERM; @@ -691,1306 +482,1761 @@ static int disconnect_ipcp(pid_t pid, return 0; } -static int bind_program(char * prog, - char * name, - uint16_t flags, - int argc, - char ** argv) +static void name_update_sec_paths(struct name_info * info) { - char * progs; - char ** argv_dup = NULL; - int i; - char * name_dup = NULL; - struct prog_entry * e = NULL; - struct reg_entry * re = NULL; + char * srv_dir = OUROBOROS_SRV_CRT_DIR; + char * cli_dir = OUROBOROS_CLI_CRT_DIR; - if (prog == NULL || name == NULL) - return -EINVAL; + assert(info != NULL); - pthread_rwlock_wrlock(&irmd.reg_lock); + if (strlen(info->s.sec) == 0) + sprintf(info->s.sec, "%s/%s/sec.conf", srv_dir, info->name); - e = prog_table_get(&irmd.prog_table, path_strip(prog)); - if (e == NULL) { - progs = strdup(path_strip(prog)); - if (progs == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - return -ENOMEM; - } + if (strlen(info->s.crt) == 0) + sprintf(info->s.crt, "%s/%s/crt.pem", srv_dir, info->name); - if ((flags & BIND_AUTO) && argc) { - /* We need to duplicate argv and set argv[0] to prog. */ - argv_dup = malloc((argc + 2) * sizeof(*argv_dup)); - argv_dup[0] = strdup(prog); - for (i = 1; i <= argc; ++i) { - argv_dup[i] = strdup(argv[i - 1]); - if (argv_dup[i] == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - argvfree(argv_dup); - log_err("Failed to bind program " - "%s to %s.", - prog, name); - free(progs); - return -ENOMEM; - } - } - argv_dup[argc + 1] = NULL; - } - e = prog_entry_create(progs, flags, argv_dup); - if (e == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - free(progs); - argvfree(argv_dup); - return -ENOMEM; - } - prog_table_add(&irmd.prog_table, e); - } + if (strlen(info->s.key) == 0) + sprintf(info->s.key, "%s/%s/key.pem", srv_dir, info->name); - name_dup = strdup(name); - if (name_dup == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - return -ENOMEM; + if (strlen(info->c.sec) == 0) + sprintf(info->c.sec, "%s/%s/sec.conf", cli_dir, info->name); + + if (strlen(info->c.crt) == 0) + sprintf(info->c.crt, "%s/%s/crt.pem", cli_dir, info->name); + + if (strlen(info->c.key) == 0) + sprintf(info->c.key, "%s/%s/key.pem", cli_dir, info->name); +} + +int name_create(struct name_info * info) +{ + int ret; + + assert(info != NULL); + + name_update_sec_paths(info); + + ret = reg_create_name(info); + if (ret == -EEXIST) { + log_info("Name %s already exists.", info->name); + return 0; } - if (prog_entry_add_name(e, name_dup)) { - log_err("Failed adding name."); - pthread_rwlock_unlock(&irmd.reg_lock); - free(name_dup); - return -ENOMEM; + if (ret < 0) { + log_err("Failed to create name %s.", info->name); + return -1; } - re = registry_get_entry(&irmd.registry, name); - if (re != NULL && reg_entry_add_prog(re, e) < 0) - log_err("Failed adding program %s for name %s.", prog, name); + log_info("Created new name: %s.", info->name); - pthread_rwlock_unlock(&irmd.reg_lock); + return 0; +} + +static int name_destroy(const char * name) +{ - log_info("Bound program %s to name %s.", prog, name); + assert(name != NULL); + + if (reg_destroy_name(name) < 0) { + log_err("Failed to destroy name %s.", name); + return -1; + } + + log_info("Destroyed name: %s.", name); return 0; } -static int bind_process(pid_t pid, - char * name) +int bind_program(char ** exec, + const char * name, + uint8_t flags) { - char * name_dup = NULL; - struct proc_entry * e = NULL; - struct reg_entry * re = NULL; - struct timespec now; - struct timespec dl = {0, 10 * MILLION}; + struct prog_info prog; + struct name_info ni; - if (name == NULL) + if (name == NULL || exec == NULL || exec[0] == NULL) return -EINVAL; - clock_gettime(PTHREAD_COND_CLOCK, &now); + memset(&prog, 0, sizeof(prog)); + memset(&ni, 0, sizeof(ni)); - ts_add(&dl, &now, &dl); + if (!reg_has_prog(exec[0])) { + strcpy(prog.name, path_strip(exec[0])); + strcpy(prog.path, exec[0]); + if (reg_create_prog(&prog) < 0) + goto fail_prog; + } - pthread_rwlock_wrlock(&irmd.reg_lock); + if (!reg_has_name(name)) { + ni.pol_lb = LB_SPILL; + strcpy(ni.name, name); + if (name_create(&ni) < 0) + goto fail_name; + } - while (!kill(pid, 0)) { - e = proc_table_get(&irmd.proc_table, pid); - if (e != NULL || ts_diff_ms(&now, &dl) > 0) - break; - clock_gettime(PTHREAD_COND_CLOCK, &now); - sched_yield(); + if (reg_bind_prog(name, exec, flags) < 0) { + log_err("Failed to bind program %s to name %s", exec[0], name); + goto fail_bind; } - if (e == NULL) { + log_info("Bound program %s to name %s.", exec[0], name); + + return 0; + + fail_bind: + if (strlen(ni.name) > 0) + reg_destroy_name(name); + fail_name: + if (strlen(prog.name) > 0) + reg_destroy_prog(exec[0]); + fail_prog: + return -1; +} + +int bind_process(pid_t pid, + const char * name) +{ + struct timespec abstime; + struct timespec timeo = TIMESPEC_INIT_MS(10); + struct name_info ni; + + if (name == NULL) + return -EINVAL; + + clock_gettime(PTHREAD_COND_CLOCK, &abstime); + ts_add(&abstime, &timeo, &abstime); + + if (reg_wait_proc(pid, &abstime) < 0) { log_err("Process %d does not %s.", pid, kill(pid, 0) ? "exist" : "respond"); - pthread_rwlock_unlock(&irmd.reg_lock); - return -1; + goto fail; } - name_dup = strdup(name); - if (name_dup == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - return -ENOMEM; + memset(&ni, 0, sizeof(ni)); + + if (!reg_has_name(name)) { + ni.pol_lb = LB_SPILL; + strcpy(ni.name, name); + if (name_create(&ni) < 0) + goto fail; } - if (proc_entry_add_name(e, name_dup)) { - pthread_rwlock_unlock(&irmd.reg_lock); + if (reg_bind_proc(name, pid) < 0) { log_err("Failed to add name %s to process %d.", name, pid); - free(name_dup); - return -1; + goto fail_bind; } - re = registry_get_entry(&irmd.registry, name); - if (re != NULL && reg_entry_add_pid(re, pid) < 0) - log_err("Failed adding process %d for name %s.", pid, name); - - pthread_rwlock_unlock(&irmd.reg_lock); - log_info("Bound process %d to name %s.", pid, name); return 0; + + fail_bind: + if (strlen(ni.name) > 0) + reg_destroy_name(name); + fail: + return -1; + } -static int unbind_program(char * prog, - char * name) +static int unbind_program(const char * prog, + const char * name) { - struct reg_entry * e; - if (prog == NULL) return -EINVAL; - pthread_rwlock_wrlock(&irmd.reg_lock); - - if (name == NULL) - prog_table_del(&irmd.prog_table, prog); - else { - struct prog_entry * en = prog_table_get(&irmd.prog_table, prog); - if (en == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - return -EINVAL; + if (name == NULL) { + if (reg_destroy_prog(prog) < 0) { + log_err("Failed to unbind %s.", prog); + goto fail; } - - prog_entry_del_name(en, name); - - e = registry_get_entry(&irmd.registry, name); - if (e != NULL) - reg_entry_del_prog(e, prog); - } - - pthread_rwlock_unlock(&irmd.reg_lock); - - if (name == NULL) log_info("Program %s unbound.", prog); - else - log_info("All names matching %s unbound for %s.", name, prog); + } else { + if (reg_unbind_prog(name, prog) < 0) { + log_err("Failed to unbind %s from %s", prog, name); + goto fail; + } + log_info("Name %s unbound for %s.", name, prog); + } return 0; + + fail: + return -1; } static int unbind_process(pid_t pid, const char * name) { - struct reg_entry * e; + if (name == NULL) { + if (reg_destroy_proc(pid) < 0) { + log_err("Failed to unbind %d.", pid); + goto fail; + } + log_info("Process %d unbound.", pid); + } else { + if (reg_unbind_proc(name, pid) < 0) { + log_err("Failed to unbind %d from %s", pid, name); + goto fail; + } + log_info("Name %s unbound for process %d.", name, pid); + } - pthread_rwlock_wrlock(&irmd.reg_lock); + return 0; - if (name == NULL) - proc_table_del(&irmd.proc_table, pid); - else { - struct proc_entry * en = proc_table_get(&irmd.proc_table, pid); - if (en != NULL) - proc_entry_del_name(en, name); + fail: + return -1; +} - e = registry_get_entry(&irmd.registry, name); - if (e != NULL) - reg_entry_del_pid(e, pid); - } +static int list_ipcps(ipcp_list_msg_t *** ipcps, + size_t * n_ipcps) +{ + int n; - pthread_rwlock_unlock(&irmd.reg_lock); + n = reg_list_ipcps(ipcps); + if (n < 0) + goto fail; - if (name == NULL) - log_info("Process %d unbound.", pid); - else - log_info("All names matching %s unbound for %d.", name, pid); + *n_ipcps = (size_t) n; return 0; + fail: + *ipcps = NULL; + *n_ipcps = 0; + return -1; } -static ssize_t list_ipcps(ipcp_info_msg_t *** ipcps, - size_t * n_ipcps) +static int list_names(name_info_msg_t *** names, + size_t * n_names) { - struct list_head * p; - int i = 0; + int n; - pthread_rwlock_rdlock(&irmd.reg_lock); + n = reg_list_names(names); + if (n < 0) + goto fail; - *n_ipcps = irmd.n_ipcps; - if (*n_ipcps == 0) { - pthread_rwlock_unlock(&irmd.reg_lock); - return 0; + *n_names = (size_t) n; + + return 0; + fail: + *names = NULL; + *n_names = 0; + return -1; +} + +int name_reg(const char * name, + pid_t pid) +{ + struct ipcp_info info; + struct layer_info layer; + buffer_t hash; + + assert(name); + + info.pid = pid; + + if (!reg_has_name(name)) { + log_err("Failed to get name %s.", name); + return -ENAME; } - *ipcps = malloc(irmd.n_ipcps * sizeof(**ipcps)); - if (*ipcps == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - *n_ipcps = 0; - return -ENOMEM; + if (reg_get_ipcp(&info, &layer) < 0) { + log_err("Failed to get IPCP %d.", pid); + return -EIPCP; } - list_for_each(p, &irmd.ipcps) { - struct ipcp_entry * e = list_entry(p, struct ipcp_entry, next); - (*ipcps)[i] = malloc(sizeof(***ipcps)); - if ((*ipcps)[i] == NULL) { - --i; - goto fail; - } + hash.len = hash_len((enum hash_algo) layer.dir_hash_algo); + hash.data = malloc(hash.len); + if (hash.data == NULL) { + log_err("Failed to malloc hash."); + return -ENOMEM; + } - ipcp_info_msg__init((*ipcps)[i]); - (*ipcps)[i]->name = strdup(e->name); - if ((*ipcps)[i]->name == NULL) - goto fail; + str_hash((enum hash_algo) layer.dir_hash_algo, hash.data, name); - (*ipcps)[i]->layer = strdup( - e->layer != NULL ? e->layer : "Not enrolled"); - if ((*ipcps)[i]->layer == NULL) - goto fail; + if (ipcp_reg(pid, hash)) { + log_err("Could not register " HASH_FMT32 " with IPCP %d.", + HASH_VAL32(hash.data), pid); + goto fail_hash; + } - (*ipcps)[i]->pid = e->pid; - (*ipcps)[i++]->type = e->type; - } + log_info("Registered %s with IPCP %d as " HASH_FMT32 ".", + name, pid, HASH_VAL32(hash.data)); - pthread_rwlock_unlock(&irmd.reg_lock); + freebuf(hash); return 0; - fail: - pthread_rwlock_unlock(&irmd.reg_lock); - while (i >= 0) { - free((*ipcps)[i]->layer); - free((*ipcps)[i]->name); - free(*ipcps[i--]); - } - free(*ipcps); - *n_ipcps = 0; - return -ENOMEM; + fail_hash: + freebuf(hash); + return -1; } -static int name_create(const char * name, - enum pol_balance pol) +static int name_unreg(const char * name, + pid_t pid) { - struct reg_entry * re; - struct list_head * p; + struct ipcp_info info; + struct layer_info layer; + buffer_t hash; assert(name); - pthread_rwlock_wrlock(&irmd.reg_lock); + info.pid = pid; - if (registry_has_name(&irmd.registry, name)) { - pthread_rwlock_unlock(&irmd.reg_lock); - log_err("Registry entry for %s already exists.", name); + if (!reg_has_name(name)) { + log_err("Failed to get name %s.", name); return -ENAME; } - re = registry_add_name(&irmd.registry, name); - if (re == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - log_err("Failed creating registry entry for %s.", name); - return -ENOMEM; + if (reg_get_ipcp(&info, &layer) < 0) { + log_err("Failed to get IPCP %d.", pid); + return -EIPCP; } - ++irmd.n_names; - reg_entry_set_policy(re, pol); - - /* check the tables for existing bindings */ - list_for_each(p, &irmd.proc_table) { - struct list_head * q; - struct proc_entry * e; - e = list_entry(p, struct proc_entry, next); - list_for_each(q, &e->names) { - struct str_el * s; - s = list_entry(q, struct str_el, next); - if (!strcmp(s->str, name)) - reg_entry_add_pid(re, e->pid); - } + + hash.len = hash_len((enum hash_algo) layer.dir_hash_algo); + hash.data = malloc(hash.len); + if (hash.data == NULL) { + log_err("Failed to malloc hash."); + return -ENOMEM; } - list_for_each(p, &irmd.prog_table) { - struct list_head * q; - struct prog_entry * e; - e = list_entry(p, struct prog_entry, next); - list_for_each(q, &e->names) { - struct str_el * s; - s = list_entry(q, struct str_el, next); - if (!strcmp(s->str, name)) - reg_entry_add_prog(re, e); - } + str_hash((enum hash_algo) layer.dir_hash_algo, hash.data, name); + + if (ipcp_unreg(pid, hash)) { + log_err("Could not unregister %s with IPCP %d.", name, pid); + goto fail_hash; } - pthread_rwlock_unlock(&irmd.reg_lock); + log_info("Unregistered %s from %d.", name, pid); - log_info("Created new name: %s.", name); + freebuf(hash); return 0; + + fail_hash: + freebuf(hash); + return -1; } -static int name_destroy(const char * name) +static int get_peer_ids(int fd, + uid_t * uid, + gid_t * gid, + pid_t * pid) { - assert(name); - - pthread_rwlock_wrlock(&irmd.reg_lock); +#if defined(__linux__) + struct ucred ucred; + socklen_t len; - if (!registry_has_name(&irmd.registry, name)) { - pthread_rwlock_unlock(&irmd.reg_lock); - log_warn("Registry entry for %s does not exist.", name); - return -ENAME; - } - - registry_del_name(&irmd.registry, name); - --irmd.n_names; + len = sizeof(ucred); - pthread_rwlock_unlock(&irmd.reg_lock); + if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) < 0) + goto fail; - log_info("Destroyed name: %s.", name); + *uid = ucred.uid; + *gid = ucred.gid; + if (pid != NULL) + *pid = ucred.pid; +#else + if (getpeereid(fd, uid, gid) < 0) + goto fail; + if (pid != NULL) + *pid = -1; /* no portable SO_PEERCRED.pid equivalent */ +#endif return 0; + fail: + return -1; } -static ssize_t list_names(name_info_msg_t *** names, - size_t * n_names) +static int proc_announce(const struct proc_info * info) { - struct list_head * p; - int i = 0; - - pthread_rwlock_rdlock(&irmd.reg_lock); - - *n_names = irmd.n_names; - if (*n_names == 0) { - pthread_rwlock_unlock(&irmd.reg_lock); - return 0; + if (reg_prepare_pool(info->uid, info->gid) < 0) { + log_err("Failed to prepare pool for uid %d.", info->uid); + goto fail; } - *names = malloc(irmd.n_names * sizeof(**names)); - if (*names == NULL) { - *n_names = 0; - pthread_rwlock_unlock(&irmd.reg_lock); - return -ENOMEM; + if (reg_create_proc(info) < 0) { + log_err("Failed to add process %d.", info->pid); + goto fail; } - list_for_each(p, &irmd.registry) { - struct reg_entry * e = list_entry(p, struct reg_entry, next); + log_info("Process added: %d (%s).", info->pid, info->prog); - (*names)[i] = malloc(sizeof(***names)); - if ((*names)[i] == NULL) { - --i; - goto fail; - } + return 0; - name_info_msg__init((*names)[i]); - (*names)[i]->name = strdup(e->name); - if ((*names)[i]->name == NULL) - goto fail; + fail: + return -1; +} - (*names)[i++]->pol_lb = e->pol_lb; - } +static int proc_exit(pid_t pid) +{ + if (reg_destroy_proc(pid) < 0) + log_err("Failed to remove process %d.", pid); - pthread_rwlock_unlock(&irmd.reg_lock); + log_info("Process removed: %d.", pid); return 0; +} - fail: - pthread_rwlock_unlock(&irmd.reg_lock); - while (i >= 0) { - free((*names)[i]->name); - free(*names[i--]); - } - free(*names); - *n_names = 0; - return -ENOMEM; +static void __cleanup_flow(void * flow) +{ + reg_destroy_flow(((struct flow_info *) flow)->id); } -static int name_reg(const char * name, - pid_t pid) +static int flow_accept(struct flow_info * flow, + buffer_t * data, + struct timespec * abstime, + struct crypt_sk * sk) { - size_t len; - struct ipcp_entry * ipcp; - uint8_t * hash; - int err; + buffer_t req_hdr; + buffer_t resp_hdr; + buffer_t peer_crt = BUF_INIT; + char name[NAME_SIZE + 1]; + struct name_info info; + int err; - assert(name); + assert(data != NULL && BUF_IS_EMPTY(data)); - pthread_rwlock_wrlock(&irmd.reg_lock); + clrbuf(req_hdr); + clrbuf(resp_hdr); - if (!registry_has_name(&irmd.registry, name)) { - err = -ENAME; - goto fail; + if (!reg_has_proc(flow->n_pid)) { + log_err("Unknown process %d calling accept.", flow->n_pid); + err = -EINVAL; + goto fail_flow; } - ipcp = get_ipcp_entry_by_pid(pid); - if (ipcp == NULL) { - err = -EIPCP; - goto fail; + if (reg_create_flow(flow) < 0) { + log_err("Failed to create flow."); + err = -EBADF; + goto fail_flow; } - if (ipcp->layer == NULL) { - err = -EPERM; - goto fail; + if (reg_prepare_flow_accept(flow) < 0) { + log_err("Failed to prepare accept."); + err = -EBADF; + goto fail_wait; } - len = IPCP_HASH_LEN(ipcp); + pthread_cleanup_push(__cleanup_flow, flow); - hash = malloc(len); - if (hash == NULL) { - err = -ENOMEM; - goto fail; + err = reg_wait_flow_accepted(flow, &req_hdr, abstime); + + pthread_cleanup_pop(false); + + if (err == -ETIMEDOUT) { + log_err("Flow accept timed out."); + goto fail_wait; } - str_hash(ipcp->dir_hash_algo, hash, name); - pthread_rwlock_unlock(&irmd.reg_lock); + if (err == -1) { + log_dbg("Flow accept terminated."); + err = -EPIPE; + goto fail_wait; + } - if (ipcp_reg(pid, hash, len)) { - log_err("Could not register " HASH_FMT " with IPCP %d.", - HASH_VAL(hash), pid); - free(hash); - return -1; + assert(err == 0); + + if (reg_get_name_for_flow_id(name, flow->id) < 0) { + log_err("Failed to get name for flow %d.", flow->id); + err = -EIPCP; + goto fail_oap; } - log_info("Registered %s with IPCP %d as " HASH_FMT ".", - name, pid, HASH_VAL(hash)); + if (reg_get_name_info(name, &info) < 0) { + log_err("Failed to get name info for %s.", name); + err = -ENAME; + goto fail_oap; + } + + log_dbg("IPCP %d accepting flow %d for %s.", + flow->n_pid, flow->id, name); + + flow->uid = reg_get_proc_uid(flow->n_pid); + + err = oap_srv_process(&info, req_hdr, &resp_hdr, data, sk, + false, NULL, &peer_crt); + if (err == -EREPLAY) { + log_warn("Dropping replayed alloc request for %s.", name); + goto fail_replay; + } + if (err < 0) { + log_err("OAP processing failed for %s.", name); + goto fail_oap; + } + + if (reg_flow_is_direct(flow->id)) { + if (reg_respond_flow_direct(flow->id, &resp_hdr) < 0) { + log_err("Failed to respond to direct flow."); + goto fail_resp; + } + if (sk->nid != NID_undef) + reg_flow_set_rekey(flow->id, false, peer_crt); + log_info("Flow %d accepted (direct) by %d for %s.", + flow->id, flow->n_pid, name); + } else if (ipcp_flow_alloc_resp(flow, 0, resp_hdr) < 0) { + log_err("Failed to respond to flow allocation."); + goto fail_resp; + } else { + if (sk->nid != NID_undef) + reg_flow_set_rekey(flow->id, false, peer_crt); + log_info("Flow %d accepted by %d for %s (uid %d).", + flow->id, flow->n_pid, name, flow->uid); + } - free(hash); + freebuf(peer_crt); + freebuf(req_hdr); + freebuf(resp_hdr); return 0; -fail: - pthread_rwlock_unlock(&irmd.reg_lock); + fail_oap: + if (!reg_flow_is_direct(flow->id)) + ipcp_flow_alloc_resp(flow, err, resp_hdr); + fail_replay: + freebuf(peer_crt); + freebuf(req_hdr); + freebuf(resp_hdr); + fail_wait: + reg_destroy_flow(flow->id); + fail_flow: return err; + + fail_resp: + flow->state = FLOW_NULL; + freebuf(peer_crt); + freebuf(req_hdr); + freebuf(resp_hdr); + reg_destroy_flow(flow->id); + return -EIPCP; } -static int name_unreg(const char * name, - pid_t pid) +static int flow_join(struct flow_info * flow, + const char * dst, + struct timespec * abstime) { - struct ipcp_entry * ipcp; - int err; - uint8_t * hash; - size_t len; + struct ipcp_info ipcp; + struct layer_info layer; + buffer_t hash; + buffer_t pbuf = BUF_INIT; /* nothing to piggyback */ + int err; - assert(name); + if (reg_create_flow(flow) < 0) { + log_err("Failed to create flow."); + err = -EBADF; + goto fail_flow; + } + + flow->uid = reg_get_proc_uid(flow->n_pid); - pthread_rwlock_wrlock(&irmd.reg_lock); + log_info("Allocating flow for %d to %s (uid %d).", + flow->n_pid, dst, flow->uid); - ipcp = get_ipcp_entry_by_pid(pid); - if (ipcp == NULL) { + strcpy(layer.name, dst); + if (reg_get_ipcp_by_layer(&ipcp, &layer) < 0) { + log_err("Failed to get IPCP for layer %s.", dst); err = -EIPCP; - goto fail; + goto fail_ipcp; } - if (ipcp->layer == NULL) { - err = -EPERM; - goto fail; + flow->n_1_pid = ipcp.pid; + + hash.len = hash_len((enum hash_algo) layer.dir_hash_algo); + hash.data = malloc(hash.len); + if (hash.data == NULL) { + log_err("Failed to malloc hash buffer."); + err = -ENOMEM; + goto fail_ipcp; } - len = IPCP_HASH_LEN(ipcp); + str_hash((enum hash_algo) layer.dir_hash_algo, hash.data, dst); - hash = malloc(len); - if (hash == NULL) { - err = -ENOMEM; - goto fail; + reg_prepare_flow_alloc(flow); + + if (ipcp_flow_join(flow, hash)) { + log_err("Flow join with layer %s failed.", dst); + err = -ENOTALLOC; + goto fail_alloc; } - str_hash(ipcp->dir_hash_algo, hash, name); + pthread_cleanup_push(__cleanup_flow, flow); + pthread_cleanup_push(free, hash.data); - pthread_rwlock_unlock(&irmd.reg_lock); + err = reg_wait_flow_allocated(flow, &pbuf, abstime); - if (ipcp_unreg(pid, hash, len)) { - log_err("Could not unregister %s with IPCP %d.", name, pid); - free(hash); - return -1; + pthread_cleanup_pop(false); + pthread_cleanup_pop(false); + + if (err == -ETIMEDOUT) { + log_err("Flow join timed out."); + goto fail_alloc; } - log_info("Unregistered %s from %d.", name, pid); + if (err == -1) { + log_dbg("Flow join terminated."); + err = -EPIPE; + goto fail_alloc; + } + + assert(pbuf.data == NULL && pbuf.len == 0); + assert(err == 0); - free(hash); + freebuf(hash); return 0; - fail: - pthread_rwlock_unlock(&irmd.reg_lock); + fail_alloc: + freebuf(hash); + fail_ipcp: + reg_destroy_flow(flow->id); + fail_flow: return err; } -static int proc_announce(pid_t pid, - char * prog) +static int get_ipcp_by_dst(const char * dst, + pid_t * pid, + buffer_t * hash) { - struct proc_entry * e; - struct prog_entry * a; - char * prog_dup; + ipcp_list_msg_t ** ipcps = NULL; + int n; + int i; + int err = -EIPCP; - assert(prog); + n = reg_list_ipcps(&ipcps); - prog_dup = strdup(prog); - if (prog_dup == NULL) - return -ENOMEM; + /* Clean up the ipcp_msgs in this loop */ + for (i = 0; i < n; ++i) { + enum hash_algo algo; + enum ipcp_type type; + pid_t tmp; + bool enrolled; - e = proc_entry_create(pid, prog_dup); - if (e == NULL) { - free(prog_dup); - return -ENOMEM; - } + type = ipcps[i]->type; + algo = ipcps[i]->hash_algo; + tmp = ipcps[i]->pid; - pthread_rwlock_wrlock(&irmd.reg_lock); + enrolled = strcmp(ipcps[i]->layer, "Not enrolled.") != 0; - proc_table_add(&irmd.proc_table, e); + ipcp_list_msg__free_unpacked(ipcps[i], NULL); - /* Copy listen names from program if it exists. */ - a = prog_table_get(&irmd.prog_table, e->prog); - if (a != NULL) { - struct list_head * p; - list_for_each(p, &a->names) { - struct str_el * s = list_entry(p, struct str_el, next); - struct str_el * n = malloc(sizeof(*n)); - if (n == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - return -ENOMEM; - } + if (type == IPCP_BROADCAST) + continue; - n->str = strdup(s->str); - if (n->str == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - free(n); - return -ENOMEM; - } + if (err == 0 /* solution found */ || !enrolled) + continue; - list_add(&n->next, &e->names); - log_dbg("Process %d inherits name %s from program %s.", - pid, n->str, e->prog); + hash->len = hash_len(algo); + hash->data = malloc(hash->len); + if (hash->data == NULL) { + log_warn("Failed to malloc hash for query."); + err = -ENOMEM; + continue; } + + str_hash(algo, hash->data, dst); + + if (ipcp_query(tmp, *hash) < 0) { + freebuf(*hash); + continue; + } + + *pid = tmp; + + err = 0; } - pthread_rwlock_unlock(&irmd.reg_lock); + free(ipcps); - return 0; + return err; } -static int flow_accept(pid_t pid, - struct timespec * timeo, - struct irm_flow * f_out, - const void * data, - size_t len) +static int wait_for_accept(const char * name) { - struct irm_flow * f = NULL; - struct proc_entry * pe = NULL; - struct reg_entry * re = NULL; - struct list_head * p = NULL; + struct timespec timeo = TIMESPEC_INIT_MS(IRMD_REQ_ARR_TIMEOUT); + struct timespec abstime; + char ** exec; + int ret; + + clock_gettime(PTHREAD_COND_CLOCK, &abstime); + ts_add(&abstime, &timeo, &abstime); + + ret = reg_wait_flow_accepting(name, &abstime); + if (ret == -ETIMEDOUT) { + if (reg_get_exec(name, &exec) < 0) { + log_dbg("No program bound for %s.", name); + goto fail; + } - pid_t pid_n; - pid_t pid_n_1; - int flow_id; - int ret; + if (spawn_program(exec) < 0) { + log_err("Failed to start %s for %s.", exec[0], name); + goto fail_spawn; + } - pthread_rwlock_wrlock(&irmd.reg_lock); + log_info("Starting %s for %s.", exec[0], name); - pe = proc_table_get(&irmd.proc_table, pid); - if (pe == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - log_err("Unknown process %d calling accept.", pid); - return -EINVAL; - } + ts_add(&abstime, &timeo, &abstime); - log_dbg("New instance (%d) of %s added.", pid, pe->prog); - log_dbg("This process accepts flows for:"); + ret = reg_wait_flow_accepting(name, &abstime); + if (ret == -ETIMEDOUT) + goto fail_spawn; - list_for_each(p, &pe->names) { - struct str_el * s = list_entry(p, struct str_el, next); - log_dbg(" %s", s->str); - re = registry_get_entry(&irmd.registry, s->str); - if (re != NULL) - reg_entry_add_pid(re, pid); + argvfree(exec); } - pthread_rwlock_unlock(&irmd.reg_lock); + return ret; - ret = proc_entry_sleep(pe, timeo); - if (ret == -ETIMEDOUT) - return -ETIMEDOUT; + fail_spawn: + argvfree(exec); + fail: + return -1; +} - if (ret == -1) - return -EPIPE; +static int flow_req_arr(struct flow_info * flow, + const uint8_t * hash, + buffer_t * data) +{ + struct ipcp_info info; + struct layer_info layer; + enum hash_algo algo; + int ret; + char name[NAME_SIZE + 1]; - if (irmd_get_state() != IRMD_RUNNING) - return -EIRMD; + info.pid = flow->n_1_pid; - pthread_rwlock_rdlock(&irmd.flows_lock); + log_dbg("Flow req arrived from IPCP %d for " HASH_FMT32 ".", + info.pid, HASH_VAL32(hash)); - f = get_irm_flow_n(pid); - if (f == NULL) { - pthread_rwlock_unlock(&irmd.flows_lock); - log_warn("Port_id was not created yet."); - return -EPERM; + if (reg_get_ipcp(&info, &layer) < 0) { + log_err("No IPCP with pid %d.", info.pid); + ret = -EIPCP; + goto fail; } - pid_n = f->n_pid; - pid_n_1 = f->n_1_pid; - flow_id = f->flow_id; - - pthread_rwlock_unlock(&irmd.flows_lock); - pthread_rwlock_rdlock(&irmd.reg_lock); - - pe = proc_table_get(&irmd.proc_table, pid); - if (pe == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - pthread_rwlock_wrlock(&irmd.flows_lock); - list_del(&f->next); - bmp_release(irmd.flow_ids, f->flow_id); - pthread_rwlock_unlock(&irmd.flows_lock); - ipcp_flow_alloc_resp(pid_n_1, flow_id, pid_n, -1, NULL, 0); - clear_irm_flow(f); - irm_flow_set_state(f, FLOW_NULL); - irm_flow_destroy(f); - log_dbg("Process gone while accepting flow."); - return -EPERM; + algo = (enum hash_algo) layer.dir_hash_algo; + + if (reg_get_name_for_hash(name, algo, hash) < 0) { + log_warn("No name for " HASH_FMT32 ".", HASH_VAL32(hash)); + ret = -ENAME; + goto fail; } - pthread_mutex_lock(&pe->lock); + log_info("Flow request arrived for %s.", name); - re = pe->re; + ret = wait_for_accept(name); + if (ret < 0) { + log_err("No active process for %s.", name); + goto fail; + } - pthread_mutex_unlock(&pe->lock); + flow->id = ret; + flow->state = FLOW_ALLOCATED; - if (reg_entry_get_state(re) != REG_NAME_FLOW_ARRIVED) { - pthread_rwlock_unlock(&irmd.reg_lock); - pthread_rwlock_wrlock(&irmd.flows_lock); - list_del(&f->next); - bmp_release(irmd.flow_ids, f->flow_id); - pthread_rwlock_unlock(&irmd.flows_lock); - ipcp_flow_alloc_resp(pid_n_1, flow_id, pid_n, -1, NULL, 0); - clear_irm_flow(f); - irm_flow_set_state(f, FLOW_NULL); - irm_flow_destroy(f); - log_err("Entry in wrong state."); - return -EPERM; + ret = reg_respond_accept(flow, data); + if (ret < 0) { + log_err("Failed to respond to flow %d.", flow->id); + goto fail; } - registry_del_process(&irmd.registry, pid); + return 0; + fail: + return ret; +} - pthread_rwlock_unlock(&irmd.reg_lock); +#ifndef DISABLE_DIRECT_IPC +static int flow_alloc_direct(const char * dst, + struct flow_info * flow, + buffer_t * data, + struct timespec * abstime, + struct crypt_sk * sk, + struct name_info * info) +{ + struct flow_info acc; /* server side flow */ + buffer_t req_hdr = BUF_INIT; + buffer_t resp_hdr = BUF_INIT; + buffer_t no_crt = BUF_INIT; + void * ctx; + int err; - pthread_rwlock_wrlock(&irmd.flows_lock); + acc.id = wait_for_accept(dst); + if (acc.id < 0) { + log_dbg("No accepting process for %s.", dst); + return -EAGAIN; + } - f_out->flow_id = f->flow_id; - f_out->n_pid = f->n_pid; - f_out->n_1_pid = f->n_1_pid; - f_out->qs = f->qs; - f_out->mpl = f->mpl; - f_out->data = f->data; /* pass owner */ - f_out->len = f->len; + if (oap_cli_prepare(&ctx, info, &req_hdr, *data, false) < 0) { + log_err("Failed to prepare OAP for %s.", dst); + return -EBADF; + } - f->data = NULL; - f->len = 0; + acc.n_1_pid = flow->n_pid; + acc.mpl = DIRECT_MPL; + acc.mtu = DIRECT_MTU; + acc.qs = flow->qs; + acc.state = FLOW_ALLOCATED; - pthread_rwlock_unlock(&irmd.flows_lock); + err = reg_prepare_flow_direct(&acc, &req_hdr, flow->uid); + if (err == -EPERM) { + log_dbg("UID mismatch, falling back."); + oap_ctx_free(ctx); + freebuf(req_hdr); + return -EPERM; + } - if (f->qs.cypher_s == 0) { /* no crypto requested, don't send pubkey */ - data = NULL; - len = 0; + if (err < 0) { + log_err("Failed to prepare direct flow."); + oap_ctx_free(ctx); + freebuf(req_hdr); + return -EBADF; } - if (ipcp_flow_alloc_resp(pid_n_1, flow_id, pid_n, 0, data, len)) { - pthread_rwlock_wrlock(&irmd.flows_lock); - list_del(&f->next); - pthread_rwlock_unlock(&irmd.flows_lock); - log_dbg("Failed to respond to alloc. Port_id invalidated."); - clear_irm_flow(f); - irm_flow_set_state(f, FLOW_NULL); - irm_flow_destroy(f); - return -EPERM; + err = reg_wait_flow_direct(acc.id, &resp_hdr, abstime); + if (err < 0) { + log_err("Timeout waiting for OAP response."); + oap_ctx_free(ctx); + return -ETIMEDOUT; + } + + err = oap_cli_complete(ctx, info, resp_hdr, data, sk, NULL, NULL); + if (err < 0) { + log_err("OAP completion failed for %s.", dst); + freebuf(resp_hdr); + return err; } - irm_flow_set_state(f, FLOW_ALLOCATED); + flow->id = acc.id; + flow->n_1_pid = acc.n_pid; + flow->mpl = DIRECT_MPL; + flow->mtu = DIRECT_MTU; + flow->state = FLOW_ALLOCATED; + + /* Mark encrypted for re-key; the acceptor caches the cert. */ + if (sk->nid != NID_undef) + reg_flow_set_rekey(acc.id, true, no_crt); - log_info("Flow on flow_id %d allocated.", f->flow_id); + log_info("Flow %d allocated (direct) for %d to %s.", + flow->id, flow->n_pid, dst); + + freebuf(resp_hdr); return 0; } +#endif /* DISABLE_DIRECT_IPC */ -static int flow_alloc(pid_t pid, - const char * dst, - qosspec_t qs, - struct timespec * timeo, - struct irm_flow * f_out, - bool join, - const void * data, - size_t len) +static int flow_alloc(const char * dst, + struct flow_info * flow, + buffer_t * data, + struct timespec * abstime, + struct crypt_sk * sk) { - struct irm_flow * f; - struct ipcp_entry * ipcp; - int flow_id; - int state; - uint8_t * hash; + buffer_t req_hdr = BUF_INIT; + buffer_t resp_hdr = BUF_INIT; + buffer_t hash = BUF_INIT; + buffer_t peer_crt = BUF_INIT; + struct name_info info; + void * ctx; + int err; + + /* piggyback of user data not yet implemented */ + assert(data != NULL && BUF_IS_EMPTY(data)); + + /* Look up name_info for dst */ + if (reg_get_name_info(dst, &info) < 0) { + log_err("Failed to get name info for %s.", dst); + err = -ENAME; + goto fail_flow; + } - log_info("Allocating flow for %d to %s.\n", - pid, dst); + flow->uid = reg_get_proc_uid(flow->n_pid); - ipcp = join ? get_ipcp_entry_by_layer(dst) - : get_ipcp_by_dst_name(dst, pid); - if (ipcp == NULL) { - log_info("Destination %s unreachable.", dst); - return -1; + log_info("Allocating flow for %d to %s (uid %d).", + flow->n_pid, dst, flow->uid); + +#ifndef DISABLE_DIRECT_IPC + err = flow_alloc_direct(dst, flow, data, abstime, sk, &info); + if (err == 0) + return 0; + + if (err != -EPERM && err != -EAGAIN) + goto fail_flow; +#endif + if (reg_create_flow(flow) < 0) { + log_err("Failed to create flow."); + err = -EBADF; + goto fail_flow; } - pthread_rwlock_wrlock(&irmd.flows_lock); + reg_set_name_for_flow_id(dst, flow->id); - flow_id = bmp_allocate(irmd.flow_ids); - if (!bmp_is_id_valid(irmd.flow_ids, flow_id)) { - pthread_rwlock_unlock(&irmd.flows_lock); - log_err("Could not allocate flow_id."); - return -EBADF; + if (get_ipcp_by_dst(dst, &flow->n_1_pid, &hash) < 0) { + log_err("Failed to find IPCP for %s.", dst); + err = -EIPCP; + goto fail_ipcp; } - f = irm_flow_create(pid, ipcp->pid, flow_id, qs); - if (f == NULL) { - bmp_release(irmd.flow_ids, flow_id); - pthread_rwlock_unlock(&irmd.flows_lock); - log_err("Could not allocate flow_id."); - return -ENOMEM; + if (reg_prepare_flow_alloc(flow) < 0) { + log_err("Failed to prepare flow allocation."); + err = -EBADF; + goto fail_prepare; } - list_add(&f->next, &irmd.irm_flows); + if (oap_cli_prepare(&ctx, &info, &req_hdr, *data, false) < 0) { + log_err("Failed to prepare OAP request for %s.", dst); + err = -EBADF; + goto fail_prepare; + } - pthread_rwlock_unlock(&irmd.flows_lock); + if (ipcp_flow_alloc(flow, hash, req_hdr)) { + log_err("Flow allocation %d failed.", flow->id); + err = -EIPCP; + goto fail_alloc; + } - assert(irm_flow_get_state(f) == FLOW_ALLOC_PENDING); + pthread_cleanup_push(__cleanup_flow, flow); + pthread_cleanup_push(free, hash.data); - hash = malloc(IPCP_HASH_LEN(ipcp)); - if (hash == NULL) - /* sanitizer cleans this */ - return -ENOMEM; + err = reg_wait_flow_allocated(flow, &resp_hdr, abstime); - str_hash(ipcp->dir_hash_algo, hash, dst); + pthread_cleanup_pop(false); + pthread_cleanup_pop(false); - if (join) { - if (ipcp_flow_join(ipcp->pid, flow_id, pid, hash, - IPCP_HASH_LEN(ipcp), qs)) { - /* sanitizer cleans this */ - log_info("Flow_join failed."); - free(hash); - return -EAGAIN; - } - } else { - if (ipcp_flow_alloc(ipcp->pid, flow_id, pid, hash, - IPCP_HASH_LEN(ipcp), qs, data, len)) { - /* sanitizer cleans this */ - log_info("Flow_allocation failed."); - free(hash); - return -EAGAIN; - } + if (err == -ETIMEDOUT) { + log_err("Flow allocation timed out."); + goto fail_wait; } - free(hash); + log_dbg("Response for flow %d to %s.", flow->id, dst); - state = irm_flow_wait_state(f, FLOW_ALLOCATED, timeo); - if (state != FLOW_ALLOCATED) { - if (state == -ETIMEDOUT) { - log_dbg("Flow allocation timed out"); - return -ETIMEDOUT; - } + if (err < 0) { + log_warn("Allocation rejected: %s (%d).", dst, err); + goto fail_peer; + } - log_info("Pending flow to %s torn down.", dst); - return -EPIPE; + err = oap_cli_complete(ctx, &info, resp_hdr, data, sk, NULL, &peer_crt); + if (err < 0) { + log_err("OAP completion failed for %s.", dst); + goto fail_complete; } - pthread_rwlock_wrlock(&irmd.flows_lock); + if (sk->nid != NID_undef) + reg_flow_set_rekey(flow->id, true, peer_crt); - assert(irm_flow_get_state(f) == FLOW_ALLOCATED); + freebuf(peer_crt); + freebuf(req_hdr); + freebuf(resp_hdr); + freebuf(hash); - f_out->flow_id = f->flow_id; - f_out->n_pid = f->n_pid; - f_out->n_1_pid = f->n_1_pid; - f_out->data = f->data; /* pass owner */ - f_out->len = f->len; - f_out->mpl = f->mpl; - f->data = NULL; - f->len = 0; + return 0; + + fail_complete: + freebuf(peer_crt); + ctx = NULL; /* free'd on complete */ + fail_peer: + flow->state = FLOW_DEALLOCATED; + fail_wait: + freebuf(resp_hdr); + fail_alloc: + freebuf(req_hdr); + oap_ctx_free(ctx); + fail_prepare: + freebuf(hash); + fail_ipcp: + reg_destroy_flow(flow->id); + fail_flow: + return err; +} - pthread_rwlock_unlock(&irmd.flows_lock); +static int flow_alloc_reply(struct flow_info * flow, + int response, + buffer_t * data) +{ + flow->state = response != 0 ? FLOW_DEALLOCATED : FLOW_ALLOCATED; - log_info("Flow on flow_id %d allocated.", flow_id); + if (reg_respond_alloc(flow, data, response) < 0) { + log_err("Failed to reply to flow %d.", flow->id); + flow->state = FLOW_DEALLOCATED; + return -EBADF; + } return 0; } -static int flow_dealloc(pid_t pid, - int flow_id, - time_t timeo) +static int flow_dealloc(struct flow_info * flow, + struct timespec * ts) { - pid_t n_1_pid = -1; - int ret = 0; + log_info("Deallocating flow %d for process %d (timeout: %ld s).", + flow->id, flow->n_pid, ts->tv_sec); - struct irm_flow * f = NULL; + reg_dealloc_flow(flow); - pthread_rwlock_wrlock(&irmd.flows_lock); - - f = get_irm_flow(flow_id); - if (f == NULL) { - pthread_rwlock_unlock(&irmd.flows_lock); - log_dbg("Deallocate unknown port %d by %d.", flow_id, pid); + if (reg_flow_is_direct(flow->id)) { + if (flow->state == FLOW_DEALLOCATED) + reg_destroy_flow(flow->id); return 0; } - if (pid == f->n_pid) { - f->n_pid = -1; - n_1_pid = f->n_1_pid; - } else if (pid == f->n_1_pid) { - f->n_1_pid = -1; - } else { - pthread_rwlock_unlock(&irmd.flows_lock); - log_dbg("Dealloc called by wrong process."); - return -EPERM; + if (ipcp_flow_dealloc(flow->n_1_pid, flow->id, ts->tv_sec) < 0) { + log_err("Failed to request dealloc from %d.", flow->n_1_pid); + return -EIPCP; } - if (irm_flow_get_state(f) == FLOW_DEALLOC_PENDING) { - list_del(&f->next); - if ((kill(f->n_pid, 0) < 0 && f->n_1_pid == -1) || - (kill(f->n_1_pid, 0) < 0 && f->n_pid == -1)) - irm_flow_set_state(f, FLOW_NULL); - clear_irm_flow(f); - irm_flow_destroy(f); - bmp_release(irmd.flow_ids, flow_id); - log_info("Completed deallocation of flow_id %d by process %d.", - flow_id, pid); - } else { - irm_flow_set_state(f, FLOW_DEALLOC_PENDING); - log_dbg("Partial deallocation of flow_id %d by process %d.", - flow_id, pid); + return 0; +} + +static int flow_dealloc_resp(struct flow_info * flow) +{ + reg_dealloc_flow_resp(flow); + + assert(flow->state == FLOW_DEALLOCATED); + + reg_destroy_flow(flow->id); + + log_info("Completed deallocation of flow_id %d by process %d.", + flow->id, flow->n_1_pid); + + return 0; +} + +/* + * Inbox producers. Any thread may post; the worker drains. INIT carries + * the flow's lower IPCP pid; RESP transfers ownership of buf. + */ +static void rekey_post(enum rekey_evt_type type, + int flow_id, + pid_t n_1_pid, + buffer_t * buf) +{ + struct rekey_evt * evt; + + evt = malloc(sizeof(*evt)); + if (evt == NULL) { + log_err("Failed to malloc re-key event for flow %d.", flow_id); + if (type == REKEY_INIT || type == REKEY_DIRECT) + reg_flow_clear_in_flight(flow_id); + else + reg_flow_rekey_arr_done(flow_id, type == REKEY_REQ); + + if (buf != NULL) + freebuf(*buf); + + return; } - pthread_rwlock_unlock(&irmd.flows_lock); + list_head_init(&evt->next); + evt->type = type; + evt->flow_id = flow_id; + evt->n_1_pid = n_1_pid; + clrbuf(evt->buf); + if (buf != NULL) { + evt->buf = *buf; + clrbuf(*buf); + } - if (n_1_pid != -1) - ret = ipcp_flow_dealloc(n_1_pid, flow_id, timeo); + pthread_mutex_lock(&irmd.rk.mtx); - return ret; + list_add_tail(&evt->next, &irmd.rk.inbox); + pthread_cond_signal(&irmd.rk.cond); + + pthread_mutex_unlock(&irmd.rk.mtx); } -static pid_t auto_execute(char ** argv) +static void rekey_post_init(int flow_id, + pid_t n_1_pid) { - pid_t pid; - struct stat s; + rekey_post(REKEY_INIT, flow_id, n_1_pid, NULL); +} - if (stat(argv[0], &s) != 0) { - log_warn("Program %s does not exist.", argv[0]); - return -1; +static void rekey_post_resp(int flow_id, + buffer_t * buf) +{ + rekey_post(REKEY_RESP, flow_id, 0, buf); +} + +static void rekey_post_req(int flow_id, + pid_t n_1_pid, + buffer_t * buf) +{ + rekey_post(REKEY_REQ, flow_id, n_1_pid, buf); +} + +static void rekey_post_direct(int flow_id) +{ + rekey_post(REKEY_DIRECT, flow_id, 0, NULL); +} + +/* Worker-only: find an in-flight entry by flow_id. */ +static struct rekey_ctx * rekey_find(struct list_head * tbl, + int flow_id) +{ + struct list_head * p; + + list_for_each(p, tbl) { + struct rekey_ctx * e = list_entry(p, struct rekey_ctx, next); + if (e->flow_id == flow_id) + return e; } - if (!(s.st_mode & S_IXUSR)) { - log_warn("Program %s is not executable.", argv[0]); + return NULL; +} + +/* Worker-only: drop an entry, freeing its OAP ctx. */ +static void rekey_drop(struct rekey_ctx * e) +{ + if (e->ctx != NULL) + oap_ctx_free(e->ctx); + + list_del(&e->next); + free(e); +} + +/* Resolve a flow's registered name info; < 0 if the flow or name is gone. */ +static int rekey_name_info(int flow_id, + struct name_info * info) +{ + char name[NAME_SIZE + 1]; + + if (reg_get_name_for_flow_id(name, flow_id) < 0) return -1; + + return reg_get_name_info(name, info); +} + +/* Flow-update relay payload: a 1-byte type prefix on an opaque body. */ +enum flow_upd_type { + FLOW_UPD_REKEY_REQ = 0, + FLOW_UPD_REKEY_RESP = 1, +}; + +/* Prepend the update type to body; caller frees out on success. */ +static int flow_upd_wrap(buffer_t * out, + uint8_t type, + const buffer_t * body) +{ + out->len = body->len + 1; + out->data = malloc(out->len); + if (out->data == NULL) + return -ENOMEM; + + out->data[0] = type; + memcpy(out->data + 1, body->data, body->len); + + return 0; +} + +/* Cleanup handlers — the re-key worker is cancelled at shutdown. */ +static void rk_free_evt(void * o) +{ + struct rekey_evt * evt = o; + + freebuf(evt->buf); + free(evt); +} + +static void rk_freebuf(void * o) +{ + freebuf(*(buffer_t *) o); +} + +static void rk_clear_in_flight(void * o) +{ + reg_flow_clear_in_flight(*(int *) o); +} + +static void rk_clear_key(void * o) +{ + crypt_secure_clear(o, SYMMKEYSZ); +} + +static void rekey_do_initiate(struct list_head * tbl, + int flow_id, + pid_t n_1_pid) +{ + struct rekey_ctx * e; + struct flow_info info; + struct name_info name; + buffer_t req = BUF_INIT; + buffer_t upd = BUF_INIT; + buffer_t data = BUF_INIT; + void * ctx = NULL; + int ret; + + e = rekey_find(tbl, flow_id); + if (e != NULL) + rekey_drop(e); /* Replace in-flight entries */ + + if (rekey_name_info(flow_id, &name) < 0) { + log_err("Failed to get name info to re-key flow %d.", flow_id); + goto fail; } - if (posix_spawn(&pid, argv[0], NULL, NULL, argv, NULL)) { - log_err("Failed to spawn new process"); - return -1; + if (oap_cli_prepare(&ctx, &name, &req, data, true) < 0) { + log_err("Failed to prepare re-key for flow %d.", flow_id); + goto fail; } - log_info("Instantiated %s as process %d.", argv[0], pid); + memset(&info, 0, sizeof(info)); + info.id = flow_id; + info.n_1_pid = n_1_pid; - return pid; + if (flow_upd_wrap(&upd, FLOW_UPD_REKEY_REQ, &req) < 0) { + log_err("Failed to wrap re-key request for flow %d.", flow_id); + goto fail_ctx; + } + + pthread_cleanup_push(rk_clear_in_flight, &flow_id); + pthread_cleanup_push(oap_ctx_free, ctx); + pthread_cleanup_push(rk_freebuf, &req); + pthread_cleanup_push(rk_freebuf, &upd); + ret = ipcp_flow_update(&info, upd); + pthread_cleanup_pop(false); + pthread_cleanup_pop(false); + pthread_cleanup_pop(false); + pthread_cleanup_pop(false); + freebuf(upd); + if (ret < 0) { + log_err("Failed to send re-key request for flow %d.", flow_id); + goto fail_ctx; + } + + e = malloc(sizeof(*e)); + if (e == NULL) { + log_err("Failed to malloc re-key ctx for flow %d.", flow_id); + goto fail_ctx; + } + + list_head_init(&e->next); + e->flow_id = flow_id; + e->ctx = ctx; + clock_gettime(PTHREAD_COND_CLOCK, &e->deadline); + e->deadline.tv_sec += REKEY_RESP_TIMEO; + + list_add(&e->next, tbl); + + log_dbg("Re-key request sent for flow %d.", flow_id); + + freebuf(req); + + return; + + fail_ctx: + oap_ctx_free(ctx); + freebuf(req); + fail: + reg_flow_clear_in_flight(flow_id); } -static int flow_req_arr(pid_t pid, - struct irm_flow * f_out, - const uint8_t * hash, - time_t mpl, - qosspec_t qs, - const void * data, - size_t len) +/* Worker-only: complete the exchange, install the pending seed. */ +static void rekey_do_complete(struct list_head * tbl, + int flow_id, + buffer_t buf) { - struct reg_entry * re = NULL; - struct prog_entry * a = NULL; - struct proc_entry * pe = NULL; - struct irm_flow * f = NULL; + struct rekey_ctx * e; + struct name_info info; + struct crypt_sk sk; + uint8_t kbuf[SYMMKEYSZ]; + buffer_t data = BUF_INIT; + buffer_t crt = BUF_INIT; + uint8_t newgen; + + e = rekey_find(tbl, flow_id); + if (e == NULL) { + log_dbg("Stale re-key RESPONSE for flow %d.", flow_id); + return; + } - struct pid_el * c_pid; - struct ipcp_entry * ipcp; - pid_t h_pid; - int flow_id; + /* A concurrent responder already parked a seed; don't overwrite. */ + if (reg_flow_rekey_pending(flow_id)) { + log_dbg("Re-key already pending for flow %d.", flow_id); + goto finish; + } - struct timespec wt = {IRMD_REQ_ARR_TIMEOUT / 1000, - (IRMD_REQ_ARR_TIMEOUT % 1000) * MILLION}; + if (rekey_name_info(flow_id, &info) < 0) { + log_err("Failed to get name info to re-key flow %d.", flow_id); + goto finish; + } - log_dbg("Flow req arrived from IPCP %d for " HASH_FMT ".", - pid, HASH_VAL(hash)); + sk.key = kbuf; - pthread_rwlock_rdlock(&irmd.reg_lock); + reg_flow_get_peer_crt(flow_id, &crt); - ipcp = get_ipcp_entry_by_pid(pid); - if (ipcp == NULL) { - log_err("IPCP died."); - return -EIPCP; + /* oap_cli_complete frees the ctx on every path. */ + if (oap_cli_complete(e->ctx, &info, buf, &data, &sk, &crt, NULL) < 0) { + log_warn("Failed to complete re-key for flow %d.", flow_id); + e->ctx = NULL; + goto finish_clear; } - re = registry_get_entry_by_hash(&irmd.registry, ipcp->dir_hash_algo, - hash, IPCP_HASH_LEN(ipcp)); - if (re == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - log_err("Unknown hash: " HASH_FMT ".", HASH_VAL(hash)); - return -1; + e->ctx = NULL; + + if (data.len != 1) { + log_warn("Re-key reply malformed for flow %d.", flow_id); + goto finish_clear; } - log_info("Flow request arrived for %s.", re->name); + newgen = *(uint8_t *) data.data; - pthread_rwlock_unlock(&irmd.reg_lock); + if (newgen >= 16) { + log_warn("Re-key gen %u out of range for flow %d.", + newgen, flow_id); + goto finish_clear; + } - /* Give the process a bit of slop time to call accept */ - if (reg_entry_leave_state(re, REG_NAME_IDLE, &wt) == -1) { - log_err("No processes for " HASH_FMT ".", HASH_VAL(hash)); - return -1; + if (reg_flow_store_pending(flow_id, kbuf, newgen, true) < 0) + log_warn("Flow %d gone during re-key.", flow_id); + else + reg_notify_flow(flow_id, FLOW_UPD); + + log_dbg("Re-key completed for flow %d (gen %u).", flow_id, newgen); + + finish_clear: + crypt_secure_clear(kbuf, SYMMKEYSZ); + freebuf(data); + finish: + freebuf(crt); + rekey_drop(e); + reg_flow_clear_in_flight(flow_id); +} + +/* Worker-only: reap entries whose RESPONSE never arrived. */ +static void rekey_reap_expired(struct list_head * tbl) +{ + struct list_head * p; + struct list_head * h; + struct timespec now; + + clock_gettime(PTHREAD_COND_CLOCK, &now); + + list_for_each_safe(p, h, tbl) { + struct rekey_ctx * e = list_entry(p, struct rekey_ctx, next); + if (ts_diff_ns(&e->deadline, &now) > 0) + continue; + + log_warn("Re-key timed out for flow %d.", e->flow_id); + reg_flow_clear_in_flight(e->flow_id); + rekey_drop(e); } +} - pthread_rwlock_wrlock(&irmd.reg_lock); +/* Responder side: process request, install pending seed, send response. */ +static int rekey_respond(struct flow_info * flow, + buffer_t * pk) +{ + struct name_info info; + struct crypt_sk sk; + uint8_t kbuf[SYMMKEYSZ]; + buffer_t rsp = BUF_INIT; + buffer_t upd = BUF_INIT; + buffer_t data = BUF_INIT; + buffer_t crt = BUF_INIT; + uint8_t newgen; + int epoch; + int err; + + epoch = reg_flow_get_epoch(flow->id); + if (epoch < 0) { + log_warn("Re-key for unknown flow %d.", flow->id); + return -EBADF; + } - switch (reg_entry_get_state(re)) { - case REG_NAME_IDLE: - pthread_rwlock_unlock(&irmd.reg_lock); - log_err("No processes for " HASH_FMT ".", HASH_VAL(hash)); - return -1; - case REG_NAME_AUTO_ACCEPT: - c_pid = malloc(sizeof(*c_pid)); - if (c_pid == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - return -1; - } + /* Collision: we are driving our own exchange; let it win. */ + if (reg_flow_rekey_should_yield(flow->id)) { + log_dbg("Yielding to own re-key for flow %d.", flow->id); + return 0; + } - reg_entry_set_state(re, REG_NAME_AUTO_EXEC); - a = prog_table_get(&irmd.prog_table, - reg_entry_get_prog(re)); - - if (a == NULL || (c_pid->pid = auto_execute(a->argv)) < 0) { - reg_entry_set_state(re, REG_NAME_AUTO_ACCEPT); - pthread_rwlock_unlock(&irmd.reg_lock); - log_err("Could not start program for reg_entry %s.", - re->name); - free(c_pid); - return -1; - } + if (rekey_name_info(flow->id, &info) < 0) { + log_err("Failed to get name info to re-key flow %d.", flow->id); + return -ENAME; + } - list_add(&c_pid->next, &irmd.spawned_pids); + if (reg_flow_rekey_pending(flow->id)) { + log_dbg("Duplicate re-key request for flow %d.", flow->id); + return 0; + } - pthread_rwlock_unlock(&irmd.reg_lock); + newgen = (uint8_t) ((epoch + 1) & 0x0F); + data.data = &newgen; + data.len = 1; - if (reg_entry_leave_state(re, REG_NAME_AUTO_EXEC, NULL)) - return -1; + sk.key = kbuf; - pthread_rwlock_wrlock(&irmd.reg_lock); - /* FALLTHRU */ - case REG_NAME_FLOW_ACCEPT: - h_pid = reg_entry_get_pid(re); - if (h_pid == -1) { - pthread_rwlock_unlock(&irmd.reg_lock); - log_err("Invalid process id returned."); - return -1; - } + reg_flow_get_peer_crt(flow->id, &crt); - break; - default: - pthread_rwlock_unlock(&irmd.reg_lock); - log_err("IRMd in wrong state."); - return -1; + err = oap_srv_process(&info, *pk, &rsp, &data, &sk, true, &crt, NULL); + if (err < 0) { + /* data still points to stack newgen; don't free it. */ + log_err("Failed to process re-key OAP for flow %d.", flow->id); + goto finish; } - pthread_rwlock_unlock(&irmd.reg_lock); - pthread_rwlock_wrlock(&irmd.flows_lock); + /* On success oap_srv_process repointed data to client output. */ + freebuf(data); - flow_id = bmp_allocate(irmd.flow_ids); - if (!bmp_is_id_valid(irmd.flow_ids, flow_id)) { - pthread_rwlock_unlock(&irmd.flows_lock); - return -1; + if (reg_flow_store_pending(flow->id, kbuf, newgen, false) < 0) { + log_warn("Flow %d gone during re-key.", flow->id); + err = -EBADF; + goto finish; } - f = irm_flow_create(h_pid, pid, flow_id, qs); - if (f == NULL) { - bmp_release(irmd.flow_ids, flow_id); - pthread_rwlock_unlock(&irmd.flows_lock); - log_err("Could not allocate flow_id."); - return -1; + reg_notify_flow(flow->id, FLOW_UPD); + + if (flow_upd_wrap(&upd, FLOW_UPD_REKEY_RESP, &rsp) == 0) { + pthread_cleanup_push(rk_clear_key, kbuf); + pthread_cleanup_push(rk_freebuf, &rsp); + pthread_cleanup_push(rk_freebuf, &crt); + pthread_cleanup_push(rk_freebuf, &upd); + if (ipcp_flow_update(flow, upd) < 0) + log_err("Failed to send re-key response for flow %d.", + flow->id); + pthread_cleanup_pop(false); + pthread_cleanup_pop(false); + pthread_cleanup_pop(false); + pthread_cleanup_pop(false); + freebuf(upd); } - f->mpl = mpl; + err = 0; + finish: + crypt_secure_clear(kbuf, SYMMKEYSZ); + freebuf(rsp); + freebuf(crt); - if (len != 0) { - assert(data); - f->data = malloc(len); - if (f->data == NULL) { - bmp_release(irmd.flow_ids, flow_id); - pthread_rwlock_unlock(&irmd.flows_lock); - log_err("Could not piggyback data."); - return -1; - } + return err; +} - f->len = len; - if (len > 0) - memcpy(f->data, data, len); +/* + * Worker-only: re-key a direct (loopback) flow, the exchange runs in-process: + * build a client request, then derive the shared seed, and hand the one seed + * to both apps with RB_REKEY. + */ +static void rekey_do_direct(int flow_id) +{ + struct name_info info; + struct crypt_sk sk; + uint8_t kbuf[SYMMKEYSZ]; + buffer_t req = BUF_INIT; + buffer_t rsp = BUF_INIT; + buffer_t data = BUF_INIT; + buffer_t crt = BUF_INIT; + void * ctx = NULL; + uint8_t newgen; + int epoch; + + epoch = reg_flow_get_epoch(flow_id); + if (epoch < 0) { + log_warn("Re-key for unknown flow %d.", flow_id); + reg_flow_clear_in_flight(flow_id); + return; } - list_add(&f->next, &irmd.irm_flows); + if (rekey_name_info(flow_id, &info) < 0) { + log_err("Failed to get name info to re-key flow %d.", flow_id); + reg_flow_clear_in_flight(flow_id); + return; + } - pthread_rwlock_unlock(&irmd.flows_lock); - pthread_rwlock_rdlock(&irmd.reg_lock); + if (oap_cli_prepare(&ctx, &info, &req, data, true) < 0) { + log_err("Failed to prepare re-key for flow %d.", flow_id); + reg_flow_clear_in_flight(flow_id); + return; + } - reg_entry_set_state(re, REG_NAME_FLOW_ARRIVED); + newgen = (uint8_t) ((epoch + 1) & 0x0F); + data.data = &newgen; + data.len = 1; - pe = proc_table_get(&irmd.proc_table, h_pid); - if (pe == NULL) { - pthread_rwlock_unlock(&irmd.reg_lock); - pthread_rwlock_wrlock(&irmd.flows_lock); - clear_irm_flow(f); - bmp_release(irmd.flow_ids, f->flow_id); - list_del(&f->next); - pthread_rwlock_unlock(&irmd.flows_lock); - log_err("Could not get process table entry for %d.", h_pid); - free(f->data); - f->len = 0; - irm_flow_destroy(f); - return -1; + sk.key = kbuf; + + reg_flow_get_peer_crt(flow_id, &crt); + + if (oap_srv_process(&info, req, &rsp, &data, &sk, true, + &crt, NULL) < 0) { + /* data still points to stack newgen; don't free it. */ + log_err("Failed to process re-key OAP for flow %d.", flow_id); + reg_flow_clear_in_flight(flow_id); + goto out; } - proc_entry_wake(pe, re); + /* On success oap_srv_process repointed data to its output. */ + freebuf(data); - pthread_rwlock_unlock(&irmd.reg_lock); + if (reg_flow_store_pending_direct(flow_id, kbuf, newgen) < 0) { + log_warn("Flow %d gone during re-key.", flow_id); + reg_flow_clear_in_flight(flow_id); + goto out; + } - reg_entry_leave_state(re, REG_NAME_FLOW_ARRIVED, NULL); + reg_notify_flow_peers(flow_id, FLOW_UPD); - f_out->flow_id = flow_id; - f_out->n_pid = h_pid; + log_dbg("Re-key completed (direct) for flow %d (gen %u).", + flow_id, newgen); + out: + crypt_secure_clear(kbuf, SYMMKEYSZ); + oap_ctx_free(ctx); + freebuf(req); + freebuf(rsp); + freebuf(crt); +} - return 0; +/* Route one snapshot entry to the wire or in-process re-key path. */ +static void rekey_dispatch(struct list_head * tbl, + const struct rekey_info * ri) +{ + if (ri->direct) + rekey_do_direct(ri->flow_id); + else + rekey_do_initiate(tbl, ri->flow_id, ri->n_1_pid); } -static int flow_alloc_reply(int flow_id, - int response, - time_t mpl, - const void * data, - size_t len) +static int flow_update_arr(struct flow_info * flow, + buffer_t * pk) { - struct irm_flow * f; + uint8_t type; + bool is_req; - pthread_rwlock_wrlock(&irmd.flows_lock); - f = get_irm_flow(flow_id); - if (f == NULL) { - pthread_rwlock_unlock(&irmd.flows_lock); - return -1; + if (pk->len < 1) + return -EINVAL; + + type = pk->data[0]; + + switch (type) { + case FLOW_UPD_REKEY_REQ: + is_req = true; + break; + case FLOW_UPD_REKEY_RESP: + is_req = false; + break; + default: + log_warn("Unknown flow update type %u.", type); + return -EINVAL; } - f->mpl = mpl; + /* Drop floods/spoofs before allocating a worker event. */ + if (!reg_flow_rekey_arr_admit(flow->id, flow->n_1_pid, is_req)) + return 0; + + /* Strip the type byte, keeping the malloc base for hand-off. */ + memmove(pk->data, pk->data + 1, pk->len - 1); + pk->len -= 1; - if (!response) - irm_flow_set_state(f, FLOW_ALLOCATED); + /* Defer to worker; an inline RESP send deadlocks loopback. */ + if (is_req) + rekey_post_req(flow->id, flow->n_1_pid, pk); else - irm_flow_set_state(f, FLOW_NULL); + rekey_post_resp(flow->id, pk); - f->data = malloc(len); - if (f->data == NULL) { - pthread_rwlock_unlock(&irmd.flows_lock); - return -1; + return 0; +} + +static int flow_update(struct flow_info * flow, + uid_t uid, + pid_t cpid, + bool rekey, + struct crypt_sk * sk, + bool * has_key, + bool * initiator) +{ + uint8_t seed[SYMMKEYSZ]; + uint8_t epoch; + int rc; + + *has_key = false; + *initiator = false; + + if (rekey) { + pid_t n_1_pid; + + if (!reg_flow_owned_by(flow->id, uid)) + return -EPERM; + + /* Direct flows re-key in-process; no lower IPCP carrier. */ + if (reg_flow_is_direct(flow->id)) { + if (reg_flow_rekey_begin(flow->id)) + rekey_post_direct(flow->id); + + return 0; + } + + /* Watermark re-key: the app can't know its lower IPCP. */ + n_1_pid = reg_flow_get_n_1_pid(flow->id); + if (n_1_pid <= 0) + return 0; + + /* One exchange per flow; the latch arbitrates collisions. */ + if (reg_flow_rekey_begin(flow->id)) + rekey_post_init(flow->id, n_1_pid); + + return 0; } - if (len > 0) - memcpy(f->data, data, len); + rc = reg_flow_take_pending(flow->id, uid, cpid, seed, &epoch, + initiator); + if (rc == -EPERM) + return -EPERM; + + if (rc != 0) + return 0; - f->len = len; + memcpy(sk->key, seed, SYMMKEYSZ); + sk->epoch = epoch; + *has_key = true; - pthread_rwlock_unlock(&irmd.flows_lock); + crypt_secure_clear(seed, SYMMKEYSZ); + + log_dbg("Delivered re-key seed for flow %d (gen %u).", + flow->id, epoch); return 0; } -static void irm_fini(void) +static void rekey_table_cleanup(void * o) { + struct list_head * tbl = o; struct list_head * p; struct list_head * h; - if (irmd_get_state() != IRMD_NULL) - log_warn("Unsafe destroy."); - - pthread_rwlock_wrlock(&irmd.reg_lock); - - /* Clear the lists. */ - list_for_each_safe(p, h, &irmd.ipcps) { - struct ipcp_entry * e = list_entry(p, struct ipcp_entry, next); - list_del(&e->next); - ipcp_entry_destroy(e); + list_for_each_safe(p, h, tbl) { + struct rekey_ctx * e = list_entry(p, struct rekey_ctx, next); + rekey_drop(e); } +} - list_for_each(p, &irmd.spawned_pids) { - struct pid_el * e = list_entry(p, struct pid_el, next); - if (kill(e->pid, SIGTERM)) - log_dbg("Could not send kill signal to %d.", e->pid); - } +static struct rekey_evt * rekey_event_wait(const struct timespec * dl) +{ + struct rekey_evt * evt = NULL; + int ret = 0; - list_for_each_safe(p, h, &irmd.spawned_pids) { - struct pid_el * e = list_entry(p, struct pid_el, next); - int status; - if (waitpid(e->pid, &status, 0) < 0) - log_dbg("Error waiting for %d to exit.", e->pid); - list_del(&e->next); - registry_del_process(&irmd.registry, e->pid); - free(e); - } + pthread_mutex_lock(&irmd.rk.mtx); + pthread_cleanup_push(__cleanup_mutex_unlock, &irmd.rk.mtx); - list_for_each_safe(p, h, &irmd.prog_table) { - struct prog_entry * e = list_entry(p, struct prog_entry, next); - list_del(&e->next); - prog_entry_destroy(e); - } + while (list_is_empty(&irmd.rk.inbox) && ret != -ETIMEDOUT) + ret = -pthread_cond_timedwait(&irmd.rk.cond, &irmd.rk.mtx, dl); - list_for_each_safe(p, h, &irmd.proc_table) { - struct proc_entry * e = list_entry(p, struct proc_entry, next); - list_del(&e->next); - e->state = PROC_INIT; /* sanitizer already joined */ - proc_entry_destroy(e); + if (!list_is_empty(&irmd.rk.inbox)) { + evt = list_first_entry(&irmd.rk.inbox, struct rekey_evt, next); + list_del(&evt->next); } - registry_destroy(&irmd.registry); + pthread_cleanup_pop(true); - pthread_rwlock_unlock(&irmd.reg_lock); + return evt; +} - close(irmd.sockfd); +static struct timespec rekey_deadline(struct list_head * tbl, + struct timespec next) +{ + struct timespec deadline = next; + struct list_head * p; - if (unlink(IRM_SOCK_PATH)) - log_dbg("Failed to unlink %s.", IRM_SOCK_PATH); + list_for_each(p, tbl) { + struct rekey_ctx * e; + e = list_entry(p, struct rekey_ctx, next); + if (ts_diff_ns(&e->deadline, &deadline) < 0) + deadline = e->deadline; + } + + return deadline; +} - pthread_rwlock_wrlock(&irmd.flows_lock); +static void rekey_handle_evt(struct list_head * tbl, + struct rekey_evt * evt) +{ + struct flow_info rinfo; - if (irmd.flow_ids != NULL) - bmp_destroy(irmd.flow_ids); + pthread_cleanup_push(rk_free_evt, evt); - list_for_each_safe(p, h, &irmd.irm_flows) { - struct irm_flow * f = list_entry(p, struct irm_flow, next); - list_del(&f->next); - irm_flow_destroy(f); + switch (evt->type) { + case REKEY_INIT: + rekey_do_initiate(tbl, evt->flow_id, evt->n_1_pid); + break; + case REKEY_REQ: + memset(&rinfo, 0, sizeof(rinfo)); + rinfo.id = evt->flow_id; + rinfo.n_1_pid = evt->n_1_pid; + rekey_respond(&rinfo, &evt->buf); + reg_flow_rekey_arr_done(evt->flow_id, true); + break; + case REKEY_RESP: + rekey_do_complete(tbl, evt->flow_id, evt->buf); + reg_flow_rekey_arr_done(evt->flow_id, false); + break; + case REKEY_DIRECT: + rekey_do_direct(evt->flow_id); + break; + default: + break; } - pthread_rwlock_unlock(&irmd.flows_lock); + pthread_cleanup_pop(true); +} +/* On the periodic tick, dispatch all flows due for re-keying. */ +static void rekey_run_periodic(struct list_head * tbl, + struct timespec * next) +{ + struct rekey_info snap[REKEY_BATCH]; + struct timespec now; + int n; + int i; - if (irmd.rdrb != NULL) - shm_rdrbuff_destroy(irmd.rdrb); + clock_gettime(PTHREAD_COND_CLOCK, &now); - if (irmd.lf != NULL) - lockfile_destroy(irmd.lf); + if (ts_diff_ns(next, &now) > 0) + return; - pthread_mutex_destroy(&irmd.cmd_lock); - pthread_cond_destroy(&irmd.cmd_cond); - pthread_rwlock_destroy(&irmd.flows_lock); - pthread_rwlock_destroy(&irmd.reg_lock); - pthread_rwlock_destroy(&irmd.state_lock); + n = reg_flow_snapshot_rekey_due(snap, REKEY_BATCH); + for (i = 0; i < n; ++i) + rekey_dispatch(tbl, &snap[i]); -#ifdef HAVE_FUSE - sleep(1); - if (rmdir(FUSE_PREFIX)) - log_warn("Failed to remove " FUSE_PREFIX); -#endif + clock_gettime(PTHREAD_COND_CLOCK, next); + next->tv_sec += OAP_REKEY_TIMER; } -void * irm_sanitize(void * o) +/* + * Single worker owning all in-flight Tier-2 re-keys. It drains the + * inbox, runs the periodic snapshot, and reaps timed-out exchanges. + * The table is touched only here, so it needs no lock. + */ +static void * rekey_worker(void * o) { - struct timespec now; - struct list_head * p = NULL; - struct list_head * h = NULL; - - struct timespec timeout = {IRMD_CLEANUP_TIMER / BILLION, - IRMD_CLEANUP_TIMER % BILLION}; - int s; + struct list_head table; + struct timespec next; (void) o; - while (true) { - if (clock_gettime(CLOCK_MONOTONIC, &now) < 0) - log_warn("Failed to get time."); - - if (irmd_get_state() != IRMD_RUNNING) - return (void *) 0; - - pthread_rwlock_wrlock(&irmd.reg_lock); - pthread_cleanup_push(__cleanup_rwlock_unlock, &irmd.reg_lock); - - list_for_each_safe(p, h, &irmd.spawned_pids) { - struct pid_el * e = list_entry(p, struct pid_el, next); - waitpid(e->pid, &s, WNOHANG); - if (kill(e->pid, 0) >= 0) - continue; - log_dbg("Child process %d died, error %d.", e->pid, s); - list_del(&e->next); - free(e); - } + list_head_init(&table); - list_for_each_safe(p, h, &irmd.proc_table) { - struct proc_entry * e = - list_entry(p, struct proc_entry, next); - if (kill(e->pid, 0) >= 0) - continue; - log_dbg("Dead process removed: %d.", e->pid); - list_del(&e->next); - proc_entry_destroy(e); - } + clock_gettime(PTHREAD_COND_CLOCK, &next); + next.tv_sec += OAP_REKEY_TIMER; - list_for_each_safe(p, h, &irmd.ipcps) { - struct ipcp_entry * e = - list_entry(p, struct ipcp_entry, next); - if (kill(e->pid, 0) >= 0) - continue; - log_dbg("Dead IPCP removed: %d.", e->pid); - list_del(&e->next); - ipcp_entry_destroy(e); - } + pthread_cleanup_push(rekey_table_cleanup, &table); - list_for_each_safe(p, h, &irmd.registry) { - struct list_head * p2; - struct list_head * h2; - struct reg_entry * e = - list_entry(p, struct reg_entry, next); - list_for_each_safe(p2, h2, &e->reg_pids) { - struct pid_el * a = - list_entry(p2, struct pid_el, next); - if (kill(a->pid, 0) >= 0) - continue; - log_dbg("Dead process removed from: %d %s.", - a->pid, e->name); - reg_entry_del_pid_el(e, a); - } - } - - pthread_cleanup_pop(true); + while (true) { + struct rekey_evt * evt; + struct timespec deadline; - pthread_rwlock_wrlock(&irmd.flows_lock); - pthread_cleanup_push(__cleanup_rwlock_unlock, &irmd.flows_lock); - - list_for_each_safe(p, h, &irmd.irm_flows) { - int ipcpi; - int flow_id; - struct irm_flow * f = - list_entry(p, struct irm_flow, next); - - if (irm_flow_get_state(f) == FLOW_ALLOC_PENDING - && ts_diff_ms(&f->t0, &now) > IRMD_FLOW_TIMEOUT) { - log_dbg("Pending flow_id %d timed out.", - f->flow_id); - f->n_pid = -1; - irm_flow_set_state(f, FLOW_DEALLOC_PENDING); - continue; - } + deadline = rekey_deadline(&table, next); - if (kill(f->n_pid, 0) < 0) { - log_dbg("Process %d gone, deallocating " - "flow %d.", - f->n_pid, f->flow_id); - f->n_pid = -1; - irm_flow_set_state(f, FLOW_DEALLOC_PENDING); - ipcpi = f->n_1_pid; - flow_id = f->flow_id; - ipcp_flow_dealloc(ipcpi, flow_id, DEALLOC_TIME); - continue; - } + evt = rekey_event_wait(&deadline); - if (kill(f->n_1_pid, 0) < 0) { - struct shm_flow_set * set; - log_err("IPCP %d gone, flow %d removed.", - f->n_1_pid, f->flow_id); - set = shm_flow_set_open(f->n_pid); - if (set != NULL) - shm_flow_set_destroy(set); - f->n_1_pid = -1; - irm_flow_set_state(f, FLOW_DEALLOC_PENDING); - } - } + if (evt != NULL) + rekey_handle_evt(&table, evt); - pthread_cleanup_pop(true); + rekey_run_periodic(&table, &next); - nanosleep(&timeout, NULL); + rekey_reap_expired(&table); } + + pthread_cleanup_pop(true); + + return (void *) 0; } -__attribute__((no_sanitize_address)) static void * acceptloop(void * o) { int csockfd; (void) o; - while (irmd_get_state() == IRMD_RUNNING) { + while (true) { struct cmd * cmd; csockfd = accept(irmd.sockfd, 0, 0); @@ -2033,40 +2279,321 @@ static void * acceptloop(void * o) return (void *) 0; } -static void free_msg(void * o) +static void __cleanup_irm_msg(void * o) { irm_msg__free_unpacked((irm_msg_t *) o, NULL); } -static void * mainloop(void * o) +static irm_msg_t * do_command_msg(irm_msg_t * msg, + int fd) { - int sfd; - irm_msg_t * msg; - buffer_t buffer; + struct ipcp_config conf; + struct ipcp_info ipcp; + struct flow_info flow; + struct proc_info proc; + struct name_info name; + struct crypt_sk sk; + uint8_t kbuf[SYMMKEYSZ]; /* stack buffer for OAP */ + uint8_t * hbuf = NULL; /* heap copy for response */ + struct timespec * abstime; + struct timespec max = TIMESPEC_INIT_MS(FLOW_ALLOC_TIMEOUT); + struct timespec now; + struct timespec ts = TIMESPEC_INIT_S(0); /* static analysis */ + int res; + bool has_key = false; + bool initiator = false; + uid_t uid; + gid_t gid; + pid_t cpid; + irm_msg_t * ret_msg; + buffer_t data; + + memset(&flow, 0, sizeof(flow)); - (void) o; + clock_gettime(PTHREAD_COND_CLOCK, &now); - while (true) { - irm_msg_t * ret_msg; - struct irm_flow e; - struct timespec * timeo = NULL; - struct timespec ts = {0, 0}; - struct cmd * cmd; - int result; + if (msg->timeo != NULL) { + ts = timespec_msg_to_s(msg->timeo); + ts_add(&ts, &now, &ts); + abstime = &ts; + } else { + ts_add(&max, &now, &max); + abstime = NULL; + } + + ret_msg = malloc(sizeof(*ret_msg)); + if (ret_msg == NULL) { + log_err("Failed to malloc return msg."); + return NULL; + } + + irm_msg__init(ret_msg); - memset(&e, 0, sizeof(e)); + ret_msg->code = IRM_MSG_CODE__IRM_REPLY; - ret_msg = malloc(sizeof(*ret_msg)); - if (ret_msg == NULL) - return (void *) -1; + pthread_cleanup_push(__cleanup_irm_msg, ret_msg); - irm_msg__init(ret_msg); + switch (msg->code) { + case IRM_MSG_CODE__IRM_CREATE_IPCP: + ipcp = ipcp_info_msg_to_s(msg->ipcp_info); + res = create_ipcp(&ipcp); + break; + case IRM_MSG_CODE__IPCP_CREATE_R: + ipcp = ipcp_info_msg_to_s(msg->ipcp_info); + res = create_ipcp_r(&ipcp); + break; + case IRM_MSG_CODE__IRM_DESTROY_IPCP: + res = destroy_ipcp(msg->pid); + break; + case IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP: + conf = ipcp_config_msg_to_s(msg->conf); + res = bootstrap_ipcp(msg->pid, &conf); + break; + case IRM_MSG_CODE__IRM_ENROLL_IPCP: + res = enroll_ipcp(msg->pid, msg->dst); + break; + case IRM_MSG_CODE__IRM_CONNECT_IPCP: + flow.qs = qos_spec_msg_to_s(msg->qosspec); + res = connect_ipcp(msg->pid, msg->dst, msg->comp, flow.qs); + break; + case IRM_MSG_CODE__IRM_DISCONNECT_IPCP: + res = disconnect_ipcp(msg->pid, msg->dst, msg->comp); + break; + case IRM_MSG_CODE__IRM_BIND_PROGRAM: + /* Terminate with NULL instead of "" */ + free(msg->exec[msg->n_exec - 1]); + msg->exec[msg->n_exec - 1] = NULL; + res = bind_program(msg->exec, msg->name, msg->opts); + break; + case IRM_MSG_CODE__IRM_UNBIND_PROGRAM: + res = unbind_program(msg->prog, msg->name); + break; + case IRM_MSG_CODE__IRM_PROC_ANNOUNCE: + proc.pid = msg->pid; + strcpy(proc.prog, msg->prog); + res = get_peer_ids(fd, &proc.uid, &proc.gid, NULL); + if (res < 0) + log_err("Failed to get UID/GID for pid %d.", msg->pid); + else + res = proc_announce(&proc); + break; + case IRM_MSG_CODE__IRM_PROC_EXIT: + res = proc_exit(msg->pid); + break; + case IRM_MSG_CODE__IRM_BIND_PROCESS: + res = bind_process(msg->pid, msg->name); + break; + case IRM_MSG_CODE__IRM_UNBIND_PROCESS: + res = unbind_process(msg->pid, msg->name); + break; + case IRM_MSG_CODE__IRM_LIST_IPCPS: + res = list_ipcps(&ret_msg->ipcps, &ret_msg->n_ipcps); + break; + case IRM_MSG_CODE__IRM_CREATE_NAME: + name = name_info_msg_to_s(msg->name_info); + res = name_create(&name); + break; + case IRM_MSG_CODE__IRM_DESTROY_NAME: + res = name_destroy(msg->name); + break; + case IRM_MSG_CODE__IRM_LIST_NAMES: + res = list_names(&ret_msg->names, &ret_msg->n_names); + break; + case IRM_MSG_CODE__IRM_REG_NAME: + res = name_reg(msg->name, msg->pid); + break; + case IRM_MSG_CODE__IRM_UNREG_NAME: + res = name_unreg(msg->name, msg->pid); + break; + case IRM_MSG_CODE__IRM_FLOW_ACCEPT: + tpm_wait_work(irmd.tpm); + data.len = msg->pk.len; + data.data = msg->pk.data; + msg->has_pk = false; + assert(data.len > 0 ? data.data != NULL : data.data == NULL); + flow = flow_info_msg_to_s(msg->flow_info); + sk.key = kbuf; + res = flow_accept(&flow, &data, abstime, &sk); + if (res != 0) + break; + + ret_msg->flow_info = flow_info_s_to_msg(&flow); + ret_msg->has_pk = data.len != 0; + ret_msg->pk.data = data.data; + ret_msg->pk.len = data.len; + ret_msg->has_cipher_nid = true; + ret_msg->cipher_nid = sk.nid; + if (sk.nid == NID_undef) + break; + + hbuf = malloc(SYMMKEYSZ); + if (hbuf == NULL) { + log_err("Failed to malloc key buf"); + res = -ENOMEM; + break; + } + + memcpy(hbuf, kbuf, SYMMKEYSZ); + ret_msg->sym_key.data = hbuf; + ret_msg->sym_key.len = SYMMKEYSZ; + ret_msg->has_sym_key = true; + break; + case IRM_MSG_CODE__IRM_FLOW_ALLOC: + data.len = msg->pk.len; + data.data = msg->pk.data; + msg->has_pk = false; + assert(data.len > 0 ? data.data != NULL : data.data == NULL); + flow = flow_info_msg_to_s(msg->flow_info); + abstime = abstime == NULL ? &max : abstime; + sk.key = kbuf; + res = flow_alloc(msg->dst, &flow, &data, abstime, &sk); + if (res != 0) + break; + + ret_msg->flow_info = flow_info_s_to_msg(&flow); + ret_msg->has_pk = data.len != 0; + ret_msg->pk.data = data.data; + ret_msg->pk.len = data.len; + ret_msg->has_cipher_nid = true; + ret_msg->cipher_nid = sk.nid; + if (sk.nid == NID_undef) + break; + + hbuf = malloc(SYMMKEYSZ); + if (hbuf == NULL) { + log_err("Failed to malloc key buf"); + res = -ENOMEM; + break; + } + + memcpy(hbuf, kbuf, SYMMKEYSZ); + ret_msg->sym_key.data = hbuf; + ret_msg->sym_key.len = SYMMKEYSZ; + ret_msg->has_sym_key = true; + break; + case IRM_MSG_CODE__IRM_FLOW_JOIN: + assert(msg->pk.len == 0 && msg->pk.data == NULL); + flow = flow_info_msg_to_s(msg->flow_info); + abstime = abstime == NULL ? &max : abstime; + res = flow_join(&flow, msg->dst, abstime); + if (res == 0) + ret_msg->flow_info = flow_info_s_to_msg(&flow); + break; + case IRM_MSG_CODE__IRM_FLOW_DEALLOC: + flow = flow_info_msg_to_s(msg->flow_info); + ts = timespec_msg_to_s(msg->timeo); + res = flow_dealloc(&flow, &ts); + break; + case IRM_MSG_CODE__IPCP_FLOW_DEALLOC: + flow = flow_info_msg_to_s(msg->flow_info); + res = flow_dealloc_resp(&flow); + break; + case IRM_MSG_CODE__IPCP_FLOW_REQ_ARR: + data.len = msg->pk.len; + data.data = msg->pk.data; + msg->pk.data = NULL; /* pass data */ + msg->pk.len = 0; + assert(data.len > 0 ? data.data != NULL : data.data == NULL); + flow = flow_info_msg_to_s(msg->flow_info); + res = flow_req_arr(&flow, msg->hash.data, &data); + if (res == 0) + ret_msg->flow_info = flow_info_s_to_msg(&flow); + break; + case IRM_MSG_CODE__IPCP_FLOW_ALLOC_REPLY: + data.len = msg->pk.len; + data.data = msg->pk.data; + msg->pk.data = NULL; /* pass data */ + msg->pk.len = 0; + assert(data.len > 0 ? data.data != NULL : data.data == NULL); + flow = flow_info_msg_to_s(msg->flow_info); + res = flow_alloc_reply(&flow, msg->response, &data); + break; + case IRM_MSG_CODE__IPCP_FLOW_UPDATE_ARR: + data.len = msg->pk.len; + data.data = msg->pk.data; + msg->pk.data = NULL; /* pass data */ + msg->pk.len = 0; + flow = flow_info_msg_to_s(msg->flow_info); + res = flow_update_arr(&flow, &data); + freebuf(data); + break; + case IRM_MSG_CODE__IRM_FLOW_UPDATE: + flow = flow_info_msg_to_s(msg->flow_info); + if (get_peer_ids(fd, &uid, &gid, &cpid) < 0) { + res = -EPERM; + break; + } + + if (cpid <= 0) /* non-Linux: fall back to asserted pid */ + cpid = flow.n_pid; + + sk.key = kbuf; + res = flow_update(&flow, uid, cpid, msg->rekey, &sk, &has_key, + &initiator); + if (res != 0) + break; + + ret_msg->flow_info = flow_info_s_to_msg(&flow); + if (!has_key) + break; + + hbuf = malloc(SYMMKEYSZ); + if (hbuf == NULL) { + log_err("Failed to malloc key buf"); + res = -ENOMEM; + break; + } + + memcpy(hbuf, kbuf, SYMMKEYSZ); + ret_msg->sym_key.data = hbuf; + ret_msg->sym_key.len = SYMMKEYSZ; + ret_msg->has_sym_key = true; + ret_msg->has_generation = true; + ret_msg->generation = sk.epoch; + ret_msg->has_rk_initiator = true; + ret_msg->rk_initiator = initiator; + break; + default: + log_err("Don't know that message code."); + res = -1; + break; + } + + pthread_cleanup_pop(false); + + ret_msg->has_result = true; + if (abstime == &max && res == -ETIMEDOUT) + ret_msg->result = -EPERM; /* No timeout requested */ + else + ret_msg->result = res; - ret_msg->code = IRM_MSG_CODE__IRM_REPLY; + crypt_secure_clear(kbuf, SYMMKEYSZ); + + return ret_msg; +} + +/* Wipe the session key from a reply before its buffers are freed. */ +static void clear_msg_key(irm_msg_t * msg) +{ + if (msg != NULL && msg->has_sym_key) + crypt_secure_clear(msg->sym_key.data, msg->sym_key.len); +} + +static void * mainloop(void * o) +{ + int sfd; + irm_msg_t * msg; + buffer_t buffer; + + (void) o; + + while (true) { + irm_msg_t * ret_msg; + struct cmd * cmd; + bool had_key; pthread_mutex_lock(&irmd.cmd_lock); - pthread_cleanup_push(free_msg, ret_msg); pthread_cleanup_push(__cleanup_mutex_unlock, &irmd.cmd_lock); while (list_is_empty(&irmd.cmds)) @@ -2076,7 +2603,6 @@ static void * mainloop(void * o) list_del(&cmd->next); pthread_cleanup_pop(true); - pthread_cleanup_pop(false); msg = irm_msg__unpack(NULL, cmd->len, cmd->cbuf); sfd = cmd->fd; @@ -2084,186 +2610,35 @@ static void * mainloop(void * o) free(cmd); if (msg == NULL) { + log_err("Failed to unpack command message."); close(sfd); - irm_msg__free_unpacked(msg, NULL); - free(ret_msg); continue; } - tpm_dec(irmd.tpm); - - if (msg->has_timeo_sec) { - assert(msg->has_timeo_nsec); - - ts.tv_sec = msg->timeo_sec; - ts.tv_nsec = msg->timeo_nsec; - timeo = &ts; - } + tpm_begin_work(irmd.tpm); pthread_cleanup_push(__cleanup_close_ptr, &sfd); - pthread_cleanup_push(free_msg, msg); - pthread_cleanup_push(free_msg, ret_msg); + pthread_cleanup_push(__cleanup_irm_msg, msg); - switch (msg->code) { - case IRM_MSG_CODE__IRM_CREATE_IPCP: - result = create_ipcp(msg->name, msg->ipcp_type); - break; - case IRM_MSG_CODE__IPCP_CREATE_R: - result = create_ipcp_r(msg->pid, msg->result); - break; - case IRM_MSG_CODE__IRM_DESTROY_IPCP: - result = destroy_ipcp(msg->pid); - break; - case IRM_MSG_CODE__IRM_BOOTSTRAP_IPCP: - result = bootstrap_ipcp(msg->pid, msg->conf); - break; - case IRM_MSG_CODE__IRM_ENROLL_IPCP: - result = enroll_ipcp(msg->pid, msg->dst); - break; - case IRM_MSG_CODE__IRM_CONNECT_IPCP: - result = connect_ipcp(msg->pid, msg->dst, msg->comp, - msg_to_spec(msg->qosspec)); - break; - case IRM_MSG_CODE__IRM_DISCONNECT_IPCP: - result = disconnect_ipcp(msg->pid, msg->dst, msg->comp); - break; - case IRM_MSG_CODE__IRM_BIND_PROGRAM: - result = bind_program(msg->prog, - msg->name, - msg->opts, - msg->n_args, - msg->args); - break; - case IRM_MSG_CODE__IRM_UNBIND_PROGRAM: - result = unbind_program(msg->prog, msg->name); - break; - case IRM_MSG_CODE__IRM_PROC_ANNOUNCE: - result = proc_announce(msg->pid, msg->prog); - break; - case IRM_MSG_CODE__IRM_BIND_PROCESS: - result = bind_process(msg->pid, msg->name); - break; - case IRM_MSG_CODE__IRM_UNBIND_PROCESS: - result = unbind_process(msg->pid, msg->name); - break; - case IRM_MSG_CODE__IRM_LIST_IPCPS: - result = list_ipcps(&ret_msg->ipcps, &ret_msg->n_ipcps); - break; - case IRM_MSG_CODE__IRM_CREATE_NAME: - result = name_create(msg->names[0]->name, - msg->names[0]->pol_lb); - break; - case IRM_MSG_CODE__IRM_DESTROY_NAME: - result = name_destroy(msg->name); - break; - case IRM_MSG_CODE__IRM_LIST_NAMES: - result = list_names(&ret_msg->names, &ret_msg->n_names); - break; - case IRM_MSG_CODE__IRM_REG_NAME: - result = name_reg(msg->name, msg->pid); - break; - case IRM_MSG_CODE__IRM_UNREG_NAME: - result = name_unreg(msg->name, msg->pid); - break; - case IRM_MSG_CODE__IRM_FLOW_ACCEPT: - assert(msg->pk.len > 0 ? msg->pk.data != NULL - : msg->pk.data == NULL); - result = flow_accept(msg->pid, timeo, &e, - msg->pk.data, msg->pk.len); - if (result == 0) { - qosspec_msg_t qs_msg; - ret_msg->has_flow_id = true; - ret_msg->flow_id = e.flow_id; - ret_msg->has_pid = true; - ret_msg->pid = e.n_1_pid; - qs_msg = spec_to_msg(&e.qs); - ret_msg->qosspec = &qs_msg; - ret_msg->has_pk = true; - ret_msg->pk.data = e.data; - ret_msg->pk.len = e.len; - ret_msg->has_mpl = true; - ret_msg->mpl = e.mpl; - } - break; - case IRM_MSG_CODE__IRM_FLOW_ALLOC: - assert(msg->pk.len > 0 ? msg->pk.data != NULL - : msg->pk.data == NULL); - result = flow_alloc(msg->pid, msg->dst, - msg_to_spec(msg->qosspec), - timeo, &e, false, msg->pk.data, - msg->pk.len); - if (result == 0) { - ret_msg->has_flow_id = true; - ret_msg->flow_id = e.flow_id; - ret_msg->has_pid = true; - ret_msg->pid = e.n_1_pid; - ret_msg->has_pk = true; - ret_msg->pk.data = e.data; - ret_msg->pk.len = e.len; - ret_msg->has_mpl = true; - ret_msg->mpl = e.mpl; - } - break; - case IRM_MSG_CODE__IRM_FLOW_JOIN: - assert(msg->pk.len == 0 && msg->pk.data == NULL); - result = flow_alloc(msg->pid, msg->dst, - msg_to_spec(msg->qosspec), - timeo, &e, true, NULL, 0); - if (result == 0) { - ret_msg->has_flow_id = true; - ret_msg->flow_id = e.flow_id; - ret_msg->has_pid = true; - ret_msg->pid = e.n_1_pid; - ret_msg->has_mpl = true; - ret_msg->mpl = e.mpl; - } - break; - case IRM_MSG_CODE__IRM_FLOW_DEALLOC: - result = flow_dealloc(msg->pid, - msg->flow_id, - msg->timeo_sec); - break; - case IRM_MSG_CODE__IPCP_FLOW_REQ_ARR: - assert(msg->pk.len > 0 ? msg->pk.data != NULL - : msg->pk.data == NULL); - result = flow_req_arr(msg->pid, - &e, - msg->hash.data, - msg->mpl, - msg_to_spec(msg->qosspec), - msg->pk.data, - msg->pk.len); - if (result == 0) { - ret_msg->has_flow_id = true; - ret_msg->flow_id = e.flow_id; - ret_msg->has_pid = true; - ret_msg->pid = e.n_pid; - } - break; - case IRM_MSG_CODE__IPCP_FLOW_ALLOC_REPLY: - assert(msg->pk.len > 0 ? msg->pk.data != NULL - : msg->pk.data == NULL); - result = flow_alloc_reply(msg->flow_id, - msg->response, - msg->mpl, - msg->pk.data, - msg->pk.len); - break; - default: - log_err("Don't know that message code."); - result = -1; - break; - } + ret_msg = do_command_msg(msg, sfd); - pthread_cleanup_pop(false); pthread_cleanup_pop(true); pthread_cleanup_pop(false); - if (result == -EPIPE) + if (ret_msg == NULL) { + log_err("Failed to create return message."); + goto fail_msg; + } + + if (ret_msg->result == -EPIPE) { + log_dbg("Terminated command: remote closed socket."); goto fail; + } - ret_msg->has_result = true; - ret_msg->result = result; + if (ret_msg->result == -EIRMD) { + log_dbg("Terminated command: IRMd not running."); + goto fail; + } buffer.len = irm_msg__get_packed_size(ret_msg); if (buffer.len == 0) { @@ -2279,55 +2654,242 @@ static void * mainloop(void * o) irm_msg__pack(ret_msg, buffer.data); - /* Can't free the qosspec. */ - ret_msg->qosspec = NULL; + had_key = ret_msg->has_sym_key; + clear_msg_key(ret_msg); + irm_msg__free_unpacked(ret_msg, NULL); pthread_cleanup_push(__cleanup_close_ptr, &sfd); + pthread_cleanup_push(free, buffer.data); + + if (write(sfd, buffer.data, buffer.len) == -1) { + if (errno != EPIPE) + log_warn("Failed to send reply message: %s.", + strerror(errno)); + else + log_dbg("Failed to send reply message: %s.", + strerror(errno)); + } - if (write(sfd, buffer.data, buffer.len) == -1) - if (result != -EIRMD) - log_warn("Failed to send reply message."); - - free(buffer.data); + if (had_key) + crypt_secure_clear(buffer.data, buffer.len); pthread_cleanup_pop(true); + pthread_cleanup_pop(true); - tpm_inc(irmd.tpm); + tpm_end_work(irmd.tpm); continue; fail: + clear_msg_key(ret_msg); irm_msg__free_unpacked(ret_msg, NULL); + fail_msg: close(sfd); - tpm_inc(irmd.tpm); + tpm_end_work(irmd.tpm); continue; } return (void *) 0; } +#ifdef HAVE_FUSE +static void destroy_mount(char * mnt) +{ + struct stat st; + + if (stat(mnt, &st) == -1){ + switch(errno) { + case ENOENT: + log_dbg("Fuse mountpoint %s not found: %s", + mnt, strerror(errno)); + break; + case ENOTCONN: + /* FALLTHRU */ + case ECONNABORTED: + log_dbg("Cleaning up fuse mountpoint %s.", + mnt); + rib_cleanup(mnt); + break; + default: + log_err("Unhandled fuse error on mnt %s: %s.", + mnt, strerror(errno)); + } + } +} +#endif + +static int ouroboros_reset(void) +{ + ssm_pool_gspp_purge(); + lockfile_destroy(irmd.lf); + + return 0; +} + +static void cleanup_pid(pid_t pid) +{ +#ifdef HAVE_FUSE + char mnt[RIB_PATH_LEN + 1]; + + if (reg_has_ipcp(pid)) { + struct ipcp_info info; + info.pid = pid; + reg_get_ipcp(&info, NULL); + sprintf(mnt, FUSE_PREFIX "/%s", info.name); + } else { + sprintf(mnt, FUSE_PREFIX "/proc.%d", pid); + } + + destroy_mount(mnt); +#endif + ssm_pool_reclaim_orphans(irmd.gspp, pid); +} + +void * irm_sanitize(void * o) +{ + pid_t pid; + struct timespec ts = TIMESPEC_INIT_MS(FLOW_ALLOC_TIMEOUT / 20); + + (void) o; + + while (true) { + while((pid = reg_get_dead_proc()) != -1) { + log_info("Process %d died.", pid); + cleanup_pid(pid); + reg_destroy_proc(pid); + } + + nanosleep(&ts, NULL); + } + + return (void *) 0; +} + +static int irm_load_store(char * dpath, + bool anchor) +{ + struct stat st; + struct dirent * dent; + DIR * dir; + void * crt; + int ret; + + if (stat(dpath, &st) == -1) { + log_dbg("Store directory %s not found.", dpath); + return 0; + } + + if (!S_ISDIR(st.st_mode)) { + log_err("%s is not a directory.", dpath); + goto fail_dir; + } + + /* loop through files in directory and load certificates */ + dir = opendir(dpath); + if (dir == NULL) { + log_err("Failed to open %s.", dpath); + goto fail_dir; + } + + while ((dent = readdir(dir)) != NULL) { + char path[NAME_PATH_SIZE + 1]; + + if (strcmp(dent->d_name, ".") == 0 || + strcmp(dent->d_name, "..") == 0) + continue; + + snprintf(path, sizeof(path), "%s/%s", dpath, + dent->d_name); + + if (stat(path, &st) == -1) { + log_dbg("Failed to stat %s.", path); + continue; + } + + if (!S_ISREG(st.st_mode)) { + log_dbg("%s is not a regular file.", path); + goto fail_file; + } + + if (crypt_load_crt_file(path, &crt) < 0) { + log_err("Failed to load certificate from %s.", path); + goto fail_file; + } + + ret = anchor ? oap_auth_add_ca_crt(crt) + : oap_auth_add_chain_crt(crt); + if (ret < 0) { + log_err("Failed to add certificate from %s to store.", + path); + goto fail_crt_add; + } + + log_dbg("Loaded certificate: %s.", path); + + crypt_free_crt(crt); + } + + closedir(dir); + + log_info("Loaded certificates from %s.", dpath); + + return 0; + + fail_crt_add: + crypt_free_crt(crt); + fail_file: + closedir(dir); + fail_dir: + return -1; +} + static int irm_init(void) { struct stat st; + struct group * grp; + gid_t gid; pthread_condattr_t cattr; #ifdef HAVE_FUSE mode_t mask; #endif memset(&st, 0, sizeof(st)); - if (pthread_rwlock_init(&irmd.state_lock, NULL)) { - log_err("Failed to initialize rwlock."); - goto fail_state_lock; + log_init(!irmd.log_stdout); + + irmd.lf = lockfile_create(); + if (irmd.lf == NULL) { + irmd.lf = lockfile_open(); + if (irmd.lf == NULL) { + log_err("Lockfile error."); + goto fail_lockfile; + } + + if (kill(lockfile_owner(irmd.lf), 0) < 0) { + log_warn("IRMd didn't properly shut down last time."); + if (ouroboros_reset() < 0) { + log_err("Failed to clean stale resources."); + lockfile_close(irmd.lf); + goto fail_lockfile; + } + + log_warn("Stale resources cleaned."); + irmd.lf = lockfile_create(); + } else { + log_warn("IRMd already running (%d), exiting.", + lockfile_owner(irmd.lf)); + lockfile_close(irmd.lf); + goto fail_lockfile; + } } - if (pthread_rwlock_init(&irmd.reg_lock, NULL)) { - log_err("Failed to initialize rwlock."); - goto fail_reg_lock; + if (irmd.lf == NULL) { + log_err("Failed to create lockfile."); + goto fail_lockfile; } - if (pthread_rwlock_init(&irmd.flows_lock, NULL)) { + if (pthread_rwlock_init(&irmd.state_lock, NULL)) { log_err("Failed to initialize rwlock."); - goto fail_flows_lock; + goto fail_state_lock; } if (pthread_mutex_init(&irmd.cmd_lock, NULL)) { @@ -2351,45 +2913,31 @@ static int irm_init(void) pthread_condattr_destroy(&cattr); - list_head_init(&irmd.ipcps); - list_head_init(&irmd.proc_table); - list_head_init(&irmd.prog_table); - list_head_init(&irmd.spawned_pids); - list_head_init(&irmd.registry); - list_head_init(&irmd.irm_flows); list_head_init(&irmd.cmds); - irmd.flow_ids = bmp_create(SYS_MAX_FLOWS, 0); - if (irmd.flow_ids == NULL) { - log_err("Failed to create flow_ids bitmap."); - goto fail_flow_ids; + if (pthread_mutex_init(&irmd.rk.mtx, NULL)) { + log_err("Failed to initialize mutex."); + goto fail_rk_mtx; } - if ((irmd.lf = lockfile_create()) == NULL) { - if ((irmd.lf = lockfile_open()) == NULL) { - log_err("Lockfile error."); - goto fail_lockfile; - } - - if (kill(lockfile_owner(irmd.lf), 0) < 0) { - log_info("IRMd didn't properly shut down last time."); - shm_rdrbuff_purge(); - log_info("Stale resources cleaned."); - lockfile_destroy(irmd.lf); - irmd.lf = lockfile_create(); - } else { - log_info("IRMd already running (%d), exiting.", - lockfile_owner(irmd.lf)); - lockfile_close(irmd.lf); - goto fail_lockfile; - } + if (pthread_condattr_init(&cattr)) { + log_err("Failed to initialize condattr."); + goto fail_rk_mtx; } - if (irmd.lf == NULL) { - log_err("Failed to create lockfile."); - goto fail_lockfile; +#ifndef __APPLE__ + pthread_condattr_setclock(&cattr, PTHREAD_COND_CLOCK); +#endif + if (pthread_cond_init(&irmd.rk.cond, &cattr)) { + log_err("Failed to initialize condvar."); + pthread_condattr_destroy(&cattr); + goto fail_rk_cond; } + pthread_condattr_destroy(&cattr); + + list_head_init(&irmd.rk.inbox); + if (stat(SOCK_PATH, &st) == -1) { if (mkdir(SOCK_PATH, 0777)) { log_err("Failed to create sockets directory."); @@ -2408,10 +2956,45 @@ static int irm_init(void) goto fail_sock_path; } - if ((irmd.rdrb = shm_rdrbuff_create()) == NULL) { - log_err("Failed to create rdrbuff."); - goto fail_rdrbuff; + grp = getgrnam("ouroboros"); + if (grp == NULL) { + log_warn("ouroboros group not found, using gid %d.", getgid()); + gid = getgid(); + } else { + gid = grp->gr_gid; } + + irmd.gspp = ssm_pool_create(getuid(), gid); + if (irmd.gspp == NULL) { + log_err("Failed to create GSPP."); + goto fail_pool; + } + + if (ssm_pool_mlock(irmd.gspp) < 0) + log_warn("Failed to mlock pool."); + + irmd.tpm = tpm_create(IRMD_MIN_THREADS, IRMD_ADD_THREADS, + mainloop, NULL); + if (irmd.tpm == NULL) { + log_err("Failed to greate thread pool."); + goto fail_tpm_create; + } + + if (oap_auth_init() < 0) { + log_err("Failed to initialize OAP module."); + goto fail_oap; + } + + if (irm_load_store(OUROBOROS_CA_CRT_DIR, true) < 0) { + log_err("Failed to load CA certificates."); + goto fail_load_store; + } + + if (irm_load_store(OUROBOROS_CHAIN_DIR, false) < 0) { + log_err("Failed to load intermediate certificates."); + goto fail_load_store; + } + #ifdef HAVE_FUSE mask = umask(0); @@ -2436,117 +3019,161 @@ static int irm_init(void) gcry_control(GCRYCTL_INITIALIZATION_FINISHED); #endif - irmd_set_state(IRMD_RUNNING); - log_info("Ouroboros IPC Resource Manager daemon started..."); + irmd_set_state(IRMD_INIT); return 0; #ifdef HAVE_LIBGCRYPT fail_gcry_version: -#ifdef HAVE_FUSE + #ifdef HAVE_FUSE rmdir(FUSE_PREFIX); + #endif #endif - shm_rdrbuff_destroy(irmd.rdrb); -#endif - fail_rdrbuff: + fail_load_store: + oap_auth_fini(); + fail_oap: + tpm_destroy(irmd.tpm); + fail_tpm_create: + ssm_pool_destroy(irmd.gspp); + fail_pool: close(irmd.sockfd); fail_sock_path: unlink(IRM_SOCK_PATH); fail_stat: - lockfile_destroy(irmd.lf); - fail_lockfile: - bmp_destroy(irmd.flow_ids); - fail_flow_ids: + pthread_cond_destroy(&irmd.rk.cond); + fail_rk_cond: + pthread_mutex_destroy(&irmd.rk.mtx); + fail_rk_mtx: pthread_cond_destroy(&irmd.cmd_cond); fail_cmd_cond: pthread_mutex_destroy(&irmd.cmd_lock); fail_cmd_lock: - pthread_rwlock_destroy(&irmd.flows_lock); - fail_flows_lock: - pthread_rwlock_destroy(&irmd.reg_lock); - fail_reg_lock: pthread_rwlock_destroy(&irmd.state_lock); fail_state_lock: + lockfile_destroy(irmd.lf); + fail_lockfile: + log_fini(); return -1; } -static void usage(void) +static void irm_fini(void) { - printf("Usage: irmd \n" - " [--stdout (Log to stdout instead of system log)]\n" - " [--version (Print version number and exit)]\n" - "\n"); -} + struct list_head * p; + struct list_head * h; +#ifdef HAVE_FUSE + struct timespec wait = TIMESPEC_INIT_MS(1); + int retries = 5; +#endif + if (irmd_get_state() != IRMD_INIT) + log_warn("Unsafe destroy."); -int main(int argc, - char ** argv) -{ - sigset_t sigset; - bool use_stdout = false; - int sig; + oap_auth_fini(); - sigemptyset(&sigset); - sigaddset(&sigset, SIGINT); - sigaddset(&sigset, SIGQUIT); - sigaddset(&sigset, SIGHUP); - sigaddset(&sigset, SIGTERM); - sigaddset(&sigset, SIGPIPE); + tpm_destroy(irmd.tpm); - argc--; - argv++; - while (argc > 0) { - if (strcmp(*argv, "--stdout") == 0) { - use_stdout = true; - argc--; - argv++; - } else if (strcmp(*argv, "--version") == 0) { - printf("Ouroboros version %d.%d.%d\n", - OUROBOROS_VERSION_MAJOR, - OUROBOROS_VERSION_MINOR, - OUROBOROS_VERSION_PATCH); - exit(EXIT_SUCCESS); - } else { - usage(); - exit(EXIT_FAILURE); - } - } + close(irmd.sockfd); - if (geteuid() != 0) { - printf("IPC Resource Manager must be run as root.\n"); - exit(EXIT_FAILURE); + if (unlink(IRM_SOCK_PATH)) + log_dbg("Failed to unlink %s.", IRM_SOCK_PATH); + + ssm_pool_destroy(irmd.gspp); + + if (irmd.lf != NULL) + lockfile_destroy(irmd.lf); + + pthread_mutex_lock(&irmd.cmd_lock); + + list_for_each_safe(p, h, &irmd.cmds) { + struct cmd * cmd = list_entry(p, struct cmd, next); + list_del(&cmd->next); + close(cmd->fd); + free(cmd); } - log_init(!use_stdout); + pthread_mutex_unlock(&irmd.cmd_lock); - if (irm_init() < 0) - goto fail_irm_init; + pthread_mutex_lock(&irmd.rk.mtx); - irmd.tpm = tpm_create(IRMD_MIN_THREADS, IRMD_ADD_THREADS, - mainloop, NULL); - if (irmd.tpm == NULL) { - irmd_set_state(IRMD_NULL); - goto fail_tpm_create; + list_for_each_safe(p, h, &irmd.rk.inbox) { + struct rekey_evt * evt; + evt = list_entry(p, struct rekey_evt, next); + list_del(&evt->next); + freebuf(evt->buf); + free(evt); } - pthread_sigmask(SIG_BLOCK, &sigset, NULL); + pthread_mutex_unlock(&irmd.rk.mtx); + + pthread_mutex_destroy(&irmd.cmd_lock); + pthread_cond_destroy(&irmd.cmd_cond); + pthread_mutex_destroy(&irmd.rk.mtx); + pthread_cond_destroy(&irmd.rk.cond); + pthread_rwlock_destroy(&irmd.state_lock); + +#ifdef HAVE_FUSE + while (rmdir(FUSE_PREFIX) < 0 && retries-- > 0) + nanosleep(&wait, NULL); + + if (retries < 0) + log_err("Failed to remove " FUSE_PREFIX); +#endif + assert(list_is_empty(&irmd.cmds)); + + irmd.state = IRMD_NULL; +} + +static void usage(void) +{ + printf("Usage: irmd \n" +#ifdef HAVE_TOML + " [--config <path> (Path to configuration file)]\n" +#endif + " [--stdout (Log to stdout instead of system log)]\n" + " [--version (Print version number and exit)]\n" + "\n"); +} + +static int irm_start(void) +{ + irmd_set_state(IRMD_RUNNING); - if (tpm_start(irmd.tpm)) { - irmd_set_state(IRMD_NULL); + if (tpm_start(irmd.tpm)) goto fail_tpm_start; - } - if (pthread_create(&irmd.irm_sanitize, NULL, irm_sanitize, NULL)) { - irmd_set_state(IRMD_NULL); + if (pthread_create(&irmd.irm_sanitize, NULL, irm_sanitize, NULL)) goto fail_irm_sanitize; - } - if (pthread_create(&irmd.acceptor, NULL, acceptloop, NULL)) { - irmd_set_state(IRMD_NULL); + if (pthread_create(&irmd.acceptor, NULL, acceptloop, NULL)) goto fail_acceptor; + + if (OAP_REKEY_TIMER > 0) { + if (pthread_create(&irmd.rk.worker, NULL, rekey_worker, NULL)) + goto fail_rekey_worker; } - while (irmd_get_state() != IRMD_NULL) { + log_info("Ouroboros IPC Resource Manager daemon started..."); + + return 0; + + fail_rekey_worker: + pthread_cancel(irmd.acceptor); + pthread_join(irmd.acceptor, NULL); + fail_acceptor: + pthread_cancel(irmd.irm_sanitize); + pthread_join(irmd.irm_sanitize, NULL); + fail_irm_sanitize: + tpm_stop(irmd.tpm); + fail_tpm_start: + irmd_set_state(IRMD_INIT); + return -1; +} + +static void irm_sigwait(sigset_t sigset) +{ + int sig; + + while (irmd_get_state() != IRMD_SHUTDOWN) { if (sigwait(&sigset, &sig) != 0) { log_warn("Bad signal."); continue; @@ -2558,7 +3185,7 @@ int main(int argc, case SIGTERM: case SIGHUP: log_info("IRMd shutting down..."); - irmd_set_state(IRMD_NULL); + irmd_set_state(IRMD_SHUTDOWN); break; case SIGPIPE: log_dbg("Ignored SIGPIPE."); @@ -2567,35 +3194,195 @@ int main(int argc, break; } } +} + +static void irm_stop(void) +{ + if (OAP_REKEY_TIMER > 0) { + pthread_cancel(irmd.rk.worker); + pthread_join(irmd.rk.worker, NULL); + } pthread_cancel(irmd.acceptor); + pthread_cancel(irmd.irm_sanitize); pthread_join(irmd.acceptor, NULL); pthread_join(irmd.irm_sanitize, NULL); tpm_stop(irmd.tpm); - tpm_destroy(irmd.tpm); + irmd_set_state(IRMD_INIT); +} - irm_fini(); +static void irm_argparse(int argc, + char ** argv) +{ +#ifdef HAVE_TOML + irmd.cfg_file = NULL; +#endif + argc--; + argv++; + while (argc > 0) { + if (strcmp(*argv, "--stdout") == 0) { + irmd.log_stdout = true; + argc--; + argv++; + } else if (strcmp(*argv, "--version") == 0) { + printf("Ouroboros version %s\n", + OUROBOROS_VERSION_STRING); + exit(EXIT_SUCCESS); +#ifdef HAVE_TOML + } else if (strcmp (*argv, "--config") == 0) { + irmd.cfg_file = *(argv + 1); + argc -= 2; + argv += 2; +#endif + } else { + usage(); + exit(EXIT_FAILURE); + } + } +} + +static void * kill_dash_nine(void * o) +{ + time_t slept = 0; +#ifdef IRMD_KILL_ALL_PROCESSES + struct timespec ts = TIMESPEC_INIT_MS(FLOW_ALLOC_TIMEOUT / 19); +#endif + (void) o; + + while (slept < IRMD_PKILL_TIMEOUT) { + time_t intv = 1; + if (reg_first_spawned() == -1) + goto finish; + sleep(intv); + slept += intv; + } + + log_dbg("I guess I’ll have to shut you down for good this time,"); + log_dbg("already tried a SIGQUIT, so now it’s KILL DASH 9."); +#ifdef IRMD_KILL_ALL_PROCESSES + reg_kill_all_proc(SIGKILL); + nanosleep(&ts, NULL); +#else + reg_kill_all_spawned(SIGKILL); +#endif + finish: + return (void *) 0; +} + +static void kill_all_spawned(void) +{ + pid_t pid; + pthread_t grimreaper; + +#ifdef IRMD_KILL_ALL_PROCESSES + reg_kill_all_proc(SIGTERM); +#else + reg_kill_all_spawned(SIGTERM); +#endif + pthread_create(&grimreaper, NULL, kill_dash_nine, NULL); + + pid = reg_first_spawned(); + while (pid != -1) { + int s; + if (kill(pid, 0) == 0) + waitpid(pid, &s, 0); + else { + log_warn("Child process %d died.", pid); + cleanup_pid(pid); + reg_destroy_proc(pid); + } + pid = reg_first_spawned(); + } + + pthread_join(grimreaper, NULL); +} + +int main(int argc, + char ** argv) +{ + sigset_t sigset; + int ret = EXIT_SUCCESS; + + sigemptyset(&sigset); + sigaddset(&sigset, SIGINT); + sigaddset(&sigset, SIGQUIT); + sigaddset(&sigset, SIGHUP); + sigaddset(&sigset, SIGTERM); + sigaddset(&sigset, SIGPIPE); + + irm_argparse(argc, argv); + + if (irmd.log_stdout) + printf(O7S_ASCII_ART); + + if (geteuid() != 0) { + printf("IPC Resource Manager must be run as root.\n"); + goto fail_irm_init; + } + + if (crypt_secure_malloc_init(IRMD_SECMEM_MAX) < 0) { + log_err("Failed to initialize secure memory allocation."); + goto fail_secmem; + } + + if (irm_init() < 0) { + log_err("Failed to initialize IRMd."); + goto fail_irm_init; + } + + if (reg_init() < 0) { + log_err("Failed to initialize registry."); + goto fail_reg; + } + + pthread_sigmask(SIG_BLOCK, &sigset, NULL); + + if (irm_start() < 0) { + log_err("Failed to start IRMd."); + goto fail_irm_start; + } + +#ifdef HAVE_TOML + if (irm_configure(irmd.cfg_file) < 0) { + log_err("Failed to load IRMd configuration."); + irmd_set_state(IRMD_SHUTDOWN); + ret = EXIT_FAILURE; + } +#endif + irm_sigwait(sigset); + + kill_all_spawned(); + + irm_stop(); pthread_sigmask(SIG_UNBLOCK, &sigset, NULL); - log_info("Bye."); + reg_clear(); + + reg_fini(); + + irm_fini(); + + crypt_secure_malloc_fini(); + + crypt_cleanup(); + + log_info("Ouroboros IPC Resource Manager daemon exited. Bye."); log_fini(); - exit(EXIT_SUCCESS); + exit(ret); - fail_acceptor: - pthread_join(irmd.irm_sanitize, NULL); - fail_irm_sanitize: - tpm_stop(irmd.tpm); - fail_tpm_start: - tpm_destroy(irmd.tpm); - fail_tpm_create: + fail_irm_start: + reg_fini(); + fail_reg: irm_fini(); fail_irm_init: - log_fini(); + crypt_secure_malloc_fini(); + crypt_cleanup(); + fail_secmem: exit(EXIT_FAILURE); } |
