Commit | Line | Data |
---|---|---|
37075862 | 1 | /* -*-c-*- |
2 | * | |
3cdc3f3a | 3 | * $Id$ |
37075862 | 4 | * |
5 | * Tunnel interface based on Linux TUN/TAP driver | |
6 | * | |
7 | * (c) 2003 Straylight/Edgeware | |
8 | */ | |
9 | ||
10 | /*----- Licensing notice --------------------------------------------------* | |
11 | * | |
12 | * This file is part of Trivial IP Encryption (TrIPE). | |
13 | * | |
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. | |
18 | * | |
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. | |
23 | * | |
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. | |
27 | */ | |
28 | ||
37075862 | 29 | /*----- Header files ------------------------------------------------------*/ |
30 | ||
42da2a58 | 31 | #define TUN_INTERNALS |
32 | ||
37075862 | 33 | #include "tripe.h" |
34 | ||
42da2a58 | 35 | #ifdef TUN_LINUX |
36 | # include <sys/ioctl.h> | |
37 | # include <linux/if.h> | |
38 | # include <linux/if_tun.h> | |
39 | #endif | |
37075862 | 40 | |
41 | /*----- Main code ---------------------------------------------------------*/ | |
42 | ||
42da2a58 | 43 | #ifdef TUN_LINUX |
44 | ||
45 | struct tunnel { | |
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 */ | |
50 | }; | |
37075862 | 51 | |
52 | /* --- @t_read@ --- * | |
53 | * | |
54 | * Arguments: @int fd@ = file descriptor to read | |
55 | * @unsigned mode@ = what's happened | |
56 | * @void *v@ = pointer to tunnel block | |
57 | * | |
58 | * Returns: --- | |
59 | * | |
60 | * Use: Reads data from the tunnel. | |
61 | */ | |
62 | ||
63 | static void t_read(int fd, unsigned mode, void *v) | |
64 | { | |
65 | tunnel *t = v; | |
66 | ssize_t n; | |
67 | buf b; | |
68 | ||
69 | n = read(fd, buf_i, sizeof(buf_i)); | |
70 | if (n < 0) { | |
f43df819 | 71 | a_warn("TUN", "%s", t->ifn, "read-error", "?ERRNO", A_END); |
37075862 | 72 | return; |
73 | } | |
74 | IF_TRACING(T_TUNNEL, { | |
060ca767 | 75 | trace(T_TUNNEL, "tun-linux: packet arrived"); |
76 | trace_block(T_PACKET, "tun-linux: packet contents", buf_i, n); | |
37075862 | 77 | }) |
78 | buf_init(&b, buf_i, n); | |
79 | p_tun(t->p, &b); | |
80 | } | |
81 | ||
42da2a58 | 82 | /* --- @t_init@ --- * |
37075862 | 83 | * |
84 | * Arguments: --- | |
85 | * | |
86 | * Returns: --- | |
87 | * | |
88 | * Use: Initializes the tunneling system. Maybe this will require | |
89 | * opening file descriptors or something. | |
90 | */ | |
91 | ||
42da2a58 | 92 | static void t_init(void) { return; } |
37075862 | 93 | |
42da2a58 | 94 | /* --- @t_create@ --- * |
37075862 | 95 | * |
42da2a58 | 96 | * Arguments: @peer *p@ = pointer to peer block |
37075862 | 97 | * |
42da2a58 | 98 | * Returns: A tunnel block if it worked, or null on failure. |
37075862 | 99 | * |
100 | * Use: Initializes a new tunnel. | |
101 | */ | |
102 | ||
42da2a58 | 103 | static tunnel *t_create(peer *p) |
37075862 | 104 | { |
105 | int fd; | |
106 | int f; | |
107 | struct ifreq iff; | |
42da2a58 | 108 | tunnel *t; |
37075862 | 109 | |
110 | if ((fd = open("/dev/net/tun", O_RDWR)) < 0) { | |
f43df819 MW |
111 | a_warn("TUN", "-", "linux", |
112 | "open-error", "/dev/net/tun", "?ERRNO", | |
113 | A_END); | |
42da2a58 | 114 | return (0); |
37075862 | 115 | } |
116 | fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); | |
060ca767 | 117 | memset(&iff, 0, sizeof(iff)); |
37075862 | 118 | iff.ifr_name[0] = 0; |
b9066fbb | 119 | iff.ifr_flags = IFF_TUN | IFF_NO_PI; |
37075862 | 120 | if ((f = ioctl(fd, TUNSETIFF, &iff)) < 0) { |
f43df819 | 121 | a_warn("TUN", "-", "linux", "config-error", "?ERRNO", A_END); |
37075862 | 122 | close(fd); |
42da2a58 | 123 | return (0); |
37075862 | 124 | } |
42da2a58 | 125 | t = CREATE(tunnel); |
126 | t->ops = &tun_linux; | |
37075862 | 127 | t->p = p; |
128 | sel_initfile(&sel, &t->f, fd, SEL_READ, t_read, t); | |
129 | sel_addfile(&t->f); | |
130 | iff.ifr_name[IFNAMSIZ - 1] = 0; | |
131 | strcpy(t->ifn, iff.ifr_name); | |
060ca767 | 132 | T( trace(T_TUNNEL, "tun-linux: attached interface %s to peer `%s'", |
37075862 | 133 | t->ifn, p_name(p)); ) |
42da2a58 | 134 | return (t); |
37075862 | 135 | } |
136 | ||
42da2a58 | 137 | /* --- @t_ifname@ --- * |
37075862 | 138 | * |
139 | * Arguments: @tunnel *t@ = pointer to tunnel block | |
140 | * | |
141 | * Returns: A pointer to the tunnel's interface name. | |
142 | */ | |
143 | ||
42da2a58 | 144 | static const char *t_ifname(tunnel *t) { return (t->ifn); } |
37075862 | 145 | |
42da2a58 | 146 | /* --- @t_inject@ --- * |
37075862 | 147 | * |
148 | * Arguments: @tunnel *t@ = pointer to tunnel block | |
149 | * @buf *b@ = buffer to send | |
150 | * | |
151 | * Returns: --- | |
152 | * | |
153 | * Use: Injects a packet into the local network stack. | |
154 | */ | |
155 | ||
42da2a58 | 156 | static void t_inject(tunnel *t, buf *b) |
37075862 | 157 | { |
158 | IF_TRACING(T_TUNNEL, { | |
060ca767 | 159 | trace(T_TUNNEL, "tun-linux: inject decrypted packet"); |
37075862 | 160 | trace_block(T_PACKET, "tunnel: packet contents", BBASE(b), BLEN(b)); |
161 | }) | |
162 | write(t->f.fd, BBASE(b), BLEN(b)); | |
163 | } | |
164 | ||
42da2a58 | 165 | /* --- @t_destroy@ --- * |
37075862 | 166 | * |
167 | * Arguments: @tunnel *t@ = pointer to tunnel block | |
168 | * | |
169 | * Returns: --- | |
170 | * | |
171 | * Use: Destroys a tunnel. | |
172 | */ | |
173 | ||
42da2a58 | 174 | static void t_destroy(tunnel *t) |
37075862 | 175 | { |
176 | sel_rmfile(&t->f); | |
177 | close(t->f.fd); | |
42da2a58 | 178 | DESTROY(t); |
37075862 | 179 | } |
180 | ||
42da2a58 | 181 | const tunnel_ops tun_linux = { |
182 | "linux", | |
183 | t_init, | |
184 | t_create, | |
185 | t_ifname, | |
186 | t_inject, | |
187 | t_destroy | |
188 | }; | |
189 | ||
190 | #endif | |
191 | ||
37075862 | 192 | /*----- That's all, folks -------------------------------------------------*/ |