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