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