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