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 * The "owner" is allowed to unset a service in the rpcbind database.
61 * We always use the following (arbitrary) fixed string.
63 #define RPCB_OWNER_STRING "rpcb"
64 #define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
66 static void rpcb_getport_done(struct rpc_task
*, void *);
67 static struct rpc_program rpcb_program
;
70 struct rpc_xprt
* r_xprt
;
75 unsigned short r_port
;
81 static struct rpc_procinfo rpcb_procedures2
[];
82 static struct rpc_procinfo rpcb_procedures3
[];
86 struct rpc_procinfo
* rpc_proc
;
89 static struct rpcb_info rpcb_next_version
[];
90 static struct rpcb_info rpcb_next_version6
[];
92 static void rpcb_map_release(void *data
)
94 struct rpcbind_args
*map
= data
;
96 xprt_put(map
->r_xprt
);
100 static const struct rpc_call_ops rpcb_getport_ops
= {
101 .rpc_call_done
= rpcb_getport_done
,
102 .rpc_release
= rpcb_map_release
,
105 static void rpcb_wake_rpcbind_waiters(struct rpc_xprt
*xprt
, int status
)
107 xprt_clear_binding(xprt
);
108 rpc_wake_up_status(&xprt
->binding
, status
);
111 static struct rpc_clnt
*rpcb_create(char *hostname
, struct sockaddr
*srvaddr
,
112 size_t salen
, int proto
, u32 version
,
115 struct rpc_create_args args
= {
119 .servername
= hostname
,
120 .program
= &rpcb_program
,
122 .authflavor
= RPC_AUTH_UNIX
,
123 .flags
= (RPC_CLNT_CREATE_NOPING
|
124 RPC_CLNT_CREATE_INTR
),
127 switch (srvaddr
->sa_family
) {
129 ((struct sockaddr_in
*)srvaddr
)->sin_port
= htons(RPCBIND_PORT
);
132 ((struct sockaddr_in6
*)srvaddr
)->sin6_port
= htons(RPCBIND_PORT
);
139 args
.flags
|= RPC_CLNT_CREATE_NONPRIVPORT
;
140 return rpc_create(&args
);
144 * rpcb_register - set or unset a port registration with the local rpcbind svc
145 * @prog: RPC program number to bind
146 * @vers: RPC version number to bind
147 * @prot: transport protocol to use to make this request
148 * @port: port value to register
151 * port == 0 means unregister, port != 0 means register.
153 * This routine supports only rpcbind version 2.
155 int rpcb_register(u32 prog
, u32 vers
, int prot
, unsigned short port
, int *okay
)
157 struct sockaddr_in sin
= {
158 .sin_family
= AF_INET
,
159 .sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
),
161 struct rpcbind_args map
= {
167 struct rpc_message msg
= {
168 .rpc_proc
= &rpcb_procedures2
[port
?
169 RPCBPROC_SET
: RPCBPROC_UNSET
],
173 struct rpc_clnt
*rpcb_clnt
;
176 dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
177 "rpcbind\n", (port
? "" : "un"),
178 prog
, vers
, prot
, port
);
180 rpcb_clnt
= rpcb_create("localhost", (struct sockaddr
*) &sin
,
181 sizeof(sin
), XPRT_TRANSPORT_UDP
, 2, 1);
182 if (IS_ERR(rpcb_clnt
))
183 return PTR_ERR(rpcb_clnt
);
185 error
= rpc_call_sync(rpcb_clnt
, &msg
, 0);
187 rpc_shutdown_client(rpcb_clnt
);
189 printk(KERN_WARNING
"RPC: failed to contact local rpcbind "
190 "server (errno %d).\n", -error
);
191 dprintk("RPC: registration status %d/%d\n", error
, *okay
);
197 * rpcb_getport_sync - obtain the port for an RPC service on a given host
198 * @sin: address of remote peer
199 * @prog: RPC program number to bind
200 * @vers: RPC version number to bind
201 * @prot: transport protocol to use to make this request
203 * Return value is the requested advertised port number,
204 * or a negative errno value.
206 * Called from outside the RPC client in a synchronous task context.
207 * Uses default timeout parameters specified by underlying transport.
209 * XXX: Needs to support IPv6
211 int rpcb_getport_sync(struct sockaddr_in
*sin
, u32 prog
, u32 vers
, int prot
)
213 struct rpcbind_args map
= {
219 struct rpc_message msg
= {
220 .rpc_proc
= &rpcb_procedures2
[RPCBPROC_GETPORT
],
222 .rpc_resp
= &map
.r_port
,
224 struct rpc_clnt
*rpcb_clnt
;
227 dprintk("RPC: %s(" NIPQUAD_FMT
", %u, %u, %d)\n",
228 __FUNCTION__
, NIPQUAD(sin
->sin_addr
.s_addr
), prog
, vers
, prot
);
230 rpcb_clnt
= rpcb_create(NULL
, (struct sockaddr
*)sin
,
231 sizeof(*sin
), prot
, 2, 0);
232 if (IS_ERR(rpcb_clnt
))
233 return PTR_ERR(rpcb_clnt
);
235 status
= rpc_call_sync(rpcb_clnt
, &msg
, 0);
236 rpc_shutdown_client(rpcb_clnt
);
245 EXPORT_SYMBOL_GPL(rpcb_getport_sync
);
247 static struct rpc_task
*rpcb_call_async(struct rpc_clnt
*rpcb_clnt
, struct rpcbind_args
*map
, int version
)
249 struct rpc_message msg
= {
250 .rpc_proc
= rpcb_next_version
[version
].rpc_proc
,
252 .rpc_resp
= &map
->r_port
,
254 struct rpc_task_setup task_setup_data
= {
255 .rpc_client
= rpcb_clnt
,
257 .callback_ops
= &rpcb_getport_ops
,
258 .callback_data
= map
,
259 .flags
= RPC_TASK_ASYNC
,
262 return rpc_run_task(&task_setup_data
);
266 * rpcb_getport_async - obtain the port for a given RPC service on a given host
267 * @task: task that is waiting for portmapper request
269 * This one can be called for an ongoing RPC request, and can be used in
270 * an async (rpciod) context.
272 void rpcb_getport_async(struct rpc_task
*task
)
274 struct rpc_clnt
*clnt
= task
->tk_client
;
276 struct rpc_xprt
*xprt
= task
->tk_xprt
;
277 struct rpc_clnt
*rpcb_clnt
;
278 static struct rpcbind_args
*map
;
279 struct rpc_task
*child
;
280 struct sockaddr_storage addr
;
281 struct sockaddr
*sap
= (struct sockaddr
*)&addr
;
284 struct rpcb_info
*info
;
286 dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
287 task
->tk_pid
, __FUNCTION__
,
288 clnt
->cl_server
, clnt
->cl_prog
, clnt
->cl_vers
, xprt
->prot
);
290 /* Autobind on cloned rpc clients is discouraged */
291 BUG_ON(clnt
->cl_parent
!= clnt
);
293 if (xprt_test_and_set_binding(xprt
)) {
294 status
= -EAGAIN
; /* tell caller to check again */
295 dprintk("RPC: %5u %s: waiting for another binder\n",
296 task
->tk_pid
, __FUNCTION__
);
300 /* Put self on queue before sending rpcbind request, in case
301 * rpcb_getport_done completes before we return from rpc_run_task */
302 rpc_sleep_on(&xprt
->binding
, task
, NULL
, NULL
);
304 /* Someone else may have bound if we slept */
305 if (xprt_bound(xprt
)) {
307 dprintk("RPC: %5u %s: already bound\n",
308 task
->tk_pid
, __FUNCTION__
);
312 salen
= rpc_peeraddr(clnt
, sap
, sizeof(addr
));
314 /* Don't ever use rpcbind v2 for AF_INET6 requests */
315 switch (sap
->sa_family
) {
317 info
= rpcb_next_version
;
320 info
= rpcb_next_version6
;
323 status
= -EAFNOSUPPORT
;
324 dprintk("RPC: %5u %s: bad address family\n",
325 task
->tk_pid
, __FUNCTION__
);
328 if (info
[xprt
->bind_index
].rpc_proc
== NULL
) {
329 xprt
->bind_index
= 0;
330 status
= -EPFNOSUPPORT
;
331 dprintk("RPC: %5u %s: no more getport versions available\n",
332 task
->tk_pid
, __FUNCTION__
);
335 bind_version
= info
[xprt
->bind_index
].rpc_vers
;
337 dprintk("RPC: %5u %s: trying rpcbind version %u\n",
338 task
->tk_pid
, __FUNCTION__
, bind_version
);
340 rpcb_clnt
= rpcb_create(clnt
->cl_server
, sap
, salen
, xprt
->prot
,
342 if (IS_ERR(rpcb_clnt
)) {
343 status
= PTR_ERR(rpcb_clnt
);
344 dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
345 task
->tk_pid
, __FUNCTION__
, PTR_ERR(rpcb_clnt
));
349 map
= kzalloc(sizeof(struct rpcbind_args
), GFP_ATOMIC
);
352 dprintk("RPC: %5u %s: no memory available\n",
353 task
->tk_pid
, __FUNCTION__
);
356 map
->r_prog
= clnt
->cl_prog
;
357 map
->r_vers
= clnt
->cl_vers
;
358 map
->r_prot
= xprt
->prot
;
360 map
->r_xprt
= xprt_get(xprt
);
361 map
->r_netid
= rpc_peeraddr2str(clnt
, RPC_DISPLAY_NETID
);
362 map
->r_addr
= rpc_peeraddr2str(rpcb_clnt
, RPC_DISPLAY_UNIVERSAL_ADDR
);
363 map
->r_owner
= RPCB_OWNER_STRING
; /* ignored for GETADDR */
365 child
= rpcb_call_async(rpcb_clnt
, map
, xprt
->bind_index
);
366 rpc_release_client(rpcb_clnt
);
369 dprintk("RPC: %5u %s: rpc_run_task failed\n",
370 task
->tk_pid
, __FUNCTION__
);
375 task
->tk_xprt
->stat
.bind_count
++;
382 rpcb_wake_rpcbind_waiters(xprt
, status
);
384 task
->tk_status
= status
;
386 EXPORT_SYMBOL_GPL(rpcb_getport_async
);
389 * Rpcbind child task calls this callback via tk_exit.
391 static void rpcb_getport_done(struct rpc_task
*child
, void *data
)
393 struct rpcbind_args
*map
= data
;
394 struct rpc_xprt
*xprt
= map
->r_xprt
;
395 int status
= child
->tk_status
;
397 /* Garbage reply: retry with a lesser rpcbind version */
399 status
= -EPROTONOSUPPORT
;
401 /* rpcbind server doesn't support this rpcbind protocol version */
402 if (status
== -EPROTONOSUPPORT
)
406 /* rpcbind server not available on remote host? */
407 xprt
->ops
->set_port(xprt
, 0);
408 } else if (map
->r_port
== 0) {
409 /* Requested RPC service wasn't registered on remote host */
410 xprt
->ops
->set_port(xprt
, 0);
414 xprt
->ops
->set_port(xprt
, map
->r_port
);
415 xprt_set_bound(xprt
);
419 dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
420 child
->tk_pid
, status
, map
->r_port
);
422 rpcb_wake_rpcbind_waiters(xprt
, status
);
425 static int rpcb_encode_mapping(struct rpc_rqst
*req
, __be32
*p
,
426 struct rpcbind_args
*rpcb
)
428 dprintk("RPC: rpcb_encode_mapping(%u, %u, %d, %u)\n",
429 rpcb
->r_prog
, rpcb
->r_vers
, rpcb
->r_prot
, rpcb
->r_port
);
430 *p
++ = htonl(rpcb
->r_prog
);
431 *p
++ = htonl(rpcb
->r_vers
);
432 *p
++ = htonl(rpcb
->r_prot
);
433 *p
++ = htonl(rpcb
->r_port
);
435 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
439 static int rpcb_decode_getport(struct rpc_rqst
*req
, __be32
*p
,
440 unsigned short *portp
)
442 *portp
= (unsigned short) ntohl(*p
++);
443 dprintk("RPC: rpcb_decode_getport result %u\n",
448 static int rpcb_decode_set(struct rpc_rqst
*req
, __be32
*p
,
451 *boolp
= (unsigned int) ntohl(*p
++);
452 dprintk("RPC: rpcb_decode_set result %u\n",
457 static int rpcb_encode_getaddr(struct rpc_rqst
*req
, __be32
*p
,
458 struct rpcbind_args
*rpcb
)
460 dprintk("RPC: rpcb_encode_getaddr(%u, %u, %s)\n",
461 rpcb
->r_prog
, rpcb
->r_vers
, rpcb
->r_addr
);
462 *p
++ = htonl(rpcb
->r_prog
);
463 *p
++ = htonl(rpcb
->r_vers
);
465 p
= xdr_encode_string(p
, rpcb
->r_netid
);
466 p
= xdr_encode_string(p
, rpcb
->r_addr
);
467 p
= xdr_encode_string(p
, rpcb
->r_owner
);
469 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
474 static int rpcb_decode_getaddr(struct rpc_rqst
*req
, __be32
*p
,
475 unsigned short *portp
)
479 int c
, i
, f
, first
, val
;
482 addr_len
= ntohl(*p
++);
485 * Simple sanity check. The smallest possible universal
486 * address is an IPv4 address string containing 11 bytes.
488 if (addr_len
< 11 || addr_len
> RPCBIND_MAXUADDRLEN
)
492 * Start at the end and walk backwards until the first dot
493 * is encountered. When the second dot is found, we have
494 * both parts of the port number.
500 for (i
= addr_len
- 1; i
> 0; i
--) {
502 if (c
>= '0' && c
<= '9') {
503 val
+= (c
- '0') * f
;
505 } else if (c
== '.') {
511 *portp
|= (val
<< 8);
518 * Simple sanity check. If we never saw a dot in the reply,
519 * then this was probably just garbage.
524 dprintk("RPC: rpcb_decode_getaddr port=%u\n", *portp
);
528 dprintk("RPC: rpcbind server returned malformed reply\n");
532 #define RPCB_program_sz (1u)
533 #define RPCB_version_sz (1u)
534 #define RPCB_protocol_sz (1u)
535 #define RPCB_port_sz (1u)
536 #define RPCB_boolean_sz (1u)
538 #define RPCB_netid_sz (1+XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
539 #define RPCB_addr_sz (1+XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
540 #define RPCB_ownerstring_sz (1+XDR_QUADLEN(RPCB_MAXOWNERLEN))
542 #define RPCB_mappingargs_sz RPCB_program_sz+RPCB_version_sz+ \
543 RPCB_protocol_sz+RPCB_port_sz
544 #define RPCB_getaddrargs_sz RPCB_program_sz+RPCB_version_sz+ \
545 RPCB_netid_sz+RPCB_addr_sz+ \
548 #define RPCB_setres_sz RPCB_boolean_sz
549 #define RPCB_getportres_sz RPCB_port_sz
552 * Note that RFC 1833 does not put any size restrictions on the
553 * address string returned by the remote rpcbind database.
555 #define RPCB_getaddrres_sz RPCB_addr_sz
557 #define PROC(proc, argtype, restype) \
558 [RPCBPROC_##proc] = { \
559 .p_proc = RPCBPROC_##proc, \
560 .p_encode = (kxdrproc_t) rpcb_encode_##argtype, \
561 .p_decode = (kxdrproc_t) rpcb_decode_##restype, \
562 .p_arglen = RPCB_##argtype##args_sz, \
563 .p_replen = RPCB_##restype##res_sz, \
564 .p_statidx = RPCBPROC_##proc, \
570 * Not all rpcbind procedures described in RFC 1833 are implemented
571 * since the Linux kernel RPC code requires only these.
573 static struct rpc_procinfo rpcb_procedures2
[] = {
574 PROC(SET
, mapping
, set
),
575 PROC(UNSET
, mapping
, set
),
576 PROC(GETADDR
, mapping
, getport
),
579 static struct rpc_procinfo rpcb_procedures3
[] = {
580 PROC(SET
, mapping
, set
),
581 PROC(UNSET
, mapping
, set
),
582 PROC(GETADDR
, getaddr
, getaddr
),
585 static struct rpc_procinfo rpcb_procedures4
[] = {
586 PROC(SET
, mapping
, set
),
587 PROC(UNSET
, mapping
, set
),
588 PROC(GETVERSADDR
, getaddr
, getaddr
),
591 static struct rpcb_info rpcb_next_version
[] = {
592 #ifdef CONFIG_SUNRPC_BIND34
593 { 4, &rpcb_procedures4
[RPCBPROC_GETVERSADDR
] },
594 { 3, &rpcb_procedures3
[RPCBPROC_GETADDR
] },
596 { 2, &rpcb_procedures2
[RPCBPROC_GETPORT
] },
600 static struct rpcb_info rpcb_next_version6
[] = {
601 #ifdef CONFIG_SUNRPC_BIND34
602 { 4, &rpcb_procedures4
[RPCBPROC_GETVERSADDR
] },
603 { 3, &rpcb_procedures3
[RPCBPROC_GETADDR
] },
608 static struct rpc_version rpcb_version2
= {
610 .nrprocs
= RPCB_HIGHPROC_2
,
611 .procs
= rpcb_procedures2
614 static struct rpc_version rpcb_version3
= {
616 .nrprocs
= RPCB_HIGHPROC_3
,
617 .procs
= rpcb_procedures3
620 static struct rpc_version rpcb_version4
= {
622 .nrprocs
= RPCB_HIGHPROC_4
,
623 .procs
= rpcb_procedures4
626 static struct rpc_version
*rpcb_version
[] = {
634 static struct rpc_stat rpcb_stats
;
636 static struct rpc_program rpcb_program
= {
638 .number
= RPCBIND_PROGRAM
,
639 .nrvers
= ARRAY_SIZE(rpcb_version
),
640 .version
= rpcb_version
,
641 .stats
= &rpcb_stats
,