chiark / gitweb /
Upgrade licence to GPLv3+.
[tripe] / server / tun-slip.c
index 2c99a81a9d7db80342229668276dc728f9a1edd1..1c452955e753c307faea0889a2c5563c04ad1c17 100644 (file)
@@ -9,19 +9,18 @@
  *
  * This file is part of Trivial IP Encryption (TrIPE).
  *
- * TrIPE is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * TrIPE is free software: you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 3 of the License, or (at your
+ * option) any later version.
  *
- * TrIPE is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * TrIPE is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with TrIPE; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * along with TrIPE.  If not, see <https://www.gnu.org/licenses/>.
  */
 
 /*----- Header files ------------------------------------------------------*/
@@ -50,7 +49,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 */
 };
@@ -62,11 +63,6 @@ static const char *slipcmd;          /* Script to make new interfaces */
 
 /*----- Main code ---------------------------------------------------------*/
 
-#define SL_END 0xc0
-#define SL_ESC 0xdb
-#define SL_ESCEND 0xdc
-#define SL_ESCESC 0xdd
-
 /* --- @t_read@ --- *
  *
  * Arguments:  @int fd@ = file descriptor to read
@@ -98,12 +94,13 @@ static void t_read(int fd, unsigned mode, void *v)
 #endif
        errno == EAGAIN)
       return;
-    a_warn("TUN", "%s", p_ifname(t->p), "read-error", "?ERRNO", A_END);
+    a_warn("TUN", "%s", p_ifname(t->p), "slip",
+          "read-error", "?ERRNO", A_END);
     return;
   }
   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;
   }
@@ -123,9 +120,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, {
@@ -137,7 +135,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)) {
@@ -248,9 +246,33 @@ 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
+ *             @int fd@ = file descriptor of tunnel device (unused)
  *             @char **ifn@ = where to put the interface name
  *
  * Returns:    A tunnel block if it worked, or null on failure.
@@ -258,15 +280,16 @@ whine:
  * Use:                Initializes a new tunnel.
  */
 
-static tunnel *t_create(peer *p, char **ifn)
+static tunnel *t_create(peer *p, int fd, char **ifn)
 {
   slipif *sl = 0;
   int pin[2] = { -1, -1 }, pout[2] = { -1, -1 };
+  mdup_fd md[2];
   pid_t kid = -1;
   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 --- */
 
@@ -296,10 +319,10 @@ static tunnel *t_create(peer *p, char **ifn)
     goto fail;
   }
   if (!kid) {
-    close(pin[1]);
-    close(pout[0]);
-    dup2(pin[0], STDIN_FILENO);
-    dup2(pout[1], STDOUT_FILENO);
+    close(pin[1]); close(pout[0]);
+    md[0].cur = pin[0];  md[0].want = STDIN_FILENO;
+    md[1].cur = pout[1]; md[1].want = STDOUT_FILENO;
+    mdup(md, 2);
     execlp(slipcmd, slipcmd, p_name(p), (char *)0);
     _exit(127);
   }
@@ -343,7 +366,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);
@@ -395,6 +419,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++) {
@@ -409,7 +437,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@ --- *
@@ -444,6 +473,7 @@ static void t_destroy(tunnel *t)
 
 const tunnel_ops tun_slip = {
   "slip",
+  0,
   t_init,
   t_create,
   t_setifname,