blob: 1b85618c639f797bd363803ce7743cbf29720e4f [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 */
Denys Vlasenko47367e12016-11-23 09:05:14 +010019//config:config DNSD
20//config: bool "dnsd"
21//config: default y
22//config: help
23//config: Small and static DNS server daemon.
24
25//applet:IF_DNSD(APPLET(dnsd, BB_DIR_USR_SBIN, BB_SUID_DROP))
26
27//kbuild:lib-$(CONFIG_DNSD) += dnsd.o
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000028
Pere Orga5bc8c002011-04-11 03:29:49 +020029//usage:#define dnsd_trivial_usage
30//usage: "[-dvs] [-c CONFFILE] [-t TTL_SEC] [-p PORT] [-i ADDR]"
31//usage:#define dnsd_full_usage "\n\n"
32//usage: "Small static DNS server daemon\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020033//usage: "\n -c FILE Config file"
34//usage: "\n -t SEC TTL"
35//usage: "\n -p PORT Listen on PORT"
36//usage: "\n -i ADDR Listen on ADDR"
37//usage: "\n -d Daemonize"
38//usage: "\n -v Verbose"
39//usage: "\n -s Send successful replies only. Use this if you want"
40//usage: "\n to use /etc/resolv.conf with two nameserver lines:"
41//usage: "\n nameserver DNSD_SERVER"
Dan Fandrichb5de0c12011-07-08 05:47:49 +020042//usage: "\n nameserver NORMAL_DNS_SERVER"
Pere Orga5bc8c002011-04-11 03:29:49 +020043
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000044#include "libbb.h"
Bernhard Reutner-Fischerf4701962008-01-27 12:50:12 +000045#include <syslog.h>
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000046
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000047//#define DEBUG 1
Denis Vlasenko2c916522007-01-12 14:57:37 +000048#define DEBUG 0
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000049
Rob Landleybc68cd12006-03-10 19:22:06 +000050enum {
Denis Vlasenkoef1b4392009-04-12 19:03:01 +000051 /* can tweak this */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000052 DEFAULT_TTL = 120,
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000053
Denis Vlasenkoef1b4392009-04-12 19:03:01 +000054 /* cannot get bigger packets than 512 per RFC1035. */
Denis Vlasenko081eb712008-03-17 09:02:21 +000055 MAX_PACK_LEN = 512,
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000056 IP_STRING_LEN = sizeof(".xxx.xxx.xxx.xxx"),
57 MAX_NAME_LEN = IP_STRING_LEN - 1 + sizeof(".in-addr.arpa"),
Rob Landleybc68cd12006-03-10 19:22:06 +000058 REQ_A = 1,
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000059 REQ_PTR = 12,
Rob Landleybc68cd12006-03-10 19:22:06 +000060};
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000061
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000062/* the message from client and first part of response msg */
63struct dns_head {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000064 uint16_t id;
65 uint16_t flags;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000066 uint16_t nquer;
67 uint16_t nansw;
68 uint16_t nauth;
69 uint16_t nadd;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000070};
Denys Vlasenko6646de02010-04-26 14:25:33 +020071/* Structure used to access type and class fields.
72 * They are totally unaligned, but gcc 4.3.4 thinks that pointer of type uint16_t*
73 * is 16-bit aligned and replaces 16-bit memcpy (in move_from_unaligned16 macro)
74 * with aligned halfword access on arm920t!
75 * Oh well. Slapping PACKED everywhere seems to help: */
Denys Vlasenko5fb38492010-02-06 22:48:10 +010076struct type_and_class {
Denys Vlasenko6646de02010-04-26 14:25:33 +020077 uint16_t type PACKED;
78 uint16_t class PACKED;
79} PACKED;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000080/* element of known name, ip address and reversed ip address */
81struct dns_entry {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +000082 struct dns_entry *next;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000083 uint32_t ip;
84 char rip[IP_STRING_LEN]; /* length decimal reversed IP */
85 char name[1];
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000086};
87
Denys Vlasenko343dfd72010-02-07 02:45:03 +010088#define OPT_verbose (option_mask32 & 1)
89#define OPT_silent (option_mask32 & 2)
Denis Vlasenkod3bac032007-03-24 12:13:04 +000090
91
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000092/*
Bernhard Reutner-Fischerc418d482006-05-31 10:19:51 +000093 * Insert length of substrings instead of dots
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000094 */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000095static void undot(char *rip)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +000096{
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +000097 int i = 0;
98 int s = 0;
99
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000100 while (rip[i])
101 i++;
102 for (--i; i >= 0; i--) {
103 if (rip[i] == '.') {
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000104 rip[i] = s;
105 s = 0;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000106 } else {
107 s++;
108 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000109 }
110}
111
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000112/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000113 * Read hostname/IP records from file
114 */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000115static struct dns_entry *parse_conf_file(const char *fileconf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000116{
Denis Vlasenko084266e2008-07-26 23:08:31 +0000117 char *token[2];
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000118 parser_t *parser;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000119 struct dns_entry *m, *conf_data;
120 struct dns_entry **nextp;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000121
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000122 conf_data = NULL;
123 nextp = &conf_data;
124
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000125 parser = config_open(fileconf);
Denis Vlasenko084266e2008-07-26 23:08:31 +0000126 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000127 struct in_addr ip;
128 uint32_t v32;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000129
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000130 if (inet_aton(token[1], &ip) == 0) {
131 bb_error_msg("error at line %u, skipping", parser->lineno);
132 continue;
133 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000134
Denis Vlasenko084266e2008-07-26 23:08:31 +0000135 if (OPT_verbose)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000136 bb_error_msg("name:%s, ip:%s", token[0], token[1]);
Denis Vlasenkoa34f1ed2008-07-20 17:48:59 +0000137
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000138 /* sizeof(*m) includes 1 byte for m->name[0] */
139 m = xzalloc(sizeof(*m) + strlen(token[0]) + 1);
140 /*m->next = NULL;*/
141 *nextp = m;
142 nextp = &m->next;
143
144 m->name[0] = '.';
145 strcpy(m->name + 1, token[0]);
146 undot(m->name);
147 m->ip = ip.s_addr; /* in network order */
148 v32 = ntohl(m->ip);
149 /* inverted order */
150 sprintf(m->rip, ".%u.%u.%u.%u",
151 (uint8_t)(v32),
152 (uint8_t)(v32 >> 8),
153 (uint8_t)(v32 >> 16),
154 (v32 >> 24)
155 );
156 undot(m->rip);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000157 }
Denis Vlasenko084266e2008-07-26 23:08:31 +0000158 config_close(parser);
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000159 return conf_data;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000160}
161
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000162/*
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000163 * Look query up in dns records and return answer if found.
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000164 */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000165static char *table_lookup(struct dns_entry *d,
166 uint16_t type,
167 char* query_string)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000168{
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000169 while (d) {
170 unsigned len = d->name[0];
171 /* d->name[len] is the last (non NUL) char */
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000172#if DEBUG
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000173 char *p, *q;
Denis Vlasenko5c329932009-04-12 12:16:21 +0000174 q = query_string + 1;
175 p = d->name + 1;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000176 fprintf(stderr, "%d/%d p:%s q:%s %d\n",
177 (int)strlen(p), len,
178 p, q, (int)strlen(q)
179 );
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000180#endif
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000181 if (type == htons(REQ_A)) {
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000182 /* search by host name */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000183 if (len != 1 || d->name[1] != '*') {
Denis Vlasenko5c329932009-04-12 12:16:21 +0000184/* we are lax, hope no name component is ever >64 so that length
185 * (which will be represented as 'A','B'...) matches a lowercase letter.
186 * Actually, I think false matches are hard to construct.
187 * Example.
188 * [31] len is represented as '1', [65] as 'A', [65+32] as 'a'.
189 * [65] <65 same chars>[31]<31 same chars>NUL
190 * [65+32]<65 same chars>1 <31 same chars>NUL
191 * This example seems to be the minimal case when false match occurs.
192 */
193 if (strcasecmp(d->name, query_string) != 0)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000194 goto next;
195 }
Denis Vlasenko5c329932009-04-12 12:16:21 +0000196 return (char *)&d->ip;
Denis Vlasenko91f20ab2007-01-20 01:47:44 +0000197#if DEBUG
Denis Vlasenko5c329932009-04-12 12:16:21 +0000198 fprintf(stderr, "Found IP:%x\n", (int)d->ip);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000199#endif
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000200 return 0;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000201 }
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000202 /* search by IP-address */
203 if ((len != 1 || d->name[1] != '*')
Denis Vlasenko5c329932009-04-12 12:16:21 +0000204 /* we assume (do not check) that query_string
205 * ends in ".in-addr.arpa" */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100206 && is_prefixed_with(query_string, d->rip)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000207 ) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000208#if DEBUG
Denis Vlasenko5c329932009-04-12 12:16:21 +0000209 fprintf(stderr, "Found name:%s\n", d->name);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000210#endif
Denis Vlasenko5c329932009-04-12 12:16:21 +0000211 return d->name;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000212 }
213 next:
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000214 d = d->next;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000215 }
216
Denis Vlasenko5c329932009-04-12 12:16:21 +0000217 return NULL;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000218}
219
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000220/*
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000221 * Decode message and generate answer
222 */
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000223/* RFC 1035
224...
225Whenever an octet represents a numeric quantity, the left most bit
226in the diagram is the high order or most significant bit.
227That is, the bit labeled 0 is the most significant bit.
228...
229
2304.1.1. Header section format
Denis Vlasenko5c329932009-04-12 12:16:21 +0000231 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000232 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
233 | ID |
234 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Denis Vlasenko5c329932009-04-12 12:16:21 +0000235 |QR| OPCODE |AA|TC|RD|RA| 0 0 0| RCODE |
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000236 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
237 | QDCOUNT |
238 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
239 | ANCOUNT |
240 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
241 | NSCOUNT |
242 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
243 | ARCOUNT |
244 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
Denis Vlasenko5c329932009-04-12 12:16:21 +0000245ID 16 bit random identifier assigned by querying peer.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000246 Used to match query/response.
247QR message is a query (0), or a response (1).
248OPCODE 0 standard query (QUERY)
249 1 inverse query (IQUERY)
250 2 server status request (STATUS)
Denis Vlasenko5c329932009-04-12 12:16:21 +0000251AA Authoritative Answer - this bit is valid in responses.
252 Responding name server is an authority for the domain name
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000253 in question section. Answer section may have multiple owner names
254 because of aliases. The AA bit corresponds to the name which matches
255 the query name, or the first owner name in the answer section.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000256TC TrunCation - this message was truncated.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000257RD Recursion Desired - this bit may be set in a query and
258 is copied into the response. If RD is set, it directs
259 the name server to pursue the query recursively.
260 Recursive query support is optional.
261RA Recursion Available - this be is set or cleared in a
262 response, and denotes whether recursive query support is
263 available in the name server.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000264RCODE Response code.
265 0 No error condition
266 1 Format error
Denis Vlasenko5c329932009-04-12 12:16:21 +0000267 2 Server failure - server was unable to process the query
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000268 due to a problem with the name server.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000269 3 Name Error - meaningful only for responses from
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000270 an authoritative name server. The referenced domain name
271 does not exist.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000272 4 Not Implemented.
273 5 Refused.
274QDCOUNT number of entries in the question section.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000275ANCOUNT number of records in the answer section.
276NSCOUNT number of records in the authority records section.
277ARCOUNT number of records in the additional records section.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000278
2794.1.2. Question section format
280
Denis Vlasenko5c329932009-04-12 12:16:21 +0000281The section contains QDCOUNT (usually 1) entries, each of this format:
282 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000283 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
284 / QNAME /
285 / /
286 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
287 | QTYPE |
288 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
289 | QCLASS |
290 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
291QNAME a domain name represented as a sequence of labels, where
292 each label consists of a length octet followed by that
293 number of octets. The domain name terminates with the
294 zero length octet for the null label of the root. Note
295 that this field may be an odd number of octets; no
296 padding is used.
297QTYPE a two octet type of the query.
298 1 a host address [REQ_A const]
299 2 an authoritative name server
300 3 a mail destination (Obsolete - use MX)
301 4 a mail forwarder (Obsolete - use MX)
302 5 the canonical name for an alias
303 6 marks the start of a zone of authority
304 7 a mailbox domain name (EXPERIMENTAL)
305 8 a mail group member (EXPERIMENTAL)
306 9 a mail rename domain name (EXPERIMENTAL)
307 10 a null RR (EXPERIMENTAL)
308 11 a well known service description
309 12 a domain name pointer [REQ_PTR const]
310 13 host information
311 14 mailbox or mail list information
312 15 mail exchange
313 16 text strings
314 0x1c IPv6?
315 252 a request for a transfer of an entire zone
316 253 a request for mailbox-related records (MB, MG or MR)
317 254 a request for mail agent RRs (Obsolete - see MX)
318 255 a request for all records
319QCLASS a two octet code that specifies the class of the query.
320 1 the Internet
Denis Vlasenkoef1b4392009-04-12 19:03:01 +0000321 (others are historic only)
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000322 255 any class
323
Denys Vlasenko5fb38492010-02-06 22:48:10 +01003244.1.3. Resource Record format
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000325
Denis Vlasenko5c329932009-04-12 12:16:21 +0000326The answer, authority, and additional sections all share the same format:
327a variable number of resource records, where the number of records
328is specified in the corresponding count field in the header.
329Each resource record has this format:
330 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000331 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
332 / /
333 / NAME /
334 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
335 | TYPE |
336 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
337 | CLASS |
338 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
339 | TTL |
340 | |
341 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
342 | RDLENGTH |
343 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--|
344 / RDATA /
345 / /
346 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
347NAME a domain name to which this resource record pertains.
348TYPE two octets containing one of the RR type codes. This
Denis Vlasenko5c329932009-04-12 12:16:21 +0000349 field specifies the meaning of the data in the RDATA field.
350CLASS two octets which specify the class of the data in the RDATA field.
351TTL a 32 bit unsigned integer that specifies the time interval
352 (in seconds) that the record may be cached.
353RDLENGTH a 16 bit integer, length in octets of the RDATA field.
354RDATA a variable length string of octets that describes the resource.
355 The format of this information varies according to the TYPE
356 and CLASS of the resource record.
357 If the TYPE is A and the CLASS is IN, it's a 4 octet IP address.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000358
3594.1.4. Message compression
360
Denis Vlasenko5c329932009-04-12 12:16:21 +0000361In order to reduce the size of messages, domain names coan be compressed.
362An entire domain name or a list of labels at the end of a domain name
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200363is replaced with a pointer to a prior occurrence of the same name.
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000364
365The pointer takes the form of a two octet sequence:
366 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
367 | 1 1| OFFSET |
368 +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
369The first two bits are ones. This allows a pointer to be distinguished
370from a label, since the label must begin with two zero bits because
371labels are restricted to 63 octets or less. The OFFSET field specifies
372an offset from the start of the message (i.e., the first octet
373of the ID field in the domain header).
374A zero offset specifies the first byte of the ID field, etc.
Denis Vlasenko5c329932009-04-12 12:16:21 +0000375Domain name in a message can be represented as either:
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000376 - a sequence of labels ending in a zero octet
377 - a pointer
378 - a sequence of labels ending with a pointer
379 */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000380static int process_packet(struct dns_entry *conf_data,
381 uint32_t conf_ttl,
382 uint8_t *buf)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000383{
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000384 struct dns_head *head;
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100385 struct type_and_class *unaligned_type_class;
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100386 const char *err_msg;
Denis Vlasenko5c329932009-04-12 12:16:21 +0000387 char *query_string;
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100388 char *answstr;
Denis Vlasenko5c329932009-04-12 12:16:21 +0000389 uint8_t *answb;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000390 uint16_t outr_rlen;
391 uint16_t outr_flags;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000392 uint16_t type;
393 uint16_t class;
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100394 int query_len;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000395
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000396 head = (struct dns_head *)buf;
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000397 if (head->nquer == 0) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000398 bb_error_msg("packet has 0 queries, ignored");
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100399 return 0; /* don't reply */
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000400 }
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000401 if (head->flags & htons(0x8000)) { /* QR bit */
402 bb_error_msg("response packet, ignored");
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100403 return 0; /* don't reply */
404 }
405 /* QR = 1 "response", RCODE = 4 "Not Implemented" */
406 outr_flags = htons(0x8000 | 4);
407 err_msg = NULL;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000408
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000409 /* start of query string */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000410 query_string = (void *)(head + 1);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000411 /* caller guarantees strlen is <= MAX_PACK_LEN */
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100412 query_len = strlen(query_string) + 1;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000413 /* may be unaligned! */
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100414 unaligned_type_class = (void *)(query_string + query_len);
Denys Vlasenkodc8ef352010-10-29 00:37:56 +0200415 query_len += sizeof(*unaligned_type_class);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000416 /* where to append answer block */
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100417 answb = (void *)(unaligned_type_class + 1);
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000418
Denys Vlasenkocbcc1232010-03-05 23:38:54 +0100419 /* OPCODE != 0 "standard query"? */
420 if ((head->flags & htons(0x7800)) != 0) {
421 err_msg = "opcode != 0";
422 goto empty_packet;
423 }
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100424 move_from_unaligned16(class, &unaligned_type_class->class);
425 if (class != htons(1)) { /* not class INET? */
426 err_msg = "class != 1";
427 goto empty_packet;
428 }
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100429 move_from_unaligned16(type, &unaligned_type_class->type);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000430 if (type != htons(REQ_A) && type != htons(REQ_PTR)) {
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100431 /* we can't handle this query type */
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100432//TODO: happens all the time with REQ_AAAA (0x1c) requests - implement those?
433 err_msg = "type is !REQ_A and !REQ_PTR";
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000434 goto empty_packet;
435 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000436
Denis Vlasenko5c329932009-04-12 12:16:21 +0000437 /* look up the name */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000438 answstr = table_lookup(conf_data, type, query_string);
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100439#if DEBUG
440 /* Shows lengths instead of dots, unusable for !DEBUG */
441 bb_error_msg("'%s'->'%s'", query_string, answstr);
442#endif
Denis Vlasenko5c329932009-04-12 12:16:21 +0000443 outr_rlen = 4;
444 if (answstr && type == htons(REQ_PTR)) {
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100445 /* returning a host name */
Denis Vlasenko5c329932009-04-12 12:16:21 +0000446 outr_rlen = strlen(answstr) + 1;
447 }
448 if (!answstr
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100449 || (unsigned)(answb - buf) + query_len + 4 + 2 + outr_rlen > MAX_PACK_LEN
Denis Vlasenko5c329932009-04-12 12:16:21 +0000450 ) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000451 /* QR = 1 "response"
452 * AA = 1 "Authoritative Answer"
453 * RCODE = 3 "Name Error" */
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100454 err_msg = "name is not found";
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000455 outr_flags = htons(0x8000 | 0x0400 | 3);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000456 goto empty_packet;
457 }
Denis Vlasenko5c329932009-04-12 12:16:21 +0000458
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100459 /* Append answer Resource Record */
460 memcpy(answb, query_string, query_len); /* name, type, class */
461 answb += query_len;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000462 move_to_unaligned32((uint32_t *)answb, htonl(conf_ttl));
Denis Vlasenko081eb712008-03-17 09:02:21 +0000463 answb += 4;
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100464 move_to_unaligned16((uint16_t *)answb, htons(outr_rlen));
Denis Vlasenko081eb712008-03-17 09:02:21 +0000465 answb += 2;
466 memcpy(answb, answstr, outr_rlen);
467 answb += outr_rlen;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000468
Denis Vlasenko5c329932009-04-12 12:16:21 +0000469 /* QR = 1 "response",
470 * AA = 1 "Authoritative Answer",
Denys Vlasenko5fb38492010-02-06 22:48:10 +0100471 * TODO: need to set RA bit 0x80? One user says nslookup complains
472 * "Got recursion not available from SERVER, trying next server"
473 * "** server can't find HOSTNAME"
474 * RCODE = 0 "success"
475 */
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100476 if (OPT_verbose)
477 bb_error_msg("returning positive reply");
Denis Vlasenko5c329932009-04-12 12:16:21 +0000478 outr_flags = htons(0x8000 | 0x0400 | 0);
479 /* we have one answer */
480 head->nansw = htons(1);
481
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000482 empty_packet:
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100483 if ((outr_flags & htons(0xf)) != 0) { /* not a positive response */
484 if (OPT_verbose) {
485 bb_error_msg("%s, %s",
486 err_msg,
487 OPT_silent ? "dropping query" : "sending error reply"
488 );
489 }
490 if (OPT_silent)
491 return 0;
492 }
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000493 head->flags |= outr_flags;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000494 head->nauth = head->nadd = 0;
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000495 head->nquer = htons(1); // why???
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000496
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000497 return answb - buf;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000498}
499
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000500int dnsd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000501int dnsd_main(int argc UNUSED_PARAM, char **argv)
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000502{
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000503 const char *listen_interface = "0.0.0.0";
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000504 const char *fileconf = "/etc/dnsd.conf";
505 struct dns_entry *conf_data;
506 uint32_t conf_ttl = DEFAULT_TTL;
Denis Vlasenko2c916522007-01-12 14:57:37 +0000507 char *sttl, *sport;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000508 len_and_sockaddr *lsa, *from, *to;
509 unsigned lsa_size;
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000510 int udps, opts;
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000511 uint16_t port = 53;
Denys Vlasenko0ecc1162010-04-14 10:14:25 -0700512 /* Ensure buf is 32bit aligned (we need 16bit, but 32bit can't hurt) */
513 uint8_t buf[MAX_PACK_LEN + 1] ALIGN4;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000514
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100515 opts = getopt32(argv, "vsi:c:t:p:d", &listen_interface, &fileconf, &sttl, &sport);
516 //if (opts & (1 << 0)) // -v
517 //if (opts & (1 << 1)) // -s
518 //if (opts & (1 << 2)) // -i
519 //if (opts & (1 << 3)) // -c
520 if (opts & (1 << 4)) // -t
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000521 conf_ttl = xatou_range(sttl, 1, 0xffffffff);
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100522 if (opts & (1 << 5)) // -p
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000523 port = xatou_range(sport, 1, 0xffff);
Denys Vlasenko343dfd72010-02-07 02:45:03 +0100524 if (opts & (1 << 6)) { // -d
Denis Vlasenko5a142022007-03-26 13:20:54 +0000525 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
Denis Vlasenko5b27fbe2007-03-24 14:06:51 +0000526 openlog(applet_name, LOG_PID, LOG_DAEMON);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000527 logmode = LOGMODE_SYSLOG;
528 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000529
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000530 conf_data = parse_conf_file(fileconf);
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000531
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000532 lsa = xdotted2sockaddr(listen_interface, port);
Bernhard Reutner-Fischer8c69afd2008-01-29 10:33:34 +0000533 udps = xsocket(lsa->u.sa.sa_family, SOCK_DGRAM, 0);
534 xbind(udps, &lsa->u.sa, lsa->len);
Denis Vlasenko081eb712008-03-17 09:02:21 +0000535 socket_want_pktinfo(udps); /* needed for recv_from_to to work */
536 lsa_size = LSA_LEN_SIZE + lsa->len;
537 from = xzalloc(lsa_size);
538 to = xzalloc(lsa_size);
539
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000540 {
541 char *p = xmalloc_sockaddr2dotted(&lsa->u.sa);
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100542 bb_error_msg("accepting UDP packets on %s", p);
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000543 free(p);
544 }
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000545
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000546 while (1) {
"Vladimir N. Oleynik"2e5ee8e2006-01-25 14:40:24 +0000547 int r;
Denis Vlasenko081eb712008-03-17 09:02:21 +0000548 /* Try to get *DEST* address (to which of our addresses
549 * this query was directed), and reply from the same address.
550 * Or else we can exhibit usual UDP ugliness:
551 * [ip1.multihomed.ip2] <= query to ip1 <= peer
552 * [ip1.multihomed.ip2] => reply from ip2 => peer (confused) */
553 memcpy(to, lsa, lsa_size);
554 r = recv_from_to(udps, buf, MAX_PACK_LEN + 1, 0, &from->u.sa, &to->u.sa, lsa->len);
555 if (r < 12 || r > MAX_PACK_LEN) {
Denis Vlasenkoddbf3bf2009-04-12 04:09:09 +0000556 bb_error_msg("packet size %d, ignored", r);
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000557 continue;
Denis Vlasenko2c916522007-01-12 14:57:37 +0000558 }
Denis Vlasenko081eb712008-03-17 09:02:21 +0000559 if (OPT_verbose)
Denys Vlasenkoe66a09b2010-02-07 01:11:18 +0100560 bb_error_msg("got UDP packet");
Denis Vlasenko081eb712008-03-17 09:02:21 +0000561 buf[r] = '\0'; /* paranoia */
Denis Vlasenkoa19e6492009-03-11 14:40:00 +0000562 r = process_packet(conf_data, conf_ttl, buf);
Denis Vlasenkod3bac032007-03-24 12:13:04 +0000563 if (r <= 0)
564 continue;
Denis Vlasenkoe9b76e12008-05-22 17:41:01 +0000565 send_to_from(udps, buf, r, 0, &from->u.sa, &to->u.sa, lsa->len);
Denis Vlasenko2c916522007-01-12 14:57:37 +0000566 }
Denis Vlasenkob5b45a92007-03-24 13:09:07 +0000567 return 0;
"Vladimir N. Oleynik"7b4aa6f2006-01-25 14:19:11 +0000568}