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 |
|
|
#include "netcompat.h" |
43 |
|
|
|
44 |
|
|
#include "vpn.h" |
45 |
|
|
|
46 |
|
|
#define MIN_RETRY 1. |
47 |
|
|
#define MAX_RETRY 60. |
48 |
|
|
|
49 |
pcg |
1.5 |
#define MAX_OUTSTANDING 40 // max. outstanding requests |
50 |
pcg |
1.4 |
#define MAX_WINDOW 100 // max. for MAX_OUTSTANDING |
51 |
pcg |
1.5 |
#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 |
pcg |
1.6 |
// 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 |
62 |
pcg |
1.1 |
|
63 |
|
|
/* |
64 |
|
|
|
65 |
|
|
protocol, in shorthand :) |
66 |
|
|
|
67 |
pcg |
1.2 |
client -> server <req> ANY? |
68 |
|
|
server -> client <req> TXT <rep> |
69 |
pcg |
1.1 |
|
70 |
pcg |
1.2 |
<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 |
pcg |
1.1 |
|
77 |
|
|
*/ |
78 |
|
|
|
79 |
pcg |
1.2 |
#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 |
pcg |
1.4 |
static int encode (char *dst, u8 *src, int len); |
95 |
|
|
static int decode (u8 *dst, char *src, int len); |
96 |
pcg |
1.2 |
|
97 |
|
|
dns64 (); |
98 |
|
|
} dns64; |
99 |
|
|
|
100 |
pcg |
1.6 |
// 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 |
pcg |
1.2 |
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 |
pcg |
1.4 |
int dns64::encode (char *dst, u8 *src, int len) |
113 |
pcg |
1.2 |
{ |
114 |
|
|
// slow, but easy to debug |
115 |
pcg |
1.4 |
char *beg = dst; |
116 |
pcg |
1.2 |
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 |
pcg |
1.4 |
|
134 |
|
|
return dst - beg; |
135 |
pcg |
1.2 |
} |
136 |
|
|
|
137 |
pcg |
1.4 |
int dns64::decode (u8 *dst, char *src, int len) |
138 |
pcg |
1.2 |
{ |
139 |
|
|
// slow, but easy to debug |
140 |
pcg |
1.4 |
u8 *beg = dst; |
141 |
pcg |
1.2 |
unsigned int accum, bits = 0; |
142 |
|
|
|
143 |
|
|
while (len--) |
144 |
|
|
{ |
145 |
|
|
s8 chr = decode_chars [(u8)*src++]; |
146 |
|
|
|
147 |
|
|
if (!chr) |
148 |
pcg |
1.5 |
continue; |
149 |
pcg |
1.2 |
|
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 |
pcg |
1.4 |
|
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 |
pcg |
1.5 |
bool put (u8 *data, unsigned int datalen); |
179 |
pcg |
1.4 |
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 |
pcg |
1.5 |
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 |
pcg |
1.4 |
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 |
pcg |
1.5 |
if (len > MAXSIZE && fill >= 2) |
233 |
|
|
abort (); // TODO handle this gracefully, connection reset |
234 |
|
|
|
235 |
pcg |
1.4 |
if (fill < len + 2) |
236 |
|
|
return 0; |
237 |
|
|
|
238 |
|
|
vpn_packet *pkt = new vpn_packet; |
239 |
pcg |
1.5 |
|
240 |
|
|
pkt->len = len; |
241 |
pcg |
1.4 |
memcpy (&((*pkt)[0]), data + 2, len); |
242 |
|
|
remove (len + 2); |
243 |
|
|
|
244 |
|
|
return pkt; |
245 |
pcg |
1.2 |
} |
246 |
|
|
|
247 |
pcg |
1.3 |
///////////////////////////////////////////////////////////////////////////// |
248 |
|
|
|
249 |
pcg |
1.2 |
#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 |
pcg |
1.5 |
#define FLAG_AUTH ( 1 << 5) |
258 |
pcg |
1.2 |
#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 |
pcg |
1.5 |
#define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA) |
267 |
pcg |
1.2 |
|
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 |
pcg |
1.3 |
///////////////////////////////////////////////////////////////////////////// |
314 |
|
|
|
315 |
|
|
struct dns_req |
316 |
|
|
{ |
317 |
|
|
dns_packet *pkt; |
318 |
|
|
tstamp next; |
319 |
|
|
int retry; |
320 |
pcg |
1.4 |
connection *conn; |
321 |
pcg |
1.5 |
int seqno; |
322 |
pcg |
1.3 |
|
323 |
pcg |
1.5 |
dns_req (connection *c); |
324 |
|
|
void gen_stream_req (int seqno, byte_stream *stream); |
325 |
pcg |
1.3 |
}; |
326 |
|
|
|
327 |
pcg |
1.5 |
static u16 dns_id = 12098; // TODO: should be per-vpn |
328 |
|
|
|
329 |
|
|
static u16 next_id () |
330 |
pcg |
1.3 |
{ |
331 |
pcg |
1.5 |
// 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 |
pcg |
1.3 |
|
338 |
pcg |
1.5 |
return dns_id; |
339 |
|
|
} |
340 |
pcg |
1.3 |
|
341 |
pcg |
1.5 |
dns_req::dns_req (connection *c) |
342 |
pcg |
1.4 |
: conn (c) |
343 |
pcg |
1.3 |
{ |
344 |
|
|
next = 0; |
345 |
|
|
retry = 0; |
346 |
|
|
|
347 |
|
|
pkt = new dns_packet; |
348 |
|
|
|
349 |
pcg |
1.5 |
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 |
pcg |
1.4 |
pkt->qdcount = htons (1); |
358 |
pcg |
1.3 |
|
359 |
pcg |
1.4 |
int offs = 6*2; |
360 |
pcg |
1.5 |
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 |
pcg |
1.4 |
|
366 |
pcg |
1.5 |
data[0] = THISNODE->id; //TODO |
367 |
pcg |
1.6 |
data[1] = seqno >> 7; //TODO |
368 |
|
|
data[2] = seqno << 1; //TODO |
369 |
pcg |
1.4 |
|
370 |
pcg |
1.5 |
int datalen = dns64::decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE) - 3; |
371 |
pcg |
1.4 |
|
372 |
pcg |
1.5 |
if (datalen > stream->size ()) |
373 |
|
|
datalen = stream->size (); |
374 |
pcg |
1.4 |
|
375 |
pcg |
1.5 |
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 |
pcg |
1.4 |
|
381 |
pcg |
1.5 |
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 |
pcg |
1.4 |
|
388 |
pcg |
1.5 |
offs += lbllen; |
389 |
|
|
encp += lbllen; |
390 |
pcg |
1.4 |
|
391 |
pcg |
1.5 |
enclen -= lbllen; |
392 |
pcg |
1.4 |
} |
393 |
|
|
|
394 |
|
|
const char *suffix = THISNODE->domain; |
395 |
pcg |
1.3 |
|
396 |
pcg |
1.4 |
// 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 |
pcg |
1.5 |
} |
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 |
pcg |
1.6 |
dns_rcv (int seqno, u8 *data, int datalen); |
431 |
pcg |
1.5 |
~dns_rcv (); |
432 |
|
|
}; |
433 |
|
|
|
434 |
pcg |
1.6 |
dns_rcv::dns_rcv (int seqno, u8 *data, int datalen) |
435 |
pcg |
1.5 |
: seqno (seqno), pkt (new dns_packet), datalen (datalen) |
436 |
|
|
{ |
437 |
|
|
memcpy (this->data, data, datalen); |
438 |
|
|
} |
439 |
pcg |
1.4 |
|
440 |
pcg |
1.5 |
dns_rcv::~dns_rcv () |
441 |
|
|
{ |
442 |
|
|
delete pkt; |
443 |
pcg |
1.3 |
} |
444 |
|
|
|
445 |
|
|
///////////////////////////////////////////////////////////////////////////// |
446 |
|
|
|
447 |
pcg |
1.2 |
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 |
pcg |
1.5 |
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 |
pcg |
1.6 |
else if ((u32)(*i)->seqno - (u32)dns_rcvseq + MAX_WINDOW > MAX_WINDOW * 2) |
491 |
pcg |
1.5 |
{ |
492 |
pcg |
1.6 |
//D |
493 |
|
|
//abort(); |
494 |
|
|
printf ("%d erasing %d (%d)\n", THISNODE->id, (u32)(*i)->seqno, dns_rcvseq); |
495 |
pcg |
1.5 |
dns_rcvpq.erase (i); |
496 |
|
|
goto redo; |
497 |
|
|
} |
498 |
|
|
} |
499 |
|
|
|
500 |
pcg |
1.4 |
dns_packet * |
501 |
|
|
vpn::dnsv4_server (dns_packet *pkt) |
502 |
pcg |
1.2 |
{ |
503 |
pcg |
1.4 |
u16 flags = ntohs (pkt->flags); |
504 |
pcg |
1.2 |
|
505 |
pcg |
1.4 |
//memcpy (&((*rep)[0]), &((*pkt)[0]), pkt->len); |
506 |
|
|
int offs = 6 * 2; // skip header |
507 |
pcg |
1.2 |
|
508 |
pcg |
1.4 |
pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
509 |
pcg |
1.2 |
|
510 |
pcg |
1.4 |
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 |
pcg |
1.2 |
|
519 |
pcg |
1.4 |
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 |
pcg |
1.2 |
|
524 |
pcg |
1.4 |
pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); |
525 |
pcg |
1.2 |
|
526 |
pcg |
1.4 |
int dlen = strlen (THISNODE->domain); |
527 |
pcg |
1.2 |
|
528 |
pcg |
1.4 |
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 |
pcg |
1.2 |
{ |
533 |
pcg |
1.5 |
// correct class, domain: parse |
534 |
pcg |
1.4 |
u8 data[MAXSIZE]; |
535 |
|
|
int datalen = dns64::decode (data, qname, qlen - dlen - 1); |
536 |
pcg |
1.2 |
|
537 |
pcg |
1.5 |
int client = data[0]; |
538 |
pcg |
1.6 |
int seqno = ((data[1] << 7) | (data[2] >> 1)) & SEQNO_MASK; |
539 |
pcg |
1.2 |
|
540 |
pcg |
1.5 |
if (0 < client && client <= conns.size ()) |
541 |
|
|
{ |
542 |
|
|
connection *c = conns [client - 1]; |
543 |
|
|
|
544 |
pcg |
1.6 |
redo: |
545 |
|
|
|
546 |
pcg |
1.5 |
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 |
pcg |
1.6 |
// already seen that request: simply reply with the cached reply |
552 |
pcg |
1.5 |
dns_rcv *r = *i; |
553 |
pcg |
1.4 |
|
554 |
pcg |
1.6 |
printf ("DUPLICATE %d\n", htons (r->pkt->id));//D |
555 |
|
|
|
556 |
pcg |
1.5 |
offs = r->pkt->len; |
557 |
|
|
memcpy (pkt->at (0), r->pkt->at (0), offs); |
558 |
|
|
goto duplicate_request; |
559 |
|
|
} |
560 |
pcg |
1.4 |
|
561 |
pcg |
1.5 |
// new packet, queue |
562 |
pcg |
1.6 |
dns_rcv *rcv = new dns_rcv (seqno, data + 3, datalen - 3); |
563 |
|
|
c->dnsv4_receive_rep (rcv); |
564 |
pcg |
1.4 |
|
565 |
pcg |
1.5 |
// now generate reply |
566 |
|
|
pkt->ancount = htons (1); // one answer RR |
567 |
|
|
pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
568 |
pcg |
1.4 |
|
569 |
pcg |
1.5 |
(*pkt) [offs++] = 0xc0; |
570 |
|
|
(*pkt) [offs++] = 6 * 2; // same as in query section |
571 |
pcg |
1.4 |
|
572 |
pcg |
1.5 |
(*pkt) [offs++] = RR_TYPE_TXT >> 8; (*pkt) [offs++] = RR_TYPE_TXT; |
573 |
|
|
(*pkt) [offs++] = RR_CLASS_IN >> 8; (*pkt) [offs++] = RR_CLASS_IN; |
574 |
pcg |
1.2 |
|
575 |
pcg |
1.5 |
(*pkt) [offs++] = 0; (*pkt) [offs++] = 0; |
576 |
|
|
(*pkt) [offs++] = 0; (*pkt) [offs++] = 0; // TTL |
577 |
pcg |
1.2 |
|
578 |
pcg |
1.5 |
int dlen = MAX_PKT_SIZE - offs - 2; |
579 |
pcg |
1.2 |
|
580 |
pcg |
1.5 |
// bind doesn't compress well, so reduce further by one label length |
581 |
|
|
dlen -= qlen; |
582 |
pcg |
1.2 |
|
583 |
pcg |
1.5 |
int rdlen_offs = offs += 2; |
584 |
pcg |
1.2 |
|
585 |
pcg |
1.5 |
while (c->dns_snddq |
586 |
|
|
&& !c->dns_snddq->empty () |
587 |
|
|
&& dlen > 1) |
588 |
|
|
{ |
589 |
|
|
int txtlen = dlen <= 255 ? dlen - 1 : 255; |
590 |
pcg |
1.2 |
|
591 |
pcg |
1.5 |
if (txtlen > c->dns_snddq->size ()) |
592 |
|
|
txtlen = c->dns_snddq->size (); |
593 |
pcg |
1.2 |
|
594 |
pcg |
1.5 |
(*pkt)[offs++] = txtlen; |
595 |
|
|
memcpy (pkt->at (offs), c->dns_snddq->begin (), txtlen); |
596 |
|
|
offs += txtlen; |
597 |
|
|
c->dns_snddq->remove (txtlen); |
598 |
pcg |
1.2 |
|
599 |
pcg |
1.5 |
dlen -= txtlen + 1; |
600 |
|
|
} |
601 |
pcg |
1.2 |
|
602 |
pcg |
1.5 |
// avoid empty TXT rdata |
603 |
|
|
if (offs == rdlen_offs) |
604 |
|
|
(*pkt)[offs++] = 0; |
605 |
pcg |
1.2 |
|
606 |
pcg |
1.5 |
int rdlen = offs - rdlen_offs; |
607 |
pcg |
1.2 |
|
608 |
pcg |
1.5 |
(*pkt) [rdlen_offs - 2] = rdlen >> 8; |
609 |
|
|
(*pkt) [rdlen_offs - 1] = rdlen; |
610 |
pcg |
1.4 |
|
611 |
pcg |
1.6 |
// now update dns_rcv copy |
612 |
|
|
rcv->pkt->len = offs; |
613 |
|
|
memcpy (rcv->pkt->at (0), pkt->at (0), offs); |
614 |
|
|
|
615 |
pcg |
1.5 |
duplicate_request: ; |
616 |
|
|
} |
617 |
|
|
else |
618 |
|
|
pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
619 |
pcg |
1.2 |
} |
620 |
pcg |
1.6 |
|
621 |
|
|
pkt->len = offs; |
622 |
pcg |
1.4 |
} |
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 |
pcg |
1.5 |
// go through our request list and find the corresponding request |
637 |
pcg |
1.4 |
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 |
pcg |
1.5 |
int seqno = (*i)->seqno; |
644 |
|
|
u8 data[MAXSIZE], *datap = data; |
645 |
|
|
|
646 |
pcg |
1.4 |
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 |
pcg |
1.5 |
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 |
pcg |
1.4 |
|
685 |
pcg |
1.5 |
rdlen -= txtlen + 1; |
686 |
|
|
} |
687 |
pcg |
1.4 |
|
688 |
|
|
} |
689 |
pcg |
1.5 |
|
690 |
pcg |
1.4 |
} |
691 |
|
|
} |
692 |
|
|
|
693 |
pcg |
1.6 |
// todo: pkt now used |
694 |
|
|
c->dnsv4_receive_rep (new dns_rcv (seqno, data, datap - data)); |
695 |
pcg |
1.5 |
|
696 |
pcg |
1.4 |
break; |
697 |
|
|
} |
698 |
pcg |
1.5 |
|
699 |
|
|
delete pkt; |
700 |
pcg |
1.4 |
} |
701 |
|
|
|
702 |
|
|
void |
703 |
|
|
vpn::dnsv4_ev (io_watcher &w, short revents) |
704 |
|
|
{ |
705 |
|
|
if (revents & EVENT_READ) |
706 |
|
|
{ |
707 |
|
|
dns_packet *pkt = new dns_packet; |
708 |
|
|
struct sockaddr_in sa; |
709 |
|
|
socklen_t sa_len = sizeof (sa); |
710 |
|
|
|
711 |
|
|
pkt->len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
712 |
|
|
|
713 |
|
|
if (pkt->len > 0) |
714 |
pcg |
1.5 |
{ |
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 |
|
|
} |
729 |
pcg |
1.1 |
} |
730 |
|
|
} |
731 |
|
|
|
732 |
|
|
bool |
733 |
pcg |
1.3 |
connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
734 |
|
|
{ |
735 |
pcg |
1.4 |
// 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 |
pcg |
1.5 |
//dns_rcvseq = dns_sndseq = 0; |
742 |
pcg |
1.4 |
|
743 |
|
|
dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
744 |
|
|
} |
745 |
|
|
|
746 |
|
|
if (!dns_snddq->put (pkt)) |
747 |
pcg |
1.3 |
return false; |
748 |
|
|
|
749 |
pcg |
1.4 |
// start timer if neccessary |
750 |
pcg |
1.5 |
if (!THISNODE->dns_port && !dnsv4_tw.active) |
751 |
pcg |
1.4 |
dnsv4_cb (dnsv4_tw); |
752 |
pcg |
1.3 |
|
753 |
|
|
return true; |
754 |
|
|
} |
755 |
|
|
|
756 |
|
|
void |
757 |
|
|
connection::dnsv4_cb (time_watcher &w) |
758 |
pcg |
1.1 |
{ |
759 |
pcg |
1.3 |
// check for timeouts and (re)transmit |
760 |
|
|
tstamp next = NOW + 60; |
761 |
pcg |
1.4 |
dns_req *send = 0; |
762 |
pcg |
1.3 |
|
763 |
pcg |
1.4 |
for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin (); |
764 |
|
|
i != vpn->dns_sndpq.end (); |
765 |
pcg |
1.3 |
++i) |
766 |
|
|
{ |
767 |
|
|
dns_req *r = *i; |
768 |
|
|
|
769 |
|
|
if (r->next <= NOW) |
770 |
|
|
{ |
771 |
pcg |
1.4 |
if (!send) |
772 |
|
|
{ |
773 |
|
|
send = r; |
774 |
|
|
|
775 |
pcg |
1.5 |
if (r->retry)//D |
776 |
|
|
printf ("req %d, retry %d\n", r->pkt->id, r->retry); |
777 |
pcg |
1.4 |
r->retry++; |
778 |
|
|
r->next = NOW + r->retry; |
779 |
|
|
} |
780 |
pcg |
1.3 |
} |
781 |
|
|
|
782 |
|
|
if (r->next < next) |
783 |
|
|
next = r->next; |
784 |
|
|
} |
785 |
|
|
|
786 |
pcg |
1.4 |
if (!send |
787 |
|
|
&& vpn->dns_sndpq.size () < MAX_OUTSTANDING) |
788 |
pcg |
1.5 |
{ |
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 |
pcg |
1.4 |
|
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 |
pcg |
1.3 |
w.start (next); |
810 |
pcg |
1.1 |
} |
811 |
|
|
|
812 |
|
|
#endif |
813 |
|
|
|