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
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
33 /*----- Data structures ---------------------------------------------------*/
35 typedef struct slipif {
36 struct slipif *next; /* Next one in the list */
37 int ifd, ofd; /* File descriptors to talk on */
38 char *name; /* Interface name */
39 pid_t kid; /* Child process id */
40 unsigned f; /* Various flags */
41 # define F_INUSE 1u /* Interface is in use */
42 # define F_DYNAMIC 2u /* Interface found dynamically */
46 const tunnel_ops *ops; /* Pointer to operations */
47 slipif *sl; /* My interface record */
48 sel_file f; /* Selector for SLIP tty */
49 struct peer *p; /* Pointer to my peer */
50 unsigned st; /* Current parser state */
51 # define ST_ESC 1u /* Last saw an escape character */
52 # define ST_BAD 2u /* This packet is malformed */
53 # define ST_MASK 3u /* Mask for the above bits */
54 # define ST_EOF 4u /* File descriptor reported EOF */
55 # define ST_BROKEN 8u /* Sending side is broken */
56 size_t n; /* Number of bytes used in buffer */
57 octet buf[PKBUFSZ]; /* Buffer for incoming data */
60 /*----- Static variables --------------------------------------------------*/
62 static slipif *slipifs; /* List of available interfaces */
63 static const char *slipcmd; /* Script to make new interfaces */
65 /*----- Main code ---------------------------------------------------------*/
69 * Arguments: @int fd@ = file descriptor to read
70 * @unsigned mode@ = what's happened
71 * @void *v@ = pointer to tunnel block
75 * Use: Reads data from the tunnel.
78 static void t_read(int fd, unsigned mode, void *v)
82 const octet *p, *l, *ll;
88 /* --- Read the input data --- */
90 n = read(fd, buf_t, sizeof(buf_t));
93 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
94 errno == EWOULDBLOCK ||
98 a_warn("TUN", "%s", p_ifname(t->p), "slip",
99 "read-error", "?ERRNO", A_END);
103 a_warn("TUN", "%s", p_ifname(t->p), "slip", "eof", A_END);
104 t->st = (t->st & ~ST_MASK) | ST_EOF;
108 IF_TRACING(T_TUNNEL, {
109 trace_block(T_PACKET, "tun-slip: SLIP-encapsulated data",
113 /* --- Decapsulate the packet --- */
115 for (p = buf_t, l = p + n, st = t->st,
116 q = t->buf + t->n, ll = t->buf + sizeof(t->buf);
124 else if (st & ST_ESC) {
125 a_warn("TUN", "%s", p_ifname(t->p), "slip", "escape-end", A_END);
127 } else if (q == t->buf) {
128 T( trace(T_TUNNEL, "tun-slip: empty packet"); )
130 IF_TRACING(T_TUNNEL, {
131 trace(T_TUNNEL, "tun-slip: packet arrived");
132 trace_block(T_PACKET, "tun-slip: packet contents",
135 buf_init(&b, t->buf, q - t->buf);
142 if ((st & ST_ESC) && !(st & ST_BAD)) {
143 a_warn("TUN", "%s", p_ifname(t->p), "slip", "bad-escape", A_END);
157 if ((st & ST_ESC) && !(st & ST_BAD)) {
158 a_warn("TUN", "%s", p_ifname(t->p), "slip", "bad-escape", A_END);
162 if (!(st & ST_BAD)) {
166 a_warn("TUN", "%s", p_ifname(t->p), "slip", "overflow", A_END);
179 /* --- @t_init@ --- *
185 * Use: Initializes the tunneling system. Maybe this will require
186 * opening file descriptors or something.
189 static void t_init(void)
193 slipif *sl, **tail = &slipifs;
194 unsigned long uli, ulo;
197 if ((p = getenv("TRIPE_SLIPIF")) == 0)
200 /* --- Build the list of available interfaces --- */
206 if (*p == '/' || *p == '.') {
208 T( trace(T_TUNNEL, "tun-slip: declared slip command `%s'", slipcmd); )
211 uli = strtoul(p, &q, 0);
212 if (uli > INT_MAX || q == p)
218 ulo = strtoul(p, &q, 0);
219 if (ulo > INT_MAX || q == p)
222 if (*q != '=' || (n = strcspn(q + 1, ":")) == 0)
227 fdflags(sl->ifd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
228 fdflags(sl->ofd, O_NONBLOCK, 0, FD_CLOEXEC, FD_CLOEXEC);
230 sl->name = xmalloc(n + 1);
233 memcpy(sl->name, q + 1, n);
237 T( trace(T_TUNNEL, "tun-slip: declared slipif %d,%d=%s",
238 sl->ifd, sl->ofd, sl->name); )
247 moan("bad slip interface list");
250 /* --- @t_broken@ --- *
252 * Arguments: @tunnel *t@ = pointer to the tunnel
256 * Use: Marks the tunnel as broken and reports an error.
259 static void t_broken(tunnel *t)
261 if (errno == EINTR ||
262 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
263 errno == EWOULDBLOCK ||
267 a_warn("TUN", "%s", p_ifname(t->p), "slip",
268 "write-error", "?ERRNO", A_END);
269 T( trace(T_TUNNEL, "tun-slip: marking tunnel broken"); )
273 /* --- @t_create@ --- *
275 * Arguments: @peer *p@ = pointer to peer block
276 * @int fd@ = file descriptor of tunnel device (unused)
277 * @char **ifn@ = where to put the interface name
279 * Returns: A tunnel block if it worked, or null on failure.
281 * Use: Initializes a new tunnel.
284 static tunnel *t_create(peer *p, int fd, char **ifn)
287 int pin[2] = { -1, -1 }, pout[2] = { -1, -1 };
293 static const octet end[] = { SL_END, SL_END };
295 /* --- Try to find a spare static interface --- */
297 for (sl = slipifs; sl; sl = sl->next) {
298 if (!(sl->f & F_INUSE)) {
299 T( trace(T_TUNNEL, "tun-slip: %s using static slipif %s",
300 p_name(p), sl->name); )
305 /* --- If no dynamic interfaces are available, give up --- */
308 a_warn("TUN", "-", "slip", "no-slip-interfaces", A_END);
312 /* --- Fork off a child process to create a dynamic SLIP interface --- */
314 if (pipe(pin) || pipe(pout)) {
315 a_warn("TUN", "-", "slip", "pipe-error", "?ERRNO", A_END);
318 if ((kid = fork()) < 0) {
319 a_warn("TUN", "-", "slip", "fork-error", "?ERRNO", A_END);
323 close(pin[1]); close(pout[0]);
324 md[0].cur = pin[0]; md[0].want = STDIN_FILENO;
325 md[1].cur = pout[1]; md[1].want = STDOUT_FILENO;
327 execlp(slipcmd, slipcmd, p_name(p), (char *)0);
331 /* --- Read the interface name --- */
334 close(pin[0]); pin[0] = -1;
335 close(pout[1]); pout[1] = -1;
338 if (read(pout[0], &ch, 1) != 1 || ch == SL_END) {
339 a_warn("TUN", "-", "slip", "read-ifname-failed", "?ERRNO", A_END);
347 sl->name = xstrdup(d.buf);
353 T( trace(T_TUNNEL, "tun-slip: %s using dynamic slipif %s",
354 p_name(p), sl->name); )
355 fdflags(pout[0], O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
356 fdflags(pin[1], O_NONBLOCK, 0, FD_CLOEXEC, FD_CLOEXEC);
358 /* --- Set up the new tunnel --- */
368 sel_initfile(&sel, &t->f, sl->ifd, SEL_READ, t_read, t);
370 if (write(sl->ofd, end, sizeof(end)) < 0)
372 *ifn = xstrdup(sl->name);
376 /* --- Tidy up after a failure --- */
379 #define CLOSE(fd) do if (fd != -1) close(fd); while (0)
380 CLOSE(pin[0]); CLOSE(pout[0]);
381 CLOSE(pin[1]); CLOSE(pout[1]);
383 if (kid != -1) kill(kid, SIGTERM);
384 if (sl && (sl->f & F_DYNAMIC)) DESTROY(sl);
389 /* --- @t_setifname@ --- *
391 * Arguments: @tunnel *t@ = pointer to tunnel block
392 * @const char *ifn@ = new interface name
396 * Use: Updates the interface name of a slip interface.
399 static void t_setifname(tunnel *t, const char *ifn)
400 { xfree(t->sl->name); t->sl->name = xstrdup(ifn); }
402 /* --- @t_inject@ --- *
404 * Arguments: @tunnel *t@ = pointer to tunnel block
405 * @buf *b@ = buffer to send
409 * Use: Injects a packet into the local network stack.
412 static void t_inject(tunnel *t, buf *b)
414 octet buf[PKBUFSZ * 2 + 2];
418 IF_TRACING(T_TUNNEL, {
419 trace(T_TUNNEL, "tun-slip: inject decrypted packet");
420 trace_block(T_PACKET, "tun-slip: packet contents", BBASE(b), BLEN(b));
423 if (t-> st & ST_BROKEN) {
424 T( trace(T_TUNNEL, "tun-slip: tunnel broken; discarding"); )
429 for (p = BBASE(b), l = BCUR(b); p < l; p++) {
431 case SL_END: *q++ = SL_ESC; *q++ = SL_ESCEND; break;
432 case SL_ESC: *q++ = SL_ESC; *q++ = SL_ESCESC; break;
433 default: *q++ = *p; break;
437 IF_TRACING(T_TUNNEL, {
438 trace_block(T_PACKET, "tun-slip: SLIP-encapsulated contents",
441 if (write(t->sl->ofd, buf, q - buf) < 0)
445 /* --- @t_destroy@ --- *
447 * Arguments: @tunnel *t@ = pointer to tunnel block
451 * Use: Destroys a tunnel.
454 static void t_destroy(tunnel *t)
458 /* --- If it reported EOF, leave it out-of-action --- */
460 if (!(t->st & ST_EOF)) {
464 if (sl && (sl->f & F_DYNAMIC)) {
465 T( trace(T_TUNNEL, "tun-slip: releasing dynamic slipif %s", sl->name); )
468 kill(sl->kid, SIGTERM);
475 const tunnel_ops tun_slip = {
485 /*----- That's all, folks -------------------------------------------------*/