rpcb_clnt.c 26 KB

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