MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / net / sunrpc / pmap_clnt.c
blob2320451c0f58264f1c566da9d4e32173478f5f93
1 /*
2 * linux/net/sunrpc/pmap_clnt.c
4 * In-kernel RPC portmapper client.
6 * Portmapper supports version 2 of the rpcbind protocol (RFC 1833).
8 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
9 */
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>
20 #ifdef RPC_DEBUG
21 # define RPCDBG_FACILITY RPCDBG_PMAP
22 #endif
24 #define PMAP_SET 1
25 #define PMAP_UNSET 2
26 #define PMAP_GETPORT 3
28 struct portmap_args {
29 u32 pm_prog;
30 u32 pm_vers;
31 u32 pm_prot;
32 unsigned short pm_port;
33 struct rpc_xprt * pm_xprt;
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;
41 static void pmap_getport_prepare(struct rpc_task *task, void *calldata)
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,
50 rpc_call_setup(task, &msg, 0);
53 static inline struct portmap_args *pmap_map_alloc(void)
55 return kmalloc(sizeof(struct portmap_args), GFP_NOFS);
58 static inline void pmap_map_free(struct portmap_args *map)
60 kfree(map);
63 static void pmap_map_release(void *data)
65 pmap_map_free(data);
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,
74 static inline void pmap_wake_portmap_waiters(struct rpc_xprt *xprt, int status)
76 xprt_clear_binding(xprt);
77 rpc_wake_up_status(&xprt->binding, status);
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
84 * This one can be called for an ongoing RPC request, and can be used in
85 * an async (rpciod) context.
87 void rpc_getport(struct rpc_task *task)
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 int status;
97 dprintk("RPC: %4d rpc_getport(%s, %u, %u, %d)\n",
98 task->tk_pid, clnt->cl_server,
99 clnt->cl_prog, clnt->cl_vers, xprt->prot);
101 /* Autobind on cloned rpc clients is discouraged */
102 BUG_ON(clnt->cl_parent != clnt);
104 /* Put self on queue before sending rpcbind request, in case
105 * pmap_getport_done completes before we return from rpc_run_task */
106 rpc_sleep_on(&xprt->binding, task, NULL, NULL);
108 status = -EACCES; /* tell caller to check again */
109 if (xprt_test_and_set_binding(xprt))
110 goto bailout_nofree;
112 /* Someone else may have bound if we slept */
113 status = 0;
114 if (xprt_bound(xprt))
115 goto bailout_nofree;
117 status = -ENOMEM;
118 map = pmap_map_alloc();
119 if (!map)
120 goto bailout_nofree;
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_xprt = xprt_get(xprt);
127 rpc_peeraddr(clnt, (struct sockaddr *) &addr, sizeof(addr));
128 pmap_clnt = pmap_create(clnt->cl_server, &addr, map->pm_prot, 0);
129 status = PTR_ERR(pmap_clnt);
130 if (IS_ERR(pmap_clnt))
131 goto bailout;
133 status = -EIO;
134 child = rpc_run_task(pmap_clnt, RPC_TASK_ASYNC, &pmap_getport_ops, map);
135 if (IS_ERR(child))
136 goto bailout;
137 rpc_release_task(child);
139 task->tk_xprt->stat.bind_count++;
140 return;
142 bailout:
143 pmap_map_free(map);
144 xprt_put(xprt);
145 bailout_nofree:
146 task->tk_status = status;
147 pmap_wake_portmap_waiters(xprt, status);
150 #ifdef CONFIG_ROOT_NFS
152 * rpc_getport_external - obtain the port for a given RPC service on a given host
153 * @sin: address of remote peer
154 * @prog: RPC program number to bind
155 * @vers: RPC version number to bind
156 * @prot: transport protocol to use to make this request
158 * This one is called from outside the RPC client in a synchronous task context.
160 int rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
162 struct portmap_args map = {
163 .pm_prog = prog,
164 .pm_vers = vers,
165 .pm_prot = prot,
166 .pm_port = 0
168 struct rpc_message msg = {
169 .rpc_proc = &pmap_procedures[PMAP_GETPORT],
170 .rpc_argp = &map,
171 .rpc_resp = &map.pm_port,
173 struct rpc_clnt *pmap_clnt;
174 char hostname[32];
175 int status;
177 dprintk("RPC: rpc_getport_external(%u.%u.%u.%u, %u, %u, %d)\n",
178 NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
180 sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr));
181 pmap_clnt = pmap_create(hostname, sin, prot, 0);
182 if (IS_ERR(pmap_clnt))
183 return PTR_ERR(pmap_clnt);
185 /* Setup the call info struct */
186 status = rpc_call_sync(pmap_clnt, &msg, 0);
188 if (status >= 0) {
189 if (map.pm_port != 0)
190 return map.pm_port;
191 status = -EACCES;
193 return status;
195 #endif
198 * Portmapper child task invokes this callback via tk_exit.
200 static void pmap_getport_done(struct rpc_task *child, void *data)
202 struct portmap_args *map = data;
203 struct rpc_xprt *xprt = map->pm_xprt;
204 int status = child->tk_status;
206 if (status < 0) {
207 /* Portmapper not available */
208 xprt->ops->set_port(xprt, 0);
209 } else if (map->pm_port == 0) {
210 /* Requested RPC service wasn't registered */
211 xprt->ops->set_port(xprt, 0);
212 status = -EACCES;
213 } else {
214 /* Succeeded */
215 xprt->ops->set_port(xprt, map->pm_port);
216 xprt_set_bound(xprt);
217 status = 0;
220 dprintk("RPC: %4d pmap_getport_done(status %d, port %u)\n",
221 child->tk_pid, status, map->pm_port);
223 pmap_wake_portmap_waiters(xprt, status);
224 xprt_put(xprt);
228 * rpc_register - set or unset a port registration with the local portmapper
229 * @prog: RPC program number to bind
230 * @vers: RPC version number to bind
231 * @prot: transport protocol to use to make this request
232 * @port: port value to register
233 * @okay: result code
235 * port == 0 means unregister, port != 0 means register.
237 int rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
239 struct sockaddr_in sin = {
240 .sin_family = AF_INET,
241 #if 0 // mask by Victor Yu. 02-12-2007
242 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
243 #endif
245 struct portmap_args map = {
246 .pm_prog = prog,
247 .pm_vers = vers,
248 .pm_prot = prot,
249 .pm_port = port,
251 struct rpc_message msg = {
252 .rpc_proc = &pmap_procedures[port ? PMAP_SET : PMAP_UNSET],
253 .rpc_argp = &map,
254 .rpc_resp = okay,
256 struct rpc_clnt *pmap_clnt;
257 int error = 0;
259 #if 1 // add by Victor Yu. 02-12-2007
260 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
261 #endif
262 dprintk("RPC: registering (%u, %u, %d, %u) with portmapper.\n",
263 prog, vers, prot, port);
265 pmap_clnt = pmap_create("localhost", &sin, IPPROTO_UDP, 1);
266 if (IS_ERR(pmap_clnt)) {
267 error = PTR_ERR(pmap_clnt);
268 dprintk("RPC: couldn't create pmap client. Error = %d\n", error);
269 return error;
272 error = rpc_call_sync(pmap_clnt, &msg, 0);
274 if (error < 0) {
275 printk(KERN_WARNING
276 "RPC: failed to contact portmap (errno %d).\n",
277 error);
279 dprintk("RPC: registration status %d/%d\n", error, *okay);
281 /* Client deleted automatically because cl_oneshot == 1 */
282 return error;
285 static struct rpc_clnt *pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto, int privileged)
287 struct rpc_create_args args = {
288 .protocol = proto,
289 .address = (struct sockaddr *)srvaddr,
290 .addrsize = sizeof(*srvaddr),
291 .servername = hostname,
292 .program = &pmap_program,
293 .version = RPC_PMAP_VERSION,
294 .authflavor = RPC_AUTH_UNIX,
295 .flags = (RPC_CLNT_CREATE_ONESHOT |
296 RPC_CLNT_CREATE_NOPING),
299 srvaddr->sin_port = htons(RPC_PMAP_PORT);
300 if (!privileged)
301 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
302 return rpc_create(&args);
306 * XDR encode/decode functions for PMAP
308 static int xdr_encode_mapping(struct rpc_rqst *req, __be32 *p, struct portmap_args *map)
310 dprintk("RPC: xdr_encode_mapping(%u, %u, %u, %u)\n",
311 map->pm_prog, map->pm_vers, map->pm_prot, map->pm_port);
312 *p++ = htonl(map->pm_prog);
313 *p++ = htonl(map->pm_vers);
314 *p++ = htonl(map->pm_prot);
315 *p++ = htonl(map->pm_port);
317 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
318 return 0;
321 static int xdr_decode_port(struct rpc_rqst *req, __be32 *p, unsigned short *portp)
323 *portp = (unsigned short) ntohl(*p++);
324 return 0;
327 static int xdr_decode_bool(struct rpc_rqst *req, __be32 *p, unsigned int *boolp)
329 *boolp = (unsigned int) ntohl(*p++);
330 return 0;
333 static struct rpc_procinfo pmap_procedures[] = {
334 [PMAP_SET] = {
335 .p_proc = PMAP_SET,
336 .p_encode = (kxdrproc_t) xdr_encode_mapping,
337 .p_decode = (kxdrproc_t) xdr_decode_bool,
338 .p_bufsiz = 4,
339 .p_count = 1,
340 .p_statidx = PMAP_SET,
341 .p_name = "SET",
343 [PMAP_UNSET] = {
344 .p_proc = PMAP_UNSET,
345 .p_encode = (kxdrproc_t) xdr_encode_mapping,
346 .p_decode = (kxdrproc_t) xdr_decode_bool,
347 .p_bufsiz = 4,
348 .p_count = 1,
349 .p_statidx = PMAP_UNSET,
350 .p_name = "UNSET",
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,
358 .p_statidx = PMAP_GETPORT,
359 .p_name = "GETPORT",
363 static struct rpc_version pmap_version2 = {
364 .number = 2,
365 .nrprocs = 4,
366 .procs = pmap_procedures
369 static struct rpc_version * pmap_version[] = {
370 NULL,
371 NULL,
372 &pmap_version2
375 static struct rpc_stat pmap_stats;
377 static 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,