From 23bb6ecd15373546363296baf38651f8dc5d9df9 Mon Sep 17 00:00:00 2001 From: Mikael Magnusson Date: Wed, 17 Apr 2019 20:49:25 +0000 Subject: quit on error EBADFD Read fails with EBADFD if the gre link is deleted with "ip link del". --- gre.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/gre.c b/gre.c index eaef4eb..8e4318d 100644 --- a/gre.c +++ b/gre.c @@ -42,7 +42,7 @@ static struct sockaddr_in remote; uint8_t buf[4096]; static void gre_cb(void); -static void tun_cb(void); +static int tun_cb(void); static int tun_new(const char *dev); static int setnonblock(int fd); static int runas(const char *user); @@ -132,7 +132,8 @@ int main(int argc, char **argv) if (FD_ISSET(tun, &readset)) { - tun_cb(); + if (tun_cb() < 0) + return 0; } } @@ -176,19 +177,24 @@ static void gre_cb(void) write(tun, buf + ihl + 4, n - ihl - 4); } -static void tun_cb(void) +static int tun_cb(void) { int n; n = read(tun, buf + 4, sizeof(buf) - 4); if (n < 0) { + int err = errno; perror("read"); - return; + if (err == EBADFD) + return -1; + + return 0; } *(uint16_t *)(buf) = 0; *(uint16_t *)(buf + 2) = htons(0x0800); sendto(sock, buf, n + 4, 0, (struct sockaddr *)&remote, sizeof(struct sockaddr)); + return 0; } static int tun_new(const char *dev) -- cgit v1.2.3