rpcb_clnt.c 23 KB

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