dns_resolve.c 9.5 KB

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