15 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/socket.h>
21 #include <linux/in6.h>
22 #include <linux/kernel.h>
23 #include <linux/errno.h>
25 #include <linux/slab.h>
36 # define RPCDBG_FACILITY RPCDBG_BIND
39 #define RPCBIND_SOCK_PATHNAME "/var/run/rpcbind.sock"
41 #define RPCBIND_PROGRAM (100000u)
42 #define RPCBIND_PORT (111u)
44 #define RPCBVERS_2 (2u)
45 #define RPCBVERS_3 (3u)
46 #define RPCBVERS_4 (4u)
79 #define RPCB_OWNER_STRING "0"
80 #define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
85 #define RPCB_program_sz (1)
86 #define RPCB_version_sz (1)
87 #define RPCB_protocol_sz (1)
88 #define RPCB_port_sz (1)
89 #define RPCB_boolean_sz (1)
91 #define RPCB_netid_sz (1 + XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
92 #define RPCB_addr_sz (1 + XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
93 #define RPCB_ownerstring_sz (1 + XDR_QUADLEN(RPCB_MAXOWNERLEN))
98 #define RPCB_mappingargs_sz (RPCB_program_sz + RPCB_version_sz + \
99 RPCB_protocol_sz + RPCB_port_sz)
100 #define RPCB_getaddrargs_sz (RPCB_program_sz + RPCB_version_sz + \
101 RPCB_netid_sz + RPCB_addr_sz + \
104 #define RPCB_getportres_sz RPCB_port_sz
105 #define RPCB_setres_sz RPCB_boolean_sz
111 #define RPCB_getaddrres_sz RPCB_addr_sz
113 static void rpcb_getport_done(
struct rpc_task *,
void *);
114 static void rpcb_map_release(
void *
data);
140 static const struct rpcb_info rpcb_next_version[];
141 static const struct rpcb_info rpcb_next_version6[];
144 .rpc_call_done = rpcb_getport_done,
145 .rpc_release = rpcb_map_release,
148 static void rpcb_wake_rpcbind_waiters(
struct rpc_xprt *xprt,
int status)
150 xprt_clear_binding(xprt);
154 static void rpcb_map_release(
void *
data)
164 static int rpcb_get_local(
struct net *
net)
206 static void rpcb_set_local(
struct net *net,
struct rpc_clnt *clnt,
216 dprintk(
"RPC: created new rpcb local clients (rpcb_local_clnt: "
217 "%p, rpcb_local_clnt4: %p) for net %p%s\n",
219 net, (net == &
init_net) ?
" (init_net)" :
"");
226 static int rpcb_create_local_unix(
struct net *net)
228 static const struct sockaddr_un rpcb_localaddr_rpcbind = {
232 struct rpc_create_args
args = {
234 .protocol = XPRT_TRANSPORT_LOCAL,
235 .address = (
struct sockaddr *)&rpcb_localaddr_rpcbind,
236 .addrsize =
sizeof(rpcb_localaddr_rpcbind),
237 .servername =
"localhost",
238 .program = &rpcb_program,
240 .authflavor = RPC_AUTH_NULL,
252 dprintk(
"RPC: failed to create AF_LOCAL rpcbind "
253 "client (errno %ld).\n", PTR_ERR(clnt));
254 result = PTR_ERR(clnt);
260 dprintk(
"RPC: failed to bind second program to "
261 "rpcbind v4 client (errno %ld).\n",
266 rpcb_set_local(net, clnt, clnt4);
276 static int rpcb_create_local_net(
struct net *net)
278 static const struct sockaddr_in rpcb_inaddr_loopback = {
283 struct rpc_create_args args = {
285 .protocol = XPRT_TRANSPORT_TCP,
286 .address = (
struct sockaddr *)&rpcb_inaddr_loopback,
287 .addrsize =
sizeof(rpcb_inaddr_loopback),
288 .servername =
"localhost",
289 .program = &rpcb_program,
291 .authflavor = RPC_AUTH_UNIX,
292 .flags = RPC_CLNT_CREATE_NOPING,
299 dprintk(
"RPC: failed to create local rpcbind "
300 "client (errno %ld).\n", PTR_ERR(clnt));
301 result = PTR_ERR(clnt);
312 dprintk(
"RPC: failed to bind second program to "
313 "rpcbind v4 client (errno %ld).\n",
318 rpcb_set_local(net, clnt, clnt4);
333 if (rpcb_get_local(net))
337 if (rpcb_get_local(net))
340 if (rpcb_create_local_unix(net) != 0)
341 result = rpcb_create_local_net(net);
348 static struct rpc_clnt *rpcb_create(
struct net *net,
const char *hostname,
349 struct sockaddr *srvaddr,
size_t salen,
352 struct rpc_create_args args = {
357 .servername = hostname,
358 .program = &rpcb_program,
360 .authflavor = RPC_AUTH_UNIX,
361 .flags = (RPC_CLNT_CREATE_NOPING |
362 RPC_CLNT_CREATE_NONPRIVPORT),
387 dprintk(
"RPC: failed to contact local rpcbind "
388 "server (errno %d).\n", -error);
443 dprintk(
"RPC: %sregistering (%u, %u, %d, %u) with local "
444 "rpcbind\n", (port ?
"" :
"un"),
445 prog, vers, prot, port);
457 static int rpcb_register_inet4(
struct sunrpc_net *sn,
468 dprintk(
"RPC: %sregistering [%u, %u, %s, '%s'] with "
469 "local rpcbind\n", (port ?
"" :
"un"),
485 static int rpcb_register_inet6(
struct sunrpc_net *sn,
496 dprintk(
"RPC: %sregistering [%u, %u, %s, '%s'] with "
497 "local rpcbind\n", (port ?
"" :
"un"),
510 static int rpcb_unregister_all_protofamilies(
struct sunrpc_net *sn,
515 dprintk(
"RPC: unregistering [%u, %u, '%s'] with "
587 return rpcb_unregister_all_protofamilies(sn, &msg);
591 return rpcb_register_inet4(sn, address, &msg);
593 return rpcb_register_inet6(sn, address, &msg);
609 .callback_ops = &rpcb_getport_ops,
610 .callback_data =
map,
629 while (parent != clnt) {
652 struct rpc_xprt *xprt;
663 clnt = rpcb_find_transport_owner(task->
tk_client);
665 }
while (xprt ==
NULL);
668 dprintk(
"RPC: %5u %s(%s, %u, %u, %d)\n",
669 task->tk_pid, __func__,
676 if (xprt_test_and_set_binding(xprt)) {
677 dprintk(
"RPC: %5u %s: waiting for another binder\n",
678 task->tk_pid, __func__);
684 if (xprt_bound(xprt)) {
686 dprintk(
"RPC: %5u %s: already bound\n",
687 task->tk_pid, __func__);
697 proc = rpcb_next_version[xprt->bind_index].rpc_proc;
698 bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
701 proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
702 bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
706 dprintk(
"RPC: %5u %s: bad address family\n",
707 task->tk_pid, __func__);
711 xprt->bind_index = 0;
713 dprintk(
"RPC: %5u %s: no more getport versions available\n",
714 task->tk_pid, __func__);
718 dprintk(
"RPC: %5u %s: trying rpcbind version %u\n",
719 task->tk_pid, __func__, bind_version);
721 rpcb_clnt = rpcb_create(xprt->xprt_net, xprt->servername, sap, salen,
722 xprt->prot, bind_version);
723 if (IS_ERR(rpcb_clnt)) {
724 status = PTR_ERR(rpcb_clnt);
725 dprintk(
"RPC: %5u %s: rpcb_create failed, error %ld\n",
726 task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
733 dprintk(
"RPC: %5u %s: no memory available\n",
734 task->tk_pid, __func__);
735 goto bailout_release_client;
744 switch (bind_version) {
747 map->
r_netid = xprt->address_strings[RPC_DISPLAY_NETID];
758 child = rpcb_call_async(rpcb_clnt, map, proc);
762 dprintk(
"RPC: %5u %s: rpc_run_task failed\n",
763 task->tk_pid, __func__);
767 xprt->stat.bind_count++;
771 bailout_release_client:
774 rpcb_wake_rpcbind_waiters(xprt, status);
786 struct rpc_xprt *xprt = map->
r_xprt;
799 xprt->ops->set_port(xprt, 0);
800 }
else if (map->
r_port == 0) {
802 xprt->ops->set_port(xprt, 0);
806 xprt->ops->set_port(xprt, map->
r_port);
807 xprt_set_bound(xprt);
811 dprintk(
"RPC: %5u rpcb_getport_done(status %d, port %u)\n",
812 child->tk_pid, status, map->
r_port);
821 static void rpcb_enc_mapping(
struct rpc_rqst *
req,
struct xdr_stream *xdr,
826 dprintk(
"RPC: %5u encoding PMAP_%s call (%u, %u, %d, %u)\n",
827 req->rq_task->tk_pid,
828 req->rq_task->tk_msg.rpc_proc->p_name,
838 static int rpcb_dec_getport(
struct rpc_rqst *
req,
struct xdr_stream *xdr,
851 dprintk(
"RPC: %5u PMAP_%s result: %lu\n", req->rq_task->tk_pid,
852 req->rq_task->tk_msg.rpc_proc->p_name, port);
860 static int rpcb_dec_set(
struct rpc_rqst *req,
struct xdr_stream *xdr,
873 dprintk(
"RPC: %5u RPCB_%s call %s\n",
874 req->rq_task->tk_pid,
875 req->rq_task->tk_msg.rpc_proc->p_name,
876 (*boolp ?
"succeeded" :
"failed"));
880 static void encode_rpcb_string(
struct xdr_stream *xdr,
const char *
string,
892 static void rpcb_enc_getaddr(
struct rpc_rqst *req,
struct xdr_stream *xdr,
897 dprintk(
"RPC: %5u encoding RPCB_%s call (%u, %u, '%s', '%s')\n",
898 req->rq_task->tk_pid,
899 req->rq_task->tk_msg.rpc_proc->p_name,
907 encode_rpcb_string(xdr, rpcb->
r_netid, RPCBIND_MAXNETIDLEN);
908 encode_rpcb_string(xdr, rpcb->
r_addr, RPCBIND_MAXUADDRLEN);
912 static int rpcb_dec_getaddr(
struct rpc_rqst *req,
struct xdr_stream *xdr,
932 dprintk(
"RPC: %5u RPCB reply: program not registered\n",
933 req->rq_task->tk_pid);
937 if (
unlikely(len > RPCBIND_MAXUADDRLEN))
943 dprintk(
"RPC: %5u RPCB_%s reply: %s\n", req->rq_task->tk_pid,
944 req->rq_task->tk_msg.rpc_proc->p_name, (
char *)p);
949 rpcb->
r_port = rpc_get_port(sap);
954 dprintk(
"RPC: %5u malformed RPCB_%s reply\n",
955 req->rq_task->tk_pid,
956 req->rq_task->tk_msg.rpc_proc->p_name);
968 .p_encode = (kxdreproc_t)rpcb_enc_mapping,
969 .
p_decode = (kxdrdproc_t)rpcb_dec_set,
978 .p_encode = (kxdreproc_t)rpcb_enc_mapping,
979 .
p_decode = (kxdrdproc_t)rpcb_dec_set,
988 .p_encode = (kxdreproc_t)rpcb_enc_mapping,
989 .
p_decode = (kxdrdproc_t)rpcb_dec_getport,
1001 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
1002 .
p_decode = (kxdrdproc_t)rpcb_dec_set,
1011 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
1012 .
p_decode = (kxdrdproc_t)rpcb_dec_set,
1021 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
1022 .
p_decode = (kxdrdproc_t)rpcb_dec_getaddr,
1027 .p_name =
"GETADDR",
1034 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
1035 .
p_decode = (kxdrdproc_t)rpcb_dec_set,
1044 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
1045 .
p_decode = (kxdrdproc_t)rpcb_dec_set,
1054 .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
1055 .
p_decode = (kxdrdproc_t)rpcb_dec_getaddr,
1060 .p_name =
"GETADDR",
1064 static const struct rpcb_info rpcb_next_version[] = {
1074 static const struct rpcb_info rpcb_next_version6[] = {
1091 .procs = rpcb_procedures2
1097 .procs = rpcb_procedures3
1103 .procs = rpcb_procedures4
1106 static const struct rpc_version *rpcb_version[] = {
1120 .version = rpcb_version,
1121 .stats = &rpcb_stats,