dns_resolve.c 10 KB

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