rpcb_clnt.c 29 KB

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