1 /* 2 * util/data/msgparse.c - parse wireformat DNS messages. 3 * 4 * Copyright (c) 2007, NLnet Labs. All rights reserved. 5 * 6 * This software is open source. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * Redistributions of source code must retain the above copyright notice, 13 * this list of conditions and the following disclaimer. 14 * 15 * Redistributions in binary form must reproduce the above copyright notice, 16 * this list of conditions and the following disclaimer in the documentation 17 * and/or other materials provided with the distribution. 18 * 19 * Neither the name of the NLNET LABS nor the names of its contributors may 20 * be used to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 /** 36 * \file 37 * Routines for message parsing a packet buffer to a descriptive structure. 38 */ 39 #include "config.h" 40 #include "util/config_file.h" 41 #include "util/data/msgparse.h" 42 #include "util/data/msgreply.h" 43 #include "util/data/dname.h" 44 #include "util/data/packed_rrset.h" 45 #include "util/netevent.h" 46 #include "util/storage/lookup3.h" 47 #include "util/regional.h" 48 #include "util/rfc_1982.h" 49 #include "util/edns.h" 50 #include "util/net_help.h" 51 #include "sldns/rrdef.h" 52 #include "sldns/sbuffer.h" 53 #include "sldns/parseutil.h" 54 #include "sldns/wire2str.h" 55 56 #define MAX_PARSED_EDNS_OPTIONS 100 57 58 /** smart comparison of (compressed, valid) dnames from packet */ 59 static int 60 smart_compare(sldns_buffer* pkt, uint8_t* dnow, 61 uint8_t* dprfirst, uint8_t* dprlast) 62 { 63 if(LABEL_IS_PTR(*dnow)) { 64 /* ptr points to a previous dname */ 65 uint8_t* p; 66 if((size_t)PTR_OFFSET(dnow[0], dnow[1]) 67 >= sldns_buffer_limit(pkt)) 68 return -1; 69 p = sldns_buffer_at(pkt, PTR_OFFSET(dnow[0], dnow[1])); 70 if( p == dprfirst || p == dprlast ) 71 return 0; 72 /* prev dname is also a ptr, both ptrs are the same. */ 73 if(LABEL_IS_PTR(*dprlast) && 74 dprlast[0] == dnow[0] && dprlast[1] == dnow[1]) 75 return 0; 76 } 77 return dname_pkt_compare(pkt, dnow, dprlast); 78 } 79 80 /** 81 * Allocate new rrset in region, fill with data. 82 */ 83 static struct rrset_parse* 84 new_rrset(struct msg_parse* msg, uint8_t* dname, size_t dnamelen, 85 uint16_t type, uint16_t dclass, hashvalue_type hash, 86 uint32_t rrset_flags, sldns_pkt_section section, 87 struct regional* region) 88 { 89 struct rrset_parse* p = regional_alloc(region, sizeof(*p)); 90 if(!p) return NULL; 91 p->rrset_bucket_next = msg->hashtable[hash & (PARSE_TABLE_SIZE-1)]; 92 msg->hashtable[hash & (PARSE_TABLE_SIZE-1)] = p; 93 p->rrset_all_next = 0; 94 if(msg->rrset_last) 95 msg->rrset_last->rrset_all_next = p; 96 else msg->rrset_first = p; 97 msg->rrset_last = p; 98 p->hash = hash; 99 p->section = section; 100 p->dname = dname; 101 p->dname_len = dnamelen; 102 p->type = type; 103 p->rrset_class = dclass; 104 p->flags = rrset_flags; 105 p->rr_count = 0; 106 p->size = 0; 107 p->rr_first = 0; 108 p->rr_last = 0; 109 p->rrsig_count = 0; 110 p->rrsig_first = 0; 111 p->rrsig_last = 0; 112 return p; 113 } 114 115 /** See if next rrset is nsec at zone apex */ 116 static int 117 nsec_at_apex(sldns_buffer* pkt) 118 { 119 /* we are at ttl position in packet. */ 120 size_t pos = sldns_buffer_position(pkt); 121 uint16_t rdatalen; 122 if(sldns_buffer_remaining(pkt) < 7) /* ttl+len+root */ 123 return 0; /* eek! */ 124 sldns_buffer_skip(pkt, 4); /* ttl */; 125 rdatalen = sldns_buffer_read_u16(pkt); 126 if(sldns_buffer_remaining(pkt) < rdatalen) { 127 sldns_buffer_set_position(pkt, pos); 128 return 0; /* parse error happens later */ 129 } 130 /* must validate the nsec next domain name format */ 131 if(pkt_dname_len(pkt) == 0) { 132 sldns_buffer_set_position(pkt, pos); 133 return 0; /* parse error */ 134 } 135 136 /* see if SOA bit is set. */ 137 if(sldns_buffer_position(pkt) < pos+4+rdatalen) { 138 /* nsec type bitmap contains items */ 139 uint8_t win, blen, bits; 140 /* need: windownum, bitmap len, firstbyte */ 141 if(sldns_buffer_position(pkt)+3 > pos+4+rdatalen) { 142 sldns_buffer_set_position(pkt, pos); 143 return 0; /* malformed nsec */ 144 } 145 win = sldns_buffer_read_u8(pkt); 146 blen = sldns_buffer_read_u8(pkt); 147 bits = sldns_buffer_read_u8(pkt); 148 /* 0window always first window. bitlen >=1 or parse 149 error really. bit 0x2 is SOA. */ 150 if(win == 0 && blen >= 1 && (bits & 0x02)) { 151 sldns_buffer_set_position(pkt, pos); 152 return 1; 153 } 154 } 155 156 sldns_buffer_set_position(pkt, pos); 157 return 0; 158 } 159 160 /** Calculate rrset flags */ 161 static uint32_t 162 pkt_rrset_flags(sldns_buffer* pkt, uint16_t type, sldns_pkt_section sec) 163 { 164 uint32_t f = 0; 165 if(type == LDNS_RR_TYPE_NSEC && nsec_at_apex(pkt)) { 166 f |= PACKED_RRSET_NSEC_AT_APEX; 167 } else if(type == LDNS_RR_TYPE_SOA && sec == LDNS_SECTION_AUTHORITY) { 168 f |= PACKED_RRSET_SOA_NEG; 169 } 170 return f; 171 } 172 173 hashvalue_type 174 pkt_hash_rrset(sldns_buffer* pkt, uint8_t* dname, uint16_t type, 175 uint16_t dclass, uint32_t rrset_flags) 176 { 177 /* note this MUST be identical to rrset_key_hash in packed_rrset.c */ 178 /* this routine handles compressed names */ 179 hashvalue_type h = 0xab; 180 h = dname_pkt_hash(pkt, dname, h); 181 h = hashlittle(&type, sizeof(type), h); /* host order */ 182 h = hashlittle(&dclass, sizeof(dclass), h); /* netw order */ 183 h = hashlittle(&rrset_flags, sizeof(uint32_t), h); 184 return h; 185 } 186 187 /** create partial dname hash for rrset hash */ 188 static hashvalue_type 189 pkt_hash_rrset_first(sldns_buffer* pkt, uint8_t* dname) 190 { 191 /* works together with pkt_hash_rrset_rest */ 192 /* note this MUST be identical to rrset_key_hash in packed_rrset.c */ 193 /* this routine handles compressed names */ 194 hashvalue_type h = 0xab; 195 h = dname_pkt_hash(pkt, dname, h); 196 return h; 197 } 198 199 /** create a rrset hash from a partial dname hash */ 200 static hashvalue_type 201 pkt_hash_rrset_rest(hashvalue_type dname_h, uint16_t type, uint16_t dclass, 202 uint32_t rrset_flags) 203 { 204 /* works together with pkt_hash_rrset_first */ 205 /* note this MUST be identical to rrset_key_hash in packed_rrset.c */ 206 hashvalue_type h; 207 h = hashlittle(&type, sizeof(type), dname_h); /* host order */ 208 h = hashlittle(&dclass, sizeof(dclass), h); /* netw order */ 209 h = hashlittle(&rrset_flags, sizeof(uint32_t), h); 210 return h; 211 } 212 213 /** compare rrset_parse with data */ 214 static int 215 rrset_parse_equals(struct rrset_parse* p, sldns_buffer* pkt, hashvalue_type h, 216 uint32_t rrset_flags, uint8_t* dname, size_t dnamelen, 217 uint16_t type, uint16_t dclass) 218 { 219 if(p->hash == h && p->dname_len == dnamelen && p->type == type && 220 p->rrset_class == dclass && p->flags == rrset_flags && 221 dname_pkt_compare(pkt, dname, p->dname) == 0) 222 return 1; 223 return 0; 224 } 225 226 227 struct rrset_parse* 228 msgparse_hashtable_lookup(struct msg_parse* msg, sldns_buffer* pkt, 229 hashvalue_type h, uint32_t rrset_flags, uint8_t* dname, 230 size_t dnamelen, uint16_t type, uint16_t dclass) 231 { 232 struct rrset_parse* p = msg->hashtable[h & (PARSE_TABLE_SIZE-1)]; 233 while(p) { 234 if(rrset_parse_equals(p, pkt, h, rrset_flags, dname, dnamelen, 235 type, dclass)) 236 return p; 237 p = p->rrset_bucket_next; 238 } 239 return NULL; 240 } 241 242 /** return type networkformat that rrsig in packet covers */ 243 static int 244 pkt_rrsig_covered(sldns_buffer* pkt, uint8_t* here, uint16_t* type) 245 { 246 size_t pos = sldns_buffer_position(pkt); 247 sldns_buffer_set_position(pkt, (size_t)(here-sldns_buffer_begin(pkt))); 248 /* ttl + len + size of small rrsig(rootlabel, no signature) */ 249 if(sldns_buffer_remaining(pkt) < 4+2+19) 250 return 0; 251 sldns_buffer_skip(pkt, 4); /* ttl */ 252 if(sldns_buffer_read_u16(pkt) < 19) /* too short */ { 253 sldns_buffer_set_position(pkt, pos); 254 return 0; 255 } 256 *type = sldns_buffer_read_u16(pkt); 257 sldns_buffer_set_position(pkt, pos); 258 return 1; 259 } 260 261 /** true if covered type equals prevtype */ 262 static int 263 pkt_rrsig_covered_equals(sldns_buffer* pkt, uint8_t* here, uint16_t type) 264 { 265 uint16_t t; 266 if(pkt_rrsig_covered(pkt, here, &t) && t == type) 267 return 1; 268 return 0; 269 } 270 271 void 272 msgparse_bucket_remove(struct msg_parse* msg, struct rrset_parse* rrset) 273 { 274 struct rrset_parse** p; 275 p = &msg->hashtable[ rrset->hash & (PARSE_TABLE_SIZE-1) ]; 276 while(*p) { 277 if(*p == rrset) { 278 *p = rrset->rrset_bucket_next; 279 return; 280 } 281 p = &( (*p)->rrset_bucket_next ); 282 } 283 } 284 285 /** change section of rrset from previous to current section */ 286 static void 287 change_section(struct msg_parse* msg, struct rrset_parse* rrset, 288 sldns_pkt_section section) 289 { 290 struct rrset_parse *p, *prev; 291 /* remove from list */ 292 if(section == rrset->section) 293 return; 294 p = msg->rrset_first; 295 prev = 0; 296 while(p) { 297 if(p == rrset) { 298 if(prev) prev->rrset_all_next = p->rrset_all_next; 299 else msg->rrset_first = p->rrset_all_next; 300 if(msg->rrset_last == rrset) 301 msg->rrset_last = prev; 302 break; 303 } 304 prev = p; 305 p = p->rrset_all_next; 306 } 307 /* remove from count */ 308 switch(rrset->section) { 309 case LDNS_SECTION_ANSWER: msg->an_rrsets--; break; 310 case LDNS_SECTION_AUTHORITY: msg->ns_rrsets--; break; 311 case LDNS_SECTION_ADDITIONAL: msg->ar_rrsets--; break; 312 default: log_assert(0); 313 } 314 /* insert at end of list */ 315 rrset->rrset_all_next = 0; 316 if(msg->rrset_last) 317 msg->rrset_last->rrset_all_next = rrset; 318 else msg->rrset_first = rrset; 319 msg->rrset_last = rrset; 320 /* up count of new section */ 321 switch(section) { 322 case LDNS_SECTION_AUTHORITY: msg->ns_rrsets++; break; 323 case LDNS_SECTION_ADDITIONAL: msg->ar_rrsets++; break; 324 default: log_assert(0); 325 } 326 rrset->section = section; 327 } 328 329 /** see if rrset of type RRSIG contains sig over given type */ 330 static int 331 rrset_has_sigover(sldns_buffer* pkt, struct rrset_parse* rrset, uint16_t type, 332 int* hasother) 333 { 334 int res = 0; 335 struct rr_parse* rr = rrset->rr_first; 336 log_assert( rrset->type == LDNS_RR_TYPE_RRSIG ); 337 while(rr) { 338 if(pkt_rrsig_covered_equals(pkt, rr->ttl_data, type)) 339 res = 1; 340 else *hasother = 1; 341 rr = rr->next; 342 } 343 return res; 344 } 345 346 /** move rrsigs from sigset to dataset */ 347 static int 348 moveover_rrsigs(sldns_buffer* pkt, struct regional* region, 349 struct rrset_parse* sigset, struct rrset_parse* dataset, int duplicate) 350 { 351 struct rr_parse* sig = sigset->rr_first; 352 struct rr_parse* prev = NULL; 353 struct rr_parse* insert; 354 struct rr_parse* nextsig; 355 while(sig) { 356 nextsig = sig->next; 357 if(pkt_rrsig_covered_equals(pkt, sig->ttl_data, 358 dataset->type)) { 359 if(duplicate) { 360 /* new */ 361 insert = (struct rr_parse*)regional_alloc( 362 region, sizeof(struct rr_parse)); 363 if(!insert) return 0; 364 insert->outside_packet = 0; 365 insert->ttl_data = sig->ttl_data; 366 insert->size = sig->size; 367 /* prev not used */ 368 } else { 369 /* remove from sigset */ 370 if(prev) prev->next = sig->next; 371 else sigset->rr_first = sig->next; 372 if(sigset->rr_last == sig) 373 sigset->rr_last = prev; 374 sigset->rr_count--; 375 sigset->size -= sig->size; 376 insert = sig; 377 /* prev not changed */ 378 } 379 /* add to dataset */ 380 dataset->rrsig_count++; 381 insert->next = 0; 382 if(dataset->rrsig_last) 383 dataset->rrsig_last->next = insert; 384 else dataset->rrsig_first = insert; 385 dataset->rrsig_last = insert; 386 dataset->size += insert->size; 387 } else { 388 prev = sig; 389 } 390 sig = nextsig; 391 } 392 return 1; 393 } 394 395 /** change an rrsig rrset for use as data rrset */ 396 static struct rrset_parse* 397 change_rrsig_rrset(struct rrset_parse* sigset, struct msg_parse* msg, 398 sldns_buffer* pkt, uint16_t datatype, uint32_t rrset_flags, 399 int hasother, sldns_pkt_section section, struct regional* region) 400 { 401 struct rrset_parse* dataset = sigset; 402 hashvalue_type hash = pkt_hash_rrset(pkt, sigset->dname, datatype, 403 sigset->rrset_class, rrset_flags); 404 log_assert( sigset->type == LDNS_RR_TYPE_RRSIG ); 405 log_assert( datatype != LDNS_RR_TYPE_RRSIG ); 406 if(hasother) { 407 /* need to make new rrset to hold data type */ 408 dataset = new_rrset(msg, sigset->dname, sigset->dname_len, 409 datatype, sigset->rrset_class, hash, rrset_flags, 410 section, region); 411 if(!dataset) 412 return NULL; 413 switch(section) { 414 case LDNS_SECTION_ANSWER: msg->an_rrsets++; break; 415 case LDNS_SECTION_AUTHORITY: msg->ns_rrsets++; break; 416 case LDNS_SECTION_ADDITIONAL: msg->ar_rrsets++; break; 417 default: log_assert(0); 418 } 419 if(!moveover_rrsigs(pkt, region, sigset, dataset, 420 msg->qtype == LDNS_RR_TYPE_RRSIG || 421 (msg->qtype == LDNS_RR_TYPE_ANY && 422 section != LDNS_SECTION_ANSWER) )) 423 return NULL; 424 return dataset; 425 } 426 /* changeover the type of the rrset to data set */ 427 msgparse_bucket_remove(msg, dataset); 428 /* insert into new hash bucket */ 429 dataset->rrset_bucket_next = msg->hashtable[hash&(PARSE_TABLE_SIZE-1)]; 430 msg->hashtable[hash&(PARSE_TABLE_SIZE-1)] = dataset; 431 dataset->hash = hash; 432 /* use section of data item for result */ 433 change_section(msg, dataset, section); 434 dataset->type = datatype; 435 dataset->flags = rrset_flags; 436 dataset->rrsig_count += dataset->rr_count; 437 dataset->rr_count = 0; 438 /* move sigs to end of siglist */ 439 if(dataset->rrsig_last) 440 dataset->rrsig_last->next = dataset->rr_first; 441 else dataset->rrsig_first = dataset->rr_first; 442 dataset->rrsig_last = dataset->rr_last; 443 dataset->rr_first = 0; 444 dataset->rr_last = 0; 445 return dataset; 446 } 447 448 /** Find rrset. If equal to previous it is fast. hash if not so. 449 * @param msg: the message with hash table. 450 * @param pkt: the packet in wireformat (needed for compression ptrs). 451 * @param dname: pointer to start of dname (compressed) in packet. 452 * @param dnamelen: uncompressed wirefmt length of dname. 453 * @param type: type of current rr. 454 * @param dclass: class of current rr. 455 * @param hash: hash value is returned if the rrset could not be found. 456 * @param rrset_flags: is returned if the rrset could not be found. 457 * @param prev_dname_first: dname of last seen RR. First seen dname. 458 * @param prev_dname_last: dname of last seen RR. Last seen dname. 459 * @param prev_dnamelen: dname len of last seen RR. 460 * @param prev_type: type of last seen RR. 461 * @param prev_dclass: class of last seen RR. 462 * @param rrset_prev: last seen RRset. 463 * @param section: the current section in the packet. 464 * @param region: used to allocate temporary parsing data. 465 * @return 0 on out of memory. 466 */ 467 static int 468 find_rrset(struct msg_parse* msg, sldns_buffer* pkt, uint8_t* dname, 469 size_t dnamelen, uint16_t type, uint16_t dclass, hashvalue_type* hash, 470 uint32_t* rrset_flags, 471 uint8_t** prev_dname_first, uint8_t** prev_dname_last, 472 size_t* prev_dnamelen, uint16_t* prev_type, 473 uint16_t* prev_dclass, struct rrset_parse** rrset_prev, 474 sldns_pkt_section section, struct regional* region) 475 { 476 hashvalue_type dname_h = pkt_hash_rrset_first(pkt, dname); 477 uint16_t covtype; 478 if(*rrset_prev) { 479 /* check if equal to previous item */ 480 if(type == *prev_type && dclass == *prev_dclass && 481 dnamelen == *prev_dnamelen && 482 smart_compare(pkt, dname, *prev_dname_first, 483 *prev_dname_last) == 0 && 484 type != LDNS_RR_TYPE_RRSIG) { 485 /* same as previous */ 486 *prev_dname_last = dname; 487 return 1; 488 } 489 /* check if rrsig over previous item */ 490 if(type == LDNS_RR_TYPE_RRSIG && dclass == *prev_dclass && 491 pkt_rrsig_covered_equals(pkt, sldns_buffer_current(pkt), 492 *prev_type) && 493 smart_compare(pkt, dname, *prev_dname_first, 494 *prev_dname_last) == 0) { 495 /* covers previous */ 496 *prev_dname_last = dname; 497 return 1; 498 } 499 } 500 /* find by hashing and lookup in hashtable */ 501 *rrset_flags = pkt_rrset_flags(pkt, type, section); 502 503 /* if rrsig - try to lookup matching data set first */ 504 if(type == LDNS_RR_TYPE_RRSIG && pkt_rrsig_covered(pkt, 505 sldns_buffer_current(pkt), &covtype)) { 506 *hash = pkt_hash_rrset_rest(dname_h, covtype, dclass, 507 *rrset_flags); 508 *rrset_prev = msgparse_hashtable_lookup(msg, pkt, *hash, 509 *rrset_flags, dname, dnamelen, covtype, dclass); 510 if(!*rrset_prev && covtype == LDNS_RR_TYPE_NSEC) { 511 /* if NSEC try with NSEC apex bit twiddled */ 512 *rrset_flags ^= PACKED_RRSET_NSEC_AT_APEX; 513 *hash = pkt_hash_rrset_rest(dname_h, covtype, dclass, 514 *rrset_flags); 515 *rrset_prev = msgparse_hashtable_lookup(msg, pkt, 516 *hash, *rrset_flags, dname, dnamelen, covtype, 517 dclass); 518 if(!*rrset_prev) /* untwiddle if not found */ 519 *rrset_flags ^= PACKED_RRSET_NSEC_AT_APEX; 520 } 521 if(!*rrset_prev && covtype == LDNS_RR_TYPE_SOA) { 522 /* if SOA try with SOA neg flag twiddled */ 523 *rrset_flags ^= PACKED_RRSET_SOA_NEG; 524 *hash = pkt_hash_rrset_rest(dname_h, covtype, dclass, 525 *rrset_flags); 526 *rrset_prev = msgparse_hashtable_lookup(msg, pkt, 527 *hash, *rrset_flags, dname, dnamelen, covtype, 528 dclass); 529 if(!*rrset_prev) /* untwiddle if not found */ 530 *rrset_flags ^= PACKED_RRSET_SOA_NEG; 531 } 532 if(*rrset_prev) { 533 *prev_dname_first = (*rrset_prev)->dname; 534 *prev_dname_last = dname; 535 *prev_dnamelen = dnamelen; 536 *prev_type = covtype; 537 *prev_dclass = dclass; 538 return 1; 539 } 540 } 541 if(type != LDNS_RR_TYPE_RRSIG) { 542 int hasother = 0; 543 /* find matching rrsig */ 544 *hash = pkt_hash_rrset_rest(dname_h, LDNS_RR_TYPE_RRSIG, 545 dclass, 0); 546 *rrset_prev = msgparse_hashtable_lookup(msg, pkt, *hash, 547 0, dname, dnamelen, LDNS_RR_TYPE_RRSIG, 548 dclass); 549 if(*rrset_prev && rrset_has_sigover(pkt, *rrset_prev, type, 550 &hasother)) { 551 /* yes! */ 552 *prev_dname_first = (*rrset_prev)->dname; 553 *prev_dname_last = dname; 554 *prev_dnamelen = dnamelen; 555 *prev_type = type; 556 *prev_dclass = dclass; 557 *rrset_prev = change_rrsig_rrset(*rrset_prev, msg, 558 pkt, type, *rrset_flags, hasother, section, 559 region); 560 if(!*rrset_prev) return 0; 561 return 1; 562 } 563 } 564 565 *hash = pkt_hash_rrset_rest(dname_h, type, dclass, *rrset_flags); 566 *rrset_prev = msgparse_hashtable_lookup(msg, pkt, *hash, *rrset_flags, 567 dname, dnamelen, type, dclass); 568 if(*rrset_prev) 569 *prev_dname_first = (*rrset_prev)->dname; 570 else *prev_dname_first = dname; 571 *prev_dname_last = dname; 572 *prev_dnamelen = dnamelen; 573 *prev_type = type; 574 *prev_dclass = dclass; 575 return 1; 576 } 577 578 /** 579 * Parse query section. 580 * @param pkt: packet, position at call must be at start of query section. 581 * at end position is after query section. 582 * @param msg: store results here. 583 * @return: 0 if OK, or rcode on error. 584 */ 585 static int 586 parse_query_section(sldns_buffer* pkt, struct msg_parse* msg) 587 { 588 if(msg->qdcount == 0) 589 return 0; 590 if(msg->qdcount > 1) 591 return LDNS_RCODE_FORMERR; 592 log_assert(msg->qdcount == 1); 593 if(sldns_buffer_remaining(pkt) <= 0) 594 return LDNS_RCODE_FORMERR; 595 msg->qname = sldns_buffer_current(pkt); 596 if((msg->qname_len = pkt_dname_len(pkt)) == 0) 597 return LDNS_RCODE_FORMERR; 598 if(sldns_buffer_remaining(pkt) < sizeof(uint16_t)*2) 599 return LDNS_RCODE_FORMERR; 600 msg->qtype = sldns_buffer_read_u16(pkt); 601 msg->qclass = sldns_buffer_read_u16(pkt); 602 return 0; 603 } 604 605 size_t 606 get_rdf_size(sldns_rdf_type rdf) 607 { 608 switch(rdf) { 609 case LDNS_RDF_TYPE_CLASS: 610 case LDNS_RDF_TYPE_ALG: 611 case LDNS_RDF_TYPE_INT8: 612 return 1; 613 break; 614 case LDNS_RDF_TYPE_INT16: 615 case LDNS_RDF_TYPE_TYPE: 616 case LDNS_RDF_TYPE_CERT_ALG: 617 return 2; 618 break; 619 case LDNS_RDF_TYPE_INT32: 620 case LDNS_RDF_TYPE_TIME: 621 case LDNS_RDF_TYPE_A: 622 case LDNS_RDF_TYPE_PERIOD: 623 return 4; 624 break; 625 case LDNS_RDF_TYPE_TSIGTIME: 626 return 6; 627 break; 628 case LDNS_RDF_TYPE_AAAA: 629 return 16; 630 break; 631 default: 632 log_assert(0); /* add type above */ 633 /* only types that appear before a domain * 634 * name are needed. rest is simply copied. */ 635 } 636 return 0; 637 } 638 639 /** calculate the size of one rr */ 640 static int 641 calc_size(sldns_buffer* pkt, uint16_t type, struct rr_parse* rr) 642 { 643 const sldns_rr_descriptor* desc; 644 uint16_t pkt_len; /* length of rr inside the packet */ 645 rr->size = sizeof(uint16_t); /* the rdatalen */ 646 sldns_buffer_skip(pkt, 4); /* skip ttl */ 647 pkt_len = sldns_buffer_read_u16(pkt); 648 if(sldns_buffer_remaining(pkt) < pkt_len) 649 return 0; 650 desc = sldns_rr_descript(type); 651 if(pkt_len > 0 && desc && desc->_dname_count > 0) { 652 int count = (int)desc->_dname_count; 653 int rdf = 0; 654 size_t len; 655 size_t oldpos; 656 /* skip first part. */ 657 while(pkt_len > 0 && count) { 658 switch(desc->_wireformat[rdf]) { 659 case LDNS_RDF_TYPE_DNAME: 660 /* decompress every domain name */ 661 oldpos = sldns_buffer_position(pkt); 662 if((len = pkt_dname_len(pkt)) == 0) 663 return 0; /* malformed dname */ 664 if(sldns_buffer_position(pkt)-oldpos > pkt_len) 665 return 0; /* dname exceeds rdata */ 666 pkt_len -= sldns_buffer_position(pkt)-oldpos; 667 rr->size += len; 668 count--; 669 len = 0; 670 break; 671 case LDNS_RDF_TYPE_STR: 672 if(pkt_len < 1) { 673 /* NOTREACHED, due to 'while(>0)' */ 674 return 0; /* len byte exceeds rdata */ 675 } 676 len = sldns_buffer_current(pkt)[0] + 1; 677 break; 678 default: 679 len = get_rdf_size(desc->_wireformat[rdf]); 680 } 681 if(len) { 682 if(pkt_len < len) 683 return 0; /* exceeds rdata */ 684 pkt_len -= len; 685 sldns_buffer_skip(pkt, (ssize_t)len); 686 rr->size += len; 687 } 688 rdf++; 689 } 690 } 691 /* remaining rdata */ 692 rr->size += pkt_len; 693 sldns_buffer_skip(pkt, (ssize_t)pkt_len); 694 return 1; 695 } 696 697 /** skip rr ttl and rdata */ 698 static int 699 skip_ttl_rdata(sldns_buffer* pkt) 700 { 701 uint16_t rdatalen; 702 if(sldns_buffer_remaining(pkt) < 6) /* ttl + rdatalen */ 703 return 0; 704 sldns_buffer_skip(pkt, 4); /* ttl */ 705 rdatalen = sldns_buffer_read_u16(pkt); 706 if(sldns_buffer_remaining(pkt) < rdatalen) 707 return 0; 708 sldns_buffer_skip(pkt, (ssize_t)rdatalen); 709 return 1; 710 } 711 712 /** see if RRSIG is a duplicate of another */ 713 static int 714 sig_is_double(sldns_buffer* pkt, struct rrset_parse* rrset, uint8_t* ttldata) 715 { 716 uint16_t rlen, siglen; 717 size_t pos = sldns_buffer_position(pkt); 718 struct rr_parse* sig; 719 if(sldns_buffer_remaining(pkt) < 6) 720 return 0; 721 sldns_buffer_skip(pkt, 4); /* ttl */ 722 rlen = sldns_buffer_read_u16(pkt); 723 if(sldns_buffer_remaining(pkt) < rlen) { 724 sldns_buffer_set_position(pkt, pos); 725 return 0; 726 } 727 sldns_buffer_set_position(pkt, pos); 728 729 sig = rrset->rrsig_first; 730 while(sig) { 731 /* check if rdatalen is same */ 732 memmove(&siglen, sig->ttl_data+4, sizeof(siglen)); 733 siglen = ntohs(siglen); 734 /* checks if data in packet is exactly the same, this means 735 * also dname in rdata is the same, but rrsig is not allowed 736 * to have compressed dnames anyway. If it is compressed anyway 737 * it will lead to duplicate rrs for qtype=RRSIG. (or ANY). 738 * 739 * Cannot use sig->size because size of the other one is not 740 * calculated yet. 741 */ 742 if(siglen == rlen) { 743 if(siglen>0 && memcmp(sig->ttl_data+6, ttldata+6, 744 siglen) == 0) { 745 /* same! */ 746 return 1; 747 } 748 } 749 sig = sig->next; 750 } 751 return 0; 752 } 753 754 /** Add rr (from packet here) to rrset, skips rr */ 755 static int 756 add_rr_to_rrset(struct rrset_parse* rrset, sldns_buffer* pkt, 757 struct msg_parse* msg, struct regional* region, 758 sldns_pkt_section section, uint16_t type) 759 { 760 struct rr_parse* rr; 761 /* check section of rrset. */ 762 if(rrset->section != section && type != LDNS_RR_TYPE_RRSIG && 763 rrset->type != LDNS_RR_TYPE_RRSIG) { 764 /* silently drop it - we drop the last part, since 765 * trust in rr data depends on the section it is in. 766 * the less trustworthy part is discarded. 767 * also the last part is more likely to be incomplete. 768 * RFC 2181: must put RRset only once in response. */ 769 /* 770 verbose(VERB_QUERY, "Packet contains rrset data in " 771 "multiple sections, dropped last part."); 772 log_buf(VERB_QUERY, "packet was", pkt); 773 */ 774 /* forwards */ 775 if(!skip_ttl_rdata(pkt)) 776 return LDNS_RCODE_FORMERR; 777 return 0; 778 } 779 780 if( (msg->qtype == LDNS_RR_TYPE_RRSIG || 781 msg->qtype == LDNS_RR_TYPE_ANY) 782 && sig_is_double(pkt, rrset, sldns_buffer_current(pkt))) { 783 if(!skip_ttl_rdata(pkt)) 784 return LDNS_RCODE_FORMERR; 785 return 0; 786 } 787 788 /* create rr */ 789 if(!(rr = (struct rr_parse*)regional_alloc(region, sizeof(*rr)))) 790 return LDNS_RCODE_SERVFAIL; 791 rr->outside_packet = 0; 792 rr->ttl_data = sldns_buffer_current(pkt); 793 rr->next = 0; 794 if(type == LDNS_RR_TYPE_RRSIG && rrset->type != LDNS_RR_TYPE_RRSIG) { 795 if(rrset->rrsig_last) 796 rrset->rrsig_last->next = rr; 797 else rrset->rrsig_first = rr; 798 rrset->rrsig_last = rr; 799 rrset->rrsig_count++; 800 } else { 801 if(rrset->rr_last) 802 rrset->rr_last->next = rr; 803 else rrset->rr_first = rr; 804 rrset->rr_last = rr; 805 rrset->rr_count++; 806 } 807 808 /* calc decompressed size */ 809 if(!calc_size(pkt, type, rr)) 810 return LDNS_RCODE_FORMERR; 811 rrset->size += rr->size; 812 813 return 0; 814 } 815 816 /** 817 * Parse packet RR section, for answer, authority and additional sections. 818 * @param pkt: packet, position at call must be at start of section. 819 * at end position is after section. 820 * @param msg: store results here. 821 * @param region: how to alloc results. 822 * @param section: section enum. 823 * @param num_rrs: how many rrs are in the section. 824 * @param num_rrsets: returns number of rrsets in the section. 825 * @return: 0 if OK, or rcode on error. 826 */ 827 static int 828 parse_section(sldns_buffer* pkt, struct msg_parse* msg, 829 struct regional* region, sldns_pkt_section section, 830 uint16_t num_rrs, size_t* num_rrsets) 831 { 832 uint16_t i; 833 uint8_t* dname, *prev_dname_f = NULL, *prev_dname_l = NULL; 834 size_t dnamelen, prev_dnamelen = 0; 835 uint16_t type, prev_type = 0; 836 uint16_t dclass, prev_dclass = 0; 837 uint32_t rrset_flags = 0; 838 hashvalue_type hash = 0; 839 struct rrset_parse* rrset = NULL; 840 int r; 841 842 if(num_rrs == 0) 843 return 0; 844 if(sldns_buffer_remaining(pkt) <= 0) 845 return LDNS_RCODE_FORMERR; 846 for(i=0; i<num_rrs; i++) { 847 /* parse this RR. */ 848 dname = sldns_buffer_current(pkt); 849 if((dnamelen = pkt_dname_len(pkt)) == 0) 850 return LDNS_RCODE_FORMERR; 851 if(sldns_buffer_remaining(pkt) < 10) /* type, class, ttl, len */ 852 return LDNS_RCODE_FORMERR; 853 type = sldns_buffer_read_u16(pkt); 854 sldns_buffer_read(pkt, &dclass, sizeof(dclass)); 855 856 if(0) { /* debug show what is being parsed. */ 857 if(type == LDNS_RR_TYPE_RRSIG) { 858 uint16_t t; 859 if(pkt_rrsig_covered(pkt, 860 sldns_buffer_current(pkt), &t)) 861 fprintf(stderr, "parse of %s(%d) [%s(%d)]", 862 sldns_rr_descript(type)? 863 sldns_rr_descript(type)->_name: "??", 864 (int)type, 865 sldns_rr_descript(t)? 866 sldns_rr_descript(t)->_name: "??", 867 (int)t); 868 } else 869 fprintf(stderr, "parse of %s(%d)", 870 sldns_rr_descript(type)? 871 sldns_rr_descript(type)->_name: "??", 872 (int)type); 873 fprintf(stderr, " %s(%d) ", 874 sldns_lookup_by_id(sldns_rr_classes, 875 (int)ntohs(dclass))?sldns_lookup_by_id( 876 sldns_rr_classes, (int)ntohs(dclass))->name: 877 "??", (int)ntohs(dclass)); 878 dname_print(stderr, pkt, dname); 879 fprintf(stderr, "\n"); 880 } 881 882 /* see if it is part of an existing RR set */ 883 if(!find_rrset(msg, pkt, dname, dnamelen, type, dclass, &hash, 884 &rrset_flags, &prev_dname_f, &prev_dname_l, 885 &prev_dnamelen, &prev_type, &prev_dclass, &rrset, 886 section, region)) 887 return LDNS_RCODE_SERVFAIL; 888 if(!rrset) { 889 /* it is a new RR set. hash&flags already calculated.*/ 890 (*num_rrsets)++; 891 rrset = new_rrset(msg, dname, dnamelen, type, dclass, 892 hash, rrset_flags, section, region); 893 if(!rrset) 894 return LDNS_RCODE_SERVFAIL; 895 } 896 else if(0) { 897 fprintf(stderr, "is part of existing: "); 898 dname_print(stderr, pkt, rrset->dname); 899 fprintf(stderr, " type %s(%d)\n", 900 sldns_rr_descript(rrset->type)? 901 sldns_rr_descript(rrset->type)->_name: "??", 902 (int)rrset->type); 903 } 904 /* add to rrset. */ 905 if((r=add_rr_to_rrset(rrset, pkt, msg, region, section, 906 type)) != 0) 907 return r; 908 } 909 return 0; 910 } 911 912 int 913 parse_packet(sldns_buffer* pkt, struct msg_parse* msg, struct regional* region) 914 { 915 int ret; 916 if(sldns_buffer_remaining(pkt) < LDNS_HEADER_SIZE) 917 return LDNS_RCODE_FORMERR; 918 /* read the header */ 919 sldns_buffer_read(pkt, &msg->id, sizeof(uint16_t)); 920 msg->flags = sldns_buffer_read_u16(pkt); 921 msg->qdcount = sldns_buffer_read_u16(pkt); 922 msg->ancount = sldns_buffer_read_u16(pkt); 923 msg->nscount = sldns_buffer_read_u16(pkt); 924 msg->arcount = sldns_buffer_read_u16(pkt); 925 if(msg->qdcount > 1) 926 return LDNS_RCODE_FORMERR; 927 if((ret = parse_query_section(pkt, msg)) != 0) 928 return ret; 929 if((ret = parse_section(pkt, msg, region, LDNS_SECTION_ANSWER, 930 msg->ancount, &msg->an_rrsets)) != 0) 931 return ret; 932 if((ret = parse_section(pkt, msg, region, LDNS_SECTION_AUTHORITY, 933 msg->nscount, &msg->ns_rrsets)) != 0) 934 return ret; 935 if(sldns_buffer_remaining(pkt) == 0 && msg->arcount == 1) { 936 /* BIND accepts leniently that an EDNS record is missing. 937 * so, we do too. */ 938 } else if((ret = parse_section(pkt, msg, region, 939 LDNS_SECTION_ADDITIONAL, msg->arcount, &msg->ar_rrsets)) != 0) 940 return ret; 941 /* if(sldns_buffer_remaining(pkt) > 0) { */ 942 /* there is spurious data at end of packet. ignore */ 943 /* } */ 944 msg->rrset_count = msg->an_rrsets + msg->ns_rrsets + msg->ar_rrsets; 945 return 0; 946 } 947 948 /** parse EDNS options from EDNS wireformat rdata */ 949 static int 950 parse_edns_options_from_query(uint8_t* rdata_ptr, size_t rdata_len, 951 struct edns_data* edns, struct config_file* cfg, struct comm_point* c, 952 struct comm_reply* repinfo, uint32_t now, struct regional* region, 953 struct cookie_secrets* cookie_secrets) 954 { 955 int i = 0, nsid_seen = 0, cookie_seen = 0, padding_seen = 0; 956 /* To respond with a Keepalive option, the client connection must have 957 * received one message with a TCP Keepalive EDNS option, and that 958 * option must have 0 length data. Subsequent messages sent on that 959 * connection will have a TCP Keepalive option. 960 * 961 * In the if-statement below, the option is added unsolicited. This 962 * means that the client has sent an KEEPALIVE option earlier. We know 963 * here this is true, because c->tcp_keepalive is set. 964 */ 965 if (cfg && cfg->do_tcp_keepalive && c && c->type != comm_udp && c->tcp_keepalive) { 966 if(!edns_opt_list_append_keepalive(&edns->opt_list_out, 967 c->tcp_timeout_msec / 100, region)) { 968 log_err("out of memory"); 969 return LDNS_RCODE_SERVFAIL; 970 } 971 } 972 973 /* while still more options, and have code+len to read */ 974 /* ignores partial content (i.e. rdata len 3) */ 975 while(rdata_len >= 4 && i < MAX_PARSED_EDNS_OPTIONS) { 976 uint16_t opt_code = sldns_read_uint16(rdata_ptr); 977 uint16_t opt_len = sldns_read_uint16(rdata_ptr+2); 978 uint8_t server_cookie[40]; 979 enum edns_cookie_val_status cookie_val_status; 980 int cookie_is_v4 = 1; 981 982 rdata_ptr += 4; 983 rdata_len -= 4; 984 if(opt_len > rdata_len) 985 break; /* option code partial */ 986 987 /* handle parse time edns options here */ 988 switch(opt_code) { 989 case LDNS_EDNS_NSID: 990 if (!cfg || !cfg->nsid || nsid_seen) 991 break; 992 nsid_seen = 1; 993 if(!edns_opt_list_append(&edns->opt_list_out, 994 LDNS_EDNS_NSID, cfg->nsid_len, 995 cfg->nsid, region)) { 996 log_err("out of memory"); 997 return LDNS_RCODE_SERVFAIL; 998 } 999 break; 1000 1001 case LDNS_EDNS_KEEPALIVE: 1002 /* To respond with a Keepalive option, the client 1003 * connection must have received one message with a TCP 1004 * Keepalive EDNS option, and that option must have 0 1005 * length data. Subsequent messages sent on that 1006 * connection will have a TCP Keepalive option. 1007 * 1008 * This should be the first time the client sends this 1009 * option, so c->tcp_keepalive is not set. 1010 * Besides adding the reply KEEPALIVE option, 1011 * c->tcp_keepalive will be set so that the 1012 * option will be added unsolicited in subsequent 1013 * responses (see the comment above the if-statement 1014 * at the start of this function). 1015 */ 1016 if (!cfg || !cfg->do_tcp_keepalive || !c || 1017 c->type == comm_udp || c->tcp_keepalive) 1018 break; 1019 if(opt_len) { 1020 verbose(VERB_ALGO, "query with bad edns keepalive."); 1021 return LDNS_RCODE_FORMERR; 1022 } 1023 if(!edns_opt_list_append_keepalive(&edns->opt_list_out, 1024 c->tcp_timeout_msec / 100, 1025 region)) { 1026 log_err("out of memory"); 1027 return LDNS_RCODE_SERVFAIL; 1028 } 1029 c->tcp_keepalive = 1; 1030 break; 1031 1032 case LDNS_EDNS_PADDING: 1033 if(!cfg || !cfg->pad_responses || 1034 !c || c->type != comm_tcp ||!c->ssl || padding_seen) 1035 break; 1036 padding_seen = 1; 1037 if(!edns_opt_list_append(&edns->opt_list_out, 1038 LDNS_EDNS_PADDING, 1039 0, NULL, region)) { 1040 log_err("out of memory"); 1041 return LDNS_RCODE_SERVFAIL; 1042 } 1043 edns->padding_block_size = cfg->pad_responses_block_size; 1044 break; 1045 1046 case LDNS_EDNS_COOKIE: 1047 if(!cfg || !cfg->do_answer_cookie || !repinfo || cookie_seen) 1048 break; 1049 cookie_seen = 1; 1050 if(opt_len != 8 && (opt_len < 16 || opt_len > 40)) { 1051 verbose(VERB_ALGO, "worker request: " 1052 "badly formatted cookie"); 1053 return LDNS_RCODE_FORMERR; 1054 } 1055 edns->cookie_present = 1; 1056 1057 /* Copy client cookie, version and timestamp for 1058 * validation and creation purposes. 1059 */ 1060 if(opt_len >= 16) { 1061 memmove(server_cookie, rdata_ptr, 16); 1062 } else { 1063 memset(server_cookie, 0, 16); 1064 memmove(server_cookie, rdata_ptr, opt_len); 1065 } 1066 1067 /* Copy client ip for validation and creation 1068 * purposes. It will be overwritten if (re)creation 1069 * is needed. 1070 */ 1071 if(repinfo->remote_addr.ss_family == AF_INET) { 1072 memcpy(server_cookie + 16, 1073 &((struct sockaddr_in*)&repinfo->remote_addr)->sin_addr, 4); 1074 } else { 1075 cookie_is_v4 = 0; 1076 memcpy(server_cookie + 16, 1077 &((struct sockaddr_in6*)&repinfo->remote_addr)->sin6_addr, 16); 1078 } 1079 1080 if(cfg->cookie_secret_file && 1081 cfg->cookie_secret_file[0]) { 1082 /* Loop over the active and staging cookies. */ 1083 cookie_val_status = 1084 cookie_secrets_server_validate( 1085 rdata_ptr, opt_len, cookie_secrets, 1086 cookie_is_v4, server_cookie, now); 1087 } else { 1088 /* Use the cookie option value to validate. */ 1089 cookie_val_status = edns_cookie_server_validate( 1090 rdata_ptr, opt_len, cfg->cookie_secret, 1091 cfg->cookie_secret_len, cookie_is_v4, 1092 server_cookie, now); 1093 } 1094 if(cookie_val_status == COOKIE_STATUS_VALID_RENEW) 1095 edns->cookie_valid = 1; 1096 switch(cookie_val_status) { 1097 case COOKIE_STATUS_VALID: 1098 edns->cookie_valid = 1; 1099 /* Reuse cookie */ 1100 if(!edns_opt_list_append( 1101 &edns->opt_list_out, LDNS_EDNS_COOKIE, 1102 opt_len, rdata_ptr, region)) { 1103 log_err("out of memory"); 1104 return LDNS_RCODE_SERVFAIL; 1105 } 1106 /* Cookie to be reused added to outgoing 1107 * options. Done! 1108 */ 1109 break; 1110 case COOKIE_STATUS_CLIENT_ONLY: 1111 edns->cookie_client = 1; 1112 ATTR_FALLTHROUGH 1113 /* fallthrough */ 1114 case COOKIE_STATUS_VALID_RENEW: 1115 case COOKIE_STATUS_FUTURE: 1116 case COOKIE_STATUS_EXPIRED: 1117 case COOKIE_STATUS_INVALID: 1118 default: 1119 if(cfg->cookie_secret_file && 1120 cfg->cookie_secret_file[0]) { 1121 if(!cookie_secrets) 1122 break; 1123 lock_basic_lock(&cookie_secrets->lock); 1124 if(cookie_secrets->cookie_count < 1) { 1125 lock_basic_unlock(&cookie_secrets->lock); 1126 break; 1127 } 1128 edns_cookie_server_write(server_cookie, 1129 cookie_secrets->cookie_secrets[0].cookie_secret, 1130 cookie_is_v4, now); 1131 lock_basic_unlock(&cookie_secrets->lock); 1132 } else { 1133 edns_cookie_server_write(server_cookie, 1134 cfg->cookie_secret, cookie_is_v4, now); 1135 } 1136 if(!edns_opt_list_append(&edns->opt_list_out, 1137 LDNS_EDNS_COOKIE, 24, server_cookie, 1138 region)) { 1139 log_err("out of memory"); 1140 return LDNS_RCODE_SERVFAIL; 1141 } 1142 break; 1143 } 1144 break; 1145 default: 1146 break; 1147 } 1148 if(!edns_opt_list_append(&edns->opt_list_in, 1149 opt_code, opt_len, rdata_ptr, region)) { 1150 log_err("out of memory"); 1151 return LDNS_RCODE_SERVFAIL; 1152 } 1153 rdata_ptr += opt_len; 1154 rdata_len -= opt_len; 1155 i++; 1156 } 1157 return LDNS_RCODE_NOERROR; 1158 } 1159 1160 int 1161 parse_extract_edns_from_response_msg(struct msg_parse* msg, 1162 struct edns_data* edns, struct regional* region) 1163 { 1164 struct rrset_parse* rrset = msg->rrset_first; 1165 struct rrset_parse* prev = 0; 1166 struct rrset_parse* found = 0; 1167 struct rrset_parse* found_prev = 0; 1168 size_t rdata_len; 1169 uint8_t* rdata_ptr; 1170 int i = 0; 1171 /* since the class encodes the UDP size, we cannot use hash table to 1172 * find the EDNS OPT record. Scan the packet. */ 1173 while(rrset) { 1174 if(rrset->type == LDNS_RR_TYPE_OPT) { 1175 /* only one OPT RR allowed. */ 1176 if(found) return LDNS_RCODE_FORMERR; 1177 /* found it! */ 1178 found_prev = prev; 1179 found = rrset; 1180 } 1181 prev = rrset; 1182 rrset = rrset->rrset_all_next; 1183 } 1184 if(!found) { 1185 memset(edns, 0, sizeof(*edns)); 1186 edns->udp_size = 512; 1187 return 0; 1188 } 1189 /* check the found RRset */ 1190 /* most lenient check possible. ignore dname, use last opt */ 1191 if(found->section != LDNS_SECTION_ADDITIONAL) 1192 return LDNS_RCODE_FORMERR; 1193 if(found->rr_count == 0) 1194 return LDNS_RCODE_FORMERR; 1195 if(0) { /* strict checking of dname and RRcount */ 1196 if(found->dname_len != 1 || !found->dname 1197 || found->dname[0] != 0) return LDNS_RCODE_FORMERR; 1198 if(found->rr_count != 1) return LDNS_RCODE_FORMERR; 1199 } 1200 log_assert(found->rr_first && found->rr_last); 1201 1202 /* remove from packet */ 1203 if(found_prev) found_prev->rrset_all_next = found->rrset_all_next; 1204 else msg->rrset_first = found->rrset_all_next; 1205 if(found == msg->rrset_last) 1206 msg->rrset_last = found_prev; 1207 msg->arcount --; 1208 msg->ar_rrsets --; 1209 msg->rrset_count --; 1210 1211 /* take the data ! */ 1212 edns->edns_present = 1; 1213 edns->ext_rcode = found->rr_last->ttl_data[0]; 1214 edns->edns_version = found->rr_last->ttl_data[1]; 1215 edns->bits = sldns_read_uint16(&found->rr_last->ttl_data[2]); 1216 edns->udp_size = ntohs(found->rrset_class); 1217 edns->opt_list_in = NULL; 1218 edns->opt_list_out = NULL; 1219 edns->opt_list_inplace_cb_out = NULL; 1220 edns->padding_block_size = 0; 1221 edns->cookie_present = 0; 1222 edns->cookie_valid = 0; 1223 1224 /* take the options */ 1225 rdata_len = found->rr_first->size-2; 1226 rdata_ptr = found->rr_first->ttl_data+6; 1227 1228 /* while still more options, and have code+len to read */ 1229 /* ignores partial content (i.e. rdata len 3) */ 1230 while(rdata_len >= 4 && i < MAX_PARSED_EDNS_OPTIONS) { 1231 uint16_t opt_code = sldns_read_uint16(rdata_ptr); 1232 uint16_t opt_len = sldns_read_uint16(rdata_ptr+2); 1233 rdata_ptr += 4; 1234 rdata_len -= 4; 1235 if(opt_len > rdata_len) 1236 break; /* option code partial */ 1237 1238 if(!edns_opt_list_append(&edns->opt_list_in, 1239 opt_code, opt_len, rdata_ptr, region)) { 1240 log_err("out of memory"); 1241 break; 1242 } 1243 rdata_ptr += opt_len; 1244 rdata_len -= opt_len; 1245 i++; 1246 } 1247 /* ignore rrsigs */ 1248 return LDNS_RCODE_NOERROR; 1249 } 1250 1251 /** skip RR in packet */ 1252 static int 1253 skip_pkt_rr(sldns_buffer* pkt) 1254 { 1255 if(sldns_buffer_remaining(pkt) < 1) return 0; 1256 if(!pkt_dname_len(pkt)) 1257 return 0; 1258 if(sldns_buffer_remaining(pkt) < 4) return 0; 1259 sldns_buffer_skip(pkt, 4); /* type and class */ 1260 if(!skip_ttl_rdata(pkt)) 1261 return 0; 1262 return 1; 1263 } 1264 1265 /** skip RRs from packet */ 1266 int 1267 skip_pkt_rrs(sldns_buffer* pkt, int num) 1268 { 1269 int i; 1270 for(i=0; i<num; i++) { 1271 if(!skip_pkt_rr(pkt)) 1272 return 0; 1273 } 1274 return 1; 1275 } 1276 1277 int 1278 parse_edns_from_query_pkt(sldns_buffer* pkt, struct edns_data* edns, 1279 struct config_file* cfg, struct comm_point* c, 1280 struct comm_reply* repinfo, time_t now, struct regional* region, 1281 struct cookie_secrets* cookie_secrets) 1282 { 1283 size_t rdata_len; 1284 uint8_t* rdata_ptr; 1285 log_assert(LDNS_QDCOUNT(sldns_buffer_begin(pkt)) == 1); 1286 memset(edns, 0, sizeof(*edns)); 1287 if(LDNS_ANCOUNT(sldns_buffer_begin(pkt)) != 0 || 1288 LDNS_NSCOUNT(sldns_buffer_begin(pkt)) != 0) { 1289 if(!skip_pkt_rrs(pkt, ((int)LDNS_ANCOUNT(sldns_buffer_begin(pkt)))+ 1290 ((int)LDNS_NSCOUNT(sldns_buffer_begin(pkt))))) 1291 return LDNS_RCODE_FORMERR; 1292 } 1293 /* check edns section is present */ 1294 if(LDNS_ARCOUNT(sldns_buffer_begin(pkt)) > 1) { 1295 return LDNS_RCODE_FORMERR; 1296 } 1297 if(LDNS_ARCOUNT(sldns_buffer_begin(pkt)) == 0) { 1298 edns->udp_size = 512; 1299 return 0; 1300 } 1301 /* domain name must be the root of length 1. */ 1302 if(pkt_dname_len(pkt) != 1) 1303 return LDNS_RCODE_FORMERR; 1304 if(sldns_buffer_remaining(pkt) < 10) /* type, class, ttl, rdatalen */ 1305 return LDNS_RCODE_FORMERR; 1306 if(sldns_buffer_read_u16(pkt) != LDNS_RR_TYPE_OPT) 1307 return LDNS_RCODE_FORMERR; 1308 edns->edns_present = 1; 1309 edns->udp_size = sldns_buffer_read_u16(pkt); /* class is udp size */ 1310 edns->ext_rcode = sldns_buffer_read_u8(pkt); /* ttl used for bits */ 1311 edns->edns_version = sldns_buffer_read_u8(pkt); 1312 edns->bits = sldns_buffer_read_u16(pkt); 1313 edns->opt_list_in = NULL; 1314 edns->opt_list_out = NULL; 1315 edns->opt_list_inplace_cb_out = NULL; 1316 edns->padding_block_size = 0; 1317 edns->cookie_present = 0; 1318 edns->cookie_valid = 0; 1319 1320 /* take the options */ 1321 rdata_len = sldns_buffer_read_u16(pkt); 1322 if(sldns_buffer_remaining(pkt) < rdata_len) 1323 return LDNS_RCODE_FORMERR; 1324 rdata_ptr = sldns_buffer_current(pkt); 1325 /* ignore rrsigs */ 1326 return parse_edns_options_from_query(rdata_ptr, rdata_len, edns, cfg, 1327 c, repinfo, now, region, cookie_secrets); 1328 } 1329 1330 void 1331 log_edns_opt_list(enum verbosity_value level, const char* info_str, 1332 struct edns_option* list) 1333 { 1334 if(verbosity >= level && list) { 1335 char str[128], *s; 1336 size_t slen; 1337 verbose(level, "%s", info_str); 1338 while(list) { 1339 s = str; 1340 slen = sizeof(str); 1341 (void)sldns_wire2str_edns_option_print(&s, &slen, list->opt_code, 1342 list->opt_data, list->opt_len); 1343 verbose(level, " %s", str); 1344 list = list->next; 1345 } 1346 } 1347 } 1348 1349 /** remove RR from msgparse RRset, return true if rrset is entirely bad */ 1350 int 1351 msgparse_rrset_remove_rr(const char* str, sldns_buffer* pkt, struct rrset_parse* rrset, 1352 struct rr_parse* prev, struct rr_parse* rr, struct sockaddr_storage* addr, socklen_t addrlen) 1353 { 1354 if(verbosity >= VERB_QUERY && rrset->dname_len <= LDNS_MAX_DOMAINLEN && str) { 1355 uint8_t buf[LDNS_MAX_DOMAINLEN+1]; 1356 dname_pkt_copy(pkt, buf, rrset->dname); 1357 if(addr) 1358 log_name_addr(VERB_QUERY, str, buf, addr, addrlen); 1359 else log_nametypeclass(VERB_QUERY, str, buf, 1360 rrset->type, ntohs(rrset->rrset_class)); 1361 } 1362 if(prev) 1363 prev->next = rr->next; 1364 else rrset->rr_first = rr->next; 1365 if(rrset->rr_last == rr) 1366 rrset->rr_last = prev; 1367 rrset->rr_count --; 1368 rrset->size -= rr->size; 1369 /* rr struct still exists, but is unlinked, so that in the for loop 1370 * the rr->next works fine to continue. */ 1371 return rrset->rr_count == 0; 1372 } 1373 1374 #ifdef UNBOUND_DEBUG 1375 time_t debug_expired_reply_ttl_calc(time_t ttl, time_t ttl_add) { 1376 /* Check that we are serving expired when this is called */ 1377 /* ttl (absolute) should be later than ttl_add */ 1378 /* It is also called during the grace period for type DNAME, 1379 * and then the 'SERVE_EXPIRED' boolean may not be on. */ 1380 log_assert(ttl_add <= ttl); 1381 return (SERVE_EXPIRED_REPLY_TTL < (ttl) - (ttl_add) ? 1382 SERVE_EXPIRED_REPLY_TTL : (ttl) - (ttl_add)); 1383 } 1384 #endif 1385