blob: 8b75c306e661686f28ab36c3aa052d378758c240 [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
Chuck Leverfc200e72008-06-25 17:24:31 -040035#define RPCBVERS_2 (2u)
36#define RPCBVERS_3 (3u)
37#define RPCBVERS_4 (4u)
38
Chuck Levera5090502007-03-29 16:48:04 -040039enum {
40 RPCBPROC_NULL,
41 RPCBPROC_SET,
42 RPCBPROC_UNSET,
43 RPCBPROC_GETPORT,
44 RPCBPROC_GETADDR = 3, /* alias for GETPORT */
45 RPCBPROC_DUMP,
46 RPCBPROC_CALLIT,
47 RPCBPROC_BCAST = 5, /* alias for CALLIT */
48 RPCBPROC_GETTIME,
49 RPCBPROC_UADDR2TADDR,
50 RPCBPROC_TADDR2UADDR,
51 RPCBPROC_GETVERSADDR,
52 RPCBPROC_INDIRECT,
53 RPCBPROC_GETADDRLIST,
54 RPCBPROC_GETSTAT,
55};
56
57#define RPCB_HIGHPROC_2 RPCBPROC_CALLIT
58#define RPCB_HIGHPROC_3 RPCBPROC_TADDR2UADDR
59#define RPCB_HIGHPROC_4 RPCBPROC_GETSTAT
60
61/*
Chuck Levera5090502007-03-29 16:48:04 -040062 * r_owner
63 *
64 * The "owner" is allowed to unset a service in the rpcbind database.
65 * We always use the following (arbitrary) fixed string.
66 */
67#define RPCB_OWNER_STRING "rpcb"
68#define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
69
70static void rpcb_getport_done(struct rpc_task *, void *);
Trond Myklebust381ba742008-07-07 12:18:53 -040071static void rpcb_map_release(void *data);
Adrian Bunk7f4adef2007-06-13 01:03:13 +020072static struct rpc_program rpcb_program;
Chuck Levera5090502007-03-29 16:48:04 -040073
74struct rpcbind_args {
75 struct rpc_xprt * r_xprt;
76
77 u32 r_prog;
78 u32 r_vers;
79 u32 r_prot;
80 unsigned short r_port;
Trond Myklebust86d61d82008-01-07 21:16:56 -050081 const char * r_netid;
82 const char * r_addr;
83 const char * r_owner;
Trond Myklebust381ba742008-07-07 12:18:53 -040084
85 int r_status;
Chuck Levera5090502007-03-29 16:48:04 -040086};
87
88static struct rpc_procinfo rpcb_procedures2[];
89static struct rpc_procinfo rpcb_procedures3[];
90
Chuck Leverd5b64432007-08-06 11:57:18 -040091struct rpcb_info {
Chuck Leverfc200e72008-06-25 17:24:31 -040092 u32 rpc_vers;
Chuck Levera5090502007-03-29 16:48:04 -040093 struct rpc_procinfo * rpc_proc;
Chuck Leverd5b64432007-08-06 11:57:18 -040094};
95
96static struct rpcb_info rpcb_next_version[];
97static struct rpcb_info rpcb_next_version6[];
Chuck Levera5090502007-03-29 16:48:04 -040098
Chuck Levera5090502007-03-29 16:48:04 -040099static const struct rpc_call_ops rpcb_getport_ops = {
Chuck Levera5090502007-03-29 16:48:04 -0400100 .rpc_call_done = rpcb_getport_done,
101 .rpc_release = rpcb_map_release,
102};
103
104static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status)
105{
106 xprt_clear_binding(xprt);
107 rpc_wake_up_status(&xprt->binding, status);
108}
109
Trond Myklebust381ba742008-07-07 12:18:53 -0400110static void rpcb_map_release(void *data)
111{
112 struct rpcbind_args *map = data;
113
114 rpcb_wake_rpcbind_waiters(map->r_xprt, map->r_status);
115 xprt_put(map->r_xprt);
116 kfree(map);
117}
118
Chuck Levercc5598b2008-07-14 16:03:27 -0400119static const struct sockaddr_in rpcb_inaddr_loopback = {
120 .sin_family = AF_INET,
121 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
122 .sin_port = htons(RPCBIND_PORT),
123};
124
125static struct rpc_clnt *rpcb_create_local(struct sockaddr *addr,
126 size_t addrlen, u32 version)
127{
128 struct rpc_create_args args = {
129 .protocol = XPRT_TRANSPORT_UDP,
130 .address = addr,
131 .addrsize = addrlen,
132 .servername = "localhost",
133 .program = &rpcb_program,
134 .version = version,
135 .authflavor = RPC_AUTH_UNIX,
136 .flags = RPC_CLNT_CREATE_NOPING,
137 };
138
139 return rpc_create(&args);
140}
141
Chuck Levera5090502007-03-29 16:48:04 -0400142static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
Chuck Lever423d8b02008-07-14 16:03:28 -0400143 size_t salen, int proto, u32 version)
Chuck Levera5090502007-03-29 16:48:04 -0400144{
145 struct rpc_create_args args = {
146 .protocol = proto,
147 .address = srvaddr,
Chuck Lever9f6ad262007-12-10 14:56:31 -0500148 .addrsize = salen,
Chuck Levera5090502007-03-29 16:48:04 -0400149 .servername = hostname,
150 .program = &rpcb_program,
151 .version = version,
152 .authflavor = RPC_AUTH_UNIX,
Chuck Lever423d8b02008-07-14 16:03:28 -0400153 .flags = (RPC_CLNT_CREATE_NOPING |
154 RPC_CLNT_CREATE_NONPRIVPORT),
Chuck Levera5090502007-03-29 16:48:04 -0400155 };
156
Chuck Leverd5b64432007-08-06 11:57:18 -0400157 switch (srvaddr->sa_family) {
158 case AF_INET:
159 ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
160 break;
161 case AF_INET6:
162 ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
163 break;
164 default:
165 return NULL;
166 }
167
Chuck Levera5090502007-03-29 16:48:04 -0400168 return rpc_create(&args);
169}
170
Chuck Leverbabe80e2008-07-14 16:03:29 -0400171static int rpcb_register_call(struct sockaddr *addr, size_t addrlen,
172 u32 version, struct rpc_message *msg,
173 int *result)
174{
175 struct rpc_clnt *rpcb_clnt;
176 int error = 0;
177
178 *result = 0;
179
180 rpcb_clnt = rpcb_create_local(addr, addrlen, version);
181 if (!IS_ERR(rpcb_clnt)) {
182 error = rpc_call_sync(rpcb_clnt, msg, 0);
183 rpc_shutdown_client(rpcb_clnt);
184 } else
185 error = PTR_ERR(rpcb_clnt);
186
187 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, *result);
191
192 return error;
193}
194
Chuck Levera5090502007-03-29 16:48:04 -0400195/**
196 * rpcb_register - set or unset a port registration with the local rpcbind svc
197 * @prog: RPC program number to bind
198 * @vers: RPC version number to bind
199 * @prot: transport protocol to use to make this request
200 * @port: port value to register
201 * @okay: result code
202 *
203 * port == 0 means unregister, port != 0 means register.
204 *
205 * This routine supports only rpcbind version 2.
206 */
207int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
208{
Chuck Levera5090502007-03-29 16:48:04 -0400209 struct rpcbind_args map = {
210 .r_prog = prog,
211 .r_vers = vers,
212 .r_prot = prot,
213 .r_port = port,
214 };
215 struct rpc_message msg = {
Chuck Levera5090502007-03-29 16:48:04 -0400216 .rpc_argp = &map,
217 .rpc_resp = okay,
218 };
Chuck Levera5090502007-03-29 16:48:04 -0400219
220 dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
221 "rpcbind\n", (port ? "" : "un"),
222 prog, vers, prot, port);
223
Chuck Leverbabe80e2008-07-14 16:03:29 -0400224 msg.rpc_proc = &rpcb_procedures2[RPCBPROC_UNSET];
225 if (port)
226 msg.rpc_proc = &rpcb_procedures2[RPCBPROC_SET];
227
228 return rpcb_register_call((struct sockaddr *)&rpcb_inaddr_loopback,
Chuck Levercc5598b2008-07-14 16:03:27 -0400229 sizeof(rpcb_inaddr_loopback),
Chuck Leverbabe80e2008-07-14 16:03:29 -0400230 RPCBVERS_2, &msg, okay);
Chuck Levera5090502007-03-29 16:48:04 -0400231}
232
Chuck Levera5090502007-03-29 16:48:04 -0400233/**
Chuck Levercce63cd2007-07-01 12:13:12 -0400234 * rpcb_getport_sync - obtain the port for an RPC service on a given host
Chuck Levera5090502007-03-29 16:48:04 -0400235 * @sin: address of remote peer
236 * @prog: RPC program number to bind
237 * @vers: RPC version number to bind
238 * @prot: transport protocol to use to make this request
239 *
Chuck Lever67d60212008-01-14 15:12:01 -0500240 * Return value is the requested advertised port number,
241 * or a negative errno value.
242 *
Chuck Levera5090502007-03-29 16:48:04 -0400243 * Called from outside the RPC client in a synchronous task context.
Chuck Levercce63cd2007-07-01 12:13:12 -0400244 * Uses default timeout parameters specified by underlying transport.
Chuck Levera5090502007-03-29 16:48:04 -0400245 *
Chuck Lever67d60212008-01-14 15:12:01 -0500246 * XXX: Needs to support IPv6
Chuck Levera5090502007-03-29 16:48:04 -0400247 */
Chuck Leverf1ec08c2008-01-14 15:11:53 -0500248int rpcb_getport_sync(struct sockaddr_in *sin, u32 prog, 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;
Chuck Levera5090502007-03-29 16:48:04 -0400262 int status;
263
Chuck Levercce63cd2007-07-01 12:13:12 -0400264 dprintk("RPC: %s(" NIPQUAD_FMT ", %u, %u, %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800265 __func__, NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
Chuck Levera5090502007-03-29 16:48:04 -0400266
Chuck Leverb91e1012008-01-14 15:11:46 -0500267 rpcb_clnt = rpcb_create(NULL, (struct sockaddr *)sin,
Chuck Lever423d8b02008-07-14 16:03:28 -0400268 sizeof(*sin), prot, RPCBVERS_2);
Chuck Levera5090502007-03-29 16:48:04 -0400269 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 Myklebust803a9062008-07-01 15:20:55 -0400284static 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 -0400285{
286 struct rpc_message msg = {
Trond Myklebust803a9062008-07-01 15:20:55 -0400287 .rpc_proc = proc,
Trond Myklebust5138fde2007-07-14 15:40:01 -0400288 .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;
Trond Myklebust803a9062008-07-01 15:20:55 -0400312 struct rpc_procinfo *proc;
Chuck Lever0a48f5d2007-12-10 14:56:38 -0500313 u32 bind_version;
Chuck Levera5090502007-03-29 16:48:04 -0400314 struct rpc_xprt *xprt = task->tk_xprt;
315 struct rpc_clnt *rpcb_clnt;
316 static struct rpcbind_args *map;
317 struct rpc_task *child;
Chuck Lever9f6ad262007-12-10 14:56:31 -0500318 struct sockaddr_storage addr;
319 struct sockaddr *sap = (struct sockaddr *)&addr;
320 size_t salen;
Chuck Levera5090502007-03-29 16:48:04 -0400321 int status;
322
Chuck Lever45160d62007-07-01 12:13:17 -0400323 dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800324 task->tk_pid, __func__,
Chuck Lever45160d62007-07-01 12:13:17 -0400325 clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot);
Chuck Levera5090502007-03-29 16:48:04 -0400326
327 /* Autobind on cloned rpc clients is discouraged */
328 BUG_ON(clnt->cl_parent != clnt);
329
Trond Myklebust381ba742008-07-07 12:18:53 -0400330 /* Put self on the wait queue to ensure we get notified if
331 * some other task is already attempting to bind the port */
332 rpc_sleep_on(&xprt->binding, task, NULL);
333
Chuck Levera5090502007-03-29 16:48:04 -0400334 if (xprt_test_and_set_binding(xprt)) {
Chuck Lever45160d62007-07-01 12:13:17 -0400335 dprintk("RPC: %5u %s: waiting for another binder\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800336 task->tk_pid, __func__);
Trond Myklebust381ba742008-07-07 12:18:53 -0400337 return;
Chuck Levera5090502007-03-29 16:48:04 -0400338 }
339
Chuck Levera5090502007-03-29 16:48:04 -0400340 /* Someone else may have bound if we slept */
341 if (xprt_bound(xprt)) {
342 status = 0;
Chuck Lever45160d62007-07-01 12:13:17 -0400343 dprintk("RPC: %5u %s: already bound\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800344 task->tk_pid, __func__);
Chuck Levera5090502007-03-29 16:48:04 -0400345 goto bailout_nofree;
346 }
347
Chuck Lever9f6ad262007-12-10 14:56:31 -0500348 salen = rpc_peeraddr(clnt, sap, sizeof(addr));
Chuck Leverd5b64432007-08-06 11:57:18 -0400349
350 /* Don't ever use rpcbind v2 for AF_INET6 requests */
Chuck Lever9f6ad262007-12-10 14:56:31 -0500351 switch (sap->sa_family) {
Chuck Leverd5b64432007-08-06 11:57:18 -0400352 case AF_INET:
Trond Myklebust803a9062008-07-01 15:20:55 -0400353 proc = rpcb_next_version[xprt->bind_index].rpc_proc;
354 bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
Chuck Leverd5b64432007-08-06 11:57:18 -0400355 break;
356 case AF_INET6:
Trond Myklebust803a9062008-07-01 15:20:55 -0400357 proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
358 bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
Chuck Leverd5b64432007-08-06 11:57:18 -0400359 break;
360 default:
361 status = -EAFNOSUPPORT;
362 dprintk("RPC: %5u %s: bad address family\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800363 task->tk_pid, __func__);
Chuck Leverd5b64432007-08-06 11:57:18 -0400364 goto bailout_nofree;
365 }
Trond Myklebust803a9062008-07-01 15:20:55 -0400366 if (proc == NULL) {
Chuck Levera5090502007-03-29 16:48:04 -0400367 xprt->bind_index = 0;
Chuck Lever906462a2007-09-11 18:00:47 -0400368 status = -EPFNOSUPPORT;
Chuck Lever45160d62007-07-01 12:13:17 -0400369 dprintk("RPC: %5u %s: no more getport versions available\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800370 task->tk_pid, __func__);
Chuck Levera5090502007-03-29 16:48:04 -0400371 goto bailout_nofree;
372 }
Chuck Levera5090502007-03-29 16:48:04 -0400373
Chuck Lever45160d62007-07-01 12:13:17 -0400374 dprintk("RPC: %5u %s: trying rpcbind version %u\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800375 task->tk_pid, __func__, bind_version);
Chuck Levera5090502007-03-29 16:48:04 -0400376
Chuck Lever9f6ad262007-12-10 14:56:31 -0500377 rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot,
Chuck Lever423d8b02008-07-14 16:03:28 -0400378 bind_version);
Chuck Lever143b6c42007-08-16 16:03:31 -0400379 if (IS_ERR(rpcb_clnt)) {
380 status = PTR_ERR(rpcb_clnt);
381 dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800382 task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
Chuck Lever143b6c42007-08-16 16:03:31 -0400383 goto bailout_nofree;
384 }
385
Chuck Levera5090502007-03-29 16:48:04 -0400386 map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
387 if (!map) {
388 status = -ENOMEM;
Chuck Lever45160d62007-07-01 12:13:17 -0400389 dprintk("RPC: %5u %s: no memory available\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800390 task->tk_pid, __func__);
Chuck Levera5090502007-03-29 16:48:04 -0400391 goto bailout_nofree;
392 }
393 map->r_prog = clnt->cl_prog;
394 map->r_vers = clnt->cl_vers;
395 map->r_prot = xprt->prot;
396 map->r_port = 0;
397 map->r_xprt = xprt_get(xprt);
Trond Myklebust86d61d82008-01-07 21:16:56 -0500398 map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
399 map->r_addr = rpc_peeraddr2str(rpcb_clnt, RPC_DISPLAY_UNIVERSAL_ADDR);
Chuck Levera5090502007-03-29 16:48:04 -0400400 map->r_owner = RPCB_OWNER_STRING; /* ignored for GETADDR */
Trond Myklebust381ba742008-07-07 12:18:53 -0400401 map->r_status = -EIO;
Chuck Levera5090502007-03-29 16:48:04 -0400402
Trond Myklebust803a9062008-07-01 15:20:55 -0400403 child = rpcb_call_async(rpcb_clnt, map, proc);
Trond Myklebust4c402b42007-06-14 16:40:32 -0400404 rpc_release_client(rpcb_clnt);
Chuck Levera5090502007-03-29 16:48:04 -0400405 if (IS_ERR(child)) {
Trond Myklebust0d3a34b2008-07-07 12:18:52 -0400406 /* rpcb_map_release() has freed the arguments */
Chuck Lever45160d62007-07-01 12:13:17 -0400407 dprintk("RPC: %5u %s: rpc_run_task failed\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800408 task->tk_pid, __func__);
Trond Myklebust381ba742008-07-07 12:18:53 -0400409 return;
Chuck Levera5090502007-03-29 16:48:04 -0400410 }
411 rpc_put_task(child);
412
413 task->tk_xprt->stat.bind_count++;
414 return;
415
Chuck Levera5090502007-03-29 16:48:04 -0400416bailout_nofree:
417 rpcb_wake_rpcbind_waiters(xprt, status);
Chuck Levera5090502007-03-29 16:48:04 -0400418 task->tk_status = status;
419}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400420EXPORT_SYMBOL_GPL(rpcb_getport_async);
Chuck Levera5090502007-03-29 16:48:04 -0400421
422/*
423 * Rpcbind child task calls this callback via tk_exit.
424 */
425static void rpcb_getport_done(struct rpc_task *child, void *data)
426{
427 struct rpcbind_args *map = data;
428 struct rpc_xprt *xprt = map->r_xprt;
429 int status = child->tk_status;
430
Chuck Lever4784cb52007-09-11 18:00:36 -0400431 /* Garbage reply: retry with a lesser rpcbind version */
432 if (status == -EIO)
433 status = -EPROTONOSUPPORT;
434
Chuck Levera5090502007-03-29 16:48:04 -0400435 /* rpcbind server doesn't support this rpcbind protocol version */
436 if (status == -EPROTONOSUPPORT)
437 xprt->bind_index++;
438
439 if (status < 0) {
440 /* rpcbind server not available on remote host? */
441 xprt->ops->set_port(xprt, 0);
442 } else if (map->r_port == 0) {
443 /* Requested RPC service wasn't registered on remote host */
444 xprt->ops->set_port(xprt, 0);
445 status = -EACCES;
446 } else {
447 /* Succeeded */
448 xprt->ops->set_port(xprt, map->r_port);
449 xprt_set_bound(xprt);
450 status = 0;
451 }
452
453 dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
454 child->tk_pid, status, map->r_port);
455
Trond Myklebust381ba742008-07-07 12:18:53 -0400456 map->r_status = status;
Chuck Levera5090502007-03-29 16:48:04 -0400457}
458
Chuck Lever166b88d2008-07-14 16:03:26 -0400459/*
460 * XDR functions for rpcbind
461 */
462
Chuck Levera5090502007-03-29 16:48:04 -0400463static int rpcb_encode_mapping(struct rpc_rqst *req, __be32 *p,
464 struct rpcbind_args *rpcb)
465{
466 dprintk("RPC: rpcb_encode_mapping(%u, %u, %d, %u)\n",
467 rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
468 *p++ = htonl(rpcb->r_prog);
469 *p++ = htonl(rpcb->r_vers);
470 *p++ = htonl(rpcb->r_prot);
471 *p++ = htonl(rpcb->r_port);
472
473 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
474 return 0;
475}
476
477static int rpcb_decode_getport(struct rpc_rqst *req, __be32 *p,
478 unsigned short *portp)
479{
480 *portp = (unsigned short) ntohl(*p++);
Chuck Lever877fcf12008-06-25 17:24:23 -0400481 dprintk("RPC: rpcb_decode_getport result %u\n",
Chuck Levera5090502007-03-29 16:48:04 -0400482 *portp);
483 return 0;
484}
485
486static int rpcb_decode_set(struct rpc_rqst *req, __be32 *p,
487 unsigned int *boolp)
488{
489 *boolp = (unsigned int) ntohl(*p++);
Chuck Lever877fcf12008-06-25 17:24:23 -0400490 dprintk("RPC: rpcb_decode_set: call %s\n",
491 (*boolp ? "succeeded" : "failed"));
Chuck Levera5090502007-03-29 16:48:04 -0400492 return 0;
493}
494
495static int rpcb_encode_getaddr(struct rpc_rqst *req, __be32 *p,
496 struct rpcbind_args *rpcb)
497{
498 dprintk("RPC: rpcb_encode_getaddr(%u, %u, %s)\n",
499 rpcb->r_prog, rpcb->r_vers, rpcb->r_addr);
500 *p++ = htonl(rpcb->r_prog);
501 *p++ = htonl(rpcb->r_vers);
502
503 p = xdr_encode_string(p, rpcb->r_netid);
504 p = xdr_encode_string(p, rpcb->r_addr);
505 p = xdr_encode_string(p, rpcb->r_owner);
506
507 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
508
509 return 0;
510}
511
512static int rpcb_decode_getaddr(struct rpc_rqst *req, __be32 *p,
513 unsigned short *portp)
514{
515 char *addr;
Chuck Leveradc24df82007-08-06 11:56:31 -0400516 u32 addr_len;
517 int c, i, f, first, val;
Chuck Levera5090502007-03-29 16:48:04 -0400518
519 *portp = 0;
Chuck Leveradc24df82007-08-06 11:56:31 -0400520 addr_len = ntohl(*p++);
Chuck Levera5090502007-03-29 16:48:04 -0400521
Chuck Levere65fe392007-09-11 18:00:31 -0400522 /*
523 * Simple sanity check. The smallest possible universal
524 * address is an IPv4 address string containing 11 bytes.
525 */
Chuck Lever0fb2b7e2007-12-10 14:56:46 -0500526 if (addr_len < 11 || addr_len > RPCBIND_MAXUADDRLEN)
Chuck Levere65fe392007-09-11 18:00:31 -0400527 goto out_err;
Chuck Levera5090502007-03-29 16:48:04 -0400528
Chuck Levere65fe392007-09-11 18:00:31 -0400529 /*
530 * Start at the end and walk backwards until the first dot
531 * is encountered. When the second dot is found, we have
532 * both parts of the port number.
533 */
Chuck Levera5090502007-03-29 16:48:04 -0400534 addr = (char *)p;
535 val = 0;
536 first = 1;
537 f = 1;
538 for (i = addr_len - 1; i > 0; i--) {
539 c = addr[i];
540 if (c >= '0' && c <= '9') {
541 val += (c - '0') * f;
542 f *= 10;
543 } else if (c == '.') {
544 if (first) {
545 *portp = val;
546 val = first = 0;
547 f = 1;
548 } else {
549 *portp |= (val << 8);
550 break;
551 }
552 }
553 }
554
Chuck Levere65fe392007-09-11 18:00:31 -0400555 /*
556 * Simple sanity check. If we never saw a dot in the reply,
557 * then this was probably just garbage.
558 */
559 if (first)
560 goto out_err;
561
Chuck Levera5090502007-03-29 16:48:04 -0400562 dprintk("RPC: rpcb_decode_getaddr port=%u\n", *portp);
563 return 0;
Chuck Levere65fe392007-09-11 18:00:31 -0400564
565out_err:
566 dprintk("RPC: rpcbind server returned malformed reply\n");
567 return -EIO;
Chuck Levera5090502007-03-29 16:48:04 -0400568}
569
570#define RPCB_program_sz (1u)
571#define RPCB_version_sz (1u)
572#define RPCB_protocol_sz (1u)
573#define RPCB_port_sz (1u)
574#define RPCB_boolean_sz (1u)
575
\"Talpey, Thomas\4f40ee42007-09-10 13:42:38 -0400576#define RPCB_netid_sz (1+XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
Chuck Lever0fb2b7e2007-12-10 14:56:46 -0500577#define RPCB_addr_sz (1+XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
Chuck Levera5090502007-03-29 16:48:04 -0400578#define RPCB_ownerstring_sz (1+XDR_QUADLEN(RPCB_MAXOWNERLEN))
579
580#define RPCB_mappingargs_sz RPCB_program_sz+RPCB_version_sz+ \
581 RPCB_protocol_sz+RPCB_port_sz
582#define RPCB_getaddrargs_sz RPCB_program_sz+RPCB_version_sz+ \
583 RPCB_netid_sz+RPCB_addr_sz+ \
584 RPCB_ownerstring_sz
585
586#define RPCB_setres_sz RPCB_boolean_sz
587#define RPCB_getportres_sz RPCB_port_sz
588
589/*
590 * Note that RFC 1833 does not put any size restrictions on the
591 * address string returned by the remote rpcbind database.
592 */
593#define RPCB_getaddrres_sz RPCB_addr_sz
594
595#define PROC(proc, argtype, restype) \
596 [RPCBPROC_##proc] = { \
597 .p_proc = RPCBPROC_##proc, \
598 .p_encode = (kxdrproc_t) rpcb_encode_##argtype, \
599 .p_decode = (kxdrproc_t) rpcb_decode_##restype, \
600 .p_arglen = RPCB_##argtype##args_sz, \
601 .p_replen = RPCB_##restype##res_sz, \
602 .p_statidx = RPCBPROC_##proc, \
603 .p_timer = 0, \
604 .p_name = #proc, \
605 }
606
607/*
608 * Not all rpcbind procedures described in RFC 1833 are implemented
609 * since the Linux kernel RPC code requires only these.
610 */
611static struct rpc_procinfo rpcb_procedures2[] = {
612 PROC(SET, mapping, set),
613 PROC(UNSET, mapping, set),
Chuck Lever6a774052008-06-25 17:24:39 -0400614 PROC(GETPORT, mapping, getport),
Chuck Levera5090502007-03-29 16:48:04 -0400615};
616
617static struct rpc_procinfo rpcb_procedures3[] = {
Chuck Lever166b88d2008-07-14 16:03:26 -0400618 PROC(SET, getaddr, set),
619 PROC(UNSET, getaddr, set),
Chuck Levera5090502007-03-29 16:48:04 -0400620 PROC(GETADDR, getaddr, getaddr),
621};
622
623static struct rpc_procinfo rpcb_procedures4[] = {
Chuck Lever166b88d2008-07-14 16:03:26 -0400624 PROC(SET, getaddr, set),
625 PROC(UNSET, getaddr, set),
Chuck Lever88424132008-06-25 17:24:47 -0400626 PROC(GETADDR, getaddr, getaddr),
Chuck Levera5090502007-03-29 16:48:04 -0400627 PROC(GETVERSADDR, getaddr, getaddr),
628};
629
630static struct rpcb_info rpcb_next_version[] = {
Chuck Leverfc200e72008-06-25 17:24:31 -0400631 {
632 .rpc_vers = RPCBVERS_2,
633 .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
634 },
635 {
636 .rpc_proc = NULL,
637 },
Chuck Levera5090502007-03-29 16:48:04 -0400638};
639
Chuck Leverd5b64432007-08-06 11:57:18 -0400640static struct rpcb_info rpcb_next_version6[] = {
Chuck Leverfc200e72008-06-25 17:24:31 -0400641 {
642 .rpc_vers = RPCBVERS_4,
Chuck Lever88424132008-06-25 17:24:47 -0400643 .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR],
Chuck Leverfc200e72008-06-25 17:24:31 -0400644 },
645 {
646 .rpc_vers = RPCBVERS_3,
647 .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR],
648 },
Chuck Leverfc200e72008-06-25 17:24:31 -0400649 {
650 .rpc_proc = NULL,
651 },
Chuck Leverd5b64432007-08-06 11:57:18 -0400652};
653
Chuck Levera5090502007-03-29 16:48:04 -0400654static struct rpc_version rpcb_version2 = {
Chuck Leverfc200e72008-06-25 17:24:31 -0400655 .number = RPCBVERS_2,
Chuck Levera5090502007-03-29 16:48:04 -0400656 .nrprocs = RPCB_HIGHPROC_2,
657 .procs = rpcb_procedures2
658};
659
660static struct rpc_version rpcb_version3 = {
Chuck Leverfc200e72008-06-25 17:24:31 -0400661 .number = RPCBVERS_3,
Chuck Levera5090502007-03-29 16:48:04 -0400662 .nrprocs = RPCB_HIGHPROC_3,
663 .procs = rpcb_procedures3
664};
665
666static struct rpc_version rpcb_version4 = {
Chuck Leverfc200e72008-06-25 17:24:31 -0400667 .number = RPCBVERS_4,
Chuck Levera5090502007-03-29 16:48:04 -0400668 .nrprocs = RPCB_HIGHPROC_4,
669 .procs = rpcb_procedures4
670};
671
672static struct rpc_version *rpcb_version[] = {
673 NULL,
674 NULL,
675 &rpcb_version2,
676 &rpcb_version3,
677 &rpcb_version4
678};
679
680static struct rpc_stat rpcb_stats;
681
Adrian Bunk7f4adef2007-06-13 01:03:13 +0200682static struct rpc_program rpcb_program = {
Chuck Levera5090502007-03-29 16:48:04 -0400683 .name = "rpcbind",
684 .number = RPCBIND_PROGRAM,
685 .nrvers = ARRAY_SIZE(rpcb_version),
686 .version = rpcb_version,
687 .stats = &rpcb_stats,
688};