rpcb_clnt.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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/in.h>
  18. #include <linux/in6.h>
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/mutex.h>
  22. #include <linux/slab.h>
  23. #include <net/ipv6.h>
  24. #include <linux/sunrpc/clnt.h>
  25. #include <linux/sunrpc/sched.h>
  26. #include <linux/sunrpc/xprtsock.h>
  27. #ifdef RPC_DEBUG
  28. # define RPCDBG_FACILITY RPCDBG_BIND
  29. #endif
  30. #define RPCBIND_PROGRAM (100000u)
  31. #define RPCBIND_PORT (111u)
  32. #define RPCBVERS_2 (2u)
  33. #define RPCBVERS_3 (3u)
  34. #define RPCBVERS_4 (4u)
  35. enum {
  36. RPCBPROC_NULL,
  37. RPCBPROC_SET,
  38. RPCBPROC_UNSET,
  39. RPCBPROC_GETPORT,
  40. RPCBPROC_GETADDR = 3, /* alias for GETPORT */
  41. RPCBPROC_DUMP,
  42. RPCBPROC_CALLIT,
  43. RPCBPROC_BCAST = 5, /* alias for CALLIT */
  44. RPCBPROC_GETTIME,
  45. RPCBPROC_UADDR2TADDR,
  46. RPCBPROC_TADDR2UADDR,
  47. RPCBPROC_GETVERSADDR,
  48. RPCBPROC_INDIRECT,
  49. RPCBPROC_GETADDRLIST,
  50. RPCBPROC_GETSTAT,
  51. };
  52. #define RPCB_HIGHPROC_2 RPCBPROC_CALLIT
  53. #define RPCB_HIGHPROC_3 RPCBPROC_TADDR2UADDR
  54. #define RPCB_HIGHPROC_4 RPCBPROC_GETSTAT
  55. /*
  56. * r_owner
  57. *
  58. * The "owner" is allowed to unset a service in the rpcbind database.
  59. *
  60. * For AF_LOCAL SET/UNSET requests, rpcbind treats this string as a
  61. * UID which it maps to a local user name via a password lookup.
  62. * In all other cases it is ignored.
  63. *
  64. * For SET/UNSET requests, user space provides a value, even for
  65. * network requests, and GETADDR uses an empty string. We follow
  66. * those precedents here.
  67. */
  68. #define RPCB_OWNER_STRING "0"
  69. #define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
  70. /*
  71. * XDR data type sizes
  72. */
  73. #define RPCB_program_sz (1)
  74. #define RPCB_version_sz (1)
  75. #define RPCB_protocol_sz (1)
  76. #define RPCB_port_sz (1)
  77. #define RPCB_boolean_sz (1)
  78. #define RPCB_netid_sz (1 + XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
  79. #define RPCB_addr_sz (1 + XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
  80. #define RPCB_ownerstring_sz (1 + XDR_QUADLEN(RPCB_MAXOWNERLEN))
  81. /*
  82. * XDR argument and result sizes
  83. */
  84. #define RPCB_mappingargs_sz (RPCB_program_sz + RPCB_version_sz + \
  85. RPCB_protocol_sz + RPCB_port_sz)
  86. #define RPCB_getaddrargs_sz (RPCB_program_sz + RPCB_version_sz + \
  87. RPCB_netid_sz + RPCB_addr_sz + \
  88. RPCB_ownerstring_sz)
  89. #define RPCB_getportres_sz RPCB_port_sz
  90. #define RPCB_setres_sz RPCB_boolean_sz
  91. /*
  92. * Note that RFC 1833 does not put any size restrictions on the
  93. * address string returned by the remote rpcbind database.
  94. */
  95. #define RPCB_getaddrres_sz RPCB_addr_sz
  96. static void rpcb_getport_done(struct rpc_task *, void *);
  97. static void rpcb_map_release(void *data);
  98. static struct rpc_program rpcb_program;
  99. static struct rpc_clnt * rpcb_local_clnt;
  100. static struct rpc_clnt * rpcb_local_clnt4;
  101. struct rpcbind_args {
  102. struct rpc_xprt * r_xprt;
  103. u32 r_prog;
  104. u32 r_vers;
  105. u32 r_prot;
  106. unsigned short r_port;
  107. const char * r_netid;
  108. const char * r_addr;
  109. const char * r_owner;
  110. int r_status;
  111. };
  112. static struct rpc_procinfo rpcb_procedures2[];
  113. static struct rpc_procinfo rpcb_procedures3[];
  114. static struct rpc_procinfo rpcb_procedures4[];
  115. struct rpcb_info {
  116. u32 rpc_vers;
  117. struct rpc_procinfo * rpc_proc;
  118. };
  119. static struct rpcb_info rpcb_next_version[];
  120. static struct rpcb_info rpcb_next_version6[];
  121. static const struct rpc_call_ops rpcb_getport_ops = {
  122. .rpc_call_done = rpcb_getport_done,
  123. .rpc_release = rpcb_map_release,
  124. };
  125. static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status)
  126. {
  127. xprt_clear_binding(xprt);
  128. rpc_wake_up_status(&xprt->binding, status);
  129. }
  130. static void rpcb_map_release(void *data)
  131. {
  132. struct rpcbind_args *map = data;
  133. rpcb_wake_rpcbind_waiters(map->r_xprt, map->r_status);
  134. xprt_put(map->r_xprt);
  135. kfree(map->r_addr);
  136. kfree(map);
  137. }
  138. static const struct sockaddr_in rpcb_inaddr_loopback = {
  139. .sin_family = AF_INET,
  140. .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
  141. .sin_port = htons(RPCBIND_PORT),
  142. };
  143. static DEFINE_MUTEX(rpcb_create_local_mutex);
  144. /*
  145. * Returns zero on success, otherwise a negative errno value
  146. * is returned.
  147. */
  148. static int rpcb_create_local(void)
  149. {
  150. struct rpc_create_args args = {
  151. .net = &init_net,
  152. .protocol = XPRT_TRANSPORT_TCP,
  153. .address = (struct sockaddr *)&rpcb_inaddr_loopback,
  154. .addrsize = sizeof(rpcb_inaddr_loopback),
  155. .servername = "localhost",
  156. .program = &rpcb_program,
  157. .version = RPCBVERS_2,
  158. .authflavor = RPC_AUTH_UNIX,
  159. .flags = RPC_CLNT_CREATE_NOPING,
  160. };
  161. struct rpc_clnt *clnt, *clnt4;
  162. int result = 0;
  163. if (rpcb_local_clnt)
  164. return result;
  165. mutex_lock(&rpcb_create_local_mutex);
  166. if (rpcb_local_clnt)
  167. goto out;
  168. clnt = rpc_create(&args);
  169. if (IS_ERR(clnt)) {
  170. dprintk("RPC: failed to create local rpcbind "
  171. "client (errno %ld).\n", PTR_ERR(clnt));
  172. result = -PTR_ERR(clnt);
  173. goto out;
  174. }
  175. /*
  176. * This results in an RPC ping. On systems running portmapper,
  177. * the v4 ping will fail. Proceed anyway, but disallow rpcb
  178. * v4 upcalls.
  179. */
  180. clnt4 = rpc_bind_new_program(clnt, &rpcb_program, RPCBVERS_4);
  181. if (IS_ERR(clnt4)) {
  182. dprintk("RPC: failed to create local rpcbind v4 "
  183. "cleint (errno %ld).\n", PTR_ERR(clnt4));
  184. clnt4 = NULL;
  185. }
  186. rpcb_local_clnt = clnt;
  187. rpcb_local_clnt4 = clnt4;
  188. out:
  189. mutex_unlock(&rpcb_create_local_mutex);
  190. return result;
  191. }
  192. static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
  193. size_t salen, int proto, u32 version)
  194. {
  195. struct rpc_create_args args = {
  196. .net = &init_net,
  197. .protocol = proto,
  198. .address = srvaddr,
  199. .addrsize = salen,
  200. .servername = hostname,
  201. .program = &rpcb_program,
  202. .version = version,
  203. .authflavor = RPC_AUTH_UNIX,
  204. .flags = (RPC_CLNT_CREATE_NOPING |
  205. RPC_CLNT_CREATE_NONPRIVPORT),
  206. };
  207. switch (srvaddr->sa_family) {
  208. case AF_INET:
  209. ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
  210. break;
  211. case AF_INET6:
  212. ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
  213. break;
  214. default:
  215. return NULL;
  216. }
  217. return rpc_create(&args);
  218. }
  219. static int rpcb_register_call(struct rpc_clnt *clnt, struct rpc_message *msg)
  220. {
  221. int result, error = 0;
  222. msg->rpc_resp = &result;
  223. error = rpc_call_sync(clnt, msg, RPC_TASK_SOFTCONN);
  224. if (error < 0) {
  225. dprintk("RPC: failed to contact local rpcbind "
  226. "server (errno %d).\n", -error);
  227. return error;
  228. }
  229. if (!result)
  230. return -EACCES;
  231. return 0;
  232. }
  233. /**
  234. * rpcb_register - set or unset a port registration with the local rpcbind svc
  235. * @prog: RPC program number to bind
  236. * @vers: RPC version number to bind
  237. * @prot: transport protocol to register
  238. * @port: port value to register
  239. *
  240. * Returns zero if the registration request was dispatched successfully
  241. * and the rpcbind daemon returned success. Otherwise, returns an errno
  242. * value that reflects the nature of the error (request could not be
  243. * dispatched, timed out, or rpcbind returned an error).
  244. *
  245. * RPC services invoke this function to advertise their contact
  246. * information via the system's rpcbind daemon. RPC services
  247. * invoke this function once for each [program, version, transport]
  248. * tuple they wish to advertise.
  249. *
  250. * Callers may also unregister RPC services that are no longer
  251. * available by setting the passed-in port to zero. This removes
  252. * all registered transports for [program, version] from the local
  253. * rpcbind database.
  254. *
  255. * This function uses rpcbind protocol version 2 to contact the
  256. * local rpcbind daemon.
  257. *
  258. * Registration works over both AF_INET and AF_INET6, and services
  259. * registered via this function are advertised as available for any
  260. * address. If the local rpcbind daemon is listening on AF_INET6,
  261. * services registered via this function will be advertised on
  262. * IN6ADDR_ANY (ie available for all AF_INET and AF_INET6
  263. * addresses).
  264. */
  265. int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port)
  266. {
  267. struct rpcbind_args map = {
  268. .r_prog = prog,
  269. .r_vers = vers,
  270. .r_prot = prot,
  271. .r_port = port,
  272. };
  273. struct rpc_message msg = {
  274. .rpc_argp = &map,
  275. };
  276. int error;
  277. error = rpcb_create_local();
  278. if (error)
  279. return error;
  280. dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
  281. "rpcbind\n", (port ? "" : "un"),
  282. prog, vers, prot, port);
  283. msg.rpc_proc = &rpcb_procedures2[RPCBPROC_UNSET];
  284. if (port)
  285. msg.rpc_proc = &rpcb_procedures2[RPCBPROC_SET];
  286. return rpcb_register_call(rpcb_local_clnt, &msg);
  287. }
  288. /*
  289. * Fill in AF_INET family-specific arguments to register
  290. */
  291. static int rpcb_register_inet4(const struct sockaddr *sap,
  292. struct rpc_message *msg)
  293. {
  294. const struct sockaddr_in *sin = (const struct sockaddr_in *)sap;
  295. struct rpcbind_args *map = msg->rpc_argp;
  296. unsigned short port = ntohs(sin->sin_port);
  297. int result;
  298. map->r_addr = rpc_sockaddr2uaddr(sap);
  299. dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
  300. "local rpcbind\n", (port ? "" : "un"),
  301. map->r_prog, map->r_vers,
  302. map->r_addr, map->r_netid);
  303. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  304. if (port)
  305. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
  306. result = rpcb_register_call(rpcb_local_clnt4, msg);
  307. kfree(map->r_addr);
  308. return result;
  309. }
  310. /*
  311. * Fill in AF_INET6 family-specific arguments to register
  312. */
  313. static int rpcb_register_inet6(const struct sockaddr *sap,
  314. struct rpc_message *msg)
  315. {
  316. const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sap;
  317. struct rpcbind_args *map = msg->rpc_argp;
  318. unsigned short port = ntohs(sin6->sin6_port);
  319. int result;
  320. map->r_addr = rpc_sockaddr2uaddr(sap);
  321. dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
  322. "local rpcbind\n", (port ? "" : "un"),
  323. map->r_prog, map->r_vers,
  324. map->r_addr, map->r_netid);
  325. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  326. if (port)
  327. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
  328. result = rpcb_register_call(rpcb_local_clnt4, msg);
  329. kfree(map->r_addr);
  330. return result;
  331. }
  332. static int rpcb_unregister_all_protofamilies(struct rpc_message *msg)
  333. {
  334. struct rpcbind_args *map = msg->rpc_argp;
  335. dprintk("RPC: unregistering [%u, %u, '%s'] with "
  336. "local rpcbind\n",
  337. map->r_prog, map->r_vers, map->r_netid);
  338. map->r_addr = "";
  339. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  340. return rpcb_register_call(rpcb_local_clnt4, msg);
  341. }
  342. /**
  343. * rpcb_v4_register - set or unset a port registration with the local rpcbind
  344. * @program: RPC program number of service to (un)register
  345. * @version: RPC version number of service to (un)register
  346. * @address: address family, IP address, and port to (un)register
  347. * @netid: netid of transport protocol to (un)register
  348. *
  349. * Returns zero if the registration request was dispatched successfully
  350. * and the rpcbind daemon returned success. Otherwise, returns an errno
  351. * value that reflects the nature of the error (request could not be
  352. * dispatched, timed out, or rpcbind returned an error).
  353. *
  354. * RPC services invoke this function to advertise their contact
  355. * information via the system's rpcbind daemon. RPC services
  356. * invoke this function once for each [program, version, address,
  357. * netid] tuple they wish to advertise.
  358. *
  359. * Callers may also unregister RPC services that are registered at a
  360. * specific address by setting the port number in @address to zero.
  361. * They may unregister all registered protocol families at once for
  362. * a service by passing a NULL @address argument. If @netid is ""
  363. * then all netids for [program, version, address] are unregistered.
  364. *
  365. * This function uses rpcbind protocol version 4 to contact the
  366. * local rpcbind daemon. The local rpcbind daemon must support
  367. * version 4 of the rpcbind protocol in order for these functions
  368. * to register a service successfully.
  369. *
  370. * Supported netids include "udp" and "tcp" for UDP and TCP over
  371. * IPv4, and "udp6" and "tcp6" for UDP and TCP over IPv6,
  372. * respectively.
  373. *
  374. * The contents of @address determine the address family and the
  375. * port to be registered. The usual practice is to pass INADDR_ANY
  376. * as the raw address, but specifying a non-zero address is also
  377. * supported by this API if the caller wishes to advertise an RPC
  378. * service on a specific network interface.
  379. *
  380. * Note that passing in INADDR_ANY does not create the same service
  381. * registration as IN6ADDR_ANY. The former advertises an RPC
  382. * service on any IPv4 address, but not on IPv6. The latter
  383. * advertises the service on all IPv4 and IPv6 addresses.
  384. */
  385. int rpcb_v4_register(const u32 program, const u32 version,
  386. const struct sockaddr *address, const char *netid)
  387. {
  388. struct rpcbind_args map = {
  389. .r_prog = program,
  390. .r_vers = version,
  391. .r_netid = netid,
  392. .r_owner = RPCB_OWNER_STRING,
  393. };
  394. struct rpc_message msg = {
  395. .rpc_argp = &map,
  396. };
  397. int error;
  398. error = rpcb_create_local();
  399. if (error)
  400. return error;
  401. if (rpcb_local_clnt4 == NULL)
  402. return -EPROTONOSUPPORT;
  403. if (address == NULL)
  404. return rpcb_unregister_all_protofamilies(&msg);
  405. switch (address->sa_family) {
  406. case AF_INET:
  407. return rpcb_register_inet4(address, &msg);
  408. case AF_INET6:
  409. return rpcb_register_inet6(address, &msg);
  410. }
  411. return -EAFNOSUPPORT;
  412. }
  413. /**
  414. * rpcb_getport_sync - obtain the port for an RPC service on a given host
  415. * @sin: address of remote peer
  416. * @prog: RPC program number to bind
  417. * @vers: RPC version number to bind
  418. * @prot: transport protocol to use to make this request
  419. *
  420. * Return value is the requested advertised port number,
  421. * or a negative errno value.
  422. *
  423. * Called from outside the RPC client in a synchronous task context.
  424. * Uses default timeout parameters specified by underlying transport.
  425. *
  426. * XXX: Needs to support IPv6
  427. */
  428. int rpcb_getport_sync(struct sockaddr_in *sin, u32 prog, u32 vers, int prot)
  429. {
  430. struct rpcbind_args map = {
  431. .r_prog = prog,
  432. .r_vers = vers,
  433. .r_prot = prot,
  434. .r_port = 0,
  435. };
  436. struct rpc_message msg = {
  437. .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
  438. .rpc_argp = &map,
  439. .rpc_resp = &map,
  440. };
  441. struct rpc_clnt *rpcb_clnt;
  442. int status;
  443. dprintk("RPC: %s(%pI4, %u, %u, %d)\n",
  444. __func__, &sin->sin_addr.s_addr, prog, vers, prot);
  445. rpcb_clnt = rpcb_create(NULL, (struct sockaddr *)sin,
  446. sizeof(*sin), prot, RPCBVERS_2);
  447. if (IS_ERR(rpcb_clnt))
  448. return PTR_ERR(rpcb_clnt);
  449. status = rpc_call_sync(rpcb_clnt, &msg, 0);
  450. rpc_shutdown_client(rpcb_clnt);
  451. if (status >= 0) {
  452. if (map.r_port != 0)
  453. return map.r_port;
  454. status = -EACCES;
  455. }
  456. return status;
  457. }
  458. EXPORT_SYMBOL_GPL(rpcb_getport_sync);
  459. static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt, struct rpcbind_args *map, struct rpc_procinfo *proc)
  460. {
  461. struct rpc_message msg = {
  462. .rpc_proc = proc,
  463. .rpc_argp = map,
  464. .rpc_resp = map,
  465. };
  466. struct rpc_task_setup task_setup_data = {
  467. .rpc_client = rpcb_clnt,
  468. .rpc_message = &msg,
  469. .callback_ops = &rpcb_getport_ops,
  470. .callback_data = map,
  471. .flags = RPC_TASK_ASYNC | RPC_TASK_SOFTCONN,
  472. };
  473. return rpc_run_task(&task_setup_data);
  474. }
  475. /*
  476. * In the case where rpc clients have been cloned, we want to make
  477. * sure that we use the program number/version etc of the actual
  478. * owner of the xprt. To do so, we walk back up the tree of parents
  479. * to find whoever created the transport and/or whoever has the
  480. * autobind flag set.
  481. */
  482. static struct rpc_clnt *rpcb_find_transport_owner(struct rpc_clnt *clnt)
  483. {
  484. struct rpc_clnt *parent = clnt->cl_parent;
  485. while (parent != clnt) {
  486. if (parent->cl_xprt != clnt->cl_xprt)
  487. break;
  488. if (clnt->cl_autobind)
  489. break;
  490. clnt = parent;
  491. parent = parent->cl_parent;
  492. }
  493. return clnt;
  494. }
  495. /**
  496. * rpcb_getport_async - obtain the port for a given RPC service on a given host
  497. * @task: task that is waiting for portmapper request
  498. *
  499. * This one can be called for an ongoing RPC request, and can be used in
  500. * an async (rpciod) context.
  501. */
  502. void rpcb_getport_async(struct rpc_task *task)
  503. {
  504. struct rpc_clnt *clnt;
  505. struct rpc_procinfo *proc;
  506. u32 bind_version;
  507. struct rpc_xprt *xprt;
  508. struct rpc_clnt *rpcb_clnt;
  509. static struct rpcbind_args *map;
  510. struct rpc_task *child;
  511. struct sockaddr_storage addr;
  512. struct sockaddr *sap = (struct sockaddr *)&addr;
  513. size_t salen;
  514. int status;
  515. clnt = rpcb_find_transport_owner(task->tk_client);
  516. xprt = clnt->cl_xprt;
  517. dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
  518. task->tk_pid, __func__,
  519. clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot);
  520. /* Put self on the wait queue to ensure we get notified if
  521. * some other task is already attempting to bind the port */
  522. rpc_sleep_on(&xprt->binding, task, NULL);
  523. if (xprt_test_and_set_binding(xprt)) {
  524. dprintk("RPC: %5u %s: waiting for another binder\n",
  525. task->tk_pid, __func__);
  526. return;
  527. }
  528. /* Someone else may have bound if we slept */
  529. if (xprt_bound(xprt)) {
  530. status = 0;
  531. dprintk("RPC: %5u %s: already bound\n",
  532. task->tk_pid, __func__);
  533. goto bailout_nofree;
  534. }
  535. /* Parent transport's destination address */
  536. salen = rpc_peeraddr(clnt, sap, sizeof(addr));
  537. /* Don't ever use rpcbind v2 for AF_INET6 requests */
  538. switch (sap->sa_family) {
  539. case AF_INET:
  540. proc = rpcb_next_version[xprt->bind_index].rpc_proc;
  541. bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
  542. break;
  543. case AF_INET6:
  544. proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
  545. bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
  546. break;
  547. default:
  548. status = -EAFNOSUPPORT;
  549. dprintk("RPC: %5u %s: bad address family\n",
  550. task->tk_pid, __func__);
  551. goto bailout_nofree;
  552. }
  553. if (proc == NULL) {
  554. xprt->bind_index = 0;
  555. status = -EPFNOSUPPORT;
  556. dprintk("RPC: %5u %s: no more getport versions available\n",
  557. task->tk_pid, __func__);
  558. goto bailout_nofree;
  559. }
  560. dprintk("RPC: %5u %s: trying rpcbind version %u\n",
  561. task->tk_pid, __func__, bind_version);
  562. rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot,
  563. bind_version);
  564. if (IS_ERR(rpcb_clnt)) {
  565. status = PTR_ERR(rpcb_clnt);
  566. dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
  567. task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
  568. goto bailout_nofree;
  569. }
  570. map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
  571. if (!map) {
  572. status = -ENOMEM;
  573. dprintk("RPC: %5u %s: no memory available\n",
  574. task->tk_pid, __func__);
  575. goto bailout_release_client;
  576. }
  577. map->r_prog = clnt->cl_prog;
  578. map->r_vers = clnt->cl_vers;
  579. map->r_prot = xprt->prot;
  580. map->r_port = 0;
  581. map->r_xprt = xprt_get(xprt);
  582. map->r_status = -EIO;
  583. switch (bind_version) {
  584. case RPCBVERS_4:
  585. case RPCBVERS_3:
  586. map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
  587. map->r_addr = rpc_sockaddr2uaddr(sap);
  588. map->r_owner = "";
  589. break;
  590. case RPCBVERS_2:
  591. map->r_addr = NULL;
  592. break;
  593. default:
  594. BUG();
  595. }
  596. child = rpcb_call_async(rpcb_clnt, map, proc);
  597. rpc_release_client(rpcb_clnt);
  598. if (IS_ERR(child)) {
  599. /* rpcb_map_release() has freed the arguments */
  600. dprintk("RPC: %5u %s: rpc_run_task failed\n",
  601. task->tk_pid, __func__);
  602. return;
  603. }
  604. xprt->stat.bind_count++;
  605. rpc_put_task(child);
  606. return;
  607. bailout_release_client:
  608. rpc_release_client(rpcb_clnt);
  609. bailout_nofree:
  610. rpcb_wake_rpcbind_waiters(xprt, status);
  611. task->tk_status = status;
  612. }
  613. EXPORT_SYMBOL_GPL(rpcb_getport_async);
  614. /*
  615. * Rpcbind child task calls this callback via tk_exit.
  616. */
  617. static void rpcb_getport_done(struct rpc_task *child, void *data)
  618. {
  619. struct rpcbind_args *map = data;
  620. struct rpc_xprt *xprt = map->r_xprt;
  621. int status = child->tk_status;
  622. /* Garbage reply: retry with a lesser rpcbind version */
  623. if (status == -EIO)
  624. status = -EPROTONOSUPPORT;
  625. /* rpcbind server doesn't support this rpcbind protocol version */
  626. if (status == -EPROTONOSUPPORT)
  627. xprt->bind_index++;
  628. if (status < 0) {
  629. /* rpcbind server not available on remote host? */
  630. xprt->ops->set_port(xprt, 0);
  631. } else if (map->r_port == 0) {
  632. /* Requested RPC service wasn't registered on remote host */
  633. xprt->ops->set_port(xprt, 0);
  634. status = -EACCES;
  635. } else {
  636. /* Succeeded */
  637. xprt->ops->set_port(xprt, map->r_port);
  638. xprt_set_bound(xprt);
  639. status = 0;
  640. }
  641. dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
  642. child->tk_pid, status, map->r_port);
  643. map->r_status = status;
  644. }
  645. /*
  646. * XDR functions for rpcbind
  647. */
  648. static int rpcb_enc_mapping(struct rpc_rqst *req, __be32 *p,
  649. const struct rpcbind_args *rpcb)
  650. {
  651. struct rpc_task *task = req->rq_task;
  652. struct xdr_stream xdr;
  653. dprintk("RPC: %5u encoding PMAP_%s call (%u, %u, %d, %u)\n",
  654. task->tk_pid, task->tk_msg.rpc_proc->p_name,
  655. rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
  656. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  657. p = xdr_reserve_space(&xdr, sizeof(__be32) * RPCB_mappingargs_sz);
  658. if (unlikely(p == NULL))
  659. return -EIO;
  660. *p++ = htonl(rpcb->r_prog);
  661. *p++ = htonl(rpcb->r_vers);
  662. *p++ = htonl(rpcb->r_prot);
  663. *p = htonl(rpcb->r_port);
  664. return 0;
  665. }
  666. static int rpcb_dec_getport(struct rpc_rqst *req, __be32 *p,
  667. struct rpcbind_args *rpcb)
  668. {
  669. struct rpc_task *task = req->rq_task;
  670. struct xdr_stream xdr;
  671. unsigned long port;
  672. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  673. rpcb->r_port = 0;
  674. p = xdr_inline_decode(&xdr, sizeof(__be32));
  675. if (unlikely(p == NULL))
  676. return -EIO;
  677. port = ntohl(*p);
  678. dprintk("RPC: %5u PMAP_%s result: %lu\n", task->tk_pid,
  679. task->tk_msg.rpc_proc->p_name, port);
  680. if (unlikely(port > USHRT_MAX))
  681. return -EIO;
  682. rpcb->r_port = port;
  683. return 0;
  684. }
  685. static int rpcb_dec_set(struct rpc_rqst *req, __be32 *p,
  686. unsigned int *boolp)
  687. {
  688. struct rpc_task *task = req->rq_task;
  689. struct xdr_stream xdr;
  690. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  691. p = xdr_inline_decode(&xdr, sizeof(__be32));
  692. if (unlikely(p == NULL))
  693. return -EIO;
  694. *boolp = 0;
  695. if (*p)
  696. *boolp = 1;
  697. dprintk("RPC: %5u RPCB_%s call %s\n",
  698. task->tk_pid, task->tk_msg.rpc_proc->p_name,
  699. (*boolp ? "succeeded" : "failed"));
  700. return 0;
  701. }
  702. static int encode_rpcb_string(struct xdr_stream *xdr, const char *string,
  703. const u32 maxstrlen)
  704. {
  705. u32 len;
  706. __be32 *p;
  707. if (unlikely(string == NULL))
  708. return -EIO;
  709. len = strlen(string);
  710. if (unlikely(len > maxstrlen))
  711. return -EIO;
  712. p = xdr_reserve_space(xdr, sizeof(__be32) + len);
  713. if (unlikely(p == NULL))
  714. return -EIO;
  715. xdr_encode_opaque(p, string, len);
  716. return 0;
  717. }
  718. static int rpcb_enc_getaddr(struct rpc_rqst *req, __be32 *p,
  719. const struct rpcbind_args *rpcb)
  720. {
  721. struct rpc_task *task = req->rq_task;
  722. struct xdr_stream xdr;
  723. dprintk("RPC: %5u encoding RPCB_%s call (%u, %u, '%s', '%s')\n",
  724. task->tk_pid, task->tk_msg.rpc_proc->p_name,
  725. rpcb->r_prog, rpcb->r_vers,
  726. rpcb->r_netid, rpcb->r_addr);
  727. xdr_init_encode(&xdr, &req->rq_snd_buf, p);
  728. p = xdr_reserve_space(&xdr,
  729. sizeof(__be32) * (RPCB_program_sz + RPCB_version_sz));
  730. if (unlikely(p == NULL))
  731. return -EIO;
  732. *p++ = htonl(rpcb->r_prog);
  733. *p = htonl(rpcb->r_vers);
  734. if (encode_rpcb_string(&xdr, rpcb->r_netid, RPCBIND_MAXNETIDLEN))
  735. return -EIO;
  736. if (encode_rpcb_string(&xdr, rpcb->r_addr, RPCBIND_MAXUADDRLEN))
  737. return -EIO;
  738. if (encode_rpcb_string(&xdr, rpcb->r_owner, RPCB_MAXOWNERLEN))
  739. return -EIO;
  740. return 0;
  741. }
  742. static int rpcb_dec_getaddr(struct rpc_rqst *req, __be32 *p,
  743. struct rpcbind_args *rpcb)
  744. {
  745. struct sockaddr_storage address;
  746. struct sockaddr *sap = (struct sockaddr *)&address;
  747. struct rpc_task *task = req->rq_task;
  748. struct xdr_stream xdr;
  749. u32 len;
  750. rpcb->r_port = 0;
  751. xdr_init_decode(&xdr, &req->rq_rcv_buf, p);
  752. p = xdr_inline_decode(&xdr, sizeof(__be32));
  753. if (unlikely(p == NULL))
  754. goto out_fail;
  755. len = ntohl(*p);
  756. /*
  757. * If the returned universal address is a null string,
  758. * the requested RPC service was not registered.
  759. */
  760. if (len == 0) {
  761. dprintk("RPC: %5u RPCB reply: program not registered\n",
  762. task->tk_pid);
  763. return 0;
  764. }
  765. if (unlikely(len > RPCBIND_MAXUADDRLEN))
  766. goto out_fail;
  767. p = xdr_inline_decode(&xdr, len);
  768. if (unlikely(p == NULL))
  769. goto out_fail;
  770. dprintk("RPC: %5u RPCB_%s reply: %s\n", task->tk_pid,
  771. task->tk_msg.rpc_proc->p_name, (char *)p);
  772. if (rpc_uaddr2sockaddr((char *)p, len, sap, sizeof(address)) == 0)
  773. goto out_fail;
  774. rpcb->r_port = rpc_get_port(sap);
  775. return 0;
  776. out_fail:
  777. dprintk("RPC: %5u malformed RPCB_%s reply\n",
  778. task->tk_pid, task->tk_msg.rpc_proc->p_name);
  779. return -EIO;
  780. }
  781. /*
  782. * Not all rpcbind procedures described in RFC 1833 are implemented
  783. * since the Linux kernel RPC code requires only these.
  784. */
  785. static struct rpc_procinfo rpcb_procedures2[] = {
  786. [RPCBPROC_SET] = {
  787. .p_proc = RPCBPROC_SET,
  788. .p_encode = (kxdrproc_t)rpcb_enc_mapping,
  789. .p_decode = (kxdrproc_t)rpcb_dec_set,
  790. .p_arglen = RPCB_mappingargs_sz,
  791. .p_replen = RPCB_setres_sz,
  792. .p_statidx = RPCBPROC_SET,
  793. .p_timer = 0,
  794. .p_name = "SET",
  795. },
  796. [RPCBPROC_UNSET] = {
  797. .p_proc = RPCBPROC_UNSET,
  798. .p_encode = (kxdrproc_t)rpcb_enc_mapping,
  799. .p_decode = (kxdrproc_t)rpcb_dec_set,
  800. .p_arglen = RPCB_mappingargs_sz,
  801. .p_replen = RPCB_setres_sz,
  802. .p_statidx = RPCBPROC_UNSET,
  803. .p_timer = 0,
  804. .p_name = "UNSET",
  805. },
  806. [RPCBPROC_GETPORT] = {
  807. .p_proc = RPCBPROC_GETPORT,
  808. .p_encode = (kxdrproc_t)rpcb_enc_mapping,
  809. .p_decode = (kxdrproc_t)rpcb_dec_getport,
  810. .p_arglen = RPCB_mappingargs_sz,
  811. .p_replen = RPCB_getportres_sz,
  812. .p_statidx = RPCBPROC_GETPORT,
  813. .p_timer = 0,
  814. .p_name = "GETPORT",
  815. },
  816. };
  817. static struct rpc_procinfo rpcb_procedures3[] = {
  818. [RPCBPROC_SET] = {
  819. .p_proc = RPCBPROC_SET,
  820. .p_encode = (kxdrproc_t)rpcb_enc_getaddr,
  821. .p_decode = (kxdrproc_t)rpcb_dec_set,
  822. .p_arglen = RPCB_getaddrargs_sz,
  823. .p_replen = RPCB_setres_sz,
  824. .p_statidx = RPCBPROC_SET,
  825. .p_timer = 0,
  826. .p_name = "SET",
  827. },
  828. [RPCBPROC_UNSET] = {
  829. .p_proc = RPCBPROC_UNSET,
  830. .p_encode = (kxdrproc_t)rpcb_enc_getaddr,
  831. .p_decode = (kxdrproc_t)rpcb_dec_set,
  832. .p_arglen = RPCB_getaddrargs_sz,
  833. .p_replen = RPCB_setres_sz,
  834. .p_statidx = RPCBPROC_UNSET,
  835. .p_timer = 0,
  836. .p_name = "UNSET",
  837. },
  838. [RPCBPROC_GETADDR] = {
  839. .p_proc = RPCBPROC_GETADDR,
  840. .p_encode = (kxdrproc_t)rpcb_enc_getaddr,
  841. .p_decode = (kxdrproc_t)rpcb_dec_getaddr,
  842. .p_arglen = RPCB_getaddrargs_sz,
  843. .p_replen = RPCB_getaddrres_sz,
  844. .p_statidx = RPCBPROC_GETADDR,
  845. .p_timer = 0,
  846. .p_name = "GETADDR",
  847. },
  848. };
  849. static struct rpc_procinfo rpcb_procedures4[] = {
  850. [RPCBPROC_SET] = {
  851. .p_proc = RPCBPROC_SET,
  852. .p_encode = (kxdrproc_t)rpcb_enc_getaddr,
  853. .p_decode = (kxdrproc_t)rpcb_dec_set,
  854. .p_arglen = RPCB_getaddrargs_sz,
  855. .p_replen = RPCB_setres_sz,
  856. .p_statidx = RPCBPROC_SET,
  857. .p_timer = 0,
  858. .p_name = "SET",
  859. },
  860. [RPCBPROC_UNSET] = {
  861. .p_proc = RPCBPROC_UNSET,
  862. .p_encode = (kxdrproc_t)rpcb_enc_getaddr,
  863. .p_decode = (kxdrproc_t)rpcb_dec_set,
  864. .p_arglen = RPCB_getaddrargs_sz,
  865. .p_replen = RPCB_setres_sz,
  866. .p_statidx = RPCBPROC_UNSET,
  867. .p_timer = 0,
  868. .p_name = "UNSET",
  869. },
  870. [RPCBPROC_GETADDR] = {
  871. .p_proc = RPCBPROC_GETADDR,
  872. .p_encode = (kxdrproc_t)rpcb_enc_getaddr,
  873. .p_decode = (kxdrproc_t)rpcb_dec_getaddr,
  874. .p_arglen = RPCB_getaddrargs_sz,
  875. .p_replen = RPCB_getaddrres_sz,
  876. .p_statidx = RPCBPROC_GETADDR,
  877. .p_timer = 0,
  878. .p_name = "GETADDR",
  879. },
  880. };
  881. static struct rpcb_info rpcb_next_version[] = {
  882. {
  883. .rpc_vers = RPCBVERS_2,
  884. .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
  885. },
  886. {
  887. .rpc_proc = NULL,
  888. },
  889. };
  890. static struct rpcb_info rpcb_next_version6[] = {
  891. {
  892. .rpc_vers = RPCBVERS_4,
  893. .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR],
  894. },
  895. {
  896. .rpc_vers = RPCBVERS_3,
  897. .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR],
  898. },
  899. {
  900. .rpc_proc = NULL,
  901. },
  902. };
  903. static struct rpc_version rpcb_version2 = {
  904. .number = RPCBVERS_2,
  905. .nrprocs = RPCB_HIGHPROC_2,
  906. .procs = rpcb_procedures2
  907. };
  908. static struct rpc_version rpcb_version3 = {
  909. .number = RPCBVERS_3,
  910. .nrprocs = RPCB_HIGHPROC_3,
  911. .procs = rpcb_procedures3
  912. };
  913. static struct rpc_version rpcb_version4 = {
  914. .number = RPCBVERS_4,
  915. .nrprocs = RPCB_HIGHPROC_4,
  916. .procs = rpcb_procedures4
  917. };
  918. static struct rpc_version *rpcb_version[] = {
  919. NULL,
  920. NULL,
  921. &rpcb_version2,
  922. &rpcb_version3,
  923. &rpcb_version4
  924. };
  925. static struct rpc_stat rpcb_stats;
  926. static struct rpc_program rpcb_program = {
  927. .name = "rpcbind",
  928. .number = RPCBIND_PROGRAM,
  929. .nrvers = ARRAY_SIZE(rpcb_version),
  930. .version = rpcb_version,
  931. .stats = &rpcb_stats,
  932. };
  933. /**
  934. * cleanup_rpcb_clnt - remove xprtsock's sysctls, unregister
  935. *
  936. */
  937. void cleanup_rpcb_clnt(void)
  938. {
  939. if (rpcb_local_clnt4)
  940. rpc_shutdown_client(rpcb_local_clnt4);
  941. if (rpcb_local_clnt)
  942. rpc_shutdown_client(rpcb_local_clnt);
  943. }