Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Chuck Lever | c4a5692 | 2006-08-22 20:06:16 -0400 | [diff] [blame] | 2 | * linux/net/sunrpc/pmap_clnt.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
Chuck Lever | c4a5692 | 2006-08-22 20:06:16 -0400 | [diff] [blame] | 4 | * In-kernel RPC portmapper client. |
| 5 | * |
| 6 | * Portmapper supports version 2 of the rpcbind protocol (RFC 1833). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de> |
| 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/types.h> |
| 12 | #include <linux/socket.h> |
| 13 | #include <linux/kernel.h> |
| 14 | #include <linux/errno.h> |
| 15 | #include <linux/uio.h> |
| 16 | #include <linux/in.h> |
| 17 | #include <linux/sunrpc/clnt.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/sunrpc/sched.h> |
| 19 | |
| 20 | #ifdef RPC_DEBUG |
| 21 | # define RPCDBG_FACILITY RPCDBG_PMAP |
| 22 | #endif |
| 23 | |
| 24 | #define PMAP_SET 1 |
| 25 | #define PMAP_UNSET 2 |
| 26 | #define PMAP_GETPORT 3 |
| 27 | |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 28 | struct portmap_args { |
| 29 | u32 pm_prog; |
| 30 | u32 pm_vers; |
| 31 | u32 pm_prot; |
| 32 | unsigned short pm_port; |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 33 | struct rpc_xprt * pm_xprt; |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 34 | }; |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | static struct rpc_procinfo pmap_procedures[]; |
Chuck Lever | 6cd7525 | 2005-09-22 21:24:59 -0400 | [diff] [blame] | 37 | static struct rpc_clnt * pmap_create(char *, struct sockaddr_in *, int, int); |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 38 | static void pmap_getport_done(struct rpc_task *, void *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | static struct rpc_program pmap_program; |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 40 | |
| 41 | static void pmap_getport_prepare(struct rpc_task *task, void *calldata) |
| 42 | { |
| 43 | struct portmap_args *map = calldata; |
| 44 | struct rpc_message msg = { |
| 45 | .rpc_proc = &pmap_procedures[PMAP_GETPORT], |
| 46 | .rpc_argp = map, |
| 47 | .rpc_resp = &map->pm_port, |
| 48 | }; |
| 49 | |
| 50 | rpc_call_setup(task, &msg, 0); |
| 51 | } |
| 52 | |
| 53 | static inline struct portmap_args *pmap_map_alloc(void) |
| 54 | { |
| 55 | return kmalloc(sizeof(struct portmap_args), GFP_NOFS); |
| 56 | } |
| 57 | |
| 58 | static inline void pmap_map_free(struct portmap_args *map) |
| 59 | { |
| 60 | kfree(map); |
| 61 | } |
| 62 | |
| 63 | static void pmap_map_release(void *data) |
| 64 | { |
| 65 | pmap_map_free(data); |
| 66 | } |
| 67 | |
| 68 | static const struct rpc_call_ops pmap_getport_ops = { |
| 69 | .rpc_call_prepare = pmap_getport_prepare, |
| 70 | .rpc_call_done = pmap_getport_done, |
| 71 | .rpc_release = pmap_map_release, |
| 72 | }; |
| 73 | |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 74 | static inline void pmap_wake_portmap_waiters(struct rpc_xprt *xprt, int status) |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 75 | { |
| 76 | xprt_clear_binding(xprt); |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 77 | rpc_wake_up_status(&xprt->binding, status); |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 78 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Chuck Lever | c4a5692 | 2006-08-22 20:06:16 -0400 | [diff] [blame] | 80 | /** |
| 81 | * rpc_getport - obtain the port for a given RPC service on a given host |
| 82 | * @task: task that is waiting for portmapper request |
Chuck Lever | c4a5692 | 2006-08-22 20:06:16 -0400 | [diff] [blame] | 83 | * |
| 84 | * This one can be called for an ongoing RPC request, and can be used in |
| 85 | * an async (rpciod) context. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | */ |
Chuck Lever | bbf7c1d | 2006-08-22 20:06:16 -0400 | [diff] [blame] | 87 | void rpc_getport(struct rpc_task *task) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
Chuck Lever | bbf7c1d | 2006-08-22 20:06:16 -0400 | [diff] [blame] | 89 | struct rpc_clnt *clnt = task->tk_client; |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 90 | struct rpc_xprt *xprt = task->tk_xprt; |
Chuck Lever | 081f79a | 2006-08-22 20:06:17 -0400 | [diff] [blame] | 91 | struct sockaddr_in addr; |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 92 | struct portmap_args *map; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | struct rpc_clnt *pmap_clnt; |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 94 | struct rpc_task *child; |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 95 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 97 | dprintk("RPC: %4d rpc_getport(%s, %u, %u, %d)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | task->tk_pid, clnt->cl_server, |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 99 | clnt->cl_prog, clnt->cl_vers, xprt->prot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Andreas Gruenbacher | 007e251 | 2005-06-22 17:16:23 +0000 | [diff] [blame] | 101 | /* Autobind on cloned rpc clients is discouraged */ |
| 102 | BUG_ON(clnt->cl_parent != clnt); |
| 103 | |
Chuck Lever | 71bdcf8 | 2006-10-19 23:28:43 -0700 | [diff] [blame] | 104 | /* Put self on queue before sending rpcbind request, in case |
| 105 | * pmap_getport_done completes before we return from rpc_run_task */ |
| 106 | rpc_sleep_on(&xprt->binding, task, NULL, NULL); |
| 107 | |
| 108 | status = -EACCES; /* tell caller to check again */ |
| 109 | if (xprt_test_and_set_binding(xprt)) |
| 110 | goto bailout_nofree; |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 111 | |
| 112 | /* Someone else may have bound if we slept */ |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 113 | status = 0; |
| 114 | if (xprt_bound(xprt)) |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 115 | goto bailout_nofree; |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 116 | |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 117 | status = -ENOMEM; |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 118 | map = pmap_map_alloc(); |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 119 | if (!map) |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 120 | goto bailout_nofree; |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 121 | map->pm_prog = clnt->cl_prog; |
| 122 | map->pm_vers = clnt->cl_vers; |
| 123 | map->pm_prot = xprt->prot; |
| 124 | map->pm_port = 0; |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 125 | map->pm_xprt = xprt_get(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Chuck Lever | 081f79a | 2006-08-22 20:06:17 -0400 | [diff] [blame] | 127 | rpc_peeraddr(clnt, (struct sockaddr *) &addr, sizeof(addr)); |
| 128 | pmap_clnt = pmap_create(clnt->cl_server, &addr, map->pm_prot, 0); |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 129 | status = PTR_ERR(pmap_clnt); |
| 130 | if (IS_ERR(pmap_clnt)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | goto bailout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 133 | status = -EIO; |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 134 | child = rpc_run_task(pmap_clnt, RPC_TASK_ASYNC, &pmap_getport_ops, map); |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 135 | if (IS_ERR(child)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | goto bailout; |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 137 | rpc_release_task(child); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Chuck Lever | 262ca07 | 2006-03-20 13:44:16 -0500 | [diff] [blame] | 139 | task->tk_xprt->stat.bind_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | return; |
| 141 | |
| 142 | bailout: |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 143 | pmap_map_free(map); |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 144 | xprt_put(xprt); |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 145 | bailout_nofree: |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 146 | task->tk_status = status; |
| 147 | pmap_wake_portmap_waiters(xprt, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | #ifdef CONFIG_ROOT_NFS |
Chuck Lever | c4a5692 | 2006-08-22 20:06:16 -0400 | [diff] [blame] | 151 | /** |
| 152 | * rpc_getport_external - obtain the port for a given RPC service on a given host |
| 153 | * @sin: address of remote peer |
| 154 | * @prog: RPC program number to bind |
| 155 | * @vers: RPC version number to bind |
| 156 | * @prot: transport protocol to use to make this request |
| 157 | * |
| 158 | * This one is called from outside the RPC client in a synchronous task context. |
| 159 | */ |
| 160 | int rpc_getport_external(struct sockaddr_in *sin, __u32 prog, __u32 vers, int prot) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 162 | struct portmap_args map = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | .pm_prog = prog, |
| 164 | .pm_vers = vers, |
| 165 | .pm_prot = prot, |
| 166 | .pm_port = 0 |
| 167 | }; |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 168 | struct rpc_message msg = { |
| 169 | .rpc_proc = &pmap_procedures[PMAP_GETPORT], |
| 170 | .rpc_argp = &map, |
| 171 | .rpc_resp = &map.pm_port, |
| 172 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | struct rpc_clnt *pmap_clnt; |
| 174 | char hostname[32]; |
| 175 | int status; |
| 176 | |
Chuck Lever | c4a5692 | 2006-08-22 20:06:16 -0400 | [diff] [blame] | 177 | dprintk("RPC: rpc_getport_external(%u.%u.%u.%u, %u, %u, %d)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot); |
| 179 | |
| 180 | sprintf(hostname, "%u.%u.%u.%u", NIPQUAD(sin->sin_addr.s_addr)); |
Chuck Lever | 6cd7525 | 2005-09-22 21:24:59 -0400 | [diff] [blame] | 181 | pmap_clnt = pmap_create(hostname, sin, prot, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | if (IS_ERR(pmap_clnt)) |
| 183 | return PTR_ERR(pmap_clnt); |
| 184 | |
| 185 | /* Setup the call info struct */ |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 186 | status = rpc_call_sync(pmap_clnt, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | |
| 188 | if (status >= 0) { |
| 189 | if (map.pm_port != 0) |
| 190 | return map.pm_port; |
| 191 | status = -EACCES; |
| 192 | } |
| 193 | return status; |
| 194 | } |
| 195 | #endif |
| 196 | |
Chuck Lever | c4a5692 | 2006-08-22 20:06:16 -0400 | [diff] [blame] | 197 | /* |
| 198 | * Portmapper child task invokes this callback via tk_exit. |
| 199 | */ |
| 200 | static void pmap_getport_done(struct rpc_task *child, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 202 | struct portmap_args *map = data; |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 203 | struct rpc_xprt *xprt = map->pm_xprt; |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 204 | int status = child->tk_status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 206 | if (status < 0) { |
| 207 | /* Portmapper not available */ |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 208 | xprt->ops->set_port(xprt, 0); |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 209 | } else if (map->pm_port == 0) { |
| 210 | /* Requested RPC service wasn't registered */ |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 211 | xprt->ops->set_port(xprt, 0); |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 212 | status = -EACCES; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } else { |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 214 | /* Succeeded */ |
| 215 | xprt->ops->set_port(xprt, map->pm_port); |
Chuck Lever | ec739ef | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 216 | xprt_set_bound(xprt); |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 217 | status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 219 | |
| 220 | dprintk("RPC: %4d pmap_getport_done(status %d, port %u)\n", |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 221 | child->tk_pid, status, map->pm_port); |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 222 | |
Trond Myklebust | 762d452 | 2006-09-03 00:51:55 -0400 | [diff] [blame] | 223 | pmap_wake_portmap_waiters(xprt, status); |
| 224 | xprt_put(xprt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Chuck Lever | c4a5692 | 2006-08-22 20:06:16 -0400 | [diff] [blame] | 227 | /** |
| 228 | * rpc_register - set or unset a port registration with the local portmapper |
| 229 | * @prog: RPC program number to bind |
| 230 | * @vers: RPC version number to bind |
| 231 | * @prot: transport protocol to use to make this request |
| 232 | * @port: port value to register |
| 233 | * @okay: result code |
| 234 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | * port == 0 means unregister, port != 0 means register. |
| 236 | */ |
Chuck Lever | c4a5692 | 2006-08-22 20:06:16 -0400 | [diff] [blame] | 237 | int rpc_register(u32 prog, u32 vers, int prot, unsigned short port, int *okay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 239 | struct sockaddr_in sin = { |
| 240 | .sin_family = AF_INET, |
| 241 | .sin_addr.s_addr = htonl(INADDR_LOOPBACK), |
| 242 | }; |
Chuck Lever | 4a68179 | 2006-08-22 20:06:15 -0400 | [diff] [blame] | 243 | struct portmap_args map = { |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 244 | .pm_prog = prog, |
| 245 | .pm_vers = vers, |
| 246 | .pm_prot = prot, |
| 247 | .pm_port = port, |
| 248 | }; |
| 249 | struct rpc_message msg = { |
| 250 | .rpc_proc = &pmap_procedures[port ? PMAP_SET : PMAP_UNSET], |
| 251 | .rpc_argp = &map, |
| 252 | .rpc_resp = okay, |
| 253 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | struct rpc_clnt *pmap_clnt; |
| 255 | int error = 0; |
| 256 | |
Chuck Lever | c4a5692 | 2006-08-22 20:06:16 -0400 | [diff] [blame] | 257 | dprintk("RPC: registering (%u, %u, %d, %u) with portmapper.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | prog, vers, prot, port); |
| 259 | |
Chuck Lever | 6cd7525 | 2005-09-22 21:24:59 -0400 | [diff] [blame] | 260 | pmap_clnt = pmap_create("localhost", &sin, IPPROTO_UDP, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | if (IS_ERR(pmap_clnt)) { |
| 262 | error = PTR_ERR(pmap_clnt); |
| 263 | dprintk("RPC: couldn't create pmap client. Error = %d\n", error); |
| 264 | return error; |
| 265 | } |
| 266 | |
Chuck Lever | dead28d | 2006-03-20 13:44:23 -0500 | [diff] [blame] | 267 | error = rpc_call_sync(pmap_clnt, &msg, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
| 269 | if (error < 0) { |
| 270 | printk(KERN_WARNING |
| 271 | "RPC: failed to contact portmap (errno %d).\n", |
| 272 | error); |
| 273 | } |
| 274 | dprintk("RPC: registration status %d/%d\n", error, *okay); |
| 275 | |
| 276 | /* Client deleted automatically because cl_oneshot == 1 */ |
| 277 | return error; |
| 278 | } |
| 279 | |
Chuck Lever | c4a5692 | 2006-08-22 20:06:16 -0400 | [diff] [blame] | 280 | static struct rpc_clnt *pmap_create(char *hostname, struct sockaddr_in *srvaddr, int proto, int privileged) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { |
Chuck Lever | 9e1968c | 2006-08-22 20:06:21 -0400 | [diff] [blame] | 282 | struct rpc_create_args args = { |
| 283 | .protocol = proto, |
| 284 | .address = (struct sockaddr *)srvaddr, |
| 285 | .addrsize = sizeof(*srvaddr), |
| 286 | .servername = hostname, |
| 287 | .program = &pmap_program, |
| 288 | .version = RPC_PMAP_VERSION, |
| 289 | .authflavor = RPC_AUTH_UNIX, |
| 290 | .flags = (RPC_CLNT_CREATE_ONESHOT | |
| 291 | RPC_CLNT_CREATE_NOPING), |
| 292 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
Chuck Lever | 9e1968c | 2006-08-22 20:06:21 -0400 | [diff] [blame] | 294 | srvaddr->sin_port = htons(RPC_PMAP_PORT); |
Chuck Lever | 6cd7525 | 2005-09-22 21:24:59 -0400 | [diff] [blame] | 295 | if (!privileged) |
Chuck Lever | 9e1968c | 2006-08-22 20:06:21 -0400 | [diff] [blame] | 296 | args.flags |= RPC_CLNT_CREATE_NONPRIVPORT; |
| 297 | return rpc_create(&args); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | /* |
| 301 | * XDR encode/decode functions for PMAP |
| 302 | */ |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 303 | static int xdr_encode_mapping(struct rpc_rqst *req, __be32 *p, struct portmap_args *map) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
Chuck Lever | c4a5692 | 2006-08-22 20:06:16 -0400 | [diff] [blame] | 305 | dprintk("RPC: xdr_encode_mapping(%u, %u, %u, %u)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | map->pm_prog, map->pm_vers, map->pm_prot, map->pm_port); |
| 307 | *p++ = htonl(map->pm_prog); |
| 308 | *p++ = htonl(map->pm_vers); |
| 309 | *p++ = htonl(map->pm_prot); |
| 310 | *p++ = htonl(map->pm_port); |
| 311 | |
| 312 | req->rq_slen = xdr_adjust_iovec(req->rq_svec, p); |
| 313 | return 0; |
| 314 | } |
| 315 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 316 | static int xdr_decode_port(struct rpc_rqst *req, __be32 *p, unsigned short *portp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | { |
| 318 | *portp = (unsigned short) ntohl(*p++); |
| 319 | return 0; |
| 320 | } |
| 321 | |
Alexey Dobriyan | d8ed029 | 2006-09-26 22:29:38 -0700 | [diff] [blame] | 322 | static int xdr_decode_bool(struct rpc_rqst *req, __be32 *p, unsigned int *boolp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | { |
| 324 | *boolp = (unsigned int) ntohl(*p++); |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static struct rpc_procinfo pmap_procedures[] = { |
| 329 | [PMAP_SET] = { |
| 330 | .p_proc = PMAP_SET, |
| 331 | .p_encode = (kxdrproc_t) xdr_encode_mapping, |
| 332 | .p_decode = (kxdrproc_t) xdr_decode_bool, |
| 333 | .p_bufsiz = 4, |
| 334 | .p_count = 1, |
Chuck Lever | cc0175c | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 335 | .p_statidx = PMAP_SET, |
| 336 | .p_name = "SET", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | }, |
| 338 | [PMAP_UNSET] = { |
| 339 | .p_proc = PMAP_UNSET, |
| 340 | .p_encode = (kxdrproc_t) xdr_encode_mapping, |
| 341 | .p_decode = (kxdrproc_t) xdr_decode_bool, |
| 342 | .p_bufsiz = 4, |
| 343 | .p_count = 1, |
Chuck Lever | cc0175c | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 344 | .p_statidx = PMAP_UNSET, |
| 345 | .p_name = "UNSET", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | }, |
| 347 | [PMAP_GETPORT] = { |
| 348 | .p_proc = PMAP_GETPORT, |
| 349 | .p_encode = (kxdrproc_t) xdr_encode_mapping, |
| 350 | .p_decode = (kxdrproc_t) xdr_decode_port, |
| 351 | .p_bufsiz = 4, |
| 352 | .p_count = 1, |
Chuck Lever | cc0175c | 2006-03-20 13:44:22 -0500 | [diff] [blame] | 353 | .p_statidx = PMAP_GETPORT, |
| 354 | .p_name = "GETPORT", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | }, |
| 356 | }; |
| 357 | |
| 358 | static struct rpc_version pmap_version2 = { |
| 359 | .number = 2, |
| 360 | .nrprocs = 4, |
| 361 | .procs = pmap_procedures |
| 362 | }; |
| 363 | |
| 364 | static struct rpc_version * pmap_version[] = { |
| 365 | NULL, |
| 366 | NULL, |
| 367 | &pmap_version2 |
| 368 | }; |
| 369 | |
| 370 | static struct rpc_stat pmap_stats; |
| 371 | |
| 372 | static struct rpc_program pmap_program = { |
| 373 | .name = "portmap", |
| 374 | .number = RPC_PMAP_PROGRAM, |
| 375 | .nrvers = ARRAY_SIZE(pmap_version), |
| 376 | .version = pmap_version, |
| 377 | .stats = &pmap_stats, |
| 378 | }; |