rpcb_clnt.c 28 KB

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