rpcb_clnt.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * In-kernel rpcbind client supporting versions 2, 3, and 4 of the rpcbind
  4. * protocol
  5. *
  6. * Based on RFC 1833: "Binding Protocols for ONC RPC Version 2" and
  7. * RFC 3530: "Network File System (NFS) version 4 Protocol"
  8. *
  9. * Original: Gilles Quillard, Bull Open Source, 2005 <gilles.quillard@bull.net>
  10. * Updated: Chuck Lever, Oracle Corporation, 2007 <chuck.lever@oracle.com>
  11. *
  12. * Descended from net/sunrpc/pmap_clnt.c,
  13. * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
  14. */
  15. #include <linux/module.h>
  16. #include <linux/types.h>
  17. #include <linux/socket.h>
  18. #include <linux/un.h>
  19. #include <linux/in.h>
  20. #include <linux/in6.h>
  21. #include <linux/kernel.h>
  22. #include <linux/errno.h>
  23. #include <linux/mutex.h>
  24. #include <linux/slab.h>
  25. #include <net/ipv6.h>
  26. #include <linux/sunrpc/clnt.h>
  27. #include <linux/sunrpc/addr.h>
  28. #include <linux/sunrpc/sched.h>
  29. #include <linux/sunrpc/xprtsock.h>
  30. #include <trace/events/sunrpc.h>
  31. #include "netns.h"
  32. #define RPCBIND_SOCK_PATHNAME "/var/run/rpcbind.sock"
  33. #define RPCBIND_PROGRAM (100000u)
  34. #define RPCBIND_PORT (111u)
  35. #define RPCBVERS_2 (2u)
  36. #define RPCBVERS_3 (3u)
  37. #define RPCBVERS_4 (4u)
  38. enum {
  39. RPCBPROC_NULL,
  40. RPCBPROC_SET,
  41. RPCBPROC_UNSET,
  42. RPCBPROC_GETPORT,
  43. RPCBPROC_GETADDR = 3, /* alias for GETPORT */
  44. RPCBPROC_DUMP,
  45. RPCBPROC_CALLIT,
  46. RPCBPROC_BCAST = 5, /* alias for CALLIT */
  47. RPCBPROC_GETTIME,
  48. RPCBPROC_UADDR2TADDR,
  49. RPCBPROC_TADDR2UADDR,
  50. RPCBPROC_GETVERSADDR,
  51. RPCBPROC_INDIRECT,
  52. RPCBPROC_GETADDRLIST,
  53. RPCBPROC_GETSTAT,
  54. };
  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 const struct rpc_program rpcb_program;
  99. struct rpcbind_args {
  100. struct rpc_xprt * r_xprt;
  101. u32 r_prog;
  102. u32 r_vers;
  103. u32 r_prot;
  104. unsigned short r_port;
  105. const char * r_netid;
  106. const char * r_addr;
  107. const char * r_owner;
  108. int r_status;
  109. };
  110. static const struct rpc_procinfo rpcb_procedures2[];
  111. static const struct rpc_procinfo rpcb_procedures3[];
  112. static const struct rpc_procinfo rpcb_procedures4[];
  113. struct rpcb_info {
  114. u32 rpc_vers;
  115. const struct rpc_procinfo *rpc_proc;
  116. };
  117. static const struct rpcb_info rpcb_next_version[];
  118. static const struct rpcb_info rpcb_next_version6[];
  119. static const struct rpc_call_ops rpcb_getport_ops = {
  120. .rpc_call_done = rpcb_getport_done,
  121. .rpc_release = rpcb_map_release,
  122. };
  123. static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status)
  124. {
  125. xprt_clear_binding(xprt);
  126. rpc_wake_up_status(&xprt->binding, status);
  127. }
  128. static void rpcb_map_release(void *data)
  129. {
  130. struct rpcbind_args *map = data;
  131. rpcb_wake_rpcbind_waiters(map->r_xprt, map->r_status);
  132. xprt_put(map->r_xprt);
  133. kfree(map->r_addr);
  134. kfree(map);
  135. }
  136. static int rpcb_get_local(struct net *net)
  137. {
  138. int cnt;
  139. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  140. spin_lock(&sn->rpcb_clnt_lock);
  141. if (sn->rpcb_users)
  142. sn->rpcb_users++;
  143. cnt = sn->rpcb_users;
  144. spin_unlock(&sn->rpcb_clnt_lock);
  145. return cnt;
  146. }
  147. void rpcb_put_local(struct net *net)
  148. {
  149. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  150. struct rpc_clnt *clnt = sn->rpcb_local_clnt;
  151. struct rpc_clnt *clnt4 = sn->rpcb_local_clnt4;
  152. int shutdown = 0;
  153. spin_lock(&sn->rpcb_clnt_lock);
  154. if (sn->rpcb_users) {
  155. if (--sn->rpcb_users == 0) {
  156. sn->rpcb_local_clnt = NULL;
  157. sn->rpcb_local_clnt4 = NULL;
  158. }
  159. shutdown = !sn->rpcb_users;
  160. }
  161. spin_unlock(&sn->rpcb_clnt_lock);
  162. if (shutdown) {
  163. /*
  164. * cleanup_rpcb_clnt - remove xprtsock's sysctls, unregister
  165. */
  166. if (clnt4)
  167. rpc_shutdown_client(clnt4);
  168. if (clnt)
  169. rpc_shutdown_client(clnt);
  170. }
  171. }
  172. static void rpcb_set_local(struct net *net, struct rpc_clnt *clnt,
  173. struct rpc_clnt *clnt4,
  174. bool is_af_local)
  175. {
  176. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  177. /* Protected by rpcb_create_local_mutex */
  178. sn->rpcb_local_clnt = clnt;
  179. sn->rpcb_local_clnt4 = clnt4;
  180. sn->rpcb_is_af_local = is_af_local ? 1 : 0;
  181. smp_wmb();
  182. sn->rpcb_users = 1;
  183. }
  184. /*
  185. * Returns zero on success, otherwise a negative errno value
  186. * is returned.
  187. */
  188. static int rpcb_create_local_unix(struct net *net)
  189. {
  190. static const struct sockaddr_un rpcb_localaddr_rpcbind = {
  191. .sun_family = AF_LOCAL,
  192. .sun_path = RPCBIND_SOCK_PATHNAME,
  193. };
  194. struct rpc_create_args args = {
  195. .net = net,
  196. .protocol = XPRT_TRANSPORT_LOCAL,
  197. .address = (struct sockaddr *)&rpcb_localaddr_rpcbind,
  198. .addrsize = sizeof(rpcb_localaddr_rpcbind),
  199. .servername = "localhost",
  200. .program = &rpcb_program,
  201. .version = RPCBVERS_2,
  202. .authflavor = RPC_AUTH_NULL,
  203. .cred = current_cred(),
  204. /*
  205. * We turn off the idle timeout to prevent the kernel
  206. * from automatically disconnecting the socket.
  207. * Otherwise, we'd have to cache the mount namespace
  208. * of the caller and somehow pass that to the socket
  209. * reconnect code.
  210. */
  211. .flags = RPC_CLNT_CREATE_NO_IDLE_TIMEOUT,
  212. };
  213. struct rpc_clnt *clnt, *clnt4;
  214. int result = 0;
  215. /*
  216. * Because we requested an RPC PING at transport creation time,
  217. * this works only if the user space portmapper is rpcbind, and
  218. * it's listening on AF_LOCAL on the named socket.
  219. */
  220. clnt = rpc_create(&args);
  221. if (IS_ERR(clnt)) {
  222. result = PTR_ERR(clnt);
  223. goto out;
  224. }
  225. clnt4 = rpc_bind_new_program(clnt, &rpcb_program, RPCBVERS_4);
  226. if (IS_ERR(clnt4))
  227. clnt4 = NULL;
  228. rpcb_set_local(net, clnt, clnt4, true);
  229. out:
  230. return result;
  231. }
  232. /*
  233. * Returns zero on success, otherwise a negative errno value
  234. * is returned.
  235. */
  236. static int rpcb_create_local_net(struct net *net)
  237. {
  238. static const struct sockaddr_in rpcb_inaddr_loopback = {
  239. .sin_family = AF_INET,
  240. .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
  241. .sin_port = htons(RPCBIND_PORT),
  242. };
  243. struct rpc_create_args args = {
  244. .net = net,
  245. .protocol = XPRT_TRANSPORT_TCP,
  246. .address = (struct sockaddr *)&rpcb_inaddr_loopback,
  247. .addrsize = sizeof(rpcb_inaddr_loopback),
  248. .servername = "localhost",
  249. .program = &rpcb_program,
  250. .version = RPCBVERS_2,
  251. .authflavor = RPC_AUTH_UNIX,
  252. .cred = current_cred(),
  253. .flags = RPC_CLNT_CREATE_NOPING,
  254. };
  255. struct rpc_clnt *clnt, *clnt4;
  256. int result = 0;
  257. clnt = rpc_create(&args);
  258. if (IS_ERR(clnt)) {
  259. result = PTR_ERR(clnt);
  260. goto out;
  261. }
  262. /*
  263. * This results in an RPC ping. On systems running portmapper,
  264. * the v4 ping will fail. Proceed anyway, but disallow rpcb
  265. * v4 upcalls.
  266. */
  267. clnt4 = rpc_bind_new_program(clnt, &rpcb_program, RPCBVERS_4);
  268. if (IS_ERR(clnt4))
  269. clnt4 = NULL;
  270. rpcb_set_local(net, clnt, clnt4, false);
  271. out:
  272. return result;
  273. }
  274. /*
  275. * Returns zero on success, otherwise a negative errno value
  276. * is returned.
  277. */
  278. int rpcb_create_local(struct net *net)
  279. {
  280. static DEFINE_MUTEX(rpcb_create_local_mutex);
  281. int result = 0;
  282. if (rpcb_get_local(net))
  283. return result;
  284. mutex_lock(&rpcb_create_local_mutex);
  285. if (rpcb_get_local(net))
  286. goto out;
  287. if (rpcb_create_local_unix(net) != 0)
  288. result = rpcb_create_local_net(net);
  289. out:
  290. mutex_unlock(&rpcb_create_local_mutex);
  291. return result;
  292. }
  293. static struct rpc_clnt *rpcb_create(struct net *net, const char *nodename,
  294. const char *hostname,
  295. struct sockaddr *srvaddr, size_t salen,
  296. int proto, u32 version,
  297. const struct cred *cred)
  298. {
  299. struct rpc_create_args args = {
  300. .net = net,
  301. .protocol = proto,
  302. .address = srvaddr,
  303. .addrsize = salen,
  304. .servername = hostname,
  305. .nodename = nodename,
  306. .program = &rpcb_program,
  307. .version = version,
  308. .authflavor = RPC_AUTH_UNIX,
  309. .cred = cred,
  310. .flags = (RPC_CLNT_CREATE_NOPING |
  311. RPC_CLNT_CREATE_NONPRIVPORT),
  312. };
  313. switch (srvaddr->sa_family) {
  314. case AF_INET:
  315. ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
  316. break;
  317. case AF_INET6:
  318. ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
  319. break;
  320. default:
  321. return ERR_PTR(-EAFNOSUPPORT);
  322. }
  323. return rpc_create(&args);
  324. }
  325. static int rpcb_register_call(struct sunrpc_net *sn, struct rpc_clnt *clnt, struct rpc_message *msg, bool is_set)
  326. {
  327. int flags = RPC_TASK_NOCONNECT;
  328. int error, result = 0;
  329. if (is_set || !sn->rpcb_is_af_local)
  330. flags = RPC_TASK_SOFTCONN;
  331. msg->rpc_resp = &result;
  332. error = rpc_call_sync(clnt, msg, flags);
  333. if (error < 0)
  334. return error;
  335. if (!result)
  336. return -EACCES;
  337. return 0;
  338. }
  339. /**
  340. * rpcb_register - set or unset a port registration with the local rpcbind svc
  341. * @net: target network namespace
  342. * @prog: RPC program number to bind
  343. * @vers: RPC version number to bind
  344. * @prot: transport protocol to register
  345. * @port: port value to 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, transport]
  355. * tuple they wish to advertise.
  356. *
  357. * Callers may also unregister RPC services that are no longer
  358. * available by setting the passed-in port to zero. This removes
  359. * all registered transports for [program, version] from the local
  360. * rpcbind database.
  361. *
  362. * This function uses rpcbind protocol version 2 to contact the
  363. * local rpcbind daemon.
  364. *
  365. * Registration works over both AF_INET and AF_INET6, and services
  366. * registered via this function are advertised as available for any
  367. * address. If the local rpcbind daemon is listening on AF_INET6,
  368. * services registered via this function will be advertised on
  369. * IN6ADDR_ANY (ie available for all AF_INET and AF_INET6
  370. * addresses).
  371. */
  372. int rpcb_register(struct net *net, u32 prog, u32 vers, int prot, unsigned short port)
  373. {
  374. struct rpcbind_args map = {
  375. .r_prog = prog,
  376. .r_vers = vers,
  377. .r_prot = prot,
  378. .r_port = port,
  379. };
  380. struct rpc_message msg = {
  381. .rpc_argp = &map,
  382. };
  383. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  384. bool is_set = false;
  385. trace_pmap_register(prog, vers, prot, port);
  386. msg.rpc_proc = &rpcb_procedures2[RPCBPROC_UNSET];
  387. if (port != 0) {
  388. msg.rpc_proc = &rpcb_procedures2[RPCBPROC_SET];
  389. is_set = true;
  390. }
  391. return rpcb_register_call(sn, sn->rpcb_local_clnt, &msg, is_set);
  392. }
  393. /*
  394. * Fill in AF_INET family-specific arguments to register
  395. */
  396. static int rpcb_register_inet4(struct sunrpc_net *sn,
  397. const struct sockaddr *sap,
  398. struct rpc_message *msg)
  399. {
  400. const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
  401. struct rpcbind_args *map = msg->rpc_argp;
  402. unsigned short port = ntohs(sin->sin_port);
  403. bool is_set = false;
  404. int result;
  405. map->r_addr = rpc_sockaddr2uaddr(sap, GFP_KERNEL);
  406. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  407. if (port != 0) {
  408. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
  409. is_set = true;
  410. }
  411. result = rpcb_register_call(sn, sn->rpcb_local_clnt4, msg, is_set);
  412. kfree(map->r_addr);
  413. return result;
  414. }
  415. /*
  416. * Fill in AF_INET6 family-specific arguments to register
  417. */
  418. static int rpcb_register_inet6(struct sunrpc_net *sn,
  419. const struct sockaddr *sap,
  420. struct rpc_message *msg)
  421. {
  422. const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sap;
  423. struct rpcbind_args *map = msg->rpc_argp;
  424. unsigned short port = ntohs(sin6->sin6_port);
  425. bool is_set = false;
  426. int result;
  427. map->r_addr = rpc_sockaddr2uaddr(sap, GFP_KERNEL);
  428. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  429. if (port != 0) {
  430. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
  431. is_set = true;
  432. }
  433. result = rpcb_register_call(sn, sn->rpcb_local_clnt4, msg, is_set);
  434. kfree(map->r_addr);
  435. return result;
  436. }
  437. static int rpcb_unregister_all_protofamilies(struct sunrpc_net *sn,
  438. struct rpc_message *msg)
  439. {
  440. struct rpcbind_args *map = msg->rpc_argp;
  441. trace_rpcb_unregister(map->r_prog, map->r_vers, map->r_netid);
  442. map->r_addr = "";
  443. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  444. return rpcb_register_call(sn, sn->rpcb_local_clnt4, msg, false);
  445. }
  446. /**
  447. * rpcb_v4_register - set or unset a port registration with the local rpcbind
  448. * @net: target network namespace
  449. * @program: RPC program number of service to (un)register
  450. * @version: RPC version number of service to (un)register
  451. * @address: address family, IP address, and port to (un)register
  452. * @netid: netid of transport protocol to (un)register
  453. *
  454. * Returns zero if the registration request was dispatched successfully
  455. * and the rpcbind daemon returned success. Otherwise, returns an errno
  456. * value that reflects the nature of the error (request could not be
  457. * dispatched, timed out, or rpcbind returned an error).
  458. *
  459. * RPC services invoke this function to advertise their contact
  460. * information via the system's rpcbind daemon. RPC services
  461. * invoke this function once for each [program, version, address,
  462. * netid] tuple they wish to advertise.
  463. *
  464. * Callers may also unregister RPC services that are registered at a
  465. * specific address by setting the port number in @address to zero.
  466. * They may unregister all registered protocol families at once for
  467. * a service by passing a NULL @address argument. If @netid is ""
  468. * then all netids for [program, version, address] are unregistered.
  469. *
  470. * This function uses rpcbind protocol version 4 to contact the
  471. * local rpcbind daemon. The local rpcbind daemon must support
  472. * version 4 of the rpcbind protocol in order for these functions
  473. * to register a service successfully.
  474. *
  475. * Supported netids include "udp" and "tcp" for UDP and TCP over
  476. * IPv4, and "udp6" and "tcp6" for UDP and TCP over IPv6,
  477. * respectively.
  478. *
  479. * The contents of @address determine the address family and the
  480. * port to be registered. The usual practice is to pass INADDR_ANY
  481. * as the raw address, but specifying a non-zero address is also
  482. * supported by this API if the caller wishes to advertise an RPC
  483. * service on a specific network interface.
  484. *
  485. * Note that passing in INADDR_ANY does not create the same service
  486. * registration as IN6ADDR_ANY. The former advertises an RPC
  487. * service on any IPv4 address, but not on IPv6. The latter
  488. * advertises the service on all IPv4 and IPv6 addresses.
  489. */
  490. int rpcb_v4_register(struct net *net, const u32 program, const u32 version,
  491. const struct sockaddr *address, const char *netid)
  492. {
  493. struct rpcbind_args map = {
  494. .r_prog = program,
  495. .r_vers = version,
  496. .r_netid = netid,
  497. .r_owner = RPCB_OWNER_STRING,
  498. };
  499. struct rpc_message msg = {
  500. .rpc_argp = &map,
  501. };
  502. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  503. if (sn->rpcb_local_clnt4 == NULL)
  504. return -EPROTONOSUPPORT;
  505. if (address == NULL)
  506. return rpcb_unregister_all_protofamilies(sn, &msg);
  507. trace_rpcb_register(map.r_prog, map.r_vers, map.r_addr, map.r_netid);
  508. switch (address->sa_family) {
  509. case AF_INET:
  510. return rpcb_register_inet4(sn, address, &msg);
  511. case AF_INET6:
  512. return rpcb_register_inet6(sn, address, &msg);
  513. }
  514. return -EAFNOSUPPORT;
  515. }
  516. static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt,
  517. struct rpcbind_args *map, const struct rpc_procinfo *proc)
  518. {
  519. struct rpc_message msg = {
  520. .rpc_proc = proc,
  521. .rpc_argp = map,
  522. .rpc_resp = map,
  523. };
  524. struct rpc_task_setup task_setup_data = {
  525. .rpc_client = rpcb_clnt,
  526. .rpc_message = &msg,
  527. .callback_ops = &rpcb_getport_ops,
  528. .callback_data = map,
  529. .flags = RPC_TASK_ASYNC | RPC_TASK_SOFTCONN,
  530. };
  531. return rpc_run_task(&task_setup_data);
  532. }
  533. /*
  534. * In the case where rpc clients have been cloned, we want to make
  535. * sure that we use the program number/version etc of the actual
  536. * owner of the xprt. To do so, we walk back up the tree of parents
  537. * to find whoever created the transport and/or whoever has the
  538. * autobind flag set.
  539. */
  540. static struct rpc_clnt *rpcb_find_transport_owner(struct rpc_clnt *clnt)
  541. {
  542. struct rpc_clnt *parent = clnt->cl_parent;
  543. struct rpc_xprt_switch *xps = rcu_access_pointer(clnt->cl_xpi.xpi_xpswitch);
  544. while (parent != clnt) {
  545. if (rcu_access_pointer(parent->cl_xpi.xpi_xpswitch) != xps)
  546. break;
  547. if (clnt->cl_autobind)
  548. break;
  549. clnt = parent;
  550. parent = parent->cl_parent;
  551. }
  552. return clnt;
  553. }
  554. /**
  555. * rpcb_getport_async - obtain the port for a given RPC service on a given host
  556. * @task: task that is waiting for portmapper request
  557. *
  558. * This one can be called for an ongoing RPC request, and can be used in
  559. * an async (rpciod) context.
  560. */
  561. void rpcb_getport_async(struct rpc_task *task)
  562. {
  563. struct rpc_clnt *clnt;
  564. const struct rpc_procinfo *proc;
  565. u32 bind_version;
  566. struct rpc_xprt *xprt;
  567. struct rpc_clnt *rpcb_clnt;
  568. struct rpcbind_args *map;
  569. struct rpc_task *child;
  570. struct sockaddr_storage addr;
  571. struct sockaddr *sap = (struct sockaddr *)&addr;
  572. size_t salen;
  573. int status;
  574. rcu_read_lock();
  575. clnt = rpcb_find_transport_owner(task->tk_client);
  576. rcu_read_unlock();
  577. xprt = xprt_get(task->tk_xprt);
  578. /* Put self on the wait queue to ensure we get notified if
  579. * some other task is already attempting to bind the port */
  580. rpc_sleep_on_timeout(&xprt->binding, task,
  581. NULL, jiffies + xprt->bind_timeout);
  582. if (xprt_test_and_set_binding(xprt)) {
  583. xprt_put(xprt);
  584. return;
  585. }
  586. /* Someone else may have bound if we slept */
  587. if (xprt_bound(xprt)) {
  588. status = 0;
  589. goto bailout_nofree;
  590. }
  591. /* Parent transport's destination address */
  592. salen = rpc_peeraddr(clnt, sap, sizeof(addr));
  593. /* Don't ever use rpcbind v2 for AF_INET6 requests */
  594. switch (sap->sa_family) {
  595. case AF_INET:
  596. proc = rpcb_next_version[xprt->bind_index].rpc_proc;
  597. bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
  598. break;
  599. case AF_INET6:
  600. proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
  601. bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
  602. break;
  603. default:
  604. status = -EAFNOSUPPORT;
  605. goto bailout_nofree;
  606. }
  607. if (proc == NULL) {
  608. xprt->bind_index = 0;
  609. status = -EPFNOSUPPORT;
  610. goto bailout_nofree;
  611. }
  612. trace_rpcb_getport(clnt, task, bind_version);
  613. rpcb_clnt = rpcb_create(xprt->xprt_net,
  614. clnt->cl_nodename,
  615. xprt->servername, sap, salen,
  616. xprt->prot, bind_version,
  617. clnt->cl_cred);
  618. if (IS_ERR(rpcb_clnt)) {
  619. status = PTR_ERR(rpcb_clnt);
  620. goto bailout_nofree;
  621. }
  622. map = kzalloc(sizeof(struct rpcbind_args), GFP_NOFS);
  623. if (!map) {
  624. status = -ENOMEM;
  625. goto bailout_release_client;
  626. }
  627. map->r_prog = clnt->cl_prog;
  628. map->r_vers = clnt->cl_vers;
  629. map->r_prot = xprt->prot;
  630. map->r_port = 0;
  631. map->r_xprt = xprt;
  632. map->r_status = -EIO;
  633. switch (bind_version) {
  634. case RPCBVERS_4:
  635. case RPCBVERS_3:
  636. map->r_netid = xprt->address_strings[RPC_DISPLAY_NETID];
  637. map->r_addr = rpc_sockaddr2uaddr(sap, GFP_NOFS);
  638. if (!map->r_addr) {
  639. status = -ENOMEM;
  640. goto bailout_free_args;
  641. }
  642. map->r_owner = "";
  643. break;
  644. case RPCBVERS_2:
  645. map->r_addr = NULL;
  646. break;
  647. default:
  648. BUG();
  649. }
  650. child = rpcb_call_async(rpcb_clnt, map, proc);
  651. rpc_release_client(rpcb_clnt);
  652. xprt->stat.bind_count++;
  653. rpc_put_task(child);
  654. return;
  655. bailout_free_args:
  656. kfree(map);
  657. bailout_release_client:
  658. rpc_release_client(rpcb_clnt);
  659. bailout_nofree:
  660. rpcb_wake_rpcbind_waiters(xprt, status);
  661. task->tk_status = status;
  662. xprt_put(xprt);
  663. }
  664. EXPORT_SYMBOL_GPL(rpcb_getport_async);
  665. /*
  666. * Rpcbind child task calls this callback via tk_exit.
  667. */
  668. static void rpcb_getport_done(struct rpc_task *child, void *data)
  669. {
  670. struct rpcbind_args *map = data;
  671. struct rpc_xprt *xprt = map->r_xprt;
  672. map->r_status = child->tk_status;
  673. /* Garbage reply: retry with a lesser rpcbind version */
  674. if (map->r_status == -EIO)
  675. map->r_status = -EPROTONOSUPPORT;
  676. /* rpcbind server doesn't support this rpcbind protocol version */
  677. if (map->r_status == -EPROTONOSUPPORT)
  678. xprt->bind_index++;
  679. if (map->r_status < 0) {
  680. /* rpcbind server not available on remote host? */
  681. map->r_port = 0;
  682. } else if (map->r_port == 0) {
  683. /* Requested RPC service wasn't registered on remote host */
  684. map->r_status = -EACCES;
  685. } else {
  686. /* Succeeded */
  687. map->r_status = 0;
  688. }
  689. trace_rpcb_setport(child, map->r_status, map->r_port);
  690. xprt->ops->set_port(xprt, map->r_port);
  691. if (map->r_port)
  692. xprt_set_bound(xprt);
  693. }
  694. /*
  695. * XDR functions for rpcbind
  696. */
  697. static void rpcb_enc_mapping(struct rpc_rqst *req, struct xdr_stream *xdr,
  698. const void *data)
  699. {
  700. const struct rpcbind_args *rpcb = data;
  701. __be32 *p;
  702. p = xdr_reserve_space(xdr, RPCB_mappingargs_sz << 2);
  703. *p++ = cpu_to_be32(rpcb->r_prog);
  704. *p++ = cpu_to_be32(rpcb->r_vers);
  705. *p++ = cpu_to_be32(rpcb->r_prot);
  706. *p = cpu_to_be32(rpcb->r_port);
  707. }
  708. static int rpcb_dec_getport(struct rpc_rqst *req, struct xdr_stream *xdr,
  709. void *data)
  710. {
  711. struct rpcbind_args *rpcb = data;
  712. unsigned long port;
  713. __be32 *p;
  714. rpcb->r_port = 0;
  715. p = xdr_inline_decode(xdr, 4);
  716. if (unlikely(p == NULL))
  717. return -EIO;
  718. port = be32_to_cpup(p);
  719. if (unlikely(port > USHRT_MAX))
  720. return -EIO;
  721. rpcb->r_port = port;
  722. return 0;
  723. }
  724. static int rpcb_dec_set(struct rpc_rqst *req, struct xdr_stream *xdr,
  725. void *data)
  726. {
  727. unsigned int *boolp = data;
  728. __be32 *p;
  729. p = xdr_inline_decode(xdr, 4);
  730. if (unlikely(p == NULL))
  731. return -EIO;
  732. *boolp = 0;
  733. if (*p != xdr_zero)
  734. *boolp = 1;
  735. return 0;
  736. }
  737. static void encode_rpcb_string(struct xdr_stream *xdr, const char *string,
  738. const u32 maxstrlen)
  739. {
  740. __be32 *p;
  741. u32 len;
  742. len = strlen(string);
  743. WARN_ON_ONCE(len > maxstrlen);
  744. if (len > maxstrlen)
  745. /* truncate and hope for the best */
  746. len = maxstrlen;
  747. p = xdr_reserve_space(xdr, 4 + len);
  748. xdr_encode_opaque(p, string, len);
  749. }
  750. static void rpcb_enc_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr,
  751. const void *data)
  752. {
  753. const struct rpcbind_args *rpcb = data;
  754. __be32 *p;
  755. p = xdr_reserve_space(xdr, (RPCB_program_sz + RPCB_version_sz) << 2);
  756. *p++ = cpu_to_be32(rpcb->r_prog);
  757. *p = cpu_to_be32(rpcb->r_vers);
  758. encode_rpcb_string(xdr, rpcb->r_netid, RPCBIND_MAXNETIDLEN);
  759. encode_rpcb_string(xdr, rpcb->r_addr, RPCBIND_MAXUADDRLEN);
  760. encode_rpcb_string(xdr, rpcb->r_owner, RPCB_MAXOWNERLEN);
  761. }
  762. static int rpcb_dec_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr,
  763. void *data)
  764. {
  765. struct rpcbind_args *rpcb = data;
  766. struct sockaddr_storage address;
  767. struct sockaddr *sap = (struct sockaddr *)&address;
  768. __be32 *p;
  769. u32 len;
  770. rpcb->r_port = 0;
  771. p = xdr_inline_decode(xdr, 4);
  772. if (unlikely(p == NULL))
  773. goto out_fail;
  774. len = be32_to_cpup(p);
  775. /*
  776. * If the returned universal address is a null string,
  777. * the requested RPC service was not registered.
  778. */
  779. if (len == 0)
  780. return 0;
  781. if (unlikely(len > RPCBIND_MAXUADDRLEN))
  782. goto out_fail;
  783. p = xdr_inline_decode(xdr, len);
  784. if (unlikely(p == NULL))
  785. goto out_fail;
  786. if (rpc_uaddr2sockaddr(req->rq_xprt->xprt_net, (char *)p, len,
  787. sap, sizeof(address)) == 0)
  788. goto out_fail;
  789. rpcb->r_port = rpc_get_port(sap);
  790. return 0;
  791. out_fail:
  792. return -EIO;
  793. }
  794. /*
  795. * Not all rpcbind procedures described in RFC 1833 are implemented
  796. * since the Linux kernel RPC code requires only these.
  797. */
  798. static const struct rpc_procinfo rpcb_procedures2[] = {
  799. [RPCBPROC_SET] = {
  800. .p_proc = RPCBPROC_SET,
  801. .p_encode = rpcb_enc_mapping,
  802. .p_decode = rpcb_dec_set,
  803. .p_arglen = RPCB_mappingargs_sz,
  804. .p_replen = RPCB_setres_sz,
  805. .p_statidx = RPCBPROC_SET,
  806. .p_timer = 0,
  807. .p_name = "SET",
  808. },
  809. [RPCBPROC_UNSET] = {
  810. .p_proc = RPCBPROC_UNSET,
  811. .p_encode = rpcb_enc_mapping,
  812. .p_decode = rpcb_dec_set,
  813. .p_arglen = RPCB_mappingargs_sz,
  814. .p_replen = RPCB_setres_sz,
  815. .p_statidx = RPCBPROC_UNSET,
  816. .p_timer = 0,
  817. .p_name = "UNSET",
  818. },
  819. [RPCBPROC_GETPORT] = {
  820. .p_proc = RPCBPROC_GETPORT,
  821. .p_encode = rpcb_enc_mapping,
  822. .p_decode = rpcb_dec_getport,
  823. .p_arglen = RPCB_mappingargs_sz,
  824. .p_replen = RPCB_getportres_sz,
  825. .p_statidx = RPCBPROC_GETPORT,
  826. .p_timer = 0,
  827. .p_name = "GETPORT",
  828. },
  829. };
  830. static const struct rpc_procinfo rpcb_procedures3[] = {
  831. [RPCBPROC_SET] = {
  832. .p_proc = RPCBPROC_SET,
  833. .p_encode = rpcb_enc_getaddr,
  834. .p_decode = rpcb_dec_set,
  835. .p_arglen = RPCB_getaddrargs_sz,
  836. .p_replen = RPCB_setres_sz,
  837. .p_statidx = RPCBPROC_SET,
  838. .p_timer = 0,
  839. .p_name = "SET",
  840. },
  841. [RPCBPROC_UNSET] = {
  842. .p_proc = RPCBPROC_UNSET,
  843. .p_encode = rpcb_enc_getaddr,
  844. .p_decode = rpcb_dec_set,
  845. .p_arglen = RPCB_getaddrargs_sz,
  846. .p_replen = RPCB_setres_sz,
  847. .p_statidx = RPCBPROC_UNSET,
  848. .p_timer = 0,
  849. .p_name = "UNSET",
  850. },
  851. [RPCBPROC_GETADDR] = {
  852. .p_proc = RPCBPROC_GETADDR,
  853. .p_encode = rpcb_enc_getaddr,
  854. .p_decode = rpcb_dec_getaddr,
  855. .p_arglen = RPCB_getaddrargs_sz,
  856. .p_replen = RPCB_getaddrres_sz,
  857. .p_statidx = RPCBPROC_GETADDR,
  858. .p_timer = 0,
  859. .p_name = "GETADDR",
  860. },
  861. };
  862. static const struct rpc_procinfo rpcb_procedures4[] = {
  863. [RPCBPROC_SET] = {
  864. .p_proc = RPCBPROC_SET,
  865. .p_encode = rpcb_enc_getaddr,
  866. .p_decode = rpcb_dec_set,
  867. .p_arglen = RPCB_getaddrargs_sz,
  868. .p_replen = RPCB_setres_sz,
  869. .p_statidx = RPCBPROC_SET,
  870. .p_timer = 0,
  871. .p_name = "SET",
  872. },
  873. [RPCBPROC_UNSET] = {
  874. .p_proc = RPCBPROC_UNSET,
  875. .p_encode = rpcb_enc_getaddr,
  876. .p_decode = rpcb_dec_set,
  877. .p_arglen = RPCB_getaddrargs_sz,
  878. .p_replen = RPCB_setres_sz,
  879. .p_statidx = RPCBPROC_UNSET,
  880. .p_timer = 0,
  881. .p_name = "UNSET",
  882. },
  883. [RPCBPROC_GETADDR] = {
  884. .p_proc = RPCBPROC_GETADDR,
  885. .p_encode = rpcb_enc_getaddr,
  886. .p_decode = rpcb_dec_getaddr,
  887. .p_arglen = RPCB_getaddrargs_sz,
  888. .p_replen = RPCB_getaddrres_sz,
  889. .p_statidx = RPCBPROC_GETADDR,
  890. .p_timer = 0,
  891. .p_name = "GETADDR",
  892. },
  893. };
  894. static const struct rpcb_info rpcb_next_version[] = {
  895. {
  896. .rpc_vers = RPCBVERS_2,
  897. .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
  898. },
  899. {
  900. .rpc_proc = NULL,
  901. },
  902. };
  903. static const struct rpcb_info rpcb_next_version6[] = {
  904. {
  905. .rpc_vers = RPCBVERS_4,
  906. .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR],
  907. },
  908. {
  909. .rpc_vers = RPCBVERS_3,
  910. .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR],
  911. },
  912. {
  913. .rpc_proc = NULL,
  914. },
  915. };
  916. static unsigned int rpcb_version2_counts[ARRAY_SIZE(rpcb_procedures2)];
  917. static const struct rpc_version rpcb_version2 = {
  918. .number = RPCBVERS_2,
  919. .nrprocs = ARRAY_SIZE(rpcb_procedures2),
  920. .procs = rpcb_procedures2,
  921. .counts = rpcb_version2_counts,
  922. };
  923. static unsigned int rpcb_version3_counts[ARRAY_SIZE(rpcb_procedures3)];
  924. static const struct rpc_version rpcb_version3 = {
  925. .number = RPCBVERS_3,
  926. .nrprocs = ARRAY_SIZE(rpcb_procedures3),
  927. .procs = rpcb_procedures3,
  928. .counts = rpcb_version3_counts,
  929. };
  930. static unsigned int rpcb_version4_counts[ARRAY_SIZE(rpcb_procedures4)];
  931. static const struct rpc_version rpcb_version4 = {
  932. .number = RPCBVERS_4,
  933. .nrprocs = ARRAY_SIZE(rpcb_procedures4),
  934. .procs = rpcb_procedures4,
  935. .counts = rpcb_version4_counts,
  936. };
  937. static const struct rpc_version *rpcb_version[] = {
  938. NULL,
  939. NULL,
  940. &rpcb_version2,
  941. &rpcb_version3,
  942. &rpcb_version4
  943. };
  944. static struct rpc_stat rpcb_stats;
  945. static const struct rpc_program rpcb_program = {
  946. .name = "rpcbind",
  947. .number = RPCBIND_PROGRAM,
  948. .nrvers = ARRAY_SIZE(rpcb_version),
  949. .version = rpcb_version,
  950. .stats = &rpcb_stats,
  951. };