ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/vpn_dns.C
(Generate patch)

Comparing gvpe/src/vpn_dns.C (file contents):
Revision 1.4 by pcg, Wed Mar 2 05:49:31 2005 UTC vs.
Revision 1.9 by pcg, Fri Mar 4 04:52:38 2005 UTC

1/* 1/*
2 vpn_dns.C -- handle the dns tunnel part of the protocol. 2 vpn_dns.C -- handle the dns tunnel part of the protocol.
3 Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com> 3 Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de>
4 4
5 This file is part of GVPE.
6
5 This program is free software; you can redistribute it and/or modify 7 GVPE is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 8 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or 9 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 10 (at your option) any later version.
9 11
10 This program is distributed in the hope that it will be useful, 12 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details. 15 GNU General Public License for more details.
14 16
15 You should have received a copy of the GNU General Public License 17 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software 18 along with gvpe; if not, write to the Free Software
17 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/ 20*/
19 21
20#include "config.h" 22#include "config.h"
21 23
35#include <unistd.h> 37#include <unistd.h>
36#include <fcntl.h> 38#include <fcntl.h>
37 39
38#include <map> 40#include <map>
39 41
42#include <gmp.h>
43
40#include "netcompat.h" 44#include "netcompat.h"
41 45
42#include "vpn.h" 46#include "vpn.h"
43 47
44#define MIN_RETRY 1. 48#define MIN_RETRY 1.
45#define MAX_RETRY 60. 49#define MAX_RETRY 6.
46 50
47#define MAX_OUTSTANDING 10 // max. outstanding requests 51#define MAX_OUTSTANDING 400 // max. outstanding requests
48#define MAX_WINDOW 100 // max. for MAX_OUTSTANDING 52#define MAX_WINDOW 1000 // max. for MAX_OUTSTANDING
49#define MAX_RATE 1 // requests/s 53#define MAX_RATE 100 // requests/s
50#define MAX_BACKLOG (10*1024) // size of protocol backlog 54#define MAX_BACKLOG (10*1024) // size of protocol backlog, must be > MAXSIZE
51 55
52/* 56#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
58// every two request byte sless give room for one reply byte
53 59
54protocol, in shorthand :) 60#define SEQNO_MASK 0xffff
55 61#define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) )
56client -> server <req> ANY?
57server -> client <req> TXT <rep>
58
59<req> is dns64-encoded <client-id:12><recv-seqno:10>[<send-seqno:10><data>]
60<rep> is dns64-encoded <0:12><recv-seqno:10>[<send-seqno:10><data>]
61
62if <client-id> is zero, the connection will be configured:
63
64<0:12><0:4>client-id:12><default-ttl:8><max-size:16><flags:16>
65
66*/
67 62
68#define MAX_LBL_SIZE 63 63#define MAX_LBL_SIZE 63
69#define MAX_PKT_SIZE 512 64#define MAX_PKT_SIZE 512
70 65
71#define RR_TYPE_TXT 16 66#define RR_TYPE_TXT 16
72#define RR_TYPE_ANY 255 67#define RR_TYPE_ANY 255
73#define RR_CLASS_IN 1 68#define RR_CLASS_IN 1
74 69
70// works for cmaps up to 255 (not 256!)
71struct charmap
72{
73 enum { INVALID = (u8)255 };
74
75 char encode [256]; // index => char
76 u8 decode [256]; // char => index
77 unsigned int size;
78
79 charmap (const char *cmap);
80};
81
82charmap::charmap (const char *cmap)
83{
84 char *enc = encode;
85 u8 *dec = decode;
86
87 memset (enc, (char) 0, 256);
88 memset (dec, (char)INVALID, 256);
89
90 for (size = 0; cmap [size]; size++)
91 {
92 enc [size] = cmap [size];
93 dec [(u8)enc [size]] = size;
94 }
95
96 assert (size < 256);
97}
98
99#define MAX_DEC_LEN 500
100#define MAX_ENC_LEN (MAX_DEC_LEN * 2)
101#define MAX_LIMBS ((MAX_DEC_LEN * 8 + GMP_NUMB_BITS - 1) / GMP_NUMB_BITS)
102
103// ugly. minimum base is 16(!)
104struct basecoder
105{
106 charmap cmap;
107 unsigned int enc_len [MAX_DEC_LEN];
108 unsigned int dec_len [MAX_ENC_LEN];
109
110 unsigned int encode_len (unsigned int len);
111 unsigned int decode_len (unsigned int len);
112
113 unsigned int encode (char *dst, u8 *src, unsigned int len);
114 unsigned int decode (u8 *dst, char *src, unsigned int len);
115
116 basecoder (const char *cmap);
117};
118
119basecoder::basecoder (const char *cmap)
120: cmap (cmap)
121{
122 for (unsigned int len = 0; len < MAX_DEC_LEN; ++len)
123 {
124 u8 src [MAX_DEC_LEN];
125 u8 dst [MAX_ENC_LEN];
126
127 memset (src, 255, len);
128
129 mp_limb_t m [MAX_LIMBS];
130 mp_size_t n;
131
132 n = mpn_set_str (m, src, len, 256);
133 n = mpn_get_str (dst, this->cmap.size, m, n);
134
135 for (int i = 0; !dst [i]; ++i)
136 n--;
137
138 enc_len [len] = n;
139 dec_len [n] = len;
140 }
141}
142
143unsigned int basecoder::encode_len (unsigned int len)
144{
145 return enc_len [len];
146}
147
148unsigned int basecoder::decode_len (unsigned int len)
149{
150 while (len && !dec_len [len])
151 --len;
152
153 return dec_len [len];
154}
155
156unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len)
157{
158 if (!len)
159 return 0;
160
161 int elen = encode_len (len);
162
163 mp_limb_t m [MAX_LIMBS];
164 mp_size_t n;
165
166 u8 dst_ [MAX_ENC_LEN];
167
168 n = mpn_set_str (m, src, len, 256);
169 n = mpn_get_str (dst_, cmap.size, m, n);
170
171 int plen = elen; // for padding
172
173 while (n < plen)
174 {
175 *dst++ = cmap.encode [0];
176 plen--;
177 }
178
179 for (unsigned int i = n - plen; i < n; ++i)
180 *dst++ = cmap.encode [dst_ [i]];
181
182 return elen;
183}
184
185unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len)
186{
187 if (!len)
188 return 0;
189
190 u8 src_ [MAX_ENC_LEN];
191 unsigned int elen = 0;
192
193 while (len--)
194 {
195 u8 val = cmap.decode [(u8)*src++];
196
197 if (val != charmap::INVALID)
198 src_ [elen++] = val;
199 }
200
201 int dlen = decode_len (elen);
202
203 mp_limb_t m [MAX_LIMBS];
204 mp_size_t n;
205
206 u8 dst_ [MAX_DEC_LEN];
207
208 n = mpn_set_str (m, src_, elen, cmap.size);
209 n = mpn_get_str (dst_, 256, m, n);
210
211 if (n < dlen)
212 {
213 memset (dst, 0, dlen - n);
214 memcpy (dst + dlen - n, dst_, n);
215 }
216 else
217 memcpy (dst, dst_ + n - dlen, dlen);
218
219 return dlen;
220}
221
222#if 0
223struct test { test (); } test;
224
225test::test ()
226{
227 basecoder cdc ("0123456789abcdefghijklmnopqrstuvwxyz");
228
229 u8 in[] = "0123456789abcdefghijklmnopqrstuvwxyz";
230 static char enc[200];
231 static u8 dec[200];
232
233 for (int i = 1; i < 20; i++)
234 {
235 int elen = cdc.encode (enc, in, i);
236 int dlen = cdc.decode (dec, enc, elen);
237
238 printf ("%d>%d>%d (%s>%s)\n", i, elen, dlen, enc, dec);
239 }
240 abort ();
241}
242#endif
243
244// the following sequence has been crafted to
245// a) look somewhat random
246// b) the even (and odd) indices never share the same character as upper/lowercase
75// the "_" is not valid but widely accepted (all octets should be supported, but let's be conservative) 247// the "_" is not valid but widely accepted (all octets should be supported, but let's be conservative)
76struct dns64 248// the other sequences are obviously derived
77{ 249//static basecoder cdc63 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI");
78 static const char encode_chars[64 + 1]; 250static basecoder cdc62 ("dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI");
79 static s8 decode_chars[256]; 251//static basecoder cdc36 ("dphzr06qmjkb34tsvl81xaef92wgyo57ucni"); // unused as of yet
252static basecoder cdc26 ("dPhZrQmJkBtSvLxAeFwGyO");
80 253
81 static int encode_len (int bytes) { return (bytes * 8 + 5) / 6; } 254/////////////////////////////////////////////////////////////////////////////
82 static int decode_len (int bytes) { return (bytes * 6) / 8; }
83 static int encode (char *dst, u8 *src, int len);
84 static int decode (u8 *dst, char *src, int len);
85 255
86 dns64 (); 256#define HDRSIZE 6
87} dns64;
88 257
89const char dns64::encode_chars[64 + 1] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"; 258inline void encode_header (char *data, int clientid, int seqno)
90s8 dns64::decode_chars[256];
91
92dns64::dns64 ()
93{ 259{
94 for (int i = 0; i < 64; i++) 260 u8 hdr[3] = { clientid, seqno >> 8, seqno };
95 decode_chars [encode_chars [i]] = i + 1;
96}
97 261
98int dns64::encode (char *dst, u8 *src, int len) 262 assert (clientid < 256);
99{
100 // slow, but easy to debug
101 char *beg = dst;
102 unsigned int accum, bits = 0;
103 263
104 while (len--) 264 cdc26.encode (data, hdr, 3);
105 {
106 accum <<= 8;
107 accum |= *src++;
108 bits += 8;
109
110 while (bits >= 6)
111 {
112 *dst++ = encode_chars [(accum >> (bits - 6)) & 63];
113 bits -= 6;
114 }
115 }
116
117 if (bits)
118 *dst++ = encode_chars [(accum << (6 - bits)) & 63];
119
120 return dst - beg;
121} 265}
122 266
123int dns64::decode (u8 *dst, char *src, int len) 267inline void decode_header (char *data, int &clientid, int &seqno)
124{ 268{
125 // slow, but easy to debug 269 u8 hdr[3];
126 u8 *beg = dst;
127 unsigned int accum, bits = 0;
128 270
129 while (len--) 271 cdc26.decode (hdr, data, HDRSIZE);
130 {
131 s8 chr = decode_chars [(u8)*src++];
132 272
133 if (!chr) 273 clientid = hdr[0];
134 break; 274 seqno = (hdr[1] << 8) | hdr[2];
135
136 accum <<= 6;
137 accum |= chr - 1;
138 bits += 6;
139
140 while (bits >= 8)
141 {
142 *dst++ = accum >> (bits - 8);
143 bits -= 8;
144 }
145 }
146
147 return dst - beg;
148} 275}
149 276
150///////////////////////////////////////////////////////////////////////////// 277/////////////////////////////////////////////////////////////////////////////
151 278
152struct byte_stream 279struct byte_stream
159 ~byte_stream (); 286 ~byte_stream ();
160 287
161 bool empty () { return !fill; } 288 bool empty () { return !fill; }
162 int size () { return fill; } 289 int size () { return fill; }
163 290
291 bool put (u8 *data, unsigned int datalen);
164 bool put (vpn_packet *pkt); 292 bool put (vpn_packet *pkt);
165 vpn_packet *get (); 293 vpn_packet *get ();
166 294
167 u8 *begin () { return data; } 295 u8 *begin () { return data; }
168 void remove (int count); 296 void remove (int count);
185 abort (); 313 abort ();
186 314
187 memmove (data, data + count, fill -= count); 315 memmove (data, data + count, fill -= count);
188} 316}
189 317
318bool byte_stream::put (u8 *data, unsigned int datalen)
319{
320 if (maxsize - fill < datalen)
321 return false;
322
323 memcpy (this->data + fill, data, datalen); fill += datalen;
324
325 return true;
326}
327
190bool byte_stream::put (vpn_packet *pkt) 328bool byte_stream::put (vpn_packet *pkt)
191{ 329{
192 if (maxsize - fill < pkt->len + 2) 330 if (maxsize - fill < pkt->len + 2)
193 return false; 331 return false;
194 332
202 340
203vpn_packet *byte_stream::get () 341vpn_packet *byte_stream::get ()
204{ 342{
205 int len = (data [0] << 8) | data [1]; 343 int len = (data [0] << 8) | data [1];
206 344
345 if (len > MAXSIZE && fill >= 2)
346 abort (); // TODO handle this gracefully, connection reset
347
207 if (fill < len + 2) 348 if (fill < len + 2)
208 return 0; 349 return 0;
209 350
210 vpn_packet *pkt = new vpn_packet; 351 vpn_packet *pkt = new vpn_packet;
352
353 pkt->len = len;
211 memcpy (&((*pkt)[0]), data + 2, len); 354 memcpy (&((*pkt)[0]), data + 2, len);
212 remove (len + 2); 355 remove (len + 2);
213 356
214 return pkt; 357 return pkt;
215} 358}
216 359
217///////////////////////////////////////////////////////////////////////////// 360/////////////////////////////////////////////////////////////////////////////
218 361
219#define FLAG_QUERY ( 0 << 15) 362#define FLAG_QUERY ( 0 << 15)
220#define FLAG_RESPONSE ( 1 << 15) 363#define FLAG_RESPONSE ( 1 << 15)
221#define FLAG_OP_MASK (15 << 14) 364#define FLAG_OP_MASK (15 << 11)
222#define FLAG_OP_QUERY ( 0 << 11) 365#define FLAG_OP_QUERY ( 0 << 11)
223#define FLAG_AA ( 1 << 10) 366#define FLAG_AA ( 1 << 10)
224#define FLAG_TC ( 1 << 9) 367#define FLAG_TC ( 1 << 9)
225#define FLAG_RD ( 1 << 8) 368#define FLAG_RD ( 1 << 8)
226#define FLAG_RA ( 1 << 7) 369#define FLAG_RA ( 1 << 7)
370#define FLAG_AUTH ( 1 << 5)
227#define FLAG_RCODE_MASK (15 << 0) 371#define FLAG_RCODE_MASK (15 << 0)
228#define FLAG_RCODE_OK ( 0 << 0) 372#define FLAG_RCODE_OK ( 0 << 0)
229#define FLAG_RCODE_FORMERR ( 1 << 0) 373#define FLAG_RCODE_FORMERR ( 1 << 0)
230#define FLAG_RCODE_SERVFAIL ( 2 << 0) 374#define FLAG_RCODE_SERVFAIL ( 2 << 0)
231#define FLAG_RCODE_NXDOMAIN ( 3 << 0) 375#define FLAG_RCODE_NXDOMAIN ( 3 << 0)
232#define FLAG_RCODE_REFUSED ( 5 << 0) 376#define FLAG_RCODE_REFUSED ( 5 << 0)
233 377
234#define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) 378#define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD)
235#define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD) 379#define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA)
236 380
237struct dns_packet : net_packet 381struct dns_packet : net_packet
238{ 382{
239 u16 id; 383 u16 id;
240 u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 384 u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4
284struct dns_req 428struct dns_req
285{ 429{
286 dns_packet *pkt; 430 dns_packet *pkt;
287 tstamp next; 431 tstamp next;
288 int retry; 432 int retry;
289 connection *conn; 433 struct dns_connection *dns;
434 int seqno;
290 435
291 dns_req (connection *c, int seqno, byte_stream *stream); 436 dns_req (dns_connection *dns);
437 void gen_stream_req (int seqno, byte_stream &stream);
292}; 438};
293 439
294struct dns_rep
295{
296};
297
298static u16 dns_id; // TODO: should be per-vpn 440static u16 dns_id = 12098; // TODO: should be per-vpn
299 441
300dns_req::dns_req (connection *c, int seqno, byte_stream *stream) 442static u16 next_id ()
301: conn (c) 443{
444 // the simplest lsfr with periodicity 65535 i could find
445 dns_id = (dns_id << 1)
446 | (((dns_id >> 1)
447 ^ (dns_id >> 2)
448 ^ (dns_id >> 4)
449 ^ (dns_id >> 15)) & 1);
450
451 return dns_id;
452}
453
454dns_req::dns_req (dns_connection *dns)
455: dns (dns)
302{ 456{
303 next = 0; 457 next = 0;
304 retry = 0; 458 retry = 0;
305 459
306 pkt = new dns_packet; 460 pkt = new dns_packet;
307 461
308 pkt->id = dns_id++; 462 pkt->id = next_id ();
463}
464
465void dns_req::gen_stream_req (int seqno, byte_stream &stream)
466{
467 this->seqno = seqno;
468
309 pkt->flags = DEFAULT_CLIENT_FLAGS; 469 pkt->flags = htons (DEFAULT_CLIENT_FLAGS);
310 pkt->qdcount = htons (1); 470 pkt->qdcount = htons (1);
311 pkt->ancount = 0;
312 pkt->nscount = 0;
313 pkt->arcount = 0;
314 471
315 int offs = 6*2; 472 int offs = 6*2;
316 int dlen = 255 - strlen (THISNODE->domain) - 1; // here we always have room for the max. label length 473 int dlen = MAX_DOMAIN_SIZE - (strlen (THISNODE->domain) + 2);
474 // 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
317 476
318 u8 lbl[64]; //D 477 char enc[256], *encp = enc;
319 char enc[65]; 478 encode_header (enc, THISNODE->id, seqno);
320 int lbllen = 3;
321 lbl[0] = c->conf->id; //D
322 lbl[1] = seqno >> 8; //D
323 lbl[2] = seqno; //D
324 479
325 while (dlen > 0) 480 int datalen = cdc62.decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE - HDRSIZE);
326 {
327 int sndlen = dlen;
328 481
329 if (sndlen + lbllen > dns64::decode_len (MAX_LBL_SIZE))
330 sndlen = dns64::decode_len (MAX_LBL_SIZE) - lbllen;
331
332 if (sndlen > stream->size ()) 482 if (datalen > stream.size ())
333 sndlen = stream->size (); 483 datalen = stream.size ();
334 484
335 if (sndlen + lbllen == 0) 485 int enclen = cdc62.encode (enc + HDRSIZE, stream.begin (), datalen) + HDRSIZE;
336 break;
337
338 memcpy (lbl + lbllen, stream->begin (), sndlen);
339 stream->remove (sndlen); 486 stream.remove (datalen);
340 lbllen += sndlen;
341 487
342 dns64::encode (enc, lbl, lbllen); 488 while (enclen)
489 {
490 int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE;
343 491
344 int elen = dns64::encode_len (lbllen);
345 (*pkt)[offs++] = elen; 492 (*pkt)[offs++] = lbllen;
346 memcpy (&((*pkt)[offs]), enc, elen); 493 memcpy (pkt->at (offs), encp, lbllen);
494
347 offs += elen; 495 offs += lbllen;
348 dlen -= elen + 1; 496 encp += lbllen;
349 497
350 lbllen = 0; 498 enclen -= lbllen;
351 } 499 }
352 500
353 const char *suffix = THISNODE->domain; 501 const char *suffix = THISNODE->domain;
354 502
355 // add tunnel domain 503 // add tunnel domain
375 (*pkt)[offs++] = 0; 523 (*pkt)[offs++] = 0;
376 (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; 524 (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY;
377 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; 525 (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN;
378 526
379 pkt->len = offs; 527 pkt->len = offs;
528}
380 529
381 c->vpn->dns_sndpq.push_back (this); 530struct dns_rcv
531{
532 int seqno;
533 dns_packet *pkt; // reply packet
534 u8 data [MAXSIZE]; // actually part of the reply packet...
535 int datalen;
536
537 dns_rcv (int seqno, u8 *data, int datalen);
538 ~dns_rcv ();
539};
540
541dns_rcv::dns_rcv (int seqno, u8 *data, int datalen)
542: seqno (seqno), pkt (new dns_packet), datalen (datalen)
543{
544 memcpy (this->data, data, datalen);
545}
546
547dns_rcv::~dns_rcv ()
548{
549 delete pkt;
382} 550}
383 551
384///////////////////////////////////////////////////////////////////////////// 552/////////////////////////////////////////////////////////////////////////////
553
554struct 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
574dns_connection::dns_connection (connection *c)
575: c (c)
576, rcvdq (MAX_BACKLOG * 2)
577, snddq (MAX_BACKLOG * 2)
578, tw (this, &dns_connection::time_cb)
579{
580 vpn = c->vpn;
581
582 rcvseq = sndseq = 0;
583}
584
585dns_connection::~dns_connection ()
586{
587 for (vector<dns_rcv *>::iterator i = rcvpq.begin ();
588 i != rcvpq.end ();
589 ++i)
590 delete *i;
591}
385 592
386struct dns_cfg 593struct dns_cfg
387{ 594{
388 u8 id1, id2, id3; 595 u8 id1, id2, id3;
389 u8 def_ttl; 596 u8 def_ttl;
390 u8 unused1; 597 u8 unused1;
391 u16 max_size; 598 u16 max_size;
392 u8 flags1, flags2; 599 u8 flags1, flags2;
393}; 600};
394 601
602void dns_connection::receive_rep (dns_rcv *r)
603{
604 rcvpq.push_back (r);
605
606 redo:
607
608 // find next packet
609 for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); )
610 if (SEQNO_EQ (rcvseq, (*i)->seqno))
611 {
612 // enter the packet into our input stream
613 r = *i;
614
615 // remove the oldest packet, look forward, as it's oldest first
616 for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j)
617 if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW))
618 {
619 delete *j;
620 rcvpq.erase (j);
621 break;
622 }
623
624 rcvseq = (rcvseq + 1) & SEQNO_MASK;
625
626 if (!rcvdq.put (r->data, r->datalen))
627 abort (); // MUST never overflow, can be caused by data corruption, TODO
628
629 while (vpn_packet *pkt = rcvdq.get ())
630 {
631 sockinfo si;
632 si.host = 0; si.port = 0; si.prot = PROT_DNSv4;
633
634 vpn->recv_vpn_packet (pkt, si);
635 }
636
637 // check for further packets
638 goto redo;
639 }
640}
641
395dns_packet * 642dns_packet *
396vpn::dnsv4_server (dns_packet *pkt) 643vpn::dnsv4_server (dns_packet *pkt)
397{ 644{
398 u16 flags = ntohs (pkt->flags); 645 u16 flags = ntohs (pkt->flags);
399 646
400 //memcpy (&((*rep)[0]), &((*pkt)[0]), pkt->len); 647 //memcpy (&((*rep)[0]), &((*pkt)[0]), pkt->len);
401 int offs = 6 * 2; // skip header 648 int offs = 6 * 2; // skip header
402 649
403 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); 650 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
404 651
405 if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) 652 if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC))
406 && pkt->qdcount == htons (1)) 653 && pkt->qdcount == htons (1))
407 { 654 {
408 char qname[MAXSIZE]; 655 char qname[MAXSIZE];
409 int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); 656 int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs);
410
411 printf ("rcvd packet len %d, id%04x flags%04x q%04x a%04x n%04x a%04x <%s>\n", pkt->len,
412 pkt->id, flags, pkt->qdcount, pkt->ancount, pkt->nscount, pkt->arcount, qname);//D
413 657
414 u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; 658 u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++];
415 u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; 659 u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++];
416 660
417 pkt->qdcount = htons (1); 661 pkt->qdcount = htons (1);
423 667
424 int dlen = strlen (THISNODE->domain); 668 int dlen = strlen (THISNODE->domain);
425 669
426 if (qclass == RR_CLASS_IN 670 if (qclass == RR_CLASS_IN
427 && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT) 671 && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT)
428 && qlen > dlen + 1 672 && qlen > dlen + 1 + HDRSIZE
429 && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) 673 && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen))
430 { 674 {
431 // correct class, domain, parse 675 // correct class, domain: parse
676 int client, seqno;
677 decode_header (qname, client, seqno);
678
432 u8 data[MAXSIZE]; 679 u8 data[MAXSIZE];
433 int datalen = dns64::decode (data, qname, qlen - dlen - 1); 680 int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE));
434 681
435 for (int i = 0; i < datalen; i++) 682 if (0 < client && client <= conns.size ())
436 printf ("%02x ", data[i]);
437 printf ("\n");
438
439
440
441
442#if 0
443 // now generate reply
444 rep->ancount = htons (1); // one answer RR
445 rep->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK);
446
447 (*rep) [offs++] = 0xc0;
448 (*rep) [offs++] = 6 * 2; // same as in query section
449
450 (*rep) [offs++] = RR_TYPE_TXT >> 8; (*rep) [offs++] = RR_TYPE_TXT;
451 (*rep) [offs++] = RR_CLASS_IN >> 8; (*rep) [offs++] = RR_CLASS_IN;
452
453 (*rep) [offs++] = 0; (*rep) [offs++] = 0;
454 (*rep) [offs++] = 0; (*rep) [offs++] = 0; // TTL
455
456 int dlen = MAX_PKT_SIZE - offs - 2;
457
458 printf ("we have room for %d bytes\n", dlen);
459
460 int rdlen_offs = offs += 2;
461
462 u8 txt[255];
463 txt[0] = 0;
464 txt[1] = 0;
465
466 int txtlen = 2;
467
468 while (dlen > 1)
469 { 683 {
470 int sndlen = --dlen; 684 connection *c = conns [client - 1];
471 685
472 if (sndlen + txtlen > 255) 686 if (!c->dns)
473 sndlen = 255 - txtlen; 687 c->dns = new dns_connection (c);
474 688
689 dns_connection *dns = c->dns;
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
475 sndlen = 0; 724 dlen -= qlen;
476 725
726 int rdlen_offs = offs += 2;
727
728 while (dlen > 1 && !dns->snddq.empty ())
729 {
730 int txtlen = dlen <= 255 ? dlen - 1 : 255;
731
732 if (txtlen > dns->snddq.size ())
733 txtlen = dns->snddq.size ();
734
477 (*rep)[offs++] = sndlen + txtlen; 735 (*pkt)[offs++] = txtlen;
478 memcpy (&((*rep)[offs]), txt, txtlen); 736 memcpy (pkt->at (offs), dns->snddq.begin (), txtlen);
479 offs += txtlen; 737 offs += txtlen;
738 dns->snddq.remove (txtlen);
480 739
481 //if (sndlen + txtlen > dns_snddq. 740 dlen -= txtlen + 1;
741 }
742
743 // avoid empty TXT rdata
744 if (offs == rdlen_offs)
745 (*pkt)[offs++] = 0;
746
747 int rdlen = offs - rdlen_offs;
748
749 (*pkt) [rdlen_offs - 2] = rdlen >> 8;
750 (*pkt) [rdlen_offs - 1] = rdlen;
751
752 // now update dns_rcv copy
753 rcv->pkt->len = offs;
754 memcpy (rcv->pkt->at (0), pkt->at (0), offs);
755
756 duplicate_request: ;
482 } 757 }
483 758 else
484 int rdlen = offs - rdlen_offs; 759 pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR);
485
486 (*rep) [rdlen_offs - 2] = rdlen >> 8;
487 (*rep) [rdlen_offs - 1] = rdlen;
488#endif
489 } 760 }
490 }
491 761
492 pkt->len = offs; 762 pkt->len = offs;
763 }
764
493 return pkt; 765 return pkt;
494} 766}
495 767
496void 768void
497vpn::dnsv4_client (dns_packet *pkt) 769vpn::dnsv4_client (dns_packet *pkt)
500 int offs = 6 * 2; // skip header 772 int offs = 6 * 2; // skip header
501 773
502 pkt->qdcount = ntohs (pkt->qdcount); 774 pkt->qdcount = ntohs (pkt->qdcount);
503 pkt->ancount = ntohs (pkt->ancount); 775 pkt->ancount = ntohs (pkt->ancount);
504 776
777 // go through our request list and find the corresponding request
505 for (vector<dns_req *>::iterator i = dns_sndpq.begin (); 778 for (vector<dns_req *>::iterator i = dns_sndpq.begin ();
506 i != dns_sndpq.end (); 779 i != dns_sndpq.end ();
507 ++i) 780 ++i)
508 if ((*i)->pkt->id == pkt->id) 781 if ((*i)->pkt->id == pkt->id)
509 { 782 {
510 connection *c = (*i)->conn; 783 dns_connection *dns = (*i)->dns;
511 printf ("GOT RESPONSE id %x %p\n", pkt->id, c);//D 784 int seqno = (*i)->seqno;
785 u8 data[MAXSIZE], *datap = data;
786
512 delete *i; 787 delete *i;
513 dns_sndpq.erase (i); 788 dns_sndpq.erase (i);
514 789
515 if (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) 790 if (flags & FLAG_RESPONSE && !(flags & (FLAG_OP_MASK | FLAG_TC)))
516 { 791 {
517 char qname[MAXSIZE]; 792 char qname[MAXSIZE];
518 793
519 while (pkt->qdcount-- && offs < MAXSIZE - 4) 794 while (pkt->qdcount-- && offs < MAXSIZE - 4)
520 { 795 {
521 int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); 796 int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs);
522 offs += 4; // skip qtype, qclass 797 offs += 4; // skip qtype, qclass
523 } 798 }
524 799
525 while (pkt->ancount-- && offs < MAXSIZE - 10) 800 while (pkt->ancount-- && offs < MAXSIZE - 10 && datap)
526 { 801 {
527 pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); 802 int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs);
528 803
529 u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; 804 u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++];
530 u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; 805 u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++];
531 u32 ttl = (*pkt) [offs++] << 24; 806 u32 ttl = (*pkt) [offs++] << 24;
532 ttl |= (*pkt) [offs++] << 16; 807 ttl |= (*pkt) [offs++] << 16;
533 ttl |= (*pkt) [offs++] << 8; 808 ttl |= (*pkt) [offs++] << 8;
534 ttl |= (*pkt) [offs++]; 809 ttl |= (*pkt) [offs++];
535 810
536 u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++]; 811 u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++];
537 812
538 printf ("REPLY %d:%d TTL %d RD %d\n", qtype, qclass, ttl, rdlen);
539
540 offs += rdlen;
541
542 if (MAXSIZE - offs < rdlen) 813 if (rdlen <= MAXSIZE - offs)
543 { 814 {
544 // decode bytes, finally 815 // decode bytes, finally
816
817 while (rdlen)
818 {
819 int txtlen = (*pkt) [offs++];
820
821 assert (txtlen + offs < MAXSIZE - 1);
822
823 memcpy (datap, pkt->at (offs), txtlen);
824 datap += txtlen; offs += txtlen;
825
826 rdlen -= txtlen + 1;
827 }
828 }
829
830 int client, rseqno;
831 decode_header (qname, client, rseqno);
832
833 if (client != THISNODE->id)
834 {
835 slog (L_INFO, _("got dns tunnel response with wrong clientid, ignoring"));
836 datap = 0;
837 }
838 else if (rseqno != seqno)
839 {
840 slog (L_DEBUG, _("got dns tunnel response with wrong seqno, badly caching nameserver?"));
841 datap = 0;
545 } 842 }
546 } 843 }
547 } 844 }
548 845
846 // todo: pkt now used
847 if (datap)
848 dns->receive_rep (new dns_rcv (seqno, data, datap - data));
849
549 break; 850 break;
550 } 851 }
852
853 delete pkt;
551} 854}
552 855
553void 856void
554vpn::dnsv4_ev (io_watcher &w, short revents) 857vpn::dnsv4_ev (io_watcher &w, short revents)
555{ 858{
560 socklen_t sa_len = sizeof (sa); 863 socklen_t sa_len = sizeof (sa);
561 864
562 pkt->len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); 865 pkt->len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len);
563 866
564 if (pkt->len > 0) 867 if (pkt->len > 0)
868 {
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
565 if (THISNODE->dns_port) 875 if (THISNODE->dns_port)
566 { 876 {
567 pkt = dnsv4_server (pkt); 877 pkt = dnsv4_server (pkt);
568 sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len); 878 sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len);
569 } 879 }
570 else 880 else
571 dnsv4_client (pkt); 881 dnsv4_client (pkt);
882 }
572 } 883 }
573} 884}
574 885
575bool 886bool
576connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) 887connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos)
577{ 888{
578 // never initialized 889 if (!dns)
579 if (!dns_snddq && !dns_rcvdq) 890 dns = new dns_connection (this);
580 {
581 dns_rcvdq = new byte_stream (MAX_BACKLOG * 2);
582 dns_snddq = new byte_stream (MAX_BACKLOG);
583
584 dns_rcvseq = dns_sndseq = 0;
585
586 dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4);
587 }
588 891
589 if (!dns_snddq->put (pkt)) 892 if (!dns->snddq.put (pkt))
590 return false; 893 return false;
591 894
592 // start timer if neccessary 895 // start timer if neccessary
593 if (!dnsv4_tw.active) 896 if (!THISNODE->dns_port && !dns->tw.active)
594 dnsv4_cb (dnsv4_tw); 897 dns->tw.trigger ();
595 898
596 return true; 899 return true;
597} 900}
598 901
599void 902void
600connection::dnsv4_cb (time_watcher &w) 903dns_connection::time_cb (time_watcher &w)
601{ 904{
602 // check for timeouts and (re)transmit 905 // check for timeouts and (re)transmit
603 tstamp next = NOW + 60; 906 tstamp next = NOW + 60;
604 dns_req *send = 0; 907 dns_req *send = 0;
605 908
606 slog (L_NOISE, _("dnsv4, %d packets in send queue\n"), vpn->dns_sndpq.size ());
607
608 for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin (); 909 for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin ();
609 i != vpn->dns_sndpq.end (); 910 i != vpn->dns_sndpq.end ();
610 ++i) 911 ++i)
611 { 912 {
612 dns_req *r = *i; 913 dns_req *r = *i;
615 { 916 {
616 if (!send) 917 if (!send)
617 { 918 {
618 send = r; 919 send = r;
619 920
620 slog (L_NOISE, _("dnsv4, send req %d\n"), r->pkt->id); 921 if (r->retry)//D
922 printf ("req %d:%d, retry %d\n", r->seqno, r->pkt->id, r->retry);
621 r->retry++; 923 r->retry++;
622 r->next = NOW + r->retry; 924 r->next = NOW + r->retry;
623 } 925 }
624 } 926 }
625 927
627 next = r->next; 929 next = r->next;
628 } 930 }
629 931
630 if (!send 932 if (!send
631 && vpn->dns_sndpq.size () < MAX_OUTSTANDING) 933 && vpn->dns_sndpq.size () < MAX_OUTSTANDING)
632 send = new dns_req (this, dns_sndseq++, dns_snddq); 934 {
935 send = new dns_req (this);
936 send->gen_stream_req (sndseq, snddq);
937 vpn->dns_sndpq.push_back (send);
938
939 sndseq = (sndseq + 1) & SEQNO_MASK;
940 }
633 941
634 tstamp min_next = NOW + (1. / (tstamp)MAX_RATE); 942 tstamp min_next = NOW + (1. / (tstamp)MAX_RATE);
635 943
636 if (send) 944 if (send)
637 { 945 {
638 dns_packet *pkt = send->pkt; 946 dns_packet *pkt = send->pkt;
639 947
640 next = min_next; 948 next = min_next;
641 949
642 sendto (vpn->dnsv4_fd, &((*pkt)[0]), pkt->len, 0, dns_si.sav4 (), dns_si.salenv4 ()); 950 sendto (vpn->dnsv4_fd, &((*pkt)[0]), pkt->len, 0,
951 vpn->dns_forwarder.sav4 (), vpn->dns_forwarder.salenv4 ());
643 } 952 }
644 else if (next < min_next) 953 else if (next < min_next)
645 next = min_next; 954 next = min_next;
646 955
647 w.start (next); 956 w.start (next);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines