summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/net/udp/dns.c
blob: 2d77477f687af0277fd96fedf57fda35425eb4db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
/*
 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
 *
 * Portions copyright (C) 2004 Anselm M. Hoffmeister
 * <stockholm@users.sourceforge.net>.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 * You can also choose to distribute this program under the terms of
 * the Unmodified Binary Distribution Licence (as given in the file
 * COPYING.UBDL), provided that you have satisfied its requirements.
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include <byteswap.h>
#include <ipxe/refcnt.h>
#include <ipxe/iobuf.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/resolv.h>
#include <ipxe/retry.h>
#include <ipxe/tcpip.h>
#include <ipxe/settings.h>
#include <ipxe/features.h>
#include <ipxe/dhcp.h>
#include <ipxe/dhcpv6.h>
#include <ipxe/dns.h>

/** @file
 *
 * DNS protocol
 *
 */

FEATURE ( FEATURE_PROTOCOL, "DNS", DHCP_EB_FEATURE_DNS, 1 );

/* Disambiguate the various error causes */
#define ENXIO_NO_RECORD __einfo_error ( EINFO_ENXIO_NO_RECORD )
#define EINFO_ENXIO_NO_RECORD \
	__einfo_uniqify ( EINFO_ENXIO, 0x01, "DNS name does not exist" )
#define ENXIO_NO_NAMESERVER __einfo_error ( EINFO_ENXIO_NO_NAMESERVER )
#define EINFO_ENXIO_NO_NAMESERVER \
	__einfo_uniqify ( EINFO_ENXIO, 0x02, "No DNS servers available" )

/** The DNS server */
static union {
	struct sockaddr sa;
	struct sockaddr_tcpip st;
	struct sockaddr_in sin;
	struct sockaddr_in6 sin6;
} nameserver = {
	.st = {
		.st_port = htons ( DNS_PORT ),
	},
};

/** The DNS search list */
static struct dns_name dns_search;

/**
 * Encode a DNS name using RFC1035 encoding
 *
 * @v string		DNS name as a string
 * @v name		DNS name to fill in
 * @ret len		Length of DNS name, or negative error
 */
int dns_encode ( const char *string, struct dns_name *name ) {
	uint8_t *start = ( name->data + name->offset );
	uint8_t *end = ( name->data + name->len );
	uint8_t *dst = start;
	size_t len = 0;
	char c;

	/* Encode name */
	while ( ( c = *(string++) ) ) {

		/* Handle '.' separators */
		if ( c == '.' ) {

			/* Reject consecutive '.' */
			if ( ( len == 0 ) && ( dst != start ) )
				return -EINVAL;

			/* Terminate if this is the trailing '.' */
			if ( *string == '\0' )
				break;

			/* Reject initial non-terminating '.' */
			if ( len == 0 )
				return -EINVAL;

			/* Reset length */
			len = 0;

		} else {

			/* Increment length */
			len++;

			/* Check for overflow */
			if ( len > DNS_MAX_LABEL_LEN )
				return -EINVAL;
		}

		/* Copy byte, update length */
		if ( ++dst < end ) {
			*dst = c;
			dst[-len] = len;
		}
	}

	/* Add terminating root marker */
	if ( len )
		dst++;
	if ( dst < end )
		*dst = '\0';
	dst++;

	return ( dst - start );
}

/**
 * Find start of valid label within an RFC1035-encoded DNS name
 *
 * @v name		DNS name
 * @v offset		Current offset
 * @ret offset		Offset of label, or negative error
 */
static int dns_label ( struct dns_name *name, size_t offset ) {
	const uint8_t *byte;
	const uint16_t *word;
	size_t len;
	size_t ptr;

	while ( 1 ) {

		/* Fail if we have overrun the DNS name */
		if ( ( offset + sizeof ( *byte) ) > name->len )
			return -EINVAL;
		byte = ( name->data + offset );

		/* Follow compression pointer, if applicable */
		if ( DNS_IS_COMPRESSED ( *byte ) ) {

			/* Fail if we have overrun the DNS name */
			if ( ( offset + sizeof ( *word ) ) > name->len )
				return -EINVAL;
			word = ( name->data + offset );

			/* Extract pointer to new offset */
			ptr = DNS_COMPRESSED_OFFSET ( ntohs ( *word ) );

			/* Fail if pointer does not point backwards.
			 * (This guarantees termination of the
			 * function.)
			 */
			if ( ptr >= offset )
				return -EINVAL;

			/* Continue from new offset */
			offset = ptr;
			continue;
		}

		/* Fail if we have overrun the DNS name */
		len = *byte;
		if ( ( offset + sizeof ( *byte ) + len ) > name->len )
			return -EINVAL;

		/* We have a valid label */
		return offset;
	}
}

