dns_resolve.c 10 KB

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