rpcb_clnt.c 28 KB

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