blob: 7d32f19ba868b6a34e87714602d5655678ffb610 [file] [log] [blame]
Chuck Levera5090502007-03-29 16:48:04 -04001/*
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
Chuck Levercce63cd2007-07-01 12:13:12 -040015#include <linux/module.h>
16
Chuck Levera5090502007-03-29 16:48:04 -040017#include <linux/types.h>
18#include <linux/socket.h>
Chuck Lever7402ab192011-05-09 15:22:55 -040019#include <linux/un.h>
Chuck Leverd5b64432007-08-06 11:57:18 -040020#include <linux/in.h>
21#include <linux/in6.h>
Chuck Levera5090502007-03-29 16:48:04 -040022#include <linux/kernel.h>
23#include <linux/errno.h>
Chuck Leverc5266112009-12-03 15:58:56 -050024#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +040026#include <linux/nsproxy.h>
Chuck Lever9d548b92008-09-15 16:27:30 -050027#include <net/ipv6.h>
Chuck Levera5090502007-03-29 16:48:04 -040028
29#include <linux/sunrpc/clnt.h>
30#include <linux/sunrpc/sched.h>
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -040031#include <linux/sunrpc/xprtsock.h>
Chuck Levera5090502007-03-29 16:48:04 -040032
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +040033#include "netns.h"
34
Chuck Levera5090502007-03-29 16:48:04 -040035#ifdef RPC_DEBUG
36# define RPCDBG_FACILITY RPCDBG_BIND
37#endif
38
Chuck Lever7402ab192011-05-09 15:22:55 -040039#define RPCBIND_SOCK_PATHNAME "/var/run/rpcbind.sock"
40
Chuck Levera5090502007-03-29 16:48:04 -040041#define RPCBIND_PROGRAM (100000u)
42#define RPCBIND_PORT (111u)
43
Chuck Leverfc200e72008-06-25 17:24:31 -040044#define RPCBVERS_2 (2u)
45#define RPCBVERS_3 (3u)
46#define RPCBVERS_4 (4u)
47
Chuck Levera5090502007-03-29 16:48:04 -040048enum {
49 RPCBPROC_NULL,
50 RPCBPROC_SET,
51 RPCBPROC_UNSET,
52 RPCBPROC_GETPORT,
53 RPCBPROC_GETADDR = 3, /* alias for GETPORT */
54 RPCBPROC_DUMP,
55 RPCBPROC_CALLIT,
56 RPCBPROC_BCAST = 5, /* alias for CALLIT */
57 RPCBPROC_GETTIME,
58 RPCBPROC_UADDR2TADDR,
59 RPCBPROC_TADDR2UADDR,
60 RPCBPROC_GETVERSADDR,
61 RPCBPROC_INDIRECT,
62 RPCBPROC_GETADDRLIST,
63 RPCBPROC_GETSTAT,
64};
65
Chuck Levera5090502007-03-29 16:48:04 -040066/*
Chuck Levera5090502007-03-29 16:48:04 -040067 * r_owner
68 *
69 * The "owner" is allowed to unset a service in the rpcbind database.
Chuck Lever126e4bc2009-03-18 20:47:14 -040070 *
71 * For AF_LOCAL SET/UNSET requests, rpcbind treats this string as a
72 * UID which it maps to a local user name via a password lookup.
73 * In all other cases it is ignored.
74 *
75 * For SET/UNSET requests, user space provides a value, even for
76 * network requests, and GETADDR uses an empty string. We follow
77 * those precedents here.
Chuck Levera5090502007-03-29 16:48:04 -040078 */
Chuck Lever126e4bc2009-03-18 20:47:14 -040079#define RPCB_OWNER_STRING "0"
Chuck Levera5090502007-03-29 16:48:04 -040080#define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
81
Chuck Lever0b10bf52009-08-09 15:09:33 -040082/*
83 * XDR data type sizes
84 */
85#define RPCB_program_sz (1)
86#define RPCB_version_sz (1)
87#define RPCB_protocol_sz (1)
88#define RPCB_port_sz (1)
89#define RPCB_boolean_sz (1)
90
91#define RPCB_netid_sz (1 + XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
92#define RPCB_addr_sz (1 + XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
93#define RPCB_ownerstring_sz (1 + XDR_QUADLEN(RPCB_MAXOWNERLEN))
94
95/*
96 * XDR argument and result sizes
97 */
98#define RPCB_mappingargs_sz (RPCB_program_sz + RPCB_version_sz + \
99 RPCB_protocol_sz + RPCB_port_sz)
100#define RPCB_getaddrargs_sz (RPCB_program_sz + RPCB_version_sz + \
101 RPCB_netid_sz + RPCB_addr_sz + \
102 RPCB_ownerstring_sz)
103
104#define RPCB_getportres_sz RPCB_port_sz
105#define RPCB_setres_sz RPCB_boolean_sz
106
107/*
108 * Note that RFC 1833 does not put any size restrictions on the
109 * address string returned by the remote rpcbind database.
110 */
111#define RPCB_getaddrres_sz RPCB_addr_sz
112
Chuck Levera5090502007-03-29 16:48:04 -0400113static void rpcb_getport_done(struct rpc_task *, void *);
Trond Myklebust381ba742008-07-07 12:18:53 -0400114static void rpcb_map_release(void *data);
Adrian Bunk7f4adef2007-06-13 01:03:13 +0200115static struct rpc_program rpcb_program;
Chuck Levera5090502007-03-29 16:48:04 -0400116
117struct rpcbind_args {
118 struct rpc_xprt * r_xprt;
119
120 u32 r_prog;
121 u32 r_vers;
122 u32 r_prot;
123 unsigned short r_port;
Trond Myklebust86d61d82008-01-07 21:16:56 -0500124 const char * r_netid;
125 const char * r_addr;
126 const char * r_owner;
Trond Myklebust381ba742008-07-07 12:18:53 -0400127
128 int r_status;
Chuck Levera5090502007-03-29 16:48:04 -0400129};
130
131static struct rpc_procinfo rpcb_procedures2[];
132static struct rpc_procinfo rpcb_procedures3[];
Chuck Leverc2e1b092008-07-14 16:03:30 -0400133static struct rpc_procinfo rpcb_procedures4[];
Chuck Levera5090502007-03-29 16:48:04 -0400134
Chuck Leverd5b64432007-08-06 11:57:18 -0400135struct rpcb_info {
Chuck Leverfc200e72008-06-25 17:24:31 -0400136 u32 rpc_vers;
Chuck Levera5090502007-03-29 16:48:04 -0400137 struct rpc_procinfo * rpc_proc;
Chuck Leverd5b64432007-08-06 11:57:18 -0400138};
139
140static struct rpcb_info rpcb_next_version[];
141static struct rpcb_info rpcb_next_version6[];
Chuck Levera5090502007-03-29 16:48:04 -0400142
Chuck Levera5090502007-03-29 16:48:04 -0400143static const struct rpc_call_ops rpcb_getport_ops = {
Chuck Levera5090502007-03-29 16:48:04 -0400144 .rpc_call_done = rpcb_getport_done,
145 .rpc_release = rpcb_map_release,
146};
147
148static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status)
149{
150 xprt_clear_binding(xprt);
151 rpc_wake_up_status(&xprt->binding, status);
152}
153
Trond Myklebust381ba742008-07-07 12:18:53 -0400154static void rpcb_map_release(void *data)
155{
156 struct rpcbind_args *map = data;
157
158 rpcb_wake_rpcbind_waiters(map->r_xprt, map->r_status);
159 xprt_put(map->r_xprt);
Chuck Leverba809132009-08-09 15:09:35 -0400160 kfree(map->r_addr);
Trond Myklebust381ba742008-07-07 12:18:53 -0400161 kfree(map);
162}
163
Stanislav Kinsbursky914edb12011-10-25 14:16:36 +0300164static int rpcb_get_local(void)
165{
166 int cnt;
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400167 struct sunrpc_net *sn = net_generic(&init_net, sunrpc_net_id);
Stanislav Kinsbursky914edb12011-10-25 14:16:36 +0300168
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400169 spin_lock(&sn->rpcb_clnt_lock);
170 if (sn->rpcb_users)
171 sn->rpcb_users++;
172 cnt = sn->rpcb_users;
173 spin_unlock(&sn->rpcb_clnt_lock);
Stanislav Kinsbursky914edb12011-10-25 14:16:36 +0300174
175 return cnt;
176}
177
178void rpcb_put_local(void)
179{
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400180 struct sunrpc_net *sn = net_generic(&init_net, sunrpc_net_id);
181 struct rpc_clnt *clnt = sn->rpcb_local_clnt;
182 struct rpc_clnt *clnt4 = sn->rpcb_local_clnt4;
Stanislav Kinsbursky914edb12011-10-25 14:16:36 +0300183 int shutdown;
184
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400185 spin_lock(&sn->rpcb_clnt_lock);
186 if (--sn->rpcb_users == 0) {
187 sn->rpcb_local_clnt = NULL;
188 sn->rpcb_local_clnt4 = NULL;
Stanislav Kinsbursky914edb12011-10-25 14:16:36 +0300189 }
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400190 shutdown = !sn->rpcb_users;
191 spin_unlock(&sn->rpcb_clnt_lock);
Stanislav Kinsbursky914edb12011-10-25 14:16:36 +0300192
193 if (shutdown) {
194 /*
195 * cleanup_rpcb_clnt - remove xprtsock's sysctls, unregister
196 */
197 if (clnt4)
198 rpc_shutdown_client(clnt4);
199 if (clnt)
200 rpc_shutdown_client(clnt);
201 }
202}
203
204static void rpcb_set_local(struct rpc_clnt *clnt, struct rpc_clnt *clnt4)
205{
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400206 struct sunrpc_net *sn = net_generic(&init_net, sunrpc_net_id);
207
Stanislav Kinsbursky914edb12011-10-25 14:16:36 +0300208 /* Protected by rpcb_create_local_mutex */
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400209 sn->rpcb_local_clnt = clnt;
210 sn->rpcb_local_clnt4 = clnt4;
Stanislav Kinsbursky914edb12011-10-25 14:16:36 +0300211 smp_wmb();
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400212 sn->rpcb_users = 1;
Stanislav Kinsbursky914edb12011-10-25 14:16:36 +0300213 dprintk("RPC: created new rpcb local clients (rpcb_local_clnt: "
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400214 "%p, rpcb_local_clnt4: %p)\n", sn->rpcb_local_clnt,
215 sn->rpcb_local_clnt4);
Stanislav Kinsbursky914edb12011-10-25 14:16:36 +0300216}
217
Chuck Lever7402ab192011-05-09 15:22:55 -0400218/*
219 * Returns zero on success, otherwise a negative errno value
220 * is returned.
221 */
222static int rpcb_create_local_unix(void)
223{
224 static const struct sockaddr_un rpcb_localaddr_rpcbind = {
225 .sun_family = AF_LOCAL,
226 .sun_path = RPCBIND_SOCK_PATHNAME,
227 };
228 struct rpc_create_args args = {
229 .net = &init_net,
230 .protocol = XPRT_TRANSPORT_LOCAL,
231 .address = (struct sockaddr *)&rpcb_localaddr_rpcbind,
232 .addrsize = sizeof(rpcb_localaddr_rpcbind),
233 .servername = "localhost",
234 .program = &rpcb_program,
235 .version = RPCBVERS_2,
236 .authflavor = RPC_AUTH_NULL,
237 };
238 struct rpc_clnt *clnt, *clnt4;
239 int result = 0;
Chuck Levercc5598b2008-07-14 16:03:27 -0400240
Chuck Lever7402ab192011-05-09 15:22:55 -0400241 /*
242 * Because we requested an RPC PING at transport creation time,
243 * this works only if the user space portmapper is rpcbind, and
244 * it's listening on AF_LOCAL on the named socket.
245 */
246 clnt = rpc_create(&args);
247 if (IS_ERR(clnt)) {
248 dprintk("RPC: failed to create AF_LOCAL rpcbind "
249 "client (errno %ld).\n", PTR_ERR(clnt));
250 result = -PTR_ERR(clnt);
251 goto out;
252 }
253
254 clnt4 = rpc_bind_new_program(clnt, &rpcb_program, RPCBVERS_4);
255 if (IS_ERR(clnt4)) {
256 dprintk("RPC: failed to bind second program to "
257 "rpcbind v4 client (errno %ld).\n",
258 PTR_ERR(clnt4));
259 clnt4 = NULL;
260 }
261
Stanislav Kinsbursky253fb072011-10-25 14:16:48 +0300262 rpcb_set_local(clnt, clnt4);
Chuck Lever7402ab192011-05-09 15:22:55 -0400263
264out:
265 return result;
266}
Chuck Leverc5266112009-12-03 15:58:56 -0500267
268/*
269 * Returns zero on success, otherwise a negative errno value
270 * is returned.
271 */
Chuck Lever7402ab192011-05-09 15:22:55 -0400272static int rpcb_create_local_net(void)
Chuck Levercc5598b2008-07-14 16:03:27 -0400273{
Chuck Lever7402ab192011-05-09 15:22:55 -0400274 static const struct sockaddr_in rpcb_inaddr_loopback = {
275 .sin_family = AF_INET,
276 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
277 .sin_port = htons(RPCBIND_PORT),
278 };
Chuck Levercc5598b2008-07-14 16:03:27 -0400279 struct rpc_create_args args = {
Pavel Emelyanovc653ce32010-09-29 16:04:45 +0400280 .net = &init_net,
Chuck Lever2a76b3b2009-12-03 15:58:56 -0500281 .protocol = XPRT_TRANSPORT_TCP,
Chuck Lever5a462112009-12-03 15:58:56 -0500282 .address = (struct sockaddr *)&rpcb_inaddr_loopback,
283 .addrsize = sizeof(rpcb_inaddr_loopback),
Chuck Levercc5598b2008-07-14 16:03:27 -0400284 .servername = "localhost",
285 .program = &rpcb_program,
Chuck Leverc5266112009-12-03 15:58:56 -0500286 .version = RPCBVERS_2,
Chuck Levercc5598b2008-07-14 16:03:27 -0400287 .authflavor = RPC_AUTH_UNIX,
288 .flags = RPC_CLNT_CREATE_NOPING,
289 };
Chuck Leverc5266112009-12-03 15:58:56 -0500290 struct rpc_clnt *clnt, *clnt4;
291 int result = 0;
Chuck Levercc5598b2008-07-14 16:03:27 -0400292
Chuck Leverc5266112009-12-03 15:58:56 -0500293 clnt = rpc_create(&args);
294 if (IS_ERR(clnt)) {
295 dprintk("RPC: failed to create local rpcbind "
296 "client (errno %ld).\n", PTR_ERR(clnt));
297 result = -PTR_ERR(clnt);
298 goto out;
299 }
300
301 /*
302 * This results in an RPC ping. On systems running portmapper,
303 * the v4 ping will fail. Proceed anyway, but disallow rpcb
304 * v4 upcalls.
305 */
306 clnt4 = rpc_bind_new_program(clnt, &rpcb_program, RPCBVERS_4);
307 if (IS_ERR(clnt4)) {
Chuck Lever38359352010-09-21 16:55:48 -0400308 dprintk("RPC: failed to bind second program to "
309 "rpcbind v4 client (errno %ld).\n",
310 PTR_ERR(clnt4));
Chuck Leverc5266112009-12-03 15:58:56 -0500311 clnt4 = NULL;
312 }
313
Stanislav Kinsbursky253fb072011-10-25 14:16:48 +0300314 rpcb_set_local(clnt, clnt4);
Chuck Leverc5266112009-12-03 15:58:56 -0500315
316out:
Chuck Lever7402ab192011-05-09 15:22:55 -0400317 return result;
318}
319
320/*
321 * Returns zero on success, otherwise a negative errno value
322 * is returned.
323 */
Stanislav Kinsburskyd9908562011-10-25 14:16:58 +0300324int rpcb_create_local(void)
Chuck Lever7402ab192011-05-09 15:22:55 -0400325{
326 static DEFINE_MUTEX(rpcb_create_local_mutex);
327 int result = 0;
328
Stanislav Kinsbursky253fb072011-10-25 14:16:48 +0300329 if (rpcb_get_local())
Chuck Lever7402ab192011-05-09 15:22:55 -0400330 return result;
331
332 mutex_lock(&rpcb_create_local_mutex);
Stanislav Kinsbursky253fb072011-10-25 14:16:48 +0300333 if (rpcb_get_local())
Chuck Lever7402ab192011-05-09 15:22:55 -0400334 goto out;
335
336 if (rpcb_create_local_unix() != 0)
337 result = rpcb_create_local_net();
338
339out:
Chuck Leverc5266112009-12-03 15:58:56 -0500340 mutex_unlock(&rpcb_create_local_mutex);
341 return result;
Chuck Levercc5598b2008-07-14 16:03:27 -0400342}
343
Chuck Levera5090502007-03-29 16:48:04 -0400344static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
Chuck Lever423d8b02008-07-14 16:03:28 -0400345 size_t salen, int proto, u32 version)
Chuck Levera5090502007-03-29 16:48:04 -0400346{
347 struct rpc_create_args args = {
Pavel Emelyanovc653ce32010-09-29 16:04:45 +0400348 .net = &init_net,
Chuck Levera5090502007-03-29 16:48:04 -0400349 .protocol = proto,
350 .address = srvaddr,
Chuck Lever9f6ad262007-12-10 14:56:31 -0500351 .addrsize = salen,
Chuck Levera5090502007-03-29 16:48:04 -0400352 .servername = hostname,
353 .program = &rpcb_program,
354 .version = version,
355 .authflavor = RPC_AUTH_UNIX,
Chuck Lever423d8b02008-07-14 16:03:28 -0400356 .flags = (RPC_CLNT_CREATE_NOPING |
357 RPC_CLNT_CREATE_NONPRIVPORT),
Chuck Levera5090502007-03-29 16:48:04 -0400358 };
359
Chuck Leverd5b64432007-08-06 11:57:18 -0400360 switch (srvaddr->sa_family) {
361 case AF_INET:
362 ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
363 break;
364 case AF_INET6:
365 ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
366 break;
367 default:
Pavel Emelyanovc636b572010-10-06 13:45:56 +0400368 return ERR_PTR(-EAFNOSUPPORT);
Chuck Leverd5b64432007-08-06 11:57:18 -0400369 }
370
Chuck Levera5090502007-03-29 16:48:04 -0400371 return rpc_create(&args);
372}
373
Chuck Leverc5266112009-12-03 15:58:56 -0500374static int rpcb_register_call(struct rpc_clnt *clnt, struct rpc_message *msg)
Chuck Leverbabe80e2008-07-14 16:03:29 -0400375{
Chuck Lever14aeb212008-08-18 19:34:00 -0400376 int result, error = 0;
Chuck Leverbabe80e2008-07-14 16:03:29 -0400377
Chuck Lever14aeb212008-08-18 19:34:00 -0400378 msg->rpc_resp = &result;
Chuck Leverbabe80e2008-07-14 16:03:29 -0400379
Chuck Lever2a76b3b2009-12-03 15:58:56 -0500380 error = rpc_call_sync(clnt, msg, RPC_TASK_SOFTCONN);
Chuck Lever14aeb212008-08-18 19:34:00 -0400381 if (error < 0) {
Chuck Lever363f7242009-03-18 20:47:44 -0400382 dprintk("RPC: failed to contact local rpcbind "
Chuck Leverbabe80e2008-07-14 16:03:29 -0400383 "server (errno %d).\n", -error);
Chuck Lever14aeb212008-08-18 19:34:00 -0400384 return error;
385 }
Chuck Leverbabe80e2008-07-14 16:03:29 -0400386
Chuck Leverdb820d62008-09-25 11:57:05 -0400387 if (!result)
Chuck Lever14aeb212008-08-18 19:34:00 -0400388 return -EACCES;
Chuck Lever14aeb212008-08-18 19:34:00 -0400389 return 0;
Chuck Leverbabe80e2008-07-14 16:03:29 -0400390}
391
Chuck Levera5090502007-03-29 16:48:04 -0400392/**
393 * rpcb_register - set or unset a port registration with the local rpcbind svc
394 * @prog: RPC program number to bind
395 * @vers: RPC version number to bind
Chuck Leverc2e1b092008-07-14 16:03:30 -0400396 * @prot: transport protocol to register
Chuck Levera5090502007-03-29 16:48:04 -0400397 * @port: port value to register
Chuck Lever14aeb212008-08-18 19:34:00 -0400398 *
399 * Returns zero if the registration request was dispatched successfully
400 * and the rpcbind daemon returned success. Otherwise, returns an errno
401 * value that reflects the nature of the error (request could not be
402 * dispatched, timed out, or rpcbind returned an error).
Chuck Levera5090502007-03-29 16:48:04 -0400403 *
Chuck Leverc2e1b092008-07-14 16:03:30 -0400404 * RPC services invoke this function to advertise their contact
405 * information via the system's rpcbind daemon. RPC services
406 * invoke this function once for each [program, version, transport]
407 * tuple they wish to advertise.
Chuck Levera5090502007-03-29 16:48:04 -0400408 *
Chuck Leverc2e1b092008-07-14 16:03:30 -0400409 * Callers may also unregister RPC services that are no longer
410 * available by setting the passed-in port to zero. This removes
411 * all registered transports for [program, version] from the local
412 * rpcbind database.
413 *
Chuck Leverc2e1b092008-07-14 16:03:30 -0400414 * This function uses rpcbind protocol version 2 to contact the
415 * local rpcbind daemon.
416 *
417 * Registration works over both AF_INET and AF_INET6, and services
418 * registered via this function are advertised as available for any
419 * address. If the local rpcbind daemon is listening on AF_INET6,
420 * services registered via this function will be advertised on
421 * IN6ADDR_ANY (ie available for all AF_INET and AF_INET6
422 * addresses).
Chuck Levera5090502007-03-29 16:48:04 -0400423 */
Chuck Lever14aeb212008-08-18 19:34:00 -0400424int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port)
Chuck Levera5090502007-03-29 16:48:04 -0400425{
Chuck Levera5090502007-03-29 16:48:04 -0400426 struct rpcbind_args map = {
427 .r_prog = prog,
428 .r_vers = vers,
429 .r_prot = prot,
430 .r_port = port,
431 };
432 struct rpc_message msg = {
Chuck Levera5090502007-03-29 16:48:04 -0400433 .rpc_argp = &map,
Chuck Levera5090502007-03-29 16:48:04 -0400434 };
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400435 struct sunrpc_net *sn = net_generic(&init_net, sunrpc_net_id);
Chuck Levera5090502007-03-29 16:48:04 -0400436
437 dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
438 "rpcbind\n", (port ? "" : "un"),
439 prog, vers, prot, port);
440
Chuck Leverbabe80e2008-07-14 16:03:29 -0400441 msg.rpc_proc = &rpcb_procedures2[RPCBPROC_UNSET];
442 if (port)
443 msg.rpc_proc = &rpcb_procedures2[RPCBPROC_SET];
444
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400445 return rpcb_register_call(sn->rpcb_local_clnt, &msg);
Chuck Levera5090502007-03-29 16:48:04 -0400446}
447
Chuck Leverc2e1b092008-07-14 16:03:30 -0400448/*
449 * Fill in AF_INET family-specific arguments to register
450 */
Chuck Lever1673d0d2009-03-18 20:47:21 -0400451static int rpcb_register_inet4(const struct sockaddr *sap,
452 struct rpc_message *msg)
Chuck Leverc2e1b092008-07-14 16:03:30 -0400453{
Chuck Lever3aba4552009-03-18 20:47:06 -0400454 const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
Chuck Leverc2e1b092008-07-14 16:03:30 -0400455 struct rpcbind_args *map = msg->rpc_argp;
Chuck Lever3aba4552009-03-18 20:47:06 -0400456 unsigned short port = ntohs(sin->sin_port);
Chuck Leverba809132009-08-09 15:09:35 -0400457 int result;
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400458 struct sunrpc_net *sn = net_generic(&init_net, sunrpc_net_id);
Chuck Leverc2e1b092008-07-14 16:03:30 -0400459
Trond Myklebustd77385f2011-10-17 16:08:10 -0700460 map->r_addr = rpc_sockaddr2uaddr(sap, GFP_KERNEL);
Chuck Leverc2e1b092008-07-14 16:03:30 -0400461
462 dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
463 "local rpcbind\n", (port ? "" : "un"),
464 map->r_prog, map->r_vers,
465 map->r_addr, map->r_netid);
466
467 msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
468 if (port)
469 msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
470
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400471 result = rpcb_register_call(sn->rpcb_local_clnt4, msg);
Chuck Leverba809132009-08-09 15:09:35 -0400472 kfree(map->r_addr);
473 return result;
Chuck Leverc2e1b092008-07-14 16:03:30 -0400474}
475
476/*
477 * Fill in AF_INET6 family-specific arguments to register
478 */
Chuck Lever1673d0d2009-03-18 20:47:21 -0400479static int rpcb_register_inet6(const struct sockaddr *sap,
480 struct rpc_message *msg)
Chuck Leverc2e1b092008-07-14 16:03:30 -0400481{
Chuck Lever3aba4552009-03-18 20:47:06 -0400482 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sap;
Chuck Leverc2e1b092008-07-14 16:03:30 -0400483 struct rpcbind_args *map = msg->rpc_argp;
Chuck Lever3aba4552009-03-18 20:47:06 -0400484 unsigned short port = ntohs(sin6->sin6_port);
Chuck Leverba809132009-08-09 15:09:35 -0400485 int result;
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400486 struct sunrpc_net *sn = net_generic(&init_net, sunrpc_net_id);
Chuck Leverc2e1b092008-07-14 16:03:30 -0400487
Trond Myklebustd77385f2011-10-17 16:08:10 -0700488 map->r_addr = rpc_sockaddr2uaddr(sap, GFP_KERNEL);
Chuck Leverc2e1b092008-07-14 16:03:30 -0400489
490 dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
491 "local rpcbind\n", (port ? "" : "un"),
492 map->r_prog, map->r_vers,
493 map->r_addr, map->r_netid);
494
495 msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
496 if (port)
497 msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
498
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400499 result = rpcb_register_call(sn->rpcb_local_clnt4, msg);
Chuck Leverba809132009-08-09 15:09:35 -0400500 kfree(map->r_addr);
501 return result;
Chuck Leverc2e1b092008-07-14 16:03:30 -0400502}
503
Chuck Lever1673d0d2009-03-18 20:47:21 -0400504static int rpcb_unregister_all_protofamilies(struct rpc_message *msg)
505{
506 struct rpcbind_args *map = msg->rpc_argp;
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400507 struct sunrpc_net *sn = net_generic(&init_net, sunrpc_net_id);
Chuck Lever1673d0d2009-03-18 20:47:21 -0400508
509 dprintk("RPC: unregistering [%u, %u, '%s'] with "
510 "local rpcbind\n",
511 map->r_prog, map->r_vers, map->r_netid);
512
513 map->r_addr = "";
514 msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
515
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400516 return rpcb_register_call(sn->rpcb_local_clnt4, msg);
Chuck Lever1673d0d2009-03-18 20:47:21 -0400517}
518
Chuck Leverc2e1b092008-07-14 16:03:30 -0400519/**
520 * rpcb_v4_register - set or unset a port registration with the local rpcbind
521 * @program: RPC program number of service to (un)register
522 * @version: RPC version number of service to (un)register
523 * @address: address family, IP address, and port to (un)register
524 * @netid: netid of transport protocol to (un)register
Chuck Lever14aeb212008-08-18 19:34:00 -0400525 *
526 * Returns zero if the registration request was dispatched successfully
527 * and the rpcbind daemon returned success. Otherwise, returns an errno
528 * value that reflects the nature of the error (request could not be
529 * dispatched, timed out, or rpcbind returned an error).
Chuck Leverc2e1b092008-07-14 16:03:30 -0400530 *
531 * RPC services invoke this function to advertise their contact
532 * information via the system's rpcbind daemon. RPC services
533 * invoke this function once for each [program, version, address,
534 * netid] tuple they wish to advertise.
535 *
Chuck Lever1673d0d2009-03-18 20:47:21 -0400536 * Callers may also unregister RPC services that are registered at a
537 * specific address by setting the port number in @address to zero.
538 * They may unregister all registered protocol families at once for
539 * a service by passing a NULL @address argument. If @netid is ""
540 * then all netids for [program, version, address] are unregistered.
Chuck Leverc2e1b092008-07-14 16:03:30 -0400541 *
Chuck Leverc2e1b092008-07-14 16:03:30 -0400542 * This function uses rpcbind protocol version 4 to contact the
543 * local rpcbind daemon. The local rpcbind daemon must support
544 * version 4 of the rpcbind protocol in order for these functions
545 * to register a service successfully.
546 *
547 * Supported netids include "udp" and "tcp" for UDP and TCP over
548 * IPv4, and "udp6" and "tcp6" for UDP and TCP over IPv6,
549 * respectively.
550 *
551 * The contents of @address determine the address family and the
552 * port to be registered. The usual practice is to pass INADDR_ANY
553 * as the raw address, but specifying a non-zero address is also
554 * supported by this API if the caller wishes to advertise an RPC
555 * service on a specific network interface.
556 *
557 * Note that passing in INADDR_ANY does not create the same service
558 * registration as IN6ADDR_ANY. The former advertises an RPC
559 * service on any IPv4 address, but not on IPv6. The latter
560 * advertises the service on all IPv4 and IPv6 addresses.
561 */
562int rpcb_v4_register(const u32 program, const u32 version,
Chuck Lever14aeb212008-08-18 19:34:00 -0400563 const struct sockaddr *address, const char *netid)
Chuck Leverc2e1b092008-07-14 16:03:30 -0400564{
565 struct rpcbind_args map = {
566 .r_prog = program,
567 .r_vers = version,
568 .r_netid = netid,
569 .r_owner = RPCB_OWNER_STRING,
570 };
571 struct rpc_message msg = {
572 .rpc_argp = &map,
Chuck Leverc2e1b092008-07-14 16:03:30 -0400573 };
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400574 struct sunrpc_net *sn = net_generic(&init_net, sunrpc_net_id);
Chuck Leverc5266112009-12-03 15:58:56 -0500575
Stanislav Kinsburskydff02d42012-01-13 12:52:10 +0400576 if (sn->rpcb_local_clnt4 == NULL)
Chuck Leverc5266112009-12-03 15:58:56 -0500577 return -EPROTONOSUPPORT;
Chuck Leverc2e1b092008-07-14 16:03:30 -0400578
Chuck Lever1673d0d2009-03-18 20:47:21 -0400579 if (address == NULL)
580 return rpcb_unregister_all_protofamilies(&msg);
581
Chuck Leverc2e1b092008-07-14 16:03:30 -0400582 switch (address->sa_family) {
583 case AF_INET:
Chuck Lever1673d0d2009-03-18 20:47:21 -0400584 return rpcb_register_inet4(address, &msg);
Chuck Leverc2e1b092008-07-14 16:03:30 -0400585 case AF_INET6:
Chuck Lever1673d0d2009-03-18 20:47:21 -0400586 return rpcb_register_inet6(address, &msg);
Chuck Leverc2e1b092008-07-14 16:03:30 -0400587 }
588
589 return -EAFNOSUPPORT;
590}
591
Trond Myklebust803a9062008-07-01 15:20:55 -0400592static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt, struct rpcbind_args *map, struct rpc_procinfo *proc)
Trond Myklebust5138fde2007-07-14 15:40:01 -0400593{
594 struct rpc_message msg = {
Trond Myklebust803a9062008-07-01 15:20:55 -0400595 .rpc_proc = proc,
Trond Myklebust5138fde2007-07-14 15:40:01 -0400596 .rpc_argp = map,
Chuck Leverc0c077d2009-08-09 15:09:43 -0400597 .rpc_resp = map,
Trond Myklebust5138fde2007-07-14 15:40:01 -0400598 };
599 struct rpc_task_setup task_setup_data = {
600 .rpc_client = rpcb_clnt,
601 .rpc_message = &msg,
602 .callback_ops = &rpcb_getport_ops,
603 .callback_data = map,
Chuck Lever012da152009-12-03 15:58:56 -0500604 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFTCONN,
Trond Myklebust5138fde2007-07-14 15:40:01 -0400605 };
606
607 return rpc_run_task(&task_setup_data);
608}
609
Trond Myklebust9a4bd292008-10-03 16:48:34 -0400610/*
611 * In the case where rpc clients have been cloned, we want to make
612 * sure that we use the program number/version etc of the actual
613 * owner of the xprt. To do so, we walk back up the tree of parents
614 * to find whoever created the transport and/or whoever has the
615 * autobind flag set.
616 */
617static struct rpc_clnt *rpcb_find_transport_owner(struct rpc_clnt *clnt)
618{
619 struct rpc_clnt *parent = clnt->cl_parent;
620
621 while (parent != clnt) {
622 if (parent->cl_xprt != clnt->cl_xprt)
623 break;
624 if (clnt->cl_autobind)
625 break;
626 clnt = parent;
627 parent = parent->cl_parent;
628 }
629 return clnt;
630}
631
Chuck Levera5090502007-03-29 16:48:04 -0400632/**
Chuck Lever45160d62007-07-01 12:13:17 -0400633 * rpcb_getport_async - obtain the port for a given RPC service on a given host
Chuck Levera5090502007-03-29 16:48:04 -0400634 * @task: task that is waiting for portmapper request
635 *
636 * This one can be called for an ongoing RPC request, and can be used in
637 * an async (rpciod) context.
638 */
Chuck Lever45160d62007-07-01 12:13:17 -0400639void rpcb_getport_async(struct rpc_task *task)
Chuck Levera5090502007-03-29 16:48:04 -0400640{
Trond Myklebust9a4bd292008-10-03 16:48:34 -0400641 struct rpc_clnt *clnt;
Trond Myklebust803a9062008-07-01 15:20:55 -0400642 struct rpc_procinfo *proc;
Chuck Lever0a48f5d2007-12-10 14:56:38 -0500643 u32 bind_version;
Trond Myklebust9a4bd292008-10-03 16:48:34 -0400644 struct rpc_xprt *xprt;
Chuck Levera5090502007-03-29 16:48:04 -0400645 struct rpc_clnt *rpcb_clnt;
Ben Greearec0dd262011-07-12 10:27:55 -0700646 struct rpcbind_args *map;
Chuck Levera5090502007-03-29 16:48:04 -0400647 struct rpc_task *child;
Chuck Lever9f6ad262007-12-10 14:56:31 -0500648 struct sockaddr_storage addr;
649 struct sockaddr *sap = (struct sockaddr *)&addr;
650 size_t salen;
Chuck Levera5090502007-03-29 16:48:04 -0400651 int status;
652
Trond Myklebust9a4bd292008-10-03 16:48:34 -0400653 clnt = rpcb_find_transport_owner(task->tk_client);
654 xprt = clnt->cl_xprt;
655
Chuck Lever45160d62007-07-01 12:13:17 -0400656 dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800657 task->tk_pid, __func__,
Chuck Lever45160d62007-07-01 12:13:17 -0400658 clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot);
Chuck Levera5090502007-03-29 16:48:04 -0400659
Trond Myklebust381ba742008-07-07 12:18:53 -0400660 /* Put self on the wait queue to ensure we get notified if
661 * some other task is already attempting to bind the port */
662 rpc_sleep_on(&xprt->binding, task, NULL);
663
Chuck Levera5090502007-03-29 16:48:04 -0400664 if (xprt_test_and_set_binding(xprt)) {
Chuck Lever45160d62007-07-01 12:13:17 -0400665 dprintk("RPC: %5u %s: waiting for another binder\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800666 task->tk_pid, __func__);
Trond Myklebust381ba742008-07-07 12:18:53 -0400667 return;
Chuck Levera5090502007-03-29 16:48:04 -0400668 }
669
Chuck Levera5090502007-03-29 16:48:04 -0400670 /* Someone else may have bound if we slept */
671 if (xprt_bound(xprt)) {
672 status = 0;
Chuck Lever45160d62007-07-01 12:13:17 -0400673 dprintk("RPC: %5u %s: already bound\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800674 task->tk_pid, __func__);
Chuck Levera5090502007-03-29 16:48:04 -0400675 goto bailout_nofree;
676 }
677
Chuck Leverba809132009-08-09 15:09:35 -0400678 /* Parent transport's destination address */
Chuck Lever9f6ad262007-12-10 14:56:31 -0500679 salen = rpc_peeraddr(clnt, sap, sizeof(addr));
Chuck Leverd5b64432007-08-06 11:57:18 -0400680
681 /* Don't ever use rpcbind v2 for AF_INET6 requests */
Chuck Lever9f6ad262007-12-10 14:56:31 -0500682 switch (sap->sa_family) {
Chuck Leverd5b64432007-08-06 11:57:18 -0400683 case AF_INET:
Trond Myklebust803a9062008-07-01 15:20:55 -0400684 proc = rpcb_next_version[xprt->bind_index].rpc_proc;
685 bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
Chuck Leverd5b64432007-08-06 11:57:18 -0400686 break;
687 case AF_INET6:
Trond Myklebust803a9062008-07-01 15:20:55 -0400688 proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
689 bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
Chuck Leverd5b64432007-08-06 11:57:18 -0400690 break;
691 default:
692 status = -EAFNOSUPPORT;
693 dprintk("RPC: %5u %s: bad address family\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800694 task->tk_pid, __func__);
Chuck Leverd5b64432007-08-06 11:57:18 -0400695 goto bailout_nofree;
696 }
Trond Myklebust803a9062008-07-01 15:20:55 -0400697 if (proc == NULL) {
Chuck Levera5090502007-03-29 16:48:04 -0400698 xprt->bind_index = 0;
Chuck Lever906462a2007-09-11 18:00:47 -0400699 status = -EPFNOSUPPORT;
Chuck Lever45160d62007-07-01 12:13:17 -0400700 dprintk("RPC: %5u %s: no more getport versions available\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800701 task->tk_pid, __func__);
Chuck Levera5090502007-03-29 16:48:04 -0400702 goto bailout_nofree;
703 }
Chuck Levera5090502007-03-29 16:48:04 -0400704
Chuck Lever45160d62007-07-01 12:13:17 -0400705 dprintk("RPC: %5u %s: trying rpcbind version %u\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800706 task->tk_pid, __func__, bind_version);
Chuck Levera5090502007-03-29 16:48:04 -0400707
Chuck Lever9f6ad262007-12-10 14:56:31 -0500708 rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot,
Chuck Lever423d8b02008-07-14 16:03:28 -0400709 bind_version);
Chuck Lever143b6c42007-08-16 16:03:31 -0400710 if (IS_ERR(rpcb_clnt)) {
711 status = PTR_ERR(rpcb_clnt);
712 dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800713 task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
Chuck Lever143b6c42007-08-16 16:03:31 -0400714 goto bailout_nofree;
715 }
716
Chuck Levera5090502007-03-29 16:48:04 -0400717 map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
718 if (!map) {
719 status = -ENOMEM;
Chuck Lever45160d62007-07-01 12:13:17 -0400720 dprintk("RPC: %5u %s: no memory available\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800721 task->tk_pid, __func__);
Trond Myklebust96165e22008-10-03 16:48:40 -0400722 goto bailout_release_client;
Chuck Levera5090502007-03-29 16:48:04 -0400723 }
724 map->r_prog = clnt->cl_prog;
725 map->r_vers = clnt->cl_vers;
726 map->r_prot = xprt->prot;
727 map->r_port = 0;
728 map->r_xprt = xprt_get(xprt);
Trond Myklebust381ba742008-07-07 12:18:53 -0400729 map->r_status = -EIO;
Chuck Levera5090502007-03-29 16:48:04 -0400730
Chuck Leverba809132009-08-09 15:09:35 -0400731 switch (bind_version) {
732 case RPCBVERS_4:
733 case RPCBVERS_3:
734 map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
Trond Myklebustd77385f2011-10-17 16:08:10 -0700735 map->r_addr = rpc_sockaddr2uaddr(sap, GFP_ATOMIC);
Chuck Leverba809132009-08-09 15:09:35 -0400736 map->r_owner = "";
737 break;
738 case RPCBVERS_2:
739 map->r_addr = NULL;
740 break;
741 default:
742 BUG();
743 }
744
Trond Myklebust803a9062008-07-01 15:20:55 -0400745 child = rpcb_call_async(rpcb_clnt, map, proc);
Trond Myklebust4c402b42007-06-14 16:40:32 -0400746 rpc_release_client(rpcb_clnt);
Chuck Levera5090502007-03-29 16:48:04 -0400747 if (IS_ERR(child)) {
Trond Myklebust0d3a34b2008-07-07 12:18:52 -0400748 /* rpcb_map_release() has freed the arguments */
Chuck Lever45160d62007-07-01 12:13:17 -0400749 dprintk("RPC: %5u %s: rpc_run_task failed\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800750 task->tk_pid, __func__);
Trond Myklebust381ba742008-07-07 12:18:53 -0400751 return;
Chuck Levera5090502007-03-29 16:48:04 -0400752 }
Chuck Levera5090502007-03-29 16:48:04 -0400753
Trond Myklebust9a4bd292008-10-03 16:48:34 -0400754 xprt->stat.bind_count++;
755 rpc_put_task(child);
Chuck Levera5090502007-03-29 16:48:04 -0400756 return;
757
Trond Myklebust96165e22008-10-03 16:48:40 -0400758bailout_release_client:
759 rpc_release_client(rpcb_clnt);
Chuck Levera5090502007-03-29 16:48:04 -0400760bailout_nofree:
761 rpcb_wake_rpcbind_waiters(xprt, status);
Chuck Levera5090502007-03-29 16:48:04 -0400762 task->tk_status = status;
763}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400764EXPORT_SYMBOL_GPL(rpcb_getport_async);
Chuck Levera5090502007-03-29 16:48:04 -0400765
766/*
767 * Rpcbind child task calls this callback via tk_exit.
768 */
769static void rpcb_getport_done(struct rpc_task *child, void *data)
770{
771 struct rpcbind_args *map = data;
772 struct rpc_xprt *xprt = map->r_xprt;
773 int status = child->tk_status;
774
Chuck Lever4784cb52007-09-11 18:00:36 -0400775 /* Garbage reply: retry with a lesser rpcbind version */
776 if (status == -EIO)
777 status = -EPROTONOSUPPORT;
778
Chuck Levera5090502007-03-29 16:48:04 -0400779 /* rpcbind server doesn't support this rpcbind protocol version */
780 if (status == -EPROTONOSUPPORT)
781 xprt->bind_index++;
782
783 if (status < 0) {
784 /* rpcbind server not available on remote host? */
785 xprt->ops->set_port(xprt, 0);
786 } else if (map->r_port == 0) {
787 /* Requested RPC service wasn't registered on remote host */
788 xprt->ops->set_port(xprt, 0);
789 status = -EACCES;
790 } else {
791 /* Succeeded */
792 xprt->ops->set_port(xprt, map->r_port);
793 xprt_set_bound(xprt);
794 status = 0;
795 }
796
797 dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
798 child->tk_pid, status, map->r_port);
799
Trond Myklebust381ba742008-07-07 12:18:53 -0400800 map->r_status = status;
Chuck Levera5090502007-03-29 16:48:04 -0400801}
802
Chuck Lever166b88d2008-07-14 16:03:26 -0400803/*
804 * XDR functions for rpcbind
805 */
806
Chuck Lever9f06c712010-12-14 14:59:18 +0000807static void rpcb_enc_mapping(struct rpc_rqst *req, struct xdr_stream *xdr,
808 const struct rpcbind_args *rpcb)
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400809{
810 struct rpc_task *task = req->rq_task;
Chuck Lever9f06c712010-12-14 14:59:18 +0000811 __be32 *p;
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400812
813 dprintk("RPC: %5u encoding PMAP_%s call (%u, %u, %d, %u)\n",
814 task->tk_pid, task->tk_msg.rpc_proc->p_name,
815 rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
816
Chuck Lever9f06c712010-12-14 14:59:18 +0000817 p = xdr_reserve_space(xdr, RPCB_mappingargs_sz << 2);
Chuck Lever4129ccf2010-12-14 14:58:59 +0000818 *p++ = cpu_to_be32(rpcb->r_prog);
819 *p++ = cpu_to_be32(rpcb->r_vers);
820 *p++ = cpu_to_be32(rpcb->r_prot);
821 *p = cpu_to_be32(rpcb->r_port);
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400822}
823
Chuck Leverbf269552010-12-14 14:59:29 +0000824static int rpcb_dec_getport(struct rpc_rqst *req, struct xdr_stream *xdr,
Chuck Leverc0c077d2009-08-09 15:09:43 -0400825 struct rpcbind_args *rpcb)
826{
827 struct rpc_task *task = req->rq_task;
Chuck Leverc0c077d2009-08-09 15:09:43 -0400828 unsigned long port;
Chuck Leverbf269552010-12-14 14:59:29 +0000829 __be32 *p;
Chuck Leverc0c077d2009-08-09 15:09:43 -0400830
831 rpcb->r_port = 0;
832
Chuck Leverbf269552010-12-14 14:59:29 +0000833 p = xdr_inline_decode(xdr, 4);
Chuck Leverc0c077d2009-08-09 15:09:43 -0400834 if (unlikely(p == NULL))
835 return -EIO;
836
Chuck Lever4129ccf2010-12-14 14:58:59 +0000837 port = be32_to_cpup(p);
Chuck Leverc0c077d2009-08-09 15:09:43 -0400838 dprintk("RPC: %5u PMAP_%s result: %lu\n", task->tk_pid,
839 task->tk_msg.rpc_proc->p_name, port);
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700840 if (unlikely(port > USHRT_MAX))
Chuck Leverc0c077d2009-08-09 15:09:43 -0400841 return -EIO;
842
843 rpcb->r_port = port;
844 return 0;
845}
846
Chuck Leverbf269552010-12-14 14:59:29 +0000847static int rpcb_dec_set(struct rpc_rqst *req, struct xdr_stream *xdr,
Chuck Lever7ed0ff92009-08-09 15:09:42 -0400848 unsigned int *boolp)
849{
850 struct rpc_task *task = req->rq_task;
Chuck Leverbf269552010-12-14 14:59:29 +0000851 __be32 *p;
Chuck Lever7ed0ff92009-08-09 15:09:42 -0400852
Chuck Leverbf269552010-12-14 14:59:29 +0000853 p = xdr_inline_decode(xdr, 4);
Chuck Lever7ed0ff92009-08-09 15:09:42 -0400854 if (unlikely(p == NULL))
855 return -EIO;
856
857 *boolp = 0;
Chuck Leverbf269552010-12-14 14:59:29 +0000858 if (*p != xdr_zero)
Chuck Lever7ed0ff92009-08-09 15:09:42 -0400859 *boolp = 1;
860
861 dprintk("RPC: %5u RPCB_%s call %s\n",
862 task->tk_pid, task->tk_msg.rpc_proc->p_name,
863 (*boolp ? "succeeded" : "failed"));
864 return 0;
865}
866
Chuck Lever4129ccf2010-12-14 14:58:59 +0000867static void encode_rpcb_string(struct xdr_stream *xdr, const char *string,
868 const u32 maxstrlen)
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400869{
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400870 __be32 *p;
Chuck Lever4129ccf2010-12-14 14:58:59 +0000871 u32 len;
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400872
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400873 len = strlen(string);
Chuck Lever4129ccf2010-12-14 14:58:59 +0000874 BUG_ON(len > maxstrlen);
875 p = xdr_reserve_space(xdr, 4 + len);
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400876 xdr_encode_opaque(p, string, len);
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400877}
878
Chuck Lever9f06c712010-12-14 14:59:18 +0000879static void rpcb_enc_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr,
880 const struct rpcbind_args *rpcb)
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400881{
882 struct rpc_task *task = req->rq_task;
Chuck Lever9f06c712010-12-14 14:59:18 +0000883 __be32 *p;
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400884
885 dprintk("RPC: %5u encoding RPCB_%s call (%u, %u, '%s', '%s')\n",
886 task->tk_pid, task->tk_msg.rpc_proc->p_name,
887 rpcb->r_prog, rpcb->r_vers,
888 rpcb->r_netid, rpcb->r_addr);
889
Chuck Lever9f06c712010-12-14 14:59:18 +0000890 p = xdr_reserve_space(xdr, (RPCB_program_sz + RPCB_version_sz) << 2);
Chuck Lever4129ccf2010-12-14 14:58:59 +0000891 *p++ = cpu_to_be32(rpcb->r_prog);
892 *p = cpu_to_be32(rpcb->r_vers);
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400893
Chuck Lever9f06c712010-12-14 14:59:18 +0000894 encode_rpcb_string(xdr, rpcb->r_netid, RPCBIND_MAXNETIDLEN);
895 encode_rpcb_string(xdr, rpcb->r_addr, RPCBIND_MAXUADDRLEN);
896 encode_rpcb_string(xdr, rpcb->r_owner, RPCB_MAXOWNERLEN);
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400897}
898
Chuck Leverbf269552010-12-14 14:59:29 +0000899static int rpcb_dec_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr,
Chuck Leverc0c077d2009-08-09 15:09:43 -0400900 struct rpcbind_args *rpcb)
901{
902 struct sockaddr_storage address;
903 struct sockaddr *sap = (struct sockaddr *)&address;
904 struct rpc_task *task = req->rq_task;
Chuck Leverbf269552010-12-14 14:59:29 +0000905 __be32 *p;
Chuck Leverc0c077d2009-08-09 15:09:43 -0400906 u32 len;
907
908 rpcb->r_port = 0;
909
Chuck Leverbf269552010-12-14 14:59:29 +0000910 p = xdr_inline_decode(xdr, 4);
Chuck Leverc0c077d2009-08-09 15:09:43 -0400911 if (unlikely(p == NULL))
912 goto out_fail;
Chuck Lever4129ccf2010-12-14 14:58:59 +0000913 len = be32_to_cpup(p);
Chuck Leverc0c077d2009-08-09 15:09:43 -0400914
915 /*
916 * If the returned universal address is a null string,
917 * the requested RPC service was not registered.
918 */
919 if (len == 0) {
920 dprintk("RPC: %5u RPCB reply: program not registered\n",
921 task->tk_pid);
922 return 0;
923 }
924
925 if (unlikely(len > RPCBIND_MAXUADDRLEN))
926 goto out_fail;
927
Chuck Leverbf269552010-12-14 14:59:29 +0000928 p = xdr_inline_decode(xdr, len);
Chuck Leverc0c077d2009-08-09 15:09:43 -0400929 if (unlikely(p == NULL))
930 goto out_fail;
931 dprintk("RPC: %5u RPCB_%s reply: %s\n", task->tk_pid,
932 task->tk_msg.rpc_proc->p_name, (char *)p);
933
934 if (rpc_uaddr2sockaddr((char *)p, len, sap, sizeof(address)) == 0)
935 goto out_fail;
936 rpcb->r_port = rpc_get_port(sap);
937
938 return 0;
939
940out_fail:
941 dprintk("RPC: %5u malformed RPCB_%s reply\n",
942 task->tk_pid, task->tk_msg.rpc_proc->p_name);
943 return -EIO;
944}
945
Chuck Levera5090502007-03-29 16:48:04 -0400946/*
947 * Not all rpcbind procedures described in RFC 1833 are implemented
948 * since the Linux kernel RPC code requires only these.
949 */
Chuck Leverf8b761e2009-08-09 15:09:44 -0400950
Chuck Levera5090502007-03-29 16:48:04 -0400951static struct rpc_procinfo rpcb_procedures2[] = {
Chuck Leverf8b761e2009-08-09 15:09:44 -0400952 [RPCBPROC_SET] = {
953 .p_proc = RPCBPROC_SET,
Chuck Lever9f06c712010-12-14 14:59:18 +0000954 .p_encode = (kxdreproc_t)rpcb_enc_mapping,
Chuck Leverbf269552010-12-14 14:59:29 +0000955 .p_decode = (kxdrdproc_t)rpcb_dec_set,
Chuck Leverf8b761e2009-08-09 15:09:44 -0400956 .p_arglen = RPCB_mappingargs_sz,
957 .p_replen = RPCB_setres_sz,
958 .p_statidx = RPCBPROC_SET,
959 .p_timer = 0,
960 .p_name = "SET",
961 },
962 [RPCBPROC_UNSET] = {
963 .p_proc = RPCBPROC_UNSET,
Chuck Lever9f06c712010-12-14 14:59:18 +0000964 .p_encode = (kxdreproc_t)rpcb_enc_mapping,
Chuck Leverbf269552010-12-14 14:59:29 +0000965 .p_decode = (kxdrdproc_t)rpcb_dec_set,
Chuck Leverf8b761e2009-08-09 15:09:44 -0400966 .p_arglen = RPCB_mappingargs_sz,
967 .p_replen = RPCB_setres_sz,
968 .p_statidx = RPCBPROC_UNSET,
969 .p_timer = 0,
970 .p_name = "UNSET",
971 },
972 [RPCBPROC_GETPORT] = {
973 .p_proc = RPCBPROC_GETPORT,
Chuck Lever9f06c712010-12-14 14:59:18 +0000974 .p_encode = (kxdreproc_t)rpcb_enc_mapping,
Chuck Leverbf269552010-12-14 14:59:29 +0000975 .p_decode = (kxdrdproc_t)rpcb_dec_getport,
Chuck Leverf8b761e2009-08-09 15:09:44 -0400976 .p_arglen = RPCB_mappingargs_sz,
977 .p_replen = RPCB_getportres_sz,
978 .p_statidx = RPCBPROC_GETPORT,
979 .p_timer = 0,
980 .p_name = "GETPORT",
981 },
Chuck Levera5090502007-03-29 16:48:04 -0400982};
983
984static struct rpc_procinfo rpcb_procedures3[] = {
Chuck Leverf8b761e2009-08-09 15:09:44 -0400985 [RPCBPROC_SET] = {
986 .p_proc = RPCBPROC_SET,
Chuck Lever9f06c712010-12-14 14:59:18 +0000987 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
Chuck Leverbf269552010-12-14 14:59:29 +0000988 .p_decode = (kxdrdproc_t)rpcb_dec_set,
Chuck Leverf8b761e2009-08-09 15:09:44 -0400989 .p_arglen = RPCB_getaddrargs_sz,
990 .p_replen = RPCB_setres_sz,
991 .p_statidx = RPCBPROC_SET,
992 .p_timer = 0,
993 .p_name = "SET",
994 },
995 [RPCBPROC_UNSET] = {
996 .p_proc = RPCBPROC_UNSET,
Chuck Lever9f06c712010-12-14 14:59:18 +0000997 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
Chuck Leverbf269552010-12-14 14:59:29 +0000998 .p_decode = (kxdrdproc_t)rpcb_dec_set,
Chuck Leverf8b761e2009-08-09 15:09:44 -0400999 .p_arglen = RPCB_getaddrargs_sz,
1000 .p_replen = RPCB_setres_sz,
1001 .p_statidx = RPCBPROC_UNSET,
1002 .p_timer = 0,
1003 .p_name = "UNSET",
1004 },
1005 [RPCBPROC_GETADDR] = {
1006 .p_proc = RPCBPROC_GETADDR,
Chuck Lever9f06c712010-12-14 14:59:18 +00001007 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
Chuck Leverbf269552010-12-14 14:59:29 +00001008 .p_decode = (kxdrdproc_t)rpcb_dec_getaddr,
Chuck Leverf8b761e2009-08-09 15:09:44 -04001009 .p_arglen = RPCB_getaddrargs_sz,
1010 .p_replen = RPCB_getaddrres_sz,
1011 .p_statidx = RPCBPROC_GETADDR,
1012 .p_timer = 0,
1013 .p_name = "GETADDR",
1014 },
Chuck Levera5090502007-03-29 16:48:04 -04001015};
1016
1017static struct rpc_procinfo rpcb_procedures4[] = {
Chuck Leverf8b761e2009-08-09 15:09:44 -04001018 [RPCBPROC_SET] = {
1019 .p_proc = RPCBPROC_SET,
Chuck Lever9f06c712010-12-14 14:59:18 +00001020 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
Chuck Leverbf269552010-12-14 14:59:29 +00001021 .p_decode = (kxdrdproc_t)rpcb_dec_set,
Chuck Leverf8b761e2009-08-09 15:09:44 -04001022 .p_arglen = RPCB_getaddrargs_sz,
1023 .p_replen = RPCB_setres_sz,
1024 .p_statidx = RPCBPROC_SET,
1025 .p_timer = 0,
1026 .p_name = "SET",
1027 },
1028 [RPCBPROC_UNSET] = {
1029 .p_proc = RPCBPROC_UNSET,
Chuck Lever9f06c712010-12-14 14:59:18 +00001030 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
Chuck Leverbf269552010-12-14 14:59:29 +00001031 .p_decode = (kxdrdproc_t)rpcb_dec_set,
Chuck Leverf8b761e2009-08-09 15:09:44 -04001032 .p_arglen = RPCB_getaddrargs_sz,
1033 .p_replen = RPCB_setres_sz,
1034 .p_statidx = RPCBPROC_UNSET,
1035 .p_timer = 0,
1036 .p_name = "UNSET",
1037 },
1038 [RPCBPROC_GETADDR] = {
1039 .p_proc = RPCBPROC_GETADDR,
Chuck Lever9f06c712010-12-14 14:59:18 +00001040 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
Chuck Leverbf269552010-12-14 14:59:29 +00001041 .p_decode = (kxdrdproc_t)rpcb_dec_getaddr,
Chuck Leverf8b761e2009-08-09 15:09:44 -04001042 .p_arglen = RPCB_getaddrargs_sz,
1043 .p_replen = RPCB_getaddrres_sz,
1044 .p_statidx = RPCBPROC_GETADDR,
1045 .p_timer = 0,
1046 .p_name = "GETADDR",
1047 },
Chuck Levera5090502007-03-29 16:48:04 -04001048};
1049
1050static struct rpcb_info rpcb_next_version[] = {
Chuck Leverfc200e72008-06-25 17:24:31 -04001051 {
1052 .rpc_vers = RPCBVERS_2,
1053 .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
1054 },
1055 {
1056 .rpc_proc = NULL,
1057 },
Chuck Levera5090502007-03-29 16:48:04 -04001058};
1059
Chuck Leverd5b64432007-08-06 11:57:18 -04001060static struct rpcb_info rpcb_next_version6[] = {
Chuck Leverfc200e72008-06-25 17:24:31 -04001061 {
1062 .rpc_vers = RPCBVERS_4,
Chuck Lever88424132008-06-25 17:24:47 -04001063 .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR],
Chuck Leverfc200e72008-06-25 17:24:31 -04001064 },
1065 {
1066 .rpc_vers = RPCBVERS_3,
1067 .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR],
1068 },
Chuck Leverfc200e72008-06-25 17:24:31 -04001069 {
1070 .rpc_proc = NULL,
1071 },
Chuck Leverd5b64432007-08-06 11:57:18 -04001072};
1073
Chuck Levera5090502007-03-29 16:48:04 -04001074static struct rpc_version rpcb_version2 = {
Chuck Leverfc200e72008-06-25 17:24:31 -04001075 .number = RPCBVERS_2,
Chuck Lever1ac7c232010-12-14 14:59:09 +00001076 .nrprocs = ARRAY_SIZE(rpcb_procedures2),
Chuck Levera5090502007-03-29 16:48:04 -04001077 .procs = rpcb_procedures2
1078};
1079
1080static struct rpc_version rpcb_version3 = {
Chuck Leverfc200e72008-06-25 17:24:31 -04001081 .number = RPCBVERS_3,
Chuck Lever1ac7c232010-12-14 14:59:09 +00001082 .nrprocs = ARRAY_SIZE(rpcb_procedures3),
Chuck Levera5090502007-03-29 16:48:04 -04001083 .procs = rpcb_procedures3
1084};
1085
1086static struct rpc_version rpcb_version4 = {
Chuck Leverfc200e72008-06-25 17:24:31 -04001087 .number = RPCBVERS_4,
Chuck Lever1ac7c232010-12-14 14:59:09 +00001088 .nrprocs = ARRAY_SIZE(rpcb_procedures4),
Chuck Levera5090502007-03-29 16:48:04 -04001089 .procs = rpcb_procedures4
1090};
1091
1092static struct rpc_version *rpcb_version[] = {
1093 NULL,
1094 NULL,
1095 &rpcb_version2,
1096 &rpcb_version3,
1097 &rpcb_version4
1098};
1099
1100static struct rpc_stat rpcb_stats;
1101
Adrian Bunk7f4adef2007-06-13 01:03:13 +02001102static struct rpc_program rpcb_program = {
Chuck Levera5090502007-03-29 16:48:04 -04001103 .name = "rpcbind",
1104 .number = RPCBIND_PROGRAM,
1105 .nrvers = ARRAY_SIZE(rpcb_version),
1106 .version = rpcb_version,
1107 .stats = &rpcb_stats,
1108};