blob: 8139ce68e915591046524f59fc2df49bc55b578b [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
9#include <linux/config.h>
10#include <linux/types.h>
11#include <linux/socket.h>
12#include <linux/kernel.h>
13#include <linux/errno.h>
14#include <linux/uio.h>
15#include <linux/in.h>
16#include <linux/sunrpc/clnt.h>
17#include <linux/sunrpc/xprt.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
28static struct rpc_procinfo pmap_procedures[];
Chuck Lever6cd75252005-09-22 21:24:59 -040029static struct rpc_clnt * pmap_create(char *, struct sockaddr_in *, int, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070030static void pmap_getport_done(struct rpc_task *);
31static struct rpc_program pmap_program;
32static DEFINE_SPINLOCK(pmap_lock);
33
34/*
35 * Obtain the port for a given RPC service on a given host. This one can
36 * be called for an ongoing RPC request.
37 */
38void
39rpc_getport(struct rpc_task *task, struct rpc_clnt *clnt)
40{
41 struct rpc_portmap *map = clnt->cl_pmap;
42 struct sockaddr_in *sap = &clnt->cl_xprt->addr;
43 struct rpc_message msg = {
44 .rpc_proc = &pmap_procedures[PMAP_GETPORT],
45 .rpc_argp = map,
46 .rpc_resp = &clnt->cl_port,
47 .rpc_cred = NULL
48 };
49 struct rpc_clnt *pmap_clnt;
50 struct rpc_task *child;
51
52 dprintk("RPC: %4d rpc_getport(%s, %d, %d, %d)\n",
53 task->tk_pid, clnt->cl_server,
54 map->pm_prog, map->pm_vers, map->pm_prot);
55
Andreas Gruenbacher007e2512005-06-22 17:16:23 +000056 /* Autobind on cloned rpc clients is discouraged */
57 BUG_ON(clnt->cl_parent != clnt);
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 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
Chuck Lever6cd75252005-09-22 21:24:59 -040068 pmap_clnt = pmap_create(clnt->cl_server, sap, map->pm_prot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 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
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 };
106 struct rpc_clnt *pmap_clnt;
107 char hostname[32];
108 int status;
109
110 dprintk("RPC: rpc_getport_external(%u.%u.%u.%u, %d, %d, %d)\n",
111 NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
112
113 sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr));
Chuck Lever6cd75252005-09-22 21:24:59 -0400114 pmap_clnt = pmap_create(hostname, sin, prot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (IS_ERR(pmap_clnt))
116 return PTR_ERR(pmap_clnt);
117
118 /* Setup the call info struct */
119 status = rpc_call(pmap_clnt, PMAP_GETPORT, &map, &map.pm_port, 0);
120
121 if (status >= 0) {
122 if (map.pm_port != 0)
123 return map.pm_port;
124 status = -EACCES;
125 }
126 return status;
127}
128#endif
129
130static void
131pmap_getport_done(struct rpc_task *task)
132{
133 struct rpc_clnt *clnt = task->tk_client;
Chuck Lever92200412006-01-03 09:55:51 +0100134 struct rpc_xprt *xprt = task->tk_xprt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 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);
Chuck Lever92200412006-01-03 09:55:51 +0100139
140 xprt->ops->set_port(xprt, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (task->tk_status < 0) {
142 /* Make the calling task exit with an error */
Trond Myklebustabbcf282006-01-03 09:55:03 +0100143 task->tk_action = rpc_exit_task;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 } else if (clnt->cl_port == 0) {
145 /* Program not registered */
Trond Myklebustabbcf282006-01-03 09:55:03 +0100146 rpc_exit(task, -EACCES);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 } else {
Chuck Lever92200412006-01-03 09:55:51 +0100148 xprt->ops->set_port(xprt, clnt->cl_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 clnt->cl_port = htons(clnt->cl_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
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 */
161int
162rpc_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);
Chuck Lever6cd75252005-09-22 21:24:59 -0400174 pmap_clnt = pmap_create("localhost", &sin, IPPROTO_UDP, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 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
200static struct rpc_clnt *
Chuck Lever6cd75252005-09-22 21:24:59 -0400201pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto, int privileged)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
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;
Chuck Lever92200412006-01-03 09:55:51 +0100210 xprt->ops->set_port(xprt, RPC_PMAP_PORT);
Chuck Lever6cd75252005-09-22 21:24:59 -0400211 if (!privileged)
212 xprt->resvport = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 /* printk("pmap: create clnt\n"); */
Trond Myklebust5ee0ed72005-06-22 17:16:20 +0000215 clnt = rpc_new_client(xprt, hostname,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 &pmap_program, RPC_PMAP_VERSION,
217 RPC_AUTH_UNIX);
Trond Myklebust5b616f52005-06-22 17:16:20 +0000218 if (!IS_ERR(clnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 clnt->cl_softrtry = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 clnt->cl_oneshot = 1;
221 }
222 return clnt;
223}
224
225/*
226 * XDR encode/decode functions for PMAP
227 */
228static int
229xdr_encode_mapping(struct rpc_rqst *req, u32 *p, struct rpc_portmap *map)
230{
231 dprintk("RPC: xdr_encode_mapping(%d, %d, %d, %d)\n",
232 map->pm_prog, map->pm_vers, map->pm_prot, map->pm_port);
233 *p++ = htonl(map->pm_prog);
234 *p++ = htonl(map->pm_vers);
235 *p++ = htonl(map->pm_prot);
236 *p++ = htonl(map->pm_port);
237
238 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
239 return 0;
240}
241
242static int
243xdr_decode_port(struct rpc_rqst *req, u32 *p, unsigned short *portp)
244{
245 *portp = (unsigned short) ntohl(*p++);
246 return 0;
247}
248
249static int
250xdr_decode_bool(struct rpc_rqst *req, u32 *p, unsigned int *boolp)
251{
252 *boolp = (unsigned int) ntohl(*p++);
253 return 0;
254}
255
256static struct rpc_procinfo pmap_procedures[] = {
257[PMAP_SET] = {
258 .p_proc = PMAP_SET,
259 .p_encode = (kxdrproc_t) xdr_encode_mapping,
260 .p_decode = (kxdrproc_t) xdr_decode_bool,
261 .p_bufsiz = 4,
262 .p_count = 1,
263 },
264[PMAP_UNSET] = {
265 .p_proc = PMAP_UNSET,
266 .p_encode = (kxdrproc_t) xdr_encode_mapping,
267 .p_decode = (kxdrproc_t) xdr_decode_bool,
268 .p_bufsiz = 4,
269 .p_count = 1,
270 },
271[PMAP_GETPORT] = {
272 .p_proc = PMAP_GETPORT,
273 .p_encode = (kxdrproc_t) xdr_encode_mapping,
274 .p_decode = (kxdrproc_t) xdr_decode_port,
275 .p_bufsiz = 4,
276 .p_count = 1,
277 },
278};
279
280static struct rpc_version pmap_version2 = {
281 .number = 2,
282 .nrprocs = 4,
283 .procs = pmap_procedures
284};
285
286static struct rpc_version * pmap_version[] = {
287 NULL,
288 NULL,
289 &pmap_version2
290};
291
292static struct rpc_stat pmap_stats;
293
294static struct rpc_program pmap_program = {
295 .name = "portmap",
296 .number = RPC_PMAP_PROGRAM,
297 .nrvers = ARRAY_SIZE(pmap_version),
298 .version = pmap_version,
299 .stats = &pmap_stats,
300};