… | |
… | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | GNU General Public License for more details. |
15 | GNU General 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 |
18 | along with gvpe; if not, write to the Free Software |
18 | along with gvpe; if not, write to the Free Software |
19 | Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 | Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
20 | */ |
20 | */ |
|
|
21 | |
|
|
22 | // TODO: EDNS0 option to increase dns mtu? |
|
|
23 | // TODO: re-write dns packet parsing/creation using a safe mem-buffer |
|
|
24 | // to ensure no buffer overflows or similar problems. |
21 | |
25 | |
22 | #include "config.h" |
26 | #include "config.h" |
23 | |
27 | |
24 | #if ENABLE_DNS |
28 | #if ENABLE_DNS |
25 | |
29 | |
26 | // dns processing is EXTREMELY ugly. For obvious(?) reasons. |
30 | // dns processing is EXTREMELY ugly. For obvious(?) reasons. |
27 | // it's a hack, use only in emergency situations please. |
31 | // it's a hack, use only in emergency situations please. |
28 | |
32 | |
29 | #include <cstring> |
33 | #include <cstring> |
|
|
34 | #include <cassert> |
30 | |
35 | |
31 | #include <sys/types.h> |
36 | #include <sys/types.h> |
32 | #include <sys/socket.h> |
37 | #include <sys/socket.h> |
33 | #include <sys/wait.h> |
38 | #include <sys/wait.h> |
34 | #include <sys/uio.h> |
39 | #include <sys/uio.h> |
… | |
… | |
37 | #include <unistd.h> |
42 | #include <unistd.h> |
38 | #include <fcntl.h> |
43 | #include <fcntl.h> |
39 | |
44 | |
40 | #include <map> |
45 | #include <map> |
41 | |
46 | |
|
|
47 | #include <cstdio> /* bug in libgmp: gmp.h relies on cstdio being included */ |
42 | #include <gmp.h> |
48 | #include <gmp.h> |
43 | |
49 | |
44 | #include "netcompat.h" |
50 | #include "netcompat.h" |
45 | |
51 | |
46 | #include "vpn.h" |
52 | #include "vpn.h" |
47 | |
53 | |
48 | #define MIN_RETRY 1. |
54 | #define MAX_POLL_INTERVAL 5. // how often to poll minimally when the server has no data |
49 | #define MAX_RETRY 6. |
55 | #define ACTIVITY_INTERVAL 5. |
50 | |
56 | |
51 | #define MAX_OUTSTANDING 400 // max. outstanding requests |
57 | #define INITIAL_TIMEOUT 0.1 // retry timeouts |
|
|
58 | #define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn |
|
|
59 | |
|
|
60 | #define MAX_SEND_INTERVAL 2. // optimistic? |
|
|
61 | |
52 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING |
62 | #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING, and backlog |
53 | #define MAX_RATE 100 // requests/s |
|
|
54 | #define MAX_BACKLOG (10*1024) // size of protocol backlog, must be > MAXSIZE |
63 | #define MAX_BACKLOG (64*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE |
55 | |
64 | |
56 | #define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well |
65 | #define MAX_DOMAIN_SIZE 240 // 255 is legal limit, but bind doesn't compress well |
57 | // 240 leaves about 4 bytes of server reply data |
66 | // 240 leaves about 4 bytes of server reply data |
58 | // every two request byte sless give room for one reply byte |
67 | // every request byte less give room for two reply bytes |
59 | |
68 | |
60 | #define SEQNO_MASK 0xffff |
69 | #define SEQNO_MASK 0x3fff |
61 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
70 | #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
62 | |
71 | |
63 | #define MAX_LBL_SIZE 63 |
72 | #define MAX_LBL_SIZE 63 |
64 | #define MAX_PKT_SIZE 512 |
73 | #define MAX_PKT_SIZE 512 |
65 | |
74 | |
|
|
75 | #define RR_TYPE_A 1 |
|
|
76 | #define RR_TYPE_NULL 10 |
66 | #define RR_TYPE_TXT 16 |
77 | #define RR_TYPE_TXT 16 |
67 | #define RR_TYPE_ANY 255 |
78 | #define RR_TYPE_ANY 255 |
|
|
79 | |
68 | #define RR_CLASS_IN 1 |
80 | #define RR_CLASS_IN 1 |
|
|
81 | |
|
|
82 | #define CMD_IP_1 207 |
|
|
83 | #define CMD_IP_2 46 |
|
|
84 | #define CMD_IP_3 236 |
|
|
85 | #define CMD_IP_RST 29 |
|
|
86 | #define CMD_IP_SYN 113 |
|
|
87 | #define CMD_IP_REJ 32 |
69 | |
88 | |
70 | // works for cmaps up to 255 (not 256!) |
89 | // works for cmaps up to 255 (not 256!) |
71 | struct charmap |
90 | struct charmap |
72 | { |
91 | { |
73 | enum { INVALID = (u8)255 }; |
92 | enum { INVALID = (u8)255 }; |
… | |
… | |
153 | return dec_len [len]; |
172 | return dec_len [len]; |
154 | } |
173 | } |
155 | |
174 | |
156 | unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len) |
175 | unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len) |
157 | { |
176 | { |
158 | if (!len) |
177 | if (!len || len > MAX_DEC_LEN) |
159 | return 0; |
178 | return 0; |
160 | |
179 | |
161 | int elen = encode_len (len); |
180 | int elen = encode_len (len); |
162 | |
181 | |
163 | mp_limb_t m [MAX_LIMBS]; |
182 | mp_limb_t m [MAX_LIMBS]; |
… | |
… | |
182 | return elen; |
201 | return elen; |
183 | } |
202 | } |
184 | |
203 | |
185 | unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len) |
204 | unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len) |
186 | { |
205 | { |
187 | if (!len) |
206 | if (!len || len > MAX_ENC_LEN) |
188 | return 0; |
207 | return 0; |
189 | |
208 | |
190 | u8 src_ [MAX_ENC_LEN]; |
209 | u8 src_ [MAX_ENC_LEN]; |
191 | unsigned int elen = 0; |
210 | unsigned int elen = 0; |
192 | |
211 | |
… | |
… | |
239 | } |
258 | } |
240 | abort (); |
259 | abort (); |
241 | } |
260 | } |
242 | #endif |
261 | #endif |
243 | |
262 | |
244 | // the following sequence has been crafted to |
263 | //static basecoder cdc64 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI-"); |
245 | // a) look somewhat random |
|
|
246 | // b) the even (and odd) indices never share the same character as upper/lowercase |
|
|
247 | // the "_" is not valid but widely accepted (all octets should be supported, but let's be conservative) |
|
|
248 | // the other sequences are obviously derived |
|
|
249 | //static basecoder cdc63 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); |
264 | //static basecoder cdc63 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); |
250 | static basecoder cdc62 ("dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); |
265 | static basecoder cdc62 ("dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI"); |
251 | //static basecoder cdc36 ("dphzr06qmjkb34tsvl81xaef92wgyo57ucni"); // unused as of yet |
266 | //static basecoder cdc36 ("dphzr06qmjkb34tsvl81xaef92wgyo57ucni"); // unused as of yet |
252 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
267 | static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO"); |
253 | |
268 | |
254 | ///////////////////////////////////////////////////////////////////////////// |
269 | ///////////////////////////////////////////////////////////////////////////// |
255 | |
270 | |
256 | #define HDRSIZE 6 |
271 | #define HDRSIZE 6 |
257 | |
272 | |
258 | inline void encode_header (char *data, int clientid, int seqno) |
273 | inline void encode_header (char *data, int clientid, int seqno, int retry = 0) |
259 | { |
274 | { |
260 | u8 hdr[3] = { clientid, seqno >> 8, seqno }; |
275 | seqno &= SEQNO_MASK; |
|
|
276 | |
|
|
277 | u8 hdr[3] = { |
|
|
278 | clientid, |
|
|
279 | (seqno >> 8) | (retry << 6), |
|
|
280 | seqno, |
|
|
281 | }; |
261 | |
282 | |
262 | assert (clientid < 256); |
283 | assert (clientid < 256); |
263 | |
284 | |
264 | cdc26.encode (data, hdr, 3); |
285 | cdc26.encode (data, hdr, 3); |
265 | } |
286 | } |
… | |
… | |
269 | u8 hdr[3]; |
290 | u8 hdr[3]; |
270 | |
291 | |
271 | cdc26.decode (hdr, data, HDRSIZE); |
292 | cdc26.decode (hdr, data, HDRSIZE); |
272 | |
293 | |
273 | clientid = hdr[0]; |
294 | clientid = hdr[0]; |
274 | seqno = (hdr[1] << 8) | hdr[2]; |
295 | seqno = ((hdr[1] << 8) | hdr[2]) & SEQNO_MASK; |
275 | } |
296 | } |
276 | |
297 | |
277 | ///////////////////////////////////////////////////////////////////////////// |
298 | ///////////////////////////////////////////////////////////////////////////// |
278 | |
299 | |
279 | struct byte_stream |
300 | struct byte_stream |
… | |
… | |
308 | } |
329 | } |
309 | |
330 | |
310 | void byte_stream::remove (int count) |
331 | void byte_stream::remove (int count) |
311 | { |
332 | { |
312 | if (count > fill) |
333 | if (count > fill) |
313 | abort (); |
334 | assert (count <= fill); |
314 | |
335 | |
315 | memmove (data, data + count, fill -= count); |
336 | memmove (data, data + count, fill -= count); |
316 | } |
337 | } |
317 | |
338 | |
318 | bool byte_stream::put (u8 *data, unsigned int datalen) |
339 | bool byte_stream::put (u8 *data, unsigned int datalen) |
… | |
… | |
331 | return false; |
352 | return false; |
332 | |
353 | |
333 | data [fill++] = pkt->len >> 8; |
354 | data [fill++] = pkt->len >> 8; |
334 | data [fill++] = pkt->len; |
355 | data [fill++] = pkt->len; |
335 | |
356 | |
336 | memcpy (data + fill, &((*pkt)[0]), pkt->len); fill += pkt->len; |
357 | memcpy (data + fill, pkt->at (0), pkt->len); fill += pkt->len; |
337 | |
358 | |
338 | return true; |
359 | return true; |
339 | } |
360 | } |
340 | |
361 | |
341 | vpn_packet *byte_stream::get () |
362 | vpn_packet *byte_stream::get () |
342 | { |
363 | { |
|
|
364 | unsigned int len; |
|
|
365 | |
|
|
366 | for (;;) |
|
|
367 | { |
343 | int len = (data [0] << 8) | data [1]; |
368 | len = (data [0] << 8) | data [1]; |
344 | |
369 | |
345 | if (len > MAXSIZE && fill >= 2) |
370 | if (len <= MAXSIZE || fill < 2) |
346 | abort (); // TODO handle this gracefully, connection reset |
371 | break; |
347 | |
372 | |
|
|
373 | // TODO: handle this better than skipping, e.g. by reset |
|
|
374 | slog (L_DEBUG, _("DNS: corrupted packet stream skipping a byte...")); |
|
|
375 | remove (1); |
|
|
376 | } |
|
|
377 | |
348 | if (fill < len + 2) |
378 | if (fill < len + 2) |
349 | return 0; |
379 | return 0; |
350 | |
380 | |
351 | vpn_packet *pkt = new vpn_packet; |
381 | vpn_packet *pkt = new vpn_packet; |
352 | |
382 | |
353 | pkt->len = len; |
383 | pkt->len = len; |
354 | memcpy (&((*pkt)[0]), data + 2, len); |
384 | memcpy (pkt->at (0), data + 2, len); |
355 | remove (len + 2); |
385 | remove (len + 2); |
356 | |
386 | |
357 | return pkt; |
387 | return pkt; |
358 | } |
388 | } |
359 | |
389 | |
… | |
… | |
376 | #define FLAG_RCODE_REFUSED ( 5 << 0) |
406 | #define FLAG_RCODE_REFUSED ( 5 << 0) |
377 | |
407 | |
378 | #define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) |
408 | #define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) |
379 | #define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA) |
409 | #define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA) |
380 | |
410 | |
|
|
411 | struct dns_cfg |
|
|
412 | { |
|
|
413 | static int next_uid; |
|
|
414 | |
|
|
415 | u8 id1, id2, id3, id4; |
|
|
416 | |
|
|
417 | u8 version; |
|
|
418 | u8 flags; |
|
|
419 | u8 rrtype; |
|
|
420 | u8 def_ttl; |
|
|
421 | |
|
|
422 | u16 client; |
|
|
423 | u16 uid; // to make request unique |
|
|
424 | |
|
|
425 | u16 max_size; |
|
|
426 | u8 seq_cdc; |
|
|
427 | u8 req_cdc; |
|
|
428 | |
|
|
429 | u8 rep_cdc; |
|
|
430 | u8 delay; // time in 0.01s units that the server may delay replying packets |
|
|
431 | u8 r3, r4; |
|
|
432 | |
|
|
433 | u8 r5, r6, r7, r8; |
|
|
434 | |
|
|
435 | void reset (int clientid); |
|
|
436 | bool valid (); |
|
|
437 | }; |
|
|
438 | |
|
|
439 | int dns_cfg::next_uid; |
|
|
440 | |
|
|
441 | void dns_cfg::reset (int clientid) |
|
|
442 | { |
|
|
443 | id1 = 'G'; |
|
|
444 | id2 = 'V'; |
|
|
445 | id3 = 'P'; |
|
|
446 | id4 = 'E'; |
|
|
447 | |
|
|
448 | version = 1; |
|
|
449 | |
|
|
450 | rrtype = RR_TYPE_TXT; |
|
|
451 | flags = 0; |
|
|
452 | def_ttl = 0; |
|
|
453 | seq_cdc = 26; |
|
|
454 | req_cdc = 62; |
|
|
455 | rep_cdc = 0; |
|
|
456 | max_size = htons (MAX_PKT_SIZE); |
|
|
457 | client = htons (clientid); |
|
|
458 | uid = next_uid++; |
|
|
459 | delay = 0; |
|
|
460 | |
|
|
461 | r3 = r4 = 0; |
|
|
462 | r4 = r5 = r6 = r7 = 0; |
|
|
463 | } |
|
|
464 | |
|
|
465 | bool dns_cfg::valid () |
|
|
466 | { |
|
|
467 | // although the protocol itself allows for some configurability, |
|
|
468 | // only the following encoding/decoding settings are implemented. |
|
|
469 | return id1 == 'G' |
|
|
470 | && id2 == 'V' |
|
|
471 | && id3 == 'P' |
|
|
472 | && id4 == 'E' |
|
|
473 | && seq_cdc == 26 |
|
|
474 | && req_cdc == 62 |
|
|
475 | && rep_cdc == 0 |
|
|
476 | && version == 1; |
|
|
477 | } |
|
|
478 | |
381 | struct dns_packet : net_packet |
479 | struct dns_packet : net_packet |
382 | { |
480 | { |
383 | u16 id; |
481 | u16 id; |
384 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
482 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
385 | u16 qdcount, ancount, nscount, arcount; |
483 | u16 qdcount, ancount, nscount, arcount; |
386 | |
484 | |
387 | u8 data[MAXSIZE - 6 * 2]; |
485 | u8 data [MAXSIZE - 6 * 2]; |
388 | |
486 | |
389 | int decode_label (char *data, int size, int &offs); |
487 | int decode_label (char *data, int size, int &offs); |
390 | }; |
488 | }; |
391 | |
489 | |
392 | int dns_packet::decode_label (char *data, int size, int &offs) |
490 | int dns_packet::decode_label (char *data, int size, int &offs) |
… | |
… | |
423 | return data - orig; |
521 | return data - orig; |
424 | } |
522 | } |
425 | |
523 | |
426 | ///////////////////////////////////////////////////////////////////////////// |
524 | ///////////////////////////////////////////////////////////////////////////// |
427 | |
525 | |
428 | struct dns_req |
|
|
429 | { |
|
|
430 | dns_packet *pkt; |
|
|
431 | tstamp next; |
|
|
432 | int retry; |
|
|
433 | struct dns_connection *dns; |
|
|
434 | int seqno; |
|
|
435 | |
|
|
436 | dns_req (dns_connection *dns); |
|
|
437 | void gen_stream_req (int seqno, byte_stream &stream); |
|
|
438 | }; |
|
|
439 | |
|
|
440 | static u16 dns_id = 12098; // TODO: should be per-vpn |
526 | static u16 dns_id = 0; // TODO: should be per-vpn |
441 | |
527 | |
442 | static u16 next_id () |
528 | static u16 next_id () |
443 | { |
529 | { |
|
|
530 | if (!dns_id) |
|
|
531 | dns_id = time (0); |
|
|
532 | |
444 | // the simplest lsfr with periodicity 65535 i could find |
533 | // the simplest lsfr with periodicity 65535 i could find |
445 | dns_id = (dns_id << 1) |
534 | dns_id = (dns_id << 1) |
446 | | (((dns_id >> 1) |
535 | | (((dns_id >> 1) |
447 | ^ (dns_id >> 2) |
536 | ^ (dns_id >> 2) |
448 | ^ (dns_id >> 4) |
537 | ^ (dns_id >> 4) |
449 | ^ (dns_id >> 15)) & 1); |
538 | ^ (dns_id >> 15)) & 1); |
450 | |
539 | |
451 | return dns_id; |
540 | return dns_id; |
452 | } |
541 | } |
453 | |
542 | |
|
|
543 | struct dns_rcv; |
|
|
544 | struct dns_snd; |
|
|
545 | |
|
|
546 | struct dns_connection |
|
|
547 | { |
|
|
548 | connection *c; |
|
|
549 | struct vpn *vpn; |
|
|
550 | |
|
|
551 | dns_cfg cfg; |
|
|
552 | |
|
|
553 | bool established; |
|
|
554 | |
|
|
555 | tstamp last_received; |
|
|
556 | tstamp last_sent; |
|
|
557 | double min_latency; |
|
|
558 | double poll_interval, send_interval; |
|
|
559 | |
|
|
560 | vector<dns_rcv *> rcvpq; |
|
|
561 | |
|
|
562 | byte_stream rcvdq; int rcvseq; int repseq; |
|
|
563 | byte_stream snddq; int sndseq; |
|
|
564 | |
|
|
565 | void time_cb (ev::timer &w, int revents); ev::timer tw; |
|
|
566 | void receive_rep (dns_rcv *r); |
|
|
567 | |
|
|
568 | dns_connection (connection *c); |
|
|
569 | ~dns_connection (); |
|
|
570 | }; |
|
|
571 | |
|
|
572 | struct dns_snd |
|
|
573 | { |
|
|
574 | dns_packet *pkt; |
|
|
575 | tstamp timeout, sent; |
|
|
576 | int retry; |
|
|
577 | struct dns_connection *dns; |
|
|
578 | int seqno; |
|
|
579 | bool stdhdr; |
|
|
580 | |
|
|
581 | void gen_stream_req (int seqno, byte_stream &stream); |
|
|
582 | void gen_syn_req (); |
|
|
583 | |
|
|
584 | dns_snd (dns_connection *dns); |
|
|
585 | ~dns_snd (); |
|
|
586 | }; |
|
|
587 | |
454 | dns_req::dns_req (dns_connection *dns) |
588 | dns_snd::dns_snd (dns_connection *dns) |
455 | : dns (dns) |
589 | : dns (dns) |
456 | { |
590 | { |
457 | next = 0; |
591 | timeout = 0; |
458 | retry = 0; |
592 | retry = 0; |
|
|
593 | seqno = 0; |
|
|
594 | sent = ev_now (); |
|
|
595 | stdhdr = false; |
459 | |
596 | |
460 | pkt = new dns_packet; |
597 | pkt = new dns_packet; |
461 | |
598 | |
462 | pkt->id = next_id (); |
599 | pkt->id = next_id (); |
463 | } |
600 | } |
464 | |
601 | |
|
|
602 | dns_snd::~dns_snd () |
|
|
603 | { |
|
|
604 | delete pkt; |
|
|
605 | } |
|
|
606 | |
|
|
607 | static void append_domain (dns_packet &pkt, int &offs, const char *domain) |
|
|
608 | { |
|
|
609 | // add tunnel domain |
|
|
610 | for (;;) |
|
|
611 | { |
|
|
612 | const char *end = strchr (domain, '.'); |
|
|
613 | |
|
|
614 | if (!end) |
|
|
615 | end = domain + strlen (domain); |
|
|
616 | |
|
|
617 | int len = end - domain; |
|
|
618 | |
|
|
619 | pkt [offs++] = len; |
|
|
620 | memcpy (pkt.at (offs), domain, len); |
|
|
621 | offs += len; |
|
|
622 | |
|
|
623 | if (!*end) |
|
|
624 | break; |
|
|
625 | |
|
|
626 | domain = end + 1; |
|
|
627 | } |
|
|
628 | } |
|
|
629 | |
465 | void dns_req::gen_stream_req (int seqno, byte_stream &stream) |
630 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
466 | { |
631 | { |
|
|
632 | stdhdr = true; |
467 | this->seqno = seqno; |
633 | this->seqno = seqno; |
|
|
634 | |
|
|
635 | timeout = ev_now () + INITIAL_TIMEOUT; |
468 | |
636 | |
469 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
637 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
470 | pkt->qdcount = htons (1); |
638 | pkt->qdcount = htons (1); |
471 | |
639 | |
472 | int offs = 6*2; |
640 | int offs = 6*2; |
473 | int dlen = MAX_DOMAIN_SIZE - (strlen (THISNODE->domain) + 2); |
641 | int dlen = MAX_DOMAIN_SIZE - (strlen (dns->c->conf->domain) + 2); |
474 | // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, |
642 | // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, |
475 | // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra |
643 | // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra |
476 | |
644 | |
477 | char enc[256], *encp = enc; |
645 | char enc[256], *encp = enc; |
478 | encode_header (enc, THISNODE->id, seqno); |
646 | encode_header (enc, THISNODE->id, seqno); |
… | |
… | |
496 | encp += lbllen; |
664 | encp += lbllen; |
497 | |
665 | |
498 | enclen -= lbllen; |
666 | enclen -= lbllen; |
499 | } |
667 | } |
500 | |
668 | |
501 | const char *suffix = THISNODE->domain; |
669 | append_domain (*pkt, offs, dns->c->conf->domain); |
502 | |
|
|
503 | // add tunnel domain |
|
|
504 | for (;;) |
|
|
505 | { |
|
|
506 | const char *end = strchr (suffix, '.'); |
|
|
507 | |
|
|
508 | if (!end) |
|
|
509 | end = suffix + strlen (suffix); |
|
|
510 | |
|
|
511 | int len = end - suffix; |
|
|
512 | |
|
|
513 | (*pkt)[offs++] = len; |
|
|
514 | memcpy (&((*pkt)[offs]), suffix, len); |
|
|
515 | offs += len; |
|
|
516 | |
|
|
517 | if (!*end) |
|
|
518 | break; |
|
|
519 | |
|
|
520 | suffix = end + 1; |
|
|
521 | } |
|
|
522 | |
670 | |
523 | (*pkt)[offs++] = 0; |
671 | (*pkt)[offs++] = 0; |
524 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
672 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
|
|
673 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
|
|
674 | |
|
|
675 | pkt->len = offs; |
|
|
676 | } |
|
|
677 | |
|
|
678 | void dns_snd::gen_syn_req () |
|
|
679 | { |
|
|
680 | timeout = ev_now () + INITIAL_SYN_TIMEOUT; |
|
|
681 | |
|
|
682 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
|
|
683 | pkt->qdcount = htons (1); |
|
|
684 | |
|
|
685 | int offs = 6 * 2; |
|
|
686 | |
|
|
687 | int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&dns->cfg, sizeof (dns_cfg)); |
|
|
688 | |
|
|
689 | assert (elen <= MAX_LBL_SIZE); |
|
|
690 | |
|
|
691 | (*pkt)[offs] = elen; |
|
|
692 | offs += elen + 1; |
|
|
693 | append_domain (*pkt, offs, dns->c->conf->domain); |
|
|
694 | |
|
|
695 | (*pkt)[offs++] = 0; |
|
|
696 | (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A; |
525 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
697 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
526 | |
698 | |
527 | pkt->len = offs; |
699 | pkt->len = offs; |
528 | } |
700 | } |
529 | |
701 | |
… | |
… | |
549 | delete pkt; |
721 | delete pkt; |
550 | } |
722 | } |
551 | |
723 | |
552 | ///////////////////////////////////////////////////////////////////////////// |
724 | ///////////////////////////////////////////////////////////////////////////// |
553 | |
725 | |
554 | struct dns_connection |
|
|
555 | { |
|
|
556 | connection *c; |
|
|
557 | struct vpn *vpn; |
|
|
558 | |
|
|
559 | vector<dns_rcv *> rcvpq; |
|
|
560 | |
|
|
561 | int rcvseq; |
|
|
562 | int sndseq; |
|
|
563 | |
|
|
564 | byte_stream rcvdq; |
|
|
565 | byte_stream snddq; |
|
|
566 | |
|
|
567 | void time_cb (time_watcher &w); time_watcher tw; |
|
|
568 | void receive_rep (dns_rcv *r); |
|
|
569 | |
|
|
570 | dns_connection (connection *c); |
|
|
571 | ~dns_connection (); |
|
|
572 | }; |
|
|
573 | |
|
|
574 | dns_connection::dns_connection (connection *c) |
726 | dns_connection::dns_connection (connection *c) |
575 | : c (c) |
727 | : c (c) |
576 | , rcvdq (MAX_BACKLOG * 2) |
728 | , rcvdq (MAX_BACKLOG * 2) |
577 | , snddq (MAX_BACKLOG * 2) |
729 | , snddq (MAX_BACKLOG) |
578 | , tw (this, &dns_connection::time_cb) |
730 | , tw (this, &dns_connection::time_cb) |
579 | { |
731 | { |
580 | vpn = c->vpn; |
732 | vpn = c->vpn; |
581 | |
733 | |
|
|
734 | established = false; |
|
|
735 | |
582 | rcvseq = sndseq = 0; |
736 | rcvseq = repseq = sndseq = 0; |
|
|
737 | |
|
|
738 | last_sent = last_received = 0; |
|
|
739 | poll_interval = 0.5; // starting here |
|
|
740 | send_interval = 0.5; // starting rate |
|
|
741 | min_latency = INITIAL_TIMEOUT; |
583 | } |
742 | } |
584 | |
743 | |
585 | dns_connection::~dns_connection () |
744 | dns_connection::~dns_connection () |
586 | { |
745 | { |
587 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
746 | for (vector<dns_rcv *>::iterator i = rcvpq.begin (); |
588 | i != rcvpq.end (); |
747 | i != rcvpq.end (); |
589 | ++i) |
748 | ++i) |
590 | delete *i; |
749 | delete *i; |
591 | } |
750 | } |
592 | |
751 | |
593 | struct dns_cfg |
|
|
594 | { |
|
|
595 | u8 id1, id2, id3; |
|
|
596 | u8 def_ttl; |
|
|
597 | u8 unused1; |
|
|
598 | u16 max_size; |
|
|
599 | u8 flags1, flags2; |
|
|
600 | }; |
|
|
601 | |
|
|
602 | void dns_connection::receive_rep (dns_rcv *r) |
752 | void dns_connection::receive_rep (dns_rcv *r) |
603 | { |
753 | { |
|
|
754 | if (r->datalen) |
|
|
755 | { |
|
|
756 | last_received = ev_now (); |
|
|
757 | tw (); |
|
|
758 | |
|
|
759 | poll_interval = send_interval; |
|
|
760 | } |
|
|
761 | else |
|
|
762 | { |
|
|
763 | poll_interval *= 1.5; |
|
|
764 | |
|
|
765 | if (poll_interval > MAX_POLL_INTERVAL) |
|
|
766 | poll_interval = MAX_POLL_INTERVAL; |
|
|
767 | } |
|
|
768 | |
604 | rcvpq.push_back (r); |
769 | rcvpq.push_back (r); |
605 | |
770 | |
606 | redo: |
771 | redo: |
607 | |
772 | |
608 | // find next packet |
773 | // find next packet |
609 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
774 | for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
610 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
775 | if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
611 | { |
776 | { |
|
|
777 | //printf ("seqno eq %x %x\n", rcvseq, (*i)->seqno);//D |
612 | // enter the packet into our input stream |
778 | // enter the packet into our input stream |
613 | r = *i; |
779 | r = *i; |
614 | |
780 | |
615 | // remove the oldest packet, look forward, as it's oldest first |
781 | // remove the oldest packet, look forward, as it's oldest first |
616 | for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
782 | for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
617 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
783 | if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
618 | { |
784 | { |
|
|
785 | //printf ("seqno RR %x %x\n", (*j)->seqno, rcvseq - MAX_WINDOW);//D |
619 | delete *j; |
786 | delete *j; |
620 | rcvpq.erase (j); |
787 | rcvpq.erase (j); |
621 | break; |
788 | break; |
622 | } |
789 | } |
623 | |
790 | |
624 | rcvseq = (rcvseq + 1) & SEQNO_MASK; |
791 | rcvseq = (rcvseq + 1) & SEQNO_MASK; |
625 | |
792 | |
626 | if (!rcvdq.put (r->data, r->datalen)) |
793 | if (!rcvdq.put (r->data, r->datalen)) |
|
|
794 | { |
|
|
795 | slog (L_ERR, "DNS: !rcvdq.put (r->data, r->datalen)"); |
627 | abort (); // MUST never overflow, can be caused by data corruption, TODO |
796 | abort (); // MUST never overflow, can be caused by data corruption, TODO |
|
|
797 | } |
628 | |
798 | |
629 | while (vpn_packet *pkt = rcvdq.get ()) |
799 | while (vpn_packet *pkt = rcvdq.get ()) |
630 | { |
800 | { |
631 | sockinfo si; |
801 | sockinfo si; |
632 | si.host = 0; si.port = 0; si.prot = PROT_DNSv4; |
802 | si.host = htonl (c->conf->id); si.port = 0; si.prot = PROT_DNSv4; |
633 | |
803 | |
634 | vpn->recv_vpn_packet (pkt, si); |
804 | vpn->recv_vpn_packet (pkt, si); |
|
|
805 | |
|
|
806 | delete pkt; |
635 | } |
807 | } |
636 | |
808 | |
637 | // check for further packets |
809 | // check for further packets |
638 | goto redo; |
810 | goto redo; |
639 | } |
811 | } |
640 | } |
812 | } |
641 | |
813 | |
642 | dns_packet * |
814 | void |
643 | vpn::dnsv4_server (dns_packet *pkt) |
815 | vpn::dnsv4_server (dns_packet &pkt) |
644 | { |
816 | { |
645 | u16 flags = ntohs (pkt->flags); |
817 | u16 flags = ntohs (pkt.flags); |
646 | |
818 | |
647 | //memcpy (&((*rep)[0]), &((*pkt)[0]), pkt->len); |
|
|
648 | int offs = 6 * 2; // skip header |
819 | int offs = 6 * 2; // skip header |
649 | |
820 | |
650 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
821 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
651 | |
822 | |
652 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
823 | if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
653 | && pkt->qdcount == htons (1)) |
824 | && pkt.qdcount == htons (1)) |
654 | { |
825 | { |
655 | char qname[MAXSIZE]; |
826 | char qname [MAXSIZE]; |
656 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
827 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
657 | |
828 | |
658 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
829 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
659 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
830 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
660 | |
831 | |
661 | pkt->qdcount = htons (1); |
832 | pkt.qdcount = htons (1); |
662 | pkt->ancount = 0; |
833 | pkt.ancount = 0; |
663 | pkt->nscount = 0; // should be self, as other nameservers reply like this |
834 | pkt.nscount = 0; // should be self, as other nameservers reply like this |
664 | pkt->arcount = 0; // a record for self, as other nameservers reply like this |
835 | pkt.arcount = 0; // a record for self, as other nameservers reply like this |
665 | |
836 | |
666 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); |
837 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_SERVFAIL); |
667 | |
838 | |
668 | int dlen = strlen (THISNODE->domain); |
839 | int dlen = strlen (THISNODE->domain); |
669 | |
840 | |
670 | if (qclass == RR_CLASS_IN |
841 | if (qclass == RR_CLASS_IN |
671 | && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT) |
|
|
672 | && qlen > dlen + 1 + HDRSIZE |
842 | && qlen > dlen + 1 |
673 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
843 | && !memcmp (qname + qlen - (dlen + 1), THISNODE->domain, dlen)) |
674 | { |
844 | { |
675 | // correct class, domain: parse |
845 | // now generate reply |
676 | int client, seqno; |
846 | pkt.ancount = htons (1); // one answer RR |
677 | decode_header (qname, client, seqno); |
847 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
678 | |
848 | |
679 | u8 data[MAXSIZE]; |
849 | if ((qtype == RR_TYPE_ANY |
680 | int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE)); |
850 | || qtype == RR_TYPE_TXT |
681 | |
851 | || qtype == RR_TYPE_NULL) |
682 | if (0 < client && client <= conns.size ()) |
852 | && qlen > dlen + 1 + HDRSIZE) |
683 | { |
853 | { |
|
|
854 | // correct class, domain: parse |
|
|
855 | int client, seqno; |
|
|
856 | decode_header (qname, client, seqno); |
|
|
857 | |
|
|
858 | u8 data[MAXSIZE]; |
|
|
859 | int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE)); |
|
|
860 | |
|
|
861 | if (0 < client && client <= conns.size ()) |
|
|
862 | { |
684 | connection *c = conns [client - 1]; |
863 | connection *c = conns [client - 1]; |
685 | |
|
|
686 | if (!c->dns) |
|
|
687 | c->dns = new dns_connection (c); |
|
|
688 | |
|
|
689 | dns_connection *dns = c->dns; |
864 | dns_connection *dns = c->dns; |
|
|
865 | dns_rcv *rcv; |
690 | |
866 | |
|
|
867 | if (dns) |
|
|
868 | { |
691 | for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) |
869 | for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) |
692 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
870 | if (SEQNO_EQ ((*i)->seqno, seqno)) |
|
|
871 | { |
|
|
872 | // already seen that request: simply reply with the cached reply |
|
|
873 | dns_rcv *r = *i; |
|
|
874 | |
|
|
875 | slog (L_DEBUG, "DNS: duplicate packet received ID %d, SEQ %d", htons (r->pkt->id), seqno); |
|
|
876 | |
|
|
877 | // refresh header & id, as the retry count could have changed |
|
|
878 | memcpy (r->pkt->at (6 * 2 + 1), pkt.at (6 * 2 + 1), HDRSIZE); |
|
|
879 | r->pkt->id = pkt.id; |
|
|
880 | |
|
|
881 | memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len); |
|
|
882 | |
|
|
883 | goto duplicate_request; |
|
|
884 | } |
|
|
885 | |
|
|
886 | // new packet, queue |
|
|
887 | rcv = new dns_rcv (seqno, data, datalen); |
|
|
888 | dns->receive_rep (rcv); |
|
|
889 | } |
|
|
890 | |
693 | { |
891 | { |
694 | // already seen that request: simply reply with the cached reply |
892 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
695 | dns_rcv *r = *i; |
|
|
696 | |
893 | |
697 | printf ("DUPLICATE %d\n", htons (r->pkt->id));//D |
894 | int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A; |
|
|
895 | pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type |
|
|
896 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
|
|
897 | pkt [offs++] = 0; pkt [offs++] = 0; |
|
|
898 | pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
698 | |
899 | |
699 | memcpy (pkt->at (0), r->pkt->at (0), offs = r->pkt->len); |
900 | int rdlen_offs = offs += 2; |
700 | pkt->id = r->pkt->id; |
901 | |
701 | goto duplicate_request; |
902 | if (dns) |
|
|
903 | { |
|
|
904 | int dlen = ntohs (dns->cfg.max_size) - offs; |
|
|
905 | |
|
|
906 | // bind doesn't compress well, so reduce further by one label length |
|
|
907 | dlen -= qlen; |
|
|
908 | |
|
|
909 | // only put data into in-order sequence packets, if |
|
|
910 | // we receive out-of-order packets we generate empty |
|
|
911 | // replies |
|
|
912 | //printf ("%d - %d & %x (=%d) < %d\n", seqno, dns->repseq, SEQNO_MASK, (seqno - dns->repseq) & SEQNO_MASK, MAX_WINDOW);//D |
|
|
913 | if (((seqno - dns->repseq) & SEQNO_MASK) <= MAX_WINDOW) |
|
|
914 | { |
|
|
915 | dns->repseq = seqno; |
|
|
916 | |
|
|
917 | while (dlen > 1 && !dns->snddq.empty ()) |
|
|
918 | { |
|
|
919 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
|
|
920 | |
|
|
921 | if (txtlen > dns->snddq.size ()) |
|
|
922 | txtlen = dns->snddq.size (); |
|
|
923 | |
|
|
924 | pkt[offs++] = txtlen; |
|
|
925 | memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
|
|
926 | offs += txtlen; |
|
|
927 | dns->snddq.remove (txtlen); |
|
|
928 | |
|
|
929 | dlen -= txtlen + 1; |
|
|
930 | } |
|
|
931 | } |
|
|
932 | |
|
|
933 | // avoid completely empty TXT rdata |
|
|
934 | if (offs == rdlen_offs) |
|
|
935 | pkt[offs++] = 0; |
|
|
936 | |
|
|
937 | slog (L_NOISE, "DNS: snddq %d", dns->snddq.size ()); |
|
|
938 | } |
|
|
939 | else |
|
|
940 | { |
|
|
941 | // send RST |
|
|
942 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
|
|
943 | pkt [offs++] = CMD_IP_RST; |
|
|
944 | } |
|
|
945 | |
|
|
946 | int rdlen = offs - rdlen_offs; |
|
|
947 | |
|
|
948 | pkt [rdlen_offs - 2] = rdlen >> 8; |
|
|
949 | pkt [rdlen_offs - 1] = rdlen; |
|
|
950 | |
|
|
951 | if (dns) |
|
|
952 | { |
|
|
953 | // now update dns_rcv copy |
|
|
954 | rcv->pkt->len = offs; |
|
|
955 | memcpy (rcv->pkt->at (0), pkt.at (0), offs); |
|
|
956 | } |
702 | } |
957 | } |
703 | |
958 | |
704 | // new packet, queue |
959 | duplicate_request: ; |
705 | dns_rcv *rcv = new dns_rcv (seqno, data, datalen); |
960 | } |
706 | dns->receive_rep (rcv); |
961 | else |
707 | |
|
|
708 | // now generate reply |
|
|
709 | pkt->ancount = htons (1); // one answer RR |
|
|
710 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
962 | pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
|
|
963 | } |
|
|
964 | else if (qtype == RR_TYPE_A |
|
|
965 | && qlen > dlen + 1 + cdc26.encode_len (sizeof (dns_cfg))) |
|
|
966 | { |
|
|
967 | dns_cfg cfg; |
|
|
968 | cdc26.decode ((u8 *)&cfg, qname, cdc26.encode_len (sizeof (dns_cfg))); |
|
|
969 | int client = ntohs (cfg.client); |
711 | |
970 | |
712 | (*pkt) [offs++] = 0xc0; |
|
|
713 | (*pkt) [offs++] = 6 * 2; // same as in query section |
971 | pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
714 | |
972 | |
715 | (*pkt) [offs++] = RR_TYPE_TXT >> 8; (*pkt) [offs++] = RR_TYPE_TXT; |
973 | pkt [offs++] = RR_TYPE_A >> 8; pkt [offs++] = RR_TYPE_A; // type |
716 | (*pkt) [offs++] = RR_CLASS_IN >> 8; (*pkt) [offs++] = RR_CLASS_IN; |
974 | pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
717 | |
|
|
718 | (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; |
975 | pkt [offs++] = 0; pkt [offs++] = 0; |
719 | (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; // TTL |
976 | pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL |
|
|
977 | pkt [offs++] = 0; pkt [offs++] = 4; // rdlength |
720 | |
978 | |
721 | int dlen = MAX_PKT_SIZE - offs - 2; |
979 | slog (L_INFO, _("DNS: client %d connects"), client); |
722 | |
980 | |
723 | // bind doesn't compress well, so reduce further by one label length |
981 | pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
724 | dlen -= qlen; |
982 | pkt [offs++] = CMD_IP_REJ; |
725 | |
983 | |
726 | int rdlen_offs = offs += 2; |
984 | if (0 < client && client <= conns.size ()) |
727 | |
|
|
728 | while (dlen > 1 && !dns->snddq.empty ()) |
|
|
729 | { |
985 | { |
730 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
986 | connection *c = conns [client - 1]; |
731 | |
987 | |
732 | if (txtlen > dns->snddq.size ()) |
988 | if (cfg.valid ()) |
733 | txtlen = dns->snddq.size (); |
989 | { |
|
|
990 | pkt [offs - 1] = CMD_IP_SYN; |
734 | |
991 | |
735 | (*pkt)[offs++] = txtlen; |
992 | delete c->dns; |
736 | memcpy (pkt->at (offs), dns->snddq.begin (), txtlen); |
993 | c->dns = new dns_connection (c); |
737 | offs += txtlen; |
994 | c->dns->cfg = cfg; |
738 | dns->snddq.remove (txtlen); |
995 | } |
739 | |
|
|
740 | dlen -= txtlen + 1; |
|
|
741 | } |
996 | } |
742 | |
|
|
743 | // avoid empty TXT rdata |
|
|
744 | if (offs == rdlen_offs) |
|
|
745 | (*pkt)[offs++] = 0; |
|
|
746 | |
|
|
747 | int rdlen = offs - rdlen_offs; |
|
|
748 | |
|
|
749 | (*pkt) [rdlen_offs - 2] = rdlen >> 8; |
|
|
750 | (*pkt) [rdlen_offs - 1] = rdlen; |
|
|
751 | |
|
|
752 | // now update dns_rcv copy |
|
|
753 | rcv->pkt->len = offs; |
|
|
754 | memcpy (rcv->pkt->at (0), pkt->at (0), offs); |
|
|
755 | |
|
|
756 | duplicate_request: ; |
|
|
757 | } |
997 | } |
758 | else |
|
|
759 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
|
|
760 | } |
998 | } |
761 | |
999 | |
762 | pkt->len = offs; |
1000 | pkt.len = offs; |
763 | } |
1001 | } |
764 | |
|
|
765 | return pkt; |
|
|
766 | } |
1002 | } |
767 | |
1003 | |
768 | void |
1004 | void |
769 | vpn::dnsv4_client (dns_packet *pkt) |
1005 | vpn::dnsv4_client (dns_packet &pkt) |
770 | { |
1006 | { |
771 | u16 flags = ntohs (pkt->flags); |
1007 | u16 flags = ntohs (pkt.flags); |
772 | int offs = 6 * 2; // skip header |
1008 | int offs = 6 * 2; // skip header |
773 | |
1009 | |
774 | pkt->qdcount = ntohs (pkt->qdcount); |
1010 | pkt.qdcount = ntohs (pkt.qdcount); |
775 | pkt->ancount = ntohs (pkt->ancount); |
1011 | pkt.ancount = ntohs (pkt.ancount); |
776 | |
1012 | |
777 | // go through our request list and find the corresponding request |
1013 | // go through our request list and find the corresponding request |
778 | for (vector<dns_req *>::iterator i = dns_sndpq.begin (); |
1014 | for (vector<dns_snd *>::iterator i = dns_sndpq.begin (); |
779 | i != dns_sndpq.end (); |
1015 | i != dns_sndpq.end (); |
780 | ++i) |
1016 | ++i) |
781 | if ((*i)->pkt->id == pkt->id) |
1017 | if ((*i)->pkt->id == pkt.id) |
782 | { |
1018 | { |
783 | dns_connection *dns = (*i)->dns; |
1019 | dns_connection *dns = (*i)->dns; |
|
|
1020 | connection *c = dns->c; |
784 | int seqno = (*i)->seqno; |
1021 | int seqno = (*i)->seqno; |
785 | u8 data[MAXSIZE], *datap = data; |
1022 | u8 data[MAXSIZE], *datap = data; |
|
|
1023 | //printf ("rcv pkt %x\n", seqno);//D |
|
|
1024 | |
|
|
1025 | if ((*i)->retry) |
|
|
1026 | { |
|
|
1027 | dns->send_interval *= 1.01; |
|
|
1028 | if (dns->send_interval > MAX_SEND_INTERVAL) |
|
|
1029 | dns->send_interval = MAX_SEND_INTERVAL; |
|
|
1030 | } |
|
|
1031 | else |
|
|
1032 | { |
|
|
1033 | #if 0 |
|
|
1034 | dns->send_interval *= 0.999; |
|
|
1035 | #endif |
|
|
1036 | // the latency surely puts an upper bound on |
|
|
1037 | // the minimum send interval |
|
|
1038 | double latency = ev_now () - (*i)->sent; |
|
|
1039 | |
|
|
1040 | if (latency < dns->min_latency) |
|
|
1041 | dns->min_latency = latency; |
|
|
1042 | |
|
|
1043 | if (dns->send_interval > dns->min_latency * conf.dns_overlap_factor) |
|
|
1044 | dns->send_interval = dns->min_latency * conf.dns_overlap_factor; |
|
|
1045 | |
|
|
1046 | if (dns->send_interval < conf.dns_send_interval) |
|
|
1047 | dns->send_interval = conf.dns_send_interval; |
|
|
1048 | } |
786 | |
1049 | |
787 | delete *i; |
1050 | delete *i; |
788 | dns_sndpq.erase (i); |
1051 | dns_sndpq.erase (i); |
789 | |
1052 | |
790 | if (flags & FLAG_RESPONSE && !(flags & (FLAG_OP_MASK | FLAG_TC))) |
1053 | if (flags & FLAG_RESPONSE && !(flags & FLAG_OP_MASK)) |
791 | { |
1054 | { |
792 | char qname[MAXSIZE]; |
1055 | char qname[MAXSIZE]; |
793 | |
1056 | |
794 | while (pkt->qdcount-- && offs < MAXSIZE - 4) |
1057 | while (pkt.qdcount-- && offs < MAXSIZE - 4) |
795 | { |
1058 | { |
796 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
1059 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
797 | offs += 4; // skip qtype, qclass |
1060 | offs += 4; // skip qtype, qclass |
798 | } |
1061 | } |
799 | |
1062 | |
800 | while (pkt->ancount-- && offs < MAXSIZE - 10 && datap) |
1063 | while (pkt.ancount-- && offs < MAXSIZE - 10 && datap) |
801 | { |
1064 | { |
802 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
1065 | int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
803 | |
1066 | |
804 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
1067 | u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
805 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
1068 | u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
806 | u32 ttl = (*pkt) [offs++] << 24; |
1069 | u32 ttl = pkt [offs++] << 24; |
807 | ttl |= (*pkt) [offs++] << 16; |
1070 | ttl |= pkt [offs++] << 16; |
808 | ttl |= (*pkt) [offs++] << 8; |
1071 | ttl |= pkt [offs++] << 8; |
809 | ttl |= (*pkt) [offs++]; |
1072 | ttl |= pkt [offs++]; |
810 | |
|
|
811 | u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++]; |
1073 | u16 rdlen = pkt [offs++] << 8; rdlen |= pkt [offs++]; |
812 | |
1074 | |
813 | if (rdlen <= MAXSIZE - offs) |
1075 | if (qtype == RR_TYPE_NULL || qtype == RR_TYPE_TXT) |
814 | { |
1076 | { |
815 | // decode bytes, finally |
1077 | if (rdlen <= MAXSIZE - offs) |
816 | |
|
|
817 | while (rdlen) |
|
|
818 | { |
1078 | { |
|
|
1079 | // decode bytes, finally |
|
|
1080 | |
|
|
1081 | while (rdlen) |
|
|
1082 | { |
819 | int txtlen = (*pkt) [offs++]; |
1083 | int txtlen = pkt [offs++]; |
820 | |
1084 | |
821 | assert (txtlen + offs < MAXSIZE - 1); |
1085 | assert (txtlen + offs < MAXSIZE - 1); |
822 | |
1086 | |
823 | memcpy (datap, pkt->at (offs), txtlen); |
1087 | memcpy (datap, pkt.at (offs), txtlen); |
824 | datap += txtlen; offs += txtlen; |
1088 | datap += txtlen; offs += txtlen; |
825 | |
1089 | |
826 | rdlen -= txtlen + 1; |
1090 | rdlen -= txtlen + 1; |
|
|
1091 | } |
827 | } |
1092 | } |
828 | } |
1093 | } |
|
|
1094 | else if (qtype == RR_TYPE_A) |
|
|
1095 | { |
|
|
1096 | u8 ip [4]; |
|
|
1097 | |
|
|
1098 | ip [0] = pkt [offs++]; |
|
|
1099 | ip [1] = pkt [offs++]; |
|
|
1100 | ip [2] = pkt [offs++]; |
|
|
1101 | ip [3] = pkt [offs++]; |
|
|
1102 | |
|
|
1103 | if (ip [0] == CMD_IP_1 |
|
|
1104 | && ip [1] == CMD_IP_2 |
|
|
1105 | && ip [2] == CMD_IP_3) |
|
|
1106 | { |
|
|
1107 | slog (L_TRACE, _("DNS: got tunnel meta command %02x"), ip [3]); |
|
|
1108 | |
|
|
1109 | if (ip [3] == CMD_IP_RST) |
|
|
1110 | { |
|
|
1111 | slog (L_DEBUG, _("DNS: got tunnel RST request")); |
|
|
1112 | |
|
|
1113 | delete dns; c->dns = 0; |
|
|
1114 | |
|
|
1115 | return; |
|
|
1116 | } |
|
|
1117 | else if (ip [3] == CMD_IP_SYN) |
|
|
1118 | { |
|
|
1119 | slog (L_DEBUG, _("DNS: got tunnel SYN reply, server likes us.")); |
|
|
1120 | dns->established = true; |
|
|
1121 | } |
|
|
1122 | else if (ip [3] == CMD_IP_REJ) |
|
|
1123 | { |
|
|
1124 | slog (L_DEBUG, _("DNS: got tunnel REJ reply, server does not like us, aborting.")); |
|
|
1125 | abort (); |
|
|
1126 | } |
|
|
1127 | else |
|
|
1128 | slog (L_INFO, _("DNS: got unknown meta command %02x"), ip [3]); |
|
|
1129 | } |
|
|
1130 | else |
|
|
1131 | slog (L_INFO, _("DNS: got spurious a record %d.%d.%d.%d"), |
|
|
1132 | ip [0], ip [1], ip [2], ip [3]); |
|
|
1133 | |
|
|
1134 | return; |
|
|
1135 | } |
829 | |
1136 | |
830 | int client, rseqno; |
1137 | int client, rseqno; |
831 | decode_header (qname, client, rseqno); |
1138 | decode_header (qname, client, rseqno); |
832 | |
1139 | |
833 | if (client != THISNODE->id) |
1140 | if (client != THISNODE->id) |
834 | { |
1141 | { |
835 | slog (L_INFO, _("got dns tunnel response with wrong clientid, ignoring")); |
1142 | slog (L_INFO, _("DNS: got dns tunnel response with wrong clientid, ignoring")); |
836 | datap = 0; |
1143 | datap = 0; |
837 | } |
1144 | } |
838 | else if (rseqno != seqno) |
1145 | else if (rseqno != seqno) |
839 | { |
1146 | { |
840 | slog (L_DEBUG, _("got dns tunnel response with wrong seqno, badly caching nameserver?")); |
1147 | slog (L_DEBUG, _("DNS: got dns tunnel response with wrong seqno, badly caching nameserver?")); |
841 | datap = 0; |
1148 | datap = 0; |
842 | } |
1149 | } |
843 | } |
1150 | } |
844 | } |
1151 | } |
845 | |
1152 | |
… | |
… | |
847 | if (datap) |
1154 | if (datap) |
848 | dns->receive_rep (new dns_rcv (seqno, data, datap - data)); |
1155 | dns->receive_rep (new dns_rcv (seqno, data, datap - data)); |
849 | |
1156 | |
850 | break; |
1157 | break; |
851 | } |
1158 | } |
852 | |
|
|
853 | delete pkt; |
|
|
854 | } |
1159 | } |
855 | |
1160 | |
856 | void |
1161 | void |
857 | vpn::dnsv4_ev (io_watcher &w, short revents) |
1162 | vpn::dnsv4_ev (ev::io &w, int revents) |
858 | { |
1163 | { |
859 | if (revents & EVENT_READ) |
1164 | if (revents & EV_READ) |
860 | { |
1165 | { |
861 | dns_packet *pkt = new dns_packet; |
1166 | dns_packet *pkt = new dns_packet; |
862 | struct sockaddr_in sa; |
1167 | struct sockaddr_in sa; |
863 | socklen_t sa_len = sizeof (sa); |
1168 | socklen_t sa_len = sizeof (sa); |
864 | |
1169 | |
865 | pkt->len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
1170 | pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
866 | |
1171 | |
867 | if (pkt->len > 0) |
1172 | if (pkt->len > 0) |
868 | { |
1173 | { |
869 | if (pkt->flags & htons (FLAG_TC)) |
1174 | if (ntohs (pkt->flags) & FLAG_RESPONSE) |
|
|
1175 | dnsv4_client (*pkt); |
|
|
1176 | else |
870 | { |
1177 | { |
871 | slog (L_WARN, _("DNS request/response truncated, check protocol settings.")); |
1178 | dnsv4_server (*pkt); |
872 | //TODO connection reset |
1179 | sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); |
873 | } |
1180 | } |
874 | |
1181 | |
875 | if (THISNODE->dns_port) |
1182 | delete pkt; |
876 | { |
|
|
877 | pkt = dnsv4_server (pkt); |
|
|
878 | sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len); |
|
|
879 | } |
|
|
880 | else |
|
|
881 | dnsv4_client (pkt); |
|
|
882 | } |
1183 | } |
883 | } |
1184 | } |
884 | } |
1185 | } |
885 | |
1186 | |
886 | bool |
1187 | bool |
887 | connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
1188 | vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
888 | { |
1189 | { |
|
|
1190 | int client = ntohl (si.host); |
|
|
1191 | |
|
|
1192 | assert (0 < client && client <= conns.size ()); |
|
|
1193 | |
|
|
1194 | connection *c = conns [client - 1]; |
|
|
1195 | |
889 | if (!dns) |
1196 | if (!c->dns) |
890 | dns = new dns_connection (this); |
1197 | c->dns = new dns_connection (c); |
891 | |
1198 | |
892 | if (!dns->snddq.put (pkt)) |
1199 | if (c->dns->snddq.put (pkt)) |
893 | return false; |
1200 | c->dns->tw (); |
894 | |
1201 | |
895 | // start timer if neccessary |
1202 | // always return true even if the buffer overflows |
896 | if (!THISNODE->dns_port && !dns->tw.active) |
|
|
897 | dns->tw.trigger (); |
|
|
898 | |
|
|
899 | return true; |
1203 | return true; |
900 | } |
1204 | } |
901 | |
1205 | |
902 | void |
1206 | void |
903 | dns_connection::time_cb (time_watcher &w) |
1207 | connection::dnsv4_reset_connection () |
904 | { |
1208 | { |
|
|
1209 | //delete dns; dns = 0; //TODO |
|
|
1210 | } |
|
|
1211 | |
|
|
1212 | #define NEXT(w) do { if (next > (w)) next = w; } while (0) |
|
|
1213 | |
|
|
1214 | void |
|
|
1215 | dns_connection::time_cb (ev::timer &w, int revents) |
|
|
1216 | { |
|
|
1217 | // servers have to be polled |
|
|
1218 | if (THISNODE->dns_port) |
|
|
1219 | return; |
|
|
1220 | |
905 | // check for timeouts and (re)transmit |
1221 | // check for timeouts and (re)transmit |
906 | tstamp next = NOW + 60; |
1222 | tstamp next = ev::now () + poll_interval; |
907 | dns_req *send = 0; |
1223 | dns_snd *send = 0; |
908 | |
1224 | |
909 | for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin (); |
1225 | for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
910 | i != vpn->dns_sndpq.end (); |
1226 | i != vpn->dns_sndpq.end (); |
911 | ++i) |
1227 | ++i) |
912 | { |
1228 | { |
913 | dns_req *r = *i; |
1229 | dns_snd *r = *i; |
914 | |
1230 | |
915 | if (r->next <= NOW) |
1231 | if (r->timeout <= ev_now ()) |
916 | { |
1232 | { |
917 | if (!send) |
1233 | if (!send) |
918 | { |
1234 | { |
919 | send = r; |
1235 | send = r; |
920 | |
1236 | |
921 | if (r->retry)//D |
|
|
922 | printf ("req %d:%d, retry %d\n", r->seqno, r->pkt->id, r->retry); |
|
|
923 | r->retry++; |
1237 | r->retry++; |
924 | r->next = NOW + r->retry; |
1238 | r->timeout = ev_now () + (r->retry * min_latency * conf.dns_timeout_factor); |
|
|
1239 | //printf ("RETRY %x (%d, %f)\n", r->seqno, r->retry, r->timeout - ev_now ());//D |
|
|
1240 | |
|
|
1241 | // the following code changes the query section a bit, forcing |
|
|
1242 | // the forwarder to generate a new request |
|
|
1243 | if (r->stdhdr) |
|
|
1244 | encode_header ((char *)r->pkt->at (6 * 2 + 1), THISNODE->id, r->seqno, r->retry); |
925 | } |
1245 | } |
926 | } |
1246 | } |
927 | |
1247 | else |
928 | if (r->next < next) |
1248 | NEXT (r->timeout); |
929 | next = r->next; |
|
|
930 | } |
1249 | } |
931 | |
1250 | |
932 | if (!send |
1251 | if (!send) |
933 | && vpn->dns_sndpq.size () < MAX_OUTSTANDING) |
|
|
934 | { |
1252 | { |
|
|
1253 | // generate a new packet, if wise |
|
|
1254 | |
|
|
1255 | if (!established) |
|
|
1256 | { |
|
|
1257 | if (vpn->dns_sndpq.empty ()) |
|
|
1258 | { |
935 | send = new dns_req (this); |
1259 | send = new dns_snd (this); |
|
|
1260 | |
|
|
1261 | cfg.reset (THISNODE->id); |
|
|
1262 | send->gen_syn_req (); |
|
|
1263 | } |
|
|
1264 | } |
|
|
1265 | else if (vpn->dns_sndpq.size () < conf.dns_max_outstanding |
|
|
1266 | && !SEQNO_EQ (rcvseq, sndseq - (MAX_WINDOW - 1))) |
|
|
1267 | { |
|
|
1268 | if (last_sent + send_interval <= ev_now ()) |
|
|
1269 | { |
|
|
1270 | //printf ("sending data request etc.\n"); //D |
|
|
1271 | if (!snddq.empty () || last_received + 1. > ev_now ()) |
|
|
1272 | { |
|
|
1273 | poll_interval = send_interval; |
|
|
1274 | NEXT (ev_now () + send_interval); |
|
|
1275 | } |
|
|
1276 | |
|
|
1277 | send = new dns_snd (this); |
936 | send->gen_stream_req (sndseq, snddq); |
1278 | send->gen_stream_req (sndseq, snddq); |
|
|
1279 | send->timeout = ev_now () + min_latency * conf.dns_timeout_factor; |
|
|
1280 | //printf ("SEND %x (%f)\n", send->seqno, send->timeout - ev_now (), min_latency, conf.dns_timeout_factor);//D |
|
|
1281 | |
|
|
1282 | sndseq = (sndseq + 1) & SEQNO_MASK; |
|
|
1283 | } |
|
|
1284 | else |
|
|
1285 | NEXT (last_sent + send_interval); |
|
|
1286 | } |
|
|
1287 | |
|
|
1288 | if (send) |
937 | vpn->dns_sndpq.push_back (send); |
1289 | vpn->dns_sndpq.push_back (send); |
938 | |
|
|
939 | sndseq = (sndseq + 1) & SEQNO_MASK; |
|
|
940 | } |
1290 | } |
941 | |
|
|
942 | tstamp min_next = NOW + (1. / (tstamp)MAX_RATE); |
|
|
943 | |
1291 | |
944 | if (send) |
1292 | if (send) |
945 | { |
1293 | { |
946 | dns_packet *pkt = send->pkt; |
1294 | last_sent = ev_now (); |
947 | |
1295 | sendto (vpn->dnsv4_fd, |
948 | next = min_next; |
1296 | send->pkt->at (0), send->pkt->len, 0, |
949 | |
|
|
950 | sendto (vpn->dnsv4_fd, &((*pkt)[0]), pkt->len, 0, |
|
|
951 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
1297 | vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ()); |
952 | } |
1298 | } |
953 | else if (next < min_next) |
|
|
954 | next = min_next; |
|
|
955 | |
1299 | |
956 | w.start (next); |
1300 | slog (L_NOISE, "DNS: pi %f si %f N %f (%d:%d %d)", |
|
|
1301 | poll_interval, send_interval, next - ev_now (), |
|
|
1302 | vpn->dns_sndpq.size (), snddq.size (), |
|
|
1303 | rcvpq.size ()); |
|
|
1304 | |
|
|
1305 | // TODO: no idea when this happens, but when next < ev_now (), we have a problem |
|
|
1306 | // doesn't seem to happen anymore |
|
|
1307 | if (next < ev_now () + 0.001) |
|
|
1308 | next = ev_now () + 0.1; |
|
|
1309 | |
|
|
1310 | w.start (next - ev_now ()); |
957 | } |
1311 | } |
958 | |
1312 | |
959 | #endif |
1313 | #endif |
960 | |
1314 | |