/**
 * Decode RFC1035-encoded DNS name
 *
 * @v name		DNS name
 * @v data		Output buffer
 * @v len		Length of output buffer
 * @ret len		Length of decoded DNS name, or negative error
 */
int dns_decode ( struct dns_name *name, char *data, size_t len ) {
	unsigned int recursion_limit = name->len; /* Generous upper bound */
	int offset = name->offset;
	const uint8_t *label;
	size_t decoded_len = 0;
	size_t label_len;
	size_t copy_len;

	while ( recursion_limit-- ) {

		/* Find valid DNS label */
		offset = dns_label ( name, offset );
		if ( offset < 0 )
			return offset;

		/* Terminate if we have reached the root */
		label = ( name->data + offset );
		label_len = *(label++);
		if ( label_len == 0 ) {
			if ( decoded_len < len )
				*data = '\0';
			return decoded_len;
		}

		/* Prepend '.' if applicable */
		if ( decoded_len && ( decoded_len++ < len ) )
			*(data++) = '.';

		/* Copy label to output buffer */
		copy_len = ( ( decoded_len < len ) ? ( len - decoded_len ) : 0);
		if ( copy_len > label_len )
			copy_len = label_len;
		memcpy ( data, label, copy_len );
		data += copy_len;
		decoded_len += label_len;

		/* Move to next label */
		offset += ( sizeof ( *label ) + label_len );
	}

	/* Recursion limit exceeded */
	return -EINVAL;
}

/**
 * Compare DNS names for equality
 *
 * @v first		First DNS name
 * @v second		Second DNS name
 * @ret rc		Return status code
 */
int dns_compare ( struct dns_name *first, struct dns_name *second ) {
	unsigned int recursion_limit = first->len; /* Generous upper bound */
	int first_offset = first->offset;
	int second_offset = second->offset;
	const uint8_t *first_label;
	const uint8_t *second_label;
	size_t label_len;
	size_t len;

	while ( recursion_limit-- ) {

		/* Find valid DNS labels */
		first_offset = dns_label ( first, first_offset );
		if ( first_offset < 0 )
			return first_offset;
		second_offset = dns_label ( second, second_offset );
		if ( second_offset < 0 )
			return second_offset;

		/* Compare label lengths */
		first_label = ( first->data + first_offset );
		second_label = ( second->data + second_offset );
		label_len = *(first_label++);
		if ( label_len != *(second_label++) )
			return -ENOENT;
		len = ( sizeof ( *first_label ) + label_len );

		/* Terminate if we have reached the root */
		if ( label_len == 0 )
			return 0;

		/* Compare label contents (case-insensitively) */
		while ( label_len-- ) {
			if ( tolower ( *(first_label++) ) !=
			     tolower ( *(second_label++) ) )
				return -ENOENT;
		}

		/* Move to next labels */
		first_offset += len;
		second_offset += len;
	}

	/* Recursion limit exceeded */
	return -EINVAL;
}

/**
 * Copy a DNS name
 *
 * @v src		Source DNS name
 * @v dst		Destination DNS name
 * @ret len		Length of copied DNS name, or negative error
 */
