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\ | 0896a72 | 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 | |
| 131 | static void rpcb_getport_prepare(struct rpc_task *task, void *calldata) |
| 132 | { |
| 133 | struct rpcbind_args *map = calldata; |
| 134 | struct rpc_xprt *xprt = map->r_xprt; |
| 135 | struct rpc_message msg = { |
| 136 | .rpc_proc = rpcb_next_version[xprt->bind_index].rpc_proc, |
| 137 | .rpc_argp = map, |
| 138 | .rpc_resp = &map->r_port, |
| 139 | }; |
| 140 | |
| 141 | rpc_call_setup(task, &msg, 0); |
| 142 | } |
| 143 | |
| 144 | static void rpcb_map_release(void *data) |
| 145 | { |
| 146 | struct rpcbind_args *map = data; |
| 147 | |
| 148 | xprt_put(map->r_xprt); |
| 149 | kfree(map); |
| 150 | } |
| 151 | |
| 152 | static const struct rpc_call_ops rpcb_getport_ops = { |
| 153 | .rpc_call_prepare = rpcb_getport_prepare, |
| 154 | .rpc_call_done = rpcb_getport_done, |
| 155 | .rpc_release = rpcb_map_release, |
| 156 | }; |
| 157 | |
| 158 | static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status) |
| 159 | { |
| 160 | xprt_clear_binding(xprt); |
| 161 | rpc_wake_up_status(&xprt->binding, status); |
| 162 | } |
| 163 | |
| 164 | static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr, |
| 165 | int proto, int version, int privileged) |
| 166 | { |
| 167 | struct rpc_create_args args = { |
| 168 | .protocol = proto, |
| 169 | .address = srvaddr, |
| 170 | .addrsize = sizeof(struct sockaddr_in), |
| 171 | .servername = hostname, |
| 172 | .program = &rpcb_program, |
| 173 | .version = version, |
| 174 | .authflavor = RPC_AUTH_UNIX, |
Chuck Lever | f7fb558 | 2007-07-01 12:13:07 -0400 | [diff] [blame] | 175 | .flags = (RPC_CLNT_CREATE_NOPING | |
| 176 | RPC_CLNT_CREATE_INTR), |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 177 | }; |
| 178 | |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 179 | switch (srvaddr->sa_family) { |
| 180 | case AF_INET: |
| 181 | ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT); |
| 182 | break; |
| 183 | case AF_INET6: |
| 184 | ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT); |
| 185 | break; |
| 186 | default: |
| 187 | return NULL; |
| 188 | } |
| 189 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 190 | if (!privileged) |
| 191 | args.flags |= RPC_CLNT_CREATE_NONPRIVPORT; |
| 192 | return rpc_create(&args); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * rpcb_register - set or unset a port registration with the local rpcbind svc |
| 197 | * @prog: RPC program number to bind |
| 198 | * @vers: RPC version number to bind |
| 199 | * @prot: transport protocol to use to make this request |
| 200 | * @port: port value to register |
| 201 | * @okay: result code |
| 202 | * |
| 203 | * port == 0 means unregister, port != 0 means register. |
| 204 | * |
| 205 | * This routine supports only rpcbind version 2. |
| 206 | */ |
| 207 | int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay) |
| 208 | { |
| 209 | struct sockaddr_in sin = { |
| 210 | .sin_family = AF_INET, |
| 211 | .sin_addr.s_addr = htonl(INADDR_LOOPBACK), |
| 212 | }; |
| 213 | struct rpcbind_args map = { |
| 214 | .r_prog = prog, |
| 215 | .r_vers = vers, |
| 216 | .r_prot = prot, |
| 217 | .r_port = port, |
| 218 | }; |
| 219 | struct rpc_message msg = { |
| 220 | .rpc_proc = &rpcb_procedures2[port ? |
| 221 | RPCBPROC_SET : RPCBPROC_UNSET], |
| 222 | .rpc_argp = &map, |
| 223 | .rpc_resp = okay, |
| 224 | }; |
| 225 | struct rpc_clnt *rpcb_clnt; |
| 226 | int error = 0; |
| 227 | |
| 228 | dprintk("RPC: %sregistering (%u, %u, %d, %u) with local " |
| 229 | "rpcbind\n", (port ? "" : "un"), |
| 230 | prog, vers, prot, port); |
| 231 | |
| 232 | rpcb_clnt = rpcb_create("localhost", (struct sockaddr *) &sin, |
\"Talpey, Thomas\ | 0896a72 | 2007-09-10 13:48:23 -0400 | [diff] [blame] | 233 | XPRT_TRANSPORT_UDP, 2, 1); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 234 | if (IS_ERR(rpcb_clnt)) |
| 235 | return PTR_ERR(rpcb_clnt); |
| 236 | |
| 237 | error = rpc_call_sync(rpcb_clnt, &msg, 0); |
| 238 | |
Trond Myklebust | 90c5755 | 2007-06-09 19:49:36 -0400 | [diff] [blame] | 239 | rpc_shutdown_client(rpcb_clnt); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 240 | if (error < 0) |
| 241 | printk(KERN_WARNING "RPC: failed to contact local rpcbind " |
| 242 | "server (errno %d).\n", -error); |
| 243 | dprintk("RPC: registration status %d/%d\n", error, *okay); |
| 244 | |
| 245 | return error; |
| 246 | } |
| 247 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 248 | /** |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 249 | * 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] | 250 | * @sin: address of remote peer |
| 251 | * @prog: RPC program number to bind |
| 252 | * @vers: RPC version number to bind |
| 253 | * @prot: transport protocol to use to make this request |
| 254 | * |
| 255 | * Called from outside the RPC client in a synchronous task context. |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 256 | * Uses default timeout parameters specified by underlying transport. |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 257 | * |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 258 | * XXX: Needs to support IPv6, and rpcbind versions 3 and 4 |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 259 | */ |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 260 | int rpcb_getport_sync(struct sockaddr_in *sin, __u32 prog, |
| 261 | __u32 vers, int prot) |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 262 | { |
| 263 | struct rpcbind_args map = { |
| 264 | .r_prog = prog, |
| 265 | .r_vers = vers, |
| 266 | .r_prot = prot, |
| 267 | .r_port = 0, |
| 268 | }; |
| 269 | struct rpc_message msg = { |
| 270 | .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT], |
| 271 | .rpc_argp = &map, |
| 272 | .rpc_resp = &map.r_port, |
| 273 | }; |
| 274 | struct rpc_clnt *rpcb_clnt; |
| 275 | char hostname[40]; |
| 276 | int status; |
| 277 | |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 278 | dprintk("RPC: %s(" NIPQUAD_FMT ", %u, %u, %d)\n", |
| 279 | __FUNCTION__, NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 280 | |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 281 | sprintf(hostname, NIPQUAD_FMT, NIPQUAD(sin->sin_addr.s_addr)); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 282 | rpcb_clnt = rpcb_create(hostname, (struct sockaddr *)sin, prot, 2, 0); |
| 283 | if (IS_ERR(rpcb_clnt)) |
| 284 | return PTR_ERR(rpcb_clnt); |
| 285 | |
| 286 | status = rpc_call_sync(rpcb_clnt, &msg, 0); |
Trond Myklebust | 90c5755 | 2007-06-09 19:49:36 -0400 | [diff] [blame] | 287 | rpc_shutdown_client(rpcb_clnt); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 288 | |
| 289 | if (status >= 0) { |
| 290 | if (map.r_port != 0) |
| 291 | return map.r_port; |
| 292 | status = -EACCES; |
| 293 | } |
| 294 | return status; |
| 295 | } |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 296 | EXPORT_SYMBOL_GPL(rpcb_getport_sync); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 297 | |
| 298 | /** |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 299 | * 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] | 300 | * @task: task that is waiting for portmapper request |
| 301 | * |
| 302 | * This one can be called for an ongoing RPC request, and can be used in |
| 303 | * an async (rpciod) context. |
| 304 | */ |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 305 | void rpcb_getport_async(struct rpc_task *task) |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 306 | { |
| 307 | struct rpc_clnt *clnt = task->tk_client; |
| 308 | int bind_version; |
| 309 | struct rpc_xprt *xprt = task->tk_xprt; |
| 310 | struct rpc_clnt *rpcb_clnt; |
| 311 | static struct rpcbind_args *map; |
| 312 | struct rpc_task *child; |
| 313 | struct sockaddr addr; |
| 314 | int status; |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 315 | struct rpcb_info *info; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 316 | |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 317 | dprintk("RPC: %5u %s(%s, %u, %u, %d)\n", |
| 318 | task->tk_pid, __FUNCTION__, |
| 319 | clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 320 | |
| 321 | /* Autobind on cloned rpc clients is discouraged */ |
| 322 | BUG_ON(clnt->cl_parent != clnt); |
| 323 | |
| 324 | if (xprt_test_and_set_binding(xprt)) { |
Chuck Lever | 2429cbf | 2007-09-11 18:00:41 -0400 | [diff] [blame] | 325 | status = -EAGAIN; /* tell caller to check again */ |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 326 | dprintk("RPC: %5u %s: waiting for another binder\n", |
| 327 | task->tk_pid, __FUNCTION__); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 328 | goto bailout_nowake; |
| 329 | } |
| 330 | |
| 331 | /* Put self on queue before sending rpcbind request, in case |
| 332 | * rpcb_getport_done completes before we return from rpc_run_task */ |
| 333 | rpc_sleep_on(&xprt->binding, task, NULL, NULL); |
| 334 | |
| 335 | /* Someone else may have bound if we slept */ |
| 336 | if (xprt_bound(xprt)) { |
| 337 | status = 0; |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 338 | dprintk("RPC: %5u %s: already bound\n", |
| 339 | task->tk_pid, __FUNCTION__); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 340 | goto bailout_nofree; |
| 341 | } |
| 342 | |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 343 | rpc_peeraddr(clnt, (void *)&addr, sizeof(addr)); |
| 344 | |
| 345 | /* Don't ever use rpcbind v2 for AF_INET6 requests */ |
| 346 | switch (addr.sa_family) { |
| 347 | case AF_INET: |
| 348 | info = rpcb_next_version; |
| 349 | break; |
| 350 | case AF_INET6: |
| 351 | info = rpcb_next_version6; |
| 352 | break; |
| 353 | default: |
| 354 | status = -EAFNOSUPPORT; |
| 355 | dprintk("RPC: %5u %s: bad address family\n", |
| 356 | task->tk_pid, __FUNCTION__); |
| 357 | goto bailout_nofree; |
| 358 | } |
| 359 | if (info[xprt->bind_index].rpc_proc == NULL) { |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 360 | xprt->bind_index = 0; |
Chuck Lever | 906462a | 2007-09-11 18:00:47 -0400 | [diff] [blame] | 361 | status = -EPFNOSUPPORT; |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 362 | dprintk("RPC: %5u %s: no more getport versions available\n", |
| 363 | task->tk_pid, __FUNCTION__); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 364 | goto bailout_nofree; |
| 365 | } |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 366 | bind_version = info[xprt->bind_index].rpc_vers; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 367 | |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 368 | dprintk("RPC: %5u %s: trying rpcbind version %u\n", |
| 369 | task->tk_pid, __FUNCTION__, bind_version); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 370 | |
Chuck Lever | 143b6c4 | 2007-08-16 16:03:31 -0400 | [diff] [blame] | 371 | rpcb_clnt = rpcb_create(clnt->cl_server, &addr, xprt->prot, |
| 372 | bind_version, 0); |
| 373 | if (IS_ERR(rpcb_clnt)) { |
| 374 | status = PTR_ERR(rpcb_clnt); |
| 375 | dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n", |
| 376 | task->tk_pid, __FUNCTION__, PTR_ERR(rpcb_clnt)); |
| 377 | goto bailout_nofree; |
| 378 | } |
| 379 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 380 | map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC); |
| 381 | if (!map) { |
| 382 | status = -ENOMEM; |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 383 | dprintk("RPC: %5u %s: no memory available\n", |
| 384 | task->tk_pid, __FUNCTION__); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 385 | goto bailout_nofree; |
| 386 | } |
| 387 | map->r_prog = clnt->cl_prog; |
| 388 | map->r_vers = clnt->cl_vers; |
| 389 | map->r_prot = xprt->prot; |
| 390 | map->r_port = 0; |
| 391 | map->r_xprt = xprt_get(xprt); |
\"Talpey, Thomas\ | 4417c8c | 2007-09-10 13:43:05 -0400 | [diff] [blame] | 392 | map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID); |
Chuck Lever | 6d0aa06 | 2007-09-11 18:00:15 -0400 | [diff] [blame] | 393 | memcpy(map->r_addr, |
Chuck Lever | 143b6c4 | 2007-08-16 16:03:31 -0400 | [diff] [blame] | 394 | rpc_peeraddr2str(rpcb_clnt, RPC_DISPLAY_UNIVERSAL_ADDR), |
| 395 | sizeof(map->r_addr)); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 396 | map->r_owner = RPCB_OWNER_STRING; /* ignored for GETADDR */ |
| 397 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 398 | child = rpc_run_task(rpcb_clnt, RPC_TASK_ASYNC, &rpcb_getport_ops, map); |
Trond Myklebust | 4c402b4 | 2007-06-14 16:40:32 -0400 | [diff] [blame] | 399 | rpc_release_client(rpcb_clnt); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 400 | if (IS_ERR(child)) { |
| 401 | status = -EIO; |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 402 | dprintk("RPC: %5u %s: rpc_run_task failed\n", |
| 403 | task->tk_pid, __FUNCTION__); |
Chuck Lever | 143b6c4 | 2007-08-16 16:03:31 -0400 | [diff] [blame] | 404 | goto bailout; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 405 | } |
| 406 | rpc_put_task(child); |
| 407 | |
| 408 | task->tk_xprt->stat.bind_count++; |
| 409 | return; |
| 410 | |
| 411 | bailout: |
| 412 | kfree(map); |
| 413 | xprt_put(xprt); |
| 414 | bailout_nofree: |
| 415 | rpcb_wake_rpcbind_waiters(xprt, status); |
| 416 | bailout_nowake: |
| 417 | task->tk_status = status; |
| 418 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 419 | EXPORT_SYMBOL_GPL(rpcb_getport_async); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 420 | |
| 421 | /* |
| 422 | * Rpcbind child task calls this callback via tk_exit. |
| 423 | */ |
| 424 | static void rpcb_getport_done(struct rpc_task *child, void *data) |
| 425 | { |
| 426 | struct rpcbind_args *map = data; |
| 427 | struct rpc_xprt *xprt = map->r_xprt; |
| 428 | int status = child->tk_status; |
| 429 | |
Chuck Lever | 4784cb5 | 2007-09-11 18:00:36 -0400 | [diff] [blame] | 430 | /* Garbage reply: retry with a lesser rpcbind version */ |
| 431 | if (status == -EIO) |
| 432 | status = -EPROTONOSUPPORT; |
| 433 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 434 | /* rpcbind server doesn't support this rpcbind protocol version */ |
| 435 | if (status == -EPROTONOSUPPORT) |
| 436 | xprt->bind_index++; |
| 437 | |
| 438 | if (status < 0) { |
| 439 | /* rpcbind server not available on remote host? */ |
| 440 | xprt->ops->set_port(xprt, 0); |
| 441 | } else if (map->r_port == 0) { |
| 442 | /* Requested RPC service wasn't registered on remote host */ |
| 443 | xprt->ops->set_port(xprt, 0); |
| 444 | status = -EACCES; |
| 445 | } else { |
| 446 | /* Succeeded */ |
| 447 | xprt->ops->set_port(xprt, map->r_port); |
| 448 | xprt_set_bound(xprt); |
| 449 | status = 0; |
| 450 | } |
| 451 | |
| 452 | dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n", |
| 453 | child->tk_pid, status, map->r_port); |
| 454 | |
| 455 | rpcb_wake_rpcbind_waiters(xprt, status); |
| 456 | } |
| 457 | |
| 458 | static int rpcb_encode_mapping(struct rpc_rqst *req, __be32 *p, |
| 459 | struct rpcbind_args *rpcb) |
| 460 | { |
| 461 | dprintk("RPC: rpcb_encode_mapping(%u, %u, %d, %u)\n", |
| 462 | rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port); |
| 463 | *p++ = htonl(rpcb->r_prog); |
| 464 | *p++ = htonl(rpcb->r_vers); |
| 465 | *p++ = htonl(rpcb->r_prot); |
| 466 | *p++ = htonl(rpcb->r_port); |
| 467 | |
| 468 | req->rq_slen = xdr_adjust_iovec(req->rq_svec, p); |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | static int rpcb_decode_getport(struct rpc_rqst *req, __be32 *p, |
| 473 | unsigned short *portp) |
| 474 | { |
| 475 | *portp = (unsigned short) ntohl(*p++); |
| 476 | dprintk("RPC: rpcb_decode_getport result %u\n", |
| 477 | *portp); |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | static int rpcb_decode_set(struct rpc_rqst *req, __be32 *p, |
| 482 | unsigned int *boolp) |
| 483 | { |
| 484 | *boolp = (unsigned int) ntohl(*p++); |
| 485 | dprintk("RPC: rpcb_decode_set result %u\n", |
| 486 | *boolp); |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static int rpcb_encode_getaddr(struct rpc_rqst *req, __be32 *p, |
| 491 | struct rpcbind_args *rpcb) |
| 492 | { |
| 493 | dprintk("RPC: rpcb_encode_getaddr(%u, %u, %s)\n", |
| 494 | rpcb->r_prog, rpcb->r_vers, rpcb->r_addr); |
| 495 | *p++ = htonl(rpcb->r_prog); |
| 496 | *p++ = htonl(rpcb->r_vers); |
| 497 | |
| 498 | p = xdr_encode_string(p, rpcb->r_netid); |
| 499 | p = xdr_encode_string(p, rpcb->r_addr); |
| 500 | p = xdr_encode_string(p, rpcb->r_owner); |
| 501 | |
| 502 | req->rq_slen = xdr_adjust_iovec(req->rq_svec, p); |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static int rpcb_decode_getaddr(struct rpc_rqst *req, __be32 *p, |
| 508 | unsigned short *portp) |
| 509 | { |
| 510 | char *addr; |
Chuck Lever | adc24df | 2007-08-06 11:56:31 -0400 | [diff] [blame] | 511 | u32 addr_len; |
| 512 | int c, i, f, first, val; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 513 | |
| 514 | *portp = 0; |
Chuck Lever | adc24df | 2007-08-06 11:56:31 -0400 | [diff] [blame] | 515 | addr_len = ntohl(*p++); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 516 | |
Chuck Lever | e65fe39 | 2007-09-11 18:00:31 -0400 | [diff] [blame] | 517 | /* |
| 518 | * Simple sanity check. The smallest possible universal |
| 519 | * address is an IPv4 address string containing 11 bytes. |
| 520 | */ |
| 521 | if (addr_len < 11 || addr_len > RPCB_MAXADDRLEN) |
| 522 | goto out_err; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 523 | |
Chuck Lever | e65fe39 | 2007-09-11 18:00:31 -0400 | [diff] [blame] | 524 | /* |
| 525 | * Start at the end and walk backwards until the first dot |
| 526 | * is encountered. When the second dot is found, we have |
| 527 | * both parts of the port number. |
| 528 | */ |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 529 | addr = (char *)p; |
| 530 | val = 0; |
| 531 | first = 1; |
| 532 | f = 1; |
| 533 | for (i = addr_len - 1; i > 0; i--) { |
| 534 | c = addr[i]; |
| 535 | if (c >= '0' && c <= '9') { |
| 536 | val += (c - '0') * f; |
| 537 | f *= 10; |
| 538 | } else if (c == '.') { |
| 539 | if (first) { |
| 540 | *portp = val; |
| 541 | val = first = 0; |
| 542 | f = 1; |
| 543 | } else { |
| 544 | *portp |= (val << 8); |
| 545 | break; |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | |
Chuck Lever | e65fe39 | 2007-09-11 18:00:31 -0400 | [diff] [blame] | 550 | /* |
| 551 | * Simple sanity check. If we never saw a dot in the reply, |
| 552 | * then this was probably just garbage. |
| 553 | */ |
| 554 | if (first) |
| 555 | goto out_err; |
| 556 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 557 | dprintk("RPC: rpcb_decode_getaddr port=%u\n", *portp); |
| 558 | return 0; |
Chuck Lever | e65fe39 | 2007-09-11 18:00:31 -0400 | [diff] [blame] | 559 | |
| 560 | out_err: |
| 561 | dprintk("RPC: rpcbind server returned malformed reply\n"); |
| 562 | return -EIO; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | #define RPCB_program_sz (1u) |
| 566 | #define RPCB_version_sz (1u) |
| 567 | #define RPCB_protocol_sz (1u) |
| 568 | #define RPCB_port_sz (1u) |
| 569 | #define RPCB_boolean_sz (1u) |
| 570 | |
\"Talpey, Thomas\ | 4f40ee4 | 2007-09-10 13:42:38 -0400 | [diff] [blame] | 571 | #define RPCB_netid_sz (1+XDR_QUADLEN(RPCBIND_MAXNETIDLEN)) |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 572 | #define RPCB_addr_sz (1+XDR_QUADLEN(RPCB_MAXADDRLEN)) |
| 573 | #define RPCB_ownerstring_sz (1+XDR_QUADLEN(RPCB_MAXOWNERLEN)) |
| 574 | |
| 575 | #define RPCB_mappingargs_sz RPCB_program_sz+RPCB_version_sz+ \ |
| 576 | RPCB_protocol_sz+RPCB_port_sz |
| 577 | #define RPCB_getaddrargs_sz RPCB_program_sz+RPCB_version_sz+ \ |
| 578 | RPCB_netid_sz+RPCB_addr_sz+ \ |
| 579 | RPCB_ownerstring_sz |
| 580 | |
| 581 | #define RPCB_setres_sz RPCB_boolean_sz |
| 582 | #define RPCB_getportres_sz RPCB_port_sz |
| 583 | |
| 584 | /* |
| 585 | * Note that RFC 1833 does not put any size restrictions on the |
| 586 | * address string returned by the remote rpcbind database. |
| 587 | */ |
| 588 | #define RPCB_getaddrres_sz RPCB_addr_sz |
| 589 | |
| 590 | #define PROC(proc, argtype, restype) \ |
| 591 | [RPCBPROC_##proc] = { \ |
| 592 | .p_proc = RPCBPROC_##proc, \ |
| 593 | .p_encode = (kxdrproc_t) rpcb_encode_##argtype, \ |
| 594 | .p_decode = (kxdrproc_t) rpcb_decode_##restype, \ |
| 595 | .p_arglen = RPCB_##argtype##args_sz, \ |
| 596 | .p_replen = RPCB_##restype##res_sz, \ |
| 597 | .p_statidx = RPCBPROC_##proc, \ |
| 598 | .p_timer = 0, \ |
| 599 | .p_name = #proc, \ |
| 600 | } |
| 601 | |
| 602 | /* |
| 603 | * Not all rpcbind procedures described in RFC 1833 are implemented |
| 604 | * since the Linux kernel RPC code requires only these. |
| 605 | */ |
| 606 | static struct rpc_procinfo rpcb_procedures2[] = { |
| 607 | PROC(SET, mapping, set), |
| 608 | PROC(UNSET, mapping, set), |
| 609 | PROC(GETADDR, mapping, getport), |
| 610 | }; |
| 611 | |
| 612 | static struct rpc_procinfo rpcb_procedures3[] = { |
| 613 | PROC(SET, mapping, set), |
| 614 | PROC(UNSET, mapping, set), |
| 615 | PROC(GETADDR, getaddr, getaddr), |
| 616 | }; |
| 617 | |
| 618 | static struct rpc_procinfo rpcb_procedures4[] = { |
| 619 | PROC(SET, mapping, set), |
| 620 | PROC(UNSET, mapping, set), |
| 621 | PROC(GETVERSADDR, getaddr, getaddr), |
| 622 | }; |
| 623 | |
| 624 | static struct rpcb_info rpcb_next_version[] = { |
| 625 | #ifdef CONFIG_SUNRPC_BIND34 |
| 626 | { 4, &rpcb_procedures4[RPCBPROC_GETVERSADDR] }, |
| 627 | { 3, &rpcb_procedures3[RPCBPROC_GETADDR] }, |
| 628 | #endif |
| 629 | { 2, &rpcb_procedures2[RPCBPROC_GETPORT] }, |
| 630 | { 0, NULL }, |
| 631 | }; |
| 632 | |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 633 | static struct rpcb_info rpcb_next_version6[] = { |
| 634 | #ifdef CONFIG_SUNRPC_BIND34 |
| 635 | { 4, &rpcb_procedures4[RPCBPROC_GETVERSADDR] }, |
| 636 | { 3, &rpcb_procedures3[RPCBPROC_GETADDR] }, |
| 637 | #endif |
| 638 | { 0, NULL }, |
| 639 | }; |
| 640 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 641 | static struct rpc_version rpcb_version2 = { |
| 642 | .number = 2, |
| 643 | .nrprocs = RPCB_HIGHPROC_2, |
| 644 | .procs = rpcb_procedures2 |
| 645 | }; |
| 646 | |
| 647 | static struct rpc_version rpcb_version3 = { |
| 648 | .number = 3, |
| 649 | .nrprocs = RPCB_HIGHPROC_3, |
| 650 | .procs = rpcb_procedures3 |
| 651 | }; |
| 652 | |
| 653 | static struct rpc_version rpcb_version4 = { |
| 654 | .number = 4, |
| 655 | .nrprocs = RPCB_HIGHPROC_4, |
| 656 | .procs = rpcb_procedures4 |
| 657 | }; |
| 658 | |
| 659 | static struct rpc_version *rpcb_version[] = { |
| 660 | NULL, |
| 661 | NULL, |
| 662 | &rpcb_version2, |
| 663 | &rpcb_version3, |
| 664 | &rpcb_version4 |
| 665 | }; |
| 666 | |
| 667 | static struct rpc_stat rpcb_stats; |
| 668 | |
Adrian Bunk | 7f4adef | 2007-06-13 01:03:13 +0200 | [diff] [blame] | 669 | static struct rpc_program rpcb_program = { |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 670 | .name = "rpcbind", |
| 671 | .number = RPCBIND_PROGRAM, |
| 672 | .nrvers = ARRAY_SIZE(rpcb_version), |
| 673 | .version = rpcb_version, |
| 674 | .stats = &rpcb_stats, |
| 675 | }; |