rpcb_clnt.c 23 KB

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