int dns_copy ( struct dns_name *src, struct dns_name *dst ) {
	unsigned int recursion_limit = src->len; /* Generous upper bound */
	int src_offset = src->offset;
	size_t dst_offset = dst->offset;
	const uint8_t *label;
	size_t label_len;
	size_t copy_len;
	size_t len;

	while ( recursion_limit-- ) {

		/* Find valid DNS label */
		src_offset = dns_label ( src, src_offset );
		if ( src_offset < 0 )
			return src_offset;

		/* Copy as an uncompressed label */
		label = ( src->data + src_offset );
		label_len = *label;
		len = ( sizeof ( *label ) + label_len );
		copy_len = ( ( dst_offset < dst->len ) ?
			     ( dst->len - dst_offset ) : 0 );
		if ( copy_len > len )
			copy_len = len;
		memcpy ( ( dst->data + dst_offset ), label, copy_len );
		src_offset += len;
		dst_offset += len;

		/* Terminate if we have reached the root */
		if ( label_len == 0 )
			return ( dst_offset - dst->offset );
	}

	/* Recursion limit exceeded */
	return -EINVAL;
}

/**
 * Skip RFC1035-encoded DNS name
 *
 * @v name		DNS name
 * @ret offset		Offset to next name, or negative error
 */
int dns_skip ( struct dns_name *name ) {
	unsigned int recursion_limit = name->len; /* Generous upper bound */
	int offset = name->offset;
	int prev_offset;
	const uint8_t *label;
	size_t label_len;

	while ( recursion_limit-- ) {

		/* Find valid DNS label */
		prev_offset = offset;
		offset = dns_label ( name, prev_offset );
		if ( offset < 0 )
			return offset;

		/* Terminate if we have reached a compression pointer */
		if ( offset != prev_offset )
			return ( prev_offset + sizeof ( uint16_t ) );

		/* Skip this label */
		label = ( name->data + offset );
		label_len = *label;
		offset += ( sizeof ( *label ) + label_len );

		/* Terminate if we have reached the root */
		if ( label_len == 0 )
			return offset;
	}

	/* Recursion limit exceeded */
	return -EINVAL;
}

/**
 * Skip RFC1035-encoded DNS name in search list
 *
 * @v name		DNS name
 * @ret offset		Offset to next non-empty name, or negative error
 */
static int dns_skip_search ( struct dns_name *name ) {
	int offset;

	/* Find next name */
	offset = dns_skip ( name );
	if ( offset < 0 )
		return offset;

	/* Skip over any subsequent empty names (e.g. due to padding
	 * bytes used in the NDP DNSSL option).
	 */
	while ( ( offset < ( ( int ) name->len ) ) &&
		( *( ( uint8_t * ) ( name->data + offset ) ) == 0 ) ) {
		offset++;
	}

	return offset;
}

/**
 * Transcribe DNS name (for debugging)
 *
 * @v name		DNS name
 * @ret string		Transcribed DNS name
 */
static const char * dns_name ( struct dns_name *name ) {
	static char buf[256];
	int len;

	len = dns_decode ( name, buf, sizeof ( buf ) );
	return ( ( len < 0 ) ? "<INVALID>" : buf );
}

/**
 * Name a DNS query type (for debugging)
 *
 * @v type		Query type (in network byte order)
 * @ret name		Type name
 */
static const char * dns_type ( uint16_t type ) {
	switch ( type ) {
	case htons ( DNS_TYPE_A ):	return "A";
	case htons ( DNS_TYPE_AAAA ):	return "AAAA";
	case htons ( DNS_TYPE_CNAME ):	return "CNAME";
	default:			return "<UNKNOWN>";
	}
}

/** A DNS request */
struct dns_request {
	/** Reference counter */
	struct refcnt refcnt;
	/** Name resolution interface */
	struct interface resolv;
	/** Data transfer interface */
	struct interface socket;
	/** Retry timer */
	struct retry_timer timer;

	/** Socket address to fill in with resolved address */
	union {
		struct sockaddr sa;
		struct sockaddr_in sin;
		struct sockaddr_in6 sin6;
	} address;
	/** Initial query type */
	uint16_t qtype;
	/** Buffer for current query */
	struct {
		/** Query header */
		struct dns_header query;
		/** Name buffer */
		char name[DNS_MAX_NAME_LEN];
		/** Space for question */
		struct dns_question padding;
	} __attribute__ (( packed )) buf;
	/** Current query name */
	struct dns_name name;
	/** Question within current query */
	struct dns_question *question;
	/** Length of current query */
	size_t len;
	/** Offset of search suffix within current query */
	size_t offset;
	/** Search list */
	struct dns_name search;
	/** Recursion counter */
	unsigned int recursion;
};

