X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/tripe/blobdiff_plain/b476642f0ecdb981d9f0183fb5421082d90d9071..add23883a6378567be6b7866bc2ff2c32400a346:/server/tun-slip.c diff --git a/server/tun-slip.c b/server/tun-slip.c index 609829f5..85e9cd84 100644 --- a/server/tun-slip.c +++ b/server/tun-slip.c @@ -50,7 +50,9 @@ struct tunnel { unsigned st; /* Current parser state */ # define ST_ESC 1u /* Last saw an escape character */ # define ST_BAD 2u /* This packet is malformed */ +# define ST_MASK 3u /* Mask for the above bits */ # define ST_EOF 4u /* File descriptor reported EOF */ +# define ST_BROKEN 8u /* Sending side is broken */ size_t n; /* Number of bytes used in buffer */ octet buf[PKBUFSZ]; /* Buffer for incoming data */ }; @@ -99,7 +101,7 @@ static void t_read(int fd, unsigned mode, void *v) } if (!n) { a_warn("TUN", "%s", p_ifname(t->p), "slip", "eof", A_END); - t->st = ST_EOF; + t->st = (t->st & ~ST_MASK) | ST_EOF; sel_rmfile(&t->f); return; } @@ -119,9 +121,10 @@ static void t_read(int fd, unsigned mode, void *v) case SL_END: if (st & ST_BAD) ; - else if (st & ST_ESC) + else if (st & ST_ESC) { a_warn("TUN", "%s", p_ifname(t->p), "slip", "escape-end", A_END); - else if (q == t->buf) { + st |= ST_BAD; + } else if (q == t->buf) { T( trace(T_TUNNEL, "tun-slip: empty packet"); ) } else { IF_TRACING(T_TUNNEL, { @@ -133,7 +136,7 @@ static void t_read(int fd, unsigned mode, void *v) p_tun(t->p, &b); } q = t->buf; - st &= ~(ST_ESC | ST_BAD); + st &= ~ST_MASK; break; case SL_ESC: if ((st & ST_ESC) && !(st & ST_BAD)) { @@ -244,6 +247,29 @@ whine: moan("bad slip interface list"); } +/* --- @t_broken@ --- * + * + * Arguments: @tunnel *t@ = pointer to the tunnel + * + * Returns: --- + * + * Use: Marks the tunnel as broken and reports an error. + */ + +static void t_broken(tunnel *t) +{ + if (errno == EINTR || +#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN + errno == EWOULDBLOCK || +#endif + errno == EAGAIN) + return; + a_warn("TUN", "%s", p_ifname(t->p), "slip", + "write-error", "?ERRNO", A_END); + T( trace(T_TUNNEL, "tun-slip: marking tunnel broken"); ) + t->st |= ST_BROKEN; +} + /* --- @t_create@ --- * * * Arguments: @peer *p@ = pointer to peer block @@ -264,7 +290,7 @@ static tunnel *t_create(peer *p, int fd, char **ifn) dstr d = DSTR_INIT; unsigned char ch; tunnel *t; - static const char end[] = { SL_END, SL_END }; + static const octet end[] = { SL_END, SL_END }; /* --- Try to find a spare static interface --- */ @@ -341,7 +367,8 @@ found: sl->f |= F_INUSE; sel_initfile(&sel, &t->f, sl->ifd, SEL_READ, t_read, t); sel_addfile(&t->f); - write(sl->ofd, end, sizeof(end)); + if (write(sl->ofd, end, sizeof(end)) < 0) + t_broken(t); *ifn = xstrdup(sl->name); dstr_destroy(&d); return (t); @@ -393,6 +420,10 @@ static void t_inject(tunnel *t, buf *b) trace_block(T_PACKET, "tun-slip: packet contents", BBASE(b), BLEN(b)); }) + if (t-> st & ST_BROKEN) { + T( trace(T_TUNNEL, "tun-slip: tunnel broken; discarding"); ) + return; + } q = buf; *q++ = SL_END; for (p = BBASE(b), l = BCUR(b); p < l; p++) { @@ -407,7 +438,8 @@ static void t_inject(tunnel *t, buf *b) trace_block(T_PACKET, "tun-slip: SLIP-encapsulated contents", buf, q - buf); }) - write(t->sl->ofd, buf, q - buf); + if (write(t->sl->ofd, buf, q - buf) < 0) + t_broken(t); } /* --- @t_destroy@ --- *