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