/**
 * Mark DNS request as complete
 *
 * @v dns		DNS request
 * @v rc		Return status code
 */
static void dns_done ( struct dns_request *dns, int rc ) {

	/* Stop the retry timer */
	stop_timer ( &dns->timer );

	/* Shut down interfaces */
	intf_shutdown ( &dns->socket, rc );
	intf_shutdown ( &dns->resolv, rc );
}

/**
 * Mark DNS request as resolved and complete
 *
 * @v dns		DNS request
 * @v rc		Return status code
 */
static void dns_resolved ( struct dns_request *dns ) {

	DBGC ( dns, "DNS %p found address %s\n",
	       dns, sock_ntoa ( &dns->address.sa ) );

	/* Return resolved address */
	resolv_done ( &dns->resolv, &dns->address.sa );

	/* Mark operation as complete */
	dns_done ( dns, 0 );
}

/**
 * Construct DNS question
 *
 * @v dns		DNS request
 * @ret rc		Return status code
 */
static int dns_question ( struct dns_request *dns ) {
	static struct dns_name search_root = {
		.data = "",
		.len = 1,
	};
	struct dns_name *search = &dns->search;
	int len;
	size_t offset;

	/* Use root suffix if search list is empty */
	if ( search->offset == search->len )
		search = &search_root;

	/* Overwrite current suffix */
	dns->name.offset = dns->offset;
	len = dns_copy ( search, &dns->name );
	if ( len < 0 )
		return len;

	/* Sanity check */
	offset = ( dns->name.offset + len );
	if ( offset > dns->name.len ) {
		DBGC ( dns, "DNS %p name is too long\n", dns );
		return -EINVAL;
	}

	/* Construct question */
	dns->question = ( ( ( void * ) &dns->buf ) + offset );
	dns->question->qtype = dns->qtype;
	dns->question->qclass = htons ( DNS_CLASS_IN );

	/* Store length */
	dns->len = ( offset + sizeof ( *(dns->question) ) );

	/* Restore name */
	dns->name.offset = offsetof ( typeof ( dns->buf ), name );

	DBGC2 ( dns, "DNS %p question is %s type %s\n", dns,
		dns_name ( &dns->name ), dns_type ( dns->question->qtype ) );

	return 0;
}

/**
 * Send DNS query
 *
 * @v dns		DNS request
 * @ret rc		Return status code
 */
static int dns_send_packet ( struct dns_request *dns ) {
	struct dns_header *query = &dns->buf.query;

	/* Start retransmission timer */
	start_timer ( &dns->timer );

	/* Generate query identifier */
	query->id = random();

	/* Send query */
	DBGC ( dns, "DNS %p sending query ID %#04x for %s type %s\n", dns,
	       ntohs ( query->id ), dns_name ( &dns->name ),
	       dns_type ( dns->question->qtype ) );

	/* Send the data */
	return xfer_deliver_raw ( &dns->socket, query, dns->len );
}

/**
 * Handle DNS retransmission timer expiry
 *
 * @v timer		Retry timer
 * @v fail		Failure indicator
 */
static void dns_timer_expired ( struct retry_timer *timer, int fail ) {
	struct dns_request *dns =
		container_of ( timer, struct dns_request, timer );

	if ( fail ) {
		dns_done ( dns, -ETIMEDOUT );
	} else {
		dns_send_packet ( dns );
	}
}

/**
 * Receive new data
 *
 * @v dns		DNS request
 * @v iobuf		I/O buffer
 * @v meta		Data transfer metadata
 * @ret rc		Return status code
 */
