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)
35 #define RPCBVERS_2 (2u)
36 #define RPCBVERS_3 (3u)
37 #define RPCBVERS_4 (4u)
44 RPCBPROC_GETADDR = 3, /* alias for GETPORT */
47 RPCBPROC_BCAST = 5, /* alias for CALLIT */
57 #define RPCB_HIGHPROC_2 RPCBPROC_CALLIT
58 #define RPCB_HIGHPROC_3 RPCBPROC_TADDR2UADDR
59 #define RPCB_HIGHPROC_4 RPCBPROC_GETSTAT
64 * The "owner" is allowed to unset a service in the rpcbind database.
65 * We always use the following (arbitrary) fixed string.
67 #define RPCB_OWNER_STRING "rpcb"
68 #define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
70 static void rpcb_getport_done(struct rpc_task *, void *);
71 static void rpcb_map_release(void *data);
72 static struct rpc_program rpcb_program;
75 struct rpc_xprt * r_xprt;
80 unsigned short r_port;
88 static struct rpc_procinfo rpcb_procedures2[];
89 static struct rpc_procinfo rpcb_procedures3[];
93 struct rpc_procinfo * rpc_proc;
96 static struct rpcb_info rpcb_next_version[];
97 static struct rpcb_info rpcb_next_version6[];
99 static const struct rpc_call_ops rpcb_getport_ops = {
100 .rpc_call_done = rpcb_getport_done,
101 .rpc_release = rpcb_map_release,
104 static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status)
106 xprt_clear_binding(xprt);
107 rpc_wake_up_status(&xprt->binding, status);
110 static void rpcb_map_release(void *data)
112 struct rpcbind_args *map = data;
114 rpcb_wake_rpcbind_waiters(map->r_xprt, map->r_status);
115 xprt_put(map->r_xprt);
119 static const struct sockaddr_in rpcb_inaddr_loopback = {
120 .sin_family = AF_INET,
121 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
122 .sin_port = htons(RPCBIND_PORT),
125 static struct rpc_clnt *rpcb_create_local(struct sockaddr *addr,
126 size_t addrlen, u32 version)
128 struct rpc_create_args args = {
129 .protocol = XPRT_TRANSPORT_UDP,
132 .servername = "localhost",
133 .program = &rpcb_program,
135 .authflavor = RPC_AUTH_UNIX,
136 .flags = RPC_CLNT_CREATE_NOPING,
139 return rpc_create(&args);
142 static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
143 size_t salen, int proto, u32 version)
145 struct rpc_create_args args = {
149 .servername = hostname,
150 .program = &rpcb_program,
152 .authflavor = RPC_AUTH_UNIX,
153 .flags = (RPC_CLNT_CREATE_NOPING |
154 RPC_CLNT_CREATE_NONPRIVPORT),
157 switch (srvaddr->sa_family) {
159 ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
162 ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
168 return rpc_create(&args);
171 static int rpcb_register_call(struct sockaddr *addr, size_t addrlen,
172 u32 version, struct rpc_message *msg,
175 struct rpc_clnt *rpcb_clnt;
180 rpcb_clnt = rpcb_create_local(addr, addrlen, version);
181 if (!IS_ERR(rpcb_clnt)) {
182 error = rpc_call_sync(rpcb_clnt, msg, 0);
183 rpc_shutdown_client(rpcb_clnt);
185 error = PTR_ERR(rpcb_clnt);
188 printk(KERN_WARNING "RPC: failed to contact local rpcbind "
189 "server (errno %d).\n", -error);
190 dprintk("RPC: registration status %d/%d\n", error, *result);
196 * rpcb_register - set or unset a port registration with the local rpcbind svc
197 * @prog: RPC program number to bind
198 * @vers: RPC version number to bind
199 * @prot: transport protocol to use to make this request
200 * @port: port value to register
203 * port == 0 means unregister, port != 0 means register.
205 * This routine supports only rpcbind version 2.
207 int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
209 struct rpcbind_args map = {
215 struct rpc_message msg = {
220 dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
221 "rpcbind\n", (port ? "" : "un"),
222 prog, vers, prot, port);
224 msg.rpc_proc = &rpcb_procedures2[RPCBPROC_UNSET];
226 msg.rpc_proc = &rpcb_procedures2[RPCBPROC_SET];
228 return rpcb_register_call((struct sockaddr *)&rpcb_inaddr_loopback,
229 sizeof(rpcb_inaddr_loopback),
230 RPCBVERS_2, &msg, okay);
234 * rpcb_getport_sync - obtain the port for an RPC service on a given host
235 * @sin: address of remote peer
236 * @prog: RPC program number to bind
237 * @vers: RPC version number to bind
238 * @prot: transport protocol to use to make this request
240 * Return value is the requested advertised port number,
241 * or a negative errno value.
243 * Called from outside the RPC client in a synchronous task context.
244 * Uses default timeout parameters specified by underlying transport.
246 * XXX: Needs to support IPv6
248 int rpcb_getport_sync(struct sockaddr_in *sin, u32 prog, u32 vers, int prot)
250 struct rpcbind_args map = {
256 struct rpc_message msg = {
257 .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
259 .rpc_resp = &map.r_port,
261 struct rpc_clnt *rpcb_clnt;
264 dprintk("RPC: %s(" NIPQUAD_FMT ", %u, %u, %d)\n",
265 __func__, NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
267 rpcb_clnt = rpcb_create(NULL, (struct sockaddr *)sin,
268 sizeof(*sin), prot, RPCBVERS_2);
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, struct rpc_procinfo *proc)
286 struct rpc_message msg = {
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;
312 struct rpc_procinfo *proc;
314 struct rpc_xprt *xprt = task->tk_xprt;
315 struct rpc_clnt *rpcb_clnt;
316 static struct rpcbind_args *map;
317 struct rpc_task *child;
318 struct sockaddr_storage addr;
319 struct sockaddr *sap = (struct sockaddr *)&addr;
323 dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
324 task->tk_pid, __func__,
325 clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot);
327 /* Autobind on cloned rpc clients is discouraged */
328 BUG_ON(clnt->cl_parent != clnt);
330 /* Put self on the wait queue to ensure we get notified if
331 * some other task is already attempting to bind the port */
332 rpc_sleep_on(&xprt->binding, task, NULL);
334 if (xprt_test_and_set_binding(xprt)) {
335 dprintk("RPC: %5u %s: waiting for another binder\n",
336 task->tk_pid, __func__);
340 /* Someone else may have bound if we slept */
341 if (xprt_bound(xprt)) {
343 dprintk("RPC: %5u %s: already bound\n",
344 task->tk_pid, __func__);
348 salen = rpc_peeraddr(clnt, sap, sizeof(addr));
350 /* Don't ever use rpcbind v2 for AF_INET6 requests */
351 switch (sap->sa_family) {
353 proc = rpcb_next_version[xprt->bind_index].rpc_proc;
354 bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
357 proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
358 bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
361 status = -EAFNOSUPPORT;
362 dprintk("RPC: %5u %s: bad address family\n",
363 task->tk_pid, __func__);
367 xprt->bind_index = 0;
368 status = -EPFNOSUPPORT;
369 dprintk("RPC: %5u %s: no more getport versions available\n",
370 task->tk_pid, __func__);
374 dprintk("RPC: %5u %s: trying rpcbind version %u\n",
375 task->tk_pid, __func__, bind_version);
377 rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot,
379 if (IS_ERR(rpcb_clnt)) {
380 status = PTR_ERR(rpcb_clnt);
381 dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
382 task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
386 map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
389 dprintk("RPC: %5u %s: no memory available\n",
390 task->tk_pid, __func__);
393 map->r_prog = clnt->cl_prog;
394 map->r_vers = clnt->cl_vers;
395 map->r_prot = xprt->prot;
397 map->r_xprt = xprt_get(xprt);
398 map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
399 map->r_addr = rpc_peeraddr2str(rpcb_clnt, RPC_DISPLAY_UNIVERSAL_ADDR);
400 map->r_owner = RPCB_OWNER_STRING; /* ignored for GETADDR */
401 map->r_status = -EIO;
403 child = rpcb_call_async(rpcb_clnt, map, proc);
404 rpc_release_client(rpcb_clnt);
406 /* rpcb_map_release() has freed the arguments */
407 dprintk("RPC: %5u %s: rpc_run_task failed\n",
408 task->tk_pid, __func__);
413 task->tk_xprt->stat.bind_count++;
417 rpcb_wake_rpcbind_waiters(xprt, status);
418 task->tk_status = status;
420 EXPORT_SYMBOL_GPL(rpcb_getport_async);
423 * Rpcbind child task calls this callback via tk_exit.
425 static void rpcb_getport_done(struct rpc_task *child, void *data)
427 struct rpcbind_args *map = data;
428 struct rpc_xprt *xprt = map->r_xprt;
429 int status = child->tk_status;
431 /* Garbage reply: retry with a lesser rpcbind version */
433 status = -EPROTONOSUPPORT;
435 /* rpcbind server doesn't support this rpcbind protocol version */
436 if (status == -EPROTONOSUPPORT)
440 /* rpcbind server not available on remote host? */
441 xprt->ops->set_port(xprt, 0);
442 } else if (map->r_port == 0) {
443 /* Requested RPC service wasn't registered on remote host */
444 xprt->ops->set_port(xprt, 0);
448 xprt->ops->set_port(xprt, map->r_port);
449 xprt_set_bound(xprt);
453 dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
454 child->tk_pid, status, map->r_port);
456 map->r_status = status;
460 * XDR functions for rpcbind
463 static int rpcb_encode_mapping(struct rpc_rqst *req, __be32 *p,
464 struct rpcbind_args *rpcb)
466 dprintk("RPC: rpcb_encode_mapping(%u, %u, %d, %u)\n",
467 rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
468 *p++ = htonl(rpcb->r_prog);
469 *p++ = htonl(rpcb->r_vers);
470 *p++ = htonl(rpcb->r_prot);
471 *p++ = htonl(rpcb->r_port);
473 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
477 static int rpcb_decode_getport(struct rpc_rqst *req, __be32 *p,
478 unsigned short *portp)
480 *portp = (unsigned short) ntohl(*p++);
481 dprintk("RPC: rpcb_decode_getport result %u\n",
486 static int rpcb_decode_set(struct rpc_rqst *req, __be32 *p,
489 *boolp = (unsigned int) ntohl(*p++);
490 dprintk("RPC: rpcb_decode_set: call %s\n",
491 (*boolp ? "succeeded" : "failed"));
495 static int rpcb_encode_getaddr(struct rpc_rqst *req, __be32 *p,
496 struct rpcbind_args *rpcb)
498 dprintk("RPC: rpcb_encode_getaddr(%u, %u, %s)\n",
499 rpcb->r_prog, rpcb->r_vers, rpcb->r_addr);
500 *p++ = htonl(rpcb->r_prog);
501 *p++ = htonl(rpcb->r_vers);
503 p = xdr_encode_string(p, rpcb->r_netid);
504 p = xdr_encode_string(p, rpcb->r_addr);
505 p = xdr_encode_string(p, rpcb->r_owner);
507 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
512 static int rpcb_decode_getaddr(struct rpc_rqst *req, __be32 *p,
513 unsigned short *portp)
517 int c, i, f, first, val;
520 addr_len = ntohl(*p++);
523 * Simple sanity check. The smallest possible universal
524 * address is an IPv4 address string containing 11 bytes.
526 if (addr_len < 11 || addr_len > RPCBIND_MAXUADDRLEN)
530 * Start at the end and walk backwards until the first dot
531 * is encountered. When the second dot is found, we have
532 * both parts of the port number.
538 for (i = addr_len - 1; i > 0; i--) {
540 if (c >= '0' && c <= '9') {
541 val += (c - '0') * f;
543 } else if (c == '.') {
549 *portp |= (val << 8);
556 * Simple sanity check. If we never saw a dot in the reply,
557 * then this was probably just garbage.
562 dprintk("RPC: rpcb_decode_getaddr port=%u\n", *portp);
566 dprintk("RPC: rpcbind server returned malformed reply\n");
570 #define RPCB_program_sz (1u)
571 #define RPCB_version_sz (1u)
572 #define RPCB_protocol_sz (1u)
573 #define RPCB_port_sz (1u)
574 #define RPCB_boolean_sz (1u)
576 #define RPCB_netid_sz (1+XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
577 #define RPCB_addr_sz (1+XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
578 #define RPCB_ownerstring_sz (1+XDR_QUADLEN(RPCB_MAXOWNERLEN))
580 #define RPCB_mappingargs_sz RPCB_program_sz+RPCB_version_sz+ \
581 RPCB_protocol_sz+RPCB_port_sz
582 #define RPCB_getaddrargs_sz RPCB_program_sz+RPCB_version_sz+ \
583 RPCB_netid_sz+RPCB_addr_sz+ \
586 #define RPCB_setres_sz RPCB_boolean_sz
587 #define RPCB_getportres_sz RPCB_port_sz
590 * Note that RFC 1833 does not put any size restrictions on the
591 * address string returned by the remote rpcbind database.
593 #define RPCB_getaddrres_sz RPCB_addr_sz
595 #define PROC(proc, argtype, restype) \
596 [RPCBPROC_##proc] = { \
597 .p_proc = RPCBPROC_##proc, \
598 .p_encode = (kxdrproc_t) rpcb_encode_##argtype, \
599 .p_decode = (kxdrproc_t) rpcb_decode_##restype, \
600 .p_arglen = RPCB_##argtype##args_sz, \
601 .p_replen = RPCB_##restype##res_sz, \
602 .p_statidx = RPCBPROC_##proc, \
608 * Not all rpcbind procedures described in RFC 1833 are implemented
609 * since the Linux kernel RPC code requires only these.
611 static struct rpc_procinfo rpcb_procedures2[] = {
612 PROC(SET, mapping, set),
613 PROC(UNSET, mapping, set),
614 PROC(GETPORT, mapping, getport),
617 static struct rpc_procinfo rpcb_procedures3[] = {
618 PROC(SET, getaddr, set),
619 PROC(UNSET, getaddr, set),
620 PROC(GETADDR, getaddr, getaddr),
623 static struct rpc_procinfo rpcb_procedures4[] = {
624 PROC(SET, getaddr, set),
625 PROC(UNSET, getaddr, set),
626 PROC(GETADDR, getaddr, getaddr),
627 PROC(GETVERSADDR, getaddr, getaddr),
630 static struct rpcb_info rpcb_next_version[] = {
632 .rpc_vers = RPCBVERS_2,
633 .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
640 static struct rpcb_info rpcb_next_version6[] = {
642 .rpc_vers = RPCBVERS_4,
643 .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR],
646 .rpc_vers = RPCBVERS_3,
647 .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR],
654 static struct rpc_version rpcb_version2 = {
655 .number = RPCBVERS_2,
656 .nrprocs = RPCB_HIGHPROC_2,
657 .procs = rpcb_procedures2
660 static struct rpc_version rpcb_version3 = {
661 .number = RPCBVERS_3,
662 .nrprocs = RPCB_HIGHPROC_3,
663 .procs = rpcb_procedures3
666 static struct rpc_version rpcb_version4 = {
667 .number = RPCBVERS_4,
668 .nrprocs = RPCB_HIGHPROC_4,
669 .procs = rpcb_procedures4
672 static struct rpc_version *rpcb_version[] = {
680 static struct rpc_stat rpcb_stats;
682 static struct rpc_program rpcb_program = {
684 .number = RPCBIND_PROGRAM,
685 .nrvers = ARRAY_SIZE(rpcb_version),
686 .version = rpcb_version,
687 .stats = &rpcb_stats,