rpcb_clnt.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. #define RPCB_HIGHPROC_2 RPCBPROC_CALLIT
  53. #define RPCB_HIGHPROC_3 RPCBPROC_TADDR2UADDR
  54. #define RPCB_HIGHPROC_4 RPCBPROC_GETSTAT
  55. /*
  56. * r_owner
  57. *
  58. * The "owner" is allowed to unset a service in the rpcbind database.
  59. *
  60. * For AF_LOCAL SET/UNSET requests, rpcbind treats this string as a
  61. * UID which it maps to a local user name via a password lookup.
  62. * In all other cases it is ignored.
  63. *
  64. * For SET/UNSET requests, user space provides a value, even for
  65. * network requests, and GETADDR uses an empty string. We follow
  66. * those precedents here.
  67. */
  68. #define RPCB_OWNER_STRING "0"
  69. #define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
  70. /*
  71. * XDR data type sizes
  72. */
  73. #define RPCB_program_sz (1)
  74. #define RPCB_version_sz (1)
  75. #define RPCB_protocol_sz (1)
  76. #define RPCB_port_sz (1)
  77. #define RPCB_boolean_sz (1)
  78. #define RPCB_netid_sz (1 + XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
  79. #define RPCB_addr_sz (1 + XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
  80. #define RPCB_ownerstring_sz (1 + XDR_QUADLEN(RPCB_MAXOWNERLEN))
  81. /*
  82. * XDR argument and result sizes
  83. */
  84. #define RPCB_mappingargs_sz (RPCB_program_sz + RPCB_version_sz + \
  85. RPCB_protocol_sz + RPCB_port_sz)
  86. #define RPCB_getaddrargs_sz (RPCB_program_sz + RPCB_version_sz + \
  87. RPCB_netid_sz + RPCB_addr_sz + \
  88. RPCB_ownerstring_sz)
  89. #define RPCB_getportres_sz RPCB_port_sz
  90. #define RPCB_setres_sz RPCB_boolean_sz
  91. /*
  92. * Note that RFC 1833 does not put any size restrictions on the
  93. * address string returned by the remote rpcbind database.
  94. */
  95. #define RPCB_getaddrres_sz RPCB_addr_sz
  96. static void rpcb_getport_done(struct rpc_task *, void *);
  97. static void rpcb_map_release(void *data);
  98. static struct rpc_program rpcb_program;
  99. static struct rpc_clnt * rpcb_local_clnt;
  100. static struct rpc_clnt * rpcb_local_clnt4;
  101. struct rpcbind_args {
  102. struct rpc_xprt * r_xprt;
  103. u32 r_prog;
  104. u32 r_vers;
  105. u32 r_prot;
  106. unsigned short r_port;
  107. const char * r_netid;
  108. const char * r_addr;
  109. const char * r_owner;
  110. int r_status;
  111. };
  112. static struct rpc_procinfo rpcb_procedures2[];
  113. static struct rpc_procinfo rpcb_procedures3[];
  114. static struct rpc_procinfo rpcb_procedures4[];
  115. struct rpcb_info {
  116. u32 rpc_vers;
  117. struct rpc_procinfo * rpc_proc;
  118. };
  119. static struct rpcb_info rpcb_next_version[];
  120. static struct rpcb_info rpcb_next_version6[];
  121. static const struct rpc_call_ops rpcb_getport_ops = {
  122. .rpc_call_done = rpcb_getport_done,
  123. .rpc_release = rpcb_map_release,
  124. };
  125. static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status)
  126. {
  127. xprt_clear_binding(xprt);
  128. rpc_wake_up_status(&xprt->binding, status);
  129. }
  130. static void rpcb_map_release(void *data)
  131. {
  132. struct rpcbind_args *map = data;
  133. rpcb_wake_rpcbind_waiters(map->r_xprt, map->r_status);
  134. xprt_put(map->r_xprt);
  135. kfree(map->r_addr);
  136. kfree(map);
  137. }
  138. static const struct sockaddr_in rpcb_inaddr_loopback = {
  139. .sin_family = AF_INET,
  140. .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
  141. .sin_port = htons(RPCBIND_PORT),
  142. };
  143. static DEFINE_MUTEX(rpcb_create_local_mutex);
  144. /*
  145. * Returns zero on success, otherwise a negative errno value
  146. * is returned.
  147. */
  148. static int rpcb_create_local(void)
  149. {
  150. struct rpc_create_args args = {
  151. .protocol = XPRT_TRANSPORT_TCP,
  152. .address = (struct sockaddr *)&rpcb_inaddr_loopback,
  153. .addrsize = sizeof(rpcb_inaddr_loopback),
  154. .servername = "localhost",
  155. .program = &rpcb_program,
  156. .version = RPCBVERS_2,
  157. .authflavor = RPC_AUTH_UNIX,
  158. .flags = RPC_CLNT_CREATE_NOPING,
  159. };
  160. struct rpc_clnt *clnt, *clnt4;
  161. int result = 0;
  162. if (rpcb_local_clnt)
  163. return result;
  164. mutex_lock(&rpcb_create_local_mutex);
  165. if (rpcb_local_clnt)
  166. goto out;
  167. clnt = rpc_create(&args);
  168. if (IS_ERR(clnt)) {
  169. dprintk("RPC: failed to create local rpcbind "
  170. "client (errno %ld).\n", PTR_ERR(clnt));
  171. result = -PTR_ERR(clnt);
  172. goto out;
  173. }
  174. /*
  175. * This results in an RPC ping. On systems running portmapper,
  176. * the v4 ping will fail. Proceed anyway, but disallow rpcb
  177. * v4 upcalls.
  178. */
  179. clnt4 = rpc_bind_new_program(clnt, &rpcb_program, RPCBVERS_4);
  180. if (IS_ERR(clnt4)) {
  181. dprintk("RPC: failed to create local rpcbind v4 "
  182. "cleint (errno %ld).\n", PTR_ERR(clnt4));
  183. clnt4 = NULL;
  184. }
  185. rpcb_local_clnt = clnt;
  186. rpcb_local_clnt4 = clnt4;
  187. out:
  188. mutex_unlock(&rpcb_create_local_mutex);
  189. return result;
  190. }
  191. static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
  192. size_t salen, int proto, u32 version)
  193. {
  194. struct rpc_create_args args = {
  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 NULL;
  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 int rpcb_enc_mapping(struct rpc_rqst *req, __be32 *p,
  601. const struct rpcbind_args *rpcb)
  602. {
  603. struct rpc_task *task = req->rq_task;
  604. struct xdr_stream xdr;
  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. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  609. p = xdr_reserve_space(&xdr, sizeof(__be32) * RPCB_mappingargs_sz);
  610. if (unlikely(p == NULL))
  611. return -EIO;
  612. *p++ = htonl(rpcb->r_prog);
  613. *p++ = htonl(rpcb->r_vers);
  614. *p++ = htonl(rpcb->r_prot);
  615. *p = htonl(rpcb->r_port);
  616. return 0;
  617. }
  618. static int rpcb_dec_getport(struct rpc_rqst *req, __be32 *p,
  619. struct rpcbind_args *rpcb)
  620. {
  621. struct rpc_task *task = req->rq_task;
  622. struct xdr_stream xdr;
  623. unsigned long port;
  624. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  625. rpcb->r_port = 0;
  626. p = xdr_inline_decode(&xdr, sizeof(__be32));
  627. if (unlikely(p == NULL))
  628. return -EIO;
  629. port = ntohl(*p);
  630. dprintk("RPC: %5u PMAP_%s result: %lu\n", task->tk_pid,
  631. task->tk_msg.rpc_proc->p_name, port);
  632. if (unlikely(port > USHRT_MAX))
  633. return -EIO;
  634. rpcb->r_port = port;
  635. return 0;
  636. }
  637. static int rpcb_dec_set(struct rpc_rqst *req, __be32 *p,
  638. unsigned int *boolp)
  639. {
  640. struct rpc_task *task = req->rq_task;
  641. struct xdr_stream xdr;
  642. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  643. p = xdr_inline_decode(&xdr, sizeof(__be32));
  644. if (unlikely(p == NULL))
  645. return -EIO;
  646. *boolp = 0;
  647. if (*p)
  648. *boolp = 1;
  649. dprintk("RPC: %5u RPCB_%s call %s\n",
  650. task->tk_pid, task->tk_msg.rpc_proc->p_name,
  651. (*boolp ? "succeeded" : "failed"));
  652. return 0;
  653. }
  654. static int encode_rpcb_string(struct xdr_stream *xdr, const char *string,
  655. const u32 maxstrlen)
  656. {
  657. u32 len;
  658. __be32 *p;
  659. if (unlikely(string == NULL))
  660. return -EIO;
  661. len = strlen(string);
  662. if (unlikely(len > maxstrlen))
  663. return -EIO;
  664. p = xdr_reserve_space(xdr, sizeof(__be32) + len);
  665. if (unlikely(p == NULL))
  666. return -EIO;
  667. xdr_encode_opaque(p, string, len);
  668. return 0;
  669. }
  670. static int rpcb_enc_getaddr(struct rpc_rqst *req, __be32 *p,
  671. const struct rpcbind_args *rpcb)
  672. {
  673. struct rpc_task *task = req->rq_task;
  674. struct xdr_stream xdr;
  675. dprintk("RPC: %5u encoding RPCB_%s call (%u, %u, '%s', '%s')\n",
  676. task->tk_pid, task->tk_msg.rpc_proc->p_name,
  677. rpcb->r_prog, rpcb->r_vers,
  678. rpcb->r_netid, rpcb->r_addr);
  679. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  680. p = xdr_reserve_space(&xdr,
  681. sizeof(__be32) * (RPCB_program_sz + RPCB_version_sz));
  682. if (unlikely(p == NULL))
  683. return -EIO;
  684. *p++ = htonl(rpcb->r_prog);
  685. *p = htonl(rpcb->r_vers);
  686. if (encode_rpcb_string(&xdr, rpcb->r_netid, RPCBIND_MAXNETIDLEN))
  687. return -EIO;
  688. if (encode_rpcb_string(&xdr, rpcb->r_addr, RPCBIND_MAXUADDRLEN))
  689. return -EIO;
  690. if (encode_rpcb_string(&xdr, rpcb->r_owner, RPCB_MAXOWNERLEN))
  691. return -EIO;
  692. return 0;
  693. }
  694. static int rpcb_dec_getaddr(struct rpc_rqst *req, __be32 *p,
  695. struct rpcbind_args *rpcb)
  696. {
  697. struct sockaddr_storage address;
  698. struct sockaddr *sap = (struct sockaddr *)&address;
  699. struct rpc_task *task = req->rq_task;
  700. struct xdr_stream xdr;
  701. u32 len;
  702. rpcb->r_port = 0;
  703. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  704. p = xdr_inline_decode(&xdr, sizeof(__be32));
  705. if (unlikely(p == NULL))
  706. goto out_fail;
  707. len = ntohl(*p);
  708. /*
  709. * If the returned universal address is a null string,
  710. * the requested RPC service was not registered.
  711. */
  712. if (len == 0) {
  713. dprintk("RPC: %5u RPCB reply: program not registered\n",
  714. task->tk_pid);
  715. return 0;
  716. }
  717. if (unlikely(len > RPCBIND_MAXUADDRLEN))
  718. goto out_fail;
  719. p = xdr_inline_decode(&xdr, len);
  720. if (unlikely(p == NULL))
  721. goto out_fail;
  722. dprintk("RPC: %5u RPCB_%s reply: %s\n", task->tk_pid,
  723. task->tk_msg.rpc_proc->p_name, (char *)p);
  724. if (rpc_uaddr2sockaddr((char *)p, len, sap, sizeof(address)) == 0)
  725. goto out_fail;
  726. rpcb->r_port = rpc_get_port(sap);
  727. return 0;
  728. out_fail:
  729. dprintk("RPC: %5u malformed RPCB_%s reply\n",
  730. task->tk_pid, task->tk_msg.rpc_proc->p_name);
  731. return -EIO;
  732. }
  733. /*
  734. * Not all rpcbind procedures described in RFC 1833 are implemented
  735. * since the Linux kernel RPC code requires only these.
  736. */
  737. static struct rpc_procinfo rpcb_procedures2[] = {
  738. [RPCBPROC_SET] = {
  739. .p_proc = RPCBPROC_SET,
  740. .p_encode = (kxdrproc_t)rpcb_enc_mapping,
  741. .p_decode = (kxdrproc_t)rpcb_dec_set,
  742. .p_arglen = RPCB_mappingargs_sz,
  743. .p_replen = RPCB_setres_sz,
  744. .p_statidx = RPCBPROC_SET,
  745. .p_timer = 0,
  746. .p_name = "SET",
  747. },
  748. [RPCBPROC_UNSET] = {
  749. .p_proc = RPCBPROC_UNSET,
  750. .p_encode = (kxdrproc_t)rpcb_enc_mapping,
  751. .p_decode = (kxdrproc_t)rpcb_dec_set,
  752. .p_arglen = RPCB_mappingargs_sz,
  753. .p_replen = RPCB_setres_sz,
  754. .p_statidx = RPCBPROC_UNSET,
  755. .p_timer = 0,
  756. .p_name = "UNSET",
  757. },
  758. [RPCBPROC_GETPORT] = {
  759. .p_proc = RPCBPROC_GETPORT,
  760. .p_encode = (kxdrproc_t)rpcb_enc_mapping,
  761. .p_decode = (kxdrproc_t)rpcb_dec_getport,
  762. .p_arglen = RPCB_mappingargs_sz,
  763. .p_replen = RPCB_getportres_sz,
  764. .p_statidx = RPCBPROC_GETPORT,
  765. .p_timer = 0,
  766. .p_name = "GETPORT",
  767. },
  768. };
  769. static struct rpc_procinfo rpcb_procedures3[] = {
  770. [RPCBPROC_SET] = {
  771. .p_proc = RPCBPROC_SET,
  772. .p_encode = (kxdrproc_t)rpcb_enc_getaddr,
  773. .p_decode = (kxdrproc_t)rpcb_dec_set,
  774. .p_arglen = RPCB_getaddrargs_sz,
  775. .p_replen = RPCB_setres_sz,
  776. .p_statidx = RPCBPROC_SET,
  777. .p_timer = 0,
  778. .p_name = "SET",
  779. },
  780. [RPCBPROC_UNSET] = {
  781. .p_proc = RPCBPROC_UNSET,
  782. .p_encode = (kxdrproc_t)rpcb_enc_getaddr,
  783. .p_decode = (kxdrproc_t)rpcb_dec_set,
  784. .p_arglen = RPCB_getaddrargs_sz,
  785. .p_replen = RPCB_setres_sz,
  786. .p_statidx = RPCBPROC_UNSET,
  787. .p_timer = 0,
  788. .p_name = "UNSET",
  789. },
  790. [RPCBPROC_GETADDR] = {
  791. .p_proc = RPCBPROC_GETADDR,
  792. .p_encode = (kxdrproc_t)rpcb_enc_getaddr,
  793. .p_decode = (kxdrproc_t)rpcb_dec_getaddr,
  794. .p_arglen = RPCB_getaddrargs_sz,
  795. .p_replen = RPCB_getaddrres_sz,
  796. .p_statidx = RPCBPROC_GETADDR,
  797. .p_timer = 0,
  798. .p_name = "GETADDR",
  799. },
  800. };
  801. static struct rpc_procinfo rpcb_procedures4[] = {
  802. [RPCBPROC_SET] = {
  803. .p_proc = RPCBPROC_SET,
  804. .p_encode = (kxdrproc_t)rpcb_enc_getaddr,
  805. .p_decode = (kxdrproc_t)rpcb_dec_set,
  806. .p_arglen = RPCB_getaddrargs_sz,
  807. .p_replen = RPCB_setres_sz,
  808. .p_statidx = RPCBPROC_SET,
  809. .p_timer = 0,
  810. .p_name = "SET",
  811. },
  812. [RPCBPROC_UNSET] = {
  813. .p_proc = RPCBPROC_UNSET,
  814. .p_encode = (kxdrproc_t)rpcb_enc_getaddr,
  815. .p_decode = (kxdrproc_t)rpcb_dec_set,
  816. .p_arglen = RPCB_getaddrargs_sz,
  817. .p_replen = RPCB_setres_sz,
  818. .p_statidx = RPCBPROC_UNSET,
  819. .p_timer = 0,
  820. .p_name = "UNSET",
  821. },
  822. [RPCBPROC_GETADDR] = {
  823. .p_proc = RPCBPROC_GETADDR,
  824. .p_encode = (kxdrproc_t)rpcb_enc_getaddr,
  825. .p_decode = (kxdrproc_t)rpcb_dec_getaddr,
  826. .p_arglen = RPCB_getaddrargs_sz,
  827. .p_replen = RPCB_getaddrres_sz,
  828. .p_statidx = RPCBPROC_GETADDR,
  829. .p_timer = 0,
  830. .p_name = "GETADDR",
  831. },
  832. };
  833. static struct rpcb_info rpcb_next_version[] = {
  834. {
  835. .rpc_vers = RPCBVERS_2,
  836. .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
  837. },
  838. {
  839. .rpc_proc = NULL,
  840. },
  841. };
  842. static struct rpcb_info rpcb_next_version6[] = {
  843. {
  844. .rpc_vers = RPCBVERS_4,
  845. .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR],
  846. },
  847. {
  848. .rpc_vers = RPCBVERS_3,
  849. .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR],
  850. },
  851. {
  852. .rpc_proc = NULL,
  853. },
  854. };
  855. static struct rpc_version rpcb_version2 = {
  856. .number = RPCBVERS_2,
  857. .nrprocs = RPCB_HIGHPROC_2,
  858. .procs = rpcb_procedures2
  859. };
  860. static struct rpc_version rpcb_version3 = {
  861. .number = RPCBVERS_3,
  862. .nrprocs = RPCB_HIGHPROC_3,
  863. .procs = rpcb_procedures3
  864. };
  865. static struct rpc_version rpcb_version4 = {
  866. .number = RPCBVERS_4,
  867. .nrprocs = RPCB_HIGHPROC_4,
  868. .procs = rpcb_procedures4
  869. };
  870. static struct rpc_version *rpcb_version[] = {
  871. NULL,
  872. NULL,
  873. &rpcb_version2,
  874. &rpcb_version3,
  875. &rpcb_version4
  876. };
  877. static struct rpc_stat rpcb_stats;
  878. static struct rpc_program rpcb_program = {
  879. .name = "rpcbind",
  880. .number = RPCBIND_PROGRAM,
  881. .nrvers = ARRAY_SIZE(rpcb_version),
  882. .version = rpcb_version,
  883. .stats = &rpcb_stats,
  884. };
  885. /**
  886. * cleanup_rpcb_clnt - remove xprtsock's sysctls, unregister
  887. *
  888. */
  889. void cleanup_rpcb_clnt(void)
  890. {
  891. if (rpcb_local_clnt4)
  892. rpc_shutdown_client(rpcb_local_clnt4);
  893. if (rpcb_local_clnt)
  894. rpc_shutdown_client(rpcb_local_clnt);
  895. }