From 0e8493ea17c1bba64ee0de588bdbad7fdb6fd9ba Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 23 Mar 2015 01:40:35 +0100 Subject: Use peer-specific interface in TUN mode --- src/fastd.c | 43 +++++++++++-------------------------------- src/fastd.h | 27 +++++++++++++++++++++++++++ src/peer.c | 13 ++++++++++++- 3 files changed, 50 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/fastd.c b/src/fastd.c index af6d147..6ab07be 100644 --- a/src/fastd.c +++ b/src/fastd.c @@ -205,32 +205,6 @@ static void close_sockets(void) { free(ctx.socks); } -/** Calls the on-pre-up command */ -static inline void on_pre_up(void) { - fastd_shell_command_exec(&conf.on_pre_up, NULL); -} - -/** Calls the on-up command */ -static inline void on_up(fastd_iface_t *iface) { - fastd_shell_env_t *env = fastd_shell_env_alloc(); - fastd_shell_env_set_iface(env, iface); - fastd_shell_command_exec(&conf.on_up, env); - fastd_shell_env_free(env); -} - -/** Calls the on-down command */ -static inline void on_down(fastd_iface_t *iface) { - fastd_shell_env_t *env = fastd_shell_env_alloc(); - fastd_shell_env_set_iface(env, iface); - fastd_shell_command_exec(&conf.on_down, env); - fastd_shell_env_free(env); -} - -/** Calls the on-post-down command */ -static inline void on_post_down(void) { - fastd_shell_command_exec(&conf.on_post_down, NULL); -} - /** Closes all open FDs except stdin, stdout and stderr */ void fastd_close_all_fds(void) { @@ -524,9 +498,10 @@ static inline void init(int argc, char *argv[]) { if (!fastd_socket_handle_binds()) exit_error("unable to bind default socket"); - on_pre_up(); + fastd_on_pre_up(); - ctx.iface = fastd_iface_open(NULL); + if (conf.mode == MODE_TAP) + ctx.iface = fastd_iface_open(NULL); /* change groups before trying to write the PID file as they can be relevant for file access */ set_groups(); @@ -547,7 +522,9 @@ static inline void init(int argc, char *argv[]) { if (conf.drop_caps == DROP_CAPS_EARLY) drop_caps(); - on_up(ctx.iface); + if (ctx.iface) + fastd_on_up(ctx.iface); + fastd_configure_peers(); if (conf.drop_caps == DROP_CAPS_ON) @@ -647,14 +624,16 @@ static inline void cleanup(void) { delete_peers(); - on_down(ctx.iface); - fastd_iface_close(ctx.iface); + if (ctx.iface) { + fastd_on_down(ctx.iface); + fastd_iface_close(ctx.iface); + } fastd_status_close(); close_sockets(); fastd_poll_free(); - on_post_down(); + fastd_on_post_down(); fastd_peer_hashtable_free(); diff --git a/src/fastd.h b/src/fastd.h index d901554..039d0c6 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -538,3 +538,30 @@ static inline bool fastd_allow_verify(void) { return false; #endif } + + +/** Calls the on-pre-up command */ +static inline void fastd_on_pre_up(void) { + fastd_shell_command_exec(&conf.on_pre_up, NULL); +} + +/** Calls the on-up command */ +static inline void fastd_on_up(fastd_iface_t *iface) { + fastd_shell_env_t *env = fastd_shell_env_alloc(); + fastd_shell_env_set_iface(env, iface); + fastd_shell_command_exec(&conf.on_up, env); + fastd_shell_env_free(env); +} + +/** Calls the on-down command */ +static inline void fastd_on_down(fastd_iface_t *iface) { + fastd_shell_env_t *env = fastd_shell_env_alloc(); + fastd_shell_env_set_iface(env, iface); + fastd_shell_command_exec(&conf.on_down, env); + fastd_shell_env_free(env); +} + +/** Calls the on-post-down command */ +static inline void fastd_on_post_down(void) { + fastd_shell_command_exec(&conf.on_post_down, NULL); +} diff --git a/src/peer.c b/src/peer.c index 35eae28..56a203d 100644 --- a/src/peer.c +++ b/src/peer.c @@ -378,7 +378,13 @@ static void setup_peer(fastd_peer_t *peer) { peer->verify_valid_timeout = ctx.now; #endif - peer->iface = ctx.iface; + if (ctx.iface) { + peer->iface = ctx.iface; + } + else if (conf.iface_persist && !peer->iface) { + peer->iface = fastd_iface_open(peer); + fastd_on_up(peer->iface); + } if (!fastd_peer_is_enabled(peer)) /* Keep the peer in STATE_INACTIVE */ @@ -437,6 +443,11 @@ static void delete_peer(fastd_peer_t *peer) { conf.protocol->free_peer_state(peer); + if (peer->iface && peer->iface->peer) { + fastd_on_down(peer->iface); + fastd_iface_close(peer->iface); + } + fastd_peer_free(peer); } -- cgit v1.2.3