dns_resolve.c 7.3 KB

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