/contrib/bind9/lib/dns/rdata/generic/nsec_47.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 396 lines · 283 code · 66 blank · 47 comment · 77 complexity · e8a389093c249d2f9fcc9d0ca70e2d1c MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2007-2009, 2011 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 2003 Internet Software Consortium.
  4. *
  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.
  8. *
  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.
  16. */
  17. /* $Id: nsec_47.c,v 1.15 2011/01/13 04:59:26 tbox Exp $ */
  18. /* reviewed: Wed Mar 15 18:21:15 PST 2000 by brister */
  19. /* RFC 3845 */
  20. #ifndef RDATA_GENERIC_NSEC_47_C
  21. #define RDATA_GENERIC_NSEC_47_C
  22. /*
  23. * The attributes do not include DNS_RDATATYPEATTR_SINGLETON
  24. * because we must be able to handle a parent/child NSEC pair.
  25. */
  26. #define RRTYPE_NSEC_ATTRIBUTES (DNS_RDATATYPEATTR_DNSSEC)
  27. static inline isc_result_t
  28. fromtext_nsec(ARGS_FROMTEXT) {
  29. isc_token_t token;
  30. dns_name_t name;
  31. isc_buffer_t buffer;
  32. unsigned char bm[8*1024]; /* 64k bits */
  33. dns_rdatatype_t covered;
  34. int octet;
  35. int window;
  36. REQUIRE(type == 47);
  37. UNUSED(type);
  38. UNUSED(rdclass);
  39. UNUSED(callbacks);
  40. /*
  41. * Next domain.
  42. */
  43. RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
  44. ISC_FALSE));
  45. dns_name_init(&name, NULL);
  46. buffer_fromregion(&buffer, &token.value.as_region);
  47. origin = (origin != NULL) ? origin : dns_rootname;
  48. RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
  49. memset(bm, 0, sizeof(bm));
  50. do {
  51. RETERR(isc_lex_getmastertoken(lexer, &token,
  52. isc_tokentype_string, ISC_TRUE));
  53. if (token.type != isc_tokentype_string)
  54. break;
  55. RETTOK(dns_rdatatype_fromtext(&covered,
  56. &token.value.as_textregion));
  57. bm[covered/8] |= (0x80>>(covered%8));
  58. } while (1);
  59. isc_lex_ungettoken(lexer, &token);
  60. for (window = 0; window < 256 ; window++) {
  61. /*
  62. * Find if we have a type in this window.
  63. */
  64. for (octet = 31; octet >= 0; octet--)
  65. if (bm[window * 32 + octet] != 0)
  66. break;
  67. if (octet < 0)
  68. continue;
  69. RETERR(uint8_tobuffer(window, target));
  70. RETERR(uint8_tobuffer(octet + 1, target));
  71. RETERR(mem_tobuffer(target, &bm[window * 32], octet + 1));
  72. }
  73. return (ISC_R_SUCCESS);
  74. }
  75. static inline isc_result_t
  76. totext_nsec(ARGS_TOTEXT) {
  77. isc_region_t sr;
  78. unsigned int i, j, k;
  79. dns_name_t name;
  80. unsigned int window, len;
  81. REQUIRE(rdata->type == 47);
  82. REQUIRE(rdata->length != 0);
  83. UNUSED(tctx);
  84. dns_name_init(&name, NULL);
  85. dns_rdata_toregion(rdata, &sr);
  86. dns_name_fromregion(&name, &sr);
  87. isc_region_consume(&sr, name_length(&name));
  88. RETERR(dns_name_totext(&name, ISC_FALSE, target));
  89. for (i = 0; i < sr.length; i += len) {
  90. INSIST(i + 2 <= sr.length);
  91. window = sr.base[i];
  92. len = sr.base[i + 1];
  93. INSIST(len > 0 && len <= 32);
  94. i += 2;
  95. INSIST(i + len <= sr.length);
  96. for (j = 0; j < len; j++) {
  97. dns_rdatatype_t t;
  98. if (sr.base[i + j] == 0)
  99. continue;
  100. for (k = 0; k < 8; k++) {
  101. if ((sr.base[i + j] & (0x80 >> k)) == 0)
  102. continue;
  103. t = window * 256 + j * 8 + k;
  104. RETERR(str_totext(" ", target));
  105. if (dns_rdatatype_isknown(t)) {
  106. RETERR(dns_rdatatype_totext(t, target));
  107. } else {
  108. char buf[sizeof("TYPE65535")];
  109. sprintf(buf, "TYPE%u", t);
  110. RETERR(str_totext(buf, target));
  111. }
  112. }
  113. }
  114. }
  115. return (ISC_R_SUCCESS);
  116. }
  117. static /* inline */ isc_result_t
  118. fromwire_nsec(ARGS_FROMWIRE) {
  119. isc_region_t sr;
  120. dns_name_t name;
  121. unsigned int window, lastwindow = 0;
  122. unsigned int len;
  123. isc_boolean_t first = ISC_TRUE;
  124. unsigned int i;
  125. REQUIRE(type == 47);
  126. UNUSED(type);
  127. UNUSED(rdclass);
  128. dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
  129. dns_name_init(&name, NULL);
  130. RETERR(dns_name_fromwire(&name, source, dctx, options, target));
  131. isc_buffer_activeregion(source, &sr);
  132. for (i = 0; i < sr.length; i += len) {
  133. /*
  134. * Check for overflow.
  135. */
  136. if (i + 2 > sr.length)
  137. RETERR(DNS_R_FORMERR);
  138. window = sr.base[i];
  139. len = sr.base[i + 1];
  140. i += 2;
  141. /*
  142. * Check that bitmap windows are in the correct order.
  143. */
  144. if (!first && window <= lastwindow)
  145. RETERR(DNS_R_FORMERR);
  146. /*
  147. * Check for legal lengths.
  148. */
  149. if (len < 1 || len > 32)
  150. RETERR(DNS_R_FORMERR);
  151. /*
  152. * Check for overflow.
  153. */
  154. if (i + len > sr.length)
  155. RETERR(DNS_R_FORMERR);
  156. /*
  157. * The last octet of the bitmap must be non zero.
  158. */
  159. if (sr.base[i + len - 1] == 0)
  160. RETERR(DNS_R_FORMERR);
  161. lastwindow = window;
  162. first = ISC_FALSE;
  163. }
  164. if (i != sr.length)
  165. return (DNS_R_EXTRADATA);
  166. if (first)
  167. RETERR(DNS_R_FORMERR);
  168. RETERR(mem_tobuffer(target, sr.base, sr.length));
  169. isc_buffer_forward(source, sr.length);
  170. return (ISC_R_SUCCESS);
  171. }
  172. static inline isc_result_t
  173. towire_nsec(ARGS_TOWIRE) {
  174. isc_region_t sr;
  175. dns_name_t name;
  176. dns_offsets_t offsets;
  177. REQUIRE(rdata->type == 47);
  178. REQUIRE(rdata->length != 0);
  179. dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
  180. dns_name_init(&name, offsets);
  181. dns_rdata_toregion(rdata, &sr);
  182. dns_name_fromregion(&name, &sr);
  183. isc_region_consume(&sr, name_length(&name));
  184. RETERR(dns_name_towire(&name, cctx, target));
  185. return (mem_tobuffer(target, sr.base, sr.length));
  186. }
  187. static inline int
  188. compare_nsec(ARGS_COMPARE) {
  189. isc_region_t r1;
  190. isc_region_t r2;
  191. REQUIRE(rdata1->type == rdata2->type);
  192. REQUIRE(rdata1->rdclass == rdata2->rdclass);
  193. REQUIRE(rdata1->type == 47);
  194. REQUIRE(rdata1->length != 0);
  195. REQUIRE(rdata2->length != 0);
  196. dns_rdata_toregion(rdata1, &r1);
  197. dns_rdata_toregion(rdata2, &r2);
  198. return (isc_region_compare(&r1, &r2));
  199. }
  200. static inline isc_result_t
  201. fromstruct_nsec(ARGS_FROMSTRUCT) {
  202. dns_rdata_nsec_t *nsec = source;
  203. isc_region_t region;
  204. unsigned int i, len, window, lastwindow = 0;
  205. isc_boolean_t first = ISC_TRUE;
  206. REQUIRE(type == 47);
  207. REQUIRE(source != NULL);
  208. REQUIRE(nsec->common.rdtype == type);
  209. REQUIRE(nsec->common.rdclass == rdclass);
  210. REQUIRE(nsec->typebits != NULL || nsec->len == 0);
  211. UNUSED(type);
  212. UNUSED(rdclass);
  213. dns_name_toregion(&nsec->next, &region);
  214. RETERR(isc_buffer_copyregion(target, &region));
  215. /*
  216. * Perform sanity check.
  217. */
  218. for (i = 0; i < nsec->len ; i += len) {
  219. INSIST(i + 2 <= nsec->len);
  220. window = nsec->typebits[i];
  221. len = nsec->typebits[i+1];
  222. i += 2;
  223. INSIST(first || window > lastwindow);
  224. INSIST(len > 0 && len <= 32);
  225. INSIST(i + len <= nsec->len);
  226. INSIST(nsec->typebits[i + len - 1] != 0);
  227. lastwindow = window;
  228. first = ISC_FALSE;
  229. }
  230. INSIST(!first);
  231. return (mem_tobuffer(target, nsec->typebits, nsec->len));
  232. }
  233. static inline isc_result_t
  234. tostruct_nsec(ARGS_TOSTRUCT) {
  235. isc_region_t region;
  236. dns_rdata_nsec_t *nsec = target;
  237. dns_name_t name;
  238. REQUIRE(rdata->type == 47);
  239. REQUIRE(target != NULL);
  240. REQUIRE(rdata->length != 0);
  241. nsec->common.rdclass = rdata->rdclass;
  242. nsec->common.rdtype = rdata->type;
  243. ISC_LINK_INIT(&nsec->common, link);
  244. dns_name_init(&name, NULL);
  245. dns_rdata_toregion(rdata, &region);
  246. dns_name_fromregion(&name, &region);
  247. isc_region_consume(&region, name_length(&name));
  248. dns_name_init(&nsec->next, NULL);
  249. RETERR(name_duporclone(&name, mctx, &nsec->next));
  250. nsec->len = region.length;
  251. nsec->typebits = mem_maybedup(mctx, region.base, region.length);
  252. if (nsec->typebits == NULL)
  253. goto cleanup;
  254. nsec->mctx = mctx;
  255. return (ISC_R_SUCCESS);
  256. cleanup:
  257. if (mctx != NULL)
  258. dns_name_free(&nsec->next, mctx);
  259. return (ISC_R_NOMEMORY);
  260. }
  261. static inline void
  262. freestruct_nsec(ARGS_FREESTRUCT) {
  263. dns_rdata_nsec_t *nsec = source;
  264. REQUIRE(source != NULL);
  265. REQUIRE(nsec->common.rdtype == 47);
  266. if (nsec->mctx == NULL)
  267. return;
  268. dns_name_free(&nsec->next, nsec->mctx);
  269. if (nsec->typebits != NULL)
  270. isc_mem_free(nsec->mctx, nsec->typebits);
  271. nsec->mctx = NULL;
  272. }
  273. static inline isc_result_t
  274. additionaldata_nsec(ARGS_ADDLDATA) {
  275. REQUIRE(rdata->type == 47);
  276. UNUSED(rdata);
  277. UNUSED(add);
  278. UNUSED(arg);
  279. return (ISC_R_SUCCESS);
  280. }
  281. static inline isc_result_t
  282. digest_nsec(ARGS_DIGEST) {
  283. isc_region_t r;
  284. REQUIRE(rdata->type == 47);
  285. dns_rdata_toregion(rdata, &r);
  286. return ((digest)(arg, &r));
  287. }
  288. static inline isc_boolean_t
  289. checkowner_nsec(ARGS_CHECKOWNER) {
  290. REQUIRE(type == 47);
  291. UNUSED(name);
  292. UNUSED(type);
  293. UNUSED(rdclass);
  294. UNUSED(wildcard);
  295. return (ISC_TRUE);
  296. }
  297. static inline isc_boolean_t
  298. checknames_nsec(ARGS_CHECKNAMES) {
  299. REQUIRE(rdata->type == 47);
  300. UNUSED(rdata);
  301. UNUSED(owner);
  302. UNUSED(bad);
  303. return (ISC_TRUE);
  304. }
  305. static inline int
  306. casecompare_nsec(ARGS_COMPARE) {
  307. isc_region_t region1;
  308. isc_region_t region2;
  309. dns_name_t name1;
  310. dns_name_t name2;
  311. int order;
  312. REQUIRE(rdata1->type == rdata2->type);
  313. REQUIRE(rdata1->rdclass == rdata2->rdclass);
  314. REQUIRE(rdata1->type == 47);
  315. REQUIRE(rdata1->length != 0);
  316. REQUIRE(rdata2->length != 0);
  317. dns_name_init(&name1, NULL);
  318. dns_name_init(&name2, NULL);
  319. dns_rdata_toregion(rdata1, &region1);
  320. dns_rdata_toregion(rdata2, &region2);
  321. dns_name_fromregion(&name1, &region1);
  322. dns_name_fromregion(&name2, &region2);
  323. order = dns_name_rdatacompare(&name1, &name2);
  324. if (order != 0)
  325. return (order);
  326. isc_region_consume(&region1, name_length(&name1));
  327. isc_region_consume(&region2, name_length(&name2));
  328. return (isc_region_compare(&region1, &region2));
  329. }
  330. #endif /* RDATA_GENERIC_NSEC_47_C */