3 * Tunnel interface based on Linux TUN/TAP driver
5 * (c) 2003 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 ------------------------------------------------------*/
34 # include <sys/ioctl.h>
35 # include <linux/if.h>
36 # include <linux/if_tun.h>
39 /*----- Main code ---------------------------------------------------------*/
44 const tunnel_ops
*ops
; /* Pointer to operations */
45 sel_file f
; /* Selector for TUN/TAP device */
46 struct peer
*p
; /* Pointer to my peer */
51 * Arguments: @int fd@ = file descriptor to read
52 * @unsigned mode@ = what's happened
53 * @void *v@ = pointer to tunnel block
57 * Use: Reads data from the tunnel.
60 static void t_read(int fd
, unsigned mode
, void *v
)
66 n
= read(fd
, buf_i
, sizeof(buf_i
));
68 a_warn("TUN", "%s", p_ifname(t
->p
), "read-error", "?ERRNO", A_END
);
71 IF_TRACING(T_TUNNEL
, {
72 trace(T_TUNNEL
, "tun-linux: packet arrived");
73 trace_block(T_PACKET
, "tun-linux: packet contents", buf_i
, n
);
75 buf_init(&b
, buf_i
, n
);
85 * Use: Initializes the tunneling system. Maybe this will require
86 * opening file descriptors or something.
89 static void t_init(void) { return; }
91 /* --- @t_create@ --- *
93 * Arguments: @peer *p@ = pointer to peer block
94 * @int fd@ = file descriptor of tunnel device
95 * @char **ifn@ = where to put the interface name
97 * Returns: A tunnel block if it worked, or null on failure.
99 * Use: Initializes a new tunnel.
102 static tunnel
*t_create(peer
*p
, int fd
, char **ifn
)
106 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, FD_CLOEXEC
, FD_CLOEXEC
);
110 sel_initfile(&sel
, &t
->f
, fd
, SEL_READ
, t_read
, t
);
115 /* --- @t_inject@ --- *
117 * Arguments: @tunnel *t@ = pointer to tunnel block
118 * @buf *b@ = buffer to send
122 * Use: Injects a packet into the local network stack.
125 static void t_inject(tunnel
*t
, buf
*b
)
127 IF_TRACING(T_TUNNEL
, {
128 trace(T_TUNNEL
, "tun-linux: inject decrypted packet");
129 trace_block(T_PACKET
, "tunnel: packet contents", BBASE(b
), BLEN(b
));
131 write(t
->f
.fd
, BBASE(b
), BLEN(b
));
134 /* --- @t_destroy@ --- *
136 * Arguments: @tunnel *t@ = pointer to tunnel block
140 * Use: Destroys a tunnel.
143 static void t_destroy(tunnel
*t
)
144 { sel_rmfile(&t
->f
); close(t
->f
.fd
); DESTROY(t
); }
146 const tunnel_ops tun_linux
= {
158 /*----- That's all, folks -------------------------------------------------*/