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