PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/nsd-3.2.12/query.c

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