SUNRPC: Clean up address type casts in rpcb_v4_register()
[linux-2.6] / net / sunrpc / rpcb_clnt.c
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
15 #include <linux/module.h>
16
17 #include <linux/types.h>
18 #include <linux/socket.h>
19 #include <linux/in.h>
20 #include <linux/in6.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <net/ipv6.h>
24
25 #include <linux/sunrpc/clnt.h>
26 #include <linux/sunrpc/sched.h>
27 #include <linux/sunrpc/xprtsock.h>
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
36 #define RPCBVERS_2              (2u)
37 #define RPCBVERS_3              (3u)
38 #define RPCBVERS_4              (4u)
39
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
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 *);
72 static void                     rpcb_map_release(void *data);
73 static struct rpc_program       rpcb_program;
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;
82         const char *            r_netid;
83         const char *            r_addr;
84         const char *            r_owner;
85
86         int                     r_status;
87 };
88
89 static struct rpc_procinfo rpcb_procedures2[];
90 static struct rpc_procinfo rpcb_procedures3[];
91 static struct rpc_procinfo rpcb_procedures4[];
92
93 struct rpcb_info {
94         u32                     rpc_vers;
95         struct rpc_procinfo *   rpc_proc;
96 };
97
98 static struct rpcb_info rpcb_next_version[];
99 static struct rpcb_info rpcb_next_version6[];
100
101 static const struct rpc_call_ops rpcb_getport_ops = {
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
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
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
144 static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
145                                     size_t salen, int proto, u32 version)
146 {
147         struct rpc_create_args args = {
148                 .protocol       = proto,
149                 .address        = srvaddr,
150                 .addrsize       = salen,
151                 .servername     = hostname,
152                 .program        = &rpcb_program,
153                 .version        = version,
154                 .authflavor     = RPC_AUTH_UNIX,
155                 .flags          = (RPC_CLNT_CREATE_NOPING |
156                                         RPC_CLNT_CREATE_NONPRIVPORT),
157         };
158
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
170         return rpc_create(&args);
171 }
172
173 static int rpcb_register_call(const u32 version, struct rpc_message *msg)
174 {
175         struct sockaddr *addr = (struct sockaddr *)&rpcb_inaddr_loopback;
176         size_t addrlen = sizeof(rpcb_inaddr_loopback);
177         struct rpc_clnt *rpcb_clnt;
178         int result, error = 0;
179
180         msg->rpc_resp = &result;
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
189         if (error < 0) {
190                 printk(KERN_WARNING "RPC: failed to contact local rpcbind "
191                                 "server (errno %d).\n", -error);
192                 return error;
193         }
194
195         if (!result)
196                 return -EACCES;
197         return 0;
198 }
199
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
204  * @prot: transport protocol to register
205  * @port: port value to register
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).
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  *
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).
231  */
232 int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port)
233 {
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 = {
241                 .rpc_argp       = &map,
242         };
243
244         dprintk("RPC:       %sregistering (%u, %u, %d, %u) with local "
245                         "rpcbind\n", (port ? "" : "un"),
246                         prog, vers, prot, port);
247
248         msg.rpc_proc = &rpcb_procedures2[RPCBPROC_UNSET];
249         if (port)
250                 msg.rpc_proc = &rpcb_procedures2[RPCBPROC_SET];
251
252         return rpcb_register_call(RPCBVERS_2, &msg);
253 }
254
255 /*
256  * Fill in AF_INET family-specific arguments to register
257  */
258 static int rpcb_register_netid4(const struct sockaddr *sap,
259                                 struct rpc_message *msg)
260 {
261         const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
262         struct rpcbind_args *map = msg->rpc_argp;
263         unsigned short port = ntohs(sin->sin_port);
264         char buf[32];
265
266         /* Construct AF_INET universal address */
267         snprintf(buf, sizeof(buf), "%pI4.%u.%u",
268                  &sin->sin_addr.s_addr, port >> 8, port & 0xff);
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
280         return rpcb_register_call(RPCBVERS_4, msg);
281 }
282
283 /*
284  * Fill in AF_INET6 family-specific arguments to register
285  */
286 static int rpcb_register_netid6(const struct sockaddr *sap,
287                                 struct rpc_message *msg)
288 {
289         const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sap;
290         struct rpcbind_args *map = msg->rpc_argp;
291         unsigned short port = ntohs(sin6->sin6_port);
292         char buf[64];
293
294         /* Construct AF_INET6 universal address */
295         if (ipv6_addr_any(&sin6->sin6_addr))
296                 snprintf(buf, sizeof(buf), "::.%u.%u",
297                                 port >> 8, port & 0xff);
298         else
299                 snprintf(buf, sizeof(buf), "%pI6.%u.%u",
300                          &sin6->sin6_addr, port >> 8, port & 0xff);
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
312         return rpcb_register_call(RPCBVERS_4, msg);
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
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).
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  *
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,
358                      const struct sockaddr *address, const char *netid)
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,
368         };
369
370         switch (address->sa_family) {
371         case AF_INET:
372                 return rpcb_register_netid4(address, &msg);
373         case AF_INET6:
374                 return rpcb_register_netid6(address, &msg);
375         }
376
377         return -EAFNOSUPPORT;
378 }
379
380 /**
381  * rpcb_getport_sync - obtain the port for an RPC service on a given host
382  * @sin: address of remote peer
383  * @prog: RPC program number to bind
384  * @vers: RPC version number to bind
385  * @prot: transport protocol to use to make this request
386  *
387  * Return value is the requested advertised port number,
388  * or a negative errno value.
389  *
390  * Called from outside the RPC client in a synchronous task context.
391  * Uses default timeout parameters specified by underlying transport.
392  *
393  * XXX: Needs to support IPv6
394  */
395 int rpcb_getport_sync(struct sockaddr_in *sin, u32 prog, u32 vers, int prot)
396 {
397         struct rpcbind_args map = {
398                 .r_prog         = prog,
399                 .r_vers         = vers,
400                 .r_prot         = prot,
401                 .r_port         = 0,
402         };
403         struct rpc_message msg = {
404                 .rpc_proc       = &rpcb_procedures2[RPCBPROC_GETPORT],
405                 .rpc_argp       = &map,
406                 .rpc_resp       = &map.r_port,
407         };
408         struct rpc_clnt *rpcb_clnt;
409         int status;
410
411         dprintk("RPC:       %s(%pI4, %u, %u, %d)\n",
412                 __func__, &sin->sin_addr.s_addr, prog, vers, prot);
413
414         rpcb_clnt = rpcb_create(NULL, (struct sockaddr *)sin,
415                                 sizeof(*sin), prot, RPCBVERS_2);
416         if (IS_ERR(rpcb_clnt))
417                 return PTR_ERR(rpcb_clnt);
418
419         status = rpc_call_sync(rpcb_clnt, &msg, 0);
420         rpc_shutdown_client(rpcb_clnt);
421
422         if (status >= 0) {
423                 if (map.r_port != 0)
424                         return map.r_port;
425                 status = -EACCES;
426         }
427         return status;
428 }
429 EXPORT_SYMBOL_GPL(rpcb_getport_sync);
430
431 static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt, struct rpcbind_args *map, struct rpc_procinfo *proc)
432 {
433         struct rpc_message msg = {
434                 .rpc_proc = proc,
435                 .rpc_argp = map,
436                 .rpc_resp = &map->r_port,
437         };
438         struct rpc_task_setup task_setup_data = {
439                 .rpc_client = rpcb_clnt,
440                 .rpc_message = &msg,
441                 .callback_ops = &rpcb_getport_ops,
442                 .callback_data = map,
443                 .flags = RPC_TASK_ASYNC,
444         };
445
446         return rpc_run_task(&task_setup_data);
447 }
448
449 /*
450  * In the case where rpc clients have been cloned, we want to make
451  * sure that we use the program number/version etc of the actual
452  * owner of the xprt. To do so, we walk back up the tree of parents
453  * to find whoever created the transport and/or whoever has the
454  * autobind flag set.
455  */
456 static struct rpc_clnt *rpcb_find_transport_owner(struct rpc_clnt *clnt)
457 {
458         struct rpc_clnt *parent = clnt->cl_parent;
459
460         while (parent != clnt) {
461                 if (parent->cl_xprt != clnt->cl_xprt)
462                         break;
463                 if (clnt->cl_autobind)
464                         break;
465                 clnt = parent;
466                 parent = parent->cl_parent;
467         }
468         return clnt;
469 }
470
471 /**
472  * rpcb_getport_async - obtain the port for a given RPC service on a given host
473  * @task: task that is waiting for portmapper request
474  *
475  * This one can be called for an ongoing RPC request, and can be used in
476  * an async (rpciod) context.
477  */
478 void rpcb_getport_async(struct rpc_task *task)
479 {
480         struct rpc_clnt *clnt;
481         struct rpc_procinfo *proc;
482         u32 bind_version;
483         struct rpc_xprt *xprt;
484         struct rpc_clnt *rpcb_clnt;
485         static struct rpcbind_args *map;
486         struct rpc_task *child;
487         struct sockaddr_storage addr;
488         struct sockaddr *sap = (struct sockaddr *)&addr;
489         size_t salen;
490         int status;
491
492         clnt = rpcb_find_transport_owner(task->tk_client);
493         xprt = clnt->cl_xprt;
494
495         dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
496                 task->tk_pid, __func__,
497                 clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot);
498
499         /* Put self on the wait queue to ensure we get notified if
500          * some other task is already attempting to bind the port */
501         rpc_sleep_on(&xprt->binding, task, NULL);
502
503         if (xprt_test_and_set_binding(xprt)) {
504                 dprintk("RPC: %5u %s: waiting for another binder\n",
505                         task->tk_pid, __func__);
506                 return;
507         }
508
509         /* Someone else may have bound if we slept */
510         if (xprt_bound(xprt)) {
511                 status = 0;
512                 dprintk("RPC: %5u %s: already bound\n",
513                         task->tk_pid, __func__);
514                 goto bailout_nofree;
515         }
516
517         salen = rpc_peeraddr(clnt, sap, sizeof(addr));
518
519         /* Don't ever use rpcbind v2 for AF_INET6 requests */
520         switch (sap->sa_family) {
521         case AF_INET:
522                 proc = rpcb_next_version[xprt->bind_index].rpc_proc;
523                 bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
524                 break;
525         case AF_INET6:
526                 proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
527                 bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
528                 break;
529         default:
530                 status = -EAFNOSUPPORT;
531                 dprintk("RPC: %5u %s: bad address family\n",
532                                 task->tk_pid, __func__);
533                 goto bailout_nofree;
534         }
535         if (proc == NULL) {
536                 xprt->bind_index = 0;
537                 status = -EPFNOSUPPORT;
538                 dprintk("RPC: %5u %s: no more getport versions available\n",
539                         task->tk_pid, __func__);
540                 goto bailout_nofree;
541         }
542
543         dprintk("RPC: %5u %s: trying rpcbind version %u\n",
544                 task->tk_pid, __func__, bind_version);
545
546         rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot,
547                                 bind_version);
548         if (IS_ERR(rpcb_clnt)) {
549                 status = PTR_ERR(rpcb_clnt);
550                 dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
551                         task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
552                 goto bailout_nofree;
553         }
554
555         map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
556         if (!map) {
557                 status = -ENOMEM;
558                 dprintk("RPC: %5u %s: no memory available\n",
559                         task->tk_pid, __func__);
560                 goto bailout_release_client;
561         }
562         map->r_prog = clnt->cl_prog;
563         map->r_vers = clnt->cl_vers;
564         map->r_prot = xprt->prot;
565         map->r_port = 0;
566         map->r_xprt = xprt_get(xprt);
567         map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
568         map->r_addr = rpc_peeraddr2str(rpcb_clnt, RPC_DISPLAY_UNIVERSAL_ADDR);
569         map->r_owner = RPCB_OWNER_STRING;       /* ignored for GETADDR */
570         map->r_status = -EIO;
571
572         child = rpcb_call_async(rpcb_clnt, map, proc);
573         rpc_release_client(rpcb_clnt);
574         if (IS_ERR(child)) {
575                 /* rpcb_map_release() has freed the arguments */
576                 dprintk("RPC: %5u %s: rpc_run_task failed\n",
577                         task->tk_pid, __func__);
578                 return;
579         }
580
581         xprt->stat.bind_count++;
582         rpc_put_task(child);
583         return;
584
585 bailout_release_client:
586         rpc_release_client(rpcb_clnt);
587 bailout_nofree:
588         rpcb_wake_rpcbind_waiters(xprt, status);
589         task->tk_status = status;
590 }
591 EXPORT_SYMBOL_GPL(rpcb_getport_async);
592
593 /*
594  * Rpcbind child task calls this callback via tk_exit.
595  */
596 static void rpcb_getport_done(struct rpc_task *child, void *data)
597 {
598         struct rpcbind_args *map = data;
599         struct rpc_xprt *xprt = map->r_xprt;
600         int status = child->tk_status;
601
602         /* Garbage reply: retry with a lesser rpcbind version */
603         if (status == -EIO)
604                 status = -EPROTONOSUPPORT;
605
606         /* rpcbind server doesn't support this rpcbind protocol version */
607         if (status == -EPROTONOSUPPORT)
608                 xprt->bind_index++;
609
610         if (status < 0) {
611                 /* rpcbind server not available on remote host? */
612                 xprt->ops->set_port(xprt, 0);
613         } else if (map->r_port == 0) {
614                 /* Requested RPC service wasn't registered on remote host */
615                 xprt->ops->set_port(xprt, 0);
616                 status = -EACCES;
617         } else {
618                 /* Succeeded */
619                 xprt->ops->set_port(xprt, map->r_port);
620                 xprt_set_bound(xprt);
621                 status = 0;
622         }
623
624         dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
625                         child->tk_pid, status, map->r_port);
626
627         map->r_status = status;
628 }
629
630 /*
631  * XDR functions for rpcbind
632  */
633
634 static int rpcb_encode_mapping(struct rpc_rqst *req, __be32 *p,
635                                struct rpcbind_args *rpcb)
636 {
637         dprintk("RPC:       encoding rpcb request (%u, %u, %d, %u)\n",
638                         rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
639         *p++ = htonl(rpcb->r_prog);
640         *p++ = htonl(rpcb->r_vers);
641         *p++ = htonl(rpcb->r_prot);
642         *p++ = htonl(rpcb->r_port);
643
644         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
645         return 0;
646 }
647
648 static int rpcb_decode_getport(struct rpc_rqst *req, __be32 *p,
649                                unsigned short *portp)
650 {
651         *portp = (unsigned short) ntohl(*p++);
652         dprintk("RPC:       rpcb getport result: %u\n",
653                         *portp);
654         return 0;
655 }
656
657 static int rpcb_decode_set(struct rpc_rqst *req, __be32 *p,
658                            unsigned int *boolp)
659 {
660         *boolp = (unsigned int) ntohl(*p++);
661         dprintk("RPC:       rpcb set/unset call %s\n",
662                         (*boolp ? "succeeded" : "failed"));
663         return 0;
664 }
665
666 static int rpcb_encode_getaddr(struct rpc_rqst *req, __be32 *p,
667                                struct rpcbind_args *rpcb)
668 {
669         dprintk("RPC:       encoding rpcb request (%u, %u, %s)\n",
670                         rpcb->r_prog, rpcb->r_vers, rpcb->r_addr);
671         *p++ = htonl(rpcb->r_prog);
672         *p++ = htonl(rpcb->r_vers);
673
674         p = xdr_encode_string(p, rpcb->r_netid);
675         p = xdr_encode_string(p, rpcb->r_addr);
676         p = xdr_encode_string(p, rpcb->r_owner);
677
678         req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
679
680         return 0;
681 }
682
683 static int rpcb_decode_getaddr(struct rpc_rqst *req, __be32 *p,
684                                unsigned short *portp)
685 {
686         char *addr;
687         u32 addr_len;
688         int c, i, f, first, val;
689
690         *portp = 0;
691         addr_len = ntohl(*p++);
692
693         if (addr_len == 0) {
694                 dprintk("RPC:       rpcb_decode_getaddr: "
695                                         "service is not registered\n");
696                 return 0;
697         }
698
699         /*
700          * Simple sanity check.
701          */
702         if (addr_len > RPCBIND_MAXUADDRLEN)
703                 goto out_err;
704
705         /*
706          * Start at the end and walk backwards until the first dot
707          * is encountered.  When the second dot is found, we have
708          * both parts of the port number.
709          */
710         addr = (char *)p;
711         val = 0;
712         first = 1;
713         f = 1;
714         for (i = addr_len - 1; i > 0; i--) {
715                 c = addr[i];
716                 if (c >= '0' && c <= '9') {
717                         val += (c - '0') * f;
718                         f *= 10;
719                 } else if (c == '.') {
720                         if (first) {
721                                 *portp = val;
722                                 val = first = 0;
723                                 f = 1;
724                         } else {
725                                 *portp |= (val << 8);
726                                 break;
727                         }
728                 }
729         }
730
731         /*
732          * Simple sanity check.  If we never saw a dot in the reply,
733          * then this was probably just garbage.
734          */
735         if (first)
736                 goto out_err;
737
738         dprintk("RPC:       rpcb_decode_getaddr port=%u\n", *portp);
739         return 0;
740
741 out_err:
742         dprintk("RPC:       rpcbind server returned malformed reply\n");
743         return -EIO;
744 }
745
746 #define RPCB_program_sz         (1u)
747 #define RPCB_version_sz         (1u)
748 #define RPCB_protocol_sz        (1u)
749 #define RPCB_port_sz            (1u)
750 #define RPCB_boolean_sz         (1u)
751
752 #define RPCB_netid_sz           (1+XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
753 #define RPCB_addr_sz            (1+XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
754 #define RPCB_ownerstring_sz     (1+XDR_QUADLEN(RPCB_MAXOWNERLEN))
755
756 #define RPCB_mappingargs_sz     RPCB_program_sz+RPCB_version_sz+        \
757                                 RPCB_protocol_sz+RPCB_port_sz
758 #define RPCB_getaddrargs_sz     RPCB_program_sz+RPCB_version_sz+        \
759                                 RPCB_netid_sz+RPCB_addr_sz+             \
760                                 RPCB_ownerstring_sz
761
762 #define RPCB_setres_sz          RPCB_boolean_sz
763 #define RPCB_getportres_sz      RPCB_port_sz
764
765 /*
766  * Note that RFC 1833 does not put any size restrictions on the
767  * address string returned by the remote rpcbind database.
768  */
769 #define RPCB_getaddrres_sz      RPCB_addr_sz
770
771 #define PROC(proc, argtype, restype)                                    \
772         [RPCBPROC_##proc] = {                                           \
773                 .p_proc         = RPCBPROC_##proc,                      \
774                 .p_encode       = (kxdrproc_t) rpcb_encode_##argtype,   \
775                 .p_decode       = (kxdrproc_t) rpcb_decode_##restype,   \
776                 .p_arglen       = RPCB_##argtype##args_sz,              \
777                 .p_replen       = RPCB_##restype##res_sz,               \
778                 .p_statidx      = RPCBPROC_##proc,                      \
779                 .p_timer        = 0,                                    \
780                 .p_name         = #proc,                                \
781         }
782
783 /*
784  * Not all rpcbind procedures described in RFC 1833 are implemented
785  * since the Linux kernel RPC code requires only these.
786  */
787 static struct rpc_procinfo rpcb_procedures2[] = {
788         PROC(SET,               mapping,        set),
789         PROC(UNSET,             mapping,        set),
790         PROC(GETPORT,           mapping,        getport),
791 };
792
793 static struct rpc_procinfo rpcb_procedures3[] = {
794         PROC(SET,               getaddr,        set),
795         PROC(UNSET,             getaddr,        set),
796         PROC(GETADDR,           getaddr,        getaddr),
797 };
798
799 static struct rpc_procinfo rpcb_procedures4[] = {
800         PROC(SET,               getaddr,        set),
801         PROC(UNSET,             getaddr,        set),
802         PROC(GETADDR,           getaddr,        getaddr),
803         PROC(GETVERSADDR,       getaddr,        getaddr),
804 };
805
806 static struct rpcb_info rpcb_next_version[] = {
807         {
808                 .rpc_vers       = RPCBVERS_2,
809                 .rpc_proc       = &rpcb_procedures2[RPCBPROC_GETPORT],
810         },
811         {
812                 .rpc_proc       = NULL,
813         },
814 };
815
816 static struct rpcb_info rpcb_next_version6[] = {
817         {
818                 .rpc_vers       = RPCBVERS_4,
819                 .rpc_proc       = &rpcb_procedures4[RPCBPROC_GETADDR],
820         },
821         {
822                 .rpc_vers       = RPCBVERS_3,
823                 .rpc_proc       = &rpcb_procedures3[RPCBPROC_GETADDR],
824         },
825         {
826                 .rpc_proc       = NULL,
827         },
828 };
829
830 static struct rpc_version rpcb_version2 = {
831         .number         = RPCBVERS_2,
832         .nrprocs        = RPCB_HIGHPROC_2,
833         .procs          = rpcb_procedures2
834 };
835
836 static struct rpc_version rpcb_version3 = {
837         .number         = RPCBVERS_3,
838         .nrprocs        = RPCB_HIGHPROC_3,
839         .procs          = rpcb_procedures3
840 };
841
842 static struct rpc_version rpcb_version4 = {
843         .number         = RPCBVERS_4,
844         .nrprocs        = RPCB_HIGHPROC_4,
845         .procs          = rpcb_procedures4
846 };
847
848 static struct rpc_version *rpcb_version[] = {
849         NULL,
850         NULL,
851         &rpcb_version2,
852         &rpcb_version3,
853         &rpcb_version4
854 };
855
856 static struct rpc_stat rpcb_stats;
857
858 static struct rpc_program rpcb_program = {
859         .name           = "rpcbind",
860         .number         = RPCBIND_PROGRAM,
861         .nrvers         = ARRAY_SIZE(rpcb_version),
862         .version        = rpcb_version,
863         .stats          = &rpcb_stats,
864 };