blob: f876e37d1972d66fdce66d55ee6387675742a8d2 [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/*
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,
151 int proto, int version, int privileged)
152{
153 struct rpc_create_args args = {
154 .protocol = proto,
155 .address = srvaddr,
156 .addrsize = sizeof(struct sockaddr_in),
157 .servername = hostname,
158 .program = &rpcb_program,
159 .version = version,
160 .authflavor = RPC_AUTH_UNIX,
Chuck Leverf7fb5582007-07-01 12:13:07 -0400161 .flags = (RPC_CLNT_CREATE_NOPING |
162 RPC_CLNT_CREATE_INTR),
Chuck Levera5090502007-03-29 16:48:04 -0400163 };
164
Chuck Leverd5b64432007-08-06 11:57:18 -0400165 switch (srvaddr->sa_family) {
166 case AF_INET:
167 ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
168 break;
169 case AF_INET6:
170 ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
171 break;
172 default:
173 return NULL;
174 }
175
Chuck Levera5090502007-03-29 16:48:04 -0400176 if (!privileged)
177 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
178 return rpc_create(&args);
179}
180
181/**
182 * rpcb_register - set or unset a port registration with the local rpcbind svc
183 * @prog: RPC program number to bind
184 * @vers: RPC version number to bind
185 * @prot: transport protocol to use to make this request
186 * @port: port value to register
187 * @okay: result code
188 *
189 * port == 0 means unregister, port != 0 means register.
190 *
191 * This routine supports only rpcbind version 2.
192 */
193int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
194{
195 struct sockaddr_in sin = {
196 .sin_family = AF_INET,
197 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
198 };
199 struct rpcbind_args map = {
200 .r_prog = prog,
201 .r_vers = vers,
202 .r_prot = prot,
203 .r_port = port,
204 };
205 struct rpc_message msg = {
206 .rpc_proc = &rpcb_procedures2[port ?
207 RPCBPROC_SET : RPCBPROC_UNSET],
208 .rpc_argp = &map,
209 .rpc_resp = okay,
210 };
211 struct rpc_clnt *rpcb_clnt;
212 int error = 0;
213
214 dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
215 "rpcbind\n", (port ? "" : "un"),
216 prog, vers, prot, port);
217
218 rpcb_clnt = rpcb_create("localhost", (struct sockaddr *) &sin,
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400219 XPRT_TRANSPORT_UDP, 2, 1);
Chuck Levera5090502007-03-29 16:48:04 -0400220 if (IS_ERR(rpcb_clnt))
221 return PTR_ERR(rpcb_clnt);
222
223 error = rpc_call_sync(rpcb_clnt, &msg, 0);
224
Trond Myklebust90c57552007-06-09 19:49:36 -0400225 rpc_shutdown_client(rpcb_clnt);
Chuck Levera5090502007-03-29 16:48:04 -0400226 if (error < 0)
227 printk(KERN_WARNING "RPC: failed to contact local rpcbind "
228 "server (errno %d).\n", -error);
229 dprintk("RPC: registration status %d/%d\n", error, *okay);
230
231 return error;
232}
233
Chuck Levera5090502007-03-29 16:48:04 -0400234/**
Chuck Levercce63cd2007-07-01 12:13:12 -0400235 * rpcb_getport_sync - obtain the port for an RPC service on a given host
Chuck Levera5090502007-03-29 16:48:04 -0400236 * @sin: address of remote peer
237 * @prog: RPC program number to bind
238 * @vers: RPC version number to bind
239 * @prot: transport protocol to use to make this request
240 *
241 * Called from outside the RPC client in a synchronous task context.
Chuck Levercce63cd2007-07-01 12:13:12 -0400242 * Uses default timeout parameters specified by underlying transport.
Chuck Levera5090502007-03-29 16:48:04 -0400243 *
Chuck Levercce63cd2007-07-01 12:13:12 -0400244 * XXX: Needs to support IPv6, and rpcbind versions 3 and 4
Chuck Levera5090502007-03-29 16:48:04 -0400245 */
Chuck Levercce63cd2007-07-01 12:13:12 -0400246int rpcb_getport_sync(struct sockaddr_in *sin, __u32 prog,
247 __u32 vers, int prot)
Chuck Levera5090502007-03-29 16:48:04 -0400248{
249 struct rpcbind_args map = {
250 .r_prog = prog,
251 .r_vers = vers,
252 .r_prot = prot,
253 .r_port = 0,
254 };
255 struct rpc_message msg = {
256 .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
257 .rpc_argp = &map,
258 .rpc_resp = &map.r_port,
259 };
260 struct rpc_clnt *rpcb_clnt;
261 char hostname[40];
262 int status;
263
Chuck Levercce63cd2007-07-01 12:13:12 -0400264 dprintk("RPC: %s(" NIPQUAD_FMT ", %u, %u, %d)\n",
265 __FUNCTION__, NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
Chuck Levera5090502007-03-29 16:48:04 -0400266
Chuck Levercce63cd2007-07-01 12:13:12 -0400267 sprintf(hostname, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
Chuck Levera5090502007-03-29 16:48:04 -0400268 rpcb_clnt = rpcb_create(hostname, (struct sockaddr *)sin, prot, 2, 0);
269 if (IS_ERR(rpcb_clnt))
270 return PTR_ERR(rpcb_clnt);
271
272 status = rpc_call_sync(rpcb_clnt, &msg, 0);
Trond Myklebust90c57552007-06-09 19:49:36 -0400273 rpc_shutdown_client(rpcb_clnt);
Chuck Levera5090502007-03-29 16:48:04 -0400274
275 if (status >= 0) {
276 if (map.r_port != 0)
277 return map.r_port;
278 status = -EACCES;
279 }
280 return status;
281}
Chuck Levercce63cd2007-07-01 12:13:12 -0400282EXPORT_SYMBOL_GPL(rpcb_getport_sync);
Chuck Levera5090502007-03-29 16:48:04 -0400283
Trond Myklebust5138fde2007-07-14 15:40:01 -0400284static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt, struct rpcbind_args *map, int version)
285{
286 struct rpc_message msg = {
287 .rpc_proc = rpcb_next_version[version].rpc_proc,
288 .rpc_argp = map,
289 .rpc_resp = &map->r_port,
290 };
291 struct rpc_task_setup task_setup_data = {
292 .rpc_client = rpcb_clnt,
293 .rpc_message = &msg,
294 .callback_ops = &rpcb_getport_ops,
295 .callback_data = map,
296 .flags = RPC_TASK_ASYNC,
297 };
298
299 return rpc_run_task(&task_setup_data);
300}
301
Chuck Levera5090502007-03-29 16:48:04 -0400302/**
Chuck Lever45160d62007-07-01 12:13:17 -0400303 * rpcb_getport_async - obtain the port for a given RPC service on a given host
Chuck Levera5090502007-03-29 16:48:04 -0400304 * @task: task that is waiting for portmapper request
305 *
306 * This one can be called for an ongoing RPC request, and can be used in
307 * an async (rpciod) context.
308 */
Chuck Lever45160d62007-07-01 12:13:17 -0400309void rpcb_getport_async(struct rpc_task *task)
Chuck Levera5090502007-03-29 16:48:04 -0400310{
311 struct rpc_clnt *clnt = task->tk_client;
312 int bind_version;
313 struct rpc_xprt *xprt = task->tk_xprt;
314 struct rpc_clnt *rpcb_clnt;
315 static struct rpcbind_args *map;
316 struct rpc_task *child;
317 struct sockaddr addr;
318 int status;
Chuck Leverd5b64432007-08-06 11:57:18 -0400319 struct rpcb_info *info;
Chuck Levera5090502007-03-29 16:48:04 -0400320
Chuck Lever45160d62007-07-01 12:13:17 -0400321 dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
322 task->tk_pid, __FUNCTION__,
323 clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot);
Chuck Levera5090502007-03-29 16:48:04 -0400324
325 /* Autobind on cloned rpc clients is discouraged */
326 BUG_ON(clnt->cl_parent != clnt);
327
328 if (xprt_test_and_set_binding(xprt)) {
Chuck Lever2429cbf2007-09-11 18:00:41 -0400329 status = -EAGAIN; /* tell caller to check again */
Chuck Lever45160d62007-07-01 12:13:17 -0400330 dprintk("RPC: %5u %s: waiting for another binder\n",
331 task->tk_pid, __FUNCTION__);
Chuck Levera5090502007-03-29 16:48:04 -0400332 goto bailout_nowake;
333 }
334
335 /* Put self on queue before sending rpcbind request, in case
336 * rpcb_getport_done completes before we return from rpc_run_task */
337 rpc_sleep_on(&xprt->binding, task, NULL, NULL);
338
339 /* Someone else may have bound if we slept */
340 if (xprt_bound(xprt)) {
341 status = 0;
Chuck Lever45160d62007-07-01 12:13:17 -0400342 dprintk("RPC: %5u %s: already bound\n",
343 task->tk_pid, __FUNCTION__);
Chuck Levera5090502007-03-29 16:48:04 -0400344 goto bailout_nofree;
345 }
346
Chuck Leverd5b64432007-08-06 11:57:18 -0400347 rpc_peeraddr(clnt, (void *)&addr, sizeof(addr));
348
349 /* Don't ever use rpcbind v2 for AF_INET6 requests */
350 switch (addr.sa_family) {
351 case AF_INET:
352 info = rpcb_next_version;
353 break;
354 case AF_INET6:
355 info = rpcb_next_version6;
356 break;
357 default:
358 status = -EAFNOSUPPORT;
359 dprintk("RPC: %5u %s: bad address family\n",
360 task->tk_pid, __FUNCTION__);
361 goto bailout_nofree;
362 }
363 if (info[xprt->bind_index].rpc_proc == NULL) {
Chuck Levera5090502007-03-29 16:48:04 -0400364 xprt->bind_index = 0;
Chuck Lever906462a2007-09-11 18:00:47 -0400365 status = -EPFNOSUPPORT;
Chuck Lever45160d62007-07-01 12:13:17 -0400366 dprintk("RPC: %5u %s: no more getport versions available\n",
367 task->tk_pid, __FUNCTION__);
Chuck Levera5090502007-03-29 16:48:04 -0400368 goto bailout_nofree;
369 }
Chuck Leverd5b64432007-08-06 11:57:18 -0400370 bind_version = info[xprt->bind_index].rpc_vers;
Chuck Levera5090502007-03-29 16:48:04 -0400371
Chuck Lever45160d62007-07-01 12:13:17 -0400372 dprintk("RPC: %5u %s: trying rpcbind version %u\n",
373 task->tk_pid, __FUNCTION__, bind_version);
Chuck Levera5090502007-03-29 16:48:04 -0400374
Chuck Lever143b6c42007-08-16 16:03:31 -0400375 rpcb_clnt = rpcb_create(clnt->cl_server, &addr, xprt->prot,
376 bind_version, 0);
377 if (IS_ERR(rpcb_clnt)) {
378 status = PTR_ERR(rpcb_clnt);
379 dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
380 task->tk_pid, __FUNCTION__, PTR_ERR(rpcb_clnt));
381 goto bailout_nofree;
382 }
383
Chuck Levera5090502007-03-29 16:48:04 -0400384 map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
385 if (!map) {
386 status = -ENOMEM;
Chuck Lever45160d62007-07-01 12:13:17 -0400387 dprintk("RPC: %5u %s: no memory available\n",
388 task->tk_pid, __FUNCTION__);
Chuck Levera5090502007-03-29 16:48:04 -0400389 goto bailout_nofree;
390 }
391 map->r_prog = clnt->cl_prog;
392 map->r_vers = clnt->cl_vers;
393 map->r_prot = xprt->prot;
394 map->r_port = 0;
395 map->r_xprt = xprt_get(xprt);
\"Talpey, Thomas\4417c8c2007-09-10 13:43:05 -0400396 map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
Chuck Lever6d0aa062007-09-11 18:00:15 -0400397 memcpy(map->r_addr,
Chuck Lever143b6c42007-08-16 16:03:31 -0400398 rpc_peeraddr2str(rpcb_clnt, RPC_DISPLAY_UNIVERSAL_ADDR),
399 sizeof(map->r_addr));
Chuck Levera5090502007-03-29 16:48:04 -0400400 map->r_owner = RPCB_OWNER_STRING; /* ignored for GETADDR */
401
Trond Myklebust5138fde2007-07-14 15:40:01 -0400402 child = rpcb_call_async(rpcb_clnt, map, xprt->bind_index);
Trond Myklebust4c402b42007-06-14 16:40:32 -0400403 rpc_release_client(rpcb_clnt);
Chuck Levera5090502007-03-29 16:48:04 -0400404 if (IS_ERR(child)) {
405 status = -EIO;
Chuck Lever45160d62007-07-01 12:13:17 -0400406 dprintk("RPC: %5u %s: rpc_run_task failed\n",
407 task->tk_pid, __FUNCTION__);
Chuck Lever143b6c42007-08-16 16:03:31 -0400408 goto bailout;
Chuck Levera5090502007-03-29 16:48:04 -0400409 }
410 rpc_put_task(child);
411
412 task->tk_xprt->stat.bind_count++;
413 return;
414
415bailout:
416 kfree(map);
417 xprt_put(xprt);
418bailout_nofree:
419 rpcb_wake_rpcbind_waiters(xprt, status);
420bailout_nowake:
421 task->tk_status = status;
422}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400423EXPORT_SYMBOL_GPL(rpcb_getport_async);
Chuck Levera5090502007-03-29 16:48:04 -0400424
425/*
426 * Rpcbind child task calls this callback via tk_exit.
427 */
428static void rpcb_getport_done(struct rpc_task *child, void *data)
429{
430 struct rpcbind_args *map = data;
431 struct rpc_xprt *xprt = map->r_xprt;
432 int status = child->tk_status;
433
Chuck Lever4784cb52007-09-11 18:00:36 -0400434 /* Garbage reply: retry with a lesser rpcbind version */
435 if (status == -EIO)
436 status = -EPROTONOSUPPORT;
437
Chuck Levera5090502007-03-29 16:48:04 -0400438 /* rpcbind server doesn't support this rpcbind protocol version */
439 if (status == -EPROTONOSUPPORT)
440 xprt->bind_index++;
441
442 if (status < 0) {
443 /* rpcbind server not available on remote host? */
444 xprt->ops->set_port(xprt, 0);
445 } else if (map->r_port == 0) {
446 /* Requested RPC service wasn't registered on remote host */
447 xprt->ops->set_port(xprt, 0);
448 status = -EACCES;
449 } else {
450 /* Succeeded */
451 xprt->ops->set_port(xprt, map->r_port);
452 xprt_set_bound(xprt);
453 status = 0;
454 }
455
456 dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
457 child->tk_pid, status, map->r_port);
458
459 rpcb_wake_rpcbind_waiters(xprt, status);
460}
461
462static int rpcb_encode_mapping(struct rpc_rqst *req, __be32 *p,
463 struct rpcbind_args *rpcb)
464{
465 dprintk("RPC: rpcb_encode_mapping(%u, %u, %d, %u)\n",
466 rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
467 *p++ = htonl(rpcb->r_prog);
468 *p++ = htonl(rpcb->r_vers);
469 *p++ = htonl(rpcb->r_prot);
470 *p++ = htonl(rpcb->r_port);
471
472 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
473 return 0;
474}
475
476static int rpcb_decode_getport(struct rpc_rqst *req, __be32 *p,
477 unsigned short *portp)
478{
479 *portp = (unsigned short) ntohl(*p++);
480 dprintk("RPC: rpcb_decode_getport result %u\n",
481 *portp);
482 return 0;
483}
484
485static int rpcb_decode_set(struct rpc_rqst *req, __be32 *p,
486 unsigned int *boolp)
487{
488 *boolp = (unsigned int) ntohl(*p++);
489 dprintk("RPC: rpcb_decode_set result %u\n",
490 *boolp);
491 return 0;
492}
493
494static int rpcb_encode_getaddr(struct rpc_rqst *req, __be32 *p,
495 struct rpcbind_args *rpcb)
496{
497 dprintk("RPC: rpcb_encode_getaddr(%u, %u, %s)\n",
498 rpcb->r_prog, rpcb->r_vers, rpcb->r_addr);
499 *p++ = htonl(rpcb->r_prog);
500 *p++ = htonl(rpcb->r_vers);
501
502 p = xdr_encode_string(p, rpcb->r_netid);
503 p = xdr_encode_string(p, rpcb->r_addr);
504 p = xdr_encode_string(p, rpcb->r_owner);
505
506 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
507
508 return 0;
509}
510
511static int rpcb_decode_getaddr(struct rpc_rqst *req, __be32 *p,
512 unsigned short *portp)
513{
514 char *addr;
Chuck Leveradc24df2007-08-06 11:56:31 -0400515 u32 addr_len;
516 int c, i, f, first, val;
Chuck Levera5090502007-03-29 16:48:04 -0400517
518 *portp = 0;
Chuck Leveradc24df2007-08-06 11:56:31 -0400519 addr_len = ntohl(*p++);
Chuck Levera5090502007-03-29 16:48:04 -0400520
Chuck Levere65fe392007-09-11 18:00:31 -0400521 /*
522 * Simple sanity check. The smallest possible universal
523 * address is an IPv4 address string containing 11 bytes.
524 */
525 if (addr_len < 11 || addr_len > RPCB_MAXADDRLEN)
526 goto out_err;
Chuck Levera5090502007-03-29 16:48:04 -0400527
Chuck Levere65fe392007-09-11 18:00:31 -0400528 /*
529 * Start at the end and walk backwards until the first dot
530 * is encountered. When the second dot is found, we have
531 * both parts of the port number.
532 */
Chuck Levera5090502007-03-29 16:48:04 -0400533 addr = (char *)p;
534 val = 0;
535 first = 1;
536 f = 1;
537 for (i = addr_len - 1; i > 0; i--) {
538 c = addr[i];
539 if (c >= '0' && c <= '9') {
540 val += (c - '0') * f;
541 f *= 10;
542 } else if (c == '.') {
543 if (first) {
544 *portp = val;
545 val = first = 0;
546 f = 1;
547 } else {
548 *portp |= (val << 8);
549 break;
550 }
551 }
552 }
553
Chuck Levere65fe392007-09-11 18:00:31 -0400554 /*
555 * Simple sanity check. If we never saw a dot in the reply,
556 * then this was probably just garbage.
557 */
558 if (first)
559 goto out_err;
560
Chuck Levera5090502007-03-29 16:48:04 -0400561 dprintk("RPC: rpcb_decode_getaddr port=%u\n", *portp);
562 return 0;
Chuck Levere65fe392007-09-11 18:00:31 -0400563
564out_err:
565 dprintk("RPC: rpcbind server returned malformed reply\n");
566 return -EIO;
Chuck Levera5090502007-03-29 16:48:04 -0400567}
568
569#define RPCB_program_sz (1u)
570#define RPCB_version_sz (1u)
571#define RPCB_protocol_sz (1u)
572#define RPCB_port_sz (1u)
573#define RPCB_boolean_sz (1u)
574
\"Talpey, Thomas\4f40ee42007-09-10 13:42:38 -0400575#define RPCB_netid_sz (1+XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
Chuck Levera5090502007-03-29 16:48:04 -0400576#define RPCB_addr_sz (1+XDR_QUADLEN(RPCB_MAXADDRLEN))
577#define RPCB_ownerstring_sz (1+XDR_QUADLEN(RPCB_MAXOWNERLEN))
578
579#define RPCB_mappingargs_sz RPCB_program_sz+RPCB_version_sz+ \
580 RPCB_protocol_sz+RPCB_port_sz
581#define RPCB_getaddrargs_sz RPCB_program_sz+RPCB_version_sz+ \
582 RPCB_netid_sz+RPCB_addr_sz+ \
583 RPCB_ownerstring_sz
584
585#define RPCB_setres_sz RPCB_boolean_sz
586#define RPCB_getportres_sz RPCB_port_sz
587
588/*
589 * Note that RFC 1833 does not put any size restrictions on the
590 * address string returned by the remote rpcbind database.
591 */
592#define RPCB_getaddrres_sz RPCB_addr_sz
593
594#define PROC(proc, argtype, restype) \
595 [RPCBPROC_##proc] = { \
596 .p_proc = RPCBPROC_##proc, \
597 .p_encode = (kxdrproc_t) rpcb_encode_##argtype, \
598 .p_decode = (kxdrproc_t) rpcb_decode_##restype, \
599 .p_arglen = RPCB_##argtype##args_sz, \
600 .p_replen = RPCB_##restype##res_sz, \
601 .p_statidx = RPCBPROC_##proc, \
602 .p_timer = 0, \
603 .p_name = #proc, \
604 }
605
606/*
607 * Not all rpcbind procedures described in RFC 1833 are implemented
608 * since the Linux kernel RPC code requires only these.
609 */
610static struct rpc_procinfo rpcb_procedures2[] = {
611 PROC(SET, mapping, set),
612 PROC(UNSET, mapping, set),
613 PROC(GETADDR, mapping, getport),
614};
615
616static struct rpc_procinfo rpcb_procedures3[] = {
617 PROC(SET, mapping, set),
618 PROC(UNSET, mapping, set),
619 PROC(GETADDR, getaddr, getaddr),
620};
621
622static struct rpc_procinfo rpcb_procedures4[] = {
623 PROC(SET, mapping, set),
624 PROC(UNSET, mapping, set),
625 PROC(GETVERSADDR, getaddr, getaddr),
626};
627
628static struct rpcb_info rpcb_next_version[] = {
629#ifdef CONFIG_SUNRPC_BIND34
630 { 4, &rpcb_procedures4[RPCBPROC_GETVERSADDR] },
631 { 3, &rpcb_procedures3[RPCBPROC_GETADDR] },
632#endif
633 { 2, &rpcb_procedures2[RPCBPROC_GETPORT] },
634 { 0, NULL },
635};
636
Chuck Leverd5b64432007-08-06 11:57:18 -0400637static struct rpcb_info rpcb_next_version6[] = {
638#ifdef CONFIG_SUNRPC_BIND34
639 { 4, &rpcb_procedures4[RPCBPROC_GETVERSADDR] },
640 { 3, &rpcb_procedures3[RPCBPROC_GETADDR] },
641#endif
642 { 0, NULL },
643};
644
Chuck Levera5090502007-03-29 16:48:04 -0400645static struct rpc_version rpcb_version2 = {
646 .number = 2,
647 .nrprocs = RPCB_HIGHPROC_2,
648 .procs = rpcb_procedures2
649};
650
651static struct rpc_version rpcb_version3 = {
652 .number = 3,
653 .nrprocs = RPCB_HIGHPROC_3,
654 .procs = rpcb_procedures3
655};
656
657static struct rpc_version rpcb_version4 = {
658 .number = 4,
659 .nrprocs = RPCB_HIGHPROC_4,
660 .procs = rpcb_procedures4
661};
662
663static struct rpc_version *rpcb_version[] = {
664 NULL,
665 NULL,
666 &rpcb_version2,
667 &rpcb_version3,
668 &rpcb_version4
669};
670
671static struct rpc_stat rpcb_stats;
672
Adrian Bunk7f4adef2007-06-13 01:03:13 +0200673static struct rpc_program rpcb_program = {
Chuck Levera5090502007-03-29 16:48:04 -0400674 .name = "rpcbind",
675 .number = RPCBIND_PROGRAM,
676 .nrvers = ARRAY_SIZE(rpcb_version),
677 .version = rpcb_version,
678 .stats = &rpcb_stats,
679};