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 ------------------------------------------------------*/
35 /*----- Data structures ---------------------------------------------------*/
37 typedef struct slipif
{
38 struct slipif
*next
; /* Next one in the list */
39 int ifd
, ofd
; /* File descriptors to talk on */
40 char *name
; /* Interface name */
41 pid_t kid
; /* Child process id */
42 unsigned f
; /* Various flags */
43 # define F_INUSE 1u /* Interface is in use */
44 # define F_DYNAMIC 2u /* Interface found dynamically */
48 const tunnel_ops
*ops
; /* Pointer to operations */
49 slipif
*sl
; /* My interface record */
50 sel_file f
; /* Selector for SLIP tty */
51 struct peer
*p
; /* Pointer to my peer */
52 unsigned st
; /* Current parser state */
53 # define ST_ESC 1u /* Last saw an escape character */
54 # define ST_BAD 2u /* This packet is malformed */
55 # define ST_EOF 4u /* File descriptor reported EOF */
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 #define SL_ESCEND 0xdc
70 #define SL_ESCESC 0xdd
74 * Arguments: @int fd@ = file descriptor to read
75 * @unsigned mode@ = what's happened
76 * @void *v@ = pointer to tunnel block
80 * Use: Reads data from the tunnel.
83 static void t_read(int fd
, unsigned mode
, void *v
)
87 const octet
*p
, *l
, *ll
;
93 /* --- Read the input data --- */
95 n
= read(fd
, buf_t
, sizeof(buf_t
));
98 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
99 errno
== EWOULDBLOCK
||
103 a_warn("TUN", "%s", t
->sl
->name
, "read-error", "?ERRNO", A_END
);
107 a_warn("TUN", "%s", t
->sl
->name
, "slip", "eof", A_END
);
112 IF_TRACING(T_TUNNEL
, {
113 trace_block(T_PACKET
, "tun-slip: SLIP-encapsulated data",
117 /* --- Decapsulate the packet --- */
119 for (p
= buf_t
, l
= p
+ n
, st
= t
->st
,
120 q
= t
->buf
+ t
->n
, ll
= t
->buf
+ sizeof(t
->buf
);
128 else if (st
& ST_ESC
)
129 a_warn("TUN", "%s", t
->sl
->name
, "slip", "escape-end", A_END
);
130 else if (q
== t
->buf
) {
131 T( trace(T_TUNNEL
, "tun-slip: empty packet"); )
133 IF_TRACING(T_TUNNEL
, {
134 trace(T_TUNNEL
, "tun-slip: packet arrived");
135 trace_block(T_PACKET
, "tun-slip: packet contents",
138 buf_init(&b
, t
->buf
, q
- t
->buf
);
142 st
&= ~(ST_ESC
| ST_BAD
);
145 if ((st
& ST_ESC
) && !(st
& ST_BAD
)) {
146 a_warn("TUN", "%s", t
->sl
->name
, "slip", "bad-escape", A_END
);
160 if ((st
& ST_ESC
) && !(st
& ST_BAD
)) {
161 a_warn("TUN", "%s", t
->sl
->name
, "slip", "bad-escape", A_END
);
165 if (!(st
& ST_BAD
)) {
169 a_warn("TUN", "%s", t
->sl
->name
, "slip", "overflow", A_END
);
182 /* --- @t_init@ --- *
188 * Use: Initializes the tunneling system. Maybe this will require
189 * opening file descriptors or something.
192 static void t_init(void)
196 slipif
*sl
, **tail
= &slipifs
;
197 unsigned long uli
, ulo
;
200 if ((p
= getenv("TRIPE_SLIPIF")) == 0)
203 /* --- Build the list of available interfaces --- */
209 if (*p
== '/' || *p
== '.') {
211 T( trace(T_TUNNEL
, "tun-slip: declared slip command `%s'", slipcmd
); )
214 uli
= strtoul(p
, &q
, 0);
215 if (uli
> INT_MAX
|| q
== p
)
221 ulo
= strtoul(p
, &q
, 0);
222 if (ulo
> INT_MAX
|| q
== p
)
225 if (*q
!= '=' || (n
= strcspn(q
+ 1, ":")) == 0)
230 fdflags(sl
->ifd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
231 fdflags(sl
->ofd
, O_NONBLOCK
, 0, FD_CLOEXEC
, FD_CLOEXEC
);
233 sl
->name
= xmalloc(n
+ 1);
236 memcpy(sl
->name
, q
+ 1, n
);
240 T( trace(T_TUNNEL
, "tun-slip: declared slipif %d,%d=%s",
241 sl
->ifd
, sl
->ofd
, sl
->name
); )
250 moan("bad slip interface list");
253 /* --- @t_create@ --- *
255 * Arguments: @peer *p@ = pointer to peer block
257 * Returns: A tunnel block if it worked, or null on failure.
259 * Use: Initializes a new tunnel.
262 static tunnel
*t_create(peer
*p
)
265 int pin
[2] = { -1, -1 }, pout
[2] = { -1, -1 };
270 static const char end
[] = { SL_END
, SL_END
};
272 /* --- Try to find a spare static interface --- */
274 for (sl
= slipifs
; sl
; sl
= sl
->next
) {
275 if (!(sl
->f
& F_INUSE
)) {
276 T( trace(T_TUNNEL
, "tun-slip: %s using static slipif %s",
277 p_name(p
), sl
->name
); )
282 /* --- If no dynamic interfaces are available, give up --- */
285 a_warn("TUN", "-", "slip", "no-slip-interfaces", A_END
);
289 /* --- Fork off a child process to create a dynamic SLIP interface --- */
291 if (pipe(pin
) || pipe(pout
)) {
292 a_warn("TUN", "-", "slip", "pipe-error", "?ERRNO", A_END
);
295 if ((kid
= fork()) < 0) {
296 a_warn("TUN", "-", "slip", "fork-error", "?ERRNO", A_END
);
302 dup2(pin
[0], STDIN_FILENO
);
303 dup2(pout
[1], STDOUT_FILENO
);
304 execlp(slipcmd
, slipcmd
, p_name(p
), (char *)0);
308 /* --- Read the interface name --- */
311 close(pin
[0]); pin
[0] = -1;
312 close(pout
[1]); pout
[1] = -1;
315 if (read(pout
[0], &ch
, 1) != 1 || ch
== SL_END
) {
316 a_warn("TUN", "-", "slip", "read-ifname-failed", "?ERRNO", A_END
);
324 sl
->name
= xstrdup(d
.buf
);
330 T( trace(T_TUNNEL
, "tun-slip: %s using dynamic slipif %s",
331 p_name(p
), sl
->name
); )
332 fdflags(pout
[0], O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
333 fdflags(pin
[1], O_NONBLOCK
, 0, FD_CLOEXEC
, FD_CLOEXEC
);
335 /* --- Set up the new tunnel --- */
345 sel_initfile(&sel
, &t
->f
, sl
->ifd
, SEL_READ
, t_read
, t
);
347 write(sl
->ofd
, end
, sizeof(end
));
351 /* --- Tidy up after a failure --- */
354 #define CLOSE(fd) do if (fd != -1) close(fd); while (0)
355 CLOSE(pin
[0]); CLOSE(pout
[0]);
356 CLOSE(pin
[1]); CLOSE(pout
[1]);
358 if (kid
!= -1) kill(kid
, SIGTERM
);
359 if (sl
&& (sl
->f
& F_DYNAMIC
)) DESTROY(sl
);
364 /* --- @t_ifname@ --- *
366 * Arguments: @tunnel *t@ = pointer to tunnel block
368 * Returns: A pointer to the tunnel's interface name.
371 static const char *t_ifname(tunnel
*t
) { return (t
->sl
->name
); }
373 /* --- @t_inject@ --- *
375 * Arguments: @tunnel *t@ = pointer to tunnel block
376 * @buf *b@ = buffer to send
380 * Use: Injects a packet into the local network stack.
383 static void t_inject(tunnel
*t
, buf
*b
)
385 octet buf
[PKBUFSZ
* 2 + 2];
389 IF_TRACING(T_TUNNEL
, {
390 trace(T_TUNNEL
, "tun-slip: inject decrypted packet");
391 trace_block(T_PACKET
, "tun-slip: packet contents", BBASE(b
), BLEN(b
));
396 for (p
= BBASE(b
), l
= BCUR(b
); p
< l
; p
++) {
398 case SL_END
: *q
++ = SL_ESC
; *q
++ = SL_ESCEND
; break;
399 case SL_ESC
: *q
++ = SL_ESC
; *q
++ = SL_ESCESC
; break;
400 default: *q
++ = *p
; break;
404 IF_TRACING(T_TUNNEL
, {
405 trace_block(T_PACKET
, "tun-slip: SLIP-encapsulated contents",
408 write(t
->sl
->ofd
, buf
, q
- buf
);
411 /* --- @t_destroy@ --- *
413 * Arguments: @tunnel *t@ = pointer to tunnel block
417 * Use: Destroys a tunnel.
420 static void t_destroy(tunnel
*t
)
424 /* --- If it reported EOF, leave it out-of-action --- */
426 if (!(t
->st
& ST_EOF
)) {
430 if (sl
&& (sl
->f
& F_DYNAMIC
)) {
431 T( trace(T_TUNNEL
, "tun-slip: releasing dynamic slipif %s", sl
->name
); )
434 kill(sl
->kid
, SIGTERM
);
441 const tunnel_ops tun_slip
= {
450 /*----- That's all, folks -------------------------------------------------*/