From 93945ca36875f69163c49f4d4b5f9649957a346b Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 9 Nov 2015 17:39:38 +0100 Subject: Add generic interface cleanup infrastructure Some systems (like FreeBSD) don't delete TUN/TAP interfaces after closing, add some infrastructure to do that. --- src/fastd.h | 1 + src/iface.c | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/fastd.h b/src/fastd.h index 7963505..88bd9a2 100644 --- a/src/fastd.h +++ b/src/fastd.h @@ -167,6 +167,7 @@ struct fastd_iface { char *name; /**< The interface name */ fastd_peer_t *peer; /**< The peer associated with the interface (if any) */ uint16_t mtu; /**< The MTU of the interface */ + bool cleanup; /**< Determines if the interface should be deleted after use; not used on all platforms */ }; diff --git a/src/iface.c b/src/iface.c index 74dda30..4f132e4 100644 --- a/src/iface.c +++ b/src/iface.c @@ -89,6 +89,7 @@ static inline fastd_iface_type_t get_iface_type(void) { } static void open_iface(fastd_iface_t *iface, const char *ifname, uint16_t mtu); +static void cleanup_iface(fastd_iface_t *iface); #ifdef __linux__ @@ -143,6 +144,9 @@ static void open_iface_linux(fastd_iface_t *iface, const char *ifname, uint16_t iface->fd.fd = -1; } +static void cleanup_iface(UNUSED fastd_iface_t *iface) { +} + #endif #if defined(__ANDROID__) @@ -273,6 +277,9 @@ static void open_iface(fastd_iface_t *iface, const char *ifname, uint16_t mtu) { } } +static void cleanup_iface(UNUSED fastd_iface_t *iface) { +} + #else /* __OpenBSD__ */ static void set_link0(fastd_iface_t *iface, bool set) { @@ -333,6 +340,9 @@ static void open_iface(fastd_iface_t *iface, const char *ifname, uint16_t mtu) { } } +static void cleanup_iface(UNUSED fastd_iface_t *iface) { +} + #endif #elif __APPLE__ @@ -374,6 +384,9 @@ static void open_iface(fastd_iface_t *iface, const char *ifname, uint16_t mtu) { exit_errno("SIOCSIFMTU ioctl failed"); } +static void cleanup_iface(UNUSED fastd_iface_t *iface) { +} + #else #error unknown TUN/TAP implementation @@ -510,7 +523,9 @@ fastd_iface_t * fastd_iface_open(fastd_peer_t *peer) { /** Closes the TUN/TAP device */ void fastd_iface_close(fastd_iface_t *iface) { - if (!fastd_poll_fd_close(&iface->fd)) + if (fastd_poll_fd_close(&iface->fd)) + cleanup_iface(iface); + else pr_warn_errno("closing TUN/TAP: close"); free(iface->name); -- cgit v1.2.3