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