… | |
… | |
42 | #include "vpn.h" |
42 | #include "vpn.h" |
43 | |
43 | |
44 | #define MIN_RETRY 1. |
44 | #define MIN_RETRY 1. |
45 | #define MAX_RETRY 60. |
45 | #define MAX_RETRY 60. |
46 | |
46 | |
47 | #define MAX_OUTSTANDING 10 // max. outstanding requests |
47 | #define MAX_OUTSTANDING 40 // max. outstanding requests |
|
|
48 | #define MAX_WINDOW 100 // max. for MAX_OUTSTANDING |
|
|
49 | #define MAX_RATE 1000 // requests/s |
|
|
50 | #define MAX_BACKLOG (10*1024) // size of protocol backlog, must be > MAXSIZE |
|
|
51 | |
|
|
52 | #define MAX_DOMAIN_SIZE 220 // 255 is legal limit, but bind doesn't compress well |
|
|
53 | // 240 leaves about 4 bytes of server reply data |
|
|
54 | // every two request byte sless give room for one reply byte |
|
|
55 | |
|
|
56 | #define SEQNO_MASK 0xffff |
48 | |
57 | |
49 | /* |
58 | /* |
50 | |
59 | |
51 | protocol, in shorthand :) |
60 | protocol, in shorthand :) |
52 | |
61 | |
… | |
… | |
75 | static const char encode_chars[64 + 1]; |
84 | static const char encode_chars[64 + 1]; |
76 | static s8 decode_chars[256]; |
85 | static s8 decode_chars[256]; |
77 | |
86 | |
78 | static int encode_len (int bytes) { return (bytes * 8 + 5) / 6; } |
87 | static int encode_len (int bytes) { return (bytes * 8 + 5) / 6; } |
79 | static int decode_len (int bytes) { return (bytes * 6) / 8; } |
88 | static int decode_len (int bytes) { return (bytes * 6) / 8; } |
80 | static void encode (char *dst, u8 *src, int len); |
89 | static int encode (char *dst, u8 *src, int len); |
81 | static void decode (u8 *dst, char *src, int len); |
90 | static int decode (u8 *dst, char *src, int len); |
82 | |
91 | |
83 | dns64 (); |
92 | dns64 (); |
84 | } dns64; |
93 | } dns64; |
85 | |
94 | |
86 | const char dns64::encode_chars[64 + 1] = "0123456789-abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
95 | const char dns64::encode_chars[64 + 1] = "_4B9dLphHzrqQmGjkTbJt5svlZX8xSaReEYfwKgF1DP2W6NyVOU70IouACcMn3i-"; |
87 | s8 dns64::decode_chars[256]; |
96 | s8 dns64::decode_chars[256]; |
88 | |
97 | |
89 | dns64::dns64 () |
98 | dns64::dns64 () |
90 | { |
99 | { |
91 | for (int i = 0; i < 64; i++) |
100 | for (int i = 0; i < 64; i++) |
92 | decode_chars [encode_chars [i]] = i + 1; |
101 | decode_chars [encode_chars [i]] = i + 1; |
93 | } |
102 | } |
94 | |
103 | |
95 | void dns64::encode (char *dst, u8 *src, int len) |
104 | int dns64::encode (char *dst, u8 *src, int len) |
96 | { |
105 | { |
97 | // slow, but easy to debug |
106 | // slow, but easy to debug |
|
|
107 | char *beg = dst; |
98 | unsigned int accum, bits = 0; |
108 | unsigned int accum, bits = 0; |
99 | |
109 | |
100 | while (len--) |
110 | while (len--) |
101 | { |
111 | { |
102 | accum <<= 8; |
112 | accum <<= 8; |
… | |
… | |
110 | } |
120 | } |
111 | } |
121 | } |
112 | |
122 | |
113 | if (bits) |
123 | if (bits) |
114 | *dst++ = encode_chars [(accum << (6 - bits)) & 63]; |
124 | *dst++ = encode_chars [(accum << (6 - bits)) & 63]; |
115 | } |
|
|
116 | |
125 | |
|
|
126 | return dst - beg; |
|
|
127 | } |
|
|
128 | |
117 | void dns64::decode (u8 *dst, char *src, int len) |
129 | int dns64::decode (u8 *dst, char *src, int len) |
118 | { |
130 | { |
119 | // slow, but easy to debug |
131 | // slow, but easy to debug |
|
|
132 | u8 *beg = dst; |
120 | unsigned int accum, bits = 0; |
133 | unsigned int accum, bits = 0; |
121 | |
134 | |
122 | while (len--) |
135 | while (len--) |
123 | { |
136 | { |
124 | s8 chr = decode_chars [(u8)*src++]; |
137 | s8 chr = decode_chars [(u8)*src++]; |
125 | |
138 | |
126 | |
|
|
127 | if (!chr) |
139 | if (!chr) |
128 | break; |
140 | continue; |
129 | |
141 | |
130 | accum <<= 6; |
142 | accum <<= 6; |
131 | accum |= chr - 1; |
143 | accum |= chr - 1; |
132 | bits += 6; |
144 | bits += 6; |
133 | |
145 | |
… | |
… | |
135 | { |
147 | { |
136 | *dst++ = accum >> (bits - 8); |
148 | *dst++ = accum >> (bits - 8); |
137 | bits -= 8; |
149 | bits -= 8; |
138 | } |
150 | } |
139 | } |
151 | } |
|
|
152 | |
|
|
153 | return dst - beg; |
|
|
154 | } |
|
|
155 | |
|
|
156 | ///////////////////////////////////////////////////////////////////////////// |
|
|
157 | |
|
|
158 | struct byte_stream |
|
|
159 | { |
|
|
160 | u8 *data; |
|
|
161 | int maxsize; |
|
|
162 | int fill; |
|
|
163 | |
|
|
164 | byte_stream (int maxsize); |
|
|
165 | ~byte_stream (); |
|
|
166 | |
|
|
167 | bool empty () { return !fill; } |
|
|
168 | int size () { return fill; } |
|
|
169 | |
|
|
170 | bool put (u8 *data, unsigned int datalen); |
|
|
171 | bool put (vpn_packet *pkt); |
|
|
172 | vpn_packet *get (); |
|
|
173 | |
|
|
174 | u8 *begin () { return data; } |
|
|
175 | void remove (int count); |
|
|
176 | }; |
|
|
177 | |
|
|
178 | byte_stream::byte_stream (int maxsize) |
|
|
179 | : maxsize (maxsize), fill (0) |
|
|
180 | { |
|
|
181 | data = new u8 [maxsize]; |
|
|
182 | } |
|
|
183 | |
|
|
184 | byte_stream::~byte_stream () |
|
|
185 | { |
|
|
186 | delete data; |
|
|
187 | } |
|
|
188 | |
|
|
189 | void byte_stream::remove (int count) |
|
|
190 | { |
|
|
191 | if (count > fill) |
|
|
192 | abort (); |
|
|
193 | |
|
|
194 | memmove (data, data + count, fill -= count); |
|
|
195 | } |
|
|
196 | |
|
|
197 | bool byte_stream::put (u8 *data, unsigned int datalen) |
|
|
198 | { |
|
|
199 | if (maxsize - fill < datalen) |
|
|
200 | return false; |
|
|
201 | |
|
|
202 | memcpy (this->data + fill, data, datalen); fill += datalen; |
|
|
203 | |
|
|
204 | return true; |
|
|
205 | } |
|
|
206 | |
|
|
207 | bool byte_stream::put (vpn_packet *pkt) |
|
|
208 | { |
|
|
209 | if (maxsize - fill < pkt->len + 2) |
|
|
210 | return false; |
|
|
211 | |
|
|
212 | data [fill++] = pkt->len >> 8; |
|
|
213 | data [fill++] = pkt->len; |
|
|
214 | |
|
|
215 | memcpy (data + fill, &((*pkt)[0]), pkt->len); fill += pkt->len; |
|
|
216 | |
|
|
217 | return true; |
|
|
218 | } |
|
|
219 | |
|
|
220 | vpn_packet *byte_stream::get () |
|
|
221 | { |
|
|
222 | int len = (data [0] << 8) | data [1]; |
|
|
223 | |
|
|
224 | printf ("get len %d, fill %d\n", len, fill);//D |
|
|
225 | |
|
|
226 | if (len > MAXSIZE && fill >= 2) |
|
|
227 | abort (); // TODO handle this gracefully, connection reset |
|
|
228 | |
|
|
229 | if (fill < len + 2) |
|
|
230 | return 0; |
|
|
231 | |
|
|
232 | vpn_packet *pkt = new vpn_packet; |
|
|
233 | |
|
|
234 | pkt->len = len; |
|
|
235 | memcpy (&((*pkt)[0]), data + 2, len); |
|
|
236 | remove (len + 2); |
|
|
237 | |
|
|
238 | return pkt; |
140 | } |
239 | } |
141 | |
240 | |
142 | ///////////////////////////////////////////////////////////////////////////// |
241 | ///////////////////////////////////////////////////////////////////////////// |
143 | |
242 | |
144 | #define FLAG_QUERY ( 0 << 15) |
243 | #define FLAG_QUERY ( 0 << 15) |
… | |
… | |
147 | #define FLAG_OP_QUERY ( 0 << 11) |
246 | #define FLAG_OP_QUERY ( 0 << 11) |
148 | #define FLAG_AA ( 1 << 10) |
247 | #define FLAG_AA ( 1 << 10) |
149 | #define FLAG_TC ( 1 << 9) |
248 | #define FLAG_TC ( 1 << 9) |
150 | #define FLAG_RD ( 1 << 8) |
249 | #define FLAG_RD ( 1 << 8) |
151 | #define FLAG_RA ( 1 << 7) |
250 | #define FLAG_RA ( 1 << 7) |
|
|
251 | #define FLAG_AUTH ( 1 << 5) |
152 | #define FLAG_RCODE_MASK (15 << 0) |
252 | #define FLAG_RCODE_MASK (15 << 0) |
153 | #define FLAG_RCODE_OK ( 0 << 0) |
253 | #define FLAG_RCODE_OK ( 0 << 0) |
154 | #define FLAG_RCODE_FORMERR ( 1 << 0) |
254 | #define FLAG_RCODE_FORMERR ( 1 << 0) |
155 | #define FLAG_RCODE_SERVFAIL ( 2 << 0) |
255 | #define FLAG_RCODE_SERVFAIL ( 2 << 0) |
156 | #define FLAG_RCODE_NXDOMAIN ( 3 << 0) |
256 | #define FLAG_RCODE_NXDOMAIN ( 3 << 0) |
157 | #define FLAG_RCODE_REFUSED ( 5 << 0) |
257 | #define FLAG_RCODE_REFUSED ( 5 << 0) |
158 | |
258 | |
159 | #define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) |
259 | #define DEFAULT_CLIENT_FLAGS (FLAG_QUERY | FLAG_OP_QUERY | FLAG_RD) |
160 | #define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD) |
260 | #define DEFAULT_SERVER_FLAGS (FLAG_RESPONSE | FLAG_OP_QUERY | FLAG_AA | FLAG_RD | FLAG_RA) |
161 | |
261 | |
162 | struct dns_packet : net_packet |
262 | struct dns_packet : net_packet |
163 | { |
263 | { |
164 | u16 id; |
264 | u16 id; |
165 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
265 | u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
… | |
… | |
209 | struct dns_req |
309 | struct dns_req |
210 | { |
310 | { |
211 | dns_packet *pkt; |
311 | dns_packet *pkt; |
212 | tstamp next; |
312 | tstamp next; |
213 | int retry; |
313 | int retry; |
|
|
314 | connection *conn; |
|
|
315 | int seqno; |
214 | |
316 | |
215 | dns_req (connection *c, vpn_packet *p, int &offs); |
317 | dns_req (connection *c); |
|
|
318 | void gen_stream_req (int seqno, byte_stream *stream); |
216 | }; |
319 | }; |
217 | |
320 | |
218 | struct dns_rep |
|
|
219 | { |
|
|
220 | }; |
|
|
221 | |
|
|
222 | static u16 dns_id; // TODO: should be per-vpn |
321 | static u16 dns_id = 12098; // TODO: should be per-vpn |
223 | |
322 | |
224 | dns_req::dns_req (connection *c, vpn_packet *p, int &offs) |
323 | static u16 next_id () |
|
|
324 | { |
|
|
325 | // the simplest lsfr with periodicity 65535 i could find |
|
|
326 | dns_id = (dns_id << 1) |
|
|
327 | | (((dns_id >> 1) |
|
|
328 | ^ (dns_id >> 2) |
|
|
329 | ^ (dns_id >> 4) |
|
|
330 | ^ (dns_id >> 15)) & 1); |
|
|
331 | |
|
|
332 | return dns_id; |
|
|
333 | } |
|
|
334 | |
|
|
335 | dns_req::dns_req (connection *c) |
|
|
336 | : conn (c) |
225 | { |
337 | { |
226 | next = 0; |
338 | next = 0; |
227 | retry = 0; |
339 | retry = 0; |
228 | |
340 | |
229 | pkt = new dns_packet; |
341 | pkt = new dns_packet; |
230 | |
342 | |
231 | pkt->id = dns_id++; |
343 | pkt->id = next_id (); |
|
|
344 | } |
|
|
345 | |
|
|
346 | void dns_req::gen_stream_req (int seqno, byte_stream *stream) |
|
|
347 | { |
|
|
348 | this->seqno = seqno; |
|
|
349 | |
232 | pkt->flags = DEFAULT_CLIENT_FLAGS; |
350 | pkt->flags = htons (DEFAULT_CLIENT_FLAGS); |
233 | pkt->qdcount = htons (1 * 0); |
351 | pkt->qdcount = htons (1); |
234 | pkt->ancount = 0; |
|
|
235 | pkt->nscount = 0; |
|
|
236 | pkt->arcount = 0; |
|
|
237 | |
352 | |
238 | offs += 10000; |
353 | int offs = 6*2; |
|
|
354 | int dlen = MAX_DOMAIN_SIZE - strlen (THISNODE->domain) - 2; |
|
|
355 | // MAX_DOMAIN_SIZE is technically 255, but bind doesn't compress responses well, |
|
|
356 | // so we need to have space for 2*MAX_DOMAIN_SIZE + header + extra |
239 | |
357 | |
240 | c->dns_sndq.push_back (this); |
358 | u8 data[256]; //TODO |
|
|
359 | |
|
|
360 | data[0] = THISNODE->id; //TODO |
|
|
361 | data[1] = seqno >> 8; //TODO |
|
|
362 | data[2] = seqno; //TODO |
|
|
363 | |
|
|
364 | int datalen = dns64::decode_len (dlen - (dlen + MAX_LBL_SIZE - 1) / MAX_LBL_SIZE) - 3; |
|
|
365 | |
|
|
366 | if (datalen > stream->size ()) |
|
|
367 | datalen = stream->size (); |
|
|
368 | |
|
|
369 | char enc[256], *encp = enc; |
|
|
370 | |
|
|
371 | memcpy (data + 3, stream->begin (), datalen); |
|
|
372 | int enclen = dns64::encode (enc, data, datalen + 3); |
|
|
373 | stream->remove (datalen); |
|
|
374 | |
|
|
375 | while (enclen) |
|
|
376 | { |
|
|
377 | int lbllen = enclen < MAX_LBL_SIZE ? enclen : MAX_LBL_SIZE; |
|
|
378 | |
|
|
379 | (*pkt)[offs++] = lbllen; |
|
|
380 | memcpy (pkt->at (offs), encp, lbllen); |
|
|
381 | |
|
|
382 | offs += lbllen; |
|
|
383 | encp += lbllen; |
|
|
384 | |
|
|
385 | enclen -= lbllen; |
|
|
386 | } |
|
|
387 | |
|
|
388 | const char *suffix = THISNODE->domain; |
|
|
389 | |
|
|
390 | // add tunnel domain |
|
|
391 | for (;;) |
|
|
392 | { |
|
|
393 | const char *end = strchr (suffix, '.'); |
|
|
394 | |
|
|
395 | if (!end) |
|
|
396 | end = suffix + strlen (suffix); |
|
|
397 | |
|
|
398 | int len = end - suffix; |
|
|
399 | |
|
|
400 | (*pkt)[offs++] = len; |
|
|
401 | memcpy (&((*pkt)[offs]), suffix, len); |
|
|
402 | offs += len; |
|
|
403 | |
|
|
404 | if (!*end) |
|
|
405 | break; |
|
|
406 | |
|
|
407 | suffix = end + 1; |
|
|
408 | } |
|
|
409 | |
|
|
410 | (*pkt)[offs++] = 0; |
|
|
411 | (*pkt)[offs++] = RR_TYPE_ANY >> 8; (*pkt)[offs++] = RR_TYPE_ANY; |
|
|
412 | (*pkt)[offs++] = RR_CLASS_IN >> 8; (*pkt)[offs++] = RR_CLASS_IN; |
|
|
413 | |
|
|
414 | pkt->len = offs; |
|
|
415 | } |
|
|
416 | |
|
|
417 | struct dns_rcv |
|
|
418 | { |
|
|
419 | int seqno; |
|
|
420 | dns_packet *pkt; // reply packet |
|
|
421 | u8 data [MAXSIZE]; // actually part of the reply packet... |
|
|
422 | int datalen; |
|
|
423 | |
|
|
424 | dns_rcv (int seqno, dns_packet *req, u8 *data, int datalen); |
|
|
425 | ~dns_rcv (); |
|
|
426 | }; |
|
|
427 | |
|
|
428 | dns_rcv::dns_rcv (int seqno, dns_packet *req, u8 *data, int datalen) |
|
|
429 | : seqno (seqno), pkt (new dns_packet), datalen (datalen) |
|
|
430 | { |
|
|
431 | memcpy (this->data, data, datalen); |
|
|
432 | pkt->len = req->len; |
|
|
433 | memcpy (pkt->at (0), req->at (0), req->len); |
|
|
434 | } |
|
|
435 | |
|
|
436 | dns_rcv::~dns_rcv () |
|
|
437 | { |
|
|
438 | delete pkt; |
241 | } |
439 | } |
242 | |
440 | |
243 | ///////////////////////////////////////////////////////////////////////////// |
441 | ///////////////////////////////////////////////////////////////////////////// |
244 | |
442 | |
245 | struct dns_cfg |
443 | struct dns_cfg |
… | |
… | |
249 | u8 unused1; |
447 | u8 unused1; |
250 | u16 max_size; |
448 | u16 max_size; |
251 | u8 flags1, flags2; |
449 | u8 flags1, flags2; |
252 | }; |
450 | }; |
253 | |
451 | |
254 | // reply to client-poll |
452 | void connection::dnsv4_receive_rep (struct dns_rcv *r) |
255 | void dnsv4_poll (dns_packet *pkt, int &offs) |
|
|
256 | { |
453 | { |
257 | int dlen = MAX_PKT_SIZE - offs - 2; |
454 | dns_rcvpq.push_back (r); |
258 | |
455 | |
259 | (*pkt) [offs++] = 0; |
456 | redo: |
260 | (*pkt) [offs++] = 5; |
|
|
261 | memcpy (&((*pkt)[offs]), "\01H\02\xff\x00", 5); |
|
|
262 | offs += 5; |
|
|
263 | |
457 | |
264 | pkt->ancount = htons (1); |
458 | for (vector<dns_rcv *>::iterator i = dns_rcvpq.begin (); |
|
|
459 | i != dns_rcvpq.end (); |
|
|
460 | ++i) |
|
|
461 | if (dns_rcvseq == (*i)->seqno) |
|
|
462 | { |
|
|
463 | dns_rcv *r = *i; |
|
|
464 | |
|
|
465 | dns_rcvseq = (dns_rcvseq + 1) & SEQNO_MASK; |
|
|
466 | |
|
|
467 | if (!dns_snddq && !dns_rcvdq) |
|
|
468 | { |
|
|
469 | dns_rcvdq = new byte_stream (MAX_BACKLOG * 2); |
|
|
470 | dns_snddq = new byte_stream (MAX_BACKLOG); |
|
|
471 | |
|
|
472 | dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
|
|
473 | } |
|
|
474 | |
|
|
475 | if (!dns_rcvdq->put (r->data, r->datalen)) |
|
|
476 | abort (); // MUST never overflow, can be caused by data corruption, TODO |
|
|
477 | |
|
|
478 | while (vpn_packet *pkt = dns_rcvdq->get ()) |
|
|
479 | { |
|
|
480 | sockinfo si; |
|
|
481 | si.host = 0; si.port = 0; si.prot = PROT_DNSv4; |
|
|
482 | |
|
|
483 | vpn->recv_vpn_packet (pkt, si); |
|
|
484 | } |
|
|
485 | } |
|
|
486 | else if ((u32)dns_rcvseq - MAX_WINDOW - (u32)(*i)->seqno < MAX_WINDOW * 2) |
|
|
487 | { |
|
|
488 | dns_rcvpq.erase (i); |
|
|
489 | goto redo; |
|
|
490 | } |
|
|
491 | } |
|
|
492 | |
|
|
493 | dns_packet * |
|
|
494 | vpn::dnsv4_server (dns_packet *pkt) |
|
|
495 | { |
|
|
496 | u16 flags = ntohs (pkt->flags); |
|
|
497 | |
|
|
498 | //memcpy (&((*rep)[0]), &((*pkt)[0]), pkt->len); |
|
|
499 | int offs = 6 * 2; // skip header |
|
|
500 | |
265 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
501 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
266 | |
502 | |
267 | printf ("we have room for %d bytes\n", dlen); |
503 | if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
|
|
504 | && pkt->qdcount == htons (1)) |
|
|
505 | { |
|
|
506 | char qname[MAXSIZE]; |
|
|
507 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
|
|
508 | |
|
|
509 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
|
|
510 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
|
|
511 | |
|
|
512 | pkt->qdcount = htons (1); |
|
|
513 | pkt->ancount = 0; |
|
|
514 | pkt->nscount = 0; // should be self, as other nameservers reply like this |
|
|
515 | pkt->arcount = 0; // a record for self, as other nameservers reply like this |
|
|
516 | |
|
|
517 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); |
|
|
518 | |
|
|
519 | int dlen = strlen (THISNODE->domain); |
|
|
520 | |
|
|
521 | if (qclass == RR_CLASS_IN |
|
|
522 | && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT) |
|
|
523 | && qlen > dlen + 1 |
|
|
524 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
|
|
525 | { |
|
|
526 | // correct class, domain: parse |
|
|
527 | u8 data[MAXSIZE]; |
|
|
528 | int datalen = dns64::decode (data, qname, qlen - dlen - 1); |
|
|
529 | |
|
|
530 | int client = data[0]; |
|
|
531 | int seqno = ((data[1] << 8) | data[2]) & SEQNO_MASK; |
|
|
532 | |
|
|
533 | if (0 < client && client <= conns.size ()) |
|
|
534 | { |
|
|
535 | connection *c = conns [client - 1]; |
|
|
536 | |
|
|
537 | for (vector<dns_rcv *>::iterator i = c->dns_rcvpq.begin (); |
|
|
538 | i != c->dns_rcvpq.end (); |
|
|
539 | ++i) |
|
|
540 | if ((*i)->seqno == seqno) |
|
|
541 | { |
|
|
542 | // already seen that request, just reply with the original reply |
|
|
543 | dns_rcv *r = *i; |
|
|
544 | |
|
|
545 | offs = r->pkt->len; |
|
|
546 | memcpy (pkt->at (0), r->pkt->at (0), offs); |
|
|
547 | goto duplicate_request; |
|
|
548 | } |
|
|
549 | |
|
|
550 | // new packet, queue |
|
|
551 | c->dnsv4_receive_rep (new dns_rcv (seqno, pkt, data + 3, datalen - 3)); |
|
|
552 | |
|
|
553 | // now generate reply |
|
|
554 | pkt->ancount = htons (1); // one answer RR |
|
|
555 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
|
|
556 | |
|
|
557 | (*pkt) [offs++] = 0xc0; |
|
|
558 | (*pkt) [offs++] = 6 * 2; // same as in query section |
|
|
559 | |
|
|
560 | (*pkt) [offs++] = RR_TYPE_TXT >> 8; (*pkt) [offs++] = RR_TYPE_TXT; |
|
|
561 | (*pkt) [offs++] = RR_CLASS_IN >> 8; (*pkt) [offs++] = RR_CLASS_IN; |
|
|
562 | |
|
|
563 | (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; |
|
|
564 | (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; // TTL |
|
|
565 | |
|
|
566 | int dlen = MAX_PKT_SIZE - offs - 2; |
|
|
567 | |
|
|
568 | // bind doesn't compress well, so reduce further by one label length |
|
|
569 | dlen -= qlen; |
|
|
570 | |
|
|
571 | int rdlen_offs = offs += 2; |
|
|
572 | |
|
|
573 | while (c->dns_snddq |
|
|
574 | && !c->dns_snddq->empty () |
|
|
575 | && dlen > 1) |
|
|
576 | { |
|
|
577 | int txtlen = dlen <= 255 ? dlen - 1 : 255; |
|
|
578 | |
|
|
579 | if (txtlen > c->dns_snddq->size ()) |
|
|
580 | txtlen = c->dns_snddq->size (); |
|
|
581 | |
|
|
582 | (*pkt)[offs++] = txtlen; |
|
|
583 | memcpy (pkt->at (offs), c->dns_snddq->begin (), txtlen); |
|
|
584 | offs += txtlen; |
|
|
585 | c->dns_snddq->remove (txtlen); |
|
|
586 | |
|
|
587 | dlen -= txtlen + 1; |
|
|
588 | } |
|
|
589 | |
|
|
590 | // avoid empty TXT rdata |
|
|
591 | if (offs == rdlen_offs) |
|
|
592 | (*pkt)[offs++] = 0; |
|
|
593 | |
|
|
594 | int rdlen = offs - rdlen_offs; |
|
|
595 | |
|
|
596 | (*pkt) [rdlen_offs - 2] = rdlen >> 8; |
|
|
597 | (*pkt) [rdlen_offs - 1] = rdlen; |
|
|
598 | |
|
|
599 | duplicate_request: ; |
|
|
600 | } |
|
|
601 | else |
|
|
602 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
|
|
603 | } |
|
|
604 | } |
|
|
605 | else |
|
|
606 | offs = pkt->len; |
|
|
607 | |
|
|
608 | pkt->len = offs; |
|
|
609 | return pkt; |
|
|
610 | } |
|
|
611 | |
|
|
612 | void |
|
|
613 | vpn::dnsv4_client (dns_packet *pkt) |
|
|
614 | { |
|
|
615 | u16 flags = ntohs (pkt->flags); |
|
|
616 | int offs = 6 * 2; // skip header |
|
|
617 | |
|
|
618 | pkt->qdcount = ntohs (pkt->qdcount); |
|
|
619 | pkt->ancount = ntohs (pkt->ancount); |
|
|
620 | |
|
|
621 | // go through our request list and find the corresponding request |
|
|
622 | for (vector<dns_req *>::iterator i = dns_sndpq.begin (); |
|
|
623 | i != dns_sndpq.end (); |
|
|
624 | ++i) |
|
|
625 | if ((*i)->pkt->id == pkt->id) |
|
|
626 | { |
|
|
627 | connection *c = (*i)->conn; |
|
|
628 | int seqno = (*i)->seqno; |
|
|
629 | u8 data[MAXSIZE], *datap = data; |
|
|
630 | |
|
|
631 | delete *i; |
|
|
632 | dns_sndpq.erase (i); |
|
|
633 | |
|
|
634 | if (flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
|
|
635 | { |
|
|
636 | char qname[MAXSIZE]; |
|
|
637 | |
|
|
638 | while (pkt->qdcount-- && offs < MAXSIZE - 4) |
|
|
639 | { |
|
|
640 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
|
|
641 | offs += 4; // skip qtype, qclass |
|
|
642 | } |
|
|
643 | |
|
|
644 | while (pkt->ancount-- && offs < MAXSIZE - 10) |
|
|
645 | { |
|
|
646 | pkt->decode_label ((char *)qname, MAXSIZE - offs, offs); |
|
|
647 | |
|
|
648 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
|
|
649 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
|
|
650 | u32 ttl = (*pkt) [offs++] << 24; |
|
|
651 | ttl |= (*pkt) [offs++] << 16; |
|
|
652 | ttl |= (*pkt) [offs++] << 8; |
|
|
653 | ttl |= (*pkt) [offs++]; |
|
|
654 | |
|
|
655 | u16 rdlen = (*pkt) [offs++] << 8; rdlen |= (*pkt) [offs++]; |
|
|
656 | |
|
|
657 | if (rdlen <= MAXSIZE - offs) |
|
|
658 | { |
|
|
659 | // decode bytes, finally |
|
|
660 | |
|
|
661 | while (rdlen) |
|
|
662 | { |
|
|
663 | int txtlen = (*pkt) [offs++]; |
|
|
664 | |
|
|
665 | assert (txtlen + offs < MAXSIZE - 1); |
|
|
666 | |
|
|
667 | memcpy (datap, pkt->at (offs), txtlen); |
|
|
668 | datap += txtlen; offs += txtlen; |
|
|
669 | |
|
|
670 | rdlen -= txtlen + 1; |
|
|
671 | } |
|
|
672 | |
|
|
673 | } |
|
|
674 | |
|
|
675 | } |
|
|
676 | } |
|
|
677 | |
|
|
678 | if (datap != data) |
|
|
679 | printf ("%02x %02x %02x %02x\n", |
|
|
680 | data[0], |
|
|
681 | data[1], |
|
|
682 | data[2], |
|
|
683 | data[3]); |
|
|
684 | |
|
|
685 | printf ("recv %d,%d\n", pkt->id, seqno, datap - data);//D |
|
|
686 | c->dnsv4_receive_rep (new dns_rcv (seqno, pkt, data, datap - data)); |
|
|
687 | |
|
|
688 | break; |
|
|
689 | } |
|
|
690 | |
|
|
691 | delete pkt; |
268 | } |
692 | } |
269 | |
693 | |
270 | void |
694 | void |
271 | vpn::dnsv4_ev (io_watcher &w, short revents) |
695 | vpn::dnsv4_ev (io_watcher &w, short revents) |
272 | { |
696 | { |
… | |
… | |
274 | { |
698 | { |
275 | dns_packet *pkt = new dns_packet; |
699 | dns_packet *pkt = new dns_packet; |
276 | struct sockaddr_in sa; |
700 | struct sockaddr_in sa; |
277 | socklen_t sa_len = sizeof (sa); |
701 | socklen_t sa_len = sizeof (sa); |
278 | |
702 | |
279 | int len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
703 | pkt->len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
280 | pkt->len = len; |
|
|
281 | |
704 | |
282 | u16 flags = ntohs (pkt->flags); |
705 | if (pkt->len > 0) |
283 | |
|
|
284 | if (THISNODE->dns_port) |
|
|
285 | { |
706 | { |
286 | // server |
707 | if (pkt->flags & htons (FLAG_TC)) |
287 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_FORMERR); |
|
|
288 | |
|
|
289 | int offs = 6 * 2; // skip header |
|
|
290 | |
|
|
291 | if (!(flags & (FLAG_RESPONSE | FLAG_OP_MASK | FLAG_TC)) |
|
|
292 | && pkt->qdcount == htons (1)) |
|
|
293 | { |
708 | { |
294 | char qname[MAXSIZE]; |
709 | slog (L_WARN, _("DNS request/response truncated, check protocol settings.")); |
295 | int qlen = pkt->decode_label ((char *)qname, MAXSIZE, offs); |
710 | //TODO connection reset |
296 | |
|
|
297 | printf ("rcvd packet len %d, id%04x flags%04x q%04x a%04x n%04x a%04x <%s>\n", len, |
|
|
298 | pkt->id, flags, pkt->qdcount, pkt->ancount, pkt->nscount, pkt->arcount, qname);//D |
|
|
299 | |
|
|
300 | u16 qtype = (*pkt) [offs++] << 8; qtype |= (*pkt) [offs++]; |
|
|
301 | u16 qclass = (*pkt) [offs++] << 8; qclass |= (*pkt) [offs++]; |
|
|
302 | |
|
|
303 | pkt->qdcount = htons (1); |
|
|
304 | pkt->ancount = 0; |
|
|
305 | pkt->nscount = 0; // should be self, as other nameservers reply like this |
|
|
306 | pkt->arcount = 0; // a record for self, as other nameservers reply like this |
|
|
307 | |
|
|
308 | pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_NXDOMAIN); |
|
|
309 | |
|
|
310 | int dlen = strlen (THISNODE->domain); |
|
|
311 | |
|
|
312 | if (qclass == RR_CLASS_IN |
|
|
313 | && (qtype == RR_TYPE_ANY || qtype == RR_TYPE_TXT) |
|
|
314 | && qlen > dlen + 1 |
|
|
315 | && !memcmp (qname + qlen - dlen - 1, THISNODE->domain, dlen)) |
|
|
316 | { |
|
|
317 | // correct class, now generate reply |
|
|
318 | pkt->ancount = htons (1); // one answer RR |
|
|
319 | |
|
|
320 | (*pkt) [offs++] = 0xc0; |
|
|
321 | (*pkt) [offs++] = 6 * 2; // same as in query section |
|
|
322 | |
|
|
323 | (*pkt) [offs++] = RR_TYPE_TXT >> 8; (*pkt) [offs++] = RR_TYPE_TXT; |
|
|
324 | (*pkt) [offs++] = RR_CLASS_IN >> 8; (*pkt) [offs++] = RR_CLASS_IN; |
|
|
325 | |
|
|
326 | (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; |
|
|
327 | (*pkt) [offs++] = 0; (*pkt) [offs++] = 0; // TTL |
|
|
328 | |
|
|
329 | dnsv4_poll (pkt, offs); |
|
|
330 | printf ("correct class\n"); |
|
|
331 | } |
|
|
332 | } |
711 | } |
333 | |
712 | |
|
|
713 | if (THISNODE->dns_port) |
|
|
714 | { |
|
|
715 | pkt = dnsv4_server (pkt); |
334 | sendto (w.fd, &((*pkt)[0]), offs, 0, (sockaddr *)&sa, sa_len); |
716 | sendto (w.fd, &((*pkt)[0]), pkt->len, 0, (sockaddr *)&sa, sa_len); |
335 | } |
717 | } |
336 | else |
718 | else |
337 | { |
719 | dnsv4_client (pkt); |
338 | // client |
|
|
339 | } |
720 | } |
340 | } |
721 | } |
341 | } |
722 | } |
342 | |
723 | |
343 | bool |
724 | bool |
344 | connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
725 | connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
345 | { |
726 | { |
346 | if (dns_sndq.size () >= MAX_OUTSTANDING) |
727 | // never initialized |
|
|
728 | if (!dns_snddq && !dns_rcvdq) |
|
|
729 | { |
|
|
730 | dns_rcvdq = new byte_stream (MAX_BACKLOG * 2); |
|
|
731 | dns_snddq = new byte_stream (MAX_BACKLOG); |
|
|
732 | |
|
|
733 | //dns_rcvseq = dns_sndseq = 0; |
|
|
734 | |
|
|
735 | dns_si.set (::conf.dns_forw_host, ::conf.dns_forw_port, PROT_DNSv4); |
|
|
736 | } |
|
|
737 | |
|
|
738 | if (!dns_snddq->put (pkt)) |
347 | return false; |
739 | return false; |
348 | |
740 | |
349 | // split vpn packet into dns packets, add to sndq |
741 | // start timer if neccessary |
350 | for (int offs = 0; offs < pkt->len; ) |
742 | if (!THISNODE->dns_port && !dnsv4_tw.active) |
351 | new dns_req (this, pkt, offs); |
|
|
352 | |
|
|
353 | // force transmit |
|
|
354 | dnsv4_cb (dnsv4_tw); |
743 | dnsv4_cb (dnsv4_tw); |
355 | |
744 | |
356 | return true; |
745 | return true; |
357 | } |
746 | } |
358 | |
747 | |
359 | void |
748 | void |
360 | connection::dnsv4_cb (time_watcher &w) |
749 | connection::dnsv4_cb (time_watcher &w) |
361 | { |
750 | { |
362 | // check for timeouts and (re)transmit |
751 | // check for timeouts and (re)transmit |
363 | tstamp next = NOW + 60; |
752 | tstamp next = NOW + 60; |
|
|
753 | dns_req *send = 0; |
364 | |
754 | |
365 | slog (L_NOISE, _("dnsv4, %d packets in send queue\n"), dns_sndq.size ()); |
|
|
366 | |
|
|
367 | if (dns_sndq.empty ()) |
|
|
368 | { |
|
|
369 | next = NOW + 1; |
|
|
370 | // push poll packet |
|
|
371 | } |
|
|
372 | |
|
|
373 | for (vector<dns_req *>::iterator i = dns_sndq.begin (); |
755 | for (vector<dns_req *>::iterator i = vpn->dns_sndpq.begin (); |
374 | i != dns_sndq.end (); |
756 | i != vpn->dns_sndpq.end (); |
375 | ++i) |
757 | ++i) |
376 | { |
758 | { |
377 | dns_req *r = *i; |
759 | dns_req *r = *i; |
378 | |
760 | |
379 | if (r->next <= NOW) |
761 | if (r->next <= NOW) |
380 | { |
762 | { |
381 | slog (L_NOISE, _("dnsv4, send req %d\n"), r->pkt->id); |
763 | if (!send) |
|
|
764 | { |
|
|
765 | send = r; |
|
|
766 | |
|
|
767 | if (r->retry)//D |
|
|
768 | printf ("req %d, retry %d\n", r->pkt->id, r->retry); |
382 | r->retry++; |
769 | r->retry++; |
383 | r->next = NOW + r->retry; |
770 | r->next = NOW + r->retry; |
|
|
771 | } |
384 | } |
772 | } |
385 | |
773 | |
386 | if (r->next < next) |
774 | if (r->next < next) |
387 | next = r->next; |
775 | next = r->next; |
388 | } |
776 | } |
389 | |
777 | |
390 | printf ("%f next\n", next); |
778 | if (!send |
|
|
779 | && vpn->dns_sndpq.size () < MAX_OUTSTANDING) |
|
|
780 | { |
|
|
781 | send = new dns_req (this); |
|
|
782 | send->gen_stream_req (dns_sndseq, dns_snddq); |
|
|
783 | vpn->dns_sndpq.push_back (send); |
|
|
784 | |
|
|
785 | dns_sndseq = (dns_sndseq + 1) & SEQNO_MASK; |
|
|
786 | } |
|
|
787 | |
|
|
788 | tstamp min_next = NOW + (1. / (tstamp)MAX_RATE); |
|
|
789 | |
|
|
790 | if (send) |
|
|
791 | { |
|
|
792 | dns_packet *pkt = send->pkt; |
|
|
793 | |
|
|
794 | next = min_next; |
|
|
795 | |
|
|
796 | sendto (vpn->dnsv4_fd, &((*pkt)[0]), pkt->len, 0, dns_si.sav4 (), dns_si.salenv4 ()); |
|
|
797 | } |
|
|
798 | else if (next < min_next) |
|
|
799 | next = min_next; |
|
|
800 | |
391 | w.start (next); |
801 | w.start (next); |
392 | } |
802 | } |
393 | |
803 | |
394 | #endif |
804 | #endif |
395 | |
805 | |