0020-Futher-address-union-tidying.patch 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. From ab194ed7ca433e4e2e8b2ec338bfa4e6aa886a4b Mon Sep 17 00:00:00 2001
  2. From: Simon Kelley <simon@thekelleys.org.uk>
  3. Date: Tue, 1 Jan 2019 01:35:30 +0000
  4. Subject: [PATCH 20/32] Futher address union tidying.
  5. Pass DNSKEY and DS data into cache_insert via the address argument,
  6. now these data types are included in struct all_addr.
  7. Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
  8. ---
  9. src/cache.c | 116 ++++++++++++++++----------------------------------
  10. src/dnsmasq.h | 26 +++++------
  11. src/dnssec.c | 53 +++++++++++------------
  12. 3 files changed, 73 insertions(+), 122 deletions(-)
  13. --- a/src/cache.c
  14. +++ b/src/cache.c
  15. @@ -202,9 +202,9 @@ static void cache_hash(struct crec *crec
  16. static void cache_blockdata_free(struct crec *crecp)
  17. {
  18. if (crecp->flags & F_DNSKEY)
  19. - blockdata_free(crecp->addr.key.keydata);
  20. + blockdata_free(crecp->addr.addr.addr.key.keydata);
  21. else if ((crecp->flags & F_DS) && !(crecp->flags & F_NEG))
  22. - blockdata_free(crecp->addr.ds.keydata);
  23. + blockdata_free(crecp->addr.addr.addr.ds.keydata);
  24. }
  25. #endif
  26. @@ -659,33 +659,22 @@ void cache_end_insert(void)
  27. read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->ttd, sizeof(new_chain->ttd), 0);
  28. read_write(daemon->pipe_to_parent, (unsigned char *)&flags, sizeof(flags), 0);
  29. - if (flags & (F_IPV4 | F_IPV6))
  30. + if (flags & (F_IPV4 | F_IPV6 | F_DNSKEY | F_DS))
  31. read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr, sizeof(new_chain->addr), 0);
  32. #ifdef HAVE_DNSSEC
  33. - else if (flags & F_DNSKEY)
  34. + if (flags & F_DNSKEY)
  35. {
  36. read_write(daemon->pipe_to_parent, (unsigned char *)&class, sizeof(class), 0);
  37. - read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr.key.algo, sizeof(new_chain->addr.key.algo), 0);
  38. - read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr.key.keytag, sizeof(new_chain->addr.key.keytag), 0);
  39. - read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr.key.flags, sizeof(new_chain->addr.key.flags), 0);
  40. - read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr.key.keylen, sizeof(new_chain->addr.key.keylen), 0);
  41. - blockdata_write(new_chain->addr.key.keydata, new_chain->addr.key.keylen, daemon->pipe_to_parent);
  42. + blockdata_write(new_chain->addr.addr.addr.key.keydata, new_chain->addr.addr.addr.key.keylen, daemon->pipe_to_parent);
  43. }
  44. else if (flags & F_DS)
  45. {
  46. read_write(daemon->pipe_to_parent, (unsigned char *)&class, sizeof(class), 0);
  47. /* A negative DS entry is possible and has no data, obviously. */
  48. if (!(flags & F_NEG))
  49. - {
  50. - read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr.ds.algo, sizeof(new_chain->addr.ds.algo), 0);
  51. - read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr.ds.keytag, sizeof(new_chain->addr.ds.keytag), 0);
  52. - read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr.ds.digest, sizeof(new_chain->addr.ds.digest), 0);
  53. - read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr.ds.keylen, sizeof(new_chain->addr.ds.keylen), 0);
  54. - blockdata_write(new_chain->addr.ds.keydata, new_chain->addr.ds.keylen, daemon->pipe_to_parent);
  55. - }
  56. + blockdata_write(new_chain->addr.addr.addr.ds.keydata, new_chain->addr.addr.addr.ds.keylen, daemon->pipe_to_parent);
  57. }
  58. #endif
  59. -
  60. }
  61. }
  62. @@ -736,11 +725,30 @@ int cache_recv_insert(time_t now, int fd
  63. ttl = difftime(ttd, now);
  64. - if (flags & (F_IPV4 | F_IPV6))
  65. + if (flags & (F_IPV4 | F_IPV6 | F_DNSKEY | F_DS))
  66. {
  67. + unsigned short class = C_IN;
  68. +
  69. if (!read_write(fd, (unsigned char *)&addr, sizeof(addr), 1))
  70. return 0;
  71. - crecp = really_insert(daemon->namebuff, &addr, C_IN, now, ttl, flags);
  72. +
  73. +#ifdef HAVE_DNSSEC
  74. + if (flags & F_DNSKEY)
  75. + {
  76. + if (!read_write(fd, (unsigned char *)&class, sizeof(class), 1) ||
  77. + !(addr.addr.key.keydata = blockdata_read(fd, addr.addr.key.keylen)))
  78. + return 0;
  79. + }
  80. + else if (flags & F_DS)
  81. + {
  82. + if (!read_write(fd, (unsigned char *)&class, sizeof(class), 1) ||
  83. + (flags & F_NEG) ||
  84. + !(addr.addr.key.keydata = blockdata_read(fd, addr.addr.key.keylen)))
  85. + return 0;
  86. + }
  87. +#endif
  88. +
  89. + crecp = really_insert(daemon->namebuff, &addr, class, now, ttl, flags);
  90. }
  91. else if (flags & F_CNAME)
  92. {
  93. @@ -764,58 +772,6 @@ int cache_recv_insert(time_t now, int fd
  94. }
  95. }
  96. }
  97. -#ifdef HAVE_DNSSEC
  98. - else if (flags & (F_DNSKEY | F_DS))
  99. - {
  100. - unsigned short class, keylen, keyflags, keytag;
  101. - unsigned char algo, digest;
  102. - struct blockdata *keydata;
  103. -
  104. - if (!read_write(fd, (unsigned char *)&class, sizeof(class), 1))
  105. - return 0;
  106. -
  107. - crecp = really_insert(daemon->namebuff, NULL, class, now, ttl, flags);
  108. -
  109. - if (flags & F_DNSKEY)
  110. - {
  111. - if (!read_write(fd, (unsigned char *)&algo, sizeof(algo), 1) ||
  112. - !read_write(fd, (unsigned char *)&keytag, sizeof(keytag), 1) ||
  113. - !read_write(fd, (unsigned char *)&keyflags, sizeof(keyflags), 1) ||
  114. - !read_write(fd, (unsigned char *)&keylen, sizeof(keylen), 1) ||
  115. - !(keydata = blockdata_read(fd, keylen)))
  116. - return 0;
  117. - }
  118. - else if (!(flags & F_NEG))
  119. - {
  120. - if (!read_write(fd, (unsigned char *)&algo, sizeof(algo), 1) ||
  121. - !read_write(fd, (unsigned char *)&keytag, sizeof(keytag), 1) ||
  122. - !read_write(fd, (unsigned char *)&digest, sizeof(digest), 1) ||
  123. - !read_write(fd, (unsigned char *)&keylen, sizeof(keylen), 1) ||
  124. - !(keydata = blockdata_read(fd, keylen)))
  125. - return 0;
  126. - }
  127. -
  128. - if (crecp)
  129. - {
  130. - if (flags & F_DNSKEY)
  131. - {
  132. - crecp->addr.key.algo = algo;
  133. - crecp->addr.key.keytag = keytag;
  134. - crecp->addr.key.flags = flags;
  135. - crecp->addr.key.keylen = keylen;
  136. - crecp->addr.key.keydata = keydata;
  137. - }
  138. - else if (!(flags & F_NEG))
  139. - {
  140. - crecp->addr.ds.algo = algo;
  141. - crecp->addr.ds.keytag = keytag;
  142. - crecp->addr.ds.digest = digest;
  143. - crecp->addr.ds.keylen = keylen;
  144. - crecp->addr.ds.keydata = keydata;
  145. - }
  146. - }
  147. - }
  148. -#endif
  149. }
  150. }
  151. @@ -1290,15 +1246,15 @@ void cache_reload(void)
  152. #ifdef HAVE_DNSSEC
  153. for (ds = daemon->ds; ds; ds = ds->next)
  154. if ((cache = whine_malloc(SIZEOF_POINTER_CREC)) &&
  155. - (cache->addr.ds.keydata = blockdata_alloc(ds->digest, ds->digestlen)))
  156. + (cache->addr.addr.addr.ds.keydata = blockdata_alloc(ds->digest, ds->digestlen)))
  157. {
  158. cache->flags = F_FORWARD | F_IMMORTAL | F_DS | F_CONFIG | F_NAMEP;
  159. cache->ttd = daemon->local_ttl;
  160. cache->name.namep = ds->name;
  161. - cache->addr.ds.keylen = ds->digestlen;
  162. - cache->addr.ds.algo = ds->algo;
  163. - cache->addr.ds.keytag = ds->keytag;
  164. - cache->addr.ds.digest = ds->digest_type;
  165. + cache->addr.addr.addr.ds.keylen = ds->digestlen;
  166. + cache->addr.addr.addr.ds.algo = ds->algo;
  167. + cache->addr.addr.addr.ds.keytag = ds->keytag;
  168. + cache->addr.addr.addr.ds.digest = ds->digest_type;
  169. cache->uid = ds->class;
  170. cache_hash(cache);
  171. make_non_terminals(cache);
  172. @@ -1775,12 +1731,12 @@ void dump_cache(time_t now)
  173. else if (cache->flags & F_DS)
  174. {
  175. if (!(cache->flags & F_NEG))
  176. - sprintf(a, "%5u %3u %3u", cache->addr.ds.keytag,
  177. - cache->addr.ds.algo, cache->addr.ds.digest);
  178. + sprintf(a, "%5u %3u %3u", cache->addr.addr.addr.ds.keytag,
  179. + cache->addr.addr.addr.ds.algo, cache->addr.addr.addr.ds.digest);
  180. }
  181. else if (cache->flags & F_DNSKEY)
  182. - sprintf(a, "%5u %3u %3u", cache->addr.key.keytag,
  183. - cache->addr.key.algo, cache->addr.key.flags);
  184. + sprintf(a, "%5u %3u %3u", cache->addr.addr.addr.key.keytag,
  185. + cache->addr.addr.addr.key.algo, cache->addr.addr.addr.key.flags);
  186. #endif
  187. else if (!(cache->flags & F_NEG) || !(cache->flags & F_FORWARD))
  188. {
  189. --- a/src/dnsmasq.h
  190. +++ b/src/dnsmasq.h
  191. @@ -277,14 +277,21 @@ struct all_addr {
  192. union {
  193. struct in_addr addr4;
  194. struct in6_addr addr6;
  195. + struct {
  196. + struct blockdata *keydata;
  197. + unsigned short keylen, flags, keytag;
  198. + unsigned char algo;
  199. + } key;
  200. + struct {
  201. + struct blockdata *keydata;
  202. + unsigned short keylen, keytag;
  203. + unsigned char algo;
  204. + unsigned char digest;
  205. + } ds;
  206. /* for log_query */
  207. struct {
  208. unsigned short keytag, algo, digest, rcode;
  209. } log;
  210. - /* for cache_insert of DNSKEY, DS */
  211. - struct {
  212. - unsigned short class, type;
  213. - } dnssec;
  214. } addr;
  215. };
  216. @@ -414,17 +421,6 @@ struct crec {
  217. } target;
  218. unsigned int uid; /* 0 if union is interface-name */
  219. } cname;
  220. - struct {
  221. - struct blockdata *keydata;
  222. - unsigned short keylen, flags, keytag;
  223. - unsigned char algo;
  224. - } key;
  225. - struct {
  226. - struct blockdata *keydata;
  227. - unsigned short keylen, keytag;
  228. - unsigned char algo;
  229. - unsigned char digest;
  230. - } ds;
  231. } addr;
  232. time_t ttd; /* time to die */
  233. /* used as class if DNSKEY/DS, index to source for F_HOSTS */
  234. --- a/src/dnssec.c
  235. +++ b/src/dnssec.c
  236. @@ -628,10 +628,10 @@ static int validate_rrset(time_t now, st
  237. {
  238. /* iterate through all possible keys 4035 5.3.1 */
  239. for (; crecp; crecp = cache_find_by_name(crecp, keyname, now, F_DNSKEY))
  240. - if (crecp->addr.key.algo == algo &&
  241. - crecp->addr.key.keytag == key_tag &&
  242. + if (crecp->addr.addr.addr.key.algo == algo &&
  243. + crecp->addr.addr.addr.key.keytag == key_tag &&
  244. crecp->uid == (unsigned int)class &&
  245. - verify(crecp->addr.key.keydata, crecp->addr.key.keylen, sig, sig_len, digest, hash->digest_size, algo))
  246. + verify(crecp->addr.addr.addr.key.keydata, crecp->addr.addr.addr.key.keylen, sig, sig_len, digest, hash->digest_size, algo))
  247. return (labels < name_labels) ? STAT_SECURE_WILDCARD : STAT_SECURE;
  248. }
  249. }
  250. @@ -728,10 +728,10 @@ int dnssec_validate_by_ds(time_t now, st
  251. const struct nettle_hash *hash;
  252. int sigcnt, rrcnt;
  253. - if (recp1->addr.ds.algo == algo &&
  254. - recp1->addr.ds.keytag == keytag &&
  255. + if (recp1->addr.addr.addr.ds.algo == algo &&
  256. + recp1->addr.addr.addr.ds.keytag == keytag &&
  257. recp1->uid == (unsigned int)class &&
  258. - (hash = hash_find(ds_digest_name(recp1->addr.ds.digest))) &&
  259. + (hash = hash_find(ds_digest_name(recp1->addr.addr.addr.ds.digest))) &&
  260. hash_init(hash, &ctx, &digest))
  261. {
  262. @@ -746,9 +746,9 @@ int dnssec_validate_by_ds(time_t now, st
  263. from_wire(name);
  264. if (!(recp1->flags & F_NEG) &&
  265. - recp1->addr.ds.keylen == (int)hash->digest_size &&
  266. - (ds_digest = blockdata_retrieve(recp1->addr.key.keydata, recp1->addr.ds.keylen, NULL)) &&
  267. - memcmp(ds_digest, digest, recp1->addr.ds.keylen) == 0 &&
  268. + recp1->addr.addr.addr.ds.keylen == (int)hash->digest_size &&
  269. + (ds_digest = blockdata_retrieve(recp1->addr.addr.addr.ds.keydata, recp1->addr.addr.addr.ds.keylen, NULL)) &&
  270. + memcmp(ds_digest, digest, recp1->addr.addr.addr.ds.keylen) == 0 &&
  271. explore_rrset(header, plen, class, T_DNSKEY, name, keyname, &sigcnt, &rrcnt) &&
  272. sigcnt != 0 && rrcnt != 0 &&
  273. validate_rrset(now, header, plen, class, T_DNSKEY, sigcnt, rrcnt, name, keyname,
  274. @@ -800,7 +800,13 @@ int dnssec_validate_by_ds(time_t now, st
  275. if ((key = blockdata_alloc((char*)p, rdlen - 4)))
  276. {
  277. - if (!(recp1 = cache_insert(name, &a, class, now, ttl, F_FORWARD | F_DNSKEY | F_DNSSECOK)))
  278. + a.addr.key.keylen = rdlen - 4;
  279. + a.addr.key.keydata = key;
  280. + a.addr.key.algo = algo;
  281. + a.addr.key.keytag = keytag;
  282. + a.addr.key.flags = flags;
  283. +
  284. + if (!cache_insert(name, &a, class, now, ttl, F_FORWARD | F_DNSKEY | F_DNSSECOK))
  285. {
  286. blockdata_free(key);
  287. return STAT_BOGUS;
  288. @@ -813,12 +819,6 @@ int dnssec_validate_by_ds(time_t now, st
  289. log_query(F_NOEXTRA | F_KEYTAG | F_UPSTREAM, name, &a, "DNSKEY keytag %hu, algo %hu");
  290. else
  291. log_query(F_NOEXTRA | F_KEYTAG | F_UPSTREAM, name, &a, "DNSKEY keytag %hu, algo %hu (not supported)");
  292. -
  293. - recp1->addr.key.keylen = rdlen - 4;
  294. - recp1->addr.key.keydata = key;
  295. - recp1->addr.key.algo = algo;
  296. - recp1->addr.key.keytag = keytag;
  297. - recp1->addr.key.flags = flags;
  298. }
  299. }
  300. }
  301. @@ -915,8 +915,7 @@ int dnssec_validate_ds(time_t now, struc
  302. int algo, digest, keytag;
  303. unsigned char *psave = p;
  304. struct blockdata *key;
  305. - struct crec *crecp;
  306. -
  307. +
  308. if (rdlen < 4)
  309. return STAT_BOGUS; /* bad packet */
  310. @@ -926,7 +925,13 @@ int dnssec_validate_ds(time_t now, struc
  311. if ((key = blockdata_alloc((char*)p, rdlen - 4)))
  312. {
  313. - if (!(crecp = cache_insert(name, NULL, class, now, ttl, F_FORWARD | F_DS | F_DNSSECOK)))
  314. + a.addr.ds.digest = digest;
  315. + a.addr.ds.keydata = key;
  316. + a.addr.ds.algo = algo;
  317. + a.addr.ds.keytag = keytag;
  318. + a.addr.ds.keylen = rdlen - 4;
  319. +
  320. + if (!cache_insert(name, &a, class, now, ttl, F_FORWARD | F_DS | F_DNSSECOK))
  321. {
  322. blockdata_free(key);
  323. return STAT_BOGUS;
  324. @@ -940,12 +945,6 @@ int dnssec_validate_ds(time_t now, struc
  325. log_query(F_NOEXTRA | F_KEYTAG | F_UPSTREAM, name, &a, "DS keytag %hu, algo %hu, digest %hu");
  326. else
  327. log_query(F_NOEXTRA | F_KEYTAG | F_UPSTREAM, name, &a, "DS keytag %hu, algo %hu, digest %hu (not supported)");
  328. -
  329. - crecp->addr.ds.digest = digest;
  330. - crecp->addr.ds.keydata = key;
  331. - crecp->addr.ds.algo = algo;
  332. - crecp->addr.ds.keytag = keytag;
  333. - crecp->addr.ds.keylen = rdlen - 4;
  334. }
  335. }
  336. @@ -1711,8 +1710,8 @@ static int zone_status(char *name, int c
  337. do
  338. {
  339. if (crecp->uid == (unsigned int)class &&
  340. - ds_digest_name(crecp->addr.ds.digest) &&
  341. - algo_digest_name(crecp->addr.ds.algo))
  342. + ds_digest_name(crecp->addr.addr.addr.ds.digest) &&
  343. + algo_digest_name(crecp->addr.addr.addr.ds.algo))
  344. break;
  345. }
  346. while ((crecp = cache_find_by_name(crecp, keyname, now, F_DS)));