5 * Tunnel interface based on Linux TUN/TAP driver
7 * (c) 2003 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 ------------------------------------------------------*/
36 # include <sys/ioctl.h>
37 # include <linux/if.h>
38 # include <linux/if_tun.h>
41 /*----- Main code ---------------------------------------------------------*/
46 const tunnel_ops *ops; /* Pointer to operations */
47 sel_file f; /* Selector for TUN/TAP device */
48 struct peer *p; /* Pointer to my peer */
49 char ifn[IFNAMSIZ]; /* Interface name buffer */
54 * Arguments: @int fd@ = file descriptor to read
55 * @unsigned mode@ = what's happened
56 * @void *v@ = pointer to tunnel block
60 * Use: Reads data from the tunnel.
63 static void t_read(int fd, unsigned mode, void *v)
69 n = read(fd, buf_i, sizeof(buf_i));
71 a_warn("TUN %s read-error -- %s", t->ifn, strerror(errno));
74 IF_TRACING(T_TUNNEL, {
75 trace(T_TUNNEL, "tun-linux: packet arrived");
76 trace_block(T_PACKET, "tun-linux: packet contents", buf_i, n);
78 buf_init(&b, buf_i, n);
88 * Use: Initializes the tunneling system. Maybe this will require
89 * opening file descriptors or something.
92 static void t_init(void) { return; }
94 /* --- @t_create@ --- *
96 * Arguments: @peer *p@ = pointer to peer block
98 * Returns: A tunnel block if it worked, or null on failure.
100 * Use: Initializes a new tunnel.
103 static tunnel *t_create(peer *p)
110 if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
111 a_warn("TUN - open-error /dev/net/tun -- %s", strerror(errno));
114 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
115 memset(&iff, 0, sizeof(iff));
117 iff.ifr_flags = IFF_TUN | IFF_NO_PI;
118 if ((f = ioctl(fd, TUNSETIFF, &iff)) < 0) {
119 a_warn("TUN - linux config-error -- %s", strerror(errno));
126 sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
128 iff.ifr_name[IFNAMSIZ - 1] = 0;
129 strcpy(t->ifn, iff.ifr_name);
130 T( trace(T_TUNNEL, "tun-linux: attached interface %s to peer `%s'",
131 t->ifn, p_name(p)); )
135 /* --- @t_ifname@ --- *
137 * Arguments: @tunnel *t@ = pointer to tunnel block
139 * Returns: A pointer to the tunnel's interface name.
142 static const char *t_ifname(tunnel *t) { return (t->ifn); }
144 /* --- @t_inject@ --- *
146 * Arguments: @tunnel *t@ = pointer to tunnel block
147 * @buf *b@ = buffer to send
151 * Use: Injects a packet into the local network stack.
154 static void t_inject(tunnel *t, buf *b)
156 IF_TRACING(T_TUNNEL, {
157 trace(T_TUNNEL, "tun-linux: inject decrypted packet");
158 trace_block(T_PACKET, "tunnel: packet contents", BBASE(b), BLEN(b));
160 write(t->f.fd, BBASE(b), BLEN(b));
163 /* --- @t_destroy@ --- *
165 * Arguments: @tunnel *t@ = pointer to tunnel block
169 * Use: Destroys a tunnel.
172 static void t_destroy(tunnel *t)
179 const tunnel_ops tun_linux = {
190 /*----- That's all, folks -------------------------------------------------*/