1 /* 2 * util/data/msgreply.c - store message and reply data. 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 data structure to store a message and its reply. 40 */ 41 42 #include "config.h" 43 #include "util/data/msgreply.h" 44 #include "util/storage/lookup3.h" 45 #include "util/log.h" 46 #include "util/alloc.h" 47 #include "util/netevent.h" 48 #include "util/net_help.h" 49 #include "util/data/dname.h" 50 #include "util/regional.h" 51 #include "util/data/msgparse.h" 52 #include "util/data/msgencode.h" 53 #include "sldns/sbuffer.h" 54 #include "sldns/wire2str.h" 55 #include "util/module.h" 56 #include "util/fptr_wlist.h" 57 58 /** MAX TTL default for messages and rrsets */ 59 time_t MAX_TTL = 3600 * 24 * 10; /* ten days */ 60 /** MIN TTL default for messages and rrsets */ 61 time_t MIN_TTL = 0; 62 /** MAX Negative TTL, for SOA records in authority section */ 63 time_t MAX_NEG_TTL = 3600; /* one hour */ 64 /** If we serve expired entries and prefetch them */ 65 int SERVE_EXPIRED = 0; 66 /** Time to serve records after expiration */ 67 time_t SERVE_EXPIRED_TTL = 0; 68 /** TTL to use for expired records */ 69 time_t SERVE_EXPIRED_REPLY_TTL = 30; 70 /** If we serve the original TTL or decrementing TTLs */ 71 int SERVE_ORIGINAL_TTL = 0; 72 73 /** allocate qinfo, return 0 on error */ 74 static int 75 parse_create_qinfo(sldns_buffer* pkt, struct msg_parse* msg, 76 struct query_info* qinf, struct regional* region) 77 { 78 if(msg->qname) { 79 if(region) 80 qinf->qname = (uint8_t*)regional_alloc(region, 81 msg->qname_len); 82 else qinf->qname = (uint8_t*)malloc(msg->qname_len); 83 if(!qinf->qname) return 0; 84 dname_pkt_copy(pkt, qinf->qname, msg->qname); 85 } else qinf->qname = 0; 86 qinf->qname_len = msg->qname_len; 87 qinf->qtype = msg->qtype; 88 qinf->qclass = msg->qclass; 89 qinf->local_alias = NULL; 90 return 1; 91 } 92 93 /** constructor for replyinfo */ 94 struct reply_info* 95 construct_reply_info_base(struct regional* region, uint16_t flags, size_t qd, 96 time_t ttl, time_t prettl, time_t expttl, size_t an, size_t ns, 97 size_t ar, size_t total, enum sec_status sec, sldns_ede_code reason_bogus) 98 { 99 struct reply_info* rep; 100 /* rrset_count-1 because the first ref is part of the struct. */ 101 size_t s = sizeof(struct reply_info) - sizeof(struct rrset_ref) + 102 sizeof(struct ub_packed_rrset_key*) * total; 103 if(total >= RR_COUNT_MAX) return NULL; /* sanity check on numRRS*/ 104 if(region) 105 rep = (struct reply_info*)regional_alloc(region, s); 106 else rep = (struct reply_info*)malloc(s + 107 sizeof(struct rrset_ref) * (total)); 108 if(!rep) 109 return NULL; 110 rep->flags = flags; 111 rep->qdcount = qd; 112 rep->ttl = ttl; 113 rep->prefetch_ttl = prettl; 114 rep->serve_expired_ttl = expttl; 115 rep->an_numrrsets = an; 116 rep->ns_numrrsets = ns; 117 rep->ar_numrrsets = ar; 118 rep->rrset_count = total; 119 rep->security = sec; 120 rep->reason_bogus = reason_bogus; 121 /* this is only allocated and used for caching on copy */ 122 rep->reason_bogus_str = NULL; 123 rep->authoritative = 0; 124 /* array starts after the refs */ 125 if(region) 126 rep->rrsets = (struct ub_packed_rrset_key**)&(rep->ref[0]); 127 else rep->rrsets = (struct ub_packed_rrset_key**)&(rep->ref[total]); 128 /* zero the arrays to assist cleanup in case of malloc failure */ 129 memset( rep->rrsets, 0, sizeof(struct ub_packed_rrset_key*) * total); 130 if(!region) 131 memset( &rep->ref[0], 0, sizeof(struct rrset_ref) * total); 132 return rep; 133 } 134 135 /** allocate replyinfo, return 0 on error */ 136 static int 137 parse_create_repinfo(struct msg_parse* msg, struct reply_info** rep, 138 struct regional* region) 139 { 140 *rep = construct_reply_info_base(region, msg->flags, msg->qdcount, 0, 141 0, 0, msg->an_rrsets, msg->ns_rrsets, msg->ar_rrsets, 142 msg->rrset_count, sec_status_unchecked, LDNS_EDE_NONE); 143 if(!*rep) 144 return 0; 145 return 1; 146 } 147 148 int 149 reply_info_alloc_rrset_keys(struct reply_info* rep, struct alloc_cache* alloc, 150 struct regional* region) 151 { 152 size_t i; 153 for(i=0; i<rep->rrset_count; i++) { 154 if(region) { 155 rep->rrsets[i] = (struct ub_packed_rrset_key*) 156 regional_alloc(region, 157 sizeof(struct ub_packed_rrset_key)); 158 if(rep->rrsets[i]) { 159 memset(rep->rrsets[i], 0, 160 sizeof(struct ub_packed_rrset_key)); 161 rep->rrsets[i]->entry.key = rep->rrsets[i]; 162 } 163 } 164 else rep->rrsets[i] = alloc_special_obtain(alloc); 165 if(!rep->rrsets[i]) 166 return 0; 167 rep->rrsets[i]->entry.data = NULL; 168 } 169 return 1; 170 } 171 172 struct reply_info * 173 make_new_reply_info(const struct reply_info* rep, struct regional* region, 174 size_t an_numrrsets, size_t copy_rrsets) 175 { 176 struct reply_info* new_rep; 177 size_t i; 178 179 /* create a base struct. we specify 'insecure' security status as 180 * the modified response won't be DNSSEC-valid. In our faked response 181 * the authority and additional sections will be empty (except possible 182 * EDNS0 OPT RR in the additional section appended on sending it out), 183 * so the total number of RRsets is an_numrrsets. */ 184 new_rep = construct_reply_info_base(region, rep->flags, 185 rep->qdcount, rep->ttl, rep->prefetch_ttl, 186 rep->serve_expired_ttl, an_numrrsets, 0, 0, an_numrrsets, 187 sec_status_insecure, LDNS_EDE_NONE); 188 if(!new_rep) 189 return NULL; 190 if(!reply_info_alloc_rrset_keys(new_rep, NULL, region)) 191 return NULL; 192 for(i=0; i<copy_rrsets; i++) 193 new_rep->rrsets[i] = rep->rrsets[i]; 194 195 return new_rep; 196 } 197 198 /** find the minimumttl in the rdata of SOA record */ 199 static time_t 200 soa_find_minttl(struct rr_parse* rr) 201 { 202 uint16_t rlen = sldns_read_uint16(rr->ttl_data+4); 203 if(rlen < 20) 204 return 0; /* rdata too small for SOA (dname, dname, 5*32bit) */ 205 /* minimum TTL is the last 32bit value in the rdata of the record */ 206 /* at position ttl_data + 4(ttl) + 2(rdatalen) + rdatalen - 4(timeval)*/ 207 return (time_t)sldns_read_uint32(rr->ttl_data+6+rlen-4); 208 } 209 210 /** do the rdata copy */ 211 static int 212 rdata_copy(sldns_buffer* pkt, struct packed_rrset_data* data, uint8_t* to, 213 struct rr_parse* rr, time_t* rr_ttl, uint16_t type, 214 sldns_pkt_section section) 215 { 216 uint16_t pkt_len; 217 const sldns_rr_descriptor* desc; 218 219 *rr_ttl = sldns_read_uint32(rr->ttl_data); 220 /* RFC 2181 Section 8. if msb of ttl is set treat as if zero. */ 221 if(*rr_ttl & 0x80000000U) 222 *rr_ttl = 0; 223 if(type == LDNS_RR_TYPE_SOA && section == LDNS_SECTION_AUTHORITY) { 224 /* negative response. see if TTL of SOA record larger than the 225 * minimum-ttl in the rdata of the SOA record */ 226 if(*rr_ttl > soa_find_minttl(rr)) 227 *rr_ttl = soa_find_minttl(rr); 228 } 229 if(!SERVE_ORIGINAL_TTL && (*rr_ttl < MIN_TTL)) 230 *rr_ttl = MIN_TTL; 231 if(!SERVE_ORIGINAL_TTL && (*rr_ttl > MAX_TTL)) 232 *rr_ttl = MAX_TTL; 233 if(type == LDNS_RR_TYPE_SOA && section == LDNS_SECTION_AUTHORITY) { 234 /* max neg ttl overrides the min and max ttl of everything 235 * else, it is for a more specific record */ 236 if(*rr_ttl > MAX_NEG_TTL) 237 *rr_ttl = MAX_NEG_TTL; 238 } 239 if(*rr_ttl < data->ttl) 240 data->ttl = *rr_ttl; 241 242 if(rr->outside_packet) { 243 /* uncompressed already, only needs copy */ 244 memmove(to, rr->ttl_data+sizeof(uint32_t), rr->size); 245 return 1; 246 } 247 248 sldns_buffer_set_position(pkt, (size_t) 249 (rr->ttl_data - sldns_buffer_begin(pkt) + sizeof(uint32_t))); 250 /* insert decompressed size into rdata len stored in memory */ 251 /* -2 because rdatalen bytes are not included. */ 252 pkt_len = htons(rr->size - 2); 253 memmove(to, &pkt_len, sizeof(uint16_t)); 254 to += 2; 255 /* read packet rdata len */ 256 pkt_len = sldns_buffer_read_u16(pkt); 257 if(sldns_buffer_remaining(pkt) < pkt_len) 258 return 0; 259 desc = sldns_rr_descript(type); 260 if(pkt_len > 0 && desc && desc->_dname_count > 0) { 261 int count = (int)desc->_dname_count; 262 int rdf = 0; 263 size_t len; 264 size_t oldpos; 265 /* decompress dnames. */ 266 while(pkt_len > 0 && count) { 267 switch(desc->_wireformat[rdf]) { 268 case LDNS_RDF_TYPE_DNAME: 269 oldpos = sldns_buffer_position(pkt); 270 dname_pkt_copy(pkt, to, 271 sldns_buffer_current(pkt)); 272 to += pkt_dname_len(pkt); 273 pkt_len -= sldns_buffer_position(pkt)-oldpos; 274 count--; 275 len = 0; 276 break; 277 case LDNS_RDF_TYPE_STR: 278 len = sldns_buffer_current(pkt)[0] + 1; 279 break; 280 default: 281 len = get_rdf_size(desc->_wireformat[rdf]); 282 break; 283 } 284 if(len) { 285 log_assert(len <= pkt_len); 286 memmove(to, sldns_buffer_current(pkt), len); 287 to += len; 288 sldns_buffer_skip(pkt, (ssize_t)len); 289 pkt_len -= len; 290 } 291 rdf++; 292 } 293 } 294 /* copy remaining rdata */ 295 if(pkt_len > 0) 296 memmove(to, sldns_buffer_current(pkt), pkt_len); 297 298 return 1; 299 } 300 301 /** copy over the data into packed rrset */ 302 static int 303 parse_rr_copy(sldns_buffer* pkt, struct rrset_parse* pset, 304 struct packed_rrset_data* data) 305 { 306 size_t i; 307 struct rr_parse* rr = pset->rr_first; 308 uint8_t* nextrdata; 309 size_t total = pset->rr_count + pset->rrsig_count; 310 data->ttl = MAX_TTL; 311 data->count = pset->rr_count; 312 data->rrsig_count = pset->rrsig_count; 313 data->trust = rrset_trust_none; 314 data->security = sec_status_unchecked; 315 /* layout: struct - rr_len - rr_data - rr_ttl - rdata - rrsig */ 316 data->rr_len = (size_t*)((uint8_t*)data + 317 sizeof(struct packed_rrset_data)); 318 data->rr_data = (uint8_t**)&(data->rr_len[total]); 319 data->rr_ttl = (time_t*)&(data->rr_data[total]); 320 nextrdata = (uint8_t*)&(data->rr_ttl[total]); 321 for(i=0; i<data->count; i++) { 322 data->rr_len[i] = rr->size; 323 data->rr_data[i] = nextrdata; 324 nextrdata += rr->size; 325 if(!rdata_copy(pkt, data, data->rr_data[i], rr, 326 &data->rr_ttl[i], pset->type, pset->section)) 327 return 0; 328 rr = rr->next; 329 } 330 /* if rrsig, its rdata is at nextrdata */ 331 rr = pset->rrsig_first; 332 for(i=data->count; i<total; i++) { 333 data->rr_len[i] = rr->size; 334 data->rr_data[i] = nextrdata; 335 nextrdata += rr->size; 336 if(!rdata_copy(pkt, data, data->rr_data[i], rr, 337 &data->rr_ttl[i], LDNS_RR_TYPE_RRSIG, pset->section)) 338 return 0; 339 rr = rr->next; 340 } 341 return 1; 342 } 343 344 /** create rrset return 0 on failure */ 345 static int 346 parse_create_rrset(sldns_buffer* pkt, struct rrset_parse* pset, 347 struct packed_rrset_data** data, struct regional* region) 348 { 349 /* allocate */ 350 size_t s; 351 if(pset->rr_count > RR_COUNT_MAX || pset->rrsig_count > RR_COUNT_MAX || 352 pset->size > RR_COUNT_MAX) 353 return 0; /* protect against integer overflow */ 354 s = sizeof(struct packed_rrset_data) + 355 (pset->rr_count + pset->rrsig_count) * 356 (sizeof(size_t)+sizeof(uint8_t*)+sizeof(time_t)) + 357 pset->size; 358 if(region) 359 *data = regional_alloc_zero(region, s); 360 else *data = calloc(1, s); 361 if(!*data) 362 return 0; 363 /* copy & decompress */ 364 if(!parse_rr_copy(pkt, pset, *data)) { 365 if(!region) { 366 free(*data); 367 *data = NULL; 368 } 369 return 0; 370 } 371 return 1; 372 } 373 374 /** get trust value for rrset */ 375 static enum rrset_trust 376 get_rrset_trust(struct msg_parse* msg, struct rrset_parse* rrset) 377 { 378 uint16_t AA = msg->flags & BIT_AA; 379 if(rrset->section == LDNS_SECTION_ANSWER) { 380 if(AA) { 381 /* RFC2181 says remainder of CNAME chain is nonauth*/ 382 if(msg->rrset_first && 383 msg->rrset_first->section==LDNS_SECTION_ANSWER 384 && msg->rrset_first->type==LDNS_RR_TYPE_CNAME){ 385 if(rrset == msg->rrset_first) 386 return rrset_trust_ans_AA; 387 else return rrset_trust_ans_noAA; 388 } 389 if(msg->rrset_first && 390 msg->rrset_first->section==LDNS_SECTION_ANSWER 391 && msg->rrset_first->type==LDNS_RR_TYPE_DNAME){ 392 if(rrset == msg->rrset_first || 393 rrset == msg->rrset_first->rrset_all_next) 394 return rrset_trust_ans_AA; 395 else return rrset_trust_ans_noAA; 396 } 397 return rrset_trust_ans_AA; 398 } 399 else return rrset_trust_ans_noAA; 400 } else if(rrset->section == LDNS_SECTION_AUTHORITY) { 401 if(AA) return rrset_trust_auth_AA; 402 else return rrset_trust_auth_noAA; 403 } else { 404 /* addit section */ 405 if(AA) return rrset_trust_add_AA; 406 else return rrset_trust_add_noAA; 407 } 408 /* NOTREACHED */ 409 return rrset_trust_none; 410 } 411 412 int 413 parse_copy_decompress_rrset(sldns_buffer* pkt, struct msg_parse* msg, 414 struct rrset_parse *pset, struct regional* region, 415 struct ub_packed_rrset_key* pk) 416 { 417 struct packed_rrset_data* data; 418 pk->rk.flags = pset->flags; 419 pk->rk.dname_len = pset->dname_len; 420 if(region) 421 pk->rk.dname = (uint8_t*)regional_alloc( 422 region, pset->dname_len); 423 else pk->rk.dname = 424 (uint8_t*)malloc(pset->dname_len); 425 if(!pk->rk.dname) 426 return 0; 427 /** copy & decompress dname */ 428 dname_pkt_copy(pkt, pk->rk.dname, pset->dname); 429 /** copy over type and class */ 430 pk->rk.type = htons(pset->type); 431 pk->rk.rrset_class = pset->rrset_class; 432 /** read data part. */ 433 if(!parse_create_rrset(pkt, pset, &data, region)) { 434 if(!region) { 435 free(pk->rk.dname); 436 pk->rk.dname = NULL; 437 } 438 return 0; 439 } 440 pk->entry.data = (void*)data; 441 pk->entry.key = (void*)pk; 442 pk->entry.hash = pset->hash; 443 data->trust = get_rrset_trust(msg, pset); 444 return 1; 445 } 446 447 /** 448 * Copy and decompress rrs 449 * @param pkt: the packet for compression pointer resolution. 450 * @param msg: the parsed message 451 * @param rep: reply info to put rrs into. 452 * @param region: if not NULL, used for allocation. 453 * @return 0 on failure. 454 */ 455 static int 456 parse_copy_decompress(sldns_buffer* pkt, struct msg_parse* msg, 457 struct reply_info* rep, struct regional* region) 458 { 459 size_t i; 460 struct rrset_parse *pset = msg->rrset_first; 461 struct packed_rrset_data* data; 462 log_assert(rep); 463 rep->ttl = MAX_TTL; 464 rep->security = sec_status_unchecked; 465 if(rep->rrset_count == 0) 466 rep->ttl = NORR_TTL; 467 468 for(i=0; i<rep->rrset_count; i++) { 469 if(!parse_copy_decompress_rrset(pkt, msg, pset, region, 470 rep->rrsets[i])) 471 return 0; 472 data = (struct packed_rrset_data*)rep->rrsets[i]->entry.data; 473 if(data->ttl < rep->ttl) 474 rep->ttl = data->ttl; 475 476 pset = pset->rrset_all_next; 477 } 478 rep->prefetch_ttl = PREFETCH_TTL_CALC(rep->ttl); 479 rep->serve_expired_ttl = rep->ttl + SERVE_EXPIRED_TTL; 480 return 1; 481 } 482 483 int 484 parse_create_msg(sldns_buffer* pkt, struct msg_parse* msg, 485 struct alloc_cache* alloc, struct query_info* qinf, 486 struct reply_info** rep, struct regional* region) 487 { 488 log_assert(pkt && msg); 489 if(!parse_create_qinfo(pkt, msg, qinf, region)) 490 return 0; 491 if(!parse_create_repinfo(msg, rep, region)) 492 return 0; 493 if(!reply_info_alloc_rrset_keys(*rep, alloc, region)) { 494 if(!region) reply_info_parsedelete(*rep, alloc); 495 return 0; 496 } 497 if(!parse_copy_decompress(pkt, msg, *rep, region)) { 498 if(!region) reply_info_parsedelete(*rep, alloc); 499 return 0; 500 } 501 return 1; 502 } 503 504 int reply_info_parse(sldns_buffer* pkt, struct alloc_cache* alloc, 505 struct query_info* qinf, struct reply_info** rep, 506 struct regional* region, struct edns_data* edns) 507 { 508 /* use scratch pad region-allocator during parsing. */ 509 struct msg_parse* msg; 510 int ret; 511 512 qinf->qname = NULL; 513 qinf->local_alias = NULL; 514 *rep = NULL; 515 if(!(msg = regional_alloc(region, sizeof(*msg)))) { 516 return LDNS_RCODE_SERVFAIL; 517 } 518 memset(msg, 0, sizeof(*msg)); 519 520 sldns_buffer_set_position(pkt, 0); 521 if((ret = parse_packet(pkt, msg, region)) != 0) { 522 return ret; 523 } 524 if((ret = parse_extract_edns_from_response_msg(msg, edns, region)) != 0) 525 return ret; 526 527 /* parse OK, allocate return structures */ 528 /* this also performs dname decompression */ 529 if(!parse_create_msg(pkt, msg, alloc, qinf, rep, NULL)) { 530 query_info_clear(qinf); 531 *rep = NULL; 532 return LDNS_RCODE_SERVFAIL; 533 } 534 return 0; 535 } 536 537 /** helper compare function to sort in lock order */ 538 static int 539 reply_info_sortref_cmp(const void* a, const void* b) 540 { 541 struct rrset_ref* x = (struct rrset_ref*)a; 542 struct rrset_ref* y = (struct rrset_ref*)b; 543 if(x->key < y->key) return -1; 544 if(x->key > y->key) return 1; 545 return 0; 546 } 547 548 void 549 reply_info_sortref(struct reply_info* rep) 550 { 551 qsort(&rep->ref[0], rep->rrset_count, sizeof(struct rrset_ref), 552 reply_info_sortref_cmp); 553 } 554 555 void 556 reply_info_set_ttls(struct reply_info* rep, time_t timenow) 557 { 558 size_t i, j; 559 rep->ttl += timenow; 560 rep->prefetch_ttl += timenow; 561 rep->serve_expired_ttl += timenow; 562 for(i=0; i<rep->rrset_count; i++) { 563 struct packed_rrset_data* data = (struct packed_rrset_data*) 564 rep->ref[i].key->entry.data; 565 if(i>0 && rep->ref[i].key == rep->ref[i-1].key) 566 continue; 567 data->ttl += timenow; 568 for(j=0; j<data->count + data->rrsig_count; j++) { 569 data->rr_ttl[j] += timenow; 570 } 571 data->ttl_add = timenow; 572 } 573 } 574 575 void 576 reply_info_parsedelete(struct reply_info* rep, struct alloc_cache* alloc) 577 { 578 size_t i; 579 if(!rep) 580 return; 581 /* no need to lock, since not shared in hashtables. */ 582 for(i=0; i<rep->rrset_count; i++) { 583 ub_packed_rrset_parsedelete(rep->rrsets[i], alloc); 584 } 585 if(rep->reason_bogus_str) { 586 free(rep->reason_bogus_str); 587 rep->reason_bogus_str = NULL; 588 } 589 free(rep); 590 } 591 592 int 593 query_info_parse(struct query_info* m, sldns_buffer* query) 594 { 595 uint8_t* q = sldns_buffer_begin(query); 596 /* minimum size: header + \0 + qtype + qclass */ 597 if(sldns_buffer_limit(query) < LDNS_HEADER_SIZE + 5) 598 return 0; 599 if((LDNS_OPCODE_WIRE(q) != LDNS_PACKET_QUERY && LDNS_OPCODE_WIRE(q) != 600 LDNS_PACKET_NOTIFY) || LDNS_QDCOUNT(q) != 1 || 601 sldns_buffer_position(query) != 0) 602 return 0; 603 sldns_buffer_skip(query, LDNS_HEADER_SIZE); 604 m->qname = sldns_buffer_current(query); 605 if((m->qname_len = query_dname_len(query)) == 0) 606 return 0; /* parse error */ 607 if(sldns_buffer_remaining(query) < 4) 608 return 0; /* need qtype, qclass */ 609 m->qtype = sldns_buffer_read_u16(query); 610 m->qclass = sldns_buffer_read_u16(query); 611 m->local_alias = NULL; 612 return 1; 613 } 614 615 /** tiny subroutine for msgreply_compare */ 616 #define COMPARE_IT(x, y) \ 617 if( (x) < (y) ) return -1; \ 618 else if( (x) > (y) ) return +1; \ 619 log_assert( (x) == (y) ); 620 621 int 622 query_info_compare(void* m1, void* m2) 623 { 624 struct query_info* msg1 = (struct query_info*)m1; 625 struct query_info* msg2 = (struct query_info*)m2; 626 int mc; 627 /* from most different to least different for speed */ 628 COMPARE_IT(msg1->qtype, msg2->qtype); 629 if((mc = query_dname_compare(msg1->qname, msg2->qname)) != 0) 630 return mc; 631 log_assert(msg1->qname_len == msg2->qname_len); 632 COMPARE_IT(msg1->qclass, msg2->qclass); 633 return 0; 634 #undef COMPARE_IT 635 } 636 637 void 638 query_info_clear(struct query_info* m) 639 { 640 free(m->qname); 641 m->qname = NULL; 642 } 643 644 size_t 645 msgreply_sizefunc(void* k, void* d) 646 { 647 struct msgreply_entry* q = (struct msgreply_entry*)k; 648 struct reply_info* r = (struct reply_info*)d; 649 size_t s = sizeof(struct msgreply_entry) + sizeof(struct reply_info) 650 + q->key.qname_len + lock_get_mem(&q->entry.lock) 651 - sizeof(struct rrset_ref); 652 s += r->rrset_count * sizeof(struct rrset_ref); 653 s += r->rrset_count * sizeof(struct ub_packed_rrset_key*); 654 return s; 655 } 656 657 void 658 query_entry_delete(void *k, void* ATTR_UNUSED(arg)) 659 { 660 struct msgreply_entry* q = (struct msgreply_entry*)k; 661 lock_rw_destroy(&q->entry.lock); 662 query_info_clear(&q->key); 663 free(q); 664 } 665 666 void 667 reply_info_delete(void* d, void* ATTR_UNUSED(arg)) 668 { 669 struct reply_info* r = (struct reply_info*)d; 670 if(r->reason_bogus_str) { 671 free(r->reason_bogus_str); 672 r->reason_bogus_str = NULL; 673 } 674 free(r); 675 } 676 677 hashvalue_type 678 query_info_hash(struct query_info *q, uint16_t flags) 679 { 680 hashvalue_type h = 0xab; 681 h = hashlittle(&q->qtype, sizeof(q->qtype), h); 682 if(q->qtype == LDNS_RR_TYPE_AAAA && (flags&BIT_CD)) 683 h++; 684 h = hashlittle(&q->qclass, sizeof(q->qclass), h); 685 h = dname_query_hash(q->qname, h); 686 return h; 687 } 688 689 struct msgreply_entry* 690 query_info_entrysetup(struct query_info* q, struct reply_info* r, 691 hashvalue_type h) 692 { 693 struct msgreply_entry* e = (struct msgreply_entry*)malloc( 694 sizeof(struct msgreply_entry)); 695 if(!e) return NULL; 696 memcpy(&e->key, q, sizeof(*q)); 697 e->entry.hash = h; 698 e->entry.key = e; 699 e->entry.data = r; 700 lock_rw_init(&e->entry.lock); 701 lock_protect(&e->entry.lock, &e->key.qname, sizeof(e->key.qname)); 702 lock_protect(&e->entry.lock, &e->key.qname_len, sizeof(e->key.qname_len)); 703 lock_protect(&e->entry.lock, &e->key.qtype, sizeof(e->key.qtype)); 704 lock_protect(&e->entry.lock, &e->key.qclass, sizeof(e->key.qclass)); 705 lock_protect(&e->entry.lock, &e->key.local_alias, sizeof(e->key.local_alias)); 706 lock_protect(&e->entry.lock, &e->entry.hash, sizeof(e->entry.hash)); 707 lock_protect(&e->entry.lock, &e->entry.key, sizeof(e->entry.key)); 708 lock_protect(&e->entry.lock, &e->entry.data, sizeof(e->entry.data)); 709 lock_protect(&e->entry.lock, e->key.qname, e->key.qname_len); 710 q->qname = NULL; 711 return e; 712 } 713 714 /** copy rrsets from replyinfo to dest replyinfo */ 715 static int 716 repinfo_copy_rrsets(struct reply_info* dest, struct reply_info* from, 717 struct regional* region) 718 { 719 size_t i, s; 720 struct packed_rrset_data* fd, *dd; 721 struct ub_packed_rrset_key* fk, *dk; 722 for(i=0; i<dest->rrset_count; i++) { 723 fk = from->rrsets[i]; 724 dk = dest->rrsets[i]; 725 fd = (struct packed_rrset_data*)fk->entry.data; 726 dk->entry.hash = fk->entry.hash; 727 dk->rk = fk->rk; 728 if(region) { 729 dk->id = fk->id; 730 dk->rk.dname = (uint8_t*)regional_alloc_init(region, 731 fk->rk.dname, fk->rk.dname_len); 732 } else 733 dk->rk.dname = (uint8_t*)memdup(fk->rk.dname, 734 fk->rk.dname_len); 735 if(!dk->rk.dname) 736 return 0; 737 s = packed_rrset_sizeof(fd); 738 if(region) 739 dd = (struct packed_rrset_data*)regional_alloc_init( 740 region, fd, s); 741 else dd = (struct packed_rrset_data*)memdup(fd, s); 742 if(!dd) 743 return 0; 744 packed_rrset_ptr_fixup(dd); 745 dk->entry.data = (void*)dd; 746 } 747 return 1; 748 } 749 750 struct reply_info* 751 reply_info_copy(struct reply_info* rep, struct alloc_cache* alloc, 752 struct regional* region) 753 { 754 struct reply_info* cp; 755 cp = construct_reply_info_base(region, rep->flags, rep->qdcount, 756 rep->ttl, rep->prefetch_ttl, rep->serve_expired_ttl, 757 rep->an_numrrsets, rep->ns_numrrsets, rep->ar_numrrsets, 758 rep->rrset_count, rep->security, rep->reason_bogus); 759 if(!cp) 760 return NULL; 761 762 if(rep->reason_bogus_str && *rep->reason_bogus_str != 0) { 763 if(region) { 764 cp->reason_bogus_str = (char*)regional_alloc(region, 765 sizeof(char) 766 * (strlen(rep->reason_bogus_str)+1)); 767 } else { 768 cp->reason_bogus_str = malloc(sizeof(char) 769 * (strlen(rep->reason_bogus_str)+1)); 770 } 771 if(!cp->reason_bogus_str) { 772 if(!region) 773 reply_info_parsedelete(cp, alloc); 774 return NULL; 775 } 776 memcpy(cp->reason_bogus_str, rep->reason_bogus_str, 777 strlen(rep->reason_bogus_str)+1); 778 } 779 780 /* allocate ub_key structures special or not */ 781 if(!reply_info_alloc_rrset_keys(cp, alloc, region)) { 782 if(!region) 783 reply_info_parsedelete(cp, alloc); 784 return NULL; 785 } 786 if(!repinfo_copy_rrsets(cp, rep, region)) { 787 if(!region) 788 reply_info_parsedelete(cp, alloc); 789 return NULL; 790 } 791 return cp; 792 } 793 794 uint8_t* 795 reply_find_final_cname_target(struct query_info* qinfo, struct reply_info* rep) 796 { 797 uint8_t* sname = qinfo->qname; 798 size_t snamelen = qinfo->qname_len; 799 size_t i; 800 for(i=0; i<rep->an_numrrsets; i++) { 801 struct ub_packed_rrset_key* s = rep->rrsets[i]; 802 /* follow CNAME chain (if any) */ 803 if(ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME && 804 ntohs(s->rk.rrset_class) == qinfo->qclass && 805 snamelen == s->rk.dname_len && 806 query_dname_compare(sname, s->rk.dname) == 0) { 807 get_cname_target(s, &sname, &snamelen); 808 } 809 } 810 if(sname != qinfo->qname) 811 return sname; 812 return NULL; 813 } 814 815 struct ub_packed_rrset_key* 816 reply_find_answer_rrset(struct query_info* qinfo, struct reply_info* rep) 817 { 818 uint8_t* sname = qinfo->qname; 819 size_t snamelen = qinfo->qname_len; 820 size_t i; 821 for(i=0; i<rep->an_numrrsets; i++) { 822 struct ub_packed_rrset_key* s = rep->rrsets[i]; 823 /* first match type, for query of qtype cname */ 824 if(ntohs(s->rk.type) == qinfo->qtype && 825 ntohs(s->rk.rrset_class) == qinfo->qclass && 826 snamelen == s->rk.dname_len && 827 query_dname_compare(sname, s->rk.dname) == 0) { 828 return s; 829 } 830 /* follow CNAME chain (if any) */ 831 if(ntohs(s->rk.type) == LDNS_RR_TYPE_CNAME && 832 ntohs(s->rk.rrset_class) == qinfo->qclass && 833 snamelen == s->rk.dname_len && 834 query_dname_compare(sname, s->rk.dname) == 0) { 835 get_cname_target(s, &sname, &snamelen); 836 } 837 } 838 return NULL; 839 } 840 841 struct ub_packed_rrset_key* reply_find_rrset_section_an(struct reply_info* rep, 842 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass) 843 { 844 size_t i; 845 for(i=0; i<rep->an_numrrsets; i++) { 846 struct ub_packed_rrset_key* s = rep->rrsets[i]; 847 if(ntohs(s->rk.type) == type && 848 ntohs(s->rk.rrset_class) == dclass && 849 namelen == s->rk.dname_len && 850 query_dname_compare(name, s->rk.dname) == 0) { 851 return s; 852 } 853 } 854 return NULL; 855 } 856 857 struct ub_packed_rrset_key* reply_find_rrset_section_ns(struct reply_info* rep, 858 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass) 859 { 860 size_t i; 861 for(i=rep->an_numrrsets; i<rep->an_numrrsets+rep->ns_numrrsets; i++) { 862 struct ub_packed_rrset_key* s = rep->rrsets[i]; 863 if(ntohs(s->rk.type) == type && 864 ntohs(s->rk.rrset_class) == dclass && 865 namelen == s->rk.dname_len && 866 query_dname_compare(name, s->rk.dname) == 0) { 867 return s; 868 } 869 } 870 return NULL; 871 } 872 873 struct ub_packed_rrset_key* reply_find_rrset(struct reply_info* rep, 874 uint8_t* name, size_t namelen, uint16_t type, uint16_t dclass) 875 { 876 size_t i; 877 for(i=0; i<rep->rrset_count; i++) { 878 struct ub_packed_rrset_key* s = rep->rrsets[i]; 879 if(ntohs(s->rk.type) == type && 880 ntohs(s->rk.rrset_class) == dclass && 881 namelen == s->rk.dname_len && 882 query_dname_compare(name, s->rk.dname) == 0) { 883 return s; 884 } 885 } 886 return NULL; 887 } 888 889 void 890 log_dns_msg(const char* str, struct query_info* qinfo, struct reply_info* rep) 891 { 892 /* not particularly fast but flexible, make wireformat and print */ 893 sldns_buffer* buf = sldns_buffer_new(65535); 894 struct regional* region = regional_create(); 895 if(!(buf && region)) { 896 log_err("%s: log_dns_msg: out of memory", str); 897 sldns_buffer_free(buf); 898 regional_destroy(region); 899 return; 900 } 901 if(!reply_info_encode(qinfo, rep, 0, rep->flags, buf, 0, 902 region, 65535, 1, 0)) { 903 log_err("%s: log_dns_msg: out of memory", str); 904 } else { 905 char* s = sldns_wire2str_pkt(sldns_buffer_begin(buf), 906 sldns_buffer_limit(buf)); 907 if(!s) { 908 log_info("%s: log_dns_msg: ldns tostr failed", str); 909 } else { 910 log_info("%s %s", str, s); 911 } 912 free(s); 913 } 914 sldns_buffer_free(buf); 915 regional_destroy(region); 916 } 917 918 void 919 log_reply_info(enum verbosity_value v, struct query_info *qinf, 920 struct sockaddr_storage *addr, socklen_t addrlen, struct timeval dur, 921 int cached, struct sldns_buffer *rmsg) 922 { 923 char qname_buf[LDNS_MAX_DOMAINLEN+1]; 924 char clientip_buf[128]; 925 char rcode_buf[16]; 926 char type_buf[16]; 927 char class_buf[16]; 928 size_t pktlen; 929 uint16_t rcode = FLAGS_GET_RCODE(sldns_buffer_read_u16_at(rmsg, 2)); 930 931 if(verbosity < v) 932 return; 933 934 sldns_wire2str_rcode_buf((int)rcode, rcode_buf, sizeof(rcode_buf)); 935 addr_to_str(addr, addrlen, clientip_buf, sizeof(clientip_buf)); 936 if(rcode == LDNS_RCODE_FORMERR) 937 { 938 if(LOG_TAG_QUERYREPLY) 939 log_reply("%s - - - %s - - - ", clientip_buf, rcode_buf); 940 else log_info("%s - - - %s - - - ", clientip_buf, rcode_buf); 941 } else { 942 if(qinf->qname) 943 dname_str(qinf->qname, qname_buf); 944 else snprintf(qname_buf, sizeof(qname_buf), "null"); 945 pktlen = sldns_buffer_limit(rmsg); 946 sldns_wire2str_type_buf(qinf->qtype, type_buf, sizeof(type_buf)); 947 sldns_wire2str_class_buf(qinf->qclass, class_buf, sizeof(class_buf)); 948 if(LOG_TAG_QUERYREPLY) 949 log_reply("%s %s %s %s %s " ARG_LL "d.%6.6d %d %d", 950 clientip_buf, qname_buf, type_buf, class_buf, 951 rcode_buf, (long long)dur.tv_sec, (int)dur.tv_usec, cached, (int)pktlen); 952 else log_info("%s %s %s %s %s " ARG_LL "d.%6.6d %d %d", 953 clientip_buf, qname_buf, type_buf, class_buf, 954 rcode_buf, (long long)dur.tv_sec, (int)dur.tv_usec, cached, (int)pktlen); 955 } 956 } 957 958 void 959 log_query_info(enum verbosity_value v, const char* str, 960 struct query_info* qinf) 961 { 962 log_nametypeclass(v, str, qinf->qname, qinf->qtype, qinf->qclass); 963 } 964 965 int 966 reply_check_cname_chain(struct query_info* qinfo, struct reply_info* rep) 967 { 968 /* check only answer section rrs for matching cname chain. 969 * the cache may return changed rdata, but owner names are untouched.*/ 970 size_t i; 971 uint8_t* sname = qinfo->qname; 972 size_t snamelen = qinfo->qname_len; 973 for(i=0; i<rep->an_numrrsets; i++) { 974 uint16_t t = ntohs(rep->rrsets[i]->rk.type); 975 if(t == LDNS_RR_TYPE_DNAME) 976 continue; /* skip dnames; note TTL 0 not cached */ 977 /* verify that owner matches current sname */ 978 if(query_dname_compare(sname, rep->rrsets[i]->rk.dname) != 0){ 979 /* cname chain broken */ 980 return 0; 981 } 982 /* if this is a cname; move on */ 983 if(t == LDNS_RR_TYPE_CNAME) { 984 get_cname_target(rep->rrsets[i], &sname, &snamelen); 985 } 986 } 987 return 1; 988 } 989 990 int 991 reply_all_rrsets_secure(struct reply_info* rep) 992 { 993 size_t i; 994 for(i=0; i<rep->rrset_count; i++) { 995 if( ((struct packed_rrset_data*)rep->rrsets[i]->entry.data) 996 ->security != sec_status_secure ) 997 return 0; 998 } 999 return 1; 1000 } 1001 1002 struct reply_info* 1003 parse_reply_in_temp_region(sldns_buffer* pkt, struct regional* region, 1004 struct query_info* qi) 1005 { 1006 struct reply_info* rep; 1007 struct msg_parse* msg; 1008 if(!(msg = regional_alloc(region, sizeof(*msg)))) { 1009 return NULL; 1010 } 1011 memset(msg, 0, sizeof(*msg)); 1012 sldns_buffer_set_position(pkt, 0); 1013 if(parse_packet(pkt, msg, region) != 0){ 1014 return 0; 1015 } 1016 if(!parse_create_msg(pkt, msg, NULL, qi, &rep, region)) { 1017 return 0; 1018 } 1019 return rep; 1020 } 1021 1022 int edns_opt_list_append_ede(struct edns_option** list, struct regional* region, 1023 sldns_ede_code code, const char *txt) 1024 { 1025 struct edns_option** prevp; 1026 struct edns_option* opt; 1027 size_t txt_len = txt ? strlen(txt) : 0; 1028 1029 /* allocate new element */ 1030 opt = (struct edns_option*)regional_alloc(region, sizeof(*opt)); 1031 if(!opt) 1032 return 0; 1033 opt->next = NULL; 1034 opt->opt_code = LDNS_EDNS_EDE; 1035 opt->opt_len = txt_len + sizeof(uint16_t); 1036 opt->opt_data = regional_alloc(region, txt_len + sizeof(uint16_t)); 1037 if(!opt->opt_data) 1038 return 0; 1039 sldns_write_uint16(opt->opt_data, (uint16_t)code); 1040 if (txt_len) 1041 memmove(opt->opt_data + 2, txt, txt_len); 1042 1043 /* append at end of list */ 1044 prevp = list; 1045 while(*prevp != NULL) 1046 prevp = &((*prevp)->next); 1047 verbose(VERB_ALGO, "attached EDE code: %d with message: %s", code, (txt?txt:"\"\"")); 1048 *prevp = opt; 1049 return 1; 1050 } 1051 1052 int edns_opt_list_append_keepalive(struct edns_option** list, int msec, 1053 struct regional* region) 1054 { 1055 uint8_t data[2]; /* For keepalive value */ 1056 data[0] = (uint8_t)((msec >> 8) & 0xff); 1057 data[1] = (uint8_t)(msec & 0xff); 1058 return edns_opt_list_append(list, LDNS_EDNS_KEEPALIVE, sizeof(data), 1059 data, region); 1060 } 1061 1062 int edns_opt_list_append(struct edns_option** list, uint16_t code, size_t len, 1063 uint8_t* data, struct regional* region) 1064 { 1065 struct edns_option** prevp; 1066 struct edns_option* opt; 1067 1068 /* allocate new element */ 1069 opt = (struct edns_option*)regional_alloc(region, sizeof(*opt)); 1070 if(!opt) 1071 return 0; 1072 opt->next = NULL; 1073 opt->opt_code = code; 1074 opt->opt_len = len; 1075 opt->opt_data = NULL; 1076 if(len > 0) { 1077 opt->opt_data = regional_alloc_init(region, data, len); 1078 if(!opt->opt_data) 1079 return 0; 1080 } 1081 1082 /* append at end of list */ 1083 prevp = list; 1084 while(*prevp != NULL) { 1085 prevp = &((*prevp)->next); 1086 } 1087 *prevp = opt; 1088 return 1; 1089 } 1090 1091 int edns_opt_list_remove(struct edns_option** list, uint16_t code) 1092 { 1093 /* The list should already be allocated in a region. Freeing the 1094 * allocated space in a region is not possible. We just unlink the 1095 * required elements and they will be freed together with the region. */ 1096 1097 struct edns_option* prev; 1098 struct edns_option* curr; 1099 if(!list || !(*list)) return 0; 1100 1101 /* Unlink and repoint if the element(s) are first in list */ 1102 while(list && *list && (*list)->opt_code == code) { 1103 *list = (*list)->next; 1104 } 1105 1106 if(!list || !(*list)) return 1; 1107 /* Unlink elements and reattach the list */ 1108 prev = *list; 1109 curr = (*list)->next; 1110 while(curr != NULL) { 1111 if(curr->opt_code == code) { 1112 prev->next = curr->next; 1113 curr = curr->next; 1114 } else { 1115 prev = curr; 1116 curr = curr->next; 1117 } 1118 } 1119 return 1; 1120 } 1121 1122 static int inplace_cb_reply_call_generic( 1123 struct inplace_cb* callback_list, enum inplace_cb_list_type type, 1124 struct query_info* qinfo, struct module_qstate* qstate, 1125 struct reply_info* rep, int rcode, struct edns_data* edns, 1126 struct comm_reply* repinfo, struct regional* region, 1127 struct timeval* start_time) 1128 { 1129 struct inplace_cb* cb; 1130 struct edns_option* opt_list_out = NULL; 1131 #if defined(EXPORT_ALL_SYMBOLS) 1132 (void)type; /* param not used when fptr_ok disabled */ 1133 #endif 1134 if(qstate) 1135 opt_list_out = qstate->edns_opts_front_out; 1136 for(cb=callback_list; cb; cb=cb->next) { 1137 fptr_ok(fptr_whitelist_inplace_cb_reply_generic( 1138 (inplace_cb_reply_func_type*)cb->cb, type)); 1139 (void)(*(inplace_cb_reply_func_type*)cb->cb)(qinfo, qstate, rep, 1140 rcode, edns, &opt_list_out, repinfo, region, start_time, cb->id, cb->cb_arg); 1141 } 1142 edns->opt_list_inplace_cb_out = opt_list_out; 1143 return 1; 1144 } 1145 1146 int inplace_cb_reply_call(struct module_env* env, struct query_info* qinfo, 1147 struct module_qstate* qstate, struct reply_info* rep, int rcode, 1148 struct edns_data* edns, struct comm_reply* repinfo, struct regional* region, 1149 struct timeval* start_time) 1150 { 1151 return inplace_cb_reply_call_generic( 1152 env->inplace_cb_lists[inplace_cb_reply], inplace_cb_reply, qinfo, 1153 qstate, rep, rcode, edns, repinfo, region, start_time); 1154 } 1155 1156 int inplace_cb_reply_cache_call(struct module_env* env, 1157 struct query_info* qinfo, struct module_qstate* qstate, 1158 struct reply_info* rep, int rcode, struct edns_data* edns, 1159 struct comm_reply* repinfo, struct regional* region, 1160 struct timeval* start_time) 1161 { 1162 return inplace_cb_reply_call_generic( 1163 env->inplace_cb_lists[inplace_cb_reply_cache], inplace_cb_reply_cache, 1164 qinfo, qstate, rep, rcode, edns, repinfo, region, start_time); 1165 } 1166 1167 int inplace_cb_reply_local_call(struct module_env* env, 1168 struct query_info* qinfo, struct module_qstate* qstate, 1169 struct reply_info* rep, int rcode, struct edns_data* edns, 1170 struct comm_reply* repinfo, struct regional* region, 1171 struct timeval* start_time) 1172 { 1173 return inplace_cb_reply_call_generic( 1174 env->inplace_cb_lists[inplace_cb_reply_local], inplace_cb_reply_local, 1175 qinfo, qstate, rep, rcode, edns, repinfo, region, start_time); 1176 } 1177 1178 int inplace_cb_reply_servfail_call(struct module_env* env, 1179 struct query_info* qinfo, struct module_qstate* qstate, 1180 struct reply_info* rep, int rcode, struct edns_data* edns, 1181 struct comm_reply* repinfo, struct regional* region, 1182 struct timeval* start_time) 1183 { 1184 /* We are going to servfail. Remove any potential edns options. */ 1185 if(qstate) 1186 qstate->edns_opts_front_out = NULL; 1187 return inplace_cb_reply_call_generic( 1188 env->inplace_cb_lists[inplace_cb_reply_servfail], 1189 inplace_cb_reply_servfail, qinfo, qstate, rep, rcode, edns, repinfo, 1190 region, start_time); 1191 } 1192 1193 int inplace_cb_query_call(struct module_env* env, struct query_info* qinfo, 1194 uint16_t flags, struct sockaddr_storage* addr, socklen_t addrlen, 1195 uint8_t* zone, size_t zonelen, struct module_qstate* qstate, 1196 struct regional* region) 1197 { 1198 struct inplace_cb* cb = env->inplace_cb_lists[inplace_cb_query]; 1199 for(; cb; cb=cb->next) { 1200 fptr_ok(fptr_whitelist_inplace_cb_query( 1201 (inplace_cb_query_func_type*)cb->cb)); 1202 (void)(*(inplace_cb_query_func_type*)cb->cb)(qinfo, flags, 1203 qstate, addr, addrlen, zone, zonelen, region, 1204 cb->id, cb->cb_arg); 1205 } 1206 return 1; 1207 } 1208 1209 int inplace_cb_edns_back_parsed_call(struct module_env* env, 1210 struct module_qstate* qstate) 1211 { 1212 struct inplace_cb* cb = 1213 env->inplace_cb_lists[inplace_cb_edns_back_parsed]; 1214 for(; cb; cb=cb->next) { 1215 fptr_ok(fptr_whitelist_inplace_cb_edns_back_parsed( 1216 (inplace_cb_edns_back_parsed_func_type*)cb->cb)); 1217 (void)(*(inplace_cb_edns_back_parsed_func_type*)cb->cb)(qstate, 1218 cb->id, cb->cb_arg); 1219 } 1220 return 1; 1221 } 1222 1223 int inplace_cb_query_response_call(struct module_env* env, 1224 struct module_qstate* qstate, struct dns_msg* response) { 1225 struct inplace_cb* cb = 1226 env->inplace_cb_lists[inplace_cb_query_response]; 1227 for(; cb; cb=cb->next) { 1228 fptr_ok(fptr_whitelist_inplace_cb_query_response( 1229 (inplace_cb_query_response_func_type*)cb->cb)); 1230 (void)(*(inplace_cb_query_response_func_type*)cb->cb)(qstate, 1231 response, cb->id, cb->cb_arg); 1232 } 1233 return 1; 1234 } 1235 1236 struct edns_option* edns_opt_copy_region(struct edns_option* list, 1237 struct regional* region) 1238 { 1239 struct edns_option* result = NULL, *cur = NULL, *s; 1240 while(list) { 1241 /* copy edns option structure */ 1242 s = regional_alloc_init(region, list, sizeof(*list)); 1243 if(!s) return NULL; 1244 s->next = NULL; 1245 1246 /* copy option data */ 1247 if(s->opt_data) { 1248 s->opt_data = regional_alloc_init(region, s->opt_data, 1249 s->opt_len); 1250 if(!s->opt_data) 1251 return NULL; 1252 } 1253 1254 /* link into list */ 1255 if(cur) 1256 cur->next = s; 1257 else result = s; 1258 cur = s; 1259 1260 /* examine next element */ 1261 list = list->next; 1262 } 1263 return result; 1264 } 1265 1266 struct edns_option* edns_opt_copy_filter_region(struct edns_option* list, 1267 uint16_t* filter_list, size_t filter_list_len, struct regional* region) 1268 { 1269 struct edns_option* result = NULL, *cur = NULL, *s; 1270 size_t i; 1271 while(list) { 1272 for(i=0; i<filter_list_len; i++) 1273 if(filter_list[i] == list->opt_code) goto found; 1274 if(i == filter_list_len) goto next; 1275 found: 1276 /* copy edns option structure */ 1277 s = regional_alloc_init(region, list, sizeof(*list)); 1278 if(!s) return NULL; 1279 s->next = NULL; 1280 1281 /* copy option data */ 1282 if(s->opt_data) { 1283 s->opt_data = regional_alloc_init(region, s->opt_data, 1284 s->opt_len); 1285 if(!s->opt_data) 1286 return NULL; 1287 } 1288 1289 /* link into list */ 1290 if(cur) 1291 cur->next = s; 1292 else result = s; 1293 cur = s; 1294 1295 next: 1296 /* examine next element */ 1297 list = list->next; 1298 } 1299 return result; 1300 } 1301 1302 int edns_opt_compare(struct edns_option* p, struct edns_option* q) 1303 { 1304 if(!p && !q) return 0; 1305 if(!p) return -1; 1306 if(!q) return 1; 1307 log_assert(p && q); 1308 if(p->opt_code != q->opt_code) 1309 return (int)q->opt_code - (int)p->opt_code; 1310 if(p->opt_len != q->opt_len) 1311 return (int)q->opt_len - (int)p->opt_len; 1312 if(p->opt_len != 0) 1313 return memcmp(p->opt_data, q->opt_data, p->opt_len); 1314 return 0; 1315 } 1316 1317 int edns_opt_list_compare(struct edns_option* p, struct edns_option* q) 1318 { 1319 int r; 1320 while(p && q) { 1321 r = edns_opt_compare(p, q); 1322 if(r != 0) 1323 return r; 1324 p = p->next; 1325 q = q->next; 1326 } 1327 if(p || q) { 1328 /* uneven length lists */ 1329 if(p) return 1; 1330 if(q) return -1; 1331 } 1332 return 0; 1333 } 1334 1335 void edns_opt_list_free(struct edns_option* list) 1336 { 1337 struct edns_option* n; 1338 while(list) { 1339 free(list->opt_data); 1340 n = list->next; 1341 free(list); 1342 list = n; 1343 } 1344 } 1345 1346 struct edns_option* edns_opt_copy_alloc(struct edns_option* list) 1347 { 1348 struct edns_option* result = NULL, *cur = NULL, *s; 1349 while(list) { 1350 /* copy edns option structure */ 1351 s = memdup(list, sizeof(*list)); 1352 if(!s) { 1353 edns_opt_list_free(result); 1354 return NULL; 1355 } 1356 s->next = NULL; 1357 1358 /* copy option data */ 1359 if(s->opt_data) { 1360 s->opt_data = memdup(s->opt_data, s->opt_len); 1361 if(!s->opt_data) { 1362 free(s); 1363 edns_opt_list_free(result); 1364 return NULL; 1365 } 1366 } 1367 1368 /* link into list */ 1369 if(cur) 1370 cur->next = s; 1371 else result = s; 1372 cur = s; 1373 1374 /* examine next element */ 1375 list = list->next; 1376 } 1377 return result; 1378 } 1379 1380 struct edns_option* edns_opt_list_find(struct edns_option* list, uint16_t code) 1381 { 1382 struct edns_option* p; 1383 for(p=list; p; p=p->next) { 1384 if(p->opt_code == code) 1385 return p; 1386 } 1387 return NULL; 1388 } 1389