callback.c 12 KB

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