blob: c652e4cc9fe97c987dbb9eb1eef3e1c639ed1af4 [file] [log] [blame]
Chuck Levera5090502007-03-29 16:48:04 -04001/*
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 Levercce63cd2007-07-01 12:13:12 -040015#include <linux/module.h>
16
Chuck Levera5090502007-03-29 16:48:04 -040017#include <linux/types.h>
18#include <linux/socket.h>
Chuck Leverd5b64432007-08-06 11:57:18 -040019#include <linux/in.h>
20#include <linux/in6.h>
Chuck Levera5090502007-03-29 16:48:04 -040021#include <linux/kernel.h>
22#include <linux/errno.h>
Chuck Leverc5266112009-12-03 15:58:56 -050023#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Chuck Lever9d548b92008-09-15 16:27:30 -050025#include <net/ipv6.h>
Chuck Levera5090502007-03-29 16:48:04 -040026
27#include <linux/sunrpc/clnt.h>
28#include <linux/sunrpc/sched.h>
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -040029#include <linux/sunrpc/xprtsock.h>
Chuck Levera5090502007-03-29 16:48:04 -040030
31#ifdef RPC_DEBUG
32# define RPCDBG_FACILITY RPCDBG_BIND
33#endif
34
35#define RPCBIND_PROGRAM (100000u)
36#define RPCBIND_PORT (111u)
37
Chuck Leverfc200e72008-06-25 17:24:31 -040038#define RPCBVERS_2 (2u)
39#define RPCBVERS_3 (3u)
40#define RPCBVERS_4 (4u)
41
Chuck Levera5090502007-03-29 16:48:04 -040042enum {
43 RPCBPROC_NULL,
44 RPCBPROC_SET,
45 RPCBPROC_UNSET,
46 RPCBPROC_GETPORT,
47 RPCBPROC_GETADDR = 3, /* alias for GETPORT */
48 RPCBPROC_DUMP,
49 RPCBPROC_CALLIT,
50 RPCBPROC_BCAST = 5, /* alias for CALLIT */
51 RPCBPROC_GETTIME,
52 RPCBPROC_UADDR2TADDR,
53 RPCBPROC_TADDR2UADDR,
54 RPCBPROC_GETVERSADDR,
55 RPCBPROC_INDIRECT,
56 RPCBPROC_GETADDRLIST,
57 RPCBPROC_GETSTAT,
58};
59
Chuck Levera5090502007-03-29 16:48:04 -040060/*
Chuck Levera5090502007-03-29 16:48:04 -040061 * r_owner
62 *
63 * The "owner" is allowed to unset a service in the rpcbind database.
Chuck Lever126e4bc2009-03-18 20:47:14 -040064 *
65 * For AF_LOCAL SET/UNSET requests, rpcbind treats this string as a
66 * UID which it maps to a local user name via a password lookup.
67 * In all other cases it is ignored.
68 *
69 * For SET/UNSET requests, user space provides a value, even for
70 * network requests, and GETADDR uses an empty string. We follow
71 * those precedents here.
Chuck Levera5090502007-03-29 16:48:04 -040072 */
Chuck Lever126e4bc2009-03-18 20:47:14 -040073#define RPCB_OWNER_STRING "0"
Chuck Levera5090502007-03-29 16:48:04 -040074#define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
75
Chuck Lever0b10bf52009-08-09 15:09:33 -040076/*
77 * XDR data type sizes
78 */
79#define RPCB_program_sz (1)
80#define RPCB_version_sz (1)
81#define RPCB_protocol_sz (1)
82#define RPCB_port_sz (1)
83#define RPCB_boolean_sz (1)
84
85#define RPCB_netid_sz (1 + XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
86#define RPCB_addr_sz (1 + XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
87#define RPCB_ownerstring_sz (1 + XDR_QUADLEN(RPCB_MAXOWNERLEN))
88
89/*
90 * XDR argument and result sizes
91 */
92#define RPCB_mappingargs_sz (RPCB_program_sz + RPCB_version_sz + \
93 RPCB_protocol_sz + RPCB_port_sz)
94#define RPCB_getaddrargs_sz (RPCB_program_sz + RPCB_version_sz + \
95 RPCB_netid_sz + RPCB_addr_sz + \
96 RPCB_ownerstring_sz)
97
98#define RPCB_getportres_sz RPCB_port_sz
99#define RPCB_setres_sz RPCB_boolean_sz
100
101/*
102 * Note that RFC 1833 does not put any size restrictions on the
103 * address string returned by the remote rpcbind database.
104 */
105#define RPCB_getaddrres_sz RPCB_addr_sz
106
Chuck Levera5090502007-03-29 16:48:04 -0400107static void rpcb_getport_done(struct rpc_task *, void *);
Trond Myklebust381ba742008-07-07 12:18:53 -0400108static void rpcb_map_release(void *data);
Adrian Bunk7f4adef2007-06-13 01:03:13 +0200109static struct rpc_program rpcb_program;
Chuck Levera5090502007-03-29 16:48:04 -0400110
Chuck Leverc5266112009-12-03 15:58:56 -0500111static struct rpc_clnt * rpcb_local_clnt;
112static struct rpc_clnt * rpcb_local_clnt4;
113
Chuck Levera5090502007-03-29 16:48:04 -0400114struct rpcbind_args {
115 struct rpc_xprt * r_xprt;
116
117 u32 r_prog;
118 u32 r_vers;
119 u32 r_prot;
120 unsigned short r_port;
Trond Myklebust86d61d82008-01-07 21:16:56 -0500121 const char * r_netid;
122 const char * r_addr;
123 const char * r_owner;
Trond Myklebust381ba742008-07-07 12:18:53 -0400124
125 int r_status;
Chuck Levera5090502007-03-29 16:48:04 -0400126};
127
128static struct rpc_procinfo rpcb_procedures2[];
129static struct rpc_procinfo rpcb_procedures3[];
Chuck Leverc2e1b092008-07-14 16:03:30 -0400130static struct rpc_procinfo rpcb_procedures4[];
Chuck Levera5090502007-03-29 16:48:04 -0400131
Chuck Leverd5b64432007-08-06 11:57:18 -0400132struct rpcb_info {
Chuck Leverfc200e72008-06-25 17:24:31 -0400133 u32 rpc_vers;
Chuck Levera5090502007-03-29 16:48:04 -0400134 struct rpc_procinfo * rpc_proc;
Chuck Leverd5b64432007-08-06 11:57:18 -0400135};
136
137static struct rpcb_info rpcb_next_version[];
138static struct rpcb_info rpcb_next_version6[];
Chuck Levera5090502007-03-29 16:48:04 -0400139
Chuck Levera5090502007-03-29 16:48:04 -0400140static const struct rpc_call_ops rpcb_getport_ops = {
Chuck Levera5090502007-03-29 16:48:04 -0400141 .rpc_call_done = rpcb_getport_done,
142 .rpc_release = rpcb_map_release,
143};
144
145static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status)
146{
147 xprt_clear_binding(xprt);
148 rpc_wake_up_status(&xprt->binding, status);
149}
150
Trond Myklebust381ba742008-07-07 12:18:53 -0400151static void rpcb_map_release(void *data)
152{
153 struct rpcbind_args *map = data;
154
155 rpcb_wake_rpcbind_waiters(map->r_xprt, map->r_status);
156 xprt_put(map->r_xprt);
Chuck Leverba809132009-08-09 15:09:35 -0400157 kfree(map->r_addr);
Trond Myklebust381ba742008-07-07 12:18:53 -0400158 kfree(map);
159}
160
Chuck Levercc5598b2008-07-14 16:03:27 -0400161static const struct sockaddr_in rpcb_inaddr_loopback = {
162 .sin_family = AF_INET,
163 .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
164 .sin_port = htons(RPCBIND_PORT),
165};
166
Chuck Leverc5266112009-12-03 15:58:56 -0500167static DEFINE_MUTEX(rpcb_create_local_mutex);
168
169/*
170 * Returns zero on success, otherwise a negative errno value
171 * is returned.
172 */
173static int rpcb_create_local(void)
Chuck Levercc5598b2008-07-14 16:03:27 -0400174{
175 struct rpc_create_args args = {
Pavel Emelyanovc653ce32010-09-29 16:04:45 +0400176 .net = &init_net,
Chuck Lever2a76b3b2009-12-03 15:58:56 -0500177 .protocol = XPRT_TRANSPORT_TCP,
Chuck Lever5a462112009-12-03 15:58:56 -0500178 .address = (struct sockaddr *)&rpcb_inaddr_loopback,
179 .addrsize = sizeof(rpcb_inaddr_loopback),
Chuck Levercc5598b2008-07-14 16:03:27 -0400180 .servername = "localhost",
181 .program = &rpcb_program,
Chuck Leverc5266112009-12-03 15:58:56 -0500182 .version = RPCBVERS_2,
Chuck Levercc5598b2008-07-14 16:03:27 -0400183 .authflavor = RPC_AUTH_UNIX,
184 .flags = RPC_CLNT_CREATE_NOPING,
185 };
Chuck Leverc5266112009-12-03 15:58:56 -0500186 struct rpc_clnt *clnt, *clnt4;
187 int result = 0;
Chuck Levercc5598b2008-07-14 16:03:27 -0400188
Chuck Leverc5266112009-12-03 15:58:56 -0500189 if (rpcb_local_clnt)
190 return result;
191
192 mutex_lock(&rpcb_create_local_mutex);
193 if (rpcb_local_clnt)
194 goto out;
195
196 clnt = rpc_create(&args);
197 if (IS_ERR(clnt)) {
198 dprintk("RPC: failed to create local rpcbind "
199 "client (errno %ld).\n", PTR_ERR(clnt));
200 result = -PTR_ERR(clnt);
201 goto out;
202 }
203
204 /*
205 * This results in an RPC ping. On systems running portmapper,
206 * the v4 ping will fail. Proceed anyway, but disallow rpcb
207 * v4 upcalls.
208 */
209 clnt4 = rpc_bind_new_program(clnt, &rpcb_program, RPCBVERS_4);
210 if (IS_ERR(clnt4)) {
Chuck Lever38359352010-09-21 16:55:48 -0400211 dprintk("RPC: failed to bind second program to "
212 "rpcbind v4 client (errno %ld).\n",
213 PTR_ERR(clnt4));
Chuck Leverc5266112009-12-03 15:58:56 -0500214 clnt4 = NULL;
215 }
216
217 rpcb_local_clnt = clnt;
218 rpcb_local_clnt4 = clnt4;
219
220out:
221 mutex_unlock(&rpcb_create_local_mutex);
222 return result;
Chuck Levercc5598b2008-07-14 16:03:27 -0400223}
224
Chuck Levera5090502007-03-29 16:48:04 -0400225static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
Chuck Lever423d8b02008-07-14 16:03:28 -0400226 size_t salen, int proto, u32 version)
Chuck Levera5090502007-03-29 16:48:04 -0400227{
228 struct rpc_create_args args = {
Pavel Emelyanovc653ce32010-09-29 16:04:45 +0400229 .net = &init_net,
Chuck Levera5090502007-03-29 16:48:04 -0400230 .protocol = proto,
231 .address = srvaddr,
Chuck Lever9f6ad262007-12-10 14:56:31 -0500232 .addrsize = salen,
Chuck Levera5090502007-03-29 16:48:04 -0400233 .servername = hostname,
234 .program = &rpcb_program,
235 .version = version,
236 .authflavor = RPC_AUTH_UNIX,
Chuck Lever423d8b02008-07-14 16:03:28 -0400237 .flags = (RPC_CLNT_CREATE_NOPING |
238 RPC_CLNT_CREATE_NONPRIVPORT),
Chuck Levera5090502007-03-29 16:48:04 -0400239 };
240
Chuck Leverd5b64432007-08-06 11:57:18 -0400241 switch (srvaddr->sa_family) {
242 case AF_INET:
243 ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
244 break;
245 case AF_INET6:
246 ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
247 break;
248 default:
Pavel Emelyanovc636b572010-10-06 13:45:56 +0400249 return ERR_PTR(-EAFNOSUPPORT);
Chuck Leverd5b64432007-08-06 11:57:18 -0400250 }
251
Chuck Levera5090502007-03-29 16:48:04 -0400252 return rpc_create(&args);
253}
254
Chuck Leverc5266112009-12-03 15:58:56 -0500255static int rpcb_register_call(struct rpc_clnt *clnt, struct rpc_message *msg)
Chuck Leverbabe80e2008-07-14 16:03:29 -0400256{
Chuck Lever14aeb212008-08-18 19:34:00 -0400257 int result, error = 0;
Chuck Leverbabe80e2008-07-14 16:03:29 -0400258
Chuck Lever14aeb212008-08-18 19:34:00 -0400259 msg->rpc_resp = &result;
Chuck Leverbabe80e2008-07-14 16:03:29 -0400260
Chuck Lever2a76b3b2009-12-03 15:58:56 -0500261 error = rpc_call_sync(clnt, msg, RPC_TASK_SOFTCONN);
Chuck Lever14aeb212008-08-18 19:34:00 -0400262 if (error < 0) {
Chuck Lever363f7242009-03-18 20:47:44 -0400263 dprintk("RPC: failed to contact local rpcbind "
Chuck Leverbabe80e2008-07-14 16:03:29 -0400264 "server (errno %d).\n", -error);
Chuck Lever14aeb212008-08-18 19:34:00 -0400265 return error;
266 }
Chuck Leverbabe80e2008-07-14 16:03:29 -0400267
Chuck Leverdb820d62008-09-25 11:57:05 -0400268 if (!result)
Chuck Lever14aeb212008-08-18 19:34:00 -0400269 return -EACCES;
Chuck Lever14aeb212008-08-18 19:34:00 -0400270 return 0;
Chuck Leverbabe80e2008-07-14 16:03:29 -0400271}
272
Chuck Levera5090502007-03-29 16:48:04 -0400273/**
274 * rpcb_register - set or unset a port registration with the local rpcbind svc
275 * @prog: RPC program number to bind
276 * @vers: RPC version number to bind
Chuck Leverc2e1b092008-07-14 16:03:30 -0400277 * @prot: transport protocol to register
Chuck Levera5090502007-03-29 16:48:04 -0400278 * @port: port value to register
Chuck Lever14aeb212008-08-18 19:34:00 -0400279 *
280 * Returns zero if the registration request was dispatched successfully
281 * and the rpcbind daemon returned success. Otherwise, returns an errno
282 * value that reflects the nature of the error (request could not be
283 * dispatched, timed out, or rpcbind returned an error).
Chuck Levera5090502007-03-29 16:48:04 -0400284 *
Chuck Leverc2e1b092008-07-14 16:03:30 -0400285 * RPC services invoke this function to advertise their contact
286 * information via the system's rpcbind daemon. RPC services
287 * invoke this function once for each [program, version, transport]
288 * tuple they wish to advertise.
Chuck Levera5090502007-03-29 16:48:04 -0400289 *
Chuck Leverc2e1b092008-07-14 16:03:30 -0400290 * Callers may also unregister RPC services that are no longer
291 * available by setting the passed-in port to zero. This removes
292 * all registered transports for [program, version] from the local
293 * rpcbind database.
294 *
Chuck Leverc2e1b092008-07-14 16:03:30 -0400295 * This function uses rpcbind protocol version 2 to contact the
296 * local rpcbind daemon.
297 *
298 * Registration works over both AF_INET and AF_INET6, and services
299 * registered via this function are advertised as available for any
300 * address. If the local rpcbind daemon is listening on AF_INET6,
301 * services registered via this function will be advertised on
302 * IN6ADDR_ANY (ie available for all AF_INET and AF_INET6
303 * addresses).
Chuck Levera5090502007-03-29 16:48:04 -0400304 */
Chuck Lever14aeb212008-08-18 19:34:00 -0400305int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port)
Chuck Levera5090502007-03-29 16:48:04 -0400306{
Chuck Levera5090502007-03-29 16:48:04 -0400307 struct rpcbind_args map = {
308 .r_prog = prog,
309 .r_vers = vers,
310 .r_prot = prot,
311 .r_port = port,
312 };
313 struct rpc_message msg = {
Chuck Levera5090502007-03-29 16:48:04 -0400314 .rpc_argp = &map,
Chuck Levera5090502007-03-29 16:48:04 -0400315 };
Chuck Leverc5266112009-12-03 15:58:56 -0500316 int error;
317
318 error = rpcb_create_local();
319 if (error)
320 return error;
Chuck Levera5090502007-03-29 16:48:04 -0400321
322 dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
323 "rpcbind\n", (port ? "" : "un"),
324 prog, vers, prot, port);
325
Chuck Leverbabe80e2008-07-14 16:03:29 -0400326 msg.rpc_proc = &rpcb_procedures2[RPCBPROC_UNSET];
327 if (port)
328 msg.rpc_proc = &rpcb_procedures2[RPCBPROC_SET];
329
Chuck Leverc5266112009-12-03 15:58:56 -0500330 return rpcb_register_call(rpcb_local_clnt, &msg);
Chuck Levera5090502007-03-29 16:48:04 -0400331}
332
Chuck Leverc2e1b092008-07-14 16:03:30 -0400333/*
334 * Fill in AF_INET family-specific arguments to register
335 */
Chuck Lever1673d0d2009-03-18 20:47:21 -0400336static int rpcb_register_inet4(const struct sockaddr *sap,
337 struct rpc_message *msg)
Chuck Leverc2e1b092008-07-14 16:03:30 -0400338{
Chuck Lever3aba4552009-03-18 20:47:06 -0400339 const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
Chuck Leverc2e1b092008-07-14 16:03:30 -0400340 struct rpcbind_args *map = msg->rpc_argp;
Chuck Lever3aba4552009-03-18 20:47:06 -0400341 unsigned short port = ntohs(sin->sin_port);
Chuck Leverba809132009-08-09 15:09:35 -0400342 int result;
Chuck Leverc2e1b092008-07-14 16:03:30 -0400343
Chuck Leverba809132009-08-09 15:09:35 -0400344 map->r_addr = rpc_sockaddr2uaddr(sap);
Chuck Leverc2e1b092008-07-14 16:03:30 -0400345
346 dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
347 "local rpcbind\n", (port ? "" : "un"),
348 map->r_prog, map->r_vers,
349 map->r_addr, map->r_netid);
350
351 msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
352 if (port)
353 msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
354
Chuck Leverc5266112009-12-03 15:58:56 -0500355 result = rpcb_register_call(rpcb_local_clnt4, msg);
Chuck Leverba809132009-08-09 15:09:35 -0400356 kfree(map->r_addr);
357 return result;
Chuck Leverc2e1b092008-07-14 16:03:30 -0400358}
359
360/*
361 * Fill in AF_INET6 family-specific arguments to register
362 */
Chuck Lever1673d0d2009-03-18 20:47:21 -0400363static int rpcb_register_inet6(const struct sockaddr *sap,
364 struct rpc_message *msg)
Chuck Leverc2e1b092008-07-14 16:03:30 -0400365{
Chuck Lever3aba4552009-03-18 20:47:06 -0400366 const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sap;
Chuck Leverc2e1b092008-07-14 16:03:30 -0400367 struct rpcbind_args *map = msg->rpc_argp;
Chuck Lever3aba4552009-03-18 20:47:06 -0400368 unsigned short port = ntohs(sin6->sin6_port);
Chuck Leverba809132009-08-09 15:09:35 -0400369 int result;
Chuck Leverc2e1b092008-07-14 16:03:30 -0400370
Chuck Leverba809132009-08-09 15:09:35 -0400371 map->r_addr = rpc_sockaddr2uaddr(sap);
Chuck Leverc2e1b092008-07-14 16:03:30 -0400372
373 dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
374 "local rpcbind\n", (port ? "" : "un"),
375 map->r_prog, map->r_vers,
376 map->r_addr, map->r_netid);
377
378 msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
379 if (port)
380 msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
381
Chuck Leverc5266112009-12-03 15:58:56 -0500382 result = rpcb_register_call(rpcb_local_clnt4, msg);
Chuck Leverba809132009-08-09 15:09:35 -0400383 kfree(map->r_addr);
384 return result;
Chuck Leverc2e1b092008-07-14 16:03:30 -0400385}
386
Chuck Lever1673d0d2009-03-18 20:47:21 -0400387static int rpcb_unregister_all_protofamilies(struct rpc_message *msg)
388{
389 struct rpcbind_args *map = msg->rpc_argp;
390
391 dprintk("RPC: unregistering [%u, %u, '%s'] with "
392 "local rpcbind\n",
393 map->r_prog, map->r_vers, map->r_netid);
394
395 map->r_addr = "";
396 msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
397
Chuck Leverc5266112009-12-03 15:58:56 -0500398 return rpcb_register_call(rpcb_local_clnt4, msg);
Chuck Lever1673d0d2009-03-18 20:47:21 -0400399}
400
Chuck Leverc2e1b092008-07-14 16:03:30 -0400401/**
402 * rpcb_v4_register - set or unset a port registration with the local rpcbind
403 * @program: RPC program number of service to (un)register
404 * @version: RPC version number of service to (un)register
405 * @address: address family, IP address, and port to (un)register
406 * @netid: netid of transport protocol to (un)register
Chuck Lever14aeb212008-08-18 19:34:00 -0400407 *
408 * Returns zero if the registration request was dispatched successfully
409 * and the rpcbind daemon returned success. Otherwise, returns an errno
410 * value that reflects the nature of the error (request could not be
411 * dispatched, timed out, or rpcbind returned an error).
Chuck Leverc2e1b092008-07-14 16:03:30 -0400412 *
413 * RPC services invoke this function to advertise their contact
414 * information via the system's rpcbind daemon. RPC services
415 * invoke this function once for each [program, version, address,
416 * netid] tuple they wish to advertise.
417 *
Chuck Lever1673d0d2009-03-18 20:47:21 -0400418 * Callers may also unregister RPC services that are registered at a
419 * specific address by setting the port number in @address to zero.
420 * They may unregister all registered protocol families at once for
421 * a service by passing a NULL @address argument. If @netid is ""
422 * then all netids for [program, version, address] are unregistered.
Chuck Leverc2e1b092008-07-14 16:03:30 -0400423 *
Chuck Leverc2e1b092008-07-14 16:03:30 -0400424 * This function uses rpcbind protocol version 4 to contact the
425 * local rpcbind daemon. The local rpcbind daemon must support
426 * version 4 of the rpcbind protocol in order for these functions
427 * to register a service successfully.
428 *
429 * Supported netids include "udp" and "tcp" for UDP and TCP over
430 * IPv4, and "udp6" and "tcp6" for UDP and TCP over IPv6,
431 * respectively.
432 *
433 * The contents of @address determine the address family and the
434 * port to be registered. The usual practice is to pass INADDR_ANY
435 * as the raw address, but specifying a non-zero address is also
436 * supported by this API if the caller wishes to advertise an RPC
437 * service on a specific network interface.
438 *
439 * Note that passing in INADDR_ANY does not create the same service
440 * registration as IN6ADDR_ANY. The former advertises an RPC
441 * service on any IPv4 address, but not on IPv6. The latter
442 * advertises the service on all IPv4 and IPv6 addresses.
443 */
444int rpcb_v4_register(const u32 program, const u32 version,
Chuck Lever14aeb212008-08-18 19:34:00 -0400445 const struct sockaddr *address, const char *netid)
Chuck Leverc2e1b092008-07-14 16:03:30 -0400446{
447 struct rpcbind_args map = {
448 .r_prog = program,
449 .r_vers = version,
450 .r_netid = netid,
451 .r_owner = RPCB_OWNER_STRING,
452 };
453 struct rpc_message msg = {
454 .rpc_argp = &map,
Chuck Leverc2e1b092008-07-14 16:03:30 -0400455 };
Chuck Leverc5266112009-12-03 15:58:56 -0500456 int error;
457
458 error = rpcb_create_local();
459 if (error)
460 return error;
461 if (rpcb_local_clnt4 == NULL)
462 return -EPROTONOSUPPORT;
Chuck Leverc2e1b092008-07-14 16:03:30 -0400463
Chuck Lever1673d0d2009-03-18 20:47:21 -0400464 if (address == NULL)
465 return rpcb_unregister_all_protofamilies(&msg);
466
Chuck Leverc2e1b092008-07-14 16:03:30 -0400467 switch (address->sa_family) {
468 case AF_INET:
Chuck Lever1673d0d2009-03-18 20:47:21 -0400469 return rpcb_register_inet4(address, &msg);
Chuck Leverc2e1b092008-07-14 16:03:30 -0400470 case AF_INET6:
Chuck Lever1673d0d2009-03-18 20:47:21 -0400471 return rpcb_register_inet6(address, &msg);
Chuck Leverc2e1b092008-07-14 16:03:30 -0400472 }
473
474 return -EAFNOSUPPORT;
475}
476
Trond Myklebust803a9062008-07-01 15:20:55 -0400477static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt, struct rpcbind_args *map, struct rpc_procinfo *proc)
Trond Myklebust5138fde2007-07-14 15:40:01 -0400478{
479 struct rpc_message msg = {
Trond Myklebust803a9062008-07-01 15:20:55 -0400480 .rpc_proc = proc,
Trond Myklebust5138fde2007-07-14 15:40:01 -0400481 .rpc_argp = map,
Chuck Leverc0c077d2009-08-09 15:09:43 -0400482 .rpc_resp = map,
Trond Myklebust5138fde2007-07-14 15:40:01 -0400483 };
484 struct rpc_task_setup task_setup_data = {
485 .rpc_client = rpcb_clnt,
486 .rpc_message = &msg,
487 .callback_ops = &rpcb_getport_ops,
488 .callback_data = map,
Chuck Lever012da152009-12-03 15:58:56 -0500489 .flags = RPC_TASK_ASYNC | RPC_TASK_SOFTCONN,
Trond Myklebust5138fde2007-07-14 15:40:01 -0400490 };
491
492 return rpc_run_task(&task_setup_data);
493}
494
Trond Myklebust9a4bd292008-10-03 16:48:34 -0400495/*
496 * In the case where rpc clients have been cloned, we want to make
497 * sure that we use the program number/version etc of the actual
498 * owner of the xprt. To do so, we walk back up the tree of parents
499 * to find whoever created the transport and/or whoever has the
500 * autobind flag set.
501 */
502static struct rpc_clnt *rpcb_find_transport_owner(struct rpc_clnt *clnt)
503{
504 struct rpc_clnt *parent = clnt->cl_parent;
505
506 while (parent != clnt) {
507 if (parent->cl_xprt != clnt->cl_xprt)
508 break;
509 if (clnt->cl_autobind)
510 break;
511 clnt = parent;
512 parent = parent->cl_parent;
513 }
514 return clnt;
515}
516
Chuck Levera5090502007-03-29 16:48:04 -0400517/**
Chuck Lever45160d62007-07-01 12:13:17 -0400518 * rpcb_getport_async - obtain the port for a given RPC service on a given host
Chuck Levera5090502007-03-29 16:48:04 -0400519 * @task: task that is waiting for portmapper request
520 *
521 * This one can be called for an ongoing RPC request, and can be used in
522 * an async (rpciod) context.
523 */
Chuck Lever45160d62007-07-01 12:13:17 -0400524void rpcb_getport_async(struct rpc_task *task)
Chuck Levera5090502007-03-29 16:48:04 -0400525{
Trond Myklebust9a4bd292008-10-03 16:48:34 -0400526 struct rpc_clnt *clnt;
Trond Myklebust803a9062008-07-01 15:20:55 -0400527 struct rpc_procinfo *proc;
Chuck Lever0a48f5d2007-12-10 14:56:38 -0500528 u32 bind_version;
Trond Myklebust9a4bd292008-10-03 16:48:34 -0400529 struct rpc_xprt *xprt;
Chuck Levera5090502007-03-29 16:48:04 -0400530 struct rpc_clnt *rpcb_clnt;
531 static struct rpcbind_args *map;
532 struct rpc_task *child;
Chuck Lever9f6ad262007-12-10 14:56:31 -0500533 struct sockaddr_storage addr;
534 struct sockaddr *sap = (struct sockaddr *)&addr;
535 size_t salen;
Chuck Levera5090502007-03-29 16:48:04 -0400536 int status;
537
Trond Myklebust9a4bd292008-10-03 16:48:34 -0400538 clnt = rpcb_find_transport_owner(task->tk_client);
539 xprt = clnt->cl_xprt;
540
Chuck Lever45160d62007-07-01 12:13:17 -0400541 dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800542 task->tk_pid, __func__,
Chuck Lever45160d62007-07-01 12:13:17 -0400543 clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot);
Chuck Levera5090502007-03-29 16:48:04 -0400544
Trond Myklebust381ba742008-07-07 12:18:53 -0400545 /* Put self on the wait queue to ensure we get notified if
546 * some other task is already attempting to bind the port */
547 rpc_sleep_on(&xprt->binding, task, NULL);
548
Chuck Levera5090502007-03-29 16:48:04 -0400549 if (xprt_test_and_set_binding(xprt)) {
Chuck Lever45160d62007-07-01 12:13:17 -0400550 dprintk("RPC: %5u %s: waiting for another binder\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800551 task->tk_pid, __func__);
Trond Myklebust381ba742008-07-07 12:18:53 -0400552 return;
Chuck Levera5090502007-03-29 16:48:04 -0400553 }
554
Chuck Levera5090502007-03-29 16:48:04 -0400555 /* Someone else may have bound if we slept */
556 if (xprt_bound(xprt)) {
557 status = 0;
Chuck Lever45160d62007-07-01 12:13:17 -0400558 dprintk("RPC: %5u %s: already bound\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800559 task->tk_pid, __func__);
Chuck Levera5090502007-03-29 16:48:04 -0400560 goto bailout_nofree;
561 }
562
Chuck Leverba809132009-08-09 15:09:35 -0400563 /* Parent transport's destination address */
Chuck Lever9f6ad262007-12-10 14:56:31 -0500564 salen = rpc_peeraddr(clnt, sap, sizeof(addr));
Chuck Leverd5b64432007-08-06 11:57:18 -0400565
566 /* Don't ever use rpcbind v2 for AF_INET6 requests */
Chuck Lever9f6ad262007-12-10 14:56:31 -0500567 switch (sap->sa_family) {
Chuck Leverd5b64432007-08-06 11:57:18 -0400568 case AF_INET:
Trond Myklebust803a9062008-07-01 15:20:55 -0400569 proc = rpcb_next_version[xprt->bind_index].rpc_proc;
570 bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
Chuck Leverd5b64432007-08-06 11:57:18 -0400571 break;
572 case AF_INET6:
Trond Myklebust803a9062008-07-01 15:20:55 -0400573 proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
574 bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
Chuck Leverd5b64432007-08-06 11:57:18 -0400575 break;
576 default:
577 status = -EAFNOSUPPORT;
578 dprintk("RPC: %5u %s: bad address family\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800579 task->tk_pid, __func__);
Chuck Leverd5b64432007-08-06 11:57:18 -0400580 goto bailout_nofree;
581 }
Trond Myklebust803a9062008-07-01 15:20:55 -0400582 if (proc == NULL) {
Chuck Levera5090502007-03-29 16:48:04 -0400583 xprt->bind_index = 0;
Chuck Lever906462a2007-09-11 18:00:47 -0400584 status = -EPFNOSUPPORT;
Chuck Lever45160d62007-07-01 12:13:17 -0400585 dprintk("RPC: %5u %s: no more getport versions available\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800586 task->tk_pid, __func__);
Chuck Levera5090502007-03-29 16:48:04 -0400587 goto bailout_nofree;
588 }
Chuck Levera5090502007-03-29 16:48:04 -0400589
Chuck Lever45160d62007-07-01 12:13:17 -0400590 dprintk("RPC: %5u %s: trying rpcbind version %u\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800591 task->tk_pid, __func__, bind_version);
Chuck Levera5090502007-03-29 16:48:04 -0400592
Chuck Lever9f6ad262007-12-10 14:56:31 -0500593 rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot,
Chuck Lever423d8b02008-07-14 16:03:28 -0400594 bind_version);
Chuck Lever143b6c42007-08-16 16:03:31 -0400595 if (IS_ERR(rpcb_clnt)) {
596 status = PTR_ERR(rpcb_clnt);
597 dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800598 task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
Chuck Lever143b6c42007-08-16 16:03:31 -0400599 goto bailout_nofree;
600 }
601
Chuck Levera5090502007-03-29 16:48:04 -0400602 map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
603 if (!map) {
604 status = -ENOMEM;
Chuck Lever45160d62007-07-01 12:13:17 -0400605 dprintk("RPC: %5u %s: no memory available\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800606 task->tk_pid, __func__);
Trond Myklebust96165e22008-10-03 16:48:40 -0400607 goto bailout_release_client;
Chuck Levera5090502007-03-29 16:48:04 -0400608 }
609 map->r_prog = clnt->cl_prog;
610 map->r_vers = clnt->cl_vers;
611 map->r_prot = xprt->prot;
612 map->r_port = 0;
613 map->r_xprt = xprt_get(xprt);
Trond Myklebust381ba742008-07-07 12:18:53 -0400614 map->r_status = -EIO;
Chuck Levera5090502007-03-29 16:48:04 -0400615
Chuck Leverba809132009-08-09 15:09:35 -0400616 switch (bind_version) {
617 case RPCBVERS_4:
618 case RPCBVERS_3:
619 map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
620 map->r_addr = rpc_sockaddr2uaddr(sap);
621 map->r_owner = "";
622 break;
623 case RPCBVERS_2:
624 map->r_addr = NULL;
625 break;
626 default:
627 BUG();
628 }
629
Trond Myklebust803a9062008-07-01 15:20:55 -0400630 child = rpcb_call_async(rpcb_clnt, map, proc);
Trond Myklebust4c402b42007-06-14 16:40:32 -0400631 rpc_release_client(rpcb_clnt);
Chuck Levera5090502007-03-29 16:48:04 -0400632 if (IS_ERR(child)) {
Trond Myklebust0d3a34b2008-07-07 12:18:52 -0400633 /* rpcb_map_release() has freed the arguments */
Chuck Lever45160d62007-07-01 12:13:17 -0400634 dprintk("RPC: %5u %s: rpc_run_task failed\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800635 task->tk_pid, __func__);
Trond Myklebust381ba742008-07-07 12:18:53 -0400636 return;
Chuck Levera5090502007-03-29 16:48:04 -0400637 }
Chuck Levera5090502007-03-29 16:48:04 -0400638
Trond Myklebust9a4bd292008-10-03 16:48:34 -0400639 xprt->stat.bind_count++;
640 rpc_put_task(child);
Chuck Levera5090502007-03-29 16:48:04 -0400641 return;
642
Trond Myklebust96165e22008-10-03 16:48:40 -0400643bailout_release_client:
644 rpc_release_client(rpcb_clnt);
Chuck Levera5090502007-03-29 16:48:04 -0400645bailout_nofree:
646 rpcb_wake_rpcbind_waiters(xprt, status);
Chuck Levera5090502007-03-29 16:48:04 -0400647 task->tk_status = status;
648}
\"Talpey, Thomas\12444802007-09-10 13:45:36 -0400649EXPORT_SYMBOL_GPL(rpcb_getport_async);
Chuck Levera5090502007-03-29 16:48:04 -0400650
651/*
652 * Rpcbind child task calls this callback via tk_exit.
653 */
654static void rpcb_getport_done(struct rpc_task *child, void *data)
655{
656 struct rpcbind_args *map = data;
657 struct rpc_xprt *xprt = map->r_xprt;
658 int status = child->tk_status;
659
Chuck Lever4784cb52007-09-11 18:00:36 -0400660 /* Garbage reply: retry with a lesser rpcbind version */
661 if (status == -EIO)
662 status = -EPROTONOSUPPORT;
663
Chuck Levera5090502007-03-29 16:48:04 -0400664 /* rpcbind server doesn't support this rpcbind protocol version */
665 if (status == -EPROTONOSUPPORT)
666 xprt->bind_index++;
667
668 if (status < 0) {
669 /* rpcbind server not available on remote host? */
670 xprt->ops->set_port(xprt, 0);
671 } else if (map->r_port == 0) {
672 /* Requested RPC service wasn't registered on remote host */
673 xprt->ops->set_port(xprt, 0);
674 status = -EACCES;
675 } else {
676 /* Succeeded */
677 xprt->ops->set_port(xprt, map->r_port);
678 xprt_set_bound(xprt);
679 status = 0;
680 }
681
682 dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
683 child->tk_pid, status, map->r_port);
684
Trond Myklebust381ba742008-07-07 12:18:53 -0400685 map->r_status = status;
Chuck Levera5090502007-03-29 16:48:04 -0400686}
687
Chuck Lever166b88d2008-07-14 16:03:26 -0400688/*
689 * XDR functions for rpcbind
690 */
691
Chuck Lever9f06c712010-12-14 14:59:18 +0000692static void rpcb_enc_mapping(struct rpc_rqst *req, struct xdr_stream *xdr,
693 const struct rpcbind_args *rpcb)
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400694{
695 struct rpc_task *task = req->rq_task;
Chuck Lever9f06c712010-12-14 14:59:18 +0000696 __be32 *p;
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400697
698 dprintk("RPC: %5u encoding PMAP_%s call (%u, %u, %d, %u)\n",
699 task->tk_pid, task->tk_msg.rpc_proc->p_name,
700 rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
701
Chuck Lever9f06c712010-12-14 14:59:18 +0000702 p = xdr_reserve_space(xdr, RPCB_mappingargs_sz << 2);
Chuck Lever4129ccf2010-12-14 14:58:59 +0000703 *p++ = cpu_to_be32(rpcb->r_prog);
704 *p++ = cpu_to_be32(rpcb->r_vers);
705 *p++ = cpu_to_be32(rpcb->r_prot);
706 *p = cpu_to_be32(rpcb->r_port);
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400707}
708
Chuck Leverbf269552010-12-14 14:59:29 +0000709static int rpcb_dec_getport(struct rpc_rqst *req, struct xdr_stream *xdr,
Chuck Leverc0c077d2009-08-09 15:09:43 -0400710 struct rpcbind_args *rpcb)
711{
712 struct rpc_task *task = req->rq_task;
Chuck Leverc0c077d2009-08-09 15:09:43 -0400713 unsigned long port;
Chuck Leverbf269552010-12-14 14:59:29 +0000714 __be32 *p;
Chuck Leverc0c077d2009-08-09 15:09:43 -0400715
716 rpcb->r_port = 0;
717
Chuck Leverbf269552010-12-14 14:59:29 +0000718 p = xdr_inline_decode(xdr, 4);
Chuck Leverc0c077d2009-08-09 15:09:43 -0400719 if (unlikely(p == NULL))
720 return -EIO;
721
Chuck Lever4129ccf2010-12-14 14:58:59 +0000722 port = be32_to_cpup(p);
Chuck Leverc0c077d2009-08-09 15:09:43 -0400723 dprintk("RPC: %5u PMAP_%s result: %lu\n", task->tk_pid,
724 task->tk_msg.rpc_proc->p_name, port);
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700725 if (unlikely(port > USHRT_MAX))
Chuck Leverc0c077d2009-08-09 15:09:43 -0400726 return -EIO;
727
728 rpcb->r_port = port;
729 return 0;
730}
731
Chuck Leverbf269552010-12-14 14:59:29 +0000732static int rpcb_dec_set(struct rpc_rqst *req, struct xdr_stream *xdr,
Chuck Lever7ed0ff92009-08-09 15:09:42 -0400733 unsigned int *boolp)
734{
735 struct rpc_task *task = req->rq_task;
Chuck Leverbf269552010-12-14 14:59:29 +0000736 __be32 *p;
Chuck Lever7ed0ff92009-08-09 15:09:42 -0400737
Chuck Leverbf269552010-12-14 14:59:29 +0000738 p = xdr_inline_decode(xdr, 4);
Chuck Lever7ed0ff92009-08-09 15:09:42 -0400739 if (unlikely(p == NULL))
740 return -EIO;
741
742 *boolp = 0;
Chuck Leverbf269552010-12-14 14:59:29 +0000743 if (*p != xdr_zero)
Chuck Lever7ed0ff92009-08-09 15:09:42 -0400744 *boolp = 1;
745
746 dprintk("RPC: %5u RPCB_%s call %s\n",
747 task->tk_pid, task->tk_msg.rpc_proc->p_name,
748 (*boolp ? "succeeded" : "failed"));
749 return 0;
750}
751
Chuck Lever4129ccf2010-12-14 14:58:59 +0000752static void encode_rpcb_string(struct xdr_stream *xdr, const char *string,
753 const u32 maxstrlen)
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400754{
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400755 __be32 *p;
Chuck Lever4129ccf2010-12-14 14:58:59 +0000756 u32 len;
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400757
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400758 len = strlen(string);
Chuck Lever4129ccf2010-12-14 14:58:59 +0000759 BUG_ON(len > maxstrlen);
760 p = xdr_reserve_space(xdr, 4 + len);
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400761 xdr_encode_opaque(p, string, len);
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400762}
763
Chuck Lever9f06c712010-12-14 14:59:18 +0000764static void rpcb_enc_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr,
765 const struct rpcbind_args *rpcb)
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400766{
767 struct rpc_task *task = req->rq_task;
Chuck Lever9f06c712010-12-14 14:59:18 +0000768 __be32 *p;
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400769
770 dprintk("RPC: %5u encoding RPCB_%s call (%u, %u, '%s', '%s')\n",
771 task->tk_pid, task->tk_msg.rpc_proc->p_name,
772 rpcb->r_prog, rpcb->r_vers,
773 rpcb->r_netid, rpcb->r_addr);
774
Chuck Lever9f06c712010-12-14 14:59:18 +0000775 p = xdr_reserve_space(xdr, (RPCB_program_sz + RPCB_version_sz) << 2);
Chuck Lever4129ccf2010-12-14 14:58:59 +0000776 *p++ = cpu_to_be32(rpcb->r_prog);
777 *p = cpu_to_be32(rpcb->r_vers);
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400778
Chuck Lever9f06c712010-12-14 14:59:18 +0000779 encode_rpcb_string(xdr, rpcb->r_netid, RPCBIND_MAXNETIDLEN);
780 encode_rpcb_string(xdr, rpcb->r_addr, RPCBIND_MAXUADDRLEN);
781 encode_rpcb_string(xdr, rpcb->r_owner, RPCB_MAXOWNERLEN);
Chuck Lever6f2c2db2009-08-09 15:09:40 -0400782}
783
Chuck Leverbf269552010-12-14 14:59:29 +0000784static int rpcb_dec_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr,
Chuck Leverc0c077d2009-08-09 15:09:43 -0400785 struct rpcbind_args *rpcb)
786{
787 struct sockaddr_storage address;
788 struct sockaddr *sap = (struct sockaddr *)&address;
789 struct rpc_task *task = req->rq_task;
Chuck Leverbf269552010-12-14 14:59:29 +0000790 __be32 *p;
Chuck Leverc0c077d2009-08-09 15:09:43 -0400791 u32 len;
792
793 rpcb->r_port = 0;
794
Chuck Leverbf269552010-12-14 14:59:29 +0000795 p = xdr_inline_decode(xdr, 4);
Chuck Leverc0c077d2009-08-09 15:09:43 -0400796 if (unlikely(p == NULL))
797 goto out_fail;
Chuck Lever4129ccf2010-12-14 14:58:59 +0000798 len = be32_to_cpup(p);
Chuck Leverc0c077d2009-08-09 15:09:43 -0400799
800 /*
801 * If the returned universal address is a null string,
802 * the requested RPC service was not registered.
803 */
804 if (len == 0) {
805 dprintk("RPC: %5u RPCB reply: program not registered\n",
806 task->tk_pid);
807 return 0;
808 }
809
810 if (unlikely(len > RPCBIND_MAXUADDRLEN))
811 goto out_fail;
812
Chuck Leverbf269552010-12-14 14:59:29 +0000813 p = xdr_inline_decode(xdr, len);
Chuck Leverc0c077d2009-08-09 15:09:43 -0400814 if (unlikely(p == NULL))
815 goto out_fail;
816 dprintk("RPC: %5u RPCB_%s reply: %s\n", task->tk_pid,
817 task->tk_msg.rpc_proc->p_name, (char *)p);
818
819 if (rpc_uaddr2sockaddr((char *)p, len, sap, sizeof(address)) == 0)
820 goto out_fail;
821 rpcb->r_port = rpc_get_port(sap);
822
823 return 0;
824
825out_fail:
826 dprintk("RPC: %5u malformed RPCB_%s reply\n",
827 task->tk_pid, task->tk_msg.rpc_proc->p_name);
828 return -EIO;
829}
830
Chuck Levera5090502007-03-29 16:48:04 -0400831/*
832 * Not all rpcbind procedures described in RFC 1833 are implemented
833 * since the Linux kernel RPC code requires only these.
834 */
Chuck Leverf8b761e2009-08-09 15:09:44 -0400835
Chuck Levera5090502007-03-29 16:48:04 -0400836static struct rpc_procinfo rpcb_procedures2[] = {
Chuck Leverf8b761e2009-08-09 15:09:44 -0400837 [RPCBPROC_SET] = {
838 .p_proc = RPCBPROC_SET,
Chuck Lever9f06c712010-12-14 14:59:18 +0000839 .p_encode = (kxdreproc_t)rpcb_enc_mapping,
Chuck Leverbf269552010-12-14 14:59:29 +0000840 .p_decode = (kxdrdproc_t)rpcb_dec_set,
Chuck Leverf8b761e2009-08-09 15:09:44 -0400841 .p_arglen = RPCB_mappingargs_sz,
842 .p_replen = RPCB_setres_sz,
843 .p_statidx = RPCBPROC_SET,
844 .p_timer = 0,
845 .p_name = "SET",
846 },
847 [RPCBPROC_UNSET] = {
848 .p_proc = RPCBPROC_UNSET,
Chuck Lever9f06c712010-12-14 14:59:18 +0000849 .p_encode = (kxdreproc_t)rpcb_enc_mapping,
Chuck Leverbf269552010-12-14 14:59:29 +0000850 .p_decode = (kxdrdproc_t)rpcb_dec_set,
Chuck Leverf8b761e2009-08-09 15:09:44 -0400851 .p_arglen = RPCB_mappingargs_sz,
852 .p_replen = RPCB_setres_sz,
853 .p_statidx = RPCBPROC_UNSET,
854 .p_timer = 0,
855 .p_name = "UNSET",
856 },
857 [RPCBPROC_GETPORT] = {
858 .p_proc = RPCBPROC_GETPORT,
Chuck Lever9f06c712010-12-14 14:59:18 +0000859 .p_encode = (kxdreproc_t)rpcb_enc_mapping,
Chuck Leverbf269552010-12-14 14:59:29 +0000860 .p_decode = (kxdrdproc_t)rpcb_dec_getport,
Chuck Leverf8b761e2009-08-09 15:09:44 -0400861 .p_arglen = RPCB_mappingargs_sz,
862 .p_replen = RPCB_getportres_sz,
863 .p_statidx = RPCBPROC_GETPORT,
864 .p_timer = 0,
865 .p_name = "GETPORT",
866 },
Chuck Levera5090502007-03-29 16:48:04 -0400867};
868
869static struct rpc_procinfo rpcb_procedures3[] = {
Chuck Leverf8b761e2009-08-09 15:09:44 -0400870 [RPCBPROC_SET] = {
871 .p_proc = RPCBPROC_SET,
Chuck Lever9f06c712010-12-14 14:59:18 +0000872 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
Chuck Leverbf269552010-12-14 14:59:29 +0000873 .p_decode = (kxdrdproc_t)rpcb_dec_set,
Chuck Leverf8b761e2009-08-09 15:09:44 -0400874 .p_arglen = RPCB_getaddrargs_sz,
875 .p_replen = RPCB_setres_sz,
876 .p_statidx = RPCBPROC_SET,
877 .p_timer = 0,
878 .p_name = "SET",
879 },
880 [RPCBPROC_UNSET] = {
881 .p_proc = RPCBPROC_UNSET,
Chuck Lever9f06c712010-12-14 14:59:18 +0000882 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
Chuck Leverbf269552010-12-14 14:59:29 +0000883 .p_decode = (kxdrdproc_t)rpcb_dec_set,
Chuck Leverf8b761e2009-08-09 15:09:44 -0400884 .p_arglen = RPCB_getaddrargs_sz,
885 .p_replen = RPCB_setres_sz,
886 .p_statidx = RPCBPROC_UNSET,
887 .p_timer = 0,
888 .p_name = "UNSET",
889 },
890 [RPCBPROC_GETADDR] = {
891 .p_proc = RPCBPROC_GETADDR,
Chuck Lever9f06c712010-12-14 14:59:18 +0000892 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
Chuck Leverbf269552010-12-14 14:59:29 +0000893 .p_decode = (kxdrdproc_t)rpcb_dec_getaddr,
Chuck Leverf8b761e2009-08-09 15:09:44 -0400894 .p_arglen = RPCB_getaddrargs_sz,
895 .p_replen = RPCB_getaddrres_sz,
896 .p_statidx = RPCBPROC_GETADDR,
897 .p_timer = 0,
898 .p_name = "GETADDR",
899 },
Chuck Levera5090502007-03-29 16:48:04 -0400900};
901
902static struct rpc_procinfo rpcb_procedures4[] = {
Chuck Leverf8b761e2009-08-09 15:09:44 -0400903 [RPCBPROC_SET] = {
904 .p_proc = RPCBPROC_SET,
Chuck Lever9f06c712010-12-14 14:59:18 +0000905 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
Chuck Leverbf269552010-12-14 14:59:29 +0000906 .p_decode = (kxdrdproc_t)rpcb_dec_set,
Chuck Leverf8b761e2009-08-09 15:09:44 -0400907 .p_arglen = RPCB_getaddrargs_sz,
908 .p_replen = RPCB_setres_sz,
909 .p_statidx = RPCBPROC_SET,
910 .p_timer = 0,
911 .p_name = "SET",
912 },
913 [RPCBPROC_UNSET] = {
914 .p_proc = RPCBPROC_UNSET,
Chuck Lever9f06c712010-12-14 14:59:18 +0000915 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
Chuck Leverbf269552010-12-14 14:59:29 +0000916 .p_decode = (kxdrdproc_t)rpcb_dec_set,
Chuck Leverf8b761e2009-08-09 15:09:44 -0400917 .p_arglen = RPCB_getaddrargs_sz,
918 .p_replen = RPCB_setres_sz,
919 .p_statidx = RPCBPROC_UNSET,
920 .p_timer = 0,
921 .p_name = "UNSET",
922 },
923 [RPCBPROC_GETADDR] = {
924 .p_proc = RPCBPROC_GETADDR,
Chuck Lever9f06c712010-12-14 14:59:18 +0000925 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
Chuck Leverbf269552010-12-14 14:59:29 +0000926 .p_decode = (kxdrdproc_t)rpcb_dec_getaddr,
Chuck Leverf8b761e2009-08-09 15:09:44 -0400927 .p_arglen = RPCB_getaddrargs_sz,
928 .p_replen = RPCB_getaddrres_sz,
929 .p_statidx = RPCBPROC_GETADDR,
930 .p_timer = 0,
931 .p_name = "GETADDR",
932 },
Chuck Levera5090502007-03-29 16:48:04 -0400933};
934
935static struct rpcb_info rpcb_next_version[] = {
Chuck Leverfc200e72008-06-25 17:24:31 -0400936 {
937 .rpc_vers = RPCBVERS_2,
938 .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
939 },
940 {
941 .rpc_proc = NULL,
942 },
Chuck Levera5090502007-03-29 16:48:04 -0400943};
944
Chuck Leverd5b64432007-08-06 11:57:18 -0400945static struct rpcb_info rpcb_next_version6[] = {
Chuck Leverfc200e72008-06-25 17:24:31 -0400946 {
947 .rpc_vers = RPCBVERS_4,
Chuck Lever88424132008-06-25 17:24:47 -0400948 .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR],
Chuck Leverfc200e72008-06-25 17:24:31 -0400949 },
950 {
951 .rpc_vers = RPCBVERS_3,
952 .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR],
953 },
Chuck Leverfc200e72008-06-25 17:24:31 -0400954 {
955 .rpc_proc = NULL,
956 },
Chuck Leverd5b64432007-08-06 11:57:18 -0400957};
958
Chuck Levera5090502007-03-29 16:48:04 -0400959static struct rpc_version rpcb_version2 = {
Chuck Leverfc200e72008-06-25 17:24:31 -0400960 .number = RPCBVERS_2,
Chuck Lever1ac7c232010-12-14 14:59:09 +0000961 .nrprocs = ARRAY_SIZE(rpcb_procedures2),
Chuck Levera5090502007-03-29 16:48:04 -0400962 .procs = rpcb_procedures2
963};
964
965static struct rpc_version rpcb_version3 = {
Chuck Leverfc200e72008-06-25 17:24:31 -0400966 .number = RPCBVERS_3,
Chuck Lever1ac7c232010-12-14 14:59:09 +0000967 .nrprocs = ARRAY_SIZE(rpcb_procedures3),
Chuck Levera5090502007-03-29 16:48:04 -0400968 .procs = rpcb_procedures3
969};
970
971static struct rpc_version rpcb_version4 = {
Chuck Leverfc200e72008-06-25 17:24:31 -0400972 .number = RPCBVERS_4,
Chuck Lever1ac7c232010-12-14 14:59:09 +0000973 .nrprocs = ARRAY_SIZE(rpcb_procedures4),
Chuck Levera5090502007-03-29 16:48:04 -0400974 .procs = rpcb_procedures4
975};
976
977static struct rpc_version *rpcb_version[] = {
978 NULL,
979 NULL,
980 &rpcb_version2,
981 &rpcb_version3,
982 &rpcb_version4
983};
984
985static struct rpc_stat rpcb_stats;
986
Adrian Bunk7f4adef2007-06-13 01:03:13 +0200987static struct rpc_program rpcb_program = {
Chuck Levera5090502007-03-29 16:48:04 -0400988 .name = "rpcbind",
989 .number = RPCBIND_PROGRAM,
990 .nrvers = ARRAY_SIZE(rpcb_version),
991 .version = rpcb_version,
992 .stats = &rpcb_stats,
993};
Chuck Leverc5266112009-12-03 15:58:56 -0500994
995/**
996 * cleanup_rpcb_clnt - remove xprtsock's sysctls, unregister
997 *
998 */
999void cleanup_rpcb_clnt(void)
1000{
1001 if (rpcb_local_clnt4)
1002 rpc_shutdown_client(rpcb_local_clnt4);
1003 if (rpcb_local_clnt)
1004 rpc_shutdown_client(rpcb_local_clnt);
1005}