dns_resolve.c 9.4 KB

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