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