2 * linux/net/sunrpc/pmap.c
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
9 #include <linux/types.h>
10 #include <linux/socket.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/uio.h>
15 #include <linux/sunrpc/clnt.h>
16 #include <linux/sunrpc/xprt.h>
17 #include <linux/sunrpc/sched.h>
20 # define RPCDBG_FACILITY RPCDBG_PMAP
25 #define PMAP_GETPORT 3
27 static struct rpc_procinfo pmap_procedures
[];
28 static struct rpc_clnt
* pmap_create(char *, struct sockaddr_in
*, int, int);
29 static void pmap_getport_done(struct rpc_task
*);
30 static struct rpc_program pmap_program
;
31 static DEFINE_SPINLOCK(pmap_lock
);
34 * Obtain the port for a given RPC service on a given host. This one can
35 * be called for an ongoing RPC request.
38 rpc_getport(struct rpc_task
*task
, struct rpc_clnt
*clnt
)
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
],
45 .rpc_resp
= &clnt
->cl_port
,
48 struct rpc_clnt
*pmap_clnt
;
49 struct rpc_task
*child
;
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
);
55 /* Autobind on cloned rpc clients is discouraged */
56 BUG_ON(clnt
->cl_parent
!= clnt
);
58 spin_lock(&pmap_lock
);
59 if (map
->pm_binding
) {
60 rpc_sleep_on(&map
->pm_bindwait
, task
, NULL
, NULL
);
61 spin_unlock(&pmap_lock
);
65 spin_unlock(&pmap_lock
);
67 pmap_clnt
= pmap_create(clnt
->cl_server
, sap
, map
->pm_prot
, 0);
68 if (IS_ERR(pmap_clnt
)) {
69 task
->tk_status
= PTR_ERR(pmap_clnt
);
75 * Note: rpc_new_child will release client after a failure.
77 if (!(child
= rpc_new_child(pmap_clnt
, task
)))
80 /* Setup the call info struct */
81 rpc_call_setup(child
, &msg
, 0);
83 /* ... and run the child task */
84 task
->tk_xprt
->stat
.bind_count
++;
85 rpc_run_child(task
, child
, pmap_getport_done
);
89 spin_lock(&pmap_lock
);
91 rpc_wake_up(&map
->pm_bindwait
);
92 spin_unlock(&pmap_lock
);
96 #ifdef CONFIG_ROOT_NFS
98 rpc_getport_external(struct sockaddr_in
*sin
, __u32 prog
, __u32 vers
, int prot
)
100 struct rpc_portmap map
= {
106 struct rpc_message msg
= {
107 .rpc_proc
= &pmap_procedures
[PMAP_GETPORT
],
109 .rpc_resp
= &map
.pm_port
,
111 struct rpc_clnt
*pmap_clnt
;
115 dprintk("RPC: rpc_getport_external(%u.%u.%u.%u, %d, %d, %d)\n",
116 NIPQUAD(sin
->sin_addr
.s_addr
), prog
, vers
, prot
);
118 sprintf(hostname
, "%u.%u.%u.%u", NIPQUAD(sin
->sin_addr
.s_addr
));
119 pmap_clnt
= pmap_create(hostname
, sin
, prot
, 0);
120 if (IS_ERR(pmap_clnt
))
121 return PTR_ERR(pmap_clnt
);
123 /* Setup the call info struct */
124 status
= rpc_call_sync(pmap_clnt
, &msg
, 0);
127 if (map
.pm_port
!= 0)
136 pmap_getport_done(struct rpc_task
*task
)
138 struct rpc_clnt
*clnt
= task
->tk_client
;
139 struct rpc_xprt
*xprt
= task
->tk_xprt
;
140 struct rpc_portmap
*map
= clnt
->cl_pmap
;
142 dprintk("RPC: %4d pmap_getport_done(status %d, port %d)\n",
143 task
->tk_pid
, task
->tk_status
, clnt
->cl_port
);
145 xprt
->ops
->set_port(xprt
, 0);
146 if (task
->tk_status
< 0) {
147 /* Make the calling task exit with an error */
148 task
->tk_action
= rpc_exit_task
;
149 } else if (clnt
->cl_port
== 0) {
150 /* Program not registered */
151 rpc_exit(task
, -EACCES
);
153 xprt
->ops
->set_port(xprt
, clnt
->cl_port
);
154 clnt
->cl_port
= htons(clnt
->cl_port
);
156 spin_lock(&pmap_lock
);
158 rpc_wake_up(&map
->pm_bindwait
);
159 spin_unlock(&pmap_lock
);
163 * Set or unset a port registration with the local portmapper.
164 * port == 0 means unregister, port != 0 means register.
167 rpc_register(u32 prog
, u32 vers
, int prot
, unsigned short port
, int *okay
)
169 struct sockaddr_in sin
= {
170 .sin_family
= AF_INET
,
171 .sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
),
173 struct rpc_portmap map
= {
179 struct rpc_message msg
= {
180 .rpc_proc
= &pmap_procedures
[port
? PMAP_SET
: PMAP_UNSET
],
184 struct rpc_clnt
*pmap_clnt
;
187 dprintk("RPC: registering (%d, %d, %d, %d) with portmapper.\n",
188 prog
, vers
, prot
, port
);
190 pmap_clnt
= pmap_create("localhost", &sin
, IPPROTO_UDP
, 1);
191 if (IS_ERR(pmap_clnt
)) {
192 error
= PTR_ERR(pmap_clnt
);
193 dprintk("RPC: couldn't create pmap client. Error = %d\n", error
);
197 error
= rpc_call_sync(pmap_clnt
, &msg
, 0);
201 "RPC: failed to contact portmap (errno %d).\n",
204 dprintk("RPC: registration status %d/%d\n", error
, *okay
);
206 /* Client deleted automatically because cl_oneshot == 1 */
210 static struct rpc_clnt
*
211 pmap_create(char *hostname
, struct sockaddr_in
*srvaddr
, int proto
, int privileged
)
213 struct rpc_xprt
*xprt
;
214 struct rpc_clnt
*clnt
;
216 /* printk("pmap: create xprt\n"); */
217 xprt
= xprt_create_proto(proto
, srvaddr
, NULL
);
219 return (struct rpc_clnt
*)xprt
;
220 xprt
->ops
->set_port(xprt
, RPC_PMAP_PORT
);
224 /* printk("pmap: create clnt\n"); */
225 clnt
= rpc_new_client(xprt
, hostname
,
226 &pmap_program
, RPC_PMAP_VERSION
,
229 clnt
->cl_softrtry
= 1;
230 clnt
->cl_oneshot
= 1;
236 * XDR encode/decode functions for PMAP
239 xdr_encode_mapping(struct rpc_rqst
*req
, u32
*p
, struct rpc_portmap
*map
)
241 dprintk("RPC: xdr_encode_mapping(%d, %d, %d, %d)\n",
242 map
->pm_prog
, map
->pm_vers
, map
->pm_prot
, map
->pm_port
);
243 *p
++ = htonl(map
->pm_prog
);
244 *p
++ = htonl(map
->pm_vers
);
245 *p
++ = htonl(map
->pm_prot
);
246 *p
++ = htonl(map
->pm_port
);
248 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
253 xdr_decode_port(struct rpc_rqst
*req
, u32
*p
, unsigned short *portp
)
255 *portp
= (unsigned short) ntohl(*p
++);
260 xdr_decode_bool(struct rpc_rqst
*req
, u32
*p
, unsigned int *boolp
)
262 *boolp
= (unsigned int) ntohl(*p
++);
266 static struct rpc_procinfo pmap_procedures
[] = {
269 .p_encode
= (kxdrproc_t
) xdr_encode_mapping
,
270 .p_decode
= (kxdrproc_t
) xdr_decode_bool
,
273 .p_statidx
= PMAP_SET
,
277 .p_proc
= PMAP_UNSET
,
278 .p_encode
= (kxdrproc_t
) xdr_encode_mapping
,
279 .p_decode
= (kxdrproc_t
) xdr_decode_bool
,
282 .p_statidx
= PMAP_UNSET
,
286 .p_proc
= PMAP_GETPORT
,
287 .p_encode
= (kxdrproc_t
) xdr_encode_mapping
,
288 .p_decode
= (kxdrproc_t
) xdr_decode_port
,
291 .p_statidx
= PMAP_GETPORT
,
296 static struct rpc_version pmap_version2
= {
299 .procs
= pmap_procedures
302 static struct rpc_version
* pmap_version
[] = {
308 static struct rpc_stat pmap_stats
;
310 static struct rpc_program pmap_program
= {
312 .number
= RPC_PMAP_PROGRAM
,
313 .nrvers
= ARRAY_SIZE(pmap_version
),
314 .version
= pmap_version
,
315 .stats
= &pmap_stats
,