blob: f494e58910ec0df5d0ea3be1bfbc59437a3230a0 [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;
76 char * r_netid;
Chuck Lever0fb2b7e2007-12-10 14:56:46 -050077 char r_addr[RPCBIND_MAXUADDRLEN];
Chuck Levera5090502007-03-29 16:48:04 -040078 char * r_owner;
79};
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,
Chuck Leverf7fb5582007-07-01 12:13:07 -0400123 .flags = (RPC_CLNT_CREATE_NOPING |
124 RPC_CLNT_CREATE_INTR),
Chuck Levera5090502007-03-29 16:48:04 -0400125 };
126
Chuck Leverd5b64432007-08-06 11:57:18 -0400127 switch (srvaddr->sa_family) {
128 case AF_INET:
129 ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
130 break;
131 case AF_INET6:
132 ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
133 break;
134 default:
135 return NULL;
136 }
137
Chuck Levera5090502007-03-29 16:48:04 -0400138 if (!privileged)
139 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
140 return rpc_create(&args);
141}
142
143/**
144 * rpcb_register - set or unset a port registration with the local rpcbind svc
145 * @prog: RPC program number to bind
146 * @vers: RPC version number to bind
147 * @prot: transport protocol to use to make this request
148 * @port: port value to register
149 * @okay: result code
150 *
151 * port == 0 means unregister, port != 0 means register.
152 *
153 * This routine supports only rpcbind version 2.
154 */
155int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
156{
157 struct sockaddr_in sin = {
158 .sin_family = AF_INET,
159 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
160 };
161 struct rpcbind_args map = {
162 .r_prog = prog,
163 .r_vers = vers,
164 .r_prot = prot,
165 .r_port = port,
166 };
167 struct rpc_message msg = {
168 .rpc_proc = &rpcb_procedures2[port ?
169 RPCBPROC_SET : RPCBPROC_UNSET],
170 .rpc_argp = &map,
171 .rpc_resp = okay,
172 };
173 struct rpc_clnt *rpcb_clnt;
174 int error = 0;
175
176 dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
177 "rpcbind\n", (port ? "" : "un"),
178 prog, vers, prot, port);
179
180 rpcb_clnt = rpcb_create("localhost", (struct sockaddr *) &sin,
Chuck Lever9f6ad262007-12-10 14:56:31 -0500181 sizeof(sin), XPRT_TRANSPORT_UDP, 2, 1);
Chuck Levera5090502007-03-29 16:48:04 -0400182 if (IS_ERR(rpcb_clnt))
183 return PTR_ERR(rpcb_clnt);
184
185 error = rpc_call_sync(rpcb_clnt, &msg, 0);
186
Trond Myklebust90c57552007-06-09 19:49:36 -0400187 rpc_shutdown_client(rpcb_clnt);
Chuck Levera5090502007-03-29 16:48:04 -0400188 if (error < 0)
189 printk(KERN_WARNING "RPC: failed to contact local rpcbind "
190 "server (errno %d).\n", -error);
191 dprintk("RPC: registration status %d/%d\n", error, *okay);
192
193 return error;
194}
195
Chuck Levera5090502007-03-29 16:48:04 -0400196/**
Chuck Levercce63cd2007-07-01 12:13:12 -0400197 * rpcb_getport_sync - obtain the port for an RPC service on a given host
Chuck Levera5090502007-03-29 16:48:04 -0400198 * @sin: address of remote peer
199 * @prog: RPC program number to bind
200 * @vers: RPC version number to bind
201 * @prot: transport protocol to use to make this request
202 *
203 * Called from outside the RPC client in a synchronous task context.
Chuck Levercce63cd2007-07-01 12:13:12 -0400204 * Uses default timeout parameters specified by underlying transport.
Chuck Levera5090502007-03-29 16:48:04 -0400205 *
Chuck Levercce63cd2007-07-01 12:13:12 -0400206 * XXX: Needs to support IPv6, and rpcbind versions 3 and 4
Chuck Levera5090502007-03-29 16:48:04 -0400207 */
Chuck Levercce63cd2007-07-01 12:13:12 -0400208int rpcb_getport_sync(struct sockaddr_in *sin, __u32 prog,
209 __u32 vers, int prot)
Chuck Levera5090502007-03-29 16:48:04 -0400210{
211 struct rpcbind_args map = {
212 .r_prog = prog,
213 .r_vers = vers,
214 .r_prot = prot,
215 .r_port = 0,
216 };
217 struct rpc_message msg = {
218 .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
219 .rpc_argp = &map,
220 .rpc_resp = &map.r_port,
221 };
222 struct rpc_clnt *rpcb_clnt;
223 char hostname[40];
224 int status;
225
Chuck Levercce63cd2007-07-01 12:13:12 -0400226 dprintk("RPC: %s(" NIPQUAD_FMT ", %u, %u, %d)\n",
227 __FUNCTION__, NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
Chuck Levera5090502007-03-29 16:48:04 -0400228
Chuck Levercce63cd2007-07-01 12:13:12 -0400229 sprintf(hostname, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr));
Chuck Lever9f6ad262007-12-10 14:56:31 -0500230 rpcb_clnt = rpcb_create(hostname, (struct sockaddr *)sin,
231 sizeof(sin), prot, 2, 0);
Chuck Levera5090502007-03-29 16:48:04 -0400232 if (IS_ERR(rpcb_clnt))
233 return PTR_ERR(rpcb_clnt);
234
235 status = rpc_call_sync(rpcb_clnt, &msg, 0);
Trond Myklebust90c57552007-06-09 19:49:36 -0400236 rpc_shutdown_client(rpcb_clnt);
Chuck Levera5090502007-03-29 16:48:04 -0400237
238 if (status >= 0) {
239 if (map.r_port != 0)
240 return map.r_port;
241 status = -EACCES;
242 }
243 return status;
244}
Chuck Levercce63cd2007-07-01 12:13:12 -0400245EXPORT_SYMBOL_GPL(rpcb_getport_sync);
Chuck Levera5090502007-03-29 16:48:04 -0400246
Trond Myklebust5138fde2007-07-14 15:40:01 -0400247static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt, struct rpcbind_args *map, int version)
248{
249 struct rpc_message msg = {
250 .rpc_proc = rpcb_next_version[version].rpc_proc,
251 .rpc_argp = map,
252 .rpc_resp = &map->r_port,
253 };
254 struct rpc_task_setup task_setup_data = {
255 .rpc_client = rpcb_clnt,
256 .rpc_message = &msg,
257 .callback_ops = &rpcb_getport_ops,
258 .callback_data = map,
259 .flags = RPC_TASK_ASYNC,
260 };
261
262 return rpc_run_task(&task_setup_data);
263}
264
Chuck Levera5090502007-03-29 16:48:04 -0400265/**
Chuck Lever45160d62007-07-01 12:13:17 -0400266 * rpcb_getport_async - obtain the port for a given RPC service on a given host
Chuck Levera5090502007-03-29 16:48:04 -0400267 * @task: task that is waiting for portmapper request
268 *
269 * This one can be called for an ongoing RPC request, and can be used in
270 * an async (rpciod) context.
271 */
Chuck Lever45160d62007-07-01 12:13:17 -0400272void rpcb_getport_async(struct rpc_task *task)
Chuck Levera5090502007-03-29 16:48:04 -0400273{
274 struct rpc_clnt *clnt = task->tk_client;
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;
Chuck Leverd5b64432007-08-06 11:57:18 -0400284 struct rpcb_info *info;
Chuck Levera5090502007-03-29 16:48:04 -0400285
Chuck Lever45160d62007-07-01 12:13:17 -0400286 dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
287 task->tk_pid, __FUNCTION__,
288 clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot);
Chuck Levera5090502007-03-29 16:48:04 -0400289
290 /* Autobind on cloned rpc clients is discouraged */
291 BUG_ON(clnt->cl_parent != clnt);
292
293 if (xprt_test_and_set_binding(xprt)) {
Chuck Lever2429cbf2007-09-11 18:00:41 -0400294 status = -EAGAIN; /* tell caller to check again */
Chuck Lever45160d62007-07-01 12:13:17 -0400295 dprintk("RPC: %5u %s: waiting for another binder\n",
296 task->tk_pid, __FUNCTION__);
Chuck Levera5090502007-03-29 16:48:04 -0400297 goto bailout_nowake;
298 }
299
300 /* Put self on queue before sending rpcbind request, in case
301 * rpcb_getport_done completes before we return from rpc_run_task */
302 rpc_sleep_on(&xprt->binding, task, NULL, NULL);
303
304 /* Someone else may have bound if we slept */
305 if (xprt_bound(xprt)) {
306 status = 0;
Chuck Lever45160d62007-07-01 12:13:17 -0400307 dprintk("RPC: %5u %s: already bound\n",
308 task->tk_pid, __FUNCTION__);
Chuck Levera5090502007-03-29 16:48:04 -0400309 goto bailout_nofree;
310 }
311
Chuck Lever9f6ad262007-12-10 14:56:31 -0500312 salen = rpc_peeraddr(clnt, sap, sizeof(addr));
Chuck Leverd5b64432007-08-06 11:57:18 -0400313
314 /* Don't ever use rpcbind v2 for AF_INET6 requests */
Chuck Lever9f6ad262007-12-10 14:56:31 -0500315 switch (sap->sa_family) {
Chuck Leverd5b64432007-08-06 11:57:18 -0400316 case AF_INET:
317 info = rpcb_next_version;
318 break;
319 case AF_INET6:
320 info = rpcb_next_version6;
321 break;
322 default:
323 status = -EAFNOSUPPORT;
324 dprintk("RPC: %5u %s: bad address family\n",
325 task->tk_pid, __FUNCTION__);
326 goto bailout_nofree;
327 }
328 if (info[xprt->bind_index].rpc_proc == NULL) {
Chuck Levera5090502007-03-29 16:48:04 -0400329 xprt->bind_index = 0;
Chuck Lever906462a2007-09-11 18:00:47 -0400330 status = -EPFNOSUPPORT;
Chuck Lever45160d62007-07-01 12:13:17 -0400331 dprintk("RPC: %5u %s: no more getport versions available\n",
332 task->tk_pid, __FUNCTION__);
Chuck Levera5090502007-03-29 16:48:04 -0400333 goto bailout_nofree;
334 }
Chuck Leverd5b64432007-08-06 11:57:18 -0400335 bind_version = info[xprt->bind_index].rpc_vers;
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",
338 task->tk_pid, __FUNCTION__, 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",
345 task->tk_pid, __FUNCTION__, PTR_ERR(rpcb_clnt));
346 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",
353 task->tk_pid, __FUNCTION__);
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);
\"Talpey, Thomas\4417c8c2007-09-10 13:43:05 -0400361 map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
Chuck Lever6d0aa062007-09-11 18:00:15 -0400362 memcpy(map->r_addr,
Chuck Lever143b6c42007-08-16 16:03:31 -0400363 rpc_peeraddr2str(rpcb_clnt, RPC_DISPLAY_UNIVERSAL_ADDR),
364 sizeof(map->r_addr));
Chuck Levera5090502007-03-29 16:48:04 -0400365 map->r_owner = RPCB_OWNER_STRING; /* ignored for GETADDR */
366
Trond Myklebust5138fde2007-07-14 15:40:01 -0400367 child = rpcb_call_async(rpcb_clnt, map, xprt->bind_index);
Trond Myklebust4c402b42007-06-14 16:40:32 -0400368 rpc_release_client(rpcb_clnt);
Chuck Levera5090502007-03-29 16:48:04 -0400369 if (IS_ERR(child)) {
370 status = -EIO;
Chuck Lever45160d62007-07-01 12:13:17 -0400371 dprintk("RPC: %5u %s: rpc_run_task failed\n",
372 task->tk_pid, __FUNCTION__);
Chuck Lever143b6c42007-08-16 16:03:31 -0400373 goto bailout;
Chuck Levera5090502007-03-29 16:48:04 -0400374 }
375 rpc_put_task(child);
376
377 task->tk_xprt->stat.bind_count++;
378 return;
379
380bailout:
381 kfree(map);
382 xprt_put(xprt);
383bailout_nofree:
384 rpcb_wake_rpcbind_waiters(xprt, status);
385bailout_nowake:
386 task->tk_status = status;
387}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400388EXPORT_SYMBOL_GPL(rpcb_getport_async);
Chuck Levera5090502007-03-29 16:48:04 -0400389
390/*
391 * Rpcbind child task calls this callback via tk_exit.
392 */
393static void rpcb_getport_done(struct rpc_task *child, void *data)
394{
395 struct rpcbind_args *map = data;
396 struct rpc_xprt *xprt = map->r_xprt;
397 int status = child->tk_status;
398
Chuck Lever4784cb52007-09-11 18:00:36 -0400399 /* Garbage reply: retry with a lesser rpcbind version */
400 if (status == -EIO)
401 status = -EPROTONOSUPPORT;
402
Chuck Levera5090502007-03-29 16:48:04 -0400403 /* rpcbind server doesn't support this rpcbind protocol version */
404 if (status == -EPROTONOSUPPORT)
405 xprt->bind_index++;
406
407 if (status < 0) {
408 /* rpcbind server not available on remote host? */
409 xprt->ops->set_port(xprt, 0);
410 } else if (map->r_port == 0) {
411 /* Requested RPC service wasn't registered on remote host */
412 xprt->ops->set_port(xprt, 0);
413 status = -EACCES;
414 } else {
415 /* Succeeded */
416 xprt->ops->set_port(xprt, map->r_port);
417 xprt_set_bound(xprt);
418 status = 0;
419 }
420
421 dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
422 child->tk_pid, status, map->r_port);
423
424 rpcb_wake_rpcbind_waiters(xprt, status);
425}
426
427static int rpcb_encode_mapping(struct rpc_rqst *req, __be32 *p,
428 struct rpcbind_args *rpcb)
429{
430 dprintk("RPC: rpcb_encode_mapping(%u, %u, %d, %u)\n",
431 rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
432 *p++ = htonl(rpcb->r_prog);
433 *p++ = htonl(rpcb->r_vers);
434 *p++ = htonl(rpcb->r_prot);
435 *p++ = htonl(rpcb->r_port);
436
437 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
438 return 0;
439}
440
441static int rpcb_decode_getport(struct rpc_rqst *req, __be32 *p,
442 unsigned short *portp)
443{
444 *portp = (unsigned short) ntohl(*p++);
445 dprintk("RPC: rpcb_decode_getport result %u\n",
446 *portp);
447 return 0;
448}
449
450static int rpcb_decode_set(struct rpc_rqst *req, __be32 *p,
451 unsigned int *boolp)
452{
453 *boolp = (unsigned int) ntohl(*p++);
454 dprintk("RPC: rpcb_decode_set result %u\n",
455 *boolp);
456 return 0;
457}
458
459static int rpcb_encode_getaddr(struct rpc_rqst *req, __be32 *p,
460 struct rpcbind_args *rpcb)
461{
462 dprintk("RPC: rpcb_encode_getaddr(%u, %u, %s)\n",
463 rpcb->r_prog, rpcb->r_vers, rpcb->r_addr);
464 *p++ = htonl(rpcb->r_prog);
465 *p++ = htonl(rpcb->r_vers);
466
467 p = xdr_encode_string(p, rpcb->r_netid);
468 p = xdr_encode_string(p, rpcb->r_addr);
469 p = xdr_encode_string(p, rpcb->r_owner);
470
471 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
472
473 return 0;
474}
475
476static int rpcb_decode_getaddr(struct rpc_rqst *req, __be32 *p,
477 unsigned short *portp)
478{
479 char *addr;
Chuck Leveradc24df2007-08-06 11:56:31 -0400480 u32 addr_len;
481 int c, i, f, first, val;
Chuck Levera5090502007-03-29 16:48:04 -0400482
483 *portp = 0;
Chuck Leveradc24df2007-08-06 11:56:31 -0400484 addr_len = ntohl(*p++);
Chuck Levera5090502007-03-29 16:48:04 -0400485
Chuck Levere65fe392007-09-11 18:00:31 -0400486 /*
487 * Simple sanity check. The smallest possible universal
488 * address is an IPv4 address string containing 11 bytes.
489 */
Chuck Lever0fb2b7e2007-12-10 14:56:46 -0500490 if (addr_len < 11 || addr_len > RPCBIND_MAXUADDRLEN)
Chuck Levere65fe392007-09-11 18:00:31 -0400491 goto out_err;
Chuck Levera5090502007-03-29 16:48:04 -0400492
Chuck Levere65fe392007-09-11 18:00:31 -0400493 /*
494 * Start at the end and walk backwards until the first dot
495 * is encountered. When the second dot is found, we have
496 * both parts of the port number.
497 */
Chuck Levera5090502007-03-29 16:48:04 -0400498 addr = (char *)p;
499 val = 0;
500 first = 1;
501 f = 1;
502 for (i = addr_len - 1; i > 0; i--) {
503 c = addr[i];
504 if (c >= '0' && c <= '9') {
505 val += (c - '0') * f;
506 f *= 10;
507 } else if (c == '.') {
508 if (first) {
509 *portp = val;
510 val = first = 0;
511 f = 1;
512 } else {
513 *portp |= (val << 8);
514 break;
515 }
516 }
517 }
518
Chuck Levere65fe392007-09-11 18:00:31 -0400519 /*
520 * Simple sanity check. If we never saw a dot in the reply,
521 * then this was probably just garbage.
522 */
523 if (first)
524 goto out_err;
525
Chuck Levera5090502007-03-29 16:48:04 -0400526 dprintk("RPC: rpcb_decode_getaddr port=%u\n", *portp);
527 return 0;
Chuck Levere65fe392007-09-11 18:00:31 -0400528
529out_err:
530 dprintk("RPC: rpcbind server returned malformed reply\n");
531 return -EIO;
Chuck Levera5090502007-03-29 16:48:04 -0400532}
533
534#define RPCB_program_sz (1u)
535#define RPCB_version_sz (1u)
536#define RPCB_protocol_sz (1u)
537#define RPCB_port_sz (1u)
538#define RPCB_boolean_sz (1u)
539
\"Talpey, Thomas\4f40ee42007-09-10 13:42:38 -0400540#define RPCB_netid_sz (1+XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
Chuck Lever0fb2b7e2007-12-10 14:56:46 -0500541#define RPCB_addr_sz (1+XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
Chuck Levera5090502007-03-29 16:48:04 -0400542#define RPCB_ownerstring_sz (1+XDR_QUADLEN(RPCB_MAXOWNERLEN))
543
544#define RPCB_mappingargs_sz RPCB_program_sz+RPCB_version_sz+ \
545 RPCB_protocol_sz+RPCB_port_sz
546#define RPCB_getaddrargs_sz RPCB_program_sz+RPCB_version_sz+ \
547 RPCB_netid_sz+RPCB_addr_sz+ \
548 RPCB_ownerstring_sz
549
550#define RPCB_setres_sz RPCB_boolean_sz
551#define RPCB_getportres_sz RPCB_port_sz
552
553/*
554 * Note that RFC 1833 does not put any size restrictions on the
555 * address string returned by the remote rpcbind database.
556 */
557#define RPCB_getaddrres_sz RPCB_addr_sz
558
559#define PROC(proc, argtype, restype) \
560 [RPCBPROC_##proc] = { \
561 .p_proc = RPCBPROC_##proc, \
562 .p_encode = (kxdrproc_t) rpcb_encode_##argtype, \
563 .p_decode = (kxdrproc_t) rpcb_decode_##restype, \
564 .p_arglen = RPCB_##argtype##args_sz, \
565 .p_replen = RPCB_##restype##res_sz, \
566 .p_statidx = RPCBPROC_##proc, \
567 .p_timer = 0, \
568 .p_name = #proc, \
569 }
570
571/*
572 * Not all rpcbind procedures described in RFC 1833 are implemented
573 * since the Linux kernel RPC code requires only these.
574 */
575static struct rpc_procinfo rpcb_procedures2[] = {
576 PROC(SET, mapping, set),
577 PROC(UNSET, mapping, set),
578 PROC(GETADDR, mapping, getport),
579};
580
581static struct rpc_procinfo rpcb_procedures3[] = {
582 PROC(SET, mapping, set),
583 PROC(UNSET, mapping, set),
584 PROC(GETADDR, getaddr, getaddr),
585};
586
587static struct rpc_procinfo rpcb_procedures4[] = {
588 PROC(SET, mapping, set),
589 PROC(UNSET, mapping, set),
590 PROC(GETVERSADDR, getaddr, getaddr),
591};
592
593static struct rpcb_info rpcb_next_version[] = {
594#ifdef CONFIG_SUNRPC_BIND34
595 { 4, &rpcb_procedures4[RPCBPROC_GETVERSADDR] },
596 { 3, &rpcb_procedures3[RPCBPROC_GETADDR] },
597#endif
598 { 2, &rpcb_procedures2[RPCBPROC_GETPORT] },
599 { 0, NULL },
600};
601
Chuck Leverd5b64432007-08-06 11:57:18 -0400602static struct rpcb_info rpcb_next_version6[] = {
603#ifdef CONFIG_SUNRPC_BIND34
604 { 4, &rpcb_procedures4[RPCBPROC_GETVERSADDR] },
605 { 3, &rpcb_procedures3[RPCBPROC_GETADDR] },
606#endif
607 { 0, NULL },
608};
609
Chuck Levera5090502007-03-29 16:48:04 -0400610static struct rpc_version rpcb_version2 = {
611 .number = 2,
612 .nrprocs = RPCB_HIGHPROC_2,
613 .procs = rpcb_procedures2
614};
615
616static struct rpc_version rpcb_version3 = {
617 .number = 3,
618 .nrprocs = RPCB_HIGHPROC_3,
619 .procs = rpcb_procedures3
620};
621
622static struct rpc_version rpcb_version4 = {
623 .number = 4,
624 .nrprocs = RPCB_HIGHPROC_4,
625 .procs = rpcb_procedures4
626};
627
628static struct rpc_version *rpcb_version[] = {
629 NULL,
630 NULL,
631 &rpcb_version2,
632 &rpcb_version3,
633 &rpcb_version4
634};
635
636static struct rpc_stat rpcb_stats;
637
Adrian Bunk7f4adef2007-06-13 01:03:13 +0200638static struct rpc_program rpcb_program = {
Chuck Levera5090502007-03-29 16:48:04 -0400639 .name = "rpcbind",
640 .number = RPCBIND_PROGRAM,
641 .nrvers = ARRAY_SIZE(rpcb_version),
642 .version = rpcb_version,
643 .stats = &rpcb_stats,
644};