blob: c1310f7962248eff81b5bb25ebee42150199794e [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\0896a7252007-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/*
58 * r_addr
59 *
60 * Quoting RFC 3530, section 2.2:
61 *
62 * For TCP over IPv4 and for UDP over IPv4, the format of r_addr is the
63 * US-ASCII string:
64 *
65 * h1.h2.h3.h4.p1.p2
66 *
67 * The prefix, "h1.h2.h3.h4", is the standard textual form for
68 * representing an IPv4 address, which is always four octets long.
69 * Assuming big-endian ordering, h1, h2, h3, and h4, are respectively,
70 * the first through fourth octets each converted to ASCII-decimal.
71 * Assuming big-endian ordering, p1 and p2 are, respectively, the first
72 * and second octets each converted to ASCII-decimal. For example, if a
73 * host, in big-endian order, has an address of 0x0A010307 and there is
74 * a service listening on, in big endian order, port 0x020F (decimal
75 * 527), then the complete universal address is "10.1.3.7.2.15".
76 *
77 * ...
78 *
79 * For TCP over IPv6 and for UDP over IPv6, the format of r_addr is the
80 * US-ASCII string:
81 *
82 * x1:x2:x3:x4:x5:x6:x7:x8.p1.p2
83 *
84 * The suffix "p1.p2" is the service port, and is computed the same way
85 * as with universal addresses for TCP and UDP over IPv4. The prefix,
86 * "x1:x2:x3:x4:x5:x6:x7:x8", is the standard textual form for
87 * representing an IPv6 address as defined in Section 2.2 of [RFC2373].
88 * Additionally, the two alternative forms specified in Section 2.2 of
89 * [RFC2373] are also acceptable.
90 *
91 * XXX: Currently this implementation does not explicitly convert the
92 * stored address to US-ASCII on non-ASCII systems.
93 */
94#define RPCB_MAXADDRLEN (128u)
95
96/*
Chuck Levera5090502007-03-29 16:48:04 -040097 * r_owner
98 *
99 * The "owner" is allowed to unset a service in the rpcbind database.
100 * We always use the following (arbitrary) fixed string.
101 */
102#define RPCB_OWNER_STRING "rpcb"
103#define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
104
105static void rpcb_getport_done(struct rpc_task *, void *);
Adrian Bunk7f4adef2007-06-13 01:03:13 +0200106static struct rpc_program rpcb_program;
Chuck Levera5090502007-03-29 16:48:04 -0400107
108struct rpcbind_args {
109 struct rpc_xprt * r_xprt;
110
111 u32 r_prog;
112 u32 r_vers;
113 u32 r_prot;
114 unsigned short r_port;
115 char * r_netid;
116 char r_addr[RPCB_MAXADDRLEN];
117 char * r_owner;
118};
119
120static struct rpc_procinfo rpcb_procedures2[];
121static struct rpc_procinfo rpcb_procedures3[];
122
Chuck Leverd5b64432007-08-06 11:57:18 -0400123struct rpcb_info {
Chuck Levera5090502007-03-29 16:48:04 -0400124 int rpc_vers;
125 struct rpc_procinfo * rpc_proc;
Chuck Leverd5b64432007-08-06 11:57:18 -0400126};
127
128static struct rpcb_info rpcb_next_version[];
129static struct rpcb_info rpcb_next_version6[];
Chuck Levera5090502007-03-29 16:48:04 -0400130
Chuck Levera5090502007-03-29 16:48:04 -0400131static void rpcb_map_release(void *data)
132{
133 struct rpcbind_args *map = data;
134
135 xprt_put(map->r_xprt);
136 kfree(map);
137}
138
139static const struct rpc_call_ops rpcb_getport_ops = {
Chuck Levera5090502007-03-29 16:48:04 -0400140 .rpc_call_done = rpcb_getport_done,
141 .rpc_release = rpcb_map_release,
142};
143
144static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status)
145{
146 xprt_clear_binding(xprt);
147 rpc_wake_up_status(&xprt->binding, status);
148}
149
150static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
Chuck Lever9f6ad262007-12-10 14:56:31 -0500151 size_t salen, int proto, int version,
152 int privileged)
Chuck Levera5090502007-03-29 16:48:04 -0400153{
154 struct rpc_create_args args = {
155 .protocol = proto,
156 .address = srvaddr,
Chuck Lever9f6ad262007-12-10 14:56:31 -0500157 .addrsize = salen,
Chuck Levera5090502007-03-29 16:48:04 -0400158 .servername = hostname,
159 .program = &rpcb_program,
160 .version = version,
161 .authflavor = RPC_AUTH_UNIX,
Chuck Leverf7fb5582007-07-01 12:13:07 -0400162 .flags = (RPC_CLNT_CREATE_NOPING |
163 RPC_CLNT_CREATE_INTR),
Chuck Levera5090502007-03-29 16:48:04 -0400164 };
165
Chuck Leverd5b64432007-08-06 11:57:18 -0400166 switch (srvaddr->sa_family) {
167 case AF_INET:
168 ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
169 break;
170 case AF_INET6:
171 ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
172 break;
173 default:
174 return NULL;
175 }
176
Chuck Levera5090502007-03-29 16:48:04 -0400177 if (!privileged)
178 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
179 return rpc_create(&args);
180}
181
182/**
183 * rpcb_register - set or unset a port registration with the local rpcbind svc
184 * @prog: RPC program number to bind
185 * @vers: RPC version number to bind
186 * @prot: transport protocol to use to make this request
187 * @port: port value to register
188 * @okay: result code
189 *
190 * port == 0 means unregister, port != 0 means register.
191 *
192 * This routine supports only rpcbind version 2.
193 */
194int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
195{
196 struct sockaddr_in sin = {
197 .sin_family = AF_INET,
198 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
199 };
200 struct rpcbind_args map = {
201 .r_prog = prog,
202 .r_vers = vers,
203 .r_prot = prot,
204 .r_port = port,
205 };
206 struct rpc_message msg = {
207 .rpc_proc = &rpcb_procedures2[port ?
208 RPCBPROC_SET : RPCBPROC_UNSET],
209 .rpc_argp = &map,
210 .rpc_resp = okay,
211 };
212 struct rpc_clnt *rpcb_clnt;
213 int error = 0;
214
215 dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
216 "rpcbind\n", (port ? "" : "un"),
217 prog, vers, prot, port);
218
219 rpcb_clnt = rpcb_create("localhost", (struct sockaddr *) &sin,
Chuck Lever9f6ad262007-12-10 14:56:31 -0500220 sizeof(sin), XPRT_TRANSPORT_UDP, 2, 1);
Chuck Levera5090502007-03-29 16:48:04 -0400221 if (IS_ERR(rpcb_clnt))
222 return PTR_ERR(rpcb_clnt);
223
224 error = rpc_call_sync(rpcb_clnt, &msg, 0);
225
Trond Myklebust90c57552007-06-09 19:49:36 -0400226 rpc_shutdown_client(rpcb_clnt);
Chuck Levera5090502007-03-29 16:48:04 -0400227 if (error < 0)
228 printk(KERN_WARNING "RPC: failed to contact local rpcbind "
229 "server (errno %d).\n", -error);
230 dprintk("RPC: registration status %d/%d\n", error, *okay);
231
232 return error;
233}
234
Chuck Levera5090502007-03-29 16:48:04 -0400235/**
Chuck Levercce63cd2007-07-01 12:13:12 -0400236 * rpcb_getport_sync - obtain the port for an RPC service on a given host
Chuck Levera5090502007-03-29 16:48:04 -0400237 * @sin: address of remote peer
238 * @prog: RPC program number to bind
239 * @vers: RPC version number to bind
240 * @prot: transport protocol to use to make this request
241 *
242 * Called from outside the RPC client in a synchronous task context.
Chuck Levercce63cd2007-07-01 12:13:12 -0400243 * Uses default timeout parameters specified by underlying transport.
Chuck Levera5090502007-03-29 16:48:04 -0400244 *
Chuck Levercce63cd2007-07-01 12:13:12 -0400245 * XXX: Needs to support IPv6, and rpcbind versions 3 and 4
Chuck Levera5090502007-03-29 16:48:04 -0400246 */
Chuck Levercce63cd2007-07-01 12:13:12 -0400247int rpcb_getport_sync(struct sockaddr_in *sin, __u32 prog,
248 __u32 vers, int prot)
Chuck Levera5090502007-03-29 16:48:04 -0400249{
250 struct rpcbind_args map = {
251 .r_prog = prog,
252 .r_vers = vers,
253 .r_prot = prot,
254 .r_port = 0,
255 };
256 struct rpc_message msg = {
257 .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
258 .rpc_argp = &map,
259 .rpc_resp = &map.r_port,
260 };
261 struct rpc_clnt *rpcb_clnt;
262 char hostname[40];
263 int status;
264
Chuck Levercce63cd2007-07-01 12:13:12 -0400265 dprintk("RPC: %s(" NIPQUAD_FMT ", %u, %u, %d)\n",
266 __FUNCTION__, NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
Chuck Levera5090502007-03-29 16:48:04 -0400267
Chuck Levercce63cd2007-07-01 12:13:12 -0400268 sprintf(hostname, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
Chuck Lever9f6ad262007-12-10 14:56:31 -0500269 rpcb_clnt = rpcb_create(hostname, (struct sockaddr *)sin,
270 sizeof(sin), prot, 2, 0);
Chuck Levera5090502007-03-29 16:48:04 -0400271 if (IS_ERR(rpcb_clnt))
272 return PTR_ERR(rpcb_clnt);
273
274 status = rpc_call_sync(rpcb_clnt, &msg, 0);
Trond Myklebust90c57552007-06-09 19:49:36 -0400275 rpc_shutdown_client(rpcb_clnt);
Chuck Levera5090502007-03-29 16:48:04 -0400276
277 if (status >= 0) {
278 if (map.r_port != 0)
279 return map.r_port;
280 status = -EACCES;
281 }
282 return status;
283}
Chuck Levercce63cd2007-07-01 12:13:12 -0400284EXPORT_SYMBOL_GPL(rpcb_getport_sync);
Chuck Levera5090502007-03-29 16:48:04 -0400285
Trond Myklebust5138fde2007-07-14 15:40:01 -0400286static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt, struct rpcbind_args *map, int version)
287{
288 struct rpc_message msg = {
289 .rpc_proc = rpcb_next_version[version].rpc_proc,
290 .rpc_argp = map,
291 .rpc_resp = &map->r_port,
292 };
293 struct rpc_task_setup task_setup_data = {
294 .rpc_client = rpcb_clnt,
295 .rpc_message = &msg,
296 .callback_ops = &rpcb_getport_ops,
297 .callback_data = map,
298 .flags = RPC_TASK_ASYNC,
299 };
300
301 return rpc_run_task(&task_setup_data);
302}
303
Chuck Levera5090502007-03-29 16:48:04 -0400304/**
Chuck Lever45160d62007-07-01 12:13:17 -0400305 * rpcb_getport_async - obtain the port for a given RPC service on a given host
Chuck Levera5090502007-03-29 16:48:04 -0400306 * @task: task that is waiting for portmapper request
307 *
308 * This one can be called for an ongoing RPC request, and can be used in
309 * an async (rpciod) context.
310 */
Chuck Lever45160d62007-07-01 12:13:17 -0400311void rpcb_getport_async(struct rpc_task *task)
Chuck Levera5090502007-03-29 16:48:04 -0400312{
313 struct rpc_clnt *clnt = task->tk_client;
314 int bind_version;
315 struct rpc_xprt *xprt = task->tk_xprt;
316 struct rpc_clnt *rpcb_clnt;
317 static struct rpcbind_args *map;
318 struct rpc_task *child;
Chuck Lever9f6ad262007-12-10 14:56:31 -0500319 struct sockaddr_storage addr;
320 struct sockaddr *sap = (struct sockaddr *)&addr;
321 size_t salen;
Chuck Levera5090502007-03-29 16:48:04 -0400322 int status;
Chuck Leverd5b64432007-08-06 11:57:18 -0400323 struct rpcb_info *info;
Chuck Levera5090502007-03-29 16:48:04 -0400324
Chuck Lever45160d62007-07-01 12:13:17 -0400325 dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
326 task->tk_pid, __FUNCTION__,
327 clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot);
Chuck Levera5090502007-03-29 16:48:04 -0400328
329 /* Autobind on cloned rpc clients is discouraged */
330 BUG_ON(clnt->cl_parent != clnt);
331
332 if (xprt_test_and_set_binding(xprt)) {
Chuck Lever2429cbf2007-09-11 18:00:41 -0400333 status = -EAGAIN; /* tell caller to check again */
Chuck Lever45160d62007-07-01 12:13:17 -0400334 dprintk("RPC: %5u %s: waiting for another binder\n",
335 task->tk_pid, __FUNCTION__);
Chuck Levera5090502007-03-29 16:48:04 -0400336 goto bailout_nowake;
337 }
338
339 /* Put self on queue before sending rpcbind request, in case
340 * rpcb_getport_done completes before we return from rpc_run_task */
341 rpc_sleep_on(&xprt->binding, task, NULL, NULL);
342
343 /* Someone else may have bound if we slept */
344 if (xprt_bound(xprt)) {
345 status = 0;
Chuck Lever45160d62007-07-01 12:13:17 -0400346 dprintk("RPC: %5u %s: already bound\n",
347 task->tk_pid, __FUNCTION__);
Chuck Levera5090502007-03-29 16:48:04 -0400348 goto bailout_nofree;
349 }
350
Chuck Lever9f6ad262007-12-10 14:56:31 -0500351 salen = rpc_peeraddr(clnt, sap, sizeof(addr));
Chuck Leverd5b64432007-08-06 11:57:18 -0400352
353 /* Don't ever use rpcbind v2 for AF_INET6 requests */
Chuck Lever9f6ad262007-12-10 14:56:31 -0500354 switch (sap->sa_family) {
Chuck Leverd5b64432007-08-06 11:57:18 -0400355 case AF_INET:
356 info = rpcb_next_version;
357 break;
358 case AF_INET6:
359 info = rpcb_next_version6;
360 break;
361 default:
362 status = -EAFNOSUPPORT;
363 dprintk("RPC: %5u %s: bad address family\n",
364 task->tk_pid, __FUNCTION__);
365 goto bailout_nofree;
366 }
367 if (info[xprt->bind_index].rpc_proc == NULL) {
Chuck Levera5090502007-03-29 16:48:04 -0400368 xprt->bind_index = 0;
Chuck Lever906462a2007-09-11 18:00:47 -0400369 status = -EPFNOSUPPORT;
Chuck Lever45160d62007-07-01 12:13:17 -0400370 dprintk("RPC: %5u %s: no more getport versions available\n",
371 task->tk_pid, __FUNCTION__);
Chuck Levera5090502007-03-29 16:48:04 -0400372 goto bailout_nofree;
373 }
Chuck Leverd5b64432007-08-06 11:57:18 -0400374 bind_version = info[xprt->bind_index].rpc_vers;
Chuck Levera5090502007-03-29 16:48:04 -0400375
Chuck Lever45160d62007-07-01 12:13:17 -0400376 dprintk("RPC: %5u %s: trying rpcbind version %u\n",
377 task->tk_pid, __FUNCTION__, bind_version);
Chuck Levera5090502007-03-29 16:48:04 -0400378
Chuck Lever9f6ad262007-12-10 14:56:31 -0500379 rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot,
Chuck Lever143b6c42007-08-16 16:03:31 -0400380 bind_version, 0);
381 if (IS_ERR(rpcb_clnt)) {
382 status = PTR_ERR(rpcb_clnt);
383 dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
384 task->tk_pid, __FUNCTION__, PTR_ERR(rpcb_clnt));
385 goto bailout_nofree;
386 }
387
Chuck Levera5090502007-03-29 16:48:04 -0400388 map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
389 if (!map) {
390 status = -ENOMEM;
Chuck Lever45160d62007-07-01 12:13:17 -0400391 dprintk("RPC: %5u %s: no memory available\n",
392 task->tk_pid, __FUNCTION__);
Chuck Levera5090502007-03-29 16:48:04 -0400393 goto bailout_nofree;
394 }
395 map->r_prog = clnt->cl_prog;
396 map->r_vers = clnt->cl_vers;
397 map->r_prot = xprt->prot;
398 map->r_port = 0;
399 map->r_xprt = xprt_get(xprt);
\"Talpey, Thomas\4417c8c2007-09-10 13:43:05 -0400400 map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
Chuck Lever6d0aa062007-09-11 18:00:15 -0400401 memcpy(map->r_addr,
Chuck Lever143b6c42007-08-16 16:03:31 -0400402 rpc_peeraddr2str(rpcb_clnt, RPC_DISPLAY_UNIVERSAL_ADDR),
403 sizeof(map->r_addr));
Chuck Levera5090502007-03-29 16:48:04 -0400404 map->r_owner = RPCB_OWNER_STRING; /* ignored for GETADDR */
405
Trond Myklebust5138fde2007-07-14 15:40:01 -0400406 child = rpcb_call_async(rpcb_clnt, map, xprt->bind_index);
Trond Myklebust4c402b42007-06-14 16:40:32 -0400407 rpc_release_client(rpcb_clnt);
Chuck Levera5090502007-03-29 16:48:04 -0400408 if (IS_ERR(child)) {
409 status = -EIO;
Chuck Lever45160d62007-07-01 12:13:17 -0400410 dprintk("RPC: %5u %s: rpc_run_task failed\n",
411 task->tk_pid, __FUNCTION__);
Chuck Lever143b6c42007-08-16 16:03:31 -0400412 goto bailout;
Chuck Levera5090502007-03-29 16:48:04 -0400413 }
414 rpc_put_task(child);
415
416 task->tk_xprt->stat.bind_count++;
417 return;
418
419bailout:
420 kfree(map);
421 xprt_put(xprt);
422bailout_nofree:
423 rpcb_wake_rpcbind_waiters(xprt, status);
424bailout_nowake:
425 task->tk_status = status;
426}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400427EXPORT_SYMBOL_GPL(rpcb_getport_async);
Chuck Levera5090502007-03-29 16:48:04 -0400428
429/*
430 * Rpcbind child task calls this callback via tk_exit.
431 */
432static void rpcb_getport_done(struct rpc_task *child, void *data)
433{
434 struct rpcbind_args *map = data;
435 struct rpc_xprt *xprt = map->r_xprt;
436 int status = child->tk_status;
437
Chuck Lever4784cb52007-09-11 18:00:36 -0400438 /* Garbage reply: retry with a lesser rpcbind version */
439 if (status == -EIO)
440 status = -EPROTONOSUPPORT;
441
Chuck Levera5090502007-03-29 16:48:04 -0400442 /* rpcbind server doesn't support this rpcbind protocol version */
443 if (status == -EPROTONOSUPPORT)
444 xprt->bind_index++;
445
446 if (status < 0) {
447 /* rpcbind server not available on remote host? */
448 xprt->ops->set_port(xprt, 0);
449 } else if (map->r_port == 0) {
450 /* Requested RPC service wasn't registered on remote host */
451 xprt->ops->set_port(xprt, 0);
452 status = -EACCES;
453 } else {
454 /* Succeeded */
455 xprt->ops->set_port(xprt, map->r_port);
456 xprt_set_bound(xprt);
457 status = 0;
458 }
459
460 dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
461 child->tk_pid, status, map->r_port);
462
463 rpcb_wake_rpcbind_waiters(xprt, status);
464}
465
466static int rpcb_encode_mapping(struct rpc_rqst *req, __be32 *p,
467 struct rpcbind_args *rpcb)
468{
469 dprintk("RPC: rpcb_encode_mapping(%u, %u, %d, %u)\n",
470 rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
471 *p++ = htonl(rpcb->r_prog);
472 *p++ = htonl(rpcb->r_vers);
473 *p++ = htonl(rpcb->r_prot);
474 *p++ = htonl(rpcb->r_port);
475
476 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
477 return 0;
478}
479
480static int rpcb_decode_getport(struct rpc_rqst *req, __be32 *p,
481 unsigned short *portp)
482{
483 *portp = (unsigned short) ntohl(*p++);
484 dprintk("RPC: rpcb_decode_getport result %u\n",
485 *portp);
486 return 0;
487}
488
489static int rpcb_decode_set(struct rpc_rqst *req, __be32 *p,
490 unsigned int *boolp)
491{
492 *boolp = (unsigned int) ntohl(*p++);
493 dprintk("RPC: rpcb_decode_set result %u\n",
494 *boolp);
495 return 0;
496}
497
498static int rpcb_encode_getaddr(struct rpc_rqst *req, __be32 *p,
499 struct rpcbind_args *rpcb)
500{
501 dprintk("RPC: rpcb_encode_getaddr(%u, %u, %s)\n",
502 rpcb->r_prog, rpcb->r_vers, rpcb->r_addr);
503 *p++ = htonl(rpcb->r_prog);
504 *p++ = htonl(rpcb->r_vers);
505
506 p = xdr_encode_string(p, rpcb->r_netid);
507 p = xdr_encode_string(p, rpcb->r_addr);
508 p = xdr_encode_string(p, rpcb->r_owner);
509
510 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
511
512 return 0;
513}
514
515static int rpcb_decode_getaddr(struct rpc_rqst *req, __be32 *p,
516 unsigned short *portp)
517{
518 char *addr;
Chuck Leveradc24df82007-08-06 11:56:31 -0400519 u32 addr_len;
520 int c, i, f, first, val;
Chuck Levera5090502007-03-29 16:48:04 -0400521
522 *portp = 0;
Chuck Leveradc24df82007-08-06 11:56:31 -0400523 addr_len = ntohl(*p++);
Chuck Levera5090502007-03-29 16:48:04 -0400524
Chuck Levere65fe392007-09-11 18:00:31 -0400525 /*
526 * Simple sanity check. The smallest possible universal
527 * address is an IPv4 address string containing 11 bytes.
528 */
529 if (addr_len < 11 || addr_len > RPCB_MAXADDRLEN)
530 goto out_err;
Chuck Levera5090502007-03-29 16:48:04 -0400531
Chuck Levere65fe392007-09-11 18:00:31 -0400532 /*
533 * Start at the end and walk backwards until the first dot
534 * is encountered. When the second dot is found, we have
535 * both parts of the port number.
536 */
Chuck Levera5090502007-03-29 16:48:04 -0400537 addr = (char *)p;
538 val = 0;
539 first = 1;
540 f = 1;
541 for (i = addr_len - 1; i > 0; i--) {
542 c = addr[i];
543 if (c >= '0' && c <= '9') {
544 val += (c - '0') * f;
545 f *= 10;
546 } else if (c == '.') {
547 if (first) {
548 *portp = val;
549 val = first = 0;
550 f = 1;
551 } else {
552 *portp |= (val << 8);
553 break;
554 }
555 }
556 }
557
Chuck Levere65fe392007-09-11 18:00:31 -0400558 /*
559 * Simple sanity check. If we never saw a dot in the reply,
560 * then this was probably just garbage.
561 */
562 if (first)
563 goto out_err;
564
Chuck Levera5090502007-03-29 16:48:04 -0400565 dprintk("RPC: rpcb_decode_getaddr port=%u\n", *portp);
566 return 0;
Chuck Levere65fe392007-09-11 18:00:31 -0400567
568out_err:
569 dprintk("RPC: rpcbind server returned malformed reply\n");
570 return -EIO;
Chuck Levera5090502007-03-29 16:48:04 -0400571}
572
573#define RPCB_program_sz (1u)
574#define RPCB_version_sz (1u)
575#define RPCB_protocol_sz (1u)
576#define RPCB_port_sz (1u)
577#define RPCB_boolean_sz (1u)
578
\"Talpey, Thomas\4f40ee42007-09-10 13:42:38 -0400579#define RPCB_netid_sz (1+XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
Chuck Levera5090502007-03-29 16:48:04 -0400580#define RPCB_addr_sz (1+XDR_QUADLEN(RPCB_MAXADDRLEN))
581#define RPCB_ownerstring_sz (1+XDR_QUADLEN(RPCB_MAXOWNERLEN))
582
583#define RPCB_mappingargs_sz RPCB_program_sz+RPCB_version_sz+ \
584 RPCB_protocol_sz+RPCB_port_sz
585#define RPCB_getaddrargs_sz RPCB_program_sz+RPCB_version_sz+ \
586 RPCB_netid_sz+RPCB_addr_sz+ \
587 RPCB_ownerstring_sz
588
589#define RPCB_setres_sz RPCB_boolean_sz
590#define RPCB_getportres_sz RPCB_port_sz
591
592/*
593 * Note that RFC 1833 does not put any size restrictions on the
594 * address string returned by the remote rpcbind database.
595 */
596#define RPCB_getaddrres_sz RPCB_addr_sz
597
598#define PROC(proc, argtype, restype) \
599 [RPCBPROC_##proc] = { \
600 .p_proc = RPCBPROC_##proc, \
601 .p_encode = (kxdrproc_t) rpcb_encode_##argtype, \
602 .p_decode = (kxdrproc_t) rpcb_decode_##restype, \
603 .p_arglen = RPCB_##argtype##args_sz, \
604 .p_replen = RPCB_##restype##res_sz, \
605 .p_statidx = RPCBPROC_##proc, \
606 .p_timer = 0, \
607 .p_name = #proc, \
608 }
609
610/*
611 * Not all rpcbind procedures described in RFC 1833 are implemented
612 * since the Linux kernel RPC code requires only these.
613 */
614static struct rpc_procinfo rpcb_procedures2[] = {
615 PROC(SET, mapping, set),
616 PROC(UNSET, mapping, set),
617 PROC(GETADDR, mapping, getport),
618};
619
620static struct rpc_procinfo rpcb_procedures3[] = {
621 PROC(SET, mapping, set),
622 PROC(UNSET, mapping, set),
623 PROC(GETADDR, getaddr, getaddr),
624};
625
626static struct rpc_procinfo rpcb_procedures4[] = {
627 PROC(SET, mapping, set),
628 PROC(UNSET, mapping, set),
629 PROC(GETVERSADDR, getaddr, getaddr),
630};
631
632static struct rpcb_info rpcb_next_version[] = {
633#ifdef CONFIG_SUNRPC_BIND34
634 { 4, &rpcb_procedures4[RPCBPROC_GETVERSADDR] },
635 { 3, &rpcb_procedures3[RPCBPROC_GETADDR] },
636#endif
637 { 2, &rpcb_procedures2[RPCBPROC_GETPORT] },
638 { 0, NULL },
639};
640
Chuck Leverd5b64432007-08-06 11:57:18 -0400641static struct rpcb_info rpcb_next_version6[] = {
642#ifdef CONFIG_SUNRPC_BIND34
643 { 4, &rpcb_procedures4[RPCBPROC_GETVERSADDR] },
644 { 3, &rpcb_procedures3[RPCBPROC_GETADDR] },
645#endif
646 { 0, NULL },
647};
648
Chuck Levera5090502007-03-29 16:48:04 -0400649static struct rpc_version rpcb_version2 = {
650 .number = 2,
651 .nrprocs = RPCB_HIGHPROC_2,
652 .procs = rpcb_procedures2
653};
654
655static struct rpc_version rpcb_version3 = {
656 .number = 3,
657 .nrprocs = RPCB_HIGHPROC_3,
658 .procs = rpcb_procedures3
659};
660
661static struct rpc_version rpcb_version4 = {
662 .number = 4,
663 .nrprocs = RPCB_HIGHPROC_4,
664 .procs = rpcb_procedures4
665};
666
667static struct rpc_version *rpcb_version[] = {
668 NULL,
669 NULL,
670 &rpcb_version2,
671 &rpcb_version3,
672 &rpcb_version4
673};
674
675static struct rpc_stat rpcb_stats;
676
Adrian Bunk7f4adef2007-06-13 01:03:13 +0200677static struct rpc_program rpcb_program = {
Chuck Levera5090502007-03-29 16:48:04 -0400678 .name = "rpcbind",
679 .number = RPCBIND_PROGRAM,
680 .nrvers = ARRAY_SIZE(rpcb_version),
681 .version = rpcb_version,
682 .stats = &rpcb_stats,
683};