rpcb_clnt.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  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 const 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 const struct rpcb_info rpcb_next_version[];
  119. static const 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(struct net *net)
  149. {
  150. struct sunrpc_net *sn = net_generic(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 = 0;
  154. spin_lock(&sn->rpcb_clnt_lock);
  155. if (sn->rpcb_users) {
  156. if (--sn->rpcb_users == 0) {
  157. sn->rpcb_local_clnt = NULL;
  158. sn->rpcb_local_clnt4 = NULL;
  159. }
  160. shutdown = !sn->rpcb_users;
  161. }
  162. spin_unlock(&sn->rpcb_clnt_lock);
  163. if (shutdown) {
  164. /*
  165. * cleanup_rpcb_clnt - remove xprtsock's sysctls, unregister
  166. */
  167. if (clnt4)
  168. rpc_shutdown_client(clnt4);
  169. if (clnt)
  170. rpc_shutdown_client(clnt);
  171. }
  172. }
  173. static void rpcb_set_local(struct net *net, struct rpc_clnt *clnt,
  174. struct rpc_clnt *clnt4)
  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. smp_wmb();
  181. sn->rpcb_users = 1;
  182. dprintk("RPC: created new rpcb local clients (rpcb_local_clnt: "
  183. "%p, rpcb_local_clnt4: %p) for net %p%s\n",
  184. sn->rpcb_local_clnt, sn->rpcb_local_clnt4,
  185. net, (net == &init_net) ? " (init_net)" : "");
  186. }
  187. /*
  188. * Returns zero on success, otherwise a negative errno value
  189. * is returned.
  190. */
  191. static int rpcb_create_local_unix(struct net *net)
  192. {
  193. static const struct sockaddr_un rpcb_localaddr_rpcbind = {
  194. .sun_family = AF_LOCAL,
  195. .sun_path = RPCBIND_SOCK_PATHNAME,
  196. };
  197. struct rpc_create_args args = {
  198. .net = net,
  199. .protocol = XPRT_TRANSPORT_LOCAL,
  200. .address = (struct sockaddr *)&rpcb_localaddr_rpcbind,
  201. .addrsize = sizeof(rpcb_localaddr_rpcbind),
  202. .servername = "localhost",
  203. .program = &rpcb_program,
  204. .version = RPCBVERS_2,
  205. .authflavor = RPC_AUTH_NULL,
  206. };
  207. struct rpc_clnt *clnt, *clnt4;
  208. int result = 0;
  209. /*
  210. * Because we requested an RPC PING at transport creation time,
  211. * this works only if the user space portmapper is rpcbind, and
  212. * it's listening on AF_LOCAL on the named socket.
  213. */
  214. clnt = rpc_create(&args);
  215. if (IS_ERR(clnt)) {
  216. dprintk("RPC: failed to create AF_LOCAL rpcbind "
  217. "client (errno %ld).\n", PTR_ERR(clnt));
  218. result = PTR_ERR(clnt);
  219. goto out;
  220. }
  221. clnt4 = rpc_bind_new_program(clnt, &rpcb_program, RPCBVERS_4);
  222. if (IS_ERR(clnt4)) {
  223. dprintk("RPC: failed to bind second program to "
  224. "rpcbind v4 client (errno %ld).\n",
  225. PTR_ERR(clnt4));
  226. clnt4 = NULL;
  227. }
  228. rpcb_set_local(net, clnt, clnt4);
  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. .flags = RPC_CLNT_CREATE_NOPING,
  253. };
  254. struct rpc_clnt *clnt, *clnt4;
  255. int result = 0;
  256. clnt = rpc_create(&args);
  257. if (IS_ERR(clnt)) {
  258. dprintk("RPC: failed to create local rpcbind "
  259. "client (errno %ld).\n", PTR_ERR(clnt));
  260. result = PTR_ERR(clnt);
  261. goto out;
  262. }
  263. /*
  264. * This results in an RPC ping. On systems running portmapper,
  265. * the v4 ping will fail. Proceed anyway, but disallow rpcb
  266. * v4 upcalls.
  267. */
  268. clnt4 = rpc_bind_new_program(clnt, &rpcb_program, RPCBVERS_4);
  269. if (IS_ERR(clnt4)) {
  270. dprintk("RPC: failed to bind second program to "
  271. "rpcbind v4 client (errno %ld).\n",
  272. PTR_ERR(clnt4));
  273. clnt4 = NULL;
  274. }
  275. rpcb_set_local(net, clnt, clnt4);
  276. out:
  277. return result;
  278. }
  279. /*
  280. * Returns zero on success, otherwise a negative errno value
  281. * is returned.
  282. */
  283. int rpcb_create_local(struct net *net)
  284. {
  285. static DEFINE_MUTEX(rpcb_create_local_mutex);
  286. int result = 0;
  287. if (rpcb_get_local(net))
  288. return result;
  289. mutex_lock(&rpcb_create_local_mutex);
  290. if (rpcb_get_local(net))
  291. goto out;
  292. if (rpcb_create_local_unix(net) != 0)
  293. result = rpcb_create_local_net(net);
  294. out:
  295. mutex_unlock(&rpcb_create_local_mutex);
  296. return result;
  297. }
  298. static struct rpc_clnt *rpcb_create(struct net *net, const char *hostname,
  299. struct sockaddr *srvaddr, size_t salen,
  300. int proto, u32 version)
  301. {
  302. struct rpc_create_args args = {
  303. .net = net,
  304. .protocol = proto,
  305. .address = srvaddr,
  306. .addrsize = salen,
  307. .servername = hostname,
  308. .program = &rpcb_program,
  309. .version = version,
  310. .authflavor = RPC_AUTH_UNIX,
  311. .flags = (RPC_CLNT_CREATE_NOPING |
  312. RPC_CLNT_CREATE_NONPRIVPORT),
  313. };
  314. switch (srvaddr->sa_family) {
  315. case AF_INET:
  316. ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
  317. break;
  318. case AF_INET6:
  319. ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
  320. break;
  321. default:
  322. return ERR_PTR(-EAFNOSUPPORT);
  323. }
  324. return rpc_create(&args);
  325. }
  326. static int rpcb_register_call(struct rpc_clnt *clnt, struct rpc_message *msg)
  327. {
  328. int result, error = 0;
  329. msg->rpc_resp = &result;
  330. error = rpc_call_sync(clnt, msg, RPC_TASK_SOFTCONN);
  331. if (error < 0) {
  332. dprintk("RPC: failed to contact local rpcbind "
  333. "server (errno %d).\n", -error);
  334. return error;
  335. }
  336. if (!result)
  337. return -EACCES;
  338. return 0;
  339. }
  340. /**
  341. * rpcb_register - set or unset a port registration with the local rpcbind svc
  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. * @program: RPC program number of service to (un)register
  452. * @version: RPC version number of service to (un)register
  453. * @address: address family, IP address, and port to (un)register
  454. * @netid: netid of transport protocol to (un)register
  455. *
  456. * Returns zero if the registration request was dispatched successfully
  457. * and the rpcbind daemon returned success. Otherwise, returns an errno
  458. * value that reflects the nature of the error (request could not be
  459. * dispatched, timed out, or rpcbind returned an error).
  460. *
  461. * RPC services invoke this function to advertise their contact
  462. * information via the system's rpcbind daemon. RPC services
  463. * invoke this function once for each [program, version, address,
  464. * netid] tuple they wish to advertise.
  465. *
  466. * Callers may also unregister RPC services that are registered at a
  467. * specific address by setting the port number in @address to zero.
  468. * They may unregister all registered protocol families at once for
  469. * a service by passing a NULL @address argument. If @netid is ""
  470. * then all netids for [program, version, address] are unregistered.
  471. *
  472. * This function uses rpcbind protocol version 4 to contact the
  473. * local rpcbind daemon. The local rpcbind daemon must support
  474. * version 4 of the rpcbind protocol in order for these functions
  475. * to register a service successfully.
  476. *
  477. * Supported netids include "udp" and "tcp" for UDP and TCP over
  478. * IPv4, and "udp6" and "tcp6" for UDP and TCP over IPv6,
  479. * respectively.
  480. *
  481. * The contents of @address determine the address family and the
  482. * port to be registered. The usual practice is to pass INADDR_ANY
  483. * as the raw address, but specifying a non-zero address is also
  484. * supported by this API if the caller wishes to advertise an RPC
  485. * service on a specific network interface.
  486. *
  487. * Note that passing in INADDR_ANY does not create the same service
  488. * registration as IN6ADDR_ANY. The former advertises an RPC
  489. * service on any IPv4 address, but not on IPv6. The latter
  490. * advertises the service on all IPv4 and IPv6 addresses.
  491. */
  492. int rpcb_v4_register(struct net *net, const u32 program, const u32 version,
  493. const struct sockaddr *address, const char *netid)
  494. {
  495. struct rpcbind_args map = {
  496. .r_prog = program,
  497. .r_vers = version,
  498. .r_netid = netid,
  499. .r_owner = RPCB_OWNER_STRING,
  500. };
  501. struct rpc_message msg = {
  502. .rpc_argp = &map,
  503. };
  504. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  505. if (sn->rpcb_local_clnt4 == NULL)
  506. return -EPROTONOSUPPORT;
  507. if (address == NULL)
  508. return rpcb_unregister_all_protofamilies(sn, &msg);
  509. switch (address->sa_family) {
  510. case AF_INET:
  511. return rpcb_register_inet4(sn, address, &msg);
  512. case AF_INET6:
  513. return rpcb_register_inet6(sn, address, &msg);
  514. }
  515. return -EAFNOSUPPORT;
  516. }
  517. static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt, struct rpcbind_args *map, 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 *xprt = rcu_dereference(clnt->cl_xprt);
  544. while (parent != clnt) {
  545. if (rcu_dereference(parent->cl_xprt) != xprt)
  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. 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. do {
  576. clnt = rpcb_find_transport_owner(task->tk_client);
  577. xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
  578. } while (xprt == NULL);
  579. rcu_read_unlock();
  580. dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
  581. task->tk_pid, __func__,
  582. xprt->servername, clnt->cl_prog, clnt->cl_vers, xprt->prot);
  583. /* Put self on the wait queue to ensure we get notified if
  584. * some other task is already attempting to bind the port */
  585. rpc_sleep_on(&xprt->binding, task, NULL);
  586. if (xprt_test_and_set_binding(xprt)) {
  587. dprintk("RPC: %5u %s: waiting for another binder\n",
  588. task->tk_pid, __func__);
  589. xprt_put(xprt);
  590. return;
  591. }
  592. /* Someone else may have bound if we slept */
  593. if (xprt_bound(xprt)) {
  594. status = 0;
  595. dprintk("RPC: %5u %s: already bound\n",
  596. task->tk_pid, __func__);
  597. goto bailout_nofree;
  598. }
  599. /* Parent transport's destination address */
  600. salen = rpc_peeraddr(clnt, sap, sizeof(addr));
  601. /* Don't ever use rpcbind v2 for AF_INET6 requests */
  602. switch (sap->sa_family) {
  603. case AF_INET:
  604. proc = rpcb_next_version[xprt->bind_index].rpc_proc;
  605. bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
  606. break;
  607. case AF_INET6:
  608. proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
  609. bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
  610. break;
  611. default:
  612. status = -EAFNOSUPPORT;
  613. dprintk("RPC: %5u %s: bad address family\n",
  614. task->tk_pid, __func__);
  615. goto bailout_nofree;
  616. }
  617. if (proc == NULL) {
  618. xprt->bind_index = 0;
  619. status = -EPFNOSUPPORT;
  620. dprintk("RPC: %5u %s: no more getport versions available\n",
  621. task->tk_pid, __func__);
  622. goto bailout_nofree;
  623. }
  624. dprintk("RPC: %5u %s: trying rpcbind version %u\n",
  625. task->tk_pid, __func__, bind_version);
  626. rpcb_clnt = rpcb_create(xprt->xprt_net, xprt->servername, sap, salen,
  627. xprt->prot, bind_version);
  628. if (IS_ERR(rpcb_clnt)) {
  629. status = PTR_ERR(rpcb_clnt);
  630. dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
  631. task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
  632. goto bailout_nofree;
  633. }
  634. map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
  635. if (!map) {
  636. status = -ENOMEM;
  637. dprintk("RPC: %5u %s: no memory available\n",
  638. task->tk_pid, __func__);
  639. goto bailout_release_client;
  640. }
  641. map->r_prog = clnt->cl_prog;
  642. map->r_vers = clnt->cl_vers;
  643. map->r_prot = xprt->prot;
  644. map->r_port = 0;
  645. map->r_xprt = xprt;
  646. map->r_status = -EIO;
  647. switch (bind_version) {
  648. case RPCBVERS_4:
  649. case RPCBVERS_3:
  650. map->r_netid = xprt->address_strings[RPC_DISPLAY_NETID];
  651. map->r_addr = rpc_sockaddr2uaddr(sap, GFP_ATOMIC);
  652. map->r_owner = "";
  653. break;
  654. case RPCBVERS_2:
  655. map->r_addr = NULL;
  656. break;
  657. default:
  658. BUG();
  659. }
  660. child = rpcb_call_async(rpcb_clnt, map, proc);
  661. rpc_release_client(rpcb_clnt);
  662. if (IS_ERR(child)) {
  663. /* rpcb_map_release() has freed the arguments */
  664. dprintk("RPC: %5u %s: rpc_run_task failed\n",
  665. task->tk_pid, __func__);
  666. return;
  667. }
  668. xprt->stat.bind_count++;
  669. rpc_put_task(child);
  670. return;
  671. bailout_release_client:
  672. rpc_release_client(rpcb_clnt);
  673. bailout_nofree:
  674. rpcb_wake_rpcbind_waiters(xprt, status);
  675. task->tk_status = status;
  676. xprt_put(xprt);
  677. }
  678. EXPORT_SYMBOL_GPL(rpcb_getport_async);
  679. /*
  680. * Rpcbind child task calls this callback via tk_exit.
  681. */
  682. static void rpcb_getport_done(struct rpc_task *child, void *data)
  683. {
  684. struct rpcbind_args *map = data;
  685. struct rpc_xprt *xprt = map->r_xprt;
  686. int status = child->tk_status;
  687. /* Garbage reply: retry with a lesser rpcbind version */
  688. if (status == -EIO)
  689. status = -EPROTONOSUPPORT;
  690. /* rpcbind server doesn't support this rpcbind protocol version */
  691. if (status == -EPROTONOSUPPORT)
  692. xprt->bind_index++;
  693. if (status < 0) {
  694. /* rpcbind server not available on remote host? */
  695. xprt->ops->set_port(xprt, 0);
  696. } else if (map->r_port == 0) {
  697. /* Requested RPC service wasn't registered on remote host */
  698. xprt->ops->set_port(xprt, 0);
  699. status = -EACCES;
  700. } else {
  701. /* Succeeded */
  702. xprt->ops->set_port(xprt, map->r_port);
  703. xprt_set_bound(xprt);
  704. status = 0;
  705. }
  706. dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
  707. child->tk_pid, status, map->r_port);
  708. map->r_status = status;
  709. }
  710. /*
  711. * XDR functions for rpcbind
  712. */
  713. static void rpcb_enc_mapping(struct rpc_rqst *req, struct xdr_stream *xdr,
  714. const struct rpcbind_args *rpcb)
  715. {
  716. __be32 *p;
  717. dprintk("RPC: %5u encoding PMAP_%s call (%u, %u, %d, %u)\n",
  718. req->rq_task->tk_pid,
  719. req->rq_task->tk_msg.rpc_proc->p_name,
  720. rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
  721. p = xdr_reserve_space(xdr, RPCB_mappingargs_sz << 2);
  722. *p++ = cpu_to_be32(rpcb->r_prog);
  723. *p++ = cpu_to_be32(rpcb->r_vers);
  724. *p++ = cpu_to_be32(rpcb->r_prot);
  725. *p = cpu_to_be32(rpcb->r_port);
  726. }
  727. static int rpcb_dec_getport(struct rpc_rqst *req, struct xdr_stream *xdr,
  728. struct rpcbind_args *rpcb)
  729. {
  730. unsigned long port;
  731. __be32 *p;
  732. rpcb->r_port = 0;
  733. p = xdr_inline_decode(xdr, 4);
  734. if (unlikely(p == NULL))
  735. return -EIO;
  736. port = be32_to_cpup(p);
  737. dprintk("RPC: %5u PMAP_%s result: %lu\n", req->rq_task->tk_pid,
  738. req->rq_task->tk_msg.rpc_proc->p_name, port);
  739. if (unlikely(port > USHRT_MAX))
  740. return -EIO;
  741. rpcb->r_port = port;
  742. return 0;
  743. }
  744. static int rpcb_dec_set(struct rpc_rqst *req, struct xdr_stream *xdr,
  745. unsigned int *boolp)
  746. {
  747. __be32 *p;
  748. p = xdr_inline_decode(xdr, 4);
  749. if (unlikely(p == NULL))
  750. return -EIO;
  751. *boolp = 0;
  752. if (*p != xdr_zero)
  753. *boolp = 1;
  754. dprintk("RPC: %5u RPCB_%s call %s\n",
  755. req->rq_task->tk_pid,
  756. req->rq_task->tk_msg.rpc_proc->p_name,
  757. (*boolp ? "succeeded" : "failed"));
  758. return 0;
  759. }
  760. static void encode_rpcb_string(struct xdr_stream *xdr, const char *string,
  761. const u32 maxstrlen)
  762. {
  763. __be32 *p;
  764. u32 len;
  765. len = strlen(string);
  766. BUG_ON(len > maxstrlen);
  767. p = xdr_reserve_space(xdr, 4 + len);
  768. xdr_encode_opaque(p, string, len);
  769. }
  770. static void rpcb_enc_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr,
  771. const struct rpcbind_args *rpcb)
  772. {
  773. __be32 *p;
  774. dprintk("RPC: %5u encoding RPCB_%s call (%u, %u, '%s', '%s')\n",
  775. req->rq_task->tk_pid,
  776. req->rq_task->tk_msg.rpc_proc->p_name,
  777. rpcb->r_prog, rpcb->r_vers,
  778. rpcb->r_netid, rpcb->r_addr);
  779. p = xdr_reserve_space(xdr, (RPCB_program_sz + RPCB_version_sz) << 2);
  780. *p++ = cpu_to_be32(rpcb->r_prog);
  781. *p = cpu_to_be32(rpcb->r_vers);
  782. encode_rpcb_string(xdr, rpcb->r_netid, RPCBIND_MAXNETIDLEN);
  783. encode_rpcb_string(xdr, rpcb->r_addr, RPCBIND_MAXUADDRLEN);
  784. encode_rpcb_string(xdr, rpcb->r_owner, RPCB_MAXOWNERLEN);
  785. }
  786. static int rpcb_dec_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr,
  787. struct rpcbind_args *rpcb)
  788. {
  789. struct sockaddr_storage address;
  790. struct sockaddr *sap = (struct sockaddr *)&address;
  791. __be32 *p;
  792. u32 len;
  793. rpcb->r_port = 0;
  794. p = xdr_inline_decode(xdr, 4);
  795. if (unlikely(p == NULL))
  796. goto out_fail;
  797. len = be32_to_cpup(p);
  798. /*
  799. * If the returned universal address is a null string,
  800. * the requested RPC service was not registered.
  801. */
  802. if (len == 0) {
  803. dprintk("RPC: %5u RPCB reply: program not registered\n",
  804. req->rq_task->tk_pid);
  805. return 0;
  806. }
  807. if (unlikely(len > RPCBIND_MAXUADDRLEN))
  808. goto out_fail;
  809. p = xdr_inline_decode(xdr, len);
  810. if (unlikely(p == NULL))
  811. goto out_fail;
  812. dprintk("RPC: %5u RPCB_%s reply: %s\n", req->rq_task->tk_pid,
  813. req->rq_task->tk_msg.rpc_proc->p_name, (char *)p);
  814. if (rpc_uaddr2sockaddr(req->rq_xprt->xprt_net, (char *)p, len,
  815. sap, sizeof(address)) == 0)
  816. goto out_fail;
  817. rpcb->r_port = rpc_get_port(sap);
  818. return 0;
  819. out_fail:
  820. dprintk("RPC: %5u malformed RPCB_%s reply\n",
  821. req->rq_task->tk_pid,
  822. req->rq_task->tk_msg.rpc_proc->p_name);
  823. return -EIO;
  824. }
  825. /*
  826. * Not all rpcbind procedures described in RFC 1833 are implemented
  827. * since the Linux kernel RPC code requires only these.
  828. */
  829. static struct rpc_procinfo rpcb_procedures2[] = {
  830. [RPCBPROC_SET] = {
  831. .p_proc = RPCBPROC_SET,
  832. .p_encode = (kxdreproc_t)rpcb_enc_mapping,
  833. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  834. .p_arglen = RPCB_mappingargs_sz,
  835. .p_replen = RPCB_setres_sz,
  836. .p_statidx = RPCBPROC_SET,
  837. .p_timer = 0,
  838. .p_name = "SET",
  839. },
  840. [RPCBPROC_UNSET] = {
  841. .p_proc = RPCBPROC_UNSET,
  842. .p_encode = (kxdreproc_t)rpcb_enc_mapping,
  843. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  844. .p_arglen = RPCB_mappingargs_sz,
  845. .p_replen = RPCB_setres_sz,
  846. .p_statidx = RPCBPROC_UNSET,
  847. .p_timer = 0,
  848. .p_name = "UNSET",
  849. },
  850. [RPCBPROC_GETPORT] = {
  851. .p_proc = RPCBPROC_GETPORT,
  852. .p_encode = (kxdreproc_t)rpcb_enc_mapping,
  853. .p_decode = (kxdrdproc_t)rpcb_dec_getport,
  854. .p_arglen = RPCB_mappingargs_sz,
  855. .p_replen = RPCB_getportres_sz,
  856. .p_statidx = RPCBPROC_GETPORT,
  857. .p_timer = 0,
  858. .p_name = "GETPORT",
  859. },
  860. };
  861. static struct rpc_procinfo rpcb_procedures3[] = {
  862. [RPCBPROC_SET] = {
  863. .p_proc = RPCBPROC_SET,
  864. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  865. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  866. .p_arglen = RPCB_getaddrargs_sz,
  867. .p_replen = RPCB_setres_sz,
  868. .p_statidx = RPCBPROC_SET,
  869. .p_timer = 0,
  870. .p_name = "SET",
  871. },
  872. [RPCBPROC_UNSET] = {
  873. .p_proc = RPCBPROC_UNSET,
  874. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  875. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  876. .p_arglen = RPCB_getaddrargs_sz,
  877. .p_replen = RPCB_setres_sz,
  878. .p_statidx = RPCBPROC_UNSET,
  879. .p_timer = 0,
  880. .p_name = "UNSET",
  881. },
  882. [RPCBPROC_GETADDR] = {
  883. .p_proc = RPCBPROC_GETADDR,
  884. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  885. .p_decode = (kxdrdproc_t)rpcb_dec_getaddr,
  886. .p_arglen = RPCB_getaddrargs_sz,
  887. .p_replen = RPCB_getaddrres_sz,
  888. .p_statidx = RPCBPROC_GETADDR,
  889. .p_timer = 0,
  890. .p_name = "GETADDR",
  891. },
  892. };
  893. static struct rpc_procinfo rpcb_procedures4[] = {
  894. [RPCBPROC_SET] = {
  895. .p_proc = RPCBPROC_SET,
  896. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  897. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  898. .p_arglen = RPCB_getaddrargs_sz,
  899. .p_replen = RPCB_setres_sz,
  900. .p_statidx = RPCBPROC_SET,
  901. .p_timer = 0,
  902. .p_name = "SET",
  903. },
  904. [RPCBPROC_UNSET] = {
  905. .p_proc = RPCBPROC_UNSET,
  906. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  907. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  908. .p_arglen = RPCB_getaddrargs_sz,
  909. .p_replen = RPCB_setres_sz,
  910. .p_statidx = RPCBPROC_UNSET,
  911. .p_timer = 0,
  912. .p_name = "UNSET",
  913. },
  914. [RPCBPROC_GETADDR] = {
  915. .p_proc = RPCBPROC_GETADDR,
  916. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  917. .p_decode = (kxdrdproc_t)rpcb_dec_getaddr,
  918. .p_arglen = RPCB_getaddrargs_sz,
  919. .p_replen = RPCB_getaddrres_sz,
  920. .p_statidx = RPCBPROC_GETADDR,
  921. .p_timer = 0,
  922. .p_name = "GETADDR",
  923. },
  924. };
  925. static const struct rpcb_info rpcb_next_version[] = {
  926. {
  927. .rpc_vers = RPCBVERS_2,
  928. .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
  929. },
  930. {
  931. .rpc_proc = NULL,
  932. },
  933. };
  934. static const struct rpcb_info rpcb_next_version6[] = {
  935. {
  936. .rpc_vers = RPCBVERS_4,
  937. .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR],
  938. },
  939. {
  940. .rpc_vers = RPCBVERS_3,
  941. .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR],
  942. },
  943. {
  944. .rpc_proc = NULL,
  945. },
  946. };
  947. static const struct rpc_version rpcb_version2 = {
  948. .number = RPCBVERS_2,
  949. .nrprocs = ARRAY_SIZE(rpcb_procedures2),
  950. .procs = rpcb_procedures2
  951. };
  952. static const struct rpc_version rpcb_version3 = {
  953. .number = RPCBVERS_3,
  954. .nrprocs = ARRAY_SIZE(rpcb_procedures3),
  955. .procs = rpcb_procedures3
  956. };
  957. static const struct rpc_version rpcb_version4 = {
  958. .number = RPCBVERS_4,
  959. .nrprocs = ARRAY_SIZE(rpcb_procedures4),
  960. .procs = rpcb_procedures4
  961. };
  962. static const struct rpc_version *rpcb_version[] = {
  963. NULL,
  964. NULL,
  965. &rpcb_version2,
  966. &rpcb_version3,
  967. &rpcb_version4
  968. };
  969. static struct rpc_stat rpcb_stats;
  970. static const struct rpc_program rpcb_program = {
  971. .name = "rpcbind",
  972. .number = RPCBIND_PROGRAM,
  973. .nrvers = ARRAY_SIZE(rpcb_version),
  974. .version = rpcb_version,
  975. .stats = &rpcb_stats,
  976. };