rpcb_clnt.c 26 KB

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