svc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  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 = (struct svc_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. if (pages > RPCSVC_MAXPAGES)
  111. BUG();
  112. while (pages) {
  113. struct page *p = alloc_page(GFP_KERNEL);
  114. if (!p)
  115. break;
  116. rqstp->rq_argpages[arghi++] = p;
  117. pages--;
  118. }
  119. rqstp->rq_arghi = arghi;
  120. return ! pages;
  121. }
  122. /*
  123. * Release an RPC server buffer
  124. */
  125. static void
  126. svc_release_buffer(struct svc_rqst *rqstp)
  127. {
  128. while (rqstp->rq_arghi)
  129. put_page(rqstp->rq_argpages[--rqstp->rq_arghi]);
  130. while (rqstp->rq_resused) {
  131. if (rqstp->rq_respages[--rqstp->rq_resused] == NULL)
  132. continue;
  133. put_page(rqstp->rq_respages[rqstp->rq_resused]);
  134. }
  135. rqstp->rq_argused = 0;
  136. }
  137. /*
  138. * Create a server thread
  139. */
  140. int
  141. svc_create_thread(svc_thread_fn func, struct svc_serv *serv)
  142. {
  143. struct svc_rqst *rqstp;
  144. int error = -ENOMEM;
  145. rqstp = kmalloc(sizeof(*rqstp), GFP_KERNEL);
  146. if (!rqstp)
  147. goto out;
  148. memset(rqstp, 0, sizeof(*rqstp));
  149. init_waitqueue_head(&rqstp->rq_wait);
  150. if (!(rqstp->rq_argp = (u32 *) kmalloc(serv->sv_xdrsize, GFP_KERNEL))
  151. || !(rqstp->rq_resp = (u32 *) kmalloc(serv->sv_xdrsize, GFP_KERNEL))
  152. || !svc_init_buffer(rqstp, serv->sv_bufsz))
  153. goto out_thread;
  154. serv->sv_nrthreads++;
  155. rqstp->rq_server = serv;
  156. error = kernel_thread((int (*)(void *)) func, rqstp, 0);
  157. if (error < 0)
  158. goto out_thread;
  159. svc_sock_update_bufs(serv);
  160. error = 0;
  161. out:
  162. return error;
  163. out_thread:
  164. svc_exit_thread(rqstp);
  165. goto out;
  166. }
  167. /*
  168. * Destroy an RPC server thread
  169. */
  170. void
  171. svc_exit_thread(struct svc_rqst *rqstp)
  172. {
  173. struct svc_serv *serv = rqstp->rq_server;
  174. svc_release_buffer(rqstp);
  175. if (rqstp->rq_resp)
  176. kfree(rqstp->rq_resp);
  177. if (rqstp->rq_argp)
  178. kfree(rqstp->rq_argp);
  179. if (rqstp->rq_auth_data)
  180. kfree(rqstp->rq_auth_data);
  181. kfree(rqstp);
  182. /* Release the server */
  183. if (serv)
  184. svc_destroy(serv);
  185. }
  186. /*
  187. * Register an RPC service with the local portmapper.
  188. * To unregister a service, call this routine with
  189. * proto and port == 0.
  190. */
  191. int
  192. svc_register(struct svc_serv *serv, int proto, unsigned short port)
  193. {
  194. struct svc_program *progp;
  195. unsigned long flags;
  196. int i, error = 0, dummy;
  197. progp = serv->sv_program;
  198. dprintk("RPC: svc_register(%s, %s, %d)\n",
  199. progp->pg_name, proto == IPPROTO_UDP? "udp" : "tcp", port);
  200. if (!port)
  201. clear_thread_flag(TIF_SIGPENDING);
  202. for (i = 0; i < progp->pg_nvers; i++) {
  203. if (progp->pg_vers[i] == NULL)
  204. continue;
  205. error = rpc_register(progp->pg_prog, i, proto, port, &dummy);
  206. if (error < 0)
  207. break;
  208. if (port && !dummy) {
  209. error = -EACCES;
  210. break;
  211. }
  212. }
  213. if (!port) {
  214. spin_lock_irqsave(&current->sighand->siglock, flags);
  215. recalc_sigpending();
  216. spin_unlock_irqrestore(&current->sighand->siglock, flags);
  217. }
  218. return error;
  219. }
  220. /*
  221. * Process the RPC request.
  222. */
  223. int
  224. svc_process(struct svc_serv *serv, struct svc_rqst *rqstp)
  225. {
  226. struct svc_program *progp;
  227. struct svc_version *versp = NULL; /* compiler food */
  228. struct svc_procedure *procp = NULL;
  229. struct kvec * argv = &rqstp->rq_arg.head[0];
  230. struct kvec * resv = &rqstp->rq_res.head[0];
  231. kxdrproc_t xdr;
  232. u32 *statp;
  233. u32 dir, prog, vers, proc,
  234. auth_stat, rpc_stat;
  235. int auth_res;
  236. u32 *accept_statp;
  237. rpc_stat = rpc_success;
  238. if (argv->iov_len < 6*4)
  239. goto err_short_len;
  240. /* setup response xdr_buf.
  241. * Initially it has just one page
  242. */
  243. svc_take_page(rqstp); /* must succeed */
  244. resv->iov_base = page_address(rqstp->rq_respages[0]);
  245. resv->iov_len = 0;
  246. rqstp->rq_res.pages = rqstp->rq_respages+1;
  247. rqstp->rq_res.len = 0;
  248. rqstp->rq_res.page_base = 0;
  249. rqstp->rq_res.page_len = 0;
  250. rqstp->rq_res.buflen = PAGE_SIZE;
  251. rqstp->rq_res.tail[0].iov_len = 0;
  252. /* tcp needs a space for the record length... */
  253. if (rqstp->rq_prot == IPPROTO_TCP)
  254. svc_putu32(resv, 0);
  255. rqstp->rq_xid = svc_getu32(argv);
  256. svc_putu32(resv, rqstp->rq_xid);
  257. dir = ntohl(svc_getu32(argv));
  258. vers = ntohl(svc_getu32(argv));
  259. /* First words of reply: */
  260. svc_putu32(resv, xdr_one); /* REPLY */
  261. if (dir != 0) /* direction != CALL */
  262. goto err_bad_dir;
  263. if (vers != 2) /* RPC version number */
  264. goto err_bad_rpc;
  265. /* Save position in case we later decide to reject: */
  266. accept_statp = resv->iov_base + resv->iov_len;
  267. svc_putu32(resv, xdr_zero); /* ACCEPT */
  268. rqstp->rq_prog = prog = ntohl(svc_getu32(argv)); /* program number */
  269. rqstp->rq_vers = vers = ntohl(svc_getu32(argv)); /* version number */
  270. rqstp->rq_proc = proc = ntohl(svc_getu32(argv)); /* procedure number */
  271. progp = serv->sv_program;
  272. /*
  273. * Decode auth data, and add verifier to reply buffer.
  274. * We do this before anything else in order to get a decent
  275. * auth verifier.
  276. */
  277. auth_res = svc_authenticate(rqstp, &auth_stat);
  278. /* Also give the program a chance to reject this call: */
  279. if (auth_res == SVC_OK) {
  280. auth_stat = rpc_autherr_badcred;
  281. auth_res = progp->pg_authenticate(rqstp);
  282. }
  283. switch (auth_res) {
  284. case SVC_OK:
  285. break;
  286. case SVC_GARBAGE:
  287. rpc_stat = rpc_garbage_args;
  288. goto err_bad;
  289. case SVC_SYSERR:
  290. rpc_stat = rpc_system_err;
  291. goto err_bad;
  292. case SVC_DENIED:
  293. goto err_bad_auth;
  294. case SVC_DROP:
  295. goto dropit;
  296. case SVC_COMPLETE:
  297. goto sendit;
  298. }
  299. for (progp = serv->sv_program; progp; progp = progp->pg_next)
  300. if (prog == progp->pg_prog)
  301. break;
  302. if (progp == NULL)
  303. goto err_bad_prog;
  304. if (vers >= progp->pg_nvers ||
  305. !(versp = progp->pg_vers[vers]))
  306. goto err_bad_vers;
  307. procp = versp->vs_proc + proc;
  308. if (proc >= versp->vs_nproc || !procp->pc_func)
  309. goto err_bad_proc;
  310. rqstp->rq_server = serv;
  311. rqstp->rq_procinfo = procp;
  312. /* Syntactic check complete */
  313. serv->sv_stats->rpccnt++;
  314. /* Build the reply header. */
  315. statp = resv->iov_base +resv->iov_len;
  316. svc_putu32(resv, rpc_success); /* RPC_SUCCESS */
  317. /* Bump per-procedure stats counter */
  318. procp->pc_count++;
  319. /* Initialize storage for argp and resp */
  320. memset(rqstp->rq_argp, 0, procp->pc_argsize);
  321. memset(rqstp->rq_resp, 0, procp->pc_ressize);
  322. /* un-reserve some of the out-queue now that we have a
  323. * better idea of reply size
  324. */
  325. if (procp->pc_xdrressize)
  326. svc_reserve(rqstp, procp->pc_xdrressize<<2);
  327. /* Call the function that processes the request. */
  328. if (!versp->vs_dispatch) {
  329. /* Decode arguments */
  330. xdr = procp->pc_decode;
  331. if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp))
  332. goto err_garbage;
  333. *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
  334. /* Encode reply */
  335. if (*statp == rpc_success && (xdr = procp->pc_encode)
  336. && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) {
  337. dprintk("svc: failed to encode reply\n");
  338. /* serv->sv_stats->rpcsystemerr++; */
  339. *statp = rpc_system_err;
  340. }
  341. } else {
  342. dprintk("svc: calling dispatcher\n");
  343. if (!versp->vs_dispatch(rqstp, statp)) {
  344. /* Release reply info */
  345. if (procp->pc_release)
  346. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  347. goto dropit;
  348. }
  349. }
  350. /* Check RPC status result */
  351. if (*statp != rpc_success)
  352. resv->iov_len = ((void*)statp) - resv->iov_base + 4;
  353. /* Release reply info */
  354. if (procp->pc_release)
  355. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  356. if (procp->pc_encode == NULL)
  357. goto dropit;
  358. sendit:
  359. if (svc_authorise(rqstp))
  360. goto dropit;
  361. return svc_send(rqstp);
  362. dropit:
  363. svc_authorise(rqstp); /* doesn't hurt to call this twice */
  364. dprintk("svc: svc_process dropit\n");
  365. svc_drop(rqstp);
  366. return 0;
  367. err_short_len:
  368. #ifdef RPC_PARANOIA
  369. printk("svc: short len %Zd, dropping request\n", argv->iov_len);
  370. #endif
  371. goto dropit; /* drop request */
  372. err_bad_dir:
  373. #ifdef RPC_PARANOIA
  374. printk("svc: bad direction %d, dropping request\n", dir);
  375. #endif
  376. serv->sv_stats->rpcbadfmt++;
  377. goto dropit; /* drop request */
  378. err_bad_rpc:
  379. serv->sv_stats->rpcbadfmt++;
  380. svc_putu32(resv, xdr_one); /* REJECT */
  381. svc_putu32(resv, xdr_zero); /* RPC_MISMATCH */
  382. svc_putu32(resv, xdr_two); /* Only RPCv2 supported */
  383. svc_putu32(resv, xdr_two);
  384. goto sendit;
  385. err_bad_auth:
  386. dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
  387. serv->sv_stats->rpcbadauth++;
  388. /* Restore write pointer to location of accept status: */
  389. xdr_ressize_check(rqstp, accept_statp);
  390. svc_putu32(resv, xdr_one); /* REJECT */
  391. svc_putu32(resv, xdr_one); /* AUTH_ERROR */
  392. svc_putu32(resv, auth_stat); /* status */
  393. goto sendit;
  394. err_bad_prog:
  395. dprintk("svc: unknown program %d\n", prog);
  396. serv->sv_stats->rpcbadfmt++;
  397. svc_putu32(resv, rpc_prog_unavail);
  398. goto sendit;
  399. err_bad_vers:
  400. #ifdef RPC_PARANOIA
  401. printk("svc: unknown version (%d)\n", vers);
  402. #endif
  403. serv->sv_stats->rpcbadfmt++;
  404. svc_putu32(resv, rpc_prog_mismatch);
  405. svc_putu32(resv, htonl(progp->pg_lovers));
  406. svc_putu32(resv, htonl(progp->pg_hivers));
  407. goto sendit;
  408. err_bad_proc:
  409. #ifdef RPC_PARANOIA
  410. printk("svc: unknown procedure (%d)\n", proc);
  411. #endif
  412. serv->sv_stats->rpcbadfmt++;
  413. svc_putu32(resv, rpc_proc_unavail);
  414. goto sendit;
  415. err_garbage:
  416. #ifdef RPC_PARANOIA
  417. printk("svc: failed to decode args\n");
  418. #endif
  419. rpc_stat = rpc_garbage_args;
  420. err_bad:
  421. serv->sv_stats->rpcbadfmt++;
  422. svc_putu32(resv, rpc_stat);
  423. goto sendit;
  424. }