svc.c 11 KB

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