dns_resolve.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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(struct net *net, 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. #include "netns.h"
  40. #define NFS_DNS_HASHBITS 4
  41. #define NFS_DNS_HASHTBL_SIZE (1 << NFS_DNS_HASHBITS)
  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 int do_cache_lookup(struct cache_detail *cd,
  213. struct nfs_dns_ent *key,
  214. struct nfs_dns_ent **item,
  215. struct nfs_cache_defer_req *dreq)
  216. {
  217. int ret = -ENOMEM;
  218. *item = nfs_dns_lookup(cd, key);
  219. if (*item) {
  220. ret = cache_check(cd, &(*item)->h, &dreq->req);
  221. if (ret)
  222. *item = NULL;
  223. }
  224. return ret;
  225. }
  226. static int do_cache_lookup_nowait(struct cache_detail *cd,
  227. struct nfs_dns_ent *key,
  228. struct nfs_dns_ent **item)
  229. {
  230. int ret = -ENOMEM;
  231. *item = nfs_dns_lookup(cd, key);
  232. if (!*item)
  233. goto out_err;
  234. ret = -ETIMEDOUT;
  235. if (!test_bit(CACHE_VALID, &(*item)->h.flags)
  236. || (*item)->h.expiry_time < seconds_since_boot()
  237. || cd->flush_time > (*item)->h.last_refresh)
  238. goto out_put;
  239. ret = -ENOENT;
  240. if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
  241. goto out_put;
  242. return 0;
  243. out_put:
  244. cache_put(&(*item)->h, cd);
  245. out_err:
  246. *item = NULL;
  247. return ret;
  248. }
  249. static int do_cache_lookup_wait(struct cache_detail *cd,
  250. struct nfs_dns_ent *key,
  251. struct nfs_dns_ent **item)
  252. {
  253. struct nfs_cache_defer_req *dreq;
  254. int ret = -ENOMEM;
  255. dreq = nfs_cache_defer_req_alloc();
  256. if (!dreq)
  257. goto out;
  258. ret = do_cache_lookup(cd, key, item, dreq);
  259. if (ret == -EAGAIN) {
  260. ret = nfs_cache_wait_for_upcall(dreq);
  261. if (!ret)
  262. ret = do_cache_lookup_nowait(cd, key, item);
  263. }
  264. nfs_cache_defer_req_put(dreq);
  265. out:
  266. return ret;
  267. }
  268. ssize_t nfs_dns_resolve_name(struct net *net, char *name,
  269. size_t namelen, struct sockaddr *sa, size_t salen)
  270. {
  271. struct nfs_dns_ent key = {
  272. .hostname = name,
  273. .namelen = namelen,
  274. };
  275. struct nfs_dns_ent *item = NULL;
  276. ssize_t ret;
  277. struct nfs_net *nn = net_generic(net, nfs_net_id);
  278. ret = do_cache_lookup_wait(nn->nfs_dns_resolve, &key, &item);
  279. if (ret == 0) {
  280. if (salen >= item->addrlen) {
  281. memcpy(sa, &item->addr, item->addrlen);
  282. ret = item->addrlen;
  283. } else
  284. ret = -EOVERFLOW;
  285. cache_put(&item->h, nn->nfs_dns_resolve);
  286. } else if (ret == -ENOENT)
  287. ret = -ESRCH;
  288. return ret;
  289. }
  290. int nfs_dns_resolver_cache_init(struct net *net)
  291. {
  292. int err = -ENOMEM;
  293. struct nfs_net *nn = net_generic(net, nfs_net_id);
  294. struct cache_detail *cd;
  295. struct cache_head **tbl;
  296. cd = kzalloc(sizeof(struct cache_detail), GFP_KERNEL);
  297. if (cd == NULL)
  298. goto err_cd;
  299. tbl = kzalloc(NFS_DNS_HASHTBL_SIZE * sizeof(struct cache_head *),
  300. GFP_KERNEL);
  301. if (tbl == NULL)
  302. goto err_tbl;
  303. cd->owner = THIS_MODULE,
  304. cd->hash_size = NFS_DNS_HASHTBL_SIZE,
  305. cd->hash_table = tbl,
  306. cd->name = "dns_resolve",
  307. cd->cache_put = nfs_dns_ent_put,
  308. cd->cache_upcall = nfs_dns_upcall,
  309. cd->cache_parse = nfs_dns_parse,
  310. cd->cache_show = nfs_dns_show,
  311. cd->match = nfs_dns_match,
  312. cd->init = nfs_dns_ent_init,
  313. cd->update = nfs_dns_ent_update,
  314. cd->alloc = nfs_dns_ent_alloc,
  315. nfs_cache_init(cd);
  316. err = nfs_cache_register_net(net, cd);
  317. if (err)
  318. goto err_reg;
  319. nn->nfs_dns_resolve = cd;
  320. return 0;
  321. err_reg:
  322. nfs_cache_destroy(cd);
  323. kfree(cd->hash_table);
  324. err_tbl:
  325. kfree(cd);
  326. err_cd:
  327. return err;
  328. }
  329. void nfs_dns_resolver_cache_destroy(struct net *net)
  330. {
  331. struct nfs_net *nn = net_generic(net, nfs_net_id);
  332. struct cache_detail *cd = nn->nfs_dns_resolve;
  333. nfs_cache_unregister_net(net, cd);
  334. nfs_cache_destroy(cd);
  335. kfree(cd->hash_table);
  336. kfree(cd);
  337. }
  338. int nfs_dns_resolver_init(void)
  339. {
  340. return 0;
  341. }
  342. void nfs_dns_resolver_destroy(void)
  343. {
  344. }
  345. #endif