xref: /freebsd/contrib/unbound/util/data/msgparse.c (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1 /*
2  * util/data/msgparse.c - parse wireformat DNS messages.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 /**
36  * \file
37  * Routines for message parsing a packet buffer to a descriptive structure.
38  */
39 #include "config.h"
40 #include "util/data/msgparse.h"
41 #include "util/data/dname.h"
42 #include "util/data/packed_rrset.h"
43 #include "util/storage/lookup3.h"
44 #include "util/regional.h"
45 #include "ldns/rrdef.h"
46 #include "ldns/sbuffer.h"
47 #include "ldns/parseutil.h"
48 #include "ldns/wire2str.h"
49 
50 /** smart comparison of (compressed, valid) dnames from packet */
51 static int
52 smart_compare(sldns_buffer* pkt, uint8_t* dnow,
53 	uint8_t* dprfirst, uint8_t* dprlast)
54 {
55 	if(LABEL_IS_PTR(*dnow)) {
56 		/* ptr points to a previous dname */
57 		uint8_t* p = sldns_buffer_at(pkt, PTR_OFFSET(dnow[0], dnow[1]));
58 		if( p == dprfirst || p == dprlast )
59 			return 0;
60 		/* prev dname is also a ptr, both ptrs are the same. */
61 		if(LABEL_IS_PTR(*dprlast) &&
62 			dprlast[0] == dnow[0] && dprlast[1] == dnow[1])
63 			return 0;
64 	}
65 	return dname_pkt_compare(pkt, dnow, dprlast);
66 }
67 
68 /**
69  * Allocate new rrset in region, fill with data.
70  */
71 static struct rrset_parse*
72 new_rrset(struct msg_parse* msg, uint8_t* dname, size_t dnamelen,
73 	uint16_t type, uint16_t dclass, hashvalue_t hash,
74 	uint32_t rrset_flags, sldns_pkt_section section,
75 	struct regional* region)
76 {
77 	struct rrset_parse* p = regional_alloc(region, sizeof(*p));
78 	if(!p) return NULL;
79 	p->rrset_bucket_next = msg->hashtable[hash & (PARSE_TABLE_SIZE-1)];
80 	msg->hashtable[hash & (PARSE_TABLE_SIZE-1)] = p;
81 	p->rrset_all_next = 0;
82 	if(msg->rrset_last)
83 		msg->rrset_last->rrset_all_next = p;
84 	else 	msg->rrset_first = p;
85 	msg->rrset_last = p;
86 	p->hash = hash;
87 	p->section = section;
88 	p->dname = dname;
89 	p->dname_len = dnamelen;
90 	p->type = type;
91 	p->rrset_class = dclass;
92 	p->flags = rrset_flags;
93 	p->rr_count = 0;
94 	p->size = 0;
95 	p->rr_first = 0;
96 	p->rr_last = 0;
97 	p->rrsig_count = 0;
98 	p->rrsig_first = 0;
99 	p->rrsig_last = 0;
100 	return p;
101 }
102 
103 /** See if next rrset is nsec at zone apex */
104 static int
105 nsec_at_apex(sldns_buffer* pkt)
106 {
107 	/* we are at ttl position in packet. */
108 	size_t pos = sldns_buffer_position(pkt);
109 	uint16_t rdatalen;
110 	if(sldns_buffer_remaining(pkt) < 7) /* ttl+len+root */
111 		return 0; /* eek! */
112 	sldns_buffer_skip(pkt, 4); /* ttl */;
113 	rdatalen = sldns_buffer_read_u16(pkt);
114 	if(sldns_buffer_remaining(pkt) < rdatalen) {
115 		sldns_buffer_set_position(pkt, pos);
116 		return 0; /* parse error happens later */
117 	}
118 	/* must validate the nsec next domain name format */
119 	if(pkt_dname_len(pkt) == 0) {
120 		sldns_buffer_set_position(pkt, pos);
121 		return 0; /* parse error */
122 	}
123 
124 	/* see if SOA bit is set. */
125 	if(sldns_buffer_position(pkt) < pos+4+rdatalen) {
126 		/* nsec type bitmap contains items */
127 		uint8_t win, blen, bits;
128 		/* need: windownum, bitmap len, firstbyte */
129 		if(sldns_buffer_position(pkt)+3 > pos+4+rdatalen) {
130 			sldns_buffer_set_position(pkt, pos);
131 			return 0; /* malformed nsec */
132 		}
133 		win = sldns_buffer_read_u8(pkt);
134 		blen = sldns_buffer_read_u8(pkt);
135 		bits = sldns_buffer_read_u8(pkt);
136 		/* 0window always first window. bitlen >=1 or parse
137 		   error really. bit 0x2 is SOA. */
138 		if(win == 0 && blen >= 1 && (bits & 0x02)) {
139 			sldns_buffer_set_position(pkt, pos);
140 			return 1;
141 		}
142 	}
143 
144 	sldns_buffer_set_position(pkt, pos);
145 	return 0;
146 }
147 
148 /** Calculate rrset flags */
149 static uint32_t
150 pkt_rrset_flags(sldns_buffer* pkt, uint16_t type, sldns_pkt_section sec)
151 {
152 	uint32_t f = 0;
153 	if(type == LDNS_RR_TYPE_NSEC && nsec_at_apex(pkt)) {
154 		f |= PACKED_RRSET_NSEC_AT_APEX;
155 	} else if(type == LDNS_RR_TYPE_SOA && sec == LDNS_SECTION_AUTHORITY) {
156 		f |= PACKED_RRSET_SOA_NEG;
157 	}
158 	return f;
159 }
160 
161 hashvalue_t
162 pkt_hash_rrset(sldns_buffer* pkt, uint8_t* dname, uint16_t type,
163 	uint16_t dclass, uint32_t rrset_flags)
164 {
165 	/* note this MUST be identical to rrset_key_hash in packed_rrset.c */
166 	/* this routine handles compressed names */
167 	hashvalue_t h = 0xab;
168 	h = dname_pkt_hash(pkt, dname, h);
169 	h = hashlittle(&type, sizeof(type), h);		/* host order */
170 	h = hashlittle(&dclass, sizeof(dclass), h);	/* netw order */
171 	h = hashlittle(&rrset_flags, sizeof(uint32_t), h);
172 	return h;
173 }
174 
175 /** create partial dname hash for rrset hash */
176 static hashvalue_t
177 pkt_hash_rrset_first(sldns_buffer* pkt, uint8_t* dname)
178 {
179 	/* works together with pkt_hash_rrset_rest */
180 	/* note this MUST be identical to rrset_key_hash in packed_rrset.c */
181 	/* this routine handles compressed names */
182 	hashvalue_t h = 0xab;
183 	h = dname_pkt_hash(pkt, dname, h);
184 	return h;
185 }
186 
187 /** create a rrset hash from a partial dname hash */
188 static hashvalue_t
189 pkt_hash_rrset_rest(hashvalue_t dname_h, uint16_t type, uint16_t dclass,
190 	uint32_t rrset_flags)
191 {
192 	/* works together with pkt_hash_rrset_first */
193 	/* note this MUST be identical to rrset_key_hash in packed_rrset.c */
194 	hashvalue_t h;
195 	h = hashlittle(&type, sizeof(type), dname_h);	/* host order */
196 	h = hashlittle(&dclass, sizeof(dclass), h);	/* netw order */
197 	h = hashlittle(&rrset_flags, sizeof(uint32_t), h);
198 	return h;
199 }
200 
201 /** compare rrset_parse with data */
202 static int
203 rrset_parse_equals(struct rrset_parse* p, sldns_buffer* pkt, hashvalue_t h,
204 	uint32_t rrset_flags, uint8_t* dname, size_t dnamelen,
205 	uint16_t type, uint16_t dclass)
206 {
207 	if(p->hash == h && p->dname_len == dnamelen && p->type == type &&
208 		p->rrset_class == dclass && p->flags == rrset_flags &&
209 		dname_pkt_compare(pkt, dname, p->dname) == 0)
210 		return 1;
211 	return 0;
212 }
213 
214 
215 struct rrset_parse*
216 msgparse_hashtable_lookup(struct msg_parse* msg, sldns_buffer* pkt,
217 	hashvalue_t h, uint32_t rrset_flags, uint8_t* dname, size_t dnamelen,
218 	uint16_t type, uint16_t dclass)
219 {
220 	struct rrset_parse* p = msg->hashtable[h & (PARSE_TABLE_SIZE-1)];
221 	while(p) {
222 		if(rrset_parse_equals(p, pkt, h, rrset_flags, dname, dnamelen,
223 			type, dclass))
224 			return p;
225 		p = p->rrset_bucket_next;
226 	}
227 	return NULL;
228 }
229 
230 /** return type networkformat that rrsig in packet covers */
231 static int
232 pkt_rrsig_covered(sldns_buffer* pkt, uint8_t* here, uint16_t* type)
233 {
234 	size_t pos = sldns_buffer_position(pkt);
235 	sldns_buffer_set_position(pkt, (size_t)(here-sldns_buffer_begin(pkt)));
236 	/* ttl + len + size of small rrsig(rootlabel, no signature) */
237 	if(sldns_buffer_remaining(pkt) < 4+2+19)
238 		return 0;
239 	sldns_buffer_skip(pkt, 4); /* ttl */
240 	if(sldns_buffer_read_u16(pkt) < 19) /* too short */ {
241 		sldns_buffer_set_position(pkt, pos);
242 		return 0;
243 	}
244 	*type = sldns_buffer_read_u16(pkt);
245 	sldns_buffer_set_position(pkt, pos);
246 	return 1;
247 }
248 
249 /** true if covered type equals prevtype */
250 static int
251 pkt_rrsig_covered_equals(sldns_buffer* pkt, uint8_t* here, uint16_t type)
252 {
253 	uint16_t t;
254 	if(pkt_rrsig_covered(pkt, here, &t) && t == type)
255 		return 1;
256 	return 0;
257 }
258 
259 void
260 msgparse_bucket_remove(struct msg_parse* msg, struct rrset_parse* rrset)
261 {
262 	struct rrset_parse** p;
263 	p = &msg->hashtable[ rrset->hash & (PARSE_TABLE_SIZE-1) ];
264 	while(*p) {
265 		if(*p == rrset) {
266 			*p = rrset->rrset_bucket_next;
267 			return;
268 		}
269 		p = &( (*p)->rrset_bucket_next );
270 	}
271 }
272 
273 /** change section of rrset from previous to current section */
274 static void
275 change_section(struct msg_parse* msg, struct rrset_parse* rrset,
276 	sldns_pkt_section section)
277 {
278 	struct rrset_parse *p, *prev;
279 	/* remove from list */
280 	if(section == rrset->section)
281 		return;
282 	p = msg->rrset_first;
283 	prev = 0;
284 	while(p) {
285 		if(p == rrset) {
286 			if(prev) prev->rrset_all_next = p->rrset_all_next;
287 			else	msg->rrset_first = p->rrset_all_next;
288 			if(msg->rrset_last == rrset)
289 				msg->rrset_last = prev;
290 			break;
291 		}
292 		prev = p;
293 		p = p->rrset_all_next;
294 	}
295 	/* remove from count */
296 	switch(rrset->section) {
297 		case LDNS_SECTION_ANSWER: msg->an_rrsets--; break;
298 		case LDNS_SECTION_AUTHORITY: msg->ns_rrsets--; break;
299 		case LDNS_SECTION_ADDITIONAL: msg->ar_rrsets--; break;
300 		default: log_assert(0);
301 	}
302 	/* insert at end of list */
303 	rrset->rrset_all_next = 0;
304 	if(msg->rrset_last)
305 		msg->rrset_last->rrset_all_next = rrset;
306 	else	msg->rrset_first = rrset;
307 	msg->rrset_last = rrset;
308 	/* up count of new section */
309 	switch(section) {
310 		case LDNS_SECTION_AUTHORITY: msg->ns_rrsets++; break;
311 		case LDNS_SECTION_ADDITIONAL: msg->ar_rrsets++; break;
312 		default: log_assert(0);
313 	}
314 	rrset->section = section;
315 }
316 
317 /** see if rrset of type RRSIG contains sig over given type */
318 static int
319 rrset_has_sigover(sldns_buffer* pkt, struct rrset_parse* rrset, uint16_t type,
320 	int* hasother)
321 {
322 	int res = 0;
323 	struct rr_parse* rr = rrset->rr_first;
324 	log_assert( rrset->type == LDNS_RR_TYPE_RRSIG );
325 	while(rr) {
326 		if(pkt_rrsig_covered_equals(pkt, rr->ttl_data, type))
327 			res = 1;
328 		else	*hasother = 1;
329 		rr = rr->next;
330 	}
331 	return res;
332 }
333 
334 /** move rrsigs from sigset to dataset */
335 static int
336 moveover_rrsigs(sldns_buffer* pkt, struct regional* region,
337 	struct rrset_parse* sigset, struct rrset_parse* dataset, int duplicate)
338 {
339 	struct rr_parse* sig = sigset->rr_first;
340 	struct rr_parse* prev = NULL;
341 	struct rr_parse* insert;
342 	struct rr_parse* nextsig;
343 	while(sig) {
344 		nextsig = sig->next;
345 		if(pkt_rrsig_covered_equals(pkt, sig->ttl_data,
346 			dataset->type)) {
347 			if(duplicate) {
348 				/* new */
349 				insert = (struct rr_parse*)regional_alloc(
350 					region, sizeof(struct rr_parse));
351 				if(!insert) return 0;
352 				insert->outside_packet = 0;
353 				insert->ttl_data = sig->ttl_data;
354 				insert->size = sig->size;
355 				/* prev not used */
356 			} else {
357 				/* remove from sigset */
358 				if(prev) prev->next = sig->next;
359 				else	sigset->rr_first = sig->next;
360 				if(sigset->rr_last == sig)
361 					sigset->rr_last = prev;
362 				sigset->rr_count--;
363 				sigset->size -= sig->size;
364 				insert = sig;
365 				/* prev not changed */
366 			}
367 			/* add to dataset */
368 			dataset->rrsig_count++;
369 			insert->next = 0;
370 			if(dataset->rrsig_last)
371 				dataset->rrsig_last->next = insert;
372 			else	dataset->rrsig_first = insert;
373 			dataset->rrsig_last = insert;
374 			dataset->size += insert->size;
375 		} else  {
376 			prev = sig;
377 		}
378 		sig = nextsig;
379 	}
380 	return 1;
381 }
382 
383 /** change an rrsig rrset for use as data rrset */
384 static struct rrset_parse*
385 change_rrsig_rrset(struct rrset_parse* sigset, struct msg_parse* msg,
386 	sldns_buffer* pkt, uint16_t datatype, uint32_t rrset_flags,
387 	int hasother, sldns_pkt_section section, struct regional* region)
388 {
389 	struct rrset_parse* dataset = sigset;
390 	hashvalue_t hash = pkt_hash_rrset(pkt, sigset->dname, datatype,
391 		sigset->rrset_class, rrset_flags);
392 	log_assert( sigset->type == LDNS_RR_TYPE_RRSIG );
393 	log_assert( datatype != LDNS_RR_TYPE_RRSIG );
394 	if(hasother) {
395 		/* need to make new rrset to hold data type */
396 		dataset = new_rrset(msg, sigset->dname, sigset->dname_len,
397 			datatype, sigset->rrset_class, hash, rrset_flags,
398 			section, region);
399 		if(!dataset)
400 			return NULL;
401 		switch(section) {
402 			case LDNS_SECTION_ANSWER: msg->an_rrsets++; break;
403 			case LDNS_SECTION_AUTHORITY: msg->ns_rrsets++; break;
404 			case LDNS_SECTION_ADDITIONAL: msg->ar_rrsets++; break;
405 			default: log_assert(0);
406 		}
407 		if(!moveover_rrsigs(pkt, region, sigset, dataset,
408 			msg->qtype == LDNS_RR_TYPE_RRSIG ||
409 			(msg->qtype == LDNS_RR_TYPE_ANY &&
410 			section != LDNS_SECTION_ANSWER) ))
411 			return NULL;
412 		return dataset;
413 	}
414 	/* changeover the type of the rrset to data set */
415 	msgparse_bucket_remove(msg, dataset);
416 	/* insert into new hash bucket */
417 	dataset->rrset_bucket_next = msg->hashtable[hash&(PARSE_TABLE_SIZE-1)];
418 	msg->hashtable[hash&(PARSE_TABLE_SIZE-1)] = dataset;
419 	dataset->hash = hash;
420 	/* use section of data item for result */
421 	change_section(msg, dataset, section);
422 	dataset->type = datatype;
423 	dataset->flags = rrset_flags;
424 	dataset->rrsig_count += dataset->rr_count;
425 	dataset->rr_count = 0;
426 	/* move sigs to end of siglist */
427 	if(dataset->rrsig_last)
428 		dataset->rrsig_last->next = dataset->rr_first;
429 	else	dataset->rrsig_first = dataset->rr_first;
430 	dataset->rrsig_last = dataset->rr_last;
431 	dataset->rr_first = 0;
432 	dataset->rr_last = 0;
433 	return dataset;
434 }
435 
436 /** Find rrset. If equal to previous it is fast. hash if not so.
437  * @param msg: the message with hash table.
438  * @param pkt: the packet in wireformat (needed for compression ptrs).
439  * @param dname: pointer to start of dname (compressed) in packet.
440  * @param dnamelen: uncompressed wirefmt length of dname.
441  * @param type: type of current rr.
442  * @param dclass: class of current rr.
443  * @param hash: hash value is returned if the rrset could not be found.
444  * @param rrset_flags: is returned if the rrset could not be found.
445  * @param prev_dname_first: dname of last seen RR. First seen dname.
446  * @param prev_dname_last: dname of last seen RR. Last seen dname.
447  * @param prev_dnamelen: dname len of last seen RR.
448  * @param prev_type: type of last seen RR.
449  * @param prev_dclass: class of last seen RR.
450  * @param rrset_prev: last seen RRset.
451  * @param section: the current section in the packet.
452  * @param region: used to allocate temporary parsing data.
453  * @return 0 on out of memory.
454  */
455 static int
456 find_rrset(struct msg_parse* msg, sldns_buffer* pkt, uint8_t* dname,
457 	size_t dnamelen, uint16_t type, uint16_t dclass, hashvalue_t* hash,
458 	uint32_t* rrset_flags,
459 	uint8_t** prev_dname_first, uint8_t** prev_dname_last,
460 	size_t* prev_dnamelen, uint16_t* prev_type,
461 	uint16_t* prev_dclass, struct rrset_parse** rrset_prev,
462 	sldns_pkt_section section, struct regional* region)
463 {
464 	hashvalue_t dname_h = pkt_hash_rrset_first(pkt, dname);
465 	uint16_t covtype;
466 	if(*rrset_prev) {
467 		/* check if equal to previous item */
468 		if(type == *prev_type && dclass == *prev_dclass &&
469 			dnamelen == *prev_dnamelen &&
470 			smart_compare(pkt, dname, *prev_dname_first,
471 				*prev_dname_last) == 0 &&
472 			type != LDNS_RR_TYPE_RRSIG) {
473 			/* same as previous */
474 			*prev_dname_last = dname;
475 			return 1;
476 		}
477 		/* check if rrsig over previous item */
478 		if(type == LDNS_RR_TYPE_RRSIG && dclass == *prev_dclass &&
479 			pkt_rrsig_covered_equals(pkt, sldns_buffer_current(pkt),
480 				*prev_type) &&
481 			smart_compare(pkt, dname, *prev_dname_first,
482 				*prev_dname_last) == 0) {
483 			/* covers previous */
484 			*prev_dname_last = dname;
485 			return 1;
486 		}
487 	}
488 	/* find by hashing and lookup in hashtable */
489 	*rrset_flags = pkt_rrset_flags(pkt, type, section);
490 
491 	/* if rrsig - try to lookup matching data set first */
492 	if(type == LDNS_RR_TYPE_RRSIG && pkt_rrsig_covered(pkt,
493 		sldns_buffer_current(pkt), &covtype)) {
494 		*hash = pkt_hash_rrset_rest(dname_h, covtype, dclass,
495 			*rrset_flags);
496 		*rrset_prev = msgparse_hashtable_lookup(msg, pkt, *hash,
497 			*rrset_flags, dname, dnamelen, covtype, dclass);
498 		if(!*rrset_prev && covtype == LDNS_RR_TYPE_NSEC) {
499 			/* if NSEC try with NSEC apex bit twiddled */
500 			*rrset_flags ^= PACKED_RRSET_NSEC_AT_APEX;
501 			*hash = pkt_hash_rrset_rest(dname_h, covtype, dclass,
502 				*rrset_flags);
503 			*rrset_prev = msgparse_hashtable_lookup(msg, pkt,
504 				*hash, *rrset_flags, dname, dnamelen, covtype,
505 				dclass);
506 			if(!*rrset_prev) /* untwiddle if not found */
507 				*rrset_flags ^= PACKED_RRSET_NSEC_AT_APEX;
508 		}
509 		if(!*rrset_prev && covtype == LDNS_RR_TYPE_SOA) {
510 			/* if SOA try with SOA neg flag twiddled */
511 			*rrset_flags ^= PACKED_RRSET_SOA_NEG;
512 			*hash = pkt_hash_rrset_rest(dname_h, covtype, dclass,
513 				*rrset_flags);
514 			*rrset_prev = msgparse_hashtable_lookup(msg, pkt,
515 				*hash, *rrset_flags, dname, dnamelen, covtype,
516 				dclass);
517 			if(!*rrset_prev) /* untwiddle if not found */
518 				*rrset_flags ^= PACKED_RRSET_SOA_NEG;
519 		}
520 		if(*rrset_prev) {
521 			*prev_dname_first = (*rrset_prev)->dname;
522 			*prev_dname_last = dname;
523 			*prev_dnamelen = dnamelen;
524 			*prev_type = covtype;
525 			*prev_dclass = dclass;
526 			return 1;
527 		}
528 	}
529 	if(type != LDNS_RR_TYPE_RRSIG) {
530 		int hasother = 0;
531 		/* find matching rrsig */
532 		*hash = pkt_hash_rrset_rest(dname_h, LDNS_RR_TYPE_RRSIG,
533 			dclass, 0);
534 		*rrset_prev = msgparse_hashtable_lookup(msg, pkt, *hash,
535 			0, dname, dnamelen, LDNS_RR_TYPE_RRSIG,
536 			dclass);
537 		if(*rrset_prev && rrset_has_sigover(pkt, *rrset_prev, type,
538 			&hasother)) {
539 			/* yes! */
540 			*prev_dname_first = (*rrset_prev)->dname;
541 			*prev_dname_last = dname;
542 			*prev_dnamelen = dnamelen;
543 			*prev_type = type;
544 			*prev_dclass = dclass;
545 			*rrset_prev = change_rrsig_rrset(*rrset_prev, msg,
546 				pkt, type, *rrset_flags, hasother, section,
547 				region);
548 			if(!*rrset_prev) return 0;
549 			return 1;
550 		}
551 	}
552 
553 	*hash = pkt_hash_rrset_rest(dname_h, type, dclass, *rrset_flags);
554 	*rrset_prev = msgparse_hashtable_lookup(msg, pkt, *hash, *rrset_flags,
555 		dname, dnamelen, type, dclass);
556 	if(*rrset_prev)
557 		*prev_dname_first = (*rrset_prev)->dname;
558 	else 	*prev_dname_first = dname;
559 	*prev_dname_last = dname;
560 	*prev_dnamelen = dnamelen;
561 	*prev_type = type;
562 	*prev_dclass = dclass;
563 	return 1;
564 }
565 
566 /**
567  * Parse query section.
568  * @param pkt: packet, position at call must be at start of query section.
569  *	at end position is after query section.
570  * @param msg: store results here.
571  * @return: 0 if OK, or rcode on error.
572  */
573 static int
574 parse_query_section(sldns_buffer* pkt, struct msg_parse* msg)
575 {
576 	if(msg->qdcount == 0)
577 		return 0;
578 	if(msg->qdcount > 1)
579 		return LDNS_RCODE_FORMERR;
580 	log_assert(msg->qdcount == 1);
581 	if(sldns_buffer_remaining(pkt) <= 0)
582 		return LDNS_RCODE_FORMERR;
583 	msg->qname = sldns_buffer_current(pkt);
584 	if((msg->qname_len = pkt_dname_len(pkt)) == 0)
585 		return LDNS_RCODE_FORMERR;
586 	if(sldns_buffer_remaining(pkt) < sizeof(uint16_t)*2)
587 		return LDNS_RCODE_FORMERR;
588 	msg->qtype = sldns_buffer_read_u16(pkt);
589 	msg->qclass = sldns_buffer_read_u16(pkt);
590 	return 0;
591 }
592 
593 size_t
594 get_rdf_size(sldns_rdf_type rdf)
595 {
596 	switch(rdf) {
597 		case LDNS_RDF_TYPE_CLASS:
598 		case LDNS_RDF_TYPE_ALG:
599 		case LDNS_RDF_TYPE_INT8:
600 			return 1;
601 			break;
602 		case LDNS_RDF_TYPE_INT16:
603 		case LDNS_RDF_TYPE_TYPE:
604 		case LDNS_RDF_TYPE_CERT_ALG:
605 			return 2;
606 			break;
607 		case LDNS_RDF_TYPE_INT32:
608 		case LDNS_RDF_TYPE_TIME:
609 		case LDNS_RDF_TYPE_A:
610 		case LDNS_RDF_TYPE_PERIOD:
611 			return 4;
612 			break;
613 		case LDNS_RDF_TYPE_TSIGTIME:
614 			return 6;
615 			break;
616 		case LDNS_RDF_TYPE_AAAA:
617 			return 16;
618 			break;
619 		default:
620 			log_assert(0); /* add type above */
621 			/* only types that appear before a domain  *
622 			 * name are needed. rest is simply copied. */
623 	}
624 	return 0;
625 }
626 
627 /** calculate the size of one rr */
628 static int
629 calc_size(sldns_buffer* pkt, uint16_t type, struct rr_parse* rr)
630 {
631 	const sldns_rr_descriptor* desc;
632 	uint16_t pkt_len; /* length of rr inside the packet */
633 	rr->size = sizeof(uint16_t); /* the rdatalen */
634 	sldns_buffer_skip(pkt, 4); /* skip ttl */
635 	pkt_len = sldns_buffer_read_u16(pkt);
636 	if(sldns_buffer_remaining(pkt) < pkt_len)
637 		return 0;
638 	desc = sldns_rr_descript(type);
639 	if(pkt_len > 0 && desc && desc->_dname_count > 0) {
640 		int count = (int)desc->_dname_count;
641 		int rdf = 0;
642 		size_t len;
643 		size_t oldpos;
644 		/* skip first part. */
645 		while(pkt_len > 0 && count) {
646 			switch(desc->_wireformat[rdf]) {
647 			case LDNS_RDF_TYPE_DNAME:
648 				/* decompress every domain name */
649 				oldpos = sldns_buffer_position(pkt);
650 				if((len = pkt_dname_len(pkt)) == 0)
651 					return 0; /* malformed dname */
652 				if(sldns_buffer_position(pkt)-oldpos > pkt_len)
653 					return 0; /* dname exceeds rdata */
654 				pkt_len -= sldns_buffer_position(pkt)-oldpos;
655 				rr->size += len;
656 				count--;
657 				len = 0;
658 				break;
659 			case LDNS_RDF_TYPE_STR:
660 				if(pkt_len < 1) {
661 					/* NOTREACHED, due to 'while(>0)' */
662 					return 0; /* len byte exceeds rdata */
663 				}
664 				len = sldns_buffer_current(pkt)[0] + 1;
665 				break;
666 			default:
667 				len = get_rdf_size(desc->_wireformat[rdf]);
668 			}
669 			if(len) {
670 				if(pkt_len < len)
671 					return 0; /* exceeds rdata */
672 				pkt_len -= len;
673 				sldns_buffer_skip(pkt, (ssize_t)len);
674 				rr->size += len;
675 			}
676 			rdf++;
677 		}
678 	}
679 	/* remaining rdata */
680 	rr->size += pkt_len;
681 	sldns_buffer_skip(pkt, (ssize_t)pkt_len);
682 	return 1;
683 }
684 
685 /** skip rr ttl and rdata */
686 static int
687 skip_ttl_rdata(sldns_buffer* pkt)
688 {
689 	uint16_t rdatalen;
690 	if(sldns_buffer_remaining(pkt) < 6) /* ttl + rdatalen */
691 		return 0;
692 	sldns_buffer_skip(pkt, 4); /* ttl */
693 	rdatalen = sldns_buffer_read_u16(pkt);
694 	if(sldns_buffer_remaining(pkt) < rdatalen)
695 		return 0;
696 	sldns_buffer_skip(pkt, (ssize_t)rdatalen);
697 	return 1;
698 }
699 
700 /** see if RRSIG is a duplicate of another */
701 static int
702 sig_is_double(sldns_buffer* pkt, struct rrset_parse* rrset, uint8_t* ttldata)
703 {
704 	uint16_t rlen, siglen;
705 	size_t pos = sldns_buffer_position(pkt);
706 	struct rr_parse* sig;
707 	if(sldns_buffer_remaining(pkt) < 6)
708 		return 0;
709 	sldns_buffer_skip(pkt, 4); /* ttl */
710 	rlen = sldns_buffer_read_u16(pkt);
711 	if(sldns_buffer_remaining(pkt) < rlen) {
712 		sldns_buffer_set_position(pkt, pos);
713 		return 0;
714 	}
715 	sldns_buffer_set_position(pkt, pos);
716 
717 	sig = rrset->rrsig_first;
718 	while(sig) {
719 		/* check if rdatalen is same */
720 		memmove(&siglen, sig->ttl_data+4, sizeof(siglen));
721 		siglen = ntohs(siglen);
722 		/* checks if data in packet is exactly the same, this means
723 		 * also dname in rdata is the same, but rrsig is not allowed
724 		 * to have compressed dnames anyway. If it is compressed anyway
725 		 * it will lead to duplicate rrs for qtype=RRSIG. (or ANY).
726 		 *
727 		 * Cannot use sig->size because size of the other one is not
728 		 * calculated yet.
729 		 */
730 		if(siglen == rlen) {
731 			if(siglen>0 && memcmp(sig->ttl_data+6, ttldata+6,
732 				siglen) == 0) {
733 				/* same! */
734 				return 1;
735 			}
736 		}
737 		sig = sig->next;
738 	}
739 	return 0;
740 }
741 
742 /** Add rr (from packet here) to rrset, skips rr */
743 static int
744 add_rr_to_rrset(struct rrset_parse* rrset, sldns_buffer* pkt,
745 	struct msg_parse* msg, struct regional* region,
746 	sldns_pkt_section section, uint16_t type)
747 {
748 	struct rr_parse* rr;
749 	/* check section of rrset. */
750 	if(rrset->section != section && type != LDNS_RR_TYPE_RRSIG &&
751 		rrset->type != LDNS_RR_TYPE_RRSIG) {
752 		/* silently drop it - we drop the last part, since
753 		 * trust in rr data depends on the section it is in.
754 		 * the less trustworthy part is discarded.
755 		 * also the last part is more likely to be incomplete.
756 		 * RFC 2181: must put RRset only once in response. */
757 		/*
758 		verbose(VERB_QUERY, "Packet contains rrset data in "
759 			"multiple sections, dropped last part.");
760 		log_buf(VERB_QUERY, "packet was", pkt);
761 		*/
762 		/* forwards */
763 		if(!skip_ttl_rdata(pkt))
764 			return LDNS_RCODE_FORMERR;
765 		return 0;
766 	}
767 
768 	if( (msg->qtype == LDNS_RR_TYPE_RRSIG ||
769 	     msg->qtype == LDNS_RR_TYPE_ANY)
770 	    && sig_is_double(pkt, rrset, sldns_buffer_current(pkt))) {
771 		if(!skip_ttl_rdata(pkt))
772 			return LDNS_RCODE_FORMERR;
773 		return 0;
774 	}
775 
776 	/* create rr */
777 	if(!(rr = (struct rr_parse*)regional_alloc(region, sizeof(*rr))))
778 		return LDNS_RCODE_SERVFAIL;
779 	rr->outside_packet = 0;
780 	rr->ttl_data = sldns_buffer_current(pkt);
781 	rr->next = 0;
782 	if(type == LDNS_RR_TYPE_RRSIG && rrset->type != LDNS_RR_TYPE_RRSIG) {
783 		if(rrset->rrsig_last)
784 			rrset->rrsig_last->next = rr;
785 		else	rrset->rrsig_first = rr;
786 		rrset->rrsig_last = rr;
787 		rrset->rrsig_count++;
788 	} else {
789 		if(rrset->rr_last)
790 			rrset->rr_last->next = rr;
791 		else	rrset->rr_first = rr;
792 		rrset->rr_last = rr;
793 		rrset->rr_count++;
794 	}
795 
796 	/* calc decompressed size */
797 	if(!calc_size(pkt, type, rr))
798 		return LDNS_RCODE_FORMERR;
799 	rrset->size += rr->size;
800 
801 	return 0;
802 }
803 
804 /**
805  * Parse packet RR section, for answer, authority and additional sections.
806  * @param pkt: packet, position at call must be at start of section.
807  *	at end position is after section.
808  * @param msg: store results here.
809  * @param region: how to alloc results.
810  * @param section: section enum.
811  * @param num_rrs: how many rrs are in the section.
812  * @param num_rrsets: returns number of rrsets in the section.
813  * @return: 0 if OK, or rcode on error.
814  */
815 static int
816 parse_section(sldns_buffer* pkt, struct msg_parse* msg,
817 	struct regional* region, sldns_pkt_section section,
818 	uint16_t num_rrs, size_t* num_rrsets)
819 {
820 	uint16_t i;
821 	uint8_t* dname, *prev_dname_f = NULL, *prev_dname_l = NULL;
822 	size_t dnamelen, prev_dnamelen = 0;
823 	uint16_t type, prev_type = 0;
824 	uint16_t dclass, prev_dclass = 0;
825 	uint32_t rrset_flags = 0;
826 	hashvalue_t hash = 0;
827 	struct rrset_parse* rrset = NULL;
828 	int r;
829 
830 	if(num_rrs == 0)
831 		return 0;
832 	if(sldns_buffer_remaining(pkt) <= 0)
833 		return LDNS_RCODE_FORMERR;
834 	for(i=0; i<num_rrs; i++) {
835 		/* parse this RR. */
836 		dname = sldns_buffer_current(pkt);
837 		if((dnamelen = pkt_dname_len(pkt)) == 0)
838 			return LDNS_RCODE_FORMERR;
839 		if(sldns_buffer_remaining(pkt) < 10) /* type, class, ttl, len */
840 			return LDNS_RCODE_FORMERR;
841 		type = sldns_buffer_read_u16(pkt);
842 		sldns_buffer_read(pkt, &dclass, sizeof(dclass));
843 
844 		if(0) { /* debug show what is being parsed. */
845 			if(type == LDNS_RR_TYPE_RRSIG) {
846 				uint16_t t;
847 				if(pkt_rrsig_covered(pkt,
848 					sldns_buffer_current(pkt), &t))
849 					fprintf(stderr, "parse of %s(%d) [%s(%d)]",
850 					sldns_rr_descript(type)?
851 					sldns_rr_descript(type)->_name: "??",
852 					(int)type,
853 					sldns_rr_descript(t)?
854 					sldns_rr_descript(t)->_name: "??",
855 					(int)t);
856 			} else
857 			  fprintf(stderr, "parse of %s(%d)",
858 				sldns_rr_descript(type)?
859 				sldns_rr_descript(type)->_name: "??",
860 				(int)type);
861 			fprintf(stderr, " %s(%d) ",
862 				sldns_lookup_by_id(sldns_rr_classes,
863 				(int)ntohs(dclass))?sldns_lookup_by_id(
864 				sldns_rr_classes, (int)ntohs(dclass))->name:
865 				"??", (int)ntohs(dclass));
866 			dname_print(stderr, pkt, dname);
867 			fprintf(stderr, "\n");
868 		}
869 
870 		/* see if it is part of an existing RR set */
871 		if(!find_rrset(msg, pkt, dname, dnamelen, type, dclass, &hash,
872 			&rrset_flags, &prev_dname_f, &prev_dname_l,
873 			&prev_dnamelen, &prev_type, &prev_dclass, &rrset,
874 			section, region))
875 			return LDNS_RCODE_SERVFAIL;
876 		if(!rrset) {
877 			/* it is a new RR set. hash&flags already calculated.*/
878 			(*num_rrsets)++;
879 			rrset = new_rrset(msg, dname, dnamelen, type, dclass,
880 				hash, rrset_flags, section, region);
881 			if(!rrset)
882 				return LDNS_RCODE_SERVFAIL;
883 		}
884 		else if(0)	{
885 			fprintf(stderr, "is part of existing: ");
886 			dname_print(stderr, pkt, rrset->dname);
887 			fprintf(stderr, " type %s(%d)\n",
888 				sldns_rr_descript(rrset->type)?
889 				sldns_rr_descript(rrset->type)->_name: "??",
890 				(int)rrset->type);
891 		}
892 		/* add to rrset. */
893 		if((r=add_rr_to_rrset(rrset, pkt, msg, region, section,
894 			type)) != 0)
895 			return r;
896 	}
897 	return 0;
898 }
899 
900 int
901 parse_packet(sldns_buffer* pkt, struct msg_parse* msg, struct regional* region)
902 {
903 	int ret;
904 	if(sldns_buffer_remaining(pkt) < LDNS_HEADER_SIZE)
905 		return LDNS_RCODE_FORMERR;
906 	/* read the header */
907 	sldns_buffer_read(pkt, &msg->id, sizeof(uint16_t));
908 	msg->flags = sldns_buffer_read_u16(pkt);
909 	msg->qdcount = sldns_buffer_read_u16(pkt);
910 	msg->ancount = sldns_buffer_read_u16(pkt);
911 	msg->nscount = sldns_buffer_read_u16(pkt);
912 	msg->arcount = sldns_buffer_read_u16(pkt);
913 	if(msg->qdcount > 1)
914 		return LDNS_RCODE_FORMERR;
915 	if((ret = parse_query_section(pkt, msg)) != 0)
916 		return ret;
917 	if((ret = parse_section(pkt, msg, region, LDNS_SECTION_ANSWER,
918 		msg->ancount, &msg->an_rrsets)) != 0)
919 		return ret;
920 	if((ret = parse_section(pkt, msg, region, LDNS_SECTION_AUTHORITY,
921 		msg->nscount, &msg->ns_rrsets)) != 0)
922 		return ret;
923 	if(sldns_buffer_remaining(pkt) == 0 && msg->arcount == 1) {
924 		/* BIND accepts leniently that an EDNS record is missing.
925 		 * so, we do too. */
926 	} else if((ret = parse_section(pkt, msg, region,
927 		LDNS_SECTION_ADDITIONAL, msg->arcount, &msg->ar_rrsets)) != 0)
928 		return ret;
929 	/* if(sldns_buffer_remaining(pkt) > 0) { */
930 		/* there is spurious data at end of packet. ignore */
931 	/* } */
932 	msg->rrset_count = msg->an_rrsets + msg->ns_rrsets + msg->ar_rrsets;
933 	return 0;
934 }
935 
936 int
937 parse_extract_edns(struct msg_parse* msg, struct edns_data* edns)
938 {
939 	struct rrset_parse* rrset = msg->rrset_first;
940 	struct rrset_parse* prev = 0;
941 	struct rrset_parse* found = 0;
942 	struct rrset_parse* found_prev = 0;
943 	/* since the class encodes the UDP size, we cannot use hash table to
944 	 * find the EDNS OPT record. Scan the packet. */
945 	while(rrset) {
946 		if(rrset->type == LDNS_RR_TYPE_OPT) {
947 			/* only one OPT RR allowed. */
948 			if(found) return LDNS_RCODE_FORMERR;
949 			/* found it! */
950 			found_prev = prev;
951 			found = rrset;
952 		}
953 		prev = rrset;
954 		rrset = rrset->rrset_all_next;
955 	}
956 	if(!found) {
957 		memset(edns, 0, sizeof(*edns));
958 		edns->udp_size = 512;
959 		return 0;
960 	}
961 	/* check the found RRset */
962 	/* most lenient check possible. ignore dname, use last opt */
963 	if(found->section != LDNS_SECTION_ADDITIONAL)
964 		return LDNS_RCODE_FORMERR;
965 	if(found->rr_count == 0)
966 		return LDNS_RCODE_FORMERR;
967 	if(0) { /* strict checking of dname and RRcount */
968 		if(found->dname_len != 1 || !found->dname
969 			|| found->dname[0] != 0) return LDNS_RCODE_FORMERR;
970 		if(found->rr_count != 1) return LDNS_RCODE_FORMERR;
971 	}
972 	log_assert(found->rr_first && found->rr_last);
973 
974 	/* remove from packet */
975 	if(found_prev)	found_prev->rrset_all_next = found->rrset_all_next;
976 	else	msg->rrset_first = found->rrset_all_next;
977 	if(found == msg->rrset_last)
978 		msg->rrset_last = found_prev;
979 	msg->arcount --;
980 	msg->ar_rrsets --;
981 	msg->rrset_count --;
982 
983 	/* take the data ! */
984 	edns->edns_present = 1;
985 	edns->ext_rcode = found->rr_last->ttl_data[0];
986 	edns->edns_version = found->rr_last->ttl_data[1];
987 	edns->bits = sldns_read_uint16(&found->rr_last->ttl_data[2]);
988 	edns->udp_size = ntohs(found->rrset_class);
989 	/* ignore rdata and rrsigs */
990 	return 0;
991 }
992 
993 int
994 parse_edns_from_pkt(sldns_buffer* pkt, struct edns_data* edns)
995 {
996 	log_assert(LDNS_QDCOUNT(sldns_buffer_begin(pkt)) == 1);
997 	log_assert(LDNS_ANCOUNT(sldns_buffer_begin(pkt)) == 0);
998 	log_assert(LDNS_NSCOUNT(sldns_buffer_begin(pkt)) == 0);
999 	/* check edns section is present */
1000 	if(LDNS_ARCOUNT(sldns_buffer_begin(pkt)) > 1) {
1001 		return LDNS_RCODE_FORMERR;
1002 	}
1003 	if(LDNS_ARCOUNT(sldns_buffer_begin(pkt)) == 0) {
1004 		memset(edns, 0, sizeof(*edns));
1005 		edns->udp_size = 512;
1006 		return 0;
1007 	}
1008 	/* domain name must be the root of length 1. */
1009 	if(pkt_dname_len(pkt) != 1)
1010 		return LDNS_RCODE_FORMERR;
1011 	if(sldns_buffer_remaining(pkt) < 10) /* type, class, ttl, rdatalen */
1012 		return LDNS_RCODE_FORMERR;
1013 	if(sldns_buffer_read_u16(pkt) != LDNS_RR_TYPE_OPT)
1014 		return LDNS_RCODE_FORMERR;
1015 	edns->edns_present = 1;
1016 	edns->udp_size = sldns_buffer_read_u16(pkt); /* class is udp size */
1017 	edns->ext_rcode = sldns_buffer_read_u8(pkt); /* ttl used for bits */
1018 	edns->edns_version = sldns_buffer_read_u8(pkt);
1019 	edns->bits = sldns_buffer_read_u16(pkt);
1020 	/* ignore rdata and rrsigs */
1021 	return 0;
1022 }
1023