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