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