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