2 * linux/net/sunrpc/pmap.c
6 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
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>
16 #include <linux/sunrpc/clnt.h>
17 #include <linux/sunrpc/xprt.h>
18 #include <linux/sunrpc/sched.h>
21 # define RPCDBG_FACILITY RPCDBG_PMAP
26 #define PMAP_GETPORT 3
28 static struct rpc_procinfo pmap_procedures
[];
29 static struct rpc_clnt
* pmap_create(char *, struct sockaddr_in
*, int, int);
30 static void pmap_getport_done(struct rpc_task
*);
31 static struct rpc_program pmap_program
;
32 static DEFINE_SPINLOCK(pmap_lock
);
35 * Obtain the port for a given RPC service on a given host. This one can
36 * be called for an ongoing RPC request.
39 rpc_getport(struct rpc_task
*task
, struct rpc_clnt
*clnt
)
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
],
46 .rpc_resp
= &clnt
->cl_port
,
49 struct rpc_clnt
*pmap_clnt
;
50 struct rpc_task
*child
;
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
);
56 /* Autobind on cloned rpc clients is discouraged */
57 BUG_ON(clnt
->cl_parent
!= clnt
);
59 spin_lock(&pmap_lock
);
60 if (map
->pm_binding
) {
61 rpc_sleep_on(&map
->pm_bindwait
, task
, NULL
, NULL
);
62 spin_unlock(&pmap_lock
);
66 spin_unlock(&pmap_lock
);
68 pmap_clnt
= pmap_create(clnt
->cl_server
, sap
, map
->pm_prot
, 0);
69 if (IS_ERR(pmap_clnt
)) {
70 task
->tk_status
= PTR_ERR(pmap_clnt
);
76 * Note: rpc_new_child will release client after a failure.
78 if (!(child
= rpc_new_child(pmap_clnt
, task
)))
81 /* Setup the call info struct */
82 rpc_call_setup(child
, &msg
, 0);
84 /* ... and run the child task */
85 task
->tk_xprt
->stat
.bind_count
++;
86 rpc_run_child(task
, child
, pmap_getport_done
);
90 spin_lock(&pmap_lock
);
92 rpc_wake_up(&map
->pm_bindwait
);
93 spin_unlock(&pmap_lock
);
97 #ifdef CONFIG_ROOT_NFS
99 rpc_getport_external(struct sockaddr_in
*sin
, __u32 prog
, __u32 vers
, int prot
)
101 struct rpc_portmap map
= {
107 struct rpc_message msg
= {
108 .rpc_proc
= &pmap_procedures
[PMAP_GETPORT
],
110 .rpc_resp
= &map
.pm_port
,
112 struct rpc_clnt
*pmap_clnt
;
116 dprintk("RPC: rpc_getport_external(%u.%u.%u.%u, %d, %d, %d)\n",
117 NIPQUAD(sin
->sin_addr
.s_addr
), prog
, vers
, prot
);
119 sprintf(hostname
, "%u.%u.%u.%u", NIPQUAD(sin
->sin_addr
.s_addr
));
120 pmap_clnt
= pmap_create(hostname
, sin
, prot
, 0);
121 if (IS_ERR(pmap_clnt
))
122 return PTR_ERR(pmap_clnt
);
124 /* Setup the call info struct */
125 status
= rpc_call_sync(pmap_clnt
, &msg
, 0);
128 if (map
.pm_port
!= 0)
137 pmap_getport_done(struct rpc_task
*task
)
139 struct rpc_clnt
*clnt
= task
->tk_client
;
140 struct rpc_xprt
*xprt
= task
->tk_xprt
;
141 struct rpc_portmap
*map
= clnt
->cl_pmap
;
143 dprintk("RPC: %4d pmap_getport_done(status %d, port %d)\n",
144 task
->tk_pid
, task
->tk_status
, clnt
->cl_port
);
146 xprt
->ops
->set_port(xprt
, 0);
147 if (task
->tk_status
< 0) {
148 /* Make the calling task exit with an error */
149 task
->tk_action
= rpc_exit_task
;
150 } else if (clnt
->cl_port
== 0) {
151 /* Program not registered */
152 rpc_exit(task
, -EACCES
);
154 xprt
->ops
->set_port(xprt
, clnt
->cl_port
);
155 clnt
->cl_port
= htons(clnt
->cl_port
);
157 spin_lock(&pmap_lock
);
159 rpc_wake_up(&map
->pm_bindwait
);
160 spin_unlock(&pmap_lock
);
164 * Set or unset a port registration with the local portmapper.
165 * port == 0 means unregister, port != 0 means register.
168 rpc_register(u32 prog
, u32 vers
, int prot
, unsigned short port
, int *okay
)
170 struct sockaddr_in sin
= {
171 .sin_family
= AF_INET
,
172 .sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
),
174 struct rpc_portmap map
= {
180 struct rpc_message msg
= {
181 .rpc_proc
= &pmap_procedures
[port
? PMAP_SET
: PMAP_UNSET
],
185 struct rpc_clnt
*pmap_clnt
;
188 dprintk("RPC: registering (%d, %d, %d, %d) with portmapper.\n",
189 prog
, vers
, prot
, port
);
191 pmap_clnt
= pmap_create("localhost", &sin
, IPPROTO_UDP
, 1);
192 if (IS_ERR(pmap_clnt
)) {
193 error
= PTR_ERR(pmap_clnt
);
194 dprintk("RPC: couldn't create pmap client. Error = %d\n", error
);
198 error
= rpc_call_sync(pmap_clnt
, &msg
, 0);
202 "RPC: failed to contact portmap (errno %d).\n",
205 dprintk("RPC: registration status %d/%d\n", error
, *okay
);
207 /* Client deleted automatically because cl_oneshot == 1 */
211 static struct rpc_clnt
*
212 pmap_create(char *hostname
, struct sockaddr_in
*srvaddr
, int proto
, int privileged
)
214 struct rpc_xprt
*xprt
;
215 struct rpc_clnt
*clnt
;
217 /* printk("pmap: create xprt\n"); */
218 xprt
= xprt_create_proto(proto
, srvaddr
, NULL
);
220 return (struct rpc_clnt
*)xprt
;
221 xprt
->ops
->set_port(xprt
, RPC_PMAP_PORT
);
225 /* printk("pmap: create clnt\n"); */
226 clnt
= rpc_new_client(xprt
, hostname
,
227 &pmap_program
, RPC_PMAP_VERSION
,
230 clnt
->cl_softrtry
= 1;
231 clnt
->cl_oneshot
= 1;
237 * XDR encode/decode functions for PMAP
240 xdr_encode_mapping(struct rpc_rqst
*req
, u32
*p
, struct rpc_portmap
*map
)
242 dprintk("RPC: xdr_encode_mapping(%d, %d, %d, %d)\n",
243 map
->pm_prog
, map
->pm_vers
, map
->pm_prot
, map
->pm_port
);
244 *p
++ = htonl(map
->pm_prog
);
245 *p
++ = htonl(map
->pm_vers
);
246 *p
++ = htonl(map
->pm_prot
);
247 *p
++ = htonl(map
->pm_port
);
249 req
->rq_slen
= xdr_adjust_iovec(req
->rq_svec
, p
);
254 xdr_decode_port(struct rpc_rqst
*req
, u32
*p
, unsigned short *portp
)
256 *portp
= (unsigned short) ntohl(*p
++);
261 xdr_decode_bool(struct rpc_rqst
*req
, u32
*p
, unsigned int *boolp
)
263 *boolp
= (unsigned int) ntohl(*p
++);
267 static struct rpc_procinfo pmap_procedures
[] = {
270 .p_encode
= (kxdrproc_t
) xdr_encode_mapping
,
271 .p_decode
= (kxdrproc_t
) xdr_decode_bool
,
274 .p_statidx
= PMAP_SET
,
278 .p_proc
= PMAP_UNSET
,
279 .p_encode
= (kxdrproc_t
) xdr_encode_mapping
,
280 .p_decode
= (kxdrproc_t
) xdr_decode_bool
,
283 .p_statidx
= PMAP_UNSET
,
287 .p_proc
= PMAP_GETPORT
,
288 .p_encode
= (kxdrproc_t
) xdr_encode_mapping
,
289 .p_decode
= (kxdrproc_t
) xdr_decode_port
,
292 .p_statidx
= PMAP_GETPORT
,
297 static struct rpc_version pmap_version2
= {
300 .procs
= pmap_procedures
303 static struct rpc_version
* pmap_version
[] = {
309 static struct rpc_stat pmap_stats
;
311 static struct rpc_program pmap_program
= {
313 .number
= RPC_PMAP_PROGRAM
,
314 .nrvers
= ARRAY_SIZE(pmap_version
),
315 .version
= pmap_version
,
316 .stats
= &pmap_stats
,