cifs_dfs_ref.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Contains the CIFS DFS referral mounting routines used for handling
  4. * traversal via DFS junction point
  5. *
  6. * Copyright (c) 2007 Igor Mammedov
  7. * Copyright (C) International Business Machines Corp., 2008
  8. * Author(s): Igor Mammedov (niallain@gmail.com)
  9. * Steve French (sfrench@us.ibm.com)
  10. */
  11. #include <linux/dcache.h>
  12. #include <linux/mount.h>
  13. #include <linux/namei.h>
  14. #include <linux/slab.h>
  15. #include <linux/vfs.h>
  16. #include <linux/fs.h>
  17. #include <linux/inet.h>
  18. #include "cifsglob.h"
  19. #include "cifsproto.h"
  20. #include "cifsfs.h"
  21. #include "dns_resolve.h"
  22. #include "cifs_debug.h"
  23. #include "cifs_unicode.h"
  24. #include "dfs_cache.h"
  25. static LIST_HEAD(cifs_dfs_automount_list);
  26. static void cifs_dfs_expire_automounts(struct work_struct *work);
  27. static DECLARE_DELAYED_WORK(cifs_dfs_automount_task,
  28. cifs_dfs_expire_automounts);
  29. static int cifs_dfs_mountpoint_expiry_timeout = 500 * HZ;
  30. static void cifs_dfs_expire_automounts(struct work_struct *work)
  31. {
  32. struct list_head *list = &cifs_dfs_automount_list;
  33. mark_mounts_for_expiry(list);
  34. if (!list_empty(list))
  35. schedule_delayed_work(&cifs_dfs_automount_task,
  36. cifs_dfs_mountpoint_expiry_timeout);
  37. }
  38. void cifs_dfs_release_automount_timer(void)
  39. {
  40. BUG_ON(!list_empty(&cifs_dfs_automount_list));
  41. cancel_delayed_work_sync(&cifs_dfs_automount_task);
  42. }
  43. /**
  44. * cifs_build_devname - build a devicename from a UNC and optional prepath
  45. * @nodename: pointer to UNC string
  46. * @prepath: pointer to prefixpath (or NULL if there isn't one)
  47. *
  48. * Build a new cifs devicename after chasing a DFS referral. Allocate a buffer
  49. * big enough to hold the final thing. Copy the UNC from the nodename, and
  50. * concatenate the prepath onto the end of it if there is one.
  51. *
  52. * Returns pointer to the built string, or a ERR_PTR. Caller is responsible
  53. * for freeing the returned string.
  54. */
  55. static char *
  56. cifs_build_devname(char *nodename, const char *prepath)
  57. {
  58. size_t pplen;
  59. size_t unclen;
  60. char *dev;
  61. char *pos;
  62. /* skip over any preceding delimiters */
  63. nodename += strspn(nodename, "\\");
  64. if (!*nodename)
  65. return ERR_PTR(-EINVAL);
  66. /* get length of UNC and set pos to last char */
  67. unclen = strlen(nodename);
  68. pos = nodename + unclen - 1;
  69. /* trim off any trailing delimiters */
  70. while (*pos == '\\') {
  71. --pos;
  72. --unclen;
  73. }
  74. /* allocate a buffer:
  75. * +2 for preceding "//"
  76. * +1 for delimiter between UNC and prepath
  77. * +1 for trailing NULL
  78. */
  79. pplen = prepath ? strlen(prepath) : 0;
  80. dev = kmalloc(2 + unclen + 1 + pplen + 1, GFP_KERNEL);
  81. if (!dev)
  82. return ERR_PTR(-ENOMEM);
  83. pos = dev;
  84. /* add the initial "//" */
  85. *pos = '/';
  86. ++pos;
  87. *pos = '/';
  88. ++pos;
  89. /* copy in the UNC portion from referral */
  90. memcpy(pos, nodename, unclen);
  91. pos += unclen;
  92. /* copy the prefixpath remainder (if there is one) */
  93. if (pplen) {
  94. *pos = '/';
  95. ++pos;
  96. memcpy(pos, prepath, pplen);
  97. pos += pplen;
  98. }
  99. /* NULL terminator */
  100. *pos = '\0';
  101. convert_delimiter(dev, '/');
  102. return dev;
  103. }
  104. /**
  105. * cifs_compose_mount_options - creates mount options for referral
  106. * @sb_mountdata: parent/root DFS mount options (template)
  107. * @fullpath: full path in UNC format
  108. * @ref: optional server's referral
  109. * @devname: optional pointer for saving device name
  110. *
  111. * creates mount options for submount based on template options sb_mountdata
  112. * and replacing unc,ip,prefixpath options with ones we've got form ref_unc.
  113. *
  114. * Returns: pointer to new mount options or ERR_PTR.
  115. * Caller is responsible for freeing returned value if it is not error.
  116. */
  117. char *cifs_compose_mount_options(const char *sb_mountdata,
  118. const char *fullpath,
  119. const struct dfs_info3_param *ref,
  120. char **devname)
  121. {
  122. int rc;
  123. char *name;
  124. char *mountdata = NULL;
  125. const char *prepath = NULL;
  126. int md_len;
  127. char *tkn_e;
  128. char *srvIP = NULL;
  129. char sep = ',';
  130. int off, noff;
  131. if (sb_mountdata == NULL)
  132. return ERR_PTR(-EINVAL);
  133. if (ref) {
  134. if (WARN_ON_ONCE(!ref->node_name || ref->path_consumed < 0))
  135. return ERR_PTR(-EINVAL);
  136. if (strlen(fullpath) - ref->path_consumed) {
  137. prepath = fullpath + ref->path_consumed;
  138. /* skip initial delimiter */
  139. if (*prepath == '/' || *prepath == '\\')
  140. prepath++;
  141. }
  142. name = cifs_build_devname(ref->node_name, prepath);
  143. if (IS_ERR(name)) {
  144. rc = PTR_ERR(name);
  145. name = NULL;
  146. goto compose_mount_options_err;
  147. }
  148. } else {
  149. name = cifs_build_devname((char *)fullpath, NULL);
  150. if (IS_ERR(name)) {
  151. rc = PTR_ERR(name);
  152. name = NULL;
  153. goto compose_mount_options_err;
  154. }
  155. }
  156. rc = dns_resolve_server_name_to_ip(name, &srvIP);
  157. if (rc < 0) {
  158. cifs_dbg(FYI, "%s: Failed to resolve server part of %s to IP: %d\n",
  159. __func__, name, rc);
  160. goto compose_mount_options_err;
  161. }
  162. /*
  163. * In most cases, we'll be building a shorter string than the original,
  164. * but we do have to assume that the address in the ip= option may be
  165. * much longer than the original. Add the max length of an address
  166. * string to the length of the original string to allow for worst case.
  167. */
  168. md_len = strlen(sb_mountdata) + INET6_ADDRSTRLEN;
  169. mountdata = kzalloc(md_len + sizeof("ip=") + 1, GFP_KERNEL);
  170. if (mountdata == NULL) {
  171. rc = -ENOMEM;
  172. goto compose_mount_options_err;
  173. }
  174. /* copy all options except of unc,ip,prefixpath */
  175. off = 0;
  176. if (strncmp(sb_mountdata, "sep=", 4) == 0) {
  177. sep = sb_mountdata[4];
  178. strncpy(mountdata, sb_mountdata, 5);
  179. off += 5;
  180. }
  181. do {
  182. tkn_e = strchr(sb_mountdata + off, sep);
  183. if (tkn_e == NULL)
  184. noff = strlen(sb_mountdata + off);
  185. else
  186. noff = tkn_e - (sb_mountdata + off) + 1;
  187. if (strncasecmp(sb_mountdata + off, "unc=", 4) == 0) {
  188. off += noff;
  189. continue;
  190. }
  191. if (strncasecmp(sb_mountdata + off, "ip=", 3) == 0) {
  192. off += noff;
  193. continue;
  194. }
  195. if (strncasecmp(sb_mountdata + off, "prefixpath=", 11) == 0) {
  196. off += noff;
  197. continue;
  198. }
  199. strncat(mountdata, sb_mountdata + off, noff);
  200. off += noff;
  201. } while (tkn_e);
  202. strcat(mountdata, sb_mountdata + off);
  203. mountdata[md_len] = '\0';
  204. /* copy new IP and ref share name */
  205. if (mountdata[strlen(mountdata) - 1] != sep)
  206. strncat(mountdata, &sep, 1);
  207. strcat(mountdata, "ip=");
  208. strcat(mountdata, srvIP);
  209. if (devname)
  210. *devname = name;
  211. else
  212. kfree(name);
  213. /*cifs_dbg(FYI, "%s: parent mountdata: %s\n", __func__, sb_mountdata);*/
  214. /*cifs_dbg(FYI, "%s: submount mountdata: %s\n", __func__, mountdata );*/
  215. compose_mount_options_out:
  216. kfree(srvIP);
  217. return mountdata;
  218. compose_mount_options_err:
  219. kfree(mountdata);
  220. mountdata = ERR_PTR(rc);
  221. kfree(name);
  222. goto compose_mount_options_out;
  223. }
  224. /**
  225. * cifs_dfs_do_mount - mounts specified path using DFS full path
  226. *
  227. * Always pass down @fullpath to smb3_do_mount() so we can use the root server
  228. * to perform failover in case we failed to connect to the first target in the
  229. * referral.
  230. *
  231. * @cifs_sb: parent/root superblock
  232. * @fullpath: full path in UNC format
  233. */
  234. static struct vfsmount *cifs_dfs_do_mount(struct dentry *mntpt,
  235. struct cifs_sb_info *cifs_sb,
  236. const char *fullpath)
  237. {
  238. struct vfsmount *mnt;
  239. char *mountdata;
  240. char *devname;
  241. devname = kstrndup(fullpath, strlen(fullpath), GFP_KERNEL);
  242. if (!devname)
  243. return ERR_PTR(-ENOMEM);
  244. convert_delimiter(devname, '/');
  245. /* strip first '\' from fullpath */
  246. mountdata = cifs_compose_mount_options(cifs_sb->mountdata,
  247. fullpath + 1, NULL, NULL);
  248. if (IS_ERR(mountdata)) {
  249. kfree(devname);
  250. return (struct vfsmount *)mountdata;
  251. }
  252. mnt = vfs_submount(mntpt, &cifs_fs_type, devname, mountdata);
  253. kfree(mountdata);
  254. kfree(devname);
  255. return mnt;
  256. }
  257. /*
  258. * Create a vfsmount that we can automount
  259. */
  260. static struct vfsmount *cifs_dfs_do_automount(struct dentry *mntpt)
  261. {
  262. struct cifs_sb_info *cifs_sb;
  263. struct cifs_ses *ses;
  264. struct cifs_tcon *tcon;
  265. char *full_path, *root_path;
  266. unsigned int xid;
  267. int rc;
  268. struct vfsmount *mnt;
  269. cifs_dbg(FYI, "in %s\n", __func__);
  270. BUG_ON(IS_ROOT(mntpt));
  271. /*
  272. * The MSDFS spec states that paths in DFS referral requests and
  273. * responses must be prefixed by a single '\' character instead of
  274. * the double backslashes usually used in the UNC. This function
  275. * gives us the latter, so we must adjust the result.
  276. */
  277. mnt = ERR_PTR(-ENOMEM);
  278. cifs_sb = CIFS_SB(mntpt->d_sb);
  279. if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) {
  280. mnt = ERR_PTR(-EREMOTE);
  281. goto cdda_exit;
  282. }
  283. /* always use tree name prefix */
  284. full_path = build_path_from_dentry_optional_prefix(mntpt, true);
  285. if (full_path == NULL)
  286. goto cdda_exit;
  287. convert_delimiter(full_path, '\\');
  288. cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
  289. if (!cifs_sb_master_tlink(cifs_sb)) {
  290. cifs_dbg(FYI, "%s: master tlink is NULL\n", __func__);
  291. goto free_full_path;
  292. }
  293. tcon = cifs_sb_master_tcon(cifs_sb);
  294. if (!tcon) {
  295. cifs_dbg(FYI, "%s: master tcon is NULL\n", __func__);
  296. goto free_full_path;
  297. }
  298. root_path = kstrdup(tcon->treeName, GFP_KERNEL);
  299. if (!root_path) {
  300. mnt = ERR_PTR(-ENOMEM);
  301. goto free_full_path;
  302. }
  303. cifs_dbg(FYI, "%s: root path: %s\n", __func__, root_path);
  304. ses = tcon->ses;
  305. xid = get_xid();
  306. /*
  307. * If DFS root has been expired, then unconditionally fetch it again to
  308. * refresh DFS referral cache.
  309. */
  310. rc = dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
  311. root_path + 1, NULL, NULL);
  312. if (!rc) {
  313. rc = dfs_cache_find(xid, ses, cifs_sb->local_nls,
  314. cifs_remap(cifs_sb), full_path + 1,
  315. NULL, NULL);
  316. }
  317. free_xid(xid);
  318. if (rc) {
  319. mnt = ERR_PTR(rc);
  320. goto free_root_path;
  321. }
  322. /*
  323. * OK - we were able to get and cache a referral for @full_path.
  324. *
  325. * Now, pass it down to cifs_mount() and it will retry every available
  326. * node server in case of failures - no need to do it here.
  327. */
  328. mnt = cifs_dfs_do_mount(mntpt, cifs_sb, full_path);
  329. cifs_dbg(FYI, "%s: cifs_dfs_do_mount:%s , mnt:%p\n", __func__,
  330. full_path + 1, mnt);
  331. free_root_path:
  332. kfree(root_path);
  333. free_full_path:
  334. kfree(full_path);
  335. cdda_exit:
  336. cifs_dbg(FYI, "leaving %s\n" , __func__);
  337. return mnt;
  338. }
  339. /*
  340. * Attempt to automount the referral
  341. */
  342. struct vfsmount *cifs_dfs_d_automount(struct path *path)
  343. {
  344. struct vfsmount *newmnt;
  345. cifs_dbg(FYI, "in %s\n", __func__);
  346. newmnt = cifs_dfs_do_automount(path->dentry);
  347. if (IS_ERR(newmnt)) {
  348. cifs_dbg(FYI, "leaving %s [automount failed]\n" , __func__);
  349. return newmnt;
  350. }
  351. mntget(newmnt); /* prevent immediate expiration */
  352. mnt_set_expiry(newmnt, &cifs_dfs_automount_list);
  353. schedule_delayed_work(&cifs_dfs_automount_task,
  354. cifs_dfs_mountpoint_expiry_timeout);
  355. cifs_dbg(FYI, "leaving %s [ok]\n" , __func__);
  356. return newmnt;
  357. }
  358. const struct inode_operations cifs_dfs_referral_inode_operations = {
  359. };