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 size_t n
; /* Number of bytes used in buffer */
56 octet buf
[PKBUFSZ
]; /* Buffer for incoming data */
59 /*----- Static variables --------------------------------------------------*/
61 static slipif
*slipifs
; /* List of available interfaces */
62 static const char *slipcmd
; /* Script to make new interfaces */
64 /*----- Main code ---------------------------------------------------------*/
68 * Arguments: @int fd@ = file descriptor to read
69 * @unsigned mode@ = what's happened
70 * @void *v@ = pointer to tunnel block
74 * Use: Reads data from the tunnel.
77 static void t_read(int fd
, unsigned mode
, void *v
)
81 const octet
*p
, *l
, *ll
;
87 /* --- Read the input data --- */
89 n
= read(fd
, buf_t
, sizeof(buf_t
));
92 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
93 errno
== EWOULDBLOCK
||
97 a_warn("TUN", "%s", p_ifname(t
->p
), "slip",
98 "read-error", "?ERRNO", A_END
);
102 a_warn("TUN", "%s", p_ifname(t
->p
), "slip", "eof", A_END
);
103 t
->st
= (t
->st
& ~ST_MASK
) | ST_EOF
;
107 IF_TRACING(T_TUNNEL
, {
108 trace_block(T_PACKET
, "tun-slip: SLIP-encapsulated data",
112 /* --- Decapsulate the packet --- */
114 for (p
= buf_t
, l
= p
+ n
, st
= t
->st
,
115 q
= t
->buf
+ t
->n
, ll
= t
->buf
+ sizeof(t
->buf
);
123 else if (st
& ST_ESC
)
124 a_warn("TUN", "%s", p_ifname(t
->p
), "slip", "escape-end", A_END
);
125 else if (q
== t
->buf
) {
126 T( trace(T_TUNNEL
, "tun-slip: empty packet"); )
128 IF_TRACING(T_TUNNEL
, {
129 trace(T_TUNNEL
, "tun-slip: packet arrived");
130 trace_block(T_PACKET
, "tun-slip: packet contents",
133 buf_init(&b
, t
->buf
, q
- t
->buf
);
140 if ((st
& ST_ESC
) && !(st
& ST_BAD
)) {
141 a_warn("TUN", "%s", p_ifname(t
->p
), "slip", "bad-escape", A_END
);
155 if ((st
& ST_ESC
) && !(st
& ST_BAD
)) {
156 a_warn("TUN", "%s", p_ifname(t
->p
), "slip", "bad-escape", A_END
);
160 if (!(st
& ST_BAD
)) {
164 a_warn("TUN", "%s", p_ifname(t
->p
), "slip", "overflow", A_END
);
177 /* --- @t_init@ --- *
183 * Use: Initializes the tunneling system. Maybe this will require
184 * opening file descriptors or something.
187 static void t_init(void)
191 slipif
*sl
, **tail
= &slipifs
;
192 unsigned long uli
, ulo
;
195 if ((p
= getenv("TRIPE_SLIPIF")) == 0)
198 /* --- Build the list of available interfaces --- */
204 if (*p
== '/' || *p
== '.') {
206 T( trace(T_TUNNEL
, "tun-slip: declared slip command `%s'", slipcmd
); )
209 uli
= strtoul(p
, &q
, 0);
210 if (uli
> INT_MAX
|| q
== p
)
216 ulo
= strtoul(p
, &q
, 0);
217 if (ulo
> INT_MAX
|| q
== p
)
220 if (*q
!= '=' || (n
= strcspn(q
+ 1, ":")) == 0)
225 fdflags(sl
->ifd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
226 fdflags(sl
->ofd
, O_NONBLOCK
, 0, FD_CLOEXEC
, FD_CLOEXEC
);
228 sl
->name
= xmalloc(n
+ 1);
231 memcpy(sl
->name
, q
+ 1, n
);
235 T( trace(T_TUNNEL
, "tun-slip: declared slipif %d,%d=%s",
236 sl
->ifd
, sl
->ofd
, sl
->name
); )
245 moan("bad slip interface list");
248 /* --- @t_create@ --- *
250 * Arguments: @peer *p@ = pointer to peer block
251 * @int fd@ = file descriptor of tunnel device (unused)
252 * @char **ifn@ = where to put the interface name
254 * Returns: A tunnel block if it worked, or null on failure.
256 * Use: Initializes a new tunnel.
259 static tunnel
*t_create(peer
*p
, int fd
, char **ifn
)
262 int pin
[2] = { -1, -1 }, pout
[2] = { -1, -1 };
268 static const char end
[] = { SL_END
, SL_END
};
270 /* --- Try to find a spare static interface --- */
272 for (sl
= slipifs
; sl
; sl
= sl
->next
) {
273 if (!(sl
->f
& F_INUSE
)) {
274 T( trace(T_TUNNEL
, "tun-slip: %s using static slipif %s",
275 p_name(p
), sl
->name
); )
280 /* --- If no dynamic interfaces are available, give up --- */
283 a_warn("TUN", "-", "slip", "no-slip-interfaces", A_END
);
287 /* --- Fork off a child process to create a dynamic SLIP interface --- */
289 if (pipe(pin
) || pipe(pout
)) {
290 a_warn("TUN", "-", "slip", "pipe-error", "?ERRNO", A_END
);
293 if ((kid
= fork()) < 0) {
294 a_warn("TUN", "-", "slip", "fork-error", "?ERRNO", A_END
);
298 close(pin
[1]); close(pout
[0]);
299 md
[0].cur
= pin
[0]; md
[0].want
= STDIN_FILENO
;
300 md
[1].cur
= pout
[1]; md
[1].want
= STDOUT_FILENO
;
302 execlp(slipcmd
, slipcmd
, p_name(p
), (char *)0);
306 /* --- Read the interface name --- */
309 close(pin
[0]); pin
[0] = -1;
310 close(pout
[1]); pout
[1] = -1;
313 if (read(pout
[0], &ch
, 1) != 1 || ch
== SL_END
) {
314 a_warn("TUN", "-", "slip", "read-ifname-failed", "?ERRNO", A_END
);
322 sl
->name
= xstrdup(d
.buf
);
328 T( trace(T_TUNNEL
, "tun-slip: %s using dynamic slipif %s",
329 p_name(p
), sl
->name
); )
330 fdflags(pout
[0], O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
331 fdflags(pin
[1], O_NONBLOCK
, 0, FD_CLOEXEC
, FD_CLOEXEC
);
333 /* --- Set up the new tunnel --- */
343 sel_initfile(&sel
, &t
->f
, sl
->ifd
, SEL_READ
, t_read
, t
);
345 write(sl
->ofd
, end
, sizeof(end
));
346 *ifn
= xstrdup(sl
->name
);
350 /* --- Tidy up after a failure --- */
353 #define CLOSE(fd) do if (fd != -1) close(fd); while (0)
354 CLOSE(pin
[0]); CLOSE(pout
[0]);
355 CLOSE(pin
[1]); CLOSE(pout
[1]);
357 if (kid
!= -1) kill(kid
, SIGTERM
);
358 if (sl
&& (sl
->f
& F_DYNAMIC
)) DESTROY(sl
);
363 /* --- @t_setifname@ --- *
365 * Arguments: @tunnel *t@ = pointer to tunnel block
366 * @const char *ifn@ = new interface name
370 * Use: Updates the interface name of a slip interface.
373 static void t_setifname(tunnel
*t
, const char *ifn
)
374 { xfree(t
->sl
->name
); t
->sl
->name
= xstrdup(ifn
); }
376 /* --- @t_inject@ --- *
378 * Arguments: @tunnel *t@ = pointer to tunnel block
379 * @buf *b@ = buffer to send
383 * Use: Injects a packet into the local network stack.
386 static void t_inject(tunnel
*t
, buf
*b
)
388 octet buf
[PKBUFSZ
* 2 + 2];
392 IF_TRACING(T_TUNNEL
, {
393 trace(T_TUNNEL
, "tun-slip: inject decrypted packet");
394 trace_block(T_PACKET
, "tun-slip: packet contents", BBASE(b
), BLEN(b
));
399 for (p
= BBASE(b
), l
= BCUR(b
); p
< l
; p
++) {
401 case SL_END
: *q
++ = SL_ESC
; *q
++ = SL_ESCEND
; break;
402 case SL_ESC
: *q
++ = SL_ESC
; *q
++ = SL_ESCESC
; break;
403 default: *q
++ = *p
; break;
407 IF_TRACING(T_TUNNEL
, {
408 trace_block(T_PACKET
, "tun-slip: SLIP-encapsulated contents",
411 write(t
->sl
->ofd
, buf
, q
- buf
);
414 /* --- @t_destroy@ --- *
416 * Arguments: @tunnel *t@ = pointer to tunnel block
420 * Use: Destroys a tunnel.
423 static void t_destroy(tunnel
*t
)
427 /* --- If it reported EOF, leave it out-of-action --- */
429 if (!(t
->st
& ST_EOF
)) {
433 if (sl
&& (sl
->f
& F_DYNAMIC
)) {
434 T( trace(T_TUNNEL
, "tun-slip: releasing dynamic slipif %s", sl
->name
); )
437 kill(sl
->kid
, SIGTERM
);
444 const tunnel_ops tun_slip
= {
454 /*----- That's all, folks -------------------------------------------------*/