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-2005 Marc Lehmann <gvpe@schmorp.de> |
3 | Copyright (C) 2003-2008 Marc Lehmann <gvpe@schmorp.de> |
4 | |
4 | |
5 | This file is part of GVPE. |
5 | This file is part of GVPE. |
6 | |
6 | |
7 | GVPE is free software; you can redistribute it and/or modify |
7 | GVPE is free software; you can redistribute it and/or modify it |
8 | it under the terms of the GNU General Public License as published by |
8 | under the terms of the GNU General Public License as published by the |
9 | the Free Software Foundation; either version 2 of the License, or |
9 | Free Software Foundation; either version 3 of the License, or (at your |
10 | (at your option) any later version. |
10 | option) any later version. |
11 | |
11 | |
12 | This program is distributed in the hope that it will be useful, |
12 | This program is distributed in the hope that it will be useful, but |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
15 | GNU General Public License for more details. |
15 | Public License for more details. |
16 | |
16 | |
17 | You should have received a copy of the GNU General Public License |
17 | You should have received a copy of the GNU General Public License along |
18 | along with gvpe; if not, write to the Free Software |
18 | with this program; if not, see <http://www.gnu.org/licenses/>. |
19 | Foundation, Inc. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
19 | |
|
|
20 | Additional permission under GNU GPL version 3 section 7 |
|
|
21 | |
|
|
22 | If you modify this Program, or any covered work, by linking or |
|
|
23 | combining it with the OpenSSL project's OpenSSL library (or a modified |
|
|
24 | version of that library), containing parts covered by the terms of the |
|
|
25 | OpenSSL or SSLeay licenses, the licensors of this Program grant you |
|
|
26 | additional permission to convey the resulting work. Corresponding |
|
|
27 | Source for a non-source form of such a combination shall include the |
|
|
28 | source code for the parts of OpenSSL used as well as that of the |
|
|
29 | covered work. |
20 | */ |
30 | */ |
21 | |
31 | |
22 | // TODO: EDNS0 option to increase dns mtu? |
32 | // TODO: EDNS0 option to increase dns mtu? |
23 | // TODO: re-write dns packet parsing/creation using a safe mem-buffer |
33 | // TODO: re-write dns packet parsing/creation using a safe mem-buffer |
24 | // to ensure no buffer overflows or similar problems. |
34 | // to ensure no buffer overflows or similar problems. |
… | |
… | |
73 | #define MAX_PKT_SIZE 512 |
83 | #define MAX_PKT_SIZE 512 |
74 | |
84 | |
75 | #define RR_TYPE_A 1 |
85 | #define RR_TYPE_A 1 |
76 | #define RR_TYPE_NULL 10 |
86 | #define RR_TYPE_NULL 10 |
77 | #define RR_TYPE_TXT 16 |
87 | #define RR_TYPE_TXT 16 |
|
|
88 | #define RR_TYPE_AAAA 28 |
78 | #define RR_TYPE_ANY 255 |
89 | #define RR_TYPE_ANY 255 |
79 | |
90 | |
80 | #define RR_CLASS_IN 1 |
91 | #define RR_CLASS_IN 1 |
81 | |
92 | |
82 | #define CMD_IP_1 207 |
93 | #define CMD_IP_1 207 |
… | |
… | |
157 | enc_len [len] = n; |
168 | enc_len [len] = n; |
158 | dec_len [n] = len; |
169 | dec_len [n] = len; |
159 | } |
170 | } |
160 | } |
171 | } |
161 | |
172 | |
|
|
173 | unsigned int |
162 | unsigned int basecoder::encode_len (unsigned int len) |
174 | basecoder::encode_len (unsigned int len) |
163 | { |
175 | { |
164 | return enc_len [len]; |
176 | return enc_len [len]; |
165 | } |
177 | } |
166 | |
178 | |
|
|
179 | unsigned int |
167 | unsigned int basecoder::decode_len (unsigned int len) |
180 | basecoder::decode_len (unsigned int len) |
168 | { |
181 | { |
169 | while (len && !dec_len [len]) |
182 | while (len && !dec_len [len]) |
170 | --len; |
183 | --len; |
171 | |
184 | |
172 | return dec_len [len]; |
185 | return dec_len [len]; |
173 | } |
186 | } |
174 | |
187 | |
|
|
188 | unsigned int |
175 | unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len) |
189 | basecoder::encode (char *dst, u8 *src, unsigned int len) |
176 | { |
190 | { |
177 | if (!len || len > MAX_DEC_LEN) |
191 | if (!len || len > MAX_DEC_LEN) |
178 | return 0; |
192 | return 0; |
179 | |
193 | |
180 | int elen = encode_len (len); |
194 | int elen = encode_len (len); |
… | |
… | |
199 | *dst++ = cmap.encode [dst_ [i]]; |
213 | *dst++ = cmap.encode [dst_ [i]]; |
200 | |
214 | |
201 | return elen; |
215 | return elen; |
202 | } |
216 | } |
203 | |
217 | |
|
|
218 | unsigned int |
204 | unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len) |
219 | basecoder::decode (u8 *dst, char *src, unsigned int len) |
205 | { |
220 | { |
206 | if (!len || len > MAX_ENC_LEN) |
221 | if (!len || len > MAX_ENC_LEN) |
207 | return 0; |
222 | return 0; |
208 | |
223 | |
209 | u8 src_ [MAX_ENC_LEN]; |
224 | u8 src_ [MAX_ENC_LEN]; |
… | |
… | |
268 | |
283 | |
269 | ///////////////////////////////////////////////////////////////////////////// |
284 | ///////////////////////////////////////////////////////////////////////////// |
270 | |
285 | |
271 | #define HDRSIZE 6 |
286 | #define HDRSIZE 6 |
272 | |
287 | |
|
|
288 | inline void |
273 | inline void encode_header (char *data, int clientid, int seqno, int retry = 0) |
289 | encode_header (char *data, int clientid, int seqno, int retry = 0) |
274 | { |
290 | { |
275 | seqno &= SEQNO_MASK; |
291 | seqno &= SEQNO_MASK; |
276 | |
292 | |
277 | u8 hdr[3] = { |
293 | u8 hdr[3] = { |
278 | clientid, |
294 | clientid, |
… | |
… | |
283 | assert (clientid < 256); |
299 | assert (clientid < 256); |
284 | |
300 | |
285 | cdc26.encode (data, hdr, 3); |
301 | cdc26.encode (data, hdr, 3); |
286 | } |
302 | } |
287 | |
303 | |
|
|
304 | inline void |
288 | inline void decode_header (char *data, int &clientid, int &seqno) |
305 | decode_header (char *data, int &clientid, int &seqno) |
289 | { |
306 | { |
290 | u8 hdr[3]; |
307 | u8 hdr[3]; |
291 | |
308 | |
292 | cdc26.decode (hdr, data, HDRSIZE); |
309 | cdc26.decode (hdr, data, HDRSIZE); |
293 | |
310 | |
… | |
… | |
326 | byte_stream::~byte_stream () |
343 | byte_stream::~byte_stream () |
327 | { |
344 | { |
328 | delete data; |
345 | delete data; |
329 | } |
346 | } |
330 | |
347 | |
|
|
348 | void |
331 | void byte_stream::remove (int count) |
349 | byte_stream::remove (int count) |
332 | { |
350 | { |
333 | if (count > fill) |
351 | if (count > fill) |
334 | assert (count <= fill); |
352 | assert (count <= fill); |
335 | |
353 | |
336 | memmove (data, data + count, fill -= count); |
354 | memmove (data, data + count, fill -= count); |
337 | } |
355 | } |
338 | |
356 | |
|
|
357 | bool |
339 | bool byte_stream::put (u8 *data, unsigned int datalen) |
358 | byte_stream::put (u8 *data, unsigned int datalen) |
340 | { |
359 | { |
341 | if (maxsize - fill < datalen) |
360 | if (maxsize - fill < datalen) |
342 | return false; |
361 | return false; |
343 | |
362 | |
344 | memcpy (this->data + fill, data, datalen); fill += datalen; |
363 | memcpy (this->data + fill, data, datalen); fill += datalen; |
345 | |
364 | |
346 | return true; |
365 | return true; |
347 | } |
366 | } |
348 | |
367 | |
|
|
368 | bool |
349 | bool byte_stream::put (vpn_packet *pkt) |
369 | byte_stream::put (vpn_packet *pkt) |
350 | { |
370 | { |
351 | if (maxsize - fill < pkt->len + 2) |
371 | if (maxsize - fill < pkt->len + 2) |
352 | return false; |
372 | return false; |
353 | |
373 | |
354 | data [fill++] = pkt->len >> 8; |
374 | data [fill++] = pkt->len >> 8; |
… | |
… | |
436 | bool valid (); |
456 | bool valid (); |
437 | }; |
457 | }; |
438 | |
458 | |
439 | int dns_cfg::next_uid; |
459 | int dns_cfg::next_uid; |
440 | |
460 | |
|
|
461 | void |
441 | void dns_cfg::reset (int clientid) |
462 | dns_cfg::reset (int clientid) |
442 | { |
463 | { |
443 | id1 = 'G'; |
464 | id1 = 'G'; |
444 | id2 = 'V'; |
465 | id2 = 'V'; |
445 | id3 = 'P'; |
466 | id3 = 'P'; |
446 | id4 = 'E'; |
467 | id4 = 'E'; |
… | |
… | |
460 | |
481 | |
461 | r3 = r4 = 0; |
482 | r3 = r4 = 0; |
462 | r4 = r5 = r6 = r7 = 0; |
483 | r4 = r5 = r6 = r7 = 0; |
463 | } |
484 | } |
464 | |
485 | |
|
|
486 | bool |
465 | bool dns_cfg::valid () |
487 | dns_cfg::valid () |
466 | { |
488 | { |
467 | // although the protocol itself allows for some configurability, |
489 | // although the protocol itself allows for some configurability, |
468 | // only the following encoding/decoding settings are implemented. |
490 | // only the following encoding/decoding settings are implemented. |
469 | return id1 == 'G' |
491 | return id1 == 'G' |
470 | && id2 == 'V' |
492 | && id2 == 'V' |
… | |
… | |
485 | u8 data [MAXSIZE - 6 * 2]; |
507 | u8 data [MAXSIZE - 6 * 2]; |
486 | |
508 | |
487 | int decode_label (char *data, int size, int &offs); |
509 | int decode_label (char *data, int size, int &offs); |
488 | }; |
510 | }; |
489 | |
511 | |
|
|
512 | int |
490 | int dns_packet::decode_label (char *data, int size, int &offs) |
513 | dns_packet::decode_label (char *data, int size, int &offs) |
491 | { |
514 | { |
492 | char *orig = data; |
515 | char *orig = data; |
493 | |
516 | |
494 | memset (data, 0, size); |
517 | memset (data, 0, size); |
495 | |
518 | |
… | |
… | |
521 | return data - orig; |
544 | return data - orig; |
522 | } |
545 | } |
523 | |
546 | |
524 | ///////////////////////////////////////////////////////////////////////////// |
547 | ///////////////////////////////////////////////////////////////////////////// |
525 | |
548 | |
|
|
549 | static |
|
|
550 | u16 next_id () |
|
|
551 | { |
526 | static u16 dns_id = 0; // TODO: should be per-vpn |
552 | static u16 dns_id = 0; // TODO: should be per-vpn |
527 | |
553 | |
528 | static u16 next_id () |
|
|
529 | { |
|
|
530 | if (!dns_id) |
554 | if (!dns_id) |
531 | dns_id = time (0); |
555 | dns_id = time (0); |
532 | |
556 | |
533 | // the simplest lsfr with periodicity 65535 i could find |
557 | // the simplest lsfr with periodicity 65535 i could find |
534 | dns_id = (dns_id << 1) |
558 | dns_id = (dns_id << 1) |
… | |
… | |
560 | vector<dns_rcv *> rcvpq; |
584 | vector<dns_rcv *> rcvpq; |
561 | |
585 | |
562 | byte_stream rcvdq; int rcvseq; int repseq; |
586 | byte_stream rcvdq; int rcvseq; int repseq; |
563 | byte_stream snddq; int sndseq; |
587 | byte_stream snddq; int sndseq; |
564 | |
588 | |
565 | void time_cb (ev::timer &w, int revents); ev::timer tw; |
589 | inline void time_cb (ev::timer &w, int revents); ev::timer tw; |
566 | void receive_rep (dns_rcv *r); |
590 | void receive_rep (dns_rcv *r); |
567 | |
591 | |
568 | dns_connection (connection *c); |
592 | dns_connection (connection *c); |
569 | ~dns_connection (); |
593 | ~dns_connection (); |
570 | }; |
594 | }; |
… | |
… | |
602 | dns_snd::~dns_snd () |
626 | dns_snd::~dns_snd () |
603 | { |
627 | { |
604 | delete pkt; |
628 | delete pkt; |
605 | } |
629 | } |
606 | |
630 | |
|
|
631 | static void |
607 | static void append_domain (dns_packet &pkt, int &offs, const char *domain) |
632 | append_domain (dns_packet &pkt, int &offs, const char *domain) |
608 | { |
633 | { |
609 | // add tunnel domain |
634 | // add tunnel domain |
610 | for (;;) |
635 | for (;;) |
611 | { |
636 | { |
612 | const char *end = strchr (domain, '.'); |
637 | const char *end = strchr (domain, '.'); |
… | |
… | |
625 | |
650 | |
626 | domain = end + 1; |
651 | domain = end + 1; |
627 | } |
652 | } |
628 | } |
653 | } |
629 | |
654 | |
|
|
655 | void |
630 | void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
656 | dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
631 | { |
657 | { |
632 | stdhdr = true; |
658 | stdhdr = true; |
633 | this->seqno = seqno; |
659 | this->seqno = seqno; |
634 | |
660 | |
635 | timeout = ev_now () + INITIAL_TIMEOUT; |
661 | timeout = ev_now () + INITIAL_TIMEOUT; |
… | |
… | |
673 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
699 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
674 | |
700 | |
675 | pkt->len = offs; |
701 | pkt->len = offs; |
676 | } |
702 | } |
677 | |
703 | |
|
|
704 | void |
678 | void dns_snd::gen_syn_req () |
705 | dns_snd::gen_syn_req () |
679 | { |
706 | { |
680 | timeout = ev_now () + INITIAL_SYN_TIMEOUT; |
707 | timeout = ev_now () + INITIAL_SYN_TIMEOUT; |
681 | |
708 | |
682 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
709 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
683 | pkt->qdcount = htons (1); |
710 | pkt->qdcount = htons (1); |
… | |
… | |
725 | |
752 | |
726 | dns_connection::dns_connection (connection *c) |
753 | dns_connection::dns_connection (connection *c) |
727 | : c (c) |
754 | : c (c) |
728 | , rcvdq (MAX_BACKLOG * 2) |
755 | , rcvdq (MAX_BACKLOG * 2) |
729 | , snddq (MAX_BACKLOG) |
756 | , snddq (MAX_BACKLOG) |
730 | , tw (this, &dns_connection::time_cb) |
|
|
731 | { |
757 | { |
|
|
758 | tw.set<dns_connection, &dns_connection::time_cb> (this); |
|
|
759 | |
732 | vpn = c->vpn; |
760 | vpn = c->vpn; |
733 | |
761 | |
734 | established = false; |
762 | established = false; |
735 | |
763 | |
736 | rcvseq = repseq = sndseq = 0; |
764 | rcvseq = repseq = sndseq = 0; |
… | |
… | |
747 | i != rcvpq.end (); |
775 | i != rcvpq.end (); |
748 | ++i) |
776 | ++i) |
749 | delete *i; |
777 | delete *i; |
750 | } |
778 | } |
751 | |
779 | |
|
|
780 | void |
752 | void dns_connection::receive_rep (dns_rcv *r) |
781 | dns_connection::receive_rep (dns_rcv *r) |
753 | { |
782 | { |
754 | if (r->datalen) |
783 | if (r->datalen) |
755 | { |
784 | { |
756 | last_received = ev_now (); |
785 | last_received = ev_now (); |
757 | tw (); |
786 | tw (); |