dns.c 12.8 KB
Newer Older
Ted Lemon's avatar
Ted Lemon committed
1
2
/* dns.c

3
   Domain Name Service subroutines. */
Ted Lemon's avatar
Ted Lemon committed
4
5

/*
Ted Lemon's avatar
Ted Lemon committed
6
7
8
9
10
11
 * Copyright (c) 1996-1999 Internet Software Consortium.
 * Use is subject to license terms which appear in the file named
 * ISC-LICENSE that should have accompanied this file when you
 * received it.   If a file named ISC-LICENSE did not accompany this
 * file, or you are not sure the one you have is correct, you may
 * obtain an applicable copy of the license at:
Ted Lemon's avatar
Ted Lemon committed
12
 *
Ted Lemon's avatar
Ted Lemon committed
13
 *             http://www.isc.org/isc-license-1.0.html. 
Ted Lemon's avatar
Ted Lemon committed
14
 *
Ted Lemon's avatar
Ted Lemon committed
15
16
17
 * This file is part of the ISC DHCP distribution.   The documentation
 * associated with this file is listed in the file DOCUMENTATION,
 * included in the top-level directory of this release.
Ted Lemon's avatar
Ted Lemon committed
18
 *
Ted Lemon's avatar
Ted Lemon committed
19
20
 * Support and other services are available for ISC products - see
 * http://www.isc.org for more information.
Ted Lemon's avatar
Ted Lemon committed
21
22
23
24
 */

#ifndef lint
static char copyright[] =
25
"$Id: dns.c,v 1.15 2000/02/01 03:19:38 mellon Exp $ Copyright (c) 1997 The Internet Software Consortium.  All rights reserved.\n";
Ted Lemon's avatar
Ted Lemon committed
26
27
28
29
30
#endif /* not lint */

#include "dhcpd.h"
#include "arpa/nameser.h"

31
32
33
int dns_protocol_initialized;
int dns_protocol_fd;

34
static unsigned addlabel PROTO ((u_int8_t *, const char *));
35
36
static int skipname PROTO ((u_int8_t *));
static int copy_out_name PROTO ((u_int8_t *, u_int8_t *, char *));
37
38
39
static int nslookup PROTO ((u_int8_t, char *, int, u_int16_t, u_int16_t));
static int zonelookup PROTO ((u_int8_t, char *, int, u_int16_t));
u_int16_t dns_port;
40

Ted Lemon's avatar
Ted Lemon committed
41
42
#define DNS_QUERY_HASH_SIZE	293
struct dns_query *dns_query_hash [DNS_QUERY_HASH_SIZE];
43

Ted Lemon's avatar
Ted Lemon committed
44
45
/* Initialize the DNS protocol. */

46
void dns_startup ()
Ted Lemon's avatar
Ted Lemon committed
47
{
48
	struct servent *srv;
Ted Lemon's avatar
Ted Lemon committed
49
50
51
	struct sockaddr_in from;

	/* Only initialize icmp once. */
52
	if (dns_protocol_initialized)
53
		log_fatal ("attempted to reinitialize dns protocol");
54
	dns_protocol_initialized = 1;
Ted Lemon's avatar
Ted Lemon committed
55
56

	/* Get the protocol number (should be 1). */
57
	srv = getservbyname ("domain", "tcp");
58
	if (srv)
59
60
61
		dns_port = srv -> s_port;
	else
		dns_port = htons (53);
Ted Lemon's avatar
Ted Lemon committed
62

63
	/* Get a socket for the DNS protocol. */
64
	dns_protocol_fd = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
65
	if (dns_protocol_fd < 0)
66
		log_fatal ("unable to create dns socket: %m");
Ted Lemon's avatar
Ted Lemon committed
67

Ted Lemon's avatar
Ted Lemon committed
68
69
70
71
72
#if defined (HAVE_SETFD)
	if (fcntl (dns_protocol_fd, F_SETFD, 1) < 0)
		log_fatal ("unable to set close-on-exec on dns fd: %m");
#endif

Ted Lemon's avatar
Ted Lemon committed
73
	first_name_server ();
74

75
	add_protocol ("dns", dns_protocol_fd, dns_packet, 0);
Ted Lemon's avatar
Ted Lemon committed
76
77
78
79
80
81
82
83
84
}

