Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 1 | /* |
| 2 | * In-kernel rpcbind client supporting versions 2, 3, and 4 of the rpcbind |
| 3 | * protocol |
| 4 | * |
| 5 | * Based on RFC 1833: "Binding Protocols for ONC RPC Version 2" and |
| 6 | * RFC 3530: "Network File System (NFS) version 4 Protocol" |
| 7 | * |
| 8 | * Original: Gilles Quillard, Bull Open Source, 2005 <gilles.quillard@bull.net> |
| 9 | * Updated: Chuck Lever, Oracle Corporation, 2007 <chuck.lever@oracle.com> |
| 10 | * |
| 11 | * Descended from net/sunrpc/pmap_clnt.c, |
| 12 | * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de> |
| 13 | */ |
| 14 | |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 15 | #include <linux/module.h> |
| 16 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 17 | #include <linux/types.h> |
| 18 | #include <linux/socket.h> |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 19 | #include <linux/in.h> |
| 20 | #include <linux/in6.h> |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 21 | #include <linux/kernel.h> |
| 22 | #include <linux/errno.h> |
| 23 | |
| 24 | #include <linux/sunrpc/clnt.h> |
| 25 | #include <linux/sunrpc/sched.h> |
\"Talpey, Thomas\ | 0896a725 | 2007-09-10 13:48:23 -0400 | [diff] [blame] | 26 | #include <linux/sunrpc/xprtsock.h> |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 27 | |
| 28 | #ifdef RPC_DEBUG |
| 29 | # define RPCDBG_FACILITY RPCDBG_BIND |
| 30 | #endif |
| 31 | |
| 32 | #define RPCBIND_PROGRAM (100000u) |
| 33 | #define RPCBIND_PORT (111u) |
| 34 | |
| 35 | enum { |
| 36 | RPCBPROC_NULL, |
| 37 | RPCBPROC_SET, |
| 38 | RPCBPROC_UNSET, |
| 39 | RPCBPROC_GETPORT, |
| 40 | RPCBPROC_GETADDR = 3, /* alias for GETPORT */ |
| 41 | RPCBPROC_DUMP, |
| 42 | RPCBPROC_CALLIT, |
| 43 | RPCBPROC_BCAST = 5, /* alias for CALLIT */ |
| 44 | RPCBPROC_GETTIME, |
| 45 | RPCBPROC_UADDR2TADDR, |
| 46 | RPCBPROC_TADDR2UADDR, |
| 47 | RPCBPROC_GETVERSADDR, |
| 48 | RPCBPROC_INDIRECT, |
| 49 | RPCBPROC_GETADDRLIST, |
| 50 | RPCBPROC_GETSTAT, |
| 51 | }; |
| 52 | |
| 53 | #define RPCB_HIGHPROC_2 RPCBPROC_CALLIT |
| 54 | #define RPCB_HIGHPROC_3 RPCBPROC_TADDR2UADDR |
| 55 | #define RPCB_HIGHPROC_4 RPCBPROC_GETSTAT |
| 56 | |
| 57 | /* |
| 58 | * r_addr |
| 59 | * |
| 60 | * Quoting RFC 3530, section 2.2: |
| 61 | * |
| 62 | * For TCP over IPv4 and for UDP over IPv4, the format of r_addr is the |
| 63 | * US-ASCII string: |
| 64 | * |
| 65 | * h1.h2.h3.h4.p1.p2 |
| 66 | * |
| 67 | * The prefix, "h1.h2.h3.h4", is the standard textual form for |
| 68 | * representing an IPv4 address, which is always four octets long. |
| 69 | * Assuming big-endian ordering, h1, h2, h3, and h4, are respectively, |
| 70 | * the first through fourth octets each converted to ASCII-decimal. |
| 71 | * Assuming big-endian ordering, p1 and p2 are, respectively, the first |
| 72 | * and second octets each converted to ASCII-decimal. For example, if a |
| 73 | * host, in big-endian order, has an address of 0x0A010307 and there is |
| 74 | * a service listening on, in big endian order, port 0x020F (decimal |
| 75 | * 527), then the complete universal address is "10.1.3.7.2.15". |
| 76 | * |
| 77 | * ... |
| 78 | * |
| 79 | * For TCP over IPv6 and for UDP over IPv6, the format of r_addr is the |
| 80 | * US-ASCII string: |
| 81 | * |
| 82 | * x1:x2:x3:x4:x5:x6:x7:x8.p1.p2 |
| 83 | * |
| 84 | * The suffix "p1.p2" is the service port, and is computed the same way |
| 85 | * as with universal addresses for TCP and UDP over IPv4. The prefix, |
| 86 | * "x1:x2:x3:x4:x5:x6:x7:x8", is the standard textual form for |
| 87 | * representing an IPv6 address as defined in Section 2.2 of [RFC2373]. |
| 88 | * Additionally, the two alternative forms specified in Section 2.2 of |
| 89 | * [RFC2373] are also acceptable. |
| 90 | * |
| 91 | * XXX: Currently this implementation does not explicitly convert the |
| 92 | * stored address to US-ASCII on non-ASCII systems. |
| 93 | */ |
| 94 | #define RPCB_MAXADDRLEN (128u) |
| 95 | |
| 96 | /* |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 97 | * r_owner |
| 98 | * |
| 99 | * The "owner" is allowed to unset a service in the rpcbind database. |
| 100 | * We always use the following (arbitrary) fixed string. |
| 101 | */ |
| 102 | #define RPCB_OWNER_STRING "rpcb" |
| 103 | #define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING) |
| 104 | |
| 105 | static void rpcb_getport_done(struct rpc_task *, void *); |
Adrian Bunk | 7f4adef | 2007-06-13 01:03:13 +0200 | [diff] [blame] | 106 | static struct rpc_program rpcb_program; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 107 | |
| 108 | struct rpcbind_args { |
| 109 | struct rpc_xprt * r_xprt; |
| 110 | |
| 111 | u32 r_prog; |
| 112 | u32 r_vers; |
| 113 | u32 r_prot; |
| 114 | unsigned short r_port; |
| 115 | char * r_netid; |
| 116 | char r_addr[RPCB_MAXADDRLEN]; |
| 117 | char * r_owner; |
| 118 | }; |
| 119 | |
| 120 | static struct rpc_procinfo rpcb_procedures2[]; |
| 121 | static struct rpc_procinfo rpcb_procedures3[]; |
| 122 | |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 123 | struct rpcb_info { |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 124 | int rpc_vers; |
| 125 | struct rpc_procinfo * rpc_proc; |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | static struct rpcb_info rpcb_next_version[]; |
| 129 | static struct rpcb_info rpcb_next_version6[]; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 130 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 131 | static void rpcb_map_release(void *data) |
| 132 | { |
| 133 | struct rpcbind_args *map = data; |
| 134 | |
| 135 | xprt_put(map->r_xprt); |
| 136 | kfree(map); |
| 137 | } |
| 138 | |
| 139 | static const struct rpc_call_ops rpcb_getport_ops = { |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 140 | .rpc_call_done = rpcb_getport_done, |
| 141 | .rpc_release = rpcb_map_release, |
| 142 | }; |
| 143 | |
| 144 | static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status) |
| 145 | { |
| 146 | xprt_clear_binding(xprt); |
| 147 | rpc_wake_up_status(&xprt->binding, status); |
| 148 | } |
| 149 | |
| 150 | static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr, |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame^] | 151 | size_t salen, int proto, int version, |
| 152 | int privileged) |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 153 | { |
| 154 | struct rpc_create_args args = { |
| 155 | .protocol = proto, |
| 156 | .address = srvaddr, |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame^] | 157 | .addrsize = salen, |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 158 | .servername = hostname, |
| 159 | .program = &rpcb_program, |
| 160 | .version = version, |
| 161 | .authflavor = RPC_AUTH_UNIX, |
Chuck Lever | f7fb558 | 2007-07-01 12:13:07 -0400 | [diff] [blame] | 162 | .flags = (RPC_CLNT_CREATE_NOPING | |
| 163 | RPC_CLNT_CREATE_INTR), |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 164 | }; |
| 165 | |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 166 | switch (srvaddr->sa_family) { |
| 167 | case AF_INET: |
| 168 | ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT); |
| 169 | break; |
| 170 | case AF_INET6: |
| 171 | ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT); |
| 172 | break; |
| 173 | default: |
| 174 | return NULL; |
| 175 | } |
| 176 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 177 | if (!privileged) |
| 178 | args.flags |= RPC_CLNT_CREATE_NONPRIVPORT; |
| 179 | return rpc_create(&args); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * rpcb_register - set or unset a port registration with the local rpcbind svc |
| 184 | * @prog: RPC program number to bind |
| 185 | * @vers: RPC version number to bind |
| 186 | * @prot: transport protocol to use to make this request |
| 187 | * @port: port value to register |
| 188 | * @okay: result code |
| 189 | * |
| 190 | * port == 0 means unregister, port != 0 means register. |
| 191 | * |
| 192 | * This routine supports only rpcbind version 2. |
| 193 | */ |
| 194 | int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay) |
| 195 | { |
| 196 | struct sockaddr_in sin = { |
| 197 | .sin_family = AF_INET, |
| 198 | .sin_addr.s_addr = htonl(INADDR_LOOPBACK), |
| 199 | }; |
| 200 | struct rpcbind_args map = { |
| 201 | .r_prog = prog, |
| 202 | .r_vers = vers, |
| 203 | .r_prot = prot, |
| 204 | .r_port = port, |
| 205 | }; |
| 206 | struct rpc_message msg = { |
| 207 | .rpc_proc = &rpcb_procedures2[port ? |
| 208 | RPCBPROC_SET : RPCBPROC_UNSET], |
| 209 | .rpc_argp = &map, |
| 210 | .rpc_resp = okay, |
| 211 | }; |
| 212 | struct rpc_clnt *rpcb_clnt; |
| 213 | int error = 0; |
| 214 | |
| 215 | dprintk("RPC: %sregistering (%u, %u, %d, %u) with local " |
| 216 | "rpcbind\n", (port ? "" : "un"), |
| 217 | prog, vers, prot, port); |
| 218 | |
| 219 | rpcb_clnt = rpcb_create("localhost", (struct sockaddr *) &sin, |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame^] | 220 | sizeof(sin), XPRT_TRANSPORT_UDP, 2, 1); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 221 | if (IS_ERR(rpcb_clnt)) |
| 222 | return PTR_ERR(rpcb_clnt); |
| 223 | |
| 224 | error = rpc_call_sync(rpcb_clnt, &msg, 0); |
| 225 | |
Trond Myklebust | 90c5755 | 2007-06-09 19:49:36 -0400 | [diff] [blame] | 226 | rpc_shutdown_client(rpcb_clnt); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 227 | if (error < 0) |
| 228 | printk(KERN_WARNING "RPC: failed to contact local rpcbind " |
| 229 | "server (errno %d).\n", -error); |
| 230 | dprintk("RPC: registration status %d/%d\n", error, *okay); |
| 231 | |
| 232 | return error; |
| 233 | } |
| 234 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 235 | /** |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 236 | * rpcb_getport_sync - obtain the port for an RPC service on a given host |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 237 | * @sin: address of remote peer |
| 238 | * @prog: RPC program number to bind |
| 239 | * @vers: RPC version number to bind |
| 240 | * @prot: transport protocol to use to make this request |
| 241 | * |
| 242 | * Called from outside the RPC client in a synchronous task context. |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 243 | * Uses default timeout parameters specified by underlying transport. |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 244 | * |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 245 | * XXX: Needs to support IPv6, and rpcbind versions 3 and 4 |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 246 | */ |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 247 | int rpcb_getport_sync(struct sockaddr_in *sin, __u32 prog, |
| 248 | __u32 vers, int prot) |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 249 | { |
| 250 | struct rpcbind_args map = { |
| 251 | .r_prog = prog, |
| 252 | .r_vers = vers, |
| 253 | .r_prot = prot, |
| 254 | .r_port = 0, |
| 255 | }; |
| 256 | struct rpc_message msg = { |
| 257 | .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT], |
| 258 | .rpc_argp = &map, |
| 259 | .rpc_resp = &map.r_port, |
| 260 | }; |
| 261 | struct rpc_clnt *rpcb_clnt; |
| 262 | char hostname[40]; |
| 263 | int status; |
| 264 | |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 265 | dprintk("RPC: %s(" NIPQUAD_FMT ", %u, %u, %d)\n", |
| 266 | __FUNCTION__, NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 267 | |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 268 | sprintf(hostname, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr)); |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame^] | 269 | rpcb_clnt = rpcb_create(hostname, (struct sockaddr *)sin, |
| 270 | sizeof(sin), prot, 2, 0); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 271 | if (IS_ERR(rpcb_clnt)) |
| 272 | return PTR_ERR(rpcb_clnt); |
| 273 | |
| 274 | status = rpc_call_sync(rpcb_clnt, &msg, 0); |
Trond Myklebust | 90c5755 | 2007-06-09 19:49:36 -0400 | [diff] [blame] | 275 | rpc_shutdown_client(rpcb_clnt); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 276 | |
| 277 | if (status >= 0) { |
| 278 | if (map.r_port != 0) |
| 279 | return map.r_port; |
| 280 | status = -EACCES; |
| 281 | } |
| 282 | return status; |
| 283 | } |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 284 | EXPORT_SYMBOL_GPL(rpcb_getport_sync); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 285 | |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 286 | static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt, struct rpcbind_args *map, int version) |
| 287 | { |
| 288 | struct rpc_message msg = { |
| 289 | .rpc_proc = rpcb_next_version[version].rpc_proc, |
| 290 | .rpc_argp = map, |
| 291 | .rpc_resp = &map->r_port, |
| 292 | }; |
| 293 | struct rpc_task_setup task_setup_data = { |
| 294 | .rpc_client = rpcb_clnt, |
| 295 | .rpc_message = &msg, |
| 296 | .callback_ops = &rpcb_getport_ops, |
| 297 | .callback_data = map, |
| 298 | .flags = RPC_TASK_ASYNC, |
| 299 | }; |
| 300 | |
| 301 | return rpc_run_task(&task_setup_data); |
| 302 | } |
| 303 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 304 | /** |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 305 | * rpcb_getport_async - obtain the port for a given RPC service on a given host |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 306 | * @task: task that is waiting for portmapper request |
| 307 | * |
| 308 | * This one can be called for an ongoing RPC request, and can be used in |
| 309 | * an async (rpciod) context. |
| 310 | */ |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 311 | void rpcb_getport_async(struct rpc_task *task) |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 312 | { |
| 313 | struct rpc_clnt *clnt = task->tk_client; |
| 314 | int bind_version; |
| 315 | struct rpc_xprt *xprt = task->tk_xprt; |
| 316 | struct rpc_clnt *rpcb_clnt; |
| 317 | static struct rpcbind_args *map; |
| 318 | struct rpc_task *child; |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame^] | 319 | struct sockaddr_storage addr; |
| 320 | struct sockaddr *sap = (struct sockaddr *)&addr; |
| 321 | size_t salen; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 322 | int status; |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 323 | struct rpcb_info *info; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 324 | |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 325 | dprintk("RPC: %5u %s(%s, %u, %u, %d)\n", |
| 326 | task->tk_pid, __FUNCTION__, |
| 327 | clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 328 | |
| 329 | /* Autobind on cloned rpc clients is discouraged */ |
| 330 | BUG_ON(clnt->cl_parent != clnt); |
| 331 | |
| 332 | if (xprt_test_and_set_binding(xprt)) { |
Chuck Lever | 2429cbf | 2007-09-11 18:00:41 -0400 | [diff] [blame] | 333 | status = -EAGAIN; /* tell caller to check again */ |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 334 | dprintk("RPC: %5u %s: waiting for another binder\n", |
| 335 | task->tk_pid, __FUNCTION__); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 336 | goto bailout_nowake; |
| 337 | } |
| 338 | |
| 339 | /* Put self on queue before sending rpcbind request, in case |
| 340 | * rpcb_getport_done completes before we return from rpc_run_task */ |
| 341 | rpc_sleep_on(&xprt->binding, task, NULL, NULL); |
| 342 | |
| 343 | /* Someone else may have bound if we slept */ |
| 344 | if (xprt_bound(xprt)) { |
| 345 | status = 0; |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 346 | dprintk("RPC: %5u %s: already bound\n", |
| 347 | task->tk_pid, __FUNCTION__); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 348 | goto bailout_nofree; |
| 349 | } |
| 350 | |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame^] | 351 | salen = rpc_peeraddr(clnt, sap, sizeof(addr)); |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 352 | |
| 353 | /* Don't ever use rpcbind v2 for AF_INET6 requests */ |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame^] | 354 | switch (sap->sa_family) { |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 355 | case AF_INET: |
| 356 | info = rpcb_next_version; |
| 357 | break; |
| 358 | case AF_INET6: |
| 359 | info = rpcb_next_version6; |
| 360 | break; |
| 361 | default: |
| 362 | status = -EAFNOSUPPORT; |
| 363 | dprintk("RPC: %5u %s: bad address family\n", |
| 364 | task->tk_pid, __FUNCTION__); |
| 365 | goto bailout_nofree; |
| 366 | } |
| 367 | if (info[xprt->bind_index].rpc_proc == NULL) { |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 368 | xprt->bind_index = 0; |
Chuck Lever | 906462a | 2007-09-11 18:00:47 -0400 | [diff] [blame] | 369 | status = -EPFNOSUPPORT; |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 370 | dprintk("RPC: %5u %s: no more getport versions available\n", |
| 371 | task->tk_pid, __FUNCTION__); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 372 | goto bailout_nofree; |
| 373 | } |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 374 | bind_version = info[xprt->bind_index].rpc_vers; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 375 | |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 376 | dprintk("RPC: %5u %s: trying rpcbind version %u\n", |
| 377 | task->tk_pid, __FUNCTION__, bind_version); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 378 | |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame^] | 379 | rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot, |
Chuck Lever | 143b6c4 | 2007-08-16 16:03:31 -0400 | [diff] [blame] | 380 | bind_version, 0); |
| 381 | if (IS_ERR(rpcb_clnt)) { |
| 382 | status = PTR_ERR(rpcb_clnt); |
| 383 | dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n", |
| 384 | task->tk_pid, __FUNCTION__, PTR_ERR(rpcb_clnt)); |
| 385 | goto bailout_nofree; |
| 386 | } |
| 387 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 388 | map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC); |
| 389 | if (!map) { |
| 390 | status = -ENOMEM; |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 391 | dprintk("RPC: %5u %s: no memory available\n", |
| 392 | task->tk_pid, __FUNCTION__); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 393 | goto bailout_nofree; |
| 394 | } |
| 395 | map->r_prog = clnt->cl_prog; |
| 396 | map->r_vers = clnt->cl_vers; |
| 397 | map->r_prot = xprt->prot; |
| 398 | map->r_port = 0; |
| 399 | map->r_xprt = xprt_get(xprt); |
\"Talpey, Thomas\ | 4417c8c | 2007-09-10 13:43:05 -0400 | [diff] [blame] | 400 | map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID); |
Chuck Lever | 6d0aa06 | 2007-09-11 18:00:15 -0400 | [diff] [blame] | 401 | memcpy(map->r_addr, |
Chuck Lever | 143b6c4 | 2007-08-16 16:03:31 -0400 | [diff] [blame] | 402 | rpc_peeraddr2str(rpcb_clnt, RPC_DISPLAY_UNIVERSAL_ADDR), |
| 403 | sizeof(map->r_addr)); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 404 | map->r_owner = RPCB_OWNER_STRING; /* ignored for GETADDR */ |
| 405 | |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 406 | child = rpcb_call_async(rpcb_clnt, map, xprt->bind_index); |
Trond Myklebust | 4c402b4 | 2007-06-14 16:40:32 -0400 | [diff] [blame] | 407 | rpc_release_client(rpcb_clnt); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 408 | if (IS_ERR(child)) { |
| 409 | status = -EIO; |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 410 | dprintk("RPC: %5u %s: rpc_run_task failed\n", |
| 411 | task->tk_pid, __FUNCTION__); |
Chuck Lever | 143b6c4 | 2007-08-16 16:03:31 -0400 | [diff] [blame] | 412 | goto bailout; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 413 | } |
| 414 | rpc_put_task(child); |
| 415 | |
| 416 | task->tk_xprt->stat.bind_count++; |
| 417 | return; |
| 418 | |
| 419 | bailout: |
| 420 | kfree(map); |
| 421 | xprt_put(xprt); |
| 422 | bailout_nofree: |
| 423 | rpcb_wake_rpcbind_waiters(xprt, status); |
| 424 | bailout_nowake: |
| 425 | task->tk_status = status; |
| 426 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 427 | EXPORT_SYMBOL_GPL(rpcb_getport_async); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * Rpcbind child task calls this callback via tk_exit. |
| 431 | */ |
| 432 | static void rpcb_getport_done(struct rpc_task *child, void *data) |
| 433 | { |
| 434 | struct rpcbind_args *map = data; |
| 435 | struct rpc_xprt *xprt = map->r_xprt; |
| 436 | int status = child->tk_status; |
| 437 | |
Chuck Lever | 4784cb5 | 2007-09-11 18:00:36 -0400 | [diff] [blame] | 438 | /* Garbage reply: retry with a lesser rpcbind version */ |
| 439 | if (status == -EIO) |
| 440 | status = -EPROTONOSUPPORT; |
| 441 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 442 | /* rpcbind server doesn't support this rpcbind protocol version */ |
| 443 | if (status == -EPROTONOSUPPORT) |
| 444 | xprt->bind_index++; |
| 445 | |
| 446 | if (status < 0) { |
| 447 | /* rpcbind server not available on remote host? */ |
| 448 | xprt->ops->set_port(xprt, 0); |
| 449 | } else if (map->r_port == 0) { |
| 450 | /* Requested RPC service wasn't registered on remote host */ |
| 451 | xprt->ops->set_port(xprt, 0); |
| 452 | status = -EACCES; |
| 453 | } else { |
| 454 | /* Succeeded */ |
| 455 | xprt->ops->set_port(xprt, map->r_port); |
| 456 | xprt_set_bound(xprt); |
| 457 | status = 0; |
| 458 | } |
| 459 | |
| 460 | dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n", |
| 461 | child->tk_pid, status, map->r_port); |
| 462 | |
| 463 | rpcb_wake_rpcbind_waiters(xprt, status); |
| 464 | } |
| 465 | |
| 466 | static int rpcb_encode_mapping(struct rpc_rqst *req, __be32 *p, |
| 467 | struct rpcbind_args *rpcb) |
| 468 | { |
| 469 | dprintk("RPC: rpcb_encode_mapping(%u, %u, %d, %u)\n", |
| 470 | rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port); |
| 471 | *p++ = htonl(rpcb->r_prog); |
| 472 | *p++ = htonl(rpcb->r_vers); |
| 473 | *p++ = htonl(rpcb->r_prot); |
| 474 | *p++ = htonl(rpcb->r_port); |
| 475 | |
| 476 | req->rq_slen = xdr_adjust_iovec(req->rq_svec, p); |
| 477 | return 0; |
| 478 | } |
| 479 | |
| 480 | static int rpcb_decode_getport(struct rpc_rqst *req, __be32 *p, |
| 481 | unsigned short *portp) |
| 482 | { |
| 483 | *portp = (unsigned short) ntohl(*p++); |
| 484 | dprintk("RPC: rpcb_decode_getport result %u\n", |
| 485 | *portp); |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static int rpcb_decode_set(struct rpc_rqst *req, __be32 *p, |
| 490 | unsigned int *boolp) |
| 491 | { |
| 492 | *boolp = (unsigned int) ntohl(*p++); |
| 493 | dprintk("RPC: rpcb_decode_set result %u\n", |
| 494 | *boolp); |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | static int rpcb_encode_getaddr(struct rpc_rqst *req, __be32 *p, |
| 499 | struct rpcbind_args *rpcb) |
| 500 | { |
| 501 | dprintk("RPC: rpcb_encode_getaddr(%u, %u, %s)\n", |
| 502 | rpcb->r_prog, rpcb->r_vers, rpcb->r_addr); |
| 503 | *p++ = htonl(rpcb->r_prog); |
| 504 | *p++ = htonl(rpcb->r_vers); |
| 505 | |
| 506 | p = xdr_encode_string(p, rpcb->r_netid); |
| 507 | p = xdr_encode_string(p, rpcb->r_addr); |
| 508 | p = xdr_encode_string(p, rpcb->r_owner); |
| 509 | |
| 510 | req->rq_slen = xdr_adjust_iovec(req->rq_svec, p); |
| 511 | |
| 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | static int rpcb_decode_getaddr(struct rpc_rqst *req, __be32 *p, |
| 516 | unsigned short *portp) |
| 517 | { |
| 518 | char *addr; |
Chuck Lever | adc24df8 | 2007-08-06 11:56:31 -0400 | [diff] [blame] | 519 | u32 addr_len; |
| 520 | int c, i, f, first, val; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 521 | |
| 522 | *portp = 0; |
Chuck Lever | adc24df8 | 2007-08-06 11:56:31 -0400 | [diff] [blame] | 523 | addr_len = ntohl(*p++); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 524 | |
Chuck Lever | e65fe39 | 2007-09-11 18:00:31 -0400 | [diff] [blame] | 525 | /* |
| 526 | * Simple sanity check. The smallest possible universal |
| 527 | * address is an IPv4 address string containing 11 bytes. |
| 528 | */ |
| 529 | if (addr_len < 11 || addr_len > RPCB_MAXADDRLEN) |
| 530 | goto out_err; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 531 | |
Chuck Lever | e65fe39 | 2007-09-11 18:00:31 -0400 | [diff] [blame] | 532 | /* |
| 533 | * Start at the end and walk backwards until the first dot |
| 534 | * is encountered. When the second dot is found, we have |
| 535 | * both parts of the port number. |
| 536 | */ |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 537 | addr = (char *)p; |
| 538 | val = 0; |
| 539 | first = 1; |
| 540 | f = 1; |
| 541 | for (i = addr_len - 1; i > 0; i--) { |
| 542 | c = addr[i]; |
| 543 | if (c >= '0' && c <= '9') { |
| 544 | val += (c - '0') * f; |
| 545 | f *= 10; |
| 546 | } else if (c == '.') { |
| 547 | if (first) { |
| 548 | *portp = val; |
| 549 | val = first = 0; |
| 550 | f = 1; |
| 551 | } else { |
| 552 | *portp |= (val << 8); |
| 553 | break; |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
Chuck Lever | e65fe39 | 2007-09-11 18:00:31 -0400 | [diff] [blame] | 558 | /* |
| 559 | * Simple sanity check. If we never saw a dot in the reply, |
| 560 | * then this was probably just garbage. |
| 561 | */ |
| 562 | if (first) |
| 563 | goto out_err; |
| 564 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 565 | dprintk("RPC: rpcb_decode_getaddr port=%u\n", *portp); |
| 566 | return 0; |
Chuck Lever | e65fe39 | 2007-09-11 18:00:31 -0400 | [diff] [blame] | 567 | |
| 568 | out_err: |
| 569 | dprintk("RPC: rpcbind server returned malformed reply\n"); |
| 570 | return -EIO; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | #define RPCB_program_sz (1u) |
| 574 | #define RPCB_version_sz (1u) |
| 575 | #define RPCB_protocol_sz (1u) |
| 576 | #define RPCB_port_sz (1u) |
| 577 | #define RPCB_boolean_sz (1u) |
| 578 | |
\"Talpey, Thomas\ | 4f40ee4 | 2007-09-10 13:42:38 -0400 | [diff] [blame] | 579 | #define RPCB_netid_sz (1+XDR_QUADLEN(RPCBIND_MAXNETIDLEN)) |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 580 | #define RPCB_addr_sz (1+XDR_QUADLEN(RPCB_MAXADDRLEN)) |
| 581 | #define RPCB_ownerstring_sz (1+XDR_QUADLEN(RPCB_MAXOWNERLEN)) |
| 582 | |
| 583 | #define RPCB_mappingargs_sz RPCB_program_sz+RPCB_version_sz+ \ |
| 584 | RPCB_protocol_sz+RPCB_port_sz |
| 585 | #define RPCB_getaddrargs_sz RPCB_program_sz+RPCB_version_sz+ \ |
| 586 | RPCB_netid_sz+RPCB_addr_sz+ \ |
| 587 | RPCB_ownerstring_sz |
| 588 | |
| 589 | #define RPCB_setres_sz RPCB_boolean_sz |
| 590 | #define RPCB_getportres_sz RPCB_port_sz |
| 591 | |
| 592 | /* |
| 593 | * Note that RFC 1833 does not put any size restrictions on the |
| 594 | * address string returned by the remote rpcbind database. |
| 595 | */ |
| 596 | #define RPCB_getaddrres_sz RPCB_addr_sz |
| 597 | |
| 598 | #define PROC(proc, argtype, restype) \ |
| 599 | [RPCBPROC_##proc] = { \ |
| 600 | .p_proc = RPCBPROC_##proc, \ |
| 601 | .p_encode = (kxdrproc_t) rpcb_encode_##argtype, \ |
| 602 | .p_decode = (kxdrproc_t) rpcb_decode_##restype, \ |
| 603 | .p_arglen = RPCB_##argtype##args_sz, \ |
| 604 | .p_replen = RPCB_##restype##res_sz, \ |
| 605 | .p_statidx = RPCBPROC_##proc, \ |
| 606 | .p_timer = 0, \ |
| 607 | .p_name = #proc, \ |
| 608 | } |
| 609 | |
| 610 | /* |
| 611 | * Not all rpcbind procedures described in RFC 1833 are implemented |
| 612 | * since the Linux kernel RPC code requires only these. |
| 613 | */ |
| 614 | static struct rpc_procinfo rpcb_procedures2[] = { |
| 615 | PROC(SET, mapping, set), |
| 616 | PROC(UNSET, mapping, set), |
| 617 | PROC(GETADDR, mapping, getport), |
| 618 | }; |
| 619 | |
| 620 | static struct rpc_procinfo rpcb_procedures3[] = { |
| 621 | PROC(SET, mapping, set), |
| 622 | PROC(UNSET, mapping, set), |
| 623 | PROC(GETADDR, getaddr, getaddr), |
| 624 | }; |
| 625 | |
| 626 | static struct rpc_procinfo rpcb_procedures4[] = { |
| 627 | PROC(SET, mapping, set), |
| 628 | PROC(UNSET, mapping, set), |
| 629 | PROC(GETVERSADDR, getaddr, getaddr), |
| 630 | }; |
| 631 | |
| 632 | static struct rpcb_info rpcb_next_version[] = { |
| 633 | #ifdef CONFIG_SUNRPC_BIND34 |
| 634 | { 4, &rpcb_procedures4[RPCBPROC_GETVERSADDR] }, |
| 635 | { 3, &rpcb_procedures3[RPCBPROC_GETADDR] }, |
| 636 | #endif |
| 637 | { 2, &rpcb_procedures2[RPCBPROC_GETPORT] }, |
| 638 | { 0, NULL }, |
| 639 | }; |
| 640 | |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 641 | static struct rpcb_info rpcb_next_version6[] = { |
| 642 | #ifdef CONFIG_SUNRPC_BIND34 |
| 643 | { 4, &rpcb_procedures4[RPCBPROC_GETVERSADDR] }, |
| 644 | { 3, &rpcb_procedures3[RPCBPROC_GETADDR] }, |
| 645 | #endif |
| 646 | { 0, NULL }, |
| 647 | }; |
| 648 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 649 | static struct rpc_version rpcb_version2 = { |
| 650 | .number = 2, |
| 651 | .nrprocs = RPCB_HIGHPROC_2, |
| 652 | .procs = rpcb_procedures2 |
| 653 | }; |
| 654 | |
| 655 | static struct rpc_version rpcb_version3 = { |
| 656 | .number = 3, |
| 657 | .nrprocs = RPCB_HIGHPROC_3, |
| 658 | .procs = rpcb_procedures3 |
| 659 | }; |
| 660 | |
| 661 | static struct rpc_version rpcb_version4 = { |
| 662 | .number = 4, |
| 663 | .nrprocs = RPCB_HIGHPROC_4, |
| 664 | .procs = rpcb_procedures4 |
| 665 | }; |
| 666 | |
| 667 | static struct rpc_version *rpcb_version[] = { |
| 668 | NULL, |
| 669 | NULL, |
| 670 | &rpcb_version2, |
| 671 | &rpcb_version3, |
| 672 | &rpcb_version4 |
| 673 | }; |
| 674 | |
| 675 | static struct rpc_stat rpcb_stats; |
| 676 | |
Adrian Bunk | 7f4adef | 2007-06-13 01:03:13 +0200 | [diff] [blame] | 677 | static struct rpc_program rpcb_program = { |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 678 | .name = "rpcbind", |
| 679 | .number = RPCBIND_PROGRAM, |
| 680 | .nrvers = ARRAY_SIZE(rpcb_version), |
| 681 | .version = rpcb_version, |
| 682 | .stats = &rpcb_stats, |
| 683 | }; |