rpcb_clnt.c 30 KB

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