rpcb_clnt.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118
  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;
  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(struct net *net)
  282. {
  283. static DEFINE_MUTEX(rpcb_create_local_mutex);
  284. int result = 0;
  285. if (rpcb_get_local(net))
  286. return result;
  287. mutex_lock(&rpcb_create_local_mutex);
  288. if (rpcb_get_local(net))
  289. goto out;
  290. if (rpcb_create_local_unix(net) != 0)
  291. result = rpcb_create_local_net(net);
  292. out:
  293. mutex_unlock(&rpcb_create_local_mutex);
  294. return result;
  295. }
  296. static struct rpc_clnt *rpcb_create(struct net *net, const char *hostname,
  297. struct sockaddr *srvaddr, size_t salen,
  298. int proto, u32 version)
  299. {
  300. struct rpc_create_args args = {
  301. .net = net,
  302. .protocol = proto,
  303. .address = srvaddr,
  304. .addrsize = salen,
  305. .servername = hostname,
  306. .program = &rpcb_program,
  307. .version = version,
  308. .authflavor = RPC_AUTH_UNIX,
  309. .flags = (RPC_CLNT_CREATE_NOPING |
  310. RPC_CLNT_CREATE_NONPRIVPORT),
  311. };
  312. switch (srvaddr->sa_family) {
  313. case AF_INET:
  314. ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
  315. break;
  316. case AF_INET6:
  317. ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
  318. break;
  319. default:
  320. return ERR_PTR(-EAFNOSUPPORT);
  321. }
  322. return rpc_create(&args);
  323. }
  324. static int rpcb_register_call(struct rpc_clnt *clnt, struct rpc_message *msg)
  325. {
  326. int result, error = 0;
  327. msg->rpc_resp = &result;
  328. error = rpc_call_sync(clnt, msg, RPC_TASK_SOFTCONN);
  329. if (error < 0) {
  330. dprintk("RPC: failed to contact local rpcbind "
  331. "server (errno %d).\n", -error);
  332. return error;
  333. }
  334. if (!result)
  335. return -EACCES;
  336. return 0;
  337. }
  338. /**
  339. * rpcb_register - set or unset a port registration with the local rpcbind svc
  340. * @prog: RPC program number to bind
  341. * @vers: RPC version number to bind
  342. * @prot: transport protocol to register
  343. * @port: port value to register
  344. *
  345. * Returns zero if the registration request was dispatched successfully
  346. * and the rpcbind daemon returned success. Otherwise, returns an errno
  347. * value that reflects the nature of the error (request could not be
  348. * dispatched, timed out, or rpcbind returned an error).
  349. *
  350. * RPC services invoke this function to advertise their contact
  351. * information via the system's rpcbind daemon. RPC services
  352. * invoke this function once for each [program, version, transport]
  353. * tuple they wish to advertise.
  354. *
  355. * Callers may also unregister RPC services that are no longer
  356. * available by setting the passed-in port to zero. This removes
  357. * all registered transports for [program, version] from the local
  358. * rpcbind database.
  359. *
  360. * This function uses rpcbind protocol version 2 to contact the
  361. * local rpcbind daemon.
  362. *
  363. * Registration works over both AF_INET and AF_INET6, and services
  364. * registered via this function are advertised as available for any
  365. * address. If the local rpcbind daemon is listening on AF_INET6,
  366. * services registered via this function will be advertised on
  367. * IN6ADDR_ANY (ie available for all AF_INET and AF_INET6
  368. * addresses).
  369. */
  370. int rpcb_register(struct net *net, u32 prog, u32 vers, int prot, unsigned short port)
  371. {
  372. struct rpcbind_args map = {
  373. .r_prog = prog,
  374. .r_vers = vers,
  375. .r_prot = prot,
  376. .r_port = port,
  377. };
  378. struct rpc_message msg = {
  379. .rpc_argp = &map,
  380. };
  381. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  382. dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
  383. "rpcbind\n", (port ? "" : "un"),
  384. prog, vers, prot, port);
  385. msg.rpc_proc = &rpcb_procedures2[RPCBPROC_UNSET];
  386. if (port)
  387. msg.rpc_proc = &rpcb_procedures2[RPCBPROC_SET];
  388. return rpcb_register_call(sn->rpcb_local_clnt, &msg);
  389. }
  390. /*
  391. * Fill in AF_INET family-specific arguments to register
  392. */
  393. static int rpcb_register_inet4(struct sunrpc_net *sn,
  394. const struct sockaddr *sap,
  395. struct rpc_message *msg)
  396. {
  397. const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
  398. struct rpcbind_args *map = msg->rpc_argp;
  399. unsigned short port = ntohs(sin->sin_port);
  400. int result;
  401. map->r_addr = rpc_sockaddr2uaddr(sap, GFP_KERNEL);
  402. dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
  403. "local rpcbind\n", (port ? "" : "un"),
  404. map->r_prog, map->r_vers,
  405. map->r_addr, map->r_netid);
  406. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  407. if (port)
  408. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
  409. result = rpcb_register_call(sn->rpcb_local_clnt4, msg);
  410. kfree(map->r_addr);
  411. return result;
  412. }
  413. /*
  414. * Fill in AF_INET6 family-specific arguments to register
  415. */
  416. static int rpcb_register_inet6(struct sunrpc_net *sn,
  417. const struct sockaddr *sap,
  418. struct rpc_message *msg)
  419. {
  420. const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sap;
  421. struct rpcbind_args *map = msg->rpc_argp;
  422. unsigned short port = ntohs(sin6->sin6_port);
  423. int result;
  424. map->r_addr = rpc_sockaddr2uaddr(sap, GFP_KERNEL);
  425. dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
  426. "local rpcbind\n", (port ? "" : "un"),
  427. map->r_prog, map->r_vers,
  428. map->r_addr, map->r_netid);
  429. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  430. if (port)
  431. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
  432. result = rpcb_register_call(sn->rpcb_local_clnt4, msg);
  433. kfree(map->r_addr);
  434. return result;
  435. }
  436. static int rpcb_unregister_all_protofamilies(struct sunrpc_net *sn,
  437. struct rpc_message *msg)
  438. {
  439. struct rpcbind_args *map = msg->rpc_argp;
  440. dprintk("RPC: unregistering [%u, %u, '%s'] with "
  441. "local rpcbind\n",
  442. map->r_prog, map->r_vers, map->r_netid);
  443. map->r_addr = "";
  444. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  445. return rpcb_register_call(sn->rpcb_local_clnt4, msg);
  446. }
  447. /**
  448. * rpcb_v4_register - set or unset a port registration with the local rpcbind
  449. * @program: RPC program number of service to (un)register
  450. * @version: RPC version number of service to (un)register
  451. * @address: address family, IP address, and port to (un)register
  452. * @netid: netid of transport protocol to (un)register
  453. *
  454. * Returns zero if the registration request was dispatched successfully
  455. * and the rpcbind daemon returned success. Otherwise, returns an errno
  456. * value that reflects the nature of the error (request could not be
  457. * dispatched, timed out, or rpcbind returned an error).
  458. *
  459. * RPC services invoke this function to advertise their contact
  460. * information via the system's rpcbind daemon. RPC services
  461. * invoke this function once for each [program, version, address,
  462. * netid] tuple they wish to advertise.
  463. *
  464. * Callers may also unregister RPC services that are registered at a
  465. * specific address by setting the port number in @address to zero.
  466. * They may unregister all registered protocol families at once for
  467. * a service by passing a NULL @address argument. If @netid is ""
  468. * then all netids for [program, version, address] are unregistered.
  469. *
  470. * This function uses rpcbind protocol version 4 to contact the
  471. * local rpcbind daemon. The local rpcbind daemon must support
  472. * version 4 of the rpcbind protocol in order for these functions
  473. * to register a service successfully.
  474. *
  475. * Supported netids include "udp" and "tcp" for UDP and TCP over
  476. * IPv4, and "udp6" and "tcp6" for UDP and TCP over IPv6,
  477. * respectively.
  478. *
  479. * The contents of @address determine the address family and the
  480. * port to be registered. The usual practice is to pass INADDR_ANY
  481. * as the raw address, but specifying a non-zero address is also
  482. * supported by this API if the caller wishes to advertise an RPC
  483. * service on a specific network interface.
  484. *
  485. * Note that passing in INADDR_ANY does not create the same service
  486. * registration as IN6ADDR_ANY. The former advertises an RPC
  487. * service on any IPv4 address, but not on IPv6. The latter
  488. * advertises the service on all IPv4 and IPv6 addresses.
  489. */
  490. int rpcb_v4_register(struct net *net, const u32 program, const u32 version,
  491. const struct sockaddr *address, const char *netid)
  492. {
  493. struct rpcbind_args map = {
  494. .r_prog = program,
  495. .r_vers = version,
  496. .r_netid = netid,
  497. .r_owner = RPCB_OWNER_STRING,
  498. };
  499. struct rpc_message msg = {
  500. .rpc_argp = &map,
  501. };
  502. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  503. if (sn->rpcb_local_clnt4 == NULL)
  504. return -EPROTONOSUPPORT;
  505. if (address == NULL)
  506. return rpcb_unregister_all_protofamilies(sn, &msg);
  507. switch (address->sa_family) {
  508. case AF_INET:
  509. return rpcb_register_inet4(sn, address, &msg);
  510. case AF_INET6:
  511. return rpcb_register_inet6(sn, address, &msg);
  512. }
  513. return -EAFNOSUPPORT;
  514. }
  515. static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt, struct rpcbind_args *map, struct rpc_procinfo *proc)
  516. {
  517. struct rpc_message msg = {
  518. .rpc_proc = proc,
  519. .rpc_argp = map,
  520. .rpc_resp = map,
  521. };
  522. struct rpc_task_setup task_setup_data = {
  523. .rpc_client = rpcb_clnt,
  524. .rpc_message = &msg,
  525. .callback_ops = &rpcb_getport_ops,
  526. .callback_data = map,
  527. .flags = RPC_TASK_ASYNC | RPC_TASK_SOFTCONN,
  528. };
  529. return rpc_run_task(&task_setup_data);
  530. }
  531. /*
  532. * In the case where rpc clients have been cloned, we want to make
  533. * sure that we use the program number/version etc of the actual
  534. * owner of the xprt. To do so, we walk back up the tree of parents
  535. * to find whoever created the transport and/or whoever has the
  536. * autobind flag set.
  537. */
  538. static struct rpc_clnt *rpcb_find_transport_owner(struct rpc_clnt *clnt)
  539. {
  540. struct rpc_clnt *parent = clnt->cl_parent;
  541. struct rpc_xprt *xprt = rcu_dereference(clnt->cl_xprt);
  542. while (parent != clnt) {
  543. if (rcu_dereference(parent->cl_xprt) != 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. rcu_read_lock();
  573. do {
  574. clnt = rpcb_find_transport_owner(task->tk_client);
  575. xprt = xprt_get(rcu_dereference(clnt->cl_xprt));
  576. } while (xprt == NULL);
  577. rcu_read_unlock();
  578. dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
  579. task->tk_pid, __func__,
  580. xprt->servername, clnt->cl_prog, clnt->cl_vers, xprt->prot);
  581. /* Put self on the wait queue to ensure we get notified if
  582. * some other task is already attempting to bind the port */
  583. rpc_sleep_on(&xprt->binding, task, NULL);
  584. if (xprt_test_and_set_binding(xprt)) {
  585. dprintk("RPC: %5u %s: waiting for another binder\n",
  586. task->tk_pid, __func__);
  587. xprt_put(xprt);
  588. return;
  589. }
  590. /* Someone else may have bound if we slept */
  591. if (xprt_bound(xprt)) {
  592. status = 0;
  593. dprintk("RPC: %5u %s: already bound\n",
  594. task->tk_pid, __func__);
  595. goto bailout_nofree;
  596. }
  597. /* Parent transport's destination address */
  598. salen = rpc_peeraddr(clnt, sap, sizeof(addr));
  599. /* Don't ever use rpcbind v2 for AF_INET6 requests */
  600. switch (sap->sa_family) {
  601. case AF_INET:
  602. proc = rpcb_next_version[xprt->bind_index].rpc_proc;
  603. bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
  604. break;
  605. case AF_INET6:
  606. proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
  607. bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
  608. break;
  609. default:
  610. status = -EAFNOSUPPORT;
  611. dprintk("RPC: %5u %s: bad address family\n",
  612. task->tk_pid, __func__);
  613. goto bailout_nofree;
  614. }
  615. if (proc == NULL) {
  616. xprt->bind_index = 0;
  617. status = -EPFNOSUPPORT;
  618. dprintk("RPC: %5u %s: no more getport versions available\n",
  619. task->tk_pid, __func__);
  620. goto bailout_nofree;
  621. }
  622. dprintk("RPC: %5u %s: trying rpcbind version %u\n",
  623. task->tk_pid, __func__, bind_version);
  624. rpcb_clnt = rpcb_create(xprt->xprt_net, xprt->servername, sap, salen,
  625. xprt->prot, bind_version);
  626. if (IS_ERR(rpcb_clnt)) {
  627. status = PTR_ERR(rpcb_clnt);
  628. dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
  629. task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
  630. goto bailout_nofree;
  631. }
  632. map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
  633. if (!map) {
  634. status = -ENOMEM;
  635. dprintk("RPC: %5u %s: no memory available\n",
  636. task->tk_pid, __func__);
  637. goto bailout_release_client;
  638. }
  639. map->r_prog = clnt->cl_prog;
  640. map->r_vers = clnt->cl_vers;
  641. map->r_prot = xprt->prot;
  642. map->r_port = 0;
  643. map->r_xprt = xprt;
  644. map->r_status = -EIO;
  645. switch (bind_version) {
  646. case RPCBVERS_4:
  647. case RPCBVERS_3:
  648. map->r_netid = xprt->address_strings[RPC_DISPLAY_NETID];
  649. map->r_addr = rpc_sockaddr2uaddr(sap, GFP_ATOMIC);
  650. map->r_owner = "";
  651. break;
  652. case RPCBVERS_2:
  653. map->r_addr = NULL;
  654. break;
  655. default:
  656. BUG();
  657. }
  658. child = rpcb_call_async(rpcb_clnt, map, proc);
  659. rpc_release_client(rpcb_clnt);
  660. if (IS_ERR(child)) {
  661. /* rpcb_map_release() has freed the arguments */
  662. dprintk("RPC: %5u %s: rpc_run_task failed\n",
  663. task->tk_pid, __func__);
  664. return;
  665. }
  666. xprt->stat.bind_count++;
  667. rpc_put_task(child);
  668. return;
  669. bailout_release_client:
  670. rpc_release_client(rpcb_clnt);
  671. bailout_nofree:
  672. rpcb_wake_rpcbind_waiters(xprt, status);
  673. task->tk_status = status;
  674. xprt_put(xprt);
  675. }
  676. EXPORT_SYMBOL_GPL(rpcb_getport_async);
  677. /*
  678. * Rpcbind child task calls this callback via tk_exit.
  679. */
  680. static void rpcb_getport_done(struct rpc_task *child, void *data)
  681. {
  682. struct rpcbind_args *map = data;
  683. struct rpc_xprt *xprt = map->r_xprt;
  684. int status = child->tk_status;
  685. /* Garbage reply: retry with a lesser rpcbind version */
  686. if (status == -EIO)
  687. status = -EPROTONOSUPPORT;
  688. /* rpcbind server doesn't support this rpcbind protocol version */
  689. if (status == -EPROTONOSUPPORT)
  690. xprt->bind_index++;
  691. if (status < 0) {
  692. /* rpcbind server not available on remote host? */
  693. xprt->ops->set_port(xprt, 0);
  694. } else if (map->r_port == 0) {
  695. /* Requested RPC service wasn't registered on remote host */
  696. xprt->ops->set_port(xprt, 0);
  697. status = -EACCES;
  698. } else {
  699. /* Succeeded */
  700. xprt->ops->set_port(xprt, map->r_port);
  701. xprt_set_bound(xprt);
  702. status = 0;
  703. }
  704. dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
  705. child->tk_pid, status, map->r_port);
  706. map->r_status = status;
  707. }
  708. /*
  709. * XDR functions for rpcbind
  710. */
  711. static void rpcb_enc_mapping(struct rpc_rqst *req, struct xdr_stream *xdr,
  712. const struct rpcbind_args *rpcb)
  713. {
  714. __be32 *p;
  715. dprintk("RPC: %5u encoding PMAP_%s call (%u, %u, %d, %u)\n",
  716. req->rq_task->tk_pid,
  717. req->rq_task->tk_msg.rpc_proc->p_name,
  718. rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
  719. p = xdr_reserve_space(xdr, RPCB_mappingargs_sz << 2);
  720. *p++ = cpu_to_be32(rpcb->r_prog);
  721. *p++ = cpu_to_be32(rpcb->r_vers);
  722. *p++ = cpu_to_be32(rpcb->r_prot);
  723. *p = cpu_to_be32(rpcb->r_port);
  724. }
  725. static int rpcb_dec_getport(struct rpc_rqst *req, struct xdr_stream *xdr,
  726. struct rpcbind_args *rpcb)
  727. {
  728. unsigned long port;
  729. __be32 *p;
  730. rpcb->r_port = 0;
  731. p = xdr_inline_decode(xdr, 4);
  732. if (unlikely(p == NULL))
  733. return -EIO;
  734. port = be32_to_cpup(p);
  735. dprintk("RPC: %5u PMAP_%s result: %lu\n", req->rq_task->tk_pid,
  736. req->rq_task->tk_msg.rpc_proc->p_name, port);
  737. if (unlikely(port > USHRT_MAX))
  738. return -EIO;
  739. rpcb->r_port = port;
  740. return 0;
  741. }
  742. static int rpcb_dec_set(struct rpc_rqst *req, struct xdr_stream *xdr,
  743. unsigned int *boolp)
  744. {
  745. __be32 *p;
  746. p = xdr_inline_decode(xdr, 4);
  747. if (unlikely(p == NULL))
  748. return -EIO;
  749. *boolp = 0;
  750. if (*p != xdr_zero)
  751. *boolp = 1;
  752. dprintk("RPC: %5u RPCB_%s call %s\n",
  753. req->rq_task->tk_pid,
  754. req->rq_task->tk_msg.rpc_proc->p_name,
  755. (*boolp ? "succeeded" : "failed"));
  756. return 0;
  757. }
  758. static void encode_rpcb_string(struct xdr_stream *xdr, const char *string,
  759. const u32 maxstrlen)
  760. {
  761. __be32 *p;
  762. u32 len;
  763. len = strlen(string);
  764. BUG_ON(len > maxstrlen);
  765. p = xdr_reserve_space(xdr, 4 + len);
  766. xdr_encode_opaque(p, string, len);
  767. }
  768. static void rpcb_enc_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr,
  769. const struct rpcbind_args *rpcb)
  770. {
  771. __be32 *p;
  772. dprintk("RPC: %5u encoding RPCB_%s call (%u, %u, '%s', '%s')\n",
  773. req->rq_task->tk_pid,
  774. req->rq_task->tk_msg.rpc_proc->p_name,
  775. rpcb->r_prog, rpcb->r_vers,
  776. rpcb->r_netid, rpcb->r_addr);
  777. p = xdr_reserve_space(xdr, (RPCB_program_sz + RPCB_version_sz) << 2);
  778. *p++ = cpu_to_be32(rpcb->r_prog);
  779. *p = cpu_to_be32(rpcb->r_vers);
  780. encode_rpcb_string(xdr, rpcb->r_netid, RPCBIND_MAXNETIDLEN);
  781. encode_rpcb_string(xdr, rpcb->r_addr, RPCBIND_MAXUADDRLEN);
  782. encode_rpcb_string(xdr, rpcb->r_owner, RPCB_MAXOWNERLEN);
  783. }
  784. static int rpcb_dec_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr,
  785. struct rpcbind_args *rpcb)
  786. {
  787. struct sockaddr_storage address;
  788. struct sockaddr *sap = (struct sockaddr *)&address;
  789. __be32 *p;
  790. u32 len;
  791. rpcb->r_port = 0;
  792. p = xdr_inline_decode(xdr, 4);
  793. if (unlikely(p == NULL))
  794. goto out_fail;
  795. len = be32_to_cpup(p);
  796. /*
  797. * If the returned universal address is a null string,
  798. * the requested RPC service was not registered.
  799. */
  800. if (len == 0) {
  801. dprintk("RPC: %5u RPCB reply: program not registered\n",
  802. req->rq_task->tk_pid);
  803. return 0;
  804. }
  805. if (unlikely(len > RPCBIND_MAXUADDRLEN))
  806. goto out_fail;
  807. p = xdr_inline_decode(xdr, len);
  808. if (unlikely(p == NULL))
  809. goto out_fail;
  810. dprintk("RPC: %5u RPCB_%s reply: %s\n", req->rq_task->tk_pid,
  811. req->rq_task->tk_msg.rpc_proc->p_name, (char *)p);
  812. if (rpc_uaddr2sockaddr(req->rq_xprt->xprt_net, (char *)p, len,
  813. sap, sizeof(address)) == 0)
  814. goto out_fail;
  815. rpcb->r_port = rpc_get_port(sap);
  816. return 0;
  817. out_fail:
  818. dprintk("RPC: %5u malformed RPCB_%s reply\n",
  819. req->rq_task->tk_pid,
  820. req->rq_task->tk_msg.rpc_proc->p_name);
  821. return -EIO;
  822. }
  823. /*
  824. * Not all rpcbind procedures described in RFC 1833 are implemented
  825. * since the Linux kernel RPC code requires only these.
  826. */
  827. static struct rpc_procinfo rpcb_procedures2[] = {
  828. [RPCBPROC_SET] = {
  829. .p_proc = RPCBPROC_SET,
  830. .p_encode = (kxdreproc_t)rpcb_enc_mapping,
  831. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  832. .p_arglen = RPCB_mappingargs_sz,
  833. .p_replen = RPCB_setres_sz,
  834. .p_statidx = RPCBPROC_SET,
  835. .p_timer = 0,
  836. .p_name = "SET",
  837. },
  838. [RPCBPROC_UNSET] = {
  839. .p_proc = RPCBPROC_UNSET,
  840. .p_encode = (kxdreproc_t)rpcb_enc_mapping,
  841. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  842. .p_arglen = RPCB_mappingargs_sz,
  843. .p_replen = RPCB_setres_sz,
  844. .p_statidx = RPCBPROC_UNSET,
  845. .p_timer = 0,
  846. .p_name = "UNSET",
  847. },
  848. [RPCBPROC_GETPORT] = {
  849. .p_proc = RPCBPROC_GETPORT,
  850. .p_encode = (kxdreproc_t)rpcb_enc_mapping,
  851. .p_decode = (kxdrdproc_t)rpcb_dec_getport,
  852. .p_arglen = RPCB_mappingargs_sz,
  853. .p_replen = RPCB_getportres_sz,
  854. .p_statidx = RPCBPROC_GETPORT,
  855. .p_timer = 0,
  856. .p_name = "GETPORT",
  857. },
  858. };
  859. static struct rpc_procinfo rpcb_procedures3[] = {
  860. [RPCBPROC_SET] = {
  861. .p_proc = RPCBPROC_SET,
  862. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  863. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  864. .p_arglen = RPCB_getaddrargs_sz,
  865. .p_replen = RPCB_setres_sz,
  866. .p_statidx = RPCBPROC_SET,
  867. .p_timer = 0,
  868. .p_name = "SET",
  869. },
  870. [RPCBPROC_UNSET] = {
  871. .p_proc = RPCBPROC_UNSET,
  872. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  873. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  874. .p_arglen = RPCB_getaddrargs_sz,
  875. .p_replen = RPCB_setres_sz,
  876. .p_statidx = RPCBPROC_UNSET,
  877. .p_timer = 0,
  878. .p_name = "UNSET",
  879. },
  880. [RPCBPROC_GETADDR] = {
  881. .p_proc = RPCBPROC_GETADDR,
  882. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  883. .p_decode = (kxdrdproc_t)rpcb_dec_getaddr,
  884. .p_arglen = RPCB_getaddrargs_sz,
  885. .p_replen = RPCB_getaddrres_sz,
  886. .p_statidx = RPCBPROC_GETADDR,
  887. .p_timer = 0,
  888. .p_name = "GETADDR",
  889. },
  890. };
  891. static struct rpc_procinfo rpcb_procedures4[] = {
  892. [RPCBPROC_SET] = {
  893. .p_proc = RPCBPROC_SET,
  894. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  895. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  896. .p_arglen = RPCB_getaddrargs_sz,
  897. .p_replen = RPCB_setres_sz,
  898. .p_statidx = RPCBPROC_SET,
  899. .p_timer = 0,
  900. .p_name = "SET",
  901. },
  902. [RPCBPROC_UNSET] = {
  903. .p_proc = RPCBPROC_UNSET,
  904. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  905. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  906. .p_arglen = RPCB_getaddrargs_sz,
  907. .p_replen = RPCB_setres_sz,
  908. .p_statidx = RPCBPROC_UNSET,
  909. .p_timer = 0,
  910. .p_name = "UNSET",
  911. },
  912. [RPCBPROC_GETADDR] = {
  913. .p_proc = RPCBPROC_GETADDR,
  914. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  915. .p_decode = (kxdrdproc_t)rpcb_dec_getaddr,
  916. .p_arglen = RPCB_getaddrargs_sz,
  917. .p_replen = RPCB_getaddrres_sz,
  918. .p_statidx = RPCBPROC_GETADDR,
  919. .p_timer = 0,
  920. .p_name = "GETADDR",
  921. },
  922. };
  923. static const struct rpcb_info rpcb_next_version[] = {
  924. {
  925. .rpc_vers = RPCBVERS_2,
  926. .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
  927. },
  928. {
  929. .rpc_proc = NULL,
  930. },
  931. };
  932. static const struct rpcb_info rpcb_next_version6[] = {
  933. {
  934. .rpc_vers = RPCBVERS_4,
  935. .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR],
  936. },
  937. {
  938. .rpc_vers = RPCBVERS_3,
  939. .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR],
  940. },
  941. {
  942. .rpc_proc = NULL,
  943. },
  944. };
  945. static const struct rpc_version rpcb_version2 = {
  946. .number = RPCBVERS_2,
  947. .nrprocs = ARRAY_SIZE(rpcb_procedures2),
  948. .procs = rpcb_procedures2
  949. };
  950. static const struct rpc_version rpcb_version3 = {
  951. .number = RPCBVERS_3,
  952. .nrprocs = ARRAY_SIZE(rpcb_procedures3),
  953. .procs = rpcb_procedures3
  954. };
  955. static const struct rpc_version rpcb_version4 = {
  956. .number = RPCBVERS_4,
  957. .nrprocs = ARRAY_SIZE(rpcb_procedures4),
  958. .procs = rpcb_procedures4
  959. };
  960. static const struct rpc_version *rpcb_version[] = {
  961. NULL,
  962. NULL,
  963. &rpcb_version2,
  964. &rpcb_version3,
  965. &rpcb_version4
  966. };
  967. static struct rpc_stat rpcb_stats;
  968. static const struct rpc_program rpcb_program = {
  969. .name = "rpcbind",
  970. .number = RPCBIND_PROGRAM,
  971. .nrvers = ARRAY_SIZE(rpcb_version),
  972. .version = rpcb_version,
  973. .stats = &rpcb_stats,
  974. };