2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2006-08-23 08:06:16 +08:00
|
|
|
* linux/net/sunrpc/pmap_clnt.c
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
2006-08-23 08:06:16 +08:00
|
|
|
* In-kernel RPC portmapper client.
|
|
|
|
*
|
|
|
|
* Portmapper supports version 2 of the rpcbind protocol (RFC 1833).
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/socket.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/uio.h>
|
|
|
|
#include <linux/in.h>
|
|
|
|
#include <linux/sunrpc/clnt.h>
|
|
|
|
#include <linux/sunrpc/xprt.h>
|
|
|
|
#include <linux/sunrpc/sched.h>
|
|
|
|
|
|
|
|
#ifdef RPC_DEBUG
|
|
|
|
# define RPCDBG_FACILITY RPCDBG_PMAP
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define PMAP_SET 1
|
|
|
|
#define PMAP_UNSET 2
|
|
|
|
#define PMAP_GETPORT 3
|
|
|
|
|
2006-08-23 08:06:15 +08:00
|
|
|
struct portmap_args {
|
|
|
|
u32 pm_prog;
|
|
|
|
u32 pm_vers;
|
|
|
|
u32 pm_prot;
|
|
|
|
unsigned short pm_port;
|
|
|
|
struct rpc_task * pm_task;
|
|
|
|
};
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static struct rpc_procinfo pmap_procedures[];
|
2005-09-23 09:24:59 +08:00
|
|
|
static struct rpc_clnt * pmap_create(char *, struct sockaddr_in *, int, int);
|
2006-08-23 08:06:15 +08:00
|
|
|
static void pmap_getport_done(struct rpc_task *, void *);
|
2005-04-17 06:20:36 +08:00
|
|
|
static struct rpc_program pmap_program;
|
2006-08-23 08:06:15 +08:00
|
|
|
|
|
|
|
static void pmap_getport_prepare(struct rpc_task *task, void *calldata)
|
|
|
|
{
|
|
|
|
struct portmap_args *map = calldata;
|
|
|
|
struct rpc_message msg = {
|
|
|
|
.rpc_proc = &pmap_procedures[PMAP_GETPORT],
|
|
|
|
.rpc_argp = map,
|
|
|
|
.rpc_resp = &map->pm_port,
|
|
|
|
};
|
|
|
|
|
|
|
|
rpc_call_setup(task, &msg, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline struct portmap_args *pmap_map_alloc(void)
|
|
|
|
{
|
|
|
|
return kmalloc(sizeof(struct portmap_args), GFP_NOFS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void pmap_map_free(struct portmap_args *map)
|
|
|
|
{
|
|
|
|
kfree(map);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pmap_map_release(void *data)
|
|
|
|
{
|
|
|
|
pmap_map_free(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct rpc_call_ops pmap_getport_ops = {
|
|
|
|
.rpc_call_prepare = pmap_getport_prepare,
|
|
|
|
.rpc_call_done = pmap_getport_done,
|
|
|
|
.rpc_release = pmap_map_release,
|
|
|
|
};
|
|
|
|
|
|
|
|
static inline void pmap_wake_portmap_waiters(struct rpc_xprt *xprt)
|
|
|
|
{
|
|
|
|
xprt_clear_binding(xprt);
|
|
|
|
rpc_wake_up(&xprt->binding);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-08-23 08:06:16 +08:00
|
|
|
/**
|
|
|
|
* rpc_getport - obtain the port for a given RPC service on a given host
|
|
|
|
* @task: task that is waiting for portmapper request
|
|
|
|
* @clnt: controlling rpc_clnt
|
|
|
|
*
|
|
|
|
* This one can be called for an ongoing RPC request, and can be used in
|
|
|
|
* an async (rpciod) context.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2006-08-23 08:06:16 +08:00
|
|
|
void rpc_getport(struct rpc_task *task, struct rpc_clnt *clnt)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-08-23 08:06:15 +08:00
|
|
|
struct rpc_xprt *xprt = task->tk_xprt;
|
|
|
|
struct sockaddr_in *sap = &xprt->addr;
|
|
|
|
struct portmap_args *map;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct rpc_clnt *pmap_clnt;
|
2006-08-23 08:06:15 +08:00
|
|
|
struct rpc_task *child;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-08-23 08:06:15 +08:00
|
|
|
dprintk("RPC: %4d rpc_getport(%s, %u, %u, %d)\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
task->tk_pid, clnt->cl_server,
|
2006-08-23 08:06:15 +08:00
|
|
|
clnt->cl_prog, clnt->cl_vers, xprt->prot);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-06-23 01:16:23 +08:00
|
|
|
/* Autobind on cloned rpc clients is discouraged */
|
|
|
|
BUG_ON(clnt->cl_parent != clnt);
|
|
|
|
|
2006-08-23 08:06:15 +08:00
|
|
|
if (xprt_test_and_set_binding(xprt)) {
|
|
|
|
task->tk_status = -EACCES; /* tell caller to check again */
|
|
|
|
rpc_sleep_on(&xprt->binding, task, NULL, NULL);
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
|
|
|
}
|
2006-08-23 08:06:15 +08:00
|
|
|
|
|
|
|
/* Someone else may have bound if we slept */
|
|
|
|
if (xprt_bound(xprt)) {
|
|
|
|
task->tk_status = 0;
|
|
|
|
goto bailout_nofree;
|
|
|
|
}
|
|
|
|
|
|
|
|
map = pmap_map_alloc();
|
|
|
|
if (!map) {
|
|
|
|
task->tk_status = -ENOMEM;
|
|
|
|
goto bailout_nofree;
|
|
|
|
}
|
|
|
|
map->pm_prog = clnt->cl_prog;
|
|
|
|
map->pm_vers = clnt->cl_vers;
|
|
|
|
map->pm_prot = xprt->prot;
|
|
|
|
map->pm_port = 0;
|
|
|
|
map->pm_task = task;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-09-23 09:24:59 +08:00
|
|
|
pmap_clnt = pmap_create(clnt->cl_server, sap, map->pm_prot, 0);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (IS_ERR(pmap_clnt)) {
|
|
|
|
task->tk_status = PTR_ERR(pmap_clnt);
|
|
|
|
goto bailout;
|
|
|
|
}
|
|
|
|
|
2006-08-23 08:06:15 +08:00
|
|
|
child = rpc_run_task(pmap_clnt, RPC_TASK_ASYNC, &pmap_getport_ops, map);
|
|
|
|
if (IS_ERR(child)) {
|
|
|
|
task->tk_status = -EIO;
|
2005-04-17 06:20:36 +08:00
|
|
|
goto bailout;
|
2006-08-23 08:06:15 +08:00
|
|
|
}
|
|
|
|
rpc_release_task(child);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-08-23 08:06:15 +08:00
|
|
|
rpc_sleep_on(&xprt->binding, task, NULL, NULL);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2006-03-21 02:44:16 +08:00
|
|
|
task->tk_xprt->stat.bind_count++;
|
2005-04-17 06:20:36 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
bailout:
|
2006-08-23 08:06:15 +08:00
|
|
|
pmap_map_free(map);
|
|
|
|
bailout_nofree:
|
|
|
|
pmap_wake_portmap_waiters(xprt);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_ROOT_NFS
|
2006-08-23 08:06:16 +08:00
|
|
|
/**
|
|
|
|
* rpc_getport_external - obtain the port for a given RPC service on a given host
|
|
|
|
* @sin: address of remote peer
|
|
|
|
* @prog: RPC program number to bind
|
|
|
|
* @vers: RPC version number to bind
|
|
|
|
* @prot: transport protocol to use to make this request
|
|
|
|
*
|
|
|
|
* This one is called from outside the RPC client in a synchronous task context.
|
|
|
|
*/
|
|
|
|
int rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-08-23 08:06:15 +08:00
|
|
|
struct portmap_args map = {
|
2005-04-17 06:20:36 +08:00
|
|
|
.pm_prog = prog,
|
|
|
|
.pm_vers = vers,
|
|
|
|
.pm_prot = prot,
|
|
|
|
.pm_port = 0
|
|
|
|
};
|
2006-03-21 02:44:23 +08:00
|
|
|
struct rpc_message msg = {
|
|
|
|
.rpc_proc = &pmap_procedures[PMAP_GETPORT],
|
|
|
|
.rpc_argp = &map,
|
|
|
|
.rpc_resp = &map.pm_port,
|
|
|
|
};
|
2005-04-17 06:20:36 +08:00
|
|
|
struct rpc_clnt *pmap_clnt;
|
|
|
|
char hostname[32];
|
|
|
|
int status;
|
|
|
|
|
2006-08-23 08:06:16 +08:00
|
|
|
dprintk("RPC: rpc_getport_external(%u.%u.%u.%u, %u, %u, %d)\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
|
|
|
|
|
|
|
|
sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr));
|
2005-09-23 09:24:59 +08:00
|
|
|
pmap_clnt = pmap_create(hostname, sin, prot, 0);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (IS_ERR(pmap_clnt))
|
|
|
|
return PTR_ERR(pmap_clnt);
|
|
|
|
|
|
|
|
/* Setup the call info struct */
|
2006-03-21 02:44:23 +08:00
|
|
|
status = rpc_call_sync(pmap_clnt, &msg, 0);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (status >= 0) {
|
|
|
|
if (map.pm_port != 0)
|
|
|
|
return map.pm_port;
|
|
|
|
status = -EACCES;
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-08-23 08:06:16 +08:00
|
|
|
/*
|
|
|
|
* Portmapper child task invokes this callback via tk_exit.
|
|
|
|
*/
|
|
|
|
static void pmap_getport_done(struct rpc_task *child, void *data)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-08-23 08:06:15 +08:00
|
|
|
struct portmap_args *map = data;
|
|
|
|
struct rpc_task *task = map->pm_task;
|
2006-01-03 16:55:51 +08:00
|
|
|
struct rpc_xprt *xprt = task->tk_xprt;
|
2006-08-23 08:06:15 +08:00
|
|
|
int status = child->tk_status;
|
2006-01-03 16:55:51 +08:00
|
|
|
|
2006-08-23 08:06:15 +08:00
|
|
|
if (status < 0) {
|
|
|
|
/* Portmapper not available */
|
2006-08-23 08:06:15 +08:00
|
|
|
xprt->ops->set_port(xprt, 0);
|
2006-08-23 08:06:15 +08:00
|
|
|
task->tk_status = status;
|
|
|
|
} else if (map->pm_port == 0) {
|
|
|
|
/* Requested RPC service wasn't registered */
|
2006-08-23 08:06:15 +08:00
|
|
|
xprt->ops->set_port(xprt, 0);
|
2006-08-23 08:06:15 +08:00
|
|
|
task->tk_status = -EACCES;
|
2005-04-17 06:20:36 +08:00
|
|
|
} else {
|
2006-08-23 08:06:15 +08:00
|
|
|
/* Succeeded */
|
|
|
|
xprt->ops->set_port(xprt, map->pm_port);
|
2006-08-23 08:06:15 +08:00
|
|
|
xprt_set_bound(xprt);
|
2006-08-23 08:06:15 +08:00
|
|
|
task->tk_status = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2006-08-23 08:06:15 +08:00
|
|
|
|
|
|
|
dprintk("RPC: %4d pmap_getport_done(status %d, port %u)\n",
|
|
|
|
child->tk_pid, child->tk_status, map->pm_port);
|
|
|
|
|
|
|
|
pmap_wake_portmap_waiters(xprt);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-08-23 08:06:16 +08:00
|
|
|
/**
|
|
|
|
* rpc_register - set or unset a port registration with the local portmapper
|
|
|
|
* @prog: RPC program number to bind
|
|
|
|
* @vers: RPC version number to bind
|
|
|
|
* @prot: transport protocol to use to make this request
|
|
|
|
* @port: port value to register
|
|
|
|
* @okay: result code
|
|
|
|
*
|
2005-04-17 06:20:36 +08:00
|
|
|
* port == 0 means unregister, port != 0 means register.
|
|
|
|
*/
|
2006-08-23 08:06:16 +08:00
|
|
|
int rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-03-21 02:44:23 +08:00
|
|
|
struct sockaddr_in sin = {
|
|
|
|
.sin_family = AF_INET,
|
|
|
|
.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
|
|
|
|
};
|
2006-08-23 08:06:15 +08:00
|
|
|
struct portmap_args map = {
|
2006-03-21 02:44:23 +08:00
|
|
|
.pm_prog = prog,
|
|
|
|
.pm_vers = vers,
|
|
|
|
.pm_prot = prot,
|
|
|
|
.pm_port = port,
|
|
|
|
};
|
|
|
|
struct rpc_message msg = {
|
|
|
|
.rpc_proc = &pmap_procedures[port ? PMAP_SET : PMAP_UNSET],
|
|
|
|
.rpc_argp = &map,
|
|
|
|
.rpc_resp = okay,
|
|
|
|
};
|
2005-04-17 06:20:36 +08:00
|
|
|
struct rpc_clnt *pmap_clnt;
|
|
|
|
int error = 0;
|
|
|
|
|
2006-08-23 08:06:16 +08:00
|
|
|
dprintk("RPC: registering (%u, %u, %d, %u) with portmapper.\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
prog, vers, prot, port);
|
|
|
|
|
2005-09-23 09:24:59 +08:00
|
|
|
pmap_clnt = pmap_create("localhost", &sin, IPPROTO_UDP, 1);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (IS_ERR(pmap_clnt)) {
|
|
|
|
error = PTR_ERR(pmap_clnt);
|
|
|
|
dprintk("RPC: couldn't create pmap client. Error = %d\n", error);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2006-03-21 02:44:23 +08:00
|
|
|
error = rpc_call_sync(pmap_clnt, &msg, 0);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
if (error < 0) {
|
|
|
|
printk(KERN_WARNING
|
|
|
|
"RPC: failed to contact portmap (errno %d).\n",
|
|
|
|
error);
|
|
|
|
}
|
|
|
|
dprintk("RPC: registration status %d/%d\n", error, *okay);
|
|
|
|
|
|
|
|
/* Client deleted automatically because cl_oneshot == 1 */
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2006-08-23 08:06:16 +08:00
|
|
|
static struct rpc_clnt *pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto, int privileged)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct rpc_xprt *xprt;
|
|
|
|
struct rpc_clnt *clnt;
|
|
|
|
|
|
|
|
xprt = xprt_create_proto(proto, srvaddr, NULL);
|
|
|
|
if (IS_ERR(xprt))
|
|
|
|
return (struct rpc_clnt *)xprt;
|
2006-01-03 16:55:51 +08:00
|
|
|
xprt->ops->set_port(xprt, RPC_PMAP_PORT);
|
2006-08-23 08:06:15 +08:00
|
|
|
xprt_set_bound(xprt);
|
2005-09-23 09:24:59 +08:00
|
|
|
if (!privileged)
|
|
|
|
xprt->resvport = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2005-06-23 01:16:20 +08:00
|
|
|
clnt = rpc_new_client(xprt, hostname,
|
2005-04-17 06:20:36 +08:00
|
|
|
&pmap_program, RPC_PMAP_VERSION,
|
|
|
|
RPC_AUTH_UNIX);
|
2005-06-23 01:16:20 +08:00
|
|
|
if (!IS_ERR(clnt)) {
|
2005-04-17 06:20:36 +08:00
|
|
|
clnt->cl_softrtry = 1;
|
|
|
|
clnt->cl_oneshot = 1;
|
|
|
|
}
|
|
|
|
return clnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* XDR encode/decode functions for PMAP
|
|
|
|
*/
|
2006-08-23 08:06:16 +08:00
|
|
|
static int xdr_encode_mapping(struct rpc_rqst *req, u32 *p, struct portmap_args *map)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2006-08-23 08:06:16 +08:00
|
|
|
dprintk("RPC: xdr_encode_mapping(%u, %u, %u, %u)\n",
|
2005-04-17 06:20:36 +08:00
|
|
|
map->pm_prog, map->pm_vers, map->pm_prot, map->pm_port);
|
|
|
|
*p++ = htonl(map->pm_prog);
|
|
|
|
*p++ = htonl(map->pm_vers);
|
|
|
|
*p++ = htonl(map->pm_prot);
|
|
|
|
*p++ = htonl(map->pm_port);
|
|
|
|
|
|
|
|
req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-23 08:06:16 +08:00
|
|
|
static int xdr_decode_port(struct rpc_rqst *req, u32 *p, unsigned short *portp)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
*portp = (unsigned short) ntohl(*p++);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-23 08:06:16 +08:00
|
|
|
static int xdr_decode_bool(struct rpc_rqst *req, u32 *p, unsigned int *boolp)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
*boolp = (unsigned int) ntohl(*p++);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct rpc_procinfo pmap_procedures[] = {
|
|
|
|
[PMAP_SET] = {
|
|
|
|
.p_proc = PMAP_SET,
|
|
|
|
.p_encode = (kxdrproc_t) xdr_encode_mapping,
|
|
|
|
.p_decode = (kxdrproc_t) xdr_decode_bool,
|
|
|
|
.p_bufsiz = 4,
|
|
|
|
.p_count = 1,
|
2006-03-21 02:44:22 +08:00
|
|
|
.p_statidx = PMAP_SET,
|
|
|
|
.p_name = "SET",
|
2005-04-17 06:20:36 +08:00
|
|
|
},
|
|
|
|
[PMAP_UNSET] = {
|
|
|
|
.p_proc = PMAP_UNSET,
|
|
|
|
.p_encode = (kxdrproc_t) xdr_encode_mapping,
|
|
|
|
.p_decode = (kxdrproc_t) xdr_decode_bool,
|
|
|
|
.p_bufsiz = 4,
|
|
|
|
.p_count = 1,
|
2006-03-21 02:44:22 +08:00
|
|
|
.p_statidx = PMAP_UNSET,
|
|
|
|
.p_name = "UNSET",
|
2005-04-17 06:20:36 +08:00
|
|
|
},
|
|
|
|
[PMAP_GETPORT] = {
|
|
|
|
.p_proc = PMAP_GETPORT,
|
|
|
|
.p_encode = (kxdrproc_t) xdr_encode_mapping,
|
|
|
|
.p_decode = (kxdrproc_t) xdr_decode_port,
|
|
|
|
.p_bufsiz = 4,
|
|
|
|
.p_count = 1,
|
2006-03-21 02:44:22 +08:00
|
|
|
.p_statidx = PMAP_GETPORT,
|
|
|
|
.p_name = "GETPORT",
|
2005-04-17 06:20:36 +08:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct rpc_version pmap_version2 = {
|
|
|
|
.number = 2,
|
|
|
|
.nrprocs = 4,
|
|
|
|
.procs = pmap_procedures
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct rpc_version * pmap_version[] = {
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
&pmap_version2
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct rpc_stat pmap_stats;
|
|
|
|
|
|
|
|
static struct rpc_program pmap_program = {
|
|
|
|
.name = "portmap",
|
|
|
|
.number = RPC_PMAP_PROGRAM,
|
|
|
|
.nrvers = ARRAY_SIZE(pmap_version),
|
|
|
|
.version = pmap_version,
|
|
|
|
.stats = &pmap_stats,
|
|
|
|
};
|