aboutsummaryrefslogtreecommitdiffstats
path: root/net/sunrpc/pmap_clnt.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/sunrpc/pmap_clnt.c')
-rw-r--r--net/sunrpc/pmap_clnt.c266
1 files changed, 164 insertions, 102 deletions
diff --git a/net/sunrpc/pmap_clnt.c b/net/sunrpc/pmap_clnt.c
index 623180f224c9..c04609d3476a 100644
--- a/net/sunrpc/pmap_clnt.c
+++ b/net/sunrpc/pmap_clnt.c
@@ -1,7 +1,9 @@
1/* 1/*
2 * linux/net/sunrpc/pmap.c 2 * linux/net/sunrpc/pmap_clnt.c
3 * 3 *
4 * Portmapper client. 4 * In-kernel RPC portmapper client.
5 *
6 * Portmapper supports version 2 of the rpcbind protocol (RFC 1833).
5 * 7 *
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de> 8 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
7 */ 9 */
@@ -13,7 +15,6 @@
13#include <linux/uio.h> 15#include <linux/uio.h>
14#include <linux/in.h> 16#include <linux/in.h>
15#include <linux/sunrpc/clnt.h> 17#include <linux/sunrpc/clnt.h>
16#include <linux/sunrpc/xprt.h>
17#include <linux/sunrpc/sched.h> 18#include <linux/sunrpc/sched.h>
18 19
19#ifdef RPC_DEBUG 20#ifdef RPC_DEBUG
@@ -24,80 +25,141 @@
24#define PMAP_UNSET 2 25#define PMAP_UNSET 2
25#define PMAP_GETPORT 3 26#define PMAP_GETPORT 3
26 27
28struct 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;
34};
35
27static struct rpc_procinfo pmap_procedures[]; 36static struct rpc_procinfo pmap_procedures[];
28static struct rpc_clnt * pmap_create(char *, struct sockaddr_in *, int, int); 37static struct rpc_clnt * pmap_create(char *, struct sockaddr_in *, int, int);
29static void pmap_getport_done(struct rpc_task *); 38static void pmap_getport_done(struct rpc_task *, void *);
30static struct rpc_program pmap_program; 39static struct rpc_program pmap_program;
31static DEFINE_SPINLOCK(pmap_lock);
32 40
33/* 41static void pmap_getport_prepare(struct rpc_task *task, void *calldata)
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{ 42{
40 struct rpc_portmap *map = clnt->cl_pmap; 43 struct portmap_args *map = calldata;
41 struct sockaddr_in *sap = &clnt->cl_xprt->addr;
42 struct rpc_message msg = { 44 struct rpc_message msg = {
43 .rpc_proc = &pmap_procedures[PMAP_GETPORT], 45 .rpc_proc = &pmap_procedures[PMAP_GETPORT],
44 .rpc_argp = map, 46 .rpc_argp = map,
45 .rpc_resp = &clnt->cl_port, 47 .rpc_resp = &map->pm_port,
46 .rpc_cred = NULL
47 }; 48 };
49
50 rpc_call_setup(task, &msg, 0);
51}
52
53static inline struct portmap_args *pmap_map_alloc(void)
54{
55 return kmalloc(sizeof(struct portmap_args), GFP_NOFS);
56}
57
58static inline void pmap_map_free(struct portmap_args *map)
59{
60 kfree(map);
61}
62
63static void pmap_map_release(void *data)
64{
65 pmap_map_free(data);
66}
67
68static 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,
72};
73
74static inline void pmap_wake_portmap_waiters(struct rpc_xprt *xprt, int status)
75{
76 xprt_clear_binding(xprt);
77 rpc_wake_up_status(&xprt->binding, status);
78}
79
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
83 *
84 * This one can be called for an ongoing RPC request, and can be used in
85 * an async (rpciod) context.
86 */
87void rpc_getport(struct rpc_task *task)
88{
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;
48 struct rpc_clnt *pmap_clnt; 93 struct rpc_clnt *pmap_clnt;
49 struct rpc_task *child; 94 struct rpc_task *child;
95 int status;
50 96
51 dprintk("RPC: %4d rpc_getport(%s, %d, %d, %d)\n", 97 dprintk("RPC: %4d rpc_getport(%s, %u, %u, %d)\n",
52 task->tk_pid, clnt->cl_server, 98 task->tk_pid, clnt->cl_server,
53 map->pm_prog, map->pm_vers, map->pm_prot); 99 clnt->cl_prog, clnt->cl_vers, xprt->prot);
54 100
55 /* Autobind on cloned rpc clients is discouraged */ 101 /* Autobind on cloned rpc clients is discouraged */
56 BUG_ON(clnt->cl_parent != clnt); 102 BUG_ON(clnt->cl_parent != clnt);
57 103
58 spin_lock(&pmap_lock); 104 if (xprt_test_and_set_binding(xprt)) {
59 if (map->pm_binding) { 105 task->tk_status = -EACCES; /* tell caller to check again */
60 rpc_sleep_on(&map->pm_bindwait, task, NULL, NULL); 106 rpc_sleep_on(&xprt->binding, task, NULL, NULL);
61 spin_unlock(&pmap_lock);
62 return; 107 return;
63 } 108 }
64 map->pm_binding = 1;
65 spin_unlock(&pmap_lock);
66 109
67 pmap_clnt = pmap_create(clnt->cl_server, sap, map->pm_prot, 0); 110 /* Someone else may have bound if we slept */
68 if (IS_ERR(pmap_clnt)) { 111 status = 0;
69 task->tk_status = PTR_ERR(pmap_clnt); 112 if (xprt_bound(xprt))
113 goto bailout_nofree;
114
115 status = -ENOMEM;
116 map = pmap_map_alloc();
117 if (!map)
118 goto bailout_nofree;
119 map->pm_prog = clnt->cl_prog;
120 map->pm_vers = clnt->cl_vers;
121 map->pm_prot = xprt->prot;
122 map->pm_port = 0;
123 map->pm_xprt = xprt_get(xprt);
124
125 rpc_peeraddr(clnt, (struct sockaddr *) &addr, sizeof(addr));
126 pmap_clnt = pmap_create(clnt->cl_server, &addr, map->pm_prot, 0);
127 status = PTR_ERR(pmap_clnt);
128 if (IS_ERR(pmap_clnt))
70 goto bailout; 129 goto bailout;
71 }
72 task->tk_status = 0;
73 130
74 /* 131 status = -EIO;
75 * Note: rpc_new_child will release client after a failure. 132 child = rpc_run_task(pmap_clnt, RPC_TASK_ASYNC, &pmap_getport_ops, map);
76 */ 133 if (IS_ERR(child))
77 if (!(child = rpc_new_child(pmap_clnt, task)))
78 goto bailout; 134 goto bailout;
135 rpc_release_task(child);
79 136
80 /* Setup the call info struct */ 137 rpc_sleep_on(&xprt->binding, task, NULL, NULL);
81 rpc_call_setup(child, &msg, 0);
82 138
83 /* ... and run the child task */
84 task->tk_xprt->stat.bind_count++; 139 task->tk_xprt->stat.bind_count++;
85 rpc_run_child(task, child, pmap_getport_done);
86 return; 140 return;
87 141
88bailout: 142bailout:
89 spin_lock(&pmap_lock); 143 pmap_map_free(map);
90 map->pm_binding = 0; 144 xprt_put(xprt);
91 rpc_wake_up(&map->pm_bindwait); 145bailout_nofree:
92 spin_unlock(&pmap_lock); 146 task->tk_status = status;
93 rpc_exit(task, -EIO); 147 pmap_wake_portmap_waiters(xprt, status);
94} 148}
95 149
96#ifdef CONFIG_ROOT_NFS 150#ifdef CONFIG_ROOT_NFS
97int 151/**
98rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot) 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
157 *
158 * This one is called from outside the RPC client in a synchronous task context.
159 */
160int rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
99{ 161{
100 struct rpc_portmap map = { 162 struct portmap_args map = {
101 .pm_prog = prog, 163 .pm_prog = prog,
102 .pm_vers = vers, 164 .pm_vers = vers,
103 .pm_prot = prot, 165 .pm_prot = prot,
@@ -112,7 +174,7 @@ rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
112 char hostname[32]; 174 char hostname[32];
113 int status; 175 int status;
114 176
115 dprintk("RPC: rpc_getport_external(%u.%u.%u.%u, %d, %d, %d)\n", 177 dprintk("RPC: rpc_getport_external(%u.%u.%u.%u, %u, %u, %d)\n",
116 NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot); 178 NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
117 179
118 sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr)); 180 sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr));
@@ -132,45 +194,53 @@ rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
132} 194}
133#endif 195#endif
134 196
135static void 197/*
136pmap_getport_done(struct rpc_task *task) 198 * Portmapper child task invokes this callback via tk_exit.
199 */
200static void pmap_getport_done(struct rpc_task *child, void *data)
137{ 201{
138 struct rpc_clnt *clnt = task->tk_client; 202 struct portmap_args *map = data;
139 struct rpc_xprt *xprt = task->tk_xprt; 203 struct rpc_xprt *xprt = map->pm_xprt;
140 struct rpc_portmap *map = clnt->cl_pmap; 204 int status = child->tk_status;
141 205
142 dprintk("RPC: %4d pmap_getport_done(status %d, port %d)\n", 206 if (status < 0) {
143 task->tk_pid, task->tk_status, clnt->cl_port); 207 /* Portmapper not available */
144 208 xprt->ops->set_port(xprt, 0);
145 xprt->ops->set_port(xprt, 0); 209 } else if (map->pm_port == 0) {
146 if (task->tk_status < 0) { 210 /* Requested RPC service wasn't registered */
147 /* Make the calling task exit with an error */ 211 xprt->ops->set_port(xprt, 0);
148 task->tk_action = rpc_exit_task; 212 status = -EACCES;
149 } else if (clnt->cl_port == 0) {
150 /* Program not registered */
151 rpc_exit(task, -EACCES);
152 } else { 213 } else {
153 xprt->ops->set_port(xprt, clnt->cl_port); 214 /* Succeeded */
154 clnt->cl_port = htons(clnt->cl_port); 215 xprt->ops->set_port(xprt, map->pm_port);
216 xprt_set_bound(xprt);
217 status = 0;
155 } 218 }
156 spin_lock(&pmap_lock); 219
157 map->pm_binding = 0; 220 dprintk("RPC: %4d pmap_getport_done(status %d, port %u)\n",
158 rpc_wake_up(&map->pm_bindwait); 221 child->tk_pid, status, map->pm_port);
159 spin_unlock(&pmap_lock); 222
223 pmap_wake_portmap_waiters(xprt, status);
224 xprt_put(xprt);
160} 225}
161 226
162/* 227/**
163 * Set or unset a port registration with the local portmapper. 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
234 *
164 * port == 0 means unregister, port != 0 means register. 235 * port == 0 means unregister, port != 0 means register.
165 */ 236 */
166int 237int rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
167rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
168{ 238{
169 struct sockaddr_in sin = { 239 struct sockaddr_in sin = {
170 .sin_family = AF_INET, 240 .sin_family = AF_INET,
171 .sin_addr.s_addr = htonl(INADDR_LOOPBACK), 241 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
172 }; 242 };
173 struct rpc_portmap map = { 243 struct portmap_args map = {
174 .pm_prog = prog, 244 .pm_prog = prog,
175 .pm_vers = vers, 245 .pm_vers = vers,
176 .pm_prot = prot, 246 .pm_prot = prot,
@@ -184,7 +254,7 @@ rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
184 struct rpc_clnt *pmap_clnt; 254 struct rpc_clnt *pmap_clnt;
185 int error = 0; 255 int error = 0;
186 256
187 dprintk("RPC: registering (%d, %d, %d, %d) with portmapper.\n", 257 dprintk("RPC: registering (%u, %u, %d, %u) with portmapper.\n",
188 prog, vers, prot, port); 258 prog, vers, prot, port);
189 259
190 pmap_clnt = pmap_create("localhost", &sin, IPPROTO_UDP, 1); 260 pmap_clnt = pmap_create("localhost", &sin, IPPROTO_UDP, 1);
@@ -207,38 +277,32 @@ rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
207 return error; 277 return error;
208} 278}
209 279
210static struct rpc_clnt * 280static struct rpc_clnt *pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto, int privileged)
211pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto, int privileged)
212{ 281{
213 struct rpc_xprt *xprt; 282 struct rpc_create_args args = {
214 struct rpc_clnt *clnt; 283 .protocol = proto,
215 284 .address = (struct sockaddr *)srvaddr,
216 /* printk("pmap: create xprt\n"); */ 285 .addrsize = sizeof(*srvaddr),
217 xprt = xprt_create_proto(proto, srvaddr, NULL); 286 .servername = hostname,
218 if (IS_ERR(xprt)) 287 .program = &pmap_program,
219 return (struct rpc_clnt *)xprt; 288 .version = RPC_PMAP_VERSION,
220 xprt->ops->set_port(xprt, RPC_PMAP_PORT); 289 .authflavor = RPC_AUTH_UNIX,
290 .flags = (RPC_CLNT_CREATE_ONESHOT |
291 RPC_CLNT_CREATE_NOPING),
292 };
293
294 srvaddr->sin_port = htons(RPC_PMAP_PORT);
221 if (!privileged) 295 if (!privileged)
222 xprt->resvport = 0; 296 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
223 297 return rpc_create(&args);
224 /* printk("pmap: create clnt\n"); */
225 clnt = rpc_new_client(xprt, hostname,
226 &pmap_program, RPC_PMAP_VERSION,
227 RPC_AUTH_UNIX);
228 if (!IS_ERR(clnt)) {
229 clnt->cl_softrtry = 1;
230 clnt->cl_oneshot = 1;
231 }
232 return clnt;
233} 298}
234 299
235/* 300/*
236 * XDR encode/decode functions for PMAP 301 * XDR encode/decode functions for PMAP
237 */ 302 */
238static int 303static int xdr_encode_mapping(struct rpc_rqst *req, u32 *p, struct portmap_args *map)
239xdr_encode_mapping(struct rpc_rqst *req, u32 *p, struct rpc_portmap *map)
240{ 304{
241 dprintk("RPC: xdr_encode_mapping(%d, %d, %d, %d)\n", 305 dprintk("RPC: xdr_encode_mapping(%u, %u, %u, %u)\n",
242 map->pm_prog, map->pm_vers, map->pm_prot, map->pm_port); 306 map->pm_prog, map->pm_vers, map->pm_prot, map->pm_port);
243 *p++ = htonl(map->pm_prog); 307 *p++ = htonl(map->pm_prog);
244 *p++ = htonl(map->pm_vers); 308 *p++ = htonl(map->pm_vers);
@@ -249,15 +313,13 @@ xdr_encode_mapping(struct rpc_rqst *req, u32 *p, struct rpc_portmap *map)
249 return 0; 313 return 0;
250} 314}
251 315
252static int 316static int xdr_decode_port(struct rpc_rqst *req, u32 *p, unsigned short *portp)
253xdr_decode_port(struct rpc_rqst *req, u32 *p, unsigned short *portp)
254{ 317{
255 *portp = (unsigned short) ntohl(*p++); 318 *portp = (unsigned short) ntohl(*p++);
256 return 0; 319 return 0;
257} 320}
258 321
259static int 322static int xdr_decode_bool(struct rpc_rqst *req, u32 *p, unsigned int *boolp)
260xdr_decode_bool(struct rpc_rqst *req, u32 *p, unsigned int *boolp)
261{ 323{
262 *boolp = (unsigned int) ntohl(*p++); 324 *boolp = (unsigned int) ntohl(*p++);
263 return 0; 325 return 0;