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