ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn_dns.C
Revision: 1.10
Committed: Fri Mar 4 08:15:04 2005 UTC (19 years, 2 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.9: +454 -212 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*
2     vpn_dns.C -- handle the dns tunnel part of the protocol.
3 pcg 1.7 Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de>
4 pcg 1.1
5 pcg 1.7 This file is part of GVPE.
6    
7     GVPE is free software; you can redistribute it and/or modify
8 pcg 1.1 it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11    
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     GNU General Public License for more details.
16    
17     You should have received a copy of the GNU General Public License
18 pcg 1.7 along with gvpe; if not, write to the Free Software
19 pcg 1.1 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20     */
21    
22     #include "config.h"
23    
24     #if ENABLE_DNS
25    
26     // dns processing is EXTREMELY ugly. For obvious(?) reasons.
27     // it's a hack, use only in emergency situations please.
28    
29     #include <cstring>
30    
31     #include <sys/types.h>
32     #include <sys/socket.h>
33     #include <sys/wait.h>
34     #include <sys/uio.h>
35     #include <errno.h>
36     #include <time.h>
37     #include <unistd.h>
38     #include <fcntl.h>
39    
40     #include <map>
41    
42 pcg 1.8 #include <gmp.h>
43    
44 pcg 1.1 #include "netcompat.h"
45    
46     #include "vpn.h"
47    
48 pcg 1.10 #define MIN_POLL_INTERVAL .2 // how often to poll minimally when the server is having data
49     #define MAX_POLL_INTERVAL 6. // how often to poll minimally when the server has no data
50     #define ACTIVITY_INTERVAL 5.
51    
52     #define INITIAL_TIMEOUT 1.
53     #define INITIAL_SYN_TIMEOUT 2.
54    
55     #define MIN_SEND_INTERVAL (1./1000.)
56     #define MAX_SEND_INTERVAL 0.5 // optimistic?
57 pcg 1.1
58 pcg 1.8 #define MAX_OUTSTANDING 400 // max. outstanding requests
59     #define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING
60 pcg 1.9 #define MAX_RATE 100 // requests/s
61 pcg 1.10 #define MAX_BACKLOG (100*1024) // size of protocol backlog, must be > MAXSIZE
62 pcg 1.5
63     #define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well
64     // 240 leaves about 4 bytes of server reply data
65     // every two request byte sless give room for one reply byte
66    
67 pcg 1.9 #define SEQNO_MASK 0xffff
68 pcg 1.8 #define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) )
69 pcg 1.1
70 pcg 1.8 #define MAX_LBL_SIZE 63
71     #define MAX_PKT_SIZE 512
72 pcg 1.1
73 pcg 1.10 #define RR_TYPE_A 1
74     #define RR_TYPE_NULL 10
75     #define RR_TYPE_TXT 16
76 pcg 1.8 #define RR_TYPE_ANY 255
77 pcg 1.10
78     #define RR_CLASS_IN 1
79    
80     #define CMD_IP_1 207
81     #define CMD_IP_2 46
82     #define CMD_IP_3 236
83     #define CMD_IP_RST 29
84     #define CMD_IP_SYN 113
85     #define CMD_IP_REJ 32
86 pcg 1.1
87 pcg 1.8 // works for cmaps up to 255 (not 256!)
88     struct charmap
89     {
90     enum { INVALID = (u8)255 };
91 pcg 1.1
92 pcg 1.8 char encode [256]; // index => char
93     u8 decode [256]; // char => index
94     unsigned int size;
95 pcg 1.2
96 pcg 1.8 charmap (const char *cmap);
97     };
98    
99     charmap::charmap (const char *cmap)
100     {
101     char *enc = encode;
102     u8 *dec = decode;
103 pcg 1.2
104 pcg 1.8 memset (enc, (char) 0, 256);
105     memset (dec, (char)INVALID, 256);
106 pcg 1.1
107 pcg 1.8 for (size = 0; cmap [size]; size++)
108     {
109     enc [size] = cmap [size];
110     dec [(u8)enc [size]] = size;
111     }
112 pcg 1.1
113 pcg 1.8 assert (size < 256);
114     }
115 pcg 1.2
116 pcg 1.8 #define MAX_DEC_LEN 500
117     #define MAX_ENC_LEN (MAX_DEC_LEN * 2)
118     #define MAX_LIMBS ((MAX_DEC_LEN * 8 + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS)
119 pcg 1.2
120 pcg 1.8 // ugly. minimum base is 16(!)
121     struct basecoder
122 pcg 1.2 {
123 pcg 1.8 charmap cmap;
124     unsigned int enc_len [MAX_DEC_LEN];
125     unsigned int dec_len [MAX_ENC_LEN];
126 pcg 1.2
127 pcg 1.8 unsigned int encode_len (unsigned int len);
128     unsigned int decode_len (unsigned int len);
129    
130     unsigned int encode (char *dst, u8 *src, unsigned int len);
131     unsigned int decode (u8 *dst, char *src, unsigned int len);
132    
133     basecoder (const char *cmap);
134     };
135    
136     basecoder::basecoder (const char *cmap)
137     : cmap (cmap)
138     {
139     for (unsigned int len = 0; len < MAX_DEC_LEN; ++len)
140     {
141     u8 src [MAX_DEC_LEN];
142     u8 dst [MAX_ENC_LEN];
143 pcg 1.2
144 pcg 1.8 memset (src, 255, len);
145 pcg 1.2
146 pcg 1.8 mp_limb_t m [MAX_LIMBS];
147     mp_size_t n;
148    
149     n = mpn_set_str (m, src, len, 256);
150     n = mpn_get_str (dst, this->cmap.size, m, n);
151    
152     for (int i = 0; !dst [i]; ++i)
153     n--;
154    
155     enc_len [len] = n;
156     dec_len [n] = len;
157     }
158     }
159    
160     unsigned int basecoder::encode_len (unsigned int len)
161     {
162     return enc_len [len];
163     }
164 pcg 1.2
165 pcg 1.8 unsigned int basecoder::decode_len (unsigned int len)
166 pcg 1.2 {
167 pcg 1.8 while (len && !dec_len [len])
168     --len;
169    
170     return dec_len [len];
171 pcg 1.2 }
172    
173 pcg 1.8 unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len)
174 pcg 1.2 {
175 pcg 1.10 if (!len || len > MAX_DEC_LEN)
176 pcg 1.8 return 0;
177    
178     int elen = encode_len (len);
179    
180     mp_limb_t m [MAX_LIMBS];
181     mp_size_t n;
182    
183     u8 dst_ [MAX_ENC_LEN];
184    
185     n = mpn_set_str (m, src, len, 256);
186     n = mpn_get_str (dst_, cmap.size, m, n);
187    
188     int plen = elen; // for padding
189 pcg 1.2
190 pcg 1.8 while (n < plen)
191 pcg 1.2 {
192 pcg 1.8 *dst++ = cmap.encode [0];
193     plen--;
194 pcg 1.2 }
195    
196 pcg 1.8 for (unsigned int i = n - plen; i < n; ++i)
197     *dst++ = cmap.encode [dst_ [i]];
198 pcg 1.4
199 pcg 1.8 return elen;
200 pcg 1.2 }
201    
202 pcg 1.8 unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len)
203 pcg 1.2 {
204 pcg 1.10 if (!len || len > MAX_ENC_LEN)
205 pcg 1.8 return 0;
206    
207     u8 src_ [MAX_ENC_LEN];
208     unsigned int elen = 0;
209 pcg 1.2
210     while (len--)
211     {
212 pcg 1.8 u8 val = cmap.decode [(u8)*src++];
213 pcg 1.2
214 pcg 1.8 if (val != charmap::INVALID)
215     src_ [elen++] = val;
216     }
217    
218     int dlen = decode_len (elen);
219    
220     mp_limb_t m [MAX_LIMBS];
221     mp_size_t n;
222    
223     u8 dst_ [MAX_DEC_LEN];
224 pcg 1.2
225 pcg 1.8 n = mpn_set_str (m, src_, elen, cmap.size);
226     n = mpn_get_str (dst_, 256, m, n);
227 pcg 1.2
228 pcg 1.8 if (n < dlen)
229     {
230     memset (dst, 0, dlen - n);
231     memcpy (dst + dlen - n, dst_, n);
232 pcg 1.2 }
233 pcg 1.8 else
234     memcpy (dst, dst_ + n - dlen, dlen);
235 pcg 1.4
236 pcg 1.8 return dlen;
237     }
238    
239     #if 0
240     struct test { test (); } test;
241    
242     test::test ()
243     {
244     basecoder cdc ("0123456789abcdefghijklmnopqrstuvwxyz");
245    
246     u8 in[] = "0123456789abcdefghijklmnopqrstuvwxyz";
247     static char enc[200];
248     static u8 dec[200];
249    
250     for (int i = 1; i < 20; i++)
251     {
252     int elen = cdc.encode (enc, in, i);
253     int dlen = cdc.decode (dec, enc, elen);
254    
255     printf ("%d>%d>%d (%s>%s)\n", i, elen, dlen, enc, dec);
256     }
257     abort ();
258     }
259     #endif
260    
261 pcg 1.10 //static basecoder cdc64 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI-");
262 pcg 1.8 //static basecoder cdc63 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI");
263     static basecoder cdc62 ("dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI");
264     //static basecoder cdc36 ("dphzr06qmjkb34tsvl81xaef92wgyo57ucni"); // unused as of yet
265     static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO");
266    
267     /////////////////////////////////////////////////////////////////////////////
268    
269     #define HDRSIZE 6
270    
271     inline void encode_header (char *data, int clientid, int seqno)
272     {
273     u8 hdr[3] = { clientid, seqno >> 8, seqno };
274    
275     assert (clientid < 256);
276    
277     cdc26.encode (data, hdr, 3);
278     }
279    
280     inline void decode_header (char *data, int &clientid, int &seqno)
281     {
282     u8 hdr[3];
283    
284     cdc26.decode (hdr, data, HDRSIZE);
285    
286     clientid = hdr[0];
287     seqno = (hdr[1] << 8) | hdr[2];
288 pcg 1.4 }
289    
290     /////////////////////////////////////////////////////////////////////////////
291    
292     struct byte_stream
293     {
294     u8 *data;
295     int maxsize;
296     int fill;
297    
298     byte_stream (int maxsize);
299     ~byte_stream ();
300    
301     bool empty () { return !fill; }
302     int size () { return fill; }
303    
304 pcg 1.5 bool put (u8 *data, unsigned int datalen);
305 pcg 1.4 bool put (vpn_packet *pkt);
306     vpn_packet *get ();
307    
308     u8 *begin () { return data; }
309     void remove (int count);
310     };
311    
312     byte_stream::byte_stream (int maxsize)
313     : maxsize (maxsize), fill (0)
314     {
315     data = new u8 [maxsize];
316     }
317    
318     byte_stream::~byte_stream ()
319     {
320     delete data;
321     }
322    
323     void byte_stream::remove (int count)
324     {
325     if (count > fill)
326     abort ();
327    
328     memmove (data, data + count, fill -= count);
329     }
330    
331 pcg 1.5 bool byte_stream::put (u8 *data, unsigned int datalen)
332     {
333     if (maxsize - fill < datalen)
334     return false;
335    
336     memcpy (this->data + fill, data, datalen); fill += datalen;
337    
338     return true;
339     }
340    
341 pcg 1.4 bool byte_stream::put (vpn_packet *pkt)
342     {
343     if (maxsize - fill < pkt->len + 2)
344     return false;
345    
346     data [fill++] = pkt->len >> 8;
347     data [fill++] = pkt->len;
348    
349 pcg 1.10 memcpy (data + fill, pkt->at (0), pkt->len); fill += pkt->len;
350 pcg 1.4
351     return true;
352     }
353    
354     vpn_packet *byte_stream::get ()
355     {
356     int len = (data [0] << 8) | data [1];
357    
358 pcg 1.5 if (len > MAXSIZE && fill >= 2)
359     abort (); // TODO handle this gracefully, connection reset
360    
361 pcg 1.4 if (fill < len + 2)
362     return 0;
363    
364     vpn_packet *pkt = new vpn_packet;
365 pcg 1.5
366     pkt->len = len;
367 pcg 1.10 memcpy (pkt->at (0), data + 2, len);
368 pcg 1.4 remove (len + 2);
369    
370     return pkt;
371 pcg 1.2 }
372    
373 pcg 1.3 /////////////////////////////////////////////////////////////////////////////
374    
375 pcg 1.2 #define FLAG_QUERY ( 0 << 15)
376     #define FLAG_RESPONSE ( 1 << 15)
377 pcg 1.9 #define FLAG_OP_MASK (15 << 11)
378 pcg 1.2 #define FLAG_OP_QUERY ( 0 << 11)
379     #define FLAG_AA ( 1 << 10)
380     #define FLAG_TC ( 1 << 9)
381     #define FLAG_RD ( 1 << 8)
382     #define FLAG_RA ( 1 << 7)
383 pcg 1.5 #define FLAG_AUTH ( 1 << 5)
384 pcg 1.2 #define FLAG_RCODE_MASK (15 << 0)
385     #define FLAG_RCODE_OK ( 0 << 0)
386     #define FLAG_RCODE_FORMERR ( 1 << 0)
387     #define FLAG_RCODE_SERVFAIL ( 2 << 0)
388     #define FLAG_RCODE_NXDOMAIN ( 3 << 0)
389     #define FLAG_RCODE_REFUSED ( 5 << 0)
390    
391     #define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD)
392 pcg 1.5 #define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA)
393 pcg 1.2
394 pcg 1.10 struct dns_cfg
395     {
396     static int next_uid;
397    
398     u8 id1, id2, id3, id4;
399     u8 version;
400     u8 rrtype;
401     u8 flags;
402     u8 def_ttl;
403     u8 rcv_cdc;
404     u8 snd_cdc;
405     u16 max_size;
406     u16 client;
407     u16 uid; // to make request unique
408    
409     u8 reserved[8];
410    
411     void reset (int clientid);
412     bool valid ();
413     };
414    
415     int dns_cfg::next_uid;
416    
417     void dns_cfg::reset (int clientid)
418     {
419     id1 = 'G';
420     id2 = 'V';
421     id3 = 'P';
422     id4 = 'E';
423    
424     version = 1;
425    
426     rrtype = RR_TYPE_TXT;
427     flags = 0;
428     def_ttl = 0;
429     rcv_cdc = 0;
430     snd_cdc = 62;
431     max_size = ntohs (MAX_PKT_SIZE);
432     client = ntohs (clientid);
433     uid = next_uid++;
434    
435     memset (reserved, 0, 8);
436     }
437    
438     bool dns_cfg::valid ()
439     {
440     return id1 == 'G'
441     && id2 == 'V'
442     && id3 == 'P'
443     && id4 == 'E'
444     && version == 1
445     && flags == 0
446     && rcv_cdc == 0
447     && snd_cdc == 62
448     && max_size == ntohs (MAX_PKT_SIZE);
449     }
450    
451 pcg 1.2 struct dns_packet : net_packet
452     {
453     u16 id;
454     u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4
455     u16 qdcount, ancount, nscount, arcount;
456    
457     u8 data[MAXSIZE - 6 * 2];
458    
459     int decode_label (char *data, int size, int &offs);
460     };
461    
462     int dns_packet::decode_label (char *data, int size, int &offs)
463     {
464     char *orig = data;
465    
466     memset (data, 0, size);
467    
468     while (offs < size - 1)
469     {
470     u8 len = (*this)[offs++];
471    
472     if (!len)
473     break;
474     else if (len < 64)
475     {
476     if (size < len + 1 || offs + len >= MAXSIZE - 1)
477     break;
478    
479     memcpy (data, &((*this)[offs]), len);
480    
481     data += len; size -= len; offs += len;
482     *data++ = '.'; size--;
483     }
484     else
485     {
486     int offs2 = ((len & 63) << 8) + (*this)[offs++];
487    
488     data += decode_label (data, size, offs2);
489     break;
490     }
491     }
492    
493     return data - orig;
494     }
495    
496 pcg 1.3 /////////////////////////////////////////////////////////////////////////////
497    
498 pcg 1.10 struct dns_snd
499 pcg 1.3 {
500     dns_packet *pkt;
501 pcg 1.10 tstamp timeout, sent;
502 pcg 1.3 int retry;
503 pcg 1.9 struct dns_connection *dns;
504 pcg 1.5 int seqno;
505 pcg 1.3
506 pcg 1.10 dns_snd (dns_connection *dns);
507 pcg 1.9 void gen_stream_req (int seqno, byte_stream &stream);
508 pcg 1.10 void gen_syn_req (const dns_cfg &cfg);
509 pcg 1.3 };
510    
511 pcg 1.5 static u16 dns_id = 12098; // TODO: should be per-vpn
512    
513     static u16 next_id ()
514 pcg 1.3 {
515 pcg 1.5 // the simplest lsfr with periodicity 65535 i could find
516     dns_id = (dns_id << 1)
517     | (((dns_id >> 1)
518     ^ (dns_id >> 2)
519     ^ (dns_id >> 4)
520     ^ (dns_id >> 15)) & 1);
521 pcg 1.3
522 pcg 1.5 return dns_id;
523     }
524 pcg 1.3
525 pcg 1.10 dns_snd::dns_snd (dns_connection *dns)
526 pcg 1.9 : dns (dns)
527 pcg 1.3 {
528 pcg 1.10 timeout = 0;
529 pcg 1.3 retry = 0;
530 pcg 1.10 seqno = 0;
531 pcg 1.3
532     pkt = new dns_packet;
533    
534 pcg 1.5 pkt->id = next_id ();
535     }
536    
537 pcg 1.10 static void append_domain (dns_packet &pkt, int &offs, const char *domain)
538     {
539     // add tunnel domain
540     for (;;)
541     {
542     const char *end = strchr (domain, '.');
543    
544     if (!end)
545     end = domain + strlen (domain);
546    
547     int len = end - domain;
548    
549     pkt [offs++] = len;
550     memcpy (pkt.at (offs), domain, len);
551     offs += len;
552    
553     if (!*end)
554     break;
555    
556     domain = end + 1;
557     }
558     }
559    
560     void dns_snd::gen_stream_req (int seqno, byte_stream &stream)
561 pcg 1.5 {
562     this->seqno = seqno;
563    
564 pcg 1.10 timeout = NOW + INITIAL_TIMEOUT;
565    
566 pcg 1.5 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
567 pcg 1.4 pkt->qdcount = htons (1);
568 pcg 1.3
569 pcg 1.4 int offs = 6*2;
570 pcg 1.8 int dlen = MAX_DOMAIN_SIZE - (strlen (THISNODE->domain) + 2);
571 pcg 1.5 // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well,
572     // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra
573    
574 pcg 1.8 char enc[256], *encp = enc;
575     encode_header (enc, THISNODE->id, seqno);
576 pcg 1.4
577 pcg 1.8 int datalen = cdc62.decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE - HDRSIZE);
578 pcg 1.4
579 pcg 1.9 if (datalen > stream.size ())
580     datalen = stream.size ();
581 pcg 1.8
582 pcg 1.9 int enclen = cdc62.encode (enc + HDRSIZE, stream.begin (), datalen) + HDRSIZE;
583     stream.remove (datalen);
584 pcg 1.4
585 pcg 1.5 while (enclen)
586     {
587     int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE;
588    
589     (*pkt)[offs++] = lbllen;
590     memcpy (pkt->at (offs), encp, lbllen);
591 pcg 1.4
592 pcg 1.5 offs += lbllen;
593     encp += lbllen;
594 pcg 1.4
595 pcg 1.5 enclen -= lbllen;
596 pcg 1.4 }
597    
598 pcg 1.10 append_domain (*pkt, offs, THISNODE->domain);
599 pcg 1.3
600 pcg 1.10 (*pkt)[offs++] = 0;
601     (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY;
602     (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
603    
604     pkt->len = offs;
605     }
606    
607     void dns_snd::gen_syn_req (const dns_cfg &cfg)
608     {
609     timeout = NOW + INITIAL_SYN_TIMEOUT;
610 pcg 1.4
611 pcg 1.10 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
612     pkt->qdcount = htons (1);
613 pcg 1.4
614 pcg 1.10 int offs = 6*2;
615 pcg 1.4
616 pcg 1.10 int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&cfg, sizeof (dns_cfg));
617 pcg 1.4
618 pcg 1.10 assert (elen <= MAX_LBL_SIZE);
619 pcg 1.4
620 pcg 1.10 (*pkt)[offs] = elen;
621     offs += elen + 1;
622     append_domain (*pkt, offs, THISNODE->domain);
623 pcg 1.4
624     (*pkt)[offs++] = 0;
625 pcg 1.10 (*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A;
626 pcg 1.4 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
627    
628     pkt->len = offs;
629 pcg 1.5 }
630    
631     struct dns_rcv
632     {
633     int seqno;
634     dns_packet *pkt; // reply packet
635     u8 data [MAXSIZE]; // actually part of the reply packet...
636     int datalen;
637    
638 pcg 1.6 dns_rcv (int seqno, u8 *data, int datalen);
639 pcg 1.5 ~dns_rcv ();
640     };
641    
642 pcg 1.6 dns_rcv::dns_rcv (int seqno, u8 *data, int datalen)
643 pcg 1.5 : seqno (seqno), pkt (new dns_packet), datalen (datalen)
644     {
645     memcpy (this->data, data, datalen);
646     }
647 pcg 1.4
648 pcg 1.5 dns_rcv::~dns_rcv ()
649     {
650     delete pkt;
651 pcg 1.3 }
652    
653     /////////////////////////////////////////////////////////////////////////////
654 pcg 1.9
655     struct dns_connection
656     {
657     connection *c;
658     struct vpn *vpn;
659    
660 pcg 1.10 dns_cfg cfg;
661    
662     bool established;
663    
664     tstamp last_received;
665     tstamp last_sent;
666     double poll_interval, send_interval;
667    
668 pcg 1.9 vector<dns_rcv *> rcvpq;
669    
670 pcg 1.10 byte_stream rcvdq; int rcvseq;
671     byte_stream snddq; int sndseq;
672 pcg 1.9
673     void time_cb (time_watcher &w); time_watcher tw;
674     void receive_rep (dns_rcv *r);
675    
676     dns_connection (connection *c);
677     ~dns_connection ();
678     };
679    
680     dns_connection::dns_connection (connection *c)
681     : c (c)
682     , rcvdq (MAX_BACKLOG * 2)
683     , snddq (MAX_BACKLOG * 2)
684     , tw (this, &dns_connection::time_cb)
685     {
686     vpn = c->vpn;
687    
688 pcg 1.10 established = false;
689    
690 pcg 1.9 rcvseq = sndseq = 0;
691 pcg 1.10
692     last_sent = last_received = 0;
693     poll_interval = MIN_POLL_INTERVAL;
694     send_interval = 0.2; // starting rate
695 pcg 1.9 }
696    
697     dns_connection::~dns_connection ()
698     {
699     for (vector<dns_rcv *>::iterator i = rcvpq.begin ();
700     i != rcvpq.end ();
701     ++i)
702     delete *i;
703     }
704 pcg 1.3
705 pcg 1.10 void dns_connection::receive_rep (dns_rcv *r)
706 pcg 1.2 {
707 pcg 1.10 if (r->datalen)
708     {
709     last_received = NOW;
710     tw.trigger ();
711    
712     poll_interval *= 0.99;
713     if (poll_interval > MIN_POLL_INTERVAL)
714     poll_interval = MIN_POLL_INTERVAL;
715     }
716     else
717     {
718     poll_interval *= 1.1;
719     if (poll_interval > MAX_POLL_INTERVAL)
720     poll_interval = MAX_POLL_INTERVAL;
721     }
722 pcg 1.2
723 pcg 1.9 rcvpq.push_back (r);
724 pcg 1.5
725     redo:
726    
727 pcg 1.8 // find next packet
728 pcg 1.9 for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); )
729     if (SEQNO_EQ (rcvseq, (*i)->seqno))
730 pcg 1.5 {
731 pcg 1.8 // enter the packet into our input stream
732     r = *i;
733    
734     // remove the oldest packet, look forward, as it's oldest first
735 pcg 1.9 for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j)
736     if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW))
737 pcg 1.8 {
738     delete *j;
739 pcg 1.9 rcvpq.erase (j);
740 pcg 1.8 break;
741     }
742 pcg 1.5
743 pcg 1.9 rcvseq = (rcvseq + 1) & SEQNO_MASK;
744 pcg 1.5
745 pcg 1.9 if (!rcvdq.put (r->data, r->datalen))
746 pcg 1.5 abort (); // MUST never overflow, can be caused by data corruption, TODO
747    
748 pcg 1.9 while (vpn_packet *pkt = rcvdq.get ())
749 pcg 1.5 {
750     sockinfo si;
751     si.host = 0; si.port = 0; si.prot = PROT_DNSv4;
752    
753     vpn->recv_vpn_packet (pkt, si);
754     }
755 pcg 1.8
756     // check for further packets
757 pcg 1.5 goto redo;
758     }
759     }
760    
761 pcg 1.10 void
762     vpn::dnsv4_server (dns_packet &pkt)
763 pcg 1.2 {
764 pcg 1.10 u16 flags = ntohs (pkt.flags);
765 pcg 1.2
766 pcg 1.4 int offs = 6 * 2; // skip header
767 pcg 1.2
768 pcg 1.10 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
769 pcg 1.2
770 pcg 1.10 if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK))
771     && pkt.qdcount == htons (1))
772 pcg 1.4 {
773     char qname[MAXSIZE];
774 pcg 1.10 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
775 pcg 1.4
776 pcg 1.10 u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++];
777     u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++];
778 pcg 1.2
779 pcg 1.10 pkt.qdcount = htons (1);
780     pkt.ancount = 0;
781     pkt.nscount = 0; // should be self, as other nameservers reply like this
782     pkt.arcount = 0; // a record for self, as other nameservers reply like this
783 pcg 1.2
784 pcg 1.10 pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN);
785 pcg 1.2
786 pcg 1.4 int dlen = strlen (THISNODE->domain);
787 pcg 1.2
788 pcg 1.4 if (qclass == RR_CLASS_IN
789 pcg 1.10 && qlen > dlen + 1
790 pcg 1.4 && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen))
791 pcg 1.2 {
792 pcg 1.10 // now generate reply
793     pkt.ancount = htons (1); // one answer RR
794     pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK);
795    
796     if ((qtype == RR_TYPE_ANY
797     || qtype == RR_TYPE_TXT
798     || qtype == RR_TYPE_NULL)
799     && qlen > dlen + 1 + HDRSIZE)
800 pcg 1.5 {
801 pcg 1.10 // correct class, domain: parse
802     int client, seqno;
803     decode_header (qname, client, seqno);
804 pcg 1.5
805 pcg 1.10 u8 data[MAXSIZE];
806     int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE));
807 pcg 1.9
808 pcg 1.10 if (0 < client && client <= conns.size ())
809     {
810     connection *c = conns [client - 1];
811     dns_connection *dns = c->dns;
812     dns_rcv *rcv;
813    
814     if (dns)
815     {
816     for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); )
817     if (SEQNO_EQ ((*i)->seqno, seqno))
818     {
819     // already seen that request: simply reply with the cached reply
820     dns_rcv *r = *i;
821    
822     printf ("DUPLICATE %d\n", htons (r->pkt->id));//D
823    
824     memcpy (pkt.at (0), r->pkt->at (0), offs = r->pkt->len);
825     pkt.id = r->pkt->id;
826     goto duplicate_request;
827     }
828    
829     // new packet, queue
830     rcv = new dns_rcv (seqno, data, datalen);
831     dns->receive_rep (rcv);
832     }
833    
834     pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section
835    
836     // type
837     int rtype = dns ? dns->cfg.rrtype : RR_TYPE_A;
838     pkt [offs++] = rtype >> 8; pkt [offs++] = rtype;
839    
840     // class
841     pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN;
842    
843     // TTL
844     pkt [offs++] = 0; pkt [offs++] = 0;
845     pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0;
846    
847     int rdlen_offs = offs += 2;
848    
849     int dlen = (dns ? ntohs (dns->cfg.max_size) : MAX_PKT_SIZE) - offs;
850     // bind doesn't compress well, so reduce further by one label length
851     dlen -= qlen;
852    
853     if (dns)
854     {
855     while (dlen > 1 && !dns->snddq.empty ())
856     {
857     int txtlen = dlen <= 255 ? dlen - 1 : 255;
858    
859     if (txtlen > dns->snddq.size ())
860     txtlen = dns->snddq.size ();
861    
862     pkt[offs++] = txtlen;
863     memcpy (pkt.at (offs), dns->snddq.begin (), txtlen);
864     offs += txtlen;
865     dns->snddq.remove (txtlen);
866    
867     dlen -= txtlen + 1;
868     }
869    
870     // avoid empty TXT rdata
871     if (offs == rdlen_offs)
872     pkt[offs++] = 0;
873     }
874     else
875     {
876     // send RST
877     pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
878     pkt [offs++] = CMD_IP_RST;
879     }
880    
881     int rdlen = offs - rdlen_offs;
882    
883     pkt [rdlen_offs - 2] = rdlen >> 8;
884     pkt [rdlen_offs - 1] = rdlen;
885    
886     if (dns)
887     {
888     // now update dns_rcv copy
889     rcv->pkt->len = offs;
890     memcpy (rcv->pkt->at (0), pkt.at (0), offs);
891     }
892 pcg 1.9
893 pcg 1.10 duplicate_request: ;
894     }
895     else
896     pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
897     }
898     else if (qtype == RR_TYPE_A
899     && qlen > dlen + 1 + cdc26.encode_len (sizeof (dns_cfg)))
900     {
901     dns_cfg cfg;
902     cdc26.decode ((u8 *)&cfg, qname, cdc26.encode_len (sizeof (dns_cfg)));
903     int client = ntohs (cfg.client);
904 pcg 1.4
905 pcg 1.10 pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section
906 pcg 1.2
907 pcg 1.10 pkt [offs++] = RR_TYPE_A >> 8; pkt [offs++] = RR_TYPE_A; // type
908     pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class
909     pkt [offs++] = 0; pkt [offs++] = 0;
910     pkt [offs++] = 0; pkt [offs++] = cfg.def_ttl; // TTL
911     pkt [offs++] = 0; pkt [offs++] = 4; // rdlength
912 pcg 1.2
913 pcg 1.10 slog (L_INFO, _("DNS tunnel: client %d tries to connect"), client);
914 pcg 1.2
915 pcg 1.10 pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3;
916     pkt [offs++] = CMD_IP_REJ;
917 pcg 1.2
918 pcg 1.10 if (0 < client && client <= conns.size ())
919 pcg 1.5 {
920 pcg 1.10 connection *c = conns [client - 1];
921 pcg 1.2
922 pcg 1.10 if (cfg.valid ())
923     {
924     pkt [offs - 1] = CMD_IP_SYN;
925    
926     delete c->dns;
927     c->dns = new dns_connection (c);
928     c->dns->cfg = cfg;
929     }
930 pcg 1.5 }
931     }
932 pcg 1.2 }
933 pcg 1.6
934 pcg 1.10 pkt.len = offs;
935 pcg 1.4 }
936     }
937    
938     void
939 pcg 1.10 vpn::dnsv4_client (dns_packet &pkt)
940 pcg 1.4 {
941 pcg 1.10 u16 flags = ntohs (pkt.flags);
942 pcg 1.4 int offs = 6 * 2; // skip header
943    
944 pcg 1.10 pkt.qdcount = ntohs (pkt.qdcount);
945     pkt.ancount = ntohs (pkt.ancount);
946 pcg 1.4
947 pcg 1.5 // go through our request list and find the corresponding request
948 pcg 1.10 for (vector<dns_snd *>::iterator i = dns_sndpq.begin ();
949 pcg 1.4 i != dns_sndpq.end ();
950     ++i)
951 pcg 1.10 if ((*i)->pkt->id == pkt.id)
952 pcg 1.4 {
953 pcg 1.9 dns_connection *dns = (*i)->dns;
954 pcg 1.5 int seqno = (*i)->seqno;
955     u8 data[MAXSIZE], *datap = data;
956    
957 pcg 1.10 if ((*i)->retry)
958     {
959     dns->send_interval *= 1.01;
960     if (dns->send_interval < MAX_SEND_INTERVAL)
961     dns->send_interval = MAX_SEND_INTERVAL;
962     }
963     else
964     {
965     dns->send_interval *= 0.99;
966     if (dns->send_interval < MIN_SEND_INTERVAL)
967     dns->send_interval = MIN_SEND_INTERVAL;
968    
969     // the latency surely puts an upper bound on
970     // the minimum send interval
971     if (dns->send_interval > NOW - (*i)->sent)
972     dns->send_interval = NOW - (*i)->sent;
973     }
974    
975 pcg 1.4 delete *i;
976     dns_sndpq.erase (i);
977    
978 pcg 1.10 if (flags & FLAG_RESPONSE && !(flags & FLAG_OP_MASK))
979 pcg 1.4 {
980     char qname[MAXSIZE];
981    
982 pcg 1.10 while (pkt.qdcount-- && offs < MAXSIZE - 4)
983 pcg 1.4 {
984 pcg 1.10 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
985 pcg 1.4 offs += 4; // skip qtype, qclass
986     }
987    
988 pcg 1.10 while (pkt.ancount-- && offs < MAXSIZE - 10 && datap)
989 pcg 1.4 {
990 pcg 1.10 int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs);
991    
992     u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++];
993     u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++];
994     u32 ttl = pkt [offs++] << 24;
995     ttl |= pkt [offs++] << 16;
996     ttl |= pkt [offs++] << 8;
997     ttl |= pkt [offs++];
998     u16 rdlen = pkt [offs++] << 8; rdlen |= pkt [offs++];
999 pcg 1.8
1000 pcg 1.10 if (qtype == RR_TYPE_NULL || qtype == RR_TYPE_TXT)
1001     {
1002     if (rdlen <= MAXSIZE - offs)
1003     {
1004     // decode bytes, finally
1005    
1006     while (rdlen)
1007     {
1008     int txtlen = pkt [offs++];
1009 pcg 1.4
1010 pcg 1.10 assert (txtlen + offs < MAXSIZE - 1);
1011 pcg 1.4
1012 pcg 1.10 memcpy (datap, pkt.at (offs), txtlen);
1013     datap += txtlen; offs += txtlen;
1014    
1015     rdlen -= txtlen + 1;
1016     }
1017     }
1018     }
1019     else if (qtype == RR_TYPE_A)
1020 pcg 1.5 {
1021 pcg 1.10 u8 ip [4];
1022 pcg 1.5
1023 pcg 1.10 ip [0] = pkt [offs++];
1024     ip [1] = pkt [offs++];
1025     ip [2] = pkt [offs++];
1026     ip [3] = pkt [offs++];
1027    
1028     if (ip [0] == CMD_IP_1
1029     && ip [1] == CMD_IP_2
1030     && ip [2] == CMD_IP_3)
1031 pcg 1.5 {
1032 pcg 1.10 slog (L_TRACE, _("got tunnel meta command %02x"), ip [3]);
1033 pcg 1.5
1034 pcg 1.10 if (ip [3] == CMD_IP_RST)
1035     {
1036     slog (L_DEBUG, _("got tunnel RST request"));
1037    
1038     connection *c = dns->c;
1039     delete c->dns; c->dns = 0;
1040    
1041     return;
1042     }
1043     else if (ip [3] == CMD_IP_SYN)
1044     dns->established = true;
1045     else
1046     slog (L_INFO, _("got unknown meta command %02x"), ip [3]);
1047     }
1048     else
1049     slog (L_INFO, _("got spurious a record %d.%d.%d.%d"),
1050     ip [0], ip [1], ip [2], ip [3]);
1051 pcg 1.5
1052 pcg 1.10 return;
1053 pcg 1.9 }
1054 pcg 1.4
1055 pcg 1.9 int client, rseqno;
1056     decode_header (qname, client, rseqno);
1057    
1058     if (client != THISNODE->id)
1059     {
1060     slog (L_INFO, _("got dns tunnel response with wrong clientid, ignoring"));
1061     datap = 0;
1062     }
1063     else if (rseqno != seqno)
1064     {
1065     slog (L_DEBUG, _("got dns tunnel response with wrong seqno, badly caching nameserver?"));
1066     datap = 0;
1067 pcg 1.4 }
1068     }
1069     }
1070    
1071 pcg 1.6 // todo: pkt now used
1072 pcg 1.9 if (datap)
1073     dns->receive_rep (new dns_rcv (seqno, data, datap - data));
1074 pcg 1.5
1075 pcg 1.4 break;
1076     }
1077     }
1078    
1079     void
1080     vpn::dnsv4_ev (io_watcher &w, short revents)
1081     {
1082     if (revents & EVENT_READ)
1083     {
1084     dns_packet *pkt = new dns_packet;
1085     struct sockaddr_in sa;
1086     socklen_t sa_len = sizeof (sa);
1087    
1088 pcg 1.10 pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
1089 pcg 1.4
1090     if (pkt->len > 0)
1091 pcg 1.5 {
1092     if (THISNODE->dns_port)
1093     {
1094 pcg 1.10 dnsv4_server (*pkt);
1095     sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len);
1096 pcg 1.5 }
1097     else
1098 pcg 1.10 dnsv4_client (*pkt);
1099    
1100     delete pkt;
1101 pcg 1.5 }
1102 pcg 1.1 }
1103     }
1104    
1105     bool
1106 pcg 1.3 connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
1107     {
1108 pcg 1.9 if (!dns)
1109     dns = new dns_connection (this);
1110 pcg 1.4
1111 pcg 1.9 if (!dns->snddq.put (pkt))
1112 pcg 1.3 return false;
1113    
1114 pcg 1.10 dns->tw.trigger ();
1115 pcg 1.3
1116     return true;
1117     }
1118    
1119 pcg 1.10 #define NEXT(w) do { if (next > (w)) next = w; } while (0)
1120    
1121 pcg 1.3 void
1122 pcg 1.9 dns_connection::time_cb (time_watcher &w)
1123 pcg 1.1 {
1124 pcg 1.10 // servers have to be polled
1125     if (THISNODE->dns_port)
1126     return;
1127    
1128 pcg 1.3 // check for timeouts and (re)transmit
1129 pcg 1.10 tstamp next = NOW + poll_interval;
1130     dns_snd *send = 0;
1131 pcg 1.3
1132 pcg 1.10 for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin ();
1133 pcg 1.4 i != vpn->dns_sndpq.end ();
1134 pcg 1.3 ++i)
1135     {
1136 pcg 1.10 dns_snd *r = *i;
1137 pcg 1.3
1138 pcg 1.10 if (r->timeout <= NOW)
1139 pcg 1.3 {
1140 pcg 1.4 if (!send)
1141     {
1142     send = r;
1143    
1144     r->retry++;
1145 pcg 1.10 r->timeout = NOW + r->retry;
1146 pcg 1.4 }
1147 pcg 1.3 }
1148 pcg 1.10 else if (r->timeout < next)
1149     NEXT (r->timeout);
1150 pcg 1.3 }
1151    
1152 pcg 1.10 if (last_sent + send_interval <= NOW)
1153 pcg 1.5 {
1154 pcg 1.10 if (!send)
1155     {
1156     // generate a new packet, if wise
1157    
1158     if (!established)
1159     {
1160     if (vpn->dns_sndpq.empty ())
1161     {
1162     send = new dns_snd (this);
1163    
1164     cfg.reset (THISNODE->id);
1165     send->gen_syn_req (cfg);
1166     }
1167     }
1168     else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING)
1169     {
1170     send = new dns_snd (this);
1171     send->gen_stream_req (sndseq, snddq);
1172 pcg 1.5
1173 pcg 1.10 sndseq = (sndseq + 1) & SEQNO_MASK;
1174     }
1175 pcg 1.4
1176 pcg 1.10 if (send)
1177     vpn->dns_sndpq.push_back (send);
1178     }
1179 pcg 1.4
1180 pcg 1.10 if (send)
1181     {
1182     printf ("send pkt\n");
1183     last_sent = NOW;
1184 pcg 1.4
1185 pcg 1.10 if (!send->retry)
1186     send->sent = NOW;
1187 pcg 1.4
1188 pcg 1.10 sendto (vpn->dnsv4_fd,
1189     send->pkt->at (0), send->pkt->len, 0,
1190     vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ());
1191     }
1192 pcg 1.4 }
1193 pcg 1.10 else
1194     NEXT (last_sent + send_interval);
1195    
1196     printf ("pi %f si %f N %f (%d:%d)\n", poll_interval, send_interval, next - NOW, vpn->dns_sndpq.size (), snddq.size ());
1197 pcg 1.4
1198 pcg 1.3 w.start (next);
1199 pcg 1.1 }
1200    
1201     #endif
1202