blob: 3eee8e907275c7269ef69c3d35d02c636b25590e [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>
18#include <linux/sunrpc/xprt.h>
19#include <linux/sunrpc/sched.h>
20
21#ifdef RPC_DEBUG
22# define RPCDBG_FACILITY RPCDBG_PMAP
23#endif
24
25#define PMAP_SET 1
26#define PMAP_UNSET 2
27#define PMAP_GETPORT 3
28
Chuck Lever4a681792006-08-22 20:06:15 -040029struct portmap_args {
30 u32 pm_prog;
31 u32 pm_vers;
32 u32 pm_prot;
33 unsigned short pm_port;
34 struct rpc_task * pm_task;
35};
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037static struct rpc_procinfo pmap_procedures[];
Chuck Lever6cd75252005-09-22 21:24:59 -040038static struct rpc_clnt * pmap_create(char *, struct sockaddr_in *, int, int);
Chuck Lever4a681792006-08-22 20:06:15 -040039static void pmap_getport_done(struct rpc_task *, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040static struct rpc_program pmap_program;
Chuck Lever4a681792006-08-22 20:06:15 -040041
42static void pmap_getport_prepare(struct rpc_task *task, void *calldata)
43{
44 struct portmap_args *map = calldata;
45 struct rpc_message msg = {
46 .rpc_proc = &pmap_procedures[PMAP_GETPORT],
47 .rpc_argp = map,
48 .rpc_resp = &map->pm_port,
49 };
50
51 rpc_call_setup(task, &msg, 0);
52}
53
54static inline struct portmap_args *pmap_map_alloc(void)
55{
56 return kmalloc(sizeof(struct portmap_args), GFP_NOFS);
57}
58
59static inline void pmap_map_free(struct portmap_args *map)
60{
61 kfree(map);
62}
63
64static void pmap_map_release(void *data)
65{
66 pmap_map_free(data);
67}
68
69static const struct rpc_call_ops pmap_getport_ops = {
70 .rpc_call_prepare = pmap_getport_prepare,
71 .rpc_call_done = pmap_getport_done,
72 .rpc_release = pmap_map_release,
73};
74
75static inline void pmap_wake_portmap_waiters(struct rpc_xprt *xprt)
76{
77 xprt_clear_binding(xprt);
78 rpc_wake_up(&xprt->binding);
79}
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Chuck Leverc4a56922006-08-22 20:06:16 -040081/**
82 * rpc_getport - obtain the port for a given RPC service on a given host
83 * @task: task that is waiting for portmapper request
Chuck Leverc4a56922006-08-22 20:06:16 -040084 *
85 * This one can be called for an ongoing RPC request, and can be used in
86 * an async (rpciod) context.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
Chuck Leverbbf7c1d2006-08-22 20:06:16 -040088void rpc_getport(struct rpc_task *task)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
Chuck Leverbbf7c1d2006-08-22 20:06:16 -040090 struct rpc_clnt *clnt = task->tk_client;
Chuck Lever4a681792006-08-22 20:06:15 -040091 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Lever081f79a2006-08-22 20:06:17 -040092 struct sockaddr_in addr;
Chuck Lever4a681792006-08-22 20:06:15 -040093 struct portmap_args *map;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct rpc_clnt *pmap_clnt;
Chuck Lever4a681792006-08-22 20:06:15 -040095 struct rpc_task *child;
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 Lever4a681792006-08-22 20:06:15 -0400104 if (xprt_test_and_set_binding(xprt)) {
105 task->tk_status = -EACCES; /* tell caller to check again */
106 rpc_sleep_on(&xprt->binding, task, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 return;
108 }
Chuck Lever4a681792006-08-22 20:06:15 -0400109
110 /* Someone else may have bound if we slept */
111 if (xprt_bound(xprt)) {
112 task->tk_status = 0;
113 goto bailout_nofree;
114 }
115
116 map = pmap_map_alloc();
117 if (!map) {
118 task->tk_status = -ENOMEM;
119 goto bailout_nofree;
120 }
121 map->pm_prog = clnt->cl_prog;
122 map->pm_vers = clnt->cl_vers;
123 map->pm_prot = xprt->prot;
124 map->pm_port = 0;
125 map->pm_task = task;
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (IS_ERR(pmap_clnt)) {
130 task->tk_status = PTR_ERR(pmap_clnt);
131 goto bailout;
132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Chuck Lever4a681792006-08-22 20:06:15 -0400134 child = rpc_run_task(pmap_clnt, RPC_TASK_ASYNC, &pmap_getport_ops, map);
135 if (IS_ERR(child)) {
136 task->tk_status = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 goto bailout;
Chuck Lever4a681792006-08-22 20:06:15 -0400138 }
139 rpc_release_task(child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Chuck Lever4a681792006-08-22 20:06:15 -0400141 rpc_sleep_on(&xprt->binding, task, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Chuck Lever262ca072006-03-20 13:44:16 -0500143 task->tk_xprt->stat.bind_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return;
145
146bailout:
Chuck Lever4a681792006-08-22 20:06:15 -0400147 pmap_map_free(map);
148bailout_nofree:
149 pmap_wake_portmap_waiters(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
152#ifdef CONFIG_ROOT_NFS
Chuck Leverc4a56922006-08-22 20:06:16 -0400153/**
154 * rpc_getport_external - obtain the port for a given RPC service on a given host
155 * @sin: address of remote peer
156 * @prog: RPC program number to bind
157 * @vers: RPC version number to bind
158 * @prot: transport protocol to use to make this request
159 *
160 * This one is called from outside the RPC client in a synchronous task context.
161 */
162int rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Chuck Lever4a681792006-08-22 20:06:15 -0400164 struct portmap_args map = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 .pm_prog = prog,
166 .pm_vers = vers,
167 .pm_prot = prot,
168 .pm_port = 0
169 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500170 struct rpc_message msg = {
171 .rpc_proc = &pmap_procedures[PMAP_GETPORT],
172 .rpc_argp = &map,
173 .rpc_resp = &map.pm_port,
174 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 struct rpc_clnt *pmap_clnt;
176 char hostname[32];
177 int status;
178
Chuck Leverc4a56922006-08-22 20:06:16 -0400179 dprintk("RPC: rpc_getport_external(%u.%u.%u.%u, %u, %u, %d)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
181
182 sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr));
Chuck Lever6cd75252005-09-22 21:24:59 -0400183 pmap_clnt = pmap_create(hostname, sin, prot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (IS_ERR(pmap_clnt))
185 return PTR_ERR(pmap_clnt);
186
187 /* Setup the call info struct */
Chuck Leverdead28d2006-03-20 13:44:23 -0500188 status = rpc_call_sync(pmap_clnt, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 if (status >= 0) {
191 if (map.pm_port != 0)
192 return map.pm_port;
193 status = -EACCES;
194 }
195 return status;
196}
197#endif
198
Chuck Leverc4a56922006-08-22 20:06:16 -0400199/*
200 * Portmapper child task invokes this callback via tk_exit.
201 */
202static void pmap_getport_done(struct rpc_task *child, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Chuck Lever4a681792006-08-22 20:06:15 -0400204 struct portmap_args *map = data;
205 struct rpc_task *task = map->pm_task;
Chuck Lever92200412006-01-03 09:55:51 +0100206 struct rpc_xprt *xprt = task->tk_xprt;
Chuck Lever4a681792006-08-22 20:06:15 -0400207 int status = child->tk_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Chuck Lever4a681792006-08-22 20:06:15 -0400209 if (status < 0) {
210 /* Portmapper not available */
Chuck Leverec739ef2006-08-22 20:06:15 -0400211 xprt->ops->set_port(xprt, 0);
Chuck Lever4a681792006-08-22 20:06:15 -0400212 task->tk_status = status;
213 } 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);
Chuck Lever4a681792006-08-22 20:06:15 -0400216 task->tk_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);
Chuck Lever4a681792006-08-22 20:06:15 -0400221 task->tk_status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Chuck Lever4a681792006-08-22 20:06:15 -0400223
224 dprintk("RPC: %4d pmap_getport_done(status %d, port %u)\n",
225 child->tk_pid, child->tk_status, map->pm_port);
226
227 pmap_wake_portmap_waiters(xprt);
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 Leverc4a56922006-08-22 20:06:16 -0400260 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);
266 dprintk("RPC: couldn't create pmap client. Error = %d\n", error);
267 return error;
268 }
269
Chuck Leverdead28d2006-03-20 13:44:23 -0500270 error = rpc_call_sync(pmap_clnt, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if (error < 0) {
273 printk(KERN_WARNING
274 "RPC: failed to contact portmap (errno %d).\n",
275 error);
276 }
277 dprintk("RPC: registration status %d/%d\n", error, *okay);
278
279 /* Client deleted automatically because cl_oneshot == 1 */
280 return error;
281}
282
Chuck Leverc4a56922006-08-22 20:06:16 -0400283static struct rpc_clnt *pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto, int privileged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 struct rpc_xprt *xprt;
286 struct rpc_clnt *clnt;
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 xprt = xprt_create_proto(proto, srvaddr, NULL);
289 if (IS_ERR(xprt))
290 return (struct rpc_clnt *)xprt;
Chuck Lever92200412006-01-03 09:55:51 +0100291 xprt->ops->set_port(xprt, RPC_PMAP_PORT);
Chuck Leverec739ef2006-08-22 20:06:15 -0400292 xprt_set_bound(xprt);
Chuck Lever6cd75252005-09-22 21:24:59 -0400293 if (!privileged)
294 xprt->resvport = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Trond Myklebust5ee0ed72005-06-22 17:16:20 +0000296 clnt = rpc_new_client(xprt, hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 &pmap_program, RPC_PMAP_VERSION,
298 RPC_AUTH_UNIX);
Trond Myklebust5b616f52005-06-22 17:16:20 +0000299 if (!IS_ERR(clnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 clnt->cl_softrtry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 clnt->cl_oneshot = 1;
302 }
303 return clnt;
304}
305
306/*
307 * XDR encode/decode functions for PMAP
308 */
Chuck Leverc4a56922006-08-22 20:06:16 -0400309static int xdr_encode_mapping(struct rpc_rqst *req, u32 *p, struct portmap_args *map)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
Chuck Leverc4a56922006-08-22 20:06:16 -0400311 dprintk("RPC: xdr_encode_mapping(%u, %u, %u, %u)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 map->pm_prog, map->pm_vers, map->pm_prot, map->pm_port);
313 *p++ = htonl(map->pm_prog);
314 *p++ = htonl(map->pm_vers);
315 *p++ = htonl(map->pm_prot);
316 *p++ = htonl(map->pm_port);
317
318 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
319 return 0;
320}
321
Chuck Leverc4a56922006-08-22 20:06:16 -0400322static int xdr_decode_port(struct rpc_rqst *req, u32 *p, unsigned short *portp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 *portp = (unsigned short) ntohl(*p++);
325 return 0;
326}
327
Chuck Leverc4a56922006-08-22 20:06:16 -0400328static int xdr_decode_bool(struct rpc_rqst *req, u32 *p, unsigned int *boolp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 *boolp = (unsigned int) ntohl(*p++);
331 return 0;
332}
333
334static struct rpc_procinfo pmap_procedures[] = {
335[PMAP_SET] = {
336 .p_proc = PMAP_SET,
337 .p_encode = (kxdrproc_t) xdr_encode_mapping,
338 .p_decode = (kxdrproc_t) xdr_decode_bool,
339 .p_bufsiz = 4,
340 .p_count = 1,
Chuck Levercc0175c2006-03-20 13:44:22 -0500341 .p_statidx = PMAP_SET,
342 .p_name = "SET",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 },
344[PMAP_UNSET] = {
345 .p_proc = PMAP_UNSET,
346 .p_encode = (kxdrproc_t) xdr_encode_mapping,
347 .p_decode = (kxdrproc_t) xdr_decode_bool,
348 .p_bufsiz = 4,
349 .p_count = 1,
Chuck Levercc0175c2006-03-20 13:44:22 -0500350 .p_statidx = PMAP_UNSET,
351 .p_name = "UNSET",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 },
353[PMAP_GETPORT] = {
354 .p_proc = PMAP_GETPORT,
355 .p_encode = (kxdrproc_t) xdr_encode_mapping,
356 .p_decode = (kxdrproc_t) xdr_decode_port,
357 .p_bufsiz = 4,
358 .p_count = 1,
Chuck Levercc0175c2006-03-20 13:44:22 -0500359 .p_statidx = PMAP_GETPORT,
360 .p_name = "GETPORT",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 },
362};
363
364static struct rpc_version pmap_version2 = {
365 .number = 2,
366 .nrprocs = 4,
367 .procs = pmap_procedures
368};
369
370static struct rpc_version * pmap_version[] = {
371 NULL,
372 NULL,
373 &pmap_version2
374};
375
376static struct rpc_stat pmap_stats;
377
378static struct rpc_program pmap_program = {
379 .name = "portmap",
380 .number = RPC_PMAP_PROGRAM,
381 .nrvers = ARRAY_SIZE(pmap_version),
382 .version = pmap_version,
383 .stats = &pmap_stats,
384};