blob: 3946ec3eb517a674881fc3b8d35277f2a5f3de44 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Chuck Leverc4a56922006-08-22 20:06:16 -04002 * linux/net/sunrpc/pmap_clnt.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Chuck Leverc4a56922006-08-22 20:06:16 -04004 * In-kernel RPC portmapper client.
5 *
6 * Portmapper supports version 2 of the rpcbind protocol (RFC 1833).
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
9 */
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/types.h>
12#include <linux/socket.h>
13#include <linux/kernel.h>
14#include <linux/errno.h>
15#include <linux/uio.h>
16#include <linux/in.h>
17#include <linux/sunrpc/clnt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/sunrpc/sched.h>
19
20#ifdef RPC_DEBUG
21# define RPCDBG_FACILITY RPCDBG_PMAP
22#endif
23
24#define PMAP_SET 1
25#define PMAP_UNSET 2
26#define PMAP_GETPORT 3
27
Chuck Lever4a681792006-08-22 20:06:15 -040028struct portmap_args {
29 u32 pm_prog;
30 u32 pm_vers;
31 u32 pm_prot;
32 unsigned short pm_port;
Trond Myklebust762d4522006-09-03 00:51:55 -040033 struct rpc_xprt * pm_xprt;
Chuck Lever4a681792006-08-22 20:06:15 -040034};
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036static struct rpc_procinfo pmap_procedures[];
Chuck Lever6cd75252005-09-22 21:24:59 -040037static struct rpc_clnt * pmap_create(char *, struct sockaddr_in *, int, int);
Chuck Lever4a681792006-08-22 20:06:15 -040038static void pmap_getport_done(struct rpc_task *, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static struct rpc_program pmap_program;
Chuck Lever4a681792006-08-22 20:06:15 -040040
41static void pmap_getport_prepare(struct rpc_task *task, void *calldata)
42{
43 struct portmap_args *map = calldata;
44 struct rpc_message msg = {
45 .rpc_proc = &pmap_procedures[PMAP_GETPORT],
46 .rpc_argp = map,
47 .rpc_resp = &map->pm_port,
48 };
49
50 rpc_call_setup(task, &msg, 0);
51}
52
53static inline struct portmap_args *pmap_map_alloc(void)
54{
55 return kmalloc(sizeof(struct portmap_args), GFP_NOFS);
56}
57
58static inline void pmap_map_free(struct portmap_args *map)
59{
60 kfree(map);
61}
62
63static void pmap_map_release(void *data)
64{
65 pmap_map_free(data);
66}
67
68static const struct rpc_call_ops pmap_getport_ops = {
69 .rpc_call_prepare = pmap_getport_prepare,
70 .rpc_call_done = pmap_getport_done,
71 .rpc_release = pmap_map_release,
72};
73
Trond Myklebust762d4522006-09-03 00:51:55 -040074static inline void pmap_wake_portmap_waiters(struct rpc_xprt *xprt, int status)
Chuck Lever4a681792006-08-22 20:06:15 -040075{
76 xprt_clear_binding(xprt);
Trond Myklebust762d4522006-09-03 00:51:55 -040077 rpc_wake_up_status(&xprt->binding, status);
Chuck Lever4a681792006-08-22 20:06:15 -040078}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Chuck Leverc4a56922006-08-22 20:06:16 -040080/**
81 * rpc_getport - obtain the port for a given RPC service on a given host
82 * @task: task that is waiting for portmapper request
Chuck Leverc4a56922006-08-22 20:06:16 -040083 *
84 * This one can be called for an ongoing RPC request, and can be used in
85 * an async (rpciod) context.
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 */
Chuck Leverbbf7c1d2006-08-22 20:06:16 -040087void rpc_getport(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Chuck Leverbbf7c1d2006-08-22 20:06:16 -040089 struct rpc_clnt *clnt = task->tk_client;
Chuck Lever4a681792006-08-22 20:06:15 -040090 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Lever081f79a2006-08-22 20:06:17 -040091 struct sockaddr_in addr;
Chuck Lever4a681792006-08-22 20:06:15 -040092 struct portmap_args *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 struct rpc_clnt *pmap_clnt;
Chuck Lever4a681792006-08-22 20:06:15 -040094 struct rpc_task *child;
Trond Myklebust762d4522006-09-03 00:51:55 -040095 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Chuck Lever4a681792006-08-22 20:06:15 -040097 dprintk("RPC: %4d rpc_getport(%s, %u, %u, %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 task->tk_pid, clnt->cl_server,
Chuck Lever4a681792006-08-22 20:06:15 -040099 clnt->cl_prog, clnt->cl_vers, xprt->prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000101 /* Autobind on cloned rpc clients is discouraged */
102 BUG_ON(clnt->cl_parent != clnt);
103
Chuck Lever2b577f12006-11-16 15:03:38 -0500104 status = -EACCES; /* tell caller to check again */
105 if (xprt_test_and_set_binding(xprt))
106 goto bailout_nowake;
107
Chuck Lever71bdcf82006-10-19 23:28:43 -0700108 /* Put self on queue before sending rpcbind request, in case
109 * pmap_getport_done completes before we return from rpc_run_task */
110 rpc_sleep_on(&xprt->binding, task, NULL, NULL);
111
Chuck Lever4a681792006-08-22 20:06:15 -0400112 /* Someone else may have bound if we slept */
Trond Myklebust762d4522006-09-03 00:51:55 -0400113 status = 0;
114 if (xprt_bound(xprt))
Chuck Lever4a681792006-08-22 20:06:15 -0400115 goto bailout_nofree;
Chuck Lever4a681792006-08-22 20:06:15 -0400116
Trond Myklebust762d4522006-09-03 00:51:55 -0400117 status = -ENOMEM;
Chuck Lever4a681792006-08-22 20:06:15 -0400118 map = pmap_map_alloc();
Trond Myklebust762d4522006-09-03 00:51:55 -0400119 if (!map)
Chuck Lever4a681792006-08-22 20:06:15 -0400120 goto bailout_nofree;
Chuck Lever4a681792006-08-22 20:06:15 -0400121 map->pm_prog = clnt->cl_prog;
122 map->pm_vers = clnt->cl_vers;
123 map->pm_prot = xprt->prot;
124 map->pm_port = 0;
Trond Myklebust762d4522006-09-03 00:51:55 -0400125 map->pm_xprt = xprt_get(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Chuck Lever081f79a2006-08-22 20:06:17 -0400127 rpc_peeraddr(clnt, (struct sockaddr *) &addr, sizeof(addr));
128 pmap_clnt = pmap_create(clnt->cl_server, &addr, map->pm_prot, 0);
Trond Myklebust762d4522006-09-03 00:51:55 -0400129 status = PTR_ERR(pmap_clnt);
130 if (IS_ERR(pmap_clnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 goto bailout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Trond Myklebust762d4522006-09-03 00:51:55 -0400133 status = -EIO;
Chuck Lever4a681792006-08-22 20:06:15 -0400134 child = rpc_run_task(pmap_clnt, RPC_TASK_ASYNC, &pmap_getport_ops, map);
Trond Myklebust762d4522006-09-03 00:51:55 -0400135 if (IS_ERR(child))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 goto bailout;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500137 rpc_put_task(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Chuck Lever262ca072006-03-20 13:44:16 -0500139 task->tk_xprt->stat.bind_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 return;
141
142bailout:
Chuck Lever4a681792006-08-22 20:06:15 -0400143 pmap_map_free(map);
Trond Myklebust762d4522006-09-03 00:51:55 -0400144 xprt_put(xprt);
Chuck Lever4a681792006-08-22 20:06:15 -0400145bailout_nofree:
Trond Myklebust762d4522006-09-03 00:51:55 -0400146 pmap_wake_portmap_waiters(xprt, status);
Chuck Lever2b577f12006-11-16 15:03:38 -0500147bailout_nowake:
148 task->tk_status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151#ifdef CONFIG_ROOT_NFS
Chuck Leverc4a56922006-08-22 20:06:16 -0400152/**
153 * rpc_getport_external - obtain the port for a given RPC service on a given host
154 * @sin: address of remote peer
155 * @prog: RPC program number to bind
156 * @vers: RPC version number to bind
157 * @prot: transport protocol to use to make this request
158 *
159 * This one is called from outside the RPC client in a synchronous task context.
160 */
161int rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Chuck Lever4a681792006-08-22 20:06:15 -0400163 struct portmap_args map = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 .pm_prog = prog,
165 .pm_vers = vers,
166 .pm_prot = prot,
167 .pm_port = 0
168 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500169 struct rpc_message msg = {
170 .rpc_proc = &pmap_procedures[PMAP_GETPORT],
171 .rpc_argp = &map,
172 .rpc_resp = &map.pm_port,
173 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 struct rpc_clnt *pmap_clnt;
175 char hostname[32];
176 int status;
177
Chuck Leverc4a56922006-08-22 20:06:16 -0400178 dprintk("RPC: rpc_getport_external(%u.%u.%u.%u, %u, %u, %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
180
181 sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr));
Chuck Lever6cd75252005-09-22 21:24:59 -0400182 pmap_clnt = pmap_create(hostname, sin, prot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (IS_ERR(pmap_clnt))
184 return PTR_ERR(pmap_clnt);
185
186 /* Setup the call info struct */
Chuck Leverdead28d2006-03-20 13:44:23 -0500187 status = rpc_call_sync(pmap_clnt, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 if (status >= 0) {
190 if (map.pm_port != 0)
191 return map.pm_port;
192 status = -EACCES;
193 }
194 return status;
195}
196#endif
197
Chuck Leverc4a56922006-08-22 20:06:16 -0400198/*
199 * Portmapper child task invokes this callback via tk_exit.
200 */
201static void pmap_getport_done(struct rpc_task *child, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Chuck Lever4a681792006-08-22 20:06:15 -0400203 struct portmap_args *map = data;
Trond Myklebust762d4522006-09-03 00:51:55 -0400204 struct rpc_xprt *xprt = map->pm_xprt;
Chuck Lever4a681792006-08-22 20:06:15 -0400205 int status = child->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Chuck Lever4a681792006-08-22 20:06:15 -0400207 if (status < 0) {
208 /* Portmapper not available */
Chuck Leverec739ef2006-08-22 20:06:15 -0400209 xprt->ops->set_port(xprt, 0);
Chuck Lever4a681792006-08-22 20:06:15 -0400210 } else if (map->pm_port == 0) {
211 /* Requested RPC service wasn't registered */
Chuck Leverec739ef2006-08-22 20:06:15 -0400212 xprt->ops->set_port(xprt, 0);
Trond Myklebust762d4522006-09-03 00:51:55 -0400213 status = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 } else {
Chuck Lever4a681792006-08-22 20:06:15 -0400215 /* Succeeded */
216 xprt->ops->set_port(xprt, map->pm_port);
Chuck Leverec739ef2006-08-22 20:06:15 -0400217 xprt_set_bound(xprt);
Trond Myklebust762d4522006-09-03 00:51:55 -0400218 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Chuck Lever4a681792006-08-22 20:06:15 -0400220
221 dprintk("RPC: %4d pmap_getport_done(status %d, port %u)\n",
Trond Myklebust762d4522006-09-03 00:51:55 -0400222 child->tk_pid, status, map->pm_port);
Chuck Lever4a681792006-08-22 20:06:15 -0400223
Trond Myklebust762d4522006-09-03 00:51:55 -0400224 pmap_wake_portmap_waiters(xprt, status);
225 xprt_put(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Chuck Leverc4a56922006-08-22 20:06:16 -0400228/**
229 * rpc_register - set or unset a port registration with the local portmapper
230 * @prog: RPC program number to bind
231 * @vers: RPC version number to bind
232 * @prot: transport protocol to use to make this request
233 * @port: port value to register
234 * @okay: result code
235 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * port == 0 means unregister, port != 0 means register.
237 */
Chuck Leverc4a56922006-08-22 20:06:16 -0400238int rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Chuck Leverdead28d2006-03-20 13:44:23 -0500240 struct sockaddr_in sin = {
241 .sin_family = AF_INET,
242 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
243 };
Chuck Lever4a681792006-08-22 20:06:15 -0400244 struct portmap_args map = {
Chuck Leverdead28d2006-03-20 13:44:23 -0500245 .pm_prog = prog,
246 .pm_vers = vers,
247 .pm_prot = prot,
248 .pm_port = port,
249 };
250 struct rpc_message msg = {
251 .rpc_proc = &pmap_procedures[port ? PMAP_SET : PMAP_UNSET],
252 .rpc_argp = &map,
253 .rpc_resp = okay,
254 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 struct rpc_clnt *pmap_clnt;
256 int error = 0;
257
Chuck Leverc4a56922006-08-22 20:06:16 -0400258 dprintk("RPC: registering (%u, %u, %d, %u) with portmapper.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 prog, vers, prot, port);
260
Chuck Lever6cd75252005-09-22 21:24:59 -0400261 pmap_clnt = pmap_create("localhost", &sin, IPPROTO_UDP, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (IS_ERR(pmap_clnt)) {
263 error = PTR_ERR(pmap_clnt);
264 dprintk("RPC: couldn't create pmap client. Error = %d\n", error);
265 return error;
266 }
267
Chuck Leverdead28d2006-03-20 13:44:23 -0500268 error = rpc_call_sync(pmap_clnt, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 if (error < 0) {
271 printk(KERN_WARNING
272 "RPC: failed to contact portmap (errno %d).\n",
273 error);
274 }
275 dprintk("RPC: registration status %d/%d\n", error, *okay);
276
277 /* Client deleted automatically because cl_oneshot == 1 */
278 return error;
279}
280
Chuck Leverc4a56922006-08-22 20:06:16 -0400281static struct rpc_clnt *pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto, int privileged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Chuck Lever9e1968c2006-08-22 20:06:21 -0400283 struct rpc_create_args args = {
284 .protocol = proto,
285 .address = (struct sockaddr *)srvaddr,
286 .addrsize = sizeof(*srvaddr),
287 .servername = hostname,
288 .program = &pmap_program,
289 .version = RPC_PMAP_VERSION,
290 .authflavor = RPC_AUTH_UNIX,
291 .flags = (RPC_CLNT_CREATE_ONESHOT |
292 RPC_CLNT_CREATE_NOPING),
293 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Chuck Lever9e1968c2006-08-22 20:06:21 -0400295 srvaddr->sin_port = htons(RPC_PMAP_PORT);
Chuck Lever6cd75252005-09-22 21:24:59 -0400296 if (!privileged)
Chuck Lever9e1968c2006-08-22 20:06:21 -0400297 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
298 return rpc_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
301/*
302 * XDR encode/decode functions for PMAP
303 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700304static int xdr_encode_mapping(struct rpc_rqst *req, __be32 *p, struct portmap_args *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Chuck Leverc4a56922006-08-22 20:06:16 -0400306 dprintk("RPC: xdr_encode_mapping(%u, %u, %u, %u)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 map->pm_prog, map->pm_vers, map->pm_prot, map->pm_port);
308 *p++ = htonl(map->pm_prog);
309 *p++ = htonl(map->pm_vers);
310 *p++ = htonl(map->pm_prot);
311 *p++ = htonl(map->pm_port);
312
313 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
314 return 0;
315}
316
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700317static int xdr_decode_port(struct rpc_rqst *req, __be32 *p, unsigned short *portp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 *portp = (unsigned short) ntohl(*p++);
320 return 0;
321}
322
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700323static int xdr_decode_bool(struct rpc_rqst *req, __be32 *p, unsigned int *boolp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 *boolp = (unsigned int) ntohl(*p++);
326 return 0;
327}
328
329static struct rpc_procinfo pmap_procedures[] = {
330[PMAP_SET] = {
331 .p_proc = PMAP_SET,
332 .p_encode = (kxdrproc_t) xdr_encode_mapping,
333 .p_decode = (kxdrproc_t) xdr_decode_bool,
334 .p_bufsiz = 4,
335 .p_count = 1,
Chuck Levercc0175c2006-03-20 13:44:22 -0500336 .p_statidx = PMAP_SET,
337 .p_name = "SET",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 },
339[PMAP_UNSET] = {
340 .p_proc = PMAP_UNSET,
341 .p_encode = (kxdrproc_t) xdr_encode_mapping,
342 .p_decode = (kxdrproc_t) xdr_decode_bool,
343 .p_bufsiz = 4,
344 .p_count = 1,
Chuck Levercc0175c2006-03-20 13:44:22 -0500345 .p_statidx = PMAP_UNSET,
346 .p_name = "UNSET",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 },
348[PMAP_GETPORT] = {
349 .p_proc = PMAP_GETPORT,
350 .p_encode = (kxdrproc_t) xdr_encode_mapping,
351 .p_decode = (kxdrproc_t) xdr_decode_port,
352 .p_bufsiz = 4,
353 .p_count = 1,
Chuck Levercc0175c2006-03-20 13:44:22 -0500354 .p_statidx = PMAP_GETPORT,
355 .p_name = "GETPORT",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 },
357};
358
359static struct rpc_version pmap_version2 = {
360 .number = 2,
361 .nrprocs = 4,
362 .procs = pmap_procedures
363};
364
365static struct rpc_version * pmap_version[] = {
366 NULL,
367 NULL,
368 &pmap_version2
369};
370
371static struct rpc_stat pmap_stats;
372
373static struct rpc_program pmap_program = {
374 .name = "portmap",
375 .number = RPC_PMAP_PROGRAM,
376 .nrvers = ARRAY_SIZE(pmap_version),
377 .version = pmap_version,
378 .stats = &pmap_stats,
379};