5 * Tunnel packets via SLIP
7 * (c) 2005 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
14 * TrIPE is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * TrIPE is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with TrIPE; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
33 /*----- Static variables --------------------------------------------------*/
35 static slipif *slipifs; /* List of available interfaces */
36 static const char *slipcmd; /* Script to make new interfaces */
38 /*----- Main code ---------------------------------------------------------*/
40 #if TUN_TYPE != TUN_SLIP
41 # error "Tunnel type mismatch: fix the Makefile"
46 #define SL_ESCEND 0xdc
47 #define SL_ESCESC 0xdd
51 * Arguments: @int fd@ = file descriptor to read
52 * @unsigned mode@ = what's happened
53 * @void *v@ = pointer to tunnel block
57 * Use: Reads data from the tunnel.
60 static void t_read(int fd, unsigned mode, void *v)
64 const octet *p, *l, *ll;
70 /* --- Read the input data --- */
72 n = read(fd, buf_t, sizeof(buf_t));
75 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
76 errno == EWOULDBLOCK ||
80 a_warn("TUN %s read-error -- %s", t->sl->name, strerror(errno));
84 a_warn("TUN %s slip eof", t->sl->name);
89 IF_TRACING(T_TUNNEL, {
90 trace_block(T_PACKET, "tunnel: SLIP-encapsulated data",
94 /* --- Decapsulate the packet --- */
96 for (p = buf_t, l = p + n, st = t->st,
97 q = t->buf + t->n, ll = t->buf + sizeof(t->buf);
105 else if (st & SLIPST_ESC)
106 a_warn("TUN %s slip escape-end", t->sl->name);
107 else if (q == t->buf) {
108 T( trace(T_TUNNEL, "tunnel: empty packet"); )
110 IF_TRACING(T_TUNNEL, {
111 trace(T_TUNNEL, "tunnel: packet arrived");
112 trace_block(T_PACKET, "tunnel: packet contents",
115 buf_init(&b, t->buf, q - t->buf);
119 st &= ~(SLIPST_ESC | SLIPST_BAD);
122 if ((st & SLIPST_ESC) && !(st & SLIPST_BAD)) {
123 a_warn("TUN %s slip bad-escape", t->sl->name);
137 if ((st & SLIPST_ESC) && !(st & SLIPST_BAD)) {
138 a_warn("TUN %s slip bad-escape", t->sl->name);
142 if (!(st & SLIPST_BAD)) {
146 a_warn("TUN %s slip overflow", t->sl->name);
159 /* --- @tun_init@ --- *
165 * Use: Initializes the tunneling system. Maybe this will require
166 * opening file descriptors or something.
173 slipif *sl, **tail = &slipifs;
174 unsigned long uli, ulo;
177 if ((p = getenv("TRIPE_SLIPIF")) == 0)
178 die(1, "no slip interfaces listed: set TRIPE_SLIPIF");
180 /* --- Build the list of available interfaces --- */
186 if (*p == '/' || *p == '.') {
188 T( trace(T_TUNNEL, "tunnel: declared slip command `%s'", slipcmd); )
191 uli = strtoul(p, &q, 0);
192 if (uli > INT_MAX || q == p)
198 ulo = strtoul(p, &q, 0);
199 if (ulo > INT_MAX || q == p)
202 if (*q != '=' || (n = strcspn(q + 1, ":")) == 0)
207 fdflags(sl->ifd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
208 fdflags(sl->ofd, O_NONBLOCK, 0, FD_CLOEXEC, FD_CLOEXEC);
210 sl->name = xmalloc(n + 1);
213 memcpy(sl->name, q + 1, n);
217 T( trace(T_TUNNEL, "tunnel: declared slipif %d,%d=%s",
218 sl->ifd, sl->ofd, sl->name); )
227 die(1, "bad slip interface list");
230 /* --- @tun_create@ --- *
232 * Arguments: @tunnel *t@ = pointer to tunnel block
233 * @peer *p@ = pointer to peer block
235 * Returns: Zero if it worked, nonzero on failure.
237 * Use: Initializes a new tunnel.
240 int tun_create(tunnel *t, peer *p)
243 int pin[2] = { -1, -1 }, pout[2] = { -1, -1 };
247 static const char end[] = { SL_END, SL_END };
249 /* --- Try to find a spare static interface --- */
251 for (sl = slipifs; sl; sl = sl->next) {
252 if (!(sl->f & SLIPIFF_INUSE)) {
253 T( trace(T_TUNNEL, "tunnel: %s using static slipif %s",
254 p_name(p), sl->name); )
259 /* --- If no dynamic interfaces are available, give up --- */
262 a_warn("TUN %s slip no-slip-interfaces", p_name(p));
266 /* --- Fork off a child process to create a dynamic SLIP interface --- */
268 if (pipe(pin) || pipe(pout)) {
269 a_warn("TUN %s slip pipe-error -- %s", p_name(p), strerror(errno));
272 if ((kid = fork()) < 0) {
273 a_warn("TUN %s slip fork-error -- %s", p_name(p), strerror(errno));
279 dup2(pin[0], STDIN_FILENO);
280 dup2(pout[1], STDOUT_FILENO);
281 execlp(slipcmd, slipcmd, p_name(p), (char *)0);
285 /* --- Read the interface name --- */
288 close(pin[0]); pin[0] = -1;
289 close(pout[1]); pout[1] = -1;
292 if (read(pout[0], &ch, 1) != 1 || ch == SL_END) {
293 a_warn("TUN %s slip read-ifname-failed -- %s",
294 p_name(p), strerror(errno));
302 sl->name = xstrdup(d.buf);
307 sl->f = SLIPIFF_DYNAMIC;
308 T( trace(T_TUNNEL, "tunnel: %s using dynamic slipif %s",
309 p_name(p), sl->name); )
310 fdflags(pout[0], O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
311 fdflags(pin[1], O_NONBLOCK, 0, FD_CLOEXEC, FD_CLOEXEC);
313 /* --- Set up the new tunnel --- */
320 sl->f |= SLIPIFF_INUSE;
321 sel_initfile(&sel, &t->f, sl->ifd, SEL_READ, t_read, t);
323 write(sl->ofd, end, sizeof(end));
327 /* --- Tidy up after a failure --- */
330 #define CLOSE(fd) do if (fd != -1) close(fd); while (0)
331 CLOSE(pin[0]); CLOSE(pout[0]);
332 CLOSE(pin[1]); CLOSE(pout[1]);
334 if (kid != -1) kill(kid, SIGTERM);
335 if (sl && (sl->f & SLIPIFF_DYNAMIC)) DESTROY(sl);
340 /* --- @tun_ifname@ --- *
342 * Arguments: @tunnel *t@ = pointer to tunnel block
344 * Returns: A pointer to the tunnel's interface name.
347 const char *tun_ifname(tunnel *t) { return (t->sl->name); }
349 /* --- @tun_inject@ --- *
351 * Arguments: @tunnel *t@ = pointer to tunnel block
352 * @buf *b@ = buffer to send
356 * Use: Injects a packet into the local network stack.
359 void tun_inject(tunnel *t, buf *b)
361 octet buf[PKBUFSZ * 2 + 2];
365 IF_TRACING(T_TUNNEL, {
366 trace(T_TUNNEL, "tunnel: inject decrypted packet");
367 trace_block(T_PACKET, "tunnel: packet contents", BBASE(b), BLEN(b));
372 for (p = BBASE(b), l = BCUR(b); p < l; p++) {
374 case SL_END: *q++ = SL_ESC; *q++ = SL_ESCEND; break;
375 case SL_ESC: *q++ = SL_ESC; *q++ = SL_ESCESC; break;
376 default: *q++ = *p; break;
380 IF_TRACING(T_TUNNEL, {
381 trace_block(T_PACKET, "tunnel: SLIP-encapsulated contents",
384 write(t->sl->ofd, buf, q - buf);
387 /* --- @tun_destroy@ --- *
389 * Arguments: @tunnel *t@ = pointer to tunnel block
393 * Use: Destroys a tunnel.
396 void tun_destroy(tunnel *t)
400 /* --- If it reported EOF, leave it out-of-action --- */
402 if (!(t->st & SLIPST_EOF)) {
404 sl->f &= ~SLIPIFF_INUSE;
406 if (sl && (sl->f & SLIPIFF_DYNAMIC)) {
407 T( trace(T_TUNNEL, "tunnel: releasing dynamic slipif %s", sl->name); )
410 kill(sl->kid, SIGTERM);
416 /*----- That's all, folks -------------------------------------------------*/