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