cm4000_cs: use bitrev
[linux-2.6/mini2440.git] / net / sunrpc / rpcb_clnt.c
blob6c7aa8a1f0c65f41ecdab07e61d2958c08569f48
1 /*
2 * In-kernel rpcbind client supporting versions 2, 3, and 4 of the rpcbind
3 * protocol
5 * Based on RFC 1833: "Binding Protocols for ONC RPC Version 2" and
6 * RFC 3530: "Network File System (NFS) version 4 Protocol"
8 * Original: Gilles Quillard, Bull Open Source, 2005 <gilles.quillard@bull.net>
9 * Updated: Chuck Lever, Oracle Corporation, 2007 <chuck.lever@oracle.com>
11 * Descended from net/sunrpc/pmap_clnt.c,
12 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
15 #include <linux/types.h>
16 #include <linux/socket.h>
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
20 #include <linux/sunrpc/clnt.h>
21 #include <linux/sunrpc/sched.h>
23 #ifdef RPC_DEBUG
24 # define RPCDBG_FACILITY RPCDBG_BIND
25 #endif
27 #define RPCBIND_PROGRAM (100000u)
28 #define RPCBIND_PORT (111u)
30 enum {
31 RPCBPROC_NULL,
32 RPCBPROC_SET,
33 RPCBPROC_UNSET,
34 RPCBPROC_GETPORT,
35 RPCBPROC_GETADDR = 3, /* alias for GETPORT */
36 RPCBPROC_DUMP,
37 RPCBPROC_CALLIT,
38 RPCBPROC_BCAST = 5, /* alias for CALLIT */
39 RPCBPROC_GETTIME,
40 RPCBPROC_UADDR2TADDR,
41 RPCBPROC_TADDR2UADDR,
42 RPCBPROC_GETVERSADDR,
43 RPCBPROC_INDIRECT,
44 RPCBPROC_GETADDRLIST,
45 RPCBPROC_GETSTAT,
48 #define RPCB_HIGHPROC_2 RPCBPROC_CALLIT
49 #define RPCB_HIGHPROC_3 RPCBPROC_TADDR2UADDR
50 #define RPCB_HIGHPROC_4 RPCBPROC_GETSTAT
53 * r_addr
55 * Quoting RFC 3530, section 2.2:
57 * For TCP over IPv4 and for UDP over IPv4, the format of r_addr is the
58 * US-ASCII string:
60 * h1.h2.h3.h4.p1.p2
62 * The prefix, "h1.h2.h3.h4", is the standard textual form for
63 * representing an IPv4 address, which is always four octets long.
64 * Assuming big-endian ordering, h1, h2, h3, and h4, are respectively,
65 * the first through fourth octets each converted to ASCII-decimal.
66 * Assuming big-endian ordering, p1 and p2 are, respectively, the first
67 * and second octets each converted to ASCII-decimal. For example, if a
68 * host, in big-endian order, has an address of 0x0A010307 and there is
69 * a service listening on, in big endian order, port 0x020F (decimal
70 * 527), then the complete universal address is "10.1.3.7.2.15".
72 * ...
74 * For TCP over IPv6 and for UDP over IPv6, the format of r_addr is the
75 * US-ASCII string:
77 * x1:x2:x3:x4:x5:x6:x7:x8.p1.p2
79 * The suffix "p1.p2" is the service port, and is computed the same way
80 * as with universal addresses for TCP and UDP over IPv4. The prefix,
81 * "x1:x2:x3:x4:x5:x6:x7:x8", is the standard textual form for
82 * representing an IPv6 address as defined in Section 2.2 of [RFC2373].
83 * Additionally, the two alternative forms specified in Section 2.2 of
84 * [RFC2373] are also acceptable.
86 * XXX: Currently this implementation does not explicitly convert the
87 * stored address to US-ASCII on non-ASCII systems.
89 #define RPCB_MAXADDRLEN (128u)
92 * r_netid
94 * Quoting RFC 3530, section 2.2:
96 * For TCP over IPv4 the value of r_netid is the string "tcp". For UDP
97 * over IPv4 the value of r_netid is the string "udp".
99 * ...
101 * For TCP over IPv6 the value of r_netid is the string "tcp6". For UDP
102 * over IPv6 the value of r_netid is the string "udp6".
104 #define RPCB_NETID_UDP "\165\144\160" /* "udp" */
105 #define RPCB_NETID_TCP "\164\143\160" /* "tcp" */
106 #define RPCB_NETID_UDP6 "\165\144\160\066" /* "udp6" */
107 #define RPCB_NETID_TCP6 "\164\143\160\066" /* "tcp6" */
109 #define RPCB_MAXNETIDLEN (4u)
112 * r_owner
114 * The "owner" is allowed to unset a service in the rpcbind database.
115 * We always use the following (arbitrary) fixed string.
117 #define RPCB_OWNER_STRING "rpcb"
118 #define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
120 static void rpcb_getport_done(struct rpc_task *, void *);
121 extern struct rpc_program rpcb_program;
123 struct rpcbind_args {
124 struct rpc_xprt * r_xprt;
126 u32 r_prog;
127 u32 r_vers;
128 u32 r_prot;
129 unsigned short r_port;
130 char * r_netid;
131 char r_addr[RPCB_MAXADDRLEN];
132 char * r_owner;
135 static struct rpc_procinfo rpcb_procedures2[];
136 static struct rpc_procinfo rpcb_procedures3[];
138 static struct rpcb_info {
139 int rpc_vers;
140 struct rpc_procinfo * rpc_proc;
141 } rpcb_next_version[];
143 static void rpcb_getport_prepare(struct rpc_task *task, void *calldata)
145 struct rpcbind_args *map = calldata;
146 struct rpc_xprt *xprt = map->r_xprt;
147 struct rpc_message msg = {
148 .rpc_proc = rpcb_next_version[xprt->bind_index].rpc_proc,
149 .rpc_argp = map,
150 .rpc_resp = &map->r_port,
153 rpc_call_setup(task, &msg, 0);
156 static void rpcb_map_release(void *data)
158 struct rpcbind_args *map = data;
160 xprt_put(map->r_xprt);
161 kfree(map);
164 static const struct rpc_call_ops rpcb_getport_ops = {
165 .rpc_call_prepare = rpcb_getport_prepare,
166 .rpc_call_done = rpcb_getport_done,
167 .rpc_release = rpcb_map_release,
170 static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status)
172 xprt_clear_binding(xprt);
173 rpc_wake_up_status(&xprt->binding, status);
176 static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
177 int proto, int version, int privileged)
179 struct rpc_create_args args = {
180 .protocol = proto,
181 .address = srvaddr,
182 .addrsize = sizeof(struct sockaddr_in),
183 .servername = hostname,
184 .program = &rpcb_program,
185 .version = version,
186 .authflavor = RPC_AUTH_UNIX,
187 .flags = (RPC_CLNT_CREATE_ONESHOT |
188 RPC_CLNT_CREATE_NOPING),
191 ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
192 if (!privileged)
193 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
194 return rpc_create(&args);
198 * rpcb_register - set or unset a port registration with the local rpcbind svc
199 * @prog: RPC program number to bind
200 * @vers: RPC version number to bind
201 * @prot: transport protocol to use to make this request
202 * @port: port value to register
203 * @okay: result code
205 * port == 0 means unregister, port != 0 means register.
207 * This routine supports only rpcbind version 2.
209 int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
211 struct sockaddr_in sin = {
212 .sin_family = AF_INET,
213 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
215 struct rpcbind_args map = {
216 .r_prog = prog,
217 .r_vers = vers,
218 .r_prot = prot,
219 .r_port = port,
221 struct rpc_message msg = {
222 .rpc_proc = &rpcb_procedures2[port ?
223 RPCBPROC_SET : RPCBPROC_UNSET],
224 .rpc_argp = &map,
225 .rpc_resp = okay,
227 struct rpc_clnt *rpcb_clnt;
228 int error = 0;
230 dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
231 "rpcbind\n", (port ? "" : "un"),
232 prog, vers, prot, port);
234 rpcb_clnt = rpcb_create("localhost", (struct sockaddr *) &sin,
235 IPPROTO_UDP, 2, 1);
236 if (IS_ERR(rpcb_clnt))
237 return PTR_ERR(rpcb_clnt);
239 error = rpc_call_sync(rpcb_clnt, &msg, 0);
241 if (error < 0)
242 printk(KERN_WARNING "RPC: failed to contact local rpcbind "
243 "server (errno %d).\n", -error);
244 dprintk("RPC: registration status %d/%d\n", error, *okay);
246 return error;
249 #ifdef CONFIG_ROOT_NFS
251 * rpcb_getport_external - obtain the port for an RPC service on a given host
252 * @sin: address of remote peer
253 * @prog: RPC program number to bind
254 * @vers: RPC version number to bind
255 * @prot: transport protocol to use to make this request
257 * Called from outside the RPC client in a synchronous task context.
259 * For now, this supports only version 2 queries, but is used only by
260 * mount_clnt for NFS_ROOT.
262 int rpcb_getport_external(struct sockaddr_in *sin, __u32 prog,
263 __u32 vers, int prot)
265 struct rpcbind_args map = {
266 .r_prog = prog,
267 .r_vers = vers,
268 .r_prot = prot,
269 .r_port = 0,
271 struct rpc_message msg = {
272 .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
273 .rpc_argp = &map,
274 .rpc_resp = &map.r_port,
276 struct rpc_clnt *rpcb_clnt;
277 char hostname[40];
278 int status;
280 dprintk("RPC: rpcb_getport_external(%u.%u.%u.%u, %u, %u, %d)\n",
281 NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
283 sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr));
284 rpcb_clnt = rpcb_create(hostname, (struct sockaddr *)sin, prot, 2, 0);
285 if (IS_ERR(rpcb_clnt))
286 return PTR_ERR(rpcb_clnt);
288 status = rpc_call_sync(rpcb_clnt, &msg, 0);
290 if (status >= 0) {
291 if (map.r_port != 0)
292 return map.r_port;
293 status = -EACCES;
295 return status;
297 #endif
300 * rpcb_getport - obtain the port for a given RPC service on a given host
301 * @task: task that is waiting for portmapper request
303 * This one can be called for an ongoing RPC request, and can be used in
304 * an async (rpciod) context.
306 void rpcb_getport(struct rpc_task *task)
308 struct rpc_clnt *clnt = task->tk_client;
309 int bind_version;
310 struct rpc_xprt *xprt = task->tk_xprt;
311 struct rpc_clnt *rpcb_clnt;
312 static struct rpcbind_args *map;
313 struct rpc_task *child;
314 struct sockaddr addr;
315 int status;
317 dprintk("RPC: %5u rpcb_getport(%s, %u, %u, %d)\n",
318 task->tk_pid, clnt->cl_server,
319 clnt->cl_prog, clnt->cl_vers, xprt->prot);
321 /* Autobind on cloned rpc clients is discouraged */
322 BUG_ON(clnt->cl_parent != clnt);
324 if (xprt_test_and_set_binding(xprt)) {
325 status = -EACCES; /* tell caller to check again */
326 dprintk("RPC: %5u rpcb_getport waiting for another binder\n",
327 task->tk_pid);
328 goto bailout_nowake;
331 /* Put self on queue before sending rpcbind request, in case
332 * rpcb_getport_done completes before we return from rpc_run_task */
333 rpc_sleep_on(&xprt->binding, task, NULL, NULL);
335 /* Someone else may have bound if we slept */
336 if (xprt_bound(xprt)) {
337 status = 0;
338 dprintk("RPC: %5u rpcb_getport already bound\n", task->tk_pid);
339 goto bailout_nofree;
342 if (rpcb_next_version[xprt->bind_index].rpc_proc == NULL) {
343 xprt->bind_index = 0;
344 status = -EACCES; /* tell caller to try again later */
345 dprintk("RPC: %5u rpcb_getport no more getport versions "
346 "available\n", task->tk_pid);
347 goto bailout_nofree;
349 bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
351 dprintk("RPC: %5u rpcb_getport trying rpcbind version %u\n",
352 task->tk_pid, bind_version);
354 map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
355 if (!map) {
356 status = -ENOMEM;
357 dprintk("RPC: %5u rpcb_getport no memory available\n",
358 task->tk_pid);
359 goto bailout_nofree;
361 map->r_prog = clnt->cl_prog;
362 map->r_vers = clnt->cl_vers;
363 map->r_prot = xprt->prot;
364 map->r_port = 0;
365 map->r_xprt = xprt_get(xprt);
366 map->r_netid = (xprt->prot == IPPROTO_TCP) ? RPCB_NETID_TCP :
367 RPCB_NETID_UDP;
368 memcpy(&map->r_addr, rpc_peeraddr2str(clnt, RPC_DISPLAY_ADDR),
369 sizeof(map->r_addr));
370 map->r_owner = RPCB_OWNER_STRING; /* ignored for GETADDR */
372 rpc_peeraddr(clnt, (void *)&addr, sizeof(addr));
373 rpcb_clnt = rpcb_create(clnt->cl_server, &addr, xprt->prot, bind_version, 0);
374 if (IS_ERR(rpcb_clnt)) {
375 status = PTR_ERR(rpcb_clnt);
376 dprintk("RPC: %5u rpcb_getport rpcb_create failed, error %ld\n",
377 task->tk_pid, PTR_ERR(rpcb_clnt));
378 goto bailout;
381 child = rpc_run_task(rpcb_clnt, RPC_TASK_ASYNC, &rpcb_getport_ops, map);
382 if (IS_ERR(child)) {
383 status = -EIO;
384 dprintk("RPC: %5u rpcb_getport rpc_run_task failed\n",
385 task->tk_pid);
386 goto bailout_nofree;
388 rpc_put_task(child);
390 task->tk_xprt->stat.bind_count++;
391 return;
393 bailout:
394 kfree(map);
395 xprt_put(xprt);
396 bailout_nofree:
397 rpcb_wake_rpcbind_waiters(xprt, status);
398 bailout_nowake:
399 task->tk_status = status;
403 * Rpcbind child task calls this callback via tk_exit.
405 static void rpcb_getport_done(struct rpc_task *child, void *data)
407 struct rpcbind_args *map = data;
408 struct rpc_xprt *xprt = map->r_xprt;
409 int status = child->tk_status;
411 /* rpcbind server doesn't support this rpcbind protocol version */
412 if (status == -EPROTONOSUPPORT)
413 xprt->bind_index++;
415 if (status < 0) {
416 /* rpcbind server not available on remote host? */
417 xprt->ops->set_port(xprt, 0);
418 } else if (map->r_port == 0) {
419 /* Requested RPC service wasn't registered on remote host */
420 xprt->ops->set_port(xprt, 0);
421 status = -EACCES;
422 } else {
423 /* Succeeded */
424 xprt->ops->set_port(xprt, map->r_port);
425 xprt_set_bound(xprt);
426 status = 0;
429 dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
430 child->tk_pid, status, map->r_port);
432 rpcb_wake_rpcbind_waiters(xprt, status);
435 static int rpcb_encode_mapping(struct rpc_rqst *req, __be32 *p,
436 struct rpcbind_args *rpcb)
438 dprintk("RPC: rpcb_encode_mapping(%u, %u, %d, %u)\n",
439 rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
440 *p++ = htonl(rpcb->r_prog);
441 *p++ = htonl(rpcb->r_vers);
442 *p++ = htonl(rpcb->r_prot);
443 *p++ = htonl(rpcb->r_port);
445 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
446 return 0;
449 static int rpcb_decode_getport(struct rpc_rqst *req, __be32 *p,
450 unsigned short *portp)
452 *portp = (unsigned short) ntohl(*p++);
453 dprintk("RPC: rpcb_decode_getport result %u\n",
454 *portp);
455 return 0;
458 static int rpcb_decode_set(struct rpc_rqst *req, __be32 *p,
459 unsigned int *boolp)
461 *boolp = (unsigned int) ntohl(*p++);
462 dprintk("RPC: rpcb_decode_set result %u\n",
463 *boolp);
464 return 0;
467 static int rpcb_encode_getaddr(struct rpc_rqst *req, __be32 *p,
468 struct rpcbind_args *rpcb)
470 dprintk("RPC: rpcb_encode_getaddr(%u, %u, %s)\n",
471 rpcb->r_prog, rpcb->r_vers, rpcb->r_addr);
472 *p++ = htonl(rpcb->r_prog);
473 *p++ = htonl(rpcb->r_vers);
475 p = xdr_encode_string(p, rpcb->r_netid);
476 p = xdr_encode_string(p, rpcb->r_addr);
477 p = xdr_encode_string(p, rpcb->r_owner);
479 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
481 return 0;
484 static int rpcb_decode_getaddr(struct rpc_rqst *req, __be32 *p,
485 unsigned short *portp)
487 char *addr;
488 int addr_len, c, i, f, first, val;
490 *portp = 0;
491 addr_len = (unsigned int) ntohl(*p++);
492 if (addr_len > RPCB_MAXADDRLEN) /* sanity */
493 return -EINVAL;
495 dprintk("RPC: rpcb_decode_getaddr returned string: '%s'\n",
496 (char *) p);
498 addr = (char *)p;
499 val = 0;
500 first = 1;
501 f = 1;
502 for (i = addr_len - 1; i > 0; i--) {
503 c = addr[i];
504 if (c >= '0' && c <= '9') {
505 val += (c - '0') * f;
506 f *= 10;
507 } else if (c == '.') {
508 if (first) {
509 *portp = val;
510 val = first = 0;
511 f = 1;
512 } else {
513 *portp |= (val << 8);
514 break;
519 dprintk("RPC: rpcb_decode_getaddr port=%u\n", *portp);
520 return 0;
523 #define RPCB_program_sz (1u)
524 #define RPCB_version_sz (1u)
525 #define RPCB_protocol_sz (1u)
526 #define RPCB_port_sz (1u)
527 #define RPCB_boolean_sz (1u)
529 #define RPCB_netid_sz (1+XDR_QUADLEN(RPCB_MAXNETIDLEN))
530 #define RPCB_addr_sz (1+XDR_QUADLEN(RPCB_MAXADDRLEN))
531 #define RPCB_ownerstring_sz (1+XDR_QUADLEN(RPCB_MAXOWNERLEN))
533 #define RPCB_mappingargs_sz RPCB_program_sz+RPCB_version_sz+ \
534 RPCB_protocol_sz+RPCB_port_sz
535 #define RPCB_getaddrargs_sz RPCB_program_sz+RPCB_version_sz+ \
536 RPCB_netid_sz+RPCB_addr_sz+ \
537 RPCB_ownerstring_sz
539 #define RPCB_setres_sz RPCB_boolean_sz
540 #define RPCB_getportres_sz RPCB_port_sz
543 * Note that RFC 1833 does not put any size restrictions on the
544 * address string returned by the remote rpcbind database.
546 #define RPCB_getaddrres_sz RPCB_addr_sz
548 #define PROC(proc, argtype, restype) \
549 [RPCBPROC_##proc] = { \
550 .p_proc = RPCBPROC_##proc, \
551 .p_encode = (kxdrproc_t) rpcb_encode_##argtype, \
552 .p_decode = (kxdrproc_t) rpcb_decode_##restype, \
553 .p_arglen = RPCB_##argtype##args_sz, \
554 .p_replen = RPCB_##restype##res_sz, \
555 .p_statidx = RPCBPROC_##proc, \
556 .p_timer = 0, \
557 .p_name = #proc, \
561 * Not all rpcbind procedures described in RFC 1833 are implemented
562 * since the Linux kernel RPC code requires only these.
564 static struct rpc_procinfo rpcb_procedures2[] = {
565 PROC(SET, mapping, set),
566 PROC(UNSET, mapping, set),
567 PROC(GETADDR, mapping, getport),
570 static struct rpc_procinfo rpcb_procedures3[] = {
571 PROC(SET, mapping, set),
572 PROC(UNSET, mapping, set),
573 PROC(GETADDR, getaddr, getaddr),
576 static struct rpc_procinfo rpcb_procedures4[] = {
577 PROC(SET, mapping, set),
578 PROC(UNSET, mapping, set),
579 PROC(GETVERSADDR, getaddr, getaddr),
582 static struct rpcb_info rpcb_next_version[] = {
583 #ifdef CONFIG_SUNRPC_BIND34
584 { 4, &rpcb_procedures4[RPCBPROC_GETVERSADDR] },
585 { 3, &rpcb_procedures3[RPCBPROC_GETADDR] },
586 #endif
587 { 2, &rpcb_procedures2[RPCBPROC_GETPORT] },
588 { 0, NULL },
591 static struct rpc_version rpcb_version2 = {
592 .number = 2,
593 .nrprocs = RPCB_HIGHPROC_2,
594 .procs = rpcb_procedures2
597 static struct rpc_version rpcb_version3 = {
598 .number = 3,
599 .nrprocs = RPCB_HIGHPROC_3,
600 .procs = rpcb_procedures3
603 static struct rpc_version rpcb_version4 = {
604 .number = 4,
605 .nrprocs = RPCB_HIGHPROC_4,
606 .procs = rpcb_procedures4
609 static struct rpc_version *rpcb_version[] = {
610 NULL,
611 NULL,
612 &rpcb_version2,
613 &rpcb_version3,
614 &rpcb_version4
617 static struct rpc_stat rpcb_stats;
619 struct rpc_program rpcb_program = {
620 .name = "rpcbind",
621 .number = RPCBIND_PROGRAM,
622 .nrvers = ARRAY_SIZE(rpcb_version),
623 .version = rpcb_version,
624 .stats = &rpcb_stats,