xref: /freebsd/contrib/unbound/util/data/msgencode.c (revision 4928135658a9d0eaee37003df6137ab363fcb0b4)
1 /*
2  * util/data/msgencode.c - Encode DNS messages, queries and replies.
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 /**
37  * \file
38  *
39  * This file contains a routines to encode DNS messages.
40  */
41 
42 #include "config.h"
43 #include "util/data/msgencode.h"
44 #include "util/data/msgreply.h"
45 #include "util/data/msgparse.h"
46 #include "util/data/dname.h"
47 #include "util/log.h"
48 #include "util/regional.h"
49 #include "util/net_help.h"
50 #include "sldns/sbuffer.h"
51 #include "services/localzone.h"
52 
53 /** return code that means the function ran out of memory. negative so it does
54  * not conflict with DNS rcodes. */
55 #define RETVAL_OUTMEM	-2
56 /** return code that means the data did not fit (completely) in the packet */
57 #define RETVAL_TRUNC	-4
58 /** return code that means all is peachy keen. Equal to DNS rcode NOERROR */
59 #define RETVAL_OK	0
60 
61 /**
62  * Data structure to help domain name compression in outgoing messages.
63  * A tree of dnames and their offsets in the packet is kept.
64  * It is kept sorted, not canonical, but by label at least, so that after
65  * a lookup of a name you know its closest match, and the parent from that
66  * closest match. These are possible compression targets.
67  *
68  * It is a binary tree, not a rbtree or balanced tree, as the effort
69  * of keeping it balanced probably outweighs usefulness (given typical
70  * DNS packet size).
71  */
72 struct compress_tree_node {
73 	/** left node in tree, all smaller to this */
74 	struct compress_tree_node* left;
75 	/** right node in tree, all larger than this */
76 	struct compress_tree_node* right;
77 
78 	/** the parent node - not for tree, but zone parent. One less label */
79 	struct compress_tree_node* parent;
80 	/** the domain name for this node. Pointer to uncompressed memory. */
81 	uint8_t* dname;
82 	/** number of labels in domain name, kept to help compare func. */
83 	int labs;
84 	/** offset in packet that points to this dname */
85 	size_t offset;
86 };
87 
88 /**
89  * Find domain name in tree, returns exact and closest match.
90  * @param tree: root of tree.
91  * @param dname: pointer to uncompressed dname.
92  * @param labs: number of labels in domain name.
93  * @param match: closest or exact match.
94  *	guaranteed to be smaller or equal to the sought dname.
95  *	can be null if the tree is empty.
96  * @param matchlabels: number of labels that match with closest match.
97  *	can be zero is there is no match.
98  * @param insertpt: insert location for dname, if not found.
99  * @return: 0 if no exact match.
100  */
101 static int
102 compress_tree_search(struct compress_tree_node** tree, uint8_t* dname,
103 	int labs, struct compress_tree_node** match, int* matchlabels,
104 	struct compress_tree_node*** insertpt)
105 {
106 	int c, n, closen=0;
107 	struct compress_tree_node* p = *tree;
108 	struct compress_tree_node* close = 0;
109 	struct compress_tree_node** prev = tree;
110 	while(p) {
111 		if((c = dname_lab_cmp(dname, labs, p->dname, p->labs, &n))
112 			== 0) {
113 			*matchlabels = n;
114 			*match = p;
115 			return 1;
116 		}
117 		if(c<0) {
118 			prev = &p->left;
119 			p = p->left;
120 		} else	{
121 			closen = n;
122 			close = p; /* p->dname is smaller than dname */
123 			prev = &p->right;
124 			p = p->right;
125 		}
126 	}
127 	*insertpt = prev;
128 	*matchlabels = closen;
129 	*match = close;
130 	return 0;
131 }
132 
133 /**
134  * Lookup a domain name in compression tree.
135  * @param tree: root of tree (not the node with '.').
136  * @param dname: pointer to uncompressed dname.
137  * @param labs: number of labels in domain name.
138  * @param insertpt: insert location for dname, if not found.
139  * @return: 0 if not found or compress treenode with best compression.
140  */
141 static struct compress_tree_node*
142 compress_tree_lookup(struct compress_tree_node** tree, uint8_t* dname,
143 	int labs, struct compress_tree_node*** insertpt)
144 {
145 	struct compress_tree_node* p;
146 	int m;
147 	if(labs <= 1)
148 		return 0; /* do not compress root node */
149 	if(compress_tree_search(tree, dname, labs, &p, &m, insertpt)) {
150 		/* exact match */
151 		return p;
152 	}
153 	/* return some ancestor of p that compresses well. */
154 	if(m>1) {
155 		/* www.example.com. (labs=4) matched foo.example.com.(labs=4)
156 		 * then matchcount = 3. need to go up. */
157 		while(p && p->labs > m)
158 			p = p->parent;
159 		return p;
160 	}
161 	return 0;
162 }
163 
164 /**
165  * Create node for domain name compression tree.
166  * @param dname: pointer to uncompressed dname (stored in tree).
167  * @param labs: number of labels in dname.
168  * @param offset: offset into packet for dname.
169  * @param region: how to allocate memory for new node.
170  * @return new node or 0 on malloc failure.
171  */
172 static struct compress_tree_node*
173 compress_tree_newnode(uint8_t* dname, int labs, size_t offset,
174 	struct regional* region)
175 {
176 	struct compress_tree_node* n = (struct compress_tree_node*)
177 		regional_alloc(region, sizeof(struct compress_tree_node));
178 	if(!n) return 0;
179 	n->left = 0;
180 	n->right = 0;
181 	n->parent = 0;
182 	n->dname = dname;
183 	n->labs = labs;
184 	n->offset = offset;
185 	return n;
186 }
187 
188 /**
189  * Store domain name and ancestors into compression tree.
190  * @param dname: pointer to uncompressed dname (stored in tree).
191  * @param labs: number of labels in dname.
192  * @param offset: offset into packet for dname.
193  * @param region: how to allocate memory for new node.
194  * @param closest: match from previous lookup, used to compress dname.
195  *	may be NULL if no previous match.
196  *	if the tree has an ancestor of dname already, this must be it.
197  * @param insertpt: where to insert the dname in tree.
198  * @return: 0 on memory error.
199  */
200 static int
201 compress_tree_store(uint8_t* dname, int labs, size_t offset,
202 	struct regional* region, struct compress_tree_node* closest,
203 	struct compress_tree_node** insertpt)
204 {
205 	uint8_t lablen;
206 	struct compress_tree_node* newnode;
207 	struct compress_tree_node* prevnode = NULL;
208 	int uplabs = labs-1; /* does not store root in tree */
209 	if(closest) uplabs = labs - closest->labs;
210 	log_assert(uplabs >= 0);
211 	/* algorithms builds up a vine of dname-labels to hang into tree */
212 	while(uplabs--) {
213 		if(offset > PTR_MAX_OFFSET) {
214 			/* insertion failed, drop vine */
215 			return 1; /* compression pointer no longer useful */
216 		}
217 		if(!(newnode = compress_tree_newnode(dname, labs, offset,
218 			region))) {
219 			/* insertion failed, drop vine */
220 			return 0;
221 		}
222 
223 		if(prevnode) {
224 			/* chain nodes together, last one has one label more,
225 			 * so is larger than newnode, thus goes right. */
226 			newnode->right = prevnode;
227 			prevnode->parent = newnode;
228 		}
229 
230 		/* next label */
231 		lablen = *dname++;
232 		dname += lablen;
233 		offset += lablen+1;
234 		prevnode = newnode;
235 		labs--;
236 	}
237 	/* if we have a vine, hang the vine into the tree */
238 	if(prevnode) {
239 		*insertpt = prevnode;
240 		prevnode->parent = closest;
241 	}
242 	return 1;
243 }
244 
245 /** compress a domain name */
246 static int
247 write_compressed_dname(sldns_buffer* pkt, uint8_t* dname, int labs,
248 	struct compress_tree_node* p)
249 {
250 	/* compress it */
251 	int labcopy = labs - p->labs;
252 	uint8_t lablen;
253 	uint16_t ptr;
254 
255 	if(labs == 1) {
256 		/* write root label */
257 		if(sldns_buffer_remaining(pkt) < 1)
258 			return 0;
259 		sldns_buffer_write_u8(pkt, 0);
260 		return 1;
261 	}
262 
263 	/* copy the first couple of labels */
264 	while(labcopy--) {
265 		lablen = *dname++;
266 		if(sldns_buffer_remaining(pkt) < (size_t)lablen+1)
267 			return 0;
268 		sldns_buffer_write_u8(pkt, lablen);
269 		sldns_buffer_write(pkt, dname, lablen);
270 		dname += lablen;
271 	}
272 	/* insert compression ptr */
273 	if(sldns_buffer_remaining(pkt) < 2)
274 		return 0;
275 	ptr = PTR_CREATE(p->offset);
276 	sldns_buffer_write_u16(pkt, ptr);
277 	return 1;
278 }
279 
280 /** compress owner name of RR, return RETVAL_OUTMEM RETVAL_TRUNC */
281 static int
282 compress_owner(struct ub_packed_rrset_key* key, sldns_buffer* pkt,
283 	struct regional* region, struct compress_tree_node** tree,
284 	size_t owner_pos, uint16_t* owner_ptr, int owner_labs)
285 {
286 	struct compress_tree_node* p;
287 	struct compress_tree_node** insertpt = NULL;
288 	if(!*owner_ptr) {
289 		/* compress first time dname */
290 		if((p = compress_tree_lookup(tree, key->rk.dname,
291 			owner_labs, &insertpt))) {
292 			if(p->labs == owner_labs)
293 				/* avoid ptr chains, since some software is
294 				 * not capable of decoding ptr after a ptr. */
295 				*owner_ptr = htons(PTR_CREATE(p->offset));
296 			if(!write_compressed_dname(pkt, key->rk.dname,
297 				owner_labs, p))
298 				return RETVAL_TRUNC;
299 			/* check if typeclass+4 ttl + rdatalen is available */
300 			if(sldns_buffer_remaining(pkt) < 4+4+2)
301 				return RETVAL_TRUNC;
302 		} else {
303 			/* no compress */
304 			if(sldns_buffer_remaining(pkt) < key->rk.dname_len+4+4+2)
305 				return RETVAL_TRUNC;
306 			sldns_buffer_write(pkt, key->rk.dname,
307 				key->rk.dname_len);
308 			if(owner_pos <= PTR_MAX_OFFSET)
309 				*owner_ptr = htons(PTR_CREATE(owner_pos));
310 		}
311 		if(!compress_tree_store(key->rk.dname, owner_labs,
312 			owner_pos, region, p, insertpt))
313 			return RETVAL_OUTMEM;
314 	} else {
315 		/* always compress 2nd-further RRs in RRset */
316 		if(owner_labs == 1) {
317 			if(sldns_buffer_remaining(pkt) < 1+4+4+2)
318 				return RETVAL_TRUNC;
319 			sldns_buffer_write_u8(pkt, 0);
320 		} else {
321 			if(sldns_buffer_remaining(pkt) < 2+4+4+2)
322 				return RETVAL_TRUNC;
323 			sldns_buffer_write(pkt, owner_ptr, 2);
324 		}
325 	}
326 	return RETVAL_OK;
327 }
328 
329 /** compress any domain name to the packet, return RETVAL_* */
330 static int
331 compress_any_dname(uint8_t* dname, sldns_buffer* pkt, int labs,
332 	struct regional* region, struct compress_tree_node** tree)
333 {
334 	struct compress_tree_node* p;
335 	struct compress_tree_node** insertpt = NULL;
336 	size_t pos = sldns_buffer_position(pkt);
337 	if((p = compress_tree_lookup(tree, dname, labs, &insertpt))) {
338 		if(!write_compressed_dname(pkt, dname, labs, p))
339 			return RETVAL_TRUNC;
340 	} else {
341 		if(!dname_buffer_write(pkt, dname))
342 			return RETVAL_TRUNC;
343 	}
344 	if(!compress_tree_store(dname, labs, pos, region, p, insertpt))
345 		return RETVAL_OUTMEM;
346 	return RETVAL_OK;
347 }
348 
349 /** return true if type needs domain name compression in rdata */
350 static const sldns_rr_descriptor*
351 type_rdata_compressable(struct ub_packed_rrset_key* key)
352 {
353 	uint16_t t = ntohs(key->rk.type);
354 	if(sldns_rr_descript(t) &&
355 		sldns_rr_descript(t)->_compress == LDNS_RR_COMPRESS)
356 		return sldns_rr_descript(t);
357 	return 0;
358 }
359 
360 /** compress domain names in rdata, return RETVAL_* */
361 static int
362 compress_rdata(sldns_buffer* pkt, uint8_t* rdata, size_t todolen,
363 	struct regional* region, struct compress_tree_node** tree,
364 	const sldns_rr_descriptor* desc)
365 {
366 	int labs, r, rdf = 0;
367 	size_t dname_len, len, pos = sldns_buffer_position(pkt);
368 	uint8_t count = desc->_dname_count;
369 
370 	sldns_buffer_skip(pkt, 2); /* rdata len fill in later */
371 	/* space for rdatalen checked for already */
372 	rdata += 2;
373 	todolen -= 2;
374 	while(todolen > 0 && count) {
375 		switch(desc->_wireformat[rdf]) {
376 		case LDNS_RDF_TYPE_DNAME:
377 			labs = dname_count_size_labels(rdata, &dname_len);
378 			if((r=compress_any_dname(rdata, pkt, labs, region,
379 				tree)) != RETVAL_OK)
380 				return r;
381 			rdata += dname_len;
382 			todolen -= dname_len;
383 			count--;
384 			len = 0;
385 			break;
386 		case LDNS_RDF_TYPE_STR:
387 			len = *rdata + 1;
388 			break;
389 		default:
390 			len = get_rdf_size(desc->_wireformat[rdf]);
391 		}
392 		if(len) {
393 			/* copy over */
394 			if(sldns_buffer_remaining(pkt) < len)
395 				return RETVAL_TRUNC;
396 			sldns_buffer_write(pkt, rdata, len);
397 			todolen -= len;
398 			rdata += len;
399 		}
400 		rdf++;
401 	}
402 	/* copy remainder */
403 	if(todolen > 0) {
404 		if(sldns_buffer_remaining(pkt) < todolen)
405 			return RETVAL_TRUNC;
406 		sldns_buffer_write(pkt, rdata, todolen);
407 	}
408 
409 	/* set rdata len */
410 	sldns_buffer_write_u16_at(pkt, pos, sldns_buffer_position(pkt)-pos-2);
411 	return RETVAL_OK;
412 }
413 
414 /** Returns true if RR type should be included */
415 static int
416 rrset_belongs_in_reply(sldns_pkt_section s, uint16_t rrtype, uint16_t qtype,
417 	int dnssec)
418 {
419 	if(dnssec)
420 		return 1;
421 	/* skip non DNSSEC types, except if directly queried for */
422 	if(s == LDNS_SECTION_ANSWER) {
423 		if(qtype == LDNS_RR_TYPE_ANY || qtype == rrtype)
424 			return 1;
425 	}
426 	/* check DNSSEC-ness */
427 	switch(rrtype) {
428 		case LDNS_RR_TYPE_SIG:
429 		case LDNS_RR_TYPE_KEY:
430 		case LDNS_RR_TYPE_NXT:
431 		case LDNS_RR_TYPE_DS:
432 		case LDNS_RR_TYPE_RRSIG:
433 		case LDNS_RR_TYPE_NSEC:
434 		case LDNS_RR_TYPE_DNSKEY:
435 		case LDNS_RR_TYPE_NSEC3:
436 		case LDNS_RR_TYPE_NSEC3PARAMS:
437 			return 0;
438 	}
439 	return 1;
440 }
441 
442 /** store rrset in buffer in wireformat, return RETVAL_* */
443 static int
444 packed_rrset_encode(struct ub_packed_rrset_key* key, sldns_buffer* pkt,
445 	uint16_t* num_rrs, time_t timenow, struct regional* region,
446 	int do_data, int do_sig, struct compress_tree_node** tree,
447 	sldns_pkt_section s, uint16_t qtype, int dnssec, size_t rr_offset)
448 {
449 	size_t i, j, owner_pos;
450 	int r, owner_labs;
451 	uint16_t owner_ptr = 0;
452 	struct packed_rrset_data* data = (struct packed_rrset_data*)
453 		key->entry.data;
454 
455 	/* does this RR type belong in the answer? */
456 	if(!rrset_belongs_in_reply(s, ntohs(key->rk.type), qtype, dnssec))
457 		return RETVAL_OK;
458 
459 	owner_labs = dname_count_labels(key->rk.dname);
460 	owner_pos = sldns_buffer_position(pkt);
461 
462 	/* For an rrset with a fixed TTL, use the rrset's TTL as given */
463 	if((key->rk.flags & PACKED_RRSET_FIXEDTTL) != 0)
464 		timenow = 0;
465 
466 	if(do_data) {
467 		const sldns_rr_descriptor* c = type_rdata_compressable(key);
468 		for(i=0; i<data->count; i++) {
469 			/* rrset roundrobin */
470 			j = (i + rr_offset) % data->count;
471 			if((r=compress_owner(key, pkt, region, tree,
472 				owner_pos, &owner_ptr, owner_labs))
473 				!= RETVAL_OK)
474 				return r;
475 			sldns_buffer_write(pkt, &key->rk.type, 2);
476 			sldns_buffer_write(pkt, &key->rk.rrset_class, 2);
477 			if(data->rr_ttl[j] < timenow)
478 				sldns_buffer_write_u32(pkt, 0);
479 			else 	sldns_buffer_write_u32(pkt,
480 					data->rr_ttl[j]-timenow);
481 			if(c) {
482 				if((r=compress_rdata(pkt, data->rr_data[j],
483 					data->rr_len[j], region, tree, c))
484 					!= RETVAL_OK)
485 					return r;
486 			} else {
487 				if(sldns_buffer_remaining(pkt) < data->rr_len[j])
488 					return RETVAL_TRUNC;
489 				sldns_buffer_write(pkt, data->rr_data[j],
490 					data->rr_len[j]);
491 			}
492 		}
493 	}
494 	/* insert rrsigs */
495 	if(do_sig && dnssec) {
496 		size_t total = data->count+data->rrsig_count;
497 		for(i=data->count; i<total; i++) {
498 			if(owner_ptr && owner_labs != 1) {
499 				if(sldns_buffer_remaining(pkt) <
500 					2+4+4+data->rr_len[i])
501 					return RETVAL_TRUNC;
502 				sldns_buffer_write(pkt, &owner_ptr, 2);
503 			} else {
504 				if((r=compress_any_dname(key->rk.dname,
505 					pkt, owner_labs, region, tree))
506 					!= RETVAL_OK)
507 					return r;
508 				if(sldns_buffer_remaining(pkt) <
509 					4+4+data->rr_len[i])
510 					return RETVAL_TRUNC;
511 			}
512 			sldns_buffer_write_u16(pkt, LDNS_RR_TYPE_RRSIG);
513 			sldns_buffer_write(pkt, &key->rk.rrset_class, 2);
514 			if(data->rr_ttl[i] < timenow)
515 				sldns_buffer_write_u32(pkt, 0);
516 			else 	sldns_buffer_write_u32(pkt,
517 					data->rr_ttl[i]-timenow);
518 			/* rrsig rdata cannot be compressed, perform 100+ byte
519 			 * memcopy. */
520 			sldns_buffer_write(pkt, data->rr_data[i],
521 				data->rr_len[i]);
522 		}
523 	}
524 	/* change rrnum only after we are sure it fits */
525 	if(do_data)
526 		*num_rrs += data->count;
527 	if(do_sig && dnssec)
528 		*num_rrs += data->rrsig_count;
529 
530 	return RETVAL_OK;
531 }
532 
533 /** store msg section in wireformat buffer, return RETVAL_* */
534 static int
535 insert_section(struct reply_info* rep, size_t num_rrsets, uint16_t* num_rrs,
536 	sldns_buffer* pkt, size_t rrsets_before, time_t timenow,
537 	struct regional* region, struct compress_tree_node** tree,
538 	sldns_pkt_section s, uint16_t qtype, int dnssec, size_t rr_offset)
539 {
540 	int r;
541 	size_t i, setstart;
542 	/* we now allow this function to be called multiple times for the
543 	 * same section, incrementally updating num_rrs.  The caller is
544 	 * responsible for initializing it (which is the case in the current
545 	 * implementation). */
546 
547 	if(s != LDNS_SECTION_ADDITIONAL) {
548 		if(s == LDNS_SECTION_ANSWER && qtype == LDNS_RR_TYPE_ANY)
549 			dnssec = 1; /* include all types in ANY answer */
550 	  	for(i=0; i<num_rrsets; i++) {
551 			setstart = sldns_buffer_position(pkt);
552 			if((r=packed_rrset_encode(rep->rrsets[rrsets_before+i],
553 				pkt, num_rrs, timenow, region, 1, 1, tree,
554 				s, qtype, dnssec, rr_offset))
555 				!= RETVAL_OK) {
556 				/* Bad, but if due to size must set TC bit */
557 				/* trim off the rrset neatly. */
558 				sldns_buffer_set_position(pkt, setstart);
559 				return r;
560 			}
561 		}
562 	} else {
563 	  	for(i=0; i<num_rrsets; i++) {
564 			setstart = sldns_buffer_position(pkt);
565 			if((r=packed_rrset_encode(rep->rrsets[rrsets_before+i],
566 				pkt, num_rrs, timenow, region, 1, 0, tree,
567 				s, qtype, dnssec, rr_offset))
568 				!= RETVAL_OK) {
569 				sldns_buffer_set_position(pkt, setstart);
570 				return r;
571 			}
572 		}
573 		if(dnssec)
574 	  	  for(i=0; i<num_rrsets; i++) {
575 			setstart = sldns_buffer_position(pkt);
576 			if((r=packed_rrset_encode(rep->rrsets[rrsets_before+i],
577 				pkt, num_rrs, timenow, region, 0, 1, tree,
578 				s, qtype, dnssec, rr_offset))
579 				!= RETVAL_OK) {
580 				sldns_buffer_set_position(pkt, setstart);
581 				return r;
582 			}
583 		  }
584 	}
585 	return RETVAL_OK;
586 }
587 
588 /** store query section in wireformat buffer, return RETVAL */
589 static int
590 insert_query(struct query_info* qinfo, struct compress_tree_node** tree,
591 	sldns_buffer* buffer, struct regional* region)
592 {
593 	uint8_t* qname = qinfo->local_alias ?
594 		qinfo->local_alias->rrset->rk.dname : qinfo->qname;
595 	size_t qname_len = qinfo->local_alias ?
596 		qinfo->local_alias->rrset->rk.dname_len : qinfo->qname_len;
597 	if(sldns_buffer_remaining(buffer) <
598 		qinfo->qname_len+sizeof(uint16_t)*2)
599 		return RETVAL_TRUNC; /* buffer too small */
600 	/* the query is the first name inserted into the tree */
601 	if(!compress_tree_store(qname, dname_count_labels(qname),
602 		sldns_buffer_position(buffer), region, NULL, tree))
603 		return RETVAL_OUTMEM;
604 	if(sldns_buffer_current(buffer) == qname)
605 		sldns_buffer_skip(buffer, (ssize_t)qname_len);
606 	else	sldns_buffer_write(buffer, qname, qname_len);
607 	sldns_buffer_write_u16(buffer, qinfo->qtype);
608 	sldns_buffer_write_u16(buffer, qinfo->qclass);
609 	return RETVAL_OK;
610 }
611 
612 static int
613 positive_answer(struct reply_info* rep, uint16_t qtype) {
614 	size_t i;
615 	if (FLAGS_GET_RCODE(rep->flags) != LDNS_RCODE_NOERROR)
616 		return 0;
617 
618 	for(i=0;i<rep->an_numrrsets; i++) {
619 		if(ntohs(rep->rrsets[i]->rk.type) == qtype) {
620 			/* in case it is a wildcard with DNSSEC, there will
621 			 * be NSEC/NSEC3 records in the authority section
622 			 * that we cannot remove */
623 			for(i=rep->an_numrrsets; i<rep->an_numrrsets+
624 				rep->ns_numrrsets; i++) {
625 				if(ntohs(rep->rrsets[i]->rk.type) ==
626 					LDNS_RR_TYPE_NSEC ||
627 				   ntohs(rep->rrsets[i]->rk.type) ==
628 				   	LDNS_RR_TYPE_NSEC3)
629 					return 0;
630 			}
631 			return 1;
632 		}
633 	}
634 	return 0;
635 }
636 
637 int
638 reply_info_encode(struct query_info* qinfo, struct reply_info* rep,
639 	uint16_t id, uint16_t flags, sldns_buffer* buffer, time_t timenow,
640 	struct regional* region, uint16_t udpsize, int dnssec)
641 {
642 	uint16_t ancount=0, nscount=0, arcount=0;
643 	struct compress_tree_node* tree = 0;
644 	int r;
645 	size_t rr_offset;
646 
647 	sldns_buffer_clear(buffer);
648 	if(udpsize < sldns_buffer_limit(buffer))
649 		sldns_buffer_set_limit(buffer, udpsize);
650 	else if(sldns_buffer_limit(buffer) < udpsize)
651 		udpsize = sldns_buffer_limit(buffer);
652 	if(sldns_buffer_remaining(buffer) < LDNS_HEADER_SIZE)
653 		return 0;
654 
655 	sldns_buffer_write(buffer, &id, sizeof(uint16_t));
656 	sldns_buffer_write_u16(buffer, flags);
657 	sldns_buffer_write_u16(buffer, rep->qdcount);
658 	/* set an, ns, ar counts to zero in case of small packets */
659 	sldns_buffer_write(buffer, "\000\000\000\000\000\000", 6);
660 
661 	/* insert query section */
662 	if(rep->qdcount) {
663 		if((r=insert_query(qinfo, &tree, buffer, region)) !=
664 			RETVAL_OK) {
665 			if(r == RETVAL_TRUNC) {
666 				/* create truncated message */
667 				sldns_buffer_write_u16_at(buffer, 4, 0);
668 				LDNS_TC_SET(sldns_buffer_begin(buffer));
669 				sldns_buffer_flip(buffer);
670 				return 1;
671 			}
672 			return 0;
673 		}
674 	}
675 	/* roundrobin offset. using query id for random number.  With ntohs
676 	 * for different roundrobins for sequential id client senders. */
677 	rr_offset = RRSET_ROUNDROBIN?ntohs(id):0;
678 
679 	/* "prepend" any local alias records in the answer section if this
680 	 * response is supposed to be authoritative.  Currently it should
681 	 * be a single CNAME record (sanity-checked in worker_handle_request())
682 	 * but it can be extended if and when we support more variations of
683 	 * aliases. */
684 	if(qinfo->local_alias && (flags & BIT_AA)) {
685 		struct reply_info arep;
686 		time_t timezero = 0; /* to use the 'authoritative' TTL */
687 		memset(&arep, 0, sizeof(arep));
688 		arep.flags = rep->flags;
689 		arep.an_numrrsets = 1;
690 		arep.rrset_count = 1;
691 		arep.rrsets = &qinfo->local_alias->rrset;
692 		if((r=insert_section(&arep, 1, &ancount, buffer, 0,
693 			timezero, region, &tree, LDNS_SECTION_ANSWER,
694 			qinfo->qtype, dnssec, rr_offset)) != RETVAL_OK) {
695 			if(r == RETVAL_TRUNC) {
696 				/* create truncated message */
697 				sldns_buffer_write_u16_at(buffer, 6, ancount);
698 				LDNS_TC_SET(sldns_buffer_begin(buffer));
699 				sldns_buffer_flip(buffer);
700 				return 1;
701 			}
702 			return 0;
703 		}
704 	}
705 
706 	/* insert answer section */
707 	if((r=insert_section(rep, rep->an_numrrsets, &ancount, buffer,
708 		0, timenow, region, &tree, LDNS_SECTION_ANSWER, qinfo->qtype,
709 		dnssec, rr_offset)) != RETVAL_OK) {
710 		if(r == RETVAL_TRUNC) {
711 			/* create truncated message */
712 			sldns_buffer_write_u16_at(buffer, 6, ancount);
713 			LDNS_TC_SET(sldns_buffer_begin(buffer));
714 			sldns_buffer_flip(buffer);
715 			return 1;
716 		}
717 		return 0;
718 	}
719 	sldns_buffer_write_u16_at(buffer, 6, ancount);
720 
721 	/* if response is positive answer, auth/add sections are not required */
722 	if( ! (MINIMAL_RESPONSES && positive_answer(rep, qinfo->qtype)) ) {
723 		/* insert auth section */
724 		if((r=insert_section(rep, rep->ns_numrrsets, &nscount, buffer,
725 			rep->an_numrrsets, timenow, region, &tree,
726 			LDNS_SECTION_AUTHORITY, qinfo->qtype,
727 			dnssec, rr_offset)) != RETVAL_OK) {
728 			if(r == RETVAL_TRUNC) {
729 				/* create truncated message */
730 				sldns_buffer_write_u16_at(buffer, 8, nscount);
731 				LDNS_TC_SET(sldns_buffer_begin(buffer));
732 				sldns_buffer_flip(buffer);
733 				return 1;
734 			}
735 			return 0;
736 		}
737 		sldns_buffer_write_u16_at(buffer, 8, nscount);
738 
739 		/* insert add section */
740 		if((r=insert_section(rep, rep->ar_numrrsets, &arcount, buffer,
741 			rep->an_numrrsets + rep->ns_numrrsets, timenow, region,
742 			&tree, LDNS_SECTION_ADDITIONAL, qinfo->qtype,
743 			dnssec, rr_offset)) != RETVAL_OK) {
744 			if(r == RETVAL_TRUNC) {
745 				/* no need to set TC bit, this is the additional */
746 				sldns_buffer_write_u16_at(buffer, 10, arcount);
747 				sldns_buffer_flip(buffer);
748 				return 1;
749 			}
750 			return 0;
751 		}
752 		sldns_buffer_write_u16_at(buffer, 10, arcount);
753 	}
754 	sldns_buffer_flip(buffer);
755 	return 1;
756 }
757 
758 uint16_t
759 calc_edns_field_size(struct edns_data* edns)
760 {
761 	size_t rdatalen = 0;
762 	struct edns_option* opt;
763 	if(!edns || !edns->edns_present)
764 		return 0;
765 	for(opt = edns->opt_list; opt; opt = opt->next) {
766 		rdatalen += 4 + opt->opt_len;
767 	}
768 	/* domain root '.' + type + class + ttl + rdatalen */
769 	return 1 + 2 + 2 + 4 + 2 + rdatalen;
770 }
771 
772 void
773 attach_edns_record(sldns_buffer* pkt, struct edns_data* edns)
774 {
775 	size_t len;
776 	size_t rdatapos;
777 	struct edns_option* opt;
778 	if(!edns || !edns->edns_present)
779 		return;
780 	/* inc additional count */
781 	sldns_buffer_write_u16_at(pkt, 10,
782 		sldns_buffer_read_u16_at(pkt, 10) + 1);
783 	len = sldns_buffer_limit(pkt);
784 	sldns_buffer_clear(pkt);
785 	sldns_buffer_set_position(pkt, len);
786 	/* write EDNS record */
787 	sldns_buffer_write_u8(pkt, 0); /* '.' label */
788 	sldns_buffer_write_u16(pkt, LDNS_RR_TYPE_OPT); /* type */
789 	sldns_buffer_write_u16(pkt, edns->udp_size); /* class */
790 	sldns_buffer_write_u8(pkt, edns->ext_rcode); /* ttl */
791 	sldns_buffer_write_u8(pkt, edns->edns_version);
792 	sldns_buffer_write_u16(pkt, edns->bits);
793 	rdatapos = sldns_buffer_position(pkt);
794 	sldns_buffer_write_u16(pkt, 0); /* rdatalen */
795 	/* write rdata */
796 	for(opt=edns->opt_list; opt; opt=opt->next) {
797 		sldns_buffer_write_u16(pkt, opt->opt_code);
798 		sldns_buffer_write_u16(pkt, opt->opt_len);
799 		if(opt->opt_len != 0)
800 			sldns_buffer_write(pkt, opt->opt_data, opt->opt_len);
801 	}
802 	if(edns->opt_list)
803 		sldns_buffer_write_u16_at(pkt, rdatapos,
804 			sldns_buffer_position(pkt)-rdatapos-2);
805 	sldns_buffer_flip(pkt);
806 }
807 
808 int
809 reply_info_answer_encode(struct query_info* qinf, struct reply_info* rep,
810 	uint16_t id, uint16_t qflags, sldns_buffer* pkt, time_t timenow,
811 	int cached, struct regional* region, uint16_t udpsize,
812 	struct edns_data* edns, int dnssec, int secure)
813 {
814 	uint16_t flags;
815 	unsigned int attach_edns = 0;
816 
817 	if(!cached || rep->authoritative) {
818 		/* original flags, copy RD and CD bits from query. */
819 		flags = rep->flags | (qflags & (BIT_RD|BIT_CD));
820 	} else {
821 		/* remove AA bit, copy RD and CD bits from query. */
822 		flags = (rep->flags & ~BIT_AA) | (qflags & (BIT_RD|BIT_CD));
823 	}
824 	if(secure && (dnssec || (qflags&BIT_AD)))
825 		flags |= BIT_AD;
826 	/* restore AA bit if we have a local alias and the response can be
827 	 * authoritative.  Also clear AD bit if set as the local data is the
828 	 * primary answer. */
829 	if(qinf->local_alias &&
830 		(FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NOERROR ||
831 		FLAGS_GET_RCODE(rep->flags) == LDNS_RCODE_NXDOMAIN)) {
832 		flags |= BIT_AA;
833 		flags &= ~BIT_AD;
834 	}
835 	log_assert(flags & BIT_QR); /* QR bit must be on in our replies */
836 	if(udpsize < LDNS_HEADER_SIZE)
837 		return 0;
838 	if(sldns_buffer_capacity(pkt) < udpsize)
839 		udpsize = sldns_buffer_capacity(pkt);
840 	if(udpsize < LDNS_HEADER_SIZE + calc_edns_field_size(edns)) {
841 		/* packet too small to contain edns, omit it. */
842 		attach_edns = 0;
843 	} else {
844 		/* reserve space for edns record */
845 		attach_edns = (unsigned int)calc_edns_field_size(edns);
846 		udpsize -= attach_edns;
847 	}
848 
849 	if(!reply_info_encode(qinf, rep, id, flags, pkt, timenow, region,
850 		udpsize, dnssec)) {
851 		log_err("reply encode: out of memory");
852 		return 0;
853 	}
854 	if(attach_edns && sldns_buffer_capacity(pkt) >=
855 		sldns_buffer_limit(pkt)+attach_edns)
856 		attach_edns_record(pkt, edns);
857 	return 1;
858 }
859 
860 void
861 qinfo_query_encode(sldns_buffer* pkt, struct query_info* qinfo)
862 {
863 	uint16_t flags = 0; /* QUERY, NOERROR */
864 	const uint8_t* qname = qinfo->local_alias ?
865 		qinfo->local_alias->rrset->rk.dname : qinfo->qname;
866 	size_t qname_len = qinfo->local_alias ?
867 		qinfo->local_alias->rrset->rk.dname_len : qinfo->qname_len;
868 	sldns_buffer_clear(pkt);
869 	log_assert(sldns_buffer_remaining(pkt) >= 12+255+4/*max query*/);
870 	sldns_buffer_skip(pkt, 2); /* id done later */
871 	sldns_buffer_write_u16(pkt, flags);
872 	sldns_buffer_write_u16(pkt, 1); /* query count */
873 	sldns_buffer_write(pkt, "\000\000\000\000\000\000", 6); /* counts */
874 	sldns_buffer_write(pkt, qname, qname_len);
875 	sldns_buffer_write_u16(pkt, qinfo->qtype);
876 	sldns_buffer_write_u16(pkt, qinfo->qclass);
877 	sldns_buffer_flip(pkt);
878 }
879 
880 void
881 error_encode(sldns_buffer* buf, int r, struct query_info* qinfo,
882 	uint16_t qid, uint16_t qflags, struct edns_data* edns)
883 {
884 	uint16_t flags;
885 
886 	sldns_buffer_clear(buf);
887 	sldns_buffer_write(buf, &qid, sizeof(uint16_t));
888 	flags = (uint16_t)(BIT_QR | BIT_RA | r); /* QR and retcode*/
889 	flags |= (qflags & (BIT_RD|BIT_CD)); /* copy RD and CD bit */
890 	sldns_buffer_write_u16(buf, flags);
891 	if(qinfo) flags = 1;
892 	else	flags = 0;
893 	sldns_buffer_write_u16(buf, flags);
894 	flags = 0;
895 	sldns_buffer_write(buf, &flags, sizeof(uint16_t));
896 	sldns_buffer_write(buf, &flags, sizeof(uint16_t));
897 	sldns_buffer_write(buf, &flags, sizeof(uint16_t));
898 	if(qinfo) {
899 		const uint8_t* qname = qinfo->local_alias ?
900 			qinfo->local_alias->rrset->rk.dname : qinfo->qname;
901 		size_t qname_len = qinfo->local_alias ?
902 			qinfo->local_alias->rrset->rk.dname_len :
903 			qinfo->qname_len;
904 		if(sldns_buffer_current(buf) == qname)
905 			sldns_buffer_skip(buf, (ssize_t)qname_len);
906 		else	sldns_buffer_write(buf, qname, qname_len);
907 		sldns_buffer_write_u16(buf, qinfo->qtype);
908 		sldns_buffer_write_u16(buf, qinfo->qclass);
909 	}
910 	sldns_buffer_flip(buf);
911 	if(edns) {
912 		struct edns_data es = *edns;
913 		es.edns_version = EDNS_ADVERTISED_VERSION;
914 		es.udp_size = EDNS_ADVERTISED_SIZE;
915 		es.ext_rcode = 0;
916 		es.bits &= EDNS_DO;
917 		if(sldns_buffer_limit(buf) + calc_edns_field_size(&es) >
918 			edns->udp_size)
919 			return;
920 		attach_edns_record(buf, &es);
921 	}
922 }
923