2 * In-kernel rpcbind client supporting versions 2, 3, and 4 of the rpcbind
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/module.h>
17 #include <linux/types.h>
18 #include <linux/socket.h>
20 #include <linux/in6.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
24 #include <linux/sunrpc/clnt.h>
25 #include <linux/sunrpc/sched.h>
26 #include <linux/sunrpc/xprtsock.h>
29 # define RPCDBG_FACILITY RPCDBG_BIND
32 #define RPCBIND_PROGRAM (100000u)
33 #define RPCBIND_PORT (111u)
40 RPCBPROC_GETADDR = 3, /* alias for GETPORT */
43 RPCBPROC_BCAST = 5, /* alias for CALLIT */
53 #define RPCB_HIGHPROC_2 RPCBPROC_CALLIT
54 #define RPCB_HIGHPROC_3 RPCBPROC_TADDR2UADDR
55 #define RPCB_HIGHPROC_4 RPCBPROC_GETSTAT
60 * Quoting RFC 3530, section 2.2:
62 * For TCP over IPv4 and for UDP over IPv4, the format of r_addr is the
67 * The prefix, "h1.h2.h3.h4", is the standard textual form for
68 * representing an IPv4 address, which is always four octets long.
69 * Assuming big-endian ordering, h1, h2, h3, and h4, are respectively,
70 * the first through fourth octets each converted to ASCII-decimal.
71 * Assuming big-endian ordering, p1 and p2 are, respectively, the first
72 * and second octets each converted to ASCII-decimal. For example, if a
73 * host, in big-endian order, has an address of 0x0A010307 and there is
74 * a service listening on, in big endian order, port 0x020F (decimal
75 * 527), then the complete universal address is "10.1.3.7.2.15".
79 * For TCP over IPv6 and for UDP over IPv6, the format of r_addr is the
82 * x1:x2:x3:x4:x5:x6:x7:x8.p1.p2
84 * The suffix "p1.p2" is the service port, and is computed the same way
85 * as with universal addresses for TCP and UDP over IPv4. The prefix,
86 * "x1:x2:x3:x4:x5:x6:x7:x8", is the standard textual form for
87 * representing an IPv6 address as defined in Section 2.2 of [RFC2373].
88 * Additionally, the two alternative forms specified in Section 2.2 of
89 * [RFC2373] are also acceptable.
91 * XXX: Currently this implementation does not explicitly convert the
92 * stored address to US-ASCII on non-ASCII systems.
94 #define RPCB_MAXADDRLEN (128u)
99 * The "owner" is allowed to unset a service in the rpcbind database.
100 * We always use the following (arbitrary) fixed string.
102 #define RPCB_OWNER_STRING "rpcb"
103 #define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
105 static void rpcb_getport_done(struct rpc_task *, void *);
106 static struct rpc_program rpcb_program;
108 struct rpcbind_args {
109 struct rpc_xprt * r_xprt;
114 unsigned short r_port;
116 char r_addr[RPCB_MAXADDRLEN];
120 static struct rpc_procinfo rpcb_procedures2[];
121 static struct rpc_procinfo rpcb_procedures3[];
125 struct rpc_procinfo * rpc_proc;
128 static struct rpcb_info rpcb_next_version[];
129 static struct rpcb_info rpcb_next_version6[];
131 static void rpcb_map_release(void *data)
133 struct rpcbind_args *map = data;
135 xprt_put(map->r_xprt);
139 static const struct rpc_call_ops rpcb_getport_ops = {
140 .rpc_call_done = rpcb_getport_done,
141 .rpc_release = rpcb_map_release,
144 static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status)
146 xprt_clear_binding(xprt);
147 rpc_wake_up_status(&xprt->binding, status);
150 static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
151 int proto, int version, int privileged)
153 struct rpc_create_args args = {
156 .addrsize = sizeof(struct sockaddr_in),
157 .servername = hostname,
158 .program = &rpcb_program,
160 .authflavor = RPC_AUTH_UNIX,
161 .flags = (RPC_CLNT_CREATE_NOPING |
162 RPC_CLNT_CREATE_INTR),
165 switch (srvaddr->sa_family) {
167 ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
170 ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
177 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
178 return rpc_create(&args);
182 * rpcb_register - set or unset a port registration with the local rpcbind svc
183 * @prog: RPC program number to bind
184 * @vers: RPC version number to bind
185 * @prot: transport protocol to use to make this request
186 * @port: port value to register
189 * port == 0 means unregister, port != 0 means register.
191 * This routine supports only rpcbind version 2.
193 int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
195 struct sockaddr_in sin = {
196 .sin_family = AF_INET,
197 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
199 struct rpcbind_args map = {
205 struct rpc_message msg = {
206 .rpc_proc = &rpcb_procedures2[port ?
207 RPCBPROC_SET : RPCBPROC_UNSET],
211 struct rpc_clnt *rpcb_clnt;
214 dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
215 "rpcbind\n", (port ? "" : "un"),
216 prog, vers, prot, port);
218 rpcb_clnt = rpcb_create("localhost", (struct sockaddr *) &sin,
219 XPRT_TRANSPORT_UDP, 2, 1);
220 if (IS_ERR(rpcb_clnt))
221 return PTR_ERR(rpcb_clnt);
223 error = rpc_call_sync(rpcb_clnt, &msg, 0);
225 rpc_shutdown_client(rpcb_clnt);
227 printk(KERN_WARNING "RPC: failed to contact local rpcbind "
228 "server (errno %d).\n", -error);
229 dprintk("RPC: registration status %d/%d\n", error, *okay);
235 * rpcb_getport_sync - obtain the port for an RPC service on a given host
236 * @sin: address of remote peer
237 * @prog: RPC program number to bind
238 * @vers: RPC version number to bind
239 * @prot: transport protocol to use to make this request
241 * Called from outside the RPC client in a synchronous task context.
242 * Uses default timeout parameters specified by underlying transport.
244 * XXX: Needs to support IPv6, and rpcbind versions 3 and 4
246 int rpcb_getport_sync(struct sockaddr_in *sin, __u32 prog,
247 __u32 vers, int prot)
249 struct rpcbind_args map = {
255 struct rpc_message msg = {
256 .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
258 .rpc_resp = &map.r_port,
260 struct rpc_clnt *rpcb_clnt;
264 dprintk("RPC: %s(" NIPQUAD_FMT ", %u, %u, %d)\n",
265 __FUNCTION__, NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
267 sprintf(hostname, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
268 rpcb_clnt = rpcb_create(hostname, (struct sockaddr *)sin, prot, 2, 0);
269 if (IS_ERR(rpcb_clnt))
270 return PTR_ERR(rpcb_clnt);
272 status = rpc_call_sync(rpcb_clnt, &msg, 0);
273 rpc_shutdown_client(rpcb_clnt);
282 EXPORT_SYMBOL_GPL(rpcb_getport_sync);
284 static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt, struct rpcbind_args *map, int version)
286 struct rpc_message msg = {
287 .rpc_proc = rpcb_next_version[version].rpc_proc,
289 .rpc_resp = &map->r_port,
291 struct rpc_task_setup task_setup_data = {
292 .rpc_client = rpcb_clnt,
294 .callback_ops = &rpcb_getport_ops,
295 .callback_data = map,
296 .flags = RPC_TASK_ASYNC,
299 return rpc_run_task(&task_setup_data);
303 * rpcb_getport_async - obtain the port for a given RPC service on a given host
304 * @task: task that is waiting for portmapper request
306 * This one can be called for an ongoing RPC request, and can be used in
307 * an async (rpciod) context.
309 void rpcb_getport_async(struct rpc_task *task)
311 struct rpc_clnt *clnt = task->tk_client;
313 struct rpc_xprt *xprt = task->tk_xprt;
314 struct rpc_clnt *rpcb_clnt;
315 static struct rpcbind_args *map;
316 struct rpc_task *child;
317 struct sockaddr addr;
319 struct rpcb_info *info;
321 dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
322 task->tk_pid, __FUNCTION__,
323 clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot);
325 /* Autobind on cloned rpc clients is discouraged */
326 BUG_ON(clnt->cl_parent != clnt);
328 if (xprt_test_and_set_binding(xprt)) {
329 status = -EAGAIN; /* tell caller to check again */
330 dprintk("RPC: %5u %s: waiting for another binder\n",
331 task->tk_pid, __FUNCTION__);
335 /* Put self on queue before sending rpcbind request, in case
336 * rpcb_getport_done completes before we return from rpc_run_task */
337 rpc_sleep_on(&xprt->binding, task, NULL, NULL);
339 /* Someone else may have bound if we slept */
340 if (xprt_bound(xprt)) {
342 dprintk("RPC: %5u %s: already bound\n",
343 task->tk_pid, __FUNCTION__);
347 rpc_peeraddr(clnt, (void *)&addr, sizeof(addr));
349 /* Don't ever use rpcbind v2 for AF_INET6 requests */
350 switch (addr.sa_family) {
352 info = rpcb_next_version;
355 info = rpcb_next_version6;
358 status = -EAFNOSUPPORT;
359 dprintk("RPC: %5u %s: bad address family\n",
360 task->tk_pid, __FUNCTION__);
363 if (info[xprt->bind_index].rpc_proc == NULL) {
364 xprt->bind_index = 0;
365 status = -EPFNOSUPPORT;
366 dprintk("RPC: %5u %s: no more getport versions available\n",
367 task->tk_pid, __FUNCTION__);
370 bind_version = info[xprt->bind_index].rpc_vers;
372 dprintk("RPC: %5u %s: trying rpcbind version %u\n",
373 task->tk_pid, __FUNCTION__, bind_version);
375 rpcb_clnt = rpcb_create(clnt->cl_server, &addr, xprt->prot,
377 if (IS_ERR(rpcb_clnt)) {
378 status = PTR_ERR(rpcb_clnt);
379 dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
380 task->tk_pid, __FUNCTION__, PTR_ERR(rpcb_clnt));
384 map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
387 dprintk("RPC: %5u %s: no memory available\n",
388 task->tk_pid, __FUNCTION__);
391 map->r_prog = clnt->cl_prog;
392 map->r_vers = clnt->cl_vers;
393 map->r_prot = xprt->prot;
395 map->r_xprt = xprt_get(xprt);
396 map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
398 rpc_peeraddr2str(rpcb_clnt, RPC_DISPLAY_UNIVERSAL_ADDR),
399 sizeof(map->r_addr));
400 map->r_owner = RPCB_OWNER_STRING; /* ignored for GETADDR */
402 child = rpcb_call_async(rpcb_clnt, map, xprt->bind_index);
403 rpc_release_client(rpcb_clnt);
406 dprintk("RPC: %5u %s: rpc_run_task failed\n",
407 task->tk_pid, __FUNCTION__);
412 task->tk_xprt->stat.bind_count++;
419 rpcb_wake_rpcbind_waiters(xprt, status);
421 task->tk_status = status;
423 EXPORT_SYMBOL_GPL(rpcb_getport_async);
426 * Rpcbind child task calls this callback via tk_exit.
428 static void rpcb_getport_done(struct rpc_task *child, void *data)
430 struct rpcbind_args *map = data;
431 struct rpc_xprt *xprt = map->r_xprt;
432 int status = child->tk_status;
434 /* Garbage reply: retry with a lesser rpcbind version */
436 status = -EPROTONOSUPPORT;
438 /* rpcbind server doesn't support this rpcbind protocol version */
439 if (status == -EPROTONOSUPPORT)
443 /* rpcbind server not available on remote host? */
444 xprt->ops->set_port(xprt, 0);
445 } else if (map->r_port == 0) {
446 /* Requested RPC service wasn't registered on remote host */
447 xprt->ops->set_port(xprt, 0);
451 xprt->ops->set_port(xprt, map->r_port);
452 xprt_set_bound(xprt);
456 dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
457 child->tk_pid, status, map->r_port);
459 rpcb_wake_rpcbind_waiters(xprt, status);
462 static int rpcb_encode_mapping(struct rpc_rqst *req, __be32 *p,
463 struct rpcbind_args *rpcb)
465 dprintk("RPC: rpcb_encode_mapping(%u, %u, %d, %u)\n",
466 rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
467 *p++ = htonl(rpcb->r_prog);
468 *p++ = htonl(rpcb->r_vers);
469 *p++ = htonl(rpcb->r_prot);
470 *p++ = htonl(rpcb->r_port);
472 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
476 static int rpcb_decode_getport(struct rpc_rqst *req, __be32 *p,
477 unsigned short *portp)
479 *portp = (unsigned short) ntohl(*p++);
480 dprintk("RPC: rpcb_decode_getport result %u\n",
485 static int rpcb_decode_set(struct rpc_rqst *req, __be32 *p,
488 *boolp = (unsigned int) ntohl(*p++);
489 dprintk("RPC: rpcb_decode_set result %u\n",
494 static int rpcb_encode_getaddr(struct rpc_rqst *req, __be32 *p,
495 struct rpcbind_args *rpcb)
497 dprintk("RPC: rpcb_encode_getaddr(%u, %u, %s)\n",
498 rpcb->r_prog, rpcb->r_vers, rpcb->r_addr);
499 *p++ = htonl(rpcb->r_prog);
500 *p++ = htonl(rpcb->r_vers);
502 p = xdr_encode_string(p, rpcb->r_netid);
503 p = xdr_encode_string(p, rpcb->r_addr);
504 p = xdr_encode_string(p, rpcb->r_owner);
506 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
511 static int rpcb_decode_getaddr(struct rpc_rqst *req, __be32 *p,
512 unsigned short *portp)
516 int c, i, f, first, val;
519 addr_len = ntohl(*p++);
522 * Simple sanity check. The smallest possible universal
523 * address is an IPv4 address string containing 11 bytes.
525 if (addr_len < 11 || addr_len > RPCB_MAXADDRLEN)
529 * Start at the end and walk backwards until the first dot
530 * is encountered. When the second dot is found, we have
531 * both parts of the port number.
537 for (i = addr_len - 1; i > 0; i--) {
539 if (c >= '0' && c <= '9') {
540 val += (c - '0') * f;
542 } else if (c == '.') {
548 *portp |= (val << 8);
555 * Simple sanity check. If we never saw a dot in the reply,
556 * then this was probably just garbage.
561 dprintk("RPC: rpcb_decode_getaddr port=%u\n", *portp);
565 dprintk("RPC: rpcbind server returned malformed reply\n");
569 #define RPCB_program_sz (1u)
570 #define RPCB_version_sz (1u)
571 #define RPCB_protocol_sz (1u)
572 #define RPCB_port_sz (1u)
573 #define RPCB_boolean_sz (1u)
575 #define RPCB_netid_sz (1+XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
576 #define RPCB_addr_sz (1+XDR_QUADLEN(RPCB_MAXADDRLEN))
577 #define RPCB_ownerstring_sz (1+XDR_QUADLEN(RPCB_MAXOWNERLEN))
579 #define RPCB_mappingargs_sz RPCB_program_sz+RPCB_version_sz+ \
580 RPCB_protocol_sz+RPCB_port_sz
581 #define RPCB_getaddrargs_sz RPCB_program_sz+RPCB_version_sz+ \
582 RPCB_netid_sz+RPCB_addr_sz+ \
585 #define RPCB_setres_sz RPCB_boolean_sz
586 #define RPCB_getportres_sz RPCB_port_sz
589 * Note that RFC 1833 does not put any size restrictions on the
590 * address string returned by the remote rpcbind database.
592 #define RPCB_getaddrres_sz RPCB_addr_sz
594 #define PROC(proc, argtype, restype) \
595 [RPCBPROC_##proc] = { \
596 .p_proc = RPCBPROC_##proc, \
597 .p_encode = (kxdrproc_t) rpcb_encode_##argtype, \
598 .p_decode = (kxdrproc_t) rpcb_decode_##restype, \
599 .p_arglen = RPCB_##argtype##args_sz, \
600 .p_replen = RPCB_##restype##res_sz, \
601 .p_statidx = RPCBPROC_##proc, \
607 * Not all rpcbind procedures described in RFC 1833 are implemented
608 * since the Linux kernel RPC code requires only these.
610 static struct rpc_procinfo rpcb_procedures2[] = {
611 PROC(SET, mapping, set),
612 PROC(UNSET, mapping, set),
613 PROC(GETADDR, mapping, getport),
616 static struct rpc_procinfo rpcb_procedures3[] = {
617 PROC(SET, mapping, set),
618 PROC(UNSET, mapping, set),
619 PROC(GETADDR, getaddr, getaddr),
622 static struct rpc_procinfo rpcb_procedures4[] = {
623 PROC(SET, mapping, set),
624 PROC(UNSET, mapping, set),
625 PROC(GETVERSADDR, getaddr, getaddr),
628 static struct rpcb_info rpcb_next_version[] = {
629 #ifdef CONFIG_SUNRPC_BIND34
630 { 4, &rpcb_procedures4[RPCBPROC_GETVERSADDR] },
631 { 3, &rpcb_procedures3[RPCBPROC_GETADDR] },
633 { 2, &rpcb_procedures2[RPCBPROC_GETPORT] },
637 static struct rpcb_info rpcb_next_version6[] = {
638 #ifdef CONFIG_SUNRPC_BIND34
639 { 4, &rpcb_procedures4[RPCBPROC_GETVERSADDR] },
640 { 3, &rpcb_procedures3[RPCBPROC_GETADDR] },
645 static struct rpc_version rpcb_version2 = {
647 .nrprocs = RPCB_HIGHPROC_2,
648 .procs = rpcb_procedures2
651 static struct rpc_version rpcb_version3 = {
653 .nrprocs = RPCB_HIGHPROC_3,
654 .procs = rpcb_procedures3
657 static struct rpc_version rpcb_version4 = {
659 .nrprocs = RPCB_HIGHPROC_4,
660 .procs = rpcb_procedures4
663 static struct rpc_version *rpcb_version[] = {
671 static struct rpc_stat rpcb_stats;
673 static struct rpc_program rpcb_program = {
675 .number = RPCBIND_PROGRAM,
676 .nrvers = ARRAY_SIZE(rpcb_version),
677 .version = rpcb_version,
678 .stats = &rpcb_stats,