rpcb_clnt.c 29 KB

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