Skip to content

Commit 23bb6ec

Browse files
committed
quit on error EBADFD
Read fails with EBADFD if the gre link is deleted with "ip link del".
1 parent 9ebd382 commit 23bb6ec

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

gre.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static struct sockaddr_in remote;
4242
uint8_t buf[4096];
4343

4444
static void gre_cb(void);
45-
static void tun_cb(void);
45+
static int tun_cb(void);
4646
static int tun_new(const char *dev);
4747
static int setnonblock(int fd);
4848
static int runas(const char *user);
@@ -132,7 +132,8 @@ int main(int argc, char **argv)
132132

133133
if (FD_ISSET(tun, &readset))
134134
{
135-
tun_cb();
135+
if (tun_cb() < 0)
136+
return 0;
136137
}
137138
}
138139

@@ -176,19 +177,24 @@ static void gre_cb(void)
176177
write(tun, buf + ihl + 4, n - ihl - 4);
177178
}
178179

179-
static void tun_cb(void)
180+
static int tun_cb(void)
180181
{
181182
int n;
182183

183184
n = read(tun, buf + 4, sizeof(buf) - 4);
184185
if (n < 0)
185186
{
187+
int err = errno;
186188
perror("read");
187-
return;
189+
if (err == EBADFD)
190+
return -1;
191+
192+
return 0;
188193
}
189194
*(uint16_t *)(buf) = 0;
190195
*(uint16_t *)(buf + 2) = htons(0x0800);
191196
sendto(sock, buf, n + 4, 0, (struct sockaddr *)&remote, sizeof(struct sockaddr));
197+
return 0;
192198
}
193199

194200
static int tun_new(const char *dev)

0 commit comments

Comments
 (0)