dns_resolve.c 9.6 KB

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