rpcb_clnt.c 28 KB

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