static int dns_xfer_deliver ( struct dns_request *dns,
			      struct io_buffer *iobuf,
			      struct xfer_metadata *meta __unused ) {
	struct dns_header *response = iobuf->data;
	struct dns_header *query = &dns->buf.query;
	unsigned int qtype = dns->question->qtype;
	struct dns_name buf;
	union dns_rr *rr;
	int offset;
	size_t answer_offset;
	size_t next_offset;
	size_t rdlength;
	size_t name_len;
	int rc;

	/* Sanity check */
	if ( iob_len ( iobuf ) < sizeof ( *response ) ) {
		DBGC ( dns, "DNS %p received underlength packet length %zd\n",
		       dns, iob_len ( iobuf ) );
		rc = -EINVAL;
		goto done;
	}

	/* Check response ID matches query ID */
	if ( response->id != query->id ) {
		DBGC ( dns, "DNS %p received unexpected response ID %#04x "
		       "(wanted %d)\n", dns, ntohs ( response->id ),
		       ntohs ( query->id ) );
		rc = -EINVAL;
		goto done;
	}
	DBGC ( dns, "DNS %p received response ID %#04x\n",
	       dns, ntohs ( response->id ) );

	/* Check that we have exactly one question */
	if ( response->qdcount != htons ( 1 ) ) {
		DBGC ( dns, "DNS %p received response with %d questions\n",
		       dns, ntohs ( response->qdcount ) );
		rc = -EINVAL;
		goto done;
	}

	/* Skip question section */
	buf.data = iobuf->data;
	buf.offset = sizeof ( *response );
	buf.len = iob_len ( iobuf );
	offset = dns_skip ( &buf );
	if ( offset < 0 ) {
		rc = offset;
		DBGC ( dns, "DNS %p received response with malformed "
		       "question: %s\n", dns, strerror ( rc ) );
		goto done;
	}
	answer_offset = ( offset + sizeof ( struct dns_question ) );

	/* Search through response for useful answers.  Do this
	 * multiple times, to take advantage of useful nameservers
	 * which send us e.g. the CNAME *and* the A record for the
	 * pointed-to name.
	 */
	for ( buf.offset = answer_offset ; buf.offset != buf.len ;
	      buf.offset = next_offset ) {

		/* Check for valid name */
		offset = dns_skip ( &buf );
		if ( offset < 0 ) {
			rc = offset;
			DBGC ( dns, "DNS %p received response with malformed "
			       "answer: %s\n", dns, strerror ( rc ) );
			goto done;
		}

		/* Check for sufficient space for resource record */
		rr = ( buf.data + offset );
		if ( ( offset + sizeof ( rr->common ) ) > buf.len ) {
			DBGC ( dns, "DNS %p received response with underlength "
			       "RR\n", dns );
			rc = -EINVAL;
			goto done;
		}
		rdlength = ntohs ( rr->common.rdlength );
		next_offset = ( offset + sizeof ( rr->common ) + rdlength );
		if ( next_offset > buf.len ) {
			DBGC ( dns, "DNS %p received response with underlength "
			       "RR\n", dns );
			rc = -EINVAL;
			goto done;
		}

		/* Skip non-matching names */
		if ( dns_compare ( &buf, &dns->name ) != 0 ) {
			DBGC2 ( dns, "DNS %p ignoring response for %s type "
				"%s\n", dns, dns_name ( &buf ),
				dns_type ( rr->common.type ) );
			continue;
		}

		/* Handle answer */
		switch ( rr->common.type ) {

		case htons ( DNS_TYPE_AAAA ):

			/* Found the target AAAA record */
			if ( rdlength < sizeof ( dns->address.sin6.sin6_addr )){
				DBGC ( dns, "DNS %p received response with "
				       "underlength AAAA\n", dns );
				rc = -EINVAL;
				goto done;
			}
			dns->address.sin6.sin6_family = AF_INET6;
			memcpy ( &dns->address.sin6.sin6_addr,
				 &rr->aaaa.in6_addr,
				 sizeof ( dns->address.sin6.sin6_addr ) );
			dns_resolved ( dns );
			rc = 0;
			goto done;

		case htons ( DNS_TYPE_A ):

			/* Found the target A record */
			if ( rdlength < sizeof ( dns->address.sin.sin_addr ) ) {
				DBGC ( dns, "DNS %p received response with "
				       "underlength A\n", dns );
				rc = -EINVAL;
				goto done;
			}
			dns->address.sin.sin_family = AF_INET;
			dns->address.sin.sin_addr = rr->a.in_addr;
			dns_resolved ( dns );
			rc = 0;
			goto done;

		case htons ( DNS_TYPE_CNAME ):

			/* Terminate the operation if we recurse too far */
			if ( ++dns->recursion > DNS_MAX_CNAME_RECURSION ) {
				DBGC ( dns, "DNS %p recursion exceeded\n",
				       dns );
				rc = -ELOOP;
				dns_done ( dns, rc );
				goto done;
			}

			/* Found a CNAME record; update query and recurse */
			buf.offset = ( offset + sizeof ( rr->cname ) );
			DBGC ( dns, "DNS %p found CNAME %s\n",
			       dns, dns_name ( &buf ) );
			dns->search.offset = dns->search.len;
			name_len = dns_copy ( &buf, &dns->name );
			dns->offset = ( offsetof ( typeof ( dns->buf ), name ) +
					name_len - 1 /* Strip root label */ );
			if ( ( rc = dns_question ( dns ) ) != 0 ) {
				dns_done ( dns, rc );
				goto done;
			}
			next_offset = answer_offset;
			break;

		default:
			DBGC ( dns, "DNS %p got unknown record type %d\n",
			       dns, ntohs ( rr->common.type ) );
			break;
		}
	}

	/* Stop the retry timer.  After this point, each code path
	 * must either restart the timer by calling dns_send_packet(),
	 * or mark the DNS operation as complete by calling
	 * dns_done()
	 */
	stop_timer ( &dns->timer );

	/* Determine what to do next based on the type of query we
	 * issued and the response we received
	 */
	switch ( qtype ) {

	case htons ( DNS_TYPE_AAAA ):
		/* We asked for an AAAA record and got nothing; try
		 * the A.
		 */
		DBGC ( dns, "DNS %p found no AAAA record; trying A\n", dns );
		dns->question->qtype = htons ( DNS_TYPE_A );
		dns_send_packet ( dns );
		rc = 0;
		goto done;

	case htons ( DNS_TYPE_A ):
		/* We asked for an A record and got nothing;
		 * try the CNAME.
		 */
		DBGC ( dns, "DNS %p found no A record; trying CNAME\n", dns );
		dns->question->qtype = htons ( DNS_TYPE_CNAME );
		dns_send_packet ( dns );
		rc = 0;
		goto done;

	case htons ( DNS_TYPE_CNAME ):
		/* We asked for a CNAME record.  If we got a response
		 * (i.e. if the next AAAA/A query is already set up),
		 * then issue it.
		 */
		if ( qtype == dns->qtype ) {
			dns_send_packet ( dns );
			rc = 0;
			goto done;
		}

		/* If we have already reached the end of the search list,
		 * then terminate lookup.
		 */
		if ( dns->search.offset == dns->search.len ) {
			DBGC ( dns, "DNS %p found no CNAME record\n", dns );
			rc = -ENXIO_NO_RECORD;
			dns_done ( dns, rc );
			goto done;
		}

		/* Move to next entry in search list.  This can never fail,
		 * since we have already used this entry.
		 */
		DBGC ( dns, "DNS %p found no CNAME record; trying next "
		       "suffix\n", dns );
		dns->search.offset = dns_skip_search ( &dns->search );
		if ( ( rc = dns_question ( dns ) ) != 0 ) {
			dns_done ( dns, rc );
			goto done;
		}
		dns_send_packet ( dns );
		goto done;

	default:
		assert ( 0 );
		rc = -EINVAL;
		dns_done ( dns, rc );
		goto done;
	}

 done:
	/* Free I/O buffer */
	free_iob ( iobuf );
	return rc;
}

