dns_resolve.c 7.1 KB

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