/* Label manipulation stuff; see RFC1035, page 28 section 4.1.2 and
   page 30, section 4.1.4. */

/* addlabel copies a label into the specified buffer, putting the length of
   the label in the first character, the contents of the label in subsequent
   characters, and returning the length of the conglomeration. */

85
static unsigned addlabel (buf, label)
86
	u_int8_t *buf;
87
	const char *label;
Ted Lemon's avatar
Ted Lemon committed
88
{
89
	*buf = strlen (label);
90
91
	memcpy (buf + 1, label, (unsigned)*buf);
	return (unsigned)(*buf + 1);
Ted Lemon's avatar
Ted Lemon committed
92
93
94
95
96
}

/* skipname skips over all of the labels in a single domain name,
   returning the length of the domain name. */

97
98
static int skipname (label)
     u_int8_t *label;
Ted Lemon's avatar
Ted Lemon committed
99
{
100
101
102
103
104
	if (*label & INDIR_MASK)
		return 2;
	if (*label == 0)
		return 1;
	return *label + 1 + skipname (label + *label + 1);
Ted Lemon's avatar
Ted Lemon committed
105
106
107
108
109
110
}

/* copy_out_name copies out the name appearing at the specified location
   into a string, stored as fields seperated by dots rather than lengths
   and labels.   The length of the label-formatted name is returned. */

111
112
113
114
static int copy_out_name (base, name, buf)
     u_int8_t *base;
     u_int8_t *name;
     char *buf;
Ted Lemon's avatar
Ted Lemon committed
115
{
116
117
118
119
120
121
122
123
124
125
126
127
	if (*name & INDIR_MASK) {
		int offset = (*name & ~INDIR_MASK) + (*name + 1);
		return copy_out_name (base, base + offset, buf);
	}
	if (!*name) {
		*buf = 0;
		return 1;
	}
	memcpy (buf, name + 1, *name);
	*(buf + *name) = '.';
	return (*name + 1
		+ copy_out_name (base, name + *name + 1, buf + *name + 1));
Ted Lemon's avatar
Ted Lemon committed
128
129
}

Ted Lemon's avatar
Ted Lemon committed
130
131
/* Compute a hash on the question. */

132
static INLINE u_int32_t dns_hash_question (struct dns_question *question)
Ted Lemon's avatar
Ted Lemon committed
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
{
	u_int32_t sum;
	u_int32_t remainder;
	u_int32_t *p = (u_int32_t *)question;
	u_int8_t *s;

	/* First word. */
	sum = *p++;
	s = (u_int8_t *)p;

	remainder = 0;
	while (s [0]) {
		remainder = s [0];
		if (s [1]) {
			remainder = (remainder << 8) + s [1];
			if (s [2]) {
				remainder = (remainder << 8) + s [2];
				if (s [3])
					remainder = (remainder << 8) + s [3];
				else
					goto done;
			} else
				goto done;
		} else {
		      done:
			sum += remainder;
			break;
		}
		if ((sum & 0x80000000) && (remainder & 0x80000000))
			++sum;
		sum += remainder;
		s += 4;
	}

	while (sum > DNS_QUERY_HASH_SIZE) {
		remainder = sum / DNS_QUERY_HASH_SIZE;
		sum = sum % DNS_QUERY_HASH_SIZE;
		while (remainder) {
			sum += remainder % DNS_QUERY_HASH_SIZE;
			remainder /= DNS_QUERY_HASH_SIZE;
		}
	} 

	return sum;
}

/* Find a query that matches the specified name.  If one can't be
   found, and new is nonzero, allocate one, hash it in, and save the
   question.  Otherwise, if new is nonzero, free() the question.
   Return the query if one was found or allocated. */

