blob: d9f765344589e5e87b53443c3787b355d440305c [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{
Trond Myklebust54cc5332007-02-03 13:38:40 -080065 struct portmap_args *map = data;
66
67 xprt_put(map->pm_xprt);
68 pmap_map_free(map);
Chuck Lever4a681792006-08-22 20:06:15 -040069}
70
71static const struct rpc_call_ops pmap_getport_ops = {
72 .rpc_call_prepare = pmap_getport_prepare,
73 .rpc_call_done = pmap_getport_done,
74 .rpc_release = pmap_map_release,
75};
76
Trond Myklebust762d4522006-09-03 00:51:55 -040077static inline void pmap_wake_portmap_waiters(struct rpc_xprt *xprt, int status)
Chuck Lever4a681792006-08-22 20:06:15 -040078{
79 xprt_clear_binding(xprt);
Trond Myklebust762d4522006-09-03 00:51:55 -040080 rpc_wake_up_status(&xprt->binding, status);
Chuck Lever4a681792006-08-22 20:06:15 -040081}
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Chuck Leverc4a56922006-08-22 20:06:16 -040083/**
84 * rpc_getport - obtain the port for a given RPC service on a given host
85 * @task: task that is waiting for portmapper request
Chuck Leverc4a56922006-08-22 20:06:16 -040086 *
87 * This one can be called for an ongoing RPC request, and can be used in
88 * an async (rpciod) context.
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 */
Chuck Leverbbf7c1d2006-08-22 20:06:16 -040090void rpc_getport(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Chuck Leverbbf7c1d2006-08-22 20:06:16 -040092 struct rpc_clnt *clnt = task->tk_client;
Chuck Lever4a681792006-08-22 20:06:15 -040093 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Lever081f79a2006-08-22 20:06:17 -040094 struct sockaddr_in addr;
Chuck Lever4a681792006-08-22 20:06:15 -040095 struct portmap_args *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 struct rpc_clnt *pmap_clnt;
Chuck Lever4a681792006-08-22 20:06:15 -040097 struct rpc_task *child;
Trond Myklebust762d4522006-09-03 00:51:55 -040098 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Chuck Lever46121cf2007-01-31 12:14:08 -0500100 dprintk("RPC: %5u rpc_getport(%s, %u, %u, %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 task->tk_pid, clnt->cl_server,
Chuck Lever4a681792006-08-22 20:06:15 -0400102 clnt->cl_prog, clnt->cl_vers, xprt->prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Andreas Gruenbacher007e2512005-06-22 17:16:23 +0000104 /* Autobind on cloned rpc clients is discouraged */
105 BUG_ON(clnt->cl_parent != clnt);
106
Chuck Lever2b577f12006-11-16 15:03:38 -0500107 status = -EACCES; /* tell caller to check again */
108 if (xprt_test_and_set_binding(xprt))
109 goto bailout_nowake;
110
Chuck Lever71bdcf82006-10-19 23:28:43 -0700111 /* Put self on queue before sending rpcbind request, in case
112 * pmap_getport_done completes before we return from rpc_run_task */
113 rpc_sleep_on(&xprt->binding, task, NULL, NULL);
114
Chuck Lever4a681792006-08-22 20:06:15 -0400115 /* Someone else may have bound if we slept */
Trond Myklebust762d4522006-09-03 00:51:55 -0400116 status = 0;
117 if (xprt_bound(xprt))
Chuck Lever4a681792006-08-22 20:06:15 -0400118 goto bailout_nofree;
Chuck Lever4a681792006-08-22 20:06:15 -0400119
Trond Myklebust762d4522006-09-03 00:51:55 -0400120 status = -ENOMEM;
Chuck Lever4a681792006-08-22 20:06:15 -0400121 map = pmap_map_alloc();
Trond Myklebust762d4522006-09-03 00:51:55 -0400122 if (!map)
Chuck Lever4a681792006-08-22 20:06:15 -0400123 goto bailout_nofree;
Chuck Lever4a681792006-08-22 20:06:15 -0400124 map->pm_prog = clnt->cl_prog;
125 map->pm_vers = clnt->cl_vers;
126 map->pm_prot = xprt->prot;
127 map->pm_port = 0;
Trond Myklebust762d4522006-09-03 00:51:55 -0400128 map->pm_xprt = xprt_get(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Chuck Lever081f79a2006-08-22 20:06:17 -0400130 rpc_peeraddr(clnt, (struct sockaddr *) &addr, sizeof(addr));
131 pmap_clnt = pmap_create(clnt->cl_server, &addr, map->pm_prot, 0);
Trond Myklebust762d4522006-09-03 00:51:55 -0400132 status = PTR_ERR(pmap_clnt);
133 if (IS_ERR(pmap_clnt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 goto bailout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Trond Myklebust762d4522006-09-03 00:51:55 -0400136 status = -EIO;
Chuck Lever4a681792006-08-22 20:06:15 -0400137 child = rpc_run_task(pmap_clnt, RPC_TASK_ASYNC, &pmap_getport_ops, map);
Trond Myklebust762d4522006-09-03 00:51:55 -0400138 if (IS_ERR(child))
Trond Myklebust54cc5332007-02-03 13:38:40 -0800139 goto bailout_nofree;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -0500140 rpc_put_task(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Chuck Lever262ca072006-03-20 13:44:16 -0500142 task->tk_xprt->stat.bind_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 return;
144
145bailout:
Chuck Lever4a681792006-08-22 20:06:15 -0400146 pmap_map_free(map);
Trond Myklebust762d4522006-09-03 00:51:55 -0400147 xprt_put(xprt);
Chuck Lever4a681792006-08-22 20:06:15 -0400148bailout_nofree:
Trond Myklebust762d4522006-09-03 00:51:55 -0400149 pmap_wake_portmap_waiters(xprt, status);
Chuck Lever2b577f12006-11-16 15:03:38 -0500150bailout_nowake:
151 task->tk_status = status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
154#ifdef CONFIG_ROOT_NFS
Chuck Leverc4a56922006-08-22 20:06:16 -0400155/**
156 * rpc_getport_external - obtain the port for a given RPC service on a given host
157 * @sin: address of remote peer
158 * @prog: RPC program number to bind
159 * @vers: RPC version number to bind
160 * @prot: transport protocol to use to make this request
161 *
162 * This one is called from outside the RPC client in a synchronous task context.
163 */
164int rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
Chuck Lever4a681792006-08-22 20:06:15 -0400166 struct portmap_args map = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 .pm_prog = prog,
168 .pm_vers = vers,
169 .pm_prot = prot,
170 .pm_port = 0
171 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500172 struct rpc_message msg = {
173 .rpc_proc = &pmap_procedures[PMAP_GETPORT],
174 .rpc_argp = &map,
175 .rpc_resp = &map.pm_port,
176 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 struct rpc_clnt *pmap_clnt;
178 char hostname[32];
179 int status;
180
Chuck Lever46121cf2007-01-31 12:14:08 -0500181 dprintk("RPC: rpc_getport_external(%u.%u.%u.%u, %u, %u, %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
183
184 sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr));
Chuck Lever6cd75252005-09-22 21:24:59 -0400185 pmap_clnt = pmap_create(hostname, sin, prot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (IS_ERR(pmap_clnt))
187 return PTR_ERR(pmap_clnt);
188
189 /* Setup the call info struct */
Chuck Leverdead28d2006-03-20 13:44:23 -0500190 status = rpc_call_sync(pmap_clnt, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 if (status >= 0) {
193 if (map.pm_port != 0)
194 return map.pm_port;
195 status = -EACCES;
196 }
197 return status;
198}
199#endif
200
Chuck Leverc4a56922006-08-22 20:06:16 -0400201/*
202 * Portmapper child task invokes this callback via tk_exit.
203 */
204static void pmap_getport_done(struct rpc_task *child, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Chuck Lever4a681792006-08-22 20:06:15 -0400206 struct portmap_args *map = data;
Trond Myklebust762d4522006-09-03 00:51:55 -0400207 struct rpc_xprt *xprt = map->pm_xprt;
Chuck Lever4a681792006-08-22 20:06:15 -0400208 int status = child->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Chuck Lever4a681792006-08-22 20:06:15 -0400210 if (status < 0) {
211 /* Portmapper not available */
Chuck Leverec739ef2006-08-22 20:06:15 -0400212 xprt->ops->set_port(xprt, 0);
Chuck Lever4a681792006-08-22 20:06:15 -0400213 } else if (map->pm_port == 0) {
214 /* Requested RPC service wasn't registered */
Chuck Leverec739ef2006-08-22 20:06:15 -0400215 xprt->ops->set_port(xprt, 0);
Trond Myklebust762d4522006-09-03 00:51:55 -0400216 status = -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 } else {
Chuck Lever4a681792006-08-22 20:06:15 -0400218 /* Succeeded */
219 xprt->ops->set_port(xprt, map->pm_port);
Chuck Leverec739ef2006-08-22 20:06:15 -0400220 xprt_set_bound(xprt);
Trond Myklebust762d4522006-09-03 00:51:55 -0400221 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Chuck Lever4a681792006-08-22 20:06:15 -0400223
Chuck Lever46121cf2007-01-31 12:14:08 -0500224 dprintk("RPC: %5u pmap_getport_done(status %d, port %u)\n",
Trond Myklebust762d4522006-09-03 00:51:55 -0400225 child->tk_pid, status, map->pm_port);
Chuck Lever4a681792006-08-22 20:06:15 -0400226
Trond Myklebust762d4522006-09-03 00:51:55 -0400227 pmap_wake_portmap_waiters(xprt, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Chuck Leverc4a56922006-08-22 20:06:16 -0400230/**
231 * rpc_register - set or unset a port registration with the local portmapper
232 * @prog: RPC program number to bind
233 * @vers: RPC version number to bind
234 * @prot: transport protocol to use to make this request
235 * @port: port value to register
236 * @okay: result code
237 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * port == 0 means unregister, port != 0 means register.
239 */
Chuck Leverc4a56922006-08-22 20:06:16 -0400240int rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Chuck Leverdead28d2006-03-20 13:44:23 -0500242 struct sockaddr_in sin = {
243 .sin_family = AF_INET,
244 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
245 };
Chuck Lever4a681792006-08-22 20:06:15 -0400246 struct portmap_args map = {
Chuck Leverdead28d2006-03-20 13:44:23 -0500247 .pm_prog = prog,
248 .pm_vers = vers,
249 .pm_prot = prot,
250 .pm_port = port,
251 };
252 struct rpc_message msg = {
253 .rpc_proc = &pmap_procedures[port ? PMAP_SET : PMAP_UNSET],
254 .rpc_argp = &map,
255 .rpc_resp = okay,
256 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 struct rpc_clnt *pmap_clnt;
258 int error = 0;
259
Chuck Lever46121cf2007-01-31 12:14:08 -0500260 dprintk("RPC: registering (%u, %u, %d, %u) with portmapper.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 prog, vers, prot, port);
262
Chuck Lever6cd75252005-09-22 21:24:59 -0400263 pmap_clnt = pmap_create("localhost", &sin, IPPROTO_UDP, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (IS_ERR(pmap_clnt)) {
265 error = PTR_ERR(pmap_clnt);
Chuck Lever46121cf2007-01-31 12:14:08 -0500266 dprintk("RPC: couldn't create pmap client. Error = %d\n",
267 error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return error;
269 }
270
Chuck Leverdead28d2006-03-20 13:44:23 -0500271 error = rpc_call_sync(pmap_clnt, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 if (error < 0) {
274 printk(KERN_WARNING
275 "RPC: failed to contact portmap (errno %d).\n",
276 error);
277 }
Chuck Lever46121cf2007-01-31 12:14:08 -0500278 dprintk("RPC: registration status %d/%d\n", error, *okay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /* Client deleted automatically because cl_oneshot == 1 */
281 return error;
282}
283
Chuck Leverc4a56922006-08-22 20:06:16 -0400284static struct rpc_clnt *pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto, int privileged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Chuck Lever9e1968c2006-08-22 20:06:21 -0400286 struct rpc_create_args args = {
287 .protocol = proto,
288 .address = (struct sockaddr *)srvaddr,
289 .addrsize = sizeof(*srvaddr),
290 .servername = hostname,
291 .program = &pmap_program,
292 .version = RPC_PMAP_VERSION,
293 .authflavor = RPC_AUTH_UNIX,
294 .flags = (RPC_CLNT_CREATE_ONESHOT |
295 RPC_CLNT_CREATE_NOPING),
296 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Chuck Lever9e1968c2006-08-22 20:06:21 -0400298 srvaddr->sin_port = htons(RPC_PMAP_PORT);
Chuck Lever6cd75252005-09-22 21:24:59 -0400299 if (!privileged)
Chuck Lever9e1968c2006-08-22 20:06:21 -0400300 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
301 return rpc_create(&args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
304/*
305 * XDR encode/decode functions for PMAP
306 */
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700307static int xdr_encode_mapping(struct rpc_rqst *req, __be32 *p, struct portmap_args *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Chuck Lever46121cf2007-01-31 12:14:08 -0500309 dprintk("RPC: xdr_encode_mapping(%u, %u, %u, %u)\n",
310 map->pm_prog, map->pm_vers,
311 map->pm_prot, map->pm_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 *p++ = htonl(map->pm_prog);
313 *p++ = htonl(map->pm_vers);
314 *p++ = htonl(map->pm_prot);
315 *p++ = htonl(map->pm_port);
316
317 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
318 return 0;
319}
320
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700321static int xdr_decode_port(struct rpc_rqst *req, __be32 *p, unsigned short *portp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 *portp = (unsigned short) ntohl(*p++);
324 return 0;
325}
326
Alexey Dobriyand8ed0292006-09-26 22:29:38 -0700327static int xdr_decode_bool(struct rpc_rqst *req, __be32 *p, unsigned int *boolp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 *boolp = (unsigned int) ntohl(*p++);
330 return 0;
331}
332
333static struct rpc_procinfo pmap_procedures[] = {
334[PMAP_SET] = {
335 .p_proc = PMAP_SET,
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800336 .p_encode = (kxdrproc_t) xdr_encode_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 .p_decode = (kxdrproc_t) xdr_decode_bool,
338 .p_bufsiz = 4,
339 .p_count = 1,
Chuck Levercc0175c2006-03-20 13:44:22 -0500340 .p_statidx = PMAP_SET,
341 .p_name = "SET",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 },
343[PMAP_UNSET] = {
344 .p_proc = PMAP_UNSET,
YOSHIFUJI Hideakicca51722007-02-09 15:38:13 -0800345 .p_encode = (kxdrproc_t) xdr_encode_mapping,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 .p_decode = (kxdrproc_t) xdr_decode_bool,
347 .p_bufsiz = 4,
348 .p_count = 1,
Chuck Levercc0175c2006-03-20 13:44:22 -0500349 .p_statidx = PMAP_UNSET,
350 .p_name = "UNSET",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 },
352[PMAP_GETPORT] = {
353 .p_proc = PMAP_GETPORT,
354 .p_encode = (kxdrproc_t) xdr_encode_mapping,
355 .p_decode = (kxdrproc_t) xdr_decode_port,
356 .p_bufsiz = 4,
357 .p_count = 1,
Chuck Levercc0175c2006-03-20 13:44:22 -0500358 .p_statidx = PMAP_GETPORT,
359 .p_name = "GETPORT",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 },
361};
362
363static struct rpc_version pmap_version2 = {
364 .number = 2,
365 .nrprocs = 4,
366 .procs = pmap_procedures
367};
368
369static struct rpc_version * pmap_version[] = {
370 NULL,
371 NULL,
372 &pmap_version2
373};
374
375static struct rpc_stat pmap_stats;
376
377static struct rpc_program pmap_program = {
378 .name = "portmap",
379 .number = RPC_PMAP_PROGRAM,
380 .nrvers = ARRAY_SIZE(pmap_version),
381 .version = pmap_version,
382 .stats = &pmap_stats,
383};