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 | 7402ab19 | 2011-05-09 15:22:55 -0400 | [diff] [blame] | 19 | #include <linux/un.h> |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 20 | #include <linux/in.h> |
| 21 | #include <linux/in6.h> |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 22 | #include <linux/kernel.h> |
| 23 | #include <linux/errno.h> |
Chuck Lever | c526611 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 24 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 26 | #include <linux/nsproxy.h> |
Chuck Lever | 9d548b9 | 2008-09-15 16:27:30 -0500 | [diff] [blame] | 27 | #include <net/ipv6.h> |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 28 | |
| 29 | #include <linux/sunrpc/clnt.h> |
| 30 | #include <linux/sunrpc/sched.h> |
\"Talpey, Thomas\ | 0896a72 | 2007-09-10 13:48:23 -0400 | [diff] [blame] | 31 | #include <linux/sunrpc/xprtsock.h> |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 32 | |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 33 | #include "netns.h" |
| 34 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 35 | #ifdef RPC_DEBUG |
| 36 | # define RPCDBG_FACILITY RPCDBG_BIND |
| 37 | #endif |
| 38 | |
Chuck Lever | 7402ab19 | 2011-05-09 15:22:55 -0400 | [diff] [blame] | 39 | #define RPCBIND_SOCK_PATHNAME "/var/run/rpcbind.sock" |
| 40 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 41 | #define RPCBIND_PROGRAM (100000u) |
| 42 | #define RPCBIND_PORT (111u) |
| 43 | |
Chuck Lever | fc200e7 | 2008-06-25 17:24:31 -0400 | [diff] [blame] | 44 | #define RPCBVERS_2 (2u) |
| 45 | #define RPCBVERS_3 (3u) |
| 46 | #define RPCBVERS_4 (4u) |
| 47 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 48 | enum { |
| 49 | RPCBPROC_NULL, |
| 50 | RPCBPROC_SET, |
| 51 | RPCBPROC_UNSET, |
| 52 | RPCBPROC_GETPORT, |
| 53 | RPCBPROC_GETADDR = 3, /* alias for GETPORT */ |
| 54 | RPCBPROC_DUMP, |
| 55 | RPCBPROC_CALLIT, |
| 56 | RPCBPROC_BCAST = 5, /* alias for CALLIT */ |
| 57 | RPCBPROC_GETTIME, |
| 58 | RPCBPROC_UADDR2TADDR, |
| 59 | RPCBPROC_TADDR2UADDR, |
| 60 | RPCBPROC_GETVERSADDR, |
| 61 | RPCBPROC_INDIRECT, |
| 62 | RPCBPROC_GETADDRLIST, |
| 63 | RPCBPROC_GETSTAT, |
| 64 | }; |
| 65 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 66 | /* |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 67 | * r_owner |
| 68 | * |
| 69 | * The "owner" is allowed to unset a service in the rpcbind database. |
Chuck Lever | 126e4bc | 2009-03-18 20:47:14 -0400 | [diff] [blame] | 70 | * |
| 71 | * For AF_LOCAL SET/UNSET requests, rpcbind treats this string as a |
| 72 | * UID which it maps to a local user name via a password lookup. |
| 73 | * In all other cases it is ignored. |
| 74 | * |
| 75 | * For SET/UNSET requests, user space provides a value, even for |
| 76 | * network requests, and GETADDR uses an empty string. We follow |
| 77 | * those precedents here. |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 78 | */ |
Chuck Lever | 126e4bc | 2009-03-18 20:47:14 -0400 | [diff] [blame] | 79 | #define RPCB_OWNER_STRING "0" |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 80 | #define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING) |
| 81 | |
Chuck Lever | 0b10bf5 | 2009-08-09 15:09:33 -0400 | [diff] [blame] | 82 | /* |
| 83 | * XDR data type sizes |
| 84 | */ |
| 85 | #define RPCB_program_sz (1) |
| 86 | #define RPCB_version_sz (1) |
| 87 | #define RPCB_protocol_sz (1) |
| 88 | #define RPCB_port_sz (1) |
| 89 | #define RPCB_boolean_sz (1) |
| 90 | |
| 91 | #define RPCB_netid_sz (1 + XDR_QUADLEN(RPCBIND_MAXNETIDLEN)) |
| 92 | #define RPCB_addr_sz (1 + XDR_QUADLEN(RPCBIND_MAXUADDRLEN)) |
| 93 | #define RPCB_ownerstring_sz (1 + XDR_QUADLEN(RPCB_MAXOWNERLEN)) |
| 94 | |
| 95 | /* |
| 96 | * XDR argument and result sizes |
| 97 | */ |
| 98 | #define RPCB_mappingargs_sz (RPCB_program_sz + RPCB_version_sz + \ |
| 99 | RPCB_protocol_sz + RPCB_port_sz) |
| 100 | #define RPCB_getaddrargs_sz (RPCB_program_sz + RPCB_version_sz + \ |
| 101 | RPCB_netid_sz + RPCB_addr_sz + \ |
| 102 | RPCB_ownerstring_sz) |
| 103 | |
| 104 | #define RPCB_getportres_sz RPCB_port_sz |
| 105 | #define RPCB_setres_sz RPCB_boolean_sz |
| 106 | |
| 107 | /* |
| 108 | * Note that RFC 1833 does not put any size restrictions on the |
| 109 | * address string returned by the remote rpcbind database. |
| 110 | */ |
| 111 | #define RPCB_getaddrres_sz RPCB_addr_sz |
| 112 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 113 | static void rpcb_getport_done(struct rpc_task *, void *); |
Trond Myklebust | 381ba74 | 2008-07-07 12:18:53 -0400 | [diff] [blame] | 114 | static void rpcb_map_release(void *data); |
Adrian Bunk | 7f4adef | 2007-06-13 01:03:13 +0200 | [diff] [blame] | 115 | static struct rpc_program rpcb_program; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 116 | |
| 117 | struct rpcbind_args { |
| 118 | struct rpc_xprt * r_xprt; |
| 119 | |
| 120 | u32 r_prog; |
| 121 | u32 r_vers; |
| 122 | u32 r_prot; |
| 123 | unsigned short r_port; |
Trond Myklebust | 86d61d8 | 2008-01-07 21:16:56 -0500 | [diff] [blame] | 124 | const char * r_netid; |
| 125 | const char * r_addr; |
| 126 | const char * r_owner; |
Trond Myklebust | 381ba74 | 2008-07-07 12:18:53 -0400 | [diff] [blame] | 127 | |
| 128 | int r_status; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | static struct rpc_procinfo rpcb_procedures2[]; |
| 132 | static struct rpc_procinfo rpcb_procedures3[]; |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 133 | static struct rpc_procinfo rpcb_procedures4[]; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 134 | |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 135 | struct rpcb_info { |
Chuck Lever | fc200e7 | 2008-06-25 17:24:31 -0400 | [diff] [blame] | 136 | u32 rpc_vers; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 137 | struct rpc_procinfo * rpc_proc; |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | static struct rpcb_info rpcb_next_version[]; |
| 141 | static struct rpcb_info rpcb_next_version6[]; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 142 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 143 | static const struct rpc_call_ops rpcb_getport_ops = { |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 144 | .rpc_call_done = rpcb_getport_done, |
| 145 | .rpc_release = rpcb_map_release, |
| 146 | }; |
| 147 | |
| 148 | static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status) |
| 149 | { |
| 150 | xprt_clear_binding(xprt); |
| 151 | rpc_wake_up_status(&xprt->binding, status); |
| 152 | } |
| 153 | |
Trond Myklebust | 381ba74 | 2008-07-07 12:18:53 -0400 | [diff] [blame] | 154 | static void rpcb_map_release(void *data) |
| 155 | { |
| 156 | struct rpcbind_args *map = data; |
| 157 | |
| 158 | rpcb_wake_rpcbind_waiters(map->r_xprt, map->r_status); |
| 159 | xprt_put(map->r_xprt); |
Chuck Lever | ba80913 | 2009-08-09 15:09:35 -0400 | [diff] [blame] | 160 | kfree(map->r_addr); |
Trond Myklebust | 381ba74 | 2008-07-07 12:18:53 -0400 | [diff] [blame] | 161 | kfree(map); |
| 162 | } |
| 163 | |
Stanislav Kinsbursky | 914edb1 | 2011-10-25 14:16:36 +0300 | [diff] [blame] | 164 | static int rpcb_get_local(void) |
| 165 | { |
| 166 | int cnt; |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 167 | struct sunrpc_net *sn = net_generic(&init_net, sunrpc_net_id); |
Stanislav Kinsbursky | 914edb1 | 2011-10-25 14:16:36 +0300 | [diff] [blame] | 168 | |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 169 | spin_lock(&sn->rpcb_clnt_lock); |
| 170 | if (sn->rpcb_users) |
| 171 | sn->rpcb_users++; |
| 172 | cnt = sn->rpcb_users; |
| 173 | spin_unlock(&sn->rpcb_clnt_lock); |
Stanislav Kinsbursky | 914edb1 | 2011-10-25 14:16:36 +0300 | [diff] [blame] | 174 | |
| 175 | return cnt; |
| 176 | } |
| 177 | |
| 178 | void rpcb_put_local(void) |
| 179 | { |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 180 | struct sunrpc_net *sn = net_generic(&init_net, sunrpc_net_id); |
| 181 | struct rpc_clnt *clnt = sn->rpcb_local_clnt; |
| 182 | struct rpc_clnt *clnt4 = sn->rpcb_local_clnt4; |
Stanislav Kinsbursky | 914edb1 | 2011-10-25 14:16:36 +0300 | [diff] [blame] | 183 | int shutdown; |
| 184 | |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 185 | spin_lock(&sn->rpcb_clnt_lock); |
| 186 | if (--sn->rpcb_users == 0) { |
| 187 | sn->rpcb_local_clnt = NULL; |
| 188 | sn->rpcb_local_clnt4 = NULL; |
Stanislav Kinsbursky | 914edb1 | 2011-10-25 14:16:36 +0300 | [diff] [blame] | 189 | } |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 190 | shutdown = !sn->rpcb_users; |
| 191 | spin_unlock(&sn->rpcb_clnt_lock); |
Stanislav Kinsbursky | 914edb1 | 2011-10-25 14:16:36 +0300 | [diff] [blame] | 192 | |
| 193 | if (shutdown) { |
| 194 | /* |
| 195 | * cleanup_rpcb_clnt - remove xprtsock's sysctls, unregister |
| 196 | */ |
| 197 | if (clnt4) |
| 198 | rpc_shutdown_client(clnt4); |
| 199 | if (clnt) |
| 200 | rpc_shutdown_client(clnt); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | static void rpcb_set_local(struct rpc_clnt *clnt, struct rpc_clnt *clnt4) |
| 205 | { |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 206 | struct sunrpc_net *sn = net_generic(&init_net, sunrpc_net_id); |
| 207 | |
Stanislav Kinsbursky | 914edb1 | 2011-10-25 14:16:36 +0300 | [diff] [blame] | 208 | /* Protected by rpcb_create_local_mutex */ |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 209 | sn->rpcb_local_clnt = clnt; |
| 210 | sn->rpcb_local_clnt4 = clnt4; |
Stanislav Kinsbursky | 914edb1 | 2011-10-25 14:16:36 +0300 | [diff] [blame] | 211 | smp_wmb(); |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 212 | sn->rpcb_users = 1; |
Stanislav Kinsbursky | 914edb1 | 2011-10-25 14:16:36 +0300 | [diff] [blame] | 213 | dprintk("RPC: created new rpcb local clients (rpcb_local_clnt: " |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 214 | "%p, rpcb_local_clnt4: %p)\n", sn->rpcb_local_clnt, |
| 215 | sn->rpcb_local_clnt4); |
Stanislav Kinsbursky | 914edb1 | 2011-10-25 14:16:36 +0300 | [diff] [blame] | 216 | } |
| 217 | |
Chuck Lever | 7402ab19 | 2011-05-09 15:22:55 -0400 | [diff] [blame] | 218 | /* |
| 219 | * Returns zero on success, otherwise a negative errno value |
| 220 | * is returned. |
| 221 | */ |
| 222 | static int rpcb_create_local_unix(void) |
| 223 | { |
| 224 | static const struct sockaddr_un rpcb_localaddr_rpcbind = { |
| 225 | .sun_family = AF_LOCAL, |
| 226 | .sun_path = RPCBIND_SOCK_PATHNAME, |
| 227 | }; |
| 228 | struct rpc_create_args args = { |
| 229 | .net = &init_net, |
| 230 | .protocol = XPRT_TRANSPORT_LOCAL, |
| 231 | .address = (struct sockaddr *)&rpcb_localaddr_rpcbind, |
| 232 | .addrsize = sizeof(rpcb_localaddr_rpcbind), |
| 233 | .servername = "localhost", |
| 234 | .program = &rpcb_program, |
| 235 | .version = RPCBVERS_2, |
| 236 | .authflavor = RPC_AUTH_NULL, |
| 237 | }; |
| 238 | struct rpc_clnt *clnt, *clnt4; |
| 239 | int result = 0; |
Chuck Lever | cc5598b | 2008-07-14 16:03:27 -0400 | [diff] [blame] | 240 | |
Chuck Lever | 7402ab19 | 2011-05-09 15:22:55 -0400 | [diff] [blame] | 241 | /* |
| 242 | * Because we requested an RPC PING at transport creation time, |
| 243 | * this works only if the user space portmapper is rpcbind, and |
| 244 | * it's listening on AF_LOCAL on the named socket. |
| 245 | */ |
| 246 | clnt = rpc_create(&args); |
| 247 | if (IS_ERR(clnt)) { |
| 248 | dprintk("RPC: failed to create AF_LOCAL rpcbind " |
| 249 | "client (errno %ld).\n", PTR_ERR(clnt)); |
| 250 | result = -PTR_ERR(clnt); |
| 251 | goto out; |
| 252 | } |
| 253 | |
| 254 | clnt4 = rpc_bind_new_program(clnt, &rpcb_program, RPCBVERS_4); |
| 255 | if (IS_ERR(clnt4)) { |
| 256 | dprintk("RPC: failed to bind second program to " |
| 257 | "rpcbind v4 client (errno %ld).\n", |
| 258 | PTR_ERR(clnt4)); |
| 259 | clnt4 = NULL; |
| 260 | } |
| 261 | |
Stanislav Kinsbursky | 253fb07 | 2011-10-25 14:16:48 +0300 | [diff] [blame] | 262 | rpcb_set_local(clnt, clnt4); |
Chuck Lever | 7402ab19 | 2011-05-09 15:22:55 -0400 | [diff] [blame] | 263 | |
| 264 | out: |
| 265 | return result; |
| 266 | } |
Chuck Lever | c526611 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 267 | |
| 268 | /* |
| 269 | * Returns zero on success, otherwise a negative errno value |
| 270 | * is returned. |
| 271 | */ |
Chuck Lever | 7402ab19 | 2011-05-09 15:22:55 -0400 | [diff] [blame] | 272 | static int rpcb_create_local_net(void) |
Chuck Lever | cc5598b | 2008-07-14 16:03:27 -0400 | [diff] [blame] | 273 | { |
Chuck Lever | 7402ab19 | 2011-05-09 15:22:55 -0400 | [diff] [blame] | 274 | static const struct sockaddr_in rpcb_inaddr_loopback = { |
| 275 | .sin_family = AF_INET, |
| 276 | .sin_addr.s_addr = htonl(INADDR_LOOPBACK), |
| 277 | .sin_port = htons(RPCBIND_PORT), |
| 278 | }; |
Chuck Lever | cc5598b | 2008-07-14 16:03:27 -0400 | [diff] [blame] | 279 | struct rpc_create_args args = { |
Pavel Emelyanov | c653ce3 | 2010-09-29 16:04:45 +0400 | [diff] [blame] | 280 | .net = &init_net, |
Chuck Lever | 2a76b3b | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 281 | .protocol = XPRT_TRANSPORT_TCP, |
Chuck Lever | 5a46211 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 282 | .address = (struct sockaddr *)&rpcb_inaddr_loopback, |
| 283 | .addrsize = sizeof(rpcb_inaddr_loopback), |
Chuck Lever | cc5598b | 2008-07-14 16:03:27 -0400 | [diff] [blame] | 284 | .servername = "localhost", |
| 285 | .program = &rpcb_program, |
Chuck Lever | c526611 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 286 | .version = RPCBVERS_2, |
Chuck Lever | cc5598b | 2008-07-14 16:03:27 -0400 | [diff] [blame] | 287 | .authflavor = RPC_AUTH_UNIX, |
| 288 | .flags = RPC_CLNT_CREATE_NOPING, |
| 289 | }; |
Chuck Lever | c526611 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 290 | struct rpc_clnt *clnt, *clnt4; |
| 291 | int result = 0; |
Chuck Lever | cc5598b | 2008-07-14 16:03:27 -0400 | [diff] [blame] | 292 | |
Chuck Lever | c526611 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 293 | clnt = rpc_create(&args); |
| 294 | if (IS_ERR(clnt)) { |
| 295 | dprintk("RPC: failed to create local rpcbind " |
| 296 | "client (errno %ld).\n", PTR_ERR(clnt)); |
| 297 | result = -PTR_ERR(clnt); |
| 298 | goto out; |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * This results in an RPC ping. On systems running portmapper, |
| 303 | * the v4 ping will fail. Proceed anyway, but disallow rpcb |
| 304 | * v4 upcalls. |
| 305 | */ |
| 306 | clnt4 = rpc_bind_new_program(clnt, &rpcb_program, RPCBVERS_4); |
| 307 | if (IS_ERR(clnt4)) { |
Chuck Lever | 3835935 | 2010-09-21 16:55:48 -0400 | [diff] [blame] | 308 | dprintk("RPC: failed to bind second program to " |
| 309 | "rpcbind v4 client (errno %ld).\n", |
| 310 | PTR_ERR(clnt4)); |
Chuck Lever | c526611 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 311 | clnt4 = NULL; |
| 312 | } |
| 313 | |
Stanislav Kinsbursky | 253fb07 | 2011-10-25 14:16:48 +0300 | [diff] [blame] | 314 | rpcb_set_local(clnt, clnt4); |
Chuck Lever | c526611 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 315 | |
| 316 | out: |
Chuck Lever | 7402ab19 | 2011-05-09 15:22:55 -0400 | [diff] [blame] | 317 | return result; |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Returns zero on success, otherwise a negative errno value |
| 322 | * is returned. |
| 323 | */ |
Stanislav Kinsbursky | d990856 | 2011-10-25 14:16:58 +0300 | [diff] [blame] | 324 | int rpcb_create_local(void) |
Chuck Lever | 7402ab19 | 2011-05-09 15:22:55 -0400 | [diff] [blame] | 325 | { |
| 326 | static DEFINE_MUTEX(rpcb_create_local_mutex); |
| 327 | int result = 0; |
| 328 | |
Stanislav Kinsbursky | 253fb07 | 2011-10-25 14:16:48 +0300 | [diff] [blame] | 329 | if (rpcb_get_local()) |
Chuck Lever | 7402ab19 | 2011-05-09 15:22:55 -0400 | [diff] [blame] | 330 | return result; |
| 331 | |
| 332 | mutex_lock(&rpcb_create_local_mutex); |
Stanislav Kinsbursky | 253fb07 | 2011-10-25 14:16:48 +0300 | [diff] [blame] | 333 | if (rpcb_get_local()) |
Chuck Lever | 7402ab19 | 2011-05-09 15:22:55 -0400 | [diff] [blame] | 334 | goto out; |
| 335 | |
| 336 | if (rpcb_create_local_unix() != 0) |
| 337 | result = rpcb_create_local_net(); |
| 338 | |
| 339 | out: |
Chuck Lever | c526611 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 340 | mutex_unlock(&rpcb_create_local_mutex); |
| 341 | return result; |
Chuck Lever | cc5598b | 2008-07-14 16:03:27 -0400 | [diff] [blame] | 342 | } |
| 343 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 344 | static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr, |
Chuck Lever | 423d8b0 | 2008-07-14 16:03:28 -0400 | [diff] [blame] | 345 | size_t salen, int proto, u32 version) |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 346 | { |
| 347 | struct rpc_create_args args = { |
Pavel Emelyanov | c653ce3 | 2010-09-29 16:04:45 +0400 | [diff] [blame] | 348 | .net = &init_net, |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 349 | .protocol = proto, |
| 350 | .address = srvaddr, |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame] | 351 | .addrsize = salen, |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 352 | .servername = hostname, |
| 353 | .program = &rpcb_program, |
| 354 | .version = version, |
| 355 | .authflavor = RPC_AUTH_UNIX, |
Chuck Lever | 423d8b0 | 2008-07-14 16:03:28 -0400 | [diff] [blame] | 356 | .flags = (RPC_CLNT_CREATE_NOPING | |
| 357 | RPC_CLNT_CREATE_NONPRIVPORT), |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 358 | }; |
| 359 | |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 360 | switch (srvaddr->sa_family) { |
| 361 | case AF_INET: |
| 362 | ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT); |
| 363 | break; |
| 364 | case AF_INET6: |
| 365 | ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT); |
| 366 | break; |
| 367 | default: |
Pavel Emelyanov | c636b57 | 2010-10-06 13:45:56 +0400 | [diff] [blame] | 368 | return ERR_PTR(-EAFNOSUPPORT); |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 369 | } |
| 370 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 371 | return rpc_create(&args); |
| 372 | } |
| 373 | |
Chuck Lever | c526611 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 374 | static int rpcb_register_call(struct rpc_clnt *clnt, struct rpc_message *msg) |
Chuck Lever | babe80e | 2008-07-14 16:03:29 -0400 | [diff] [blame] | 375 | { |
Chuck Lever | 14aeb21 | 2008-08-18 19:34:00 -0400 | [diff] [blame] | 376 | int result, error = 0; |
Chuck Lever | babe80e | 2008-07-14 16:03:29 -0400 | [diff] [blame] | 377 | |
Chuck Lever | 14aeb21 | 2008-08-18 19:34:00 -0400 | [diff] [blame] | 378 | msg->rpc_resp = &result; |
Chuck Lever | babe80e | 2008-07-14 16:03:29 -0400 | [diff] [blame] | 379 | |
Chuck Lever | 2a76b3b | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 380 | error = rpc_call_sync(clnt, msg, RPC_TASK_SOFTCONN); |
Chuck Lever | 14aeb21 | 2008-08-18 19:34:00 -0400 | [diff] [blame] | 381 | if (error < 0) { |
Chuck Lever | 363f724 | 2009-03-18 20:47:44 -0400 | [diff] [blame] | 382 | dprintk("RPC: failed to contact local rpcbind " |
Chuck Lever | babe80e | 2008-07-14 16:03:29 -0400 | [diff] [blame] | 383 | "server (errno %d).\n", -error); |
Chuck Lever | 14aeb21 | 2008-08-18 19:34:00 -0400 | [diff] [blame] | 384 | return error; |
| 385 | } |
Chuck Lever | babe80e | 2008-07-14 16:03:29 -0400 | [diff] [blame] | 386 | |
Chuck Lever | db820d6 | 2008-09-25 11:57:05 -0400 | [diff] [blame] | 387 | if (!result) |
Chuck Lever | 14aeb21 | 2008-08-18 19:34:00 -0400 | [diff] [blame] | 388 | return -EACCES; |
Chuck Lever | 14aeb21 | 2008-08-18 19:34:00 -0400 | [diff] [blame] | 389 | return 0; |
Chuck Lever | babe80e | 2008-07-14 16:03:29 -0400 | [diff] [blame] | 390 | } |
| 391 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 392 | /** |
| 393 | * rpcb_register - set or unset a port registration with the local rpcbind svc |
| 394 | * @prog: RPC program number to bind |
| 395 | * @vers: RPC version number to bind |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 396 | * @prot: transport protocol to register |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 397 | * @port: port value to register |
Chuck Lever | 14aeb21 | 2008-08-18 19:34:00 -0400 | [diff] [blame] | 398 | * |
| 399 | * Returns zero if the registration request was dispatched successfully |
| 400 | * and the rpcbind daemon returned success. Otherwise, returns an errno |
| 401 | * value that reflects the nature of the error (request could not be |
| 402 | * dispatched, timed out, or rpcbind returned an error). |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 403 | * |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 404 | * RPC services invoke this function to advertise their contact |
| 405 | * information via the system's rpcbind daemon. RPC services |
| 406 | * invoke this function once for each [program, version, transport] |
| 407 | * tuple they wish to advertise. |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 408 | * |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 409 | * Callers may also unregister RPC services that are no longer |
| 410 | * available by setting the passed-in port to zero. This removes |
| 411 | * all registered transports for [program, version] from the local |
| 412 | * rpcbind database. |
| 413 | * |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 414 | * This function uses rpcbind protocol version 2 to contact the |
| 415 | * local rpcbind daemon. |
| 416 | * |
| 417 | * Registration works over both AF_INET and AF_INET6, and services |
| 418 | * registered via this function are advertised as available for any |
| 419 | * address. If the local rpcbind daemon is listening on AF_INET6, |
| 420 | * services registered via this function will be advertised on |
| 421 | * IN6ADDR_ANY (ie available for all AF_INET and AF_INET6 |
| 422 | * addresses). |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 423 | */ |
Chuck Lever | 14aeb21 | 2008-08-18 19:34:00 -0400 | [diff] [blame] | 424 | int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port) |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 425 | { |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 426 | struct rpcbind_args map = { |
| 427 | .r_prog = prog, |
| 428 | .r_vers = vers, |
| 429 | .r_prot = prot, |
| 430 | .r_port = port, |
| 431 | }; |
| 432 | struct rpc_message msg = { |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 433 | .rpc_argp = &map, |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 434 | }; |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 435 | struct sunrpc_net *sn = net_generic(&init_net, sunrpc_net_id); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 436 | |
| 437 | dprintk("RPC: %sregistering (%u, %u, %d, %u) with local " |
| 438 | "rpcbind\n", (port ? "" : "un"), |
| 439 | prog, vers, prot, port); |
| 440 | |
Chuck Lever | babe80e | 2008-07-14 16:03:29 -0400 | [diff] [blame] | 441 | msg.rpc_proc = &rpcb_procedures2[RPCBPROC_UNSET]; |
| 442 | if (port) |
| 443 | msg.rpc_proc = &rpcb_procedures2[RPCBPROC_SET]; |
| 444 | |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 445 | return rpcb_register_call(sn->rpcb_local_clnt, &msg); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 446 | } |
| 447 | |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 448 | /* |
| 449 | * Fill in AF_INET family-specific arguments to register |
| 450 | */ |
Chuck Lever | 1673d0d | 2009-03-18 20:47:21 -0400 | [diff] [blame] | 451 | static int rpcb_register_inet4(const struct sockaddr *sap, |
| 452 | struct rpc_message *msg) |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 453 | { |
Chuck Lever | 3aba455 | 2009-03-18 20:47:06 -0400 | [diff] [blame] | 454 | const struct sockaddr_in *sin = (const struct sockaddr_in *)sap; |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 455 | struct rpcbind_args *map = msg->rpc_argp; |
Chuck Lever | 3aba455 | 2009-03-18 20:47:06 -0400 | [diff] [blame] | 456 | unsigned short port = ntohs(sin->sin_port); |
Chuck Lever | ba80913 | 2009-08-09 15:09:35 -0400 | [diff] [blame] | 457 | int result; |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 458 | struct sunrpc_net *sn = net_generic(&init_net, sunrpc_net_id); |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 459 | |
Trond Myklebust | d77385f | 2011-10-17 16:08:10 -0700 | [diff] [blame] | 460 | map->r_addr = rpc_sockaddr2uaddr(sap, GFP_KERNEL); |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 461 | |
| 462 | dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with " |
| 463 | "local rpcbind\n", (port ? "" : "un"), |
| 464 | map->r_prog, map->r_vers, |
| 465 | map->r_addr, map->r_netid); |
| 466 | |
| 467 | msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET]; |
| 468 | if (port) |
| 469 | msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET]; |
| 470 | |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 471 | result = rpcb_register_call(sn->rpcb_local_clnt4, msg); |
Chuck Lever | ba80913 | 2009-08-09 15:09:35 -0400 | [diff] [blame] | 472 | kfree(map->r_addr); |
| 473 | return result; |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | /* |
| 477 | * Fill in AF_INET6 family-specific arguments to register |
| 478 | */ |
Chuck Lever | 1673d0d | 2009-03-18 20:47:21 -0400 | [diff] [blame] | 479 | static int rpcb_register_inet6(const struct sockaddr *sap, |
| 480 | struct rpc_message *msg) |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 481 | { |
Chuck Lever | 3aba455 | 2009-03-18 20:47:06 -0400 | [diff] [blame] | 482 | const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sap; |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 483 | struct rpcbind_args *map = msg->rpc_argp; |
Chuck Lever | 3aba455 | 2009-03-18 20:47:06 -0400 | [diff] [blame] | 484 | unsigned short port = ntohs(sin6->sin6_port); |
Chuck Lever | ba80913 | 2009-08-09 15:09:35 -0400 | [diff] [blame] | 485 | int result; |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 486 | struct sunrpc_net *sn = net_generic(&init_net, sunrpc_net_id); |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 487 | |
Trond Myklebust | d77385f | 2011-10-17 16:08:10 -0700 | [diff] [blame] | 488 | map->r_addr = rpc_sockaddr2uaddr(sap, GFP_KERNEL); |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 489 | |
| 490 | dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with " |
| 491 | "local rpcbind\n", (port ? "" : "un"), |
| 492 | map->r_prog, map->r_vers, |
| 493 | map->r_addr, map->r_netid); |
| 494 | |
| 495 | msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET]; |
| 496 | if (port) |
| 497 | msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET]; |
| 498 | |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 499 | result = rpcb_register_call(sn->rpcb_local_clnt4, msg); |
Chuck Lever | ba80913 | 2009-08-09 15:09:35 -0400 | [diff] [blame] | 500 | kfree(map->r_addr); |
| 501 | return result; |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 502 | } |
| 503 | |
Chuck Lever | 1673d0d | 2009-03-18 20:47:21 -0400 | [diff] [blame] | 504 | static int rpcb_unregister_all_protofamilies(struct rpc_message *msg) |
| 505 | { |
| 506 | struct rpcbind_args *map = msg->rpc_argp; |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 507 | struct sunrpc_net *sn = net_generic(&init_net, sunrpc_net_id); |
Chuck Lever | 1673d0d | 2009-03-18 20:47:21 -0400 | [diff] [blame] | 508 | |
| 509 | dprintk("RPC: unregistering [%u, %u, '%s'] with " |
| 510 | "local rpcbind\n", |
| 511 | map->r_prog, map->r_vers, map->r_netid); |
| 512 | |
| 513 | map->r_addr = ""; |
| 514 | msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET]; |
| 515 | |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 516 | return rpcb_register_call(sn->rpcb_local_clnt4, msg); |
Chuck Lever | 1673d0d | 2009-03-18 20:47:21 -0400 | [diff] [blame] | 517 | } |
| 518 | |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 519 | /** |
| 520 | * rpcb_v4_register - set or unset a port registration with the local rpcbind |
| 521 | * @program: RPC program number of service to (un)register |
| 522 | * @version: RPC version number of service to (un)register |
| 523 | * @address: address family, IP address, and port to (un)register |
| 524 | * @netid: netid of transport protocol to (un)register |
Chuck Lever | 14aeb21 | 2008-08-18 19:34:00 -0400 | [diff] [blame] | 525 | * |
| 526 | * Returns zero if the registration request was dispatched successfully |
| 527 | * and the rpcbind daemon returned success. Otherwise, returns an errno |
| 528 | * value that reflects the nature of the error (request could not be |
| 529 | * dispatched, timed out, or rpcbind returned an error). |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 530 | * |
| 531 | * RPC services invoke this function to advertise their contact |
| 532 | * information via the system's rpcbind daemon. RPC services |
| 533 | * invoke this function once for each [program, version, address, |
| 534 | * netid] tuple they wish to advertise. |
| 535 | * |
Chuck Lever | 1673d0d | 2009-03-18 20:47:21 -0400 | [diff] [blame] | 536 | * Callers may also unregister RPC services that are registered at a |
| 537 | * specific address by setting the port number in @address to zero. |
| 538 | * They may unregister all registered protocol families at once for |
| 539 | * a service by passing a NULL @address argument. If @netid is "" |
| 540 | * then all netids for [program, version, address] are unregistered. |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 541 | * |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 542 | * This function uses rpcbind protocol version 4 to contact the |
| 543 | * local rpcbind daemon. The local rpcbind daemon must support |
| 544 | * version 4 of the rpcbind protocol in order for these functions |
| 545 | * to register a service successfully. |
| 546 | * |
| 547 | * Supported netids include "udp" and "tcp" for UDP and TCP over |
| 548 | * IPv4, and "udp6" and "tcp6" for UDP and TCP over IPv6, |
| 549 | * respectively. |
| 550 | * |
| 551 | * The contents of @address determine the address family and the |
| 552 | * port to be registered. The usual practice is to pass INADDR_ANY |
| 553 | * as the raw address, but specifying a non-zero address is also |
| 554 | * supported by this API if the caller wishes to advertise an RPC |
| 555 | * service on a specific network interface. |
| 556 | * |
| 557 | * Note that passing in INADDR_ANY does not create the same service |
| 558 | * registration as IN6ADDR_ANY. The former advertises an RPC |
| 559 | * service on any IPv4 address, but not on IPv6. The latter |
| 560 | * advertises the service on all IPv4 and IPv6 addresses. |
| 561 | */ |
| 562 | int rpcb_v4_register(const u32 program, const u32 version, |
Chuck Lever | 14aeb21 | 2008-08-18 19:34:00 -0400 | [diff] [blame] | 563 | const struct sockaddr *address, const char *netid) |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 564 | { |
| 565 | struct rpcbind_args map = { |
| 566 | .r_prog = program, |
| 567 | .r_vers = version, |
| 568 | .r_netid = netid, |
| 569 | .r_owner = RPCB_OWNER_STRING, |
| 570 | }; |
| 571 | struct rpc_message msg = { |
| 572 | .rpc_argp = &map, |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 573 | }; |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 574 | struct sunrpc_net *sn = net_generic(&init_net, sunrpc_net_id); |
Chuck Lever | c526611 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 575 | |
Stanislav Kinsbursky | dff02d4 | 2012-01-13 12:52:10 +0400 | [diff] [blame^] | 576 | if (sn->rpcb_local_clnt4 == NULL) |
Chuck Lever | c526611 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 577 | return -EPROTONOSUPPORT; |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 578 | |
Chuck Lever | 1673d0d | 2009-03-18 20:47:21 -0400 | [diff] [blame] | 579 | if (address == NULL) |
| 580 | return rpcb_unregister_all_protofamilies(&msg); |
| 581 | |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 582 | switch (address->sa_family) { |
| 583 | case AF_INET: |
Chuck Lever | 1673d0d | 2009-03-18 20:47:21 -0400 | [diff] [blame] | 584 | return rpcb_register_inet4(address, &msg); |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 585 | case AF_INET6: |
Chuck Lever | 1673d0d | 2009-03-18 20:47:21 -0400 | [diff] [blame] | 586 | return rpcb_register_inet6(address, &msg); |
Chuck Lever | c2e1b09 | 2008-07-14 16:03:30 -0400 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | return -EAFNOSUPPORT; |
| 590 | } |
| 591 | |
Trond Myklebust | 803a906 | 2008-07-01 15:20:55 -0400 | [diff] [blame] | 592 | 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] | 593 | { |
| 594 | struct rpc_message msg = { |
Trond Myklebust | 803a906 | 2008-07-01 15:20:55 -0400 | [diff] [blame] | 595 | .rpc_proc = proc, |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 596 | .rpc_argp = map, |
Chuck Lever | c0c077d | 2009-08-09 15:09:43 -0400 | [diff] [blame] | 597 | .rpc_resp = map, |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 598 | }; |
| 599 | struct rpc_task_setup task_setup_data = { |
| 600 | .rpc_client = rpcb_clnt, |
| 601 | .rpc_message = &msg, |
| 602 | .callback_ops = &rpcb_getport_ops, |
| 603 | .callback_data = map, |
Chuck Lever | 012da15 | 2009-12-03 15:58:56 -0500 | [diff] [blame] | 604 | .flags = RPC_TASK_ASYNC | RPC_TASK_SOFTCONN, |
Trond Myklebust | 5138fde | 2007-07-14 15:40:01 -0400 | [diff] [blame] | 605 | }; |
| 606 | |
| 607 | return rpc_run_task(&task_setup_data); |
| 608 | } |
| 609 | |
Trond Myklebust | 9a4bd29 | 2008-10-03 16:48:34 -0400 | [diff] [blame] | 610 | /* |
| 611 | * In the case where rpc clients have been cloned, we want to make |
| 612 | * sure that we use the program number/version etc of the actual |
| 613 | * owner of the xprt. To do so, we walk back up the tree of parents |
| 614 | * to find whoever created the transport and/or whoever has the |
| 615 | * autobind flag set. |
| 616 | */ |
| 617 | static struct rpc_clnt *rpcb_find_transport_owner(struct rpc_clnt *clnt) |
| 618 | { |
| 619 | struct rpc_clnt *parent = clnt->cl_parent; |
| 620 | |
| 621 | while (parent != clnt) { |
| 622 | if (parent->cl_xprt != clnt->cl_xprt) |
| 623 | break; |
| 624 | if (clnt->cl_autobind) |
| 625 | break; |
| 626 | clnt = parent; |
| 627 | parent = parent->cl_parent; |
| 628 | } |
| 629 | return clnt; |
| 630 | } |
| 631 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 632 | /** |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 633 | * 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] | 634 | * @task: task that is waiting for portmapper request |
| 635 | * |
| 636 | * This one can be called for an ongoing RPC request, and can be used in |
| 637 | * an async (rpciod) context. |
| 638 | */ |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 639 | void rpcb_getport_async(struct rpc_task *task) |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 640 | { |
Trond Myklebust | 9a4bd29 | 2008-10-03 16:48:34 -0400 | [diff] [blame] | 641 | struct rpc_clnt *clnt; |
Trond Myklebust | 803a906 | 2008-07-01 15:20:55 -0400 | [diff] [blame] | 642 | struct rpc_procinfo *proc; |
Chuck Lever | 0a48f5d | 2007-12-10 14:56:38 -0500 | [diff] [blame] | 643 | u32 bind_version; |
Trond Myklebust | 9a4bd29 | 2008-10-03 16:48:34 -0400 | [diff] [blame] | 644 | struct rpc_xprt *xprt; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 645 | struct rpc_clnt *rpcb_clnt; |
Ben Greear | ec0dd26 | 2011-07-12 10:27:55 -0700 | [diff] [blame] | 646 | struct rpcbind_args *map; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 647 | struct rpc_task *child; |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame] | 648 | struct sockaddr_storage addr; |
| 649 | struct sockaddr *sap = (struct sockaddr *)&addr; |
| 650 | size_t salen; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 651 | int status; |
| 652 | |
Trond Myklebust | 9a4bd29 | 2008-10-03 16:48:34 -0400 | [diff] [blame] | 653 | clnt = rpcb_find_transport_owner(task->tk_client); |
| 654 | xprt = clnt->cl_xprt; |
| 655 | |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 656 | dprintk("RPC: %5u %s(%s, %u, %u, %d)\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 657 | task->tk_pid, __func__, |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 658 | clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 659 | |
Trond Myklebust | 381ba74 | 2008-07-07 12:18:53 -0400 | [diff] [blame] | 660 | /* Put self on the wait queue to ensure we get notified if |
| 661 | * some other task is already attempting to bind the port */ |
| 662 | rpc_sleep_on(&xprt->binding, task, NULL); |
| 663 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 664 | if (xprt_test_and_set_binding(xprt)) { |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 665 | dprintk("RPC: %5u %s: waiting for another binder\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 666 | task->tk_pid, __func__); |
Trond Myklebust | 381ba74 | 2008-07-07 12:18:53 -0400 | [diff] [blame] | 667 | return; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 668 | } |
| 669 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 670 | /* Someone else may have bound if we slept */ |
| 671 | if (xprt_bound(xprt)) { |
| 672 | status = 0; |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 673 | dprintk("RPC: %5u %s: already bound\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 674 | task->tk_pid, __func__); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 675 | goto bailout_nofree; |
| 676 | } |
| 677 | |
Chuck Lever | ba80913 | 2009-08-09 15:09:35 -0400 | [diff] [blame] | 678 | /* Parent transport's destination address */ |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame] | 679 | salen = rpc_peeraddr(clnt, sap, sizeof(addr)); |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 680 | |
| 681 | /* Don't ever use rpcbind v2 for AF_INET6 requests */ |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame] | 682 | switch (sap->sa_family) { |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 683 | case AF_INET: |
Trond Myklebust | 803a906 | 2008-07-01 15:20:55 -0400 | [diff] [blame] | 684 | proc = rpcb_next_version[xprt->bind_index].rpc_proc; |
| 685 | bind_version = rpcb_next_version[xprt->bind_index].rpc_vers; |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 686 | break; |
| 687 | case AF_INET6: |
Trond Myklebust | 803a906 | 2008-07-01 15:20:55 -0400 | [diff] [blame] | 688 | proc = rpcb_next_version6[xprt->bind_index].rpc_proc; |
| 689 | bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers; |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 690 | break; |
| 691 | default: |
| 692 | status = -EAFNOSUPPORT; |
| 693 | dprintk("RPC: %5u %s: bad address family\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 694 | task->tk_pid, __func__); |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 695 | goto bailout_nofree; |
| 696 | } |
Trond Myklebust | 803a906 | 2008-07-01 15:20:55 -0400 | [diff] [blame] | 697 | if (proc == NULL) { |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 698 | xprt->bind_index = 0; |
Chuck Lever | 906462a | 2007-09-11 18:00:47 -0400 | [diff] [blame] | 699 | status = -EPFNOSUPPORT; |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 700 | dprintk("RPC: %5u %s: no more getport versions available\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 701 | task->tk_pid, __func__); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 702 | goto bailout_nofree; |
| 703 | } |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 704 | |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 705 | dprintk("RPC: %5u %s: trying rpcbind version %u\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 706 | task->tk_pid, __func__, bind_version); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 707 | |
Chuck Lever | 9f6ad26 | 2007-12-10 14:56:31 -0500 | [diff] [blame] | 708 | rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot, |
Chuck Lever | 423d8b0 | 2008-07-14 16:03:28 -0400 | [diff] [blame] | 709 | bind_version); |
Chuck Lever | 143b6c4 | 2007-08-16 16:03:31 -0400 | [diff] [blame] | 710 | if (IS_ERR(rpcb_clnt)) { |
| 711 | status = PTR_ERR(rpcb_clnt); |
| 712 | dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 713 | task->tk_pid, __func__, PTR_ERR(rpcb_clnt)); |
Chuck Lever | 143b6c4 | 2007-08-16 16:03:31 -0400 | [diff] [blame] | 714 | goto bailout_nofree; |
| 715 | } |
| 716 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 717 | map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC); |
| 718 | if (!map) { |
| 719 | status = -ENOMEM; |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 720 | dprintk("RPC: %5u %s: no memory available\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 721 | task->tk_pid, __func__); |
Trond Myklebust | 96165e2 | 2008-10-03 16:48:40 -0400 | [diff] [blame] | 722 | goto bailout_release_client; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 723 | } |
| 724 | map->r_prog = clnt->cl_prog; |
| 725 | map->r_vers = clnt->cl_vers; |
| 726 | map->r_prot = xprt->prot; |
| 727 | map->r_port = 0; |
| 728 | map->r_xprt = xprt_get(xprt); |
Trond Myklebust | 381ba74 | 2008-07-07 12:18:53 -0400 | [diff] [blame] | 729 | map->r_status = -EIO; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 730 | |
Chuck Lever | ba80913 | 2009-08-09 15:09:35 -0400 | [diff] [blame] | 731 | switch (bind_version) { |
| 732 | case RPCBVERS_4: |
| 733 | case RPCBVERS_3: |
| 734 | map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID); |
Trond Myklebust | d77385f | 2011-10-17 16:08:10 -0700 | [diff] [blame] | 735 | map->r_addr = rpc_sockaddr2uaddr(sap, GFP_ATOMIC); |
Chuck Lever | ba80913 | 2009-08-09 15:09:35 -0400 | [diff] [blame] | 736 | map->r_owner = ""; |
| 737 | break; |
| 738 | case RPCBVERS_2: |
| 739 | map->r_addr = NULL; |
| 740 | break; |
| 741 | default: |
| 742 | BUG(); |
| 743 | } |
| 744 | |
Trond Myklebust | 803a906 | 2008-07-01 15:20:55 -0400 | [diff] [blame] | 745 | child = rpcb_call_async(rpcb_clnt, map, proc); |
Trond Myklebust | 4c402b4 | 2007-06-14 16:40:32 -0400 | [diff] [blame] | 746 | rpc_release_client(rpcb_clnt); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 747 | if (IS_ERR(child)) { |
Trond Myklebust | 0d3a34b | 2008-07-07 12:18:52 -0400 | [diff] [blame] | 748 | /* rpcb_map_release() has freed the arguments */ |
Chuck Lever | 45160d6 | 2007-07-01 12:13:17 -0400 | [diff] [blame] | 749 | dprintk("RPC: %5u %s: rpc_run_task failed\n", |
Harvey Harrison | 0dc4787 | 2008-03-05 20:47:47 -0800 | [diff] [blame] | 750 | task->tk_pid, __func__); |
Trond Myklebust | 381ba74 | 2008-07-07 12:18:53 -0400 | [diff] [blame] | 751 | return; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 752 | } |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 753 | |
Trond Myklebust | 9a4bd29 | 2008-10-03 16:48:34 -0400 | [diff] [blame] | 754 | xprt->stat.bind_count++; |
| 755 | rpc_put_task(child); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 756 | return; |
| 757 | |
Trond Myklebust | 96165e2 | 2008-10-03 16:48:40 -0400 | [diff] [blame] | 758 | bailout_release_client: |
| 759 | rpc_release_client(rpcb_clnt); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 760 | bailout_nofree: |
| 761 | rpcb_wake_rpcbind_waiters(xprt, status); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 762 | task->tk_status = status; |
| 763 | } |
\"Talpey, Thomas\ | 1244480 | 2007-09-10 13:45:36 -0400 | [diff] [blame] | 764 | EXPORT_SYMBOL_GPL(rpcb_getport_async); |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 765 | |
| 766 | /* |
| 767 | * Rpcbind child task calls this callback via tk_exit. |
| 768 | */ |
| 769 | static void rpcb_getport_done(struct rpc_task *child, void *data) |
| 770 | { |
| 771 | struct rpcbind_args *map = data; |
| 772 | struct rpc_xprt *xprt = map->r_xprt; |
| 773 | int status = child->tk_status; |
| 774 | |
Chuck Lever | 4784cb5 | 2007-09-11 18:00:36 -0400 | [diff] [blame] | 775 | /* Garbage reply: retry with a lesser rpcbind version */ |
| 776 | if (status == -EIO) |
| 777 | status = -EPROTONOSUPPORT; |
| 778 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 779 | /* rpcbind server doesn't support this rpcbind protocol version */ |
| 780 | if (status == -EPROTONOSUPPORT) |
| 781 | xprt->bind_index++; |
| 782 | |
| 783 | if (status < 0) { |
| 784 | /* rpcbind server not available on remote host? */ |
| 785 | xprt->ops->set_port(xprt, 0); |
| 786 | } else if (map->r_port == 0) { |
| 787 | /* Requested RPC service wasn't registered on remote host */ |
| 788 | xprt->ops->set_port(xprt, 0); |
| 789 | status = -EACCES; |
| 790 | } else { |
| 791 | /* Succeeded */ |
| 792 | xprt->ops->set_port(xprt, map->r_port); |
| 793 | xprt_set_bound(xprt); |
| 794 | status = 0; |
| 795 | } |
| 796 | |
| 797 | dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n", |
| 798 | child->tk_pid, status, map->r_port); |
| 799 | |
Trond Myklebust | 381ba74 | 2008-07-07 12:18:53 -0400 | [diff] [blame] | 800 | map->r_status = status; |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 801 | } |
| 802 | |
Chuck Lever | 166b88d | 2008-07-14 16:03:26 -0400 | [diff] [blame] | 803 | /* |
| 804 | * XDR functions for rpcbind |
| 805 | */ |
| 806 | |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 807 | static void rpcb_enc_mapping(struct rpc_rqst *req, struct xdr_stream *xdr, |
| 808 | const struct rpcbind_args *rpcb) |
Chuck Lever | 6f2c2db | 2009-08-09 15:09:40 -0400 | [diff] [blame] | 809 | { |
| 810 | struct rpc_task *task = req->rq_task; |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 811 | __be32 *p; |
Chuck Lever | 6f2c2db | 2009-08-09 15:09:40 -0400 | [diff] [blame] | 812 | |
| 813 | dprintk("RPC: %5u encoding PMAP_%s call (%u, %u, %d, %u)\n", |
| 814 | task->tk_pid, task->tk_msg.rpc_proc->p_name, |
| 815 | rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port); |
| 816 | |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 817 | p = xdr_reserve_space(xdr, RPCB_mappingargs_sz << 2); |
Chuck Lever | 4129ccf | 2010-12-14 14:58:59 +0000 | [diff] [blame] | 818 | *p++ = cpu_to_be32(rpcb->r_prog); |
| 819 | *p++ = cpu_to_be32(rpcb->r_vers); |
| 820 | *p++ = cpu_to_be32(rpcb->r_prot); |
| 821 | *p = cpu_to_be32(rpcb->r_port); |
Chuck Lever | 6f2c2db | 2009-08-09 15:09:40 -0400 | [diff] [blame] | 822 | } |
| 823 | |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 824 | static int rpcb_dec_getport(struct rpc_rqst *req, struct xdr_stream *xdr, |
Chuck Lever | c0c077d | 2009-08-09 15:09:43 -0400 | [diff] [blame] | 825 | struct rpcbind_args *rpcb) |
| 826 | { |
| 827 | struct rpc_task *task = req->rq_task; |
Chuck Lever | c0c077d | 2009-08-09 15:09:43 -0400 | [diff] [blame] | 828 | unsigned long port; |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 829 | __be32 *p; |
Chuck Lever | c0c077d | 2009-08-09 15:09:43 -0400 | [diff] [blame] | 830 | |
| 831 | rpcb->r_port = 0; |
| 832 | |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 833 | p = xdr_inline_decode(xdr, 4); |
Chuck Lever | c0c077d | 2009-08-09 15:09:43 -0400 | [diff] [blame] | 834 | if (unlikely(p == NULL)) |
| 835 | return -EIO; |
| 836 | |
Chuck Lever | 4129ccf | 2010-12-14 14:58:59 +0000 | [diff] [blame] | 837 | port = be32_to_cpup(p); |
Chuck Lever | c0c077d | 2009-08-09 15:09:43 -0400 | [diff] [blame] | 838 | dprintk("RPC: %5u PMAP_%s result: %lu\n", task->tk_pid, |
| 839 | task->tk_msg.rpc_proc->p_name, port); |
Alexey Dobriyan | 4be929b | 2010-05-24 14:33:03 -0700 | [diff] [blame] | 840 | if (unlikely(port > USHRT_MAX)) |
Chuck Lever | c0c077d | 2009-08-09 15:09:43 -0400 | [diff] [blame] | 841 | return -EIO; |
| 842 | |
| 843 | rpcb->r_port = port; |
| 844 | return 0; |
| 845 | } |
| 846 | |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 847 | static int rpcb_dec_set(struct rpc_rqst *req, struct xdr_stream *xdr, |
Chuck Lever | 7ed0ff9 | 2009-08-09 15:09:42 -0400 | [diff] [blame] | 848 | unsigned int *boolp) |
| 849 | { |
| 850 | struct rpc_task *task = req->rq_task; |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 851 | __be32 *p; |
Chuck Lever | 7ed0ff9 | 2009-08-09 15:09:42 -0400 | [diff] [blame] | 852 | |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 853 | p = xdr_inline_decode(xdr, 4); |
Chuck Lever | 7ed0ff9 | 2009-08-09 15:09:42 -0400 | [diff] [blame] | 854 | if (unlikely(p == NULL)) |
| 855 | return -EIO; |
| 856 | |
| 857 | *boolp = 0; |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 858 | if (*p != xdr_zero) |
Chuck Lever | 7ed0ff9 | 2009-08-09 15:09:42 -0400 | [diff] [blame] | 859 | *boolp = 1; |
| 860 | |
| 861 | dprintk("RPC: %5u RPCB_%s call %s\n", |
| 862 | task->tk_pid, task->tk_msg.rpc_proc->p_name, |
| 863 | (*boolp ? "succeeded" : "failed")); |
| 864 | return 0; |
| 865 | } |
| 866 | |
Chuck Lever | 4129ccf | 2010-12-14 14:58:59 +0000 | [diff] [blame] | 867 | static void encode_rpcb_string(struct xdr_stream *xdr, const char *string, |
| 868 | const u32 maxstrlen) |
Chuck Lever | 6f2c2db | 2009-08-09 15:09:40 -0400 | [diff] [blame] | 869 | { |
Chuck Lever | 6f2c2db | 2009-08-09 15:09:40 -0400 | [diff] [blame] | 870 | __be32 *p; |
Chuck Lever | 4129ccf | 2010-12-14 14:58:59 +0000 | [diff] [blame] | 871 | u32 len; |
Chuck Lever | 6f2c2db | 2009-08-09 15:09:40 -0400 | [diff] [blame] | 872 | |
Chuck Lever | 6f2c2db | 2009-08-09 15:09:40 -0400 | [diff] [blame] | 873 | len = strlen(string); |
Chuck Lever | 4129ccf | 2010-12-14 14:58:59 +0000 | [diff] [blame] | 874 | BUG_ON(len > maxstrlen); |
| 875 | p = xdr_reserve_space(xdr, 4 + len); |
Chuck Lever | 6f2c2db | 2009-08-09 15:09:40 -0400 | [diff] [blame] | 876 | xdr_encode_opaque(p, string, len); |
Chuck Lever | 6f2c2db | 2009-08-09 15:09:40 -0400 | [diff] [blame] | 877 | } |
| 878 | |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 879 | static void rpcb_enc_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr, |
| 880 | const struct rpcbind_args *rpcb) |
Chuck Lever | 6f2c2db | 2009-08-09 15:09:40 -0400 | [diff] [blame] | 881 | { |
| 882 | struct rpc_task *task = req->rq_task; |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 883 | __be32 *p; |
Chuck Lever | 6f2c2db | 2009-08-09 15:09:40 -0400 | [diff] [blame] | 884 | |
| 885 | dprintk("RPC: %5u encoding RPCB_%s call (%u, %u, '%s', '%s')\n", |
| 886 | task->tk_pid, task->tk_msg.rpc_proc->p_name, |
| 887 | rpcb->r_prog, rpcb->r_vers, |
| 888 | rpcb->r_netid, rpcb->r_addr); |
| 889 | |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 890 | p = xdr_reserve_space(xdr, (RPCB_program_sz + RPCB_version_sz) << 2); |
Chuck Lever | 4129ccf | 2010-12-14 14:58:59 +0000 | [diff] [blame] | 891 | *p++ = cpu_to_be32(rpcb->r_prog); |
| 892 | *p = cpu_to_be32(rpcb->r_vers); |
Chuck Lever | 6f2c2db | 2009-08-09 15:09:40 -0400 | [diff] [blame] | 893 | |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 894 | encode_rpcb_string(xdr, rpcb->r_netid, RPCBIND_MAXNETIDLEN); |
| 895 | encode_rpcb_string(xdr, rpcb->r_addr, RPCBIND_MAXUADDRLEN); |
| 896 | encode_rpcb_string(xdr, rpcb->r_owner, RPCB_MAXOWNERLEN); |
Chuck Lever | 6f2c2db | 2009-08-09 15:09:40 -0400 | [diff] [blame] | 897 | } |
| 898 | |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 899 | static int rpcb_dec_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr, |
Chuck Lever | c0c077d | 2009-08-09 15:09:43 -0400 | [diff] [blame] | 900 | struct rpcbind_args *rpcb) |
| 901 | { |
| 902 | struct sockaddr_storage address; |
| 903 | struct sockaddr *sap = (struct sockaddr *)&address; |
| 904 | struct rpc_task *task = req->rq_task; |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 905 | __be32 *p; |
Chuck Lever | c0c077d | 2009-08-09 15:09:43 -0400 | [diff] [blame] | 906 | u32 len; |
| 907 | |
| 908 | rpcb->r_port = 0; |
| 909 | |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 910 | p = xdr_inline_decode(xdr, 4); |
Chuck Lever | c0c077d | 2009-08-09 15:09:43 -0400 | [diff] [blame] | 911 | if (unlikely(p == NULL)) |
| 912 | goto out_fail; |
Chuck Lever | 4129ccf | 2010-12-14 14:58:59 +0000 | [diff] [blame] | 913 | len = be32_to_cpup(p); |
Chuck Lever | c0c077d | 2009-08-09 15:09:43 -0400 | [diff] [blame] | 914 | |
| 915 | /* |
| 916 | * If the returned universal address is a null string, |
| 917 | * the requested RPC service was not registered. |
| 918 | */ |
| 919 | if (len == 0) { |
| 920 | dprintk("RPC: %5u RPCB reply: program not registered\n", |
| 921 | task->tk_pid); |
| 922 | return 0; |
| 923 | } |
| 924 | |
| 925 | if (unlikely(len > RPCBIND_MAXUADDRLEN)) |
| 926 | goto out_fail; |
| 927 | |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 928 | p = xdr_inline_decode(xdr, len); |
Chuck Lever | c0c077d | 2009-08-09 15:09:43 -0400 | [diff] [blame] | 929 | if (unlikely(p == NULL)) |
| 930 | goto out_fail; |
| 931 | dprintk("RPC: %5u RPCB_%s reply: %s\n", task->tk_pid, |
| 932 | task->tk_msg.rpc_proc->p_name, (char *)p); |
| 933 | |
| 934 | if (rpc_uaddr2sockaddr((char *)p, len, sap, sizeof(address)) == 0) |
| 935 | goto out_fail; |
| 936 | rpcb->r_port = rpc_get_port(sap); |
| 937 | |
| 938 | return 0; |
| 939 | |
| 940 | out_fail: |
| 941 | dprintk("RPC: %5u malformed RPCB_%s reply\n", |
| 942 | task->tk_pid, task->tk_msg.rpc_proc->p_name); |
| 943 | return -EIO; |
| 944 | } |
| 945 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 946 | /* |
| 947 | * Not all rpcbind procedures described in RFC 1833 are implemented |
| 948 | * since the Linux kernel RPC code requires only these. |
| 949 | */ |
Chuck Lever | f8b761e | 2009-08-09 15:09:44 -0400 | [diff] [blame] | 950 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 951 | static struct rpc_procinfo rpcb_procedures2[] = { |
Chuck Lever | f8b761e | 2009-08-09 15:09:44 -0400 | [diff] [blame] | 952 | [RPCBPROC_SET] = { |
| 953 | .p_proc = RPCBPROC_SET, |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 954 | .p_encode = (kxdreproc_t)rpcb_enc_mapping, |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 955 | .p_decode = (kxdrdproc_t)rpcb_dec_set, |
Chuck Lever | f8b761e | 2009-08-09 15:09:44 -0400 | [diff] [blame] | 956 | .p_arglen = RPCB_mappingargs_sz, |
| 957 | .p_replen = RPCB_setres_sz, |
| 958 | .p_statidx = RPCBPROC_SET, |
| 959 | .p_timer = 0, |
| 960 | .p_name = "SET", |
| 961 | }, |
| 962 | [RPCBPROC_UNSET] = { |
| 963 | .p_proc = RPCBPROC_UNSET, |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 964 | .p_encode = (kxdreproc_t)rpcb_enc_mapping, |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 965 | .p_decode = (kxdrdproc_t)rpcb_dec_set, |
Chuck Lever | f8b761e | 2009-08-09 15:09:44 -0400 | [diff] [blame] | 966 | .p_arglen = RPCB_mappingargs_sz, |
| 967 | .p_replen = RPCB_setres_sz, |
| 968 | .p_statidx = RPCBPROC_UNSET, |
| 969 | .p_timer = 0, |
| 970 | .p_name = "UNSET", |
| 971 | }, |
| 972 | [RPCBPROC_GETPORT] = { |
| 973 | .p_proc = RPCBPROC_GETPORT, |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 974 | .p_encode = (kxdreproc_t)rpcb_enc_mapping, |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 975 | .p_decode = (kxdrdproc_t)rpcb_dec_getport, |
Chuck Lever | f8b761e | 2009-08-09 15:09:44 -0400 | [diff] [blame] | 976 | .p_arglen = RPCB_mappingargs_sz, |
| 977 | .p_replen = RPCB_getportres_sz, |
| 978 | .p_statidx = RPCBPROC_GETPORT, |
| 979 | .p_timer = 0, |
| 980 | .p_name = "GETPORT", |
| 981 | }, |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 982 | }; |
| 983 | |
| 984 | static struct rpc_procinfo rpcb_procedures3[] = { |
Chuck Lever | f8b761e | 2009-08-09 15:09:44 -0400 | [diff] [blame] | 985 | [RPCBPROC_SET] = { |
| 986 | .p_proc = RPCBPROC_SET, |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 987 | .p_encode = (kxdreproc_t)rpcb_enc_getaddr, |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 988 | .p_decode = (kxdrdproc_t)rpcb_dec_set, |
Chuck Lever | f8b761e | 2009-08-09 15:09:44 -0400 | [diff] [blame] | 989 | .p_arglen = RPCB_getaddrargs_sz, |
| 990 | .p_replen = RPCB_setres_sz, |
| 991 | .p_statidx = RPCBPROC_SET, |
| 992 | .p_timer = 0, |
| 993 | .p_name = "SET", |
| 994 | }, |
| 995 | [RPCBPROC_UNSET] = { |
| 996 | .p_proc = RPCBPROC_UNSET, |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 997 | .p_encode = (kxdreproc_t)rpcb_enc_getaddr, |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 998 | .p_decode = (kxdrdproc_t)rpcb_dec_set, |
Chuck Lever | f8b761e | 2009-08-09 15:09:44 -0400 | [diff] [blame] | 999 | .p_arglen = RPCB_getaddrargs_sz, |
| 1000 | .p_replen = RPCB_setres_sz, |
| 1001 | .p_statidx = RPCBPROC_UNSET, |
| 1002 | .p_timer = 0, |
| 1003 | .p_name = "UNSET", |
| 1004 | }, |
| 1005 | [RPCBPROC_GETADDR] = { |
| 1006 | .p_proc = RPCBPROC_GETADDR, |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 1007 | .p_encode = (kxdreproc_t)rpcb_enc_getaddr, |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 1008 | .p_decode = (kxdrdproc_t)rpcb_dec_getaddr, |
Chuck Lever | f8b761e | 2009-08-09 15:09:44 -0400 | [diff] [blame] | 1009 | .p_arglen = RPCB_getaddrargs_sz, |
| 1010 | .p_replen = RPCB_getaddrres_sz, |
| 1011 | .p_statidx = RPCBPROC_GETADDR, |
| 1012 | .p_timer = 0, |
| 1013 | .p_name = "GETADDR", |
| 1014 | }, |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 1015 | }; |
| 1016 | |
| 1017 | static struct rpc_procinfo rpcb_procedures4[] = { |
Chuck Lever | f8b761e | 2009-08-09 15:09:44 -0400 | [diff] [blame] | 1018 | [RPCBPROC_SET] = { |
| 1019 | .p_proc = RPCBPROC_SET, |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 1020 | .p_encode = (kxdreproc_t)rpcb_enc_getaddr, |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 1021 | .p_decode = (kxdrdproc_t)rpcb_dec_set, |
Chuck Lever | f8b761e | 2009-08-09 15:09:44 -0400 | [diff] [blame] | 1022 | .p_arglen = RPCB_getaddrargs_sz, |
| 1023 | .p_replen = RPCB_setres_sz, |
| 1024 | .p_statidx = RPCBPROC_SET, |
| 1025 | .p_timer = 0, |
| 1026 | .p_name = "SET", |
| 1027 | }, |
| 1028 | [RPCBPROC_UNSET] = { |
| 1029 | .p_proc = RPCBPROC_UNSET, |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 1030 | .p_encode = (kxdreproc_t)rpcb_enc_getaddr, |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 1031 | .p_decode = (kxdrdproc_t)rpcb_dec_set, |
Chuck Lever | f8b761e | 2009-08-09 15:09:44 -0400 | [diff] [blame] | 1032 | .p_arglen = RPCB_getaddrargs_sz, |
| 1033 | .p_replen = RPCB_setres_sz, |
| 1034 | .p_statidx = RPCBPROC_UNSET, |
| 1035 | .p_timer = 0, |
| 1036 | .p_name = "UNSET", |
| 1037 | }, |
| 1038 | [RPCBPROC_GETADDR] = { |
| 1039 | .p_proc = RPCBPROC_GETADDR, |
Chuck Lever | 9f06c71 | 2010-12-14 14:59:18 +0000 | [diff] [blame] | 1040 | .p_encode = (kxdreproc_t)rpcb_enc_getaddr, |
Chuck Lever | bf26955 | 2010-12-14 14:59:29 +0000 | [diff] [blame] | 1041 | .p_decode = (kxdrdproc_t)rpcb_dec_getaddr, |
Chuck Lever | f8b761e | 2009-08-09 15:09:44 -0400 | [diff] [blame] | 1042 | .p_arglen = RPCB_getaddrargs_sz, |
| 1043 | .p_replen = RPCB_getaddrres_sz, |
| 1044 | .p_statidx = RPCBPROC_GETADDR, |
| 1045 | .p_timer = 0, |
| 1046 | .p_name = "GETADDR", |
| 1047 | }, |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 1048 | }; |
| 1049 | |
| 1050 | static struct rpcb_info rpcb_next_version[] = { |
Chuck Lever | fc200e7 | 2008-06-25 17:24:31 -0400 | [diff] [blame] | 1051 | { |
| 1052 | .rpc_vers = RPCBVERS_2, |
| 1053 | .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT], |
| 1054 | }, |
| 1055 | { |
| 1056 | .rpc_proc = NULL, |
| 1057 | }, |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 1058 | }; |
| 1059 | |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 1060 | static struct rpcb_info rpcb_next_version6[] = { |
Chuck Lever | fc200e7 | 2008-06-25 17:24:31 -0400 | [diff] [blame] | 1061 | { |
| 1062 | .rpc_vers = RPCBVERS_4, |
Chuck Lever | 8842413 | 2008-06-25 17:24:47 -0400 | [diff] [blame] | 1063 | .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR], |
Chuck Lever | fc200e7 | 2008-06-25 17:24:31 -0400 | [diff] [blame] | 1064 | }, |
| 1065 | { |
| 1066 | .rpc_vers = RPCBVERS_3, |
| 1067 | .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR], |
| 1068 | }, |
Chuck Lever | fc200e7 | 2008-06-25 17:24:31 -0400 | [diff] [blame] | 1069 | { |
| 1070 | .rpc_proc = NULL, |
| 1071 | }, |
Chuck Lever | d5b6443 | 2007-08-06 11:57:18 -0400 | [diff] [blame] | 1072 | }; |
| 1073 | |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 1074 | static struct rpc_version rpcb_version2 = { |
Chuck Lever | fc200e7 | 2008-06-25 17:24:31 -0400 | [diff] [blame] | 1075 | .number = RPCBVERS_2, |
Chuck Lever | 1ac7c23 | 2010-12-14 14:59:09 +0000 | [diff] [blame] | 1076 | .nrprocs = ARRAY_SIZE(rpcb_procedures2), |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 1077 | .procs = rpcb_procedures2 |
| 1078 | }; |
| 1079 | |
| 1080 | static struct rpc_version rpcb_version3 = { |
Chuck Lever | fc200e7 | 2008-06-25 17:24:31 -0400 | [diff] [blame] | 1081 | .number = RPCBVERS_3, |
Chuck Lever | 1ac7c23 | 2010-12-14 14:59:09 +0000 | [diff] [blame] | 1082 | .nrprocs = ARRAY_SIZE(rpcb_procedures3), |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 1083 | .procs = rpcb_procedures3 |
| 1084 | }; |
| 1085 | |
| 1086 | static struct rpc_version rpcb_version4 = { |
Chuck Lever | fc200e7 | 2008-06-25 17:24:31 -0400 | [diff] [blame] | 1087 | .number = RPCBVERS_4, |
Chuck Lever | 1ac7c23 | 2010-12-14 14:59:09 +0000 | [diff] [blame] | 1088 | .nrprocs = ARRAY_SIZE(rpcb_procedures4), |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 1089 | .procs = rpcb_procedures4 |
| 1090 | }; |
| 1091 | |
| 1092 | static struct rpc_version *rpcb_version[] = { |
| 1093 | NULL, |
| 1094 | NULL, |
| 1095 | &rpcb_version2, |
| 1096 | &rpcb_version3, |
| 1097 | &rpcb_version4 |
| 1098 | }; |
| 1099 | |
| 1100 | static struct rpc_stat rpcb_stats; |
| 1101 | |
Adrian Bunk | 7f4adef | 2007-06-13 01:03:13 +0200 | [diff] [blame] | 1102 | static struct rpc_program rpcb_program = { |
Chuck Lever | a509050 | 2007-03-29 16:48:04 -0400 | [diff] [blame] | 1103 | .name = "rpcbind", |
| 1104 | .number = RPCBIND_PROGRAM, |
| 1105 | .nrvers = ARRAY_SIZE(rpcb_version), |
| 1106 | .version = rpcb_version, |
| 1107 | .stats = &rpcb_stats, |
| 1108 | }; |