dns_resolve.c 7.8 KB

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