callback.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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/smp_lock.h>
  12. #include <linux/sunrpc/svc.h>
  13. #include <linux/sunrpc/svcsock.h>
  14. #include <linux/nfs_fs.h>
  15. #include <linux/mutex.h>
  16. #include <linux/freezer.h>
  17. #include <linux/kthread.h>
  18. #include <linux/sunrpc/svcauth_gss.h>
  19. #if defined(CONFIG_NFS_V4_1)
  20. #include <linux/sunrpc/bc_xprt.h>
  21. #endif
  22. #include <net/inet_sock.h>
  23. #include "nfs4_fs.h"
  24. #include "callback.h"
  25. #include "internal.h"
  26. #define NFSDBG_FACILITY NFSDBG_CALLBACK
  27. struct nfs_callback_data {
  28. unsigned int users;
  29. struct svc_serv *serv;
  30. struct svc_rqst *rqst;
  31. struct task_struct *task;
  32. };
  33. static struct nfs_callback_data nfs_callback_info[NFS4_MAX_MINOR_VERSION + 1];
  34. static DEFINE_MUTEX(nfs_callback_mutex);
  35. static struct svc_program nfs4_callback_program;
  36. unsigned int nfs_callback_set_tcpport;
  37. unsigned short nfs_callback_tcpport;
  38. unsigned short nfs_callback_tcpport6;
  39. static const int nfs_set_port_min = 0;
  40. static const int nfs_set_port_max = 65535;
  41. static int param_set_port(const char *val, struct kernel_param *kp)
  42. {
  43. char *endp;
  44. int num = simple_strtol(val, &endp, 0);
  45. if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max)
  46. return -EINVAL;
  47. *((int *)kp->arg) = num;
  48. return 0;
  49. }
  50. module_param_call(callback_tcpport, param_set_port, param_get_int,
  51. &nfs_callback_set_tcpport, 0644);
  52. /*
  53. * This is the NFSv4 callback kernel thread.
  54. */
  55. static int
  56. nfs4_callback_svc(void *vrqstp)
  57. {
  58. int err, preverr = 0;
  59. struct svc_rqst *rqstp = vrqstp;
  60. set_freezable();
  61. /*
  62. * FIXME: do we really need to run this under the BKL? If so, please
  63. * add a comment about what it's intended to protect.
  64. */
  65. lock_kernel();
  66. while (!kthread_should_stop()) {
  67. /*
  68. * Listen for a request on the socket
  69. */
  70. err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT);
  71. if (err == -EAGAIN || err == -EINTR) {
  72. preverr = err;
  73. continue;
  74. }
  75. if (err < 0) {
  76. if (err != preverr) {
  77. printk(KERN_WARNING "%s: unexpected error "
  78. "from svc_recv (%d)\n", __func__, err);
  79. preverr = err;
  80. }
  81. schedule_timeout_uninterruptible(HZ);
  82. continue;
  83. }
  84. preverr = err;
  85. svc_process(rqstp);
  86. }
  87. unlock_kernel();
  88. return 0;
  89. }
  90. /*
  91. * Prepare to bring up the NFSv4 callback service
  92. */
  93. struct svc_rqst *
  94. nfs4_callback_up(struct svc_serv *serv)
  95. {
  96. int ret;
  97. ret = svc_create_xprt(serv, "tcp", PF_INET,
  98. nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS);
  99. if (ret <= 0)
  100. goto out_err;
  101. nfs_callback_tcpport = ret;
  102. dprintk("NFS: Callback listener port = %u (af %u)\n",
  103. nfs_callback_tcpport, PF_INET);
  104. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  105. ret = svc_create_xprt(serv, "tcp", PF_INET6,
  106. nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS);
  107. if (ret > 0) {
  108. nfs_callback_tcpport6 = ret;
  109. dprintk("NFS: Callback listener port = %u (af %u)\n",
  110. nfs_callback_tcpport6, PF_INET6);
  111. } else if (ret != -EAFNOSUPPORT)
  112. goto out_err;
  113. #endif /* defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) */
  114. return svc_prepare_thread(serv, &serv->sv_pools[0]);
  115. out_err:
  116. if (ret == 0)
  117. ret = -ENOMEM;
  118. return ERR_PTR(ret);
  119. }
  120. #if defined(CONFIG_NFS_V4_1)
  121. /*
  122. * The callback service for NFSv4.1 callbacks
  123. */
  124. static int
  125. nfs41_callback_svc(void *vrqstp)
  126. {
  127. struct svc_rqst *rqstp = vrqstp;
  128. struct svc_serv *serv = rqstp->rq_server;
  129. struct rpc_rqst *req;
  130. int error;
  131. DEFINE_WAIT(wq);
  132. set_freezable();
  133. /*
  134. * FIXME: do we really need to run this under the BKL? If so, please
  135. * add a comment about what it's intended to protect.
  136. */
  137. lock_kernel();
  138. while (!kthread_should_stop()) {
  139. prepare_to_wait(&serv->sv_cb_waitq, &wq, TASK_INTERRUPTIBLE);
  140. spin_lock_bh(&serv->sv_cb_lock);
  141. if (!list_empty(&serv->sv_cb_list)) {
  142. req = list_first_entry(&serv->sv_cb_list,
  143. struct rpc_rqst, rq_bc_list);
  144. list_del(&req->rq_bc_list);
  145. spin_unlock_bh(&serv->sv_cb_lock);
  146. dprintk("Invoking bc_svc_process()\n");
  147. error = bc_svc_process(serv, req, rqstp);
  148. dprintk("bc_svc_process() returned w/ error code= %d\n",
  149. error);
  150. } else {
  151. spin_unlock_bh(&serv->sv_cb_lock);
  152. schedule();
  153. }
  154. finish_wait(&serv->sv_cb_waitq, &wq);
  155. }
  156. unlock_kernel();
  157. return 0;
  158. }
  159. /*
  160. * Bring up the NFSv4.1 callback service
  161. */
  162. struct svc_rqst *
  163. nfs41_callback_up(struct svc_serv *serv, struct rpc_xprt *xprt)
  164. {
  165. struct svc_xprt *bc_xprt;
  166. struct svc_rqst *rqstp = ERR_PTR(-ENOMEM);
  167. dprintk("--> %s\n", __func__);
  168. /* Create a svc_sock for the service */
  169. bc_xprt = svc_sock_create(serv, xprt->prot);
  170. if (!bc_xprt)
  171. goto out;
  172. /*
  173. * Save the svc_serv in the transport so that it can
  174. * be referenced when the session backchannel is initialized
  175. */
  176. serv->bc_xprt = bc_xprt;
  177. xprt->bc_serv = serv;
  178. INIT_LIST_HEAD(&serv->sv_cb_list);
  179. spin_lock_init(&serv->sv_cb_lock);
  180. init_waitqueue_head(&serv->sv_cb_waitq);
  181. rqstp = svc_prepare_thread(serv, &serv->sv_pools[0]);
  182. if (IS_ERR(rqstp))
  183. svc_sock_destroy(bc_xprt);
  184. out:
  185. dprintk("--> %s return %p\n", __func__, rqstp);
  186. return rqstp;
  187. }
  188. static inline int nfs_minorversion_callback_svc_setup(u32 minorversion,
  189. struct svc_serv *serv, struct rpc_xprt *xprt,
  190. struct svc_rqst **rqstpp, int (**callback_svc)(void *vrqstp))
  191. {
  192. if (minorversion) {
  193. *rqstpp = nfs41_callback_up(serv, xprt);
  194. *callback_svc = nfs41_callback_svc;
  195. }
  196. return minorversion;
  197. }
  198. static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt,
  199. struct nfs_callback_data *cb_info)
  200. {
  201. if (minorversion)
  202. xprt->bc_serv = cb_info->serv;
  203. }
  204. #else
  205. static inline int nfs_minorversion_callback_svc_setup(u32 minorversion,
  206. struct svc_serv *serv, struct rpc_xprt *xprt,
  207. struct svc_rqst **rqstpp, int (**callback_svc)(void *vrqstp))
  208. {
  209. return 0;
  210. }
  211. static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt,
  212. struct nfs_callback_data *cb_info)
  213. {
  214. }
  215. #endif /* CONFIG_NFS_V4_1 */
  216. /*
  217. * Bring up the callback thread if it is not already up.
  218. */
  219. int nfs_callback_up(u32 minorversion, struct rpc_xprt *xprt)
  220. {
  221. struct svc_serv *serv = NULL;
  222. struct svc_rqst *rqstp;
  223. int (*callback_svc)(void *vrqstp);
  224. struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
  225. char svc_name[12];
  226. int ret = 0;
  227. int minorversion_setup;
  228. mutex_lock(&nfs_callback_mutex);
  229. if (cb_info->users++ || cb_info->task != NULL) {
  230. nfs_callback_bc_serv(minorversion, xprt, cb_info);
  231. goto out;
  232. }
  233. serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL);
  234. if (!serv) {
  235. ret = -ENOMEM;
  236. goto out_err;
  237. }
  238. minorversion_setup = nfs_minorversion_callback_svc_setup(minorversion,
  239. serv, xprt, &rqstp, &callback_svc);
  240. if (!minorversion_setup) {
  241. /* v4.0 callback setup */
  242. rqstp = nfs4_callback_up(serv);
  243. callback_svc = nfs4_callback_svc;
  244. }
  245. if (IS_ERR(rqstp)) {
  246. ret = PTR_ERR(rqstp);
  247. goto out_err;
  248. }
  249. svc_sock_update_bufs(serv);
  250. sprintf(svc_name, "nfsv4.%u-svc", minorversion);
  251. cb_info->serv = serv;
  252. cb_info->rqst = rqstp;
  253. cb_info->task = kthread_run(callback_svc, cb_info->rqst, svc_name);
  254. if (IS_ERR(cb_info->task)) {
  255. ret = PTR_ERR(cb_info->task);
  256. svc_exit_thread(cb_info->rqst);
  257. cb_info->rqst = NULL;
  258. cb_info->task = NULL;
  259. goto out_err;
  260. }
  261. out:
  262. /*
  263. * svc_create creates the svc_serv with sv_nrthreads == 1, and then
  264. * svc_prepare_thread increments that. So we need to call svc_destroy
  265. * on both success and failure so that the refcount is 1 when the
  266. * thread exits.
  267. */
  268. if (serv)
  269. svc_destroy(serv);
  270. mutex_unlock(&nfs_callback_mutex);
  271. return ret;
  272. out_err:
  273. dprintk("NFS: Couldn't create callback socket or server thread; "
  274. "err = %d\n", ret);
  275. cb_info->users--;
  276. goto out;
  277. }
  278. /*
  279. * Kill the callback thread if it's no longer being used.
  280. */
  281. void nfs_callback_down(int minorversion)
  282. {
  283. struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion];
  284. mutex_lock(&nfs_callback_mutex);
  285. cb_info->users--;
  286. if (cb_info->users == 0 && cb_info->task != NULL) {
  287. kthread_stop(cb_info->task);
  288. svc_exit_thread(cb_info->rqst);
  289. cb_info->serv = NULL;
  290. cb_info->rqst = NULL;
  291. cb_info->task = NULL;
  292. }
  293. mutex_unlock(&nfs_callback_mutex);
  294. }
  295. static int check_gss_callback_principal(struct nfs_client *clp,
  296. struct svc_rqst *rqstp)
  297. {
  298. struct rpc_clnt *r = clp->cl_rpcclient;
  299. char *p = svc_gss_principal(rqstp);
  300. /*
  301. * It might just be a normal user principal, in which case
  302. * userspace won't bother to tell us the name at all.
  303. */
  304. if (p == NULL)
  305. return SVC_DENIED;
  306. /* Expect a GSS_C_NT_HOSTBASED_NAME like "nfs@serverhostname" */
  307. if (memcmp(p, "nfs@", 4) != 0)
  308. return SVC_DENIED;
  309. p += 4;
  310. if (strcmp(p, r->cl_server) != 0)
  311. return SVC_DENIED;
  312. return SVC_OK;
  313. }
  314. static int nfs_callback_authenticate(struct svc_rqst *rqstp)
  315. {
  316. struct nfs_client *clp;
  317. RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]);
  318. int ret = SVC_OK;
  319. /* Don't talk to strangers */
  320. clp = nfs_find_client(svc_addr(rqstp), 4);
  321. if (clp == NULL)
  322. return SVC_DROP;
  323. dprintk("%s: %s NFSv4 callback!\n", __func__,
  324. svc_print_addr(rqstp, buf, sizeof(buf)));
  325. switch (rqstp->rq_authop->flavour) {
  326. case RPC_AUTH_NULL:
  327. if (rqstp->rq_proc != CB_NULL)
  328. ret = SVC_DENIED;
  329. break;
  330. case RPC_AUTH_UNIX:
  331. break;
  332. case RPC_AUTH_GSS:
  333. ret = check_gss_callback_principal(clp, rqstp);
  334. break;
  335. default:
  336. ret = SVC_DENIED;
  337. }
  338. nfs_put_client(clp);
  339. return ret;
  340. }
  341. /*
  342. * Define NFS4 callback program
  343. */
  344. static struct svc_version *nfs4_callback_version[] = {
  345. [1] = &nfs4_callback_version1,
  346. };
  347. static struct svc_stat nfs4_callback_stats;
  348. static struct svc_program nfs4_callback_program = {
  349. .pg_prog = NFS4_CALLBACK, /* RPC service number */
  350. .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */
  351. .pg_vers = nfs4_callback_version, /* version table */
  352. .pg_name = "NFSv4 callback", /* service name */
  353. .pg_class = "nfs", /* authentication class */
  354. .pg_stats = &nfs4_callback_stats,
  355. .pg_authenticate = nfs_callback_authenticate,
  356. };