rpcb_clnt.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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. #ifdef RPC_DEBUG
  29. # define RPCDBG_FACILITY RPCDBG_BIND
  30. #endif
  31. #define RPCBIND_SOCK_PATHNAME "/var/run/rpcbind.sock"
  32. #define RPCBIND_PROGRAM (100000u)
  33. #define RPCBIND_PORT (111u)
  34. #define RPCBVERS_2 (2u)
  35. #define RPCBVERS_3 (3u)
  36. #define RPCBVERS_4 (4u)
  37. enum {
  38. RPCBPROC_NULL,
  39. RPCBPROC_SET,
  40. RPCBPROC_UNSET,
  41. RPCBPROC_GETPORT,
  42. RPCBPROC_GETADDR = 3, /* alias for GETPORT */
  43. RPCBPROC_DUMP,
  44. RPCBPROC_CALLIT,
  45. RPCBPROC_BCAST = 5, /* alias for CALLIT */
  46. RPCBPROC_GETTIME,
  47. RPCBPROC_UADDR2TADDR,
  48. RPCBPROC_TADDR2UADDR,
  49. RPCBPROC_GETVERSADDR,
  50. RPCBPROC_INDIRECT,
  51. RPCBPROC_GETADDRLIST,
  52. RPCBPROC_GETSTAT,
  53. };
  54. /*
  55. * r_owner
  56. *
  57. * The "owner" is allowed to unset a service in the rpcbind database.
  58. *
  59. * For AF_LOCAL SET/UNSET requests, rpcbind treats this string as a
  60. * UID which it maps to a local user name via a password lookup.
  61. * In all other cases it is ignored.
  62. *
  63. * For SET/UNSET requests, user space provides a value, even for
  64. * network requests, and GETADDR uses an empty string. We follow
  65. * those precedents here.
  66. */
  67. #define RPCB_OWNER_STRING "0"
  68. #define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
  69. /*
  70. * XDR data type sizes
  71. */
  72. #define RPCB_program_sz (1)
  73. #define RPCB_version_sz (1)
  74. #define RPCB_protocol_sz (1)
  75. #define RPCB_port_sz (1)
  76. #define RPCB_boolean_sz (1)
  77. #define RPCB_netid_sz (1 + XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
  78. #define RPCB_addr_sz (1 + XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
  79. #define RPCB_ownerstring_sz (1 + XDR_QUADLEN(RPCB_MAXOWNERLEN))
  80. /*
  81. * XDR argument and result sizes
  82. */
  83. #define RPCB_mappingargs_sz (RPCB_program_sz + RPCB_version_sz + \
  84. RPCB_protocol_sz + RPCB_port_sz)
  85. #define RPCB_getaddrargs_sz (RPCB_program_sz + RPCB_version_sz + \
  86. RPCB_netid_sz + RPCB_addr_sz + \
  87. RPCB_ownerstring_sz)
  88. #define RPCB_getportres_sz RPCB_port_sz
  89. #define RPCB_setres_sz RPCB_boolean_sz
  90. /*
  91. * Note that RFC 1833 does not put any size restrictions on the
  92. * address string returned by the remote rpcbind database.
  93. */
  94. #define RPCB_getaddrres_sz RPCB_addr_sz
  95. static void rpcb_getport_done(struct rpc_task *, void *);
  96. static void rpcb_map_release(void *data);
  97. static struct rpc_program rpcb_program;
  98. static struct rpc_clnt * rpcb_local_clnt;
  99. static struct rpc_clnt * rpcb_local_clnt4;
  100. DEFINE_SPINLOCK(rpcb_clnt_lock);
  101. unsigned int rpcb_users;
  102. struct rpcbind_args {
  103. struct rpc_xprt * r_xprt;
  104. u32 r_prog;
  105. u32 r_vers;
  106. u32 r_prot;
  107. unsigned short r_port;
  108. const char * r_netid;
  109. const char * r_addr;
  110. const char * r_owner;
  111. int r_status;
  112. };
  113. static struct rpc_procinfo rpcb_procedures2[];
  114. static struct rpc_procinfo rpcb_procedures3[];
  115. static struct rpc_procinfo rpcb_procedures4[];
  116. struct rpcb_info {
  117. u32 rpc_vers;
  118. struct rpc_procinfo * rpc_proc;
  119. };
  120. static struct rpcb_info rpcb_next_version[];
  121. static struct rpcb_info rpcb_next_version6[];
  122. static const struct rpc_call_ops rpcb_getport_ops = {
  123. .rpc_call_done = rpcb_getport_done,
  124. .rpc_release = rpcb_map_release,
  125. };
  126. static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status)
  127. {
  128. xprt_clear_binding(xprt);
  129. rpc_wake_up_status(&xprt->binding, status);
  130. }
  131. static void rpcb_map_release(void *data)
  132. {
  133. struct rpcbind_args *map = data;
  134. rpcb_wake_rpcbind_waiters(map->r_xprt, map->r_status);
  135. xprt_put(map->r_xprt);
  136. kfree(map->r_addr);
  137. kfree(map);
  138. }
  139. static int rpcb_get_local(void)
  140. {
  141. int cnt;
  142. spin_lock(&rpcb_clnt_lock);
  143. if (rpcb_users)
  144. rpcb_users++;
  145. cnt = rpcb_users;
  146. spin_unlock(&rpcb_clnt_lock);
  147. return cnt;
  148. }
  149. void rpcb_put_local(void)
  150. {
  151. struct rpc_clnt *clnt = rpcb_local_clnt;
  152. struct rpc_clnt *clnt4 = rpcb_local_clnt4;
  153. int shutdown;
  154. spin_lock(&rpcb_clnt_lock);
  155. if (--rpcb_users == 0) {
  156. rpcb_local_clnt = NULL;
  157. rpcb_local_clnt4 = NULL;
  158. }
  159. shutdown = !rpcb_users;
  160. spin_unlock(&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 rpc_clnt *clnt, struct rpc_clnt *clnt4)
  172. {
  173. /* Protected by rpcb_create_local_mutex */
  174. rpcb_local_clnt = clnt;
  175. rpcb_local_clnt4 = clnt4;
  176. smp_wmb();
  177. rpcb_users = 1;
  178. dprintk("RPC: created new rpcb local clients (rpcb_local_clnt: "
  179. "%p, rpcb_local_clnt4: %p)\n", rpcb_local_clnt,
  180. rpcb_local_clnt4);
  181. }
  182. /*
  183. * Returns zero on success, otherwise a negative errno value
  184. * is returned.
  185. */
  186. static int rpcb_create_local_unix(void)
  187. {
  188. static const struct sockaddr_un rpcb_localaddr_rpcbind = {
  189. .sun_family = AF_LOCAL,
  190. .sun_path = RPCBIND_SOCK_PATHNAME,
  191. };
  192. struct rpc_create_args args = {
  193. .net = &init_net,
  194. .protocol = XPRT_TRANSPORT_LOCAL,
  195. .address = (struct sockaddr *)&rpcb_localaddr_rpcbind,
  196. .addrsize = sizeof(rpcb_localaddr_rpcbind),
  197. .servername = "localhost",
  198. .program = &rpcb_program,
  199. .version = RPCBVERS_2,
  200. .authflavor = RPC_AUTH_NULL,
  201. };
  202. struct rpc_clnt *clnt, *clnt4;
  203. int result = 0;
  204. /*
  205. * Because we requested an RPC PING at transport creation time,
  206. * this works only if the user space portmapper is rpcbind, and
  207. * it's listening on AF_LOCAL on the named socket.
  208. */
  209. clnt = rpc_create(&args);
  210. if (IS_ERR(clnt)) {
  211. dprintk("RPC: failed to create AF_LOCAL rpcbind "
  212. "client (errno %ld).\n", PTR_ERR(clnt));
  213. result = -PTR_ERR(clnt);
  214. goto out;
  215. }
  216. clnt4 = rpc_bind_new_program(clnt, &rpcb_program, RPCBVERS_4);
  217. if (IS_ERR(clnt4)) {
  218. dprintk("RPC: failed to bind second program to "
  219. "rpcbind v4 client (errno %ld).\n",
  220. PTR_ERR(clnt4));
  221. clnt4 = NULL;
  222. }
  223. /* Protected by rpcb_create_local_mutex */
  224. rpcb_local_clnt = clnt;
  225. rpcb_local_clnt4 = clnt4;
  226. out:
  227. return result;
  228. }
  229. /*
  230. * Returns zero on success, otherwise a negative errno value
  231. * is returned.
  232. */
  233. static int rpcb_create_local_net(void)
  234. {
  235. static const struct sockaddr_in rpcb_inaddr_loopback = {
  236. .sin_family = AF_INET,
  237. .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
  238. .sin_port = htons(RPCBIND_PORT),
  239. };
  240. struct rpc_create_args args = {
  241. .net = &init_net,
  242. .protocol = XPRT_TRANSPORT_TCP,
  243. .address = (struct sockaddr *)&rpcb_inaddr_loopback,
  244. .addrsize = sizeof(rpcb_inaddr_loopback),
  245. .servername = "localhost",
  246. .program = &rpcb_program,
  247. .version = RPCBVERS_2,
  248. .authflavor = RPC_AUTH_UNIX,
  249. .flags = RPC_CLNT_CREATE_NOPING,
  250. };
  251. struct rpc_clnt *clnt, *clnt4;
  252. int result = 0;
  253. clnt = rpc_create(&args);
  254. if (IS_ERR(clnt)) {
  255. dprintk("RPC: failed to create local rpcbind "
  256. "client (errno %ld).\n", PTR_ERR(clnt));
  257. result = -PTR_ERR(clnt);
  258. goto out;
  259. }
  260. /*
  261. * This results in an RPC ping. On systems running portmapper,
  262. * the v4 ping will fail. Proceed anyway, but disallow rpcb
  263. * v4 upcalls.
  264. */
  265. clnt4 = rpc_bind_new_program(clnt, &rpcb_program, RPCBVERS_4);
  266. if (IS_ERR(clnt4)) {
  267. dprintk("RPC: failed to bind second program to "
  268. "rpcbind v4 client (errno %ld).\n",
  269. PTR_ERR(clnt4));
  270. clnt4 = NULL;
  271. }
  272. /* Protected by rpcb_create_local_mutex */
  273. rpcb_local_clnt = clnt;
  274. rpcb_local_clnt4 = clnt4;
  275. out:
  276. return result;
  277. }
  278. /*
  279. * Returns zero on success, otherwise a negative errno value
  280. * is returned.
  281. */
  282. static int rpcb_create_local(void)
  283. {
  284. static DEFINE_MUTEX(rpcb_create_local_mutex);
  285. int result = 0;
  286. if (rpcb_local_clnt)
  287. return result;
  288. mutex_lock(&rpcb_create_local_mutex);
  289. if (rpcb_local_clnt)
  290. goto out;
  291. if (rpcb_create_local_unix() != 0)
  292. result = rpcb_create_local_net();
  293. out:
  294. mutex_unlock(&rpcb_create_local_mutex);
  295. return result;
  296. }
  297. static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
  298. size_t salen, int proto, u32 version)
  299. {
  300. struct rpc_create_args args = {
  301. .net = &init_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(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. int error;
  382. error = rpcb_create_local();
  383. if (error)
  384. return error;
  385. dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
  386. "rpcbind\n", (port ? "" : "un"),
  387. prog, vers, prot, port);
  388. msg.rpc_proc = &rpcb_procedures2[RPCBPROC_UNSET];
  389. if (port)
  390. msg.rpc_proc = &rpcb_procedures2[RPCBPROC_SET];
  391. return rpcb_register_call(rpcb_local_clnt, &msg);
  392. }
  393. /*
  394. * Fill in AF_INET family-specific arguments to register
  395. */
  396. static int rpcb_register_inet4(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(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(const struct sockaddr *sap,
  419. struct rpc_message *msg)
  420. {
  421. const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sap;
  422. struct rpcbind_args *map = msg->rpc_argp;
  423. unsigned short port = ntohs(sin6->sin6_port);
  424. int result;
  425. map->r_addr = rpc_sockaddr2uaddr(sap, GFP_KERNEL);
  426. dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
  427. "local rpcbind\n", (port ? "" : "un"),
  428. map->r_prog, map->r_vers,
  429. map->r_addr, map->r_netid);
  430. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  431. if (port)
  432. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
  433. result = rpcb_register_call(rpcb_local_clnt4, msg);
  434. kfree(map->r_addr);
  435. return result;
  436. }
  437. static int rpcb_unregister_all_protofamilies(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(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(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. int error;
  503. error = rpcb_create_local();
  504. if (error)
  505. return error;
  506. if (rpcb_local_clnt4 == NULL)
  507. return -EPROTONOSUPPORT;
  508. if (address == NULL)
  509. return rpcb_unregister_all_protofamilies(&msg);
  510. switch (address->sa_family) {
  511. case AF_INET:
  512. return rpcb_register_inet4(address, &msg);
  513. case AF_INET6:
  514. return rpcb_register_inet6(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. while (parent != clnt) {
  545. if (parent->cl_xprt != clnt->cl_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. clnt = rpcb_find_transport_owner(task->tk_client);
  575. xprt = clnt->cl_xprt;
  576. dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
  577. task->tk_pid, __func__,
  578. clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot);
  579. /* Put self on the wait queue to ensure we get notified if
  580. * some other task is already attempting to bind the port */
  581. rpc_sleep_on(&xprt->binding, task, NULL);
  582. if (xprt_test_and_set_binding(xprt)) {
  583. dprintk("RPC: %5u %s: waiting for another binder\n",
  584. task->tk_pid, __func__);
  585. return;
  586. }
  587. /* Someone else may have bound if we slept */
  588. if (xprt_bound(xprt)) {
  589. status = 0;
  590. dprintk("RPC: %5u %s: already bound\n",
  591. task->tk_pid, __func__);
  592. goto bailout_nofree;
  593. }
  594. /* Parent transport's destination address */
  595. salen = rpc_peeraddr(clnt, sap, sizeof(addr));
  596. /* Don't ever use rpcbind v2 for AF_INET6 requests */
  597. switch (sap->sa_family) {
  598. case AF_INET:
  599. proc = rpcb_next_version[xprt->bind_index].rpc_proc;
  600. bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
  601. break;
  602. case AF_INET6:
  603. proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
  604. bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
  605. break;
  606. default:
  607. status = -EAFNOSUPPORT;
  608. dprintk("RPC: %5u %s: bad address family\n",
  609. task->tk_pid, __func__);
  610. goto bailout_nofree;
  611. }
  612. if (proc == NULL) {
  613. xprt->bind_index = 0;
  614. status = -EPFNOSUPPORT;
  615. dprintk("RPC: %5u %s: no more getport versions available\n",
  616. task->tk_pid, __func__);
  617. goto bailout_nofree;
  618. }
  619. dprintk("RPC: %5u %s: trying rpcbind version %u\n",
  620. task->tk_pid, __func__, bind_version);
  621. rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot,
  622. bind_version);
  623. if (IS_ERR(rpcb_clnt)) {
  624. status = PTR_ERR(rpcb_clnt);
  625. dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
  626. task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
  627. goto bailout_nofree;
  628. }
  629. map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
  630. if (!map) {
  631. status = -ENOMEM;
  632. dprintk("RPC: %5u %s: no memory available\n",
  633. task->tk_pid, __func__);
  634. goto bailout_release_client;
  635. }
  636. map->r_prog = clnt->cl_prog;
  637. map->r_vers = clnt->cl_vers;
  638. map->r_prot = xprt->prot;
  639. map->r_port = 0;
  640. map->r_xprt = xprt_get(xprt);
  641. map->r_status = -EIO;
  642. switch (bind_version) {
  643. case RPCBVERS_4:
  644. case RPCBVERS_3:
  645. map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
  646. map->r_addr = rpc_sockaddr2uaddr(sap, GFP_ATOMIC);
  647. map->r_owner = "";
  648. break;
  649. case RPCBVERS_2:
  650. map->r_addr = NULL;
  651. break;
  652. default:
  653. BUG();
  654. }
  655. child = rpcb_call_async(rpcb_clnt, map, proc);
  656. rpc_release_client(rpcb_clnt);
  657. if (IS_ERR(child)) {
  658. /* rpcb_map_release() has freed the arguments */
  659. dprintk("RPC: %5u %s: rpc_run_task failed\n",
  660. task->tk_pid, __func__);
  661. return;
  662. }
  663. xprt->stat.bind_count++;
  664. rpc_put_task(child);
  665. return;
  666. bailout_release_client:
  667. rpc_release_client(rpcb_clnt);
  668. bailout_nofree:
  669. rpcb_wake_rpcbind_waiters(xprt, status);
  670. task->tk_status = status;
  671. }
  672. EXPORT_SYMBOL_GPL(rpcb_getport_async);
  673. /*
  674. * Rpcbind child task calls this callback via tk_exit.
  675. */
  676. static void rpcb_getport_done(struct rpc_task *child, void *data)
  677. {
  678. struct rpcbind_args *map = data;
  679. struct rpc_xprt *xprt = map->r_xprt;
  680. int status = child->tk_status;
  681. /* Garbage reply: retry with a lesser rpcbind version */
  682. if (status == -EIO)
  683. status = -EPROTONOSUPPORT;
  684. /* rpcbind server doesn't support this rpcbind protocol version */
  685. if (status == -EPROTONOSUPPORT)
  686. xprt->bind_index++;
  687. if (status < 0) {
  688. /* rpcbind server not available on remote host? */
  689. xprt->ops->set_port(xprt, 0);
  690. } else if (map->r_port == 0) {
  691. /* Requested RPC service wasn't registered on remote host */
  692. xprt->ops->set_port(xprt, 0);
  693. status = -EACCES;
  694. } else {
  695. /* Succeeded */
  696. xprt->ops->set_port(xprt, map->r_port);
  697. xprt_set_bound(xprt);
  698. status = 0;
  699. }
  700. dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
  701. child->tk_pid, status, map->r_port);
  702. map->r_status = status;
  703. }
  704. /*
  705. * XDR functions for rpcbind
  706. */
  707. static void rpcb_enc_mapping(struct rpc_rqst *req, struct xdr_stream *xdr,
  708. const struct rpcbind_args *rpcb)
  709. {
  710. struct rpc_task *task = req->rq_task;
  711. __be32 *p;
  712. dprintk("RPC: %5u encoding PMAP_%s call (%u, %u, %d, %u)\n",
  713. task->tk_pid, task->tk_msg.rpc_proc->p_name,
  714. rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
  715. p = xdr_reserve_space(xdr, RPCB_mappingargs_sz << 2);
  716. *p++ = cpu_to_be32(rpcb->r_prog);
  717. *p++ = cpu_to_be32(rpcb->r_vers);
  718. *p++ = cpu_to_be32(rpcb->r_prot);
  719. *p = cpu_to_be32(rpcb->r_port);
  720. }
  721. static int rpcb_dec_getport(struct rpc_rqst *req, struct xdr_stream *xdr,
  722. struct rpcbind_args *rpcb)
  723. {
  724. struct rpc_task *task = req->rq_task;
  725. unsigned long port;
  726. __be32 *p;
  727. rpcb->r_port = 0;
  728. p = xdr_inline_decode(xdr, 4);
  729. if (unlikely(p == NULL))
  730. return -EIO;
  731. port = be32_to_cpup(p);
  732. dprintk("RPC: %5u PMAP_%s result: %lu\n", task->tk_pid,
  733. task->tk_msg.rpc_proc->p_name, port);
  734. if (unlikely(port > USHRT_MAX))
  735. return -EIO;
  736. rpcb->r_port = port;
  737. return 0;
  738. }
  739. static int rpcb_dec_set(struct rpc_rqst *req, struct xdr_stream *xdr,
  740. unsigned int *boolp)
  741. {
  742. struct rpc_task *task = req->rq_task;
  743. __be32 *p;
  744. p = xdr_inline_decode(xdr, 4);
  745. if (unlikely(p == NULL))
  746. return -EIO;
  747. *boolp = 0;
  748. if (*p != xdr_zero)
  749. *boolp = 1;
  750. dprintk("RPC: %5u RPCB_%s call %s\n",
  751. task->tk_pid, task->tk_msg.rpc_proc->p_name,
  752. (*boolp ? "succeeded" : "failed"));
  753. return 0;
  754. }
  755. static void encode_rpcb_string(struct xdr_stream *xdr, const char *string,
  756. const u32 maxstrlen)
  757. {
  758. __be32 *p;
  759. u32 len;
  760. len = strlen(string);
  761. BUG_ON(len > maxstrlen);
  762. p = xdr_reserve_space(xdr, 4 + len);
  763. xdr_encode_opaque(p, string, len);
  764. }
  765. static void rpcb_enc_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr,
  766. const struct rpcbind_args *rpcb)
  767. {
  768. struct rpc_task *task = req->rq_task;
  769. __be32 *p;
  770. dprintk("RPC: %5u encoding RPCB_%s call (%u, %u, '%s', '%s')\n",
  771. task->tk_pid, task->tk_msg.rpc_proc->p_name,
  772. rpcb->r_prog, rpcb->r_vers,
  773. rpcb->r_netid, rpcb->r_addr);
  774. p = xdr_reserve_space(xdr, (RPCB_program_sz + RPCB_version_sz) << 2);
  775. *p++ = cpu_to_be32(rpcb->r_prog);
  776. *p = cpu_to_be32(rpcb->r_vers);
  777. encode_rpcb_string(xdr, rpcb->r_netid, RPCBIND_MAXNETIDLEN);
  778. encode_rpcb_string(xdr, rpcb->r_addr, RPCBIND_MAXUADDRLEN);
  779. encode_rpcb_string(xdr, rpcb->r_owner, RPCB_MAXOWNERLEN);
  780. }
  781. static int rpcb_dec_getaddr(struct rpc_rqst *req, struct xdr_stream *xdr,
  782. struct rpcbind_args *rpcb)
  783. {
  784. struct sockaddr_storage address;
  785. struct sockaddr *sap = (struct sockaddr *)&address;
  786. struct rpc_task *task = req->rq_task;
  787. __be32 *p;
  788. u32 len;
  789. rpcb->r_port = 0;
  790. p = xdr_inline_decode(xdr, 4);
  791. if (unlikely(p == NULL))
  792. goto out_fail;
  793. len = be32_to_cpup(p);
  794. /*
  795. * If the returned universal address is a null string,
  796. * the requested RPC service was not registered.
  797. */
  798. if (len == 0) {
  799. dprintk("RPC: %5u RPCB reply: program not registered\n",
  800. task->tk_pid);
  801. return 0;
  802. }
  803. if (unlikely(len > RPCBIND_MAXUADDRLEN))
  804. goto out_fail;
  805. p = xdr_inline_decode(xdr, len);
  806. if (unlikely(p == NULL))
  807. goto out_fail;
  808. dprintk("RPC: %5u RPCB_%s reply: %s\n", task->tk_pid,
  809. task->tk_msg.rpc_proc->p_name, (char *)p);
  810. if (rpc_uaddr2sockaddr((char *)p, len, sap, sizeof(address)) == 0)
  811. goto out_fail;
  812. rpcb->r_port = rpc_get_port(sap);
  813. return 0;
  814. out_fail:
  815. dprintk("RPC: %5u malformed RPCB_%s reply\n",
  816. task->tk_pid, task->tk_msg.rpc_proc->p_name);
  817. return -EIO;
  818. }
  819. /*
  820. * Not all rpcbind procedures described in RFC 1833 are implemented
  821. * since the Linux kernel RPC code requires only these.
  822. */
  823. static struct rpc_procinfo rpcb_procedures2[] = {
  824. [RPCBPROC_SET] = {
  825. .p_proc = RPCBPROC_SET,
  826. .p_encode = (kxdreproc_t)rpcb_enc_mapping,
  827. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  828. .p_arglen = RPCB_mappingargs_sz,
  829. .p_replen = RPCB_setres_sz,
  830. .p_statidx = RPCBPROC_SET,
  831. .p_timer = 0,
  832. .p_name = "SET",
  833. },
  834. [RPCBPROC_UNSET] = {
  835. .p_proc = RPCBPROC_UNSET,
  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_UNSET,
  841. .p_timer = 0,
  842. .p_name = "UNSET",
  843. },
  844. [RPCBPROC_GETPORT] = {
  845. .p_proc = RPCBPROC_GETPORT,
  846. .p_encode = (kxdreproc_t)rpcb_enc_mapping,
  847. .p_decode = (kxdrdproc_t)rpcb_dec_getport,
  848. .p_arglen = RPCB_mappingargs_sz,
  849. .p_replen = RPCB_getportres_sz,
  850. .p_statidx = RPCBPROC_GETPORT,
  851. .p_timer = 0,
  852. .p_name = "GETPORT",
  853. },
  854. };
  855. static struct rpc_procinfo rpcb_procedures3[] = {
  856. [RPCBPROC_SET] = {
  857. .p_proc = RPCBPROC_SET,
  858. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  859. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  860. .p_arglen = RPCB_getaddrargs_sz,
  861. .p_replen = RPCB_setres_sz,
  862. .p_statidx = RPCBPROC_SET,
  863. .p_timer = 0,
  864. .p_name = "SET",
  865. },
  866. [RPCBPROC_UNSET] = {
  867. .p_proc = RPCBPROC_UNSET,
  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_UNSET,
  873. .p_timer = 0,
  874. .p_name = "UNSET",
  875. },
  876. [RPCBPROC_GETADDR] = {
  877. .p_proc = RPCBPROC_GETADDR,
  878. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  879. .p_decode = (kxdrdproc_t)rpcb_dec_getaddr,
  880. .p_arglen = RPCB_getaddrargs_sz,
  881. .p_replen = RPCB_getaddrres_sz,
  882. .p_statidx = RPCBPROC_GETADDR,
  883. .p_timer = 0,
  884. .p_name = "GETADDR",
  885. },
  886. };
  887. static struct rpc_procinfo rpcb_procedures4[] = {
  888. [RPCBPROC_SET] = {
  889. .p_proc = RPCBPROC_SET,
  890. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  891. .p_decode = (kxdrdproc_t)rpcb_dec_set,
  892. .p_arglen = RPCB_getaddrargs_sz,
  893. .p_replen = RPCB_setres_sz,
  894. .p_statidx = RPCBPROC_SET,
  895. .p_timer = 0,
  896. .p_name = "SET",
  897. },
  898. [RPCBPROC_UNSET] = {
  899. .p_proc = RPCBPROC_UNSET,
  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_UNSET,
  905. .p_timer = 0,
  906. .p_name = "UNSET",
  907. },
  908. [RPCBPROC_GETADDR] = {
  909. .p_proc = RPCBPROC_GETADDR,
  910. .p_encode = (kxdreproc_t)rpcb_enc_getaddr,
  911. .p_decode = (kxdrdproc_t)rpcb_dec_getaddr,
  912. .p_arglen = RPCB_getaddrargs_sz,
  913. .p_replen = RPCB_getaddrres_sz,
  914. .p_statidx = RPCBPROC_GETADDR,
  915. .p_timer = 0,
  916. .p_name = "GETADDR",
  917. },
  918. };
  919. static struct rpcb_info rpcb_next_version[] = {
  920. {
  921. .rpc_vers = RPCBVERS_2,
  922. .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
  923. },
  924. {
  925. .rpc_proc = NULL,
  926. },
  927. };
  928. static struct rpcb_info rpcb_next_version6[] = {
  929. {
  930. .rpc_vers = RPCBVERS_4,
  931. .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR],
  932. },
  933. {
  934. .rpc_vers = RPCBVERS_3,
  935. .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR],
  936. },
  937. {
  938. .rpc_proc = NULL,
  939. },
  940. };
  941. static struct rpc_version rpcb_version2 = {
  942. .number = RPCBVERS_2,
  943. .nrprocs = ARRAY_SIZE(rpcb_procedures2),
  944. .procs = rpcb_procedures2
  945. };
  946. static struct rpc_version rpcb_version3 = {
  947. .number = RPCBVERS_3,
  948. .nrprocs = ARRAY_SIZE(rpcb_procedures3),
  949. .procs = rpcb_procedures3
  950. };
  951. static struct rpc_version rpcb_version4 = {
  952. .number = RPCBVERS_4,
  953. .nrprocs = ARRAY_SIZE(rpcb_procedures4),
  954. .procs = rpcb_procedures4
  955. };
  956. static struct rpc_version *rpcb_version[] = {
  957. NULL,
  958. NULL,
  959. &rpcb_version2,
  960. &rpcb_version3,
  961. &rpcb_version4
  962. };
  963. static struct rpc_stat rpcb_stats;
  964. static struct rpc_program rpcb_program = {
  965. .name = "rpcbind",
  966. .number = RPCBIND_PROGRAM,
  967. .nrvers = ARRAY_SIZE(rpcb_version),
  968. .version = rpcb_version,
  969. .stats = &rpcb_stats,
  970. };
  971. /**
  972. * cleanup_rpcb_clnt - remove xprtsock's sysctls, unregister
  973. *
  974. */
  975. void cleanup_rpcb_clnt(void)
  976. {
  977. if (rpcb_local_clnt4)
  978. rpc_shutdown_client(rpcb_local_clnt4);
  979. if (rpcb_local_clnt)
  980. rpc_shutdown_client(rpcb_local_clnt);
  981. }