1 /* 2 * query.c -- nsd(8) the resolver. 3 * 4 * Copyright (c) 2001-2006, NLnet Labs. All rights reserved. 5 * 6 * See LICENSE for the license. 7 * 8 */ 9 10 #include "config.h" 11 12 #include <sys/types.h> 13 #include <sys/socket.h> 14 #include <netinet/in.h> 15 #include <arpa/inet.h> 16 #include <assert.h> 17 #include <ctype.h> 18 #include <errno.h> 19 #include <limits.h> 20 #include <stddef.h> 21 #include <stdio.h> 22 #include <stdlib.h> 23 #include <string.h> 24 #include <time.h> 25 #include <unistd.h> 26 #include <netdb.h> 27 28 #include "answer.h" 29 #include "axfr.h" 30 #include "dns.h" 31 #include "dname.h" 32 #include "nsd.h" 33 #include "namedb.h" 34 #include "query.h" 35 #include "util.h" 36 #include "options.h" 37 #include "nsec3.h" 38 #include "tsig.h" 39 40 /* [Bug #253] Adding unnecessary NS RRset may lead to undesired truncation. 41 * This function determines if the final response packet needs the NS RRset 42 * included. Currently, it will only return negative if QTYPE == DNSKEY|DS. 43 * This way, resolvers won't fallback to TCP unnecessarily when priming 44 * trust anchors. 45 */ 46 static int answer_needs_ns(struct query *query); 47 48 static int add_rrset(struct query *query, 49 answer_type *answer, 50 rr_section_type section, 51 domain_type *owner, 52 rrset_type *rrset); 53 54 static void answer_authoritative(struct nsd *nsd, 55 struct query *q, 56 answer_type *answer, 57 size_t domain_number, 58 int exact, 59 domain_type *closest_match, 60 domain_type *closest_encloser, 61 const dname_type *qname); 62 63 static void answer_lookup_zone(struct nsd *nsd, struct query *q, 64 answer_type *answer, size_t domain_number, 65 int exact, domain_type *closest_match, 66 domain_type *closest_encloser, 67 const dname_type *qname); 68 69 void 70 query_put_dname_offset(struct query *q, domain_type *domain, uint16_t offset) 71 { 72 assert(q); 73 assert(domain); 74 assert(domain->number > 0); 75 76 if (offset > MAX_COMPRESSION_OFFSET) 77 return; 78 if (q->compressed_dname_count >= MAX_COMPRESSED_DNAMES) 79 return; 80 81 q->compressed_dname_offsets[domain->number] = offset; 82 q->compressed_dnames[q->compressed_dname_count] = domain; 83 ++q->compressed_dname_count; 84 } 85 86 void 87 query_clear_dname_offsets(struct query *q, size_t max_offset) 88 { 89 while (q->compressed_dname_count > 0 90 && (q->compressed_dname_offsets[q->compressed_dnames[q->compressed_dname_count - 1]->number] 91 >= max_offset)) 92 { 93 q->compressed_dname_offsets[q->compressed_dnames[q->compressed_dname_count - 1]->number] = 0; 94 --q->compressed_dname_count; 95 } 96 } 97 98 void 99 query_clear_compression_tables(struct query *q) 100 { 101 uint16_t i; 102 103 for (i = 0; i < q->compressed_dname_count; ++i) { 104 assert(q->compressed_dnames); 105 q->compressed_dname_offsets[q->compressed_dnames[i]->number] = 0; 106 } 107 q->compressed_dname_count = 0; 108 } 109 110 void 111 query_add_compression_domain(struct query *q, domain_type *domain, uint16_t offset) 112 { 113 while (domain->parent) { 114 DEBUG(DEBUG_NAME_COMPRESSION, 2, 115 (LOG_INFO, "query dname: %s, number: %lu, offset: %u\n", 116 domain_to_string(domain), 117 (unsigned long) domain->number, 118 offset)); 119 query_put_dname_offset(q, domain, offset); 120 offset += label_length(dname_name(domain_dname(domain))) + 1; 121 domain = domain->parent; 122 } 123 } 124 125 /* 126 * Generate an error response with the specified RCODE. 127 */ 128 query_state_type 129 query_error (struct query *q, nsd_rc_type rcode) 130 { 131 if (rcode == NSD_RC_DISCARD) { 132 return QUERY_DISCARDED; 133 } 134 135 buffer_clear(q->packet); 136 137 QR_SET(q->packet); /* This is an answer. */ 138 AD_CLR(q->packet); 139 RCODE_SET(q->packet, (int) rcode); /* Error code. */ 140 141 /* Truncate the question as well... */ 142 QDCOUNT_SET(q->packet, 0); 143 ANCOUNT_SET(q->packet, 0); 144 NSCOUNT_SET(q->packet, 0); 145 ARCOUNT_SET(q->packet, 0); 146 buffer_set_position(q->packet, QHEADERSZ); 147 return QUERY_PROCESSED; 148 } 149 150 static int 151 query_ratelimit_err(nsd_type* nsd) 152 { 153 time_t now = time(NULL); 154 if(nsd->err_limit_time == now) { 155 /* see if limit is exceeded for this second */ 156 if(nsd->err_limit_count++ > ERROR_RATELIMIT) 157 return 1; 158 } else { 159 /* new second, new limits */ 160 nsd->err_limit_time = now; 161 nsd->err_limit_count = 1; 162 } 163 return 0; 164 } 165 166 static query_state_type 167 query_formerr (struct query *query, nsd_type* nsd) 168 { 169 int opcode = OPCODE(query->packet); 170 if(query_ratelimit_err(nsd)) 171 return QUERY_DISCARDED; 172 FLAGS_SET(query->packet, FLAGS(query->packet) & 0x0100U); 173 /* Preserve the RD flag. Clear the rest. */ 174 OPCODE_SET(query->packet, opcode); 175 return query_error(query, NSD_RC_FORMAT); 176 } 177 178 static void 179 query_cleanup(void *data) 180 { 181 query_type *query = (query_type *) data; 182 region_destroy(query->region); 183 } 184 185 query_type * 186 query_create(region_type *region, uint16_t *compressed_dname_offsets, 187 size_t compressed_dname_size) 188 { 189 query_type *query 190 = (query_type *) region_alloc_zero(region, sizeof(query_type)); 191 /* create region with large block size, because the initial chunk 192 saves many mallocs in the server */ 193 query->region = region_create_custom(xalloc, free, 16384, 16384/8, 32, 0); 194 query->compressed_dname_offsets = compressed_dname_offsets; 195 query->packet = buffer_create(region, QIOBUFSZ); 196 region_add_cleanup(region, query_cleanup, query); 197 query->compressed_dname_offsets_size = compressed_dname_size; 198 tsig_create_record(&query->tsig, region); 199 query->tsig_prepare_it = 1; 200 query->tsig_update_it = 1; 201 query->tsig_sign_it = 1; 202 return query; 203 } 204 205 void 206 query_reset(query_type *q, size_t maxlen, int is_tcp) 207 { 208 /* 209 * As long as less than 4Kb (region block size) has been used, 210 * this call to free_all is free, the block is saved for re-use, 211 * so no malloc() or free() calls are done. 212 * at present use of the region is for: 213 * o query qname dname_type (255 max). 214 * o wildcard expansion domain_type (7*ptr+u32+2bytes)+(5*ptr nsec3) 215 * o wildcard expansion for additional section domain_type. 216 * o nsec3 hashed name(s) (3 dnames for a nonexist_proof, 217 * one proof per wildcard and for nx domain). 218 */ 219 region_free_all(q->region); 220 q->addrlen = sizeof(q->addr); 221 q->maxlen = maxlen; 222 q->reserved_space = 0; 223 buffer_clear(q->packet); 224 edns_init_record(&q->edns); 225 tsig_init_record(&q->tsig, NULL, NULL); 226 q->tsig_prepare_it = 1; 227 q->tsig_update_it = 1; 228 q->tsig_sign_it = 1; 229 q->tcp = is_tcp; 230 q->qname = NULL; 231 q->qtype = 0; 232 q->qclass = 0; 233 q->zone = NULL; 234 q->opcode = 0; 235 q->cname_count = 0; 236 q->delegation_domain = NULL; 237 q->delegation_rrset = NULL; 238 q->compressed_dname_count = 0; 239 q->number_temporary_domains = 0; 240 241 q->axfr_is_done = 0; 242 q->axfr_zone = NULL; 243 q->axfr_current_domain = NULL; 244 q->axfr_current_rrset = NULL; 245 q->axfr_current_rr = 0; 246 247 #ifdef RATELIMIT 248 q->wildcard_domain = NULL; 249 #endif 250 } 251 252 /* get a temporary domain number (or 0=failure) */ 253 static domain_type* 254 query_get_tempdomain(struct query *q) 255 { 256 static domain_type d[EXTRA_DOMAIN_NUMBERS]; 257 if(q->number_temporary_domains >= EXTRA_DOMAIN_NUMBERS) 258 return 0; 259 q->number_temporary_domains ++; 260 memset(&d[q->number_temporary_domains-1], 0, sizeof(domain_type)); 261 d[q->number_temporary_domains-1].number = q->compressed_dname_offsets_size + 262 q->number_temporary_domains - 1; 263 return &d[q->number_temporary_domains-1]; 264 } 265 266 static void 267 query_addtxt(struct query *q, 268 const uint8_t *dname, 269 uint16_t klass, 270 uint32_t ttl, 271 const char *txt) 272 { 273 size_t txt_length = strlen(txt); 274 uint8_t len = (uint8_t) txt_length; 275 276 assert(txt_length <= UCHAR_MAX); 277 278 /* Add the dname */ 279 if (dname >= buffer_begin(q->packet) 280 && dname <= buffer_current(q->packet)) 281 { 282 buffer_write_u16(q->packet, 283 0xc000 | (dname - buffer_begin(q->packet))); 284 } else { 285 buffer_write(q->packet, dname + 1, *dname); 286 } 287 288 buffer_write_u16(q->packet, TYPE_TXT); 289 buffer_write_u16(q->packet, klass); 290 buffer_write_u32(q->packet, ttl); 291 buffer_write_u16(q->packet, len + 1); 292 buffer_write_u8(q->packet, len); 293 buffer_write(q->packet, txt, len); 294 } 295 296 /* 297 * Parse the question section of a query. The normalized query name 298 * is stored in QUERY->name, the class in QUERY->klass, and the type 299 * in QUERY->type. 300 */ 301 static int 302 process_query_section(query_type *query) 303 { 304 uint8_t qnamebuf[MAXDOMAINLEN]; 305 306 buffer_set_position(query->packet, QHEADERSZ); 307 /* Lets parse the query name and convert it to lower case. */ 308 if(!packet_read_query_section(query->packet, qnamebuf, 309 &query->qtype, &query->qclass)) 310 return 0; 311 query->qname = dname_make(query->region, qnamebuf, 1); 312 return 1; 313 } 314 315 316 /* 317 * Process an optional EDNS OPT record. Sets QUERY->EDNS to 0 if 318 * there was no EDNS record, to -1 if there was an invalid or 319 * unsupported EDNS record, and to 1 otherwise. Updates QUERY->MAXLEN 320 * if the EDNS record specifies a maximum supported response length. 321 * 322 * Return NSD_RC_FORMAT on failure, NSD_RC_OK on success. 323 */ 324 static nsd_rc_type 325 process_edns(nsd_type* nsd, struct query *q) 326 { 327 if (q->edns.status == EDNS_ERROR) { 328 /* The only error is VERSION not implemented */ 329 return NSD_RC_FORMAT; 330 } 331 332 if (q->edns.status == EDNS_OK) { 333 /* Only care about UDP size larger than normal... */ 334 if (!q->tcp && q->edns.maxlen > UDP_MAX_MESSAGE_LEN) { 335 size_t edns_size; 336 #if defined(INET6) 337 if (q->addr.ss_family == AF_INET6) { 338 edns_size = nsd->ipv6_edns_size; 339 } else 340 #endif 341 edns_size = nsd->ipv4_edns_size; 342 343 if (q->edns.maxlen < edns_size) { 344 q->maxlen = q->edns.maxlen; 345 } else { 346 q->maxlen = edns_size; 347 } 348 349 #if defined(INET6) && !defined(IPV6_USE_MIN_MTU) && !defined(IPV6_MTU) 350 /* 351 * Use IPv6 minimum MTU to avoid sending 352 * packets that are too large for some links. 353 * IPv6 will not automatically fragment in 354 * this case (unlike IPv4). 355 */ 356 if (q->addr.ss_family == AF_INET6 357 && q->maxlen > IPV6_MIN_MTU) 358 { 359 q->maxlen = IPV6_MIN_MTU; 360 } 361 #endif 362 } 363 364 /* Strip the OPT resource record off... */ 365 buffer_set_position(q->packet, q->edns.position); 366 buffer_set_limit(q->packet, q->edns.position); 367 ARCOUNT_SET(q->packet, ARCOUNT(q->packet) - 1); 368 } 369 return NSD_RC_OK; 370 } 371 372 /* 373 * Processes TSIG. 374 * Sets error when tsig does not verify on the query. 375 */ 376 static nsd_rc_type 377 process_tsig(struct query* q) 378 { 379 if(q->tsig.status == TSIG_ERROR) 380 return NSD_RC_FORMAT; 381 if(q->tsig.status == TSIG_OK) { 382 if(!tsig_from_query(&q->tsig)) { 383 char a[128]; 384 addr2str(&q->addr, a, sizeof(a)); 385 log_msg(LOG_ERR, "query: bad tsig (%s) for key %s from %s", 386 tsig_error(q->tsig.error_code), 387 dname_to_string(q->tsig.key_name, NULL), a); 388 return NSD_RC_NOTAUTH; 389 } 390 buffer_set_limit(q->packet, q->tsig.position); 391 ARCOUNT_SET(q->packet, ARCOUNT(q->packet) - 1); 392 tsig_prepare(&q->tsig); 393 tsig_update(&q->tsig, q->packet, buffer_limit(q->packet)); 394 if(!tsig_verify(&q->tsig)) { 395 char a[128]; 396 addr2str(&q->addr, a, sizeof(a)); 397 log_msg(LOG_ERR, "query: bad tsig signature for key %s from %s", 398 dname_to_string(q->tsig.key->name, NULL), a); 399 return NSD_RC_NOTAUTH; 400 } 401 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "query good tsig signature for %s", 402 dname_to_string(q->tsig.key->name, NULL))); 403 } 404 return NSD_RC_OK; 405 } 406 407 /* 408 * Check notify acl and forward to xfrd (or return an error). 409 */ 410 static query_state_type 411 answer_notify(struct nsd* nsd, struct query *query) 412 { 413 int acl_num, acl_num_xfr; 414 acl_options_t *why; 415 nsd_rc_type rc; 416 417 zone_options_t* zone_opt; 418 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "got notify %s processing acl", 419 dname_to_string(query->qname, NULL))); 420 421 zone_opt = zone_options_find(nsd->options, query->qname); 422 if(!zone_opt) 423 return query_error(query, NSD_RC_NXDOMAIN); 424 425 if(!nsd->this_child) /* we are in debug mode or something */ 426 return query_error(query, NSD_RC_SERVFAIL); 427 428 if(!tsig_find_rr(&query->tsig, query->packet)) { 429 DEBUG(DEBUG_XFRD,2, (LOG_ERR, "bad tsig RR format")); 430 return query_error(query, NSD_RC_FORMAT); 431 } 432 rc = process_tsig(query); 433 if(rc != NSD_RC_OK) 434 return query_error(query, rc); 435 436 /* check if it passes acl */ 437 if((acl_num = acl_check_incoming(zone_opt->pattern->allow_notify, query, 438 &why)) != -1) 439 { 440 sig_atomic_t mode = NSD_PASS_TO_XFRD; 441 int s = nsd->this_child->parent_fd; 442 uint16_t sz; 443 uint32_t acl_send = htonl(acl_num); 444 uint32_t acl_xfr; 445 size_t pos; 446 447 /* Find priority candidate for request XFR. -1 if no match */ 448 acl_num_xfr = acl_check_incoming( 449 zone_opt->pattern->request_xfr, query, NULL); 450 451 acl_xfr = htonl(acl_num_xfr); 452 453 assert(why); 454 DEBUG(DEBUG_XFRD,1, (LOG_INFO, "got notify %s passed acl %s %s", 455 dname_to_string(query->qname, NULL), 456 why->ip_address_spec, 457 why->nokey?"NOKEY": 458 (why->blocked?"BLOCKED":why->key_name))); 459 sz = buffer_limit(query->packet); 460 if(buffer_limit(query->packet) > MAX_PACKET_SIZE) 461 return query_error(query, NSD_RC_SERVFAIL); 462 /* forward to xfrd for processing 463 Note. Blocking IPC I/O, but acl is OK. */ 464 sz = htons(sz); 465 if(!write_socket(s, &mode, sizeof(mode)) || 466 !write_socket(s, &sz, sizeof(sz)) || 467 !write_socket(s, buffer_begin(query->packet), 468 buffer_limit(query->packet)) || 469 !write_socket(s, &acl_send, sizeof(acl_send)) || 470 !write_socket(s, &acl_xfr, sizeof(acl_xfr))) { 471 log_msg(LOG_ERR, "error in IPC notify server2main, %s", 472 strerror(errno)); 473 return query_error(query, NSD_RC_SERVFAIL); 474 } 475 if(verbosity >= 1) { 476 uint32_t serial = 0; 477 char address[128]; 478 addr2str(&query->addr, address, sizeof(address)); 479 if(packet_find_notify_serial(query->packet, &serial)) 480 VERBOSITY(1, (LOG_INFO, "notify for %s from %s serial %u", 481 dname_to_string(query->qname, NULL), address, 482 (unsigned)serial)); 483 else 484 VERBOSITY(1, (LOG_INFO, "notify for %s from %s", 485 dname_to_string(query->qname, NULL), address)); 486 } 487 488 /* create notify reply - keep same query contents */ 489 QR_SET(query->packet); /* This is an answer. */ 490 AA_SET(query->packet); /* we are authoritative. */ 491 ANCOUNT_SET(query->packet, 0); 492 NSCOUNT_SET(query->packet, 0); 493 ARCOUNT_SET(query->packet, 0); 494 RCODE_SET(query->packet, RCODE_OK); /* Error code. */ 495 /* position is right after the query */ 496 pos = buffer_position(query->packet); 497 buffer_clear(query->packet); 498 buffer_set_position(query->packet, pos); 499 /* tsig is added in add_additional later (if needed) */ 500 return QUERY_PROCESSED; 501 } 502 503 if (verbosity >= 2) { 504 char address[128]; 505 addr2str(&query->addr, address, sizeof(address)); 506 VERBOSITY(2, (LOG_INFO, "notify for %s from %s refused, %s%s", 507 dname_to_string(query->qname, NULL), 508 address, 509 why?why->key_name:"no acl matches", 510 why?why->ip_address_spec:".")); 511 } 512 513 return query_error(query, NSD_RC_REFUSE); 514 } 515 516 517 /* 518 * Answer a query in the CHAOS class. 519 */ 520 static query_state_type 521 answer_chaos(struct nsd *nsd, query_type *q) 522 { 523 AA_CLR(q->packet); 524 switch (q->qtype) { 525 case TYPE_ANY: 526 case TYPE_TXT: 527 if ((q->qname->name_size == 11 528 && memcmp(dname_name(q->qname), "\002id\006server", 11) == 0) || 529 (q->qname->name_size == 15 530 && memcmp(dname_name(q->qname), "\010hostname\004bind", 15) == 0)) 531 { 532 /* Add ID */ 533 query_addtxt(q, 534 buffer_begin(q->packet) + QHEADERSZ, 535 CLASS_CH, 536 0, 537 nsd->identity); 538 ANCOUNT_SET(q->packet, ANCOUNT(q->packet) + 1); 539 } else if ((q->qname->name_size == 16 540 && memcmp(dname_name(q->qname), "\007version\006server", 16) == 0) || 541 (q->qname->name_size == 14 542 && memcmp(dname_name(q->qname), "\007version\004bind", 14) == 0)) 543 { 544 if(!nsd->options->hide_version) { 545 /* Add version */ 546 query_addtxt(q, 547 buffer_begin(q->packet) + QHEADERSZ, 548 CLASS_CH, 549 0, 550 nsd->version); 551 ANCOUNT_SET(q->packet, ANCOUNT(q->packet) + 1); 552 } else { 553 RCODE_SET(q->packet, RCODE_REFUSE); 554 } 555 } else { 556 RCODE_SET(q->packet, RCODE_REFUSE); 557 } 558 break; 559 default: 560 RCODE_SET(q->packet, RCODE_REFUSE); 561 break; 562 } 563 564 return QUERY_PROCESSED; 565 } 566 567 568 /* 569 * Find the covering NSEC for a non-existent domain name. Normally 570 * the NSEC will be located at CLOSEST_MATCH, except when it is an 571 * empty non-terminal. In this case the NSEC may be located at the 572 * previous domain name (in canonical ordering). 573 */ 574 static domain_type * 575 find_covering_nsec(domain_type *closest_match, 576 zone_type *zone, 577 rrset_type **nsec_rrset) 578 { 579 assert(closest_match); 580 assert(nsec_rrset); 581 582 /* loop away temporary created domains. For real ones it is &RBTREE_NULL */ 583 #ifdef USE_RADIX_TREE 584 while (closest_match->rnode == NULL) 585 #else 586 while (closest_match->node.parent == NULL) 587 #endif 588 closest_match = closest_match->parent; 589 while (closest_match) { 590 *nsec_rrset = domain_find_rrset(closest_match, zone, TYPE_NSEC); 591 if (*nsec_rrset) { 592 return closest_match; 593 } 594 if (closest_match == zone->apex) { 595 /* Don't look outside the current zone. */ 596 return NULL; 597 } 598 closest_match = domain_previous(closest_match); 599 } 600 return NULL; 601 } 602 603 604 struct additional_rr_types 605 { 606 uint16_t rr_type; 607 rr_section_type rr_section; 608 }; 609 610 struct additional_rr_types default_additional_rr_types[] = { 611 { TYPE_A, ADDITIONAL_A_SECTION }, 612 { TYPE_AAAA, ADDITIONAL_AAAA_SECTION }, 613 { 0, (rr_section_type) 0 } 614 }; 615 616 struct additional_rr_types swap_aaaa_additional_rr_types[] = { 617 { TYPE_AAAA, ADDITIONAL_A_SECTION }, 618 { TYPE_A, ADDITIONAL_AAAA_SECTION }, 619 { 0, (rr_section_type) 0 } 620 }; 621 622 struct additional_rr_types rt_additional_rr_types[] = { 623 { TYPE_A, ADDITIONAL_A_SECTION }, 624 { TYPE_AAAA, ADDITIONAL_AAAA_SECTION }, 625 { TYPE_X25, ADDITIONAL_OTHER_SECTION }, 626 { TYPE_ISDN, ADDITIONAL_OTHER_SECTION }, 627 { 0, (rr_section_type) 0 } 628 }; 629 630 static void 631 add_additional_rrsets(struct query *query, answer_type *answer, 632 rrset_type *master_rrset, size_t rdata_index, 633 int allow_glue, struct additional_rr_types types[]) 634 { 635 size_t i; 636 637 assert(query); 638 assert(answer); 639 assert(master_rrset); 640 assert(rdata_atom_is_domain(rrset_rrtype(master_rrset), rdata_index)); 641 642 for (i = 0; i < master_rrset->rr_count; ++i) { 643 int j; 644 domain_type *additional = rdata_atom_domain(master_rrset->rrs[i].rdatas[rdata_index]); 645 domain_type *match = additional; 646 647 assert(additional); 648 649 if (!allow_glue && domain_is_glue(match, query->zone)) 650 continue; 651 652 /* 653 * Check to see if we need to generate the dependent 654 * based on a wildcard domain. 655 */ 656 while (!match->is_existing) { 657 match = match->parent; 658 } 659 if (additional != match && domain_wildcard_child(match)) { 660 domain_type *wildcard_child = domain_wildcard_child(match); 661 domain_type *temp = (domain_type *) region_alloc( 662 query->region, sizeof(domain_type)); 663 #ifdef USE_RADIX_TREE 664 temp->rnode = NULL; 665 temp->dname = additional->dname; 666 #else 667 memcpy(&temp->node, &additional->node, sizeof(rbnode_t)); 668 temp->node.parent = NULL; 669 #endif 670 temp->number = additional->number; 671 temp->parent = match; 672 temp->wildcard_child_closest_match = temp; 673 temp->rrsets = wildcard_child->rrsets; 674 temp->is_existing = wildcard_child->is_existing; 675 additional = temp; 676 } 677 678 for (j = 0; types[j].rr_type != 0; ++j) { 679 rrset_type *rrset = domain_find_rrset( 680 additional, query->zone, types[j].rr_type); 681 if (rrset) { 682 answer_add_rrset(answer, types[j].rr_section, 683 additional, rrset); 684 } 685 } 686 } 687 } 688 689 static int 690 answer_needs_ns(struct query* query) 691 { 692 assert(query); 693 /* Currently, only troublesome for DNSKEY and DS, 694 * cuz their RRSETs are quite large. */ 695 return (query->qtype != TYPE_DNSKEY && query->qtype != TYPE_DS); 696 } 697 698 static int 699 add_rrset(struct query *query, 700 answer_type *answer, 701 rr_section_type section, 702 domain_type *owner, 703 rrset_type *rrset) 704 { 705 int result; 706 707 assert(query); 708 assert(answer); 709 assert(owner); 710 assert(rrset); 711 assert(rrset_rrclass(rrset) == CLASS_IN); 712 713 result = answer_add_rrset(answer, section, owner, rrset); 714 switch (rrset_rrtype(rrset)) { 715 case TYPE_NS: 716 #if defined(INET6) 717 /* if query over IPv6, swap A and AAAA; put AAAA first */ 718 add_additional_rrsets(query, answer, rrset, 0, 1, 719 (query->addr.ss_family == AF_INET6)? 720 swap_aaaa_additional_rr_types: 721 default_additional_rr_types); 722 #else 723 add_additional_rrsets(query, answer, rrset, 0, 1, 724 default_additional_rr_types); 725 #endif 726 break; 727 case TYPE_MB: 728 add_additional_rrsets(query, answer, rrset, 0, 0, 729 default_additional_rr_types); 730 break; 731 case TYPE_MX: 732 case TYPE_KX: 733 add_additional_rrsets(query, answer, rrset, 1, 0, 734 default_additional_rr_types); 735 break; 736 case TYPE_RT: 737 add_additional_rrsets(query, answer, rrset, 1, 0, 738 rt_additional_rr_types); 739 break; 740 case TYPE_SRV: 741 add_additional_rrsets(query, answer, rrset, 3, 0, 742 default_additional_rr_types); 743 break; 744 default: 745 break; 746 } 747 748 return result; 749 } 750 751 752 /* returns 0 on error, or the domain number for to_name. 753 from_name is changes to to_name by the DNAME rr. 754 DNAME rr is from src to dest. 755 closest encloser encloses the to_name. */ 756 static size_t 757 query_synthesize_cname(struct query* q, struct answer* answer, const dname_type* from_name, 758 const dname_type* to_name, domain_type* src, domain_type* to_closest_encloser, 759 domain_type** to_closest_match, uint32_t ttl) 760 { 761 /* add temporary domains for from_name and to_name and all 762 their (not allocated yet) parents */ 763 /* any domains below src are not_existing (because of DNAME at src) */ 764 int i; 765 domain_type* cname_domain; 766 domain_type* cname_dest; 767 rrset_type* rrset; 768 769 /* allocate source part */ 770 domain_type* lastparent = src; 771 assert(q && answer && from_name && to_name && src && to_closest_encloser); 772 assert(to_closest_match); 773 for(i=0; i < from_name->label_count - domain_dname(src)->label_count; i++) 774 { 775 domain_type* newdom = query_get_tempdomain(q); 776 if(!newdom) 777 return 0; 778 newdom->is_existing = 1; 779 newdom->parent = lastparent; 780 #ifdef USE_RADIX_TREE 781 newdom->dname 782 #else 783 newdom->node.key 784 #endif 785 = dname_partial_copy(q->region, 786 from_name, domain_dname(src)->label_count + i + 1); 787 if(dname_compare(domain_dname(newdom), q->qname) == 0) { 788 /* 0 good for query name, otherwise new number */ 789 newdom->number = 0; 790 } 791 DEBUG(DEBUG_QUERY,2, (LOG_INFO, "created temp domain src %d. %s nr %d", i, 792 domain_to_string(newdom), (int)newdom->number)); 793 lastparent = newdom; 794 } 795 cname_domain = lastparent; 796 797 /* allocate dest part */ 798 lastparent = to_closest_encloser; 799 for(i=0; i < to_name->label_count - domain_dname(to_closest_encloser)->label_count; 800 i++) 801 { 802 domain_type* newdom = query_get_tempdomain(q); 803 if(!newdom) 804 return 0; 805 newdom->is_existing = 0; 806 newdom->parent = lastparent; 807 #ifdef USE_RADIX_TREE 808 newdom->dname 809 #else 810 newdom->node.key 811 #endif 812 = dname_partial_copy(q->region, 813 to_name, domain_dname(to_closest_encloser)->label_count + i + 1); 814 DEBUG(DEBUG_QUERY,2, (LOG_INFO, "created temp domain dest %d. %s nr %d", i, 815 domain_to_string(newdom), (int)newdom->number)); 816 lastparent = newdom; 817 } 818 cname_dest = lastparent; 819 *to_closest_match = cname_dest; 820 821 /* allocate the CNAME RR */ 822 rrset = (rrset_type*) region_alloc(q->region, sizeof(rrset_type)); 823 memset(rrset, 0, sizeof(rrset_type)); 824 rrset->zone = q->zone; 825 rrset->rr_count = 1; 826 rrset->rrs = (rr_type*) region_alloc(q->region, sizeof(rr_type)); 827 memset(rrset->rrs, 0, sizeof(rr_type)); 828 rrset->rrs->owner = cname_domain; 829 rrset->rrs->ttl = ttl; 830 rrset->rrs->type = TYPE_CNAME; 831 rrset->rrs->klass = CLASS_IN; 832 rrset->rrs->rdata_count = 1; 833 rrset->rrs->rdatas = (rdata_atom_type*)region_alloc(q->region, 834 sizeof(rdata_atom_type)); 835 rrset->rrs->rdatas->domain = cname_dest; 836 837 if(!add_rrset(q, answer, ANSWER_SECTION, cname_domain, rrset)) { 838 log_msg(LOG_ERR, "could not add synthesized CNAME rrset to packet"); 839 } 840 841 return cname_dest->number; 842 } 843 844 /* 845 * Answer delegation information. 846 * 847 * DNSSEC: Include the DS RRset if present. Otherwise include an NSEC 848 * record proving the DS RRset does not exist. 849 */ 850 static void 851 answer_delegation(query_type *query, answer_type *answer) 852 { 853 assert(answer); 854 assert(query->delegation_domain); 855 assert(query->delegation_rrset); 856 857 if (query->cname_count == 0) { 858 AA_CLR(query->packet); 859 } else { 860 AA_SET(query->packet); 861 } 862 863 add_rrset(query, 864 answer, 865 AUTHORITY_SECTION, 866 query->delegation_domain, 867 query->delegation_rrset); 868 if (query->edns.dnssec_ok && zone_is_secure(query->zone)) { 869 rrset_type *rrset; 870 if ((rrset = domain_find_rrset(query->delegation_domain, query->zone, TYPE_DS))) { 871 add_rrset(query, answer, AUTHORITY_SECTION, 872 query->delegation_domain, rrset); 873 #ifdef NSEC3 874 } else if (query->zone->nsec3_param) { 875 nsec3_answer_delegation(query, answer); 876 #endif 877 } else if ((rrset = domain_find_rrset(query->delegation_domain, query->zone, TYPE_NSEC))) { 878 add_rrset(query, answer, AUTHORITY_SECTION, 879 query->delegation_domain, rrset); 880 } 881 } 882 } 883 884 885 /* 886 * Answer SOA information. 887 */ 888 static void 889 answer_soa(struct query *query, answer_type *answer) 890 { 891 if (query->qclass != CLASS_ANY) { 892 add_rrset(query, answer, 893 AUTHORITY_SECTION, 894 query->zone->apex, 895 query->zone->soa_nx_rrset); 896 } 897 } 898 899 900 /* 901 * Answer that the domain name exists but there is no RRset with the 902 * requested type. 903 * 904 * DNSSEC: Include the correct NSEC record proving that the type does 905 * not exist. In the wildcard no data (3.1.3.4) case the wildcard IS 906 * NOT expanded, so the ORIGINAL parameter must point to the original 907 * wildcard entry, not to the generated entry. 908 */ 909 static void 910 answer_nodata(struct query *query, answer_type *answer, domain_type *original) 911 { 912 if (query->cname_count == 0) { 913 answer_soa(query, answer); 914 } 915 916 #ifdef NSEC3 917 if (query->edns.dnssec_ok && query->zone->nsec3_param) { 918 nsec3_answer_nodata(query, answer, original); 919 } else 920 #endif 921 if (query->edns.dnssec_ok && zone_is_secure(query->zone)) { 922 domain_type *nsec_domain; 923 rrset_type *nsec_rrset; 924 925 nsec_domain = find_covering_nsec(original, query->zone, &nsec_rrset); 926 if (nsec_domain) { 927 add_rrset(query, answer, AUTHORITY_SECTION, nsec_domain, nsec_rrset); 928 } 929 } 930 } 931 932 static void 933 answer_nxdomain(query_type *query, answer_type *answer) 934 { 935 RCODE_SET(query->packet, RCODE_NXDOMAIN); 936 answer_soa(query, answer); 937 } 938 939 940 /* 941 * Answer domain information (or SOA if we do not have an RRset for 942 * the type specified by the query). 943 */ 944 static void 945 answer_domain(struct nsd* nsd, struct query *q, answer_type *answer, 946 domain_type *domain, domain_type *original) 947 { 948 rrset_type *rrset; 949 950 if (q->qtype == TYPE_ANY) { 951 int added = 0; 952 for (rrset = domain_find_any_rrset(domain, q->zone); rrset; rrset = rrset->next) { 953 if (rrset->zone == q->zone 954 #ifdef NSEC3 955 && rrset_rrtype(rrset) != TYPE_NSEC3 956 #endif 957 /* 958 * Don't include the RRSIG RRset when 959 * DNSSEC is used, because it is added 960 * automatically on an per-RRset basis. 961 */ 962 && !(q->edns.dnssec_ok 963 && zone_is_secure(q->zone) 964 && rrset_rrtype(rrset) == TYPE_RRSIG)) 965 { 966 add_rrset(q, answer, ANSWER_SECTION, domain, rrset); 967 ++added; 968 } 969 } 970 if (added == 0) { 971 answer_nodata(q, answer, original); 972 return; 973 } 974 #ifdef NSEC3 975 } else if (q->qtype == TYPE_NSEC3) { 976 answer_nodata(q, answer, original); 977 return; 978 #endif 979 } else if ((rrset = domain_find_rrset(domain, q->zone, q->qtype))) { 980 add_rrset(q, answer, ANSWER_SECTION, domain, rrset); 981 } else if ((rrset = domain_find_rrset(domain, q->zone, TYPE_CNAME))) { 982 int added; 983 984 /* 985 * If the CNAME is not added it is already in the 986 * answer, so we have a CNAME loop. Don't follow the 987 * CNAME target in this case. 988 */ 989 added = add_rrset(q, answer, ANSWER_SECTION, domain, rrset); 990 assert(rrset->rr_count > 0); 991 if (added) { 992 /* only process first CNAME record */ 993 domain_type *closest_match = rdata_atom_domain(rrset->rrs[0].rdatas[0]); 994 domain_type *closest_encloser = closest_match; 995 zone_type* origzone = q->zone; 996 ++q->cname_count; 997 998 answer_lookup_zone(nsd, q, answer, closest_match->number, 999 closest_match == closest_encloser, 1000 closest_match, closest_encloser, 1001 domain_dname(closest_match)); 1002 q->zone = origzone; 1003 } 1004 return; 1005 } else { 1006 answer_nodata(q, answer, original); 1007 return; 1008 } 1009 1010 if (q->qclass != CLASS_ANY && q->zone->ns_rrset && answer_needs_ns(q)) { 1011 add_rrset(q, answer, OPTIONAL_AUTHORITY_SECTION, q->zone->apex, 1012 q->zone->ns_rrset); 1013 } 1014 } 1015 1016 1017 /* 1018 * Answer with authoritative data. If a wildcard is matched the owner 1019 * name will be expanded to the domain name specified by 1020 * DOMAIN_NUMBER. DOMAIN_NUMBER 0 (zero) is reserved for the original 1021 * query name. 1022 * 1023 * DNSSEC: Include the necessary NSEC records in case the request 1024 * domain name does not exist and/or a wildcard match does not exist. 1025 */ 1026 static void 1027 answer_authoritative(struct nsd *nsd, 1028 struct query *q, 1029 answer_type *answer, 1030 size_t domain_number, 1031 int exact, 1032 domain_type *closest_match, 1033 domain_type *closest_encloser, 1034 const dname_type *qname) 1035 { 1036 domain_type *match; 1037 domain_type *original = closest_match; 1038 domain_type *dname_ce; 1039 rrset_type *rrset; 1040 1041 #ifdef NSEC3 1042 if(exact && domain_has_only_NSEC3(closest_match, q->zone)) { 1043 exact = 0; /* pretend it does not exist */ 1044 if(closest_encloser->parent) 1045 closest_encloser = closest_encloser->parent; 1046 } 1047 #endif /* NSEC3 */ 1048 if((dname_ce = find_dname_above(closest_encloser, q->zone)) != NULL) { 1049 /* occlude the found data, the DNAME is closest_encloser */ 1050 closest_encloser = dname_ce; 1051 exact = 0; 1052 } 1053 1054 if (exact) { 1055 match = closest_match; 1056 } else if ((rrset=domain_find_rrset(closest_encloser, q->zone, TYPE_DNAME))) { 1057 /* process DNAME */ 1058 const dname_type* name = qname; 1059 domain_type *dest = rdata_atom_domain(rrset->rrs[0].rdatas[0]); 1060 int added; 1061 assert(rrset->rr_count > 0); 1062 if(domain_number != 0) /* we followed CNAMEs or DNAMEs */ 1063 name = domain_dname(closest_match); 1064 DEBUG(DEBUG_QUERY,2, (LOG_INFO, "expanding DNAME for q=%s", dname_to_string(name, NULL))); 1065 DEBUG(DEBUG_QUERY,2, (LOG_INFO, "->src is %s", 1066 domain_to_string(closest_encloser))); 1067 DEBUG(DEBUG_QUERY,2, (LOG_INFO, "->dest is %s", 1068 domain_to_string(dest))); 1069 /* if the DNAME set is not added we have a loop, do not follow */ 1070 added = add_rrset(q, answer, ANSWER_SECTION, closest_encloser, rrset); 1071 if(added) { 1072 domain_type* src = closest_encloser; 1073 const dname_type* newname = dname_replace(q->region, name, 1074 domain_dname(src), domain_dname(dest)); 1075 size_t newnum = 0; 1076 zone_type* origzone = q->zone; 1077 ++q->cname_count; 1078 if(!newname) { /* newname too long */ 1079 RCODE_SET(q->packet, RCODE_YXDOMAIN); 1080 return; 1081 } 1082 DEBUG(DEBUG_QUERY,2, (LOG_INFO, "->result is %s", dname_to_string(newname, NULL))); 1083 /* follow the DNAME */ 1084 exact = namedb_lookup(nsd->db, newname, &closest_match, &closest_encloser); 1085 /* synthesize CNAME record */ 1086 newnum = query_synthesize_cname(q, answer, name, newname, 1087 src, closest_encloser, &closest_match, rrset->rrs[0].ttl); 1088 if(!newnum) { 1089 /* could not synthesize the CNAME. */ 1090 /* return previous CNAMEs to make resolver recurse for us */ 1091 return; 1092 } 1093 1094 answer_lookup_zone(nsd, q, answer, newnum, 1095 closest_match == closest_encloser, 1096 closest_match, closest_encloser, newname); 1097 q->zone = origzone; 1098 } 1099 if(!added) /* log the error so operator can find looping recursors */ 1100 log_msg(LOG_INFO, "DNAME processing stopped due to loop, qname %s", 1101 dname_to_string(q->qname, NULL)); 1102 return; 1103 } else if (domain_wildcard_child(closest_encloser)) { 1104 /* Generate the domain from the wildcard. */ 1105 domain_type *wildcard_child = domain_wildcard_child(closest_encloser); 1106 #ifdef RATELIMIT 1107 q->wildcard_domain = wildcard_child; 1108 #endif 1109 1110 match = (domain_type *) region_alloc(q->region, 1111 sizeof(domain_type)); 1112 #ifdef USE_RADIX_TREE 1113 match->rnode = NULL; 1114 match->dname = wildcard_child->dname; 1115 #else 1116 memcpy(&match->node, &wildcard_child->node, sizeof(rbnode_t)); 1117 match->node.parent = NULL; 1118 #endif 1119 match->parent = closest_encloser; 1120 match->wildcard_child_closest_match = match; 1121 match->number = domain_number; 1122 match->rrsets = wildcard_child->rrsets; 1123 match->is_existing = wildcard_child->is_existing; 1124 #ifdef NSEC3 1125 match->nsec3 = wildcard_child->nsec3; 1126 /* copy over these entries: 1127 match->nsec3_is_exact = wildcard_child->nsec3_is_exact; 1128 match->nsec3_cover = wildcard_child->nsec3_cover; 1129 match->nsec3_wcard_child_cover = wildcard_child->nsec3_wcard_child_cover; 1130 match->nsec3_ds_parent_is_exact = wildcard_child->nsec3_ds_parent_is_exact; 1131 match->nsec3_ds_parent_cover = wildcard_child->nsec3_ds_parent_cover; 1132 */ 1133 1134 if (q->edns.dnssec_ok && q->zone->nsec3_param) { 1135 /* Only add nsec3 wildcard data when do bit is set */ 1136 nsec3_answer_wildcard(q, answer, wildcard_child, qname); 1137 } 1138 #endif 1139 1140 /* 1141 * Remember the original domain in case a Wildcard No 1142 * Data (3.1.3.4) response needs to be generated. In 1143 * this particular case the wildcard IS NOT 1144 * expanded. 1145 */ 1146 original = wildcard_child; 1147 } else { 1148 match = NULL; 1149 } 1150 1151 /* Authoritative zone. */ 1152 #ifdef NSEC3 1153 if (q->edns.dnssec_ok && q->zone->nsec3_param) { 1154 nsec3_answer_authoritative(&match, q, answer, 1155 closest_encloser, qname); 1156 } else 1157 #endif 1158 if (q->edns.dnssec_ok && zone_is_secure(q->zone)) { 1159 if (match != closest_encloser) { 1160 domain_type *nsec_domain; 1161 rrset_type *nsec_rrset; 1162 1163 /* 1164 * No match found or generated from wildcard, 1165 * include NSEC record. 1166 */ 1167 nsec_domain = find_covering_nsec(closest_match, q->zone, &nsec_rrset); 1168 if (nsec_domain) { 1169 add_rrset(q, answer, AUTHORITY_SECTION, nsec_domain, nsec_rrset); 1170 } 1171 } 1172 if (!match) { 1173 domain_type *nsec_domain; 1174 rrset_type *nsec_rrset; 1175 1176 /* 1177 * No match and no wildcard. Include NSEC 1178 * proving there is no wildcard. 1179 */ 1180 nsec_domain = find_covering_nsec(closest_encloser->wildcard_child_closest_match, q->zone, &nsec_rrset); 1181 if (nsec_domain) { 1182 add_rrset(q, answer, AUTHORITY_SECTION, nsec_domain, nsec_rrset); 1183 } 1184 } 1185 } 1186 1187 #ifdef NSEC3 1188 if (RCODE(q->packet)!=RCODE_OK) { 1189 return; /* nsec3 collision failure */ 1190 } 1191 #endif 1192 if (match) { 1193 answer_domain(nsd, q, answer, match, original); 1194 } else { 1195 answer_nxdomain(q, answer); 1196 } 1197 } 1198 1199 /* 1200 * qname may be different after CNAMEs have been followed from query->qname. 1201 */ 1202 static void 1203 answer_lookup_zone(struct nsd *nsd, struct query *q, answer_type *answer, 1204 size_t domain_number, int exact, domain_type *closest_match, 1205 domain_type *closest_encloser, const dname_type *qname) 1206 { 1207 q->zone = domain_find_zone(nsd->db, closest_encloser); 1208 if (!q->zone) { 1209 /* no zone for this */ 1210 if(q->cname_count == 0) 1211 RCODE_SET(q->packet, RCODE_REFUSE); 1212 return; 1213 } 1214 if(!q->zone->apex || !q->zone->soa_rrset) { 1215 /* zone is configured but not loaded */ 1216 if(q->cname_count == 0) 1217 RCODE_SET(q->packet, RCODE_SERVFAIL); 1218 return; 1219 } 1220 /* now move up the closest encloser until it exists, previous 1221 * (possibly empty) closest encloser was useful to finding the zone 1222 * (for empty zones too), but now we want actual data nodes */ 1223 if (closest_encloser && !closest_encloser->is_existing) { 1224 exact = 0; 1225 while (closest_encloser != NULL && !closest_encloser->is_existing) 1226 closest_encloser = closest_encloser->parent; 1227 } 1228 1229 /* 1230 * See RFC 4035 (DNSSEC protocol) section 3.1.4.1 Responding 1231 * to Queries for DS RRs. 1232 */ 1233 if (exact && q->qtype == TYPE_DS && closest_encloser == q->zone->apex) { 1234 /* 1235 * Type DS query at a zone cut, use the responsible 1236 * parent zone to generate the answer if we are 1237 * authoritative for the parent zone. 1238 */ 1239 zone_type *zone = domain_find_parent_zone(nsd->db, q->zone); 1240 if (zone) 1241 q->zone = zone; 1242 } 1243 1244 /* see if the zone has expired (for secondary zones) */ 1245 if(q->zone && q->zone->opts && q->zone->opts->pattern && 1246 q->zone->opts->pattern->request_xfr != 0 && !q->zone->is_ok) { 1247 if(q->cname_count == 0) 1248 RCODE_SET(q->packet, RCODE_SERVFAIL); 1249 return; 1250 } 1251 1252 if (exact && q->qtype == TYPE_DS && closest_encloser == q->zone->apex) { 1253 /* 1254 * Type DS query at the zone apex (and the server is 1255 * not authoritative for the parent zone). 1256 */ 1257 if (q->qclass == CLASS_ANY) { 1258 AA_CLR(q->packet); 1259 } else { 1260 AA_SET(q->packet); 1261 } 1262 answer_nodata(q, answer, closest_encloser); 1263 } else { 1264 q->delegation_domain = domain_find_ns_rrsets( 1265 closest_encloser, q->zone, &q->delegation_rrset); 1266 if(q->delegation_domain && find_dname_above(q->delegation_domain, q->zone)) { 1267 q->delegation_domain = NULL; /* use higher DNAME */ 1268 } 1269 1270 if (!q->delegation_domain 1271 || (exact && q->qtype == TYPE_DS && closest_encloser == q->delegation_domain)) 1272 { 1273 if (q->qclass == CLASS_ANY) { 1274 AA_CLR(q->packet); 1275 } else { 1276 AA_SET(q->packet); 1277 } 1278 answer_authoritative(nsd, q, answer, domain_number, exact, 1279 closest_match, closest_encloser, qname); 1280 } 1281 else { 1282 answer_delegation(q, answer); 1283 } 1284 } 1285 } 1286 1287 static void 1288 answer_query(struct nsd *nsd, struct query *q) 1289 { 1290 domain_type *closest_match; 1291 domain_type *closest_encloser; 1292 int exact; 1293 uint16_t offset; 1294 answer_type answer; 1295 1296 answer_init(&answer); 1297 1298 exact = namedb_lookup(nsd->db, q->qname, &closest_match, &closest_encloser); 1299 1300 answer_lookup_zone(nsd, q, &answer, 0, exact, closest_match, 1301 closest_encloser, q->qname); 1302 ZTATUP2(nsd, q->zone, opcode, q->opcode); 1303 ZTATUP2(nsd, q->zone, qtype, q->qtype); 1304 ZTATUP2(nsd, q->zone, qclass, q->qclass); 1305 1306 offset = dname_label_offsets(q->qname)[domain_dname(closest_encloser)->label_count - 1] + QHEADERSZ; 1307 query_add_compression_domain(q, closest_encloser, offset); 1308 encode_answer(q, &answer); 1309 query_clear_compression_tables(q); 1310 } 1311 1312 void 1313 query_prepare_response(query_type *q) 1314 { 1315 uint16_t flags; 1316 1317 /* 1318 * Preserve the data up-to the current packet's limit. 1319 */ 1320 buffer_set_position(q->packet, buffer_limit(q->packet)); 1321 buffer_set_limit(q->packet, buffer_capacity(q->packet)); 1322 1323 /* 1324 * Reserve space for the EDNS records if required. 1325 */ 1326 q->reserved_space = edns_reserved_space(&q->edns); 1327 q->reserved_space += tsig_reserved_space(&q->tsig); 1328 1329 /* Update the flags. */ 1330 flags = FLAGS(q->packet); 1331 flags &= 0x0100U; /* Preserve the RD flag. */ 1332 /* CD flag must be cleared for auth answers */ 1333 flags |= 0x8000U; /* Set the QR flag. */ 1334 FLAGS_SET(q->packet, flags); 1335 } 1336 1337 /* 1338 * Processes the query. 1339 * 1340 */ 1341 query_state_type 1342 query_process(query_type *q, nsd_type *nsd) 1343 { 1344 /* The query... */ 1345 nsd_rc_type rc; 1346 query_state_type query_state; 1347 uint16_t arcount; 1348 1349 /* Sanity checks */ 1350 if (buffer_limit(q->packet) < QHEADERSZ) { 1351 /* packet too small to contain DNS header. 1352 Now packet investigation macros will work without problems. */ 1353 return QUERY_DISCARDED; 1354 } 1355 if (QR(q->packet)) { 1356 /* Not a query? Drop it on the floor. */ 1357 return QUERY_DISCARDED; 1358 } 1359 1360 /* check opcode early on, because new opcodes may have different 1361 * specification of the meaning of the rest of the packet */ 1362 q->opcode = OPCODE(q->packet); 1363 if(q->opcode != OPCODE_QUERY && q->opcode != OPCODE_NOTIFY) { 1364 if(query_ratelimit_err(nsd)) 1365 return QUERY_DISCARDED; 1366 return query_error(q, NSD_RC_IMPL); 1367 } 1368 1369 if (RCODE(q->packet) != RCODE_OK || !process_query_section(q)) { 1370 return query_formerr(q, nsd); 1371 } 1372 1373 /* Update statistics. */ 1374 STATUP2(nsd, opcode, q->opcode); 1375 STATUP2(nsd, qtype, q->qtype); 1376 STATUP2(nsd, qclass, q->qclass); 1377 1378 if (q->opcode != OPCODE_QUERY) { 1379 if (q->opcode == OPCODE_NOTIFY) { 1380 return answer_notify(nsd, q); 1381 } else { 1382 if(query_ratelimit_err(nsd)) 1383 return QUERY_DISCARDED; 1384 return query_error(q, NSD_RC_IMPL); 1385 } 1386 } 1387 1388 /* Dont bother to answer more than one question at once... */ 1389 if (QDCOUNT(q->packet) != 1) { 1390 FLAGS_SET(q->packet, 0); 1391 return query_formerr(q, nsd); 1392 } 1393 /* Ignore settings of flags */ 1394 1395 /* Dont allow any records in the answer or authority section... 1396 except for IXFR queries. */ 1397 if (ANCOUNT(q->packet) != 0 || 1398 (q->qtype!=TYPE_IXFR && NSCOUNT(q->packet) != 0)) { 1399 return query_formerr(q, nsd); 1400 } 1401 if(q->qtype==TYPE_IXFR && NSCOUNT(q->packet) > 0) { 1402 int i; /* skip ixfr soa information data here */ 1403 for(i=0; i< NSCOUNT(q->packet); i++) 1404 if(!packet_skip_rr(q->packet, 0)) 1405 return query_formerr(q, nsd); 1406 } 1407 1408 arcount = ARCOUNT(q->packet); 1409 if (arcount > 0) { 1410 /* According to draft-ietf-dnsext-rfc2671bis-edns0-10: 1411 * "The placement flexibility for the OPT RR does not 1412 * override the need for the TSIG or SIG(0) RRs to be 1413 * the last in the additional section whenever they are 1414 * present." 1415 * So we should not have to check for TSIG RR before 1416 * OPT RR. Keep the code for backwards compatibility. 1417 */ 1418 1419 /* see if tsig is before edns record */ 1420 if (!tsig_parse_rr(&q->tsig, q->packet)) 1421 return query_formerr(q, nsd); 1422 if(q->tsig.status != TSIG_NOT_PRESENT) 1423 --arcount; 1424 } 1425 /* See if there is an OPT RR. */ 1426 if (arcount > 0) { 1427 if (edns_parse_record(&q->edns, q->packet, q, nsd)) 1428 --arcount; 1429 } 1430 /* See if there is a TSIG RR. */ 1431 if (arcount > 0 && q->tsig.status == TSIG_NOT_PRESENT) { 1432 /* see if tsig is after the edns record */ 1433 if (!tsig_parse_rr(&q->tsig, q->packet)) 1434 return query_formerr(q, nsd); 1435 if(q->tsig.status != TSIG_NOT_PRESENT) 1436 --arcount; 1437 } 1438 /* If more RRs left in Add. Section, FORMERR. */ 1439 if (arcount > 0) { 1440 return query_formerr(q, nsd); 1441 } 1442 1443 /* Do we have any trailing garbage? */ 1444 #ifdef STRICT_MESSAGE_PARSE 1445 if (buffer_remaining(q->packet) > 0) { 1446 /* If we're strict.... */ 1447 return query_formerr(q, nsd); 1448 } 1449 #endif 1450 /* Remove trailing garbage. */ 1451 buffer_set_limit(q->packet, buffer_position(q->packet)); 1452 1453 rc = process_tsig(q); 1454 if (rc != NSD_RC_OK) { 1455 return query_error(q, rc); 1456 } 1457 rc = process_edns(nsd, q); 1458 if (rc != NSD_RC_OK) { 1459 /* We should not return FORMERR, but BADVERS (=16). 1460 * BADVERS is created with Ext. RCODE, followed by RCODE. 1461 * Ext. RCODE is set to 1, RCODE must be 0 (getting 0x10 = 16). 1462 * Thus RCODE = NOERROR = NSD_RC_OK. */ 1463 return query_error(q, NSD_RC_OK); 1464 } 1465 1466 query_prepare_response(q); 1467 1468 if (q->qclass != CLASS_IN && q->qclass != CLASS_ANY) { 1469 if (q->qclass == CLASS_CH) { 1470 return answer_chaos(nsd, q); 1471 } else { 1472 return query_error(q, NSD_RC_REFUSE); 1473 } 1474 } 1475 1476 query_state = answer_axfr_ixfr(nsd, q); 1477 if (query_state == QUERY_PROCESSED || query_state == QUERY_IN_AXFR) { 1478 return query_state; 1479 } 1480 1481 answer_query(nsd, q); 1482 1483 return QUERY_PROCESSED; 1484 } 1485 1486 void 1487 query_add_optional(query_type *q, nsd_type *nsd) 1488 { 1489 struct edns_data *edns = &nsd->edns_ipv4; 1490 #if defined(INET6) 1491 if (q->addr.ss_family == AF_INET6) { 1492 edns = &nsd->edns_ipv6; 1493 } 1494 #endif 1495 if (RCODE(q->packet) == RCODE_FORMAT) { 1496 return; 1497 } 1498 switch (q->edns.status) { 1499 case EDNS_NOT_PRESENT: 1500 break; 1501 case EDNS_OK: 1502 if (q->edns.dnssec_ok) edns->ok[7] = 0x80; 1503 else edns->ok[7] = 0x00; 1504 buffer_write(q->packet, edns->ok, OPT_LEN); 1505 if(q->edns.opt_reserved_space == 0 || !buffer_available( 1506 q->packet, 2+q->edns.opt_reserved_space)) { 1507 /* fill with NULLs */ 1508 buffer_write(q->packet, edns->rdata_none, OPT_RDATA); 1509 } else { 1510 /* rdata length */ 1511 buffer_write_u16(q->packet, q->edns.opt_reserved_space); 1512 /* edns options */ 1513 if(q->edns.nsid) { 1514 /* nsid opt header */ 1515 buffer_write(q->packet, edns->nsid, OPT_HDR); 1516 /* nsid payload */ 1517 buffer_write(q->packet, nsd->nsid, nsd->nsid_len); 1518 } 1519 } 1520 ARCOUNT_SET(q->packet, ARCOUNT(q->packet) + 1); 1521 STATUP(nsd, edns); 1522 ZTATUP(nsd, q->zone, edns); 1523 break; 1524 case EDNS_ERROR: 1525 if (q->edns.dnssec_ok) edns->error[7] = 0x80; 1526 else edns->error[7] = 0x00; 1527 buffer_write(q->packet, edns->error, OPT_LEN); 1528 buffer_write(q->packet, edns->rdata_none, OPT_RDATA); 1529 ARCOUNT_SET(q->packet, ARCOUNT(q->packet) + 1); 1530 STATUP(nsd, ednserr); 1531 ZTATUP(nsd, q->zone, ednserr); 1532 break; 1533 } 1534 1535 if (q->tsig.status != TSIG_NOT_PRESENT) { 1536 if (q->tsig.status == TSIG_ERROR || 1537 q->tsig.error_code != TSIG_ERROR_NOERROR) { 1538 tsig_error_reply(&q->tsig); 1539 tsig_append_rr(&q->tsig, q->packet); 1540 ARCOUNT_SET(q->packet, ARCOUNT(q->packet) + 1); 1541 } else if(q->tsig.status == TSIG_OK && 1542 q->tsig.error_code == TSIG_ERROR_NOERROR) 1543 { 1544 if(q->tsig_prepare_it) 1545 tsig_prepare(&q->tsig); 1546 if(q->tsig_update_it) 1547 tsig_update(&q->tsig, q->packet, buffer_position(q->packet)); 1548 if(q->tsig_sign_it) { 1549 tsig_sign(&q->tsig); 1550 tsig_append_rr(&q->tsig, q->packet); 1551 ARCOUNT_SET(q->packet, ARCOUNT(q->packet) + 1); 1552 } 1553 } 1554 } 1555 } 1556