/**
 * Receive new data
 *
 * @v dns		DNS request
 * @v rc		Reason for close
 */
static void dns_xfer_close ( struct dns_request *dns, int rc ) {

	if ( ! rc )
		rc = -ECONNABORTED;

	dns_done ( dns, rc );
}

/** DNS socket interface operations */
static struct interface_operation dns_socket_operations[] = {
	INTF_OP ( xfer_deliver, struct dns_request *, dns_xfer_deliver ),
	INTF_OP ( intf_close, struct dns_request *, dns_xfer_close ),
};

/** DNS socket interface descriptor */
static struct interface_descriptor dns_socket_desc =
	INTF_DESC ( struct dns_request, socket, dns_socket_operations );

/** DNS resolver interface operations */
static struct interface_operation dns_resolv_op[] = {
	INTF_OP ( intf_close, struct dns_request *, dns_done ),
};

/** DNS resolver interface descriptor */
static struct interface_descriptor dns_resolv_desc =
	INTF_DESC ( struct dns_request, resolv, dns_resolv_op );

/**
 * Resolve name using DNS
 *
 * @v resolv		Name resolution interface
 * @v name		Name to resolve
 * @v sa		Socket address to fill in
 * @ret rc		Return status code
 */
static int dns_resolv ( struct interface *resolv,
			const char *name, struct sockaddr *sa ) {
	struct dns_request *dns;
	struct dns_header *query;
	size_t search_len;
	int name_len;
	int rc;

	/* Fail immediately if no DNS servers */
	if ( ! nameserver.sa.sa_family ) {
		DBG ( "DNS not attempting to resolve \"%s\": "
		      "no DNS servers\n", name );
		rc = -ENXIO_NO_NAMESERVER;
		goto err_no_nameserver;
	}

	/* Determine whether or not to use search list */
	search_len = ( strchr ( name, '.' ) ? 0 : dns_search.len );

	/* Allocate DNS structure */
	dns = zalloc ( sizeof ( *dns ) + search_len );
	if ( ! dns ) {
		rc = -ENOMEM;
		goto err_alloc_dns;
	}
	ref_init ( &dns->refcnt, NULL );
	intf_init ( &dns->resolv, &dns_resolv_desc, &dns->refcnt );
	intf_init ( &dns->socket, &dns_socket_desc, &dns->refcnt );
	timer_init ( &dns->timer, dns_timer_expired, &dns->refcnt );
	memcpy ( &dns->address.sa, sa, sizeof ( dns->address.sa ) );
	dns->search.data = ( ( ( void * ) dns ) + sizeof ( *dns ) );
	dns->search.len = search_len;
	memcpy ( dns->search.data, dns_search.data, search_len );

	/* Determine initial query type */
	switch ( nameserver.sa.sa_family ) {
	case AF_INET:
		dns->qtype = htons ( DNS_TYPE_A );
		break;
	case AF_INET6:
		dns->qtype = htons ( DNS_TYPE_AAAA );
		break;
	default:
		rc = -ENOTSUP;
		goto err_type;
	}

	/* Construct query */
	query = &dns->buf.query;
	query->flags = htons ( DNS_FLAG_RD );
	query->qdcount = htons ( 1 );
	dns->name.data = &dns->buf;
	dns->name.offset = offsetof ( typeof ( dns->buf ), name );
	dns->name.len = offsetof ( typeof ( dns->buf ), padding );
	name_len = dns_encode ( name, &dns->name );
	if ( name_len < 0 ) {
		rc = name_len;
		goto err_encode;
	}
	dns->offset = ( offsetof ( typeof ( dns->buf ), name ) +
			name_len - 1 /* Strip root label */ );
	if ( ( rc = dns_question ( dns ) ) != 0 )
		goto err_question;

	/* Open UDP connection */
	if ( ( rc = xfer_open_socket ( &dns->socket, SOCK_DGRAM,
				       &nameserver.sa, NULL ) ) != 0 ) {
		DBGC ( dns, "DNS %p could not open socket: %s\n",
		       dns, strerror ( rc ) );
		goto err_open_socket;
	}

	/* Start timer to trigger first packet */
	start_timer_nodelay ( &dns->timer );

	/* Attach parent interface, mortalise self, and return */
	intf_plug_plug ( &dns->resolv, resolv );
	ref_put ( &dns->refcnt );
	return 0;	

 err_open_socket:
 err_question:
 err_encode:
 err_type:
	ref_put ( &dns->refcnt );
 err_alloc_dns:
 err_no_nameserver:
	return rc;
}

