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