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 struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
120 size_t salen, int proto, u32 version,
123 struct rpc_create_args args = {
127 .servername = hostname,
128 .program = &rpcb_program,
130 .authflavor = RPC_AUTH_UNIX,
131 .flags = RPC_CLNT_CREATE_NOPING,
134 switch (srvaddr->sa_family) {
136 ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
139 ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
146 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
147 return rpc_create(&args);
151 * rpcb_register - set or unset a port registration with the local rpcbind svc
152 * @prog: RPC program number to bind
153 * @vers: RPC version number to bind
154 * @prot: transport protocol to use to make this request
155 * @port: port value to register
158 * port == 0 means unregister, port != 0 means register.
160 * This routine supports only rpcbind version 2.
162 int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
164 struct sockaddr_in sin = {
165 .sin_family = AF_INET,
166 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
168 struct rpcbind_args map = {
174 struct rpc_message msg = {
175 .rpc_proc = &rpcb_procedures2[port ?
176 RPCBPROC_SET : RPCBPROC_UNSET],
180 struct rpc_clnt *rpcb_clnt;
183 dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
184 "rpcbind\n", (port ? "" : "un"),
185 prog, vers, prot, port);
187 rpcb_clnt = rpcb_create("localhost", (struct sockaddr *) &sin,
188 sizeof(sin), XPRT_TRANSPORT_UDP, RPCBVERS_2, 1);
189 if (IS_ERR(rpcb_clnt))
190 return PTR_ERR(rpcb_clnt);
192 error = rpc_call_sync(rpcb_clnt, &msg, 0);
194 rpc_shutdown_client(rpcb_clnt);
196 printk(KERN_WARNING "RPC: failed to contact local rpcbind "
197 "server (errno %d).\n", -error);
198 dprintk("RPC: registration status %d/%d\n", error, *okay);
204 * rpcb_getport_sync - obtain the port for an RPC service on a given host
205 * @sin: address of remote peer
206 * @prog: RPC program number to bind
207 * @vers: RPC version number to bind
208 * @prot: transport protocol to use to make this request
210 * Return value is the requested advertised port number,
211 * or a negative errno value.
213 * Called from outside the RPC client in a synchronous task context.
214 * Uses default timeout parameters specified by underlying transport.
216 * XXX: Needs to support IPv6
218 int rpcb_getport_sync(struct sockaddr_in *sin, u32 prog, u32 vers, int prot)
220 struct rpcbind_args map = {
226 struct rpc_message msg = {
227 .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
229 .rpc_resp = &map.r_port,
231 struct rpc_clnt *rpcb_clnt;
234 dprintk("RPC: %s(" NIPQUAD_FMT ", %u, %u, %d)\n",
235 __func__, NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
237 rpcb_clnt = rpcb_create(NULL, (struct sockaddr *)sin,
238 sizeof(*sin), prot, RPCBVERS_2, 0);
239 if (IS_ERR(rpcb_clnt))
240 return PTR_ERR(rpcb_clnt);
242 status = rpc_call_sync(rpcb_clnt, &msg, 0);
243 rpc_shutdown_client(rpcb_clnt);
252 EXPORT_SYMBOL_GPL(rpcb_getport_sync);
254 static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt, struct rpcbind_args *map, struct rpc_procinfo *proc)
256 struct rpc_message msg = {
259 .rpc_resp = &map->r_port,
261 struct rpc_task_setup task_setup_data = {
262 .rpc_client = rpcb_clnt,
264 .callback_ops = &rpcb_getport_ops,
265 .callback_data = map,
266 .flags = RPC_TASK_ASYNC,
269 return rpc_run_task(&task_setup_data);
273 * rpcb_getport_async - obtain the port for a given RPC service on a given host
274 * @task: task that is waiting for portmapper request
276 * This one can be called for an ongoing RPC request, and can be used in
277 * an async (rpciod) context.
279 void rpcb_getport_async(struct rpc_task *task)
281 struct rpc_clnt *clnt = task->tk_client;
282 struct rpc_procinfo *proc;
284 struct rpc_xprt *xprt = task->tk_xprt;
285 struct rpc_clnt *rpcb_clnt;
286 static struct rpcbind_args *map;
287 struct rpc_task *child;
288 struct sockaddr_storage addr;
289 struct sockaddr *sap = (struct sockaddr *)&addr;
293 dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
294 task->tk_pid, __func__,
295 clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot);
297 /* Autobind on cloned rpc clients is discouraged */
298 BUG_ON(clnt->cl_parent != clnt);
300 /* Put self on the wait queue to ensure we get notified if
301 * some other task is already attempting to bind the port */
302 rpc_sleep_on(&xprt->binding, task, NULL);
304 if (xprt_test_and_set_binding(xprt)) {
305 dprintk("RPC: %5u %s: waiting for another binder\n",
306 task->tk_pid, __func__);
310 /* Someone else may have bound if we slept */
311 if (xprt_bound(xprt)) {
313 dprintk("RPC: %5u %s: already bound\n",
314 task->tk_pid, __func__);
318 salen = rpc_peeraddr(clnt, sap, sizeof(addr));
320 /* Don't ever use rpcbind v2 for AF_INET6 requests */
321 switch (sap->sa_family) {
323 proc = rpcb_next_version[xprt->bind_index].rpc_proc;
324 bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
327 proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
328 bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
331 status = -EAFNOSUPPORT;
332 dprintk("RPC: %5u %s: bad address family\n",
333 task->tk_pid, __func__);
337 xprt->bind_index = 0;
338 status = -EPFNOSUPPORT;
339 dprintk("RPC: %5u %s: no more getport versions available\n",
340 task->tk_pid, __func__);
344 dprintk("RPC: %5u %s: trying rpcbind version %u\n",
345 task->tk_pid, __func__, bind_version);
347 rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot,
349 if (IS_ERR(rpcb_clnt)) {
350 status = PTR_ERR(rpcb_clnt);
351 dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
352 task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
356 map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
359 dprintk("RPC: %5u %s: no memory available\n",
360 task->tk_pid, __func__);
363 map->r_prog = clnt->cl_prog;
364 map->r_vers = clnt->cl_vers;
365 map->r_prot = xprt->prot;
367 map->r_xprt = xprt_get(xprt);
368 map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
369 map->r_addr = rpc_peeraddr2str(rpcb_clnt, RPC_DISPLAY_UNIVERSAL_ADDR);
370 map->r_owner = RPCB_OWNER_STRING; /* ignored for GETADDR */
371 map->r_status = -EIO;
373 child = rpcb_call_async(rpcb_clnt, map, proc);
374 rpc_release_client(rpcb_clnt);
376 /* rpcb_map_release() has freed the arguments */
377 dprintk("RPC: %5u %s: rpc_run_task failed\n",
378 task->tk_pid, __func__);
383 task->tk_xprt->stat.bind_count++;
387 rpcb_wake_rpcbind_waiters(xprt, status);
388 task->tk_status = status;
390 EXPORT_SYMBOL_GPL(rpcb_getport_async);
393 * Rpcbind child task calls this callback via tk_exit.
395 static void rpcb_getport_done(struct rpc_task *child, void *data)
397 struct rpcbind_args *map = data;
398 struct rpc_xprt *xprt = map->r_xprt;
399 int status = child->tk_status;
401 /* Garbage reply: retry with a lesser rpcbind version */
403 status = -EPROTONOSUPPORT;
405 /* rpcbind server doesn't support this rpcbind protocol version */
406 if (status == -EPROTONOSUPPORT)
410 /* rpcbind server not available on remote host? */
411 xprt->ops->set_port(xprt, 0);
412 } else if (map->r_port == 0) {
413 /* Requested RPC service wasn't registered on remote host */
414 xprt->ops->set_port(xprt, 0);
418 xprt->ops->set_port(xprt, map->r_port);
419 xprt_set_bound(xprt);
423 dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
424 child->tk_pid, status, map->r_port);
426 map->r_status = status;
430 * XDR functions for rpcbind
433 static int rpcb_encode_mapping(struct rpc_rqst *req, __be32 *p,
434 struct rpcbind_args *rpcb)
436 dprintk("RPC: rpcb_encode_mapping(%u, %u, %d, %u)\n",
437 rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
438 *p++ = htonl(rpcb->r_prog);
439 *p++ = htonl(rpcb->r_vers);
440 *p++ = htonl(rpcb->r_prot);
441 *p++ = htonl(rpcb->r_port);
443 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
447 static int rpcb_decode_getport(struct rpc_rqst *req, __be32 *p,
448 unsigned short *portp)
450 *portp = (unsigned short) ntohl(*p++);
451 dprintk("RPC: rpcb_decode_getport result %u\n",
456 static int rpcb_decode_set(struct rpc_rqst *req, __be32 *p,
459 *boolp = (unsigned int) ntohl(*p++);
460 dprintk("RPC: rpcb_decode_set: call %s\n",
461 (*boolp ? "succeeded" : "failed"));
465 static int rpcb_encode_getaddr(struct rpc_rqst *req, __be32 *p,
466 struct rpcbind_args *rpcb)
468 dprintk("RPC: rpcb_encode_getaddr(%u, %u, %s)\n",
469 rpcb->r_prog, rpcb->r_vers, rpcb->r_addr);
470 *p++ = htonl(rpcb->r_prog);
471 *p++ = htonl(rpcb->r_vers);
473 p = xdr_encode_string(p, rpcb->r_netid);
474 p = xdr_encode_string(p, rpcb->r_addr);
475 p = xdr_encode_string(p, rpcb->r_owner);
477 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
482 static int rpcb_decode_getaddr(struct rpc_rqst *req, __be32 *p,
483 unsigned short *portp)
487 int c, i, f, first, val;
490 addr_len = ntohl(*p++);
493 * Simple sanity check. The smallest possible universal
494 * address is an IPv4 address string containing 11 bytes.
496 if (addr_len < 11 || addr_len > RPCBIND_MAXUADDRLEN)
500 * Start at the end and walk backwards until the first dot
501 * is encountered. When the second dot is found, we have
502 * both parts of the port number.
508 for (i = addr_len - 1; i > 0; i--) {
510 if (c >= '0' && c <= '9') {
511 val += (c - '0') * f;
513 } else if (c == '.') {
519 *portp |= (val << 8);
526 * Simple sanity check. If we never saw a dot in the reply,
527 * then this was probably just garbage.
532 dprintk("RPC: rpcb_decode_getaddr port=%u\n", *portp);
536 dprintk("RPC: rpcbind server returned malformed reply\n");
540 #define RPCB_program_sz (1u)
541 #define RPCB_version_sz (1u)
542 #define RPCB_protocol_sz (1u)
543 #define RPCB_port_sz (1u)
544 #define RPCB_boolean_sz (1u)
546 #define RPCB_netid_sz (1+XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
547 #define RPCB_addr_sz (1+XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
548 #define RPCB_ownerstring_sz (1+XDR_QUADLEN(RPCB_MAXOWNERLEN))
550 #define RPCB_mappingargs_sz RPCB_program_sz+RPCB_version_sz+ \
551 RPCB_protocol_sz+RPCB_port_sz
552 #define RPCB_getaddrargs_sz RPCB_program_sz+RPCB_version_sz+ \
553 RPCB_netid_sz+RPCB_addr_sz+ \
556 #define RPCB_setres_sz RPCB_boolean_sz
557 #define RPCB_getportres_sz RPCB_port_sz
560 * Note that RFC 1833 does not put any size restrictions on the
561 * address string returned by the remote rpcbind database.
563 #define RPCB_getaddrres_sz RPCB_addr_sz
565 #define PROC(proc, argtype, restype) \
566 [RPCBPROC_##proc] = { \
567 .p_proc = RPCBPROC_##proc, \
568 .p_encode = (kxdrproc_t) rpcb_encode_##argtype, \
569 .p_decode = (kxdrproc_t) rpcb_decode_##restype, \
570 .p_arglen = RPCB_##argtype##args_sz, \
571 .p_replen = RPCB_##restype##res_sz, \
572 .p_statidx = RPCBPROC_##proc, \
578 * Not all rpcbind procedures described in RFC 1833 are implemented
579 * since the Linux kernel RPC code requires only these.
581 static struct rpc_procinfo rpcb_procedures2[] = {
582 PROC(SET, mapping, set),
583 PROC(UNSET, mapping, set),
584 PROC(GETPORT, mapping, getport),
587 static struct rpc_procinfo rpcb_procedures3[] = {
588 PROC(SET, getaddr, set),
589 PROC(UNSET, getaddr, set),
590 PROC(GETADDR, getaddr, getaddr),
593 static struct rpc_procinfo rpcb_procedures4[] = {
594 PROC(SET, getaddr, set),
595 PROC(UNSET, getaddr, set),
596 PROC(GETADDR, getaddr, getaddr),
597 PROC(GETVERSADDR, getaddr, getaddr),
600 static struct rpcb_info rpcb_next_version[] = {
602 .rpc_vers = RPCBVERS_2,
603 .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
610 static struct rpcb_info rpcb_next_version6[] = {
612 .rpc_vers = RPCBVERS_4,
613 .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR],
616 .rpc_vers = RPCBVERS_3,
617 .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR],
624 static struct rpc_version rpcb_version2 = {
625 .number = RPCBVERS_2,
626 .nrprocs = RPCB_HIGHPROC_2,
627 .procs = rpcb_procedures2
630 static struct rpc_version rpcb_version3 = {
631 .number = RPCBVERS_3,
632 .nrprocs = RPCB_HIGHPROC_3,
633 .procs = rpcb_procedures3
636 static struct rpc_version rpcb_version4 = {
637 .number = RPCBVERS_4,
638 .nrprocs = RPCB_HIGHPROC_4,
639 .procs = rpcb_procedures4
642 static struct rpc_version *rpcb_version[] = {
650 static struct rpc_stat rpcb_stats;
652 static struct rpc_program rpcb_program = {
654 .number = RPCBIND_PROGRAM,
655 .nrvers = ARRAY_SIZE(rpcb_version),
656 .version = rpcb_version,
657 .stats = &rpcb_stats,