~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/sunrpc/svc.c

Version: ~ [ linux-5.9.1 ] ~ [ linux-5.8.16 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.72 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.152 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.202 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.240 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.240 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp