rpcb_clnt.c 28 KB

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