3 * $Id: tun-linux.c,v 1.3 2004/04/08 01:36:17 mdw Exp $
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 ------------------------------------------------------*/
33 #include <sys/ioctl.h>
35 /*----- Main code ---------------------------------------------------------*/
37 #if TUN_TYPE != TUN_LINUX
38 # error "Tunnel type mismatch: fix the Makefile"
43 * Arguments: @int fd@ = file descriptor to read
44 * @unsigned mode@ = what's happened
45 * @void *v@ = pointer to tunnel block
49 * Use: Reads data from the tunnel.
52 static void t_read(int fd, unsigned mode, void *v)
58 n = read(fd, buf_i, sizeof(buf_i));
60 a_warn("tunnel read failed (%s): %s", t->ifn, strerror(errno));
63 IF_TRACING(T_TUNNEL, {
64 trace(T_TUNNEL, "tunnel: packet arrived");
65 trace_block(T_PACKET, "tunnel: packet contents", buf_i, n);
67 buf_init(&b, buf_i, n);
71 /* --- @tun_init@ --- *
77 * Use: Initializes the tunneling system. Maybe this will require
78 * opening file descriptors or something.
86 /* --- @tun_create@ --- *
88 * Arguments: @tunnel *t@ = pointer to tunnel block
89 * @peer *p@ = pointer to peer block
91 * Returns: Zero if it worked, nonzero on failure.
93 * Use: Initializes a new tunnel.
96 int tun_create(tunnel *t, peer *p)
102 if ((fd = open("/dev/net/tun", O_RDWR)) < 0) {
103 a_warn("open `/dev/net/tun' failed: %s", strerror(errno));
106 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
108 iff.ifr_flags = IFF_TUN;
109 if ((f = ioctl(fd, TUNSETIFF, &iff)) < 0) {
110 a_warn("couldn't set configure new TUN/TAP interface: %s",
116 sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t);
118 iff.ifr_name[IFNAMSIZ - 1] = 0;
119 strcpy(t->ifn, iff.ifr_name);
120 T( trace(T_TUNNEL, "tunnel: attached interface %s to peer `%s'",
121 t->ifn, p_name(p)); )
125 /* --- @tun_ifname@ --- *
127 * Arguments: @tunnel *t@ = pointer to tunnel block
129 * Returns: A pointer to the tunnel's interface name.
132 const char *tun_ifname(tunnel *t)
137 /* --- @tun_inject@ --- *
139 * Arguments: @tunnel *t@ = pointer to tunnel block
140 * @buf *b@ = buffer to send
144 * Use: Injects a packet into the local network stack.
147 void tun_inject(tunnel *t, buf *b)
149 IF_TRACING(T_TUNNEL, {
150 trace(T_TUNNEL, "tunnel: inject decrypted packet");
151 trace_block(T_PACKET, "tunnel: packet contents", BBASE(b), BLEN(b));
153 write(t->f.fd, BBASE(b), BLEN(b));
156 /* --- @tun_destroy@ --- *
158 * Arguments: @tunnel *t@ = pointer to tunnel block
162 * Use: Destroys a tunnel.
165 void tun_destroy(tunnel *t)
171 /*----- That's all, folks -------------------------------------------------*/