rpcb_clnt.c 29 KB

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