1/*
2 * In-kernel rpcbind client supporting versions 2, 3, and 4 of the rpcbind
3 * protocol
4 *
5 * Based on RFC 1833: "Binding Protocols for ONC RPC Version 2" and
6 * RFC 3530: "Network File System (NFS) version 4 Protocol"
7 *
8 * Original: Gilles Quillard, Bull Open Source, 2005 <gilles.quillard@bull.net>
9 * Updated: Chuck Lever, Oracle Corporation, 2007 <chuck.lever@oracle.com>
10 *
11 * Descended from net/sunrpc/pmap_clnt.c,
12 *  Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
13 */
14
15#include <linux/module.h>
16
17#include <linux/types.h>
18#include <linux/socket.h>
19#include <linux/in.h>
20#include <linux/in6.h>
21#include <linux/kernel.h>
22#include <linux/errno.h>
23#include <linux/mutex.h>
24#include <linux/slab.h>
25#include <net/ipv6.h>
26
27#include <linux/sunrpc/clnt.h>
28#include <linux/sunrpc/sched.h>
29#include <linux/sunrpc/xprtsock.h>
30
31#ifdef RPC_DEBUG
32# define RPCDBG_FACILITY	RPCDBG_BIND
33#endif
34
35#define RPCBIND_PROGRAM		(100000u)
36#define RPCBIND_PORT		(111u)
37
38#define RPCBVERS_2		(2u)
39#define RPCBVERS_3		(3u)
40#define RPCBVERS_4		(4u)
41
42enum {
43	RPCBPROC_NULL,
44	RPCBPROC_SET,
45	RPCBPROC_UNSET,
46	RPCBPROC_GETPORT,
47	RPCBPROC_GETADDR = 3,		/* alias for GETPORT */
48	RPCBPROC_DUMP,
49	RPCBPROC_CALLIT,
50	RPCBPROC_BCAST = 5,		/* alias for CALLIT */
51	RPCBPROC_GETTIME,
52	RPCBPROC_UADDR2TADDR,
53	RPCBPROC_TADDR2UADDR,
54	RPCBPROC_GETVERSADDR,
55	RPCBPROC_INDIRECT,
56	RPCBPROC_GETADDRLIST,
57	RPCBPROC_GETSTAT,
58};
59
60#define RPCB_HIGHPROC_2		RPCBPROC_CALLIT
61#define RPCB_HIGHPROC_3		RPCBPROC_TADDR2UADDR
62#define RPCB_HIGHPROC_4		RPCBPROC_GETSTAT
63
64/*
65 * r_owner
66 *
67 * The "owner" is allowed to unset a service in the rpcbind database.
68 *
69 * For AF_LOCAL SET/UNSET requests, rpcbind treats this string as a
70 * UID which it maps to a local user name via a password lookup.
71 * In all other cases it is ignored.
72 *
73 * For SET/UNSET requests, user space provides a value, even for
74 * network requests, and GETADDR uses an empty string.  We follow
75 * those precedents here.
76 */
77#define RPCB_OWNER_STRING	"0"
78#define RPCB_MAXOWNERLEN	sizeof(RPCB_OWNER_STRING)
79
80/*
81 * XDR data type sizes
82 */
83#define RPCB_program_sz		(1)
84#define RPCB_version_sz		(1)
85#define RPCB_protocol_sz	(1)
86#define RPCB_port_sz		(1)
87#define RPCB_boolean_sz		(1)
88
89#define RPCB_netid_sz		(1 + XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
90#define RPCB_addr_sz		(1 + XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
91#define RPCB_ownerstring_sz	(1 + XDR_QUADLEN(RPCB_MAXOWNERLEN))
92
93/*
94 * XDR argument and result sizes
95 */
96#define RPCB_mappingargs_sz	(RPCB_program_sz + RPCB_version_sz + \
97				RPCB_protocol_sz + RPCB_port_sz)
98#define RPCB_getaddrargs_sz	(RPCB_program_sz + RPCB_version_sz + \
99				RPCB_netid_sz + RPCB_addr_sz + \
100				RPCB_ownerstring_sz)
101
102#define RPCB_getportres_sz	RPCB_port_sz
103#define RPCB_setres_sz		RPCB_boolean_sz
104
105/*
106 * Note that RFC 1833 does not put any size restrictions on the
107 * address string returned by the remote rpcbind database.
108 */
109#define RPCB_getaddrres_sz	RPCB_addr_sz
110
111static void			rpcb_getport_done(struct rpc_task *, void *);
112static void			rpcb_map_release(void *data);
113static struct rpc_program	rpcb_program;
114
115static struct rpc_clnt *	rpcb_local_clnt;
116static struct rpc_clnt *	rpcb_local_clnt4;
117
118struct rpcbind_args {
119	struct rpc_xprt *	r_xprt;
120
121	u32			r_prog;
122	u32			r_vers;
123	u32			r_prot;
124	unsigned short		r_port;
125	const char *		r_netid;
126	const char *		r_addr;
127	const char *		r_owner;
128
129	int			r_status;
130};
131
132static struct rpc_procinfo rpcb_procedures2[];
133static struct rpc_procinfo rpcb_procedures3[];
134static struct rpc_procinfo rpcb_procedures4[];
135
136struct rpcb_info {
137	u32			rpc_vers;
138	struct rpc_procinfo *	rpc_proc;
139};
140
141static struct rpcb_info rpcb_next_version[];
142static struct rpcb_info rpcb_next_version6[];
143
144static const struct rpc_call_ops rpcb_getport_ops = {
145	.rpc_call_done		= rpcb_getport_done,
146	.rpc_release		= rpcb_map_release,
147};
148
149static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status)
150{
151	xprt_clear_binding(xprt);
152	rpc_wake_up_status(&xprt->binding, status);
153}
154
155static void rpcb_map_release(void *data)
156{
157	struct rpcbind_args *map = data;
158
159	rpcb_wake_rpcbind_waiters(map->r_xprt, map->r_status);
160	xprt_put(map->r_xprt);
161	kfree(map->r_addr);
162	kfree(map);
163}
164
165static const struct sockaddr_in rpcb_inaddr_loopback = {
166	.sin_family		= AF_INET,
167	.sin_addr.s_addr	= htonl(INADDR_LOOPBACK),
168	.sin_port		= htons(RPCBIND_PORT),
169};
170
171static DEFINE_MUTEX(rpcb_create_local_mutex);
172
173/*
174 * Returns zero on success, otherwise a negative errno value
175 * is returned.
176 */
177static int rpcb_create_local(void)
178{
179	struct rpc_create_args args = {
180		.protocol	= XPRT_TRANSPORT_TCP,
181		.address	= (struct sockaddr *)&rpcb_inaddr_loopback,
182		.addrsize	= sizeof(rpcb_inaddr_loopback),
183		.servername	= "localhost",
184		.program	= &rpcb_program,
185		.version	= RPCBVERS_2,
186		.authflavor	= RPC_AUTH_UNIX,
187		.flags		= RPC_CLNT_CREATE_NOPING,
188	};
189	struct rpc_clnt *clnt, *clnt4;
190	int result = 0;
191
192	if (rpcb_local_clnt)
193		return result;
194
195	mutex_lock(&rpcb_create_local_mutex);
196	if (rpcb_local_clnt)
197		goto out;
198
199	clnt = rpc_create(&args);
200	if (IS_ERR(clnt)) {
201		dprintk("RPC:       failed to create local rpcbind "
202				"client (errno %ld).\n", PTR_ERR(clnt));
203		result = -PTR_ERR(clnt);
204		goto out;
205	}
206
207	/*
208	 * This results in an RPC ping.  On systems running portmapper,
209	 * the v4 ping will fail.  Proceed anyway, but disallow rpcb
210	 * v4 upcalls.
211	 */
212	clnt4 = rpc_bind_new_program(clnt, &rpcb_program, RPCBVERS_4);
213	if (IS_ERR(clnt4)) {
214		dprintk("RPC:       failed to create local rpcbind v4 "
215				"cleint (errno %ld).\n", PTR_ERR(clnt4));
216		clnt4 = NULL;
217	}
218
219	rpcb_local_clnt = clnt;
220	rpcb_local_clnt4 = clnt4;
221
222out:
223	mutex_unlock(&rpcb_create_local_mutex);
224	return result;
225}
226
227static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
228				    size_t salen, int proto, u32 version)
229{
230	struct rpc_create_args args = {
231		.protocol	= proto,
232		.address	= srvaddr,
233		.addrsize	= salen,
234		.servername	= hostname,
235		.program	= &rpcb_program,
236		.version	= version,
237		.authflavor	= RPC_AUTH_UNIX,
238		.flags		= (RPC_CLNT_CREATE_NOPING |
239					RPC_CLNT_CREATE_NONPRIVPORT),
240	};
241
242	switch (srvaddr->sa_family) {
243	case AF_INET:
244		((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
245		break;
246	case AF_INET6:
247		((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
248		break;
249	default:
250		return NULL;
251	}
252
253	return rpc_create(&args);
254}
255
256static int rpcb_register_call(struct rpc_clnt *clnt, struct rpc_message *msg)
257{
258	int result, error = 0;
259
260	msg->rpc_resp = &result;
261
262	error = rpc_call_sync(clnt, msg, RPC_TASK_SOFTCONN);
263	if (error < 0) {
264		dprintk("RPC:       failed to contact local rpcbind "
265				"server (errno %d).\n", -error);
266		return error;
267	}
268
269	if (!result)
270		return -EACCES;
271	return 0;
272}
273
274/**
275 * rpcb_register - set or unset a port registration with the local rpcbind svc
276 * @prog: RPC program number to bind
277 * @vers: RPC version number to bind
278 * @prot: transport protocol to register
279 * @port: port value to register
280 *
281 * Returns zero if the registration request was dispatched successfully
282 * and the rpcbind daemon returned success.  Otherwise, returns an errno
283 * value that reflects the nature of the error (request could not be
284 * dispatched, timed out, or rpcbind returned an error).
285 *
286 * RPC services invoke this function to advertise their contact
287 * information via the system's rpcbind daemon.  RPC services
288 * invoke this function once for each [program, version, transport]
289 * tuple they wish to advertise.
290 *
291 * Callers may also unregister RPC services that are no longer
292 * available by setting the passed-in port to zero.  This removes
293 * all registered transports for [program, version] from the local
294 * rpcbind database.
295 *
296 * This function uses rpcbind protocol version 2 to contact the
297 * local rpcbind daemon.
298 *
299 * Registration works over both AF_INET and AF_INET6, and services
300 * registered via this function are advertised as available for any
301 * address.  If the local rpcbind daemon is listening on AF_INET6,
302 * services registered via this function will be advertised on
303 * IN6ADDR_ANY (ie available for all AF_INET and AF_INET6
304 * addresses).
305 */
306int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port)
307{
308	struct rpcbind_args map = {
309		.r_prog		= prog,
310		.r_vers		= vers,
311		.r_prot		= prot,
312		.r_port		= port,
313	};
314	struct rpc_message msg = {
315		.rpc_argp	= &map,
316	};
317	int error;
318
319	error = rpcb_create_local();
320	if (error)
321		return error;
322
323	dprintk("RPC:       %sregistering (%u, %u, %d, %u) with local "
324			"rpcbind\n", (port ? "" : "un"),
325			prog, vers, prot, port);
326
327	msg.rpc_proc = &rpcb_procedures2[RPCBPROC_UNSET];
328	if (port)
329		msg.rpc_proc = &rpcb_procedures2[RPCBPROC_SET];
330
331	return rpcb_register_call(rpcb_local_clnt, &msg);
332}
333
334/*
335 * Fill in AF_INET family-specific arguments to register
336 */
337static int rpcb_register_inet4(const struct sockaddr *sap,
338			       struct rpc_message *msg)
339{
340	const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
341	struct rpcbind_args *map = msg->rpc_argp;
342	unsigned short port = ntohs(sin->sin_port);
343	int result;
344
345	map->r_addr = rpc_sockaddr2uaddr(sap);
346
347	dprintk("RPC:       %sregistering [%u, %u, %s, '%s'] with "
348		"local rpcbind\n", (port ? "" : "un"),
349			map->r_prog, map->r_vers,
350			map->r_addr, map->r_netid);
351
352	msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
353	if (port)
354		msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
355
356	result = rpcb_register_call(rpcb_local_clnt4, msg);
357	kfree(map->r_addr);
358	return result;
359}
360
361/*
362 * Fill in AF_INET6 family-specific arguments to register
363 */
364static int rpcb_register_inet6(const struct sockaddr *sap,
365			       struct rpc_message *msg)
366{
367	const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sap;
368	struct rpcbind_args *map = msg->rpc_argp;
369	unsigned short port = ntohs(sin6->sin6_port);
370	int result;
371
372	map->r_addr = rpc_sockaddr2uaddr(sap);
373
374	dprintk("RPC:       %sregistering [%u, %u, %s, '%s'] with "
375		"local rpcbind\n", (port ? "" : "un"),
376			map->r_prog, map->r_vers,
377			map->r_addr, map->r_netid);
378
379	msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
380	if (port)
381		msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
382
383	result = rpcb_register_call(rpcb_local_clnt4, msg);
384	kfree(map->r_addr);
385	return result;
386}
387
388static int rpcb_unregister_all_protofamilies(struct rpc_message *msg)
389{
390	struct rpcbind_args *map = msg->rpc_argp;
391
392	dprintk("RPC:       unregistering [%u, %u, '%s'] with "
393		"local rpcbind\n",
394			map->r_prog, map->r_vers, map->r_netid);
395
396	map->r_addr = "";
397	msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
398
399	return rpcb_register_call(rpcb_local_clnt4, msg);
400}
401
402/**
403 * rpcb_v4_register - set or unset a port registration with the local rpcbind
404 * @program: RPC program number of service to (un)register
405 * @version: RPC version number of service to (un)register
406 * @address: address family, IP address, and port to (un)register
407 * @netid: netid of transport protocol to (un)register
408 *
409 * Returns zero if the registration request was dispatched successfully
410 * and the rpcbind daemon returned success.  Otherwise, returns an errno
411 * value that reflects the nature of the error (request could not be
412 * dispatched, timed out, or rpcbind returned an error).
413 *
414 * RPC services invoke this function to advertise their contact
415 * information via the system's rpcbind daemon.  RPC services
416 * invoke this function once for each [program, version, address,
417 * netid] tuple they wish to advertise.
418 *
419 * Callers may also unregister RPC services that are registered at a
420 * specific address by setting the port number in @address to zero.
421 * They may unregister all registered protocol families at once for
422 * a service by passing a NULL @address argument.  If @netid is ""
423 * then all netids for [program, version, address] are unregistered.
424 *
425 * This function uses rpcbind protocol version 4 to contact the
426 * local rpcbind daemon.  The local rpcbind daemon must support
427 * version 4 of the rpcbind protocol in order for these functions
428 * to register a service successfully.
429 *
430 * Supported netids include "udp" and "tcp" for UDP and TCP over
431 * IPv4, and "udp6" and "tcp6" for UDP and TCP over IPv6,
432 * respectively.
433 *
434 * The contents of @address determine the address family and the
435 * port to be registered.  The usual practice is to pass INADDR_ANY
436 * as the raw address, but specifying a non-zero address is also
437 * supported by this API if the caller wishes to advertise an RPC
438 * service on a specific network interface.
439 *
440 * Note that passing in INADDR_ANY does not create the same service
441 * registration as IN6ADDR_ANY.  The former advertises an RPC
442 * service on any IPv4 address, but not on IPv6.  The latter
443 * advertises the service on all IPv4 and IPv6 addresses.
444 */
445int rpcb_v4_register(const u32 program, const u32 version,
446		     const struct sockaddr *address, const char *netid)
447{
448	struct rpcbind_args map = {
449		.r_prog		= program,
450		.r_vers		= version,
451		.r_netid	= netid,
452		.r_owner	= RPCB_OWNER_STRING,
453	};
454	struct rpc_message msg = {
455		.rpc_argp	= &map,
456	};
457	int error;
458
459	error = rpcb_create_local();
460	if (error)
461		return error;
462	if (rpcb_local_clnt4 == NULL)
463		return -EPROTONOSUPPORT;
464
465	if (address == NULL)
466		return rpcb_unregister_all_protofamilies(&msg);
467
468	switch (address->sa_family) {
469	case AF_INET:
470		return rpcb_register_inet4(address, &msg);
471	case AF_INET6:
472		return rpcb_register_inet6(address, &msg);
473	}
474
475	return -EAFNOSUPPORT;
476}
477
478int rpcb_getport_sync(struct sockaddr_in *sin, u32 prog, u32 vers, int prot)
479{
480	struct rpcbind_args map = {
481		.r_prog		= prog,
482		.r_vers		= vers,
483		.r_prot		= prot,
484		.r_port		= 0,
485	};
486	struct rpc_message msg = {
487		.rpc_proc	= &rpcb_procedures2[RPCBPROC_GETPORT],
488		.rpc_argp	= &map,
489		.rpc_resp	= &map,
490	};
491	struct rpc_clnt	*rpcb_clnt;
492	int status;
493
494	dprintk("RPC:       %s(%pI4, %u, %u, %d)\n",
495		__func__, &sin->sin_addr.s_addr, prog, vers, prot);
496
497	rpcb_clnt = rpcb_create(NULL, (struct sockaddr *)sin,
498				sizeof(*sin), prot, RPCBVERS_2);
499	if (IS_ERR(rpcb_clnt))
500		return PTR_ERR(rpcb_clnt);
501
502	status = rpc_call_sync(rpcb_clnt, &msg, 0);
503	rpc_shutdown_client(rpcb_clnt);
504
505	if (status >= 0) {
506		if (map.r_port != 0)
507			return map.r_port;
508		status = -EACCES;
509	}
510	return status;
511}
512EXPORT_SYMBOL_GPL(rpcb_getport_sync);
513
514static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt, struct rpcbind_args *map, struct rpc_procinfo *proc)
515{
516	struct rpc_message msg = {
517		.rpc_proc = proc,
518		.rpc_argp = map,
519		.rpc_resp = map,
520	};
521	struct rpc_task_setup task_setup_data = {
522		.rpc_client = rpcb_clnt,
523		.rpc_message = &msg,
524		.callback_ops = &rpcb_getport_ops,
525		.callback_data = map,
526		.flags = RPC_TASK_ASYNC | RPC_TASK_SOFTCONN,
527	};
528
529	return rpc_run_task(&task_setup_data);
530}
531
532/*
533 * In the case where rpc clients have been cloned, we want to make
534 * sure that we use the program number/version etc of the actual
535 * owner of the xprt. To do so, we walk back up the tree of parents
536 * to find whoever created the transport and/or whoever has the
537 * autobind flag set.
538 */
539static struct rpc_clnt *rpcb_find_transport_owner(struct rpc_clnt *clnt)
540{
541	struct rpc_clnt *parent = clnt->cl_parent;
542
543	while (parent != clnt) {
544		if (parent->cl_xprt != clnt->cl_xprt)
545			break;
546		if (clnt->cl_autobind)
547			break;
548		clnt = parent;
549		parent = parent->cl_parent;
550	}
551	return clnt;
552}
553
554/**
555 * rpcb_getport_async - obtain the port for a given RPC service on a given host
556 * @task: task that is waiting for portmapper request
557 *
558 * This one can be called for an ongoing RPC request, and can be used in
559 * an async (rpciod) context.
560 */
561void rpcb_getport_async(struct rpc_task *task)
562{
563	struct rpc_clnt *clnt;
564	struct rpc_procinfo *proc;
565	u32 bind_version;
566	struct rpc_xprt *xprt;
567	struct rpc_clnt	*rpcb_clnt;
568	static struct rpcbind_args *map;
569	struct rpc_task	*child;
570	struct sockaddr_storage addr;
571	struct sockaddr *sap = (struct sockaddr *)&addr;
572	size_t salen;
573	int status;
574
575	clnt = rpcb_find_transport_owner(task->tk_client);
576	xprt = clnt->cl_xprt;
577
578	dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
579		task->tk_pid, __func__,
580		clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot);
581
582	/* Put self on the wait queue to ensure we get notified if
583	 * some other task is already attempting to bind the port */
584	rpc_sleep_on(&xprt->binding, task, NULL);
585
586	if (xprt_test_and_set_binding(xprt)) {
587		dprintk("RPC: %5u %s: waiting for another binder\n",
588			task->tk_pid, __func__);
589		return;
590	}
591
592	/* Someone else may have bound if we slept */
593	if (xprt_bound(xprt)) {
594		status = 0;
595		dprintk("RPC: %5u %s: already bound\n",
596			task->tk_pid, __func__);
597		goto bailout_nofree;
598	}
599
600	/* Parent transport's destination address */
601	salen = rpc_peeraddr(clnt, sap, sizeof(addr));
602
603	/* Don't ever use rpcbind v2 for AF_INET6 requests */
604	switch (sap->sa_family) {
605	case AF_INET:
606		proc = rpcb_next_version[xprt->bind_index].rpc_proc;
607		bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
608		break;
609	case AF_INET6:
610		proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
611		bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
612		break;
613	default:
614		status = -EAFNOSUPPORT;
615		dprintk("RPC: %5u %s: bad address family\n",
616				task->tk_pid, __func__);
617		goto bailout_nofree;
618	}
619	if (proc == NULL) {
620		xprt->bind_index = 0;
621		status = -EPFNOSUPPORT;
622		dprintk("RPC: %5u %s: no more getport versions available\n",
623			task->tk_pid, __func__);
624		goto bailout_nofree;
625	}
626
627	dprintk("RPC: %5u %s: trying rpcbind version %u\n",
628		task->tk_pid, __func__, bind_version);
629
630	rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot,
631				bind_version);
632	if (IS_ERR(rpcb_clnt)) {
633		status = PTR_ERR(rpcb_clnt);
634		dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
635			task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
636		goto bailout_nofree;
637	}
638
639	map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
640	if (!map) {
641		status = -ENOMEM;
642		dprintk("RPC: %5u %s: no memory available\n",
643			task->tk_pid, __func__);
644		goto bailout_release_client;
645	}
646	map->r_prog = clnt->cl_prog;
647	map->r_vers = clnt->cl_vers;
648	map->r_prot = xprt->prot;
649	map->r_port = 0;
650	map->r_xprt = xprt_get(xprt);
651	map->r_status = -EIO;
652
653	switch (bind_version) {
654	case RPCBVERS_4:
655	case RPCBVERS_3:
656		map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
657		map->r_addr = rpc_sockaddr2uaddr(sap);
658		map->r_owner = "";
659		break;
660	case RPCBVERS_2:
661		map->r_addr = NULL;
662		break;
663	default:
664		BUG();
665	}
666
667	child = rpcb_call_async(rpcb_clnt, map, proc);
668	rpc_release_client(rpcb_clnt);
669	if (IS_ERR(child)) {
670		/* rpcb_map_release() has freed the arguments */
671		dprintk("RPC: %5u %s: rpc_run_task failed\n",
672			task->tk_pid, __func__);
673		return;
674	}
675
676	xprt->stat.bind_count++;
677	rpc_put_task(child);
678	return;
679
680bailout_release_client:
681	rpc_release_client(rpcb_clnt);
682bailout_nofree:
683	rpcb_wake_rpcbind_waiters(xprt, status);
684	task->tk_status = status;
685}
686EXPORT_SYMBOL_GPL(rpcb_getport_async);
687
688/*
689 * Rpcbind child task calls this callback via tk_exit.
690 */
691static void rpcb_getport_done(struct rpc_task *child, void *data)
692{
693	struct rpcbind_args *map = data;
694	struct rpc_xprt *xprt = map->r_xprt;
695	int status = child->tk_status;
696
697	/* Garbage reply: retry with a lesser rpcbind version */
698	if (status == -EIO)
699		status = -EPROTONOSUPPORT;
700
701	/* rpcbind server doesn't support this rpcbind protocol version */
702	if (status == -EPROTONOSUPPORT)
703		xprt->bind_index++;
704
705	if (status < 0) {
706		/* rpcbind server not available on remote host? */
707		xprt->ops->set_port(xprt, 0);
708	} else if (map->r_port == 0) {
709		/* Requested RPC service wasn't registered on remote host */
710		xprt->ops->set_port(xprt, 0);
711		status = -EACCES;
712	} else {
713		/* Succeeded */
714		xprt->ops->set_port(xprt, map->r_port);
715		xprt_set_bound(xprt);
716		status = 0;
717	}
718
719	dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
720			child->tk_pid, status, map->r_port);
721
722	map->r_status = status;
723}
724
725/*
726 * XDR functions for rpcbind
727 */
728
729static int rpcb_enc_mapping(struct rpc_rqst *req, __be32 *p,
730			    const struct rpcbind_args *rpcb)
731{
732	struct rpc_task *task = req->rq_task;
733	struct xdr_stream xdr;
734
735	dprintk("RPC: %5u encoding PMAP_%s call (%u, %u, %d, %u)\n",
736			task->tk_pid, task->tk_msg.rpc_proc->p_name,
737			rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
738
739	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
740
741	p = xdr_reserve_space(&xdr, sizeof(__be32) * RPCB_mappingargs_sz);
742	if (unlikely(p == NULL))
743		return -EIO;
744
745	*p++ = htonl(rpcb->r_prog);
746	*p++ = htonl(rpcb->r_vers);
747	*p++ = htonl(rpcb->r_prot);
748	*p   = htonl(rpcb->r_port);
749
750	return 0;
751}
752
753static int rpcb_dec_getport(struct rpc_rqst *req, __be32 *p,
754			    struct rpcbind_args *rpcb)
755{
756	struct rpc_task *task = req->rq_task;
757	struct xdr_stream xdr;
758	unsigned long port;
759
760	xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
761
762	rpcb->r_port = 0;
763
764	p = xdr_inline_decode(&xdr, sizeof(__be32));
765	if (unlikely(p == NULL))
766		return -EIO;
767
768	port = ntohl(*p);
769	dprintk("RPC: %5u PMAP_%s result: %lu\n", task->tk_pid,
770			task->tk_msg.rpc_proc->p_name, port);
771	if (unlikely(port > USHRT_MAX))
772		return -EIO;
773
774	rpcb->r_port = port;
775	return 0;
776}
777
778static int rpcb_dec_set(struct rpc_rqst *req, __be32 *p,
779			unsigned int *boolp)
780{
781	struct rpc_task *task = req->rq_task;
782	struct xdr_stream xdr;
783
784	xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
785
786	p = xdr_inline_decode(&xdr, sizeof(__be32));
787	if (unlikely(p == NULL))
788		return -EIO;
789
790	*boolp = 0;
791	if (*p)
792		*boolp = 1;
793
794	dprintk("RPC: %5u RPCB_%s call %s\n",
795			task->tk_pid, task->tk_msg.rpc_proc->p_name,
796			(*boolp ? "succeeded" : "failed"));
797	return 0;
798}
799
800static int encode_rpcb_string(struct xdr_stream *xdr, const char *string,
801				const u32 maxstrlen)
802{
803	u32 len;
804	__be32 *p;
805
806	if (unlikely(string == NULL))
807		return -EIO;
808	len = strlen(string);
809	if (unlikely(len > maxstrlen))
810		return -EIO;
811
812	p = xdr_reserve_space(xdr, sizeof(__be32) + len);
813	if (unlikely(p == NULL))
814		return -EIO;
815	xdr_encode_opaque(p, string, len);
816
817	return 0;
818}
819
820static int rpcb_enc_getaddr(struct rpc_rqst *req, __be32 *p,
821			    const struct rpcbind_args *rpcb)
822{
823	struct rpc_task *task = req->rq_task;
824	struct xdr_stream xdr;
825
826	dprintk("RPC: %5u encoding RPCB_%s call (%u, %u, '%s', '%s')\n",
827			task->tk_pid, task->tk_msg.rpc_proc->p_name,
828			rpcb->r_prog, rpcb->r_vers,
829			rpcb->r_netid, rpcb->r_addr);
830
831	xdr_init_encode(&xdr, &req->rq_snd_buf, p);
832
833	p = xdr_reserve_space(&xdr,
834			sizeof(__be32) * (RPCB_program_sz + RPCB_version_sz));
835	if (unlikely(p == NULL))
836		return -EIO;
837	*p++ = htonl(rpcb->r_prog);
838	*p = htonl(rpcb->r_vers);
839
840	if (encode_rpcb_string(&xdr, rpcb->r_netid, RPCBIND_MAXNETIDLEN))
841		return -EIO;
842	if (encode_rpcb_string(&xdr, rpcb->r_addr, RPCBIND_MAXUADDRLEN))
843		return -EIO;
844	if (encode_rpcb_string(&xdr, rpcb->r_owner, RPCB_MAXOWNERLEN))
845		return -EIO;
846
847	return 0;
848}
849
850static int rpcb_dec_getaddr(struct rpc_rqst *req, __be32 *p,
851			    struct rpcbind_args *rpcb)
852{
853	struct sockaddr_storage address;
854	struct sockaddr *sap = (struct sockaddr *)&address;
855	struct rpc_task *task = req->rq_task;
856	struct xdr_stream xdr;
857	u32 len;
858
859	rpcb->r_port = 0;
860
861	xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
862
863	p = xdr_inline_decode(&xdr, sizeof(__be32));
864	if (unlikely(p == NULL))
865		goto out_fail;
866	len = ntohl(*p);
867
868	/*
869	 * If the returned universal address is a null string,
870	 * the requested RPC service was not registered.
871	 */
872	if (len == 0) {
873		dprintk("RPC: %5u RPCB reply: program not registered\n",
874				task->tk_pid);
875		return 0;
876	}
877
878	if (unlikely(len > RPCBIND_MAXUADDRLEN))
879		goto out_fail;
880
881	p = xdr_inline_decode(&xdr, len);
882	if (unlikely(p == NULL))
883		goto out_fail;
884	dprintk("RPC: %5u RPCB_%s reply: %s\n", task->tk_pid,
885			task->tk_msg.rpc_proc->p_name, (char *)p);
886
887	if (rpc_uaddr2sockaddr((char *)p, len, sap, sizeof(address)) == 0)
888		goto out_fail;
889	rpcb->r_port = rpc_get_port(sap);
890
891	return 0;
892
893out_fail:
894	dprintk("RPC: %5u malformed RPCB_%s reply\n",
895			task->tk_pid, task->tk_msg.rpc_proc->p_name);
896	return -EIO;
897}
898
899/*
900 * Not all rpcbind procedures described in RFC 1833 are implemented
901 * since the Linux kernel RPC code requires only these.
902 */
903
904static struct rpc_procinfo rpcb_procedures2[] = {
905	[RPCBPROC_SET] = {
906		.p_proc		= RPCBPROC_SET,
907		.p_encode	= (kxdrproc_t)rpcb_enc_mapping,
908		.p_decode	= (kxdrproc_t)rpcb_dec_set,
909		.p_arglen	= RPCB_mappingargs_sz,
910		.p_replen	= RPCB_setres_sz,
911		.p_statidx	= RPCBPROC_SET,
912		.p_timer	= 0,
913		.p_name		= "SET",
914	},
915	[RPCBPROC_UNSET] = {
916		.p_proc		= RPCBPROC_UNSET,
917		.p_encode	= (kxdrproc_t)rpcb_enc_mapping,
918		.p_decode	= (kxdrproc_t)rpcb_dec_set,
919		.p_arglen	= RPCB_mappingargs_sz,
920		.p_replen	= RPCB_setres_sz,
921		.p_statidx	= RPCBPROC_UNSET,
922		.p_timer	= 0,
923		.p_name		= "UNSET",
924	},
925	[RPCBPROC_GETPORT] = {
926		.p_proc		= RPCBPROC_GETPORT,
927		.p_encode	= (kxdrproc_t)rpcb_enc_mapping,
928		.p_decode	= (kxdrproc_t)rpcb_dec_getport,
929		.p_arglen	= RPCB_mappingargs_sz,
930		.p_replen	= RPCB_getportres_sz,
931		.p_statidx	= RPCBPROC_GETPORT,
932		.p_timer	= 0,
933		.p_name		= "GETPORT",
934	},
935};
936
937static struct rpc_procinfo rpcb_procedures3[] = {
938	[RPCBPROC_SET] = {
939		.p_proc		= RPCBPROC_SET,
940		.p_encode	= (kxdrproc_t)rpcb_enc_getaddr,
941		.p_decode	= (kxdrproc_t)rpcb_dec_set,
942		.p_arglen	= RPCB_getaddrargs_sz,
943		.p_replen	= RPCB_setres_sz,
944		.p_statidx	= RPCBPROC_SET,
945		.p_timer	= 0,
946		.p_name		= "SET",
947	},
948	[RPCBPROC_UNSET] = {
949		.p_proc		= RPCBPROC_UNSET,
950		.p_encode	= (kxdrproc_t)rpcb_enc_getaddr,
951		.p_decode	= (kxdrproc_t)rpcb_dec_set,
952		.p_arglen	= RPCB_getaddrargs_sz,
953		.p_replen	= RPCB_setres_sz,
954		.p_statidx	= RPCBPROC_UNSET,
955		.p_timer	= 0,
956		.p_name		= "UNSET",
957	},
958	[RPCBPROC_GETADDR] = {
959		.p_proc		= RPCBPROC_GETADDR,
960		.p_encode	= (kxdrproc_t)rpcb_enc_getaddr,
961		.p_decode	= (kxdrproc_t)rpcb_dec_getaddr,
962		.p_arglen	= RPCB_getaddrargs_sz,
963		.p_replen	= RPCB_getaddrres_sz,
964		.p_statidx	= RPCBPROC_GETADDR,
965		.p_timer	= 0,
966		.p_name		= "GETADDR",
967	},
968};
969
970static struct rpc_procinfo rpcb_procedures4[] = {
971	[RPCBPROC_SET] = {
972		.p_proc		= RPCBPROC_SET,
973		.p_encode	= (kxdrproc_t)rpcb_enc_getaddr,
974		.p_decode	= (kxdrproc_t)rpcb_dec_set,
975		.p_arglen	= RPCB_getaddrargs_sz,
976		.p_replen	= RPCB_setres_sz,
977		.p_statidx	= RPCBPROC_SET,
978		.p_timer	= 0,
979		.p_name		= "SET",
980	},
981	[RPCBPROC_UNSET] = {
982		.p_proc		= RPCBPROC_UNSET,
983		.p_encode	= (kxdrproc_t)rpcb_enc_getaddr,
984		.p_decode	= (kxdrproc_t)rpcb_dec_set,
985		.p_arglen	= RPCB_getaddrargs_sz,
986		.p_replen	= RPCB_setres_sz,
987		.p_statidx	= RPCBPROC_UNSET,
988		.p_timer	= 0,
989		.p_name		= "UNSET",
990	},
991	[RPCBPROC_GETADDR] = {
992		.p_proc		= RPCBPROC_GETADDR,
993		.p_encode	= (kxdrproc_t)rpcb_enc_getaddr,
994		.p_decode	= (kxdrproc_t)rpcb_dec_getaddr,
995		.p_arglen	= RPCB_getaddrargs_sz,
996		.p_replen	= RPCB_getaddrres_sz,
997		.p_statidx	= RPCBPROC_GETADDR,
998		.p_timer	= 0,
999		.p_name		= "GETADDR",
1000	},
1001};
1002
1003static struct rpcb_info rpcb_next_version[] = {
1004	{
1005		.rpc_vers	= RPCBVERS_2,
1006		.rpc_proc	= &rpcb_procedures2[RPCBPROC_GETPORT],
1007	},
1008	{
1009		.rpc_proc	= NULL,
1010	},
1011};
1012
1013static struct rpcb_info rpcb_next_version6[] = {
1014	{
1015		.rpc_vers	= RPCBVERS_4,
1016		.rpc_proc	= &rpcb_procedures4[RPCBPROC_GETADDR],
1017	},
1018	{
1019		.rpc_vers	= RPCBVERS_3,
1020		.rpc_proc	= &rpcb_procedures3[RPCBPROC_GETADDR],
1021	},
1022	{
1023		.rpc_proc	= NULL,
1024	},
1025};
1026
1027static struct rpc_version rpcb_version2 = {
1028	.number		= RPCBVERS_2,
1029	.nrprocs	= RPCB_HIGHPROC_2,
1030	.procs		= rpcb_procedures2
1031};
1032
1033static struct rpc_version rpcb_version3 = {
1034	.number		= RPCBVERS_3,
1035	.nrprocs	= RPCB_HIGHPROC_3,
1036	.procs		= rpcb_procedures3
1037};
1038
1039static struct rpc_version rpcb_version4 = {
1040	.number		= RPCBVERS_4,
1041	.nrprocs	= RPCB_HIGHPROC_4,
1042	.procs		= rpcb_procedures4
1043};
1044
1045static struct rpc_version *rpcb_version[] = {
1046	NULL,
1047	NULL,
1048	&rpcb_version2,
1049	&rpcb_version3,
1050	&rpcb_version4
1051};
1052
1053static struct rpc_stat rpcb_stats;
1054
1055static struct rpc_program rpcb_program = {
1056	.name		= "rpcbind",
1057	.number		= RPCBIND_PROGRAM,
1058	.nrvers		= ARRAY_SIZE(rpcb_version),
1059	.version	= rpcb_version,
1060	.stats		= &rpcb_stats,
1061};
1062
1063/**
1064 * cleanup_rpcb_clnt - remove xprtsock's sysctls, unregister
1065 *
1066 */
1067void cleanup_rpcb_clnt(void)
1068{
1069	if (rpcb_local_clnt4)
1070		rpc_shutdown_client(rpcb_local_clnt4);
1071	if (rpcb_local_clnt)
1072		rpc_shutdown_client(rpcb_local_clnt);
1073}
1074