dns_resolve.c 10.0 KB

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