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