rpcb_clnt.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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 <net/ipv6.h>
  25. #include <linux/sunrpc/clnt.h>
  26. #include <linux/sunrpc/sched.h>
  27. #include <linux/sunrpc/xprtsock.h>
  28. #include "netns.h"
  29. #ifdef RPC_DEBUG
  30. # define RPCDBG_FACILITY RPCDBG_BIND
  31. #endif
  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 struct rpc_procinfo rpcb_procedures2[];
  111. static struct rpc_procinfo rpcb_procedures3[];
  112. static struct rpc_procinfo rpcb_procedures4[];
  113. struct rpcb_info {
  114. u32 rpc_vers;
  115. 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. {
  175. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  176. /* Protected by rpcb_create_local_mutex */
  177. sn->rpcb_local_clnt = clnt;
  178. sn->rpcb_local_clnt4 = clnt4;
  179. smp_wmb();
  180. sn->rpcb_users = 1;
  181. dprintk("RPC: created new rpcb local clients (rpcb_local_clnt: "
  182. "%p, rpcb_local_clnt4: %p) for net %p%s\n",
  183. sn->rpcb_local_clnt, sn->rpcb_local_clnt4,
  184. net, (net == &init_net) ? " (init_net)" : "");
  185. }
  186. /*
  187. * Returns zero on success, otherwise a negative errno value
  188. * is returned.
  189. */
  190. static int rpcb_create_local_unix(struct net *net)
  191. {
  192. static const struct sockaddr_un rpcb_localaddr_rpcbind = {
  193. .sun_family = AF_LOCAL,
  194. .sun_path = RPCBIND_SOCK_PATHNAME,
  195. };
  196. struct rpc_create_args args = {
  197. .net = net,
  198. .protocol = XPRT_TRANSPORT_LOCAL,
  199. .address = (struct sockaddr *)&rpcb_localaddr_rpcbind,
  200. .addrsize = sizeof(rpcb_localaddr_rpcbind),
  201. .servername = "localhost",
  202. .program = &rpcb_program,
  203. .version = RPCBVERS_2,
  204. .authflavor = RPC_AUTH_NULL,
  205. };
  206. struct rpc_clnt *clnt, *clnt4;
  207. int result = 0;
  208. /*
  209. * Because we requested an RPC PING at transport creation time,
  210. * this works only if the user space portmapper is rpcbind, and
  211. * it's listening on AF_LOCAL on the named socket.
  212. */
  213. clnt = rpc_create(&args);
  214. if (IS_ERR(clnt)) {
  215. dprintk("RPC: failed to create AF_LOCAL rpcbind "
  216. "client (errno %ld).\n", PTR_ERR(clnt));
  217. result = PTR_ERR(clnt);
  218. goto out;
  219. }
  220. clnt4 = rpc_bind_new_program(clnt, &rpcb_program, RPCBVERS_4);
  221. if (IS_ERR(clnt4)) {
  222. dprintk("RPC: failed to bind second program to "
  223. "rpcbind v4 client (errno %ld).\n",
  224. PTR_ERR(clnt4));
  225. clnt4 = NULL;
  226. }
  227. rpcb_set_local(net, clnt, clnt4);
  228. out:
  229. return result;
  230. }
  231. /*
  232. * Returns zero on success, otherwise a negative errno value
  233. * is returned.
  234. */
  235. static int rpcb_create_local_net(struct net *net)
  236. {
  237. static const struct sockaddr_in rpcb_inaddr_loopback = {
  238. .sin_family = AF_INET,
  239. .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
  240. .sin_port = htons(RPCBIND_PORT),
  241. };
  242. struct rpc_create_args args = {
  243. .net = net,
  244. .protocol = XPRT_TRANSPORT_TCP,
  245. .address = (struct sockaddr *)&rpcb_inaddr_loopback,
  246. .addrsize = sizeof(rpcb_inaddr_loopback),
  247. .servername = "localhost",
  248. .program = &rpcb_program,
  249. .version = RPCBVERS_2,
  250. .authflavor = RPC_AUTH_UNIX,
  251. .flags = RPC_CLNT_CREATE_NOPING,
  252. };
  253. struct rpc_clnt *clnt, *clnt4;
  254. int result = 0;
  255. clnt = rpc_create(&args);
  256. if (IS_ERR(clnt)) {
  257. dprintk("RPC: failed to create local rpcbind "
  258. "client (errno %ld).\n", PTR_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. dprintk("RPC: failed to bind second program to "
  270. "rpcbind v4 client (errno %ld).\n",
  271. PTR_ERR(clnt4));
  272. clnt4 = NULL;
  273. }
  274. rpcb_set_local(net, clnt, clnt4);
  275. out:
  276. return result;
  277. }
  278. /*
  279. * Returns zero on success, otherwise a negative errno value
  280. * is returned.
  281. */
  282. int rpcb_create_local(struct net *net)
  283. {
  284. static DEFINE_MUTEX(rpcb_create_local_mutex);
  285. int result = 0;
  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, const 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. * @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. dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
  385. "rpcbind\n", (port ? "" : "un"),
  386. prog, vers, prot, port);
  387. msg.rpc_proc = &rpcb_procedures2[RPCBPROC_UNSET];
  388. if (port)
  389. msg.rpc_proc = &rpcb_procedures2[RPCBPROC_SET];
  390. return rpcb_register_call(sn->rpcb_local_clnt, &msg);
  391. }
  392. /*
  393. * Fill in AF_INET family-specific arguments to register
  394. */
  395. static int rpcb_register_inet4(struct sunrpc_net *sn,
  396. const struct sockaddr *sap,
  397. struct rpc_message *msg)
  398. {
  399. const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
  400. struct rpcbind_args *map = msg->rpc_argp;
  401. unsigned short port = ntohs(sin->sin_port);
  402. int result;
  403. map->r_addr = rpc_sockaddr2uaddr(sap, GFP_KERNEL);
  404. dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
  405. "local rpcbind\n", (port ? "" : "un"),
  406. map->r_prog, map->r_vers,
  407. map->r_addr, map->r_netid);
  408. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  409. if (port)
  410. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
  411. result = rpcb_register_call(sn->rpcb_local_clnt4, msg);
  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. int result;
  426. map->r_addr = rpc_sockaddr2uaddr(sap, GFP_KERNEL);
  427. dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
  428. "local rpcbind\n", (port ? "" : "un"),
  429. map->r_prog, map->r_vers,
  430. map->r_addr, map->r_netid);
  431. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  432. if (port)
  433. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
  434. result = rpcb_register_call(sn->rpcb_local_clnt4, msg);
  435. kfree(map->r_addr);
  436. return result;
  437. }
  438. static int rpcb_unregister_all_protofamilies(struct sunrpc_net *sn,
  439. struct rpc_message *msg)
  440. {
  441. struct rpcbind_args *map = msg->rpc_argp;
  442. dprintk("RPC: unregistering [%u, %u, '%s'] with "
  443. "local rpcbind\n",
  444. map->r_prog, map->r_vers, map->r_netid);
  445. map->r_addr = "";
  446. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  447. return rpcb_register_call(sn->rpcb_local_clnt4, msg);
  448. }
  449. /**
  450. * rpcb_v4_register - set or unset a port registration with the local rpcbind
  451. * @net: target network namespace
  452. * @program: RPC program number of service to (un)register
  453. * @version: RPC version number of service to (un)register
  454. * @address: address family, IP address, and port to (un)register
  455. * @netid: netid of transport protocol to (un)register
  456. *
  457. * Returns zero if the registration request was dispatched successfully
  458. * and the rpcbind daemon returned success. Otherwise, returns an errno
  459. * value that reflects the nature of the error (request could not be
  460. * dispatched, timed out, or rpcbind returned an error).
  461. *
  462. * RPC services invoke this function to advertise their contact
  463. * information via the system's rpcbind daemon. RPC services
  464. * invoke this function once for each [program, version, address,
  465. * netid] tuple they wish to advertise.
  466. *
  467. * Callers may also unregister RPC services that are registered at a
  468. * specific address by setting the port number in @address to zero.
  469. * They may unregister all registered protocol families at once for
  470. * a service by passing a NULL @address argument. If @netid is ""
  471. * then all netids for [program, version, address] are unregistered.
  472. *
  473. * This function uses rpcbind protocol version 4 to contact the
  474. * local rpcbind daemon. The local rpcbind daemon must support
  475. * version 4 of the rpcbind protocol in order for these functions
  476. * to register a service successfully.
  477. *
  478. * Supported netids include "udp" and "tcp" for UDP and TCP over
  479. * IPv4, and "udp6" and "tcp6" for UDP and TCP over IPv6,
  480. * respectively.
  481. *
  482. * The contents of @address determine the address family and the
  483. * port to be registered. The usual practice is to pass INADDR_ANY
  484. * as the raw address, but specifying a non-zero address is also
  485. * supported by this API if the caller wishes to advertise an RPC
  486. * service on a specific network interface.
  487. *
  488. * Note that passing in INADDR_ANY does not create the same service
  489. * registration as IN6ADDR_ANY. The former advertises an RPC
  490. * service on any IPv4 address, but not on IPv6. The latter
  491. * advertises the service on all IPv4 and IPv6 addresses.
  492. */
  493. int rpcb_v4_register(struct net *net, const u32 program, const u32 version,
  494. const struct sockaddr *address, const char *netid)
  495. {
  496. struct rpcbind_args map = {
  497. .r_prog = program,
  498. .r_vers = version,
  499. .r_netid = netid,
  500. .r_owner = RPCB_OWNER_STRING,
  501. };
  502. struct rpc_message msg = {
  503. .rpc_argp = &map,
  504. };
  505. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  506. if (sn->rpcb_local_clnt4 == NULL)
  507. return -EPROTONOSUPPORT;
  508. if (address == NULL)
  509. return rpcb_unregister_all_protofamilies(sn, &msg);
  510. switch (address->sa_family) {
  511. case AF_INET:
  512. return rpcb_register_inet4(sn, address, &msg);
  513. case AF_INET6:
  514. return rpcb_register_inet6(sn, address, &msg);
  515. }
  516. return -EAFNOSUPPORT;
  517. }
  518. static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt, struct rpcbind_args *map, struct rpc_procinfo *proc)
  519. {
  520. struct rpc_message msg = {
  521. .rpc_proc = proc,
  522. .rpc_argp = map,
  523. .rpc_resp = map,
  524. };
  525. struct rpc_task_setup task_setup_data = {
  526. .rpc_client = rpcb_clnt,
  527. .rpc_message = &msg,
  528. .callback_ops = &rpcb_getport_ops,
  529. .callback_data = map,
  530. .flags = RPC_TASK_ASYNC | RPC_TASK_SOFTCONN,
  531. };
  532. return rpc_run_task(&task_setup_data);
  533. }
  534. /*
  535. * In the case where rpc clients have been cloned, we want to make
  536. * sure that we use the program number/version etc of the actual
  537. * owner of the xprt. To do so, we walk back up the tree of parents
  538. * to find whoever created the transport and/or whoever has the
  539. * autobind flag set.
  540. */
  541. static struct rpc_clnt *rpcb_find_transport_owner(struct rpc_clnt *clnt)
  542. {
  543. struct rpc_clnt *parent = clnt->cl_parent;
  544. struct rpc_xprt *xprt = rcu_dereference(clnt->cl_xprt);
  545. while (parent != clnt) {
  546. if (rcu_dereference(parent->cl_xprt) != xprt)
  547. break;
  548. if (clnt->cl_autobind)
  549. break;
  550. clnt = parent;
  551. parent = parent->cl_parent;
  552. }
  553. return clnt;
  554. }
  555. /**
  556. * rpcb_getport_async - obtain the port for a given RPC service on a given host
  557. * @task: task that is waiting for portmapper request
  558. *
  559. * This one can be called for an ongoing RPC request, and can be used in
  560. * an async (rpciod) context.
  561. */
  562. void rpcb_getport_async(struct rpc_task *task)
  563. {
  564. struct rpc_clnt *clnt;
  565. struct rpc_procinfo *proc;
  566. u32 bind_version;
  567. struct rpc_xprt *xprt;
  568. struct rpc_clnt *rpcb_clnt;
  569. struct rpcbind_args *map;
  570. struct rpc_task *child;
  571. struct sockaddr_storage addr;
  572. struct sockaddr *sap = (struct sockaddr *)&addr;
  573. size_t salen;
  574. int status;
  575. rcu_read_lock();
  576. do {
  577. clnt = rpcb_find_transport_owner(task->tk_client);
  578. xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
  579. } while (xprt == NULL);
  580. rcu_read_unlock();
  581. dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
  582. task->tk_pid, __func__,
  583. xprt->servername, clnt->cl_prog, clnt->cl_vers, xprt->prot);
  584. /* Put self on the wait queue to ensure we get notified if
  585. * some other task is already attempting to bind the port */
  586. rpc_sleep_on(&xprt->binding, task, NULL);
  587. if (xprt_test_and_set_binding(xprt)) {
  588. dprintk("RPC: %5u %s: waiting for another binder\n",
  589. task->tk_pid, __func__);
  590. xprt_put(xprt);
  591. return;
  592. }
  593. /* Someone else may have bound if we slept */
  594. if (xprt_bound(xprt)) {
  595. status = 0;
  596. dprintk("RPC: %5u %s: already bound\n",
  597. task->tk_pid, __func__);
  598. goto bailout_nofree;
  599. }
  600. /* Parent transport's destination address */
  601. salen = rpc_peeraddr(clnt, sap, sizeof(addr));
  602. /* Don't ever use rpcbind v2 for AF_INET6 requests */
  603. switch (sap->sa_family) {
  604. case AF_INET:
  605. proc = rpcb_next_version[xprt->bind_index].rpc_proc;
  606. bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
  607. break;
  608. case AF_INET6:
  609. proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
  610. bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
  611. break;
  612. default:
  613. status = -EAFNOSUPPORT;
  614. dprintk("RPC: %5u %s: bad address family\n",
  615. task->tk_pid, __func__);
  616. goto bailout_nofree;
  617. }
  618. if (proc == NULL) {
  619. xprt->bind_index = 0;
  620. status = -EPFNOSUPPORT;
  621. dprintk("RPC: %5u %s: no more getport versions available\n",
  622. task->tk_pid, __func__);
  623. goto bailout_nofree;
  624. }
  625. dprintk("RPC: %5u %s: trying rpcbind version %u\n",
  626. task->tk_pid, __func__, bind_version);
  627. rpcb_clnt = rpcb_create(xprt->xprt_net, xprt->servername, sap, salen,
  628. xprt->prot, bind_version);
  629. if (IS_ERR(rpcb_clnt)) {
  630. status = PTR_ERR(rpcb_clnt);
  631. dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
  632. task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
  633. goto bailout_nofree;
  634. }
  635. map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
  636. if (!map) {
  637. status = -ENOMEM;
  638. dprintk("RPC: %5u %s: no memory available\n",
  639. task->tk_pid, __func__);
  640. goto bailout_release_client;
  641. }
  642. map->r_prog = clnt->cl_prog;
  643. map->r_vers = clnt->cl_vers;
  644. map->r_prot = xprt->prot;
  645. map->r_port = 0;
  646. map->r_xprt = xprt;
  647. map->r_status = -EIO;
  648. switch (bind_version) {
  649. case RPCBVERS_4:
  650. case RPCBVERS_3:
  651. map->r_netid = xprt->address_strings[RPC_DISPLAY_NETID];
  652. map->r_addr = rpc_sockaddr2uaddr(sap, GFP_ATOMIC);
  653. map->r_owner = "";
  654. break;
  655. case RPCBVERS_2:
  656. map->r_addr = NULL;
  657. break;
  658. default:
  659. BUG();
  660. }
  661. child = rpcb_call_async(rpcb_clnt, map, proc);
  662. rpc_release_client(rpcb_clnt);
  663. if (IS_ERR(child)) {
  664. /* rpcb_map_release() has freed the arguments */
  665. dprintk("RPC: %5u %s: rpc_run_task failed\n",
  666. task->tk_pid, __func__);
  667. return;
  668. }
  669. xprt->stat.bind_count++;
  670. rpc_put_task(child);
  671. return;
  672. bailout_release_client:
  673. rpc_release_client(rpcb_clnt);
  674. bailout_nofree:
  675. rpcb_wake_rpcbind_waiters(xprt, status);
  676. task->tk_status = status;
  677. xprt_put(xprt);
  678. }
  679. EXPORT_SYMBOL_GPL(rpcb_getport_async);
  680. /*
  681. * Rpcbind child task calls this callback via tk_exit.
  682. */
  683. static void rpcb_getport_done(struct rpc_task *child, void *data)
  684. {
  685. struct rpcbind_args *map = data;
  686. struct rpc_xprt *xprt = map->r_xprt;
  687. int status = child->tk_status;
  688. /* Garbage reply: retry with a lesser rpcbind version */
  689. if (status == -EIO)
  690. status = -EPROTONOSUPPORT;
  691. /* rpcbind server doesn't support this rpcbind protocol version */
  692. if (status == -EPROTONOSUPPORT)
  693. xprt->bind_index++;
  694. if (status < 0) {
  695. /* rpcbind server not available on remote host? */
  696. xprt->ops->set_port(xprt, 0);
  697. } else if (map->r_port == 0) {
  698. /* Requested RPC service wasn't registered on remote host */
  699. xprt->ops->set_port(xprt, 0);
  700. status = -EACCES;
  701. } else {
  702. /* Succeeded */
  703. xprt->ops->set_port(xprt, map->r_port);
  704. xprt_set_bound(xprt);
  705. status = 0;
  706. }
  707. dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
  708. child->tk_pid, status, map->r_port);
  709. map->r_status = status;
  710. }
  711. /*
  712. * XDR functions for rpcbind
  713. */
  714. static void rpcb_enc_mapping(struct rpc_rqst *req, struct xdr_stream *xdr,
  715. const struct rpcbind_args *rpcb)
  716. {
  717. __be32 *p;
  718. dprintk("RPC: %5u encoding PMAP_%s call (%u, %u, %d, %u)\n",
  719. req->rq_task->tk_pid,
  720. req->rq_task->tk_msg.rpc_proc->p_name,
  721. rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
  722. p = xdr_reserve_space(xdr, RPCB_mappingargs_sz << 2);
  723. *p++ = cpu_to_be32(rpcb->r_prog);
  724. *p++ = cpu_to_be32(rpcb->r_vers);
  725. *p++ = cpu_to_be32(rpcb->r_prot);
  726. *p = cpu_to_be32(rpcb->r_port);
  727. }
  728. static int rpcb_dec_getport(struct rpc_rqst *req, struct xdr_stream *xdr,
  729. struct rpcbind_args *rpcb)
  730. {
  731. unsigned long port;
  732. __be32 *p;
  733. rpcb->r_port = 0;
  734. p = xdr_inline_decode(xdr, 4);
  735. if (unlikely(p == NULL))
  736. return -EIO;
  737. port = be32_to_cpup(p);
  738. dprintk("RPC: %5u PMAP_%s result: %lu\n", req->rq_task->tk_pid,
  739. req->rq_task->tk_msg.rpc_proc->p_name, port);
  740. if (unlikely(port > USHRT_MAX))
  741. return -EIO;
  742. rpcb->r_port = port;
  743. return 0;
  744. }
  745. static int rpcb_dec_set(struct rpc_rqst *req, struct xdr_stream *xdr,
  746. unsigned int *boolp)
  747. {
  748. __be32 *p;
  749. p = xdr_inline_decode(xdr, 4);
  750. if (unlikely(p == NULL))
  751. return -EIO;
  752. *boolp = 0;
  753. if (*p != xdr_zero)
  754. *boolp = 1;
  755. dprintk("RPC: %5u RPCB_%s call %s\n",
  756. req->rq_task->tk_pid,
  757. req->rq_task->tk_msg.rpc_proc->p_name,
  758. (*boolp ? "succeeded" : "failed"));
  759. return 0;
  760. }
  761. static void encode_rpcb_string(struct xdr_stream *xdr, const char *string,
  762. const u32 maxstrlen)
  763. {
  764. __be32 *p;
  765. u32 len;
  766. len = strlen(string);
  767. WARN_ON_ONCE(len > maxstrlen);
  768. if (len > maxstrlen)
  769. /* truncate and hope for the best */
  770. len = maxstrlen;
  771. p = xdr_reserve_space(xdr, 4 + len);
  772. xdr_encode_opaque(p, string, len);
  773. }
  774. static void rpcb_enc_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr,
  775. const struct rpcbind_args *rpcb)
  776. {
  777. __be32 *p;
  778. dprintk("RPC: %5u encoding RPCB_%s call (%u, %u, '%s', '%s')\n",
  779. req->rq_task->tk_pid,
  780. req->rq_task->tk_msg.rpc_proc->p_name,
  781. rpcb->r_prog, rpcb->r_vers,
  782. rpcb->r_netid, rpcb->r_addr);
  783. p = xdr_reserve_space(xdr, (RPCB_program_sz + RPCB_version_sz) << 2);
  784. *p++ = cpu_to_be32(rpcb->r_prog);
  785. *p = cpu_to_be32(rpcb->r_vers);
  786. encode_rpcb_string(xdr, rpcb->r_netid, RPCBIND_MAXNETIDLEN);
  787. encode_rpcb_string(xdr, rpcb->r_addr, RPCBIND_MAXUADDRLEN);
  788. encode_rpcb_string(xdr, rpcb->r_owner, RPCB_MAXOWNERLEN);
  789. }
  790. static int rpcb_dec_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr,
  791. struct rpcbind_args *rpcb)
  792. {
  793. struct sockaddr_storage address;
  794. struct sockaddr *sap = (struct sockaddr *)&address;
  795. __be32 *p;
  796. u32 len;
  797. rpcb->r_port = 0;
  798. p = xdr_inline_decode(xdr, 4);
  799. if (unlikely(p == NULL))
  800. goto out_fail;
  801. len = be32_to_cpup(p);
  802. /*
  803. * If the returned universal address is a null string,
  804. * the requested RPC service was not registered.
  805. */
  806. if (len == 0) {
  807. dprintk("RPC: %5u RPCB reply: program not registered\n",
  808. req->rq_task->tk_pid);
  809. return 0;
  810. }
  811. if (unlikely(len > RPCBIND_MAXUADDRLEN))
  812. goto out_fail;
  813. p = xdr_inline_decode(xdr, len);
  814. if (unlikely(p == NULL))
  815. goto out_fail;
  816. dprintk("RPC: %5u RPCB_%s reply: %s\n", req->rq_task->tk_pid,
  817. req->rq_task->tk_msg.rpc_proc->p_name, (char *)p);
  818. if (rpc_uaddr2sockaddr(req->rq_xprt->xprt_net, (char *)p, len,
  819. sap, sizeof(address)) == 0)
  820. goto out_fail;
  821. rpcb->r_port = rpc_get_port(sap);
  822. return 0;
  823. out_fail:
  824. dprintk("RPC: %5u malformed RPCB_%s reply\n",
  825. req->rq_task->tk_pid,
  826. req->rq_task->tk_msg.rpc_proc->p_name);
  827. return -EIO;
  828. }
  829. /*
  830. * Not all rpcbind procedures described in RFC 1833 are implemented
  831. * since the Linux kernel RPC code requires only these.
  832. */
  833. static struct rpc_procinfo rpcb_procedures2[] = {
  834. [RPCBPROC_SET] = {
  835. .p_proc = RPCBPROC_SET,
  836. .p_encode = (kxdreproc_t)rpcb_enc_mapping,
  837. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  838. .p_arglen = RPCB_mappingargs_sz,
  839. .p_replen = RPCB_setres_sz,
  840. .p_statidx = RPCBPROC_SET,
  841. .p_timer = 0,
  842. .p_name = "SET",
  843. },
  844. [RPCBPROC_UNSET] = {
  845. .p_proc = RPCBPROC_UNSET,
  846. .p_encode = (kxdreproc_t)rpcb_enc_mapping,
  847. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  848. .p_arglen = RPCB_mappingargs_sz,
  849. .p_replen = RPCB_setres_sz,
  850. .p_statidx = RPCBPROC_UNSET,
  851. .p_timer = 0,
  852. .p_name = "UNSET",
  853. },
  854. [RPCBPROC_GETPORT] = {
  855. .p_proc = RPCBPROC_GETPORT,
  856. .p_encode = (kxdreproc_t)rpcb_enc_mapping,
  857. .p_decode = (kxdrdproc_t)rpcb_dec_getport,
  858. .p_arglen = RPCB_mappingargs_sz,
  859. .p_replen = RPCB_getportres_sz,
  860. .p_statidx = RPCBPROC_GETPORT,
  861. .p_timer = 0,
  862. .p_name = "GETPORT",
  863. },
  864. };
  865. static struct rpc_procinfo rpcb_procedures3[] = {
  866. [RPCBPROC_SET] = {
  867. .p_proc = RPCBPROC_SET,
  868. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  869. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  870. .p_arglen = RPCB_getaddrargs_sz,
  871. .p_replen = RPCB_setres_sz,
  872. .p_statidx = RPCBPROC_SET,
  873. .p_timer = 0,
  874. .p_name = "SET",
  875. },
  876. [RPCBPROC_UNSET] = {
  877. .p_proc = RPCBPROC_UNSET,
  878. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  879. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  880. .p_arglen = RPCB_getaddrargs_sz,
  881. .p_replen = RPCB_setres_sz,
  882. .p_statidx = RPCBPROC_UNSET,
  883. .p_timer = 0,
  884. .p_name = "UNSET",
  885. },
  886. [RPCBPROC_GETADDR] = {
  887. .p_proc = RPCBPROC_GETADDR,
  888. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  889. .p_decode = (kxdrdproc_t)rpcb_dec_getaddr,
  890. .p_arglen = RPCB_getaddrargs_sz,
  891. .p_replen = RPCB_getaddrres_sz,
  892. .p_statidx = RPCBPROC_GETADDR,
  893. .p_timer = 0,
  894. .p_name = "GETADDR",
  895. },
  896. };
  897. static struct rpc_procinfo rpcb_procedures4[] = {
  898. [RPCBPROC_SET] = {
  899. .p_proc = RPCBPROC_SET,
  900. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  901. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  902. .p_arglen = RPCB_getaddrargs_sz,
  903. .p_replen = RPCB_setres_sz,
  904. .p_statidx = RPCBPROC_SET,
  905. .p_timer = 0,
  906. .p_name = "SET",
  907. },
  908. [RPCBPROC_UNSET] = {
  909. .p_proc = RPCBPROC_UNSET,
  910. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  911. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  912. .p_arglen = RPCB_getaddrargs_sz,
  913. .p_replen = RPCB_setres_sz,
  914. .p_statidx = RPCBPROC_UNSET,
  915. .p_timer = 0,
  916. .p_name = "UNSET",
  917. },
  918. [RPCBPROC_GETADDR] = {
  919. .p_proc = RPCBPROC_GETADDR,
  920. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  921. .p_decode = (kxdrdproc_t)rpcb_dec_getaddr,
  922. .p_arglen = RPCB_getaddrargs_sz,
  923. .p_replen = RPCB_getaddrres_sz,
  924. .p_statidx = RPCBPROC_GETADDR,
  925. .p_timer = 0,
  926. .p_name = "GETADDR",
  927. },
  928. };
  929. static const struct rpcb_info rpcb_next_version[] = {
  930. {
  931. .rpc_vers = RPCBVERS_2,
  932. .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
  933. },
  934. {
  935. .rpc_proc = NULL,
  936. },
  937. };
  938. static const struct rpcb_info rpcb_next_version6[] = {
  939. {
  940. .rpc_vers = RPCBVERS_4,
  941. .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR],
  942. },
  943. {
  944. .rpc_vers = RPCBVERS_3,
  945. .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR],
  946. },
  947. {
  948. .rpc_proc = NULL,
  949. },
  950. };
  951. static const struct rpc_version rpcb_version2 = {
  952. .number = RPCBVERS_2,
  953. .nrprocs = ARRAY_SIZE(rpcb_procedures2),
  954. .procs = rpcb_procedures2
  955. };
  956. static const struct rpc_version rpcb_version3 = {
  957. .number = RPCBVERS_3,
  958. .nrprocs = ARRAY_SIZE(rpcb_procedures3),
  959. .procs = rpcb_procedures3
  960. };
  961. static const struct rpc_version rpcb_version4 = {
  962. .number = RPCBVERS_4,
  963. .nrprocs = ARRAY_SIZE(rpcb_procedures4),
  964. .procs = rpcb_procedures4
  965. };
  966. static const struct rpc_version *rpcb_version[] = {
  967. NULL,
  968. NULL,
  969. &rpcb_version2,
  970. &rpcb_version3,
  971. &rpcb_version4
  972. };
  973. static struct rpc_stat rpcb_stats;
  974. static const struct rpc_program rpcb_program = {
  975. .name = "rpcbind",
  976. .number = RPCBIND_PROGRAM,
  977. .nrvers = ARRAY_SIZE(rpcb_version),
  978. .version = rpcb_version,
  979. .stats = &rpcb_stats,
  980. };