/** DNS name resolver */
struct resolver dns_resolver __resolver ( RESOLV_NORMAL ) = {
	.name = "DNS",
	.resolv = dns_resolv,
};

/******************************************************************************
 *
 * Settings
 *
 ******************************************************************************
 */

/**
 * Format DNS search list setting
 *
 * @v type		Setting type
 * @v raw		Raw setting value
 * @v raw_len		Length of raw setting value
 * @v buf		Buffer to contain formatted value
 * @v len		Length of buffer
 * @ret len		Length of formatted value, or negative error
 */
static int format_dnssl_setting ( const struct setting_type *type __unused,
				  const void *raw, size_t raw_len,
				  char *buf, size_t len ) {
	struct dns_name name = {
		.data = ( ( void * ) raw ),
		.len = raw_len,
	};
	size_t remaining = len;
	size_t total = 0;
	int name_len;

	while ( name.offset < raw_len ) {

		/* Decode name */
		remaining = ( ( total < len ) ? ( len - total ) : 0 );
		name_len = dns_decode ( &name, ( buf + total ), remaining );
		if ( name_len < 0 )
			return name_len;
		total += name_len;

		/* Move to next name */
		name.offset = dns_skip_search ( &name );

		/* Add separator if applicable */
		if ( name.offset != raw_len ) {
			if ( total < len )
				buf[total] = ' ';
			total++;
		}
	}

	return total;
}