struct dns_query *find_dns_query (question, new)
	struct dns_question *question;
	int new;
{
	int hash = dns_hash_question (question);
	struct dns_query *q;

	for (q = dns_query_hash [hash]; q; q = q -> next) {
		if (q -> question -> type == question -> type &&
		    q -> question -> class == question -> class &&
194
195
		    !strcmp ((char *)q -> question -> data,
			     (char *)question -> data))
Ted Lemon's avatar
Ted Lemon committed
196
197
198
199
			break;
	}
	if (q || !new) {
		if (new)
200
			dfree (question, MDL);
Ted Lemon's avatar
Ted Lemon committed
201
202
203
204
		return q;
	}

	/* Allocate and zap a new query. */
205
	q = (struct dns_query *)dmalloc (sizeof (struct dns_query), MDL);
Ted Lemon's avatar
Ted Lemon committed
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
	memset (q, 0, sizeof *q);

	/* All we need to set up is the question and the hash. */
	q -> question = question;
	q -> next = dns_query_hash [hash];
	dns_query_hash [hash] = q;
	q -> hash = hash;
	return q;
}

/* Free up all memory associated with a DNS query and remove it from the
   query hash. */

void destroy_dns_query (query)
	struct dns_query *query;
{
	struct dns_query *q;

	/* Free up attached free data. */
	if (query -> question)
226
		dfree (query -> question, MDL);
Ted Lemon's avatar
Ted Lemon committed
227
	if (query -> answer)
228
		dfree (query -> answer, MDL);
Ted Lemon's avatar
Ted Lemon committed
229
	if (query -> query)
230
		dfree (query -> query, MDL);
Ted Lemon's avatar
Ted Lemon committed
231
232
233
234
235
236
237
238
239
240
241
242
243

	/* Remove query from hash table. */
	if (dns_query_hash [query -> hash] == query)
		dns_query_hash [query -> hash] = query -> next;
	else {
		for (q = dns_query_hash [query -> hash];
		     q -> next && q -> next != query; q = q -> next)
			;
		if (q -> next)
			q -> next = query -> next;
	}

	/* Free the query structure. */
244
	dfree (query, MDL);
Ted Lemon's avatar
Ted Lemon committed
245
246
}

Ted Lemon's avatar
Ted Lemon committed
247
/* ns_inaddr_lookup constructs a PTR lookup query for an internet address -
Ted Lemon's avatar
Ted Lemon committed
248
249
   e.g., 1.200.9.192.in-addr.arpa.   It then passes it on to ns_query for
   completion. */
Ted Lemon's avatar
Ted Lemon committed
250

251
struct dns_query *ns_inaddr_lookup (inaddr, wakeup)
252
	struct iaddr inaddr;
253
	struct dns_wakeup *wakeup;
Ted Lemon's avatar
Ted Lemon committed
254
{
Ted Lemon's avatar
Ted Lemon committed
255
	unsigned char query [512];
256
	unsigned char *s;
257
258
259
	unsigned char *label;
	int i;
	unsigned char c;
Ted Lemon's avatar
Ted Lemon committed
260
261
262
	struct dns_question *question;

	/* First format the query in the internal format. */
263
	sprintf ((char *)query, "%d.%d.%d.%d.in-addr.arpa.",
Ted Lemon's avatar
Ted Lemon committed
264
265
		 inaddr.iabuf [0], inaddr.iabuf [1],
		 inaddr.iabuf [2], inaddr.iabuf [3]);
266

267
268
	question = (struct dns_question *)dmalloc (strlen ((char *)query) +
						   sizeof *question, MDL);
Ted Lemon's avatar
Ted Lemon committed
269
270
271
272
	if (!question)
		return (struct dns_query *)-1;
	question -> type = T_PTR;
	question -> class = C_IN;
273
	strcpy ((char *)question -> data, (char *)query);
Ted Lemon's avatar
Ted Lemon committed
274
275
276

	/* Now format the query for the name server. */
	s = query;
277
278

	/* Copy out the digits. */
279
280
	for (i = 3; i >= 0; --i) {
		label = s++;
281
282
		sprintf ((char *)s, "%d", inaddr.iabuf [i]);
		*label = strlen ((char *)s);
Ted Lemon's avatar
Ted Lemon committed
283
		s += *label;
Ted Lemon's avatar
Ted Lemon committed
284
	}
285
286
287
	s += addlabel (s, "in-addr");
	s += addlabel (s, "arpa");
	*s++ = 0;
Ted Lemon's avatar
Ted Lemon committed
288

289
290
291
292
293
294
295
296
	/* Set the query type. */
	putUShort (s, T_PTR);
	s += sizeof (u_int16_t);

	/* Set the query class. */
	putUShort (s, C_IN);
	s += sizeof (u_int16_t);

297
	return ns_query (question, query, (unsigned)(s - query), wakeup);
298
}
Ted Lemon's avatar
Ted Lemon committed
299

