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>
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
22 #include <linux/sunrpc/clnt.h>
23 #include <linux/sunrpc/sched.h>
26 # define RPCDBG_FACILITY RPCDBG_BIND
29 #define RPCBIND_PROGRAM (100000u)
30 #define RPCBIND_PORT (111u)
37 RPCBPROC_GETADDR
= 3, /* alias for GETPORT */
40 RPCBPROC_BCAST
= 5, /* alias for CALLIT */
50 #define RPCB_HIGHPROC_2 RPCBPROC_CALLIT
51 #define RPCB_HIGHPROC_3 RPCBPROC_TADDR2UADDR
52 #define RPCB_HIGHPROC_4 RPCBPROC_GETSTAT
57 * Quoting RFC 3530, section 2.2:
59 * For TCP over IPv4 and for UDP over IPv4, the format of r_addr is the
64 * The prefix, "h1.h2.h3.h4", is the standard textual form for
65 * representing an IPv4 address, which is always four octets long.
66 * Assuming big-endian ordering, h1, h2, h3, and h4, are respectively,
67 * the first through fourth octets each converted to ASCII-decimal.
68 * Assuming big-endian ordering, p1 and p2 are, respectively, the first
69 * and second octets each converted to ASCII-decimal. For example, if a
70 * host, in big-endian order, has an address of 0x0A010307 and there is
71 * a service listening on, in big endian order, port 0x020F (decimal
72 * 527), then the complete universal address is "10.1.3.7.2.15".
76 * For TCP over IPv6 and for UDP over IPv6, the format of r_addr is the
79 * x1:x2:x3:x4:x5:x6:x7:x8.p1.p2
81 * The suffix "p1.p2" is the service port, and is computed the same way
82 * as with universal addresses for TCP and UDP over IPv4. The prefix,
83 * "x1:x2:x3:x4:x5:x6:x7:x8", is the standard textual form for
84 * representing an IPv6 address as defined in Section 2.2 of [RFC2373].
85 * Additionally, the two alternative forms specified in Section 2.2 of
86 * [RFC2373] are also acceptable.
88 * XXX: Currently this implementation does not explicitly convert the
89 * stored address to US-ASCII on non-ASCII systems.
91 #define RPCB_MAXADDRLEN (128u)
96 * Quoting RFC 3530, section 2.2:
98 * For TCP over IPv4 the value of r_netid is the string "tcp". For UDP
99 * over IPv4 the value of r_netid is the string "udp".
103 * For TCP over IPv6 the value of r_netid is the string "tcp6". For UDP
104 * over IPv6 the value of r_netid is the string "udp6".
106 #define RPCB_NETID_UDP "\165\144\160" /* "udp" */
107 #define RPCB_NETID_TCP "\164\143\160" /* "tcp" */
108 #define RPCB_NETID_UDP6 "\165\144\160\066" /* "udp6" */
109 #define RPCB_NETID_TCP6 "\164\143\160\066" /* "tcp6" */
111 #define RPCB_MAXNETIDLEN (4u)
116 * The "owner" is allowed to unset a service in the rpcbind database.
117 * We always use the following (arbitrary) fixed string.
119 #define RPCB_OWNER_STRING "rpcb"
120 #define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
122 static void rpcb_getport_done(struct rpc_task
*, void *);
123 extern struct rpc_program rpcb_program
;
125 struct rpcbind_args
{
126 struct rpc_xprt
* r_xprt
;
131 unsigned short r_port
;
133 char r_addr
[RPCB_MAXADDRLEN
];
137 static struct rpc_procinfo rpcb_procedures2
[];
138 static struct rpc_procinfo rpcb_procedures3
[];
140 static struct rpcb_info
{
142 struct rpc_procinfo
* rpc_proc
;
143 } rpcb_next_version
[];
145 static void rpcb_getport_prepare(struct rpc_task
*task
, void *calldata
)
147 struct rpcbind_args
*map
= calldata
;
148 struct rpc_xprt
*xprt
= map
->r_xprt
;
149 struct rpc_message msg
= {
150 .rpc_proc
= rpcb_next_version
[xprt
->bind_index
].rpc_proc
,
152 .rpc_resp
= &map
->r_port
,
155 rpc_call_setup(task
, &msg
, 0);
158 static void rpcb_map_release(void *data
)
160 struct rpcbind_args
*map
= data
;
162 xprt_put(map
->r_xprt
);
166 static const struct rpc_call_ops rpcb_getport_ops
= {
167 .rpc_call_prepare
= rpcb_getport_prepare
,
168 .rpc_call_done
= rpcb_getport_done
,
169 .rpc_release
= rpcb_map_release
,
172 static void rpcb_wake_rpcbind_waiters(struct rpc_xprt
*xprt
, int status
)
174 xprt_clear_binding(xprt
);
175 rpc_wake_up_status(&xprt
->binding
, status
);
178 static struct rpc_clnt
*rpcb_create(char *hostname
, struct sockaddr
*srvaddr
,
179 int proto
, int version
, int privileged
)
181 struct rpc_create_args args
= {
184 .addrsize
= sizeof(struct sockaddr_in
),
185 .servername
= hostname
,
186 .program
= &rpcb_program
,
188 .authflavor
= RPC_AUTH_UNIX
,
189 .flags
= (RPC_CLNT_CREATE_NOPING
|
190 RPC_CLNT_CREATE_INTR
),
193 ((struct sockaddr_in
*)srvaddr
)->sin_port
= htons(RPCBIND_PORT
);
195 args
.flags
|= RPC_CLNT_CREATE_NONPRIVPORT
;
196 return rpc_create(&args
);
200 * rpcb_register - set or unset a port registration with the local rpcbind svc
201 * @prog: RPC program number to bind
202 * @vers: RPC version number to bind
203 * @prot: transport protocol to use to make this request
204 * @port: port value to register
207 * port == 0 means unregister, port != 0 means register.
209 * This routine supports only rpcbind version 2.
211 int rpcb_register(u32 prog
, u32 vers
, int prot
, unsigned short port
, int *okay
)
213 struct sockaddr_in sin
= {
214 .sin_family
= AF_INET
,
215 .sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
),
217 struct rpcbind_args map
= {
223 struct rpc_message msg
= {
224 .rpc_proc
= &rpcb_procedures2
[port
?
225 RPCBPROC_SET
: RPCBPROC_UNSET
],
229 struct rpc_clnt
*rpcb_clnt
;
232 dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
233 "rpcbind\n", (port
? "" : "un"),
234 prog
, vers
, prot
, port
);
236 rpcb_clnt
= rpcb_create("localhost", (struct sockaddr
*) &sin
,
238 if (IS_ERR(rpcb_clnt
))
239 return PTR_ERR(rpcb_clnt
);
241 error
= rpc_call_sync(rpcb_clnt
, &msg
, 0);
243 rpc_shutdown_client(rpcb_clnt
);
245 printk(KERN_WARNING
"RPC: failed to contact local rpcbind "
246 "server (errno %d).\n", -error
);
247 dprintk("RPC: registration status %d/%d\n", error
, *okay
);
253 * rpcb_getport_sync - obtain the port for an RPC service on a given host
254 * @sin: address of remote peer
255 * @prog: RPC program number to bind
256 * @vers: RPC version number to bind
257 * @prot: transport protocol to use to make this request
259 * Called from outside the RPC client in a synchronous task context.
260 * Uses default timeout parameters specified by underlying transport.
262 * XXX: Needs to support IPv6, and rpcbind versions 3 and 4
264 int rpcb_getport_sync(struct sockaddr_in
*sin
, __u32 prog
,
265 __u32 vers
, int prot
)
267 struct rpcbind_args map
= {
273 struct rpc_message msg
= {
274 .rpc_proc
= &rpcb_procedures2
[RPCBPROC_GETPORT
],
276 .rpc_resp
= &map
.r_port
,
278 struct rpc_clnt
*rpcb_clnt
;
282 dprintk("RPC: %s(" NIPQUAD_FMT
", %u, %u, %d)\n",
283 __FUNCTION__
, NIPQUAD(sin
->sin_addr
.s_addr
), prog
, vers
, prot
);
285 sprintf(hostname
, NIPQUAD_FMT
, NIPQUAD(sin
->sin_addr
.s_addr
));
286 rpcb_clnt
= rpcb_create(hostname
, (struct sockaddr
*)sin
, prot
, 2, 0);
287 if (IS_ERR(rpcb_clnt
))
288 return PTR_ERR(rpcb_clnt
);
290 status
= rpc_call_sync(rpcb_clnt
, &msg
, 0);
291 rpc_shutdown_client(rpcb_clnt
);
300 EXPORT_SYMBOL_GPL(rpcb_getport_sync
);
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
;
320 dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
321 task
->tk_pid
, __FUNCTION__
,
322 clnt
->cl_server
, clnt
->cl_prog
, clnt
->cl_vers
, xprt
->prot
);
324 /* Autobind on cloned rpc clients is discouraged */
325 BUG_ON(clnt
->cl_parent
!= clnt
);
327 if (xprt_test_and_set_binding(xprt
)) {
328 status
= -EACCES
; /* tell caller to check again */
329 dprintk("RPC: %5u %s: waiting for another binder\n",
330 task
->tk_pid
, __FUNCTION__
);
334 /* Put self on queue before sending rpcbind request, in case
335 * rpcb_getport_done completes before we return from rpc_run_task */
336 rpc_sleep_on(&xprt
->binding
, task
, NULL
, NULL
);
338 /* Someone else may have bound if we slept */
339 if (xprt_bound(xprt
)) {
341 dprintk("RPC: %5u %s: already bound\n",
342 task
->tk_pid
, __FUNCTION__
);
346 if (rpcb_next_version
[xprt
->bind_index
].rpc_proc
== NULL
) {
347 xprt
->bind_index
= 0;
348 status
= -EACCES
; /* tell caller to try again later */
349 dprintk("RPC: %5u %s: no more getport versions available\n",
350 task
->tk_pid
, __FUNCTION__
);
353 bind_version
= rpcb_next_version
[xprt
->bind_index
].rpc_vers
;
355 dprintk("RPC: %5u %s: trying rpcbind version %u\n",
356 task
->tk_pid
, __FUNCTION__
, bind_version
);
358 map
= kzalloc(sizeof(struct rpcbind_args
), GFP_ATOMIC
);
361 dprintk("RPC: %5u %s: no memory available\n",
362 task
->tk_pid
, __FUNCTION__
);
365 map
->r_prog
= clnt
->cl_prog
;
366 map
->r_vers
= clnt
->cl_vers
;
367 map
->r_prot
= xprt
->prot
;
369 map
->r_xprt
= xprt_get(xprt
);
370 map
->r_netid
= (xprt
->prot
== IPPROTO_TCP
) ? RPCB_NETID_TCP
:
372 memcpy(&map
->r_addr
, rpc_peeraddr2str(clnt
, RPC_DISPLAY_ADDR
),
373 sizeof(map
->r_addr
));
374 map
->r_owner
= RPCB_OWNER_STRING
; /* ignored for GETADDR */
376 rpc_peeraddr(clnt
, (void *)&addr
, sizeof(addr
));
377 rpcb_clnt
= rpcb_create(clnt
->cl_server
, &addr
, xprt
->prot
, bind_version
, 0);
378 if (IS_ERR(rpcb_clnt
)) {
379 status
= PTR_ERR(rpcb_clnt
);
380 dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
381 task
->tk_pid
, __FUNCTION__
, PTR_ERR(rpcb_clnt
));
385 child
= rpc_run_task(rpcb_clnt
, RPC_TASK_ASYNC
, &rpcb_getport_ops
, map
);
386 rpc_release_client(rpcb_clnt
);
389 dprintk("RPC: %5u %s: rpc_run_task failed\n",
390 task
->tk_pid
, __FUNCTION__
);
395 task
->tk_xprt
->stat
.bind_count
++;
402 rpcb_wake_rpcbind_waiters(xprt
, status
);
404 task
->tk_status
= status
;
408 * Rpcbind child task calls this callback via tk_exit.
410 static void rpcb_getport_done(struct rpc_task
*child
, void *data
)
412 struct rpcbind_args
*map
= data
;
413 struct rpc_xprt
*xprt
= map
->r_xprt
;
414 int status
= child
->tk_status
;
416 /* rpcbind server doesn't support this rpcbind protocol version */
417 if (status
== -EPROTONOSUPPORT
)
421 /* rpcbind server not available on remote host? */
422 xprt
->ops
->set_port(xprt
, 0);
423 } else if (map
->r_port
== 0) {
424 /* Requested RPC service wasn't registered on remote host */
425 xprt
->ops
->set_port(xprt
, 0);
429 xprt
->ops
->set_port(xprt
, map
->r_port
);
430 xprt_set_bound(xprt
);
434 dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
435 child
->tk_pid
, status
, map
->r_port
);
437 rpcb_wake_rpcbind_waiters(xprt
, status
);
440 static int rpcb_encode_mapping(struct rpc_rqst
*req
, __be32
*p
,
441 struct rpcbind_args
*rpcb
)
443 dprintk("RPC: rpcb_encode_mapping(%u, %u, %d, %u)\n",
444 rpcb
->r_prog
, rpcb
->r_vers
, rpcb
->r_prot
, rpcb
->r_port
);
445 *p
++ = htonl(rpcb
->r_prog
);
446 *p
++ = htonl(rpcb
->r_vers
);
447 *p
++ = htonl(rpcb
->r_prot
);
448 *p
++ = htonl(rpcb
->r_port
);
450 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
454 static int rpcb_decode_getport(struct rpc_rqst
*req
, __be32
*p
,
455 unsigned short *portp
)
457 *portp
= (unsigned short) ntohl(*p
++);
458 dprintk("RPC: rpcb_decode_getport result %u\n",
463 static int rpcb_decode_set(struct rpc_rqst
*req
, __be32
*p
,
466 *boolp
= (unsigned int) ntohl(*p
++);
467 dprintk("RPC: rpcb_decode_set result %u\n",
472 static int rpcb_encode_getaddr(struct rpc_rqst
*req
, __be32
*p
,
473 struct rpcbind_args
*rpcb
)
475 dprintk("RPC: rpcb_encode_getaddr(%u, %u, %s)\n",
476 rpcb
->r_prog
, rpcb
->r_vers
, rpcb
->r_addr
);
477 *p
++ = htonl(rpcb
->r_prog
);
478 *p
++ = htonl(rpcb
->r_vers
);
480 p
= xdr_encode_string(p
, rpcb
->r_netid
);
481 p
= xdr_encode_string(p
, rpcb
->r_addr
);
482 p
= xdr_encode_string(p
, rpcb
->r_owner
);
484 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
489 static int rpcb_decode_getaddr(struct rpc_rqst
*req
, __be32
*p
,
490 unsigned short *portp
)
494 int c
, i
, f
, first
, val
;
497 addr_len
= ntohl(*p
++);
498 if (addr_len
> RPCB_MAXADDRLEN
) /* sanity */
501 dprintk("RPC: rpcb_decode_getaddr returned string: '%s'\n",
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);
525 dprintk("RPC: rpcb_decode_getaddr port=%u\n", *portp
);
529 #define RPCB_program_sz (1u)
530 #define RPCB_version_sz (1u)
531 #define RPCB_protocol_sz (1u)
532 #define RPCB_port_sz (1u)
533 #define RPCB_boolean_sz (1u)
535 #define RPCB_netid_sz (1+XDR_QUADLEN(RPCB_MAXNETIDLEN))
536 #define RPCB_addr_sz (1+XDR_QUADLEN(RPCB_MAXADDRLEN))
537 #define RPCB_ownerstring_sz (1+XDR_QUADLEN(RPCB_MAXOWNERLEN))
539 #define RPCB_mappingargs_sz RPCB_program_sz+RPCB_version_sz+ \
540 RPCB_protocol_sz+RPCB_port_sz
541 #define RPCB_getaddrargs_sz RPCB_program_sz+RPCB_version_sz+ \
542 RPCB_netid_sz+RPCB_addr_sz+ \
545 #define RPCB_setres_sz RPCB_boolean_sz
546 #define RPCB_getportres_sz RPCB_port_sz
549 * Note that RFC 1833 does not put any size restrictions on the
550 * address string returned by the remote rpcbind database.
552 #define RPCB_getaddrres_sz RPCB_addr_sz
554 #define PROC(proc, argtype, restype) \
555 [RPCBPROC_##proc] = { \
556 .p_proc = RPCBPROC_##proc, \
557 .p_encode = (kxdrproc_t) rpcb_encode_##argtype, \
558 .p_decode = (kxdrproc_t) rpcb_decode_##restype, \
559 .p_arglen = RPCB_##argtype##args_sz, \
560 .p_replen = RPCB_##restype##res_sz, \
561 .p_statidx = RPCBPROC_##proc, \
567 * Not all rpcbind procedures described in RFC 1833 are implemented
568 * since the Linux kernel RPC code requires only these.
570 static struct rpc_procinfo rpcb_procedures2
[] = {
571 PROC(SET
, mapping
, set
),
572 PROC(UNSET
, mapping
, set
),
573 PROC(GETADDR
, mapping
, getport
),
576 static struct rpc_procinfo rpcb_procedures3
[] = {
577 PROC(SET
, mapping
, set
),
578 PROC(UNSET
, mapping
, set
),
579 PROC(GETADDR
, getaddr
, getaddr
),
582 static struct rpc_procinfo rpcb_procedures4
[] = {
583 PROC(SET
, mapping
, set
),
584 PROC(UNSET
, mapping
, set
),
585 PROC(GETVERSADDR
, getaddr
, getaddr
),
588 static struct rpcb_info rpcb_next_version
[] = {
589 #ifdef CONFIG_SUNRPC_BIND34
590 { 4, &rpcb_procedures4
[RPCBPROC_GETVERSADDR
] },
591 { 3, &rpcb_procedures3
[RPCBPROC_GETADDR
] },
593 { 2, &rpcb_procedures2
[RPCBPROC_GETPORT
] },
597 static struct rpc_version rpcb_version2
= {
599 .nrprocs
= RPCB_HIGHPROC_2
,
600 .procs
= rpcb_procedures2
603 static struct rpc_version rpcb_version3
= {
605 .nrprocs
= RPCB_HIGHPROC_3
,
606 .procs
= rpcb_procedures3
609 static struct rpc_version rpcb_version4
= {
611 .nrprocs
= RPCB_HIGHPROC_4
,
612 .procs
= rpcb_procedures4
615 static struct rpc_version
*rpcb_version
[] = {
623 static struct rpc_stat rpcb_stats
;
625 struct rpc_program rpcb_program
= {
627 .number
= RPCBIND_PROGRAM
,
628 .nrvers
= ARRAY_SIZE(rpcb_version
),
629 .version
= rpcb_version
,
630 .stats
= &rpcb_stats
,