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[]; |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame^] | 90 | static struct rpc_procinfo rpcb_procedures4[]; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 91 | |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 92 | struct rpcb_info { |
Chuck Lever | fc200e7 | 2008-06-25 17:24:31 -0400 | [diff] [blame] | 93 | u32 rpc_vers; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 94 | struct rpc_procinfo * rpc_proc; |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | static struct rpcb_info rpcb_next_version[]; |
| 98 | static struct rpcb_info rpcb_next_version6[]; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 99 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 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 | |
Trond Myklebust | 381ba74 | 2008-07-07 12:18:53 -0400 | [diff] [blame] | 111 | static void rpcb_map_release(void *data) |
| 112 | { |
| 113 | struct rpcbind_args *map = data; |
| 114 | |
| 115 | rpcb_wake_rpcbind_waiters(map->r_xprt, map->r_status); |
| 116 | xprt_put(map->r_xprt); |
| 117 | kfree(map); |
| 118 | } |
| 119 | |
Chuck Lever | cc5598b | 2008-07-14 16:03:27 -0400 | [diff] [blame] | 120 | static const struct sockaddr_in rpcb_inaddr_loopback = { |
| 121 | .sin_family = AF_INET, |
| 122 | .sin_addr.s_addr = htonl(INADDR_LOOPBACK), |
| 123 | .sin_port = htons(RPCBIND_PORT), |
| 124 | }; |
| 125 | |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame^] | 126 | static const struct sockaddr_in6 rpcb_in6addr_loopback = { |
| 127 | .sin6_family = AF_INET6, |
| 128 | .sin6_addr = IN6ADDR_LOOPBACK_INIT, |
| 129 | .sin6_port = htons(RPCBIND_PORT), |
| 130 | }; |
| 131 | |
Chuck Lever | cc5598b | 2008-07-14 16:03:27 -0400 | [diff] [blame] | 132 | static struct rpc_clnt *rpcb_create_local(struct sockaddr *addr, |
| 133 | size_t addrlen, u32 version) |
| 134 | { |
| 135 | struct rpc_create_args args = { |
| 136 | .protocol = XPRT_TRANSPORT_UDP, |
| 137 | .address = addr, |
| 138 | .addrsize = addrlen, |
| 139 | .servername = "localhost", |
| 140 | .program = &rpcb_program, |
| 141 | .version = version, |
| 142 | .authflavor = RPC_AUTH_UNIX, |
| 143 | .flags = RPC_CLNT_CREATE_NOPING, |
| 144 | }; |
| 145 | |
| 146 | return rpc_create(&args); |
| 147 | } |
| 148 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 149 | static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr, |
Chuck Lever | 423d8b0 | 2008-07-14 16:03:28 -0400 | [diff] [blame] | 150 | size_t salen, int proto, u32 version) |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 151 | { |
| 152 | struct rpc_create_args args = { |
| 153 | .protocol = proto, |
| 154 | .address = srvaddr, |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame] | 155 | .addrsize = salen, |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 156 | .servername = hostname, |
| 157 | .program = &rpcb_program, |
| 158 | .version = version, |
| 159 | .authflavor = RPC_AUTH_UNIX, |
Chuck Lever | 423d8b0 | 2008-07-14 16:03:28 -0400 | [diff] [blame] | 160 | .flags = (RPC_CLNT_CREATE_NOPING | |
| 161 | RPC_CLNT_CREATE_NONPRIVPORT), |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 162 | }; |
| 163 | |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 164 | switch (srvaddr->sa_family) { |
| 165 | case AF_INET: |
| 166 | ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT); |
| 167 | break; |
| 168 | case AF_INET6: |
| 169 | ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT); |
| 170 | break; |
| 171 | default: |
| 172 | return NULL; |
| 173 | } |
| 174 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 175 | return rpc_create(&args); |
| 176 | } |
| 177 | |
Chuck Lever | babe80e | 2008-07-14 16:03:29 -0400 | [diff] [blame] | 178 | static int rpcb_register_call(struct sockaddr *addr, size_t addrlen, |
| 179 | u32 version, struct rpc_message *msg, |
| 180 | int *result) |
| 181 | { |
| 182 | struct rpc_clnt *rpcb_clnt; |
| 183 | int error = 0; |
| 184 | |
| 185 | *result = 0; |
| 186 | |
| 187 | rpcb_clnt = rpcb_create_local(addr, addrlen, version); |
| 188 | if (!IS_ERR(rpcb_clnt)) { |
| 189 | error = rpc_call_sync(rpcb_clnt, msg, 0); |
| 190 | rpc_shutdown_client(rpcb_clnt); |
| 191 | } else |
| 192 | error = PTR_ERR(rpcb_clnt); |
| 193 | |
| 194 | if (error < 0) |
| 195 | printk(KERN_WARNING "RPC: failed to contact local rpcbind " |
| 196 | "server (errno %d).\n", -error); |
| 197 | dprintk("RPC: registration status %d/%d\n", error, *result); |
| 198 | |
| 199 | return error; |
| 200 | } |
| 201 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 202 | /** |
| 203 | * rpcb_register - set or unset a port registration with the local rpcbind svc |
| 204 | * @prog: RPC program number to bind |
| 205 | * @vers: RPC version number to bind |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame^] | 206 | * @prot: transport protocol to register |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 207 | * @port: port value to register |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame^] | 208 | * @okay: OUT: result code |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 209 | * |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame^] | 210 | * RPC services invoke this function to advertise their contact |
| 211 | * information via the system's rpcbind daemon. RPC services |
| 212 | * invoke this function once for each [program, version, transport] |
| 213 | * tuple they wish to advertise. |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 214 | * |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame^] | 215 | * Callers may also unregister RPC services that are no longer |
| 216 | * available by setting the passed-in port to zero. This removes |
| 217 | * all registered transports for [program, version] from the local |
| 218 | * rpcbind database. |
| 219 | * |
| 220 | * Returns zero if the registration request was dispatched |
| 221 | * successfully and a reply was received. The rpcbind daemon's |
| 222 | * boolean result code is stored in *okay. |
| 223 | * |
| 224 | * Returns an errno value and sets *result to zero if there was |
| 225 | * some problem that prevented the rpcbind request from being |
| 226 | * dispatched, or if the rpcbind daemon did not respond within |
| 227 | * the timeout. |
| 228 | * |
| 229 | * This function uses rpcbind protocol version 2 to contact the |
| 230 | * local rpcbind daemon. |
| 231 | * |
| 232 | * Registration works over both AF_INET and AF_INET6, and services |
| 233 | * registered via this function are advertised as available for any |
| 234 | * address. If the local rpcbind daemon is listening on AF_INET6, |
| 235 | * services registered via this function will be advertised on |
| 236 | * IN6ADDR_ANY (ie available for all AF_INET and AF_INET6 |
| 237 | * addresses). |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 238 | */ |
| 239 | int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay) |
| 240 | { |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 241 | struct rpcbind_args map = { |
| 242 | .r_prog = prog, |
| 243 | .r_vers = vers, |
| 244 | .r_prot = prot, |
| 245 | .r_port = port, |
| 246 | }; |
| 247 | struct rpc_message msg = { |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 248 | .rpc_argp = &map, |
| 249 | .rpc_resp = okay, |
| 250 | }; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 251 | |
| 252 | dprintk("RPC: %sregistering (%u, %u, %d, %u) with local " |
| 253 | "rpcbind\n", (port ? "" : "un"), |
| 254 | prog, vers, prot, port); |
| 255 | |
Chuck Lever | babe80e | 2008-07-14 16:03:29 -0400 | [diff] [blame] | 256 | msg.rpc_proc = &rpcb_procedures2[RPCBPROC_UNSET]; |
| 257 | if (port) |
| 258 | msg.rpc_proc = &rpcb_procedures2[RPCBPROC_SET]; |
| 259 | |
| 260 | return rpcb_register_call((struct sockaddr *)&rpcb_inaddr_loopback, |
Chuck Lever | cc5598b | 2008-07-14 16:03:27 -0400 | [diff] [blame] | 261 | sizeof(rpcb_inaddr_loopback), |
Chuck Lever | babe80e | 2008-07-14 16:03:29 -0400 | [diff] [blame] | 262 | RPCBVERS_2, &msg, okay); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 263 | } |
| 264 | |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame^] | 265 | /* |
| 266 | * Fill in AF_INET family-specific arguments to register |
| 267 | */ |
| 268 | static int rpcb_register_netid4(struct sockaddr_in *address_to_register, |
| 269 | struct rpc_message *msg) |
| 270 | { |
| 271 | struct rpcbind_args *map = msg->rpc_argp; |
| 272 | unsigned short port = ntohs(address_to_register->sin_port); |
| 273 | char buf[32]; |
| 274 | |
| 275 | /* Construct AF_INET universal address */ |
| 276 | snprintf(buf, sizeof(buf), |
| 277 | NIPQUAD_FMT".%u.%u", |
| 278 | NIPQUAD(address_to_register->sin_addr.s_addr), |
| 279 | port >> 8, port & 0xff); |
| 280 | map->r_addr = buf; |
| 281 | |
| 282 | dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with " |
| 283 | "local rpcbind\n", (port ? "" : "un"), |
| 284 | map->r_prog, map->r_vers, |
| 285 | map->r_addr, map->r_netid); |
| 286 | |
| 287 | msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET]; |
| 288 | if (port) |
| 289 | msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET]; |
| 290 | |
| 291 | return rpcb_register_call((struct sockaddr *)&rpcb_inaddr_loopback, |
| 292 | sizeof(rpcb_inaddr_loopback), |
| 293 | RPCBVERS_4, msg, msg->rpc_resp); |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * Fill in AF_INET6 family-specific arguments to register |
| 298 | */ |
| 299 | static int rpcb_register_netid6(struct sockaddr_in6 *address_to_register, |
| 300 | struct rpc_message *msg) |
| 301 | { |
| 302 | struct rpcbind_args *map = msg->rpc_argp; |
| 303 | unsigned short port = ntohs(address_to_register->sin6_port); |
| 304 | char buf[64]; |
| 305 | |
| 306 | /* Construct AF_INET6 universal address */ |
| 307 | snprintf(buf, sizeof(buf), |
| 308 | NIP6_FMT".%u.%u", |
| 309 | NIP6(address_to_register->sin6_addr), |
| 310 | port >> 8, port & 0xff); |
| 311 | map->r_addr = buf; |
| 312 | |
| 313 | dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with " |
| 314 | "local rpcbind\n", (port ? "" : "un"), |
| 315 | map->r_prog, map->r_vers, |
| 316 | map->r_addr, map->r_netid); |
| 317 | |
| 318 | msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET]; |
| 319 | if (port) |
| 320 | msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET]; |
| 321 | |
| 322 | return rpcb_register_call((struct sockaddr *)&rpcb_in6addr_loopback, |
| 323 | sizeof(rpcb_in6addr_loopback), |
| 324 | RPCBVERS_4, msg, msg->rpc_resp); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * rpcb_v4_register - set or unset a port registration with the local rpcbind |
| 329 | * @program: RPC program number of service to (un)register |
| 330 | * @version: RPC version number of service to (un)register |
| 331 | * @address: address family, IP address, and port to (un)register |
| 332 | * @netid: netid of transport protocol to (un)register |
| 333 | * @result: result code from rpcbind RPC call |
| 334 | * |
| 335 | * RPC services invoke this function to advertise their contact |
| 336 | * information via the system's rpcbind daemon. RPC services |
| 337 | * invoke this function once for each [program, version, address, |
| 338 | * netid] tuple they wish to advertise. |
| 339 | * |
| 340 | * Callers may also unregister RPC services that are no longer |
| 341 | * available by setting the port number in the passed-in address |
| 342 | * to zero. Callers pass a netid of "" to unregister all |
| 343 | * transport netids associated with [program, version, address]. |
| 344 | * |
| 345 | * Returns zero if the registration request was dispatched |
| 346 | * successfully and a reply was received. The rpcbind daemon's |
| 347 | * result code is stored in *result. |
| 348 | * |
| 349 | * Returns an errno value and sets *result to zero if there was |
| 350 | * some problem that prevented the rpcbind request from being |
| 351 | * dispatched, or if the rpcbind daemon did not respond within |
| 352 | * the timeout. |
| 353 | * |
| 354 | * This function uses rpcbind protocol version 4 to contact the |
| 355 | * local rpcbind daemon. The local rpcbind daemon must support |
| 356 | * version 4 of the rpcbind protocol in order for these functions |
| 357 | * to register a service successfully. |
| 358 | * |
| 359 | * Supported netids include "udp" and "tcp" for UDP and TCP over |
| 360 | * IPv4, and "udp6" and "tcp6" for UDP and TCP over IPv6, |
| 361 | * respectively. |
| 362 | * |
| 363 | * The contents of @address determine the address family and the |
| 364 | * port to be registered. The usual practice is to pass INADDR_ANY |
| 365 | * as the raw address, but specifying a non-zero address is also |
| 366 | * supported by this API if the caller wishes to advertise an RPC |
| 367 | * service on a specific network interface. |
| 368 | * |
| 369 | * Note that passing in INADDR_ANY does not create the same service |
| 370 | * registration as IN6ADDR_ANY. The former advertises an RPC |
| 371 | * service on any IPv4 address, but not on IPv6. The latter |
| 372 | * advertises the service on all IPv4 and IPv6 addresses. |
| 373 | */ |
| 374 | int rpcb_v4_register(const u32 program, const u32 version, |
| 375 | const struct sockaddr *address, const char *netid, |
| 376 | int *result) |
| 377 | { |
| 378 | struct rpcbind_args map = { |
| 379 | .r_prog = program, |
| 380 | .r_vers = version, |
| 381 | .r_netid = netid, |
| 382 | .r_owner = RPCB_OWNER_STRING, |
| 383 | }; |
| 384 | struct rpc_message msg = { |
| 385 | .rpc_argp = &map, |
| 386 | .rpc_resp = result, |
| 387 | }; |
| 388 | |
| 389 | *result = 0; |
| 390 | |
| 391 | switch (address->sa_family) { |
| 392 | case AF_INET: |
| 393 | return rpcb_register_netid4((struct sockaddr_in *)address, |
| 394 | &msg); |
| 395 | case AF_INET6: |
| 396 | return rpcb_register_netid6((struct sockaddr_in6 *)address, |
| 397 | &msg); |
| 398 | } |
| 399 | |
| 400 | return -EAFNOSUPPORT; |
| 401 | } |
| 402 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 403 | /** |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 404 | * 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] | 405 | * @sin: address of remote peer |
| 406 | * @prog: RPC program number to bind |
| 407 | * @vers: RPC version number to bind |
| 408 | * @prot: transport protocol to use to make this request |
| 409 | * |
Chuck Lever | 67d6021 | 2008-01-14 15:12:01 -0500 | [diff] [blame] | 410 | * Return value is the requested advertised port number, |
| 411 | * or a negative errno value. |
| 412 | * |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 413 | * Called from outside the RPC client in a synchronous task context. |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 414 | * Uses default timeout parameters specified by underlying transport. |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 415 | * |
Chuck Lever | 67d6021 | 2008-01-14 15:12:01 -0500 | [diff] [blame] | 416 | * XXX: Needs to support IPv6 |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 417 | */ |
Chuck Lever | f1ec08c | 2008-01-14 15:11:53 -0500 | [diff] [blame] | 418 | 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] | 419 | { |
| 420 | struct rpcbind_args map = { |
| 421 | .r_prog = prog, |
| 422 | .r_vers = vers, |
| 423 | .r_prot = prot, |
| 424 | .r_port = 0, |
| 425 | }; |
| 426 | struct rpc_message msg = { |
| 427 | .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT], |
| 428 | .rpc_argp = &map, |
| 429 | .rpc_resp = &map.r_port, |
| 430 | }; |
| 431 | struct rpc_clnt *rpcb_clnt; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 432 | int status; |
| 433 | |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 434 | dprintk("RPC: %s(" NIPQUAD_FMT ", %u, %u, %d)\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 435 | __func__, NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 436 | |
Chuck Lever | b91e101 | 2008-01-14 15:11:46 -0500 | [diff] [blame] | 437 | rpcb_clnt = rpcb_create(NULL, (struct sockaddr *)sin, |
Chuck Lever | 423d8b0 | 2008-07-14 16:03:28 -0400 | [diff] [blame] | 438 | sizeof(*sin), prot, RPCBVERS_2); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 439 | if (IS_ERR(rpcb_clnt)) |
| 440 | return PTR_ERR(rpcb_clnt); |
| 441 | |
| 442 | status = rpc_call_sync(rpcb_clnt, &msg, 0); |
Trond Myklebust | 90c5755 | 2007-06-09 19:49:36 -0400 | [diff] [blame] | 443 | rpc_shutdown_client(rpcb_clnt); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 444 | |
| 445 | if (status >= 0) { |
| 446 | if (map.r_port != 0) |
| 447 | return map.r_port; |
| 448 | status = -EACCES; |
| 449 | } |
| 450 | return status; |
| 451 | } |
Chuck Lever | cce63cd | 2007-07-01 12:13:12 -0400 | [diff] [blame] | 452 | EXPORT_SYMBOL_GPL(rpcb_getport_sync); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 453 | |
Trond Myklebust | 803a906 | 2008-07-01 15:20:55 -0400 | [diff] [blame] | 454 | 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] | 455 | { |
| 456 | struct rpc_message msg = { |
Trond Myklebust | 803a906 | 2008-07-01 15:20:55 -0400 | [diff] [blame] | 457 | .rpc_proc = proc, |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 458 | .rpc_argp = map, |
| 459 | .rpc_resp = &map->r_port, |
| 460 | }; |
| 461 | struct rpc_task_setup task_setup_data = { |
| 462 | .rpc_client = rpcb_clnt, |
| 463 | .rpc_message = &msg, |
| 464 | .callback_ops = &rpcb_getport_ops, |
| 465 | .callback_data = map, |
| 466 | .flags = RPC_TASK_ASYNC, |
| 467 | }; |
| 468 | |
| 469 | return rpc_run_task(&task_setup_data); |
| 470 | } |
| 471 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 472 | /** |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 473 | * 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] | 474 | * @task: task that is waiting for portmapper request |
| 475 | * |
| 476 | * This one can be called for an ongoing RPC request, and can be used in |
| 477 | * an async (rpciod) context. |
| 478 | */ |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 479 | void rpcb_getport_async(struct rpc_task *task) |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 480 | { |
| 481 | struct rpc_clnt *clnt = task->tk_client; |
Trond Myklebust | 803a906 | 2008-07-01 15:20:55 -0400 | [diff] [blame] | 482 | struct rpc_procinfo *proc; |
Chuck Lever | 0a48f5d | 2007-12-10 14:56:38 -0500 | [diff] [blame] | 483 | u32 bind_version; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 484 | struct rpc_xprt *xprt = task->tk_xprt; |
| 485 | struct rpc_clnt *rpcb_clnt; |
| 486 | static struct rpcbind_args *map; |
| 487 | struct rpc_task *child; |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame] | 488 | struct sockaddr_storage addr; |
| 489 | struct sockaddr *sap = (struct sockaddr *)&addr; |
| 490 | size_t salen; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 491 | int status; |
| 492 | |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 493 | dprintk("RPC: %5u %s(%s, %u, %u, %d)\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 494 | task->tk_pid, __func__, |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 495 | clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 496 | |
| 497 | /* Autobind on cloned rpc clients is discouraged */ |
| 498 | BUG_ON(clnt->cl_parent != clnt); |
| 499 | |
Trond Myklebust | 381ba74 | 2008-07-07 12:18:53 -0400 | [diff] [blame] | 500 | /* Put self on the wait queue to ensure we get notified if |
| 501 | * some other task is already attempting to bind the port */ |
| 502 | rpc_sleep_on(&xprt->binding, task, NULL); |
| 503 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 504 | if (xprt_test_and_set_binding(xprt)) { |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 505 | dprintk("RPC: %5u %s: waiting for another binder\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 506 | task->tk_pid, __func__); |
Trond Myklebust | 381ba74 | 2008-07-07 12:18:53 -0400 | [diff] [blame] | 507 | return; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 508 | } |
| 509 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 510 | /* Someone else may have bound if we slept */ |
| 511 | if (xprt_bound(xprt)) { |
| 512 | status = 0; |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 513 | dprintk("RPC: %5u %s: already bound\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 514 | task->tk_pid, __func__); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 515 | goto bailout_nofree; |
| 516 | } |
| 517 | |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame] | 518 | salen = rpc_peeraddr(clnt, sap, sizeof(addr)); |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 519 | |
| 520 | /* Don't ever use rpcbind v2 for AF_INET6 requests */ |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame] | 521 | switch (sap->sa_family) { |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 522 | case AF_INET: |
Trond Myklebust | 803a906 | 2008-07-01 15:20:55 -0400 | [diff] [blame] | 523 | proc = rpcb_next_version[xprt->bind_index].rpc_proc; |
| 524 | bind_version = rpcb_next_version[xprt->bind_index].rpc_vers; |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 525 | break; |
| 526 | case AF_INET6: |
Trond Myklebust | 803a906 | 2008-07-01 15:20:55 -0400 | [diff] [blame] | 527 | proc = rpcb_next_version6[xprt->bind_index].rpc_proc; |
| 528 | bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers; |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 529 | break; |
| 530 | default: |
| 531 | status = -EAFNOSUPPORT; |
| 532 | dprintk("RPC: %5u %s: bad address family\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 533 | task->tk_pid, __func__); |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 534 | goto bailout_nofree; |
| 535 | } |
Trond Myklebust | 803a906 | 2008-07-01 15:20:55 -0400 | [diff] [blame] | 536 | if (proc == NULL) { |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 537 | xprt->bind_index = 0; |
Chuck Lever | 906462a | 2007-09-11 18:00:47 -0400 | [diff] [blame] | 538 | status = -EPFNOSUPPORT; |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 539 | dprintk("RPC: %5u %s: no more getport versions available\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 540 | task->tk_pid, __func__); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 541 | goto bailout_nofree; |
| 542 | } |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 543 | |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 544 | dprintk("RPC: %5u %s: trying rpcbind version %u\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 545 | task->tk_pid, __func__, bind_version); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 546 | |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame] | 547 | rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot, |
Chuck Lever | 423d8b0 | 2008-07-14 16:03:28 -0400 | [diff] [blame] | 548 | bind_version); |
Chuck Lever | 143b6c4 | 2007-08-16 16:03:31 -0400 | [diff] [blame] | 549 | if (IS_ERR(rpcb_clnt)) { |
| 550 | status = PTR_ERR(rpcb_clnt); |
| 551 | dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 552 | task->tk_pid, __func__, PTR_ERR(rpcb_clnt)); |
Chuck Lever | 143b6c4 | 2007-08-16 16:03:31 -0400 | [diff] [blame] | 553 | goto bailout_nofree; |
| 554 | } |
| 555 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 556 | map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC); |
| 557 | if (!map) { |
| 558 | status = -ENOMEM; |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 559 | dprintk("RPC: %5u %s: no memory available\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 560 | task->tk_pid, __func__); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 561 | goto bailout_nofree; |
| 562 | } |
| 563 | map->r_prog = clnt->cl_prog; |
| 564 | map->r_vers = clnt->cl_vers; |
| 565 | map->r_prot = xprt->prot; |
| 566 | map->r_port = 0; |
| 567 | map->r_xprt = xprt_get(xprt); |
Trond Myklebust | 86d61d8 | 2008-01-07 21:16:56 -0500 | [diff] [blame] | 568 | map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID); |
| 569 | map->r_addr = rpc_peeraddr2str(rpcb_clnt, RPC_DISPLAY_UNIVERSAL_ADDR); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 570 | map->r_owner = RPCB_OWNER_STRING; /* ignored for GETADDR */ |
Trond Myklebust | 381ba74 | 2008-07-07 12:18:53 -0400 | [diff] [blame] | 571 | map->r_status = -EIO; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 572 | |
Trond Myklebust | 803a906 | 2008-07-01 15:20:55 -0400 | [diff] [blame] | 573 | child = rpcb_call_async(rpcb_clnt, map, proc); |
Trond Myklebust | 4c402b4 | 2007-06-14 16:40:32 -0400 | [diff] [blame] | 574 | rpc_release_client(rpcb_clnt); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 575 | if (IS_ERR(child)) { |
Trond Myklebust | 0d3a34b | 2008-07-07 12:18:52 -0400 | [diff] [blame] | 576 | /* rpcb_map_release() has freed the arguments */ |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 577 | dprintk("RPC: %5u %s: rpc_run_task failed\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 578 | task->tk_pid, __func__); |
Trond Myklebust | 381ba74 | 2008-07-07 12:18:53 -0400 | [diff] [blame] | 579 | return; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 580 | } |
| 581 | rpc_put_task(child); |
| 582 | |
| 583 | task->tk_xprt->stat.bind_count++; |
| 584 | return; |
| 585 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 586 | bailout_nofree: |
| 587 | rpcb_wake_rpcbind_waiters(xprt, status); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 588 | task->tk_status = status; |
| 589 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 590 | EXPORT_SYMBOL_GPL(rpcb_getport_async); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 591 | |
| 592 | /* |
| 593 | * Rpcbind child task calls this callback via tk_exit. |
| 594 | */ |
| 595 | static void rpcb_getport_done(struct rpc_task *child, void *data) |
| 596 | { |
| 597 | struct rpcbind_args *map = data; |
| 598 | struct rpc_xprt *xprt = map->r_xprt; |
| 599 | int status = child->tk_status; |
| 600 | |
Chuck Lever | 4784cb5 | 2007-09-11 18:00:36 -0400 | [diff] [blame] | 601 | /* Garbage reply: retry with a lesser rpcbind version */ |
| 602 | if (status == -EIO) |
| 603 | status = -EPROTONOSUPPORT; |
| 604 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 605 | /* rpcbind server doesn't support this rpcbind protocol version */ |
| 606 | if (status == -EPROTONOSUPPORT) |
| 607 | xprt->bind_index++; |
| 608 | |
| 609 | if (status < 0) { |
| 610 | /* rpcbind server not available on remote host? */ |
| 611 | xprt->ops->set_port(xprt, 0); |
| 612 | } else if (map->r_port == 0) { |
| 613 | /* Requested RPC service wasn't registered on remote host */ |
| 614 | xprt->ops->set_port(xprt, 0); |
| 615 | status = -EACCES; |
| 616 | } else { |
| 617 | /* Succeeded */ |
| 618 | xprt->ops->set_port(xprt, map->r_port); |
| 619 | xprt_set_bound(xprt); |
| 620 | status = 0; |
| 621 | } |
| 622 | |
| 623 | dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n", |
| 624 | child->tk_pid, status, map->r_port); |
| 625 | |
Trond Myklebust | 381ba74 | 2008-07-07 12:18:53 -0400 | [diff] [blame] | 626 | map->r_status = status; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 627 | } |
| 628 | |
Chuck Lever | 166b88d | 2008-07-14 16:03:26 -0400 | [diff] [blame] | 629 | /* |
| 630 | * XDR functions for rpcbind |
| 631 | */ |
| 632 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 633 | static int rpcb_encode_mapping(struct rpc_rqst *req, __be32 *p, |
| 634 | struct rpcbind_args *rpcb) |
| 635 | { |
| 636 | dprintk("RPC: rpcb_encode_mapping(%u, %u, %d, %u)\n", |
| 637 | rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port); |
| 638 | *p++ = htonl(rpcb->r_prog); |
| 639 | *p++ = htonl(rpcb->r_vers); |
| 640 | *p++ = htonl(rpcb->r_prot); |
| 641 | *p++ = htonl(rpcb->r_port); |
| 642 | |
| 643 | req->rq_slen = xdr_adjust_iovec(req->rq_svec, p); |
| 644 | return 0; |
| 645 | } |
| 646 | |
| 647 | static int rpcb_decode_getport(struct rpc_rqst *req, __be32 *p, |
| 648 | unsigned short *portp) |
| 649 | { |
| 650 | *portp = (unsigned short) ntohl(*p++); |
Chuck Lever | 877fcf1 | 2008-06-25 17:24:23 -0400 | [diff] [blame] | 651 | dprintk("RPC: rpcb_decode_getport result %u\n", |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 652 | *portp); |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | static int rpcb_decode_set(struct rpc_rqst *req, __be32 *p, |
| 657 | unsigned int *boolp) |
| 658 | { |
| 659 | *boolp = (unsigned int) ntohl(*p++); |
Chuck Lever | 877fcf1 | 2008-06-25 17:24:23 -0400 | [diff] [blame] | 660 | dprintk("RPC: rpcb_decode_set: call %s\n", |
| 661 | (*boolp ? "succeeded" : "failed")); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | static int rpcb_encode_getaddr(struct rpc_rqst *req, __be32 *p, |
| 666 | struct rpcbind_args *rpcb) |
| 667 | { |
| 668 | dprintk("RPC: rpcb_encode_getaddr(%u, %u, %s)\n", |
| 669 | rpcb->r_prog, rpcb->r_vers, rpcb->r_addr); |
| 670 | *p++ = htonl(rpcb->r_prog); |
| 671 | *p++ = htonl(rpcb->r_vers); |
| 672 | |
| 673 | p = xdr_encode_string(p, rpcb->r_netid); |
| 674 | p = xdr_encode_string(p, rpcb->r_addr); |
| 675 | p = xdr_encode_string(p, rpcb->r_owner); |
| 676 | |
| 677 | req->rq_slen = xdr_adjust_iovec(req->rq_svec, p); |
| 678 | |
| 679 | return 0; |
| 680 | } |
| 681 | |
| 682 | static int rpcb_decode_getaddr(struct rpc_rqst *req, __be32 *p, |
| 683 | unsigned short *portp) |
| 684 | { |
| 685 | char *addr; |
Chuck Lever | adc24df | 2007-08-06 11:56:31 -0400 | [diff] [blame] | 686 | u32 addr_len; |
| 687 | int c, i, f, first, val; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 688 | |
| 689 | *portp = 0; |
Chuck Lever | adc24df | 2007-08-06 11:56:31 -0400 | [diff] [blame] | 690 | addr_len = ntohl(*p++); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 691 | |
Chuck Lever | e65fe39 | 2007-09-11 18:00:31 -0400 | [diff] [blame] | 692 | /* |
| 693 | * Simple sanity check. The smallest possible universal |
| 694 | * address is an IPv4 address string containing 11 bytes. |
| 695 | */ |
Chuck Lever | 0fb2b7e | 2007-12-10 14:56:46 -0500 | [diff] [blame] | 696 | if (addr_len < 11 || addr_len > RPCBIND_MAXUADDRLEN) |
Chuck Lever | e65fe39 | 2007-09-11 18:00:31 -0400 | [diff] [blame] | 697 | goto out_err; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 698 | |
Chuck Lever | e65fe39 | 2007-09-11 18:00:31 -0400 | [diff] [blame] | 699 | /* |
| 700 | * Start at the end and walk backwards until the first dot |
| 701 | * is encountered. When the second dot is found, we have |
| 702 | * both parts of the port number. |
| 703 | */ |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 704 | addr = (char *)p; |
| 705 | val = 0; |
| 706 | first = 1; |
| 707 | f = 1; |
| 708 | for (i = addr_len - 1; i > 0; i--) { |
| 709 | c = addr[i]; |
| 710 | if (c >= '0' && c <= '9') { |
| 711 | val += (c - '0') * f; |
| 712 | f *= 10; |
| 713 | } else if (c == '.') { |
| 714 | if (first) { |
| 715 | *portp = val; |
| 716 | val = first = 0; |
| 717 | f = 1; |
| 718 | } else { |
| 719 | *portp |= (val << 8); |
| 720 | break; |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | |
Chuck Lever | e65fe39 | 2007-09-11 18:00:31 -0400 | [diff] [blame] | 725 | /* |
| 726 | * Simple sanity check. If we never saw a dot in the reply, |
| 727 | * then this was probably just garbage. |
| 728 | */ |
| 729 | if (first) |
| 730 | goto out_err; |
| 731 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 732 | dprintk("RPC: rpcb_decode_getaddr port=%u\n", *portp); |
| 733 | return 0; |
Chuck Lever | e65fe39 | 2007-09-11 18:00:31 -0400 | [diff] [blame] | 734 | |
| 735 | out_err: |
| 736 | dprintk("RPC: rpcbind server returned malformed reply\n"); |
| 737 | return -EIO; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 738 | } |
| 739 | |
| 740 | #define RPCB_program_sz (1u) |
| 741 | #define RPCB_version_sz (1u) |
| 742 | #define RPCB_protocol_sz (1u) |
| 743 | #define RPCB_port_sz (1u) |
| 744 | #define RPCB_boolean_sz (1u) |
| 745 | |
\"Talpey, Thomas\ | 4f40ee4 | 2007-09-10 13:42:38 -0400 | [diff] [blame] | 746 | #define RPCB_netid_sz (1+XDR_QUADLEN(RPCBIND_MAXNETIDLEN)) |
Chuck Lever | 0fb2b7e | 2007-12-10 14:56:46 -0500 | [diff] [blame] | 747 | #define RPCB_addr_sz (1+XDR_QUADLEN(RPCBIND_MAXUADDRLEN)) |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 748 | #define RPCB_ownerstring_sz (1+XDR_QUADLEN(RPCB_MAXOWNERLEN)) |
| 749 | |
| 750 | #define RPCB_mappingargs_sz RPCB_program_sz+RPCB_version_sz+ \ |
| 751 | RPCB_protocol_sz+RPCB_port_sz |
| 752 | #define RPCB_getaddrargs_sz RPCB_program_sz+RPCB_version_sz+ \ |
| 753 | RPCB_netid_sz+RPCB_addr_sz+ \ |
| 754 | RPCB_ownerstring_sz |
| 755 | |
| 756 | #define RPCB_setres_sz RPCB_boolean_sz |
| 757 | #define RPCB_getportres_sz RPCB_port_sz |
| 758 | |
| 759 | /* |
| 760 | * Note that RFC 1833 does not put any size restrictions on the |
| 761 | * address string returned by the remote rpcbind database. |
| 762 | */ |
| 763 | #define RPCB_getaddrres_sz RPCB_addr_sz |
| 764 | |
| 765 | #define PROC(proc, argtype, restype) \ |
| 766 | [RPCBPROC_##proc] = { \ |
| 767 | .p_proc = RPCBPROC_##proc, \ |
| 768 | .p_encode = (kxdrproc_t) rpcb_encode_##argtype, \ |
| 769 | .p_decode = (kxdrproc_t) rpcb_decode_##restype, \ |
| 770 | .p_arglen = RPCB_##argtype##args_sz, \ |
| 771 | .p_replen = RPCB_##restype##res_sz, \ |
| 772 | .p_statidx = RPCBPROC_##proc, \ |
| 773 | .p_timer = 0, \ |
| 774 | .p_name = #proc, \ |
| 775 | } |
| 776 | |
| 777 | /* |
| 778 | * Not all rpcbind procedures described in RFC 1833 are implemented |
| 779 | * since the Linux kernel RPC code requires only these. |
| 780 | */ |
| 781 | static struct rpc_procinfo rpcb_procedures2[] = { |
| 782 | PROC(SET, mapping, set), |
| 783 | PROC(UNSET, mapping, set), |
Chuck Lever | 6a77405 | 2008-06-25 17:24:39 -0400 | [diff] [blame] | 784 | PROC(GETPORT, mapping, getport), |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 785 | }; |
| 786 | |
| 787 | static struct rpc_procinfo rpcb_procedures3[] = { |
Chuck Lever | 166b88d | 2008-07-14 16:03:26 -0400 | [diff] [blame] | 788 | PROC(SET, getaddr, set), |
| 789 | PROC(UNSET, getaddr, set), |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 790 | PROC(GETADDR, getaddr, getaddr), |
| 791 | }; |
| 792 | |
| 793 | static struct rpc_procinfo rpcb_procedures4[] = { |
Chuck Lever | 166b88d | 2008-07-14 16:03:26 -0400 | [diff] [blame] | 794 | PROC(SET, getaddr, set), |
| 795 | PROC(UNSET, getaddr, set), |
Chuck Lever | 8842413 | 2008-06-25 17:24:47 -0400 | [diff] [blame] | 796 | PROC(GETADDR, getaddr, getaddr), |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 797 | PROC(GETVERSADDR, getaddr, getaddr), |
| 798 | }; |
| 799 | |
| 800 | static struct rpcb_info rpcb_next_version[] = { |
Chuck Lever | fc200e7 | 2008-06-25 17:24:31 -0400 | [diff] [blame] | 801 | { |
| 802 | .rpc_vers = RPCBVERS_2, |
| 803 | .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT], |
| 804 | }, |
| 805 | { |
| 806 | .rpc_proc = NULL, |
| 807 | }, |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 808 | }; |
| 809 | |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 810 | static struct rpcb_info rpcb_next_version6[] = { |
Chuck Lever | fc200e7 | 2008-06-25 17:24:31 -0400 | [diff] [blame] | 811 | { |
| 812 | .rpc_vers = RPCBVERS_4, |
Chuck Lever | 8842413 | 2008-06-25 17:24:47 -0400 | [diff] [blame] | 813 | .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR], |
Chuck Lever | fc200e7 | 2008-06-25 17:24:31 -0400 | [diff] [blame] | 814 | }, |
| 815 | { |
| 816 | .rpc_vers = RPCBVERS_3, |
| 817 | .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR], |
| 818 | }, |
Chuck Lever | fc200e7 | 2008-06-25 17:24:31 -0400 | [diff] [blame] | 819 | { |
| 820 | .rpc_proc = NULL, |
| 821 | }, |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 822 | }; |
| 823 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 824 | static struct rpc_version rpcb_version2 = { |
Chuck Lever | fc200e7 | 2008-06-25 17:24:31 -0400 | [diff] [blame] | 825 | .number = RPCBVERS_2, |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 826 | .nrprocs = RPCB_HIGHPROC_2, |
| 827 | .procs = rpcb_procedures2 |
| 828 | }; |
| 829 | |
| 830 | static struct rpc_version rpcb_version3 = { |
Chuck Lever | fc200e7 | 2008-06-25 17:24:31 -0400 | [diff] [blame] | 831 | .number = RPCBVERS_3, |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 832 | .nrprocs = RPCB_HIGHPROC_3, |
| 833 | .procs = rpcb_procedures3 |
| 834 | }; |
| 835 | |
| 836 | static struct rpc_version rpcb_version4 = { |
Chuck Lever | fc200e7 | 2008-06-25 17:24:31 -0400 | [diff] [blame] | 837 | .number = RPCBVERS_4, |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 838 | .nrprocs = RPCB_HIGHPROC_4, |
| 839 | .procs = rpcb_procedures4 |
| 840 | }; |
| 841 | |
| 842 | static struct rpc_version *rpcb_version[] = { |
| 843 | NULL, |
| 844 | NULL, |
| 845 | &rpcb_version2, |
| 846 | &rpcb_version3, |
| 847 | &rpcb_version4 |
| 848 | }; |
| 849 | |
| 850 | static struct rpc_stat rpcb_stats; |
| 851 | |
Adrian Bunk | 7f4adef | 2007-06-13 01:03:13 +0200 | [diff] [blame] | 852 | static struct rpc_program rpcb_program = { |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 853 | .name = "rpcbind", |
| 854 | .number = RPCBIND_PROGRAM, |
| 855 | .nrvers = ARRAY_SIZE(rpcb_version), |
| 856 | .version = rpcb_version, |
| 857 | .stats = &rpcb_stats, |
| 858 | }; |