dns_resolve.c 9.9 KB

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