rpcb_clnt.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088
  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. /**
  412. * rpcb_getport_sync - obtain the port for an RPC service on a given host
  413. * @sin: address of remote peer
  414. * @prog: RPC program number to bind
  415. * @vers: RPC version number to bind
  416. * @prot: transport protocol to use to make this request
  417. *
  418. * Return value is the requested advertised port number,
  419. * or a negative errno value.
  420. *
  421. * Called from outside the RPC client in a synchronous task context.
  422. * Uses default timeout parameters specified by underlying transport.
  423. *
  424. * XXX: Needs to support IPv6
  425. */
  426. int rpcb_getport_sync(struct sockaddr_in *sin, u32 prog, u32 vers, int prot)
  427. {
  428. struct rpcbind_args map = {
  429. .r_prog = prog,
  430. .r_vers = vers,
  431. .r_prot = prot,
  432. .r_port = 0,
  433. };
  434. struct rpc_message msg = {
  435. .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
  436. .rpc_argp = &map,
  437. .rpc_resp = &map,
  438. };
  439. struct rpc_clnt *rpcb_clnt;
  440. int status;
  441. dprintk("RPC: %s(%pI4, %u, %u, %d)\n",
  442. __func__, &sin->sin_addr.s_addr, prog, vers, prot);
  443. rpcb_clnt = rpcb_create(NULL, (struct sockaddr *)sin,
  444. sizeof(*sin), prot, RPCBVERS_2);
  445. if (IS_ERR(rpcb_clnt))
  446. return PTR_ERR(rpcb_clnt);
  447. status = rpc_call_sync(rpcb_clnt, &msg, 0);
  448. rpc_shutdown_client(rpcb_clnt);
  449. if (status >= 0) {
  450. if (map.r_port != 0)
  451. return map.r_port;
  452. status = -EACCES;
  453. }
  454. return status;
  455. }
  456. EXPORT_SYMBOL_GPL(rpcb_getport_sync);
  457. static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt, struct rpcbind_args *map, struct rpc_procinfo *proc)
  458. {
  459. struct rpc_message msg = {
  460. .rpc_proc = proc,
  461. .rpc_argp = map,
  462. .rpc_resp = map,
  463. };
  464. struct rpc_task_setup task_setup_data = {
  465. .rpc_client = rpcb_clnt,
  466. .rpc_message = &msg,
  467. .callback_ops = &rpcb_getport_ops,
  468. .callback_data = map,
  469. .flags = RPC_TASK_ASYNC | RPC_TASK_SOFTCONN,
  470. };
  471. return rpc_run_task(&task_setup_data);
  472. }
  473. /*
  474. * In the case where rpc clients have been cloned, we want to make
  475. * sure that we use the program number/version etc of the actual
  476. * owner of the xprt. To do so, we walk back up the tree of parents
  477. * to find whoever created the transport and/or whoever has the
  478. * autobind flag set.
  479. */
  480. static struct rpc_clnt *rpcb_find_transport_owner(struct rpc_clnt *clnt)
  481. {
  482. struct rpc_clnt *parent = clnt->cl_parent;
  483. while (parent != clnt) {
  484. if (parent->cl_xprt != clnt->cl_xprt)
  485. break;
  486. if (clnt->cl_autobind)
  487. break;
  488. clnt = parent;
  489. parent = parent->cl_parent;
  490. }
  491. return clnt;
  492. }
  493. /**
  494. * rpcb_getport_async - obtain the port for a given RPC service on a given host
  495. * @task: task that is waiting for portmapper request
  496. *
  497. * This one can be called for an ongoing RPC request, and can be used in
  498. * an async (rpciod) context.
  499. */
  500. void rpcb_getport_async(struct rpc_task *task)
  501. {
  502. struct rpc_clnt *clnt;
  503. struct rpc_procinfo *proc;
  504. u32 bind_version;
  505. struct rpc_xprt *xprt;
  506. struct rpc_clnt *rpcb_clnt;
  507. static struct rpcbind_args *map;
  508. struct rpc_task *child;
  509. struct sockaddr_storage addr;
  510. struct sockaddr *sap = (struct sockaddr *)&addr;
  511. size_t salen;
  512. int status;
  513. clnt = rpcb_find_transport_owner(task->tk_client);
  514. xprt = clnt->cl_xprt;
  515. dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
  516. task->tk_pid, __func__,
  517. clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot);
  518. /* Put self on the wait queue to ensure we get notified if
  519. * some other task is already attempting to bind the port */
  520. rpc_sleep_on(&xprt->binding, task, NULL);
  521. if (xprt_test_and_set_binding(xprt)) {
  522. dprintk("RPC: %5u %s: waiting for another binder\n",
  523. task->tk_pid, __func__);
  524. return;
  525. }
  526. /* Someone else may have bound if we slept */
  527. if (xprt_bound(xprt)) {
  528. status = 0;
  529. dprintk("RPC: %5u %s: already bound\n",
  530. task->tk_pid, __func__);
  531. goto bailout_nofree;
  532. }
  533. /* Parent transport's destination address */
  534. salen = rpc_peeraddr(clnt, sap, sizeof(addr));
  535. /* Don't ever use rpcbind v2 for AF_INET6 requests */
  536. switch (sap->sa_family) {
  537. case AF_INET:
  538. proc = rpcb_next_version[xprt->bind_index].rpc_proc;
  539. bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
  540. break;
  541. case AF_INET6:
  542. proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
  543. bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
  544. break;
  545. default:
  546. status = -EAFNOSUPPORT;
  547. dprintk("RPC: %5u %s: bad address family\n",
  548. task->tk_pid, __func__);
  549. goto bailout_nofree;
  550. }
  551. if (proc == NULL) {
  552. xprt->bind_index = 0;
  553. status = -EPFNOSUPPORT;
  554. dprintk("RPC: %5u %s: no more getport versions available\n",
  555. task->tk_pid, __func__);
  556. goto bailout_nofree;
  557. }
  558. dprintk("RPC: %5u %s: trying rpcbind version %u\n",
  559. task->tk_pid, __func__, bind_version);
  560. rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot,
  561. bind_version);
  562. if (IS_ERR(rpcb_clnt)) {
  563. status = PTR_ERR(rpcb_clnt);
  564. dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
  565. task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
  566. goto bailout_nofree;
  567. }
  568. map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
  569. if (!map) {
  570. status = -ENOMEM;
  571. dprintk("RPC: %5u %s: no memory available\n",
  572. task->tk_pid, __func__);
  573. goto bailout_release_client;
  574. }
  575. map->r_prog = clnt->cl_prog;
  576. map->r_vers = clnt->cl_vers;
  577. map->r_prot = xprt->prot;
  578. map->r_port = 0;
  579. map->r_xprt = xprt_get(xprt);
  580. map->r_status = -EIO;
  581. switch (bind_version) {
  582. case RPCBVERS_4:
  583. case RPCBVERS_3:
  584. map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
  585. map->r_addr = rpc_sockaddr2uaddr(sap);
  586. map->r_owner = "";
  587. break;
  588. case RPCBVERS_2:
  589. map->r_addr = NULL;
  590. break;
  591. default:
  592. BUG();
  593. }
  594. child = rpcb_call_async(rpcb_clnt, map, proc);
  595. rpc_release_client(rpcb_clnt);
  596. if (IS_ERR(child)) {
  597. /* rpcb_map_release() has freed the arguments */
  598. dprintk("RPC: %5u %s: rpc_run_task failed\n",
  599. task->tk_pid, __func__);
  600. return;
  601. }
  602. xprt->stat.bind_count++;
  603. rpc_put_task(child);
  604. return;
  605. bailout_release_client:
  606. rpc_release_client(rpcb_clnt);
  607. bailout_nofree:
  608. rpcb_wake_rpcbind_waiters(xprt, status);
  609. task->tk_status = status;
  610. }
  611. EXPORT_SYMBOL_GPL(rpcb_getport_async);
  612. /*
  613. * Rpcbind child task calls this callback via tk_exit.
  614. */
  615. static void rpcb_getport_done(struct rpc_task *child, void *data)
  616. {
  617. struct rpcbind_args *map = data;
  618. struct rpc_xprt *xprt = map->r_xprt;
  619. int status = child->tk_status;
  620. /* Garbage reply: retry with a lesser rpcbind version */
  621. if (status == -EIO)
  622. status = -EPROTONOSUPPORT;
  623. /* rpcbind server doesn't support this rpcbind protocol version */
  624. if (status == -EPROTONOSUPPORT)
  625. xprt->bind_index++;
  626. if (status < 0) {
  627. /* rpcbind server not available on remote host? */
  628. xprt->ops->set_port(xprt, 0);
  629. } else if (map->r_port == 0) {
  630. /* Requested RPC service wasn't registered on remote host */
  631. xprt->ops->set_port(xprt, 0);
  632. status = -EACCES;
  633. } else {
  634. /* Succeeded */
  635. xprt->ops->set_port(xprt, map->r_port);
  636. xprt_set_bound(xprt);
  637. status = 0;
  638. }
  639. dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
  640. child->tk_pid, status, map->r_port);
  641. map->r_status = status;
  642. }
  643. /*
  644. * XDR functions for rpcbind
  645. */
  646. static int rpcb_enc_mapping(struct rpc_rqst *req, __be32 *p,
  647. const struct rpcbind_args *rpcb)
  648. {
  649. struct rpc_task *task = req->rq_task;
  650. struct xdr_stream xdr;
  651. dprintk("RPC: %5u encoding PMAP_%s call (%u, %u, %d, %u)\n",
  652. task->tk_pid, task->tk_msg.rpc_proc->p_name,
  653. rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
  654. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  655. p = xdr_reserve_space(&xdr, sizeof(__be32) * RPCB_mappingargs_sz);
  656. if (unlikely(p == NULL))
  657. return -EIO;
  658. *p++ = htonl(rpcb->r_prog);
  659. *p++ = htonl(rpcb->r_vers);
  660. *p++ = htonl(rpcb->r_prot);
  661. *p = htonl(rpcb->r_port);
  662. return 0;
  663. }
  664. static int rpcb_dec_getport(struct rpc_rqst *req, __be32 *p,
  665. struct rpcbind_args *rpcb)
  666. {
  667. struct rpc_task *task = req->rq_task;
  668. struct xdr_stream xdr;
  669. unsigned long port;
  670. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  671. rpcb->r_port = 0;
  672. p = xdr_inline_decode(&xdr, sizeof(__be32));
  673. if (unlikely(p == NULL))
  674. return -EIO;
  675. port = ntohl(*p);
  676. dprintk("RPC: %5u PMAP_%s result: %lu\n", task->tk_pid,
  677. task->tk_msg.rpc_proc->p_name, port);
  678. if (unlikely(port > USHRT_MAX))
  679. return -EIO;
  680. rpcb->r_port = port;
  681. return 0;
  682. }
  683. static int rpcb_dec_set(struct rpc_rqst *req, __be32 *p,
  684. unsigned int *boolp)
  685. {
  686. struct rpc_task *task = req->rq_task;
  687. struct xdr_stream xdr;
  688. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  689. p = xdr_inline_decode(&xdr, sizeof(__be32));
  690. if (unlikely(p == NULL))
  691. return -EIO;
  692. *boolp = 0;
  693. if (*p)
  694. *boolp = 1;
  695. dprintk("RPC: %5u RPCB_%s call %s\n",
  696. task->tk_pid, task->tk_msg.rpc_proc->p_name,
  697. (*boolp ? "succeeded" : "failed"));
  698. return 0;
  699. }
  700. static int encode_rpcb_string(struct xdr_stream *xdr, const char *string,
  701. const u32 maxstrlen)
  702. {
  703. u32 len;
  704. __be32 *p;
  705. if (unlikely(string == NULL))
  706. return -EIO;
  707. len = strlen(string);
  708. if (unlikely(len > maxstrlen))
  709. return -EIO;
  710. p = xdr_reserve_space(xdr, sizeof(__be32) + len);
  711. if (unlikely(p == NULL))
  712. return -EIO;
  713. xdr_encode_opaque(p, string, len);
  714. return 0;
  715. }
  716. static int rpcb_enc_getaddr(struct rpc_rqst *req, __be32 *p,
  717. const struct rpcbind_args *rpcb)
  718. {
  719. struct rpc_task *task = req->rq_task;
  720. struct xdr_stream xdr;
  721. dprintk("RPC: %5u encoding RPCB_%s call (%u, %u, '%s', '%s')\n",
  722. task->tk_pid, task->tk_msg.rpc_proc->p_name,
  723. rpcb->r_prog, rpcb->r_vers,
  724. rpcb->r_netid, rpcb->r_addr);
  725. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  726. p = xdr_reserve_space(&xdr,
  727. sizeof(__be32) * (RPCB_program_sz + RPCB_version_sz));
  728. if (unlikely(p == NULL))
  729. return -EIO;
  730. *p++ = htonl(rpcb->r_prog);
  731. *p = htonl(rpcb->r_vers);
  732. if (encode_rpcb_string(&xdr, rpcb->r_netid, RPCBIND_MAXNETIDLEN))
  733. return -EIO;
  734. if (encode_rpcb_string(&xdr, rpcb->r_addr, RPCBIND_MAXUADDRLEN))
  735. return -EIO;
  736. if (encode_rpcb_string(&xdr, rpcb->r_owner, RPCB_MAXOWNERLEN))
  737. return -EIO;
  738. return 0;
  739. }
  740. static int rpcb_dec_getaddr(struct rpc_rqst *req, __be32 *p,
  741. struct rpcbind_args *rpcb)
  742. {
  743. struct sockaddr_storage address;
  744. struct sockaddr *sap = (struct sockaddr *)&address;
  745. struct rpc_task *task = req->rq_task;
  746. struct xdr_stream xdr;
  747. u32 len;
  748. rpcb->r_port = 0;
  749. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  750. p = xdr_inline_decode(&xdr, sizeof(__be32));
  751. if (unlikely(p == NULL))
  752. goto out_fail;
  753. len = ntohl(*p);
  754. /*
  755. * If the returned universal address is a null string,
  756. * the requested RPC service was not registered.
  757. */
  758. if (len == 0) {
  759. dprintk("RPC: %5u RPCB reply: program not registered\n",
  760. task->tk_pid);
  761. return 0;
  762. }
  763. if (unlikely(len > RPCBIND_MAXUADDRLEN))
  764. goto out_fail;
  765. p = xdr_inline_decode(&xdr, len);
  766. if (unlikely(p == NULL))
  767. goto out_fail;
  768. dprintk("RPC: %5u RPCB_%s reply: %s\n", task->tk_pid,
  769. task->tk_msg.rpc_proc->p_name, (char *)p);
  770. if (rpc_uaddr2sockaddr((char *)p, len, sap, sizeof(address)) == 0)
  771. goto out_fail;
  772. rpcb->r_port = rpc_get_port(sap);
  773. return 0;
  774. out_fail:
  775. dprintk("RPC: %5u malformed RPCB_%s reply\n",
  776. task->tk_pid, task->tk_msg.rpc_proc->p_name);
  777. return -EIO;
  778. }
  779. /*
  780. * Not all rpcbind procedures described in RFC 1833 are implemented
  781. * since the Linux kernel RPC code requires only these.
  782. */
  783. static struct rpc_procinfo rpcb_procedures2[] = {
  784. [RPCBPROC_SET] = {
  785. .p_proc = RPCBPROC_SET,
  786. .p_encode = (kxdrproc_t)rpcb_enc_mapping,
  787. .p_decode = (kxdrproc_t)rpcb_dec_set,
  788. .p_arglen = RPCB_mappingargs_sz,
  789. .p_replen = RPCB_setres_sz,
  790. .p_statidx = RPCBPROC_SET,
  791. .p_timer = 0,
  792. .p_name = "SET",
  793. },
  794. [RPCBPROC_UNSET] = {
  795. .p_proc = RPCBPROC_UNSET,
  796. .p_encode = (kxdrproc_t)rpcb_enc_mapping,
  797. .p_decode = (kxdrproc_t)rpcb_dec_set,
  798. .p_arglen = RPCB_mappingargs_sz,
  799. .p_replen = RPCB_setres_sz,
  800. .p_statidx = RPCBPROC_UNSET,
  801. .p_timer = 0,
  802. .p_name = "UNSET",
  803. },
  804. [RPCBPROC_GETPORT] = {
  805. .p_proc = RPCBPROC_GETPORT,
  806. .p_encode = (kxdrproc_t)rpcb_enc_mapping,
  807. .p_decode = (kxdrproc_t)rpcb_dec_getport,
  808. .p_arglen = RPCB_mappingargs_sz,
  809. .p_replen = RPCB_getportres_sz,
  810. .p_statidx = RPCBPROC_GETPORT,
  811. .p_timer = 0,
  812. .p_name = "GETPORT",
  813. },
  814. };
  815. static struct rpc_procinfo rpcb_procedures3[] = {
  816. [RPCBPROC_SET] = {
  817. .p_proc = RPCBPROC_SET,
  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_SET,
  823. .p_timer = 0,
  824. .p_name = "SET",
  825. },
  826. [RPCBPROC_UNSET] = {
  827. .p_proc = RPCBPROC_UNSET,
  828. .p_encode = (kxdrproc_t)rpcb_enc_getaddr,
  829. .p_decode = (kxdrproc_t)rpcb_dec_set,
  830. .p_arglen = RPCB_getaddrargs_sz,
  831. .p_replen = RPCB_setres_sz,
  832. .p_statidx = RPCBPROC_UNSET,
  833. .p_timer = 0,
  834. .p_name = "UNSET",
  835. },
  836. [RPCBPROC_GETADDR] = {
  837. .p_proc = RPCBPROC_GETADDR,
  838. .p_encode = (kxdrproc_t)rpcb_enc_getaddr,
  839. .p_decode = (kxdrproc_t)rpcb_dec_getaddr,
  840. .p_arglen = RPCB_getaddrargs_sz,
  841. .p_replen = RPCB_getaddrres_sz,
  842. .p_statidx = RPCBPROC_GETADDR,
  843. .p_timer = 0,
  844. .p_name = "GETADDR",
  845. },
  846. };
  847. static struct rpc_procinfo rpcb_procedures4[] = {
  848. [RPCBPROC_SET] = {
  849. .p_proc = RPCBPROC_SET,
  850. .p_encode = (kxdrproc_t)rpcb_enc_getaddr,
  851. .p_decode = (kxdrproc_t)rpcb_dec_set,
  852. .p_arglen = RPCB_getaddrargs_sz,
  853. .p_replen = RPCB_setres_sz,
  854. .p_statidx = RPCBPROC_SET,
  855. .p_timer = 0,
  856. .p_name = "SET",
  857. },
  858. [RPCBPROC_UNSET] = {
  859. .p_proc = RPCBPROC_UNSET,
  860. .p_encode = (kxdrproc_t)rpcb_enc_getaddr,
  861. .p_decode = (kxdrproc_t)rpcb_dec_set,
  862. .p_arglen = RPCB_getaddrargs_sz,
  863. .p_replen = RPCB_setres_sz,
  864. .p_statidx = RPCBPROC_UNSET,
  865. .p_timer = 0,
  866. .p_name = "UNSET",
  867. },
  868. [RPCBPROC_GETADDR] = {
  869. .p_proc = RPCBPROC_GETADDR,
  870. .p_encode = (kxdrproc_t)rpcb_enc_getaddr,
  871. .p_decode = (kxdrproc_t)rpcb_dec_getaddr,
  872. .p_arglen = RPCB_getaddrargs_sz,
  873. .p_replen = RPCB_getaddrres_sz,
  874. .p_statidx = RPCBPROC_GETADDR,
  875. .p_timer = 0,
  876. .p_name = "GETADDR",
  877. },
  878. };
  879. static struct rpcb_info rpcb_next_version[] = {
  880. {
  881. .rpc_vers = RPCBVERS_2,
  882. .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
  883. },
  884. {
  885. .rpc_proc = NULL,
  886. },
  887. };
  888. static struct rpcb_info rpcb_next_version6[] = {
  889. {
  890. .rpc_vers = RPCBVERS_4,
  891. .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR],
  892. },
  893. {
  894. .rpc_vers = RPCBVERS_3,
  895. .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR],
  896. },
  897. {
  898. .rpc_proc = NULL,
  899. },
  900. };
  901. static struct rpc_version rpcb_version2 = {
  902. .number = RPCBVERS_2,
  903. .nrprocs = RPCB_HIGHPROC_2,
  904. .procs = rpcb_procedures2
  905. };
  906. static struct rpc_version rpcb_version3 = {
  907. .number = RPCBVERS_3,
  908. .nrprocs = RPCB_HIGHPROC_3,
  909. .procs = rpcb_procedures3
  910. };
  911. static struct rpc_version rpcb_version4 = {
  912. .number = RPCBVERS_4,
  913. .nrprocs = RPCB_HIGHPROC_4,
  914. .procs = rpcb_procedures4
  915. };
  916. static struct rpc_version *rpcb_version[] = {
  917. NULL,
  918. NULL,
  919. &rpcb_version2,
  920. &rpcb_version3,
  921. &rpcb_version4
  922. };
  923. static struct rpc_stat rpcb_stats;
  924. static struct rpc_program rpcb_program = {
  925. .name = "rpcbind",
  926. .number = RPCBIND_PROGRAM,
  927. .nrvers = ARRAY_SIZE(rpcb_version),
  928. .version = rpcb_version,
  929. .stats = &rpcb_stats,
  930. };
  931. /**
  932. * cleanup_rpcb_clnt - remove xprtsock's sysctls, unregister
  933. *
  934. */
  935. void cleanup_rpcb_clnt(void)
  936. {
  937. if (rpcb_local_clnt4)
  938. rpc_shutdown_client(rpcb_local_clnt4);
  939. if (rpcb_local_clnt)
  940. rpc_shutdown_client(rpcb_local_clnt);
  941. }