]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/sunrpc/pmap_clnt.c
Linux-2.6.12-rc2
[mirror_ubuntu-artful-kernel.git] / net / sunrpc / pmap_clnt.c
1 /*
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
28 static struct rpc_procinfo pmap_procedures[];
29 static struct rpc_clnt * pmap_create(char *, struct sockaddr_in *, int);
30 static void pmap_getport_done(struct rpc_task *);
31 static struct rpc_program pmap_program;
32 static 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 */
38 void
39 rpc_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
56 spin_lock(&pmap_lock);
57 if (map->pm_binding) {
58 rpc_sleep_on(&map->pm_bindwait, task, NULL, NULL);
59 spin_unlock(&pmap_lock);
60 return;
61 }
62 map->pm_binding = 1;
63 spin_unlock(&pmap_lock);
64
65 pmap_clnt = pmap_create(clnt->cl_server, sap, map->pm_prot);
66 if (IS_ERR(pmap_clnt)) {
67 task->tk_status = PTR_ERR(pmap_clnt);
68 goto bailout;
69 }
70 task->tk_status = 0;
71
72 /*
73 * Note: rpc_new_child will release client after a failure.
74 */
75 if (!(child = rpc_new_child(pmap_clnt, task)))
76 goto bailout;
77
78 /* Setup the call info struct */
79 rpc_call_setup(child, &msg, 0);
80
81 /* ... and run the child task */
82 rpc_run_child(task, child, pmap_getport_done);
83 return;
84
85 bailout:
86 spin_lock(&pmap_lock);
87 map->pm_binding = 0;
88 rpc_wake_up(&map->pm_bindwait);
89 spin_unlock(&pmap_lock);
90 task->tk_status = -EIO;
91 task->tk_action = NULL;
92 }
93
94 #ifdef CONFIG_ROOT_NFS
95 int
96 rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
97 {
98 struct rpc_portmap map = {
99 .pm_prog = prog,
100 .pm_vers = vers,
101 .pm_prot = prot,
102 .pm_port = 0
103 };
104 struct rpc_clnt *pmap_clnt;
105 char hostname[32];
106 int status;
107
108 dprintk("RPC: rpc_getport_external(%u.%u.%u.%u, %d, %d, %d)\n",
109 NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
110
111 sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr));
112 pmap_clnt = pmap_create(hostname, sin, prot);
113 if (IS_ERR(pmap_clnt))
114 return PTR_ERR(pmap_clnt);
115
116 /* Setup the call info struct */
117 status = rpc_call(pmap_clnt, PMAP_GETPORT, &map, &map.pm_port, 0);
118
119 if (status >= 0) {
120 if (map.pm_port != 0)
121 return map.pm_port;
122 status = -EACCES;
123 }
124 return status;
125 }
126 #endif
127
128 static void
129 pmap_getport_done(struct rpc_task *task)
130 {
131 struct rpc_clnt *clnt = task->tk_client;
132 struct rpc_portmap *map = clnt->cl_pmap;
133
134 dprintk("RPC: %4d pmap_getport_done(status %d, port %d)\n",
135 task->tk_pid, task->tk_status, clnt->cl_port);
136 if (task->tk_status < 0) {
137 /* Make the calling task exit with an error */
138 task->tk_action = NULL;
139 } else if (clnt->cl_port == 0) {
140 /* Program not registered */
141 task->tk_status = -EACCES;
142 task->tk_action = NULL;
143 } else {
144 /* byte-swap port number first */
145 clnt->cl_port = htons(clnt->cl_port);
146 clnt->cl_xprt->addr.sin_port = clnt->cl_port;
147 }
148 spin_lock(&pmap_lock);
149 map->pm_binding = 0;
150 rpc_wake_up(&map->pm_bindwait);
151 spin_unlock(&pmap_lock);
152 }
153
154 /*
155 * Set or unset a port registration with the local portmapper.
156 * port == 0 means unregister, port != 0 means register.
157 */
158 int
159 rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
160 {
161 struct sockaddr_in sin;
162 struct rpc_portmap map;
163 struct rpc_clnt *pmap_clnt;
164 int error = 0;
165
166 dprintk("RPC: registering (%d, %d, %d, %d) with portmapper.\n",
167 prog, vers, prot, port);
168
169 sin.sin_family = AF_INET;
170 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
171 pmap_clnt = pmap_create("localhost", &sin, IPPROTO_UDP);
172 if (IS_ERR(pmap_clnt)) {
173 error = PTR_ERR(pmap_clnt);
174 dprintk("RPC: couldn't create pmap client. Error = %d\n", error);
175 return error;
176 }
177
178 map.pm_prog = prog;
179 map.pm_vers = vers;
180 map.pm_prot = prot;
181 map.pm_port = port;
182
183 error = rpc_call(pmap_clnt, port? PMAP_SET : PMAP_UNSET,
184 &map, okay, 0);
185
186 if (error < 0) {
187 printk(KERN_WARNING
188 "RPC: failed to contact portmap (errno %d).\n",
189 error);
190 }
191 dprintk("RPC: registration status %d/%d\n", error, *okay);
192
193 /* Client deleted automatically because cl_oneshot == 1 */
194 return error;
195 }
196
197 static struct rpc_clnt *
198 pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto)
199 {
200 struct rpc_xprt *xprt;
201 struct rpc_clnt *clnt;
202
203 /* printk("pmap: create xprt\n"); */
204 xprt = xprt_create_proto(proto, srvaddr, NULL);
205 if (IS_ERR(xprt))
206 return (struct rpc_clnt *)xprt;
207 xprt->addr.sin_port = htons(RPC_PMAP_PORT);
208
209 /* printk("pmap: create clnt\n"); */
210 clnt = rpc_create_client(xprt, hostname,
211 &pmap_program, RPC_PMAP_VERSION,
212 RPC_AUTH_UNIX);
213 if (IS_ERR(clnt)) {
214 xprt_destroy(xprt);
215 } else {
216 clnt->cl_softrtry = 1;
217 clnt->cl_chatty = 1;
218 clnt->cl_oneshot = 1;
219 }
220 return clnt;
221 }
222
223 /*
224 * XDR encode/decode functions for PMAP
225 */
226 static int
227 xdr_encode_mapping(struct rpc_rqst *req, u32 *p, struct rpc_portmap *map)
228 {
229 dprintk("RPC: xdr_encode_mapping(%d, %d, %d, %d)\n",
230 map->pm_prog, map->pm_vers, map->pm_prot, map->pm_port);
231 *p++ = htonl(map->pm_prog);
232 *p++ = htonl(map->pm_vers);
233 *p++ = htonl(map->pm_prot);
234 *p++ = htonl(map->pm_port);
235
236 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
237 return 0;
238 }
239
240 static int
241 xdr_decode_port(struct rpc_rqst *req, u32 *p, unsigned short *portp)
242 {
243 *portp = (unsigned short) ntohl(*p++);
244 return 0;
245 }
246
247 static int
248 xdr_decode_bool(struct rpc_rqst *req, u32 *p, unsigned int *boolp)
249 {
250 *boolp = (unsigned int) ntohl(*p++);
251 return 0;
252 }
253
254 static struct rpc_procinfo pmap_procedures[] = {
255 [PMAP_SET] = {
256 .p_proc = PMAP_SET,
257 .p_encode = (kxdrproc_t) xdr_encode_mapping,
258 .p_decode = (kxdrproc_t) xdr_decode_bool,
259 .p_bufsiz = 4,
260 .p_count = 1,
261 },
262 [PMAP_UNSET] = {
263 .p_proc = PMAP_UNSET,
264 .p_encode = (kxdrproc_t) xdr_encode_mapping,
265 .p_decode = (kxdrproc_t) xdr_decode_bool,
266 .p_bufsiz = 4,
267 .p_count = 1,
268 },
269 [PMAP_GETPORT] = {
270 .p_proc = PMAP_GETPORT,
271 .p_encode = (kxdrproc_t) xdr_encode_mapping,
272 .p_decode = (kxdrproc_t) xdr_decode_port,
273 .p_bufsiz = 4,
274 .p_count = 1,
275 },
276 };
277
278 static struct rpc_version pmap_version2 = {
279 .number = 2,
280 .nrprocs = 4,
281 .procs = pmap_procedures
282 };
283
284 static struct rpc_version * pmap_version[] = {
285 NULL,
286 NULL,
287 &pmap_version2
288 };
289
290 static struct rpc_stat pmap_stats;
291
292 static struct rpc_program pmap_program = {
293 .name = "portmap",
294 .number = RPC_PMAP_PROGRAM,
295 .nrvers = ARRAY_SIZE(pmap_version),
296 .version = pmap_version,
297 .stats = &pmap_stats,
298 };