dns_resolve.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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 int 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. if (get_uint(&buf, &ttl) < 0)
  197. goto out;
  198. if (ttl == 0)
  199. goto out;
  200. key.h.expiry_time = ttl + seconds_since_boot();
  201. ret = -ENOMEM;
  202. item = nfs_dns_lookup(cd, &key);
  203. if (item == NULL)
  204. goto out;
  205. if (key.addrlen == 0)
  206. set_bit(CACHE_NEGATIVE, &key.h.flags);
  207. item = nfs_dns_update(cd, &key, item);
  208. if (item == NULL)
  209. goto out;
  210. ret = 0;
  211. cache_put(&item->h, cd);
  212. out:
  213. return ret;
  214. }
  215. static int do_cache_lookup(struct cache_detail *cd,
  216. struct nfs_dns_ent *key,
  217. struct nfs_dns_ent **item,
  218. struct nfs_cache_defer_req *dreq)
  219. {
  220. int ret = -ENOMEM;
  221. *item = nfs_dns_lookup(cd, key);
  222. if (*item) {
  223. ret = cache_check(cd, &(*item)->h, &dreq->req);
  224. if (ret)
  225. *item = NULL;
  226. }
  227. return ret;
  228. }
  229. static int do_cache_lookup_nowait(struct cache_detail *cd,
  230. struct nfs_dns_ent *key,
  231. struct nfs_dns_ent **item)
  232. {
  233. int ret = -ENOMEM;
  234. *item = nfs_dns_lookup(cd, key);
  235. if (!*item)
  236. goto out_err;
  237. ret = -ETIMEDOUT;
  238. if (!test_bit(CACHE_VALID, &(*item)->h.flags)
  239. || (*item)->h.expiry_time < seconds_since_boot()
  240. || cd->flush_time > (*item)->h.last_refresh)
  241. goto out_put;
  242. ret = -ENOENT;
  243. if (test_bit(CACHE_NEGATIVE, &(*item)->h.flags))
  244. goto out_put;
  245. return 0;
  246. out_put:
  247. cache_put(&(*item)->h, cd);
  248. out_err:
  249. *item = NULL;
  250. return ret;
  251. }
  252. static int do_cache_lookup_wait(struct cache_detail *cd,
  253. struct nfs_dns_ent *key,
  254. struct nfs_dns_ent **item)
  255. {
  256. struct nfs_cache_defer_req *dreq;
  257. int ret = -ENOMEM;
  258. dreq = nfs_cache_defer_req_alloc();
  259. if (!dreq)
  260. goto out;
  261. ret = do_cache_lookup(cd, key, item, dreq);
  262. if (ret == -EAGAIN) {
  263. ret = nfs_cache_wait_for_upcall(dreq);
  264. if (!ret)
  265. ret = do_cache_lookup_nowait(cd, key, item);
  266. }
  267. nfs_cache_defer_req_put(dreq);
  268. out:
  269. return ret;
  270. }
  271. ssize_t nfs_dns_resolve_name(struct net *net, char *name,
  272. size_t namelen, struct sockaddr *sa, size_t salen)
  273. {
  274. struct nfs_dns_ent key = {
  275. .hostname = name,
  276. .namelen = namelen,
  277. };
  278. struct nfs_dns_ent *item = NULL;
  279. ssize_t ret;
  280. struct nfs_net *nn = net_generic(net, nfs_net_id);
  281. ret = do_cache_lookup_wait(nn->nfs_dns_resolve, &key, &item);
  282. if (ret == 0) {
  283. if (salen >= item->addrlen) {
  284. memcpy(sa, &item->addr, item->addrlen);
  285. ret = item->addrlen;
  286. } else
  287. ret = -EOVERFLOW;
  288. cache_put(&item->h, nn->nfs_dns_resolve);
  289. } else if (ret == -ENOENT)
  290. ret = -ESRCH;
  291. return ret;
  292. }
  293. int nfs_dns_resolver_cache_init(struct net *net)
  294. {
  295. int err = -ENOMEM;
  296. struct nfs_net *nn = net_generic(net, nfs_net_id);
  297. struct cache_detail *cd;
  298. struct cache_head **tbl;
  299. cd = kzalloc(sizeof(struct cache_detail), GFP_KERNEL);
  300. if (cd == NULL)
  301. goto err_cd;
  302. tbl = kzalloc(NFS_DNS_HASHTBL_SIZE * sizeof(struct cache_head *),
  303. GFP_KERNEL);
  304. if (tbl == NULL)
  305. goto err_tbl;
  306. cd->owner = THIS_MODULE,
  307. cd->hash_size = NFS_DNS_HASHTBL_SIZE,
  308. cd->hash_table = tbl,
  309. cd->name = "dns_resolve",
  310. cd->cache_put = nfs_dns_ent_put,
  311. cd->cache_upcall = nfs_dns_upcall,
  312. cd->cache_parse = nfs_dns_parse,
  313. cd->cache_show = nfs_dns_show,
  314. cd->match = nfs_dns_match,
  315. cd->init = nfs_dns_ent_init,
  316. cd->update = nfs_dns_ent_update,
  317. cd->alloc = nfs_dns_ent_alloc,
  318. nfs_cache_init(cd);
  319. err = nfs_cache_register_net(net, cd);
  320. if (err)
  321. goto err_reg;
  322. nn->nfs_dns_resolve = cd;
  323. return 0;
  324. err_reg:
  325. nfs_cache_destroy(cd);
  326. kfree(cd->hash_table);
  327. err_tbl:
  328. kfree(cd);
  329. err_cd:
  330. return err;
  331. }
  332. void nfs_dns_resolver_cache_destroy(struct net *net)
  333. {
  334. struct nfs_net *nn = net_generic(net, nfs_net_id);
  335. struct cache_detail *cd = nn->nfs_dns_resolve;
  336. nfs_cache_unregister_net(net, cd);
  337. nfs_cache_destroy(cd);
  338. kfree(cd->hash_table);
  339. kfree(cd);
  340. }
  341. static int rpc_pipefs_event(struct notifier_block *nb, unsigned long event,
  342. void *ptr)
  343. {
  344. struct super_block *sb = ptr;
  345. struct net *net = sb->s_fs_info;
  346. struct nfs_net *nn = net_generic(net, nfs_net_id);
  347. struct cache_detail *cd = nn->nfs_dns_resolve;
  348. int ret = 0;
  349. if (cd == NULL)
  350. return 0;
  351. if (!try_module_get(THIS_MODULE))
  352. return 0;
  353. switch (event) {
  354. case RPC_PIPEFS_MOUNT:
  355. ret = nfs_cache_register_sb(sb, cd);
  356. break;
  357. case RPC_PIPEFS_UMOUNT:
  358. nfs_cache_unregister_sb(sb, cd);
  359. break;
  360. default:
  361. ret = -ENOTSUPP;
  362. break;
  363. }
  364. module_put(THIS_MODULE);
  365. return ret;
  366. }
  367. static struct notifier_block nfs_dns_resolver_block = {
  368. .notifier_call = rpc_pipefs_event,
  369. };
  370. int nfs_dns_resolver_init(void)
  371. {
  372. return rpc_pipefs_notifier_register(&nfs_dns_resolver_block);
  373. }
  374. void nfs_dns_resolver_destroy(void)
  375. {
  376. rpc_pipefs_notifier_unregister(&nfs_dns_resolver_block);
  377. }
  378. #endif