rpcb_clnt.c 30 KB

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