| 1 |
pcg |
1.1 |
/* |
| 2 |
|
|
vpn_dns.C -- handle the dns tunnel part of the protocol. |
| 3 |
pcg |
1.7 |
Copyright (C) 2003-2005 Marc Lehmann <gvpe@schmorp.de> |
| 4 |
pcg |
1.1 |
|
| 5 |
pcg |
1.7 |
This file is part of GVPE. |
| 6 |
|
|
|
| 7 |
|
|
GVPE is free software; you can redistribute it and/or modify |
| 8 |
pcg |
1.1 |
it under the terms of the GNU General Public License as published by |
| 9 |
|
|
the Free Software Foundation; either version 2 of the License, or |
| 10 |
|
|
(at your option) any later version. |
| 11 |
|
|
|
| 12 |
|
|
This program is distributed in the hope that it will be useful, |
| 13 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
|
|
GNU General Public License for more details. |
| 16 |
|
|
|
| 17 |
|
|
You should have received a copy of the GNU General Public License |
| 18 |
pcg |
1.7 |
along with gvpe; if not, write to the Free Software |
| 19 |
pcg |
1.1 |
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 |
|
|
*/ |
| 21 |
|
|
|
| 22 |
|
|
#include "config.h" |
| 23 |
|
|
|
| 24 |
|
|
#if ENABLE_DNS |
| 25 |
|
|
|
| 26 |
|
|
// dns processing is EXTREMELY ugly. For obvious(?) reasons. |
| 27 |
|
|
// it's a hack, use only in emergency situations please. |
| 28 |
|
|
|
| 29 |
|
|
#include <cstring> |
| 30 |
|
|
|
| 31 |
|
|
#include <sys/types.h> |
| 32 |
|
|
#include <sys/socket.h> |
| 33 |
|
|
#include <sys/wait.h> |
| 34 |
|
|
#include <sys/uio.h> |
| 35 |
|
|
#include <errno.h> |
| 36 |
|
|
#include <time.h> |
| 37 |
|
|
#include <unistd.h> |
| 38 |
|
|
#include <fcntl.h> |
| 39 |
|
|
|
| 40 |
|
|
#include <map> |
| 41 |
|
|
|
| 42 |
pcg |
1.8 |
#include <gmp.h> |
| 43 |
|
|
|
| 44 |
pcg |
1.1 |
#include "netcompat.h" |
| 45 |
|
|
|
| 46 |
|
|
#include "vpn.h" |
| 47 |
|
|
|
| 48 |
pcg |
1.15 |
#define MIN_POLL_INTERVAL .02 // how often to poll minimally when the server has data |
| 49 |
pcg |
1.10 |
#define MAX_POLL_INTERVAL 6. // how often to poll minimally when the server has no data |
| 50 |
|
|
#define ACTIVITY_INTERVAL 5. |
| 51 |
|
|
|
| 52 |
pcg |
1.15 |
#define INITIAL_TIMEOUT 1. // retry timeouts |
| 53 |
|
|
#define INITIAL_SYN_TIMEOUT 2. // retry timeout for initial syn |
| 54 |
pcg |
1.10 |
|
| 55 |
pcg |
1.15 |
#define MIN_SEND_INTERVAL 0.01 // wait at least this time between sending requests |
| 56 |
pcg |
1.10 |
#define MAX_SEND_INTERVAL 0.5 // optimistic? |
| 57 |
pcg |
1.1 |
|
| 58 |
pcg |
1.13 |
#define MAX_OUTSTANDING 40 // max. outstanding requests |
| 59 |
|
|
#define MAX_WINDOW 100 // max. for MAX_OUTSTANDING |
| 60 |
pcg |
1.15 |
#define MAX_BACKLOG (100*1024) // size of gvpe protocol backlog (bytes), must be > MAXSIZE |
| 61 |
pcg |
1.5 |
|
| 62 |
|
|
#define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well |
| 63 |
|
|
// 240 leaves about 4 bytes of server reply data |
| 64 |
pcg |
1.15 |
// every two request bytes less give room for one reply byte |
| 65 |
pcg |
1.5 |
|
| 66 |
pcg |
1.9 |
#define SEQNO_MASK 0xffff |
| 67 |
pcg |
1.8 |
#define SEQNO_EQ(a,b) ( 0 == ( ((a) ^ (b)) & SEQNO_MASK) ) |
| 68 |
pcg |
1.1 |
|
| 69 |
pcg |
1.8 |
#define MAX_LBL_SIZE 63 |
| 70 |
|
|
#define MAX_PKT_SIZE 512 |
| 71 |
pcg |
1.1 |
|
| 72 |
pcg |
1.10 |
#define RR_TYPE_A 1 |
| 73 |
|
|
#define RR_TYPE_NULL 10 |
| 74 |
|
|
#define RR_TYPE_TXT 16 |
| 75 |
pcg |
1.8 |
#define RR_TYPE_ANY 255 |
| 76 |
pcg |
1.10 |
|
| 77 |
|
|
#define RR_CLASS_IN 1 |
| 78 |
|
|
|
| 79 |
|
|
#define CMD_IP_1 207 |
| 80 |
|
|
#define CMD_IP_2 46 |
| 81 |
|
|
#define CMD_IP_3 236 |
| 82 |
|
|
#define CMD_IP_RST 29 |
| 83 |
|
|
#define CMD_IP_SYN 113 |
| 84 |
|
|
#define CMD_IP_REJ 32 |
| 85 |
pcg |
1.1 |
|
| 86 |
pcg |
1.8 |
// works for cmaps up to 255 (not 256!) |
| 87 |
|
|
struct charmap |
| 88 |
|
|
{ |
| 89 |
|
|
enum { INVALID = (u8)255 }; |
| 90 |
pcg |
1.1 |
|
| 91 |
pcg |
1.8 |
char encode [256]; // index => char |
| 92 |
|
|
u8 decode [256]; // char => index |
| 93 |
|
|
unsigned int size; |
| 94 |
pcg |
1.2 |
|
| 95 |
pcg |
1.8 |
charmap (const char *cmap); |
| 96 |
|
|
}; |
| 97 |
|
|
|
| 98 |
|
|
charmap::charmap (const char *cmap) |
| 99 |
|
|
{ |
| 100 |
|
|
char *enc = encode; |
| 101 |
|
|
u8 *dec = decode; |
| 102 |
pcg |
1.2 |
|
| 103 |
pcg |
1.8 |
memset (enc, (char) 0, 256); |
| 104 |
|
|
memset (dec, (char)INVALID, 256); |
| 105 |
pcg |
1.1 |
|
| 106 |
pcg |
1.8 |
for (size = 0; cmap [size]; size++) |
| 107 |
|
|
{ |
| 108 |
|
|
enc [size] = cmap [size]; |
| 109 |
|
|
dec [(u8)enc [size]] = size; |
| 110 |
|
|
} |
| 111 |
pcg |
1.1 |
|
| 112 |
pcg |
1.8 |
assert (size < 256); |
| 113 |
|
|
} |
| 114 |
pcg |
1.2 |
|
| 115 |
pcg |
1.8 |
#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 |
pcg |
1.2 |
|
| 119 |
pcg |
1.8 |
// ugly. minimum base is 16(!) |
| 120 |
|
|
struct basecoder |
| 121 |
pcg |
1.2 |
{ |
| 122 |
pcg |
1.8 |
charmap cmap; |
| 123 |
|
|
unsigned int enc_len [MAX_DEC_LEN]; |
| 124 |
|
|
unsigned int dec_len [MAX_ENC_LEN]; |
| 125 |
pcg |
1.2 |
|
| 126 |
pcg |
1.8 |
unsigned int encode_len (unsigned int len); |
| 127 |
|
|
unsigned int decode_len (unsigned int len); |
| 128 |
|
|
|
| 129 |
|
|
unsigned int encode (char *dst, u8 *src, unsigned int len); |
| 130 |
|
|
unsigned int decode (u8 *dst, char *src, unsigned int len); |
| 131 |
|
|
|
| 132 |
|
|
basecoder (const char *cmap); |
| 133 |
|
|
}; |
| 134 |
|
|
|
| 135 |
|
|
basecoder::basecoder (const char *cmap) |
| 136 |
|
|
: cmap (cmap) |
| 137 |
|
|
{ |
| 138 |
|
|
for (unsigned int len = 0; len < MAX_DEC_LEN; ++len) |
| 139 |
|
|
{ |
| 140 |
|
|
u8 src [MAX_DEC_LEN]; |
| 141 |
|
|
u8 dst [MAX_ENC_LEN]; |
| 142 |
pcg |
1.2 |
|
| 143 |
pcg |
1.8 |
memset (src, 255, len); |
| 144 |
pcg |
1.2 |
|
| 145 |
pcg |
1.8 |
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) |
| 160 |
|
|
{ |
| 161 |
|
|
return enc_len [len]; |
| 162 |
|
|
} |
| 163 |
pcg |
1.2 |
|
| 164 |
pcg |
1.8 |
unsigned int basecoder::decode_len (unsigned int len) |
| 165 |
pcg |
1.2 |
{ |
| 166 |
pcg |
1.8 |
while (len && !dec_len [len]) |
| 167 |
|
|
--len; |
| 168 |
|
|
|
| 169 |
|
|
return dec_len [len]; |
| 170 |
pcg |
1.2 |
} |
| 171 |
|
|
|
| 172 |
pcg |
1.8 |
unsigned int basecoder::encode (char *dst, u8 *src, unsigned int len) |
| 173 |
pcg |
1.2 |
{ |
| 174 |
pcg |
1.10 |
if (!len || len > MAX_DEC_LEN) |
| 175 |
pcg |
1.8 |
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 |
pcg |
1.2 |
|
| 189 |
pcg |
1.8 |
while (n < plen) |
| 190 |
pcg |
1.2 |
{ |
| 191 |
pcg |
1.8 |
*dst++ = cmap.encode [0]; |
| 192 |
|
|
plen--; |
| 193 |
pcg |
1.2 |
} |
| 194 |
|
|
|
| 195 |
pcg |
1.8 |
for (unsigned int i = n - plen; i < n; ++i) |
| 196 |
|
|
*dst++ = cmap.encode [dst_ [i]]; |
| 197 |
pcg |
1.4 |
|
| 198 |
pcg |
1.8 |
return elen; |
| 199 |
pcg |
1.2 |
} |
| 200 |
|
|
|
| 201 |
pcg |
1.8 |
unsigned int basecoder::decode (u8 *dst, char *src, unsigned int len) |
| 202 |
pcg |
1.2 |
{ |
| 203 |
pcg |
1.10 |
if (!len || len > MAX_ENC_LEN) |
| 204 |
pcg |
1.8 |
return 0; |
| 205 |
|
|
|
| 206 |
|
|
u8 src_ [MAX_ENC_LEN]; |
| 207 |
|
|
unsigned int elen = 0; |
| 208 |
pcg |
1.2 |
|
| 209 |
|
|
while (len--) |
| 210 |
|
|
{ |
| 211 |
pcg |
1.8 |
u8 val = cmap.decode [(u8)*src++]; |
| 212 |
pcg |
1.2 |
|
| 213 |
pcg |
1.8 |
if (val != charmap::INVALID) |
| 214 |
|
|
src_ [elen++] = val; |
| 215 |
|
|
} |
| 216 |
|
|
|
| 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 |
pcg |
1.2 |
|
| 224 |
pcg |
1.8 |
n = mpn_set_str (m, src_, elen, cmap.size); |
| 225 |
|
|
n = mpn_get_str (dst_, 256, m, n); |
| 226 |
pcg |
1.2 |
|
| 227 |
pcg |
1.8 |
if (n < dlen) |
| 228 |
|
|
{ |
| 229 |
|
|
memset (dst, 0, dlen - n); |
| 230 |
|
|
memcpy (dst + dlen - n, dst_, n); |
| 231 |
pcg |
1.2 |
} |
| 232 |
pcg |
1.8 |
else |
| 233 |
|
|
memcpy (dst, dst_ + n - dlen, dlen); |
| 234 |
pcg |
1.4 |
|
| 235 |
pcg |
1.8 |
return dlen; |
| 236 |
|
|
} |
| 237 |
|
|
|
| 238 |
|
|
#if 0 |
| 239 |
|
|
struct test { test (); } test; |
| 240 |
|
|
|
| 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++) |
| 250 |
|
|
{ |
| 251 |
|
|
int elen = cdc.encode (enc, in, i); |
| 252 |
|
|
int dlen = cdc.decode (dec, enc, elen); |
| 253 |
|
|
|
| 254 |
|
|
printf ("%d>%d>%d (%s>%s)\n", i, elen, dlen, enc, dec); |
| 255 |
|
|
} |
| 256 |
|
|
abort (); |
| 257 |
|
|
} |
| 258 |
|
|
#endif |
| 259 |
|
|
|
| 260 |
pcg |
1.10 |
//static basecoder cdc64 ("_dDpPhHzZrR06QqMmjJkKBb34TtSsvVlL81xXaAeEFf92WwGgYyoO57UucCNniI-"); |
| 261 |
pcg |
1.8 |
//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]; |
| 287 |
pcg |
1.4 |
} |
| 288 |
|
|
|
| 289 |
|
|
///////////////////////////////////////////////////////////////////////////// |
| 290 |
|
|
|
| 291 |
|
|
struct byte_stream |
| 292 |
|
|
{ |
| 293 |
|
|
u8 *data; |
| 294 |
|
|
int maxsize; |
| 295 |
|
|
int fill; |
| 296 |
|
|
|
| 297 |
|
|
byte_stream (int maxsize); |
| 298 |
|
|
~byte_stream (); |
| 299 |
|
|
|
| 300 |
|
|
bool empty () { return !fill; } |
| 301 |
|
|
int size () { return fill; } |
| 302 |
|
|
|
| 303 |
pcg |
1.5 |
bool put (u8 *data, unsigned int datalen); |
| 304 |
pcg |
1.4 |
bool put (vpn_packet *pkt); |
| 305 |
|
|
vpn_packet *get (); |
| 306 |
|
|
|
| 307 |
|
|
u8 *begin () { return data; } |
| 308 |
|
|
void remove (int count); |
| 309 |
|
|
}; |
| 310 |
|
|
|
| 311 |
|
|
byte_stream::byte_stream (int maxsize) |
| 312 |
|
|
: maxsize (maxsize), fill (0) |
| 313 |
|
|
{ |
| 314 |
|
|
data = new u8 [maxsize]; |
| 315 |
|
|
} |
| 316 |
|
|
|
| 317 |
|
|
byte_stream::~byte_stream () |
| 318 |
|
|
{ |
| 319 |
|
|
delete data; |
| 320 |
|
|
} |
| 321 |
|
|
|
| 322 |
|
|
void byte_stream::remove (int count) |
| 323 |
|
|
{ |
| 324 |
|
|
if (count > fill) |
| 325 |
|
|
abort (); |
| 326 |
|
|
|
| 327 |
|
|
memmove (data, data + count, fill -= count); |
| 328 |
|
|
} |
| 329 |
|
|
|
| 330 |
pcg |
1.5 |
bool byte_stream::put (u8 *data, unsigned int datalen) |
| 331 |
|
|
{ |
| 332 |
|
|
if (maxsize - fill < datalen) |
| 333 |
|
|
return false; |
| 334 |
|
|
|
| 335 |
|
|
memcpy (this->data + fill, data, datalen); fill += datalen; |
| 336 |
|
|
|
| 337 |
|
|
return true; |
| 338 |
|
|
} |
| 339 |
|
|
|
| 340 |
pcg |
1.4 |
bool byte_stream::put (vpn_packet *pkt) |
| 341 |
|
|
{ |
| 342 |
|
|
if (maxsize - fill < pkt->len + 2) |
| 343 |
|
|
return false; |
| 344 |
|
|
|
| 345 |
|
|
data [fill++] = pkt->len >> 8; |
| 346 |
|
|
data [fill++] = pkt->len; |
| 347 |
|
|
|
| 348 |
pcg |
1.10 |
memcpy (data + fill, pkt->at (0), pkt->len); fill += pkt->len; |
| 349 |
pcg |
1.4 |
|
| 350 |
|
|
return true; |
| 351 |
|
|
} |
| 352 |
|
|
|
| 353 |
|
|
vpn_packet *byte_stream::get () |
| 354 |
|
|
{ |
| 355 |
pcg |
1.12 |
unsigned int len = (data [0] << 8) | data [1]; |
| 356 |
pcg |
1.4 |
|
| 357 |
pcg |
1.5 |
if (len > MAXSIZE && fill >= 2) |
| 358 |
|
|
abort (); // TODO handle this gracefully, connection reset |
| 359 |
|
|
|
| 360 |
pcg |
1.4 |
if (fill < len + 2) |
| 361 |
|
|
return 0; |
| 362 |
|
|
|
| 363 |
|
|
vpn_packet *pkt = new vpn_packet; |
| 364 |
pcg |
1.5 |
|
| 365 |
|
|
pkt->len = len; |
| 366 |
pcg |
1.10 |
memcpy (pkt->at (0), data + 2, len); |
| 367 |
pcg |
1.4 |
remove (len + 2); |
| 368 |
|
|
|
| 369 |
|
|
return pkt; |
| 370 |
pcg |
1.2 |
} |
| 371 |
|
|
|
| 372 |
pcg |
1.3 |
///////////////////////////////////////////////////////////////////////////// |
| 373 |
|
|
|
| 374 |
pcg |
1.2 |
#define FLAG_QUERY ( 0 << 15) |
| 375 |
|
|
#define FLAG_RESPONSE ( 1 << 15) |
| 376 |
pcg |
1.9 |
#define FLAG_OP_MASK (15 << 11) |
| 377 |
pcg |
1.2 |
#define FLAG_OP_QUERY ( 0 << 11) |
| 378 |
|
|
#define FLAG_AA ( 1 << 10) |
| 379 |
|
|
#define FLAG_TC ( 1 << 9) |
| 380 |
|
|
#define FLAG_RD ( 1 << 8) |
| 381 |
|
|
#define FLAG_RA ( 1 << 7) |
| 382 |
pcg |
1.5 |
#define FLAG_AUTH ( 1 << 5) |
| 383 |
pcg |
1.2 |
#define FLAG_RCODE_MASK (15 << 0) |
| 384 |
|
|
#define FLAG_RCODE_OK ( 0 << 0) |
| 385 |
|
|
#define FLAG_RCODE_FORMERR ( 1 << 0) |
| 386 |
|
|
#define FLAG_RCODE_SERVFAIL ( 2 << 0) |
| 387 |
|
|
#define FLAG_RCODE_NXDOMAIN ( 3 << 0) |
| 388 |
|
|
#define FLAG_RCODE_REFUSED ( 5 << 0) |
| 389 |
|
|
|
| 390 |
|
|
#define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) |
| 391 |
pcg |
1.5 |
#define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA) |
| 392 |
pcg |
1.2 |
|
| 393 |
pcg |
1.10 |
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 |
|
|
|
| 450 |
pcg |
1.2 |
struct dns_packet : net_packet |
| 451 |
|
|
{ |
| 452 |
|
|
u16 id; |
| 453 |
|
|
u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
| 454 |
|
|
u16 qdcount, ancount, nscount, arcount; |
| 455 |
|
|
|
| 456 |
|
|
u8 data[MAXSIZE - 6 * 2]; |
| 457 |
|
|
|
| 458 |
|
|
int decode_label (char *data, int size, int &offs); |
| 459 |
|
|
}; |
| 460 |
|
|
|
| 461 |
|
|
int dns_packet::decode_label (char *data, int size, int &offs) |
| 462 |
|
|
{ |
| 463 |
|
|
char *orig = data; |
| 464 |
|
|
|
| 465 |
|
|
memset (data, 0, size); |
| 466 |
|
|
|
| 467 |
|
|
while (offs < size - 1) |
| 468 |
|
|
{ |
| 469 |
|
|
u8 len = (*this)[offs++]; |
| 470 |
|
|
|
| 471 |
|
|
if (!len) |
| 472 |
|
|
break; |
| 473 |
|
|
else if (len < 64) |
| 474 |
|
|
{ |
| 475 |
|
|
if (size < len + 1 || offs + len >= MAXSIZE - 1) |
| 476 |
|
|
break; |
| 477 |
|
|
|
| 478 |
|
|
memcpy (data, &((*this)[offs]), len); |
| 479 |
|
|
|
| 480 |
|
|
data += len; size -= len; offs += len; |
| 481 |
|
|
*data++ = '.'; size--; |
| 482 |
|
|
} |
| 483 |
|
|
else |
| 484 |
|
|
{ |
| 485 |
|
|
int offs2 = ((len & 63) << 8) + (*this)[offs++]; |
| 486 |
|
|
|
| 487 |
|
|
data += decode_label (data, size, offs2); |
| 488 |
|
|
break; |
| 489 |
|
|
} |
| 490 |
|
|
} |
| 491 |
|
|
|
| 492 |
|
|
return data - orig; |
| 493 |
|
|
} |
| 494 |
|
|
|
| 495 |
pcg |
1.3 |
///////////////////////////////////////////////////////////////////////////// |
| 496 |
|
|
|
| 497 |
pcg |
1.10 |
struct dns_snd |
| 498 |
pcg |
1.3 |
{ |
| 499 |
|
|
dns_packet *pkt; |
| 500 |
pcg |
1.10 |
tstamp timeout, sent; |
| 501 |
pcg |
1.3 |
int retry; |
| 502 |
pcg |
1.9 |
struct dns_connection *dns; |
| 503 |
pcg |
1.5 |
int seqno; |
| 504 |
pcg |
1.3 |
|
| 505 |
pcg |
1.9 |
void gen_stream_req (int seqno, byte_stream &stream); |
| 506 |
pcg |
1.10 |
void gen_syn_req (const dns_cfg &cfg); |
| 507 |
pcg |
1.11 |
|
| 508 |
|
|
dns_snd (dns_connection *dns); |
| 509 |
|
|
~dns_snd (); |
| 510 |
pcg |
1.3 |
}; |
| 511 |
|
|
|
| 512 |
pcg |
1.5 |
static u16 dns_id = 12098; // TODO: should be per-vpn |
| 513 |
|
|
|
| 514 |
|
|
static u16 next_id () |
| 515 |
pcg |
1.3 |
{ |
| 516 |
pcg |
1.5 |
// the simplest lsfr with periodicity 65535 i could find |
| 517 |
|
|
dns_id = (dns_id << 1) |
| 518 |
|
|
| (((dns_id >> 1) |
| 519 |
|
|
^ (dns_id >> 2) |
| 520 |
|
|
^ (dns_id >> 4) |
| 521 |
|
|
^ (dns_id >> 15)) & 1); |
| 522 |
pcg |
1.3 |
|
| 523 |
pcg |
1.5 |
return dns_id; |
| 524 |
|
|
} |
| 525 |
pcg |
1.3 |
|
| 526 |
pcg |
1.10 |
dns_snd::dns_snd (dns_connection *dns) |
| 527 |
pcg |
1.9 |
: dns (dns) |
| 528 |
pcg |
1.3 |
{ |
| 529 |
pcg |
1.10 |
timeout = 0; |
| 530 |
pcg |
1.3 |
retry = 0; |
| 531 |
pcg |
1.10 |
seqno = 0; |
| 532 |
pcg |
1.11 |
sent = NOW; |
| 533 |
pcg |
1.3 |
|
| 534 |
|
|
pkt = new dns_packet; |
| 535 |
|
|
|
| 536 |
pcg |
1.5 |
pkt->id = next_id (); |
| 537 |
|
|
} |
| 538 |
|
|
|
| 539 |
pcg |
1.11 |
dns_snd::~dns_snd () |
| 540 |
|
|
{ |
| 541 |
|
|
delete pkt; |
| 542 |
|
|
} |
| 543 |
|
|
|
| 544 |
pcg |
1.10 |
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 |
|
|
|
| 567 |
|
|
void dns_snd::gen_stream_req (int seqno, byte_stream &stream) |
| 568 |
pcg |
1.5 |
{ |
| 569 |
|
|
this->seqno = seqno; |
| 570 |
|
|
|
| 571 |
pcg |
1.10 |
timeout = NOW + INITIAL_TIMEOUT; |
| 572 |
|
|
|
| 573 |
pcg |
1.5 |
pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
| 574 |
pcg |
1.4 |
pkt->qdcount = htons (1); |
| 575 |
pcg |
1.3 |
|
| 576 |
pcg |
1.4 |
int offs = 6*2; |
| 577 |
pcg |
1.8 |
int dlen = MAX_DOMAIN_SIZE - (strlen (THISNODE->domain) + 2); |
| 578 |
pcg |
1.5 |
// MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, |
| 579 |
|
|
// so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra |
| 580 |
|
|
|
| 581 |
pcg |
1.8 |
char enc[256], *encp = enc; |
| 582 |
|
|
encode_header (enc, THISNODE->id, seqno); |
| 583 |
pcg |
1.4 |
|
| 584 |
pcg |
1.8 |
int datalen = cdc62.decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE - HDRSIZE); |
| 585 |
pcg |
1.4 |
|
| 586 |
pcg |
1.9 |
if (datalen > stream.size ()) |
| 587 |
|
|
datalen = stream.size (); |
| 588 |
pcg |
1.8 |
|
| 589 |
pcg |
1.9 |
int enclen = cdc62.encode (enc + HDRSIZE, stream.begin (), datalen) + HDRSIZE; |
| 590 |
|
|
stream.remove (datalen); |
| 591 |
pcg |
1.4 |
|
| 592 |
pcg |
1.5 |
while (enclen) |
| 593 |
|
|
{ |
| 594 |
|
|
int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE; |
| 595 |
|
|
|
| 596 |
|
|
(*pkt)[offs++] = lbllen; |
| 597 |
|
|
memcpy (pkt->at (offs), encp, lbllen); |
| 598 |
pcg |
1.4 |
|
| 599 |
pcg |
1.5 |
offs += lbllen; |
| 600 |
|
|
encp += lbllen; |
| 601 |
pcg |
1.4 |
|
| 602 |
pcg |
1.5 |
enclen -= lbllen; |
| 603 |
pcg |
1.4 |
} |
| 604 |
|
|
|
| 605 |
pcg |
1.10 |
append_domain (*pkt, offs, THISNODE->domain); |
| 606 |
pcg |
1.3 |
|
| 607 |
pcg |
1.10 |
(*pkt)[offs++] = 0; |
| 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 |
pcg |
1.4 |
|
| 618 |
pcg |
1.10 |
pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
| 619 |
|
|
pkt->qdcount = htons (1); |
| 620 |
pcg |
1.4 |
|
| 621 |
pcg |
1.10 |
int offs = 6*2; |
| 622 |
pcg |
1.4 |
|
| 623 |
pcg |
1.10 |
int elen = cdc26.encode ((char *)pkt->at (offs + 1), (u8 *)&cfg, sizeof (dns_cfg)); |
| 624 |
pcg |
1.4 |
|
| 625 |
pcg |
1.10 |
assert (elen <= MAX_LBL_SIZE); |
| 626 |
pcg |
1.4 |
|
| 627 |
pcg |
1.10 |
(*pkt)[offs] = elen; |
| 628 |
|
|
offs += elen + 1; |
| 629 |
|
|
append_domain (*pkt, offs, THISNODE->domain); |
| 630 |
pcg |
1.4 |
|
| 631 |
|
|
(*pkt)[offs++] = 0; |
| 632 |
pcg |
1.10 |
(*pkt)[offs++] = RR_TYPE_A >> 8; (*pkt)[offs++] = RR_TYPE_A; |
| 633 |
pcg |
1.4 |
(*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
| 634 |
|
|
|
| 635 |
|
|
pkt->len = offs; |
| 636 |
pcg |
1.5 |
} |
| 637 |
|
|
|
| 638 |
|
|
struct dns_rcv |
| 639 |
|
|
{ |
| 640 |
|
|
int seqno; |
| 641 |
|
|
dns_packet *pkt; // reply packet |
| 642 |
|
|
u8 data [MAXSIZE]; // actually part of the reply packet... |
| 643 |
|
|
int datalen; |
| 644 |
|
|
|
| 645 |
pcg |
1.6 |
dns_rcv (int seqno, u8 *data, int datalen); |
| 646 |
pcg |
1.5 |
~dns_rcv (); |
| 647 |
|
|
}; |
| 648 |
|
|
|
| 649 |
pcg |
1.6 |
dns_rcv::dns_rcv (int seqno, u8 *data, int datalen) |
| 650 |
pcg |
1.5 |
: seqno (seqno), pkt (new dns_packet), datalen (datalen) |
| 651 |
|
|
{ |
| 652 |
|
|
memcpy (this->data, data, datalen); |
| 653 |
|
|
} |
| 654 |
pcg |
1.4 |
|
| 655 |
pcg |
1.5 |
dns_rcv::~dns_rcv () |
| 656 |
|
|
{ |
| 657 |
|
|
delete pkt; |
| 658 |
pcg |
1.3 |
} |
| 659 |
|
|
|
| 660 |
|
|
///////////////////////////////////////////////////////////////////////////// |
| 661 |
pcg |
1.9 |
|
| 662 |
|
|
struct dns_connection |
| 663 |
|
|
{ |
| 664 |
|
|
connection *c; |
| 665 |
|
|
struct vpn *vpn; |
| 666 |
|
|
|
| 667 |
pcg |
1.10 |
dns_cfg cfg; |
| 668 |
|
|
|
| 669 |
|
|
bool established; |
| 670 |
|
|
|
| 671 |
|
|
tstamp last_received; |
| 672 |
|
|
tstamp last_sent; |
| 673 |
|
|
double poll_interval, send_interval; |
| 674 |
|
|
|
| 675 |
pcg |
1.9 |
vector<dns_rcv *> rcvpq; |
| 676 |
|
|
|
| 677 |
pcg |
1.10 |
byte_stream rcvdq; int rcvseq; |
| 678 |
|
|
byte_stream snddq; int sndseq; |
| 679 |
pcg |
1.9 |
|
| 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 (); |
| 685 |
|
|
}; |
| 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 |
pcg |
1.10 |
established = false; |
| 696 |
|
|
|
| 697 |
pcg |
1.9 |
rcvseq = sndseq = 0; |
| 698 |
pcg |
1.10 |
|
| 699 |
|
|
last_sent = last_received = 0; |
| 700 |
|
|
poll_interval = MIN_POLL_INTERVAL; |
| 701 |
|
|
send_interval = 0.2; // starting rate |
| 702 |
pcg |
1.9 |
} |
| 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 |
pcg |
1.3 |
|
| 712 |
pcg |
1.10 |
void dns_connection::receive_rep (dns_rcv *r) |
| 713 |
pcg |
1.2 |
{ |
| 714 |
pcg |
1.10 |
if (r->datalen) |
| 715 |
|
|
{ |
| 716 |
|
|
last_received = NOW; |
| 717 |
|
|
tw.trigger (); |
| 718 |
|
|
|
| 719 |
pcg |
1.12 |
poll_interval = send_interval; |
| 720 |
pcg |
1.10 |
} |
| 721 |
|
|
else |
| 722 |
|
|
{ |
| 723 |
|
|
poll_interval *= 1.1; |
| 724 |
|
|
if (poll_interval > MAX_POLL_INTERVAL) |
| 725 |
|
|
poll_interval = MAX_POLL_INTERVAL; |
| 726 |
|
|
} |
| 727 |
pcg |
1.2 |
|
| 728 |
pcg |
1.9 |
rcvpq.push_back (r); |
| 729 |
pcg |
1.5 |
|
| 730 |
|
|
redo: |
| 731 |
|
|
|
| 732 |
pcg |
1.8 |
// find next packet |
| 733 |
pcg |
1.9 |
for (vector<dns_rcv *>::iterator i = rcvpq.end (); i-- != rcvpq.begin (); ) |
| 734 |
|
|
if (SEQNO_EQ (rcvseq, (*i)->seqno)) |
| 735 |
pcg |
1.5 |
{ |
| 736 |
pcg |
1.8 |
// enter the packet into our input stream |
| 737 |
|
|
r = *i; |
| 738 |
|
|
|
| 739 |
|
|
// remove the oldest packet, look forward, as it's oldest first |
| 740 |
pcg |
1.9 |
for (vector<dns_rcv *>::iterator j = rcvpq.begin (); j != rcvpq.end (); ++j) |
| 741 |
|
|
if (SEQNO_EQ ((*j)->seqno, rcvseq - MAX_WINDOW)) |
| 742 |
pcg |
1.8 |
{ |
| 743 |
|
|
delete *j; |
| 744 |
pcg |
1.9 |
rcvpq.erase (j); |
| 745 |
pcg |
1.8 |
break; |
| 746 |
|
|
} |
| 747 |
pcg |
1.5 |
|
| 748 |
pcg |
1.9 |
rcvseq = (rcvseq + 1) & SEQNO_MASK; |
| 749 |
pcg |
1.5 |
|
| 750 |
pcg |
1.9 |
if (!rcvdq.put (r->data, r->datalen)) |
| 751 |
pcg |
1.5 |
abort (); // MUST never overflow, can be caused by data corruption, TODO |
| 752 |
|
|
|
| 753 |
pcg |
1.9 |
while (vpn_packet *pkt = rcvdq.get ()) |
| 754 |
pcg |
1.5 |
{ |
| 755 |
|
|
sockinfo si; |
| 756 |
|
|
si.host = 0; si.port = 0; si.prot = PROT_DNSv4; |
| 757 |
|
|
|
| 758 |
|
|
vpn->recv_vpn_packet (pkt, si); |
| 759 |
pcg |
1.11 |
|
| 760 |
|
|
delete pkt; |
| 761 |
pcg |
1.5 |
} |
| 762 |
pcg |
1.8 |
|
| 763 |
|
|
// check for further packets |
| 764 |
pcg |
1.5 |
goto redo; |
| 765 |
|
|
} |
| 766 |
|
|
} |
| 767 |
|
|
|
| 768 |
pcg |
1.10 |
void |
| 769 |
|
|
vpn::dnsv4_server (dns_packet &pkt) |
| 770 |
pcg |
1.2 |
{ |
| 771 |
pcg |
1.10 |
u16 flags = ntohs (pkt.flags); |
| 772 |
pcg |
1.2 |
|
| 773 |
pcg |
1.4 |
int offs = 6 * 2; // skip header |
| 774 |
pcg |
1.2 |
|
| 775 |
pcg |
1.10 |
pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
| 776 |
pcg |
1.2 |
|
| 777 |
pcg |
1.10 |
if (0 == (flags & (FLAG_RESPONSE | FLAG_OP_MASK)) |
| 778 |
|
|
&& pkt.qdcount == htons (1)) |
| 779 |
pcg |
1.4 |
{ |
| 780 |
|
|
char qname[MAXSIZE]; |
| 781 |
pcg |
1.10 |
int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
| 782 |
pcg |
1.4 |
|
| 783 |
pcg |
1.10 |
u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
| 784 |
|
|
u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
| 785 |
pcg |
1.2 |
|
| 786 |
pcg |
1.10 |
pkt.qdcount = htons (1); |
| 787 |
|
|
pkt.ancount = 0; |
| 788 |
|
|
pkt.nscount = 0; // should be self, as other nameservers reply like this |
| 789 |
|
|
pkt.arcount = 0; // a record for self, as other nameservers reply like this |
| 790 |
pcg |
1.2 |
|
| 791 |
pcg |
1.16 |
pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_SERVFAIL); |
| 792 |
pcg |
1.2 |
|
| 793 |
pcg |
1.4 |
int dlen = strlen (THISNODE->domain); |
| 794 |
pcg |
1.2 |
|
| 795 |
pcg |
1.4 |
if (qclass == RR_CLASS_IN |
| 796 |
pcg |
1.10 |
&& qlen > dlen + 1 |
| 797 |
pcg |
1.4 |
&& !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
| 798 |
pcg |
1.2 |
{ |
| 799 |
pcg |
1.10 |
// now generate reply |
| 800 |
|
|
pkt.ancount = htons (1); // one answer RR |
| 801 |
|
|
pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
| 802 |
|
|
|
| 803 |
|
|
if ((qtype == RR_TYPE_ANY |
| 804 |
|
|
|| qtype == RR_TYPE_TXT |
| 805 |
|
|
|| qtype == RR_TYPE_NULL) |
| 806 |
|
|
&& qlen > dlen + 1 + HDRSIZE) |
| 807 |
pcg |
1.5 |
{ |
| 808 |
pcg |
1.10 |
// correct class, domain: parse |
| 809 |
|
|
int client, seqno; |
| 810 |
|
|
decode_header (qname, client, seqno); |
| 811 |
pcg |
1.5 |
|
| 812 |
pcg |
1.10 |
u8 data[MAXSIZE]; |
| 813 |
|
|
int datalen = cdc62.decode (data, qname + HDRSIZE, qlen - (dlen + 1 + HDRSIZE)); |
| 814 |
pcg |
1.9 |
|
| 815 |
pcg |
1.10 |
if (0 < client && client <= conns.size ()) |
| 816 |
|
|
{ |
| 817 |
|
|
connection *c = conns [client - 1]; |
| 818 |
|
|
dns_connection *dns = c->dns; |
| 819 |
|
|
dns_rcv *rcv; |
| 820 |
|
|
|
| 821 |
|
|
if (dns) |
| 822 |
|
|
{ |
| 823 |
|
|
for (vector<dns_rcv *>::iterator i = dns->rcvpq.end (); i-- != dns->rcvpq.begin (); ) |
| 824 |
|
|
if (SEQNO_EQ ((*i)->seqno, seqno)) |
| 825 |
|
|
{ |
| 826 |
|
|
// already seen that request: simply reply with the cached reply |
| 827 |
|
|
dns_rcv *r = *i; |
| 828 |
|
|
|
| 829 |
pcg |
1.14 |
slog (L_DEBUG, "DUPLICATE %d\n", htons (r->pkt->id)); |
| 830 |
pcg |
1.10 |
|
| 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 |
pcg |
1.11 |
pkt [offs++] = rtype >> 8; pkt [offs++] = rtype; // type |
| 845 |
|
|
pkt [offs++] = RR_CLASS_IN >> 8; pkt [offs++] = RR_CLASS_IN; // class |
| 846 |
pcg |
1.10 |
pkt [offs++] = 0; pkt [offs++] = 0; |
| 847 |
pcg |
1.11 |
pkt [offs++] = 0; pkt [offs++] = dns ? dns->cfg.def_ttl : 0; // TTL |
| 848 |
pcg |
1.10 |
|
| 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 |
|
|
{ |
| 859 |
|
|
int txtlen = dlen <= 255 ? dlen - 1 : 255; |
| 860 |
|
|
|
| 861 |
|
|
if (txtlen > dns->snddq.size ()) |
| 862 |
|
|
txtlen = dns->snddq.size (); |
| 863 |
|
|
|
| 864 |
|
|
pkt[offs++] = txtlen; |
| 865 |
|
|
memcpy (pkt.at (offs), dns->snddq.begin (), txtlen); |
| 866 |
|
|
offs += txtlen; |
| 867 |
|
|
dns->snddq.remove (txtlen); |
| 868 |
|
|
|
| 869 |
|
|
dlen -= txtlen + 1; |
| 870 |
|
|
} |
| 871 |
|
|
|
| 872 |
|
|
// avoid empty TXT rdata |
| 873 |
|
|
if (offs == rdlen_offs) |
| 874 |
|
|
pkt[offs++] = 0; |
| 875 |
pcg |
1.15 |
|
| 876 |
|
|
slog (L_NOISE, "snddq %d", dns->snddq.size ()); |
| 877 |
pcg |
1.10 |
} |
| 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 |
pcg |
1.9 |
|
| 897 |
pcg |
1.10 |
duplicate_request: ; |
| 898 |
|
|
} |
| 899 |
|
|
else |
| 900 |
|
|
pkt.flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
| 901 |
|
|
} |
| 902 |
|
|
else if (qtype == RR_TYPE_A |
| 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 |
pcg |
1.4 |
|
| 909 |
pcg |
1.10 |
pkt [offs++] = 0xc0; pkt [offs++] = 6 * 2; // refer to name in query section |
| 910 |
pcg |
1.2 |
|
| 911 |
pcg |
1.10 |
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 |
pcg |
1.2 |
|
| 917 |
pcg |
1.10 |
slog (L_INFO, _("DNS tunnel: client %d tries to connect"), client); |
| 918 |
pcg |
1.2 |
|
| 919 |
pcg |
1.10 |
pkt [offs++] = CMD_IP_1; pkt [offs++] = CMD_IP_2; pkt [offs++] = CMD_IP_3; |
| 920 |
|
|
pkt [offs++] = CMD_IP_REJ; |
| 921 |
pcg |
1.2 |
|
| 922 |
pcg |
1.10 |
if (0 < client && client <= conns.size ()) |
| 923 |
pcg |
1.5 |
{ |
| 924 |
pcg |
1.10 |
connection *c = conns [client - 1]; |
| 925 |
pcg |
1.2 |
|
| 926 |
pcg |
1.10 |
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 |
pcg |
1.5 |
} |
| 935 |
|
|
} |
| 936 |
pcg |
1.2 |
} |
| 937 |
pcg |
1.6 |
|
| 938 |
pcg |
1.10 |
pkt.len = offs; |
| 939 |
pcg |
1.4 |
} |
| 940 |
|
|
} |
| 941 |
|
|
|
| 942 |
|
|
void |
| 943 |
pcg |
1.10 |
vpn::dnsv4_client (dns_packet &pkt) |
| 944 |
pcg |
1.4 |
{ |
| 945 |
pcg |
1.10 |
u16 flags = ntohs (pkt.flags); |
| 946 |
pcg |
1.4 |
int offs = 6 * 2; // skip header |
| 947 |
|
|
|
| 948 |
pcg |
1.10 |
pkt.qdcount = ntohs (pkt.qdcount); |
| 949 |
|
|
pkt.ancount = ntohs (pkt.ancount); |
| 950 |
pcg |
1.4 |
|
| 951 |
pcg |
1.5 |
// go through our request list and find the corresponding request |
| 952 |
pcg |
1.10 |
for (vector<dns_snd *>::iterator i = dns_sndpq.begin (); |
| 953 |
pcg |
1.4 |
i != dns_sndpq.end (); |
| 954 |
|
|
++i) |
| 955 |
pcg |
1.10 |
if ((*i)->pkt->id == pkt.id) |
| 956 |
pcg |
1.4 |
{ |
| 957 |
pcg |
1.9 |
dns_connection *dns = (*i)->dns; |
| 958 |
pcg |
1.12 |
connection *c = dns->c; |
| 959 |
pcg |
1.5 |
int seqno = (*i)->seqno; |
| 960 |
|
|
u8 data[MAXSIZE], *datap = data; |
| 961 |
|
|
|
| 962 |
pcg |
1.10 |
if ((*i)->retry) |
| 963 |
|
|
{ |
| 964 |
pcg |
1.12 |
dns->send_interval *= 1.001; |
| 965 |
|
|
if (dns->send_interval > MAX_SEND_INTERVAL) |
| 966 |
pcg |
1.10 |
dns->send_interval = MAX_SEND_INTERVAL; |
| 967 |
|
|
} |
| 968 |
|
|
else |
| 969 |
|
|
{ |
| 970 |
pcg |
1.12 |
#if 1 |
| 971 |
|
|
dns->send_interval *= 0.9999; |
| 972 |
|
|
#endif |
| 973 |
pcg |
1.10 |
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 |
pcg |
1.12 |
double latency = NOW - (*i)->sent; |
| 979 |
|
|
|
| 980 |
|
|
if (dns->send_interval > latency) |
| 981 |
|
|
dns->send_interval = latency; |
| 982 |
pcg |
1.10 |
} |
| 983 |
|
|
|
| 984 |
pcg |
1.4 |
delete *i; |
| 985 |
|
|
dns_sndpq.erase (i); |
| 986 |
|
|
|
| 987 |
pcg |
1.10 |
if (flags & FLAG_RESPONSE && !(flags & FLAG_OP_MASK)) |
| 988 |
pcg |
1.4 |
{ |
| 989 |
|
|
char qname[MAXSIZE]; |
| 990 |
|
|
|
| 991 |
pcg |
1.10 |
while (pkt.qdcount-- && offs < MAXSIZE - 4) |
| 992 |
pcg |
1.4 |
{ |
| 993 |
pcg |
1.10 |
int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
| 994 |
pcg |
1.4 |
offs += 4; // skip qtype, qclass |
| 995 |
|
|
} |
| 996 |
|
|
|
| 997 |
pcg |
1.10 |
while (pkt.ancount-- && offs < MAXSIZE - 10 && datap) |
| 998 |
pcg |
1.4 |
{ |
| 999 |
pcg |
1.10 |
int qlen = pkt.decode_label ((char *)qname, MAXSIZE - offs, offs); |
| 1000 |
|
|
|
| 1001 |
|
|
u16 qtype = pkt [offs++] << 8; qtype |= pkt [offs++]; |
| 1002 |
|
|
u16 qclass = pkt [offs++] << 8; qclass |= pkt [offs++]; |
| 1003 |
|
|
u32 ttl = pkt [offs++] << 24; |
| 1004 |
|
|
ttl |= pkt [offs++] << 16; |
| 1005 |
|
|
ttl |= pkt [offs++] << 8; |
| 1006 |
|
|
ttl |= pkt [offs++]; |
| 1007 |
|
|
u16 rdlen = pkt [offs++] << 8; rdlen |= pkt [offs++]; |
| 1008 |
pcg |
1.8 |
|
| 1009 |
pcg |
1.10 |
if (qtype == RR_TYPE_NULL || qtype == RR_TYPE_TXT) |
| 1010 |
|
|
{ |
| 1011 |
|
|
if (rdlen <= MAXSIZE - offs) |
| 1012 |
|
|
{ |
| 1013 |
|
|
// decode bytes, finally |
| 1014 |
|
|
|
| 1015 |
|
|
while (rdlen) |
| 1016 |
|
|
{ |
| 1017 |
|
|
int txtlen = pkt [offs++]; |
| 1018 |
pcg |
1.4 |
|
| 1019 |
pcg |
1.10 |
assert (txtlen + offs < MAXSIZE - 1); |
| 1020 |
pcg |
1.4 |
|
| 1021 |
pcg |
1.10 |
memcpy (datap, pkt.at (offs), txtlen); |
| 1022 |
|
|
datap += txtlen; offs += txtlen; |
| 1023 |
|
|
|
| 1024 |
|
|
rdlen -= txtlen + 1; |
| 1025 |
|
|
} |
| 1026 |
|
|
} |
| 1027 |
|
|
} |
| 1028 |
|
|
else if (qtype == RR_TYPE_A) |
| 1029 |
pcg |
1.5 |
{ |
| 1030 |
pcg |
1.10 |
u8 ip [4]; |
| 1031 |
pcg |
1.5 |
|
| 1032 |
pcg |
1.10 |
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 |
pcg |
1.5 |
{ |
| 1041 |
pcg |
1.10 |
slog (L_TRACE, _("got tunnel meta command %02x"), ip [3]); |
| 1042 |
pcg |
1.5 |
|
| 1043 |
pcg |
1.10 |
if (ip [3] == CMD_IP_RST) |
| 1044 |
|
|
{ |
| 1045 |
|
|
slog (L_DEBUG, _("got tunnel RST request")); |
| 1046 |
|
|
|
| 1047 |
pcg |
1.12 |
delete dns; c->dns = 0; |
| 1048 |
pcg |
1.10 |
|
| 1049 |
|
|
return; |
| 1050 |
|
|
} |
| 1051 |
|
|
else if (ip [3] == CMD_IP_SYN) |
| 1052 |
pcg |
1.12 |
{ |
| 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 |
pcg |
1.10 |
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 |
pcg |
1.5 |
|
| 1068 |
pcg |
1.10 |
return; |
| 1069 |
pcg |
1.9 |
} |
| 1070 |
pcg |
1.4 |
|
| 1071 |
pcg |
1.9 |
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 |
pcg |
1.4 |
} |
| 1084 |
|
|
} |
| 1085 |
|
|
} |
| 1086 |
|
|
|
| 1087 |
pcg |
1.6 |
// todo: pkt now used |
| 1088 |
pcg |
1.9 |
if (datap) |
| 1089 |
|
|
dns->receive_rep (new dns_rcv (seqno, data, datap - data)); |
| 1090 |
pcg |
1.5 |
|
| 1091 |
pcg |
1.4 |
break; |
| 1092 |
|
|
} |
| 1093 |
|
|
} |
| 1094 |
|
|
|
| 1095 |
|
|
void |
| 1096 |
|
|
vpn::dnsv4_ev (io_watcher &w, short revents) |
| 1097 |
|
|
{ |
| 1098 |
|
|
if (revents & EVENT_READ) |
| 1099 |
|
|
{ |
| 1100 |
|
|
dns_packet *pkt = new dns_packet; |
| 1101 |
|
|
struct sockaddr_in sa; |
| 1102 |
|
|
socklen_t sa_len = sizeof (sa); |
| 1103 |
|
|
|
| 1104 |
pcg |
1.10 |
pkt->len = recvfrom (w.fd, pkt->at (0), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
| 1105 |
pcg |
1.4 |
|
| 1106 |
|
|
if (pkt->len > 0) |
| 1107 |
pcg |
1.5 |
{ |
| 1108 |
|
|
if (THISNODE->dns_port) |
| 1109 |
|
|
{ |
| 1110 |
pcg |
1.10 |
dnsv4_server (*pkt); |
| 1111 |
|
|
sendto (w.fd, pkt->at (0), pkt->len, 0, (sockaddr *)&sa, sa_len); |
| 1112 |
pcg |
1.5 |
} |
| 1113 |
|
|
else |
| 1114 |
pcg |
1.10 |
dnsv4_client (*pkt); |
| 1115 |
|
|
|
| 1116 |
|
|
delete pkt; |
| 1117 |
pcg |
1.5 |
} |
| 1118 |
pcg |
1.1 |
} |
| 1119 |
|
|
} |
| 1120 |
|
|
|
| 1121 |
|
|
bool |
| 1122 |
pcg |
1.3 |
connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
| 1123 |
|
|
{ |
| 1124 |
pcg |
1.9 |
if (!dns) |
| 1125 |
|
|
dns = new dns_connection (this); |
| 1126 |
pcg |
1.4 |
|
| 1127 |
pcg |
1.9 |
if (!dns->snddq.put (pkt)) |
| 1128 |
pcg |
1.3 |
return false; |
| 1129 |
|
|
|
| 1130 |
pcg |
1.10 |
dns->tw.trigger (); |
| 1131 |
pcg |
1.3 |
|
| 1132 |
|
|
return true; |
| 1133 |
|
|
} |
| 1134 |
|
|
|
| 1135 |
pcg |
1.12 |
void |
| 1136 |
|
|
connection::dnsv4_reset_connection () |
| 1137 |
|
|
{ |
| 1138 |
|
|
//delete dns; dns = 0; //TODO |
| 1139 |
|
|
} |
| 1140 |
|
|
|
| 1141 |
pcg |
1.10 |
#define NEXT(w) do { if (next > (w)) next = w; } while (0) |
| 1142 |
|
|
|
| 1143 |
pcg |
1.3 |
void |
| 1144 |
pcg |
1.9 |
dns_connection::time_cb (time_watcher &w) |
| 1145 |
pcg |
1.1 |
{ |
| 1146 |
pcg |
1.10 |
// servers have to be polled |
| 1147 |
|
|
if (THISNODE->dns_port) |
| 1148 |
|
|
return; |
| 1149 |
|
|
|
| 1150 |
pcg |
1.3 |
// check for timeouts and (re)transmit |
| 1151 |
pcg |
1.10 |
tstamp next = NOW + poll_interval; |
| 1152 |
|
|
dns_snd *send = 0; |
| 1153 |
pcg |
1.3 |
|
| 1154 |
pcg |
1.10 |
for (vector<dns_snd *>::iterator i = vpn->dns_sndpq.begin (); |
| 1155 |
pcg |
1.4 |
i != vpn->dns_sndpq.end (); |
| 1156 |
pcg |
1.3 |
++i) |
| 1157 |
|
|
{ |
| 1158 |
pcg |
1.10 |
dns_snd *r = *i; |
| 1159 |
pcg |
1.3 |
|
| 1160 |
pcg |
1.10 |
if (r->timeout <= NOW) |
| 1161 |
pcg |
1.3 |
{ |
| 1162 |
pcg |
1.4 |
if (!send) |
| 1163 |
|
|
{ |
| 1164 |
|
|
send = r; |
| 1165 |
|
|
|
| 1166 |
|
|
r->retry++; |
| 1167 |
pcg |
1.10 |
r->timeout = NOW + r->retry; |
| 1168 |
pcg |
1.4 |
} |
| 1169 |
pcg |
1.3 |
} |
| 1170 |
pcg |
1.11 |
else |
| 1171 |
pcg |
1.10 |
NEXT (r->timeout); |
| 1172 |
pcg |
1.3 |
} |
| 1173 |
|
|
|
| 1174 |
pcg |
1.10 |
if (last_sent + send_interval <= NOW) |
| 1175 |
pcg |
1.5 |
{ |
| 1176 |
pcg |
1.10 |
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 |
|
|
} |
| 1190 |
|
|
else if (vpn->dns_sndpq.size () < MAX_OUTSTANDING) |
| 1191 |
|
|
{ |
| 1192 |
|
|
send = new dns_snd (this); |
| 1193 |
|
|
send->gen_stream_req (sndseq, snddq); |
| 1194 |
pcg |
1.5 |
|
| 1195 |
pcg |
1.10 |
sndseq = (sndseq + 1) & SEQNO_MASK; |
| 1196 |
|
|
} |
| 1197 |
pcg |
1.4 |
|
| 1198 |
pcg |
1.10 |
if (send) |
| 1199 |
|
|
vpn->dns_sndpq.push_back (send); |
| 1200 |
|
|
} |
| 1201 |
pcg |
1.4 |
|
| 1202 |
pcg |
1.10 |
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 |
|
|
} |
| 1209 |
pcg |
1.4 |
} |
| 1210 |
pcg |
1.10 |
else |
| 1211 |
|
|
NEXT (last_sent + send_interval); |
| 1212 |
|
|
|
| 1213 |
pcg |
1.12 |
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 ()); |
| 1216 |
pcg |
1.11 |
|
| 1217 |
|
|
// TODO: no idea when this happens, but when next < NOW, we have a problem |
| 1218 |
|
|
if (next < NOW + 0.0001) |
| 1219 |
|
|
next = NOW + 0.1; |
| 1220 |
pcg |
1.4 |
|
| 1221 |
pcg |
1.3 |
w.start (next); |
| 1222 |
pcg |
1.1 |
} |
| 1223 |
|
|
|
| 1224 |
|
|
#endif |
| 1225 |
|
|
|