blob: e6fb21b19b86b6421011f76400e74e5d2a919acc [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 Leverd5b64432007-08-06 11:57:18 -040019#include <linux/in.h>
20#include <linux/in6.h>
Chuck Levera5090502007-03-29 16:48:04 -040021#include <linux/kernel.h>
22#include <linux/errno.h>
23
24#include <linux/sunrpc/clnt.h>
25#include <linux/sunrpc/sched.h>
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -040026#include <linux/sunrpc/xprtsock.h>
Chuck Levera5090502007-03-29 16:48:04 -040027
28#ifdef RPC_DEBUG
29# define RPCDBG_FACILITY RPCDBG_BIND
30#endif
31
32#define RPCBIND_PROGRAM (100000u)
33#define RPCBIND_PORT (111u)
34
35enum {
36 RPCBPROC_NULL,
37 RPCBPROC_SET,
38 RPCBPROC_UNSET,
39 RPCBPROC_GETPORT,
40 RPCBPROC_GETADDR = 3, /* alias for GETPORT */
41 RPCBPROC_DUMP,
42 RPCBPROC_CALLIT,
43 RPCBPROC_BCAST = 5, /* alias for CALLIT */
44 RPCBPROC_GETTIME,
45 RPCBPROC_UADDR2TADDR,
46 RPCBPROC_TADDR2UADDR,
47 RPCBPROC_GETVERSADDR,
48 RPCBPROC_INDIRECT,
49 RPCBPROC_GETADDRLIST,
50 RPCBPROC_GETSTAT,
51};
52
53#define RPCB_HIGHPROC_2 RPCBPROC_CALLIT
54#define RPCB_HIGHPROC_3 RPCBPROC_TADDR2UADDR
55#define RPCB_HIGHPROC_4 RPCBPROC_GETSTAT
56
57/*
Chuck Levera5090502007-03-29 16:48:04 -040058 * r_owner
59 *
60 * The "owner" is allowed to unset a service in the rpcbind database.
61 * We always use the following (arbitrary) fixed string.
62 */
63#define RPCB_OWNER_STRING "rpcb"
64#define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
65
66static void rpcb_getport_done(struct rpc_task *, void *);
Adrian Bunk7f4adef2007-06-13 01:03:13 +020067static struct rpc_program rpcb_program;
Chuck Levera5090502007-03-29 16:48:04 -040068
69struct rpcbind_args {
70 struct rpc_xprt * r_xprt;
71
72 u32 r_prog;
73 u32 r_vers;
74 u32 r_prot;
75 unsigned short r_port;
Trond Myklebust86d61d82008-01-07 21:16:56 -050076 const char * r_netid;
77 const char * r_addr;
78 const char * r_owner;
Chuck Levera5090502007-03-29 16:48:04 -040079};
80
81static struct rpc_procinfo rpcb_procedures2[];
82static struct rpc_procinfo rpcb_procedures3[];
83
Chuck Leverd5b64432007-08-06 11:57:18 -040084struct rpcb_info {
Chuck Levera5090502007-03-29 16:48:04 -040085 int rpc_vers;
86 struct rpc_procinfo * rpc_proc;
Chuck Leverd5b64432007-08-06 11:57:18 -040087};
88
89static struct rpcb_info rpcb_next_version[];
90static struct rpcb_info rpcb_next_version6[];
Chuck Levera5090502007-03-29 16:48:04 -040091
Chuck Levera5090502007-03-29 16:48:04 -040092static void rpcb_map_release(void *data)
93{
94 struct rpcbind_args *map = data;
95
96 xprt_put(map->r_xprt);
97 kfree(map);
98}
99
100static const struct rpc_call_ops rpcb_getport_ops = {
Chuck Levera5090502007-03-29 16:48:04 -0400101 .rpc_call_done = rpcb_getport_done,
102 .rpc_release = rpcb_map_release,
103};
104
105static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status)
106{
107 xprt_clear_binding(xprt);
108 rpc_wake_up_status(&xprt->binding, status);
109}
110
111static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
Chuck Lever0a48f5d2007-12-10 14:56:38 -0500112 size_t salen, int proto, u32 version,
Chuck Lever9f6ad262007-12-10 14:56:31 -0500113 int privileged)
Chuck Levera5090502007-03-29 16:48:04 -0400114{
115 struct rpc_create_args args = {
116 .protocol = proto,
117 .address = srvaddr,
Chuck Lever9f6ad262007-12-10 14:56:31 -0500118 .addrsize = salen,
Chuck Levera5090502007-03-29 16:48:04 -0400119 .servername = hostname,
120 .program = &rpcb_program,
121 .version = version,
122 .authflavor = RPC_AUTH_UNIX,
Matthew Wilcox150030b2007-12-06 16:24:39 -0500123 .flags = RPC_CLNT_CREATE_NOPING,
Chuck Levera5090502007-03-29 16:48:04 -0400124 };
125
Chuck Leverd5b64432007-08-06 11:57:18 -0400126 switch (srvaddr->sa_family) {
127 case AF_INET:
128 ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
129 break;
130 case AF_INET6:
131 ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
132 break;
133 default:
134 return NULL;
135 }
136
Chuck Levera5090502007-03-29 16:48:04 -0400137 if (!privileged)
138 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
139 return rpc_create(&args);
140}
141
142/**
143 * rpcb_register - set or unset a port registration with the local rpcbind svc
144 * @prog: RPC program number to bind
145 * @vers: RPC version number to bind
146 * @prot: transport protocol to use to make this request
147 * @port: port value to register
148 * @okay: result code
149 *
150 * port == 0 means unregister, port != 0 means register.
151 *
152 * This routine supports only rpcbind version 2.
153 */
154int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
155{
156 struct sockaddr_in sin = {
157 .sin_family = AF_INET,
158 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
159 };
160 struct rpcbind_args map = {
161 .r_prog = prog,
162 .r_vers = vers,
163 .r_prot = prot,
164 .r_port = port,
165 };
166 struct rpc_message msg = {
167 .rpc_proc = &rpcb_procedures2[port ?
168 RPCBPROC_SET : RPCBPROC_UNSET],
169 .rpc_argp = &map,
170 .rpc_resp = okay,
171 };
172 struct rpc_clnt *rpcb_clnt;
173 int error = 0;
174
175 dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
176 "rpcbind\n", (port ? "" : "un"),
177 prog, vers, prot, port);
178
179 rpcb_clnt = rpcb_create("localhost", (struct sockaddr *) &sin,
Chuck Lever9f6ad262007-12-10 14:56:31 -0500180 sizeof(sin), XPRT_TRANSPORT_UDP, 2, 1);
Chuck Levera5090502007-03-29 16:48:04 -0400181 if (IS_ERR(rpcb_clnt))
182 return PTR_ERR(rpcb_clnt);
183
184 error = rpc_call_sync(rpcb_clnt, &msg, 0);
185
Trond Myklebust90c57552007-06-09 19:49:36 -0400186 rpc_shutdown_client(rpcb_clnt);
Chuck Levera5090502007-03-29 16:48:04 -0400187 if (error < 0)
188 printk(KERN_WARNING "RPC: failed to contact local rpcbind "
189 "server (errno %d).\n", -error);
190 dprintk("RPC: registration status %d/%d\n", error, *okay);
191
192 return error;
193}
194
Chuck Levera5090502007-03-29 16:48:04 -0400195/**
Chuck Levercce63cd2007-07-01 12:13:12 -0400196 * rpcb_getport_sync - obtain the port for an RPC service on a given host
Chuck Levera5090502007-03-29 16:48:04 -0400197 * @sin: address of remote peer
198 * @prog: RPC program number to bind
199 * @vers: RPC version number to bind
200 * @prot: transport protocol to use to make this request
201 *
Chuck Lever67d60212008-01-14 15:12:01 -0500202 * Return value is the requested advertised port number,
203 * or a negative errno value.
204 *
Chuck Levera5090502007-03-29 16:48:04 -0400205 * Called from outside the RPC client in a synchronous task context.
Chuck Levercce63cd2007-07-01 12:13:12 -0400206 * Uses default timeout parameters specified by underlying transport.
Chuck Levera5090502007-03-29 16:48:04 -0400207 *
Chuck Lever67d60212008-01-14 15:12:01 -0500208 * XXX: Needs to support IPv6
Chuck Levera5090502007-03-29 16:48:04 -0400209 */
Chuck Leverf1ec08c2008-01-14 15:11:53 -0500210int rpcb_getport_sync(struct sockaddr_in *sin, u32 prog, u32 vers, int prot)
Chuck Levera5090502007-03-29 16:48:04 -0400211{
212 struct rpcbind_args map = {
213 .r_prog = prog,
214 .r_vers = vers,
215 .r_prot = prot,
216 .r_port = 0,
217 };
218 struct rpc_message msg = {
219 .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
220 .rpc_argp = &map,
221 .rpc_resp = &map.r_port,
222 };
223 struct rpc_clnt *rpcb_clnt;
Chuck Levera5090502007-03-29 16:48:04 -0400224 int status;
225
Chuck Levercce63cd2007-07-01 12:13:12 -0400226 dprintk("RPC: %s(" NIPQUAD_FMT ", %u, %u, %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800227 __func__, NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
Chuck Levera5090502007-03-29 16:48:04 -0400228
Chuck Leverb91e1012008-01-14 15:11:46 -0500229 rpcb_clnt = rpcb_create(NULL, (struct sockaddr *)sin,
Chuck Leverafc88112008-01-14 15:12:08 -0500230 sizeof(*sin), prot, 2, 0);
Chuck Levera5090502007-03-29 16:48:04 -0400231 if (IS_ERR(rpcb_clnt))
232 return PTR_ERR(rpcb_clnt);
233
234 status = rpc_call_sync(rpcb_clnt, &msg, 0);
Trond Myklebust90c57552007-06-09 19:49:36 -0400235 rpc_shutdown_client(rpcb_clnt);
Chuck Levera5090502007-03-29 16:48:04 -0400236
237 if (status >= 0) {
238 if (map.r_port != 0)
239 return map.r_port;
240 status = -EACCES;
241 }
242 return status;
243}
Chuck Levercce63cd2007-07-01 12:13:12 -0400244EXPORT_SYMBOL_GPL(rpcb_getport_sync);
Chuck Levera5090502007-03-29 16:48:04 -0400245
Trond Myklebust803a9062008-07-01 15:20:55 -0400246static 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 -0400247{
248 struct rpc_message msg = {
Trond Myklebust803a9062008-07-01 15:20:55 -0400249 .rpc_proc = proc,
Trond Myklebust5138fde2007-07-14 15:40:01 -0400250 .rpc_argp = map,
251 .rpc_resp = &map->r_port,
252 };
253 struct rpc_task_setup task_setup_data = {
254 .rpc_client = rpcb_clnt,
255 .rpc_message = &msg,
256 .callback_ops = &rpcb_getport_ops,
257 .callback_data = map,
258 .flags = RPC_TASK_ASYNC,
259 };
260
261 return rpc_run_task(&task_setup_data);
262}
263
Chuck Levera5090502007-03-29 16:48:04 -0400264/**
Chuck Lever45160d62007-07-01 12:13:17 -0400265 * rpcb_getport_async - obtain the port for a given RPC service on a given host
Chuck Levera5090502007-03-29 16:48:04 -0400266 * @task: task that is waiting for portmapper request
267 *
268 * This one can be called for an ongoing RPC request, and can be used in
269 * an async (rpciod) context.
270 */
Chuck Lever45160d62007-07-01 12:13:17 -0400271void rpcb_getport_async(struct rpc_task *task)
Chuck Levera5090502007-03-29 16:48:04 -0400272{
273 struct rpc_clnt *clnt = task->tk_client;
Trond Myklebust803a9062008-07-01 15:20:55 -0400274 struct rpc_procinfo *proc;
Chuck Lever0a48f5d2007-12-10 14:56:38 -0500275 u32 bind_version;
Chuck Levera5090502007-03-29 16:48:04 -0400276 struct rpc_xprt *xprt = task->tk_xprt;
277 struct rpc_clnt *rpcb_clnt;
278 static struct rpcbind_args *map;
279 struct rpc_task *child;
Chuck Lever9f6ad262007-12-10 14:56:31 -0500280 struct sockaddr_storage addr;
281 struct sockaddr *sap = (struct sockaddr *)&addr;
282 size_t salen;
Chuck Levera5090502007-03-29 16:48:04 -0400283 int status;
284
Chuck Lever45160d62007-07-01 12:13:17 -0400285 dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800286 task->tk_pid, __func__,
Chuck Lever45160d62007-07-01 12:13:17 -0400287 clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot);
Chuck Levera5090502007-03-29 16:48:04 -0400288
289 /* Autobind on cloned rpc clients is discouraged */
290 BUG_ON(clnt->cl_parent != clnt);
291
292 if (xprt_test_and_set_binding(xprt)) {
Chuck Lever2429cbf2007-09-11 18:00:41 -0400293 status = -EAGAIN; /* tell caller to check again */
Chuck Lever45160d62007-07-01 12:13:17 -0400294 dprintk("RPC: %5u %s: waiting for another binder\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800295 task->tk_pid, __func__);
Chuck Levera5090502007-03-29 16:48:04 -0400296 goto bailout_nowake;
297 }
298
299 /* Put self on queue before sending rpcbind request, in case
300 * rpcb_getport_done completes before we return from rpc_run_task */
Trond Myklebust5d008372008-02-22 16:34:17 -0500301 rpc_sleep_on(&xprt->binding, task, NULL);
Chuck Levera5090502007-03-29 16:48:04 -0400302
303 /* Someone else may have bound if we slept */
304 if (xprt_bound(xprt)) {
305 status = 0;
Chuck Lever45160d62007-07-01 12:13:17 -0400306 dprintk("RPC: %5u %s: already bound\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800307 task->tk_pid, __func__);
Chuck Levera5090502007-03-29 16:48:04 -0400308 goto bailout_nofree;
309 }
310
Chuck Lever9f6ad262007-12-10 14:56:31 -0500311 salen = rpc_peeraddr(clnt, sap, sizeof(addr));
Chuck Leverd5b64432007-08-06 11:57:18 -0400312
313 /* Don't ever use rpcbind v2 for AF_INET6 requests */
Chuck Lever9f6ad262007-12-10 14:56:31 -0500314 switch (sap->sa_family) {
Chuck Leverd5b64432007-08-06 11:57:18 -0400315 case AF_INET:
Trond Myklebust803a9062008-07-01 15:20:55 -0400316 proc = rpcb_next_version[xprt->bind_index].rpc_proc;
317 bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
Chuck Leverd5b64432007-08-06 11:57:18 -0400318 break;
319 case AF_INET6:
Trond Myklebust803a9062008-07-01 15:20:55 -0400320 proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
321 bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
Chuck Leverd5b64432007-08-06 11:57:18 -0400322 break;
323 default:
324 status = -EAFNOSUPPORT;
325 dprintk("RPC: %5u %s: bad address family\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800326 task->tk_pid, __func__);
Chuck Leverd5b64432007-08-06 11:57:18 -0400327 goto bailout_nofree;
328 }
Trond Myklebust803a9062008-07-01 15:20:55 -0400329 if (proc == NULL) {
Chuck Levera5090502007-03-29 16:48:04 -0400330 xprt->bind_index = 0;
Chuck Lever906462a2007-09-11 18:00:47 -0400331 status = -EPFNOSUPPORT;
Chuck Lever45160d62007-07-01 12:13:17 -0400332 dprintk("RPC: %5u %s: no more getport versions available\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800333 task->tk_pid, __func__);
Chuck Levera5090502007-03-29 16:48:04 -0400334 goto bailout_nofree;
335 }
Chuck Levera5090502007-03-29 16:48:04 -0400336
Chuck Lever45160d62007-07-01 12:13:17 -0400337 dprintk("RPC: %5u %s: trying rpcbind version %u\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800338 task->tk_pid, __func__, bind_version);
Chuck Levera5090502007-03-29 16:48:04 -0400339
Chuck Lever9f6ad262007-12-10 14:56:31 -0500340 rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot,
Chuck Lever143b6c42007-08-16 16:03:31 -0400341 bind_version, 0);
342 if (IS_ERR(rpcb_clnt)) {
343 status = PTR_ERR(rpcb_clnt);
344 dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800345 task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
Chuck Lever143b6c42007-08-16 16:03:31 -0400346 goto bailout_nofree;
347 }
348
Chuck Levera5090502007-03-29 16:48:04 -0400349 map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
350 if (!map) {
351 status = -ENOMEM;
Chuck Lever45160d62007-07-01 12:13:17 -0400352 dprintk("RPC: %5u %s: no memory available\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800353 task->tk_pid, __func__);
Chuck Levera5090502007-03-29 16:48:04 -0400354 goto bailout_nofree;
355 }
356 map->r_prog = clnt->cl_prog;
357 map->r_vers = clnt->cl_vers;
358 map->r_prot = xprt->prot;
359 map->r_port = 0;
360 map->r_xprt = xprt_get(xprt);
Trond Myklebust86d61d82008-01-07 21:16:56 -0500361 map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
362 map->r_addr = rpc_peeraddr2str(rpcb_clnt, RPC_DISPLAY_UNIVERSAL_ADDR);
Chuck Levera5090502007-03-29 16:48:04 -0400363 map->r_owner = RPCB_OWNER_STRING; /* ignored for GETADDR */
364
Trond Myklebust803a9062008-07-01 15:20:55 -0400365 child = rpcb_call_async(rpcb_clnt, map, proc);
Trond Myklebust4c402b42007-06-14 16:40:32 -0400366 rpc_release_client(rpcb_clnt);
Chuck Levera5090502007-03-29 16:48:04 -0400367 if (IS_ERR(child)) {
368 status = -EIO;
Trond Myklebust0d3a34b2008-07-07 12:18:52 -0400369 /* rpcb_map_release() has freed the arguments */
Chuck Lever45160d62007-07-01 12:13:17 -0400370 dprintk("RPC: %5u %s: rpc_run_task failed\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800371 task->tk_pid, __func__);
Trond Myklebust0d3a34b2008-07-07 12:18:52 -0400372 goto bailout_nofree;
Chuck Levera5090502007-03-29 16:48:04 -0400373 }
374 rpc_put_task(child);
375
376 task->tk_xprt->stat.bind_count++;
377 return;
378
Chuck Levera5090502007-03-29 16:48:04 -0400379bailout_nofree:
380 rpcb_wake_rpcbind_waiters(xprt, status);
381bailout_nowake:
382 task->tk_status = status;
383}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400384EXPORT_SYMBOL_GPL(rpcb_getport_async);
Chuck Levera5090502007-03-29 16:48:04 -0400385
386/*
387 * Rpcbind child task calls this callback via tk_exit.
388 */
389static void rpcb_getport_done(struct rpc_task *child, void *data)
390{
391 struct rpcbind_args *map = data;
392 struct rpc_xprt *xprt = map->r_xprt;
393 int status = child->tk_status;
394
Chuck Lever4784cb52007-09-11 18:00:36 -0400395 /* Garbage reply: retry with a lesser rpcbind version */
396 if (status == -EIO)
397 status = -EPROTONOSUPPORT;
398
Chuck Levera5090502007-03-29 16:48:04 -0400399 /* rpcbind server doesn't support this rpcbind protocol version */
400 if (status == -EPROTONOSUPPORT)
401 xprt->bind_index++;
402
403 if (status < 0) {
404 /* rpcbind server not available on remote host? */
405 xprt->ops->set_port(xprt, 0);
406 } else if (map->r_port == 0) {
407 /* Requested RPC service wasn't registered on remote host */
408 xprt->ops->set_port(xprt, 0);
409 status = -EACCES;
410 } else {
411 /* Succeeded */
412 xprt->ops->set_port(xprt, map->r_port);
413 xprt_set_bound(xprt);
414 status = 0;
415 }
416
417 dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
418 child->tk_pid, status, map->r_port);
419
420 rpcb_wake_rpcbind_waiters(xprt, status);
421}
422
423static int rpcb_encode_mapping(struct rpc_rqst *req, __be32 *p,
424 struct rpcbind_args *rpcb)
425{
426 dprintk("RPC: rpcb_encode_mapping(%u, %u, %d, %u)\n",
427 rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
428 *p++ = htonl(rpcb->r_prog);
429 *p++ = htonl(rpcb->r_vers);
430 *p++ = htonl(rpcb->r_prot);
431 *p++ = htonl(rpcb->r_port);
432
433 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
434 return 0;
435}
436
437static int rpcb_decode_getport(struct rpc_rqst *req, __be32 *p,
438 unsigned short *portp)
439{
440 *portp = (unsigned short) ntohl(*p++);
441 dprintk("RPC: rpcb_decode_getport result %u\n",
442 *portp);
443 return 0;
444}
445
446static int rpcb_decode_set(struct rpc_rqst *req, __be32 *p,
447 unsigned int *boolp)
448{
449 *boolp = (unsigned int) ntohl(*p++);
450 dprintk("RPC: rpcb_decode_set result %u\n",
451 *boolp);
452 return 0;
453}
454
455static int rpcb_encode_getaddr(struct rpc_rqst *req, __be32 *p,
456 struct rpcbind_args *rpcb)
457{
458 dprintk("RPC: rpcb_encode_getaddr(%u, %u, %s)\n",
459 rpcb->r_prog, rpcb->r_vers, rpcb->r_addr);
460 *p++ = htonl(rpcb->r_prog);
461 *p++ = htonl(rpcb->r_vers);
462
463 p = xdr_encode_string(p, rpcb->r_netid);
464 p = xdr_encode_string(p, rpcb->r_addr);
465 p = xdr_encode_string(p, rpcb->r_owner);
466
467 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
468
469 return 0;
470}
471
472static int rpcb_decode_getaddr(struct rpc_rqst *req, __be32 *p,
473 unsigned short *portp)
474{
475 char *addr;
Chuck Leveradc24df2007-08-06 11:56:31 -0400476 u32 addr_len;
477 int c, i, f, first, val;
Chuck Levera5090502007-03-29 16:48:04 -0400478
479 *portp = 0;
Chuck Leveradc24df2007-08-06 11:56:31 -0400480 addr_len = ntohl(*p++);
Chuck Levera5090502007-03-29 16:48:04 -0400481
Chuck Levere65fe392007-09-11 18:00:31 -0400482 /*
483 * Simple sanity check. The smallest possible universal
484 * address is an IPv4 address string containing 11 bytes.
485 */
Chuck Lever0fb2b7e2007-12-10 14:56:46 -0500486 if (addr_len < 11 || addr_len > RPCBIND_MAXUADDRLEN)
Chuck Levere65fe392007-09-11 18:00:31 -0400487 goto out_err;
Chuck Levera5090502007-03-29 16:48:04 -0400488
Chuck Levere65fe392007-09-11 18:00:31 -0400489 /*
490 * Start at the end and walk backwards until the first dot
491 * is encountered. When the second dot is found, we have
492 * both parts of the port number.
493 */
Chuck Levera5090502007-03-29 16:48:04 -0400494 addr = (char *)p;
495 val = 0;
496 first = 1;
497 f = 1;
498 for (i = addr_len - 1; i > 0; i--) {
499 c = addr[i];
500 if (c >= '0' && c <= '9') {
501 val += (c - '0') * f;
502 f *= 10;
503 } else if (c == '.') {
504 if (first) {
505 *portp = val;
506 val = first = 0;
507 f = 1;
508 } else {
509 *portp |= (val << 8);
510 break;
511 }
512 }
513 }
514
Chuck Levere65fe392007-09-11 18:00:31 -0400515 /*
516 * Simple sanity check. If we never saw a dot in the reply,
517 * then this was probably just garbage.
518 */
519 if (first)
520 goto out_err;
521
Chuck Levera5090502007-03-29 16:48:04 -0400522 dprintk("RPC: rpcb_decode_getaddr port=%u\n", *portp);
523 return 0;
Chuck Levere65fe392007-09-11 18:00:31 -0400524
525out_err:
526 dprintk("RPC: rpcbind server returned malformed reply\n");
527 return -EIO;
Chuck Levera5090502007-03-29 16:48:04 -0400528}
529
530#define RPCB_program_sz (1u)
531#define RPCB_version_sz (1u)
532#define RPCB_protocol_sz (1u)
533#define RPCB_port_sz (1u)
534#define RPCB_boolean_sz (1u)
535
\"Talpey, Thomas\4f40ee42007-09-10 13:42:38 -0400536#define RPCB_netid_sz (1+XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
Chuck Lever0fb2b7e2007-12-10 14:56:46 -0500537#define RPCB_addr_sz (1+XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
Chuck Levera5090502007-03-29 16:48:04 -0400538#define RPCB_ownerstring_sz (1+XDR_QUADLEN(RPCB_MAXOWNERLEN))
539
540#define RPCB_mappingargs_sz RPCB_program_sz+RPCB_version_sz+ \
541 RPCB_protocol_sz+RPCB_port_sz
542#define RPCB_getaddrargs_sz RPCB_program_sz+RPCB_version_sz+ \
543 RPCB_netid_sz+RPCB_addr_sz+ \
544 RPCB_ownerstring_sz
545
546#define RPCB_setres_sz RPCB_boolean_sz
547#define RPCB_getportres_sz RPCB_port_sz
548
549/*
550 * Note that RFC 1833 does not put any size restrictions on the
551 * address string returned by the remote rpcbind database.
552 */
553#define RPCB_getaddrres_sz RPCB_addr_sz
554
555#define PROC(proc, argtype, restype) \
556 [RPCBPROC_##proc] = { \
557 .p_proc = RPCBPROC_##proc, \
558 .p_encode = (kxdrproc_t) rpcb_encode_##argtype, \
559 .p_decode = (kxdrproc_t) rpcb_decode_##restype, \
560 .p_arglen = RPCB_##argtype##args_sz, \
561 .p_replen = RPCB_##restype##res_sz, \
562 .p_statidx = RPCBPROC_##proc, \
563 .p_timer = 0, \
564 .p_name = #proc, \
565 }
566
567/*
568 * Not all rpcbind procedures described in RFC 1833 are implemented
569 * since the Linux kernel RPC code requires only these.
570 */
571static struct rpc_procinfo rpcb_procedures2[] = {
572 PROC(SET, mapping, set),
573 PROC(UNSET, mapping, set),
574 PROC(GETADDR, mapping, getport),
575};
576
577static struct rpc_procinfo rpcb_procedures3[] = {
578 PROC(SET, mapping, set),
579 PROC(UNSET, mapping, set),
580 PROC(GETADDR, getaddr, getaddr),
581};
582
583static struct rpc_procinfo rpcb_procedures4[] = {
584 PROC(SET, mapping, set),
585 PROC(UNSET, mapping, set),
586 PROC(GETVERSADDR, getaddr, getaddr),
587};
588
589static struct rpcb_info rpcb_next_version[] = {
590#ifdef CONFIG_SUNRPC_BIND34
591 { 4, &rpcb_procedures4[RPCBPROC_GETVERSADDR] },
592 { 3, &rpcb_procedures3[RPCBPROC_GETADDR] },
593#endif
594 { 2, &rpcb_procedures2[RPCBPROC_GETPORT] },
595 { 0, NULL },
596};
597
Chuck Leverd5b64432007-08-06 11:57:18 -0400598static struct rpcb_info rpcb_next_version6[] = {
599#ifdef CONFIG_SUNRPC_BIND34
600 { 4, &rpcb_procedures4[RPCBPROC_GETVERSADDR] },
601 { 3, &rpcb_procedures3[RPCBPROC_GETADDR] },
602#endif
603 { 0, NULL },
604};
605
Chuck Levera5090502007-03-29 16:48:04 -0400606static struct rpc_version rpcb_version2 = {
607 .number = 2,
608 .nrprocs = RPCB_HIGHPROC_2,
609 .procs = rpcb_procedures2
610};
611
612static struct rpc_version rpcb_version3 = {
613 .number = 3,
614 .nrprocs = RPCB_HIGHPROC_3,
615 .procs = rpcb_procedures3
616};
617
618static struct rpc_version rpcb_version4 = {
619 .number = 4,
620 .nrprocs = RPCB_HIGHPROC_4,
621 .procs = rpcb_procedures4
622};
623
624static struct rpc_version *rpcb_version[] = {
625 NULL,
626 NULL,
627 &rpcb_version2,
628 &rpcb_version3,
629 &rpcb_version4
630};
631
632static struct rpc_stat rpcb_stats;
633
Adrian Bunk7f4adef2007-06-13 01:03:13 +0200634static struct rpc_program rpcb_program = {
Chuck Levera5090502007-03-29 16:48:04 -0400635 .name = "rpcbind",
636 .number = RPCBIND_PROGRAM,
637 .nrvers = ARRAY_SIZE(rpcb_version),
638 .version = rpcb_version,
639 .stats = &rpcb_stats,
640};