Ted Lemon's avatar
Ted Lemon committed
300
301
302
303
304
305
306
307
308
309
310
/* Try to satisfy a query out of the local cache.  If no answer has
   been cached, and if there isn't already a query pending on this
   question, send it.  If the query can be immediately satisfied,
   a pointer to the dns_query structure is returned.  If the query
   can't even be made for some reason, (struct dns_query *)-1 is
   returned.  Otherwise, the null pointer is returned, indicating that
   a wakeup will be performed later when the answer comes back. */

struct dns_query *ns_query (question, formatted_query, len, wakeup)
	struct dns_question *question;
	unsigned char *formatted_query;
311
	unsigned len;
312
	struct dns_wakeup *wakeup;
Ted Lemon's avatar
Ted Lemon committed
313
{
314
	HEADER *hdr;
315
316
	struct dns_query *query;
	unsigned char *s;
Ted Lemon's avatar
Ted Lemon committed
317
318
319
320
	unsigned char buf [512];

	/* If the query won't fit, don't bother setting it up. */
	if (len > 255) {
321
		dfree (question, MDL);
Ted Lemon's avatar
Ted Lemon committed
322
323
		return (struct dns_query *)-1;
	}
324
325
326

	/* See if there's already a query for this name, and allocate a
	   query if none exists. */
Ted Lemon's avatar
Ted Lemon committed
327
	query = find_dns_query (question, 1);
328
329

	/* If we can't allocate a query, report that the query failed. */
Ted Lemon's avatar
Ted Lemon committed
330
	if (!query)
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
		return (struct dns_query *)-1;

	/* If the query has already been answered, return it. */
	if (query -> expiry > cur_time)
		return query;

	/* The query hasn't yet been answered, so we have to wait, one
	   way or another.   Put the wakeup on the list. */
	if (wakeup) {
		wakeup -> next = query -> wakeups;
		query -> wakeups = wakeup;
	}

	/* If the query has already been sent, but we don't yet have
	   an answer, we're done. */
	if (query -> sent)
		return (struct dns_query *)0;
348

349
	/* Construct a header... */
Ted Lemon's avatar
Ted Lemon committed
350
	hdr = (HEADER *)buf;
351
	memset (hdr, 0, sizeof *hdr);
352
	hdr -> id = query -> id;
353
354
355
356
	hdr -> rd = 1;
	hdr -> opcode = QUERY;
	hdr -> qdcount = htons (1);

Ted Lemon's avatar
Ted Lemon committed
357
	/* Copy the formatted name into the buffer. */
358
	s = (unsigned char *)hdr + 1;
Ted Lemon's avatar
Ted Lemon committed
359
	memcpy (s, formatted_query, len);
360

361
362
	/* Figure out how long the whole message is */
	s += len;
Ted Lemon's avatar
Ted Lemon committed
363
364
365
	query -> len = s - buf;

	/* Save the raw query data. */
366
	query -> query = dmalloc (len, MDL);
Ted Lemon's avatar
Ted Lemon committed
367
368
369
370
	if (!query -> query) {
		destroy_dns_query (query);
		return (struct dns_query *)-1;
	}
371
	memcpy (query -> query, buf, query -> len);
372
373
374

	/* Flag the query as having been sent. */
	query -> sent = 1;
375
376

	/* Send the query. */
377
	dns_timeout (query);
378

379
380
	/* No answer yet, obviously. */
	return (struct dns_query *)0;
Ted Lemon's avatar
Ted Lemon committed
381
382
}

383
/* Retransmit a DNS query. */
384

