rpcb_clnt.c 24 KB

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