2 * linux/net/sunrpc/pmap.c
6 * FIXME: In a secure environment, we may want to use an authentication
7 * flavor other than AUTH_NULL.
9 * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
12 #include <linux/config.h>
13 #include <linux/types.h>
14 #include <linux/socket.h>
15 #include <linux/kernel.h>
16 #include <linux/errno.h>
17 #include <linux/uio.h>
19 #include <linux/sunrpc/clnt.h>
20 #include <linux/sunrpc/xprt.h>
21 #include <linux/sunrpc/sched.h>
24 # define RPCDBG_FACILITY RPCDBG_PMAP
29 #define PMAP_GETPORT 3
31 static struct rpc_clnt * pmap_create(char *, struct sockaddr_in *, int);
32 static void pmap_getport_done(struct rpc_task *);
33 extern struct rpc_program pmap_program;
34 static spinlock_t pmap_lock = SPIN_LOCK_UNLOCKED;
37 * Obtain the port for a given RPC service on a given host. This one can
38 * be called for an ongoing RPC request.
41 rpc_getport(struct rpc_task *task, struct rpc_clnt *clnt)
43 struct rpc_portmap *map = &clnt->cl_pmap;
44 struct sockaddr_in *sap = &clnt->cl_xprt->addr;
45 struct rpc_message msg = { PMAP_GETPORT, map, &clnt->cl_port, NULL };
46 struct rpc_clnt *pmap_clnt;
47 struct rpc_task *child;
49 dprintk("RPC: %4d rpc_getport(%s, %d, %d, %d)\n",
50 task->tk_pid, clnt->cl_server,
51 map->pm_prog, map->pm_vers, map->pm_prot);
53 spin_lock(&pmap_lock);
54 if (clnt->cl_binding) {
55 rpc_sleep_on(&clnt->cl_bindwait, task, NULL, 0);
56 spin_unlock(&pmap_lock);
60 spin_unlock(&pmap_lock);
62 task->tk_status = -EACCES; /* why set this? returns -EIO below */
63 if (!(pmap_clnt = pmap_create(clnt->cl_server, sap, map->pm_prot)))
68 * Note: rpc_new_child will release client after a failure.
70 if (!(child = rpc_new_child(pmap_clnt, task)))
73 /* Setup the call info struct */
74 rpc_call_setup(child, &msg, 0);
76 /* ... and run the child task */
77 rpc_run_child(task, child, pmap_getport_done);
81 spin_lock(&pmap_lock);
83 rpc_wake_up(&clnt->cl_bindwait);
84 spin_unlock(&pmap_lock);
85 task->tk_status = -EIO;
86 task->tk_action = NULL;
89 #ifdef CONFIG_ROOT_NFS
91 rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
93 struct rpc_portmap map = { prog, vers, prot, 0 };
94 struct rpc_clnt *pmap_clnt;
98 dprintk("RPC: rpc_getport_external(%u.%u.%u.%u, %d, %d, %d)\n",
99 NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
101 sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr));
102 if (!(pmap_clnt = pmap_create(hostname, sin, prot)))
105 /* Setup the call info struct */
106 status = rpc_call(pmap_clnt, PMAP_GETPORT, &map, &map.pm_port, 0);
109 if (map.pm_port != 0)
118 pmap_getport_done(struct rpc_task *task)
120 struct rpc_clnt *clnt = task->tk_client;
122 dprintk("RPC: %4d pmap_getport_done(status %d, port %d)\n",
123 task->tk_pid, task->tk_status, clnt->cl_port);
124 if (task->tk_status < 0) {
125 /* Make the calling task exit with an error */
126 task->tk_action = NULL;
127 } else if (clnt->cl_port == 0) {
128 /* Program not registered */
129 task->tk_status = -EACCES;
130 task->tk_action = NULL;
132 /* byte-swap port number first */
133 clnt->cl_port = htons(clnt->cl_port);
134 clnt->cl_xprt->addr.sin_port = clnt->cl_port;
136 spin_lock(&pmap_lock);
137 clnt->cl_binding = 0;
138 rpc_wake_up(&clnt->cl_bindwait);
139 spin_unlock(&pmap_lock);
143 * Set or unset a port registration with the local portmapper.
144 * port == 0 means unregister, port != 0 means register.
147 rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
149 struct sockaddr_in sin;
150 struct rpc_portmap map;
151 struct rpc_clnt *pmap_clnt;
152 unsigned int error = 0;
154 dprintk("RPC: registering (%d, %d, %d, %d) with portmapper.\n",
155 prog, vers, prot, port);
157 sin.sin_family = AF_INET;
158 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
159 if (!(pmap_clnt = pmap_create("localhost", &sin, IPPROTO_UDP))) {
160 dprintk("RPC: couldn't create pmap client\n");
169 error = rpc_call(pmap_clnt, port? PMAP_SET : PMAP_UNSET,
174 "RPC: failed to contact portmap (errno %d).\n",
177 dprintk("RPC: registration status %d/%d\n", error, *okay);
179 /* Client deleted automatically because cl_oneshot == 1 */
183 static struct rpc_clnt *
184 pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto)
186 struct rpc_xprt *xprt;
187 struct rpc_clnt *clnt;
189 /* printk("pmap: create xprt\n"); */
190 if (!(xprt = xprt_create_proto(proto, srvaddr, NULL)))
192 xprt->addr.sin_port = htons(RPC_PMAP_PORT);
194 /* printk("pmap: create clnt\n"); */
195 clnt = rpc_create_client(xprt, hostname,
196 &pmap_program, RPC_PMAP_VERSION,
201 clnt->cl_softrtry = 1;
203 clnt->cl_oneshot = 1;
209 * XDR encode/decode functions for PMAP
212 xdr_error(struct rpc_rqst *req, u32 *p, void *dummy)
218 xdr_encode_mapping(struct rpc_rqst *req, u32 *p, struct rpc_portmap *map)
220 dprintk("RPC: xdr_encode_mapping(%d, %d, %d, %d)\n",
221 map->pm_prog, map->pm_vers, map->pm_prot, map->pm_port);
222 *p++ = htonl(map->pm_prog);
223 *p++ = htonl(map->pm_vers);
224 *p++ = htonl(map->pm_prot);
225 *p++ = htonl(map->pm_port);
227 req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
232 xdr_decode_port(struct rpc_rqst *req, u32 *p, unsigned short *portp)
234 *portp = (unsigned short) ntohl(*p++);
239 xdr_decode_bool(struct rpc_rqst *req, u32 *p, unsigned int *boolp)
241 *boolp = (unsigned int) ntohl(*p++);
245 static struct rpc_procinfo pmap_procedures[4] = {
247 (kxdrproc_t) xdr_error,
248 (kxdrproc_t) xdr_error, 0, 0 },
250 (kxdrproc_t) xdr_encode_mapping,
251 (kxdrproc_t) xdr_decode_bool, 4, 1 },
253 (kxdrproc_t) xdr_encode_mapping,
254 (kxdrproc_t) xdr_decode_bool, 4, 1 },
256 (kxdrproc_t) xdr_encode_mapping,
257 (kxdrproc_t) xdr_decode_port, 4, 1 },
260 static struct rpc_version pmap_version2 = {
261 2, 4, pmap_procedures
264 static struct rpc_version * pmap_version[] = {
270 static struct rpc_stat pmap_stats;
272 struct rpc_program pmap_program = {
275 sizeof(pmap_version)/sizeof(pmap_version[0]),