rpcb_clnt.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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/in.h>
  18. #include <linux/in6.h>
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <net/ipv6.h>
  22. #include <linux/sunrpc/clnt.h>
  23. #include <linux/sunrpc/sched.h>
  24. #include <linux/sunrpc/xprtsock.h>
  25. #ifdef RPC_DEBUG
  26. # define RPCDBG_FACILITY RPCDBG_BIND
  27. #endif
  28. #define RPCBIND_PROGRAM (100000u)
  29. #define RPCBIND_PORT (111u)
  30. #define RPCBVERS_2 (2u)
  31. #define RPCBVERS_3 (3u)
  32. #define RPCBVERS_4 (4u)
  33. enum {
  34. RPCBPROC_NULL,
  35. RPCBPROC_SET,
  36. RPCBPROC_UNSET,
  37. RPCBPROC_GETPORT,
  38. RPCBPROC_GETADDR = 3, /* alias for GETPORT */
  39. RPCBPROC_DUMP,
  40. RPCBPROC_CALLIT,
  41. RPCBPROC_BCAST = 5, /* alias for CALLIT */
  42. RPCBPROC_GETTIME,
  43. RPCBPROC_UADDR2TADDR,
  44. RPCBPROC_TADDR2UADDR,
  45. RPCBPROC_GETVERSADDR,
  46. RPCBPROC_INDIRECT,
  47. RPCBPROC_GETADDRLIST,
  48. RPCBPROC_GETSTAT,
  49. };
  50. #define RPCB_HIGHPROC_2 RPCBPROC_CALLIT
  51. #define RPCB_HIGHPROC_3 RPCBPROC_TADDR2UADDR
  52. #define RPCB_HIGHPROC_4 RPCBPROC_GETSTAT
  53. /*
  54. * r_owner
  55. *
  56. * The "owner" is allowed to unset a service in the rpcbind database.
  57. *
  58. * For AF_LOCAL SET/UNSET requests, rpcbind treats this string as a
  59. * UID which it maps to a local user name via a password lookup.
  60. * In all other cases it is ignored.
  61. *
  62. * For SET/UNSET requests, user space provides a value, even for
  63. * network requests, and GETADDR uses an empty string. We follow
  64. * those precedents here.
  65. */
  66. #define RPCB_OWNER_STRING "0"
  67. #define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
  68. static void rpcb_getport_done(struct rpc_task *, void *);
  69. static void rpcb_map_release(void *data);
  70. static struct rpc_program rpcb_program;
  71. struct rpcbind_args {
  72. struct rpc_xprt * r_xprt;
  73. u32 r_prog;
  74. u32 r_vers;
  75. u32 r_prot;
  76. unsigned short r_port;
  77. const char * r_netid;
  78. const char * r_addr;
  79. const char * r_owner;
  80. int r_status;
  81. };
  82. static struct rpc_procinfo rpcb_procedures2[];
  83. static struct rpc_procinfo rpcb_procedures3[];
  84. static struct rpc_procinfo rpcb_procedures4[];
  85. struct rpcb_info {
  86. u32 rpc_vers;
  87. struct rpc_procinfo * rpc_proc;
  88. };
  89. static struct rpcb_info rpcb_next_version[];
  90. static struct rpcb_info rpcb_next_version6[];
  91. static const struct rpc_call_ops rpcb_getport_ops = {
  92. .rpc_call_done = rpcb_getport_done,
  93. .rpc_release = rpcb_map_release,
  94. };
  95. static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status)
  96. {
  97. xprt_clear_binding(xprt);
  98. rpc_wake_up_status(&xprt->binding, status);
  99. }
  100. static void rpcb_map_release(void *data)
  101. {
  102. struct rpcbind_args *map = data;
  103. rpcb_wake_rpcbind_waiters(map->r_xprt, map->r_status);
  104. xprt_put(map->r_xprt);
  105. kfree(map);
  106. }
  107. static const struct sockaddr_in rpcb_inaddr_loopback = {
  108. .sin_family = AF_INET,
  109. .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
  110. .sin_port = htons(RPCBIND_PORT),
  111. };
  112. static struct rpc_clnt *rpcb_create_local(struct sockaddr *addr,
  113. size_t addrlen, u32 version)
  114. {
  115. struct rpc_create_args args = {
  116. .protocol = XPRT_TRANSPORT_UDP,
  117. .address = addr,
  118. .addrsize = addrlen,
  119. .servername = "localhost",
  120. .program = &rpcb_program,
  121. .version = version,
  122. .authflavor = RPC_AUTH_UNIX,
  123. .flags = RPC_CLNT_CREATE_NOPING,
  124. };
  125. return rpc_create(&args);
  126. }
  127. static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
  128. size_t salen, int proto, u32 version)
  129. {
  130. struct rpc_create_args args = {
  131. .protocol = proto,
  132. .address = srvaddr,
  133. .addrsize = salen,
  134. .servername = hostname,
  135. .program = &rpcb_program,
  136. .version = version,
  137. .authflavor = RPC_AUTH_UNIX,
  138. .flags = (RPC_CLNT_CREATE_NOPING |
  139. RPC_CLNT_CREATE_NONPRIVPORT),
  140. };
  141. switch (srvaddr->sa_family) {
  142. case AF_INET:
  143. ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
  144. break;
  145. case AF_INET6:
  146. ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
  147. break;
  148. default:
  149. return NULL;
  150. }
  151. return rpc_create(&args);
  152. }
  153. static int rpcb_register_call(const u32 version, struct rpc_message *msg)
  154. {
  155. struct sockaddr *addr = (struct sockaddr *)&rpcb_inaddr_loopback;
  156. size_t addrlen = sizeof(rpcb_inaddr_loopback);
  157. struct rpc_clnt *rpcb_clnt;
  158. int result, error = 0;
  159. msg->rpc_resp = &result;
  160. rpcb_clnt = rpcb_create_local(addr, addrlen, version);
  161. if (!IS_ERR(rpcb_clnt)) {
  162. error = rpc_call_sync(rpcb_clnt, msg, 0);
  163. rpc_shutdown_client(rpcb_clnt);
  164. } else
  165. error = PTR_ERR(rpcb_clnt);
  166. if (error < 0) {
  167. printk(KERN_WARNING "RPC: failed to contact local rpcbind "
  168. "server (errno %d).\n", -error);
  169. return error;
  170. }
  171. if (!result)
  172. return -EACCES;
  173. return 0;
  174. }
  175. /**
  176. * rpcb_register - set or unset a port registration with the local rpcbind svc
  177. * @prog: RPC program number to bind
  178. * @vers: RPC version number to bind
  179. * @prot: transport protocol to register
  180. * @port: port value to register
  181. *
  182. * Returns zero if the registration request was dispatched successfully
  183. * and the rpcbind daemon returned success. Otherwise, returns an errno
  184. * value that reflects the nature of the error (request could not be
  185. * dispatched, timed out, or rpcbind returned an error).
  186. *
  187. * RPC services invoke this function to advertise their contact
  188. * information via the system's rpcbind daemon. RPC services
  189. * invoke this function once for each [program, version, transport]
  190. * tuple they wish to advertise.
  191. *
  192. * Callers may also unregister RPC services that are no longer
  193. * available by setting the passed-in port to zero. This removes
  194. * all registered transports for [program, version] from the local
  195. * rpcbind database.
  196. *
  197. * This function uses rpcbind protocol version 2 to contact the
  198. * local rpcbind daemon.
  199. *
  200. * Registration works over both AF_INET and AF_INET6, and services
  201. * registered via this function are advertised as available for any
  202. * address. If the local rpcbind daemon is listening on AF_INET6,
  203. * services registered via this function will be advertised on
  204. * IN6ADDR_ANY (ie available for all AF_INET and AF_INET6
  205. * addresses).
  206. */
  207. int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port)
  208. {
  209. struct rpcbind_args map = {
  210. .r_prog = prog,
  211. .r_vers = vers,
  212. .r_prot = prot,
  213. .r_port = port,
  214. };
  215. struct rpc_message msg = {
  216. .rpc_argp = &map,
  217. };
  218. dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
  219. "rpcbind\n", (port ? "" : "un"),
  220. prog, vers, prot, port);
  221. msg.rpc_proc = &rpcb_procedures2[RPCBPROC_UNSET];
  222. if (port)
  223. msg.rpc_proc = &rpcb_procedures2[RPCBPROC_SET];
  224. return rpcb_register_call(RPCBVERS_2, &msg);
  225. }
  226. /*
  227. * Fill in AF_INET family-specific arguments to register
  228. */
  229. static int rpcb_register_netid4(const struct sockaddr *sap,
  230. struct rpc_message *msg)
  231. {
  232. const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
  233. struct rpcbind_args *map = msg->rpc_argp;
  234. unsigned short port = ntohs(sin->sin_port);
  235. char buf[32];
  236. /* Construct AF_INET universal address */
  237. snprintf(buf, sizeof(buf), "%pI4.%u.%u",
  238. &sin->sin_addr.s_addr, port >> 8, port & 0xff);
  239. map->r_addr = buf;
  240. dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
  241. "local rpcbind\n", (port ? "" : "un"),
  242. map->r_prog, map->r_vers,
  243. map->r_addr, map->r_netid);
  244. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  245. if (port)
  246. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
  247. return rpcb_register_call(RPCBVERS_4, msg);
  248. }
  249. /*
  250. * Fill in AF_INET6 family-specific arguments to register
  251. */
  252. static int rpcb_register_netid6(const struct sockaddr *sap,
  253. struct rpc_message *msg)
  254. {
  255. const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sap;
  256. struct rpcbind_args *map = msg->rpc_argp;
  257. unsigned short port = ntohs(sin6->sin6_port);
  258. char buf[64];
  259. /* Construct AF_INET6 universal address */
  260. if (ipv6_addr_any(&sin6->sin6_addr))
  261. snprintf(buf, sizeof(buf), "::.%u.%u",
  262. port >> 8, port & 0xff);
  263. else
  264. snprintf(buf, sizeof(buf), "%pI6.%u.%u",
  265. &sin6->sin6_addr, port >> 8, port & 0xff);
  266. map->r_addr = buf;
  267. dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
  268. "local rpcbind\n", (port ? "" : "un"),
  269. map->r_prog, map->r_vers,
  270. map->r_addr, map->r_netid);
  271. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  272. if (port)
  273. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
  274. return rpcb_register_call(RPCBVERS_4, msg);
  275. }
  276. /**
  277. * rpcb_v4_register - set or unset a port registration with the local rpcbind
  278. * @program: RPC program number of service to (un)register
  279. * @version: RPC version number of service to (un)register
  280. * @address: address family, IP address, and port to (un)register
  281. * @netid: netid of transport protocol to (un)register
  282. *
  283. * Returns zero if the registration request was dispatched successfully
  284. * and the rpcbind daemon returned success. Otherwise, returns an errno
  285. * value that reflects the nature of the error (request could not be
  286. * dispatched, timed out, or rpcbind returned an error).
  287. *
  288. * RPC services invoke this function to advertise their contact
  289. * information via the system's rpcbind daemon. RPC services
  290. * invoke this function once for each [program, version, address,
  291. * netid] tuple they wish to advertise.
  292. *
  293. * Callers may also unregister RPC services that are no longer
  294. * available by setting the port number in the passed-in address
  295. * to zero. Callers pass a netid of "" to unregister all
  296. * transport netids associated with [program, version, address].
  297. *
  298. * This function uses rpcbind protocol version 4 to contact the
  299. * local rpcbind daemon. The local rpcbind daemon must support
  300. * version 4 of the rpcbind protocol in order for these functions
  301. * to register a service successfully.
  302. *
  303. * Supported netids include "udp" and "tcp" for UDP and TCP over
  304. * IPv4, and "udp6" and "tcp6" for UDP and TCP over IPv6,
  305. * respectively.
  306. *
  307. * The contents of @address determine the address family and the
  308. * port to be registered. The usual practice is to pass INADDR_ANY
  309. * as the raw address, but specifying a non-zero address is also
  310. * supported by this API if the caller wishes to advertise an RPC
  311. * service on a specific network interface.
  312. *
  313. * Note that passing in INADDR_ANY does not create the same service
  314. * registration as IN6ADDR_ANY. The former advertises an RPC
  315. * service on any IPv4 address, but not on IPv6. The latter
  316. * advertises the service on all IPv4 and IPv6 addresses.
  317. */
  318. int rpcb_v4_register(const u32 program, const u32 version,
  319. const struct sockaddr *address, const char *netid)
  320. {
  321. struct rpcbind_args map = {
  322. .r_prog = program,
  323. .r_vers = version,
  324. .r_netid = netid,
  325. .r_owner = RPCB_OWNER_STRING,
  326. };
  327. struct rpc_message msg = {
  328. .rpc_argp = &map,
  329. };
  330. switch (address->sa_family) {
  331. case AF_INET:
  332. return rpcb_register_netid4(address, &msg);
  333. case AF_INET6:
  334. return rpcb_register_netid6(address, &msg);
  335. }
  336. return -EAFNOSUPPORT;
  337. }
  338. /**
  339. * rpcb_getport_sync - obtain the port for an RPC service on a given host
  340. * @sin: address of remote peer
  341. * @prog: RPC program number to bind
  342. * @vers: RPC version number to bind
  343. * @prot: transport protocol to use to make this request
  344. *
  345. * Return value is the requested advertised port number,
  346. * or a negative errno value.
  347. *
  348. * Called from outside the RPC client in a synchronous task context.
  349. * Uses default timeout parameters specified by underlying transport.
  350. *
  351. * XXX: Needs to support IPv6
  352. */
  353. int rpcb_getport_sync(struct sockaddr_in *sin, u32 prog, u32 vers, int prot)
  354. {
  355. struct rpcbind_args map = {
  356. .r_prog = prog,
  357. .r_vers = vers,
  358. .r_prot = prot,
  359. .r_port = 0,
  360. };
  361. struct rpc_message msg = {
  362. .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
  363. .rpc_argp = &map,
  364. .rpc_resp = &map.r_port,
  365. };
  366. struct rpc_clnt *rpcb_clnt;
  367. int status;
  368. dprintk("RPC: %s(%pI4, %u, %u, %d)\n",
  369. __func__, &sin->sin_addr.s_addr, prog, vers, prot);
  370. rpcb_clnt = rpcb_create(NULL, (struct sockaddr *)sin,
  371. sizeof(*sin), prot, RPCBVERS_2);
  372. if (IS_ERR(rpcb_clnt))
  373. return PTR_ERR(rpcb_clnt);
  374. status = rpc_call_sync(rpcb_clnt, &msg, 0);
  375. rpc_shutdown_client(rpcb_clnt);
  376. if (status >= 0) {
  377. if (map.r_port != 0)
  378. return map.r_port;
  379. status = -EACCES;
  380. }
  381. return status;
  382. }
  383. EXPORT_SYMBOL_GPL(rpcb_getport_sync);
  384. static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt, struct rpcbind_args *map, struct rpc_procinfo *proc)
  385. {
  386. struct rpc_message msg = {
  387. .rpc_proc = proc,
  388. .rpc_argp = map,
  389. .rpc_resp = &map->r_port,
  390. };
  391. struct rpc_task_setup task_setup_data = {
  392. .rpc_client = rpcb_clnt,
  393. .rpc_message = &msg,
  394. .callback_ops = &rpcb_getport_ops,
  395. .callback_data = map,
  396. .flags = RPC_TASK_ASYNC,
  397. };
  398. return rpc_run_task(&task_setup_data);
  399. }
  400. /*
  401. * In the case where rpc clients have been cloned, we want to make
  402. * sure that we use the program number/version etc of the actual
  403. * owner of the xprt. To do so, we walk back up the tree of parents
  404. * to find whoever created the transport and/or whoever has the
  405. * autobind flag set.
  406. */
  407. static struct rpc_clnt *rpcb_find_transport_owner(struct rpc_clnt *clnt)
  408. {
  409. struct rpc_clnt *parent = clnt->cl_parent;
  410. while (parent != clnt) {
  411. if (parent->cl_xprt != clnt->cl_xprt)
  412. break;
  413. if (clnt->cl_autobind)
  414. break;
  415. clnt = parent;
  416. parent = parent->cl_parent;
  417. }
  418. return clnt;
  419. }
  420. /**
  421. * rpcb_getport_async - obtain the port for a given RPC service on a given host
  422. * @task: task that is waiting for portmapper request
  423. *
  424. * This one can be called for an ongoing RPC request, and can be used in
  425. * an async (rpciod) context.
  426. */
  427. void rpcb_getport_async(struct rpc_task *task)
  428. {
  429. struct rpc_clnt *clnt;
  430. struct rpc_procinfo *proc;
  431. u32 bind_version;
  432. struct rpc_xprt *xprt;
  433. struct rpc_clnt *rpcb_clnt;
  434. static struct rpcbind_args *map;
  435. struct rpc_task *child;
  436. struct sockaddr_storage addr;
  437. struct sockaddr *sap = (struct sockaddr *)&addr;
  438. size_t salen;
  439. int status;
  440. clnt = rpcb_find_transport_owner(task->tk_client);
  441. xprt = clnt->cl_xprt;
  442. dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
  443. task->tk_pid, __func__,
  444. clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot);
  445. /* Put self on the wait queue to ensure we get notified if
  446. * some other task is already attempting to bind the port */
  447. rpc_sleep_on(&xprt->binding, task, NULL);
  448. if (xprt_test_and_set_binding(xprt)) {
  449. dprintk("RPC: %5u %s: waiting for another binder\n",
  450. task->tk_pid, __func__);
  451. return;
  452. }
  453. /* Someone else may have bound if we slept */
  454. if (xprt_bound(xprt)) {
  455. status = 0;
  456. dprintk("RPC: %5u %s: already bound\n",
  457. task->tk_pid, __func__);
  458. goto bailout_nofree;
  459. }
  460. salen = rpc_peeraddr(clnt, sap, sizeof(addr));
  461. /* Don't ever use rpcbind v2 for AF_INET6 requests */
  462. switch (sap->sa_family) {
  463. case AF_INET:
  464. proc = rpcb_next_version[xprt->bind_index].rpc_proc;
  465. bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
  466. break;
  467. case AF_INET6:
  468. proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
  469. bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
  470. break;
  471. default:
  472. status = -EAFNOSUPPORT;
  473. dprintk("RPC: %5u %s: bad address family\n",
  474. task->tk_pid, __func__);
  475. goto bailout_nofree;
  476. }
  477. if (proc == NULL) {
  478. xprt->bind_index = 0;
  479. status = -EPFNOSUPPORT;
  480. dprintk("RPC: %5u %s: no more getport versions available\n",
  481. task->tk_pid, __func__);
  482. goto bailout_nofree;
  483. }
  484. dprintk("RPC: %5u %s: trying rpcbind version %u\n",
  485. task->tk_pid, __func__, bind_version);
  486. rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot,
  487. bind_version);
  488. if (IS_ERR(rpcb_clnt)) {
  489. status = PTR_ERR(rpcb_clnt);
  490. dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
  491. task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
  492. goto bailout_nofree;
  493. }
  494. map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
  495. if (!map) {
  496. status = -ENOMEM;
  497. dprintk("RPC: %5u %s: no memory available\n",
  498. task->tk_pid, __func__);
  499. goto bailout_release_client;
  500. }
  501. map->r_prog = clnt->cl_prog;
  502. map->r_vers = clnt->cl_vers;
  503. map->r_prot = xprt->prot;
  504. map->r_port = 0;
  505. map->r_xprt = xprt_get(xprt);
  506. map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
  507. map->r_addr = rpc_peeraddr2str(rpcb_clnt, RPC_DISPLAY_UNIVERSAL_ADDR);
  508. map->r_owner = "";
  509. map->r_status = -EIO;
  510. child = rpcb_call_async(rpcb_clnt, map, proc);
  511. rpc_release_client(rpcb_clnt);
  512. if (IS_ERR(child)) {
  513. /* rpcb_map_release() has freed the arguments */
  514. dprintk("RPC: %5u %s: rpc_run_task failed\n",
  515. task->tk_pid, __func__);
  516. return;
  517. }
  518. xprt->stat.bind_count++;
  519. rpc_put_task(child);
  520. return;
  521. bailout_release_client:
  522. rpc_release_client(rpcb_clnt);
  523. bailout_nofree:
  524. rpcb_wake_rpcbind_waiters(xprt, status);
  525. task->tk_status = status;
  526. }
  527. EXPORT_SYMBOL_GPL(rpcb_getport_async);
  528. /*
  529. * Rpcbind child task calls this callback via tk_exit.
  530. */
  531. static void rpcb_getport_done(struct rpc_task *child, void *data)
  532. {
  533. struct rpcbind_args *map = data;
  534. struct rpc_xprt *xprt = map->r_xprt;
  535. int status = child->tk_status;
  536. /* Garbage reply: retry with a lesser rpcbind version */
  537. if (status == -EIO)
  538. status = -EPROTONOSUPPORT;
  539. /* rpcbind server doesn't support this rpcbind protocol version */
  540. if (status == -EPROTONOSUPPORT)
  541. xprt->bind_index++;
  542. if (status < 0) {
  543. /* rpcbind server not available on remote host? */
  544. xprt->ops->set_port(xprt, 0);
  545. } else if (map->r_port == 0) {
  546. /* Requested RPC service wasn't registered on remote host */
  547. xprt->ops->set_port(xprt, 0);
  548. status = -EACCES;
  549. } else {
  550. /* Succeeded */
  551. xprt->ops->set_port(xprt, map->r_port);
  552. xprt_set_bound(xprt);
  553. status = 0;
  554. }
  555. dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
  556. child->tk_pid, status, map->r_port);
  557. map->r_status = status;
  558. }
  559. /*
  560. * XDR functions for rpcbind
  561. */
  562. static int rpcb_encode_mapping(struct rpc_rqst *req, __be32 *p,
  563. struct rpcbind_args *rpcb)
  564. {
  565. dprintk("RPC: encoding rpcb request (%u, %u, %d, %u)\n",
  566. rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
  567. *p++ = htonl(rpcb->r_prog);
  568. *p++ = htonl(rpcb->r_vers);
  569. *p++ = htonl(rpcb->r_prot);
  570. *p++ = htonl(rpcb->r_port);
  571. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  572. return 0;
  573. }
  574. static int rpcb_decode_getport(struct rpc_rqst *req, __be32 *p,
  575. unsigned short *portp)
  576. {
  577. *portp = (unsigned short) ntohl(*p++);
  578. dprintk("RPC: rpcb getport result: %u\n",
  579. *portp);
  580. return 0;
  581. }
  582. static int rpcb_decode_set(struct rpc_rqst *req, __be32 *p,
  583. unsigned int *boolp)
  584. {
  585. *boolp = (unsigned int) ntohl(*p++);
  586. dprintk("RPC: rpcb set/unset call %s\n",
  587. (*boolp ? "succeeded" : "failed"));
  588. return 0;
  589. }
  590. static int rpcb_encode_getaddr(struct rpc_rqst *req, __be32 *p,
  591. struct rpcbind_args *rpcb)
  592. {
  593. dprintk("RPC: encoding rpcb request (%u, %u, %s)\n",
  594. rpcb->r_prog, rpcb->r_vers, rpcb->r_addr);
  595. *p++ = htonl(rpcb->r_prog);
  596. *p++ = htonl(rpcb->r_vers);
  597. p = xdr_encode_string(p, rpcb->r_netid);
  598. p = xdr_encode_string(p, rpcb->r_addr);
  599. p = xdr_encode_string(p, rpcb->r_owner);
  600. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  601. return 0;
  602. }
  603. static int rpcb_decode_getaddr(struct rpc_rqst *req, __be32 *p,
  604. unsigned short *portp)
  605. {
  606. char *addr;
  607. u32 addr_len;
  608. int c, i, f, first, val;
  609. *portp = 0;
  610. addr_len = ntohl(*p++);
  611. if (addr_len == 0) {
  612. dprintk("RPC: rpcb_decode_getaddr: "
  613. "service is not registered\n");
  614. return 0;
  615. }
  616. /*
  617. * Simple sanity check.
  618. */
  619. if (addr_len > RPCBIND_MAXUADDRLEN)
  620. goto out_err;
  621. /*
  622. * Start at the end and walk backwards until the first dot
  623. * is encountered. When the second dot is found, we have
  624. * both parts of the port number.
  625. */
  626. addr = (char *)p;
  627. val = 0;
  628. first = 1;
  629. f = 1;
  630. for (i = addr_len - 1; i > 0; i--) {
  631. c = addr[i];
  632. if (c >= '0' && c <= '9') {
  633. val += (c - '0') * f;
  634. f *= 10;
  635. } else if (c == '.') {
  636. if (first) {
  637. *portp = val;
  638. val = first = 0;
  639. f = 1;
  640. } else {
  641. *portp |= (val << 8);
  642. break;
  643. }
  644. }
  645. }
  646. /*
  647. * Simple sanity check. If we never saw a dot in the reply,
  648. * then this was probably just garbage.
  649. */
  650. if (first)
  651. goto out_err;
  652. dprintk("RPC: rpcb_decode_getaddr port=%u\n", *portp);
  653. return 0;
  654. out_err:
  655. dprintk("RPC: rpcbind server returned malformed reply\n");
  656. return -EIO;
  657. }
  658. #define RPCB_program_sz (1u)
  659. #define RPCB_version_sz (1u)
  660. #define RPCB_protocol_sz (1u)
  661. #define RPCB_port_sz (1u)
  662. #define RPCB_boolean_sz (1u)
  663. #define RPCB_netid_sz (1+XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
  664. #define RPCB_addr_sz (1+XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
  665. #define RPCB_ownerstring_sz (1+XDR_QUADLEN(RPCB_MAXOWNERLEN))
  666. #define RPCB_mappingargs_sz RPCB_program_sz+RPCB_version_sz+ \
  667. RPCB_protocol_sz+RPCB_port_sz
  668. #define RPCB_getaddrargs_sz RPCB_program_sz+RPCB_version_sz+ \
  669. RPCB_netid_sz+RPCB_addr_sz+ \
  670. RPCB_ownerstring_sz
  671. #define RPCB_setres_sz RPCB_boolean_sz
  672. #define RPCB_getportres_sz RPCB_port_sz
  673. /*
  674. * Note that RFC 1833 does not put any size restrictions on the
  675. * address string returned by the remote rpcbind database.
  676. */
  677. #define RPCB_getaddrres_sz RPCB_addr_sz
  678. #define PROC(proc, argtype, restype) \
  679. [RPCBPROC_##proc] = { \
  680. .p_proc = RPCBPROC_##proc, \
  681. .p_encode = (kxdrproc_t) rpcb_encode_##argtype, \
  682. .p_decode = (kxdrproc_t) rpcb_decode_##restype, \
  683. .p_arglen = RPCB_##argtype##args_sz, \
  684. .p_replen = RPCB_##restype##res_sz, \
  685. .p_statidx = RPCBPROC_##proc, \
  686. .p_timer = 0, \
  687. .p_name = #proc, \
  688. }
  689. /*
  690. * Not all rpcbind procedures described in RFC 1833 are implemented
  691. * since the Linux kernel RPC code requires only these.
  692. */
  693. static struct rpc_procinfo rpcb_procedures2[] = {
  694. PROC(SET, mapping, set),
  695. PROC(UNSET, mapping, set),
  696. PROC(GETPORT, mapping, getport),
  697. };
  698. static struct rpc_procinfo rpcb_procedures3[] = {
  699. PROC(SET, getaddr, set),
  700. PROC(UNSET, getaddr, set),
  701. PROC(GETADDR, getaddr, getaddr),
  702. };
  703. static struct rpc_procinfo rpcb_procedures4[] = {
  704. PROC(SET, getaddr, set),
  705. PROC(UNSET, getaddr, set),
  706. PROC(GETADDR, getaddr, getaddr),
  707. PROC(GETVERSADDR, getaddr, getaddr),
  708. };
  709. static struct rpcb_info rpcb_next_version[] = {
  710. {
  711. .rpc_vers = RPCBVERS_2,
  712. .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
  713. },
  714. {
  715. .rpc_proc = NULL,
  716. },
  717. };
  718. static struct rpcb_info rpcb_next_version6[] = {
  719. {
  720. .rpc_vers = RPCBVERS_4,
  721. .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR],
  722. },
  723. {
  724. .rpc_vers = RPCBVERS_3,
  725. .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR],
  726. },
  727. {
  728. .rpc_proc = NULL,
  729. },
  730. };
  731. static struct rpc_version rpcb_version2 = {
  732. .number = RPCBVERS_2,
  733. .nrprocs = RPCB_HIGHPROC_2,
  734. .procs = rpcb_procedures2
  735. };
  736. static struct rpc_version rpcb_version3 = {
  737. .number = RPCBVERS_3,
  738. .nrprocs = RPCB_HIGHPROC_3,
  739. .procs = rpcb_procedures3
  740. };
  741. static struct rpc_version rpcb_version4 = {
  742. .number = RPCBVERS_4,
  743. .nrprocs = RPCB_HIGHPROC_4,
  744. .procs = rpcb_procedures4
  745. };
  746. static struct rpc_version *rpcb_version[] = {
  747. NULL,
  748. NULL,
  749. &rpcb_version2,
  750. &rpcb_version3,
  751. &rpcb_version4
  752. };
  753. static struct rpc_stat rpcb_stats;
  754. static struct rpc_program rpcb_program = {
  755. .name = "rpcbind",
  756. .number = RPCBIND_PROGRAM,
  757. .nrvers = ARRAY_SIZE(rpcb_version),
  758. .version = rpcb_version,
  759. .stats = &rpcb_stats,
  760. };