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