blob: 209ffdfee10be93b52ae49569bd4ccd081da38e0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/net/sunrpc/pmap.c
3 *
4 * Portmapper client.
5 *
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/types.h>
10#include <linux/socket.h>
11#include <linux/kernel.h>
12#include <linux/errno.h>
13#include <linux/uio.h>
14#include <linux/in.h>
15#include <linux/sunrpc/clnt.h>
16#include <linux/sunrpc/xprt.h>
17#include <linux/sunrpc/sched.h>
18
19#ifdef RPC_DEBUG
20# define RPCDBG_FACILITY RPCDBG_PMAP
21#endif
22
23#define PMAP_SET 1
24#define PMAP_UNSET 2
25#define PMAP_GETPORT 3
26
27static struct rpc_procinfo pmap_procedures[];
Chuck Lever6cd75252005-09-22 21:24:59 -040028static struct rpc_clnt * pmap_create(char *, struct sockaddr_in *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static void pmap_getport_done(struct rpc_task *);
30static struct rpc_program pmap_program;
31static DEFINE_SPINLOCK(pmap_lock);
32
33/*
34 * Obtain the port for a given RPC service on a given host. This one can
35 * be called for an ongoing RPC request.
36 */
37void
38rpc_getport(struct rpc_task *task, struct rpc_clnt *clnt)
39{
40 struct rpc_portmap *map = clnt->cl_pmap;
41 struct sockaddr_in *sap = &clnt->cl_xprt->addr;
42 struct rpc_message msg = {
43 .rpc_proc = &pmap_procedures[PMAP_GETPORT],
44 .rpc_argp = map,
45 .rpc_resp = &clnt->cl_port,
46 .rpc_cred = NULL
47 };
48 struct rpc_clnt *pmap_clnt;
49 struct rpc_task *child;
50
51 dprintk("RPC: %4d rpc_getport(%s, %d, %d, %d)\n",
52 task->tk_pid, clnt->cl_server,
53 map->pm_prog, map->pm_vers, map->pm_prot);
54
Andreas Gruenbacher007e2512005-06-22 17:16:23 +000055 /* Autobind on cloned rpc clients is discouraged */
56 BUG_ON(clnt->cl_parent != clnt);
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 spin_lock(&pmap_lock);
59 if (map->pm_binding) {
60 rpc_sleep_on(&map->pm_bindwait, task, NULL, NULL);
61 spin_unlock(&pmap_lock);
62 return;
63 }
64 map->pm_binding = 1;
65 spin_unlock(&pmap_lock);
66
Chuck Lever6cd75252005-09-22 21:24:59 -040067 pmap_clnt = pmap_create(clnt->cl_server, sap, map->pm_prot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (IS_ERR(pmap_clnt)) {
69 task->tk_status = PTR_ERR(pmap_clnt);
70 goto bailout;
71 }
72 task->tk_status = 0;
73
74 /*
75 * Note: rpc_new_child will release client after a failure.
76 */
77 if (!(child = rpc_new_child(pmap_clnt, task)))
78 goto bailout;
79
80 /* Setup the call info struct */
81 rpc_call_setup(child, &msg, 0);
82
83 /* ... and run the child task */
Chuck Lever262ca072006-03-20 13:44:16 -050084 task->tk_xprt->stat.bind_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 rpc_run_child(task, child, pmap_getport_done);
86 return;
87
88bailout:
89 spin_lock(&pmap_lock);
90 map->pm_binding = 0;
91 rpc_wake_up(&map->pm_bindwait);
92 spin_unlock(&pmap_lock);
Trond Myklebustabbcf282006-01-03 09:55:03 +010093 rpc_exit(task, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
95
96#ifdef CONFIG_ROOT_NFS
97int
98rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
99{
100 struct rpc_portmap map = {
101 .pm_prog = prog,
102 .pm_vers = vers,
103 .pm_prot = prot,
104 .pm_port = 0
105 };
Chuck Leverdead28d2006-03-20 13:44:23 -0500106 struct rpc_message msg = {
107 .rpc_proc = &pmap_procedures[PMAP_GETPORT],
108 .rpc_argp = &map,
109 .rpc_resp = &map.pm_port,
110 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 struct rpc_clnt *pmap_clnt;
112 char hostname[32];
113 int status;
114
115 dprintk("RPC: rpc_getport_external(%u.%u.%u.%u, %d, %d, %d)\n",
116 NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
117
118 sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr));
Chuck Lever6cd75252005-09-22 21:24:59 -0400119 pmap_clnt = pmap_create(hostname, sin, prot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (IS_ERR(pmap_clnt))
121 return PTR_ERR(pmap_clnt);
122
123 /* Setup the call info struct */
Chuck Leverdead28d2006-03-20 13:44:23 -0500124 status = rpc_call_sync(pmap_clnt, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 if (status >= 0) {
127 if (map.pm_port != 0)
128 return map.pm_port;
129 status = -EACCES;
130 }
131 return status;
132}
133#endif
134
135static void
136pmap_getport_done(struct rpc_task *task)
137{
138 struct rpc_clnt *clnt = task->tk_client;
Chuck Lever92200412006-01-03 09:55:51 +0100139 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 struct rpc_portmap *map = clnt->cl_pmap;
141
142 dprintk("RPC: %4d pmap_getport_done(status %d, port %d)\n",
143 task->tk_pid, task->tk_status, clnt->cl_port);
Chuck Lever92200412006-01-03 09:55:51 +0100144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (task->tk_status < 0) {
146 /* Make the calling task exit with an error */
Chuck Leverec739ef2006-08-22 20:06:15 -0400147 xprt->ops->set_port(xprt, 0);
Trond Myklebustabbcf282006-01-03 09:55:03 +0100148 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 } else if (clnt->cl_port == 0) {
150 /* Program not registered */
Chuck Leverec739ef2006-08-22 20:06:15 -0400151 xprt->ops->set_port(xprt, 0);
Trond Myklebustabbcf282006-01-03 09:55:03 +0100152 rpc_exit(task, -EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 } else {
Chuck Lever92200412006-01-03 09:55:51 +0100154 xprt->ops->set_port(xprt, clnt->cl_port);
Chuck Leverec739ef2006-08-22 20:06:15 -0400155 xprt_set_bound(xprt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 clnt->cl_port = htons(clnt->cl_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 }
158 spin_lock(&pmap_lock);
159 map->pm_binding = 0;
160 rpc_wake_up(&map->pm_bindwait);
161 spin_unlock(&pmap_lock);
162}
163
164/*
165 * Set or unset a port registration with the local portmapper.
166 * port == 0 means unregister, port != 0 means register.
167 */
168int
169rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
170{
Chuck Leverdead28d2006-03-20 13:44:23 -0500171 struct sockaddr_in sin = {
172 .sin_family = AF_INET,
173 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
174 };
175 struct rpc_portmap map = {
176 .pm_prog = prog,
177 .pm_vers = vers,
178 .pm_prot = prot,
179 .pm_port = port,
180 };
181 struct rpc_message msg = {
182 .rpc_proc = &pmap_procedures[port ? PMAP_SET : PMAP_UNSET],
183 .rpc_argp = &map,
184 .rpc_resp = okay,
185 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 struct rpc_clnt *pmap_clnt;
187 int error = 0;
188
189 dprintk("RPC: registering (%d, %d, %d, %d) with portmapper.\n",
190 prog, vers, prot, port);
191
Chuck Lever6cd75252005-09-22 21:24:59 -0400192 pmap_clnt = pmap_create("localhost", &sin, IPPROTO_UDP, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if (IS_ERR(pmap_clnt)) {
194 error = PTR_ERR(pmap_clnt);
195 dprintk("RPC: couldn't create pmap client. Error = %d\n", error);
196 return error;
197 }
198
Chuck Leverdead28d2006-03-20 13:44:23 -0500199 error = rpc_call_sync(pmap_clnt, &msg, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 if (error < 0) {
202 printk(KERN_WARNING
203 "RPC: failed to contact portmap (errno %d).\n",
204 error);
205 }
206 dprintk("RPC: registration status %d/%d\n", error, *okay);
207
208 /* Client deleted automatically because cl_oneshot == 1 */
209 return error;
210}
211
212static struct rpc_clnt *
Chuck Lever6cd75252005-09-22 21:24:59 -0400213pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto, int privileged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 struct rpc_xprt *xprt;
216 struct rpc_clnt *clnt;
217
218 /* printk("pmap: create xprt\n"); */
219 xprt = xprt_create_proto(proto, srvaddr, NULL);
220 if (IS_ERR(xprt))
221 return (struct rpc_clnt *)xprt;
Chuck Lever92200412006-01-03 09:55:51 +0100222 xprt->ops->set_port(xprt, RPC_PMAP_PORT);
Chuck Leverec739ef2006-08-22 20:06:15 -0400223 xprt_set_bound(xprt);
Chuck Lever6cd75252005-09-22 21:24:59 -0400224 if (!privileged)
225 xprt->resvport = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
227 /* printk("pmap: create clnt\n"); */
Trond Myklebust5ee0ed72005-06-22 17:16:20 +0000228 clnt = rpc_new_client(xprt, hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 &pmap_program, RPC_PMAP_VERSION,
230 RPC_AUTH_UNIX);
Trond Myklebust5b616f52005-06-22 17:16:20 +0000231 if (!IS_ERR(clnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 clnt->cl_softrtry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 clnt->cl_oneshot = 1;
234 }
235 return clnt;
236}
237
238/*
239 * XDR encode/decode functions for PMAP
240 */
241static int
242xdr_encode_mapping(struct rpc_rqst *req, u32 *p, struct rpc_portmap *map)
243{
244 dprintk("RPC: xdr_encode_mapping(%d, %d, %d, %d)\n",
245 map->pm_prog, map->pm_vers, map->pm_prot, map->pm_port);
246 *p++ = htonl(map->pm_prog);
247 *p++ = htonl(map->pm_vers);
248 *p++ = htonl(map->pm_prot);
249 *p++ = htonl(map->pm_port);
250
251 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
252 return 0;
253}
254
255static int
256xdr_decode_port(struct rpc_rqst *req, u32 *p, unsigned short *portp)
257{
258 *portp = (unsigned short) ntohl(*p++);
259 return 0;
260}
261
262static int
263xdr_decode_bool(struct rpc_rqst *req, u32 *p, unsigned int *boolp)
264{
265 *boolp = (unsigned int) ntohl(*p++);
266 return 0;
267}
268
269static struct rpc_procinfo pmap_procedures[] = {
270[PMAP_SET] = {
271 .p_proc = PMAP_SET,
272 .p_encode = (kxdrproc_t) xdr_encode_mapping,
273 .p_decode = (kxdrproc_t) xdr_decode_bool,
274 .p_bufsiz = 4,
275 .p_count = 1,
Chuck Levercc0175c2006-03-20 13:44:22 -0500276 .p_statidx = PMAP_SET,
277 .p_name = "SET",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 },
279[PMAP_UNSET] = {
280 .p_proc = PMAP_UNSET,
281 .p_encode = (kxdrproc_t) xdr_encode_mapping,
282 .p_decode = (kxdrproc_t) xdr_decode_bool,
283 .p_bufsiz = 4,
284 .p_count = 1,
Chuck Levercc0175c2006-03-20 13:44:22 -0500285 .p_statidx = PMAP_UNSET,
286 .p_name = "UNSET",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 },
288[PMAP_GETPORT] = {
289 .p_proc = PMAP_GETPORT,
290 .p_encode = (kxdrproc_t) xdr_encode_mapping,
291 .p_decode = (kxdrproc_t) xdr_decode_port,
292 .p_bufsiz = 4,
293 .p_count = 1,
Chuck Levercc0175c2006-03-20 13:44:22 -0500294 .p_statidx = PMAP_GETPORT,
295 .p_name = "GETPORT",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 },
297};
298
299static struct rpc_version pmap_version2 = {
300 .number = 2,
301 .nrprocs = 4,
302 .procs = pmap_procedures
303};
304
305static struct rpc_version * pmap_version[] = {
306 NULL,
307 NULL,
308 &pmap_version2
309};
310
311static struct rpc_stat pmap_stats;
312
313static struct rpc_program pmap_program = {
314 .name = "portmap",
315 .number = RPC_PMAP_PROGRAM,
316 .nrvers = ARRAY_SIZE(pmap_version),
317 .version = pmap_version,
318 .stats = &pmap_stats,
319};