/** A DNS search list setting type */
const struct setting_type setting_type_dnssl __setting_type = {
	.name = "dnssl",
	.format = format_dnssl_setting,
};

/** IPv4 DNS server setting */
const struct setting dns_setting __setting ( SETTING_IP_EXTRA, dns ) = {
	.name = "dns",
	.description = "DNS server",
	.tag = DHCP_DNS_SERVERS,
	.type = &setting_type_ipv4,
};

/** IPv6 DNS server setting */
const struct setting dns6_setting __setting ( SETTING_IP_EXTRA, dns6 ) = {
	.name = "dns6",
	.description = "DNS server",
	.tag = DHCPV6_DNS_SERVERS,
	.type = &setting_type_ipv6,
	.scope = &ipv6_scope,
};

/** DNS search list */
const struct setting dnssl_setting __setting ( SETTING_IP_EXTRA, dnssl ) = {
	.name = "dnssl",
	.description = "DNS search list",
	.tag = DHCP_DOMAIN_SEARCH,
	.type = &setting_type_dnssl,
};

/**
 * Apply DNS search list
 *
 */
static void apply_dns_search ( void ) {
	char *localdomain;
	int len;

	/* Free existing search list */
	free ( dns_search.data );
	memset ( &dns_search, 0, sizeof ( dns_search ) );

	/* Fetch DNS search list */
	len = fetch_setting_copy ( NULL, &dnssl_setting, NULL, NULL,
				   &dns_search.data );
	if ( len >= 0 ) {
		dns_search.len = len;
		return;
	}

	/* If no DNS search list exists, try to fetch the local domain */
	fetch_string_setting_copy ( NULL, &domain_setting, &localdomain );
	if ( localdomain ) {
		len = dns_encode ( localdomain, &dns_search );
		if ( len >= 0 ) {
			dns_search.data = malloc ( len );
			if ( dns_search.data ) {
				dns_search.len = len;
				dns_encode ( localdomain, &dns_search );
			}
		}
		free ( localdomain );
		return;
	}
}

/**
 * Apply DNS settings
 *
 * @ret rc		Return status code
 */
static int apply_dns_settings ( void ) {

	/* Fetch DNS server address */
	nameserver.sa.sa_family = 0;
	if ( fetch_ipv6_setting ( NULL, &dns6_setting,
				  &nameserver.sin6.sin6_addr ) >= 0 ) {
		nameserver.sin6.sin6_family = AF_INET6;
	} else if ( fetch_ipv4_setting ( NULL, &dns_setting,
					 &nameserver.sin.sin_addr ) >= 0 ) {
		nameserver.sin.sin_family = AF_INET;
	}
	if ( nameserver.sa.sa_family ) {
		DBG ( "DNS using nameserver %s\n",
		      sock_ntoa ( &nameserver.sa ) );
	}

	/* Fetch DNS search list */
	apply_dns_search();
	if ( DBG_LOG && ( dns_search.len != 0 ) ) {
		struct dns_name name;
		int offset;

		DBG ( "DNS search list:" );
		memcpy ( &name, &dns_search, sizeof ( name ) );
		while ( name.offset != name.len ) {
			DBG ( " %s", dns_name ( &name ) );
			offset = dns_skip_search ( &name );
			if ( offset < 0 )
				break;
			name.offset = offset;
		}
		DBG ( "\n" );
	}

	return 0;
}

/** DNS settings applicator */
struct settings_applicator dns_applicator __settings_applicator = {
	.apply = apply_dns_settings,
};