3 * Tunnel packets via SLIP
5 * (c) 2005 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your
15 * option) any later version.
17 * TrIPE is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
32 /*----- Data structures ---------------------------------------------------*/
34 typedef struct slipif {
35 struct slipif *next; /* Next one in the list */
36 int ifd, ofd; /* File descriptors to talk on */
37 char *name; /* Interface name */
38 pid_t kid; /* Child process id */
39 unsigned f; /* Various flags */
40 # define F_INUSE 1u /* Interface is in use */
41 # define F_DYNAMIC 2u /* Interface found dynamically */
45 const tunnel_ops *ops; /* Pointer to operations */
46 slipif *sl; /* My interface record */
47 sel_file f; /* Selector for SLIP tty */
48 struct peer *p; /* Pointer to my peer */
49 unsigned st; /* Current parser state */
50 # define ST_ESC 1u /* Last saw an escape character */
51 # define ST_BAD 2u /* This packet is malformed */
52 # define ST_MASK 3u /* Mask for the above bits */
53 # define ST_EOF 4u /* File descriptor reported EOF */
54 # define ST_BROKEN 8u /* Sending side is broken */
55 size_t n; /* Number of bytes used in buffer */
56 octet buf[PKBUFSZ]; /* Buffer for incoming data */
59 /*----- Static variables --------------------------------------------------*/
61 static slipif *slipifs; /* List of available interfaces */
62 static const char *slipcmd; /* Script to make new interfaces */
64 /*----- Main code ---------------------------------------------------------*/
68 * Arguments: @int fd@ = file descriptor to read
69 * @unsigned mode@ = what's happened
70 * @void *v@ = pointer to tunnel block
74 * Use: Reads data from the tunnel.
77 static void t_read(int fd, unsigned mode, void *v)
81 const octet *p, *l, *ll;
87 /* --- Read the input data --- */
89 n = read(fd, buf_t, sizeof(buf_t));
92 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
93 errno == EWOULDBLOCK ||
97 a_warn("TUN", "%s", p_ifname(t->p), "slip",
98 "read-error", "?ERRNO", A_END);
102 a_warn("TUN", "%s", p_ifname(t->p), "slip", "eof", A_END);
103 t->st = (t->st & ~ST_MASK) | ST_EOF;
107 IF_TRACING(T_TUNNEL, {
108 trace_block(T_PACKET, "tun-slip: SLIP-encapsulated data",
112 /* --- Decapsulate the packet --- */
114 for (p = buf_t, l = p + n, st = t->st,
115 q = t->buf + t->n, ll = t->buf + sizeof(t->buf);
123 else if (st & ST_ESC) {
124 a_warn("TUN", "%s", p_ifname(t->p), "slip", "escape-end", A_END);
126 } else if (q == t->buf) {
127 T( trace(T_TUNNEL, "tun-slip: empty packet"); )
129 IF_TRACING(T_TUNNEL, {
130 trace(T_TUNNEL, "tun-slip: packet arrived");
131 trace_block(T_PACKET, "tun-slip: packet contents",
134 buf_init(&b, t->buf, q - t->buf);
141 if ((st & ST_ESC) && !(st & ST_BAD)) {
142 a_warn("TUN", "%s", p_ifname(t->p), "slip", "bad-escape", A_END);
156 if ((st & ST_ESC) && !(st & ST_BAD)) {
157 a_warn("TUN", "%s", p_ifname(t->p), "slip", "bad-escape", A_END);
161 if (!(st & ST_BAD)) {
165 a_warn("TUN", "%s", p_ifname(t->p), "slip", "overflow", A_END);
178 /* --- @t_init@ --- *
184 * Use: Initializes the tunneling system. Maybe this will require
185 * opening file descriptors or something.
188 static void t_init(void)
192 slipif *sl, **tail = &slipifs;
193 unsigned long uli, ulo;
196 if ((p = getenv("TRIPE_SLIPIF")) == 0)
199 /* --- Build the list of available interfaces --- */
205 if (*p == '/' || *p == '.') {
207 T( trace(T_TUNNEL, "tun-slip: declared slip command `%s'", slipcmd); )
210 uli = strtoul(p, &q, 0);
211 if (uli > INT_MAX || q == p)
217 ulo = strtoul(p, &q, 0);
218 if (ulo > INT_MAX || q == p)
221 if (*q != '=' || (n = strcspn(q + 1, ":")) == 0)
226 fdflags(sl->ifd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
227 fdflags(sl->ofd, O_NONBLOCK, 0, FD_CLOEXEC, FD_CLOEXEC);
229 sl->name = xmalloc(n + 1);
232 memcpy(sl->name, q + 1, n);
236 T( trace(T_TUNNEL, "tun-slip: declared slipif %d,%d=%s",
237 sl->ifd, sl->ofd, sl->name); )
246 moan("bad slip interface list");
249 /* --- @t_broken@ --- *
251 * Arguments: @tunnel *t@ = pointer to the tunnel
255 * Use: Marks the tunnel as broken and reports an error.
258 static void t_broken(tunnel *t)
260 if (errno == EINTR ||
261 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
262 errno == EWOULDBLOCK ||
266 a_warn("TUN", "%s", p_ifname(t->p), "slip",
267 "write-error", "?ERRNO", A_END);
268 T( trace(T_TUNNEL, "tun-slip: marking tunnel broken"); )
272 /* --- @t_create@ --- *
274 * Arguments: @peer *p@ = pointer to peer block
275 * @int fd@ = file descriptor of tunnel device (unused)
276 * @char **ifn@ = where to put the interface name
278 * Returns: A tunnel block if it worked, or null on failure.
280 * Use: Initializes a new tunnel.
283 static tunnel *t_create(peer *p, int fd, char **ifn)
286 int pin[2] = { -1, -1 }, pout[2] = { -1, -1 };
292 static const octet end[] = { SL_END, SL_END };
294 /* --- Try to find a spare static interface --- */
296 for (sl = slipifs; sl; sl = sl->next) {
297 if (!(sl->f & F_INUSE)) {
298 T( trace(T_TUNNEL, "tun-slip: %s using static slipif %s",
299 p_name(p), sl->name); )
304 /* --- If no dynamic interfaces are available, give up --- */
307 a_warn("TUN", "-", "slip", "no-slip-interfaces", A_END);
311 /* --- Fork off a child process to create a dynamic SLIP interface --- */
313 if (pipe(pin) || pipe(pout)) {
314 a_warn("TUN", "-", "slip", "pipe-error", "?ERRNO", A_END);
317 if ((kid = fork()) < 0) {
318 a_warn("TUN", "-", "slip", "fork-error", "?ERRNO", A_END);
322 close(pin[1]); close(pout[0]);
323 md[0].cur = pin[0]; md[0].want = STDIN_FILENO;
324 md[1].cur = pout[1]; md[1].want = STDOUT_FILENO;
326 execlp(slipcmd, slipcmd, p_name(p), (char *)0);
330 /* --- Read the interface name --- */
333 close(pin[0]); pin[0] = -1;
334 close(pout[1]); pout[1] = -1;
337 if (read(pout[0], &ch, 1) != 1 || ch == SL_END) {
338 a_warn("TUN", "-", "slip", "read-ifname-failed", "?ERRNO", A_END);
346 sl->name = xstrdup(d.buf);
352 T( trace(T_TUNNEL, "tun-slip: %s using dynamic slipif %s",
353 p_name(p), sl->name); )
354 fdflags(pout[0], O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
355 fdflags(pin[1], O_NONBLOCK, 0, FD_CLOEXEC, FD_CLOEXEC);
357 /* --- Set up the new tunnel --- */
367 sel_initfile(&sel, &t->f, sl->ifd, SEL_READ, t_read, t);
369 if (write(sl->ofd, end, sizeof(end)) < 0)
371 *ifn = xstrdup(sl->name);
375 /* --- Tidy up after a failure --- */
378 #define CLOSE(fd) do if (fd != -1) close(fd); while (0)
379 CLOSE(pin[0]); CLOSE(pout[0]);
380 CLOSE(pin[1]); CLOSE(pout[1]);
382 if (kid != -1) kill(kid, SIGTERM);
383 if (sl && (sl->f & F_DYNAMIC)) DESTROY(sl);
388 /* --- @t_setifname@ --- *
390 * Arguments: @tunnel *t@ = pointer to tunnel block
391 * @const char *ifn@ = new interface name
395 * Use: Updates the interface name of a slip interface.
398 static void t_setifname(tunnel *t, const char *ifn)
399 { xfree(t->sl->name); t->sl->name = xstrdup(ifn); }
401 /* --- @t_inject@ --- *
403 * Arguments: @tunnel *t@ = pointer to tunnel block
404 * @buf *b@ = buffer to send
408 * Use: Injects a packet into the local network stack.
411 static void t_inject(tunnel *t, buf *b)
413 octet buf[PKBUFSZ * 2 + 2];
417 IF_TRACING(T_TUNNEL, {
418 trace(T_TUNNEL, "tun-slip: inject decrypted packet");
419 trace_block(T_PACKET, "tun-slip: packet contents", BBASE(b), BLEN(b));
422 if (t-> st & ST_BROKEN) {
423 T( trace(T_TUNNEL, "tun-slip: tunnel broken; discarding"); )
428 for (p = BBASE(b), l = BCUR(b); p < l; p++) {
430 case SL_END: *q++ = SL_ESC; *q++ = SL_ESCEND; break;
431 case SL_ESC: *q++ = SL_ESC; *q++ = SL_ESCESC; break;
432 default: *q++ = *p; break;
436 IF_TRACING(T_TUNNEL, {
437 trace_block(T_PACKET, "tun-slip: SLIP-encapsulated contents",
440 if (write(t->sl->ofd, buf, q - buf) < 0)
444 /* --- @t_destroy@ --- *
446 * Arguments: @tunnel *t@ = pointer to tunnel block
450 * Use: Destroys a tunnel.
453 static void t_destroy(tunnel *t)
457 /* --- If it reported EOF, leave it out-of-action --- */
459 if (!(t->st & ST_EOF)) {
463 if (sl && (sl->f & F_DYNAMIC)) {
464 T( trace(T_TUNNEL, "tun-slip: releasing dynamic slipif %s", sl->name); )
467 kill(sl->kid, SIGTERM);
474 const tunnel_ops tun_slip = {
484 /*----- That's all, folks -------------------------------------------------*/