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_EOF 4u /* File descriptor reported EOF */
54 size_t n
; /* Number of bytes used in buffer */
55 octet buf
[PKBUFSZ
]; /* Buffer for incoming data */
58 /*----- Static variables --------------------------------------------------*/
60 static slipif
*slipifs
; /* List of available interfaces */
61 static const char *slipcmd
; /* Script to make new interfaces */
63 /*----- Main code ---------------------------------------------------------*/
67 * Arguments: @int fd@ = file descriptor to read
68 * @unsigned mode@ = what's happened
69 * @void *v@ = pointer to tunnel block
73 * Use: Reads data from the tunnel.
76 static void t_read(int fd
, unsigned mode
, void *v
)
80 const octet
*p
, *l
, *ll
;
86 /* --- Read the input data --- */
88 n
= read(fd
, buf_t
, sizeof(buf_t
));
91 #if defined(EWOULDBLOCK) && EWOULDBLOCK != EAGAIN
92 errno
== EWOULDBLOCK
||
96 a_warn("TUN", "%s", p_ifname(t
->p
), "read-error", "?ERRNO", A_END
);
100 a_warn("TUN", "%s", p_ifname(t
->p
), "slip", "eof", A_END
);
105 IF_TRACING(T_TUNNEL
, {
106 trace_block(T_PACKET
, "tun-slip: SLIP-encapsulated data",
110 /* --- Decapsulate the packet --- */
112 for (p
= buf_t
, l
= p
+ n
, st
= t
->st
,
113 q
= t
->buf
+ t
->n
, ll
= t
->buf
+ sizeof(t
->buf
);
121 else if (st
& ST_ESC
)
122 a_warn("TUN", "%s", p_ifname(t
->p
), "slip", "escape-end", A_END
);
123 else if (q
== t
->buf
) {
124 T( trace(T_TUNNEL
, "tun-slip: empty packet"); )
126 IF_TRACING(T_TUNNEL
, {
127 trace(T_TUNNEL
, "tun-slip: packet arrived");
128 trace_block(T_PACKET
, "tun-slip: packet contents",
131 buf_init(&b
, t
->buf
, q
- t
->buf
);
135 st
&= ~(ST_ESC
| ST_BAD
);
138 if ((st
& ST_ESC
) && !(st
& ST_BAD
)) {
139 a_warn("TUN", "%s", p_ifname(t
->p
), "slip", "bad-escape", A_END
);
153 if ((st
& ST_ESC
) && !(st
& ST_BAD
)) {
154 a_warn("TUN", "%s", p_ifname(t
->p
), "slip", "bad-escape", A_END
);
158 if (!(st
& ST_BAD
)) {
162 a_warn("TUN", "%s", p_ifname(t
->p
), "slip", "overflow", A_END
);
175 /* --- @t_init@ --- *
181 * Use: Initializes the tunneling system. Maybe this will require
182 * opening file descriptors or something.
185 static void t_init(void)
189 slipif
*sl
, **tail
= &slipifs
;
190 unsigned long uli
, ulo
;
193 if ((p
= getenv("TRIPE_SLIPIF")) == 0)
196 /* --- Build the list of available interfaces --- */
202 if (*p
== '/' || *p
== '.') {
204 T( trace(T_TUNNEL
, "tun-slip: declared slip command `%s'", slipcmd
); )
207 uli
= strtoul(p
, &q
, 0);
208 if (uli
> INT_MAX
|| q
== p
)
214 ulo
= strtoul(p
, &q
, 0);
215 if (ulo
> INT_MAX
|| q
== p
)
218 if (*q
!= '=' || (n
= strcspn(q
+ 1, ":")) == 0)
223 fdflags(sl
->ifd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
224 fdflags(sl
->ofd
, O_NONBLOCK
, 0, FD_CLOEXEC
, FD_CLOEXEC
);
226 sl
->name
= xmalloc(n
+ 1);
229 memcpy(sl
->name
, q
+ 1, n
);
233 T( trace(T_TUNNEL
, "tun-slip: declared slipif %d,%d=%s",
234 sl
->ifd
, sl
->ofd
, sl
->name
); )
243 moan("bad slip interface list");
246 /* --- @t_create@ --- *
248 * Arguments: @peer *p@ = pointer to peer block
249 * @char **ifn@ = where to put the interface name
251 * Returns: A tunnel block if it worked, or null on failure.
253 * Use: Initializes a new tunnel.
256 static tunnel
*t_create(peer
*p
, char **ifn
)
259 int pin
[2] = { -1, -1 }, pout
[2] = { -1, -1 };
264 static const char end
[] = { SL_END
, SL_END
};
266 /* --- Try to find a spare static interface --- */
268 for (sl
= slipifs
; sl
; sl
= sl
->next
) {
269 if (!(sl
->f
& F_INUSE
)) {
270 T( trace(T_TUNNEL
, "tun-slip: %s using static slipif %s",
271 p_name(p
), sl
->name
); )
276 /* --- If no dynamic interfaces are available, give up --- */
279 a_warn("TUN", "-", "slip", "no-slip-interfaces", A_END
);
283 /* --- Fork off a child process to create a dynamic SLIP interface --- */
285 if (pipe(pin
) || pipe(pout
)) {
286 a_warn("TUN", "-", "slip", "pipe-error", "?ERRNO", A_END
);
289 if ((kid
= fork()) < 0) {
290 a_warn("TUN", "-", "slip", "fork-error", "?ERRNO", A_END
);
296 dup2(pin
[0], STDIN_FILENO
);
297 dup2(pout
[1], STDOUT_FILENO
);
298 execlp(slipcmd
, slipcmd
, p_name(p
), (char *)0);
302 /* --- Read the interface name --- */
305 close(pin
[0]); pin
[0] = -1;
306 close(pout
[1]); pout
[1] = -1;
309 if (read(pout
[0], &ch
, 1) != 1 || ch
== SL_END
) {
310 a_warn("TUN", "-", "slip", "read-ifname-failed", "?ERRNO", A_END
);
318 sl
->name
= xstrdup(d
.buf
);
324 T( trace(T_TUNNEL
, "tun-slip: %s using dynamic slipif %s",
325 p_name(p
), sl
->name
); )
326 fdflags(pout
[0], O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
327 fdflags(pin
[1], O_NONBLOCK
, 0, FD_CLOEXEC
, FD_CLOEXEC
);
329 /* --- Set up the new tunnel --- */
339 sel_initfile(&sel
, &t
->f
, sl
->ifd
, SEL_READ
, t_read
, t
);
341 write(sl
->ofd
, end
, sizeof(end
));
342 *ifn
= xstrdup(sl
->name
);
346 /* --- Tidy up after a failure --- */
349 #define CLOSE(fd) do if (fd != -1) close(fd); while (0)
350 CLOSE(pin
[0]); CLOSE(pout
[0]);
351 CLOSE(pin
[1]); CLOSE(pout
[1]);
353 if (kid
!= -1) kill(kid
, SIGTERM
);
354 if (sl
&& (sl
->f
& F_DYNAMIC
)) DESTROY(sl
);
359 /* --- @t_setifname@ --- *
361 * Arguments: @tunnel *t@ = pointer to tunnel block
362 * @const char *ifn@ = new interface name
366 * Use: Updates the interface name of a slip interface.
369 static void t_setifname(tunnel
*t
, const char *ifn
)
370 { xfree(t
->sl
->name
); t
->sl
->name
= xstrdup(ifn
); }
372 /* --- @t_inject@ --- *
374 * Arguments: @tunnel *t@ = pointer to tunnel block
375 * @buf *b@ = buffer to send
379 * Use: Injects a packet into the local network stack.
382 static void t_inject(tunnel
*t
, buf
*b
)
384 octet buf
[PKBUFSZ
* 2 + 2];
388 IF_TRACING(T_TUNNEL
, {
389 trace(T_TUNNEL
, "tun-slip: inject decrypted packet");
390 trace_block(T_PACKET
, "tun-slip: packet contents", BBASE(b
), BLEN(b
));
395 for (p
= BBASE(b
), l
= BCUR(b
); p
< l
; p
++) {
397 case SL_END
: *q
++ = SL_ESC
; *q
++ = SL_ESCEND
; break;
398 case SL_ESC
: *q
++ = SL_ESC
; *q
++ = SL_ESCESC
; break;
399 default: *q
++ = *p
; break;
403 IF_TRACING(T_TUNNEL
, {
404 trace_block(T_PACKET
, "tun-slip: SLIP-encapsulated contents",
407 write(t
->sl
->ofd
, buf
, q
- buf
);
410 /* --- @t_destroy@ --- *
412 * Arguments: @tunnel *t@ = pointer to tunnel block
416 * Use: Destroys a tunnel.
419 static void t_destroy(tunnel
*t
)
423 /* --- If it reported EOF, leave it out-of-action --- */
425 if (!(t
->st
& ST_EOF
)) {
429 if (sl
&& (sl
->f
& F_DYNAMIC
)) {
430 T( trace(T_TUNNEL
, "tun-slip: releasing dynamic slipif %s", sl
->name
); )
433 kill(sl
->kid
, SIGTERM
);
440 const tunnel_ops tun_slip
= {
449 /*----- That's all, folks -------------------------------------------------*/