PageRenderTime 1517ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/net/sunrpc/rpcb_clnt.c

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