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