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