rpcb_clnt.c 23 KB

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