PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/usr.sbin/nsd/query.c

https://bitbucket.org/pythonos/pythonos
C | 1433 lines | 1079 code | 137 blank | 217 comment | 287 complexity | b6582e710338864f39ab7853703c8c32 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, GPL-3.0, AGPL-1.0, MIT, ISC, LGPL-2.1, GPL-2.0, BSD-3-Clause, 0BSD
  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. return NSD_RC_FORMAT;
  277. }
  278. if (q->edns.status == EDNS_OK) {
  279. /* Only care about UDP size larger than normal... */
  280. if (!q->tcp && q->edns.maxlen > UDP_MAX_MESSAGE_LEN) {
  281. size_t edns_size;
  282. #if defined(INET6)
  283. if (q->addr.ss_family == AF_INET6) {
  284. edns_size = nsd->ipv6_edns_size;
  285. } else
  286. #endif
  287. edns_size = nsd->ipv4_edns_size;
  288. if (q->edns.maxlen < edns_size) {
  289. q->maxlen = q->edns.maxlen;
  290. } else {
  291. q->maxlen = edns_size;
  292. }
  293. #if defined(INET6) && !defined(IPV6_USE_MIN_MTU) && !defined(IPV6_MTU)
  294. /*
  295. * Use IPv6 minimum MTU to avoid sending
  296. * packets that are too large for some links.
  297. * IPv6 will not automatically fragment in
  298. * this case (unlike IPv4).
  299. */
  300. if (q->addr.ss_family == AF_INET6
  301. && q->maxlen > IPV6_MIN_MTU)
  302. {
  303. q->maxlen = IPV6_MIN_MTU;
  304. }
  305. #endif
  306. }
  307. /* Strip the OPT resource record off... */
  308. buffer_set_position(q->packet, q->edns.position);
  309. buffer_set_limit(q->packet, q->edns.position);
  310. ARCOUNT_SET(q->packet, ARCOUNT(q->packet) - 1);
  311. }
  312. return NSD_RC_OK;
  313. }
  314. /*
  315. * Processes TSIG.
  316. * Sets error when tsig does not verify on the query.
  317. */
  318. static nsd_rc_type
  319. process_tsig(struct query* q)
  320. {
  321. if(q->tsig.status == TSIG_ERROR)
  322. return NSD_RC_FORMAT;
  323. if(q->tsig.status == TSIG_OK) {
  324. if(!tsig_from_query(&q->tsig)) {
  325. log_msg(LOG_ERR, "query tsig unknown key/algorithm");
  326. return NSD_RC_REFUSE;
  327. }
  328. buffer_set_limit(q->packet, q->tsig.position);
  329. ARCOUNT_SET(q->packet, ARCOUNT(q->packet) - 1);
  330. tsig_prepare(&q->tsig);
  331. tsig_update(&q->tsig, q->packet, buffer_limit(q->packet));
  332. if(!tsig_verify(&q->tsig)) {
  333. log_msg(LOG_ERR, "query: bad tsig signature for key %s",
  334. dname_to_string(q->tsig.key->name, NULL));
  335. return NSD_RC_REFUSE;
  336. }
  337. DEBUG(DEBUG_XFRD,1, (LOG_INFO, "query good tsig signature for %s",
  338. dname_to_string(q->tsig.key->name, NULL)));
  339. }
  340. return NSD_RC_OK;
  341. }
  342. /*
  343. * Check notify acl and forward to xfrd (or return an error).
  344. */
  345. static query_state_type
  346. answer_notify(struct nsd* nsd, struct query *query)
  347. {
  348. int acl_num;
  349. acl_options_t *why;
  350. nsd_rc_type rc;
  351. zone_options_t* zone_opt;
  352. DEBUG(DEBUG_XFRD,1, (LOG_INFO, "got notify %s processing acl",
  353. dname_to_string(query->qname, NULL)));
  354. zone_opt = zone_options_find(nsd->options, query->qname);
  355. if(!zone_opt)
  356. return query_error(query, NSD_RC_NXDOMAIN);
  357. if(!nsd->this_child) /* we are in debug mode or something */
  358. return query_error(query, NSD_RC_SERVFAIL);
  359. if(!tsig_find_rr(&query->tsig, query->packet)) {
  360. DEBUG(DEBUG_XFRD,2, (LOG_ERR, "bad tsig RR format"));
  361. return query_error(query, NSD_RC_FORMAT);
  362. }
  363. rc = process_tsig(query);
  364. if(rc != NSD_RC_OK)
  365. return query_error(query, rc);
  366. /* check if it passes acl */
  367. if((acl_num = acl_check_incoming(zone_opt->allow_notify, query,
  368. &why)) != -1)
  369. {
  370. sig_atomic_t mode = NSD_PASS_TO_XFRD;
  371. int s = nsd->this_child->parent_fd;
  372. uint16_t sz;
  373. uint32_t acl_send = htonl(acl_num);
  374. size_t pos;
  375. assert(why);
  376. DEBUG(DEBUG_XFRD,1, (LOG_INFO, "got notify %s passed acl %s %s",
  377. dname_to_string(query->qname, NULL),
  378. why->ip_address_spec,
  379. why->nokey?"NOKEY":
  380. (why->blocked?"BLOCKED":why->key_name)));
  381. sz = buffer_limit(query->packet);
  382. if(buffer_limit(query->packet) > MAX_PACKET_SIZE)
  383. return query_error(query, NSD_RC_SERVFAIL);
  384. /* forward to xfrd for processing
  385. Note. Blocking IPC I/O, but acl is OK. */
  386. sz = htons(sz);
  387. if(!write_socket(s, &mode, sizeof(mode)) ||
  388. !write_socket(s, &sz, sizeof(sz)) ||
  389. !write_socket(s, buffer_begin(query->packet),
  390. buffer_limit(query->packet)) ||
  391. !write_socket(s, &acl_send, sizeof(acl_send))) {
  392. log_msg(LOG_ERR, "error in IPC notify server2main, %s",
  393. strerror(errno));
  394. return query_error(query, NSD_RC_SERVFAIL);
  395. }
  396. /* create notify reply - keep same query contents */
  397. QR_SET(query->packet); /* This is an answer. */
  398. AA_SET(query->packet); /* we are authoritative. */
  399. ANCOUNT_SET(query->packet, 0);
  400. NSCOUNT_SET(query->packet, 0);
  401. ARCOUNT_SET(query->packet, 0);
  402. RCODE_SET(query->packet, RCODE_OK); /* Error code. */
  403. /* position is right after the query */
  404. pos = buffer_position(query->packet);
  405. buffer_clear(query->packet);
  406. buffer_set_position(query->packet, pos);
  407. VERBOSITY(2, (LOG_INFO, "Notify received and accepted, forward to xfrd"));
  408. /* tsig is added in add_additional later (if needed) */
  409. return QUERY_PROCESSED;
  410. }
  411. if (verbosity > 1) {
  412. char address[128];
  413. if (addr2ip(query->addr, address, sizeof(address))) {
  414. DEBUG(DEBUG_XFRD,1, (LOG_INFO, "addr2ip failed"));
  415. strlcpy(address, "[unknown]", sizeof(address));
  416. }
  417. VERBOSITY(1, (LOG_INFO, "notify for zone %s from client %s refused, %s%s",
  418. dname_to_string(query->qname, NULL),
  419. address,
  420. why?why->key_name:"no acl matches",
  421. why?why->ip_address_spec:"."));
  422. }
  423. return query_error(query, NSD_RC_REFUSE);
  424. }
  425. /*
  426. * Answer a query in the CHAOS class.
  427. */
  428. static query_state_type
  429. answer_chaos(struct nsd *nsd, query_type *q)
  430. {
  431. AA_CLR(q->packet);
  432. switch (q->qtype) {
  433. case TYPE_ANY:
  434. case TYPE_TXT:
  435. if ((q->qname->name_size == 11
  436. && memcmp(dname_name(q->qname), "\002id\006server", 11) == 0) ||
  437. (q->qname->name_size == 15
  438. && memcmp(dname_name(q->qname), "\010hostname\004bind", 15) == 0))
  439. {
  440. /* Add ID */
  441. query_addtxt(q,
  442. buffer_begin(q->packet) + QHEADERSZ,
  443. CLASS_CH,
  444. 0,
  445. nsd->identity);
  446. ANCOUNT_SET(q->packet, ANCOUNT(q->packet) + 1);
  447. } else if ((q->qname->name_size == 16
  448. && memcmp(dname_name(q->qname), "\007version\006server", 16) == 0) ||
  449. (q->qname->name_size == 14
  450. && memcmp(dname_name(q->qname), "\007version\004bind", 14) == 0))
  451. {
  452. if(!nsd->options->hide_version) {
  453. /* Add version */
  454. query_addtxt(q,
  455. buffer_begin(q->packet) + QHEADERSZ,
  456. CLASS_CH,
  457. 0,
  458. nsd->version);
  459. ANCOUNT_SET(q->packet, ANCOUNT(q->packet) + 1);
  460. } else {
  461. RCODE_SET(q->packet, RCODE_REFUSE);
  462. }
  463. }
  464. break;
  465. default:
  466. RCODE_SET(q->packet, RCODE_REFUSE);
  467. break;
  468. }
  469. return QUERY_PROCESSED;
  470. }
  471. /*
  472. * Find the covering NSEC for a non-existent domain name. Normally
  473. * the NSEC will be located at CLOSEST_MATCH, except when it is an
  474. * empty non-terminal. In this case the NSEC may be located at the
  475. * previous domain name (in canonical ordering).
  476. */
  477. static domain_type *
  478. find_covering_nsec(domain_type *closest_match,
  479. zone_type *zone,
  480. rrset_type **nsec_rrset)
  481. {
  482. assert(closest_match);
  483. assert(nsec_rrset);
  484. /* loop away temporary created domains. For real ones it is &RBTREE_NULL */
  485. while (closest_match->node.parent == NULL)
  486. closest_match = closest_match->parent;
  487. while (closest_match) {
  488. *nsec_rrset = domain_find_rrset(closest_match, zone, TYPE_NSEC);
  489. if (*nsec_rrset) {
  490. return closest_match;
  491. }
  492. if (closest_match == zone->apex) {
  493. /* Don't look outside the current zone. */
  494. return NULL;
  495. }
  496. closest_match = domain_previous(closest_match);
  497. }
  498. return NULL;
  499. }
  500. struct additional_rr_types
  501. {
  502. uint16_t rr_type;
  503. rr_section_type rr_section;
  504. };
  505. struct additional_rr_types default_additional_rr_types[] = {
  506. { TYPE_A, ADDITIONAL_A_SECTION },
  507. { TYPE_AAAA, ADDITIONAL_AAAA_SECTION },
  508. { 0, (rr_section_type) 0 }
  509. };
  510. struct additional_rr_types rt_additional_rr_types[] = {
  511. { TYPE_A, ADDITIONAL_A_SECTION },
  512. { TYPE_AAAA, ADDITIONAL_AAAA_SECTION },
  513. { TYPE_X25, ADDITIONAL_OTHER_SECTION },
  514. { TYPE_ISDN, ADDITIONAL_OTHER_SECTION },
  515. { 0, (rr_section_type) 0 }
  516. };
  517. static void
  518. add_additional_rrsets(struct query *query, answer_type *answer,
  519. rrset_type *master_rrset, size_t rdata_index,
  520. int allow_glue, struct additional_rr_types types[])
  521. {
  522. size_t i;
  523. assert(query);
  524. assert(answer);
  525. assert(master_rrset);
  526. assert(rdata_atom_is_domain(rrset_rrtype(master_rrset), rdata_index));
  527. for (i = 0; i < master_rrset->rr_count; ++i) {
  528. int j;
  529. domain_type *additional = rdata_atom_domain(master_rrset->rrs[i].rdatas[rdata_index]);
  530. domain_type *match = additional;
  531. assert(additional);
  532. if (!allow_glue && domain_is_glue(match, query->zone))
  533. continue;
  534. /*
  535. * Check to see if we need to generate the dependent
  536. * based on a wildcard domain.
  537. */
  538. while (!match->is_existing) {
  539. match = match->parent;
  540. }
  541. if (additional != match && domain_wildcard_child(match)) {
  542. domain_type *wildcard_child = domain_wildcard_child(match);
  543. domain_type *temp = (domain_type *) region_alloc(
  544. query->region, sizeof(domain_type));
  545. memcpy(&temp->node, &additional->node, sizeof(rbnode_t));
  546. temp->number = additional->number;
  547. temp->parent = match;
  548. temp->wildcard_child_closest_match = temp;
  549. temp->rrsets = wildcard_child->rrsets;
  550. temp->is_existing = wildcard_child->is_existing;
  551. additional = temp;
  552. }
  553. for (j = 0; types[j].rr_type != 0; ++j) {
  554. rrset_type *rrset = domain_find_rrset(
  555. additional, query->zone, types[j].rr_type);
  556. if (rrset) {
  557. answer_add_rrset(answer, types[j].rr_section,
  558. additional, rrset);
  559. }
  560. }
  561. }
  562. }
  563. static int
  564. answer_needs_ns(struct query* query)
  565. {
  566. assert(query);
  567. /* Currently, only troublesome for DNSKEY and DS,
  568. * cuz their RRSETs are quite large. */
  569. return (query->qtype != TYPE_DNSKEY && query->qtype != TYPE_DS);
  570. }
  571. static int
  572. add_rrset(struct query *query,
  573. answer_type *answer,
  574. rr_section_type section,
  575. domain_type *owner,
  576. rrset_type *rrset)
  577. {
  578. int result;
  579. assert(query);
  580. assert(answer);
  581. assert(owner);
  582. assert(rrset);
  583. assert(rrset_rrclass(rrset) == CLASS_IN);
  584. result = answer_add_rrset(answer, section, owner, rrset);
  585. switch (rrset_rrtype(rrset)) {
  586. case TYPE_NS:
  587. add_additional_rrsets(query, answer, rrset, 0, 1,
  588. default_additional_rr_types);
  589. break;
  590. case TYPE_MB:
  591. add_additional_rrsets(query, answer, rrset, 0, 0,
  592. default_additional_rr_types);
  593. break;
  594. case TYPE_MX:
  595. case TYPE_KX:
  596. add_additional_rrsets(query, answer, rrset, 1, 0,
  597. default_additional_rr_types);
  598. break;
  599. case TYPE_RT:
  600. add_additional_rrsets(query, answer, rrset, 1, 0,
  601. rt_additional_rr_types);
  602. break;
  603. default:
  604. break;
  605. }
  606. return result;
  607. }
  608. /* returns 0 on error, or the domain number for to_name.
  609. from_name is changes to to_name by the DNAME rr.
  610. DNAME rr is from src to dest.
  611. closest encloser encloses the to_name. */
  612. static uint32_t
  613. query_synthesize_cname(struct query* q, struct answer* answer, const dname_type* from_name,
  614. const dname_type* to_name, domain_type* src, domain_type* to_closest_encloser,
  615. domain_type** to_closest_match)
  616. {
  617. /* add temporary domains for from_name and to_name and all
  618. their (not allocated yet) parents */
  619. /* any domains below src are not_existing (because of DNAME at src) */
  620. int i;
  621. domain_type* cname_domain;
  622. domain_type* cname_dest;
  623. rrset_type* rrset;
  624. /* allocate source part */
  625. domain_type* lastparent = src;
  626. assert(q && answer && from_name && to_name && src && to_closest_encloser);
  627. assert(to_closest_match);
  628. for(i=0; i < from_name->label_count - domain_dname(src)->label_count; i++)
  629. {
  630. domain_type* newdom = query_get_tempdomain(q);
  631. if(!newdom)
  632. return 0;
  633. newdom->is_existing = 1;
  634. newdom->parent = lastparent;
  635. newdom->node.key = dname_partial_copy(q->region,
  636. from_name, domain_dname(src)->label_count + i + 1);
  637. if(dname_compare(domain_dname(newdom), q->qname) == 0) {
  638. /* 0 good for query name, otherwise new number */
  639. newdom->number = 0;
  640. }
  641. DEBUG(DEBUG_QUERY,2, (LOG_INFO, "created temp domain src %d. %s nr %d", i,
  642. dname_to_string(domain_dname(newdom), NULL),
  643. newdom->number));
  644. lastparent = newdom;
  645. }
  646. cname_domain = lastparent;
  647. /* allocate dest part */
  648. lastparent = to_closest_encloser;
  649. for(i=0; i < to_name->label_count - domain_dname(to_closest_encloser)->label_count;
  650. i++)
  651. {
  652. domain_type* newdom = query_get_tempdomain(q);
  653. if(!newdom)
  654. return 0;
  655. newdom->is_existing = 0;
  656. newdom->parent = lastparent;
  657. newdom->node.key = dname_partial_copy(q->region,
  658. to_name, domain_dname(to_closest_encloser)->label_count + i + 1);
  659. DEBUG(DEBUG_QUERY,2, (LOG_INFO, "created temp domain dest %d. %s nr %d", i,
  660. dname_to_string(domain_dname(newdom), NULL),
  661. newdom->number));
  662. lastparent = newdom;
  663. }
  664. cname_dest = lastparent;
  665. *to_closest_match = cname_dest;
  666. /* allocate the CNAME RR */
  667. rrset = (rrset_type*) region_alloc(q->region, sizeof(rrset_type));
  668. memset(rrset, 0, sizeof(rrset_type));
  669. rrset->zone = q->zone;
  670. rrset->rr_count = 1;
  671. rrset->rrs = (rr_type*) region_alloc(q->region, sizeof(rr_type));
  672. memset(rrset->rrs, 0, sizeof(rr_type));
  673. rrset->rrs->owner = cname_domain;
  674. rrset->rrs->ttl = 0;
  675. rrset->rrs->type = TYPE_CNAME;
  676. rrset->rrs->klass = CLASS_IN;
  677. rrset->rrs->rdata_count = 1;
  678. rrset->rrs->rdatas = (rdata_atom_type*)region_alloc(q->region,
  679. sizeof(rdata_atom_type));
  680. rrset->rrs->rdatas->domain = cname_dest;
  681. if(!add_rrset(q, answer, ANSWER_SECTION, cname_domain, rrset)) {
  682. log_msg(LOG_ERR, "could not add synthesized CNAME rrset to packet");
  683. }
  684. return cname_dest->number;
  685. }
  686. /*
  687. * Answer delegation information.
  688. *
  689. * DNSSEC: Include the DS RRset if present. Otherwise include an NSEC
  690. * record proving the DS RRset does not exist.
  691. */
  692. static void
  693. answer_delegation(query_type *query, answer_type *answer)
  694. {
  695. assert(answer);
  696. assert(query->delegation_domain);
  697. assert(query->delegation_rrset);
  698. if (query->cname_count == 0) {
  699. AA_CLR(query->packet);
  700. } else {
  701. AA_SET(query->packet);
  702. }
  703. add_rrset(query,
  704. answer,
  705. AUTHORITY_SECTION,
  706. query->delegation_domain,
  707. query->delegation_rrset);
  708. if (query->edns.dnssec_ok && zone_is_secure(query->zone)) {
  709. rrset_type *rrset;
  710. if ((rrset = domain_find_rrset(query->delegation_domain, query->zone, TYPE_DS))) {
  711. add_rrset(query, answer, AUTHORITY_SECTION,
  712. query->delegation_domain, rrset);
  713. #ifdef NSEC3
  714. } else if (query->zone->nsec3_soa_rr) {
  715. nsec3_answer_delegation(query, answer);
  716. #endif
  717. } else if ((rrset = domain_find_rrset(query->delegation_domain, query->zone, TYPE_NSEC))) {
  718. add_rrset(query, answer, AUTHORITY_SECTION,
  719. query->delegation_domain, rrset);
  720. }
  721. }
  722. query->domain = query->delegation_domain;
  723. }
  724. /*
  725. * Answer SOA information.
  726. */
  727. static void
  728. answer_soa(struct query *query, answer_type *answer)
  729. {
  730. query->domain = query->zone->apex;
  731. if (query->qclass != CLASS_ANY) {
  732. add_rrset(query, answer,
  733. AUTHORITY_SECTION,
  734. query->zone->apex,
  735. query->zone->soa_nx_rrset);
  736. }
  737. }
  738. /*
  739. * Answer that the domain name exists but there is no RRset with the
  740. * requested type.
  741. *
  742. * DNSSEC: Include the correct NSEC record proving that the type does
  743. * not exist. In the wildcard no data (3.1.3.4) case the wildcard IS
  744. * NOT expanded, so the ORIGINAL parameter must point to the original
  745. * wildcard entry, not to the generated entry.
  746. */
  747. static void
  748. answer_nodata(struct query *query, answer_type *answer, domain_type *original)
  749. {
  750. if (query->cname_count == 0) {
  751. answer_soa(query, answer);
  752. }
  753. #ifdef NSEC3
  754. if (query->edns.dnssec_ok && query->zone->nsec3_soa_rr) {
  755. nsec3_answer_nodata(query, answer, original);
  756. } else
  757. #endif
  758. if (query->edns.dnssec_ok && zone_is_secure(query->zone)) {
  759. domain_type *nsec_domain;
  760. rrset_type *nsec_rrset;
  761. nsec_domain = find_covering_nsec(original, query->zone, &nsec_rrset);
  762. if (nsec_domain) {
  763. add_rrset(query, answer, AUTHORITY_SECTION, nsec_domain, nsec_rrset);
  764. }
  765. }
  766. }
  767. static void
  768. answer_nxdomain(query_type *query, answer_type *answer)
  769. {
  770. if (query->cname_count == 0) {
  771. RCODE_SET(query->packet, RCODE_NXDOMAIN);
  772. answer_soa(query, answer);
  773. }
  774. }
  775. /*
  776. * Answer domain information (or SOA if we do not have an RRset for
  777. * the type specified by the query).
  778. */
  779. static void
  780. answer_domain(struct nsd* nsd, struct query *q, answer_type *answer,
  781. domain_type *domain, domain_type *original)
  782. {
  783. rrset_type *rrset;
  784. if (q->qtype == TYPE_ANY) {
  785. int added = 0;
  786. for (rrset = domain_find_any_rrset(domain, q->zone); rrset; rrset = rrset->next) {
  787. if (rrset->zone == q->zone
  788. #ifdef NSEC3
  789. && rrset_rrtype(rrset) != TYPE_NSEC3
  790. #endif
  791. /*
  792. * Don't include the RRSIG RRset when
  793. * DNSSEC is used, because it is added
  794. * automatically on an per-RRset basis.
  795. */
  796. && !(q->edns.dnssec_ok
  797. && zone_is_secure(q->zone)
  798. && rrset_rrtype(rrset) == TYPE_RRSIG))
  799. {
  800. add_rrset(q, answer, ANSWER_SECTION, domain, rrset);
  801. ++added;
  802. }
  803. }
  804. if (added == 0) {
  805. answer_nodata(q, answer, original);
  806. return;
  807. }
  808. #ifdef NSEC3
  809. } else if (q->qtype == TYPE_NSEC3) {
  810. answer_nodata(q, answer, original);
  811. return;
  812. #endif
  813. } else if ((rrset = domain_find_rrset(domain, q->zone, q->qtype))) {
  814. add_rrset(q, answer, ANSWER_SECTION, domain, rrset);
  815. } else if ((rrset = domain_find_rrset(domain, q->zone, TYPE_CNAME))) {
  816. int added;
  817. /*
  818. * If the CNAME is not added it is already in the
  819. * answer, so we have a CNAME loop. Don't follow the
  820. * CNAME target in this case.
  821. */
  822. added = add_rrset(q, answer, ANSWER_SECTION, domain, rrset);
  823. assert(rrset->rr_count > 0);
  824. if (added) {
  825. /* only process first CNAME record */
  826. domain_type *closest_match = rdata_atom_domain(rrset->rrs[0].rdatas[0]);
  827. domain_type *closest_encloser = closest_match;
  828. zone_type* origzone = q->zone;
  829. ++q->cname_count;
  830. while (!closest_encloser->is_existing)
  831. closest_encloser = closest_encloser->parent;
  832. answer_lookup_zone(nsd, q, answer, closest_match->number,
  833. closest_match == closest_encloser,
  834. closest_match, closest_encloser,
  835. domain_dname(closest_match));
  836. q->zone = origzone;
  837. }
  838. /* example 6.2.7 shows no NS-set from zone in auth (RFC1034) */
  839. q->domain = domain;
  840. return;
  841. } else {
  842. answer_nodata(q, answer, original);
  843. return;
  844. }
  845. q->domain = domain;
  846. if (q->qclass != CLASS_ANY && q->zone->ns_rrset && answer_needs_ns(q)) {
  847. add_rrset(q, answer, OPTIONAL_AUTHORITY_SECTION, q->zone->apex,
  848. q->zone->ns_rrset);
  849. }
  850. }
  851. /*
  852. * Answer with authoritative data. If a wildcard is matched the owner
  853. * name will be expanded to the domain name specified by
  854. * DOMAIN_NUMBER. DOMAIN_NUMBER 0 (zero) is reserved for the original
  855. * query name.
  856. *
  857. * DNSSEC: Include the necessary NSEC records in case the request
  858. * domain name does not exist and/or a wildcard match does not exist.
  859. */
  860. static void
  861. answer_authoritative(struct nsd *nsd,
  862. struct query *q,
  863. answer_type *answer,
  864. uint32_t domain_number,
  865. int exact,
  866. domain_type *closest_match,
  867. domain_type *closest_encloser,
  868. const dname_type *qname)
  869. {
  870. domain_type *match;
  871. domain_type *original = closest_match;
  872. rrset_type *rrset;
  873. #ifdef NSEC3
  874. if(exact && domain_has_only_NSEC3(closest_match, q->zone)) {
  875. exact = 0; /* pretend it does not exist */
  876. if(closest_encloser->parent)
  877. closest_encloser = closest_encloser->parent;
  878. }
  879. #endif /* NSEC3 */
  880. if (exact) {
  881. match = closest_match;
  882. } else if ((rrset=domain_find_rrset(closest_encloser, q->zone, TYPE_DNAME))) {
  883. /* process DNAME */
  884. const dname_type* name = qname;
  885. domain_type *dest = rdata_atom_domain(rrset->rrs[0].rdatas[0]);
  886. int added;
  887. assert(rrset->rr_count > 0);
  888. if(domain_number != 0) /* we followed CNAMEs or DNAMEs */
  889. name = domain_dname(closest_match);
  890. DEBUG(DEBUG_QUERY,2, (LOG_INFO, "expanding DNAME for q=%s", dname_to_string(name, NULL)));
  891. DEBUG(DEBUG_QUERY,2, (LOG_INFO, "->src is %s",
  892. dname_to_string(domain_dname(closest_encloser), NULL)));
  893. DEBUG(DEBUG_QUERY,2, (LOG_INFO, "->dest is %s",
  894. dname_to_string(domain_dname(dest), NULL)));
  895. /* if the DNAME set is not added we have a loop, do not follow */
  896. added = add_rrset(q, answer, ANSWER_SECTION, closest_encloser, rrset);
  897. if(added) {
  898. domain_type* src = closest_encloser;
  899. const dname_type* newname = dname_replace(q->region, name,
  900. domain_dname(src), domain_dname(dest));
  901. uint32_t newnum = 0;
  902. zone_type* origzone = q->zone;
  903. ++q->cname_count;
  904. if(!newname) { /* newname too long */
  905. RCODE_SET(q->packet, RCODE_YXDOMAIN);
  906. return;
  907. }
  908. DEBUG(DEBUG_QUERY,2, (LOG_INFO, "->result is %s", dname_to_string(newname, NULL)));
  909. /* follow the DNAME */
  910. exact = namedb_lookup(nsd->db, newname, &closest_match, &closest_encloser);
  911. /* synthesize CNAME record */
  912. newnum = query_synthesize_cname(q, answer, name, newname,
  913. src, closest_encloser, &closest_match);
  914. if(!newnum) {
  915. /* could not synthesize the CNAME. */
  916. /* return previous CNAMEs to make resolver recurse for us */
  917. return;
  918. }
  919. while (closest_encloser && !closest_encloser->is_existing)
  920. closest_encloser = closest_encloser->parent;
  921. answer_lookup_zone(nsd, q, answer, newnum,
  922. closest_match == closest_encloser,
  923. closest_match, closest_encloser, newname);
  924. q->zone = origzone;
  925. }
  926. if(!added) /* log the error so operator can find looping recursors */
  927. log_msg(LOG_INFO, "DNAME processing stopped due to loop, qname %s",
  928. dname_to_string(q->qname, NULL));
  929. return;
  930. } else if (domain_wildcard_child(closest_encloser)) {
  931. /* Generate the domain from the wildcard. */
  932. domain_type *wildcard_child = domain_wildcard_child(closest_encloser);
  933. match = (domain_type *) region_alloc(q->region,
  934. sizeof(domain_type));
  935. memcpy(&match->node, &wildcard_child->node, sizeof(rbnode_t));
  936. match->parent = closest_encloser;
  937. match->wildcard_child_closest_match = match;
  938. match->number = domain_number;
  939. match->rrsets = wildcard_child->rrsets;
  940. match->is_existing = wildcard_child->is_existing;
  941. #ifdef NSEC3
  942. match->nsec3_cover = wildcard_child->nsec3_cover;
  943. #ifdef FULL_PREHASH
  944. match->nsec3_is_exact = wildcard_child->nsec3_is_exact;
  945. match->nsec3_wcard_child_cover = wildcard_child->nsec3_wcard_child_cover;
  946. match->nsec3_ds_parent_is_exact = wildcard_child->nsec3_ds_parent_is_exact;
  947. match->nsec3_ds_parent_cover = wildcard_child->nsec3_ds_parent_cover;
  948. #endif
  949. if (q->edns.dnssec_ok && q->zone->nsec3_soa_rr) {
  950. /* Only add nsec3 wildcard data when do bit is set */
  951. nsec3_answer_wildcard(q, answer, wildcard_child, nsd->db, qname);
  952. }
  953. #endif
  954. /*
  955. * Remember the original domain in case a Wildcard No
  956. * Data (3.1.3.4) response needs to be generated. In
  957. * this particular case the wildcard IS NOT
  958. * expanded.
  959. */
  960. original = wildcard_child;
  961. } else {
  962. match = NULL;
  963. }
  964. /* Authorative zone. */
  965. #ifdef NSEC3
  966. if (q->edns.dnssec_ok && q->zone->nsec3_soa_rr) {
  967. nsec3_answer_authoritative(&match, q, answer,
  968. closest_encloser, nsd->db, qname);
  969. } else
  970. #endif
  971. if (q->edns.dnssec_ok && zone_is_secure(q->zone)) {
  972. if (match != closest_encloser) {
  973. domain_type *nsec_domain;
  974. rrset_type *nsec_rrset;
  975. /*
  976. * No match found or generated from wildcard,
  977. * include NSEC record.
  978. */
  979. nsec_domain = find_covering_nsec(closest_match, q->zone, &nsec_rrset);
  980. if (nsec_domain) {
  981. add_rrset(q, answer, AUTHORITY_SECTION, nsec_domain, nsec_rrset);
  982. }
  983. }
  984. if (!match) {
  985. domain_type *nsec_domain;
  986. rrset_type *nsec_rrset;
  987. /*
  988. * No match and no wildcard. Include NSEC
  989. * proving there is no wildcard.
  990. */
  991. nsec_domain = find_covering_nsec(closest_encloser->wildcard_child_closest_match, q->zone, &nsec_rrset);
  992. if (nsec_domain) {
  993. add_rrset(q, answer, AUTHORITY_SECTION, nsec_domain, nsec_rrset);
  994. }
  995. }
  996. }
  997. #ifdef NSEC3
  998. if (RCODE(q->packet)!=RCODE_OK) {
  999. return; /* nsec3 collision failure */
  1000. }
  1001. #endif
  1002. if (match) {
  1003. answer_domain(nsd, q, answer, match, original);
  1004. } else {
  1005. answer_nxdomain(q, answer);
  1006. }
  1007. }
  1008. /*
  1009. * qname may be different after CNAMEs have been followed from query->qname.
  1010. */
  1011. static void
  1012. answer_lookup_zone(struct nsd *nsd, struct query *q, answer_type *answer,
  1013. uint32_t domain_number, int exact, domain_type *closest_match,
  1014. domain_type *closest_encloser, const dname_type *qname)
  1015. {
  1016. q->zone = domain_find_zone(closest_encloser);
  1017. if (!q->zone) {
  1018. if(q->cname_count == 0)
  1019. RCODE_SET(q->packet, RCODE_SERVFAIL);
  1020. return;
  1021. }
  1022. /*
  1023. * See RFC 4035 (DNSSEC protocol) section 3.1.4.1 Responding
  1024. * to Queries for DS RRs.
  1025. */
  1026. if (exact && q->qtype == TYPE_DS && closest_encloser == q->zone->apex) {
  1027. /*
  1028. * Type DS query at a zone cut, use the responsible
  1029. * parent zone to generate the answer if we are
  1030. * authoritative for the parent zone.
  1031. */
  1032. zone_type *zone = domain_find_parent_zone(q->zone);
  1033. if (zone)
  1034. q->zone = zone;
  1035. }
  1036. /* see if the zone has expired (for secondary zones) */
  1037. if(q->zone && q->zone->opts && zone_is_slave(q->zone->opts)
  1038. && !q->zone->is_ok) {
  1039. if(q->cname_count == 0)
  1040. RCODE_SET(q->packet, RCODE_SERVFAIL);
  1041. return;
  1042. }
  1043. if (exact && q->qtype == TYPE_DS && closest_encloser == q->zone->apex) {
  1044. /*
  1045. * Type DS query at the zone apex (and the server is
  1046. * not authoratitive for the parent zone).
  1047. */
  1048. if (q->qclass == CLASS_ANY) {
  1049. AA_CLR(q->packet);
  1050. } else {
  1051. AA_SET(q->packet);
  1052. }
  1053. answer_nodata(q, answer, closest_encloser);
  1054. } else {
  1055. q->delegation_domain = domain_find_ns_rrsets(
  1056. closest_encloser, q->zone, &q->delegation_rrset);
  1057. if (!q->delegation_domain
  1058. || (exact && q->qtype == TYPE_DS && closest_encloser == q->delegation_domain))
  1059. {
  1060. if (q->qclass == CLASS_ANY) {
  1061. AA_CLR(q->packet);
  1062. } else {
  1063. AA_SET(q->packet);
  1064. }
  1065. answer_authoritative(nsd, q, answer, domain_number, exact,
  1066. closest_match, closest_encloser, qname);
  1067. }
  1068. else {
  1069. answer_delegation(q, answer);
  1070. }
  1071. }
  1072. }
  1073. static void
  1074. answer_query(struct nsd *nsd, struct query *q)
  1075. {
  1076. domain_type *closest_match;
  1077. domain_type *closest_encloser;
  1078. int exact;
  1079. uint16_t offset;
  1080. answer_type answer;
  1081. answer_init(&answer);
  1082. exact = namedb_lookup(nsd->db, q->qname, &closest_match, &closest_encloser);
  1083. if (!closest_encloser->is_existing) {
  1084. exact = 0;
  1085. while (closest_encloser != NULL && !closest_encloser->is_existing)
  1086. closest_encloser = closest_encloser->parent;
  1087. }
  1088. if(!closest_encloser) {
  1089. RCODE_SET(q->packet, RCODE_SERVFAIL);
  1090. return;
  1091. }
  1092. q->domain = closest_encloser;
  1093. answer_lookup_zone(nsd, q, &answer, 0, exact, closest_match,
  1094. closest_encloser, q->qname);
  1095. encode_answer(q, &answer);
  1096. if (ANCOUNT(q->packet) + NSCOUNT(q->packet) + ARCOUNT(q->packet) == 0)
  1097. {
  1098. /* no answers, no need for compression */
  1099. return;
  1100. }
  1101. offset = dname_label_offsets(q->qname)[domain_dname(closest_encloser)->label_count - 1] + QHEADERSZ;
  1102. query_add_compression_domain(q, closest_encloser, offset);
  1103. query_clear_compression_tables(q);
  1104. }
  1105. void
  1106. query_prepare_response(query_type *q)
  1107. {
  1108. uint16_t flags;
  1109. /*
  1110. * Preserve the data up-to the current packet's limit.
  1111. */
  1112. buffer_set_position(q->packet, buffer_limit(q->packet));
  1113. buffer_set_limit(q->packet, buffer_capacity(q->packet));
  1114. /*
  1115. * Reserve space for the EDNS records if required.
  1116. */
  1117. q->reserved_space = edns_reserved_space(&q->edns);
  1118. q->reserved_space += tsig_reserved_space(&q->tsig);
  1119. /* Update the flags. */
  1120. flags = FLAGS(q->packet);
  1121. flags &= 0x0100U; /* Preserve the RD flag. */
  1122. /* CD flag must be cleared for auth answers */
  1123. flags |= 0x8000U; /* Set the QR flag. */
  1124. FLAGS_SET(q->packet, flags);
  1125. }
  1126. /*
  1127. * Processes the query.
  1128. *
  1129. */
  1130. query_state_type
  1131. query_process(query_type *q, nsd_type *nsd)
  1132. {
  1133. /* The query... */
  1134. nsd_rc_type rc;
  1135. query_state_type query_state;
  1136. uint16_t arcount;
  1137. /* Sanity checks */
  1138. if (buffer_limit(q->packet) < QHEADERSZ) {
  1139. /* packet too small to contain DNS header.
  1140. Now packet investigation macros will work without problems. */
  1141. return QUERY_DISCARDED;
  1142. }
  1143. if (QR(q->packet)) {
  1144. /* Not a query? Drop it on the floor. */
  1145. return QUERY_DISCARDED;
  1146. }
  1147. if(!process_query_section(q)) {
  1148. return query_formerr(q);
  1149. }
  1150. /* Update statistics. */
  1151. STATUP2(nsd, opcode, q->opcode);
  1152. STATUP2(nsd, qtype, q->qtype);
  1153. STATUP2(nsd, qclass, q->qclass);
  1154. if (q->opcode != OPCODE_QUERY) {
  1155. if (q->opcode == OPCODE_NOTIFY) {
  1156. return answer_notify(nsd, q);
  1157. } else {
  1158. return query_error(q, NSD_RC_IMPL);
  1159. }
  1160. }
  1161. /* Dont bother to answer more than one question at once... */
  1162. if (QDCOUNT(q->packet) != 1 || TC(q->packet)) {
  1163. FLAGS_SET(q->packet, 0);
  1164. return query_formerr(q);
  1165. }
  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. switch (q->edns.status) {
  1246. case EDNS_NOT_PRESENT:
  1247. break;
  1248. case EDNS_OK:
  1249. if (q->edns.dnssec_ok) edns->ok[7] = 0x80;
  1250. else edns->ok[7] = 0x00;
  1251. buffer_write(q->packet, edns->ok, OPT_LEN);
  1252. if (nsd->nsid_len > 0 && q->edns.nsid == 1 &&
  1253. !query_overflow_nsid(q, nsd->nsid_len)) {
  1254. /* rdata length */
  1255. buffer_write(q->packet, edns->rdata_nsid, OPT_RDATA);
  1256. /* nsid opt header */
  1257. buffer_write(q->packet, edns->nsid, OPT_HDR);
  1258. /* nsid payload */
  1259. buffer_write(q->packet, nsd->nsid, nsd->nsid_len);
  1260. } else {
  1261. /* fill with NULLs */
  1262. buffer_write(q->packet, edns->rdata_none, OPT_RDATA);
  1263. }
  1264. ARCOUNT_SET(q->packet, ARCOUNT(q->packet) + 1);
  1265. STATUP(nsd, edns);
  1266. break;
  1267. case EDNS_ERROR:
  1268. if (q->edns.dnssec_ok) edns->error[7] = 0x80;
  1269. else edns->error[7] = 0x00;
  1270. buffer_write(q->packet, edns->error, OPT_LEN);
  1271. buffer_write(q->packet, edns->rdata_none, OPT_RDATA);
  1272. ARCOUNT_SET(q->packet, ARCOUNT(q->packet) + 1);
  1273. STATUP(nsd, ednserr);
  1274. break;
  1275. }
  1276. if (q->tsig.status != TSIG_NOT_PRESENT) {
  1277. if (q->tsig.status == TSIG_ERROR ||
  1278. q->tsig.error_code != TSIG_ERROR_NOERROR) {
  1279. tsig_error_reply(&q->tsig);
  1280. tsig_append_rr(&q->tsig, q->packet);
  1281. ARCOUNT_SET(q->packet, ARCOUNT(q->packet) + 1);
  1282. } else if(q->tsig.status == TSIG_OK &&
  1283. q->tsig.error_code == TSIG_ERROR_NOERROR)
  1284. {
  1285. if(q->tsig_prepare_it)
  1286. tsig_prepare(&q->tsig);
  1287. if(q->tsig_update_it)
  1288. tsig_update(&q->tsig, q->packet, buffer_position(q->packet));
  1289. if(q->tsig_sign_it) {
  1290. tsig_sign(&q->tsig);
  1291. tsig_append_rr(&q->tsig, q->packet);
  1292. ARCOUNT_SET(q->packet, ARCOUNT(q->packet) + 1);
  1293. }
  1294. }
  1295. }
  1296. }