1 | /* |
1 | /* |
2 | vpn_dns.C -- handle the dns tunnel part of the protocol. |
2 | vpn_dns.C -- handle the dns tunnel part of the protocol. |
3 | Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de> |
3 | Copyright (C) 2003-2011 Marc Lehmann <gvpe@schmorp.de> |
4 | |
4 | |
5 | This file is part of GVPE. |
5 | This file is part of GVPE. |
6 | |
6 | |
7 | GVPE is free software; you can redistribute it and/or modify |
7 | GVPE is free software; you can redistribute it and/or modify it |
8 | it under the terms of the GNU General Public License as published by |
8 | under the terms of the GNU General Public License as published by the |
9 | the Free Software Foundation; either version 2 of the License, or |
9 | Free Software Foundation; either version 3 of the License, or (at your |
10 | (at your option) any later version. |
10 | option) any later version. |
11 | |
11 | |
12 | This program is distributed in the hope that it will be useful, |
12 | This program is distributed in the hope that it will be useful, but |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
15 | GNU General Public License for more details. |
15 | Public License for more details. |
16 | |
16 | |
17 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License along |
18 | along with gvpe; if not, write to the Free Software |
18 | with this program; if not, see <http://www.gnu.org/licenses/>. |
19 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | |
|
|
20 | Additional permission under GNU GPL version 3 section 7 |
|
|
21 | |
|
|
22 | If you modify this Program, or any covered work, by linking or |
|
|
23 | combining it with the OpenSSL project's OpenSSL library (or a modified |
|
|
24 | version of that library), containing parts covered by the terms of the |
|
|
25 | OpenSSL or SSLeay licenses, the licensors of this Program grant you |
|
|
26 | additional permission to convey the resulting work. Corresponding |
|
|
27 | Source for a non-source form of such a combination shall include the |
|
|
28 | source code for the parts of OpenSSL used as well as that of the |
|
|
29 | covered work. |
20 | */ |
30 | */ |
|
|
31 | |
|
|
32 | // TODO: EDNS0 option to increase dns mtu? |
|
|
33 | // TODO: re-write dns packet parsing/creation using a safe mem-buffer |
|
|
34 | // to ensure no buffer overflows or similar problems. |
21 | |
35 | |
22 | #include "config.h" |
36 | #include "config.h" |
23 | |
37 | |
24 | #if ENABLE_DNS |
38 | #if ENABLE_DNS |
25 | |
39 | |
… | |
… | |
38 | #include <unistd.h> |
52 | #include <unistd.h> |
39 | #include <fcntl.h> |
53 | #include <fcntl.h> |
40 | |
54 | |
41 | #include <map> |
55 | #include <map> |
42 | |
56 | |
|
|
57 | #include <cstdio> /* bug in libgmp: gmp.h relies on cstdio being included */ |
43 | #include <gmp.h> |
58 | #include <gmp.h> |
44 | |
59 | |
45 | #include "netcompat.h" |
60 | #include "netcompat.h" |
46 | |
61 | |
47 | #include "vpn.h" |
62 | #include "vpn.h" |
48 | |
63 | |
49 | #define MIN_POLL_INTERVAL .02 // how often to poll minimally when the server has data |
64 | #define MIN_POLL_INTERVAL 0.025 // poll at most this often when no data received |
50 | #define MAX_POLL_INTERVAL 6. // how often to poll minimally when the server has no data |
65 | #define MAX_POLL_INTERVAL 1. // how often to poll minimally when the server has no data |
51 | #define ACTIVITY_INTERVAL 5. |
|
|
52 | |
66 | |
53 | #define INITIAL_TIMEOUT 0.1 // retry timeouts |
67 | #define INITIAL_TIMEOUT 0.1 // retry timeouts |
54 | #define INITIAL_SYN_TIMEOUT 10. // retry timeout for initial syn |
68 | #define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn |
55 | |
69 | |
56 | #define MIN_SEND_INTERVAL 0.01 // wait at least this time between sending requests |
|
|
57 | #define MAX_SEND_INTERVAL 0.5 // optimistic? |
70 | #define MAX_SEND_INTERVAL 5. // optimistic? |
58 | |
71 | |
59 | #define MAX_OUTSTANDING 10 // max. outstanding requests |
|
|
60 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
72 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
61 | #define MAX_BACKLOG (100*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE |
73 | #define MAX_BACKLOG (64*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE |
62 | |
74 | |
63 | #define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well |
75 | #define MAX_DOMAIN_SIZE 235 // 255 is legal limit, but bind doesn't compress well |
64 | // 240 leaves about 4 bytes of server reply data |
76 | // 240 leaves about 4 bytes of server reply data |
65 | // every two request bytes less give room for one reply byte |
77 | // every request byte less give room for two reply bytes |
66 | |
78 | |
67 | #define SEQNO_MASK 0x3fff |
79 | #define SEQNO_MASK 0x3fff |
68 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
80 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
69 | |
81 | |
70 | #define MAX_LBL_SIZE 63 |
82 | #define MAX_LBL_SIZE 63 |
71 | #define MAX_PKT_SIZE 512 |
83 | #define MAX_PKT_SIZE 512 |
72 | |
84 | |
73 | #define RR_TYPE_A 1 |
85 | #define RR_TYPE_A 1 |
74 | #define RR_TYPE_NULL 10 |
86 | #define RR_TYPE_NULL 10 |
75 | #define RR_TYPE_TXT 16 |
87 | #define RR_TYPE_TXT 16 |
|
|
88 | #define RR_TYPE_AAAA 28 |
76 | #define RR_TYPE_ANY 255 |
89 | #define RR_TYPE_ANY 255 |
77 | |
90 | |
78 | #define RR_CLASS_IN 1 |
91 | #define RR_CLASS_IN 1 |
79 | |
92 | |
80 | #define CMD_IP_1 207 |
93 | #define CMD_IP_1 207 |
81 | #define CMD_IP_2 46 |
94 | #define CMD_IP_2 46 |
82 | #define CMD_IP_3 236 |
95 | #define CMD_IP_3 236 |
83 | #define CMD_IP_RST 29 |
96 | |
84 | #define CMD_IP_SYN 113 |
97 | #define CMD_IP_RST 29 // some error, reset and retry |
85 | #define CMD_IP_REJ 32 |
98 | #define CMD_IP_REJ 32 // do not want you |
|
|
99 | #define CMD_IP_SYN 113 // connection established |
|
|
100 | #define CMD_IP_CSE 213 // connection established, but likely case mismatch |
|
|
101 | |
|
|
102 | static bool |
|
|
103 | is_uc (char c) |
|
|
104 | { |
|
|
105 | return 'A' <= c && c <= 'Z'; |
|
|
106 | } |
|
|
107 | |
|
|
108 | static bool |
|
|
109 | is_lc (char c) |
|
|
110 | { |
|
|
111 | return 'a' <= c && c <= 'z'; |
|
|
112 | } |
86 | |
113 | |
87 | // works for cmaps up to 255 (not 256!) |
114 | // works for cmaps up to 255 (not 256!) |
88 | struct charmap |
115 | struct charmap |
89 | { |
116 | { |
90 | enum { INVALID = (u8)255 }; |
117 | enum { INVALID = (u8)255 }; |
… | |
… | |
104 | memset (enc, (char) 0, 256); |
131 | memset (enc, (char) 0, 256); |
105 | memset (dec, (char)INVALID, 256); |
132 | memset (dec, (char)INVALID, 256); |
106 | |
133 | |
107 | for (size = 0; cmap [size]; size++) |
134 | for (size = 0; cmap [size]; size++) |
108 | { |
135 | { |
|
|
136 | char c = cmap [size]; |
|
|
137 | |
109 | enc [size] = cmap [size]; |
138 | enc [size] = c; |
110 | dec [(u8)enc [size]] = size; |
139 | dec [(u8)c] = size; |
|
|
140 | |
|
|
141 | // allow lowercase/uppercase aliases if possible |
|
|
142 | if (is_uc (c) && dec [c + ('a' - 'A')] == INVALID) dec [c + ('a' - 'A')] = size; |
|
|
143 | if (is_lc (c) && dec [c - ('a' - 'A')] == INVALID) dec [c - ('a' - 'A')] = size; |
111 | } |
144 | } |
112 | |
145 | |
113 | assert (size < 256); |
146 | assert (size < 256); |
114 | } |
147 | } |
115 | |
148 | |
… | |
… | |
122 | { |
155 | { |
123 | charmap cmap; |
156 | charmap cmap; |
124 | unsigned int enc_len [MAX_DEC_LEN]; |
157 | unsigned int enc_len [MAX_DEC_LEN]; |
125 | unsigned int dec_len [MAX_ENC_LEN]; |
158 | unsigned int dec_len [MAX_ENC_LEN]; |
126 | |
159 | |
127 | unsigned int encode_len (unsigned int len); |
160 | unsigned int encode_len (unsigned int len) const; |
128 | unsigned int decode_len (unsigned int len); |
161 | unsigned int decode_len (unsigned int len) const; |
129 | |
162 | |
130 | unsigned int encode (char *dst, u8 *src, unsigned int len); |
163 | unsigned int encode (char *dst, u8 *src, unsigned int len) const; |
131 | unsigned int decode (u8 *dst, char *src, unsigned int len); |
164 | unsigned int decode (u8 *dst, char *src, unsigned int len) const; |
132 | |
165 | |
133 | basecoder (const char *cmap); |
166 | basecoder (const char *cmap); |
134 | }; |
167 | }; |
135 | |
168 | |
136 | basecoder::basecoder (const char *cmap) |
169 | basecoder::basecoder (const char *cmap) |
… | |
… | |
155 | enc_len [len] = n; |
188 | enc_len [len] = n; |
156 | dec_len [n] = len; |
189 | dec_len [n] = len; |
157 | } |
190 | } |
158 | } |
191 | } |
159 | |
192 | |
|
|
193 | unsigned int |
160 | unsigned int basecoder::encode_len (unsigned int len) |
194 | basecoder::encode_len (unsigned int len) const |
161 | { |
195 | { |
162 | return enc_len [len]; |
196 | return enc_len [len]; |
163 | } |
197 | } |
164 | |
198 | |
|
|
199 | unsigned int |
165 | unsigned int basecoder::decode_len (unsigned int len) |
200 | basecoder::decode_len (unsigned int len) const |
166 | { |
201 | { |
167 | while (len && !dec_len [len]) |
202 | while (len && !dec_len [len]) |
168 | --len; |
203 | --len; |
169 | |
204 | |
170 | return dec_len [len]; |
205 | return dec_len [len]; |
171 | } |
206 | } |
172 | |
207 | |
|
|
208 | unsigned int |
173 | unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len) |
209 | basecoder::encode (char *dst, u8 *src, unsigned int len) const |
174 | { |
210 | { |
175 | if (!len || len > MAX_DEC_LEN) |
211 | if (!len || len > MAX_DEC_LEN) |
176 | return 0; |
212 | return 0; |
177 | |
213 | |
178 | int elen = encode_len (len); |
214 | int elen = encode_len (len); |
… | |
… | |
197 | *dst++ = cmap.encode [dst_ [i]]; |
233 | *dst++ = cmap.encode [dst_ [i]]; |
198 | |
234 | |
199 | return elen; |
235 | return elen; |
200 | } |
236 | } |
201 | |
237 | |
|
|
238 | unsigned int |
202 | unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len) |
239 | basecoder::decode (u8 *dst, char *src, unsigned int len) const |
203 | { |
240 | { |
204 | if (!len || len > MAX_ENC_LEN) |
241 | if (!len || len > MAX_ENC_LEN) |
205 | return 0; |
242 | return 0; |
206 | |
243 | |
207 | u8 src_ [MAX_ENC_LEN]; |
244 | u8 src_ [MAX_ENC_LEN]; |
… | |
… | |
256 | } |
293 | } |
257 | abort (); |
294 | abort (); |
258 | } |
295 | } |
259 | #endif |
296 | #endif |
260 | |
297 | |
261 | //static basecoder cdc64 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI-"); |
|
|
262 | //static basecoder cdc63 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); |
|
|
263 | static basecoder cdc62 ("dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); |
298 | static basecoder cdc62 ("dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); // a-zA-Z0-9 |
264 | //static basecoder cdc36 ("dphzr06qmjkb34tsvl81xaef92wgyo57ucni"); // unused as of yet |
299 | static basecoder cdc36 ("dPhZr06QmJkB34tSvL81xAeF92wGyO57uCnI"); // a-z0-9 for case-changers |
265 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
300 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyOuCnI"); // a-z |
266 | |
301 | |
267 | ///////////////////////////////////////////////////////////////////////////// |
302 | ///////////////////////////////////////////////////////////////////////////// |
268 | |
303 | |
269 | #define HDRSIZE 6 |
304 | #define HDRSIZE 5 |
270 | |
305 | |
|
|
306 | inline void |
271 | inline void encode_header (char *data, int clientid, int seqno, int retry = 0) |
307 | encode_header (char *data, int clientid, int seqno, int retry = 0) |
272 | { |
308 | { |
|
|
309 | assert (clientid < 256); |
|
|
310 | |
273 | seqno &= SEQNO_MASK; |
311 | seqno &= SEQNO_MASK; |
274 | |
312 | |
275 | u8 hdr[3] = { |
313 | u8 hdr[3] = { |
|
|
314 | seqno, |
|
|
315 | (seqno >> 8) | (retry << 6), |
276 | clientid, |
316 | clientid, |
277 | (seqno >> 8) | (retry << 6), |
|
|
278 | seqno, |
|
|
279 | }; |
317 | }; |
280 | |
318 | |
281 | assert (clientid < 256); |
|
|
282 | |
|
|
283 | cdc26.encode (data, hdr, 3); |
319 | cdc36.encode (data, hdr, 3); |
284 | } |
320 | } |
285 | |
321 | |
|
|
322 | inline void |
286 | inline void decode_header (char *data, int &clientid, int &seqno) |
323 | decode_header (char *data, int &clientid, int &seqno) |
287 | { |
324 | { |
288 | u8 hdr[3]; |
325 | u8 hdr[3]; |
289 | |
326 | |
290 | cdc26.decode (hdr, data, HDRSIZE); |
327 | cdc36.decode (hdr, data, HDRSIZE); |
291 | |
328 | |
292 | clientid = hdr[0]; |
329 | clientid = hdr[2]; |
293 | seqno = ((hdr[1] << 8) | hdr[2]) & SEQNO_MASK; |
330 | seqno = ((hdr[1] << 8) | hdr[0]) & SEQNO_MASK; |
294 | } |
331 | } |
295 | |
332 | |
296 | ///////////////////////////////////////////////////////////////////////////// |
333 | ///////////////////////////////////////////////////////////////////////////// |
297 | |
334 | |
298 | struct byte_stream |
335 | struct byte_stream |
… | |
… | |
324 | byte_stream::~byte_stream () |
361 | byte_stream::~byte_stream () |
325 | { |
362 | { |
326 | delete data; |
363 | delete data; |
327 | } |
364 | } |
328 | |
365 | |
|
|
366 | void |
329 | void byte_stream::remove (int count) |
367 | byte_stream::remove (int count) |
330 | { |
368 | { |
331 | if (count > fill) |
369 | if (count > fill) |
332 | assert (count <= fill); |
370 | assert (count <= fill); |
333 | |
371 | |
334 | memmove (data, data + count, fill -= count); |
372 | memmove (data, data + count, fill -= count); |
335 | } |
373 | } |
336 | |
374 | |
|
|
375 | bool |
337 | bool byte_stream::put (u8 *data, unsigned int datalen) |
376 | byte_stream::put (u8 *data, unsigned int datalen) |
338 | { |
377 | { |
339 | if (maxsize - fill < datalen) |
378 | if (maxsize - fill < datalen) |
340 | return false; |
379 | return false; |
341 | |
380 | |
342 | memcpy (this->data + fill, data, datalen); fill += datalen; |
381 | memcpy (this->data + fill, data, datalen); fill += datalen; |
343 | |
382 | |
344 | return true; |
383 | return true; |
345 | } |
384 | } |
346 | |
385 | |
|
|
386 | bool |
347 | bool byte_stream::put (vpn_packet *pkt) |
387 | byte_stream::put (vpn_packet *pkt) |
348 | { |
388 | { |
349 | if (maxsize - fill < pkt->len + 2) |
389 | if (maxsize - fill < pkt->len + 2) |
350 | return false; |
390 | return false; |
351 | |
391 | |
352 | data [fill++] = pkt->len >> 8; |
392 | data [fill++] = pkt->len >> 8; |
… | |
… | |
361 | { |
401 | { |
362 | unsigned int len; |
402 | unsigned int len; |
363 | |
403 | |
364 | for (;;) |
404 | for (;;) |
365 | { |
405 | { |
|
|
406 | if (fill < 2) |
|
|
407 | return 0; |
|
|
408 | |
366 | len = (data [0] << 8) | data [1]; |
409 | len = (data [0] << 8) | data [1]; |
367 | |
410 | |
368 | if (len <= MAXSIZE || fill < 2) |
411 | if (len <= MAXSIZE) |
369 | break; |
412 | break; |
370 | |
413 | |
371 | // TODO: handle this better than skipping, e.g. by reset |
414 | // TODO: handle this better than skipping, e.g. by reset |
372 | slog (L_DEBUG, _("DNS: corrupted packet stream skipping a byte...")); |
415 | slog (L_DEBUG, _("DNS: corrupted packet (%02x %02x) stream skipping a byte..."), data [0], data [1]); |
373 | remove (1); |
416 | remove (1); |
374 | } |
417 | } |
375 | |
418 | |
376 | if (fill < len + 2) |
419 | if (fill < len + 2) |
377 | return 0; |
420 | return 0; |
… | |
… | |
408 | |
451 | |
409 | struct dns_cfg |
452 | struct dns_cfg |
410 | { |
453 | { |
411 | static int next_uid; |
454 | static int next_uid; |
412 | |
455 | |
|
|
456 | u8 chksum; |
|
|
457 | u8 rrtype; |
|
|
458 | u16 uid; // to make request unique |
|
|
459 | |
|
|
460 | u8 version; |
|
|
461 | u8 flags; |
|
|
462 | u16 max_size; |
|
|
463 | |
413 | u8 id1, id2, id3, id4; |
464 | u8 id1, id2, id3, id4; |
414 | u8 version; |
465 | |
415 | u8 rrtype; |
466 | u16 client; |
416 | u8 flags; |
|
|
417 | u8 def_ttl; |
467 | u8 def_ttl; |
418 | u8 rcv_cdc; |
468 | u8 r0; |
419 | u8 snd_cdc; |
|
|
420 | u16 max_size; |
|
|
421 | u16 client; |
|
|
422 | u16 uid; // to make request unique |
|
|
423 | |
469 | |
424 | u8 reserved[8]; |
470 | u8 syn_cdc; // cdc en/decoder for syn (A?) requests |
|
|
471 | u8 hdr_cdc; // cdc en/decoder for regular request headers |
|
|
472 | u8 req_cdc; // cdc en/decoder for regular (ANY?) request data |
|
|
473 | u8 rep_cdc; // cdc en/decoder for regular (TXT) replies, 0 == 8 bit encoding |
|
|
474 | |
|
|
475 | u8 r1, r2, r3, r4; |
425 | |
476 | |
426 | void reset (int clientid); |
477 | void reset (int clientid); |
427 | bool valid (); |
478 | bool valid (); |
|
|
479 | u8 get_chksum (); |
428 | }; |
480 | }; |
429 | |
481 | |
430 | int dns_cfg::next_uid; |
482 | int dns_cfg::next_uid; |
431 | |
483 | |
|
|
484 | void |
432 | void dns_cfg::reset (int clientid) |
485 | dns_cfg::reset (int clientid) |
433 | { |
486 | { |
|
|
487 | // this ID must result in some mixed-case characters in cdc26-encoding |
434 | id1 = 'G'; |
488 | id1 = 'G'; |
435 | id2 = 'V'; |
489 | id2 = 'V'; |
436 | id3 = 'P'; |
490 | id3 = 'P'; |
437 | id4 = 'E'; |
491 | id4 = 'E'; |
438 | |
492 | |
439 | version = 1; |
493 | version = 2; |
440 | |
494 | |
441 | rrtype = RR_TYPE_TXT; |
495 | rrtype = RR_TYPE_TXT; |
442 | flags = 0; |
496 | flags = 0; |
443 | def_ttl = 1; |
497 | def_ttl = 0; |
444 | rcv_cdc = 0; |
|
|
445 | snd_cdc = 62; |
498 | syn_cdc = 26; |
|
|
499 | hdr_cdc = 36; |
|
|
500 | req_cdc = conf.dns_case_preserving ? 62 : 36; |
|
|
501 | rep_cdc = 0; |
446 | max_size = ntohs (MAX_PKT_SIZE); |
502 | max_size = htons (MAX_PKT_SIZE); |
447 | client = ntohs (clientid); |
503 | client = htons (clientid); |
448 | uid = next_uid++; |
504 | uid = ++next_uid; |
449 | |
505 | |
450 | memset (reserved, 0, 8); |
506 | r0 = r1 = r2 = r3 = r4 = 0; |
451 | } |
|
|
452 | |
507 | |
|
|
508 | chksum = get_chksum (); |
|
|
509 | } |
|
|
510 | |
|
|
511 | // simple but not trivial chksum |
|
|
512 | u8 |
|
|
513 | dns_cfg::get_chksum () |
|
|
514 | { |
|
|
515 | unsigned int sum = 0xff00; // only 16 bits required |
|
|
516 | |
|
|
517 | u8 old_chksum = chksum; |
|
|
518 | chksum = 0; |
|
|
519 | |
|
|
520 | for (unsigned int i = 0; i < sizeof (*this); ++i) |
|
|
521 | sum += ((u8 *)this)[i] * (i + 1); |
|
|
522 | |
|
|
523 | chksum = old_chksum; |
|
|
524 | |
|
|
525 | return sum + (sum >> 8); |
|
|
526 | } |
|
|
527 | |
|
|
528 | bool |
453 | bool dns_cfg::valid () |
529 | dns_cfg::valid () |
454 | { |
530 | { |
|
|
531 | // although the protocol itself allows for some configurability, |
|
|
532 | // only the following encoding/decoding settings are implemented. |
455 | return id1 == 'G' |
533 | return id1 == 'G' |
456 | && id2 == 'V' |
534 | && id2 == 'V' |
457 | && id3 == 'P' |
535 | && id3 == 'P' |
458 | && id4 == 'E' |
536 | && id4 == 'E' |
459 | && version == 1 |
537 | && version == 2 |
460 | && flags == 0 |
|
|
461 | && rcv_cdc == 0 |
|
|
462 | && snd_cdc == 62 |
538 | && syn_cdc == 26 |
463 | && max_size == ntohs (MAX_PKT_SIZE); |
539 | && hdr_cdc == 36 |
|
|
540 | && (req_cdc == 36 || req_cdc == 62) |
|
|
541 | && rep_cdc == 0 |
|
|
542 | && chksum == get_chksum (); |
464 | } |
543 | } |
465 | |
544 | |
466 | struct dns_packet : net_packet |
545 | struct dns_packet : net_packet |
467 | { |
546 | { |
468 | u16 id; |
547 | u16 id; |
469 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
548 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
470 | u16 qdcount, ancount, nscount, arcount; |
549 | u16 qdcount, ancount, nscount, arcount; |
471 | |
550 | |
472 | u8 data[MAXSIZE - 6 * 2]; |
551 | u8 data [MAXSIZE - 6 * 2]; |
473 | |
552 | |
474 | int decode_label (char *data, int size, int &offs); |
553 | int decode_label (char *data, int size, int &offs); |
475 | }; |
554 | }; |
476 | |
555 | |
|
|
556 | int |
477 | int dns_packet::decode_label (char *data, int size, int &offs) |
557 | dns_packet::decode_label (char *data, int size, int &offs) |
478 | { |
558 | { |
479 | char *orig = data; |
559 | char *orig = data; |
480 | |
560 | |
481 | memset (data, 0, size); |
561 | memset (data, 0, size); |
482 | |
562 | |
… | |
… | |
508 | return data - orig; |
588 | return data - orig; |
509 | } |
589 | } |
510 | |
590 | |
511 | ///////////////////////////////////////////////////////////////////////////// |
591 | ///////////////////////////////////////////////////////////////////////////// |
512 | |
592 | |
|
|
593 | static |
|
|
594 | u16 next_id () |
|
|
595 | { |
513 | static u16 dns_id = 0; // TODO: should be per-vpn |
596 | static u16 dns_id = 0; // TODO: should be per-vpn |
514 | |
597 | |
515 | static u16 next_id () |
598 | #if 1 |
516 | { |
|
|
517 | if (!dns_id) |
599 | if (!dns_id) |
518 | dns_id = time (0); |
600 | dns_id = time (0); |
519 | |
601 | |
520 | // the simplest lsfr with periodicity 65535 i could find |
602 | // the simplest lsfr with periodicity 65535 i could find |
521 | dns_id = (dns_id << 1) |
603 | dns_id = (dns_id << 1) |
… | |
… | |
523 | ^ (dns_id >> 2) |
605 | ^ (dns_id >> 2) |
524 | ^ (dns_id >> 4) |
606 | ^ (dns_id >> 4) |
525 | ^ (dns_id >> 15)) & 1); |
607 | ^ (dns_id >> 15)) & 1); |
526 | |
608 | |
527 | return dns_id; |
609 | return dns_id; |
|
|
610 | #else |
|
|
611 | dns_id++;//D |
|
|
612 | |
|
|
613 | return htons (dns_id); |
|
|
614 | #endif |
528 | } |
615 | } |
529 | |
616 | |
530 | struct dns_rcv; |
617 | struct dns_rcv; |
531 | struct dns_snd; |
618 | struct dns_snd; |
532 | |
619 | |
… | |
… | |
536 | struct vpn *vpn; |
623 | struct vpn *vpn; |
537 | |
624 | |
538 | dns_cfg cfg; |
625 | dns_cfg cfg; |
539 | |
626 | |
540 | bool established; |
627 | bool established; |
|
|
628 | const basecoder *cdc; |
541 | |
629 | |
542 | tstamp last_received; |
630 | tstamp last_received; |
543 | tstamp last_sent; |
631 | tstamp last_sent; |
544 | double last_latency; |
632 | double min_latency; |
545 | double poll_interval, send_interval; |
633 | double poll_interval, send_interval; |
546 | |
634 | |
547 | vector<dns_rcv *> rcvpq; |
635 | vector<dns_rcv *> rcvpq; |
548 | |
636 | |
549 | byte_stream rcvdq; int rcvseq; |
637 | byte_stream rcvdq; int rcvseq; int repseq; |
550 | byte_stream snddq; int sndseq; |
638 | byte_stream snddq; int sndseq; |
551 | |
639 | |
552 | void time_cb (time_watcher &w); time_watcher tw; |
640 | inline void time_cb (ev::timer &w, int revents); ev::timer tw; |
553 | void receive_rep (dns_rcv *r); |
641 | void receive_rep (dns_rcv *r); |
|
|
642 | |
|
|
643 | void reset (); // quite like tcp RST |
|
|
644 | void set_cfg (); // to be called after any cfg changes |
554 | |
645 | |
555 | dns_connection (connection *c); |
646 | dns_connection (connection *c); |
556 | ~dns_connection (); |
647 | ~dns_connection (); |
557 | }; |
648 | }; |
558 | |
649 | |
… | |
… | |
576 | : dns (dns) |
667 | : dns (dns) |
577 | { |
668 | { |
578 | timeout = 0; |
669 | timeout = 0; |
579 | retry = 0; |
670 | retry = 0; |
580 | seqno = 0; |
671 | seqno = 0; |
581 | sent = NOW; |
672 | sent = ev_now (); |
582 | stdhdr = false; |
673 | stdhdr = false; |
583 | |
674 | |
584 | pkt = new dns_packet; |
675 | pkt = new dns_packet; |
585 | |
676 | |
586 | pkt->id = next_id (); |
677 | pkt->id = next_id (); |
… | |
… | |
589 | dns_snd::~dns_snd () |
680 | dns_snd::~dns_snd () |
590 | { |
681 | { |
591 | delete pkt; |
682 | delete pkt; |
592 | } |
683 | } |
593 | |
684 | |
|
|
685 | static void |
594 | static void append_domain (dns_packet &pkt, int &offs, const char *domain) |
686 | append_domain (dns_packet &pkt, int &offs, const char *domain) |
595 | { |
687 | { |
596 | // add tunnel domain |
688 | // add tunnel domain |
597 | for (;;) |
689 | for (;;) |
598 | { |
690 | { |
599 | const char *end = strchr (domain, '.'); |
691 | const char *end = strchr (domain, '.'); |
… | |
… | |
612 | |
704 | |
613 | domain = end + 1; |
705 | domain = end + 1; |
614 | } |
706 | } |
615 | } |
707 | } |
616 | |
708 | |
|
|
709 | void |
617 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
710 | dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
618 | { |
711 | { |
619 | stdhdr = true; |
712 | stdhdr = true; |
620 | this->seqno = seqno; |
713 | this->seqno = seqno; |
621 | |
714 | |
622 | timeout = NOW + INITIAL_TIMEOUT; |
715 | timeout = ev_now () + INITIAL_TIMEOUT; |
623 | |
716 | |
624 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
717 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
625 | pkt->qdcount = htons (1); |
718 | pkt->qdcount = htons (1); |
626 | |
719 | |
627 | int offs = 6*2; |
720 | int offs = 6*2; |
… | |
… | |
630 | // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra |
723 | // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra |
631 | |
724 | |
632 | char enc[256], *encp = enc; |
725 | char enc[256], *encp = enc; |
633 | encode_header (enc, THISNODE->id, seqno); |
726 | encode_header (enc, THISNODE->id, seqno); |
634 | |
727 | |
635 | int datalen = cdc62.decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE - HDRSIZE); |
728 | int datalen = dns->cdc->decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE - HDRSIZE); |
636 | |
729 | |
637 | if (datalen > stream.size ()) |
730 | if (datalen > stream.size ()) |
638 | datalen = stream.size (); |
731 | datalen = stream.size (); |
639 | |
732 | |
640 | int enclen = cdc62.encode (enc + HDRSIZE, stream.begin (), datalen) + HDRSIZE; |
733 | int enclen = dns->cdc->encode (enc + HDRSIZE, stream.begin (), datalen) + HDRSIZE; |
641 | stream.remove (datalen); |
734 | stream.remove (datalen); |
642 | |
735 | |
643 | while (enclen) |
736 | while (enclen) |
644 | { |
737 | { |
645 | int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE; |
738 | int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE; |
… | |
… | |
660 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
753 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
661 | |
754 | |
662 | pkt->len = offs; |
755 | pkt->len = offs; |
663 | } |
756 | } |
664 | |
757 | |
|
|
758 | void |
665 | void dns_snd::gen_syn_req () |
759 | dns_snd::gen_syn_req () |
666 | { |
760 | { |
667 | timeout = NOW + INITIAL_SYN_TIMEOUT; |
761 | timeout = ev_now () + INITIAL_SYN_TIMEOUT; |
668 | |
762 | |
669 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
763 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
670 | pkt->qdcount = htons (1); |
764 | pkt->qdcount = htons (1); |
671 | |
765 | |
672 | int offs = 6 * 2; |
766 | int offs = 6 * 2; |
… | |
… | |
711 | ///////////////////////////////////////////////////////////////////////////// |
805 | ///////////////////////////////////////////////////////////////////////////// |
712 | |
806 | |
713 | dns_connection::dns_connection (connection *c) |
807 | dns_connection::dns_connection (connection *c) |
714 | : c (c) |
808 | : c (c) |
715 | , rcvdq (MAX_BACKLOG * 2) |
809 | , rcvdq (MAX_BACKLOG * 2) |
716 | , snddq (MAX_BACKLOG * 2) |
810 | , snddq (MAX_BACKLOG) |
717 | , tw (this, &dns_connection::time_cb) |
|
|
718 | { |
811 | { |
|
|
812 | tw.set<dns_connection, &dns_connection::time_cb> (this); |
|
|
813 | |
719 | vpn = c->vpn; |
814 | vpn = c->vpn; |
720 | |
815 | |
|
|
816 | reset (); |
|
|
817 | } |
|
|
818 | |
|
|
819 | dns_connection::~dns_connection () |
|
|
820 | { |
|
|
821 | reset (); |
|
|
822 | } |
|
|
823 | |
|
|
824 | void |
|
|
825 | dns_connection::reset () |
|
|
826 | { |
|
|
827 | while (!rcvpq.empty ()) |
|
|
828 | { |
|
|
829 | delete rcvpq.back (); |
|
|
830 | rcvpq.pop_back (); |
|
|
831 | } |
|
|
832 | |
|
|
833 | for (int i = vpn->dns_sndpq.size (); i--; ) |
|
|
834 | if (vpn->dns_sndpq [i]->dns == this) |
|
|
835 | { |
|
|
836 | vpn->dns_sndpq [i] = vpn->dns_sndpq.back (); |
|
|
837 | vpn->dns_sndpq.pop_back (); |
|
|
838 | } |
|
|
839 | |
721 | established = false; |
840 | established = false; |
722 | |
841 | |
723 | rcvseq = sndseq = 0; |
842 | rcvseq = repseq = sndseq = 0; |
724 | |
843 | |
725 | last_sent = last_received = 0; |
844 | last_sent = 0; |
726 | poll_interval = MIN_POLL_INTERVAL; |
845 | poll_interval = 0.5; // starting here |
727 | send_interval = 0.5; // starting rate |
846 | send_interval = 0.5; // starting rate |
728 | last_latency = INITIAL_TIMEOUT; |
847 | min_latency = INITIAL_TIMEOUT; |
729 | } |
848 | } |
730 | |
849 | |
731 | dns_connection::~dns_connection () |
850 | void |
|
|
851 | dns_connection::set_cfg () |
732 | { |
852 | { |
733 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
853 | cdc = cfg.req_cdc == 36 ? &cdc36 : &cdc62; |
734 | i != rcvpq.end (); |
|
|
735 | ++i) |
|
|
736 | delete *i; |
|
|
737 | } |
854 | } |
738 | |
855 | |
|
|
856 | void |
739 | void dns_connection::receive_rep (dns_rcv *r) |
857 | dns_connection::receive_rep (dns_rcv *r) |
740 | { |
858 | { |
741 | if (r->datalen) |
859 | if (r->datalen) |
742 | { |
860 | poll_interval = max (poll_interval * (1. / 1.2), MIN_POLL_INTERVAL); |
743 | last_received = NOW; |
|
|
744 | tw.trigger (); |
|
|
745 | |
|
|
746 | poll_interval = send_interval; |
|
|
747 | } |
|
|
748 | else |
861 | else |
749 | { |
862 | poll_interval = min (poll_interval * 1.1, MAX_POLL_INTERVAL); |
750 | poll_interval *= 1.5; |
|
|
751 | if (poll_interval > MAX_POLL_INTERVAL) |
|
|
752 | poll_interval = MAX_POLL_INTERVAL; |
|
|
753 | } |
|
|
754 | |
863 | |
755 | rcvpq.push_back (r); |
864 | rcvpq.push_back (r); |
756 | |
865 | |
757 | redo: |
866 | redo: |
758 | |
867 | |
759 | // find next packet |
868 | // find next packet |
760 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
869 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
761 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
870 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
762 | { |
871 | { |
|
|
872 | //printf ("seqno eq %x %x\n", rcvseq, (*i)->seqno);//D |
763 | // enter the packet into our input stream |
873 | // enter the packet into our input stream |
764 | r = *i; |
874 | r = *i; |
765 | |
875 | |
766 | // remove the oldest packet, look forward, as it's oldest first |
876 | // remove the oldest packet, look forward, as it's oldest first |
767 | for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
877 | for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
768 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
878 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
769 | { |
879 | { |
|
|
880 | //printf ("seqno RR %x %x\n", (*j)->seqno, rcvseq - MAX_WINDOW);//D |
770 | delete *j; |
881 | delete *j; |
771 | rcvpq.erase (j); |
882 | rcvpq.erase (j); |
772 | break; |
883 | break; |
773 | } |
884 | } |
774 | |
885 | |
775 | rcvseq = (rcvseq + 1) & SEQNO_MASK; |
886 | rcvseq = (rcvseq + 1) & SEQNO_MASK; |
776 | |
887 | |
777 | if (!rcvdq.put (r->data, r->datalen)) |
888 | if (!rcvdq.put (r->data, r->datalen)) |
778 | { |
889 | { |
|
|
890 | // MUST never overflow, can be caused by data corruption, TODO |
779 | slog (L_ERR, "DNS: !rcvdq.put (r->data, r->datalen)"); |
891 | slog (L_CRIT, "DNS: !rcvdq.put (r->data, r->datalen)"); |
780 | abort (); // MUST never overflow, can be caused by data corruption, TODO |
892 | reset (); |
|
|
893 | return; |
781 | } |
894 | } |
782 | |
895 | |
783 | while (vpn_packet *pkt = rcvdq.get ()) |
896 | while (vpn_packet *pkt = rcvdq.get ()) |
784 | { |
897 | { |
785 | sockinfo si; |
898 | sockinfo si; |
786 | si.host = 0x01010101; si.port = htons (c->conf->id); si.prot = PROT_DNSv4; |
899 | si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4; |
787 | |
900 | |
788 | vpn->recv_vpn_packet (pkt, si); |
901 | vpn->recv_vpn_packet (pkt, si); |
789 | |
|
|
790 | delete pkt; |
902 | delete pkt; |
791 | } |
903 | } |
792 | |
904 | |
793 | // check for further packets |
905 | // check for further packets |
794 | goto redo; |
906 | goto redo; |
… | |
… | |
805 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
917 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
806 | |
918 | |
807 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
919 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
808 | && pkt.qdcount == htons (1)) |
920 | && pkt.qdcount == htons (1)) |
809 | { |
921 | { |
810 | char qname[MAXSIZE]; |
922 | char qname [MAXSIZE]; |
811 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
923 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
812 | |
924 | |
813 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
925 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
814 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
926 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
815 | |
927 | |
… | |
… | |
822 | |
934 | |
823 | int dlen = strlen (THISNODE->domain); |
935 | int dlen = strlen (THISNODE->domain); |
824 | |
936 | |
825 | if (qclass == RR_CLASS_IN |
937 | if (qclass == RR_CLASS_IN |
826 | && qlen > dlen + 1 |
938 | && qlen > dlen + 1 |
827 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
939 | && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen)) |
828 | { |
940 | { |
829 | // now generate reply |
941 | // now generate reply |
830 | pkt.ancount = htons (1); // one answer RR |
942 | pkt.ancount = htons (1); // one answer RR |
831 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
943 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
832 | |
944 | |
… | |
… | |
837 | { |
949 | { |
838 | // correct class, domain: parse |
950 | // correct class, domain: parse |
839 | int client, seqno; |
951 | int client, seqno; |
840 | decode_header (qname, client, seqno); |
952 | decode_header (qname, client, seqno); |
841 | |
953 | |
842 | u8 data[MAXSIZE]; |
|
|
843 | int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE)); |
|
|
844 | |
|
|
845 | if (0 < client && client <= conns.size ()) |
954 | if (0 < client && client <= conns.size ()) |
846 | { |
955 | { |
847 | connection *c = conns [client - 1]; |
956 | connection *c = conns [client - 1]; |
848 | dns_connection *dns = c->dns; |
957 | dns_connection *dns = c->dns; |
849 | dns_rcv *rcv; |
958 | dns_rcv *rcv; |
850 | bool in_seq; |
|
|
851 | |
959 | |
852 | if (dns) |
960 | if (dns) |
853 | { |
961 | { |
|
|
962 | u8 data[MAXSIZE]; |
|
|
963 | int datalen = dns->cdc->decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE)); |
|
|
964 | |
854 | for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) |
965 | for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) |
855 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
966 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
856 | { |
967 | { |
857 | // already seen that request: simply reply with the cached reply |
968 | // already seen that request: simply reply with the cached reply |
858 | dns_rcv *r = *i; |
969 | dns_rcv *r = *i; |
… | |
… | |
866 | memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len); |
977 | memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len); |
867 | |
978 | |
868 | goto duplicate_request; |
979 | goto duplicate_request; |
869 | } |
980 | } |
870 | |
981 | |
871 | in_seq = dns->rcvseq == seqno; |
|
|
872 | |
|
|
873 | // new packet, queue |
982 | // new packet, queue |
874 | rcv = new dns_rcv (seqno, data, datalen); |
983 | rcv = new dns_rcv (seqno, data, datalen); |
875 | dns->receive_rep (rcv); |
984 | dns->receive_rep (rcv); |
876 | } |
985 | } |
877 | |
986 | |
… | |
… | |
884 | pkt [offs++] = 0; pkt [offs++] = 0; |
993 | pkt [offs++] = 0; pkt [offs++] = 0; |
885 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
994 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
886 | |
995 | |
887 | int rdlen_offs = offs += 2; |
996 | int rdlen_offs = offs += 2; |
888 | |
997 | |
889 | int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs; |
|
|
890 | // bind doesn't compress well, so reduce further by one label length |
|
|
891 | dlen -= qlen; |
|
|
892 | |
|
|
893 | if (dns) |
998 | if (dns) |
894 | { |
999 | { |
|
|
1000 | int dlen = ntohs (dns->cfg.max_size) - offs; |
|
|
1001 | |
|
|
1002 | // bind doesn't compress well, so reduce further by one label length |
|
|
1003 | dlen -= qlen; |
|
|
1004 | |
895 | // only put data into in-order sequence packets, if |
1005 | // only put data into in-order sequence packets, if |
896 | // we receive out-of-order packets we generate empty |
1006 | // we receive out-of-order packets we generate empty |
897 | // replies |
1007 | // replies |
898 | while (dlen > 1 && !dns->snddq.empty () && in_seq) |
1008 | //printf ("%d - %d & %x (=%d) < %d\n", seqno, dns->repseq, SEQNO_MASK, (seqno - dns->repseq) & SEQNO_MASK, MAX_WINDOW);//D |
|
|
1009 | if (((seqno - dns->repseq) & SEQNO_MASK) <= MAX_WINDOW) |
899 | { |
1010 | { |
|
|
1011 | dns->repseq = seqno; |
|
|
1012 | |
|
|
1013 | while (dlen > 1 && !dns->snddq.empty ()) |
|
|
1014 | { |
900 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
1015 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
901 | |
1016 | |
902 | if (txtlen > dns->snddq.size ()) |
1017 | if (txtlen > dns->snddq.size ()) |
903 | txtlen = dns->snddq.size (); |
1018 | txtlen = dns->snddq.size (); |
904 | |
1019 | |
905 | pkt[offs++] = txtlen; |
1020 | pkt[offs++] = txtlen; |
906 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
1021 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
907 | offs += txtlen; |
1022 | offs += txtlen; |
908 | dns->snddq.remove (txtlen); |
1023 | dns->snddq.remove (txtlen); |
909 | |
1024 | |
910 | dlen -= txtlen + 1; |
1025 | dlen -= txtlen + 1; |
|
|
1026 | } |
911 | } |
1027 | } |
912 | |
1028 | |
913 | // avoid empty TXT rdata |
1029 | // avoid completely empty TXT rdata |
914 | if (offs == rdlen_offs) |
1030 | if (offs == rdlen_offs) |
915 | pkt[offs++] = 0; |
1031 | pkt[offs++] = 0; |
916 | |
1032 | |
917 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
1033 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
918 | } |
1034 | } |
… | |
… | |
954 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
1070 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
955 | pkt [offs++] = 0; pkt [offs++] = 0; |
1071 | pkt [offs++] = 0; pkt [offs++] = 0; |
956 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
1072 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
957 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
1073 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
958 | |
1074 | |
959 | slog (L_INFO, _("DNS: client %d tries to connect"), client); |
|
|
960 | |
|
|
961 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
1075 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
962 | pkt [offs++] = CMD_IP_REJ; |
1076 | pkt [offs++] = CMD_IP_REJ; |
963 | |
1077 | |
964 | if (0 < client && client <= conns.size ()) |
1078 | if (0 < client && client <= conns.size ()) |
965 | { |
1079 | { |
966 | connection *c = conns [client - 1]; |
1080 | connection *c = conns [client - 1]; |
967 | |
1081 | |
968 | if (cfg.valid ()) |
1082 | if (cfg.valid ()) |
969 | { |
1083 | { |
970 | pkt [offs - 1] = CMD_IP_SYN; |
1084 | slog (L_INFO, _("DNS: client %d connects (version %d, req_cdc %d)"), client, cfg.version, cfg.req_cdc); |
|
|
1085 | |
|
|
1086 | // check for any encoding mismatches - hints at a case problem |
|
|
1087 | char qname2 [MAX_ENC_LEN]; |
|
|
1088 | cdc26.encode (qname2, (u8 *)&cfg, sizeof (dns_cfg)); |
971 | |
1089 | |
972 | delete c->dns; |
1090 | delete c->dns; |
|
|
1091 | |
|
|
1092 | pkt [offs - 1] = memcmp (qname, qname2, cdc26.encode_len (sizeof (dns_cfg))) |
|
|
1093 | ? CMD_IP_CSE : CMD_IP_SYN; |
|
|
1094 | |
973 | c->dns = new dns_connection (c); |
1095 | c->dns = new dns_connection (c); |
974 | c->dns->cfg = cfg; |
1096 | c->dns->cfg = cfg; |
|
|
1097 | c->dns->set_cfg (); |
975 | } |
1098 | } |
976 | } |
1099 | } |
977 | } |
1100 | } |
978 | } |
1101 | } |
979 | |
1102 | |
… | |
… | |
998 | { |
1121 | { |
999 | dns_connection *dns = (*i)->dns; |
1122 | dns_connection *dns = (*i)->dns; |
1000 | connection *c = dns->c; |
1123 | connection *c = dns->c; |
1001 | int seqno = (*i)->seqno; |
1124 | int seqno = (*i)->seqno; |
1002 | u8 data[MAXSIZE], *datap = data; |
1125 | u8 data[MAXSIZE], *datap = data; |
|
|
1126 | //printf ("rcv pkt %x\n", seqno);//D |
1003 | |
1127 | |
1004 | if ((*i)->retry) |
1128 | if ((*i)->retry) |
1005 | { |
1129 | { |
1006 | dns->send_interval *= 1.01; |
1130 | dns->send_interval *= 1.01; |
1007 | if (dns->send_interval > MAX_SEND_INTERVAL) |
1131 | if (dns->send_interval > MAX_SEND_INTERVAL) |
1008 | dns->send_interval = MAX_SEND_INTERVAL; |
1132 | dns->send_interval = MAX_SEND_INTERVAL; |
1009 | } |
1133 | } |
1010 | else |
1134 | else |
1011 | { |
1135 | { |
1012 | #if 1 |
1136 | #if 0 |
1013 | dns->send_interval *= 0.999; |
1137 | dns->send_interval *= 0.999; |
1014 | #endif |
1138 | #endif |
1015 | if (dns->send_interval < MIN_SEND_INTERVAL) |
|
|
1016 | dns->send_interval = MIN_SEND_INTERVAL; |
|
|
1017 | |
|
|
1018 | // the latency surely puts an upper bound on |
1139 | // the latency surely puts an upper bound on |
1019 | // the minimum send interval |
1140 | // the minimum send interval |
1020 | double latency = NOW - (*i)->sent; |
1141 | double latency = ev_now () - (*i)->sent; |
|
|
1142 | |
|
|
1143 | if (latency < dns->min_latency) |
1021 | dns->last_latency = latency; |
1144 | dns->min_latency = latency; |
1022 | |
1145 | |
1023 | if (dns->send_interval > latency) |
1146 | if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) |
|
|
1147 | dns->send_interval = dns->min_latency * conf.dns_overlap_factor; |
|
|
1148 | |
|
|
1149 | if (dns->send_interval < conf.dns_send_interval) |
1024 | dns->send_interval = latency; |
1150 | dns->send_interval = conf.dns_send_interval; |
1025 | } |
1151 | } |
1026 | |
1152 | |
1027 | delete *i; |
1153 | delete *i; |
1028 | dns_sndpq.erase (i); |
1154 | dns_sndpq.erase (i); |
1029 | |
1155 | |
… | |
… | |
1047 | ttl |= pkt [offs++] << 16; |
1173 | ttl |= pkt [offs++] << 16; |
1048 | ttl |= pkt [offs++] << 8; |
1174 | ttl |= pkt [offs++] << 8; |
1049 | ttl |= pkt [offs++]; |
1175 | ttl |= pkt [offs++]; |
1050 | u16 rdlen = pkt [offs++] << 8; rdlen |= pkt [offs++]; |
1176 | u16 rdlen = pkt [offs++] << 8; rdlen |= pkt [offs++]; |
1051 | |
1177 | |
1052 | if (qtype == RR_TYPE_NULL || qtype == RR_TYPE_TXT) |
1178 | if (qtype == RR_TYPE_NULL || qtype == RR_TYPE_TXT || qtype == dns->cfg.rrtype) |
1053 | { |
1179 | { |
1054 | if (rdlen <= MAXSIZE - offs) |
1180 | if (rdlen <= MAXSIZE - offs) |
1055 | { |
1181 | { |
1056 | // decode bytes, finally |
1182 | // decode bytes, finally |
1057 | |
1183 | |
… | |
… | |
1083 | { |
1209 | { |
1084 | slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]); |
1210 | slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]); |
1085 | |
1211 | |
1086 | if (ip [3] == CMD_IP_RST) |
1212 | if (ip [3] == CMD_IP_RST) |
1087 | { |
1213 | { |
1088 | slog (L_DEBUG, _("DNS: got tunnel RST request")); |
1214 | slog (L_DEBUG, _("DNS: got tunnel RST request.")); |
1089 | |
1215 | |
1090 | delete dns; c->dns = 0; |
1216 | dns->reset (); |
1091 | |
|
|
1092 | return; |
1217 | return; |
1093 | } |
1218 | } |
1094 | else if (ip [3] == CMD_IP_SYN) |
1219 | else if (ip [3] == CMD_IP_SYN) |
1095 | { |
1220 | { |
1096 | slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us.")); |
1221 | slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us.")); |
1097 | dns->established = true; |
1222 | dns->established = true; |
1098 | } |
1223 | } |
|
|
1224 | else if (ip [3] == CMD_IP_CSE) |
|
|
1225 | { |
|
|
1226 | if (conf.dns_case_preserving) |
|
|
1227 | { |
|
|
1228 | slog (L_INFO, _("DNS: got tunnel CSE reply, globally downgrading to case-insensitive protocol.")); |
|
|
1229 | conf.dns_case_preserving = false; |
|
|
1230 | dns->reset (); |
|
|
1231 | return; |
|
|
1232 | } |
|
|
1233 | else |
|
|
1234 | { |
|
|
1235 | slog (L_DEBUG, _("DNS: got tunnel CSE reply, server likes us.")); |
|
|
1236 | dns->established = true; |
|
|
1237 | } |
|
|
1238 | } |
1099 | else if (ip [3] == CMD_IP_REJ) |
1239 | else if (ip [3] == CMD_IP_REJ) |
1100 | { |
1240 | { |
1101 | slog (L_DEBUG, _("DNS: got tunnel REJ reply, server does not like us, aborting.")); |
1241 | slog (L_ERR, _("DNS: got tunnel REJ reply, server does not like us.")); |
1102 | abort (); |
1242 | dns->tw.start (60.); |
1103 | } |
1243 | } |
1104 | else |
1244 | else |
|
|
1245 | { |
1105 | slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]); |
1246 | slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]); |
|
|
1247 | dns->tw.start (60.); |
|
|
1248 | } |
1106 | } |
1249 | } |
1107 | else |
1250 | else |
1108 | slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"), |
1251 | slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"), |
1109 | ip [0], ip [1], ip [2], ip [3]); |
1252 | ip [0], ip [1], ip [2], ip [3]); |
1110 | |
1253 | |
… | |
… | |
1134 | break; |
1277 | break; |
1135 | } |
1278 | } |
1136 | } |
1279 | } |
1137 | |
1280 | |
1138 | void |
1281 | void |
1139 | vpn::dnsv4_ev (io_watcher &w, short revents) |
1282 | vpn::dnsv4_ev (ev::io &w, int revents) |
1140 | { |
1283 | { |
1141 | if (revents & EVENT_READ) |
1284 | if (revents & EV_READ) |
1142 | { |
1285 | { |
1143 | dns_packet *pkt = new dns_packet; |
1286 | dns_packet *pkt = new dns_packet; |
1144 | struct sockaddr_in sa; |
1287 | struct sockaddr_in sa; |
1145 | socklen_t sa_len = sizeof (sa); |
1288 | socklen_t sa_len = sizeof (sa); |
1146 | |
1289 | |
1147 | pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
1290 | pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
1148 | |
1291 | |
1149 | if (pkt->len > 0) |
1292 | if (pkt->len > 0) |
1150 | { |
1293 | { |
1151 | if (THISNODE->dns_port) |
1294 | if (ntohs (pkt->flags) & FLAG_RESPONSE) |
|
|
1295 | dnsv4_client (*pkt); |
|
|
1296 | else |
1152 | { |
1297 | { |
1153 | dnsv4_server (*pkt); |
1298 | dnsv4_server (*pkt); |
1154 | sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); |
1299 | sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); |
1155 | } |
1300 | } |
1156 | else |
|
|
1157 | dnsv4_client (*pkt); |
|
|
1158 | |
1301 | |
1159 | delete pkt; |
1302 | delete pkt; |
1160 | } |
1303 | } |
1161 | } |
1304 | } |
1162 | } |
1305 | } |
1163 | |
1306 | |
1164 | bool |
1307 | bool |
1165 | vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1308 | vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1166 | { |
1309 | { |
1167 | int client = ntohs (si.port); |
1310 | int client = ntohl (si.host); |
1168 | |
1311 | |
1169 | assert (0 < client && client <= conns.size ()); |
1312 | assert (0 < client && client <= conns.size ()); |
1170 | |
1313 | |
1171 | connection *c = conns [client - 1]; |
1314 | connection *c = conns [client - 1]; |
1172 | |
1315 | |
1173 | if (!c->dns) |
1316 | if (!c->dns) |
1174 | c->dns = new dns_connection (c); |
1317 | c->dns = new dns_connection (c); |
1175 | |
1318 | |
1176 | if (!c->dns->snddq.put (pkt)) |
1319 | if (c->dns->snddq.put (pkt)) |
1177 | return false; |
1320 | { |
|
|
1321 | min_it (c->dns->poll_interval, 0.25); |
|
|
1322 | c->dns->tw (); |
|
|
1323 | } |
1178 | |
1324 | |
1179 | c->dns->tw.trigger (); |
1325 | // always return true even if the buffer overflows |
1180 | |
|
|
1181 | return true; |
1326 | return true; |
1182 | } |
1327 | } |
1183 | |
1328 | |
1184 | void |
1329 | void |
1185 | connection::dnsv4_reset_connection () |
1330 | dns_connection::time_cb (ev::timer &w, int revents) |
1186 | { |
|
|
1187 | //delete dns; dns = 0; //TODO |
|
|
1188 | } |
|
|
1189 | |
|
|
1190 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
|
|
1191 | |
|
|
1192 | void |
|
|
1193 | dns_connection::time_cb (time_watcher &w) |
|
|
1194 | { |
1331 | { |
1195 | // servers have to be polled |
1332 | // servers have to be polled |
1196 | if (THISNODE->dns_port) |
1333 | if (THISNODE->dns_port) |
1197 | return; |
1334 | return; |
1198 | |
1335 | |
1199 | // check for timeouts and (re)transmit |
1336 | // check for timeouts and (re)transmit |
1200 | tstamp next = NOW + poll_interval; |
1337 | tstamp next = 86400 * 365; |
1201 | dns_snd *send = 0; |
1338 | dns_snd *send = 0; |
1202 | |
1339 | |
1203 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1340 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
1204 | i != vpn->dns_sndpq.end (); |
1341 | i != vpn->dns_sndpq.end (); |
1205 | ++i) |
1342 | ++i) |
1206 | { |
1343 | { |
1207 | dns_snd *r = *i; |
1344 | dns_snd *r = *i; |
1208 | |
1345 | |
1209 | if (r->timeout <= NOW) |
1346 | if (r->timeout <= ev_now ()) |
1210 | { |
1347 | { |
1211 | if (!send) |
1348 | if (!send) |
1212 | { |
1349 | { |
1213 | send = r; |
1350 | send = r; |
1214 | |
1351 | |
1215 | r->retry++; |
1352 | r->retry++; |
1216 | r->timeout = NOW + (r->retry * last_latency * 8.); |
1353 | r->timeout = ev_now () + r->retry * min_latency * conf.dns_timeout_factor; |
|
|
1354 | //printf ("RETRY %x (%d, %f)\n", r->seqno, r->retry, r->timeout - ev_now ());//D |
1217 | |
1355 | |
1218 | // the following code changes the query section a bit, forcing |
1356 | // the following code changes the query section a bit, forcing |
1219 | // the forwarder to generate a new request |
1357 | // the forwarder to generate a new request |
1220 | if (r->stdhdr) |
1358 | if (r->stdhdr) |
1221 | { |
|
|
1222 | //printf ("reencoded header for ID %d retry %d:%d:%d\n", htons (r->pkt->id), THISNODE->id, r->seqno, r->retry);printf ("reencoded header for ID %d retry %d:%d:%d\n", htons (r->pkt->id), THISNODE->id, r->seqno, r->retry); |
|
|
1223 | //encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); |
1359 | encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); |
1224 | } |
|
|
1225 | } |
1360 | } |
1226 | } |
1361 | } |
1227 | else |
1362 | else |
1228 | NEXT (r->timeout); |
1363 | min_it (next, r->timeout - ev_now ()); |
1229 | } |
|
|
1230 | |
|
|
1231 | if (last_sent + send_interval <= NOW) |
|
|
1232 | { |
1364 | } |
|
|
1365 | |
1233 | if (!send) |
1366 | if (!send) |
|
|
1367 | { |
|
|
1368 | // generate a new packet, if wise |
|
|
1369 | |
|
|
1370 | if (!established) |
1234 | { |
1371 | { |
1235 | // generate a new packet, if wise |
1372 | if (vpn->dns_sndpq.empty ()) |
1236 | |
|
|
1237 | if (!established) |
|
|
1238 | { |
1373 | { |
1239 | if (vpn->dns_sndpq.empty ()) |
|
|
1240 | { |
|
|
1241 | send = new dns_snd (this); |
1374 | send = new dns_snd (this); |
1242 | |
1375 | |
1243 | cfg.reset (THISNODE->id); |
1376 | cfg.reset (THISNODE->id); |
|
|
1377 | set_cfg (); |
1244 | send->gen_syn_req (); |
1378 | send->gen_syn_req (); |
1245 | } |
|
|
1246 | } |
1379 | } |
1247 | else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING |
1380 | } |
|
|
1381 | else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding |
1248 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
1382 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
|
|
1383 | { |
|
|
1384 | if (last_sent + send_interval <= ev_now ()) |
1249 | { |
1385 | { |
1250 | //printf ("sending data request etc.\n"); //D |
1386 | //printf ("sending data request etc.\n"); //D |
1251 | if (!snddq.empty ()) |
1387 | if (!snddq.empty ()) |
1252 | { |
|
|
1253 | poll_interval = send_interval; |
|
|
1254 | NEXT (NOW + send_interval); |
1388 | min_it (next, send_interval); |
1255 | } |
|
|
1256 | |
1389 | |
1257 | send = new dns_snd (this); |
1390 | send = new dns_snd (this); |
1258 | send->gen_stream_req (sndseq, snddq); |
1391 | send->gen_stream_req (sndseq, snddq); |
1259 | send->timeout = NOW + last_latency * 8.; |
1392 | send->timeout = ev_now () + min_latency * conf.dns_timeout_factor; |
|
|
1393 | //printf ("SEND %x (%f)\n", send->seqno, send->timeout - ev_now (), min_latency, conf.dns_timeout_factor);//D |
1260 | |
1394 | |
1261 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1395 | sndseq = (sndseq + 1) & SEQNO_MASK; |
1262 | } |
1396 | } |
1263 | |
1397 | else |
1264 | if (send) |
1398 | min_it (next, last_sent + send_interval - ev_now ()); |
1265 | vpn->dns_sndpq.push_back (send); |
|
|
1266 | } |
1399 | } |
1267 | |
1400 | |
1268 | if (send) |
1401 | if (send) |
1269 | { |
1402 | vpn->dns_sndpq.push_back (send); |
1270 | last_sent = NOW; |
1403 | } |
|
|
1404 | |
|
|
1405 | if (send) |
|
|
1406 | { |
|
|
1407 | last_sent = ev_now (); |
1271 | sendto (vpn->dnsv4_fd, |
1408 | sendto (vpn->dnsv4_fd, |
1272 | send->pkt->at (0), send->pkt->len, 0, |
1409 | send->pkt->at (0), send->pkt->len, 0, |
1273 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1410 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1274 | } |
|
|
1275 | } |
1411 | } |
1276 | else |
|
|
1277 | NEXT (last_sent + send_interval); |
|
|
1278 | |
1412 | |
|
|
1413 | min_it (next, last_sent + max (poll_interval, send_interval) - ev_now ()); |
|
|
1414 | |
1279 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d)", |
1415 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", |
1280 | poll_interval, send_interval, next - NOW, |
1416 | poll_interval, send_interval, next - ev_now (), |
1281 | vpn->dns_sndpq.size (), snddq.size ()); |
1417 | vpn->dns_sndpq.size (), snddq.size (), |
1282 | |
1418 | rcvpq.size ()); |
1283 | // TODO: no idea when this happens, but when next < NOW, we have a problem |
|
|
1284 | if (next < NOW + 0.0001) |
|
|
1285 | next = NOW + 0.1; |
|
|
1286 | |
1419 | |
1287 | w.start (next); |
1420 | w.start (next); |
1288 | } |
1421 | } |
1289 | |
1422 | |
1290 | #endif |
1423 | #endif |