callback.c 12 KB

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