rpcb_clnt.c 29 KB

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