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