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 | |
… | |
… | |
39 | |
41 | |
40 | #include "netcompat.h" |
42 | #include "netcompat.h" |
41 | |
43 | |
42 | #include "vpn.h" |
44 | #include "vpn.h" |
43 | |
45 | |
44 | #if ENABLE_HTTP_PROXY |
|
|
45 | # include "conf.h" |
|
|
46 | #endif |
|
|
47 | |
|
|
48 | #define MIN_RETRY 1. |
46 | #define MIN_RETRY 1. |
49 | #define MAX_RETRY 60. |
47 | #define MAX_RETRY 60. |
50 | |
48 | |
51 | #define SERVER conf.dns_port |
49 | #define MAX_OUTSTANDING 40 // max. outstanding requests |
|
|
50 | #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 |
|
|
53 | |
|
|
54 | #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 |
|
|
56 | // every two request byte sless give room for one reply byte |
|
|
57 | |
|
|
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 |
52 | |
62 | |
53 | /* |
63 | /* |
54 | |
64 | |
55 | protocol, in shorthand :) |
65 | protocol, in shorthand :) |
56 | |
66 | |
57 | <cseqno> ANY? poll for more data |
67 | client -> server <req> ANY? |
58 | => TXT <sseqno><data> |
68 | server -> client <req> TXT <rep> |
59 | <cseqno><data> TXT? send more data |
|
|
60 | => TXT <empty> |
|
|
61 | |
69 | |
62 | sequence numbers are 12 bit, 9 bit packet-number and 3 bit fragment id (7 |
70 | <req> is dns64-encoded <client-id:12><recv-seqno:10>[<send-seqno:10><data>] |
63 | - id, actually) last fragment is sent first, however |
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> |
64 | |
76 | |
65 | */ |
77 | */ |
|
|
78 | |
|
|
79 | #define MAX_LBL_SIZE 63 |
|
|
80 | #define MAX_PKT_SIZE 512 |
|
|
81 | |
|
|
82 | #define RR_TYPE_TXT 16 |
|
|
83 | #define RR_TYPE_ANY 255 |
|
|
84 | #define RR_CLASS_IN 1 |
|
|
85 | |
|
|
86 | // the "_" is not valid but widely accepted (all octets should be supported, but let's be conservative) |
|
|
87 | struct dns64 |
|
|
88 | { |
|
|
89 | static const char encode_chars[64 + 1]; |
|
|
90 | static s8 decode_chars[256]; |
|
|
91 | |
|
|
92 | static int encode_len (int bytes) { return (bytes * 8 + 5) / 6; } |
|
|
93 | static int decode_len (int bytes) { return (bytes * 6) / 8; } |
|
|
94 | static int encode (char *dst, u8 *src, int len); |
|
|
95 | static int decode (u8 *dst, char *src, int len); |
|
|
96 | |
|
|
97 | dns64 (); |
|
|
98 | } dns64; |
|
|
99 | |
|
|
100 | // the following sequence has been crafted to |
|
|
101 | // a) look somewhat random |
|
|
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 | { |
|
|
108 | for (int i = 0; i < 64; i++) |
|
|
109 | decode_chars [encode_chars [i]] = i + 1; |
|
|
110 | } |
|
|
111 | |
|
|
112 | int dns64::encode (char *dst, u8 *src, int len) |
|
|
113 | { |
|
|
114 | // slow, but easy to debug |
|
|
115 | char *beg = dst; |
|
|
116 | unsigned int accum, bits = 0; |
|
|
117 | |
|
|
118 | while (len--) |
|
|
119 | { |
|
|
120 | accum <<= 8; |
|
|
121 | accum |= *src++; |
|
|
122 | bits += 8; |
|
|
123 | |
|
|
124 | while (bits >= 6) |
|
|
125 | { |
|
|
126 | *dst++ = encode_chars [(accum >> (bits - 6)) & 63]; |
|
|
127 | bits -= 6; |
|
|
128 | } |
|
|
129 | } |
|
|
130 | |
|
|
131 | if (bits) |
|
|
132 | *dst++ = encode_chars [(accum << (6 - bits)) & 63]; |
|
|
133 | |
|
|
134 | return dst - beg; |
|
|
135 | } |
|
|
136 | |
|
|
137 | int dns64::decode (u8 *dst, char *src, int len) |
|
|
138 | { |
|
|
139 | // slow, but easy to debug |
|
|
140 | u8 *beg = dst; |
|
|
141 | unsigned int accum, bits = 0; |
|
|
142 | |
|
|
143 | while (len--) |
|
|
144 | { |
|
|
145 | s8 chr = decode_chars [(u8)*src++]; |
|
|
146 | |
|
|
147 | if (!chr) |
|
|
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 | } |
|
|
160 | |
|
|
161 | return dst - beg; |
|
|
162 | } |
|
|
163 | |
|
|
164 | ///////////////////////////////////////////////////////////////////////////// |
|
|
165 | |
|
|
166 | struct byte_stream |
|
|
167 | { |
|
|
168 | u8 *data; |
|
|
169 | int maxsize; |
|
|
170 | int fill; |
|
|
171 | |
|
|
172 | byte_stream (int maxsize); |
|
|
173 | ~byte_stream (); |
|
|
174 | |
|
|
175 | bool empty () { return !fill; } |
|
|
176 | int size () { return fill; } |
|
|
177 | |
|
|
178 | bool put (u8 *data, unsigned int datalen); |
|
|
179 | bool put (vpn_packet *pkt); |
|
|
180 | vpn_packet *get (); |
|
|
181 | |
|
|
182 | u8 *begin () { return data; } |
|
|
183 | void remove (int count); |
|
|
184 | }; |
|
|
185 | |
|
|
186 | byte_stream::byte_stream (int maxsize) |
|
|
187 | : maxsize (maxsize), fill (0) |
|
|
188 | { |
|
|
189 | data = new u8 [maxsize]; |
|
|
190 | } |
|
|
191 | |
|
|
192 | byte_stream::~byte_stream () |
|
|
193 | { |
|
|
194 | delete data; |
|
|
195 | } |
|
|
196 | |
|
|
197 | void byte_stream::remove (int count) |
|
|
198 | { |
|
|
199 | if (count > fill) |
|
|
200 | abort (); |
|
|
201 | |
|
|
202 | memmove (data, data + count, fill -= count); |
|
|
203 | } |
|
|
204 | |
|
|
205 | bool byte_stream::put (u8 *data, unsigned int datalen) |
|
|
206 | { |
|
|
207 | if (maxsize - fill < datalen) |
|
|
208 | return false; |
|
|
209 | |
|
|
210 | memcpy (this->data + fill, data, datalen); fill += datalen; |
|
|
211 | |
|
|
212 | return true; |
|
|
213 | } |
|
|
214 | |
|
|
215 | bool byte_stream::put (vpn_packet *pkt) |
|
|
216 | { |
|
|
217 | if (maxsize - fill < pkt->len + 2) |
|
|
218 | return false; |
|
|
219 | |
|
|
220 | data [fill++] = pkt->len >> 8; |
|
|
221 | data [fill++] = pkt->len; |
|
|
222 | |
|
|
223 | memcpy (data + fill, &((*pkt)[0]), pkt->len); fill += pkt->len; |
|
|
224 | |
|
|
225 | return true; |
|
|
226 | } |
|
|
227 | |
|
|
228 | vpn_packet *byte_stream::get () |
|
|
229 | { |
|
|
230 | int len = (data [0] << 8) | data [1]; |
|
|
231 | |
|
|
232 | if (len > MAXSIZE && fill >= 2) |
|
|
233 | abort (); // TODO handle this gracefully, connection reset |
|
|
234 | |
|
|
235 | if (fill < len + 2) |
|
|
236 | return 0; |
|
|
237 | |
|
|
238 | vpn_packet *pkt = new vpn_packet; |
|
|
239 | |
|
|
240 | pkt->len = len; |
|
|
241 | memcpy (&((*pkt)[0]), data + 2, len); |
|
|
242 | remove (len + 2); |
|
|
243 | |
|
|
244 | return pkt; |
|
|
245 | } |
|
|
246 | |
|
|
247 | ///////////////////////////////////////////////////////////////////////////// |
|
|
248 | |
|
|
249 | #define FLAG_QUERY ( 0 << 15) |
|
|
250 | #define FLAG_RESPONSE ( 1 << 15) |
|
|
251 | #define FLAG_OP_MASK (15 << 14) |
|
|
252 | #define FLAG_OP_QUERY ( 0 << 11) |
|
|
253 | #define FLAG_AA ( 1 << 10) |
|
|
254 | #define FLAG_TC ( 1 << 9) |
|
|
255 | #define FLAG_RD ( 1 << 8) |
|
|
256 | #define FLAG_RA ( 1 << 7) |
|
|
257 | #define FLAG_AUTH ( 1 << 5) |
|
|
258 | #define FLAG_RCODE_MASK (15 << 0) |
|
|
259 | #define FLAG_RCODE_OK ( 0 << 0) |
|
|
260 | #define FLAG_RCODE_FORMERR ( 1 << 0) |
|
|
261 | #define FLAG_RCODE_SERVFAIL ( 2 << 0) |
|
|
262 | #define FLAG_RCODE_NXDOMAIN ( 3 << 0) |
|
|
263 | #define FLAG_RCODE_REFUSED ( 5 << 0) |
|
|
264 | |
|
|
265 | #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) |
|
|
267 | |
|
|
268 | struct dns_packet : net_packet |
|
|
269 | { |
|
|
270 | u16 id; |
|
|
271 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
|
|
272 | u16 qdcount, ancount, nscount, arcount; |
|
|
273 | |
|
|
274 | u8 data[MAXSIZE - 6 * 2]; |
|
|
275 | |
|
|
276 | int decode_label (char *data, int size, int &offs); |
|
|
277 | }; |
|
|
278 | |
|
|
279 | int dns_packet::decode_label (char *data, int size, int &offs) |
|
|
280 | { |
|
|
281 | char *orig = data; |
|
|
282 | |
|
|
283 | memset (data, 0, size); |
|
|
284 | |
|
|
285 | while (offs < size - 1) |
|
|
286 | { |
|
|
287 | u8 len = (*this)[offs++]; |
|
|
288 | |
|
|
289 | if (!len) |
|
|
290 | break; |
|
|
291 | else if (len < 64) |
|
|
292 | { |
|
|
293 | if (size < len + 1 || offs + len >= MAXSIZE - 1) |
|
|
294 | break; |
|
|
295 | |
|
|
296 | memcpy (data, &((*this)[offs]), len); |
|
|
297 | |
|
|
298 | data += len; size -= len; offs += len; |
|
|
299 | *data++ = '.'; size--; |
|
|
300 | } |
|
|
301 | else |
|
|
302 | { |
|
|
303 | int offs2 = ((len & 63) << 8) + (*this)[offs++]; |
|
|
304 | |
|
|
305 | data += decode_label (data, size, offs2); |
|
|
306 | break; |
|
|
307 | } |
|
|
308 | } |
|
|
309 | |
|
|
310 | return data - orig; |
|
|
311 | } |
|
|
312 | |
|
|
313 | ///////////////////////////////////////////////////////////////////////////// |
|
|
314 | |
|
|
315 | struct dns_req |
|
|
316 | { |
|
|
317 | dns_packet *pkt; |
|
|
318 | tstamp next; |
|
|
319 | int retry; |
|
|
320 | connection *conn; |
|
|
321 | int seqno; |
|
|
322 | |
|
|
323 | dns_req (connection *c); |
|
|
324 | void gen_stream_req (int seqno, byte_stream *stream); |
|
|
325 | }; |
|
|
326 | |
|
|
327 | static u16 dns_id = 12098; // TODO: should be per-vpn |
|
|
328 | |
|
|
329 | static u16 next_id () |
|
|
330 | { |
|
|
331 | // the simplest lsfr with periodicity 65535 i could find |
|
|
332 | dns_id = (dns_id << 1) |
|
|
333 | | (((dns_id >> 1) |
|
|
334 | ^ (dns_id >> 2) |
|
|
335 | ^ (dns_id >> 4) |
|
|
336 | ^ (dns_id >> 15)) & 1); |
|
|
337 | |
|
|
338 | return dns_id; |
|
|
339 | } |
|
|
340 | |
|
|
341 | dns_req::dns_req (connection *c) |
|
|
342 | : conn (c) |
|
|
343 | { |
|
|
344 | next = 0; |
|
|
345 | retry = 0; |
|
|
346 | |
|
|
347 | pkt = new dns_packet; |
|
|
348 | |
|
|
349 | pkt->id = next_id (); |
|
|
350 | } |
|
|
351 | |
|
|
352 | void dns_req::gen_stream_req (int seqno, byte_stream *stream) |
|
|
353 | { |
|
|
354 | this->seqno = seqno; |
|
|
355 | |
|
|
356 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
|
|
357 | pkt->qdcount = htons (1); |
|
|
358 | |
|
|
359 | int offs = 6*2; |
|
|
360 | int dlen = MAX_DOMAIN_SIZE - strlen (THISNODE->domain) - 2; |
|
|
361 | // 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 |
|
|
363 | |
|
|
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; |
|
|
376 | |
|
|
377 | memcpy (data + 3, stream->begin (), datalen); |
|
|
378 | int enclen = dns64::encode (enc, data, datalen + 3); |
|
|
379 | stream->remove (datalen); |
|
|
380 | |
|
|
381 | while (enclen) |
|
|
382 | { |
|
|
383 | int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE; |
|
|
384 | |
|
|
385 | (*pkt)[offs++] = lbllen; |
|
|
386 | memcpy (pkt->at (offs), encp, lbllen); |
|
|
387 | |
|
|
388 | offs += lbllen; |
|
|
389 | encp += lbllen; |
|
|
390 | |
|
|
391 | enclen -= lbllen; |
|
|
392 | } |
|
|
393 | |
|
|
394 | const char *suffix = 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 | |
|
|
416 | (*pkt)[offs++] = 0; |
|
|
417 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
|
|
418 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
|
|
419 | |
|
|
420 | pkt->len = offs; |
|
|
421 | } |
|
|
422 | |
|
|
423 | struct dns_rcv |
|
|
424 | { |
|
|
425 | int seqno; |
|
|
426 | dns_packet *pkt; // reply packet |
|
|
427 | u8 data [MAXSIZE]; // actually part of the reply packet... |
|
|
428 | int datalen; |
|
|
429 | |
|
|
430 | dns_rcv (int seqno, u8 *data, int datalen); |
|
|
431 | ~dns_rcv (); |
|
|
432 | }; |
|
|
433 | |
|
|
434 | dns_rcv::dns_rcv (int seqno, u8 *data, int datalen) |
|
|
435 | : seqno (seqno), pkt (new dns_packet), datalen (datalen) |
|
|
436 | { |
|
|
437 | memcpy (this->data, data, datalen); |
|
|
438 | } |
|
|
439 | |
|
|
440 | dns_rcv::~dns_rcv () |
|
|
441 | { |
|
|
442 | delete pkt; |
|
|
443 | } |
|
|
444 | |
|
|
445 | ///////////////////////////////////////////////////////////////////////////// |
|
|
446 | |
|
|
447 | struct dns_cfg |
|
|
448 | { |
|
|
449 | u8 id1, id2, id3; |
|
|
450 | u8 def_ttl; |
|
|
451 | u8 unused1; |
|
|
452 | u16 max_size; |
|
|
453 | u8 flags1, flags2; |
|
|
454 | }; |
|
|
455 | |
|
|
456 | void connection::dnsv4_receive_rep (struct dns_rcv *r) |
|
|
457 | { |
|
|
458 | dns_rcvpq.push_back (r); |
|
|
459 | |
|
|
460 | redo: |
|
|
461 | |
|
|
462 | for (vector<dns_rcv *>::iterator i = dns_rcvpq.begin (); |
|
|
463 | i != dns_rcvpq.end (); |
|
|
464 | ++i) |
|
|
465 | if (dns_rcvseq == (*i)->seqno) |
|
|
466 | { |
|
|
467 | dns_rcv *r = *i; |
|
|
468 | |
|
|
469 | dns_rcvseq = (dns_rcvseq + 1) & SEQNO_MASK; |
|
|
470 | |
|
|
471 | if (!dns_snddq && !dns_rcvdq) |
|
|
472 | { |
|
|
473 | dns_rcvdq = new byte_stream (MAX_BACKLOG * 2); |
|
|
474 | dns_snddq = new byte_stream (MAX_BACKLOG); |
|
|
475 | |
|
|
476 | dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
|
|
477 | } |
|
|
478 | |
|
|
479 | if (!dns_rcvdq->put (r->data, r->datalen)) |
|
|
480 | abort (); // MUST never overflow, can be caused by data corruption, TODO |
|
|
481 | |
|
|
482 | while (vpn_packet *pkt = dns_rcvdq->get ()) |
|
|
483 | { |
|
|
484 | sockinfo si; |
|
|
485 | si.host = 0; si.port = 0; si.prot = PROT_DNSv4; |
|
|
486 | |
|
|
487 | vpn->recv_vpn_packet (pkt, si); |
|
|
488 | } |
|
|
489 | } |
|
|
490 | else if ((u32)(*i)->seqno - (u32)dns_rcvseq + MAX_WINDOW > MAX_WINDOW * 2) |
|
|
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; |
|
|
497 | } |
|
|
498 | } |
|
|
499 | |
|
|
500 | dns_packet * |
|
|
501 | vpn::dnsv4_server (dns_packet *pkt) |
|
|
502 | { |
|
|
503 | u16 flags = ntohs (pkt->flags); |
|
|
504 | |
|
|
505 | //memcpy (&((*rep)[0]), &((*pkt)[0]), pkt->len); |
|
|
506 | int offs = 6 * 2; // skip header |
|
|
507 | |
|
|
508 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
|
|
509 | |
|
|
510 | if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
|
|
511 | && pkt->qdcount == htons (1)) |
|
|
512 | { |
|
|
513 | char qname[MAXSIZE]; |
|
|
514 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
|
|
515 | |
|
|
516 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
|
|
517 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
|
|
518 | |
|
|
519 | pkt->qdcount = htons (1); |
|
|
520 | pkt->ancount = 0; |
|
|
521 | 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 |
|
|
523 | |
|
|
524 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); |
|
|
525 | |
|
|
526 | int dlen = strlen (THISNODE->domain); |
|
|
527 | |
|
|
528 | if (qclass == RR_CLASS_IN |
|
|
529 | && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT) |
|
|
530 | && qlen > dlen + 1 |
|
|
531 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
|
|
532 | { |
|
|
533 | // correct class, domain: parse |
|
|
534 | u8 data[MAXSIZE]; |
|
|
535 | int datalen = dns64::decode (data, qname, qlen - dlen - 1); |
|
|
536 | |
|
|
537 | int client = data[0]; |
|
|
538 | int seqno = ((data[1] << 7) | (data[2] >> 1)) & SEQNO_MASK; |
|
|
539 | |
|
|
540 | if (0 < client && client <= conns.size ()) |
|
|
541 | { |
|
|
542 | connection *c = conns [client - 1]; |
|
|
543 | |
|
|
544 | redo: |
|
|
545 | |
|
|
546 | for (vector<dns_rcv *>::iterator i = c->dns_rcvpq.begin (); |
|
|
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 | { |
|
|
589 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
|
|
590 | |
|
|
591 | if (txtlen > c->dns_snddq->size ()) |
|
|
592 | txtlen = c->dns_snddq->size (); |
|
|
593 | |
|
|
594 | (*pkt)[offs++] = txtlen; |
|
|
595 | memcpy (pkt->at (offs), c->dns_snddq->begin (), txtlen); |
|
|
596 | offs += txtlen; |
|
|
597 | c->dns_snddq->remove (txtlen); |
|
|
598 | |
|
|
599 | dlen -= txtlen + 1; |
|
|
600 | } |
|
|
601 | |
|
|
602 | // avoid empty TXT rdata |
|
|
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 | } |
|
|
617 | else |
|
|
618 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
|
|
619 | } |
|
|
620 | |
|
|
621 | pkt->len = offs; |
|
|
622 | } |
|
|
623 | |
|
|
624 | return pkt; |
|
|
625 | } |
|
|
626 | |
|
|
627 | void |
|
|
628 | vpn::dnsv4_client (dns_packet *pkt) |
|
|
629 | { |
|
|
630 | u16 flags = ntohs (pkt->flags); |
|
|
631 | int offs = 6 * 2; // skip header |
|
|
632 | |
|
|
633 | pkt->qdcount = ntohs (pkt->qdcount); |
|
|
634 | pkt->ancount = ntohs (pkt->ancount); |
|
|
635 | |
|
|
636 | // go through our request list and find the corresponding request |
|
|
637 | for (vector<dns_req *>::iterator i = dns_sndpq.begin (); |
|
|
638 | i != dns_sndpq.end (); |
|
|
639 | ++i) |
|
|
640 | if ((*i)->pkt->id == pkt->id) |
|
|
641 | { |
|
|
642 | connection *c = (*i)->conn; |
|
|
643 | int seqno = (*i)->seqno; |
|
|
644 | u8 data[MAXSIZE], *datap = data; |
|
|
645 | |
|
|
646 | delete *i; |
|
|
647 | dns_sndpq.erase (i); |
|
|
648 | |
|
|
649 | if (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
|
|
650 | { |
|
|
651 | char qname[MAXSIZE]; |
|
|
652 | |
|
|
653 | while (pkt->qdcount-- && offs < MAXSIZE - 4) |
|
|
654 | { |
|
|
655 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
|
|
656 | offs += 4; // skip qtype, qclass |
|
|
657 | } |
|
|
658 | |
|
|
659 | while (pkt->ancount-- && offs < MAXSIZE - 10) |
|
|
660 | { |
|
|
661 | pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
|
|
662 | |
|
|
663 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
|
|
664 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
|
|
665 | u32 ttl = (*pkt) [offs++] << 24; |
|
|
666 | ttl |= (*pkt) [offs++] << 16; |
|
|
667 | ttl |= (*pkt) [offs++] << 8; |
|
|
668 | ttl |= (*pkt) [offs++]; |
|
|
669 | |
|
|
670 | u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++]; |
|
|
671 | |
|
|
672 | if (rdlen <= MAXSIZE - offs) |
|
|
673 | { |
|
|
674 | // decode bytes, finally |
|
|
675 | |
|
|
676 | while (rdlen) |
|
|
677 | { |
|
|
678 | int txtlen = (*pkt) [offs++]; |
|
|
679 | |
|
|
680 | assert (txtlen + offs < MAXSIZE - 1); |
|
|
681 | |
|
|
682 | memcpy (datap, pkt->at (offs), txtlen); |
|
|
683 | datap += txtlen; offs += txtlen; |
|
|
684 | |
|
|
685 | rdlen -= txtlen + 1; |
|
|
686 | } |
|
|
687 | |
|
|
688 | } |
|
|
689 | |
|
|
690 | } |
|
|
691 | } |
|
|
692 | |
|
|
693 | // todo: pkt now used |
|
|
694 | c->dnsv4_receive_rep (new dns_rcv (seqno, data, datap - data)); |
|
|
695 | |
|
|
696 | break; |
|
|
697 | } |
|
|
698 | |
|
|
699 | delete pkt; |
|
|
700 | } |
66 | |
701 | |
67 | void |
702 | void |
68 | vpn::dnsv4_ev (io_watcher &w, short revents) |
703 | vpn::dnsv4_ev (io_watcher &w, short revents) |
69 | { |
704 | { |
70 | if (revents & EVENT_READ) |
705 | if (revents & EVENT_READ) |
71 | { |
706 | { |
|
|
707 | dns_packet *pkt = new dns_packet; |
72 | struct sockaddr_in sa; |
708 | struct sockaddr_in sa; |
73 | socklen_t sa_len = sizeof (sa); |
709 | socklen_t sa_len = sizeof (sa); |
74 | int len; |
710 | |
|
|
711 | pkt->len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
|
|
712 | |
|
|
713 | if (pkt->len > 0) |
|
|
714 | { |
|
|
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) |
|
|
722 | { |
|
|
723 | pkt = dnsv4_server (pkt); |
|
|
724 | sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len); |
|
|
725 | } |
|
|
726 | else |
|
|
727 | dnsv4_client (pkt); |
|
|
728 | } |
75 | } |
729 | } |
76 | } |
730 | } |
77 | |
731 | |
78 | bool |
732 | bool |
79 | vpn::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
733 | connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
80 | { |
734 | { |
81 | //return i->send_packet (pkt, tos); |
735 | // never initialized |
|
|
736 | if (!dns_snddq && !dns_rcvdq) |
|
|
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 | |
|
|
746 | if (!dns_snddq->put (pkt)) |
|
|
747 | return false; |
|
|
748 | |
|
|
749 | // start timer if neccessary |
|
|
750 | if (!THISNODE->dns_port && !dnsv4_tw.active) |
|
|
751 | dnsv4_cb (dnsv4_tw); |
|
|
752 | |
|
|
753 | return true; |
|
|
754 | } |
|
|
755 | |
|
|
756 | void |
|
|
757 | connection::dnsv4_cb (time_watcher &w) |
|
|
758 | { |
|
|
759 | // check for timeouts and (re)transmit |
|
|
760 | tstamp next = NOW + 60; |
|
|
761 | dns_req *send = 0; |
|
|
762 | |
|
|
763 | for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin (); |
|
|
764 | i != vpn->dns_sndpq.end (); |
|
|
765 | ++i) |
|
|
766 | { |
|
|
767 | dns_req *r = *i; |
|
|
768 | |
|
|
769 | if (r->next <= NOW) |
|
|
770 | { |
|
|
771 | if (!send) |
|
|
772 | { |
|
|
773 | send = r; |
|
|
774 | |
|
|
775 | if (r->retry)//D |
|
|
776 | printf ("req %d, retry %d\n", r->pkt->id, r->retry); |
|
|
777 | r->retry++; |
|
|
778 | r->next = NOW + r->retry; |
|
|
779 | } |
|
|
780 | } |
|
|
781 | |
|
|
782 | if (r->next < next) |
|
|
783 | next = r->next; |
|
|
784 | } |
|
|
785 | |
|
|
786 | if (!send |
|
|
787 | && vpn->dns_sndpq.size () < MAX_OUTSTANDING) |
|
|
788 | { |
|
|
789 | send = new dns_req (this); |
|
|
790 | send->gen_stream_req (dns_sndseq, dns_snddq); |
|
|
791 | vpn->dns_sndpq.push_back (send); |
|
|
792 | |
|
|
793 | dns_sndseq = (dns_sndseq + 1) & SEQNO_MASK; |
|
|
794 | } |
|
|
795 | |
|
|
796 | tstamp min_next = NOW + (1. / (tstamp)MAX_RATE); |
|
|
797 | |
|
|
798 | if (send) |
|
|
799 | { |
|
|
800 | dns_packet *pkt = send->pkt; |
|
|
801 | |
|
|
802 | next = min_next; |
|
|
803 | |
|
|
804 | sendto (vpn->dnsv4_fd, &((*pkt)[0]), pkt->len, 0, dns_si.sav4 (), dns_si.salenv4 ()); |
|
|
805 | } |
|
|
806 | else if (next < min_next) |
|
|
807 | next = min_next; |
|
|
808 | |
|
|
809 | w.start (next); |
82 | } |
810 | } |
83 | |
811 | |
84 | #endif |
812 | #endif |
85 | |
813 | |