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