svc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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.buflen = PAGE_SIZE;
  248. rqstp->rq_res.tail[0].iov_len = 0;
  249. /* tcp needs a space for the record length... */
  250. if (rqstp->rq_prot == IPPROTO_TCP)
  251. svc_putu32(resv, 0);
  252. rqstp->rq_xid = svc_getu32(argv);
  253. svc_putu32(resv, rqstp->rq_xid);
  254. dir = ntohl(svc_getu32(argv));
  255. vers = ntohl(svc_getu32(argv));
  256. /* First words of reply: */
  257. svc_putu32(resv, xdr_one); /* REPLY */
  258. if (dir != 0) /* direction != CALL */
  259. goto err_bad_dir;
  260. if (vers != 2) /* RPC version number */
  261. goto err_bad_rpc;
  262. /* Save position in case we later decide to reject: */
  263. accept_statp = resv->iov_base + resv->iov_len;
  264. svc_putu32(resv, xdr_zero); /* ACCEPT */
  265. rqstp->rq_prog = prog = ntohl(svc_getu32(argv)); /* program number */
  266. rqstp->rq_vers = vers = ntohl(svc_getu32(argv)); /* version number */
  267. rqstp->rq_proc = proc = ntohl(svc_getu32(argv)); /* procedure number */
  268. progp = serv->sv_program;
  269. /*
  270. * Decode auth data, and add verifier to reply buffer.
  271. * We do this before anything else in order to get a decent
  272. * auth verifier.
  273. */
  274. auth_res = svc_authenticate(rqstp, &auth_stat);
  275. /* Also give the program a chance to reject this call: */
  276. if (auth_res == SVC_OK) {
  277. auth_stat = rpc_autherr_badcred;
  278. auth_res = progp->pg_authenticate(rqstp);
  279. }
  280. switch (auth_res) {
  281. case SVC_OK:
  282. break;
  283. case SVC_GARBAGE:
  284. rpc_stat = rpc_garbage_args;
  285. goto err_bad;
  286. case SVC_SYSERR:
  287. rpc_stat = rpc_system_err;
  288. goto err_bad;
  289. case SVC_DENIED:
  290. goto err_bad_auth;
  291. case SVC_DROP:
  292. goto dropit;
  293. case SVC_COMPLETE:
  294. goto sendit;
  295. }
  296. if (prog != progp->pg_prog)
  297. goto err_bad_prog;
  298. if (vers >= progp->pg_nvers ||
  299. !(versp = progp->pg_vers[vers]))
  300. goto err_bad_vers;
  301. procp = versp->vs_proc + proc;
  302. if (proc >= versp->vs_nproc || !procp->pc_func)
  303. goto err_bad_proc;
  304. rqstp->rq_server = serv;
  305. rqstp->rq_procinfo = procp;
  306. /* Syntactic check complete */
  307. serv->sv_stats->rpccnt++;
  308. /* Build the reply header. */
  309. statp = resv->iov_base +resv->iov_len;
  310. svc_putu32(resv, rpc_success); /* RPC_SUCCESS */
  311. /* Bump per-procedure stats counter */
  312. procp->pc_count++;
  313. /* Initialize storage for argp and resp */
  314. memset(rqstp->rq_argp, 0, procp->pc_argsize);
  315. memset(rqstp->rq_resp, 0, procp->pc_ressize);
  316. /* un-reserve some of the out-queue now that we have a
  317. * better idea of reply size
  318. */
  319. if (procp->pc_xdrressize)
  320. svc_reserve(rqstp, procp->pc_xdrressize<<2);
  321. /* Call the function that processes the request. */
  322. if (!versp->vs_dispatch) {
  323. /* Decode arguments */
  324. xdr = procp->pc_decode;
  325. if (xdr && !xdr(rqstp, argv->iov_base, rqstp->rq_argp))
  326. goto err_garbage;
  327. *statp = procp->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
  328. /* Encode reply */
  329. if (*statp == rpc_success && (xdr = procp->pc_encode)
  330. && !xdr(rqstp, resv->iov_base+resv->iov_len, rqstp->rq_resp)) {
  331. dprintk("svc: failed to encode reply\n");
  332. /* serv->sv_stats->rpcsystemerr++; */
  333. *statp = rpc_system_err;
  334. }
  335. } else {
  336. dprintk("svc: calling dispatcher\n");
  337. if (!versp->vs_dispatch(rqstp, statp)) {
  338. /* Release reply info */
  339. if (procp->pc_release)
  340. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  341. goto dropit;
  342. }
  343. }
  344. /* Check RPC status result */
  345. if (*statp != rpc_success)
  346. resv->iov_len = ((void*)statp) - resv->iov_base + 4;
  347. /* Release reply info */
  348. if (procp->pc_release)
  349. procp->pc_release(rqstp, NULL, rqstp->rq_resp);
  350. if (procp->pc_encode == NULL)
  351. goto dropit;
  352. sendit:
  353. if (svc_authorise(rqstp))
  354. goto dropit;
  355. return svc_send(rqstp);
  356. dropit:
  357. svc_authorise(rqstp); /* doesn't hurt to call this twice */
  358. dprintk("svc: svc_process dropit\n");
  359. svc_drop(rqstp);
  360. return 0;
  361. err_short_len:
  362. #ifdef RPC_PARANOIA
  363. printk("svc: short len %Zd, dropping request\n", argv->iov_len);
  364. #endif
  365. goto dropit; /* drop request */
  366. err_bad_dir:
  367. #ifdef RPC_PARANOIA
  368. printk("svc: bad direction %d, dropping request\n", dir);
  369. #endif
  370. serv->sv_stats->rpcbadfmt++;
  371. goto dropit; /* drop request */
  372. err_bad_rpc:
  373. serv->sv_stats->rpcbadfmt++;
  374. svc_putu32(resv, xdr_one); /* REJECT */
  375. svc_putu32(resv, xdr_zero); /* RPC_MISMATCH */
  376. svc_putu32(resv, xdr_two); /* Only RPCv2 supported */
  377. svc_putu32(resv, xdr_two);
  378. goto sendit;
  379. err_bad_auth:
  380. dprintk("svc: authentication failed (%d)\n", ntohl(auth_stat));
  381. serv->sv_stats->rpcbadauth++;
  382. /* Restore write pointer to location of accept status: */
  383. xdr_ressize_check(rqstp, accept_statp);
  384. svc_putu32(resv, xdr_one); /* REJECT */
  385. svc_putu32(resv, xdr_one); /* AUTH_ERROR */
  386. svc_putu32(resv, auth_stat); /* status */
  387. goto sendit;
  388. err_bad_prog:
  389. #ifdef RPC_PARANOIA
  390. if (prog != 100227 || progp->pg_prog != 100003)
  391. printk("svc: unknown program %d (me %d)\n", prog, progp->pg_prog);
  392. /* else it is just a Solaris client seeing if ACLs are supported */
  393. #endif
  394. serv->sv_stats->rpcbadfmt++;
  395. svc_putu32(resv, rpc_prog_unavail);
  396. goto sendit;
  397. err_bad_vers:
  398. #ifdef RPC_PARANOIA
  399. printk("svc: unknown version (%d)\n", vers);
  400. #endif
  401. serv->sv_stats->rpcbadfmt++;
  402. svc_putu32(resv, rpc_prog_mismatch);
  403. svc_putu32(resv, htonl(progp->pg_lovers));
  404. svc_putu32(resv, htonl(progp->pg_hivers));
  405. goto sendit;
  406. err_bad_proc:
  407. #ifdef RPC_PARANOIA
  408. printk("svc: unknown procedure (%d)\n", proc);
  409. #endif
  410. serv->sv_stats->rpcbadfmt++;
  411. svc_putu32(resv, rpc_proc_unavail);
  412. goto sendit;
  413. err_garbage:
  414. #ifdef RPC_PARANOIA
  415. printk("svc: failed to decode args\n");
  416. #endif
  417. rpc_stat = rpc_garbage_args;
  418. err_bad:
  419. serv->sv_stats->rpcbadfmt++;
  420. svc_putu32(resv, rpc_stat);
  421. goto sendit;
  422. }