/net/sunrpc/rpcb_clnt.c

http://github.com/mirrors/linux · C · 1176 lines · 839 code · 144 blank · 193 comment · 73 complexity · 0145c07dc6f5c92fcd0214c5426c9fc1 MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * In-kernel rpcbind client supporting versions 2, 3, and 4 of the rpcbind
  4. * protocol
  5. *
  6. * Based on RFC 1833: "Binding Protocols for ONC RPC Version 2" and
  7. * RFC 3530: "Network File System (NFS) version 4 Protocol"
  8. *
  9. * Original: Gilles Quillard, Bull Open Source, 2005 <gilles.quillard@bull.net>
  10. * Updated: Chuck Lever, Oracle Corporation, 2007 <chuck.lever@oracle.com>
  11. *
  12. * Descended from net/sunrpc/pmap_clnt.c,
  13. * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
  14. */
  15. #include <linux/module.h>
  16. #include <linux/types.h>
  17. #include <linux/socket.h>
  18. #include <linux/un.h>
  19. #include <linux/in.h>
  20. #include <linux/in6.h>
  21. #include <linux/kernel.h>
  22. #include <linux/errno.h>
  23. #include <linux/mutex.h>
  24. #include <linux/slab.h>
  25. #include <net/ipv6.h>
  26. #include <linux/sunrpc/clnt.h>
  27. #include <linux/sunrpc/addr.h>
  28. #include <linux/sunrpc/sched.h>
  29. #include <linux/sunrpc/xprtsock.h>
  30. #include "netns.h"
  31. #if IS_ENABLED(CONFIG_SUNRPC_DEBUG)
  32. # define RPCDBG_FACILITY RPCDBG_BIND
  33. #endif
  34. #define RPCBIND_SOCK_PATHNAME "/var/run/rpcbind.sock"
  35. #define RPCBIND_PROGRAM (100000u)
  36. #define RPCBIND_PORT (111u)
  37. #define RPCBVERS_2 (2u)
  38. #define RPCBVERS_3 (3u)
  39. #define RPCBVERS_4 (4u)
  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. * r_owner
  59. *
  60. * The "owner" is allowed to unset a service in the rpcbind database.
  61. *
  62. * For AF_LOCAL SET/UNSET requests, rpcbind treats this string as a
  63. * UID which it maps to a local user name via a password lookup.
  64. * In all other cases it is ignored.
  65. *
  66. * For SET/UNSET requests, user space provides a value, even for
  67. * network requests, and GETADDR uses an empty string. We follow
  68. * those precedents here.
  69. */
  70. #define RPCB_OWNER_STRING "0"
  71. #define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
  72. /*
  73. * XDR data type sizes
  74. */
  75. #define RPCB_program_sz (1)
  76. #define RPCB_version_sz (1)
  77. #define RPCB_protocol_sz (1)
  78. #define RPCB_port_sz (1)
  79. #define RPCB_boolean_sz (1)
  80. #define RPCB_netid_sz (1 + XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
  81. #define RPCB_addr_sz (1 + XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
  82. #define RPCB_ownerstring_sz (1 + XDR_QUADLEN(RPCB_MAXOWNERLEN))
  83. /*
  84. * XDR argument and result sizes
  85. */
  86. #define RPCB_mappingargs_sz (RPCB_program_sz + RPCB_version_sz + \
  87. RPCB_protocol_sz + RPCB_port_sz)
  88. #define RPCB_getaddrargs_sz (RPCB_program_sz + RPCB_version_sz + \
  89. RPCB_netid_sz + RPCB_addr_sz + \
  90. RPCB_ownerstring_sz)
  91. #define RPCB_getportres_sz RPCB_port_sz
  92. #define RPCB_setres_sz RPCB_boolean_sz
  93. /*
  94. * Note that RFC 1833 does not put any size restrictions on the
  95. * address string returned by the remote rpcbind database.
  96. */
  97. #define RPCB_getaddrres_sz RPCB_addr_sz
  98. static void rpcb_getport_done(struct rpc_task *, void *);
  99. static void rpcb_map_release(void *data);
  100. static const struct rpc_program rpcb_program;
  101. struct rpcbind_args {
  102. struct rpc_xprt * r_xprt;
  103. u32 r_prog;
  104. u32 r_vers;
  105. u32 r_prot;
  106. unsigned short r_port;
  107. const char * r_netid;
  108. const char * r_addr;
  109. const char * r_owner;
  110. int r_status;
  111. };
  112. static const struct rpc_procinfo rpcb_procedures2[];
  113. static const struct rpc_procinfo rpcb_procedures3[];
  114. static const struct rpc_procinfo rpcb_procedures4[];
  115. struct rpcb_info {
  116. u32 rpc_vers;
  117. const struct rpc_procinfo *rpc_proc;
  118. };
  119. static const struct rpcb_info rpcb_next_version[];
  120. static const struct rpcb_info rpcb_next_version6[];
  121. static const struct rpc_call_ops rpcb_getport_ops = {
  122. .rpc_call_done = rpcb_getport_done,
  123. .rpc_release = rpcb_map_release,
  124. };
  125. static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status)
  126. {
  127. xprt_clear_binding(xprt);
  128. rpc_wake_up_status(&xprt->binding, status);
  129. }
  130. static void rpcb_map_release(void *data)
  131. {
  132. struct rpcbind_args *map = data;
  133. rpcb_wake_rpcbind_waiters(map->r_xprt, map->r_status);
  134. xprt_put(map->r_xprt);
  135. kfree(map->r_addr);
  136. kfree(map);
  137. }
  138. static int rpcb_get_local(struct net *net)
  139. {
  140. int cnt;
  141. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  142. spin_lock(&sn->rpcb_clnt_lock);
  143. if (sn->rpcb_users)
  144. sn->rpcb_users++;
  145. cnt = sn->rpcb_users;
  146. spin_unlock(&sn->rpcb_clnt_lock);
  147. return cnt;
  148. }
  149. void rpcb_put_local(struct net *net)
  150. {
  151. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  152. struct rpc_clnt *clnt = sn->rpcb_local_clnt;
  153. struct rpc_clnt *clnt4 = sn->rpcb_local_clnt4;
  154. int shutdown = 0;
  155. spin_lock(&sn->rpcb_clnt_lock);
  156. if (sn->rpcb_users) {
  157. if (--sn->rpcb_users == 0) {
  158. sn->rpcb_local_clnt = NULL;
  159. sn->rpcb_local_clnt4 = NULL;
  160. }
  161. shutdown = !sn->rpcb_users;
  162. }
  163. spin_unlock(&sn->rpcb_clnt_lock);
  164. if (shutdown) {
  165. /*
  166. * cleanup_rpcb_clnt - remove xprtsock's sysctls, unregister
  167. */
  168. if (clnt4)
  169. rpc_shutdown_client(clnt4);
  170. if (clnt)
  171. rpc_shutdown_client(clnt);
  172. }
  173. }
  174. static void rpcb_set_local(struct net *net, struct rpc_clnt *clnt,
  175. struct rpc_clnt *clnt4,
  176. bool is_af_local)
  177. {
  178. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  179. /* Protected by rpcb_create_local_mutex */
  180. sn->rpcb_local_clnt = clnt;
  181. sn->rpcb_local_clnt4 = clnt4;
  182. sn->rpcb_is_af_local = is_af_local ? 1 : 0;
  183. smp_wmb();
  184. sn->rpcb_users = 1;
  185. dprintk("RPC: created new rpcb local clients (rpcb_local_clnt: "
  186. "%p, rpcb_local_clnt4: %p) for net %x%s\n",
  187. sn->rpcb_local_clnt, sn->rpcb_local_clnt4,
  188. net->ns.inum, (net == &init_net) ? " (init_net)" : "");
  189. }
  190. /*
  191. * Returns zero on success, otherwise a negative errno value
  192. * is returned.
  193. */
  194. static int rpcb_create_local_unix(struct net *net)
  195. {
  196. static const struct sockaddr_un rpcb_localaddr_rpcbind = {
  197. .sun_family = AF_LOCAL,
  198. .sun_path = RPCBIND_SOCK_PATHNAME,
  199. };
  200. struct rpc_create_args args = {
  201. .net = net,
  202. .protocol = XPRT_TRANSPORT_LOCAL,
  203. .address = (struct sockaddr *)&rpcb_localaddr_rpcbind,
  204. .addrsize = sizeof(rpcb_localaddr_rpcbind),
  205. .servername = "localhost",
  206. .program = &rpcb_program,
  207. .version = RPCBVERS_2,
  208. .authflavor = RPC_AUTH_NULL,
  209. .cred = current_cred(),
  210. /*
  211. * We turn off the idle timeout to prevent the kernel
  212. * from automatically disconnecting the socket.
  213. * Otherwise, we'd have to cache the mount namespace
  214. * of the caller and somehow pass that to the socket
  215. * reconnect code.
  216. */
  217. .flags = RPC_CLNT_CREATE_NO_IDLE_TIMEOUT,
  218. };
  219. struct rpc_clnt *clnt, *clnt4;
  220. int result = 0;
  221. /*
  222. * Because we requested an RPC PING at transport creation time,
  223. * this works only if the user space portmapper is rpcbind, and
  224. * it's listening on AF_LOCAL on the named socket.
  225. */
  226. clnt = rpc_create(&args);
  227. if (IS_ERR(clnt)) {
  228. dprintk("RPC: failed to create AF_LOCAL rpcbind "
  229. "client (errno %ld).\n", PTR_ERR(clnt));
  230. result = PTR_ERR(clnt);
  231. goto out;
  232. }
  233. clnt4 = rpc_bind_new_program(clnt, &rpcb_program, RPCBVERS_4);
  234. if (IS_ERR(clnt4)) {
  235. dprintk("RPC: failed to bind second program to "
  236. "rpcbind v4 client (errno %ld).\n",
  237. PTR_ERR(clnt4));
  238. clnt4 = NULL;
  239. }
  240. rpcb_set_local(net, clnt, clnt4, true);
  241. out:
  242. return result;
  243. }
  244. /*
  245. * Returns zero on success, otherwise a negative errno value
  246. * is returned.
  247. */
  248. static int rpcb_create_local_net(struct net *net)
  249. {
  250. static const struct sockaddr_in rpcb_inaddr_loopback = {
  251. .sin_family = AF_INET,
  252. .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
  253. .sin_port = htons(RPCBIND_PORT),
  254. };
  255. struct rpc_create_args args = {
  256. .net = net,
  257. .protocol = XPRT_TRANSPORT_TCP,
  258. .address = (struct sockaddr *)&rpcb_inaddr_loopback,
  259. .addrsize = sizeof(rpcb_inaddr_loopback),
  260. .servername = "localhost",
  261. .program = &rpcb_program,
  262. .version = RPCBVERS_2,
  263. .authflavor = RPC_AUTH_UNIX,
  264. .cred = current_cred(),
  265. .flags = RPC_CLNT_CREATE_NOPING,
  266. };
  267. struct rpc_clnt *clnt, *clnt4;
  268. int result = 0;
  269. clnt = rpc_create(&args);
  270. if (IS_ERR(clnt)) {
  271. dprintk("RPC: failed to create local rpcbind "
  272. "client (errno %ld).\n", PTR_ERR(clnt));
  273. result = PTR_ERR(clnt);
  274. goto out;
  275. }
  276. /*
  277. * This results in an RPC ping. On systems running portmapper,
  278. * the v4 ping will fail. Proceed anyway, but disallow rpcb
  279. * v4 upcalls.
  280. */
  281. clnt4 = rpc_bind_new_program(clnt, &rpcb_program, RPCBVERS_4);
  282. if (IS_ERR(clnt4)) {
  283. dprintk("RPC: failed to bind second program to "
  284. "rpcbind v4 client (errno %ld).\n",
  285. PTR_ERR(clnt4));
  286. clnt4 = NULL;
  287. }
  288. rpcb_set_local(net, clnt, clnt4, false);
  289. out:
  290. return result;
  291. }
  292. /*
  293. * Returns zero on success, otherwise a negative errno value
  294. * is returned.
  295. */
  296. int rpcb_create_local(struct net *net)
  297. {
  298. static DEFINE_MUTEX(rpcb_create_local_mutex);
  299. int result = 0;
  300. if (rpcb_get_local(net))
  301. return result;
  302. mutex_lock(&rpcb_create_local_mutex);
  303. if (rpcb_get_local(net))
  304. goto out;
  305. if (rpcb_create_local_unix(net) != 0)
  306. result = rpcb_create_local_net(net);
  307. out:
  308. mutex_unlock(&rpcb_create_local_mutex);
  309. return result;
  310. }
  311. static struct rpc_clnt *rpcb_create(struct net *net, const char *nodename,
  312. const char *hostname,
  313. struct sockaddr *srvaddr, size_t salen,
  314. int proto, u32 version,
  315. const struct cred *cred)
  316. {
  317. struct rpc_create_args args = {
  318. .net = net,
  319. .protocol = proto,
  320. .address = srvaddr,
  321. .addrsize = salen,
  322. .servername = hostname,
  323. .nodename = nodename,
  324. .program = &rpcb_program,
  325. .version = version,
  326. .authflavor = RPC_AUTH_UNIX,
  327. .cred = cred,
  328. .flags = (RPC_CLNT_CREATE_NOPING |
  329. RPC_CLNT_CREATE_NONPRIVPORT),
  330. };
  331. switch (srvaddr->sa_family) {
  332. case AF_INET:
  333. ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
  334. break;
  335. case AF_INET6:
  336. ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
  337. break;
  338. default:
  339. return ERR_PTR(-EAFNOSUPPORT);
  340. }
  341. return rpc_create(&args);
  342. }
  343. static int rpcb_register_call(struct sunrpc_net *sn, struct rpc_clnt *clnt, struct rpc_message *msg, bool is_set)
  344. {
  345. int flags = RPC_TASK_NOCONNECT;
  346. int error, result = 0;
  347. if (is_set || !sn->rpcb_is_af_local)
  348. flags = RPC_TASK_SOFTCONN;
  349. msg->rpc_resp = &result;
  350. error = rpc_call_sync(clnt, msg, flags);
  351. if (error < 0) {
  352. dprintk("RPC: failed to contact local rpcbind "
  353. "server (errno %d).\n", -error);
  354. return error;
  355. }
  356. if (!result)
  357. return -EACCES;
  358. return 0;
  359. }
  360. /**
  361. * rpcb_register - set or unset a port registration with the local rpcbind svc
  362. * @net: target network namespace
  363. * @prog: RPC program number to bind
  364. * @vers: RPC version number to bind
  365. * @prot: transport protocol to register
  366. * @port: port value to register
  367. *
  368. * Returns zero if the registration request was dispatched successfully
  369. * and the rpcbind daemon returned success. Otherwise, returns an errno
  370. * value that reflects the nature of the error (request could not be
  371. * dispatched, timed out, or rpcbind returned an error).
  372. *
  373. * RPC services invoke this function to advertise their contact
  374. * information via the system's rpcbind daemon. RPC services
  375. * invoke this function once for each [program, version, transport]
  376. * tuple they wish to advertise.
  377. *
  378. * Callers may also unregister RPC services that are no longer
  379. * available by setting the passed-in port to zero. This removes
  380. * all registered transports for [program, version] from the local
  381. * rpcbind database.
  382. *
  383. * This function uses rpcbind protocol version 2 to contact the
  384. * local rpcbind daemon.
  385. *
  386. * Registration works over both AF_INET and AF_INET6, and services
  387. * registered via this function are advertised as available for any
  388. * address. If the local rpcbind daemon is listening on AF_INET6,
  389. * services registered via this function will be advertised on
  390. * IN6ADDR_ANY (ie available for all AF_INET and AF_INET6
  391. * addresses).
  392. */
  393. int rpcb_register(struct net *net, u32 prog, u32 vers, int prot, unsigned short port)
  394. {
  395. struct rpcbind_args map = {
  396. .r_prog = prog,
  397. .r_vers = vers,
  398. .r_prot = prot,
  399. .r_port = port,
  400. };
  401. struct rpc_message msg = {
  402. .rpc_argp = &map,
  403. };
  404. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  405. bool is_set = false;
  406. dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
  407. "rpcbind\n", (port ? "" : "un"),
  408. prog, vers, prot, port);
  409. msg.rpc_proc = &rpcb_procedures2[RPCBPROC_UNSET];
  410. if (port != 0) {
  411. msg.rpc_proc = &rpcb_procedures2[RPCBPROC_SET];
  412. is_set = true;
  413. }
  414. return rpcb_register_call(sn, sn->rpcb_local_clnt, &msg, is_set);
  415. }
  416. /*
  417. * Fill in AF_INET family-specific arguments to register
  418. */
  419. static int rpcb_register_inet4(struct sunrpc_net *sn,
  420. const struct sockaddr *sap,
  421. struct rpc_message *msg)
  422. {
  423. const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
  424. struct rpcbind_args *map = msg->rpc_argp;
  425. unsigned short port = ntohs(sin->sin_port);
  426. bool is_set = false;
  427. int result;
  428. map->r_addr = rpc_sockaddr2uaddr(sap, GFP_KERNEL);
  429. dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
  430. "local rpcbind\n", (port ? "" : "un"),
  431. map->r_prog, map->r_vers,
  432. map->r_addr, map->r_netid);
  433. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  434. if (port != 0) {
  435. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
  436. is_set = true;
  437. }
  438. result = rpcb_register_call(sn, sn->rpcb_local_clnt4, msg, is_set);
  439. kfree(map->r_addr);
  440. return result;
  441. }
  442. /*
  443. * Fill in AF_INET6 family-specific arguments to register
  444. */
  445. static int rpcb_register_inet6(struct sunrpc_net *sn,
  446. const struct sockaddr *sap,
  447. struct rpc_message *msg)
  448. {
  449. const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sap;
  450. struct rpcbind_args *map = msg->rpc_argp;
  451. unsigned short port = ntohs(sin6->sin6_port);
  452. bool is_set = false;
  453. int result;
  454. map->r_addr = rpc_sockaddr2uaddr(sap, GFP_KERNEL);
  455. dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
  456. "local rpcbind\n", (port ? "" : "un"),
  457. map->r_prog, map->r_vers,
  458. map->r_addr, map->r_netid);
  459. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  460. if (port != 0) {
  461. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
  462. is_set = true;
  463. }
  464. result = rpcb_register_call(sn, sn->rpcb_local_clnt4, msg, is_set);
  465. kfree(map->r_addr);
  466. return result;
  467. }
  468. static int rpcb_unregister_all_protofamilies(struct sunrpc_net *sn,
  469. struct rpc_message *msg)
  470. {
  471. struct rpcbind_args *map = msg->rpc_argp;
  472. dprintk("RPC: unregistering [%u, %u, '%s'] with "
  473. "local rpcbind\n",
  474. map->r_prog, map->r_vers, map->r_netid);
  475. map->r_addr = "";
  476. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  477. return rpcb_register_call(sn, sn->rpcb_local_clnt4, msg, false);
  478. }
  479. /**
  480. * rpcb_v4_register - set or unset a port registration with the local rpcbind
  481. * @net: target network namespace
  482. * @program: RPC program number of service to (un)register
  483. * @version: RPC version number of service to (un)register
  484. * @address: address family, IP address, and port to (un)register
  485. * @netid: netid of transport protocol to (un)register
  486. *
  487. * Returns zero if the registration request was dispatched successfully
  488. * and the rpcbind daemon returned success. Otherwise, returns an errno
  489. * value that reflects the nature of the error (request could not be
  490. * dispatched, timed out, or rpcbind returned an error).
  491. *
  492. * RPC services invoke this function to advertise their contact
  493. * information via the system's rpcbind daemon. RPC services
  494. * invoke this function once for each [program, version, address,
  495. * netid] tuple they wish to advertise.
  496. *
  497. * Callers may also unregister RPC services that are registered at a
  498. * specific address by setting the port number in @address to zero.
  499. * They may unregister all registered protocol families at once for
  500. * a service by passing a NULL @address argument. If @netid is ""
  501. * then all netids for [program, version, address] are unregistered.
  502. *
  503. * This function uses rpcbind protocol version 4 to contact the
  504. * local rpcbind daemon. The local rpcbind daemon must support
  505. * version 4 of the rpcbind protocol in order for these functions
  506. * to register a service successfully.
  507. *
  508. * Supported netids include "udp" and "tcp" for UDP and TCP over
  509. * IPv4, and "udp6" and "tcp6" for UDP and TCP over IPv6,
  510. * respectively.
  511. *
  512. * The contents of @address determine the address family and the
  513. * port to be registered. The usual practice is to pass INADDR_ANY
  514. * as the raw address, but specifying a non-zero address is also
  515. * supported by this API if the caller wishes to advertise an RPC
  516. * service on a specific network interface.
  517. *
  518. * Note that passing in INADDR_ANY does not create the same service
  519. * registration as IN6ADDR_ANY. The former advertises an RPC
  520. * service on any IPv4 address, but not on IPv6. The latter
  521. * advertises the service on all IPv4 and IPv6 addresses.
  522. */
  523. int rpcb_v4_register(struct net *net, const u32 program, const u32 version,
  524. const struct sockaddr *address, const char *netid)
  525. {
  526. struct rpcbind_args map = {
  527. .r_prog = program,
  528. .r_vers = version,
  529. .r_netid = netid,
  530. .r_owner = RPCB_OWNER_STRING,
  531. };
  532. struct rpc_message msg = {
  533. .rpc_argp = &map,
  534. };
  535. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  536. if (sn->rpcb_local_clnt4 == NULL)
  537. return -EPROTONOSUPPORT;
  538. if (address == NULL)
  539. return rpcb_unregister_all_protofamilies(sn, &msg);
  540. switch (address->sa_family) {
  541. case AF_INET:
  542. return rpcb_register_inet4(sn, address, &msg);
  543. case AF_INET6:
  544. return rpcb_register_inet6(sn, address, &msg);
  545. }
  546. return -EAFNOSUPPORT;
  547. }
  548. static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt,
  549. struct rpcbind_args *map, const struct rpc_procinfo *proc)
  550. {
  551. struct rpc_message msg = {
  552. .rpc_proc = proc,
  553. .rpc_argp = map,
  554. .rpc_resp = map,
  555. };
  556. struct rpc_task_setup task_setup_data = {
  557. .rpc_client = rpcb_clnt,
  558. .rpc_message = &msg,
  559. .callback_ops = &rpcb_getport_ops,
  560. .callback_data = map,
  561. .flags = RPC_TASK_ASYNC | RPC_TASK_SOFTCONN,
  562. };
  563. return rpc_run_task(&task_setup_data);
  564. }
  565. /*
  566. * In the case where rpc clients have been cloned, we want to make
  567. * sure that we use the program number/version etc of the actual
  568. * owner of the xprt. To do so, we walk back up the tree of parents
  569. * to find whoever created the transport and/or whoever has the
  570. * autobind flag set.
  571. */
  572. static struct rpc_clnt *rpcb_find_transport_owner(struct rpc_clnt *clnt)
  573. {
  574. struct rpc_clnt *parent = clnt->cl_parent;
  575. struct rpc_xprt_switch *xps = rcu_access_pointer(clnt->cl_xpi.xpi_xpswitch);
  576. while (parent != clnt) {
  577. if (rcu_access_pointer(parent->cl_xpi.xpi_xpswitch) != xps)
  578. break;
  579. if (clnt->cl_autobind)
  580. break;
  581. clnt = parent;
  582. parent = parent->cl_parent;
  583. }
  584. return clnt;
  585. }
  586. /**
  587. * rpcb_getport_async - obtain the port for a given RPC service on a given host
  588. * @task: task that is waiting for portmapper request
  589. *
  590. * This one can be called for an ongoing RPC request, and can be used in
  591. * an async (rpciod) context.
  592. */
  593. void rpcb_getport_async(struct rpc_task *task)
  594. {
  595. struct rpc_clnt *clnt;
  596. const struct rpc_procinfo *proc;
  597. u32 bind_version;
  598. struct rpc_xprt *xprt;
  599. struct rpc_clnt *rpcb_clnt;
  600. struct rpcbind_args *map;
  601. struct rpc_task *child;
  602. struct sockaddr_storage addr;
  603. struct sockaddr *sap = (struct sockaddr *)&addr;
  604. size_t salen;
  605. int status;
  606. rcu_read_lock();
  607. clnt = rpcb_find_transport_owner(task->tk_client);
  608. rcu_read_unlock();
  609. xprt = xprt_get(task->tk_xprt);
  610. dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
  611. task->tk_pid, __func__,
  612. xprt->servername, clnt->cl_prog, clnt->cl_vers, xprt->prot);
  613. /* Put self on the wait queue to ensure we get notified if
  614. * some other task is already attempting to bind the port */
  615. rpc_sleep_on_timeout(&xprt->binding, task,
  616. NULL, jiffies + xprt->bind_timeout);
  617. if (xprt_test_and_set_binding(xprt)) {
  618. dprintk("RPC: %5u %s: waiting for another binder\n",
  619. task->tk_pid, __func__);
  620. xprt_put(xprt);
  621. return;
  622. }
  623. /* Someone else may have bound if we slept */
  624. if (xprt_bound(xprt)) {
  625. status = 0;
  626. dprintk("RPC: %5u %s: already bound\n",
  627. task->tk_pid, __func__);
  628. goto bailout_nofree;
  629. }
  630. /* Parent transport's destination address */
  631. salen = rpc_peeraddr(clnt, sap, sizeof(addr));
  632. /* Don't ever use rpcbind v2 for AF_INET6 requests */
  633. switch (sap->sa_family) {
  634. case AF_INET:
  635. proc = rpcb_next_version[xprt->bind_index].rpc_proc;
  636. bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
  637. break;
  638. case AF_INET6:
  639. proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
  640. bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
  641. break;
  642. default:
  643. status = -EAFNOSUPPORT;
  644. dprintk("RPC: %5u %s: bad address family\n",
  645. task->tk_pid, __func__);
  646. goto bailout_nofree;
  647. }
  648. if (proc == NULL) {
  649. xprt->bind_index = 0;
  650. status = -EPFNOSUPPORT;
  651. dprintk("RPC: %5u %s: no more getport versions available\n",
  652. task->tk_pid, __func__);
  653. goto bailout_nofree;
  654. }
  655. dprintk("RPC: %5u %s: trying rpcbind version %u\n",
  656. task->tk_pid, __func__, bind_version);
  657. rpcb_clnt = rpcb_create(xprt->xprt_net,
  658. clnt->cl_nodename,
  659. xprt->servername, sap, salen,
  660. xprt->prot, bind_version,
  661. clnt->cl_cred);
  662. if (IS_ERR(rpcb_clnt)) {
  663. status = PTR_ERR(rpcb_clnt);
  664. dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
  665. task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
  666. goto bailout_nofree;
  667. }
  668. map = kzalloc(sizeof(struct rpcbind_args), GFP_NOFS);
  669. if (!map) {
  670. status = -ENOMEM;
  671. dprintk("RPC: %5u %s: no memory available\n",
  672. task->tk_pid, __func__);
  673. goto bailout_release_client;
  674. }
  675. map->r_prog = clnt->cl_prog;
  676. map->r_vers = clnt->cl_vers;
  677. map->r_prot = xprt->prot;
  678. map->r_port = 0;
  679. map->r_xprt = xprt;
  680. map->r_status = -EIO;
  681. switch (bind_version) {
  682. case RPCBVERS_4:
  683. case RPCBVERS_3:
  684. map->r_netid = xprt->address_strings[RPC_DISPLAY_NETID];
  685. map->r_addr = rpc_sockaddr2uaddr(sap, GFP_NOFS);
  686. if (!map->r_addr) {
  687. status = -ENOMEM;
  688. dprintk("RPC: %5u %s: no memory available\n",
  689. task->tk_pid, __func__);
  690. goto bailout_free_args;
  691. }
  692. map->r_owner = "";
  693. break;
  694. case RPCBVERS_2:
  695. map->r_addr = NULL;
  696. break;
  697. default:
  698. BUG();
  699. }
  700. child = rpcb_call_async(rpcb_clnt, map, proc);
  701. rpc_release_client(rpcb_clnt);
  702. if (IS_ERR(child)) {
  703. /* rpcb_map_release() has freed the arguments */
  704. dprintk("RPC: %5u %s: rpc_run_task failed\n",
  705. task->tk_pid, __func__);
  706. return;
  707. }
  708. xprt->stat.bind_count++;
  709. rpc_put_task(child);
  710. return;
  711. bailout_free_args:
  712. kfree(map);
  713. bailout_release_client:
  714. rpc_release_client(rpcb_clnt);
  715. bailout_nofree:
  716. rpcb_wake_rpcbind_waiters(xprt, status);
  717. task->tk_status = status;
  718. xprt_put(xprt);
  719. }
  720. EXPORT_SYMBOL_GPL(rpcb_getport_async);
  721. /*
  722. * Rpcbind child task calls this callback via tk_exit.
  723. */
  724. static void rpcb_getport_done(struct rpc_task *child, void *data)
  725. {
  726. struct rpcbind_args *map = data;
  727. struct rpc_xprt *xprt = map->r_xprt;
  728. int status = child->tk_status;
  729. /* Garbage reply: retry with a lesser rpcbind version */
  730. if (status == -EIO)
  731. status = -EPROTONOSUPPORT;
  732. /* rpcbind server doesn't support this rpcbind protocol version */
  733. if (status == -EPROTONOSUPPORT)
  734. xprt->bind_index++;
  735. if (status < 0) {
  736. /* rpcbind server not available on remote host? */
  737. xprt->ops->set_port(xprt, 0);
  738. } else if (map->r_port == 0) {
  739. /* Requested RPC service wasn't registered on remote host */
  740. xprt->ops->set_port(xprt, 0);
  741. status = -EACCES;
  742. } else {
  743. /* Succeeded */
  744. xprt->ops->set_port(xprt, map->r_port);
  745. xprt_set_bound(xprt);
  746. status = 0;
  747. }
  748. dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
  749. child->tk_pid, status, map->r_port);
  750. map->r_status = status;
  751. }
  752. /*
  753. * XDR functions for rpcbind
  754. */
  755. static void rpcb_enc_mapping(struct rpc_rqst *req, struct xdr_stream *xdr,
  756. const void *data)
  757. {
  758. const struct rpcbind_args *rpcb = data;
  759. __be32 *p;
  760. dprintk("RPC: %5u encoding PMAP_%s call (%u, %u, %d, %u)\n",
  761. req->rq_task->tk_pid,
  762. req->rq_task->tk_msg.rpc_proc->p_name,
  763. rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
  764. p = xdr_reserve_space(xdr, RPCB_mappingargs_sz << 2);
  765. *p++ = cpu_to_be32(rpcb->r_prog);
  766. *p++ = cpu_to_be32(rpcb->r_vers);
  767. *p++ = cpu_to_be32(rpcb->r_prot);
  768. *p = cpu_to_be32(rpcb->r_port);
  769. }
  770. static int rpcb_dec_getport(struct rpc_rqst *req, struct xdr_stream *xdr,
  771. void *data)
  772. {
  773. struct rpcbind_args *rpcb = data;
  774. unsigned long port;
  775. __be32 *p;
  776. rpcb->r_port = 0;
  777. p = xdr_inline_decode(xdr, 4);
  778. if (unlikely(p == NULL))
  779. return -EIO;
  780. port = be32_to_cpup(p);
  781. dprintk("RPC: %5u PMAP_%s result: %lu\n", req->rq_task->tk_pid,
  782. req->rq_task->tk_msg.rpc_proc->p_name, port);
  783. if (unlikely(port > USHRT_MAX))
  784. return -EIO;
  785. rpcb->r_port = port;
  786. return 0;
  787. }
  788. static int rpcb_dec_set(struct rpc_rqst *req, struct xdr_stream *xdr,
  789. void *data)
  790. {
  791. unsigned int *boolp = data;
  792. __be32 *p;
  793. p = xdr_inline_decode(xdr, 4);
  794. if (unlikely(p == NULL))
  795. return -EIO;
  796. *boolp = 0;
  797. if (*p != xdr_zero)
  798. *boolp = 1;
  799. dprintk("RPC: %5u RPCB_%s call %s\n",
  800. req->rq_task->tk_pid,
  801. req->rq_task->tk_msg.rpc_proc->p_name,
  802. (*boolp ? "succeeded" : "failed"));
  803. return 0;
  804. }
  805. static void encode_rpcb_string(struct xdr_stream *xdr, const char *string,
  806. const u32 maxstrlen)
  807. {
  808. __be32 *p;
  809. u32 len;
  810. len = strlen(string);
  811. WARN_ON_ONCE(len > maxstrlen);
  812. if (len > maxstrlen)
  813. /* truncate and hope for the best */
  814. len = maxstrlen;
  815. p = xdr_reserve_space(xdr, 4 + len);
  816. xdr_encode_opaque(p, string, len);
  817. }
  818. static void rpcb_enc_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr,
  819. const void *data)
  820. {
  821. const struct rpcbind_args *rpcb = data;
  822. __be32 *p;
  823. dprintk("RPC: %5u encoding RPCB_%s call (%u, %u, '%s', '%s')\n",
  824. req->rq_task->tk_pid,
  825. req->rq_task->tk_msg.rpc_proc->p_name,
  826. rpcb->r_prog, rpcb->r_vers,
  827. rpcb->r_netid, rpcb->r_addr);
  828. p = xdr_reserve_space(xdr, (RPCB_program_sz + RPCB_version_sz) << 2);
  829. *p++ = cpu_to_be32(rpcb->r_prog);
  830. *p = cpu_to_be32(rpcb->r_vers);
  831. encode_rpcb_string(xdr, rpcb->r_netid, RPCBIND_MAXNETIDLEN);
  832. encode_rpcb_string(xdr, rpcb->r_addr, RPCBIND_MAXUADDRLEN);
  833. encode_rpcb_string(xdr, rpcb->r_owner, RPCB_MAXOWNERLEN);
  834. }
  835. static int rpcb_dec_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr,
  836. void *data)
  837. {
  838. struct rpcbind_args *rpcb = data;
  839. struct sockaddr_storage address;
  840. struct sockaddr *sap = (struct sockaddr *)&address;
  841. __be32 *p;
  842. u32 len;
  843. rpcb->r_port = 0;
  844. p = xdr_inline_decode(xdr, 4);
  845. if (unlikely(p == NULL))
  846. goto out_fail;
  847. len = be32_to_cpup(p);
  848. /*
  849. * If the returned universal address is a null string,
  850. * the requested RPC service was not registered.
  851. */
  852. if (len == 0) {
  853. dprintk("RPC: %5u RPCB reply: program not registered\n",
  854. req->rq_task->tk_pid);
  855. return 0;
  856. }
  857. if (unlikely(len > RPCBIND_MAXUADDRLEN))
  858. goto out_fail;
  859. p = xdr_inline_decode(xdr, len);
  860. if (unlikely(p == NULL))
  861. goto out_fail;
  862. dprintk("RPC: %5u RPCB_%s reply: %s\n", req->rq_task->tk_pid,
  863. req->rq_task->tk_msg.rpc_proc->p_name, (char *)p);
  864. if (rpc_uaddr2sockaddr(req->rq_xprt->xprt_net, (char *)p, len,
  865. sap, sizeof(address)) == 0)
  866. goto out_fail;
  867. rpcb->r_port = rpc_get_port(sap);
  868. return 0;
  869. out_fail:
  870. dprintk("RPC: %5u malformed RPCB_%s reply\n",
  871. req->rq_task->tk_pid,
  872. req->rq_task->tk_msg.rpc_proc->p_name);
  873. return -EIO;
  874. }
  875. /*
  876. * Not all rpcbind procedures described in RFC 1833 are implemented
  877. * since the Linux kernel RPC code requires only these.
  878. */
  879. static const struct rpc_procinfo rpcb_procedures2[] = {
  880. [RPCBPROC_SET] = {
  881. .p_proc = RPCBPROC_SET,
  882. .p_encode = rpcb_enc_mapping,
  883. .p_decode = rpcb_dec_set,
  884. .p_arglen = RPCB_mappingargs_sz,
  885. .p_replen = RPCB_setres_sz,
  886. .p_statidx = RPCBPROC_SET,
  887. .p_timer = 0,
  888. .p_name = "SET",
  889. },
  890. [RPCBPROC_UNSET] = {
  891. .p_proc = RPCBPROC_UNSET,
  892. .p_encode = rpcb_enc_mapping,
  893. .p_decode = rpcb_dec_set,
  894. .p_arglen = RPCB_mappingargs_sz,
  895. .p_replen = RPCB_setres_sz,
  896. .p_statidx = RPCBPROC_UNSET,
  897. .p_timer = 0,
  898. .p_name = "UNSET",
  899. },
  900. [RPCBPROC_GETPORT] = {
  901. .p_proc = RPCBPROC_GETPORT,
  902. .p_encode = rpcb_enc_mapping,
  903. .p_decode = rpcb_dec_getport,
  904. .p_arglen = RPCB_mappingargs_sz,
  905. .p_replen = RPCB_getportres_sz,
  906. .p_statidx = RPCBPROC_GETPORT,
  907. .p_timer = 0,
  908. .p_name = "GETPORT",
  909. },
  910. };
  911. static const struct rpc_procinfo rpcb_procedures3[] = {
  912. [RPCBPROC_SET] = {
  913. .p_proc = RPCBPROC_SET,
  914. .p_encode = rpcb_enc_getaddr,
  915. .p_decode = rpcb_dec_set,
  916. .p_arglen = RPCB_getaddrargs_sz,
  917. .p_replen = RPCB_setres_sz,
  918. .p_statidx = RPCBPROC_SET,
  919. .p_timer = 0,
  920. .p_name = "SET",
  921. },
  922. [RPCBPROC_UNSET] = {
  923. .p_proc = RPCBPROC_UNSET,
  924. .p_encode = rpcb_enc_getaddr,
  925. .p_decode = rpcb_dec_set,
  926. .p_arglen = RPCB_getaddrargs_sz,
  927. .p_replen = RPCB_setres_sz,
  928. .p_statidx = RPCBPROC_UNSET,
  929. .p_timer = 0,
  930. .p_name = "UNSET",
  931. },
  932. [RPCBPROC_GETADDR] = {
  933. .p_proc = RPCBPROC_GETADDR,
  934. .p_encode = rpcb_enc_getaddr,
  935. .p_decode = rpcb_dec_getaddr,
  936. .p_arglen = RPCB_getaddrargs_sz,
  937. .p_replen = RPCB_getaddrres_sz,
  938. .p_statidx = RPCBPROC_GETADDR,
  939. .p_timer = 0,
  940. .p_name = "GETADDR",
  941. },
  942. };
  943. static const struct rpc_procinfo rpcb_procedures4[] = {
  944. [RPCBPROC_SET] = {
  945. .p_proc = RPCBPROC_SET,
  946. .p_encode = rpcb_enc_getaddr,
  947. .p_decode = rpcb_dec_set,
  948. .p_arglen = RPCB_getaddrargs_sz,
  949. .p_replen = RPCB_setres_sz,
  950. .p_statidx = RPCBPROC_SET,
  951. .p_timer = 0,
  952. .p_name = "SET",
  953. },
  954. [RPCBPROC_UNSET] = {
  955. .p_proc = RPCBPROC_UNSET,
  956. .p_encode = rpcb_enc_getaddr,
  957. .p_decode = rpcb_dec_set,
  958. .p_arglen = RPCB_getaddrargs_sz,
  959. .p_replen = RPCB_setres_sz,
  960. .p_statidx = RPCBPROC_UNSET,
  961. .p_timer = 0,
  962. .p_name = "UNSET",
  963. },
  964. [RPCBPROC_GETADDR] = {
  965. .p_proc = RPCBPROC_GETADDR,
  966. .p_encode = rpcb_enc_getaddr,
  967. .p_decode = rpcb_dec_getaddr,
  968. .p_arglen = RPCB_getaddrargs_sz,
  969. .p_replen = RPCB_getaddrres_sz,
  970. .p_statidx = RPCBPROC_GETADDR,
  971. .p_timer = 0,
  972. .p_name = "GETADDR",
  973. },
  974. };
  975. static const struct rpcb_info rpcb_next_version[] = {
  976. {
  977. .rpc_vers = RPCBVERS_2,
  978. .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
  979. },
  980. {
  981. .rpc_proc = NULL,
  982. },
  983. };
  984. static const struct rpcb_info rpcb_next_version6[] = {
  985. {
  986. .rpc_vers = RPCBVERS_4,
  987. .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR],
  988. },
  989. {
  990. .rpc_vers = RPCBVERS_3,
  991. .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR],
  992. },
  993. {
  994. .rpc_proc = NULL,
  995. },
  996. };
  997. static unsigned int rpcb_version2_counts[ARRAY_SIZE(rpcb_procedures2)];
  998. static const struct rpc_version rpcb_version2 = {
  999. .number = RPCBVERS_2,
  1000. .nrprocs = ARRAY_SIZE(rpcb_procedures2),
  1001. .procs = rpcb_procedures2,
  1002. .counts = rpcb_version2_counts,
  1003. };
  1004. static unsigned int rpcb_version3_counts[ARRAY_SIZE(rpcb_procedures3)];
  1005. static const struct rpc_version rpcb_version3 = {
  1006. .number = RPCBVERS_3,
  1007. .nrprocs = ARRAY_SIZE(rpcb_procedures3),
  1008. .procs = rpcb_procedures3,
  1009. .counts = rpcb_version3_counts,
  1010. };
  1011. static unsigned int rpcb_version4_counts[ARRAY_SIZE(rpcb_procedures4)];
  1012. static const struct rpc_version rpcb_version4 = {
  1013. .number = RPCBVERS_4,
  1014. .nrprocs = ARRAY_SIZE(rpcb_procedures4),
  1015. .procs = rpcb_procedures4,
  1016. .counts = rpcb_version4_counts,
  1017. };
  1018. static const struct rpc_version *rpcb_version[] = {
  1019. NULL,
  1020. NULL,
  1021. &rpcb_version2,
  1022. &rpcb_version3,
  1023. &rpcb_version4
  1024. };
  1025. static struct rpc_stat rpcb_stats;
  1026. static const struct rpc_program rpcb_program = {
  1027. .name = "rpcbind",
  1028. .number = RPCBIND_PROGRAM,
  1029. .nrvers = ARRAY_SIZE(rpcb_version),
  1030. .version = rpcb_version,
  1031. .stats = &rpcb_stats,
  1032. };