svc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * linux/net/sunrpc/svc.c
  3. *
  4. * High-level RPC service routines
  5. *
  6. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  7. */
  8. #include <linux/linkage.h>
  9. #include <linux/sched.h>
  10. #include <linux/errno.h>
  11. #include <linux/net.h>
  12. #include <linux/in.h>
  13. #include <linux/mm.h>
  14. #include <linux/sunrpc/types.h>
  15. #include <linux/sunrpc/xdr.h>
  16. #include <linux/sunrpc/stats.h>
  17. #include <linux/sunrpc/svcsock.h>
  18. #include <linux/sunrpc/clnt.h>
  19. #define RPCDBG_FACILITY RPCDBG_SVCDSP
  20. #define RPC_PARANOIA 1
  21. /*
  22. * Create an RPC service
  23. */
  24. struct svc_serv *
  25. svc_create(struct svc_program *prog, unsigned int bufsize)
  26. {
  27. struct svc_serv *serv;
  28. int vers;
  29. unsigned int xdrsize;
  30. if (!(serv = kmalloc(sizeof(*serv), GFP_KERNEL)))
  31. return NULL;
  32. memset(serv, 0, sizeof(*serv));
  33. serv->sv_name = prog->pg_name;
  34. serv->sv_program = prog;
  35. serv->sv_nrthreads = 1;
  36. serv->sv_stats = prog->pg_stats;
  37. serv->sv_bufsz = bufsize? bufsize : 4096;
  38. xdrsize = 0;
  39. while (prog) {
  40. prog->pg_lovers = prog->pg_nvers-1;
  41. for (vers=0; vers<prog->pg_nvers ; vers++)
  42. if (prog->pg_vers[vers]) {
  43. prog->pg_hivers = vers;
  44. if (prog->pg_lovers > vers)
  45. prog->pg_lovers = vers;
  46. if (prog->pg_vers[vers]->vs_xdrsize > xdrsize)
  47. xdrsize = prog->pg_vers[vers]->vs_xdrsize;
  48. }
  49. prog = prog->pg_next;
  50. }
  51. serv->sv_xdrsize = xdrsize;
  52. INIT_LIST_HEAD(&serv->sv_threads);
  53. INIT_LIST_HEAD(&serv->sv_sockets);
  54. INIT_LIST_HEAD(&serv->sv_tempsocks);
  55. INIT_LIST_HEAD(&serv->sv_permsocks);
  56. spin_lock_init(&serv->sv_lock);
  57. /* Remove any stale portmap registrations */
  58. svc_register(serv, 0, 0);
  59. return serv;
  60. }
  61. /*
  62. * Destroy an RPC service
  63. */
  64. void
  65. svc_destroy(struct svc_serv *serv)
  66. {
  67. struct svc_sock *svsk;
  68. dprintk("RPC: svc_destroy(%s, %d)\n",
  69. serv->sv_program->pg_name,
  70. serv->sv_nrthreads);
  71. if (serv->sv_nrthreads) {
  72. if (--(serv->sv_nrthreads) != 0) {
  73. svc_sock_update_bufs(serv);
  74. return;
  75. }
  76. } else
  77. printk("svc_destroy: no threads for serv=%p!\n", serv);
  78. while (!list_empty(&serv->sv_tempsocks)) {
  79. svsk = list_entry(serv->sv_tempsocks.next,
  80. struct svc_sock,
  81. sk_list);
  82. svc_delete_socket(svsk);
  83. }
  84. while (!list_empty(&serv->sv_permsocks)) {
  85. svsk = list_entry(serv->sv_permsocks.next,
  86. struct svc_sock,
  87. sk_list);
  88. svc_delete_socket(svsk);
  89. }
  90. cache_clean_deferred(serv);
  91. /* Unregister service with the portmapper */
  92. svc_register(serv, 0, 0);
  93. kfree(serv);
  94. }
  95. /*
  96. * Allocate an RPC server's buffer space.
  97. * We allocate pages and place them in rq_argpages.
  98. */
  99. static int
  100. svc_init_buffer(struct svc_rqst *rqstp, unsigned int size)
  101. {
  102. int pages;
  103. int arghi;
  104. if (size > RPCSVC_MAXPAYLOAD)
  105. size = RPCSVC_MAXPAYLOAD;
  106. pages = 2 + (size+ PAGE_SIZE -1) / PAGE_SIZE;
  107. rqstp->rq_argused = 0;
  108. rqstp->rq_resused = 0;
  109. arghi = 0;
  110. BUG_ON(pages > RPCSVC_MAXPAGES);
  111. while (pages) {
  112. struct page *p = alloc_page(GFP_KERNEL);
  113. if (!p)
  114. break;
  115. rqstp->rq_argpages[arghi++] = p;
  116. pages--;
  117. }
  118. rqstp->rq_arghi = arghi;
  119. return ! pages;
  120. }
  121. /*
  122. * Release an RPC server buffer
  123. */
  124. static void
  125. svc_release_buffer(struct svc_rqst *rqstp)
  126. {
  127. while (rqstp->rq_arghi)
  128. put_page(rqstp->rq_argpages[--rqstp->rq_arghi]);
  129. while (rqstp->rq_resused) {
  130. if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
  131. continue;
  132. put_page(rqstp->rq_respages[rqstp->rq_resused]);
  133. }
  134. rqstp->rq_argused = 0;
  135. }
  136. /*
  137. * Create a server thread
  138. */
  139. int
  140. svc_create_thread(svc_thread_fn func, struct svc_serv *serv)
  141. {
  142. struct svc_rqst *rqstp;
  143. int error = -ENOMEM;
  144. rqstp = kmalloc(sizeof(*rqstp), GFP_KERNEL);
  145. if (!rqstp)
  146. goto out;
  147. memset(rqstp, 0, sizeof(*rqstp));
  148. init_waitqueue_head(&rqstp->rq_wait);
  149. if (!(rqstp->rq_argp = kmalloc(serv->sv_xdrsize, GFP_KERNEL))
  150. || !(rqstp->rq_resp = kmalloc(serv->sv_xdrsize, GFP_KERNEL))
  151. || !svc_init_buffer(rqstp, serv->sv_bufsz))
  152. goto out_thread;
  153. serv->sv_nrthreads++;
  154. rqstp->rq_server = serv;
  155. error = kernel_thread((int (*)(void *)) func, rqstp, 0);
  156. if (error < 0)
  157. goto out_thread;
  158. svc_sock_update_bufs(serv);
  159. error = 0;
  160. out:
  161. return error;
  162. out_thread:
  163. svc_exit_thread(rqstp);
  164. goto out;
  165. }
  166. /*
  167. * Destroy an RPC server thread
  168. */
  169. void
  170. svc_exit_thread(struct svc_rqst *rqstp)
  171. {
  172. struct svc_serv *serv = rqstp->rq_server;
  173. svc_release_buffer(rqstp);
  174. kfree(rqstp->rq_resp);
  175. kfree(rqstp->rq_argp);
  176. kfree(rqstp->rq_auth_data);
  177. kfree(rqstp);
  178. /* Release the server */
  179. if (serv)
  180. svc_destroy(serv);
  181. }
  182. /*
  183. * Register an RPC service with the local portmapper.
  184. * To unregister a service, call this routine with
  185. * proto and port == 0.
  186. */
  187. int
  188. svc_register(struct svc_serv *serv, int proto, unsigned short port)
  189. {
  190. struct svc_program *progp;
  191. unsigned long flags;
  192. int i, error = 0, dummy;
  193. progp = serv->sv_program;
  194. dprintk("RPC: svc_register(%s, %s, %d)\n",
  195. progp->pg_name, proto == IPPROTO_UDP? "udp" : "tcp", port);
  196. if (!port)
  197. clear_thread_flag(TIF_SIGPENDING);
  198. for (i = 0; i < progp->pg_nvers; i++) {
  199. if (progp->pg_vers[i] == NULL)
  200. continue;
  201. error = rpc_register(progp->pg_prog, i, proto, port, &dummy);
  202. if (error < 0)
  203. break;
  204. if (port && !dummy) {
  205. error = -EACCES;
  206. break;
  207. }
  208. }
  209. if (!port) {
  210. spin_lock_irqsave(&current->sighand->siglock, flags);
  211. recalc_sigpending();
  212. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  213. }
  214. return error;
  215. }
  216. /*
  217. * Process the RPC request.
  218. */
  219. int
  220. svc_process(struct svc_serv *serv, struct svc_rqst *rqstp)
  221. {
  222. struct svc_program *progp;
  223. struct svc_version *versp = NULL; /* compiler food */
  224. struct svc_procedure *procp = NULL;
  225. struct kvec * argv = &rqstp->rq_arg.head[0];
  226. struct kvec * resv = &rqstp->rq_res.head[0];
  227. kxdrproc_t xdr;
  228. u32 *statp;
  229. u32 dir, prog, vers, proc,
  230. auth_stat, rpc_stat;
  231. int auth_res;
  232. u32 *accept_statp;
  233. rpc_stat = rpc_success;
  234. if (argv->iov_len < 6*4)
  235. goto err_short_len;
  236. /* setup response xdr_buf.
  237. * Initially it has just one page
  238. */
  239. svc_take_page(rqstp); /* must succeed */
  240. resv->iov_base = page_address(rqstp->rq_respages[0]);
  241. resv->iov_len = 0;
  242. rqstp->rq_res.pages = rqstp->rq_respages+1;
  243. rqstp->rq_res.len = 0;
  244. rqstp->rq_res.page_base = 0;
  245. rqstp->rq_res.page_len = 0;
  246. rqstp->rq_res.buflen = PAGE_SIZE;
  247. rqstp->rq_res.tail[0].iov_base = NULL;
  248. rqstp->rq_res.tail[0].iov_len = 0;
  249. /* Will be turned off only in gss privacy case: */
  250. rqstp->rq_sendfile_ok = 1;
  251. /* tcp needs a space for the record length... */
  252. if (rqstp->rq_prot == IPPROTO_TCP)
  253. svc_putu32(resv, 0);
  254. rqstp->rq_xid = svc_getu32(argv);
  255. svc_putu32(resv, rqstp->rq_xid);
  256. dir = ntohl(svc_getu32(argv));
  257. vers = ntohl(svc_getu32(argv));
  258. /* First words of reply: */
  259. svc_putu32(resv, xdr_one); /* REPLY */
  260. if (dir != 0) /* direction != CALL */
  261. goto err_bad_dir;
  262. if (vers != 2) /* RPC version number */
  263. goto err_bad_rpc;
  264. /* Save position in case we later decide to reject: */
  265. accept_statp = resv->iov_base + resv->iov_len;
  266. svc_putu32(resv, xdr_zero); /* ACCEPT */
  267. rqstp->rq_prog = prog = ntohl(svc_getu32(argv)); /* program number */
  268. rqstp->rq_vers = vers = ntohl(svc_getu32(argv)); /* version number */
  269. rqstp->rq_proc = proc = ntohl(svc_getu32(argv)); /* procedure number */
  270. progp = serv->sv_program;
  271. for (progp = serv->sv_program; progp; progp = progp->pg_next)
  272. if (prog == progp->pg_prog)
  273. break;
  274. /*
  275. * Decode auth data, and add verifier to reply buffer.
  276. * We do this before anything else in order to get a decent
  277. * auth verifier.
  278. */
  279. auth_res = svc_authenticate(rqstp, &auth_stat);
  280. /* Also give the program a chance to reject this call: */
  281. if (auth_res == SVC_OK && progp) {
  282. auth_stat = rpc_autherr_badcred;
  283. auth_res = progp->pg_authenticate(rqstp);
  284. }
  285. switch (auth_res) {
  286. case SVC_OK:
  287. break;
  288. case SVC_GARBAGE:
  289. rpc_stat = rpc_garbage_args;
  290. goto err_bad;
  291. case SVC_SYSERR:
  292. rpc_stat = rpc_system_err;
  293. goto err_bad;
  294. case SVC_DENIED:
  295. goto err_bad_auth;
  296. case SVC_DROP:
  297. goto dropit;
  298. case SVC_COMPLETE:
  299. goto sendit;
  300. }
  301. if (progp == NULL)
  302. goto err_bad_prog;
  303. if (vers >= progp->pg_nvers ||
  304. !(versp = progp->pg_vers[vers]))
  305. goto err_bad_vers;
  306. procp = versp->vs_proc + proc;
  307. if (proc >= versp->vs_nproc || !procp->pc_func)
  308. goto err_bad_proc;
  309. rqstp->rq_server = serv;
  310. rqstp->rq_procinfo = procp;
  311. /* Syntactic check complete */
  312. serv->sv_stats->rpccnt++;
  313. /* Build the reply header. */
  314. statp = resv->iov_base +resv->iov_len;
  315. svc_putu32(resv, rpc_success); /* RPC_SUCCESS */
  316. /* Bump per-procedure stats counter */
  317. procp->pc_count++;
  318. /* Initialize storage for argp and resp */
  319. memset(rqstp->rq_argp, 0, procp->pc_argsize);
  320. memset(rqstp->rq_resp, 0, procp->pc_ressize);
  321. /* un-reserve some of the out-queue now that we have a
  322. * better idea of reply size
  323. */
  324. if (procp->pc_xdrressize)
  325. svc_reserve(rqstp, procp->pc_xdrressize<<2);
  326. /* Call the function that processes the request. */
  327. if (!versp->vs_dispatch) {
  328. /* Decode arguments */
  329. xdr = procp->pc_decode;
  330. if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp))
  331. goto err_garbage;
  332. *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
  333. /* Encode reply */
  334. if (*statp == rpc_success && (xdr = procp->pc_encode)
  335. && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) {
  336. dprintk("svc: failed to encode reply\n");
  337. /* serv->sv_stats->rpcsystemerr++; */
  338. *statp = rpc_system_err;
  339. }
  340. } else {
  341. dprintk("svc: calling dispatcher\n");
  342. if (!versp->vs_dispatch(rqstp, statp)) {
  343. /* Release reply info */
  344. if (procp->pc_release)
  345. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  346. goto dropit;
  347. }
  348. }
  349. /* Check RPC status result */
  350. if (*statp != rpc_success)
  351. resv->iov_len = ((void*)statp) - resv->iov_base + 4;
  352. /* Release reply info */
  353. if (procp->pc_release)
  354. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  355. if (procp->pc_encode == NULL)
  356. goto dropit;
  357. sendit:
  358. if (svc_authorise(rqstp))
  359. goto dropit;
  360. return svc_send(rqstp);
  361. dropit:
  362. svc_authorise(rqstp); /* doesn't hurt to call this twice */
  363. dprintk("svc: svc_process dropit\n");
  364. svc_drop(rqstp);
  365. return 0;
  366. err_short_len:
  367. #ifdef RPC_PARANOIA
  368. printk("svc: short len %Zd, dropping request\n", argv->iov_len);
  369. #endif
  370. goto dropit; /* drop request */
  371. err_bad_dir:
  372. #ifdef RPC_PARANOIA
  373. printk("svc: bad direction %d, dropping request\n", dir);
  374. #endif
  375. serv->sv_stats->rpcbadfmt++;
  376. goto dropit; /* drop request */
  377. err_bad_rpc:
  378. serv->sv_stats->rpcbadfmt++;
  379. svc_putu32(resv, xdr_one); /* REJECT */
  380. svc_putu32(resv, xdr_zero); /* RPC_MISMATCH */
  381. svc_putu32(resv, xdr_two); /* Only RPCv2 supported */
  382. svc_putu32(resv, xdr_two);
  383. goto sendit;
  384. err_bad_auth:
  385. dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
  386. serv->sv_stats->rpcbadauth++;
  387. /* Restore write pointer to location of accept status: */
  388. xdr_ressize_check(rqstp, accept_statp);
  389. svc_putu32(resv, xdr_one); /* REJECT */
  390. svc_putu32(resv, xdr_one); /* AUTH_ERROR */
  391. svc_putu32(resv, auth_stat); /* status */
  392. goto sendit;
  393. err_bad_prog:
  394. dprintk("svc: unknown program %d\n", prog);
  395. serv->sv_stats->rpcbadfmt++;
  396. svc_putu32(resv, rpc_prog_unavail);
  397. goto sendit;
  398. err_bad_vers:
  399. #ifdef RPC_PARANOIA
  400. printk("svc: unknown version (%d)\n", vers);
  401. #endif
  402. serv->sv_stats->rpcbadfmt++;
  403. svc_putu32(resv, rpc_prog_mismatch);
  404. svc_putu32(resv, htonl(progp->pg_lovers));
  405. svc_putu32(resv, htonl(progp->pg_hivers));
  406. goto sendit;
  407. err_bad_proc:
  408. #ifdef RPC_PARANOIA
  409. printk("svc: unknown procedure (%d)\n", proc);
  410. #endif
  411. serv->sv_stats->rpcbadfmt++;
  412. svc_putu32(resv, rpc_proc_unavail);
  413. goto sendit;
  414. err_garbage:
  415. #ifdef RPC_PARANOIA
  416. printk("svc: failed to decode args\n");
  417. #endif
  418. rpc_stat = rpc_garbage_args;
  419. err_bad:
  420. serv->sv_stats->rpcbadfmt++;
  421. svc_putu32(resv, rpc_stat);
  422. goto sendit;
  423. }