1 |
pcg |
1.1 |
/* |
2 |
|
|
vpn_dns.C -- handle the dns tunnel part of the protocol. |
3 |
|
|
Copyright (C) 2003-2004 Marc Lehmann <pcg@goof.com> |
4 |
|
|
|
5 |
|
|
This program is free software; you can redistribute it and/or modify |
6 |
|
|
it under the terms of the GNU General Public License as published by |
7 |
|
|
the Free Software Foundation; either version 2 of the License, or |
8 |
|
|
(at your option) any later version. |
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, |
11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
GNU General Public License for more details. |
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU General Public License |
16 |
|
|
along with this program; if not, write to the Free Software |
17 |
|
|
Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
18 |
|
|
*/ |
19 |
|
|
|
20 |
|
|
#include "config.h" |
21 |
|
|
|
22 |
|
|
#if ENABLE_DNS |
23 |
|
|
|
24 |
|
|
// dns processing is EXTREMELY ugly. For obvious(?) reasons. |
25 |
|
|
// it's a hack, use only in emergency situations please. |
26 |
|
|
|
27 |
|
|
#include <cstring> |
28 |
|
|
|
29 |
|
|
#include <sys/types.h> |
30 |
|
|
#include <sys/socket.h> |
31 |
|
|
#include <sys/wait.h> |
32 |
|
|
#include <sys/uio.h> |
33 |
|
|
#include <errno.h> |
34 |
|
|
#include <time.h> |
35 |
|
|
#include <unistd.h> |
36 |
|
|
#include <fcntl.h> |
37 |
|
|
|
38 |
|
|
#include <map> |
39 |
|
|
|
40 |
|
|
#include "netcompat.h" |
41 |
|
|
|
42 |
|
|
#include "vpn.h" |
43 |
|
|
|
44 |
|
|
#define MIN_RETRY 1. |
45 |
|
|
#define MAX_RETRY 60. |
46 |
|
|
|
47 |
pcg |
1.3 |
#define MAX_OUTSTANDING 10 // max. outstanding requests |
48 |
pcg |
1.1 |
|
49 |
|
|
/* |
50 |
|
|
|
51 |
|
|
protocol, in shorthand :) |
52 |
|
|
|
53 |
pcg |
1.2 |
client -> server <req> ANY? |
54 |
|
|
server -> client <req> TXT <rep> |
55 |
pcg |
1.1 |
|
56 |
pcg |
1.2 |
<req> is dns64-encoded <client-id:12><recv-seqno:10>[<send-seqno:10><data>] |
57 |
|
|
<rep> is dns64-encoded <0:12><recv-seqno:10>[<send-seqno:10><data>] |
58 |
|
|
|
59 |
|
|
if <client-id> is zero, the connection will be configured: |
60 |
|
|
|
61 |
|
|
<0:12><0:4>client-id:12><default-ttl:8><max-size:16><flags:16> |
62 |
pcg |
1.1 |
|
63 |
|
|
*/ |
64 |
|
|
|
65 |
pcg |
1.2 |
#define MAX_LBL_SIZE 63 |
66 |
|
|
#define MAX_PKT_SIZE 512 |
67 |
|
|
|
68 |
|
|
#define RR_TYPE_TXT 16 |
69 |
|
|
#define RR_TYPE_ANY 255 |
70 |
|
|
#define RR_CLASS_IN 1 |
71 |
|
|
|
72 |
|
|
// the "_" is not valid but widely accepted (all octets should be supported, but let's be conservative) |
73 |
|
|
struct dns64 |
74 |
|
|
{ |
75 |
|
|
static const char encode_chars[64 + 1]; |
76 |
|
|
static s8 decode_chars[256]; |
77 |
|
|
|
78 |
|
|
static int encode_len (int bytes) { return (bytes * 8 + 5) / 6; } |
79 |
|
|
static int decode_len (int bytes) { return (bytes * 6) / 8; } |
80 |
|
|
static void encode (char *dst, u8 *src, int len); |
81 |
|
|
static void decode (u8 *dst, char *src, int len); |
82 |
|
|
|
83 |
|
|
dns64 (); |
84 |
|
|
} dns64; |
85 |
|
|
|
86 |
|
|
const char dns64::encode_chars[64 + 1] = "0123456789-abcdefghijklmnopqrstuvwxyz_ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
87 |
|
|
s8 dns64::decode_chars[256]; |
88 |
|
|
|
89 |
|
|
dns64::dns64 () |
90 |
|
|
{ |
91 |
|
|
for (int i = 0; i < 64; i++) |
92 |
|
|
decode_chars [encode_chars [i]] = i + 1; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
void dns64::encode (char *dst, u8 *src, int len) |
96 |
|
|
{ |
97 |
|
|
// slow, but easy to debug |
98 |
|
|
unsigned int accum, bits = 0; |
99 |
|
|
|
100 |
|
|
while (len--) |
101 |
|
|
{ |
102 |
|
|
accum <<= 8; |
103 |
|
|
accum |= *src++; |
104 |
|
|
bits += 8; |
105 |
|
|
|
106 |
|
|
while (bits >= 6) |
107 |
|
|
{ |
108 |
|
|
*dst++ = encode_chars [(accum >> (bits - 6)) & 63]; |
109 |
|
|
bits -= 6; |
110 |
|
|
} |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
if (bits) |
114 |
|
|
*dst++ = encode_chars [(accum << (6 - bits)) & 63]; |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
void dns64::decode (u8 *dst, char *src, int len) |
118 |
|
|
{ |
119 |
|
|
// slow, but easy to debug |
120 |
|
|
unsigned int accum, bits = 0; |
121 |
|
|
|
122 |
|
|
while (len--) |
123 |
|
|
{ |
124 |
|
|
s8 chr = decode_chars [(u8)*src++]; |
125 |
|
|
|
126 |
|
|
|
127 |
|
|
if (!chr) |
128 |
|
|
break; |
129 |
|
|
|
130 |
|
|
accum <<= 6; |
131 |
|
|
accum |= chr - 1; |
132 |
|
|
bits += 6; |
133 |
|
|
|
134 |
|
|
while (bits >= 8) |
135 |
|
|
{ |
136 |
|
|
*dst++ = accum >> (bits - 8); |
137 |
|
|
bits -= 8; |
138 |
|
|
} |
139 |
|
|
} |
140 |
|
|
} |
141 |
|
|
|
142 |
pcg |
1.3 |
///////////////////////////////////////////////////////////////////////////// |
143 |
|
|
|
144 |
pcg |
1.2 |
#define FLAG_QUERY ( 0 << 15) |
145 |
|
|
#define FLAG_RESPONSE ( 1 << 15) |
146 |
|
|
#define FLAG_OP_MASK (15 << 14) |
147 |
|
|
#define FLAG_OP_QUERY ( 0 << 11) |
148 |
|
|
#define FLAG_AA ( 1 << 10) |
149 |
|
|
#define FLAG_TC ( 1 << 9) |
150 |
|
|
#define FLAG_RD ( 1 << 8) |
151 |
|
|
#define FLAG_RA ( 1 << 7) |
152 |
|
|
#define FLAG_RCODE_MASK (15 << 0) |
153 |
|
|
#define FLAG_RCODE_OK ( 0 << 0) |
154 |
|
|
#define FLAG_RCODE_FORMERR ( 1 << 0) |
155 |
|
|
#define FLAG_RCODE_SERVFAIL ( 2 << 0) |
156 |
|
|
#define FLAG_RCODE_NXDOMAIN ( 3 << 0) |
157 |
|
|
#define FLAG_RCODE_REFUSED ( 5 << 0) |
158 |
|
|
|
159 |
|
|
#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) |
161 |
|
|
|
162 |
|
|
struct dns_packet : net_packet |
163 |
|
|
{ |
164 |
|
|
u16 id; |
165 |
|
|
u16 flags; // QR:1 Opcode:4 AA:1 TC:1 RD:1 RA:1 Z:3 RCODE:4 |
166 |
|
|
u16 qdcount, ancount, nscount, arcount; |
167 |
|
|
|
168 |
|
|
u8 data[MAXSIZE - 6 * 2]; |
169 |
|
|
|
170 |
|
|
int decode_label (char *data, int size, int &offs); |
171 |
|
|
}; |
172 |
|
|
|
173 |
|
|
int dns_packet::decode_label (char *data, int size, int &offs) |
174 |
|
|
{ |
175 |
|
|
char *orig = data; |
176 |
|
|
|
177 |
|
|
memset (data, 0, size); |
178 |
|
|
|
179 |
|
|
while (offs < size - 1) |
180 |
|
|
{ |
181 |
|
|
u8 len = (*this)[offs++]; |
182 |
|
|
|
183 |
|
|
if (!len) |
184 |
|
|
break; |
185 |
|
|
else if (len < 64) |
186 |
|
|
{ |
187 |
|
|
if (size < len + 1 || offs + len >= MAXSIZE - 1) |
188 |
|
|
break; |
189 |
|
|
|
190 |
|
|
memcpy (data, &((*this)[offs]), len); |
191 |
|
|
|
192 |
|
|
data += len; size -= len; offs += len; |
193 |
|
|
*data++ = '.'; size--; |
194 |
|
|
} |
195 |
|
|
else |
196 |
|
|
{ |
197 |
|
|
int offs2 = ((len & 63) << 8) + (*this)[offs++]; |
198 |
|
|
|
199 |
|
|
data += decode_label (data, size, offs2); |
200 |
|
|
break; |
201 |
|
|
} |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
return data - orig; |
205 |
|
|
} |
206 |
|
|
|
207 |
pcg |
1.3 |
///////////////////////////////////////////////////////////////////////////// |
208 |
|
|
|
209 |
|
|
struct dns_req |
210 |
|
|
{ |
211 |
|
|
dns_packet *pkt; |
212 |
|
|
tstamp next; |
213 |
|
|
int retry; |
214 |
|
|
|
215 |
|
|
dns_req (connection *c, vpn_packet *p, int &offs); |
216 |
|
|
}; |
217 |
|
|
|
218 |
|
|
struct dns_rep |
219 |
|
|
{ |
220 |
|
|
}; |
221 |
|
|
|
222 |
|
|
static u16 dns_id; // TODO: should be per-vpn |
223 |
|
|
|
224 |
|
|
dns_req::dns_req (connection *c, vpn_packet *p, int &offs) |
225 |
|
|
{ |
226 |
|
|
next = 0; |
227 |
|
|
retry = 0; |
228 |
|
|
|
229 |
|
|
pkt = new dns_packet; |
230 |
|
|
|
231 |
|
|
pkt->id = dns_id++; |
232 |
|
|
pkt->flags = DEFAULT_CLIENT_FLAGS; |
233 |
|
|
pkt->qdcount = htons (1 * 0); |
234 |
|
|
pkt->ancount = 0; |
235 |
|
|
pkt->nscount = 0; |
236 |
|
|
pkt->arcount = 0; |
237 |
|
|
|
238 |
|
|
offs += 10000; |
239 |
|
|
|
240 |
|
|
c->dns_sndq.push_back (this); |
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
///////////////////////////////////////////////////////////////////////////// |
244 |
|
|
|
245 |
pcg |
1.2 |
struct dns_cfg |
246 |
|
|
{ |
247 |
|
|
u8 id1, id2, id3; |
248 |
|
|
u8 def_ttl; |
249 |
|
|
u8 unused1; |
250 |
|
|
u16 max_size; |
251 |
|
|
u8 flags1, flags2; |
252 |
|
|
}; |
253 |
|
|
|
254 |
|
|
// reply to client-poll |
255 |
|
|
void dnsv4_poll (dns_packet *pkt, int &offs) |
256 |
|
|
{ |
257 |
|
|
int dlen = MAX_PKT_SIZE - offs - 2; |
258 |
|
|
|
259 |
|
|
(*pkt) [offs++] = 0; |
260 |
|
|
(*pkt) [offs++] = 5; |
261 |
|
|
memcpy (&((*pkt)[offs]), "\01H\02\xff\x00", 5); |
262 |
|
|
offs += 5; |
263 |
|
|
|
264 |
|
|
pkt->ancount = htons (1); |
265 |
|
|
pkt->flags = htons (DEFAULT_SERVER_FLAGS | FLAG_RCODE_OK); |
266 |
|
|
|
267 |
|
|
printf ("we have room for %d bytes\n", dlen); |
268 |
|
|
} |
269 |
|
|
|
270 |
pcg |
1.1 |
void |
271 |
|
|
vpn::dnsv4_ev (io_watcher &w, short revents) |
272 |
|
|
{ |
273 |
|
|
if (revents & EVENT_READ) |
274 |
|
|
{ |
275 |
pcg |
1.2 |
dns_packet *pkt = new dns_packet; |
276 |
pcg |
1.1 |
struct sockaddr_in sa; |
277 |
|
|
socklen_t sa_len = sizeof (sa); |
278 |
pcg |
1.2 |
|
279 |
|
|
int len = recvfrom (w.fd, &((*pkt)[0]), MAXSIZE, 0, (sockaddr *)&sa, &sa_len); |
280 |
|
|
pkt->len = len; |
281 |
|
|
|
282 |
|
|
u16 flags = ntohs (pkt->flags); |
283 |
|
|
|
284 |
|
|
if (THISNODE->dns_port) |
285 |
|
|
{ |
286 |
|
|
// server |
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 |
|
|
{ |
294 |
|
|
char qname[MAXSIZE]; |
295 |
|
|
int qlen = pkt->decode_label ((char *)qname, MAXSIZE, offs); |
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 |
|
|
} |
333 |
|
|
|
334 |
|
|
sendto (w.fd, &((*pkt)[0]), offs, 0, (sockaddr *)&sa, sa_len); |
335 |
|
|
} |
336 |
|
|
else |
337 |
|
|
{ |
338 |
|
|
// client |
339 |
|
|
} |
340 |
pcg |
1.1 |
} |
341 |
|
|
} |
342 |
|
|
|
343 |
|
|
bool |
344 |
pcg |
1.3 |
connection::send_dnsv4_packet (vpn_packet *pkt, const sockinfo &si, int tos) |
345 |
|
|
{ |
346 |
|
|
if (dns_sndq.size () >= MAX_OUTSTANDING) |
347 |
|
|
return false; |
348 |
|
|
|
349 |
|
|
// split vpn packet into dns packets, add to sndq |
350 |
|
|
for (int offs = 0; offs < pkt->len; ) |
351 |
|
|
new dns_req (this, pkt, offs); |
352 |
|
|
|
353 |
|
|
// force transmit |
354 |
|
|
dnsv4_cb (dnsv4_tw); |
355 |
|
|
|
356 |
|
|
return true; |
357 |
|
|
} |
358 |
|
|
|
359 |
|
|
void |
360 |
|
|
connection::dnsv4_cb (time_watcher &w) |
361 |
pcg |
1.1 |
{ |
362 |
pcg |
1.3 |
// check for timeouts and (re)transmit |
363 |
|
|
tstamp next = NOW + 60; |
364 |
|
|
|
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 (); |
374 |
|
|
i != dns_sndq.end (); |
375 |
|
|
++i) |
376 |
|
|
{ |
377 |
|
|
dns_req *r = *i; |
378 |
|
|
|
379 |
|
|
if (r->next <= NOW) |
380 |
|
|
{ |
381 |
|
|
slog (L_NOISE, _("dnsv4, send req %d\n"), r->pkt->id); |
382 |
|
|
r->retry++; |
383 |
|
|
r->next = NOW + r->retry; |
384 |
|
|
} |
385 |
|
|
|
386 |
|
|
if (r->next < next) |
387 |
|
|
next = r->next; |
388 |
|
|
} |
389 |
|
|
|
390 |
|
|
printf ("%f next\n", next); |
391 |
|
|
w.start (next); |
392 |
pcg |
1.1 |
} |
393 |
|
|
|
394 |
|
|
#endif |
395 |
|
|
|