mount_clnt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * In-kernel MOUNT protocol client
  4. *
  5. * Copyright (C) 1997, Olaf Kirch <okir@monad.swb.de>
  6. */
  7. #include <linux/types.h>
  8. #include <linux/socket.h>
  9. #include <linux/kernel.h>
  10. #include <linux/errno.h>
  11. #include <linux/uio.h>
  12. #include <linux/net.h>
  13. #include <linux/in.h>
  14. #include <linux/sunrpc/clnt.h>
  15. #include <linux/sunrpc/sched.h>
  16. #include <linux/nfs_fs.h>
  17. #include "internal.h"
  18. #define NFSDBG_FACILITY NFSDBG_MOUNT
  19. /*
  20. * Defined by RFC 1094, section A.3; and RFC 1813, section 5.1.4
  21. */
  22. #define MNTPATHLEN (1024)
  23. /*
  24. * XDR data type sizes
  25. */
  26. #define encode_dirpath_sz (1 + XDR_QUADLEN(MNTPATHLEN))
  27. #define MNT_status_sz (1)
  28. #define MNT_fhandle_sz XDR_QUADLEN(NFS2_FHSIZE)
  29. #define MNT_fhandlev3_sz XDR_QUADLEN(NFS3_FHSIZE)
  30. #define MNT_authflav3_sz (1 + NFS_MAX_SECFLAVORS)
  31. /*
  32. * XDR argument and result sizes
  33. */
  34. #define MNT_enc_dirpath_sz encode_dirpath_sz
  35. #define MNT_dec_mountres_sz (MNT_status_sz + MNT_fhandle_sz)
  36. #define MNT_dec_mountres3_sz (MNT_status_sz + MNT_fhandlev3_sz + \
  37. MNT_authflav3_sz)
  38. /*
  39. * Defined by RFC 1094, section A.5
  40. */
  41. enum {
  42. MOUNTPROC_NULL = 0,
  43. MOUNTPROC_MNT = 1,
  44. MOUNTPROC_DUMP = 2,
  45. MOUNTPROC_UMNT = 3,
  46. MOUNTPROC_UMNTALL = 4,
  47. MOUNTPROC_EXPORT = 5,
  48. };
  49. /*
  50. * Defined by RFC 1813, section 5.2
  51. */
  52. enum {
  53. MOUNTPROC3_NULL = 0,
  54. MOUNTPROC3_MNT = 1,
  55. MOUNTPROC3_DUMP = 2,
  56. MOUNTPROC3_UMNT = 3,
  57. MOUNTPROC3_UMNTALL = 4,
  58. MOUNTPROC3_EXPORT = 5,
  59. };
  60. static const struct rpc_program mnt_program;
  61. /*
  62. * Defined by OpenGroup XNFS Version 3W, chapter 8
  63. */
  64. enum mountstat {
  65. MNT_OK = 0,
  66. MNT_EPERM = 1,
  67. MNT_ENOENT = 2,
  68. MNT_EACCES = 13,
  69. MNT_EINVAL = 22,
  70. };
  71. static struct {
  72. u32 status;
  73. int errno;
  74. } mnt_errtbl[] = {
  75. { .status = MNT_OK, .errno = 0, },
  76. { .status = MNT_EPERM, .errno = -EPERM, },
  77. { .status = MNT_ENOENT, .errno = -ENOENT, },
  78. { .status = MNT_EACCES, .errno = -EACCES, },
  79. { .status = MNT_EINVAL, .errno = -EINVAL, },
  80. };
  81. /*
  82. * Defined by RFC 1813, section 5.1.5
  83. */
  84. enum mountstat3 {
  85. MNT3_OK = 0, /* no error */
  86. MNT3ERR_PERM = 1, /* Not owner */
  87. MNT3ERR_NOENT = 2, /* No such file or directory */
  88. MNT3ERR_IO = 5, /* I/O error */
  89. MNT3ERR_ACCES = 13, /* Permission denied */
  90. MNT3ERR_NOTDIR = 20, /* Not a directory */
  91. MNT3ERR_INVAL = 22, /* Invalid argument */
  92. MNT3ERR_NAMETOOLONG = 63, /* Filename too long */
  93. MNT3ERR_NOTSUPP = 10004, /* Operation not supported */
  94. MNT3ERR_SERVERFAULT = 10006, /* A failure on the server */
  95. };
  96. static struct {
  97. u32 status;
  98. int errno;
  99. } mnt3_errtbl[] = {
  100. { .status = MNT3_OK, .errno = 0, },
  101. { .status = MNT3ERR_PERM, .errno = -EPERM, },
  102. { .status = MNT3ERR_NOENT, .errno = -ENOENT, },
  103. { .status = MNT3ERR_IO, .errno = -EIO, },
  104. { .status = MNT3ERR_ACCES, .errno = -EACCES, },
  105. { .status = MNT3ERR_NOTDIR, .errno = -ENOTDIR, },
  106. { .status = MNT3ERR_INVAL, .errno = -EINVAL, },
  107. { .status = MNT3ERR_NAMETOOLONG, .errno = -ENAMETOOLONG, },
  108. { .status = MNT3ERR_NOTSUPP, .errno = -ENOTSUPP, },
  109. { .status = MNT3ERR_SERVERFAULT, .errno = -EREMOTEIO, },
  110. };
  111. struct mountres {
  112. int errno;
  113. struct nfs_fh *fh;
  114. unsigned int *auth_count;
  115. rpc_authflavor_t *auth_flavors;
  116. };
  117. /**
  118. * nfs_mount - Obtain an NFS file handle for the given host and path
  119. * @info: pointer to mount request arguments
  120. * @timeo: deciseconds the mount waits for a response before it retries
  121. * @retrans: number of times the mount retries a request
  122. *
  123. * Uses timeout parameters specified by caller. On successful return, the
  124. * auth_flavs list and auth_flav_len will be populated with the list from the
  125. * server or a faked-up list if the server didn't provide one.
  126. */
  127. int nfs_mount(struct nfs_mount_request *info, int timeo, int retrans)
  128. {
  129. struct rpc_timeout mnt_timeout;
  130. struct mountres result = {
  131. .fh = info->fh,
  132. .auth_count = info->auth_flav_len,
  133. .auth_flavors = info->auth_flavs,
  134. };
  135. struct rpc_message msg = {
  136. .rpc_argp = info->dirpath,
  137. .rpc_resp = &result,
  138. };
  139. struct rpc_create_args args = {
  140. .net = info->net,
  141. .protocol = info->protocol,
  142. .address = (struct sockaddr *)info->sap,
  143. .addrsize = info->salen,
  144. .timeout = &mnt_timeout,
  145. .servername = info->hostname,
  146. .program = &mnt_program,
  147. .version = info->version,
  148. .authflavor = RPC_AUTH_UNIX,
  149. .cred = current_cred(),
  150. };
  151. struct rpc_clnt *mnt_clnt;
  152. int status;
  153. dprintk("NFS: sending MNT request for %s:%s\n",
  154. (info->hostname ? info->hostname : "server"),
  155. info->dirpath);
  156. if (strlen(info->dirpath) > MNTPATHLEN)
  157. return -ENAMETOOLONG;
  158. if (info->noresvport)
  159. args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
  160. nfs_init_timeout_values(&mnt_timeout, info->protocol, timeo, retrans);
  161. mnt_clnt = rpc_create(&args);
  162. if (IS_ERR(mnt_clnt))
  163. goto out_clnt_err;
  164. if (info->version == NFS_MNT3_VERSION)
  165. msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC3_MNT];
  166. else
  167. msg.rpc_proc = &mnt_clnt->cl_procinfo[MOUNTPROC_MNT];
  168. status = rpc_call_sync(mnt_clnt, &msg, RPC_TASK_SOFT|RPC_TASK_TIMEOUT);
  169. rpc_shutdown_client(mnt_clnt);
  170. if (status < 0)
  171. goto out_call_err;
  172. if (result.errno != 0)
  173. goto out_mnt_err;
  174. dprintk("NFS: MNT request succeeded\n");
  175. status = 0;
  176. /*
  177. * If the server didn't provide a flavor list, allow the
  178. * client to try any flavor.
  179. */
  180. if (info->version != NFS_MNT3_VERSION || *info->auth_flav_len == 0) {
  181. dprintk("NFS: Faking up auth_flavs list\n");
  182. info->auth_flavs[0] = RPC_AUTH_NULL;
  183. *info->auth_flav_len = 1;
  184. }
  185. out:
  186. return status;
  187. out_clnt_err:
  188. status = PTR_ERR(mnt_clnt);
  189. dprintk("NFS: failed to create MNT RPC client, status=%d\n", status);
  190. goto out;
  191. out_call_err:
  192. dprintk("NFS: MNT request failed, status=%d\n", status);
  193. goto out;
  194. out_mnt_err:
  195. dprintk("NFS: MNT server returned result %d\n", result.errno);
  196. status = result.errno;
  197. goto out;
  198. }
  199. /*
  200. * XDR encode/decode functions for MOUNT
  201. */
  202. static void encode_mntdirpath(struct xdr_stream *xdr, const char *pathname)
  203. {
  204. const u32 pathname_len = strlen(pathname);
  205. __be32 *p;
  206. p = xdr_reserve_space(xdr, 4 + pathname_len);
  207. xdr_encode_opaque(p, pathname, pathname_len);
  208. }
  209. static void mnt_xdr_enc_dirpath(struct rpc_rqst *req, struct xdr_stream *xdr,
  210. const void *dirpath)
  211. {
  212. encode_mntdirpath(xdr, dirpath);
  213. }
  214. /*
  215. * RFC 1094: "A non-zero status indicates some sort of error. In this
  216. * case, the status is a UNIX error number." This can be problematic
  217. * if the server and client use different errno values for the same
  218. * error.
  219. *
  220. * However, the OpenGroup XNFS spec provides a simple mapping that is
  221. * independent of local errno values on the server and the client.
  222. */
  223. static int decode_status(struct xdr_stream *xdr, struct mountres *res)
  224. {
  225. unsigned int i;
  226. u32 status;
  227. __be32 *p;
  228. p = xdr_inline_decode(xdr, 4);
  229. if (unlikely(p == NULL))
  230. return -EIO;
  231. status = be32_to_cpup(p);
  232. for (i = 0; i < ARRAY_SIZE(mnt_errtbl); i++) {
  233. if (mnt_errtbl[i].status == status) {
  234. res->errno = mnt_errtbl[i].errno;
  235. return 0;
  236. }
  237. }
  238. dprintk("NFS: unrecognized MNT status code: %u\n", status);
  239. res->errno = -EACCES;
  240. return 0;
  241. }
  242. static int decode_fhandle(struct xdr_stream *xdr, struct mountres *res)
  243. {
  244. struct nfs_fh *fh = res->fh;
  245. __be32 *p;
  246. p = xdr_inline_decode(xdr, NFS2_FHSIZE);
  247. if (unlikely(p == NULL))
  248. return -EIO;
  249. fh->size = NFS2_FHSIZE;
  250. memcpy(fh->data, p, NFS2_FHSIZE);
  251. return 0;
  252. }
  253. static int mnt_xdr_dec_mountres(struct rpc_rqst *req,
  254. struct xdr_stream *xdr,
  255. void *data)
  256. {
  257. struct mountres *res = data;
  258. int status;
  259. status = decode_status(xdr, res);
  260. if (unlikely(status != 0 || res->errno != 0))
  261. return status;
  262. return decode_fhandle(xdr, res);
  263. }
  264. static int decode_fhs_status(struct xdr_stream *xdr, struct mountres *res)
  265. {
  266. unsigned int i;
  267. u32 status;
  268. __be32 *p;
  269. p = xdr_inline_decode(xdr, 4);
  270. if (unlikely(p == NULL))
  271. return -EIO;
  272. status = be32_to_cpup(p);
  273. for (i = 0; i < ARRAY_SIZE(mnt3_errtbl); i++) {
  274. if (mnt3_errtbl[i].status == status) {
  275. res->errno = mnt3_errtbl[i].errno;
  276. return 0;
  277. }
  278. }
  279. dprintk("NFS: unrecognized MNT3 status code: %u\n", status);
  280. res->errno = -EACCES;
  281. return 0;
  282. }
  283. static int decode_fhandle3(struct xdr_stream *xdr, struct mountres *res)
  284. {
  285. struct nfs_fh *fh = res->fh;
  286. u32 size;
  287. __be32 *p;
  288. p = xdr_inline_decode(xdr, 4);
  289. if (unlikely(p == NULL))
  290. return -EIO;
  291. size = be32_to_cpup(p);
  292. if (size > NFS3_FHSIZE || size == 0)
  293. return -EIO;
  294. p = xdr_inline_decode(xdr, size);
  295. if (unlikely(p == NULL))
  296. return -EIO;
  297. fh->size = size;
  298. memcpy(fh->data, p, size);
  299. return 0;
  300. }
  301. static int decode_auth_flavors(struct xdr_stream *xdr, struct mountres *res)
  302. {
  303. rpc_authflavor_t *flavors = res->auth_flavors;
  304. unsigned int *count = res->auth_count;
  305. u32 entries, i;
  306. __be32 *p;
  307. if (*count == 0)
  308. return 0;
  309. p = xdr_inline_decode(xdr, 4);
  310. if (unlikely(p == NULL))
  311. return -EIO;
  312. entries = be32_to_cpup(p);
  313. dprintk("NFS: received %u auth flavors\n", entries);
  314. if (entries > NFS_MAX_SECFLAVORS)
  315. entries = NFS_MAX_SECFLAVORS;
  316. p = xdr_inline_decode(xdr, 4 * entries);
  317. if (unlikely(p == NULL))
  318. return -EIO;
  319. if (entries > *count)
  320. entries = *count;
  321. for (i = 0; i < entries; i++) {
  322. flavors[i] = be32_to_cpup(p++);
  323. dprintk("NFS: auth flavor[%u]: %d\n", i, flavors[i]);
  324. }
  325. *count = i;
  326. return 0;
  327. }
  328. static int mnt_xdr_dec_mountres3(struct rpc_rqst *req,
  329. struct xdr_stream *xdr,
  330. void *data)
  331. {
  332. struct mountres *res = data;
  333. int status;
  334. status = decode_fhs_status(xdr, res);
  335. if (unlikely(status != 0 || res->errno != 0))
  336. return status;
  337. status = decode_fhandle3(xdr, res);
  338. if (unlikely(status != 0)) {
  339. res->errno = -EBADHANDLE;
  340. return 0;
  341. }
  342. return decode_auth_flavors(xdr, res);
  343. }
  344. static const struct rpc_procinfo mnt_procedures[] = {
  345. [MOUNTPROC_MNT] = {
  346. .p_proc = MOUNTPROC_MNT,
  347. .p_encode = mnt_xdr_enc_dirpath,
  348. .p_decode = mnt_xdr_dec_mountres,
  349. .p_arglen = MNT_enc_dirpath_sz,
  350. .p_replen = MNT_dec_mountres_sz,
  351. .p_statidx = MOUNTPROC_MNT,
  352. .p_name = "MOUNT",
  353. },
  354. [MOUNTPROC_UMNT] = {
  355. .p_proc = MOUNTPROC_UMNT,
  356. .p_encode = mnt_xdr_enc_dirpath,
  357. .p_arglen = MNT_enc_dirpath_sz,
  358. .p_statidx = MOUNTPROC_UMNT,
  359. .p_name = "UMOUNT",
  360. },
  361. };
  362. static const struct rpc_procinfo mnt3_procedures[] = {
  363. [MOUNTPROC3_MNT] = {
  364. .p_proc = MOUNTPROC3_MNT,
  365. .p_encode = mnt_xdr_enc_dirpath,
  366. .p_decode = mnt_xdr_dec_mountres3,
  367. .p_arglen = MNT_enc_dirpath_sz,
  368. .p_replen = MNT_dec_mountres3_sz,
  369. .p_statidx = MOUNTPROC3_MNT,
  370. .p_name = "MOUNT",
  371. },
  372. [MOUNTPROC3_UMNT] = {
  373. .p_proc = MOUNTPROC3_UMNT,
  374. .p_encode = mnt_xdr_enc_dirpath,
  375. .p_arglen = MNT_enc_dirpath_sz,
  376. .p_statidx = MOUNTPROC3_UMNT,
  377. .p_name = "UMOUNT",
  378. },
  379. };
  380. static unsigned int mnt_counts[ARRAY_SIZE(mnt_procedures)];
  381. static const struct rpc_version mnt_version1 = {
  382. .number = 1,
  383. .nrprocs = ARRAY_SIZE(mnt_procedures),
  384. .procs = mnt_procedures,
  385. .counts = mnt_counts,
  386. };
  387. static unsigned int mnt3_counts[ARRAY_SIZE(mnt3_procedures)];
  388. static const struct rpc_version mnt_version3 = {
  389. .number = 3,
  390. .nrprocs = ARRAY_SIZE(mnt3_procedures),
  391. .procs = mnt3_procedures,
  392. .counts = mnt3_counts,
  393. };
  394. static const struct rpc_version *mnt_version[] = {
  395. NULL,
  396. &mnt_version1,
  397. NULL,
  398. &mnt_version3,
  399. };
  400. static struct rpc_stat mnt_stats;
  401. static const struct rpc_program mnt_program = {
  402. .name = "mount",
  403. .number = NFS_MNT_PROGRAM,
  404. .nrvers = ARRAY_SIZE(mnt_version),
  405. .version = mnt_version,
  406. .stats = &mnt_stats,
  407. };