callback.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. * linux/fs/nfs/callback.c
  3. *
  4. * Copyright (C) 2004 Trond Myklebust
  5. *
  6. * NFSv4 callback handling
  7. */
  8. #include <linux/completion.h>
  9. #include <linux/ip.h>
  10. #include <linux/module.h>
  11. #include <linux/sunrpc/svc.h>
  12. #include <linux/sunrpc/svcsock.h>
  13. #include <linux/nfs_fs.h>
  14. #include <linux/errno.h>
  15. #include <linux/mutex.h>
  16. #include <linux/freezer.h>
  17. #include <linux/kthread.h>
  18. #include <linux/sunrpc/svcauth_gss.h>
  19. #include <linux/sunrpc/bc_xprt.h>
  20. #include <net/inet_sock.h>
  21. #include "nfs4_fs.h"
  22. #include "callback.h"
  23. #include "internal.h"
  24. #include "netns.h"
  25. #define NFSDBG_FACILITY NFSDBG_CALLBACK
  26. struct nfs_callback_data {
  27. unsigned int users;
  28. struct svc_serv *serv;
  29. };
  30. static struct nfs_callback_data nfs_callback_info[NFS4_MAX_MINOR_VERSION + 1];
  31. static DEFINE_MUTEX(nfs_callback_mutex);
  32. static struct svc_program nfs4_callback_program;
  33. static int nfs4_callback_up_net(struct svc_serv *serv, struct net *net)
  34. {
  35. int ret;
  36. struct nfs_net *nn = net_generic(net, nfs_net_id);
  37. ret = svc_create_xprt(serv, "tcp", net, PF_INET,
  38. nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS);
  39. if (ret <= 0)
  40. goto out_err;
  41. nn->nfs_callback_tcpport = ret;
  42. dprintk("NFS: Callback listener port = %u (af %u, net %p)\n",
  43. nn->nfs_callback_tcpport, PF_INET, net);
  44. ret = svc_create_xprt(serv, "tcp", net, PF_INET6,
  45. nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS);
  46. if (ret > 0) {
  47. nn->nfs_callback_tcpport6 = ret;
  48. dprintk("NFS: Callback listener port = %u (af %u, net %p)\n",
  49. nn->nfs_callback_tcpport6, PF_INET6, net);
  50. } else if (ret != -EAFNOSUPPORT)
  51. goto out_err;
  52. return 0;
  53. out_err:
  54. return (ret) ? ret : -ENOMEM;
  55. }
  56. /*
  57. * This is the NFSv4 callback kernel thread.
  58. */
  59. static int
  60. nfs4_callback_svc(void *vrqstp)
  61. {
  62. int err;
  63. struct svc_rqst *rqstp = vrqstp;
  64. set_freezable();
  65. while (!kthread_freezable_should_stop(NULL)) {
  66. if (signal_pending(current))
  67. flush_signals(current);
  68. /*
  69. * Listen for a request on the socket
  70. */
  71. err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
  72. if (err == -EAGAIN || err == -EINTR)
  73. continue;
  74. svc_process(rqstp);
  75. }
  76. svc_exit_thread(rqstp);
  77. module_put_and_exit(0);
  78. return 0;
  79. }
  80. #if defined(CONFIG_NFS_V4_1)
  81. /*
  82. * The callback service for NFSv4.1 callbacks
  83. */
  84. static int
  85. nfs41_callback_svc(void *vrqstp)
  86. {
  87. struct svc_rqst *rqstp = vrqstp;
  88. struct svc_serv *serv = rqstp->rq_server;
  89. struct rpc_rqst *req;
  90. int error;
  91. DEFINE_WAIT(wq);
  92. set_freezable();
  93. while (!kthread_freezable_should_stop(NULL)) {
  94. if (signal_pending(current))
  95. flush_signals(current);
  96. prepare_to_wait(&serv->sv_cb_waitq, &wq, TASK_INTERRUPTIBLE);
  97. spin_lock_bh(&serv->sv_cb_lock);
  98. if (!list_empty(&serv->sv_cb_list)) {
  99. req = list_first_entry(&serv->sv_cb_list,
  100. struct rpc_rqst, rq_bc_list);
  101. list_del(&req->rq_bc_list);
  102. spin_unlock_bh(&serv->sv_cb_lock);
  103. finish_wait(&serv->sv_cb_waitq, &wq);
  104. dprintk("Invoking bc_svc_process()\n");
  105. error = bc_svc_process(serv, req, rqstp);
  106. dprintk("bc_svc_process() returned w/ error code= %d\n",
  107. error);
  108. } else {
  109. spin_unlock_bh(&serv->sv_cb_lock);
  110. if (!kthread_should_stop())
  111. schedule();
  112. finish_wait(&serv->sv_cb_waitq, &wq);
  113. }
  114. }
  115. svc_exit_thread(rqstp);
  116. module_put_and_exit(0);
  117. return 0;
  118. }
  119. static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt,
  120. struct svc_serv *serv)
  121. {
  122. if (minorversion)
  123. /*
  124. * Save the svc_serv in the transport so that it can
  125. * be referenced when the session backchannel is initialized
  126. */
  127. xprt->bc_serv = serv;
  128. }
  129. #else
  130. static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt,
  131. struct svc_serv *serv)
  132. {
  133. }
  134. #endif /* CONFIG_NFS_V4_1 */
  135. static int nfs_callback_start_svc(int minorversion, struct rpc_xprt *xprt,
  136. struct svc_serv *serv)
  137. {
  138. int nrservs = nfs_callback_nr_threads;
  139. int ret;
  140. nfs_callback_bc_serv(minorversion, xprt, serv);
  141. if (nrservs < NFS4_MIN_NR_CALLBACK_THREADS)
  142. nrservs = NFS4_MIN_NR_CALLBACK_THREADS;
  143. if (serv->sv_nrthreads-1 == nrservs)
  144. return 0;
  145. ret = serv->sv_ops->svo_setup(serv, NULL, nrservs);
  146. if (ret) {
  147. serv->sv_ops->svo_setup(serv, NULL, 0);
  148. return ret;
  149. }
  150. dprintk("nfs_callback_up: service started\n");
  151. return 0;
  152. }
  153. static void nfs_callback_down_net(u32 minorversion, struct svc_serv *serv, struct net *net)
  154. {
  155. struct nfs_net *nn = net_generic(net, nfs_net_id);
  156. if (--nn->cb_users[minorversion])
  157. return;
  158. dprintk("NFS: destroy per-net callback data; net=%p\n", net);
  159. svc_shutdown_net(serv, net);
  160. }
  161. static int nfs_callback_up_net(int minorversion, struct svc_serv *serv,
  162. struct net *net, struct rpc_xprt *xprt)
  163. {
  164. struct nfs_net *nn = net_generic(net, nfs_net_id);
  165. int ret;
  166. if (nn->cb_users[minorversion]++)
  167. return 0;
  168. dprintk("NFS: create per-net callback data; net=%p\n", net);
  169. ret = svc_bind(serv, net);
  170. if (ret < 0) {
  171. printk(KERN_WARNING "NFS: bind callback service failed\n");
  172. goto err_bind;
  173. }
  174. ret = -EPROTONOSUPPORT;
  175. if (!IS_ENABLED(CONFIG_NFS_V4_1) || minorversion == 0)
  176. ret = nfs4_callback_up_net(serv, net);
  177. else if (xprt->ops->bc_up)
  178. ret = xprt->ops->bc_up(serv, net);
  179. if (ret < 0) {
  180. printk(KERN_ERR "NFS: callback service start failed\n");
  181. goto err_socks;
  182. }
  183. return 0;
  184. err_socks:
  185. svc_rpcb_cleanup(serv, net);
  186. err_bind:
  187. nn->cb_users[minorversion]--;
  188. dprintk("NFS: Couldn't create callback socket: err = %d; "
  189. "net = %p\n", ret, net);
  190. return ret;
  191. }
  192. static struct svc_serv_ops nfs40_cb_sv_ops = {
  193. .svo_function = nfs4_callback_svc,
  194. .svo_enqueue_xprt = svc_xprt_do_enqueue,
  195. .svo_setup = svc_set_num_threads_sync,
  196. .svo_module = THIS_MODULE,
  197. };
  198. #if defined(CONFIG_NFS_V4_1)
  199. static struct svc_serv_ops nfs41_cb_sv_ops = {
  200. .svo_function = nfs41_callback_svc,
  201. .svo_enqueue_xprt = svc_xprt_do_enqueue,
  202. .svo_setup = svc_set_num_threads_sync,
  203. .svo_module = THIS_MODULE,
  204. };
  205. static struct svc_serv_ops *nfs4_cb_sv_ops[] = {
  206. [0] = &nfs40_cb_sv_ops,
  207. [1] = &nfs41_cb_sv_ops,
  208. };
  209. #else
  210. static struct svc_serv_ops *nfs4_cb_sv_ops[] = {
  211. [0] = &nfs40_cb_sv_ops,
  212. [1] = NULL,
  213. };
  214. #endif
  215. static struct svc_serv *nfs_callback_create_svc(int minorversion)
  216. {
  217. struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
  218. struct svc_serv *serv;
  219. struct svc_serv_ops *sv_ops;
  220. /*
  221. * Check whether we're already up and running.
  222. */
  223. if (cb_info->serv) {
  224. /*
  225. * Note: increase service usage, because later in case of error
  226. * svc_destroy() will be called.
  227. */
  228. svc_get(cb_info->serv);
  229. return cb_info->serv;
  230. }
  231. switch (minorversion) {
  232. case 0:
  233. sv_ops = nfs4_cb_sv_ops[0];
  234. break;
  235. default:
  236. sv_ops = nfs4_cb_sv_ops[1];
  237. }
  238. if (sv_ops == NULL)
  239. return ERR_PTR(-ENOTSUPP);
  240. /*
  241. * Sanity check: if there's no task,
  242. * we should be the first user ...
  243. */
  244. if (cb_info->users)
  245. printk(KERN_WARNING "nfs_callback_create_svc: no kthread, %d users??\n",
  246. cb_info->users);
  247. serv = svc_create_pooled(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, sv_ops);
  248. if (!serv) {
  249. printk(KERN_ERR "nfs_callback_create_svc: create service failed\n");
  250. return ERR_PTR(-ENOMEM);
  251. }
  252. cb_info->serv = serv;
  253. /* As there is only one thread we need to over-ride the
  254. * default maximum of 80 connections
  255. */
  256. serv->sv_maxconn = 1024;
  257. dprintk("nfs_callback_create_svc: service created\n");
  258. return serv;
  259. }
  260. /*
  261. * Bring up the callback thread if it is not already up.
  262. */
  263. int nfs_callback_up(u32 minorversion, struct rpc_xprt *xprt)
  264. {
  265. struct svc_serv *serv;
  266. struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
  267. int ret;
  268. struct net *net = xprt->xprt_net;
  269. mutex_lock(&nfs_callback_mutex);
  270. serv = nfs_callback_create_svc(minorversion);
  271. if (IS_ERR(serv)) {
  272. ret = PTR_ERR(serv);
  273. goto err_create;
  274. }
  275. ret = nfs_callback_up_net(minorversion, serv, net, xprt);
  276. if (ret < 0)
  277. goto err_net;
  278. ret = nfs_callback_start_svc(minorversion, xprt, serv);
  279. if (ret < 0)
  280. goto err_start;
  281. cb_info->users++;
  282. /*
  283. * svc_create creates the svc_serv with sv_nrthreads == 1, and then
  284. * svc_prepare_thread increments that. So we need to call svc_destroy
  285. * on both success and failure so that the refcount is 1 when the
  286. * thread exits.
  287. */
  288. err_net:
  289. if (!cb_info->users)
  290. cb_info->serv = NULL;
  291. svc_destroy(serv);
  292. err_create:
  293. mutex_unlock(&nfs_callback_mutex);
  294. return ret;
  295. err_start:
  296. nfs_callback_down_net(minorversion, serv, net);
  297. dprintk("NFS: Couldn't create server thread; err = %d\n", ret);
  298. goto err_net;
  299. }
  300. /*
  301. * Kill the callback thread if it's no longer being used.
  302. */
  303. void nfs_callback_down(int minorversion, struct net *net)
  304. {
  305. struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
  306. struct svc_serv *serv;
  307. mutex_lock(&nfs_callback_mutex);
  308. serv = cb_info->serv;
  309. nfs_callback_down_net(minorversion, serv, net);
  310. cb_info->users--;
  311. if (cb_info->users == 0) {
  312. svc_get(serv);
  313. serv->sv_ops->svo_setup(serv, NULL, 0);
  314. svc_destroy(serv);
  315. dprintk("nfs_callback_down: service destroyed\n");
  316. cb_info->serv = NULL;
  317. }
  318. mutex_unlock(&nfs_callback_mutex);
  319. }
  320. /* Boolean check of RPC_AUTH_GSS principal */
  321. int
  322. check_gss_callback_principal(struct nfs_client *clp, struct svc_rqst *rqstp)
  323. {
  324. char *p = rqstp->rq_cred.cr_principal;
  325. if (rqstp->rq_authop->flavour != RPC_AUTH_GSS)
  326. return 1;
  327. /* No RPC_AUTH_GSS on NFSv4.1 back channel yet */
  328. if (clp->cl_minorversion != 0)
  329. return 0;
  330. /*
  331. * It might just be a normal user principal, in which case
  332. * userspace won't bother to tell us the name at all.
  333. */
  334. if (p == NULL)
  335. return 0;
  336. /*
  337. * Did we get the acceptor from userland during the SETCLIENID
  338. * negotiation?
  339. */
  340. if (clp->cl_acceptor)
  341. return !strcmp(p, clp->cl_acceptor);
  342. /*
  343. * Otherwise try to verify it using the cl_hostname. Note that this
  344. * doesn't work if a non-canonical hostname was used in the devname.
  345. */
  346. /* Expect a GSS_C_NT_HOSTBASED_NAME like "nfs@serverhostname" */
  347. if (memcmp(p, "nfs@", 4) != 0)
  348. return 0;
  349. p += 4;
  350. if (strcmp(p, clp->cl_hostname) != 0)
  351. return 0;
  352. return 1;
  353. }
  354. /*
  355. * pg_authenticate method for nfsv4 callback threads.
  356. *
  357. * The authflavor has been negotiated, so an incorrect flavor is a server
  358. * bug. Deny packets with incorrect authflavor.
  359. *
  360. * All other checking done after NFS decoding where the nfs_client can be
  361. * found in nfs4_callback_compound
  362. */
  363. static int nfs_callback_authenticate(struct svc_rqst *rqstp)
  364. {
  365. switch (rqstp->rq_authop->flavour) {
  366. case RPC_AUTH_NULL:
  367. if (rqstp->rq_proc != CB_NULL)
  368. return SVC_DENIED;
  369. break;
  370. case RPC_AUTH_GSS:
  371. /* No RPC_AUTH_GSS support yet in NFSv4.1 */
  372. if (svc_is_backchannel(rqstp))
  373. return SVC_DENIED;
  374. }
  375. return SVC_OK;
  376. }
  377. /*
  378. * Define NFS4 callback program
  379. */
  380. static struct svc_version *nfs4_callback_version[] = {
  381. [1] = &nfs4_callback_version1,
  382. [4] = &nfs4_callback_version4,
  383. };
  384. static struct svc_stat nfs4_callback_stats;
  385. static struct svc_program nfs4_callback_program = {
  386. .pg_prog = NFS4_CALLBACK, /* RPC service number */
  387. .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
  388. .pg_vers = nfs4_callback_version, /* version table */
  389. .pg_name = "NFSv4 callback", /* service name */
  390. .pg_class = "nfs", /* authentication class */
  391. .pg_stats = &nfs4_callback_stats,
  392. .pg_authenticate = nfs_callback_authenticate,
  393. };