rpcb_clnt.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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 <net/ipv6.h>
  22. #include <linux/sunrpc/clnt.h>
  23. #include <linux/sunrpc/sched.h>
  24. #include <linux/sunrpc/xprtsock.h>
  25. #ifdef RPC_DEBUG
  26. # define RPCDBG_FACILITY RPCDBG_BIND
  27. #endif
  28. #define RPCBIND_PROGRAM (100000u)
  29. #define RPCBIND_PORT (111u)
  30. #define RPCBVERS_2 (2u)
  31. #define RPCBVERS_3 (3u)
  32. #define RPCBVERS_4 (4u)
  33. enum {
  34. RPCBPROC_NULL,
  35. RPCBPROC_SET,
  36. RPCBPROC_UNSET,
  37. RPCBPROC_GETPORT,
  38. RPCBPROC_GETADDR = 3, /* alias for GETPORT */
  39. RPCBPROC_DUMP,
  40. RPCBPROC_CALLIT,
  41. RPCBPROC_BCAST = 5, /* alias for CALLIT */
  42. RPCBPROC_GETTIME,
  43. RPCBPROC_UADDR2TADDR,
  44. RPCBPROC_TADDR2UADDR,
  45. RPCBPROC_GETVERSADDR,
  46. RPCBPROC_INDIRECT,
  47. RPCBPROC_GETADDRLIST,
  48. RPCBPROC_GETSTAT,
  49. };
  50. #define RPCB_HIGHPROC_2 RPCBPROC_CALLIT
  51. #define RPCB_HIGHPROC_3 RPCBPROC_TADDR2UADDR
  52. #define RPCB_HIGHPROC_4 RPCBPROC_GETSTAT
  53. /*
  54. * r_owner
  55. *
  56. * The "owner" is allowed to unset a service in the rpcbind database.
  57. * We always use the following (arbitrary) fixed string.
  58. */
  59. #define RPCB_OWNER_STRING "rpcb"
  60. #define RPCB_MAXOWNERLEN sizeof(RPCB_OWNER_STRING)
  61. static void rpcb_getport_done(struct rpc_task *, void *);
  62. static void rpcb_map_release(void *data);
  63. static struct rpc_program rpcb_program;
  64. struct rpcbind_args {
  65. struct rpc_xprt * r_xprt;
  66. u32 r_prog;
  67. u32 r_vers;
  68. u32 r_prot;
  69. unsigned short r_port;
  70. const char * r_netid;
  71. const char * r_addr;
  72. const char * r_owner;
  73. int r_status;
  74. };
  75. static struct rpc_procinfo rpcb_procedures2[];
  76. static struct rpc_procinfo rpcb_procedures3[];
  77. static struct rpc_procinfo rpcb_procedures4[];
  78. struct rpcb_info {
  79. u32 rpc_vers;
  80. struct rpc_procinfo * rpc_proc;
  81. };
  82. static struct rpcb_info rpcb_next_version[];
  83. static struct rpcb_info rpcb_next_version6[];
  84. static const struct rpc_call_ops rpcb_getport_ops = {
  85. .rpc_call_done = rpcb_getport_done,
  86. .rpc_release = rpcb_map_release,
  87. };
  88. static void rpcb_wake_rpcbind_waiters(struct rpc_xprt *xprt, int status)
  89. {
  90. xprt_clear_binding(xprt);
  91. rpc_wake_up_status(&xprt->binding, status);
  92. }
  93. static void rpcb_map_release(void *data)
  94. {
  95. struct rpcbind_args *map = data;
  96. rpcb_wake_rpcbind_waiters(map->r_xprt, map->r_status);
  97. xprt_put(map->r_xprt);
  98. kfree(map);
  99. }
  100. static const struct sockaddr_in rpcb_inaddr_loopback = {
  101. .sin_family = AF_INET,
  102. .sin_addr.s_addr = htonl(INADDR_LOOPBACK),
  103. .sin_port = htons(RPCBIND_PORT),
  104. };
  105. static const struct sockaddr_in6 rpcb_in6addr_loopback = {
  106. .sin6_family = AF_INET6,
  107. .sin6_addr = IN6ADDR_LOOPBACK_INIT,
  108. .sin6_port = htons(RPCBIND_PORT),
  109. };
  110. static struct rpc_clnt *rpcb_create_local(struct sockaddr *addr,
  111. size_t addrlen, u32 version)
  112. {
  113. struct rpc_create_args args = {
  114. .protocol = XPRT_TRANSPORT_UDP,
  115. .address = addr,
  116. .addrsize = addrlen,
  117. .servername = "localhost",
  118. .program = &rpcb_program,
  119. .version = version,
  120. .authflavor = RPC_AUTH_UNIX,
  121. .flags = RPC_CLNT_CREATE_NOPING,
  122. };
  123. return rpc_create(&args);
  124. }
  125. static struct rpc_clnt *rpcb_create(char *hostname, struct sockaddr *srvaddr,
  126. size_t salen, int proto, u32 version)
  127. {
  128. struct rpc_create_args args = {
  129. .protocol = proto,
  130. .address = srvaddr,
  131. .addrsize = salen,
  132. .servername = hostname,
  133. .program = &rpcb_program,
  134. .version = version,
  135. .authflavor = RPC_AUTH_UNIX,
  136. .flags = (RPC_CLNT_CREATE_NOPING |
  137. RPC_CLNT_CREATE_NONPRIVPORT),
  138. };
  139. switch (srvaddr->sa_family) {
  140. case AF_INET:
  141. ((struct sockaddr_in *)srvaddr)->sin_port = htons(RPCBIND_PORT);
  142. break;
  143. case AF_INET6:
  144. ((struct sockaddr_in6 *)srvaddr)->sin6_port = htons(RPCBIND_PORT);
  145. break;
  146. default:
  147. return NULL;
  148. }
  149. return rpc_create(&args);
  150. }
  151. static int rpcb_register_call(struct sockaddr *addr, size_t addrlen,
  152. u32 version, struct rpc_message *msg)
  153. {
  154. struct rpc_clnt *rpcb_clnt;
  155. int result, error = 0;
  156. msg->rpc_resp = &result;
  157. rpcb_clnt = rpcb_create_local(addr, addrlen, version);
  158. if (!IS_ERR(rpcb_clnt)) {
  159. error = rpc_call_sync(rpcb_clnt, msg, 0);
  160. rpc_shutdown_client(rpcb_clnt);
  161. } else
  162. error = PTR_ERR(rpcb_clnt);
  163. if (error < 0) {
  164. printk(KERN_WARNING "RPC: failed to contact local rpcbind "
  165. "server (errno %d).\n", -error);
  166. return error;
  167. }
  168. if (!result)
  169. return -EACCES;
  170. return 0;
  171. }
  172. /**
  173. * rpcb_register - set or unset a port registration with the local rpcbind svc
  174. * @prog: RPC program number to bind
  175. * @vers: RPC version number to bind
  176. * @prot: transport protocol to register
  177. * @port: port value to register
  178. *
  179. * Returns zero if the registration request was dispatched successfully
  180. * and the rpcbind daemon returned success. Otherwise, returns an errno
  181. * value that reflects the nature of the error (request could not be
  182. * dispatched, timed out, or rpcbind returned an error).
  183. *
  184. * RPC services invoke this function to advertise their contact
  185. * information via the system's rpcbind daemon. RPC services
  186. * invoke this function once for each [program, version, transport]
  187. * tuple they wish to advertise.
  188. *
  189. * Callers may also unregister RPC services that are no longer
  190. * available by setting the passed-in port to zero. This removes
  191. * all registered transports for [program, version] from the local
  192. * rpcbind database.
  193. *
  194. * This function uses rpcbind protocol version 2 to contact the
  195. * local rpcbind daemon.
  196. *
  197. * Registration works over both AF_INET and AF_INET6, and services
  198. * registered via this function are advertised as available for any
  199. * address. If the local rpcbind daemon is listening on AF_INET6,
  200. * services registered via this function will be advertised on
  201. * IN6ADDR_ANY (ie available for all AF_INET and AF_INET6
  202. * addresses).
  203. */
  204. int rpcb_register(u32 prog, u32 vers, int prot, unsigned short port)
  205. {
  206. struct rpcbind_args map = {
  207. .r_prog = prog,
  208. .r_vers = vers,
  209. .r_prot = prot,
  210. .r_port = port,
  211. };
  212. struct rpc_message msg = {
  213. .rpc_argp = &map,
  214. };
  215. dprintk("RPC: %sregistering (%u, %u, %d, %u) with local "
  216. "rpcbind\n", (port ? "" : "un"),
  217. prog, vers, prot, port);
  218. msg.rpc_proc = &rpcb_procedures2[RPCBPROC_UNSET];
  219. if (port)
  220. msg.rpc_proc = &rpcb_procedures2[RPCBPROC_SET];
  221. return rpcb_register_call((struct sockaddr *)&rpcb_inaddr_loopback,
  222. sizeof(rpcb_inaddr_loopback),
  223. RPCBVERS_2, &msg);
  224. }
  225. /*
  226. * Fill in AF_INET family-specific arguments to register
  227. */
  228. static int rpcb_register_netid4(struct sockaddr_in *address_to_register,
  229. struct rpc_message *msg)
  230. {
  231. struct rpcbind_args *map = msg->rpc_argp;
  232. unsigned short port = ntohs(address_to_register->sin_port);
  233. char buf[32];
  234. /* Construct AF_INET universal address */
  235. snprintf(buf, sizeof(buf),
  236. NIPQUAD_FMT".%u.%u",
  237. NIPQUAD(address_to_register->sin_addr.s_addr),
  238. port >> 8, port & 0xff);
  239. map->r_addr = buf;
  240. dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
  241. "local rpcbind\n", (port ? "" : "un"),
  242. map->r_prog, map->r_vers,
  243. map->r_addr, map->r_netid);
  244. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  245. if (port)
  246. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
  247. return rpcb_register_call((struct sockaddr *)&rpcb_inaddr_loopback,
  248. sizeof(rpcb_inaddr_loopback),
  249. RPCBVERS_4, msg);
  250. }
  251. /*
  252. * Fill in AF_INET6 family-specific arguments to register
  253. */
  254. static int rpcb_register_netid6(struct sockaddr_in6 *address_to_register,
  255. struct rpc_message *msg)
  256. {
  257. struct rpcbind_args *map = msg->rpc_argp;
  258. unsigned short port = ntohs(address_to_register->sin6_port);
  259. char buf[64];
  260. /* Construct AF_INET6 universal address */
  261. if (ipv6_addr_any(&address_to_register->sin6_addr))
  262. snprintf(buf, sizeof(buf), "::.%u.%u",
  263. port >> 8, port & 0xff);
  264. else
  265. snprintf(buf, sizeof(buf), NIP6_FMT".%u.%u",
  266. NIP6(address_to_register->sin6_addr),
  267. port >> 8, port & 0xff);
  268. map->r_addr = buf;
  269. dprintk("RPC: %sregistering [%u, %u, %s, '%s'] with "
  270. "local rpcbind\n", (port ? "" : "un"),
  271. map->r_prog, map->r_vers,
  272. map->r_addr, map->r_netid);
  273. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_UNSET];
  274. if (port)
  275. msg->rpc_proc = &rpcb_procedures4[RPCBPROC_SET];
  276. return rpcb_register_call((struct sockaddr *)&rpcb_in6addr_loopback,
  277. sizeof(rpcb_in6addr_loopback),
  278. RPCBVERS_4, msg);
  279. }
  280. /**
  281. * rpcb_v4_register - set or unset a port registration with the local rpcbind
  282. * @program: RPC program number of service to (un)register
  283. * @version: RPC version number of service to (un)register
  284. * @address: address family, IP address, and port to (un)register
  285. * @netid: netid of transport protocol to (un)register
  286. *
  287. * Returns zero if the registration request was dispatched successfully
  288. * and the rpcbind daemon returned success. Otherwise, returns an errno
  289. * value that reflects the nature of the error (request could not be
  290. * dispatched, timed out, or rpcbind returned an error).
  291. *
  292. * RPC services invoke this function to advertise their contact
  293. * information via the system's rpcbind daemon. RPC services
  294. * invoke this function once for each [program, version, address,
  295. * netid] tuple they wish to advertise.
  296. *
  297. * Callers may also unregister RPC services that are no longer
  298. * available by setting the port number in the passed-in address
  299. * to zero. Callers pass a netid of "" to unregister all
  300. * transport netids associated with [program, version, address].
  301. *
  302. * This function uses rpcbind protocol version 4 to contact the
  303. * local rpcbind daemon. The local rpcbind daemon must support
  304. * version 4 of the rpcbind protocol in order for these functions
  305. * to register a service successfully.
  306. *
  307. * Supported netids include "udp" and "tcp" for UDP and TCP over
  308. * IPv4, and "udp6" and "tcp6" for UDP and TCP over IPv6,
  309. * respectively.
  310. *
  311. * The contents of @address determine the address family and the
  312. * port to be registered. The usual practice is to pass INADDR_ANY
  313. * as the raw address, but specifying a non-zero address is also
  314. * supported by this API if the caller wishes to advertise an RPC
  315. * service on a specific network interface.
  316. *
  317. * Note that passing in INADDR_ANY does not create the same service
  318. * registration as IN6ADDR_ANY. The former advertises an RPC
  319. * service on any IPv4 address, but not on IPv6. The latter
  320. * advertises the service on all IPv4 and IPv6 addresses.
  321. */
  322. int rpcb_v4_register(const u32 program, const u32 version,
  323. const struct sockaddr *address, const char *netid)
  324. {
  325. struct rpcbind_args map = {
  326. .r_prog = program,
  327. .r_vers = version,
  328. .r_netid = netid,
  329. .r_owner = RPCB_OWNER_STRING,
  330. };
  331. struct rpc_message msg = {
  332. .rpc_argp = &map,
  333. };
  334. switch (address->sa_family) {
  335. case AF_INET:
  336. return rpcb_register_netid4((struct sockaddr_in *)address,
  337. &msg);
  338. case AF_INET6:
  339. return rpcb_register_netid6((struct sockaddr_in6 *)address,
  340. &msg);
  341. }
  342. return -EAFNOSUPPORT;
  343. }
  344. /**
  345. * rpcb_getport_sync - obtain the port for an RPC service on a given host
  346. * @sin: address of remote peer
  347. * @prog: RPC program number to bind
  348. * @vers: RPC version number to bind
  349. * @prot: transport protocol to use to make this request
  350. *
  351. * Return value is the requested advertised port number,
  352. * or a negative errno value.
  353. *
  354. * Called from outside the RPC client in a synchronous task context.
  355. * Uses default timeout parameters specified by underlying transport.
  356. *
  357. * XXX: Needs to support IPv6
  358. */
  359. int rpcb_getport_sync(struct sockaddr_in *sin, u32 prog, u32 vers, int prot)
  360. {
  361. struct rpcbind_args map = {
  362. .r_prog = prog,
  363. .r_vers = vers,
  364. .r_prot = prot,
  365. .r_port = 0,
  366. };
  367. struct rpc_message msg = {
  368. .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
  369. .rpc_argp = &map,
  370. .rpc_resp = &map.r_port,
  371. };
  372. struct rpc_clnt *rpcb_clnt;
  373. int status;
  374. dprintk("RPC: %s(" NIPQUAD_FMT ", %u, %u, %d)\n",
  375. __func__, NIPQUAD(sin->sin_addr.s_addr), prog, vers, prot);
  376. rpcb_clnt = rpcb_create(NULL, (struct sockaddr *)sin,
  377. sizeof(*sin), prot, RPCBVERS_2);
  378. if (IS_ERR(rpcb_clnt))
  379. return PTR_ERR(rpcb_clnt);
  380. status = rpc_call_sync(rpcb_clnt, &msg, 0);
  381. rpc_shutdown_client(rpcb_clnt);
  382. if (status >= 0) {
  383. if (map.r_port != 0)
  384. return map.r_port;
  385. status = -EACCES;
  386. }
  387. return status;
  388. }
  389. EXPORT_SYMBOL_GPL(rpcb_getport_sync);
  390. static struct rpc_task *rpcb_call_async(struct rpc_clnt *rpcb_clnt, struct rpcbind_args *map, struct rpc_procinfo *proc)
  391. {
  392. struct rpc_message msg = {
  393. .rpc_proc = proc,
  394. .rpc_argp = map,
  395. .rpc_resp = &map->r_port,
  396. };
  397. struct rpc_task_setup task_setup_data = {
  398. .rpc_client = rpcb_clnt,
  399. .rpc_message = &msg,
  400. .callback_ops = &rpcb_getport_ops,
  401. .callback_data = map,
  402. .flags = RPC_TASK_ASYNC,
  403. };
  404. return rpc_run_task(&task_setup_data);
  405. }
  406. /**
  407. * rpcb_getport_async - obtain the port for a given RPC service on a given host
  408. * @task: task that is waiting for portmapper request
  409. *
  410. * This one can be called for an ongoing RPC request, and can be used in
  411. * an async (rpciod) context.
  412. */
  413. void rpcb_getport_async(struct rpc_task *task)
  414. {
  415. struct rpc_clnt *clnt = task->tk_client;
  416. struct rpc_procinfo *proc;
  417. u32 bind_version;
  418. struct rpc_xprt *xprt = task->tk_xprt;
  419. struct rpc_clnt *rpcb_clnt;
  420. static struct rpcbind_args *map;
  421. struct rpc_task *child;
  422. struct sockaddr_storage addr;
  423. struct sockaddr *sap = (struct sockaddr *)&addr;
  424. size_t salen;
  425. int status;
  426. dprintk("RPC: %5u %s(%s, %u, %u, %d)\n",
  427. task->tk_pid, __func__,
  428. clnt->cl_server, clnt->cl_prog, clnt->cl_vers, xprt->prot);
  429. /* Autobind on cloned rpc clients is discouraged */
  430. BUG_ON(clnt->cl_parent != clnt);
  431. /* Put self on the wait queue to ensure we get notified if
  432. * some other task is already attempting to bind the port */
  433. rpc_sleep_on(&xprt->binding, task, NULL);
  434. if (xprt_test_and_set_binding(xprt)) {
  435. dprintk("RPC: %5u %s: waiting for another binder\n",
  436. task->tk_pid, __func__);
  437. return;
  438. }
  439. /* Someone else may have bound if we slept */
  440. if (xprt_bound(xprt)) {
  441. status = 0;
  442. dprintk("RPC: %5u %s: already bound\n",
  443. task->tk_pid, __func__);
  444. goto bailout_nofree;
  445. }
  446. salen = rpc_peeraddr(clnt, sap, sizeof(addr));
  447. /* Don't ever use rpcbind v2 for AF_INET6 requests */
  448. switch (sap->sa_family) {
  449. case AF_INET:
  450. proc = rpcb_next_version[xprt->bind_index].rpc_proc;
  451. bind_version = rpcb_next_version[xprt->bind_index].rpc_vers;
  452. break;
  453. case AF_INET6:
  454. proc = rpcb_next_version6[xprt->bind_index].rpc_proc;
  455. bind_version = rpcb_next_version6[xprt->bind_index].rpc_vers;
  456. break;
  457. default:
  458. status = -EAFNOSUPPORT;
  459. dprintk("RPC: %5u %s: bad address family\n",
  460. task->tk_pid, __func__);
  461. goto bailout_nofree;
  462. }
  463. if (proc == NULL) {
  464. xprt->bind_index = 0;
  465. status = -EPFNOSUPPORT;
  466. dprintk("RPC: %5u %s: no more getport versions available\n",
  467. task->tk_pid, __func__);
  468. goto bailout_nofree;
  469. }
  470. dprintk("RPC: %5u %s: trying rpcbind version %u\n",
  471. task->tk_pid, __func__, bind_version);
  472. rpcb_clnt = rpcb_create(clnt->cl_server, sap, salen, xprt->prot,
  473. bind_version);
  474. if (IS_ERR(rpcb_clnt)) {
  475. status = PTR_ERR(rpcb_clnt);
  476. dprintk("RPC: %5u %s: rpcb_create failed, error %ld\n",
  477. task->tk_pid, __func__, PTR_ERR(rpcb_clnt));
  478. goto bailout_nofree;
  479. }
  480. map = kzalloc(sizeof(struct rpcbind_args), GFP_ATOMIC);
  481. if (!map) {
  482. status = -ENOMEM;
  483. dprintk("RPC: %5u %s: no memory available\n",
  484. task->tk_pid, __func__);
  485. goto bailout_nofree;
  486. }
  487. map->r_prog = clnt->cl_prog;
  488. map->r_vers = clnt->cl_vers;
  489. map->r_prot = xprt->prot;
  490. map->r_port = 0;
  491. map->r_xprt = xprt_get(xprt);
  492. map->r_netid = rpc_peeraddr2str(clnt, RPC_DISPLAY_NETID);
  493. map->r_addr = rpc_peeraddr2str(rpcb_clnt, RPC_DISPLAY_UNIVERSAL_ADDR);
  494. map->r_owner = RPCB_OWNER_STRING; /* ignored for GETADDR */
  495. map->r_status = -EIO;
  496. child = rpcb_call_async(rpcb_clnt, map, proc);
  497. rpc_release_client(rpcb_clnt);
  498. if (IS_ERR(child)) {
  499. /* rpcb_map_release() has freed the arguments */
  500. dprintk("RPC: %5u %s: rpc_run_task failed\n",
  501. task->tk_pid, __func__);
  502. return;
  503. }
  504. rpc_put_task(child);
  505. task->tk_xprt->stat.bind_count++;
  506. return;
  507. bailout_nofree:
  508. rpcb_wake_rpcbind_waiters(xprt, status);
  509. task->tk_status = status;
  510. }
  511. EXPORT_SYMBOL_GPL(rpcb_getport_async);
  512. /*
  513. * Rpcbind child task calls this callback via tk_exit.
  514. */
  515. static void rpcb_getport_done(struct rpc_task *child, void *data)
  516. {
  517. struct rpcbind_args *map = data;
  518. struct rpc_xprt *xprt = map->r_xprt;
  519. int status = child->tk_status;
  520. /* Garbage reply: retry with a lesser rpcbind version */
  521. if (status == -EIO)
  522. status = -EPROTONOSUPPORT;
  523. /* rpcbind server doesn't support this rpcbind protocol version */
  524. if (status == -EPROTONOSUPPORT)
  525. xprt->bind_index++;
  526. if (status < 0) {
  527. /* rpcbind server not available on remote host? */
  528. xprt->ops->set_port(xprt, 0);
  529. } else if (map->r_port == 0) {
  530. /* Requested RPC service wasn't registered on remote host */
  531. xprt->ops->set_port(xprt, 0);
  532. status = -EACCES;
  533. } else {
  534. /* Succeeded */
  535. xprt->ops->set_port(xprt, map->r_port);
  536. xprt_set_bound(xprt);
  537. status = 0;
  538. }
  539. dprintk("RPC: %5u rpcb_getport_done(status %d, port %u)\n",
  540. child->tk_pid, status, map->r_port);
  541. map->r_status = status;
  542. }
  543. /*
  544. * XDR functions for rpcbind
  545. */
  546. static int rpcb_encode_mapping(struct rpc_rqst *req, __be32 *p,
  547. struct rpcbind_args *rpcb)
  548. {
  549. dprintk("RPC: encoding rpcb request (%u, %u, %d, %u)\n",
  550. rpcb->r_prog, rpcb->r_vers, rpcb->r_prot, rpcb->r_port);
  551. *p++ = htonl(rpcb->r_prog);
  552. *p++ = htonl(rpcb->r_vers);
  553. *p++ = htonl(rpcb->r_prot);
  554. *p++ = htonl(rpcb->r_port);
  555. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  556. return 0;
  557. }
  558. static int rpcb_decode_getport(struct rpc_rqst *req, __be32 *p,
  559. unsigned short *portp)
  560. {
  561. *portp = (unsigned short) ntohl(*p++);
  562. dprintk("RPC: rpcb getport result: %u\n",
  563. *portp);
  564. return 0;
  565. }
  566. static int rpcb_decode_set(struct rpc_rqst *req, __be32 *p,
  567. unsigned int *boolp)
  568. {
  569. *boolp = (unsigned int) ntohl(*p++);
  570. dprintk("RPC: rpcb set/unset call %s\n",
  571. (*boolp ? "succeeded" : "failed"));
  572. return 0;
  573. }
  574. static int rpcb_encode_getaddr(struct rpc_rqst *req, __be32 *p,
  575. struct rpcbind_args *rpcb)
  576. {
  577. dprintk("RPC: encoding rpcb request (%u, %u, %s)\n",
  578. rpcb->r_prog, rpcb->r_vers, rpcb->r_addr);
  579. *p++ = htonl(rpcb->r_prog);
  580. *p++ = htonl(rpcb->r_vers);
  581. p = xdr_encode_string(p, rpcb->r_netid);
  582. p = xdr_encode_string(p, rpcb->r_addr);
  583. p = xdr_encode_string(p, rpcb->r_owner);
  584. req->rq_slen = xdr_adjust_iovec(req->rq_svec, p);
  585. return 0;
  586. }
  587. static int rpcb_decode_getaddr(struct rpc_rqst *req, __be32 *p,
  588. unsigned short *portp)
  589. {
  590. char *addr;
  591. u32 addr_len;
  592. int c, i, f, first, val;
  593. *portp = 0;
  594. addr_len = ntohl(*p++);
  595. /*
  596. * Simple sanity check. The smallest possible universal
  597. * address is an IPv4 address string containing 11 bytes.
  598. */
  599. if (addr_len < 11 || addr_len > RPCBIND_MAXUADDRLEN)
  600. goto out_err;
  601. /*
  602. * Start at the end and walk backwards until the first dot
  603. * is encountered. When the second dot is found, we have
  604. * both parts of the port number.
  605. */
  606. addr = (char *)p;
  607. val = 0;
  608. first = 1;
  609. f = 1;
  610. for (i = addr_len - 1; i > 0; i--) {
  611. c = addr[i];
  612. if (c >= '0' && c <= '9') {
  613. val += (c - '0') * f;
  614. f *= 10;
  615. } else if (c == '.') {
  616. if (first) {
  617. *portp = val;
  618. val = first = 0;
  619. f = 1;
  620. } else {
  621. *portp |= (val << 8);
  622. break;
  623. }
  624. }
  625. }
  626. /*
  627. * Simple sanity check. If we never saw a dot in the reply,
  628. * then this was probably just garbage.
  629. */
  630. if (first)
  631. goto out_err;
  632. dprintk("RPC: rpcb_decode_getaddr port=%u\n", *portp);
  633. return 0;
  634. out_err:
  635. dprintk("RPC: rpcbind server returned malformed reply\n");
  636. return -EIO;
  637. }
  638. #define RPCB_program_sz (1u)
  639. #define RPCB_version_sz (1u)
  640. #define RPCB_protocol_sz (1u)
  641. #define RPCB_port_sz (1u)
  642. #define RPCB_boolean_sz (1u)
  643. #define RPCB_netid_sz (1+XDR_QUADLEN(RPCBIND_MAXNETIDLEN))
  644. #define RPCB_addr_sz (1+XDR_QUADLEN(RPCBIND_MAXUADDRLEN))
  645. #define RPCB_ownerstring_sz (1+XDR_QUADLEN(RPCB_MAXOWNERLEN))
  646. #define RPCB_mappingargs_sz RPCB_program_sz+RPCB_version_sz+ \
  647. RPCB_protocol_sz+RPCB_port_sz
  648. #define RPCB_getaddrargs_sz RPCB_program_sz+RPCB_version_sz+ \
  649. RPCB_netid_sz+RPCB_addr_sz+ \
  650. RPCB_ownerstring_sz
  651. #define RPCB_setres_sz RPCB_boolean_sz
  652. #define RPCB_getportres_sz RPCB_port_sz
  653. /*
  654. * Note that RFC 1833 does not put any size restrictions on the
  655. * address string returned by the remote rpcbind database.
  656. */
  657. #define RPCB_getaddrres_sz RPCB_addr_sz
  658. #define PROC(proc, argtype, restype) \
  659. [RPCBPROC_##proc] = { \
  660. .p_proc = RPCBPROC_##proc, \
  661. .p_encode = (kxdrproc_t) rpcb_encode_##argtype, \
  662. .p_decode = (kxdrproc_t) rpcb_decode_##restype, \
  663. .p_arglen = RPCB_##argtype##args_sz, \
  664. .p_replen = RPCB_##restype##res_sz, \
  665. .p_statidx = RPCBPROC_##proc, \
  666. .p_timer = 0, \
  667. .p_name = #proc, \
  668. }
  669. /*
  670. * Not all rpcbind procedures described in RFC 1833 are implemented
  671. * since the Linux kernel RPC code requires only these.
  672. */
  673. static struct rpc_procinfo rpcb_procedures2[] = {
  674. PROC(SET, mapping, set),
  675. PROC(UNSET, mapping, set),
  676. PROC(GETPORT, mapping, getport),
  677. };
  678. static struct rpc_procinfo rpcb_procedures3[] = {
  679. PROC(SET, getaddr, set),
  680. PROC(UNSET, getaddr, set),
  681. PROC(GETADDR, getaddr, getaddr),
  682. };
  683. static struct rpc_procinfo rpcb_procedures4[] = {
  684. PROC(SET, getaddr, set),
  685. PROC(UNSET, getaddr, set),
  686. PROC(GETADDR, getaddr, getaddr),
  687. PROC(GETVERSADDR, getaddr, getaddr),
  688. };
  689. static struct rpcb_info rpcb_next_version[] = {
  690. {
  691. .rpc_vers = RPCBVERS_2,
  692. .rpc_proc = &rpcb_procedures2[RPCBPROC_GETPORT],
  693. },
  694. {
  695. .rpc_proc = NULL,
  696. },
  697. };
  698. static struct rpcb_info rpcb_next_version6[] = {
  699. {
  700. .rpc_vers = RPCBVERS_4,
  701. .rpc_proc = &rpcb_procedures4[RPCBPROC_GETADDR],
  702. },
  703. {
  704. .rpc_vers = RPCBVERS_3,
  705. .rpc_proc = &rpcb_procedures3[RPCBPROC_GETADDR],
  706. },
  707. {
  708. .rpc_proc = NULL,
  709. },
  710. };
  711. static struct rpc_version rpcb_version2 = {
  712. .number = RPCBVERS_2,
  713. .nrprocs = RPCB_HIGHPROC_2,
  714. .procs = rpcb_procedures2
  715. };
  716. static struct rpc_version rpcb_version3 = {
  717. .number = RPCBVERS_3,
  718. .nrprocs = RPCB_HIGHPROC_3,
  719. .procs = rpcb_procedures3
  720. };
  721. static struct rpc_version rpcb_version4 = {
  722. .number = RPCBVERS_4,
  723. .nrprocs = RPCB_HIGHPROC_4,
  724. .procs = rpcb_procedures4
  725. };
  726. static struct rpc_version *rpcb_version[] = {
  727. NULL,
  728. NULL,
  729. &rpcb_version2,
  730. &rpcb_version3,
  731. &rpcb_version4
  732. };
  733. static struct rpc_stat rpcb_stats;
  734. static struct rpc_program rpcb_program = {
  735. .name = "rpcbind",
  736. .number = RPCBIND_PROGRAM,
  737. .nrvers = ARRAY_SIZE(rpcb_version),
  738. .version = rpcb_version,
  739. .stats = &rpcb_stats,
  740. };