2 * Copyright (C) 2004, 2005, 2007, 2008, 2010 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: ncache.c,v 1.43.48.7.6.1 2011-05-27 00:19:19 each Exp $ */
24 #include <isc/buffer.h>
28 #include <dns/message.h>
29 #include <dns/ncache.h>
30 #include <dns/rdata.h>
31 #include <dns/rdatalist.h>
32 #include <dns/rdataset.h>
33 #include <dns/rdatastruct.h>
35 #define DNS_NCACHE_RDATA 20U
38 * The format of an ncache rdata is a sequence of one or more records of
39 * the following format:
45 * rdata length These two occur 'rdata count'
50 static inline isc_result_t
51 copy_rdataset(dns_rdataset_t *rdataset, isc_buffer_t *buffer) {
55 dns_rdata_t rdata = DNS_RDATA_INIT;
58 * Copy the rdataset count to the buffer.
60 isc_buffer_availableregion(buffer, &ar);
62 return (ISC_R_NOSPACE);
63 count = dns_rdataset_count(rdataset);
64 INSIST(count <= 65535);
65 isc_buffer_putuint16(buffer, (isc_uint16_t)count);
67 result = dns_rdataset_first(rdataset);
68 while (result == ISC_R_SUCCESS) {
69 dns_rdataset_current(rdataset, &rdata);
70 dns_rdata_toregion(&rdata, &r);
71 INSIST(r.length <= 65535);
72 isc_buffer_availableregion(buffer, &ar);
74 return (ISC_R_NOSPACE);
76 * Copy the rdata length to the buffer.
78 isc_buffer_putuint16(buffer, (isc_uint16_t)r.length);
80 * Copy the rdata to the buffer.
82 result = isc_buffer_copyregion(buffer, &r);
83 if (result != ISC_R_SUCCESS)
85 dns_rdata_reset(&rdata);
86 result = dns_rdataset_next(rdataset);
88 if (result != ISC_R_NOMORE)
91 return (ISC_R_SUCCESS);
95 dns_ncache_add(dns_message_t *message, dns_db_t *cache, dns_dbnode_t *node,
96 dns_rdatatype_t covers, isc_stdtime_t now, dns_ttl_t maxttl,
97 dns_rdataset_t *addedrdataset)
99 return (dns_ncache_addoptout(message, cache, node, covers, now, maxttl,
100 ISC_FALSE, addedrdataset));
104 dns_ncache_addoptout(dns_message_t *message, dns_db_t *cache,
105 dns_dbnode_t *node, dns_rdatatype_t covers,
106 isc_stdtime_t now, dns_ttl_t maxttl,
107 isc_boolean_t optout, dns_rdataset_t *addedrdataset)
112 dns_rdataset_t *rdataset;
113 dns_rdatatype_t type;
117 dns_rdata_t rdata[DNS_NCACHE_RDATA];
118 dns_rdataset_t ncrdataset;
119 dns_rdatalist_t ncrdatalist;
120 unsigned char data[4096];
121 unsigned int next = 0;
124 * Convert the authority data from 'message' into a negative cache
125 * rdataset, and store it in 'cache' at 'node'.
128 REQUIRE(message != NULL);
131 * We assume that all data in the authority section has been
132 * validated by the caller.
136 * Initialize the list.
138 ncrdatalist.rdclass = dns_db_class(cache);
139 ncrdatalist.type = 0;
140 ncrdatalist.covers = covers;
141 ncrdatalist.ttl = maxttl;
142 ISC_LIST_INIT(ncrdatalist.rdata);
143 ISC_LINK_INIT(&ncrdatalist, link);
146 * Build an ncache rdatas into buffer.
150 isc_buffer_init(&buffer, data, sizeof(data));
151 if (message->counts[DNS_SECTION_AUTHORITY])
152 result = dns_message_firstname(message, DNS_SECTION_AUTHORITY);
154 result = ISC_R_NOMORE;
155 while (result == ISC_R_SUCCESS) {
157 dns_message_currentname(message, DNS_SECTION_AUTHORITY,
159 if ((name->attributes & DNS_NAMEATTR_NCACHE) != 0) {
160 for (rdataset = ISC_LIST_HEAD(name->list);
162 rdataset = ISC_LIST_NEXT(rdataset, link)) {
163 if ((rdataset->attributes &
164 DNS_RDATASETATTR_NCACHE) == 0)
166 type = rdataset->type;
167 if (type == dns_rdatatype_rrsig)
168 type = rdataset->covers;
169 if (type == dns_rdatatype_soa ||
170 type == dns_rdatatype_nsec ||
171 type == dns_rdatatype_nsec3) {
172 if (ttl > rdataset->ttl)
174 if (trust > rdataset->trust)
175 trust = rdataset->trust;
177 * Copy the owner name to the buffer.
179 dns_name_toregion(name, &r);
180 result = isc_buffer_copyregion(&buffer,
182 if (result != ISC_R_SUCCESS)
185 * Copy the type to the buffer.
187 isc_buffer_availableregion(&buffer,
190 return (ISC_R_NOSPACE);
191 isc_buffer_putuint16(&buffer,
193 isc_buffer_putuint8(&buffer,
194 (unsigned char)rdataset->trust);
196 * Copy the rdataset into the buffer.
198 result = copy_rdataset(rdataset,
200 if (result != ISC_R_SUCCESS)
203 if (next >= DNS_NCACHE_RDATA)
204 return (ISC_R_NOSPACE);
205 dns_rdata_init(&rdata[next]);
206 isc_buffer_remainingregion(&buffer, &r);
207 rdata[next].data = r.base;
208 rdata[next].length = r.length;
209 rdata[next].rdclass =
211 rdata[next].type = 0;
212 rdata[next].flags = 0;
213 ISC_LIST_APPEND(ncrdatalist.rdata,
215 isc_buffer_forward(&buffer, r.length);
220 result = dns_message_nextname(message, DNS_SECTION_AUTHORITY);
222 if (result != ISC_R_NOMORE)
225 if (trust == 0xffff) {
227 * We didn't find any authority data from which to create a
228 * negative cache rdataset. In particular, we have no SOA.
230 * We trust that the caller wants negative caching, so this
231 * means we have a "type 3 nxdomain" or "type 3 nodata"
232 * response (see RFC2308 for details).
234 * We will now build a suitable negative cache rdataset that
235 * will cause zero bytes to be emitted when converted to
240 * The ownername must exist, but it doesn't matter what value
241 * it has. We use the root name.
243 dns_name_toregion(dns_rootname, &r);
244 result = isc_buffer_copyregion(&buffer, &r);
245 if (result != ISC_R_SUCCESS)
248 * Copy the type and a zero rdata count to the buffer.
250 isc_buffer_availableregion(&buffer, &r);
252 return (ISC_R_NOSPACE);
253 isc_buffer_putuint16(&buffer, 0); /* type */
255 * RFC2308, section 5, says that negative answers without
256 * SOAs should not be cached.
262 if ((message->flags & DNS_MESSAGEFLAG_AA) != 0 &&
263 message->counts[DNS_SECTION_ANSWER] == 0) {
265 * The response has aa set and we haven't followed
266 * any CNAME or DNAME chains.
268 trust = dns_trust_authauthority;
270 trust = dns_trust_additional;
271 isc_buffer_putuint8(&buffer, (unsigned char)trust); /* trust */
272 isc_buffer_putuint16(&buffer, 0); /* count */
275 * Now add it to the cache.
277 if (next >= DNS_NCACHE_RDATA)
278 return (ISC_R_NOSPACE);
279 dns_rdata_init(&rdata[next]);
280 isc_buffer_remainingregion(&buffer, &r);
281 rdata[next].data = r.base;
282 rdata[next].length = r.length;
283 rdata[next].rdclass = ncrdatalist.rdclass;
284 rdata[next].type = 0;
285 rdata[next].flags = 0;
286 ISC_LIST_APPEND(ncrdatalist.rdata, &rdata[next], link);
289 INSIST(trust != 0xffff);
291 ncrdatalist.ttl = ttl;
293 dns_rdataset_init(&ncrdataset);
294 RUNTIME_CHECK(dns_rdatalist_tordataset(&ncrdatalist, &ncrdataset)
296 ncrdataset.trust = trust;
297 if (message->rcode == dns_rcode_nxdomain)
298 ncrdataset.attributes |= DNS_RDATASETATTR_NXDOMAIN;
300 ncrdataset.attributes |= DNS_RDATASETATTR_OPTOUT;
302 return (dns_db_addrdataset(cache, node, NULL, now, &ncrdataset,
307 dns_ncache_towire(dns_rdataset_t *rdataset, dns_compress_t *cctx,
308 isc_buffer_t *target, unsigned int options,
309 unsigned int *countp)
311 dns_rdata_t rdata = DNS_RDATA_INIT;
313 isc_region_t remaining, tavailable;
314 isc_buffer_t source, savedbuffer, rdlen;
316 dns_rdatatype_t type;
317 unsigned int i, rcount, count;
320 * Convert the negative caching rdataset 'rdataset' to wire format,
321 * compressing names as specified in 'cctx', and storing the result in
325 REQUIRE(rdataset != NULL);
326 REQUIRE(rdataset->type == 0);
328 savedbuffer = *target;
331 result = dns_rdataset_first(rdataset);
332 while (result == ISC_R_SUCCESS) {
333 dns_rdataset_current(rdataset, &rdata);
334 isc_buffer_init(&source, rdata.data, rdata.length);
335 isc_buffer_add(&source, rdata.length);
336 dns_name_init(&name, NULL);
337 isc_buffer_remainingregion(&source, &remaining);
338 dns_name_fromregion(&name, &remaining);
339 INSIST(remaining.length >= name.length);
340 isc_buffer_forward(&source, name.length);
341 remaining.length -= name.length;
343 INSIST(remaining.length >= 5);
344 type = isc_buffer_getuint16(&source);
345 isc_buffer_forward(&source, 1);
346 rcount = isc_buffer_getuint16(&source);
348 for (i = 0; i < rcount; i++) {
350 * Get the length of this rdata and set up an
351 * rdata structure for it.
353 isc_buffer_remainingregion(&source, &remaining);
354 INSIST(remaining.length >= 2);
355 dns_rdata_reset(&rdata);
356 rdata.length = isc_buffer_getuint16(&source);
357 isc_buffer_remainingregion(&source, &remaining);
358 rdata.data = remaining.base;
360 rdata.rdclass = rdataset->rdclass;
361 INSIST(remaining.length >= rdata.length);
362 isc_buffer_forward(&source, rdata.length);
364 if ((options & DNS_NCACHETOWIRE_OMITDNSSEC) != 0 &&
365 dns_rdatatype_isdnssec(type))
371 dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14);
372 result = dns_name_towire(&name, cctx, target);
373 if (result != ISC_R_SUCCESS)
377 * See if we have space for type, class, ttl, and
378 * rdata length. Write the type, class, and ttl.
380 isc_buffer_availableregion(target, &tavailable);
381 if (tavailable.length < 10) {
382 result = ISC_R_NOSPACE;
385 isc_buffer_putuint16(target, type);
386 isc_buffer_putuint16(target, rdataset->rdclass);
387 isc_buffer_putuint32(target, rdataset->ttl);
390 * Save space for rdata length.
393 isc_buffer_add(target, 2);
398 result = dns_rdata_towire(&rdata, cctx, target);
399 if (result != ISC_R_SUCCESS)
403 * Set the rdata length field to the compressed
406 INSIST((target->used >= rdlen.used + 2) &&
407 (target->used - rdlen.used - 2 < 65536));
408 isc_buffer_putuint16(&rdlen,
409 (isc_uint16_t)(target->used -
414 INSIST(isc_buffer_remaininglength(&source) == 0);
415 result = dns_rdataset_next(rdataset);
416 dns_rdata_reset(&rdata);
418 if (result != ISC_R_NOMORE)
423 return (ISC_R_SUCCESS);
426 INSIST(savedbuffer.used < 65536);
427 dns_compress_rollback(cctx, (isc_uint16_t)savedbuffer.used);
429 *target = savedbuffer;
435 rdataset_disassociate(dns_rdataset_t *rdataset) {
440 rdataset_first(dns_rdataset_t *rdataset) {
441 unsigned char *raw = rdataset->private3;
444 count = raw[0] * 256 + raw[1];
446 rdataset->private5 = NULL;
447 return (ISC_R_NOMORE);
451 * The privateuint4 field is the number of rdata beyond the cursor
452 * position, so we decrement the total count by one before storing
456 rdataset->privateuint4 = count;
457 rdataset->private5 = raw;
459 return (ISC_R_SUCCESS);
463 rdataset_next(dns_rdataset_t *rdataset) {
468 count = rdataset->privateuint4;
470 return (ISC_R_NOMORE);
472 rdataset->privateuint4 = count;
473 raw = rdataset->private5;
474 length = raw[0] * 256 + raw[1];
476 rdataset->private5 = raw;
478 return (ISC_R_SUCCESS);
482 rdataset_current(dns_rdataset_t *rdataset, dns_rdata_t *rdata) {
483 unsigned char *raw = rdataset->private5;
486 REQUIRE(raw != NULL);
488 r.length = raw[0] * 256 + raw[1];
491 dns_rdata_fromregion(rdata, rdataset->rdclass, rdataset->type, &r);
495 rdataset_clone(dns_rdataset_t *source, dns_rdataset_t *target) {
499 * Reset iterator state.
501 target->privateuint4 = 0;
502 target->private5 = NULL;
506 rdataset_count(dns_rdataset_t *rdataset) {
507 unsigned char *raw = rdataset->private3;
510 count = raw[0] * 256 + raw[1];
516 rdataset_settrust(dns_rdataset_t *rdataset, dns_trust_t trust) {
517 unsigned char *raw = rdataset->private3;
519 raw[-1] = (unsigned char)trust;
522 static dns_rdatasetmethods_t rdataset_methods = {
523 rdataset_disassociate,
541 dns_ncache_getrdataset(dns_rdataset_t *ncacherdataset, dns_name_t *name,
542 dns_rdatatype_t type, dns_rdataset_t *rdataset)
545 dns_rdata_t rdata = DNS_RDATA_INIT;
546 isc_region_t remaining;
549 dns_rdatatype_t ttype;
550 dns_trust_t trust = dns_trust_none;
551 dns_rdataset_t clone;
553 REQUIRE(ncacherdataset != NULL);
554 REQUIRE(ncacherdataset->type == 0);
555 REQUIRE(name != NULL);
556 REQUIRE(!dns_rdataset_isassociated(rdataset));
557 REQUIRE(type != dns_rdatatype_rrsig);
559 dns_rdataset_init(&clone);
560 dns_rdataset_clone(ncacherdataset, &clone);
561 result = dns_rdataset_first(&clone);
562 while (result == ISC_R_SUCCESS) {
563 dns_rdataset_current(&clone, &rdata);
564 isc_buffer_init(&source, rdata.data, rdata.length);
565 isc_buffer_add(&source, rdata.length);
566 dns_name_init(&tname, NULL);
567 isc_buffer_remainingregion(&source, &remaining);
568 dns_name_fromregion(&tname, &remaining);
569 INSIST(remaining.length >= tname.length);
570 isc_buffer_forward(&source, tname.length);
571 remaining.length -= tname.length;
573 INSIST(remaining.length >= 3);
574 ttype = isc_buffer_getuint16(&source);
576 if (ttype == type && dns_name_equal(&tname, name)) {
577 trust = isc_buffer_getuint8(&source);
578 INSIST(trust <= dns_trust_ultimate);
579 isc_buffer_remainingregion(&source, &remaining);
582 result = dns_rdataset_next(&clone);
583 dns_rdata_reset(&rdata);
585 dns_rdataset_disassociate(&clone);
586 if (result == ISC_R_NOMORE)
587 return (ISC_R_NOTFOUND);
588 if (result != ISC_R_SUCCESS)
591 INSIST(remaining.length != 0);
593 rdataset->methods = &rdataset_methods;
594 rdataset->rdclass = ncacherdataset->rdclass;
595 rdataset->type = type;
596 rdataset->covers = 0;
597 rdataset->ttl = ncacherdataset->ttl;
598 rdataset->trust = trust;
599 rdataset->private1 = NULL;
600 rdataset->private2 = NULL;
602 rdataset->private3 = remaining.base;
605 * Reset iterator state.
607 rdataset->privateuint4 = 0;
608 rdataset->private5 = NULL;
609 rdataset->private6 = NULL;
610 return (ISC_R_SUCCESS);
614 dns_ncache_getsigrdataset(dns_rdataset_t *ncacherdataset, dns_name_t *name,
615 dns_rdatatype_t covers, dns_rdataset_t *rdataset)
618 dns_rdata_rrsig_t rrsig;
619 dns_rdata_t rdata = DNS_RDATA_INIT;
620 dns_rdataset_t clone;
621 dns_rdatatype_t type;
622 dns_trust_t trust = dns_trust_none;
624 isc_region_t remaining, sigregion;
629 REQUIRE(ncacherdataset != NULL);
630 REQUIRE(ncacherdataset->type == 0);
631 REQUIRE(name != NULL);
632 REQUIRE(!dns_rdataset_isassociated(rdataset));
634 dns_rdataset_init(&clone);
635 dns_rdataset_clone(ncacherdataset, &clone);
636 result = dns_rdataset_first(&clone);
637 while (result == ISC_R_SUCCESS) {
638 dns_rdataset_current(&clone, &rdata);
639 isc_buffer_init(&source, rdata.data, rdata.length);
640 isc_buffer_add(&source, rdata.length);
641 dns_name_init(&tname, NULL);
642 isc_buffer_remainingregion(&source, &remaining);
643 dns_name_fromregion(&tname, &remaining);
644 INSIST(remaining.length >= tname.length);
645 isc_buffer_forward(&source, tname.length);
646 remaining.length -= tname.length;
647 remaining.base += tname.length;
649 INSIST(remaining.length >= 2);
650 type = isc_buffer_getuint16(&source);
651 remaining.length -= 2;
654 if (type != dns_rdatatype_rrsig ||
655 !dns_name_equal(&tname, name)) {
656 result = dns_rdataset_next(&clone);
657 dns_rdata_reset(&rdata);
661 INSIST(remaining.length >= 1);
662 trust = isc_buffer_getuint8(&source);
663 INSIST(trust <= dns_trust_ultimate);
664 remaining.length -= 1;
667 raw = remaining.base;
668 count = raw[0] * 256 + raw[1];
671 sigregion.length = raw[0] * 256 + raw[1];
673 sigregion.base = raw;
674 dns_rdata_reset(&rdata);
675 dns_rdata_fromregion(&rdata, rdataset->rdclass,
676 dns_rdatatype_rrsig, &sigregion);
677 (void)dns_rdata_tostruct(&rdata, &rrsig, NULL);
678 if (rrsig.covered == covers) {
679 isc_buffer_remainingregion(&source, &remaining);
683 result = dns_rdataset_next(&clone);
684 dns_rdata_reset(&rdata);
686 dns_rdataset_disassociate(&clone);
687 if (result == ISC_R_NOMORE)
688 return (ISC_R_NOTFOUND);
689 if (result != ISC_R_SUCCESS)
692 INSIST(remaining.length != 0);
694 rdataset->methods = &rdataset_methods;
695 rdataset->rdclass = ncacherdataset->rdclass;
696 rdataset->type = dns_rdatatype_rrsig;
697 rdataset->covers = covers;
698 rdataset->ttl = ncacherdataset->ttl;
699 rdataset->trust = trust;
700 rdataset->private1 = NULL;
701 rdataset->private2 = NULL;
703 rdataset->private3 = remaining.base;
706 * Reset iterator state.
708 rdataset->privateuint4 = 0;
709 rdataset->private5 = NULL;
710 rdataset->private6 = NULL;
711 return (ISC_R_SUCCESS);
715 dns_ncache_current(dns_rdataset_t *ncacherdataset, dns_name_t *found,
716 dns_rdataset_t *rdataset)
718 dns_rdata_t rdata = DNS_RDATA_INIT;
720 isc_region_t remaining, sigregion;
723 dns_rdatatype_t type;
725 dns_rdata_rrsig_t rrsig;
728 REQUIRE(ncacherdataset != NULL);
729 REQUIRE(ncacherdataset->type == 0);
730 REQUIRE(found != NULL);
731 REQUIRE(!dns_rdataset_isassociated(rdataset));
733 dns_rdataset_current(ncacherdataset, &rdata);
734 isc_buffer_init(&source, rdata.data, rdata.length);
735 isc_buffer_add(&source, rdata.length);
737 dns_name_init(&tname, NULL);
738 isc_buffer_remainingregion(&source, &remaining);
739 dns_name_fromregion(found, &remaining);
740 INSIST(remaining.length >= found->length);
741 isc_buffer_forward(&source, found->length);
742 remaining.length -= found->length;
744 INSIST(remaining.length >= 5);
745 type = isc_buffer_getuint16(&source);
746 trust = isc_buffer_getuint8(&source);
747 INSIST(trust <= dns_trust_ultimate);
748 isc_buffer_remainingregion(&source, &remaining);
750 rdataset->methods = &rdataset_methods;
751 rdataset->rdclass = ncacherdataset->rdclass;
752 rdataset->type = type;
753 if (type == dns_rdatatype_rrsig) {
755 * Extract covers from RRSIG.
757 raw = remaining.base;
758 count = raw[0] * 256 + raw[1];
761 sigregion.length = raw[0] * 256 + raw[1];
763 sigregion.base = raw;
764 dns_rdata_reset(&rdata);
765 dns_rdata_fromregion(&rdata, rdataset->rdclass,
766 rdataset->type, &sigregion);
767 (void)dns_rdata_tostruct(&rdata, &rrsig, NULL);
768 rdataset->covers = rrsig.covered;
770 rdataset->covers = 0;
771 rdataset->ttl = ncacherdataset->ttl;
772 rdataset->trust = trust;
773 rdataset->private1 = NULL;
774 rdataset->private2 = NULL;
776 rdataset->private3 = remaining.base;
779 * Reset iterator state.
781 rdataset->privateuint4 = 0;
782 rdataset->private5 = NULL;
783 rdataset->private6 = NULL;