Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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[]; |
Chuck Lever | 6cd7525 | 2005-09-22 21:24:59 -0400 | [diff] [blame] | 29 | static struct rpc_clnt * pmap_create(char *, struct sockaddr_in *, int, int); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Andreas Gruenbacher | 007e251 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 56 | /* Autobind on cloned rpc clients is discouraged */ |
| 57 | BUG_ON(clnt->cl_parent != clnt); |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
| 63 | return; |
| 64 | } |
| 65 | map->pm_binding = 1; |
| 66 | spin_unlock(&pmap_lock); |
| 67 | |
Chuck Lever | 6cd7525 | 2005-09-22 21:24:59 -0400 | [diff] [blame] | 68 | pmap_clnt = pmap_create(clnt->cl_server, sap, map->pm_prot, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | if (IS_ERR(pmap_clnt)) { |
| 70 | task->tk_status = PTR_ERR(pmap_clnt); |
| 71 | goto bailout; |
| 72 | } |
| 73 | task->tk_status = 0; |
| 74 | |
| 75 | /* |
| 76 | * Note: rpc_new_child will release client after a failure. |
| 77 | */ |
| 78 | if (!(child = rpc_new_child(pmap_clnt, task))) |
| 79 | goto bailout; |
| 80 | |
| 81 | /* Setup the call info struct */ |
| 82 | rpc_call_setup(child, &msg, 0); |
| 83 | |
| 84 | /* ... and run the child task */ |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 85 | task->tk_xprt->stat.bind_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | rpc_run_child(task, child, pmap_getport_done); |
| 87 | return; |
| 88 | |
| 89 | bailout: |
| 90 | spin_lock(&pmap_lock); |
| 91 | map->pm_binding = 0; |
| 92 | rpc_wake_up(&map->pm_bindwait); |
| 93 | spin_unlock(&pmap_lock); |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 94 | rpc_exit(task, -EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | #ifdef CONFIG_ROOT_NFS |
| 98 | int |
| 99 | rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot) |
| 100 | { |
| 101 | struct rpc_portmap map = { |
| 102 | .pm_prog = prog, |
| 103 | .pm_vers = vers, |
| 104 | .pm_prot = prot, |
| 105 | .pm_port = 0 |
| 106 | }; |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 107 | struct rpc_message msg = { |
| 108 | .rpc_proc = &pmap_procedures[PMAP_GETPORT], |
| 109 | .rpc_argp = &map, |
| 110 | .rpc_resp = &map.pm_port, |
| 111 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | struct rpc_clnt *pmap_clnt; |
| 113 | char hostname[32]; |
| 114 | int status; |
| 115 | |
| 116 | dprintk("RPC: rpc_getport_external(%u.%u.%u.%u, %d, %d, %d)\n", |
| 117 | NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot); |
| 118 | |
| 119 | sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr)); |
Chuck Lever | 6cd7525 | 2005-09-22 21:24:59 -0400 | [diff] [blame] | 120 | pmap_clnt = pmap_create(hostname, sin, prot, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | if (IS_ERR(pmap_clnt)) |
| 122 | return PTR_ERR(pmap_clnt); |
| 123 | |
| 124 | /* Setup the call info struct */ |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 125 | status = rpc_call_sync(pmap_clnt, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | if (status >= 0) { |
| 128 | if (map.pm_port != 0) |
| 129 | return map.pm_port; |
| 130 | status = -EACCES; |
| 131 | } |
| 132 | return status; |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | static void |
| 137 | pmap_getport_done(struct rpc_task *task) |
| 138 | { |
| 139 | struct rpc_clnt *clnt = task->tk_client; |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 140 | struct rpc_xprt *xprt = task->tk_xprt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | struct rpc_portmap *map = clnt->cl_pmap; |
| 142 | |
| 143 | dprintk("RPC: %4d pmap_getport_done(status %d, port %d)\n", |
| 144 | task->tk_pid, task->tk_status, clnt->cl_port); |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 145 | |
| 146 | xprt->ops->set_port(xprt, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | if (task->tk_status < 0) { |
| 148 | /* Make the calling task exit with an error */ |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 149 | task->tk_action = rpc_exit_task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } else if (clnt->cl_port == 0) { |
| 151 | /* Program not registered */ |
Trond Myklebust | abbcf28 | 2006-01-03 09:55:03 +0100 | [diff] [blame] | 152 | rpc_exit(task, -EACCES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } else { |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 154 | xprt->ops->set_port(xprt, clnt->cl_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | clnt->cl_port = htons(clnt->cl_port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | spin_lock(&pmap_lock); |
| 158 | map->pm_binding = 0; |
| 159 | rpc_wake_up(&map->pm_bindwait); |
| 160 | spin_unlock(&pmap_lock); |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Set or unset a port registration with the local portmapper. |
| 165 | * port == 0 means unregister, port != 0 means register. |
| 166 | */ |
| 167 | int |
| 168 | rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay) |
| 169 | { |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 170 | struct sockaddr_in sin = { |
| 171 | .sin_family = AF_INET, |
| 172 | .sin_addr.s_addr = htonl(INADDR_LOOPBACK), |
| 173 | }; |
| 174 | struct rpc_portmap map = { |
| 175 | .pm_prog = prog, |
| 176 | .pm_vers = vers, |
| 177 | .pm_prot = prot, |
| 178 | .pm_port = port, |
| 179 | }; |
| 180 | struct rpc_message msg = { |
| 181 | .rpc_proc = &pmap_procedures[port ? PMAP_SET : PMAP_UNSET], |
| 182 | .rpc_argp = &map, |
| 183 | .rpc_resp = okay, |
| 184 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | struct rpc_clnt *pmap_clnt; |
| 186 | int error = 0; |
| 187 | |
| 188 | dprintk("RPC: registering (%d, %d, %d, %d) with portmapper.\n", |
| 189 | prog, vers, prot, port); |
| 190 | |
Chuck Lever | 6cd7525 | 2005-09-22 21:24:59 -0400 | [diff] [blame] | 191 | pmap_clnt = pmap_create("localhost", &sin, IPPROTO_UDP, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | if (IS_ERR(pmap_clnt)) { |
| 193 | error = PTR_ERR(pmap_clnt); |
| 194 | dprintk("RPC: couldn't create pmap client. Error = %d\n", error); |
| 195 | return error; |
| 196 | } |
| 197 | |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 198 | error = rpc_call_sync(pmap_clnt, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
| 200 | if (error < 0) { |
| 201 | printk(KERN_WARNING |
| 202 | "RPC: failed to contact portmap (errno %d).\n", |
| 203 | error); |
| 204 | } |
| 205 | dprintk("RPC: registration status %d/%d\n", error, *okay); |
| 206 | |
| 207 | /* Client deleted automatically because cl_oneshot == 1 */ |
| 208 | return error; |
| 209 | } |
| 210 | |
| 211 | static struct rpc_clnt * |
Chuck Lever | 6cd7525 | 2005-09-22 21:24:59 -0400 | [diff] [blame] | 212 | pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto, int privileged) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | { |
| 214 | struct rpc_xprt *xprt; |
| 215 | struct rpc_clnt *clnt; |
| 216 | |
| 217 | /* printk("pmap: create xprt\n"); */ |
| 218 | xprt = xprt_create_proto(proto, srvaddr, NULL); |
| 219 | if (IS_ERR(xprt)) |
| 220 | return (struct rpc_clnt *)xprt; |
Chuck Lever | 9220041 | 2006-01-03 09:55:51 +0100 | [diff] [blame] | 221 | xprt->ops->set_port(xprt, RPC_PMAP_PORT); |
Chuck Lever | 6cd7525 | 2005-09-22 21:24:59 -0400 | [diff] [blame] | 222 | if (!privileged) |
| 223 | xprt->resvport = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
| 225 | /* printk("pmap: create clnt\n"); */ |
Trond Myklebust | 5ee0ed7 | 2005-06-22 17:16:20 +0000 | [diff] [blame] | 226 | clnt = rpc_new_client(xprt, hostname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | &pmap_program, RPC_PMAP_VERSION, |
| 228 | RPC_AUTH_UNIX); |
Trond Myklebust | 5b616f5 | 2005-06-22 17:16:20 +0000 | [diff] [blame] | 229 | if (!IS_ERR(clnt)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | clnt->cl_softrtry = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | clnt->cl_oneshot = 1; |
| 232 | } |
| 233 | return clnt; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * XDR encode/decode functions for PMAP |
| 238 | */ |
| 239 | static int |
| 240 | xdr_encode_mapping(struct rpc_rqst *req, u32 *p, struct rpc_portmap *map) |
| 241 | { |
| 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); |
| 248 | |
| 249 | req->rq_slen = xdr_adjust_iovec(req->rq_svec, p); |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | static int |
| 254 | xdr_decode_port(struct rpc_rqst *req, u32 *p, unsigned short *portp) |
| 255 | { |
| 256 | *portp = (unsigned short) ntohl(*p++); |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int |
| 261 | xdr_decode_bool(struct rpc_rqst *req, u32 *p, unsigned int *boolp) |
| 262 | { |
| 263 | *boolp = (unsigned int) ntohl(*p++); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static struct rpc_procinfo pmap_procedures[] = { |
| 268 | [PMAP_SET] = { |
| 269 | .p_proc = PMAP_SET, |
| 270 | .p_encode = (kxdrproc_t) xdr_encode_mapping, |
| 271 | .p_decode = (kxdrproc_t) xdr_decode_bool, |
| 272 | .p_bufsiz = 4, |
| 273 | .p_count = 1, |
Chuck Lever | cc0175c | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 274 | .p_statidx = PMAP_SET, |
| 275 | .p_name = "SET", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | }, |
| 277 | [PMAP_UNSET] = { |
| 278 | .p_proc = PMAP_UNSET, |
| 279 | .p_encode = (kxdrproc_t) xdr_encode_mapping, |
| 280 | .p_decode = (kxdrproc_t) xdr_decode_bool, |
| 281 | .p_bufsiz = 4, |
| 282 | .p_count = 1, |
Chuck Lever | cc0175c | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 283 | .p_statidx = PMAP_UNSET, |
| 284 | .p_name = "UNSET", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | }, |
| 286 | [PMAP_GETPORT] = { |
| 287 | .p_proc = PMAP_GETPORT, |
| 288 | .p_encode = (kxdrproc_t) xdr_encode_mapping, |
| 289 | .p_decode = (kxdrproc_t) xdr_decode_port, |
| 290 | .p_bufsiz = 4, |
| 291 | .p_count = 1, |
Chuck Lever | cc0175c | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 292 | .p_statidx = PMAP_GETPORT, |
| 293 | .p_name = "GETPORT", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | }, |
| 295 | }; |
| 296 | |
| 297 | static struct rpc_version pmap_version2 = { |
| 298 | .number = 2, |
| 299 | .nrprocs = 4, |
| 300 | .procs = pmap_procedures |
| 301 | }; |
| 302 | |
| 303 | static struct rpc_version * pmap_version[] = { |
| 304 | NULL, |
| 305 | NULL, |
| 306 | &pmap_version2 |
| 307 | }; |
| 308 | |
| 309 | static struct rpc_stat pmap_stats; |
| 310 | |
| 311 | static struct rpc_program pmap_program = { |
| 312 | .name = "portmap", |
| 313 | .number = RPC_PMAP_PROGRAM, |
| 314 | .nrvers = ARRAY_SIZE(pmap_version), |
| 315 | .version = pmap_version, |
| 316 | .stats = &pmap_stats, |
| 317 | }; |