]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - net/sunrpc/pmap_clnt.c
SUNRPC: Convert RPC portmapper to use new rpc_create() API
[linux-2.6-omap-h63xx.git] / net / sunrpc / pmap_clnt.c
1 /*
2  * linux/net/sunrpc/pmap_clnt.c
3  *
4  * In-kernel RPC portmapper client.
5  *
6  * Portmapper supports version 2 of the rpcbind protocol (RFC 1833).
7  *
8  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
9  */
10
11 #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/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
28 struct portmap_args {
29         u32                     pm_prog;
30         u32                     pm_vers;
31         u32                     pm_prot;
32         unsigned short          pm_port;
33         struct rpc_task *       pm_task;
34 };
35
36 static struct rpc_procinfo      pmap_procedures[];
37 static struct rpc_clnt *        pmap_create(char *, struct sockaddr_in *, int, int);
38 static void                     pmap_getport_done(struct rpc_task *, void *);
39 static struct rpc_program       pmap_program;
40
41 static 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
53 static inline struct portmap_args *pmap_map_alloc(void)
54 {
55         return kmalloc(sizeof(struct portmap_args), GFP_NOFS);
56 }
57
58 static inline void pmap_map_free(struct portmap_args *map)
59 {
60         kfree(map);
61 }
62
63 static void pmap_map_release(void *data)
64 {
65         pmap_map_free(data);
66 }
67
68 static 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
74 static inline void pmap_wake_portmap_waiters(struct rpc_xprt *xprt)
75 {
76         xprt_clear_binding(xprt);
77         rpc_wake_up(&xprt->binding);
78 }
79
80 /**
81  * rpc_getport - obtain the port for a given RPC service on a given host
82  * @task: task that is waiting for portmapper request
83  *
84  * This one can be called for an ongoing RPC request, and can be used in
85  * an async (rpciod) context.
86  */
87 void rpc_getport(struct rpc_task *task)
88 {
89         struct rpc_clnt *clnt = task->tk_client;
90         struct rpc_xprt *xprt = task->tk_xprt;
91         struct sockaddr_in addr;
92         struct portmap_args *map;
93         struct rpc_clnt *pmap_clnt;
94         struct rpc_task *child;
95
96         dprintk("RPC: %4d rpc_getport(%s, %u, %u, %d)\n",
97                         task->tk_pid, clnt->cl_server,
98                         clnt->cl_prog, clnt->cl_vers, xprt->prot);
99
100         /* Autobind on cloned rpc clients is discouraged */
101         BUG_ON(clnt->cl_parent != clnt);
102
103         if (xprt_test_and_set_binding(xprt)) {
104                 task->tk_status = -EACCES;      /* tell caller to check again */
105                 rpc_sleep_on(&xprt->binding, task, NULL, NULL);
106                 return;
107         }
108
109         /* Someone else may have bound if we slept */
110         if (xprt_bound(xprt)) {
111                 task->tk_status = 0;
112                 goto bailout_nofree;
113         }
114
115         map = pmap_map_alloc();
116         if (!map) {
117                 task->tk_status = -ENOMEM;
118                 goto bailout_nofree;
119         }
120         map->pm_prog = clnt->cl_prog;
121         map->pm_vers = clnt->cl_vers;
122         map->pm_prot = xprt->prot;
123         map->pm_port = 0;
124         map->pm_task = task;
125
126         rpc_peeraddr(clnt, (struct sockaddr *) &addr, sizeof(addr));
127         pmap_clnt = pmap_create(clnt->cl_server, &addr, map->pm_prot, 0);
128         if (IS_ERR(pmap_clnt)) {
129                 task->tk_status = PTR_ERR(pmap_clnt);
130                 goto bailout;
131         }
132
133         child = rpc_run_task(pmap_clnt, RPC_TASK_ASYNC, &pmap_getport_ops, map);
134         if (IS_ERR(child)) {
135                 task->tk_status = -EIO;
136                 goto bailout;
137         }
138         rpc_release_task(child);
139
140         rpc_sleep_on(&xprt->binding, task, NULL, NULL);
141
142         task->tk_xprt->stat.bind_count++;
143         return;
144
145 bailout:
146         pmap_map_free(map);
147 bailout_nofree:
148         pmap_wake_portmap_waiters(xprt);
149 }
150
151 #ifdef CONFIG_ROOT_NFS
152 /**
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  */
161 int rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
162 {
163         struct portmap_args map = {
164                 .pm_prog        = prog,
165                 .pm_vers        = vers,
166                 .pm_prot        = prot,
167                 .pm_port        = 0
168         };
169         struct rpc_message msg = {
170                 .rpc_proc       = &pmap_procedures[PMAP_GETPORT],
171                 .rpc_argp       = &map,
172                 .rpc_resp       = &map.pm_port,
173         };
174         struct rpc_clnt *pmap_clnt;
175         char            hostname[32];
176         int             status;
177
178         dprintk("RPC:      rpc_getport_external(%u.%u.%u.%u, %u, %u, %d)\n",
179                         NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
180
181         sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr));
182         pmap_clnt = pmap_create(hostname, sin, prot, 0);
183         if (IS_ERR(pmap_clnt))
184                 return PTR_ERR(pmap_clnt);
185
186         /* Setup the call info struct */
187         status = rpc_call_sync(pmap_clnt, &msg, 0);
188
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
198 /*
199  * Portmapper child task invokes this callback via tk_exit.
200  */
201 static void pmap_getport_done(struct rpc_task *child, void *data)
202 {
203         struct portmap_args *map = data;
204         struct rpc_task *task = map->pm_task;
205         struct rpc_xprt *xprt = task->tk_xprt;
206         int status = child->tk_status;
207
208         if (status < 0) {
209                 /* Portmapper not available */
210                 xprt->ops->set_port(xprt, 0);
211                 task->tk_status = status;
212         } else if (map->pm_port == 0) {
213                 /* Requested RPC service wasn't registered */
214                 xprt->ops->set_port(xprt, 0);
215                 task->tk_status = -EACCES;
216         } else {
217                 /* Succeeded */
218                 xprt->ops->set_port(xprt, map->pm_port);
219                 xprt_set_bound(xprt);
220                 task->tk_status = 0;
221         }
222
223         dprintk("RPC: %4d pmap_getport_done(status %d, port %u)\n",
224                         child->tk_pid, child->tk_status, map->pm_port);
225
226         pmap_wake_portmap_waiters(xprt);
227 }
228
229 /**
230  * rpc_register - set or unset a port registration with the local portmapper
231  * @prog: RPC program number to bind
232  * @vers: RPC version number to bind
233  * @prot: transport protocol to use to make this request
234  * @port: port value to register
235  * @okay: result code
236  *
237  * port == 0 means unregister, port != 0 means register.
238  */
239 int rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
240 {
241         struct sockaddr_in      sin = {
242                 .sin_family     = AF_INET,
243                 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
244         };
245         struct portmap_args     map = {
246                 .pm_prog        = prog,
247                 .pm_vers        = vers,
248                 .pm_prot        = prot,
249                 .pm_port        = port,
250         };
251         struct rpc_message msg = {
252                 .rpc_proc       = &pmap_procedures[port ? PMAP_SET : PMAP_UNSET],
253                 .rpc_argp       = &map,
254                 .rpc_resp       = okay,
255         };
256         struct rpc_clnt         *pmap_clnt;
257         int error = 0;
258
259         dprintk("RPC: registering (%u, %u, %d, %u) with portmapper.\n",
260                         prog, vers, prot, port);
261
262         pmap_clnt = pmap_create("localhost", &sin, IPPROTO_UDP, 1);
263         if (IS_ERR(pmap_clnt)) {
264                 error = PTR_ERR(pmap_clnt);
265                 dprintk("RPC: couldn't create pmap client. Error = %d\n", error);
266                 return error;
267         }
268
269         error = rpc_call_sync(pmap_clnt, &msg, 0);
270
271         if (error < 0) {
272                 printk(KERN_WARNING
273                         "RPC: failed to contact portmap (errno %d).\n",
274                         error);
275         }
276         dprintk("RPC: registration status %d/%d\n", error, *okay);
277
278         /* Client deleted automatically because cl_oneshot == 1 */
279         return error;
280 }
281
282 static struct rpc_clnt *pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto, int privileged)
283 {
284         struct rpc_create_args args = {
285                 .protocol       = proto,
286                 .address        = (struct sockaddr *)srvaddr,
287                 .addrsize       = sizeof(*srvaddr),
288                 .servername     = hostname,
289                 .program        = &pmap_program,
290                 .version        = RPC_PMAP_VERSION,
291                 .authflavor     = RPC_AUTH_UNIX,
292                 .flags          = (RPC_CLNT_CREATE_ONESHOT |
293                                    RPC_CLNT_CREATE_NOPING),
294         };
295
296         srvaddr->sin_port = htons(RPC_PMAP_PORT);
297         if (!privileged)
298                 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
299         return rpc_create(&args);
300 }
301
302 /*
303  * XDR encode/decode functions for PMAP
304  */
305 static int xdr_encode_mapping(struct rpc_rqst *req, u32 *p, struct portmap_args *map)
306 {
307         dprintk("RPC: xdr_encode_mapping(%u, %u, %u, %u)\n",
308                 map->pm_prog, map->pm_vers, map->pm_prot, map->pm_port);
309         *p++ = htonl(map->pm_prog);
310         *p++ = htonl(map->pm_vers);
311         *p++ = htonl(map->pm_prot);
312         *p++ = htonl(map->pm_port);
313
314         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
315         return 0;
316 }
317
318 static int xdr_decode_port(struct rpc_rqst *req, u32 *p, unsigned short *portp)
319 {
320         *portp = (unsigned short) ntohl(*p++);
321         return 0;
322 }
323
324 static int xdr_decode_bool(struct rpc_rqst *req, u32 *p, unsigned int *boolp)
325 {
326         *boolp = (unsigned int) ntohl(*p++);
327         return 0;
328 }
329
330 static struct rpc_procinfo      pmap_procedures[] = {
331 [PMAP_SET] = {
332           .p_proc               = PMAP_SET,
333           .p_encode             = (kxdrproc_t) xdr_encode_mapping,      
334           .p_decode             = (kxdrproc_t) xdr_decode_bool,
335           .p_bufsiz             = 4,
336           .p_count              = 1,
337           .p_statidx            = PMAP_SET,
338           .p_name               = "SET",
339         },
340 [PMAP_UNSET] = {
341           .p_proc               = PMAP_UNSET,
342           .p_encode             = (kxdrproc_t) xdr_encode_mapping,      
343           .p_decode             = (kxdrproc_t) xdr_decode_bool,
344           .p_bufsiz             = 4,
345           .p_count              = 1,
346           .p_statidx            = PMAP_UNSET,
347           .p_name               = "UNSET",
348         },
349 [PMAP_GETPORT] = {
350           .p_proc               = PMAP_GETPORT,
351           .p_encode             = (kxdrproc_t) xdr_encode_mapping,
352           .p_decode             = (kxdrproc_t) xdr_decode_port,
353           .p_bufsiz             = 4,
354           .p_count              = 1,
355           .p_statidx            = PMAP_GETPORT,
356           .p_name               = "GETPORT",
357         },
358 };
359
360 static struct rpc_version       pmap_version2 = {
361         .number         = 2,
362         .nrprocs        = 4,
363         .procs          = pmap_procedures
364 };
365
366 static struct rpc_version *     pmap_version[] = {
367         NULL,
368         NULL,
369         &pmap_version2
370 };
371
372 static struct rpc_stat          pmap_stats;
373
374 static struct rpc_program       pmap_program = {
375         .name           = "portmap",
376         .number         = RPC_PMAP_PROGRAM,
377         .nrvers         = ARRAY_SIZE(pmap_version),
378         .version        = pmap_version,
379         .stats          = &pmap_stats,
380 };