VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / net / sunrpc / pmap_clnt.c
1 /*
2  * linux/net/sunrpc/pmap.c
3  *
4  * Portmapper client.
5  *
6  * FIXME: In a secure environment, we may want to use an authentication
7  * flavor other than AUTH_NULL.
8  *
9  * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
10  */
11
12 #include <linux/config.h>
13 #include <linux/types.h>
14 #include <linux/socket.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/uio.h>
18 #include <linux/in.h>
19 #include <linux/sunrpc/clnt.h>
20 #include <linux/sunrpc/xprt.h>
21 #include <linux/sunrpc/sched.h>
22
23 #ifdef RPC_DEBUG
24 # define RPCDBG_FACILITY        RPCDBG_PMAP
25 #endif
26
27 #define PMAP_SET                1
28 #define PMAP_UNSET              2
29 #define PMAP_GETPORT            3
30
31 static struct rpc_procinfo      pmap_procedures[];
32 static struct rpc_clnt *        pmap_create(char *, struct sockaddr_in *, int);
33 static void                     pmap_getport_done(struct rpc_task *);
34 extern struct rpc_program       pmap_program;
35 static spinlock_t               pmap_lock = SPIN_LOCK_UNLOCKED;
36
37 /*
38  * Obtain the port for a given RPC service on a given host. This one can
39  * be called for an ongoing RPC request.
40  */
41 void
42 rpc_getport(struct rpc_task *task, struct rpc_clnt *clnt)
43 {
44         struct rpc_portmap *map = clnt->cl_pmap;
45         struct sockaddr_in *sap = &clnt->cl_xprt->addr;
46         struct rpc_message msg = {
47                 .rpc_proc       = &pmap_procedures[PMAP_GETPORT],
48                 .rpc_argp       = map,
49                 .rpc_resp       = &clnt->cl_port,
50                 .rpc_cred       = NULL
51         };
52         struct rpc_clnt *pmap_clnt;
53         struct rpc_task *child;
54
55         dprintk("RPC: %4d rpc_getport(%s, %d, %d, %d)\n",
56                         task->tk_pid, clnt->cl_server,
57                         map->pm_prog, map->pm_vers, map->pm_prot);
58
59         spin_lock(&pmap_lock);
60         if (map->pm_binding) {
61                 rpc_sleep_on(&map->pm_bindwait, task, NULL, NULL);
62                 spin_unlock(&pmap_lock);
63                 return;
64         }
65         map->pm_binding = 1;
66         spin_unlock(&pmap_lock);
67
68         pmap_clnt = pmap_create(clnt->cl_server, sap, map->pm_prot);
69         if (IS_ERR(pmap_clnt)) {
70                 task->tk_status = PTR_ERR(pmap_clnt);
71                 goto bailout;
72         }
73         task->tk_status = 0;
74
75         /*
76          * Note: rpc_new_child will release client after a failure.
77          */
78         if (!(child = rpc_new_child(pmap_clnt, task)))
79                 goto bailout;
80
81         /* Setup the call info struct */
82         rpc_call_setup(child, &msg, 0);
83
84         /* ... and run the child task */
85         rpc_run_child(task, child, pmap_getport_done);
86         return;
87
88 bailout:
89         spin_lock(&pmap_lock);
90         map->pm_binding = 0;
91         rpc_wake_up(&map->pm_bindwait);
92         spin_unlock(&pmap_lock);
93         task->tk_status = -EIO;
94         task->tk_action = NULL;
95 }
96
97 #ifdef CONFIG_ROOT_NFS
98 int
99 rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
100 {
101         struct rpc_portmap map = {
102                 .pm_prog        = prog,
103                 .pm_vers        = vers,
104                 .pm_prot        = prot,
105                 .pm_port        = 0
106         };
107         struct rpc_clnt *pmap_clnt;
108         char            hostname[32];
109         int             status;
110
111         dprintk("RPC:      rpc_getport_external(%u.%u.%u.%u, %d, %d, %d)\n",
112                         NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
113
114         sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr));
115         pmap_clnt = pmap_create(hostname, sin, prot);
116         if (IS_ERR(pmap_clnt))
117                 return PTR_ERR(pmap_clnt);
118
119         /* Setup the call info struct */
120         status = rpc_call(pmap_clnt, PMAP_GETPORT, &map, &map.pm_port, 0);
121
122         if (status >= 0) {
123                 if (map.pm_port != 0)
124                         return map.pm_port;
125                 status = -EACCES;
126         }
127         return status;
128 }
129 #endif
130
131 static void
132 pmap_getport_done(struct rpc_task *task)
133 {
134         struct rpc_clnt *clnt = task->tk_client;
135         struct rpc_portmap *map = clnt->cl_pmap;
136
137         dprintk("RPC: %4d pmap_getport_done(status %d, port %d)\n",
138                         task->tk_pid, task->tk_status, clnt->cl_port);
139         if (task->tk_status < 0) {
140                 /* Make the calling task exit with an error */
141                 task->tk_action = NULL;
142         } else if (clnt->cl_port == 0) {
143                 /* Program not registered */
144                 task->tk_status = -EACCES;
145                 task->tk_action = NULL;
146         } else {
147                 /* byte-swap port number first */
148                 clnt->cl_port = htons(clnt->cl_port);
149                 clnt->cl_xprt->addr.sin_port = clnt->cl_port;
150         }
151         spin_lock(&pmap_lock);
152         map->pm_binding = 0;
153         rpc_wake_up(&map->pm_bindwait);
154         spin_unlock(&pmap_lock);
155 }
156
157 /*
158  * Set or unset a port registration with the local portmapper.
159  * port == 0 means unregister, port != 0 means register.
160  */
161 int
162 rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
163 {
164         struct sockaddr_in      sin;
165         struct rpc_portmap      map;
166         struct rpc_clnt         *pmap_clnt;
167         int error = 0;
168
169         dprintk("RPC: registering (%d, %d, %d, %d) with portmapper.\n",
170                         prog, vers, prot, port);
171
172         sin.sin_family = AF_INET;
173         sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
174         pmap_clnt = pmap_create("localhost", &sin, IPPROTO_UDP);
175         if (IS_ERR(pmap_clnt)) {
176                 error = PTR_ERR(pmap_clnt);
177                 dprintk("RPC: couldn't create pmap client. Error = %d\n", error);
178                 return error;
179         }
180
181         map.pm_prog = prog;
182         map.pm_vers = vers;
183         map.pm_prot = prot;
184         map.pm_port = port;
185
186         error = rpc_call(pmap_clnt, port? PMAP_SET : PMAP_UNSET,
187                                         &map, okay, 0);
188
189         if (error < 0) {
190                 printk(KERN_WARNING
191                         "RPC: failed to contact portmap (errno %d).\n",
192                         error);
193         }
194         dprintk("RPC: registration status %d/%d\n", error, *okay);
195
196         /* Client deleted automatically because cl_oneshot == 1 */
197         return error;
198 }
199
200 static struct rpc_clnt *
201 pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto)
202 {
203         struct rpc_xprt *xprt;
204         struct rpc_clnt *clnt;
205
206         /* printk("pmap: create xprt\n"); */
207         xprt = xprt_create_proto(proto, srvaddr, NULL);
208         if (IS_ERR(xprt))
209                 return (struct rpc_clnt *)xprt;
210         xprt->addr.sin_port = htons(RPC_PMAP_PORT);
211
212         /* printk("pmap: create clnt\n"); */
213         clnt = rpc_create_client(xprt, hostname,
214                                 &pmap_program, RPC_PMAP_VERSION,
215                                 RPC_AUTH_NULL);
216         if (IS_ERR(clnt)) {
217                 xprt_destroy(xprt);
218         } else {
219                 clnt->cl_softrtry = 1;
220                 clnt->cl_chatty   = 1;
221                 clnt->cl_oneshot  = 1;
222         }
223         return clnt;
224 }
225
226 /*
227  * XDR encode/decode functions for PMAP
228  */
229 static int
230 xdr_encode_mapping(struct rpc_rqst *req, u32 *p, struct rpc_portmap *map)
231 {
232         dprintk("RPC: xdr_encode_mapping(%d, %d, %d, %d)\n",
233                 map->pm_prog, map->pm_vers, map->pm_prot, map->pm_port);
234         *p++ = htonl(map->pm_prog);
235         *p++ = htonl(map->pm_vers);
236         *p++ = htonl(map->pm_prot);
237         *p++ = htonl(map->pm_port);
238
239         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
240         return 0;
241 }
242
243 static int
244 xdr_decode_port(struct rpc_rqst *req, u32 *p, unsigned short *portp)
245 {
246         *portp = (unsigned short) ntohl(*p++);
247         return 0;
248 }
249
250 static int
251 xdr_decode_bool(struct rpc_rqst *req, u32 *p, unsigned int *boolp)
252 {
253         *boolp = (unsigned int) ntohl(*p++);
254         return 0;
255 }
256
257 static struct rpc_procinfo      pmap_procedures[] = {
258 [PMAP_SET] = {
259           .p_proc               = PMAP_SET,
260           .p_encode             = (kxdrproc_t) xdr_encode_mapping,      
261           .p_decode             = (kxdrproc_t) xdr_decode_bool,
262           .p_bufsiz             = 4,
263           .p_count              = 1,
264         },
265 [PMAP_UNSET] = {
266           .p_proc               = PMAP_UNSET,
267           .p_encode             = (kxdrproc_t) xdr_encode_mapping,      
268           .p_decode             = (kxdrproc_t) xdr_decode_bool,
269           .p_bufsiz             = 4,
270           .p_count              = 1,
271         },
272 [PMAP_GETPORT] = {
273           .p_proc               = PMAP_GETPORT,
274           .p_encode             = (kxdrproc_t) xdr_encode_mapping,
275           .p_decode             = (kxdrproc_t) xdr_decode_port,
276           .p_bufsiz             = 4,
277           .p_count              = 1,
278         },
279 };
280
281 static struct rpc_version       pmap_version2 = {
282         .number         = 2,
283         .nrprocs        = 4,
284         .procs          = pmap_procedures
285 };
286
287 static struct rpc_version *     pmap_version[] = {
288         NULL,
289         NULL,
290         &pmap_version2
291 };
292
293 static struct rpc_stat          pmap_stats;
294
295 struct rpc_program      pmap_program = {
296         .name           = "portmap",
297         .number         = RPC_PMAP_PROGRAM,
298         .nrvers         = ARRAY_SIZE(pmap_version),
299         .version        = pmap_version,
300         .stats          = &pmap_stats,
301 };