385
386
387
388
389
void dns_timeout (qv)
	void *qv;
{
	struct dns_query *query = qv;
	int status;
390

391
392
393
	/* Choose the server to send to. */
	if (!query -> next_server)
		query -> next_server = first_name_server ();
394

395
396
397
	/* Send the query. */
	if (query -> next_server)
		status = sendto (dns_protocol_fd,
398
				 (char *)query -> query, query -> len, 0,
399
400
401
402
403
				 ((struct sockaddr *)&query ->
				  next_server -> addr),
				 sizeof query -> next_server -> addr);
	else
		status = -1;
404

405
406
	/* Look for the next server... */
	query -> next_server = query -> next_server -> next;
407

408
409
410
	/* If this is our first time, backoff one second. */
	if (!query -> backoff)
		query -> backoff = 1;
411

412
413
414
	/* If the send failed, don't advance the backoff. */
	else if (status < 0)
		;
415

416
417
418
419
420
421
422
	/* If we haven't run out of servers to try, don't backoff. */
	else if (query -> next_server)
		;

	/* If we haven't backed off enough yet, back off some more. */
	else if (query -> backoff < 30)
		query -> backoff += random() % query -> backoff;
423

424
425
	/* Set up the timeout. */
	add_timeout (cur_time + query -> backoff, dns_timeout, query);
426
427
}

428
429
/* Process a reply from a name server. */

430
void dns_packet (protocol)
431
	struct protocol *protocol;
Ted Lemon's avatar
Ted Lemon committed
432
{
433
434
	HEADER *ns_header;
	struct sockaddr_in from;
435
436
	struct dns_wakeup *wakeup;
	unsigned char buf [512];
Ted Lemon's avatar
Ted Lemon committed
437
438
439
440
	union {
		unsigned char u [512];
		struct dns_question q;
	} qbuf;
441
	unsigned char *base;
442
	unsigned char *dptr, *name;
443
444
445
446
	u_int16_t type;
	u_int16_t class;
	TIME ttl;
	u_int16_t rdlength;
447
448
	socklen_t len;
	int status;
449
	int i;
450
	struct dns_query *query;
451
452

	len = sizeof from;
453
	status = recvfrom (protocol -> fd, (char *)buf, sizeof buf, 0,
454
455
			  (struct sockaddr *)&from, &len);
	if (status < 0) {
456
		log_error ("dns_packet: %m");
457
458
459
460
461
		return;
	}

	/* Response is too long? */
	if (len > 512) {
462
		log_error ("dns_packet: dns message too long (%d)", len);
463
464
		return;
	}
Ted Lemon's avatar
Ted Lemon committed
465

466
467
	ns_header = (HEADER *)buf;
	base = (unsigned char *)(ns_header + 1);
Ted Lemon's avatar
Ted Lemon committed
468

469
470
471
472
473
474
	/* Parse the response... */
	dptr = base;

	/* If this is a response to a query from us, there should have
           been only one query. */
	if (ntohs (ns_header -> qdcount) != 1) {
475
		log_error ("Bogus DNS answer packet from %s claims %d queries.\n",
Ted Lemon's avatar
Ted Lemon committed
476
477
		      inet_ntoa (from.sin_addr),
		      ntohs (ns_header -> qdcount));
478
479
		return;
	}
Ted Lemon's avatar
Ted Lemon committed
480

481
482
	/* Find the start of the name in the query. */
	name = dptr;
Ted Lemon's avatar
Ted Lemon committed
483

484
	/* Skip over the name. */
485
	dptr += copy_out_name (name, name, (char *)qbuf.q.data);
486
487

	/* Skip over the query type and query class. */
Ted Lemon's avatar
Ted Lemon committed
488
489
490
491
	qbuf.q.type = getUShort (dptr);
	dptr += sizeof (u_int16_t);
	qbuf.q.class = getUShort (dptr);
	dptr += sizeof (u_int16_t);
492
493

	/* See if we asked this question. */
Ted Lemon's avatar
Ted Lemon committed
494
	query = find_dns_query (&qbuf.q, 0);
495
	if (!query) {
496
log_error ("got answer for question %s from DNS, which we didn't ask.",
Ted Lemon's avatar
Ted Lemon committed
497
qbuf.q.data);
498
		return;
499
	}
500

501
log_info ("got answer for question %s from DNS", qbuf.q.data);
502
503
504
505

	/* Wake up everybody who's waiting. */
	for (wakeup = query -> wakeups; wakeup; wakeup = wakeup -> next) {
		(*wakeup -> func) (query);
506
	}
Ted Lemon's avatar
Ted Lemon committed
507
}