rpcb_clnt.c 26 KB

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