rpcb_clnt.c 23 KB

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