dns_resolve.c 10 KB

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