blob: fe98400f7d6047fc38c40e02f317afa8e51b4c2d [file] [log] [blame]
Bernhard Reutner-Fischerdac7ff12006-04-12 17:55:51 +00001/* vi: set sw=4 ts=4: */
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +00002/*
3 * Mini DNS server implementation for busybox
4 *
5 * Copyright (C) 2005 Roberto A. Foglietta (me@roberto.foglietta.name)
6 * Copyright (C) 2005 Odd Arild Olsen (oao at fibula dot no)
7 * Copyright (C) 2003 Paul Sheer
Bernhard Reutner-Fischer2c998512006-04-12 18:09:26 +00008 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02009 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000010 *
11 * Odd Arild Olsen started out with the sheerdns [1] of Paul Sheer and rewrote
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000012 * it into a shape which I believe is both easier to understand and maintain.
13 * I also reused the input buffer for output and removed services he did not
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000014 * need. [1] http://threading.2038bug.com/sheerdns/
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000015 *
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000016 * Some bugfix and minor changes was applied by Roberto A. Foglietta who made
17 * the first porting of oao' scdns to busybox also.
18 */
19
Pere Orga5bc8c002011-04-11 03:29:49 +020020//usage:#define dnsd_trivial_usage
21//usage: "[-dvs] [-c CONFFILE] [-t TTL_SEC] [-p PORT] [-i ADDR]"
22//usage:#define dnsd_full_usage "\n\n"
23//usage: "Small static DNS server daemon\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020024//usage: "\n -c FILE Config file"
25//usage: "\n -t SEC TTL"
26//usage: "\n -p PORT Listen on PORT"
27//usage: "\n -i ADDR Listen on ADDR"
28//usage: "\n -d Daemonize"
29//usage: "\n -v Verbose"
30//usage: "\n -s Send successful replies only. Use this if you want"
31//usage: "\n to use /etc/resolv.conf with two nameserver lines:"
32//usage: "\n nameserver DNSD_SERVER"
Dan Fandrichb5de0c12011-07-08 05:47:49 +020033//usage: "\n nameserver NORMAL_DNS_SERVER"
Pere Orga5bc8c002011-04-11 03:29:49 +020034
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000035#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000036#include <syslog.h>
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000037
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000038//#define DEBUG 1
Denis Vlasenko2c916522007-01-12 14:57:37 +000039#define DEBUG 0
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000040
Rob Landleybc68cd12006-03-10 19:22:06 +000041enum {
Denis Vlasenkoef1b4392009-04-12 19:03:01 +000042 /* can tweak this */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000043 DEFAULT_TTL = 120,
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000044
Denis Vlasenkoef1b4392009-04-12 19:03:01 +000045 /* cannot get bigger packets than 512 per RFC1035. */
Denis Vlasenko081eb712008-03-17 09:02:21 +000046 MAX_PACK_LEN = 512,
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000047 IP_STRING_LEN = sizeof(".xxx.xxx.xxx.xxx"),
48 MAX_NAME_LEN = IP_STRING_LEN - 1 + sizeof(".in-addr.arpa"),
Rob Landleybc68cd12006-03-10 19:22:06 +000049 REQ_A = 1,
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000050 REQ_PTR = 12,
Rob Landleybc68cd12006-03-10 19:22:06 +000051};
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000052
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000053/* the message from client and first part of response msg */
54struct dns_head {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000055 uint16_t id;
56 uint16_t flags;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000057 uint16_t nquer;
58 uint16_t nansw;
59 uint16_t nauth;
60 uint16_t nadd;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000061};
Denys Vlasenko6646de02010-04-26 14:25:33 +020062/* Structure used to access type and class fields.
63 * They are totally unaligned, but gcc 4.3.4 thinks that pointer of type uint16_t*
64 * is 16-bit aligned and replaces 16-bit memcpy (in move_from_unaligned16 macro)
65 * with aligned halfword access on arm920t!
66 * Oh well. Slapping PACKED everywhere seems to help: */
Denys Vlasenko5fb38492010-02-06 22:48:10 +010067struct type_and_class {
Denys Vlasenko6646de02010-04-26 14:25:33 +020068 uint16_t type PACKED;
69 uint16_t class PACKED;
70} PACKED;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000071/* element of known name, ip address and reversed ip address */
72struct dns_entry {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000073 struct dns_entry *next;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000074 uint32_t ip;
75 char rip[IP_STRING_LEN]; /* length decimal reversed IP */
76 char name[1];
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000077};
78
Denys Vlasenko343dfd72010-02-07 02:45:03 +010079#define OPT_verbose (option_mask32 & 1)
80#define OPT_silent (option_mask32 & 2)
Denis Vlasenkod3bac032007-03-24 12:13:04 +000081
82
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000083/*
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +000084 * Insert length of substrings instead of dots
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000085 */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000086static void undot(char *rip)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000087{
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000088 int i = 0;
89 int s = 0;
90
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000091 while (rip[i])
92 i++;
93 for (--i; i >= 0; i--) {
94 if (rip[i] == '.') {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000095 rip[i] = s;
96 s = 0;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000097 } else {
98 s++;
99 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000100 }
101}
102
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000103/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000104 * Read hostname/IP records from file
105 */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000106static struct dns_entry *parse_conf_file(const char *fileconf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000107{
Denis Vlasenko084266e2008-07-26 23:08:31 +0000108 char *token[2];
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000109 parser_t *parser;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000110 struct dns_entry *m, *conf_data;
111 struct dns_entry **nextp;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000112
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000113 conf_data = NULL;
114 nextp = &conf_data;
115
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000116 parser = config_open(fileconf);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000117 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000118 struct in_addr ip;
119 uint32_t v32;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000120
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000121 if (inet_aton(token[1], &ip) == 0) {
122 bb_error_msg("error at line %u, skipping", parser->lineno);
123 continue;
124 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000125
Denis Vlasenko084266e2008-07-26 23:08:31 +0000126 if (OPT_verbose)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000127 bb_error_msg("name:%s, ip:%s", token[0], token[1]);
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000128
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000129 /* sizeof(*m) includes 1 byte for m->name[0] */
130 m = xzalloc(sizeof(*m) + strlen(token[0]) + 1);
131 /*m->next = NULL;*/
132 *nextp = m;
133 nextp = &m->next;
134
135 m->name[0] = '.';
136 strcpy(m->name + 1, token[0]);
137 undot(m->name);
138 m->ip = ip.s_addr; /* in network order */
139 v32 = ntohl(m->ip);
140 /* inverted order */
141 sprintf(m->rip, ".%u.%u.%u.%u",
142 (uint8_t)(v32),
143 (uint8_t)(v32 >> 8),
144 (uint8_t)(v32 >> 16),
145 (v32 >> 24)
146 );
147 undot(m->rip);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000148 }
Denis Vlasenko084266e2008-07-26 23:08:31 +0000149 config_close(parser);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000150 return conf_data;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000151}
152
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000153/*
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000154 * Look query up in dns records and return answer if found.
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000155 */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000156static char *table_lookup(struct dns_entry *d,
157 uint16_t type,
158 char* query_string)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000159{
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000160 while (d) {
161 unsigned len = d->name[0];
162 /* d->name[len] is the last (non NUL) char */
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000163#if DEBUG
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000164 char *p, *q;
Denis Vlasenko5c329932009-04-12 12:16:21 +0000165 q = query_string + 1;
166 p = d->name + 1;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000167 fprintf(stderr, "%d/%d p:%s q:%s %d\n",
168 (int)strlen(p), len,
169 p, q, (int)strlen(q)
170 );
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000171#endif
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000172 if (type == htons(REQ_A)) {
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000173 /* search by host name */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000174 if (len != 1 || d->name[1] != '*') {
Denis Vlasenko5c329932009-04-12 12:16:21 +0000175/* we are lax, hope no name component is ever >64 so that length
176 * (which will be represented as 'A','B'...) matches a lowercase letter.
177 * Actually, I think false matches are hard to construct.
178 * Example.
179 * [31] len is represented as '1', [65] as 'A', [65+32] as 'a'.
180 * [65] <65 same chars>[31]<31 same chars>NUL
181 * [65+32]<65 same chars>1 <31 same chars>NUL
182 * This example seems to be the minimal case when false match occurs.
183 */
184 if (strcasecmp(d->name, query_string) != 0)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000185 goto next;
186 }
Denis Vlasenko5c329932009-04-12 12:16:21 +0000187 return (char *)&d->ip;
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000188#if DEBUG
Denis Vlasenko5c329932009-04-12 12:16:21 +0000189 fprintf(stderr, "Found IP:%x\n", (int)d->ip);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000190#endif
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000191 return 0;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000192 }
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000193 /* search by IP-address */
194 if ((len != 1 || d->name[1] != '*')
Denis Vlasenko5c329932009-04-12 12:16:21 +0000195 /* we assume (do not check) that query_string
196 * ends in ".in-addr.arpa" */
197 && strncmp(d->rip, query_string, strlen(d->rip)) == 0
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000198 ) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000199#if DEBUG
Denis Vlasenko5c329932009-04-12 12:16:21 +0000200 fprintf(stderr, "Found name:%s\n", d->name);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000201#endif
Denis Vlasenko5c329932009-04-12 12:16:21 +0000202 return d->name;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000203 }
204 next:
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000205 d = d->next;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000206 }
207
Denis Vlasenko5c329932009-04-12 12:16:21 +0000208 return NULL;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000209}
210
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000211/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000212 * Decode message and generate answer
213 */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000214/* RFC 1035
215...
216Whenever an octet represents a numeric quantity, the left most bit
217in the diagram is the high order or most significant bit.
218That is, the bit labeled 0 is the most significant bit.
219...
220
2214.1.1. Header section format
Denis Vlasenko5c329932009-04-12 12:16:21 +0000222 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000223 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
224 | ID |
225 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Denis Vlasenko5c329932009-04-12 12:16:21 +0000226 |QR| OPCODE |AA|TC|RD|RA| 0 0 0| RCODE |
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000227 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
228 | QDCOUNT |
229 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
230 | ANCOUNT |
231 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
232 | NSCOUNT |
233 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
234 | ARCOUNT |
235 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Denis Vlasenko5c329932009-04-12 12:16:21 +0000236ID 16 bit random identifier assigned by querying peer.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000237 Used to match query/response.
238QR message is a query (0), or a response (1).
239OPCODE 0 standard query (QUERY)
240 1 inverse query (IQUERY)
241 2 server status request (STATUS)
Denis Vlasenko5c329932009-04-12 12:16:21 +0000242AA Authoritative Answer - this bit is valid in responses.
243 Responding name server is an authority for the domain name
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000244 in question section. Answer section may have multiple owner names
245 because of aliases. The AA bit corresponds to the name which matches
246 the query name, or the first owner name in the answer section.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000247TC TrunCation - this message was truncated.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000248RD Recursion Desired - this bit may be set in a query and
249 is copied into the response. If RD is set, it directs
250 the name server to pursue the query recursively.
251 Recursive query support is optional.
252RA Recursion Available - this be is set or cleared in a
253 response, and denotes whether recursive query support is
254 available in the name server.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000255RCODE Response code.
256 0 No error condition
257 1 Format error
Denis Vlasenko5c329932009-04-12 12:16:21 +0000258 2 Server failure - server was unable to process the query
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000259 due to a problem with the name server.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000260 3 Name Error - meaningful only for responses from
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000261 an authoritative name server. The referenced domain name
262 does not exist.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000263 4 Not Implemented.
264 5 Refused.
265QDCOUNT number of entries in the question section.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000266ANCOUNT number of records in the answer section.
267NSCOUNT number of records in the authority records section.
268ARCOUNT number of records in the additional records section.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000269
2704.1.2. Question section format
271
Denis Vlasenko5c329932009-04-12 12:16:21 +0000272The section contains QDCOUNT (usually 1) entries, each of this format:
273 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000274 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
275 / QNAME /
276 / /
277 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
278 | QTYPE |
279 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
280 | QCLASS |
281 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
282QNAME a domain name represented as a sequence of labels, where
283 each label consists of a length octet followed by that
284 number of octets. The domain name terminates with the
285 zero length octet for the null label of the root. Note
286 that this field may be an odd number of octets; no
287 padding is used.
288QTYPE a two octet type of the query.
289 1 a host address [REQ_A const]
290 2 an authoritative name server
291 3 a mail destination (Obsolete - use MX)
292 4 a mail forwarder (Obsolete - use MX)
293 5 the canonical name for an alias
294 6 marks the start of a zone of authority
295 7 a mailbox domain name (EXPERIMENTAL)
296 8 a mail group member (EXPERIMENTAL)
297 9 a mail rename domain name (EXPERIMENTAL)
298 10 a null RR (EXPERIMENTAL)
299 11 a well known service description
300 12 a domain name pointer [REQ_PTR const]
301 13 host information
302 14 mailbox or mail list information
303 15 mail exchange
304 16 text strings
305 0x1c IPv6?
306 252 a request for a transfer of an entire zone
307 253 a request for mailbox-related records (MB, MG or MR)
308 254 a request for mail agent RRs (Obsolete - see MX)
309 255 a request for all records
310QCLASS a two octet code that specifies the class of the query.
311 1 the Internet
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000312 (others are historic only)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000313 255 any class
314
Denys Vlasenko5fb38492010-02-06 22:48:10 +01003154.1.3. Resource Record format
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000316
Denis Vlasenko5c329932009-04-12 12:16:21 +0000317The answer, authority, and additional sections all share the same format:
318a variable number of resource records, where the number of records
319is specified in the corresponding count field in the header.
320Each resource record has this format:
321 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000322 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
323 / /
324 / NAME /
325 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
326 | TYPE |
327 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
328 | CLASS |
329 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
330 | TTL |
331 | |
332 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
333 | RDLENGTH |
334 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
335 / RDATA /
336 / /
337 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
338NAME a domain name to which this resource record pertains.
339TYPE two octets containing one of the RR type codes. This
Denis Vlasenko5c329932009-04-12 12:16:21 +0000340 field specifies the meaning of the data in the RDATA field.
341CLASS two octets which specify the class of the data in the RDATA field.
342TTL a 32 bit unsigned integer that specifies the time interval
343 (in seconds) that the record may be cached.
344RDLENGTH a 16 bit integer, length in octets of the RDATA field.
345RDATA a variable length string of octets that describes the resource.
346 The format of this information varies according to the TYPE
347 and CLASS of the resource record.
348 If the TYPE is A and the CLASS is IN, it's a 4 octet IP address.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000349
3504.1.4. Message compression
351
Denis Vlasenko5c329932009-04-12 12:16:21 +0000352In order to reduce the size of messages, domain names coan be compressed.
353An entire domain name or a list of labels at the end of a domain name
354is replaced with a pointer to a prior occurance of the same name.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000355
356The pointer takes the form of a two octet sequence:
357 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
358 | 1 1| OFFSET |
359 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
360The first two bits are ones. This allows a pointer to be distinguished
361from a label, since the label must begin with two zero bits because
362labels are restricted to 63 octets or less. The OFFSET field specifies
363an offset from the start of the message (i.e., the first octet
364of the ID field in the domain header).
365A zero offset specifies the first byte of the ID field, etc.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000366Domain name in a message can be represented as either:
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000367 - a sequence of labels ending in a zero octet
368 - a pointer
369 - a sequence of labels ending with a pointer
370 */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000371static int process_packet(struct dns_entry *conf_data,
372 uint32_t conf_ttl,
373 uint8_t *buf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000374{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000375 struct dns_head *head;
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100376 struct type_and_class *unaligned_type_class;
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100377 const char *err_msg;
Denis Vlasenko5c329932009-04-12 12:16:21 +0000378 char *query_string;
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100379 char *answstr;
Denis Vlasenko5c329932009-04-12 12:16:21 +0000380 uint8_t *answb;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000381 uint16_t outr_rlen;
382 uint16_t outr_flags;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000383 uint16_t type;
384 uint16_t class;
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100385 int query_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000386
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000387 head = (struct dns_head *)buf;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000388 if (head->nquer == 0) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000389 bb_error_msg("packet has 0 queries, ignored");
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100390 return 0; /* don't reply */
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000391 }
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000392 if (head->flags & htons(0x8000)) { /* QR bit */
393 bb_error_msg("response packet, ignored");
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100394 return 0; /* don't reply */
395 }
396 /* QR = 1 "response", RCODE = 4 "Not Implemented" */
397 outr_flags = htons(0x8000 | 4);
398 err_msg = NULL;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000399
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000400 /* start of query string */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000401 query_string = (void *)(head + 1);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000402 /* caller guarantees strlen is <= MAX_PACK_LEN */
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100403 query_len = strlen(query_string) + 1;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000404 /* may be unaligned! */
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100405 unaligned_type_class = (void *)(query_string + query_len);
Denys Vlasenkodc8ef352010-10-29 00:37:56 +0200406 query_len += sizeof(*unaligned_type_class);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000407 /* where to append answer block */
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100408 answb = (void *)(unaligned_type_class + 1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000409
Denys Vlasenkocbcc1232010-03-05 23:38:54 +0100410 /* OPCODE != 0 "standard query"? */
411 if ((head->flags & htons(0x7800)) != 0) {
412 err_msg = "opcode != 0";
413 goto empty_packet;
414 }
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100415 move_from_unaligned16(class, &unaligned_type_class->class);
416 if (class != htons(1)) { /* not class INET? */
417 err_msg = "class != 1";
418 goto empty_packet;
419 }
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100420 move_from_unaligned16(type, &unaligned_type_class->type);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000421 if (type != htons(REQ_A) && type != htons(REQ_PTR)) {
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100422 /* we can't handle this query type */
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100423//TODO: happens all the time with REQ_AAAA (0x1c) requests - implement those?
424 err_msg = "type is !REQ_A and !REQ_PTR";
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000425 goto empty_packet;
426 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000427
Denis Vlasenko5c329932009-04-12 12:16:21 +0000428 /* look up the name */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000429 answstr = table_lookup(conf_data, type, query_string);
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100430#if DEBUG
431 /* Shows lengths instead of dots, unusable for !DEBUG */
432 bb_error_msg("'%s'->'%s'", query_string, answstr);
433#endif
Denis Vlasenko5c329932009-04-12 12:16:21 +0000434 outr_rlen = 4;
435 if (answstr && type == htons(REQ_PTR)) {
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100436 /* returning a host name */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000437 outr_rlen = strlen(answstr) + 1;
438 }
439 if (!answstr
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100440 || (unsigned)(answb - buf) + query_len + 4 + 2 + outr_rlen > MAX_PACK_LEN
Denis Vlasenko5c329932009-04-12 12:16:21 +0000441 ) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000442 /* QR = 1 "response"
443 * AA = 1 "Authoritative Answer"
444 * RCODE = 3 "Name Error" */
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100445 err_msg = "name is not found";
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000446 outr_flags = htons(0x8000 | 0x0400 | 3);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000447 goto empty_packet;
448 }
Denis Vlasenko5c329932009-04-12 12:16:21 +0000449
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100450 /* Append answer Resource Record */
451 memcpy(answb, query_string, query_len); /* name, type, class */
452 answb += query_len;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000453 move_to_unaligned32((uint32_t *)answb, htonl(conf_ttl));
Denis Vlasenko081eb712008-03-17 09:02:21 +0000454 answb += 4;
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100455 move_to_unaligned16((uint16_t *)answb, htons(outr_rlen));
Denis Vlasenko081eb712008-03-17 09:02:21 +0000456 answb += 2;
457 memcpy(answb, answstr, outr_rlen);
458 answb += outr_rlen;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000459
Denis Vlasenko5c329932009-04-12 12:16:21 +0000460 /* QR = 1 "response",
461 * AA = 1 "Authoritative Answer",
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100462 * TODO: need to set RA bit 0x80? One user says nslookup complains
463 * "Got recursion not available from SERVER, trying next server"
464 * "** server can't find HOSTNAME"
465 * RCODE = 0 "success"
466 */
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100467 if (OPT_verbose)
468 bb_error_msg("returning positive reply");
Denis Vlasenko5c329932009-04-12 12:16:21 +0000469 outr_flags = htons(0x8000 | 0x0400 | 0);
470 /* we have one answer */
471 head->nansw = htons(1);
472
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000473 empty_packet:
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100474 if ((outr_flags & htons(0xf)) != 0) { /* not a positive response */
475 if (OPT_verbose) {
476 bb_error_msg("%s, %s",
477 err_msg,
478 OPT_silent ? "dropping query" : "sending error reply"
479 );
480 }
481 if (OPT_silent)
482 return 0;
483 }
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000484 head->flags |= outr_flags;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000485 head->nauth = head->nadd = 0;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000486 head->nquer = htons(1); // why???
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000487
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000488 return answb - buf;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000489}
490
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000491int dnsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000492int dnsd_main(int argc UNUSED_PARAM, char **argv)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000493{
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000494 const char *listen_interface = "0.0.0.0";
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000495 const char *fileconf = "/etc/dnsd.conf";
496 struct dns_entry *conf_data;
497 uint32_t conf_ttl = DEFAULT_TTL;
Denis Vlasenko2c916522007-01-12 14:57:37 +0000498 char *sttl, *sport;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000499 len_and_sockaddr *lsa, *from, *to;
500 unsigned lsa_size;
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000501 int udps, opts;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000502 uint16_t port = 53;
Denys Vlasenko0ecc1162010-04-14 10:14:25 -0700503 /* Ensure buf is 32bit aligned (we need 16bit, but 32bit can't hurt) */
504 uint8_t buf[MAX_PACK_LEN + 1] ALIGN4;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000505
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100506 opts = getopt32(argv, "vsi:c:t:p:d", &listen_interface, &fileconf, &sttl, &sport);
507 //if (opts & (1 << 0)) // -v
508 //if (opts & (1 << 1)) // -s
509 //if (opts & (1 << 2)) // -i
510 //if (opts & (1 << 3)) // -c
511 if (opts & (1 << 4)) // -t
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000512 conf_ttl = xatou_range(sttl, 1, 0xffffffff);
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100513 if (opts & (1 << 5)) // -p
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000514 port = xatou_range(sport, 1, 0xffff);
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100515 if (opts & (1 << 6)) { // -d
Denis Vlasenko5a142022007-03-26 13:20:54 +0000516 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000517 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000518 logmode = LOGMODE_SYSLOG;
519 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000520
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000521 conf_data = parse_conf_file(fileconf);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000522
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000523 lsa = xdotted2sockaddr(listen_interface, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000524 udps = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
525 xbind(udps, &lsa->u.sa, lsa->len);
Denis Vlasenko081eb712008-03-17 09:02:21 +0000526 socket_want_pktinfo(udps); /* needed for recv_from_to to work */
527 lsa_size = LSA_LEN_SIZE + lsa->len;
528 from = xzalloc(lsa_size);
529 to = xzalloc(lsa_size);
530
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000531 {
532 char *p = xmalloc_sockaddr2dotted(&lsa->u.sa);
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100533 bb_error_msg("accepting UDP packets on %s", p);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000534 free(p);
535 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000536
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000537 while (1) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000538 int r;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000539 /* Try to get *DEST* address (to which of our addresses
540 * this query was directed), and reply from the same address.
541 * Or else we can exhibit usual UDP ugliness:
542 * [ip1.multihomed.ip2] <= query to ip1 <= peer
543 * [ip1.multihomed.ip2] => reply from ip2 => peer (confused) */
544 memcpy(to, lsa, lsa_size);
545 r = recv_from_to(udps, buf, MAX_PACK_LEN + 1, 0, &from->u.sa, &to->u.sa, lsa->len);
546 if (r < 12 || r > MAX_PACK_LEN) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000547 bb_error_msg("packet size %d, ignored", r);
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000548 continue;
Denis Vlasenko2c916522007-01-12 14:57:37 +0000549 }
Denis Vlasenko081eb712008-03-17 09:02:21 +0000550 if (OPT_verbose)
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100551 bb_error_msg("got UDP packet");
Denis Vlasenko081eb712008-03-17 09:02:21 +0000552 buf[r] = '\0'; /* paranoia */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000553 r = process_packet(conf_data, conf_ttl, buf);
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000554 if (r <= 0)
555 continue;
Denis Vlasenkoe9b76e12008-05-22 17:41:01 +0000556 send_to_from(udps, buf, r, 0, &from->u.sa, &to->u.sa, lsa->len);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000557 }
Denis Vlasenkob5b45a92007-03-24 13:09:07 +0000558 return 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000559}