dns_resolve.c 9.5 KB

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