dns_resolve.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * linux/fs/nfs/dns_resolve.c
  3. *
  4. * Copyright (c) 2009 Trond Myklebust <Trond.Myklebust@netapp.com>
  5. *
  6. * Resolves DNS hostnames into valid ip addresses
  7. */
  8. #include <linux/hash.h>
  9. #include <linux/string.h>
  10. #include <linux/kmod.h>
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/socket.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/inet.h>
  16. #include <linux/sunrpc/clnt.h>
  17. #include <linux/sunrpc/cache.h>
  18. #include <linux/sunrpc/svcauth.h>
  19. #include "dns_resolve.h"
  20. #include "cache_lib.h"
  21. #define NFS_DNS_HASHBITS 4
  22. #define NFS_DNS_HASHTBL_SIZE (1 << NFS_DNS_HASHBITS)
  23. static struct cache_head *nfs_dns_table[NFS_DNS_HASHTBL_SIZE];
  24. struct nfs_dns_ent {
  25. struct cache_head h;
  26. char *hostname;
  27. size_t namelen;
  28. struct sockaddr_storage addr;
  29. size_t addrlen;
  30. };
  31. static void nfs_dns_ent_update(struct cache_head *cnew,
  32. struct cache_head *ckey)
  33. {
  34. struct nfs_dns_ent *new;
  35. struct nfs_dns_ent *key;
  36. new = container_of(cnew, struct nfs_dns_ent, h);
  37. key = container_of(ckey, struct nfs_dns_ent, h);
  38. memcpy(&new->addr, &key->addr, key->addrlen);
  39. new->addrlen = key->addrlen;
  40. }
  41. static void nfs_dns_ent_init(struct cache_head *cnew,
  42. struct cache_head *ckey)
  43. {
  44. struct nfs_dns_ent *new;
  45. struct nfs_dns_ent *key;
  46. new = container_of(cnew, struct nfs_dns_ent, h);
  47. key = container_of(ckey, struct nfs_dns_ent, h);
  48. kfree(new->hostname);
  49. new->hostname = kstrndup(key->hostname, key->namelen, GFP_KERNEL);
  50. if (new->hostname) {
  51. new->namelen = key->namelen;
  52. nfs_dns_ent_update(cnew, ckey);
  53. } else {
  54. new->namelen = 0;
  55. new->addrlen = 0;
  56. }
  57. }
  58. static void nfs_dns_ent_put(struct kref *ref)
  59. {
  60. struct nfs_dns_ent *item;
  61. item = container_of(ref, struct nfs_dns_ent, h.ref);
  62. kfree(item->hostname);
  63. kfree(item);
  64. }
  65. static struct cache_head *nfs_dns_ent_alloc(void)
  66. {
  67. struct nfs_dns_ent *item = kmalloc(sizeof(*item), GFP_KERNEL);
  68. if (item != NULL) {
  69. item->hostname = NULL;
  70. item->namelen = 0;
  71. item->addrlen = 0;
  72. return &item->h;
  73. }
  74. return NULL;
  75. };
  76. static unsigned int nfs_dns_hash(const struct nfs_dns_ent *key)
  77. {
  78. return hash_str(key->hostname, NFS_DNS_HASHBITS);
  79. }
  80. static void nfs_dns_request(struct cache_detail *cd,
  81. struct cache_head *ch,
  82. char **bpp, int *blen)
  83. {
  84. struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
  85. qword_add(bpp, blen, key->hostname);
  86. (*bpp)[-1] = '\n';
  87. }
  88. static int nfs_dns_upcall(struct cache_detail *cd,
  89. struct cache_head *ch)
  90. {
  91. struct nfs_dns_ent *key = container_of(ch, struct nfs_dns_ent, h);
  92. int ret;
  93. ret = nfs_cache_upcall(cd, key->hostname);
  94. if (ret)
  95. ret = sunrpc_cache_pipe_upcall(cd, ch, nfs_dns_request);
  96. return ret;
  97. }
  98. static int nfs_dns_match(struct cache_head *ca,
  99. struct cache_head *cb)
  100. {
  101. struct nfs_dns_ent *a;
  102. struct nfs_dns_ent *b;
  103. a = container_of(ca, struct nfs_dns_ent, h);
  104. b = container_of(cb, struct nfs_dns_ent, h);
  105. if (a->namelen == 0 || a->namelen != b->namelen)
  106. return 0;
  107. return memcmp(a->hostname, b->hostname, a->namelen) == 0;
  108. }
  109. static int nfs_dns_show(struct seq_file *m, struct cache_detail *cd,
  110. struct cache_head *h)
  111. {
  112. struct nfs_dns_ent *item;
  113. long ttl;
  114. if (h == NULL) {
  115. seq_puts(m, "# ip address hostname ttl\n");
  116. return 0;
  117. }
  118. item = container_of(h, struct nfs_dns_ent, h);
  119. ttl = (long)item->h.expiry_time - (long)get_seconds();
  120. if (ttl < 0)
  121. ttl = 0;
  122. if (!test_bit(CACHE_NEGATIVE, &h->flags)) {
  123. char buf[INET6_ADDRSTRLEN+IPV6_SCOPE_ID_LEN+1];
  124. rpc_ntop((struct sockaddr *)&item->addr, buf, sizeof(buf));
  125. seq_printf(m, "%15s ", buf);
  126. } else
  127. seq_puts(m, "<none> ");
  128. seq_printf(m, "%15s %ld\n", item->hostname, ttl);
  129. return 0;
  130. }
  131. static struct nfs_dns_ent *nfs_dns_lookup(struct cache_detail *cd,
  132. struct nfs_dns_ent *key)
  133. {
  134. struct cache_head *ch;
  135. ch = sunrpc_cache_lookup(cd,
  136. &key->h,
  137. nfs_dns_hash(key));
  138. if (!ch)
  139. return NULL;
  140. return container_of(ch, struct nfs_dns_ent, h);
  141. }
  142. static struct nfs_dns_ent *nfs_dns_update(struct cache_detail *cd,
  143. struct nfs_dns_ent *new,
  144. struct nfs_dns_ent *key)
  145. {
  146. struct cache_head *ch;
  147. ch = sunrpc_cache_update(cd,
  148. &new->h, &key->h,
  149. nfs_dns_hash(key));
  150. if (!ch)
  151. return NULL;
  152. return container_of(ch, struct nfs_dns_ent, h);
  153. }
  154. static int nfs_dns_parse(struct cache_detail *cd, char *buf, int buflen)
  155. {
  156. char buf1[NFS_DNS_HOSTNAME_MAXLEN+1];
  157. struct nfs_dns_ent key, *item;
  158. unsigned long ttl;
  159. ssize_t len;
  160. int ret = -EINVAL;
  161. if (buf[buflen-1] != '\n')
  162. goto out;
  163. buf[buflen-1] = '\0';
  164. len = qword_get(&buf, buf1, sizeof(buf1));
  165. if (len <= 0)
  166. goto out;
  167. key.addrlen = rpc_pton(buf1, len,
  168. (struct sockaddr *)&key.addr,
  169. sizeof(key.addr));
  170. len = qword_get(&buf, buf1, sizeof(buf1));
  171. if (len <= 0)
  172. goto out;
  173. key.hostname = buf1;
  174. key.namelen = len;
  175. memset(&key.h, 0, sizeof(key.h));
  176. ttl = get_expiry(&buf);
  177. if (ttl == 0)
  178. goto out;
  179. key.h.expiry_time = ttl + get_seconds();
  180. ret = -ENOMEM;
  181. item = nfs_dns_lookup(cd, &key);
  182. if (item == NULL)
  183. goto out;
  184. if (key.addrlen == 0)
  185. set_bit(CACHE_NEGATIVE, &key.h.flags);
  186. item = nfs_dns_update(cd, &key, item);
  187. if (item == NULL)
  188. goto out;
  189. ret = 0;
  190. cache_put(&item->h, cd);
  191. out:
  192. return ret;
  193. }
  194. static struct cache_detail nfs_dns_resolve = {
  195. .owner = THIS_MODULE,
  196. .hash_size = NFS_DNS_HASHTBL_SIZE,
  197. .hash_table = nfs_dns_table,
  198. .name = "dns_resolve",
  199. .cache_put = nfs_dns_ent_put,
  200. .cache_upcall = nfs_dns_upcall,
  201. .cache_parse = nfs_dns_parse,
  202. .cache_show = nfs_dns_show,
  203. .match = nfs_dns_match,
  204. .init = nfs_dns_ent_init,
  205. .update = nfs_dns_ent_update,
  206. .alloc = nfs_dns_ent_alloc,
  207. };
  208. static int do_cache_lookup(struct cache_detail *cd,
  209. struct nfs_dns_ent *key,
  210. struct nfs_dns_ent **item,
  211. struct nfs_cache_defer_req *dreq)
  212. {
  213. int ret = -ENOMEM;
  214. *item = nfs_dns_lookup(cd, key);
  215. if (*item) {
  216. ret = cache_check(cd, &(*item)->h, &dreq->req);
  217. if (ret)
  218. *item = NULL;
  219. }
  220. return ret;
  221. }
  222. static int do_cache_lookup_nowait(struct cache_detail *cd,
  223. struct nfs_dns_ent *key,
  224. struct nfs_dns_ent **item)
  225. {
  226. int ret = -ENOMEM;
  227. *item = nfs_dns_lookup(cd, key);
  228. if (!*item)
  229. goto out_err;
  230. ret = -ETIMEDOUT;
  231. if (!test_bit(CACHE_VALID, &(*item)->h.flags)
  232. || (*item)->h.expiry_time < get_seconds()
  233. || cd->flush_time > (*item)->h.last_refresh)
  234. goto out_put;
  235. ret = -ENOENT;
  236. if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
  237. goto out_put;
  238. return 0;
  239. out_put:
  240. cache_put(&(*item)->h, cd);
  241. out_err:
  242. *item = NULL;
  243. return ret;
  244. }
  245. static int do_cache_lookup_wait(struct cache_detail *cd,
  246. struct nfs_dns_ent *key,
  247. struct nfs_dns_ent **item)
  248. {
  249. struct nfs_cache_defer_req *dreq;
  250. int ret = -ENOMEM;
  251. dreq = nfs_cache_defer_req_alloc();
  252. if (!dreq)
  253. goto out;
  254. ret = do_cache_lookup(cd, key, item, dreq);
  255. if (ret == -EAGAIN) {
  256. ret = nfs_cache_wait_for_upcall(dreq);
  257. if (!ret)
  258. ret = do_cache_lookup_nowait(cd, key, item);
  259. }
  260. nfs_cache_defer_req_put(dreq);
  261. out:
  262. return ret;
  263. }
  264. ssize_t nfs_dns_resolve_name(char *name, size_t namelen,
  265. struct sockaddr *sa, size_t salen)
  266. {
  267. struct nfs_dns_ent key = {
  268. .hostname = name,
  269. .namelen = namelen,
  270. };
  271. struct nfs_dns_ent *item = NULL;
  272. ssize_t ret;
  273. ret = do_cache_lookup_wait(&nfs_dns_resolve, &key, &item);
  274. if (ret == 0) {
  275. if (salen >= item->addrlen) {
  276. memcpy(sa, &item->addr, item->addrlen);
  277. ret = item->addrlen;
  278. } else
  279. ret = -EOVERFLOW;
  280. cache_put(&item->h, &nfs_dns_resolve);
  281. } else if (ret == -ENOENT)
  282. ret = -ESRCH;
  283. return ret;
  284. }
  285. int nfs_dns_resolver_init(void)
  286. {
  287. return nfs_cache_register(&nfs_dns_resolve);
  288. }
  289. void nfs_dns_resolver_destroy(void)
  290. {
  291. nfs_cache_unregister(&nfs_dns_resolve);
  292. }