1 |
/* |
2 |
vpn_tcp.C -- handle the tcp part of the protocol. |
3 |
Copyright (C) 2003-2007 Marc Lehmann <gvpe@schmorp.de> |
4 |
|
5 |
This file is part of GVPE. |
6 |
|
7 |
GVPE is free software; you can redistribute it and/or modify |
8 |
it under the terms of the GNU General Public License as published by |
9 |
the Free Software Foundation; either version 2 of the License, or |
10 |
(at your option) any later version. |
11 |
|
12 |
This program is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU General Public License |
18 |
along with gvpe; if not, write to the Free Software |
19 |
Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 |
*/ |
21 |
|
22 |
#include "config.h" |
23 |
|
24 |
#if ENABLE_TCP |
25 |
|
26 |
// tcp processing is extremely ugly, since the gvpe protocol is simply |
27 |
// designed for unreliable datagram networks. tcp is implemented by |
28 |
// multiplexing packets over tcp. errors are completely ignored, as we |
29 |
// rely on the higher level layers to time out and reconnect. |
30 |
|
31 |
#include <cstring> |
32 |
|
33 |
#include <sys/types.h> |
34 |
#include <sys/socket.h> |
35 |
#include <sys/wait.h> |
36 |
#include <sys/uio.h> |
37 |
#include <errno.h> |
38 |
#include <time.h> |
39 |
#include <unistd.h> |
40 |
#include <fcntl.h> |
41 |
|
42 |
#include <map> |
43 |
|
44 |
#include "netcompat.h" |
45 |
|
46 |
#include "vpn.h" |
47 |
|
48 |
#if ENABLE_HTTP_PROXY |
49 |
# include "conf.h" |
50 |
#endif |
51 |
|
52 |
struct tcp_connection; |
53 |
|
54 |
struct lt_sockinfo |
55 |
{ |
56 |
bool operator()(const sockinfo *a, const sockinfo *b) const |
57 |
{ |
58 |
return *a < *b; |
59 |
} |
60 |
}; |
61 |
|
62 |
struct tcp_si_map : public map<const sockinfo *, tcp_connection *, lt_sockinfo> |
63 |
{ |
64 |
inline void cleaner_cb (ev::timer &w, int revents); ev::timer cleaner; |
65 |
|
66 |
tcp_si_map () |
67 |
{ |
68 |
ev_default_loop (0); |
69 |
cleaner.set<tcp_si_map, &tcp_si_map::cleaner_cb> (this); |
70 |
cleaner.start (::conf.keepalive / 2, ::conf.keepalive / 2); |
71 |
} |
72 |
|
73 |
} tcp_si; |
74 |
|
75 |
struct tcp_connection : ev::io |
76 |
{ |
77 |
int tos; |
78 |
tstamp last_activity; |
79 |
const sockinfo si; |
80 |
vpn &v; |
81 |
bool active; // this connection has been actively established |
82 |
enum { ERROR, IDLE, CONNECTING, CONNECTING_PROXY, ESTABLISHED } state; |
83 |
|
84 |
vpn_packet *r_pkt; |
85 |
u32 r_len, r_ofs; |
86 |
|
87 |
vpn_packet *w_pkt; |
88 |
u32 w_len, w_ofs; |
89 |
|
90 |
#if ENABLE_HTTP_PROXY |
91 |
char *proxy_req; |
92 |
int proxy_req_len; |
93 |
#endif |
94 |
|
95 |
inline void tcpv4_ev (ev::io &w, int revents); |
96 |
|
97 |
bool send_packet (vpn_packet *pkt, int tos); |
98 |
bool write_packet (); |
99 |
|
100 |
void error (); // abort conenction && cleanup |
101 |
|
102 |
operator tcp_si_map::value_type() |
103 |
{ |
104 |
return tcp_si_map::value_type (&si, this); |
105 |
} |
106 |
|
107 |
tcp_connection (int fd_, const sockinfo &si_, vpn &v_); |
108 |
~tcp_connection (); |
109 |
}; |
110 |
|
111 |
void tcp_si_map::cleaner_cb (ev::timer &w, int revents) |
112 |
{ |
113 |
tstamp to = ev_now () - ::conf.keepalive - 30 - 60; |
114 |
|
115 |
for (iterator i = begin (); i != end(); ) |
116 |
if (i->second->last_activity >= to) |
117 |
++i; |
118 |
else |
119 |
{ |
120 |
delete i->second; |
121 |
erase (i); |
122 |
i = begin (); |
123 |
} |
124 |
} |
125 |
|
126 |
void |
127 |
vpn::tcpv4_ev (ev::io &w, int revents) |
128 |
{ |
129 |
if (revents & EV_READ) |
130 |
{ |
131 |
struct sockaddr_in sa; |
132 |
socklen_t sa_len = sizeof (sa); |
133 |
int len; |
134 |
|
135 |
int fd = accept (w.fd, (sockaddr *)&sa, &sa_len); |
136 |
|
137 |
if (fd >= 0) |
138 |
{ |
139 |
fcntl (fd, F_SETFL, O_NONBLOCK); |
140 |
fcntl (fd, F_SETFD, FD_CLOEXEC); |
141 |
|
142 |
sockinfo si(sa, PROT_TCPv4); |
143 |
|
144 |
slog (L_DEBUG, _("%s: accepted tcp connection"), (const char *)si);//D |
145 |
|
146 |
tcp_connection *i = new tcp_connection (fd, si, *this); |
147 |
tcp_si.insert (*i); |
148 |
} |
149 |
} |
150 |
} |
151 |
|
152 |
bool |
153 |
vpn::send_tcpv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
154 |
{ |
155 |
tcp_si_map::iterator info = tcp_si.find (&si); |
156 |
|
157 |
tcp_connection *i; |
158 |
|
159 |
if (info == tcp_si.end ()) |
160 |
{ |
161 |
i = new tcp_connection (-1, si, *this); |
162 |
tcp_si.insert (*i); |
163 |
} |
164 |
else |
165 |
i = info->second; |
166 |
|
167 |
return i->send_packet (pkt, tos); |
168 |
} |
169 |
|
170 |
bool |
171 |
tcp_connection::write_packet () |
172 |
{ |
173 |
ssize_t len; |
174 |
|
175 |
if (w_ofs < 2) |
176 |
{ |
177 |
u16 plen = htons (w_pkt->len); |
178 |
|
179 |
iovec vec[2]; |
180 |
//TODO: char* is the right type? hardly... |
181 |
vec[0].iov_base = (char *)((u8 *)&plen) + w_ofs; |
182 |
vec[0].iov_len = 2 - w_ofs; |
183 |
vec[1].iov_base = (char *)&((*w_pkt)[0]); |
184 |
vec[1].iov_len = w_len - 2; |
185 |
|
186 |
len = writev (fd, vec, 2); |
187 |
} |
188 |
else |
189 |
len = write (fd, &((*w_pkt)[w_ofs - 2]), w_len); |
190 |
|
191 |
if (len > 0) |
192 |
{ |
193 |
w_ofs += len; |
194 |
w_len -= len; |
195 |
|
196 |
return w_len == 0; |
197 |
} |
198 |
else if (len < 0 && (errno == EAGAIN || errno == EINTR)) |
199 |
return false; |
200 |
else |
201 |
{ |
202 |
error (); |
203 |
return false; |
204 |
} |
205 |
} |
206 |
|
207 |
void |
208 |
tcp_connection::tcpv4_ev (ev::io &w, int revents) |
209 |
{ |
210 |
last_activity = ev_now (); |
211 |
|
212 |
if (revents & EV_WRITE) |
213 |
{ |
214 |
if (state == CONNECTING) |
215 |
{ |
216 |
state = ESTABLISHED; |
217 |
set (EV_READ); |
218 |
#if ENABLE_HTTP_PROXY |
219 |
if (::conf.proxy_host && ::conf.proxy_port) |
220 |
{ |
221 |
state = CONNECTING_PROXY; |
222 |
|
223 |
if (write (fd, proxy_req, proxy_req_len) == 0) |
224 |
{ |
225 |
error (); |
226 |
return; |
227 |
} |
228 |
|
229 |
free (proxy_req); proxy_req = 0; |
230 |
} |
231 |
#endif |
232 |
} |
233 |
else if (state == ESTABLISHED) |
234 |
{ |
235 |
if (w_pkt) |
236 |
{ |
237 |
if (write_packet ()) |
238 |
{ |
239 |
delete w_pkt; w_pkt = 0; |
240 |
|
241 |
set (EV_READ); |
242 |
} |
243 |
} |
244 |
else |
245 |
set (EV_READ); |
246 |
} |
247 |
else |
248 |
set (EV_READ); |
249 |
} |
250 |
|
251 |
if (revents & EV_READ) |
252 |
{ |
253 |
if (state == ESTABLISHED) |
254 |
for (;;) |
255 |
{ |
256 |
if (!r_pkt) |
257 |
{ |
258 |
r_pkt = new vpn_packet; |
259 |
r_ofs = 0; |
260 |
r_len = 2; // header |
261 |
} |
262 |
|
263 |
ssize_t len = read (fd, &((*r_pkt)[r_ofs < 2 ? r_ofs : r_ofs - 2]), r_len); |
264 |
|
265 |
if (len > 0) |
266 |
{ |
267 |
r_len -= len; |
268 |
r_ofs += len; |
269 |
|
270 |
if (r_len == 0) |
271 |
{ |
272 |
if (r_ofs == 2) |
273 |
{ |
274 |
r_len = ntohs (*(u16 *)&((*r_pkt)[0])); |
275 |
r_pkt->len = r_len; |
276 |
|
277 |
if (r_len > 0 && r_len < MAXSIZE) |
278 |
continue; |
279 |
} |
280 |
else |
281 |
{ |
282 |
v.recv_vpn_packet (r_pkt, si); |
283 |
delete r_pkt; |
284 |
r_pkt = 0; |
285 |
|
286 |
continue; |
287 |
} |
288 |
} |
289 |
else |
290 |
break; |
291 |
} |
292 |
else if (len < 0 && (errno == EINTR || errno == EAGAIN)) |
293 |
break; |
294 |
|
295 |
// len == 0 <-> EOF |
296 |
error (); |
297 |
break; |
298 |
} |
299 |
#if ENABLE_HTTP_PROXY |
300 |
else if (state == CONNECTING_PROXY) |
301 |
{ |
302 |
fcntl (fd, F_SETFL, 0); |
303 |
char r[1024]; |
304 |
int i; |
305 |
bool emptyline = false; |
306 |
|
307 |
// we do a blocking read of the response, to hell with it |
308 |
for (i = 0; i < 1023; i++) |
309 |
{ |
310 |
int l = read (fd, &r[i], 1); |
311 |
|
312 |
if (l <= 0) |
313 |
{ |
314 |
error (); |
315 |
return; |
316 |
} |
317 |
|
318 |
if (r[i] == '\012') |
319 |
{ |
320 |
if (emptyline) |
321 |
break; |
322 |
else |
323 |
emptyline = true; |
324 |
} |
325 |
else if (r[i] != '\015') |
326 |
emptyline = false; |
327 |
} |
328 |
|
329 |
fcntl (fd, F_SETFL, O_NONBLOCK); |
330 |
|
331 |
if (i < 12) |
332 |
{ |
333 |
slog (L_ERR, _("(%s): unable to do proxy-forwarding, short response"), |
334 |
(const char *)si); |
335 |
error (); |
336 |
} |
337 |
else if (r[0] != 'H' || r[1] != 'T' || r[2] != 'T' || r[3] != 'P' || r[4] != '/' |
338 |
|| r[5] != '1' // http-major |
339 |
|| r[9] != '2') // response |
340 |
{ |
341 |
slog (L_ERR, _("(%s): malformed or unexpected proxy response (%.12s)"), |
342 |
(const char *)si, r); |
343 |
error (); |
344 |
} |
345 |
else |
346 |
state = ESTABLISHED; |
347 |
} |
348 |
#endif |
349 |
} |
350 |
} |
351 |
|
352 |
bool |
353 |
tcp_connection::send_packet (vpn_packet *pkt, int tos) |
354 |
{ |
355 |
last_activity = ev_now (); |
356 |
|
357 |
if (state == IDLE) |
358 |
{ |
359 |
// woaw, the first lost packet ;) |
360 |
fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP); |
361 |
|
362 |
if (fd >= 0) |
363 |
{ |
364 |
const sockinfo *csi = &si; |
365 |
|
366 |
#if ENABLE_HTTP_PROXY |
367 |
sockinfo psi; |
368 |
|
369 |
if (::conf.proxy_host && ::conf.proxy_port) |
370 |
{ |
371 |
psi.set (::conf.proxy_host, ::conf.proxy_port, PROT_TCPv4); |
372 |
|
373 |
if (psi.valid ()) |
374 |
{ |
375 |
csi = ψ |
376 |
|
377 |
proxy_req_len = asprintf (&proxy_req, |
378 |
"CONNECT %s:%d HTTP/1.0\015\012" |
379 |
"%s%s%s" // optional proxy-auth |
380 |
"\015\012", |
381 |
si.ntoa (), |
382 |
ntohs (si.port), |
383 |
::conf.proxy_auth ? "Proxy-Authorization: Basic " : "", |
384 |
::conf.proxy_auth ? ::conf.proxy_auth : "", |
385 |
::conf.proxy_auth ? "\015\012" : ""); |
386 |
|
387 |
} |
388 |
else |
389 |
slog (L_ERR, _("unable to resolve http proxy hostname '%s', trying direct"), |
390 |
::conf.proxy_host); |
391 |
} |
392 |
#endif |
393 |
|
394 |
fcntl (fd, F_SETFL, O_NONBLOCK); |
395 |
|
396 |
if (connect (fd, csi->sav4 (), csi->salenv4 ()) >= 0 |
397 |
|| errno == EINPROGRESS) |
398 |
{ |
399 |
fcntl (fd, F_SETFL, O_NONBLOCK); |
400 |
fcntl (fd, F_SETFD, FD_CLOEXEC); |
401 |
|
402 |
state = CONNECTING; |
403 |
start (fd, EV_WRITE); |
404 |
} |
405 |
else |
406 |
close (fd); |
407 |
} |
408 |
} |
409 |
else if (state == ESTABLISHED) |
410 |
{ |
411 |
// drop packet if the tcp write buffer is full. this *is* the |
412 |
// right thing to do, not using tcp *is* the right thing to do. |
413 |
if (!w_pkt) |
414 |
{ |
415 |
// how this maps to the underlying tcp packets we don't know |
416 |
// and we don't care. at least we tried ;) |
417 |
#if defined(SOL_IP) && defined(IP_TOS) |
418 |
if (tos != this->tos) |
419 |
{ |
420 |
this->tos = tos; |
421 |
setsockopt (fd, SOL_IP, IP_TOS, &tos, sizeof tos); |
422 |
} |
423 |
#endif |
424 |
|
425 |
w_pkt = pkt; |
426 |
w_ofs = 0; |
427 |
w_len = pkt->len + 2; // length + size header |
428 |
|
429 |
if (write_packet ()) |
430 |
w_pkt = 0; |
431 |
else |
432 |
{ |
433 |
w_pkt = new vpn_packet; |
434 |
w_pkt->set (*pkt); |
435 |
|
436 |
set (EV_READ | EV_WRITE); |
437 |
} |
438 |
} |
439 |
} |
440 |
|
441 |
return state != ERROR; |
442 |
} |
443 |
|
444 |
void tcp_connection::error () |
445 |
{ |
446 |
stop (); |
447 |
|
448 |
if (fd >= 0) |
449 |
{ |
450 |
close (fd); |
451 |
tos = -1; |
452 |
fd = -1; |
453 |
} |
454 |
|
455 |
delete r_pkt; r_pkt = 0; |
456 |
delete w_pkt; w_pkt = 0; |
457 |
#if ENABLE_HTTP_PROXY |
458 |
free (proxy_req); proxy_req = 0; |
459 |
#endif |
460 |
|
461 |
state = active ? IDLE : ERROR; |
462 |
} |
463 |
|
464 |
tcp_connection::tcp_connection (int fd_, const sockinfo &si_, vpn &v_) |
465 |
: v(v_), si(si_) |
466 |
{ |
467 |
set<tcp_connection, &tcp_connection::tcpv4_ev> (this); |
468 |
|
469 |
last_activity = ev_now (); |
470 |
r_pkt = 0; |
471 |
w_pkt = 0; |
472 |
tos = -1; |
473 |
fd = fd_; |
474 |
#if ENABLE_HTTP_PROXY |
475 |
proxy_req = 0; |
476 |
#endif |
477 |
|
478 |
if (fd < 0) |
479 |
{ |
480 |
active = true; |
481 |
state = IDLE; |
482 |
} |
483 |
else |
484 |
{ |
485 |
active = false; |
486 |
state = ESTABLISHED; |
487 |
start (fd, EV_READ); |
488 |
} |
489 |
} |
490 |
|
491 |
tcp_connection::~tcp_connection () |
492 |
{ |
493 |
error (); |
494 |
} |
495 |
|
496 |
#endif |
497 |
|