dns_resolve.c 10 KB

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