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 ------------------------------------------------------*/
33 /*----- Static variables --------------------------------------------------*/
35 static slipif
*slipifs
; /* List of available interfaces */
37 /*----- Main code ---------------------------------------------------------*/
39 #if TUN_TYPE != TUN_SLIP
40 # error "Tunnel type mismatch: fix the Makefile"
45 #define SL_ESCEND 0xdc
46 #define SL_ESCESC 0xdd
50 * Arguments: @int fd@ = file descriptor to read
51 * @unsigned mode@ = what's happened
52 * @void *v@ = pointer to tunnel block
56 * Use: Reads data from the tunnel.
59 static void t_read(int fd
, unsigned mode
, void *v
)
63 const octet
*p
, *l
, *ll
;
69 /* --- Read the input data --- */
71 n
= read(fd
, buf_t
, sizeof(buf_t
));
75 errno
== EWOULDBLOCK
||
79 a_warn("TUN %s read-error -- %s", t
->sl
->name
, strerror(errno
));
83 a_warn("TUN %s slip eof", t
->sl
->name
);
88 IF_TRACING(T_TUNNEL
, {
89 trace_block(T_PACKET
, "tunnel: SLIP-encapsulated data",
93 /* --- Decapsulate the packet --- */
95 for (p
= buf_t
, l
= p
+ n
, st
= t
->st
,
96 q
= t
->buf
+ t
->n
, ll
= t
->buf
+ sizeof(t
->buf
);
104 else if (st
& SLIPST_ESC
)
105 a_warn("TUN %s slip escape-end", t
->sl
->name
);
106 else if (q
== t
->buf
) {
107 T( trace(T_TUNNEL
, "tunnel: empty packet"); )
109 IF_TRACING(T_TUNNEL
, {
110 trace(T_TUNNEL
, "tunnel: packet arrived");
111 trace_block(T_PACKET
, "tunnel: packet contents",
114 buf_init(&b
, t
->buf
, q
- t
->buf
);
118 st
&= ~(SLIPST_ESC
| SLIPST_BAD
);
121 if ((st
& SLIPST_ESC
) && !(st
& SLIPST_BAD
)) {
122 a_warn("TUN %s slip bad-escape", t
->sl
->name
);
136 if ((st
& SLIPST_ESC
) && !(st
& SLIPST_BAD
)) {
137 a_warn("TUN %s slip bad-escape", t
->sl
->name
);
141 if (!(st
& SLIPST_BAD
)) {
145 a_warn("TUN %s slip overflow", t
->sl
->name
);
158 /* --- @tun_init@ --- *
164 * Use: Initializes the tunneling system. Maybe this will require
165 * opening file descriptors or something.
172 slipif
*sl
, **tail
= &slipifs
;
173 unsigned long uli
, ulo
;
176 /* --- Build the list of available interfaces --- */
178 if ((p
= getenv("TRIPE_SLIPIF")) == 0)
179 die(1, "no slip interfaces listed: set TRIPE_SLIPIF");
184 uli
= strtoul(p
, &q
, 0);
185 if (uli
> INT_MAX
|| q
== p
)
191 ulo
= strtoul(p
, &q
, 0);
192 if (ulo
> INT_MAX
|| q
== p
)
195 if (*q
!= '=' || (n
= strcspn(q
+ 1, ":")) == 0)
201 sl
->name
= xmalloc(n
+ 1);
203 memcpy(sl
->name
, q
+ 1, n
);
207 T( trace(T_TUNNEL
, "tunnel: declared slipif %d,%d=%s",
208 sl
->ifd
, sl
->ofd
, sl
->name
); )
217 die(1, "bad slip interface list");
220 /* --- @tun_create@ --- *
222 * Arguments: @tunnel *t@ = pointer to tunnel block
223 * @peer *p@ = pointer to peer block
225 * Returns: Zero if it worked, nonzero on failure.
227 * Use: Initializes a new tunnel.
230 int tun_create(tunnel
*t
, peer
*p
)
233 static const char end
[] = { SL_END
, SL_END
};
235 for (sl
= slipifs
; sl
; sl
= sl
->next
) {
236 if (!(sl
->f
& SLIPIFF_INUSE
))
239 a_warn("TUN - slip no-slip-interfaces");
247 sl
->f
|= SLIPIFF_INUSE
;
248 sel_initfile(&sel
, &t
->f
, sl
->ifd
, SEL_READ
, t_read
, t
);
250 write(sl
->ofd
, end
, sizeof(end
));
251 T( trace(T_TUNNEL
, "tunnel: attached interface %s to peer `%s'",
252 sl
->name
, p_name(p
)); )
256 /* --- @tun_ifname@ --- *
258 * Arguments: @tunnel *t@ = pointer to tunnel block
260 * Returns: A pointer to the tunnel's interface name.
263 const char *tun_ifname(tunnel
*t
)
265 return (t
->sl
->name
);
268 /* --- @tun_inject@ --- *
270 * Arguments: @tunnel *t@ = pointer to tunnel block
271 * @buf *b@ = buffer to send
275 * Use: Injects a packet into the local network stack.
278 void tun_inject(tunnel
*t
, buf
*b
)
280 octet buf
[PKBUFSZ
* 2 + 2];
284 IF_TRACING(T_TUNNEL
, {
285 trace(T_TUNNEL
, "tunnel: inject decrypted packet");
286 trace_block(T_PACKET
, "tunnel: packet contents", BBASE(b
), BLEN(b
));
291 for (p
= BBASE(b
), l
= BCUR(b
); p
< l
; p
++) {
293 case SL_END
: *q
++ = SL_ESC
; *q
++ = SL_ESCEND
; break;
294 case SL_ESC
: *q
++ = SL_ESC
; *q
++ = SL_ESCESC
; break;
295 default: *q
++ = *p
; break;
299 IF_TRACING(T_TUNNEL
, {
300 trace_block(T_PACKET
, "tunnel: SLIP-encapsulated contents",
303 write(t
->sl
->ofd
, buf
, q
- buf
);
306 /* --- @tun_destroy@ --- *
308 * Arguments: @tunnel *t@ = pointer to tunnel block
312 * Use: Destroys a tunnel.
315 void tun_destroy(tunnel
*t
)
317 /* --- If it reported EOF, leave it out-of-action --- */
319 if (!(t
->st
& SLIPST_EOF
)) {
321 t
->sl
->f
&= ~SLIPIFF_INUSE
;
325 /*----- That's all, folks -------------------------------------------------*/