chiark / gitweb /
server/tests.at: Fix TRIPECTL_INTERACT argument order.
[tripe] / server / tun-slip.c
CommitLineData
b9066fbb 1/* -*-c-*-
2 *
b9066fbb 3 * Tunnel packets via SLIP
4 *
5 * (c) 2005 Straylight/Edgeware
6 */
7
e04c2d50 8/*----- Licensing notice --------------------------------------------------*
b9066fbb 9 *
10 * This file is part of Trivial IP Encryption (TrIPE).
11 *
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.
e04c2d50 16 *
b9066fbb 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.
e04c2d50 21 *
b9066fbb 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.
25 */
26
27/*----- Header files ------------------------------------------------------*/
28
42da2a58 29#define TUN_INTERNALS
30
b9066fbb 31#include "tripe.h"
32
42da2a58 33/*----- Data structures ---------------------------------------------------*/
34
35typedef 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 */
43} slipif;
44
45struct tunnel {
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 */
574318e7 53# define ST_MASK 3u /* Mask for the above bits */
42da2a58 54# define ST_EOF 4u /* File descriptor reported EOF */
898975ee 55# define ST_BROKEN 8u /* Sending side is broken */
42da2a58 56 size_t n; /* Number of bytes used in buffer */
57 octet buf[PKBUFSZ]; /* Buffer for incoming data */
e04c2d50 58};
42da2a58 59
b9066fbb 60/*----- Static variables --------------------------------------------------*/
61
62static slipif *slipifs; /* List of available interfaces */
98fdb08d 63static const char *slipcmd; /* Script to make new interfaces */
b9066fbb 64
65/*----- Main code ---------------------------------------------------------*/
66
b9066fbb 67/* --- @t_read@ --- *
68 *
69 * Arguments: @int fd@ = file descriptor to read
70 * @unsigned mode@ = what's happened
71 * @void *v@ = pointer to tunnel block
72 *
73 * Returns: ---
74 *
75 * Use: Reads data from the tunnel.
76 */
77
78static void t_read(int fd, unsigned mode, void *v)
79{
80 tunnel *t = v;
81 ssize_t n;
82 const octet *p, *l, *ll;
83 octet *q;
84 unsigned st;
85 octet o;
86 buf b;
87
88 /* --- Read the input data --- */
89
90 n = read(fd, buf_t, sizeof(buf_t));
91 if (n < 0) {
92 if (errno == EINTR ||
98fdb08d 93#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
b9066fbb 94 errno == EWOULDBLOCK ||
95#endif
96 errno == EAGAIN)
97 return;
b476642f
MW
98 a_warn("TUN", "%s", p_ifname(t->p), "slip",
99 "read-error", "?ERRNO", A_END);
b9066fbb 100 return;
101 }
102 if (!n) {
72917fe7 103 a_warn("TUN", "%s", p_ifname(t->p), "slip", "eof", A_END);
574318e7 104 t->st = (t->st & ~ST_MASK) | ST_EOF;
b9066fbb 105 sel_rmfile(&t->f);
106 return;
107 }
108 IF_TRACING(T_TUNNEL, {
060ca767 109 trace_block(T_PACKET, "tun-slip: SLIP-encapsulated data",
b9066fbb 110 buf_t, n);
111 })
112
113 /* --- Decapsulate the packet --- */
114
115 for (p = buf_t, l = p + n, st = t->st,
116 q = t->buf + t->n, ll = t->buf + sizeof(t->buf);
117 p < l;
118 p++) {
119 o = *p;
120 switch (o) {
121 case SL_END:
42da2a58 122 if (st & ST_BAD)
b9066fbb 123 ;
42da2a58 124 else if (st & ST_ESC)
72917fe7 125 a_warn("TUN", "%s", p_ifname(t->p), "slip", "escape-end", A_END);
b9066fbb 126 else if (q == t->buf) {
060ca767 127 T( trace(T_TUNNEL, "tun-slip: empty packet"); )
b9066fbb 128 } else {
129 IF_TRACING(T_TUNNEL, {
060ca767 130 trace(T_TUNNEL, "tun-slip: packet arrived");
131 trace_block(T_PACKET, "tun-slip: packet contents",
b9066fbb 132 t->buf, q - t->buf);
133 })
134 buf_init(&b, t->buf, q - t->buf);
135 p_tun(t->p, &b);
136 }
137 q = t->buf;
574318e7 138 st &= ~ST_MASK;
b9066fbb 139 break;
140 case SL_ESC:
42da2a58 141 if ((st & ST_ESC) && !(st & ST_BAD)) {
72917fe7 142 a_warn("TUN", "%s", p_ifname(t->p), "slip", "bad-escape", A_END);
42da2a58 143 st |= ST_BAD;
b9066fbb 144 } else
42da2a58 145 st |= ST_ESC;
b9066fbb 146 break;
147 case SL_ESCEND:
42da2a58 148 if (st & ST_ESC)
b9066fbb 149 o = SL_END;
150 goto emit;
151 case SL_ESCESC:
42da2a58 152 if (st & ST_ESC)
b9066fbb 153 o = SL_ESC;
154 goto emit;
155 default:
42da2a58 156 if ((st & ST_ESC) && !(st & ST_BAD)) {
72917fe7 157 a_warn("TUN", "%s", p_ifname(t->p), "slip", "bad-escape", A_END);
42da2a58 158 st |= ST_BAD;
b9066fbb 159 }
160 emit:
42da2a58 161 if (!(st & ST_BAD)) {
b9066fbb 162 if (q < ll)
163 *q++ = o;
164 else {
72917fe7 165 a_warn("TUN", "%s", p_ifname(t->p), "slip", "overflow", A_END);
42da2a58 166 st |= ST_BAD;
b9066fbb 167 }
168 }
42da2a58 169 st &= ~ST_ESC;
b9066fbb 170 break;
171 }
172 }
173
174 t->n = q - t->buf;
175 t->st = st;
176}
177
42da2a58 178/* --- @t_init@ --- *
b9066fbb 179 *
180 * Arguments: ---
181 *
182 * Returns: ---
183 *
184 * Use: Initializes the tunneling system. Maybe this will require
185 * opening file descriptors or something.
186 */
187
42da2a58 188static void t_init(void)
b9066fbb 189{
190 char *p, *q;
191 dstr d = DSTR_INIT;
192 slipif *sl, **tail = &slipifs;
193 unsigned long uli, ulo;
194 size_t n;
195
b9066fbb 196 if ((p = getenv("TRIPE_SLIPIF")) == 0)
42da2a58 197 return;
98fdb08d 198
199 /* --- Build the list of available interfaces --- */
200
b9066fbb 201 dstr_puts(&d, p);
202
203 p = d.buf;
204 for (;;) {
98fdb08d 205 if (*p == '/' || *p == '.') {
206 slipcmd = p;
060ca767 207 T( trace(T_TUNNEL, "tun-slip: declared slip command `%s'", slipcmd); )
98fdb08d 208 break;
209 }
b9066fbb 210 uli = strtoul(p, &q, 0);
211 if (uli > INT_MAX || q == p)
212 goto whine;
213 if (*q != ',')
214 ulo = uli;
215 else {
216 p = q + 1;
217 ulo = strtoul(p, &q, 0);
218 if (ulo > INT_MAX || q == p)
219 goto whine;
220 }
221 if (*q != '=' || (n = strcspn(q + 1, ":")) == 0)
222 goto whine;
223 sl = CREATE(slipif);
224 sl->next = 0;
225 sl->ifd = uli;
98fdb08d 226 fdflags(sl->ifd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
227 fdflags(sl->ofd, O_NONBLOCK, 0, FD_CLOEXEC, FD_CLOEXEC);
b9066fbb 228 sl->ofd = ulo;
229 sl->name = xmalloc(n + 1);
98fdb08d 230 sl->kid = -1;
b9066fbb 231 sl->f = 0;
232 memcpy(sl->name, q + 1, n);
233 sl->name[n] = 0;
234 *tail = sl;
235 tail = &sl->next;
060ca767 236 T( trace(T_TUNNEL, "tun-slip: declared slipif %d,%d=%s",
b9066fbb 237 sl->ifd, sl->ofd, sl->name); )
238 p = q + n + 1;
239 if (!*p)
240 break;
241 p++;
242 }
243 return;
244
245whine:
42da2a58 246 moan("bad slip interface list");
b9066fbb 247}
248
898975ee
MW
249/* --- @t_broken@ --- *
250 *
251 * Arguments: @tunnel *t@ = pointer to the tunnel
252 *
253 * Returns: ---
254 *
255 * Use: Marks the tunnel as broken and reports an error.
256 */
257
258static void t_broken(tunnel *t)
259{
260 if (errno == EINTR ||
261#if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
262 errno == EWOULDBLOCK ||
263#endif
264 errno == EAGAIN)
265 return;
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"); )
269 t->st |= ST_BROKEN;
270}
271
42da2a58 272/* --- @t_create@ --- *
b9066fbb 273 *
42da2a58 274 * Arguments: @peer *p@ = pointer to peer block
eb5f3fea 275 * @int fd@ = file descriptor of tunnel device (unused)
72917fe7 276 * @char **ifn@ = where to put the interface name
b9066fbb 277 *
42da2a58 278 * Returns: A tunnel block if it worked, or null on failure.
b9066fbb 279 *
280 * Use: Initializes a new tunnel.
281 */
282
eb5f3fea 283static tunnel *t_create(peer *p, int fd, char **ifn)
b9066fbb 284{
98fdb08d 285 slipif *sl = 0;
286 int pin[2] = { -1, -1 }, pout[2] = { -1, -1 };
b9537f3b 287 mdup_fd md[2];
98fdb08d 288 pid_t kid = -1;
289 dstr d = DSTR_INIT;
290 unsigned char ch;
42da2a58 291 tunnel *t;
b9066fbb 292 static const char end[] = { SL_END, SL_END };
293
98fdb08d 294 /* --- Try to find a spare static interface --- */
295
b9066fbb 296 for (sl = slipifs; sl; sl = sl->next) {
42da2a58 297 if (!(sl->f & F_INUSE)) {
060ca767 298 T( trace(T_TUNNEL, "tun-slip: %s using static slipif %s",
98fdb08d 299 p_name(p), sl->name); )
b9066fbb 300 goto found;
98fdb08d 301 }
b9066fbb 302 }
98fdb08d 303
304 /* --- If no dynamic interfaces are available, give up --- */
305
306 if (!slipcmd) {
f43df819 307 a_warn("TUN", "-", "slip", "no-slip-interfaces", A_END);
98fdb08d 308 goto fail;
309 }
310
311 /* --- Fork off a child process to create a dynamic SLIP interface --- */
312
313 if (pipe(pin) || pipe(pout)) {
f43df819 314 a_warn("TUN", "-", "slip", "pipe-error", "?ERRNO", A_END);
98fdb08d 315 goto fail;
316 }
317 if ((kid = fork()) < 0) {
f43df819 318 a_warn("TUN", "-", "slip", "fork-error", "?ERRNO", A_END);
98fdb08d 319 goto fail;
320 }
321 if (!kid) {
b9537f3b
MW
322 close(pin[1]); close(pout[0]);
323 md[0].cur = pin[0]; md[0].want = STDIN_FILENO;
2d5c3e54 324 md[1].cur = pout[1]; md[1].want = STDOUT_FILENO;
b9537f3b 325 mdup(md, 2);
98fdb08d 326 execlp(slipcmd, slipcmd, p_name(p), (char *)0);
327 _exit(127);
328 }
329
330 /* --- Read the interface name --- */
331
332 sl = CREATE(slipif);
333 close(pin[0]); pin[0] = -1;
334 close(pout[1]); pout[1] = -1;
335 for (;;) {
336 errno = EIO;
337 if (read(pout[0], &ch, 1) != 1 || ch == SL_END) {
f43df819 338 a_warn("TUN", "-", "slip", "read-ifname-failed", "?ERRNO", A_END);
98fdb08d 339 goto fail;
340 }
341 if (ch == '\n')
342 break;
343 DPUTC(&d, (char)ch);
344 }
345 DPUTZ(&d);
346 sl->name = xstrdup(d.buf);
347 sl->ifd = pout[0];
348 sl->ofd = pin[1];
349 sl->kid = kid;
350 sl->next = 0;
42da2a58 351 sl->f = F_DYNAMIC;
060ca767 352 T( trace(T_TUNNEL, "tun-slip: %s using dynamic slipif %s",
98fdb08d 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);
356
357 /* --- Set up the new tunnel --- */
b9066fbb 358
359found:
42da2a58 360 t = CREATE(tunnel);
361 t->ops = &tun_slip;
b9066fbb 362 t->p = p;
363 t->sl = sl;
364 t->st = 0;
365 t->n = 0;
42da2a58 366 sl->f |= F_INUSE;
b9066fbb 367 sel_initfile(&sel, &t->f, sl->ifd, SEL_READ, t_read, t);
368 sel_addfile(&t->f);
898975ee
MW
369 if (write(sl->ofd, end, sizeof(end)) < 0)
370 t_broken(t);
72917fe7 371 *ifn = xstrdup(sl->name);
98fdb08d 372 dstr_destroy(&d);
42da2a58 373 return (t);
98fdb08d 374
375 /* --- Tidy up after a failure --- */
376
377fail:
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]);
381#undef CLOSE
382 if (kid != -1) kill(kid, SIGTERM);
42da2a58 383 if (sl && (sl->f & F_DYNAMIC)) DESTROY(sl);
98fdb08d 384 dstr_destroy(&d);
42da2a58 385 return (0);
b9066fbb 386}
387
72917fe7 388/* --- @t_setifname@ --- *
b9066fbb 389 *
390 * Arguments: @tunnel *t@ = pointer to tunnel block
72917fe7 391 * @const char *ifn@ = new interface name
b9066fbb 392 *
72917fe7
MW
393 * Returns: ---
394 *
395 * Use: Updates the interface name of a slip interface.
b9066fbb 396 */
397
72917fe7
MW
398static void t_setifname(tunnel *t, const char *ifn)
399 { xfree(t->sl->name); t->sl->name = xstrdup(ifn); }
b9066fbb 400
42da2a58 401/* --- @t_inject@ --- *
b9066fbb 402 *
403 * Arguments: @tunnel *t@ = pointer to tunnel block
404 * @buf *b@ = buffer to send
405 *
406 * Returns: ---
407 *
408 * Use: Injects a packet into the local network stack.
409 */
410
42da2a58 411static void t_inject(tunnel *t, buf *b)
b9066fbb 412{
413 octet buf[PKBUFSZ * 2 + 2];
414 const octet *p, *l;
415 octet *q;
416
417 IF_TRACING(T_TUNNEL, {
060ca767 418 trace(T_TUNNEL, "tun-slip: inject decrypted packet");
419 trace_block(T_PACKET, "tun-slip: packet contents", BBASE(b), BLEN(b));
b9066fbb 420 })
421
898975ee
MW
422 if (t-> st & ST_BROKEN) {
423 T( trace(T_TUNNEL, "tun-slip: tunnel broken; discarding"); )
424 return;
425 }
b9066fbb 426 q = buf;
427 *q++ = SL_END;
428 for (p = BBASE(b), l = BCUR(b); p < l; p++) {
429 switch (*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;
433 }
434 }
435 *q++ = SL_END;
436 IF_TRACING(T_TUNNEL, {
060ca767 437 trace_block(T_PACKET, "tun-slip: SLIP-encapsulated contents",
b9066fbb 438 buf, q - buf);
439 })
898975ee
MW
440 if (write(t->sl->ofd, buf, q - buf) < 0)
441 t_broken(t);
b9066fbb 442}
443
42da2a58 444/* --- @t_destroy@ --- *
b9066fbb 445 *
446 * Arguments: @tunnel *t@ = pointer to tunnel block
447 *
448 * Returns: ---
449 *
450 * Use: Destroys a tunnel.
451 */
452
42da2a58 453static void t_destroy(tunnel *t)
b9066fbb 454{
98fdb08d 455 slipif *sl = t->sl;
456
b9066fbb 457 /* --- If it reported EOF, leave it out-of-action --- */
458
42da2a58 459 if (!(t->st & ST_EOF)) {
b9066fbb 460 sel_rmfile(&t->f);
42da2a58 461 sl->f &= ~F_INUSE;
98fdb08d 462 }
42da2a58 463 if (sl && (sl->f & F_DYNAMIC)) {
060ca767 464 T( trace(T_TUNNEL, "tun-slip: releasing dynamic slipif %s", sl->name); )
98fdb08d 465 close(sl->ofd);
466 close(sl->ifd);
467 kill(sl->kid, SIGTERM);
468 xfree(sl->name);
469 DESTROY(sl);
b9066fbb 470 }
42da2a58 471 DESTROY(t);
b9066fbb 472}
473
42da2a58 474const tunnel_ops tun_slip = {
475 "slip",
eb5f3fea 476 0,
388e0319 477 t_init,
42da2a58 478 t_create,
72917fe7 479 t_setifname,
42da2a58 480 t_inject,
481 t_destroy
482};
483
b9066fbb 484/*----- That's all, folks -------------------------------------------------*/