1 // SPDX-License-Identifier: GPL-2.0
6 #include <linux/namei.h>
8 #include "cifs_debug.h"
9 #include "dns_resolve.h"
10 #include "fs_context.h"
14 * dfs_parse_target_referral - set fs context for dfs target referral
16 * @full_path: full path in UNC format.
17 * @ref: dfs referral pointer.
18 * @ctx: smb3 fs context pointer.
20 * Return zero if dfs referral was parsed correctly, otherwise non-zero.
22 int dfs_parse_target_referral(const char *full_path, const struct dfs_info3_param *ref,
23 struct smb3_fs_context *ctx)
26 const char *prepath = NULL;
29 if (!full_path || !*full_path || !ref || !ctx)
32 if (WARN_ON_ONCE(!ref->node_name || ref->path_consumed < 0))
35 if (strlen(full_path) - ref->path_consumed) {
36 prepath = full_path + ref->path_consumed;
37 /* skip initial delimiter */
38 if (*prepath == '/' || *prepath == '\\')
42 path = cifs_build_devname(ref->node_name, prepath);
46 rc = smb3_parse_devname(path, ctx);
50 rc = dns_resolve_server_name_to_ip(path, (struct sockaddr *)&ctx->dstaddr, NULL);
58 * cifs_build_path_to_root returns full path to root when we do not have an
59 * existing connection (tcon)
61 static char *build_unc_path_to_root(const struct smb3_fs_context *ctx,
62 const struct cifs_sb_info *cifs_sb, bool useppath)
64 char *full_path, *pos;
65 unsigned int pplen = useppath && ctx->prepath ? strlen(ctx->prepath) + 1 : 0;
66 unsigned int unc_len = strnlen(ctx->UNC, MAX_TREE_SIZE + 1);
68 if (unc_len > MAX_TREE_SIZE)
69 return ERR_PTR(-EINVAL);
71 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
72 if (full_path == NULL)
73 return ERR_PTR(-ENOMEM);
75 memcpy(full_path, ctx->UNC, unc_len);
76 pos = full_path + unc_len;
79 *pos = CIFS_DIR_SEP(cifs_sb);
80 memcpy(pos + 1, ctx->prepath, pplen);
84 *pos = '\0'; /* add trailing null */
85 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
86 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
90 static int get_session(struct cifs_mount_ctx *mnt_ctx, const char *full_path)
92 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
95 ctx->leaf_fullpath = (char *)full_path;
96 rc = cifs_mount_get_session(mnt_ctx);
97 ctx->leaf_fullpath = NULL;
102 static int get_root_smb_session(struct cifs_mount_ctx *mnt_ctx)
104 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
105 struct dfs_root_ses *root_ses;
106 struct cifs_ses *ses = mnt_ctx->ses;
109 root_ses = kmalloc(sizeof(*root_ses), GFP_KERNEL);
113 INIT_LIST_HEAD(&root_ses->list);
115 spin_lock(&cifs_tcp_ses_lock);
117 spin_unlock(&cifs_tcp_ses_lock);
119 list_add_tail(&root_ses->list, &mnt_ctx->dfs_ses_list);
121 ctx->dfs_root_ses = ses;
125 static int get_dfs_conn(struct cifs_mount_ctx *mnt_ctx, const char *ref_path, const char *full_path,
126 const struct dfs_cache_tgt_iterator *tit)
128 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
129 struct dfs_info3_param ref = {};
130 bool is_refsrv = false;
133 rc = dfs_cache_get_tgt_referral(ref_path + 1, tit, &ref);
137 rc = dfs_parse_target_referral(full_path + 1, &ref, ctx);
141 cifs_mount_put_conns(mnt_ctx);
142 rc = get_session(mnt_ctx, ref_path);
146 is_refsrv = !!(ref.flags & DFSREF_REFERRAL_SERVER);
149 if (ref.flags & DFSREF_STORAGE_SERVER) {
150 rc = cifs_mount_get_tcon(mnt_ctx);
154 /* some servers may not advertise referral capability under ref.flags */
155 is_refsrv |= is_tcon_dfs(mnt_ctx->tcon);
157 rc = cifs_is_path_remote(mnt_ctx);
160 if (rc == -EREMOTE && is_refsrv) {
161 rc2 = get_root_smb_session(mnt_ctx);
167 free_dfs_info_param(&ref);
171 static int __dfs_mount_share(struct cifs_mount_ctx *mnt_ctx)
173 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
174 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
175 char *ref_path = NULL, *full_path = NULL;
176 struct dfs_cache_tgt_iterator *tit;
177 struct TCP_Server_Info *server;
178 struct cifs_tcon *tcon;
179 char *origin_fullpath = NULL;
183 ref_path = dfs_get_path(cifs_sb, ctx->UNC);
184 if (IS_ERR(ref_path))
185 return PTR_ERR(ref_path);
187 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
188 if (IS_ERR(full_path)) {
189 rc = PTR_ERR(full_path);
194 origin_fullpath = kstrdup(full_path, GFP_KERNEL);
195 if (!origin_fullpath) {
201 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
203 rc = dfs_get_referral(mnt_ctx, ref_path + 1, NULL, &tl);
207 tit = dfs_cache_get_tgt_iterator(&tl);
209 cifs_dbg(VFS, "%s: dfs referral (%s) with no targets\n", __func__,
212 dfs_cache_free_tgts(&tl);
217 rc = get_dfs_conn(mnt_ctx, ref_path, full_path, tit);
220 if (rc == -EREMOTE) {
221 if (++num_links > MAX_NESTED_LINKS) {
227 ref_path = full_path = NULL;
229 full_path = build_unc_path_to_root(ctx, cifs_sb, true);
230 if (IS_ERR(full_path)) {
231 rc = PTR_ERR(full_path);
234 ref_path = dfs_get_path(cifs_sb, full_path);
235 if (IS_ERR(ref_path)) {
236 rc = PTR_ERR(ref_path);
242 } while ((tit = dfs_cache_get_next_tgt(&tl, tit)));
243 dfs_cache_free_tgts(&tl);
244 } while (rc == -EREMOTE);
247 server = mnt_ctx->server;
248 tcon = mnt_ctx->tcon;
250 mutex_lock(&server->refpath_lock);
251 if (!server->origin_fullpath) {
252 server->origin_fullpath = origin_fullpath;
253 server->current_fullpath = server->leaf_fullpath;
254 origin_fullpath = NULL;
256 mutex_unlock(&server->refpath_lock);
258 if (list_empty(&tcon->dfs_ses_list)) {
259 list_replace_init(&mnt_ctx->dfs_ses_list,
260 &tcon->dfs_ses_list);
262 dfs_put_root_smb_sessions(&mnt_ctx->dfs_ses_list);
267 kfree(origin_fullpath);
273 int dfs_mount_share(struct cifs_mount_ctx *mnt_ctx, bool *isdfs)
275 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
276 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
281 rc = get_session(mnt_ctx, NULL);
284 ctx->dfs_root_ses = mnt_ctx->ses;
286 * If called with 'nodfs' mount option, then skip DFS resolving. Otherwise unconditionally
287 * try to get an DFS referral (even cached) to determine whether it is an DFS mount.
289 * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem
290 * to respond with PATH_NOT_COVERED to requests that include the prefix.
292 if ((cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS) ||
293 dfs_get_referral(mnt_ctx, ctx->UNC + 1, NULL, NULL)) {
294 rc = cifs_mount_get_tcon(mnt_ctx);
298 rc = cifs_is_path_remote(mnt_ctx);
299 if (!rc || rc != -EREMOTE)
304 rc = get_root_smb_session(mnt_ctx);
308 return __dfs_mount_share(mnt_ctx);
311 /* Update dfs referral path of superblock */
312 static int update_server_fullpath(struct TCP_Server_Info *server, struct cifs_sb_info *cifs_sb,
316 size_t len = strlen(target);
317 char *refpath, *npath;
319 if (unlikely(len < 2 || *target != '\\'))
322 if (target[1] == '\\') {
324 refpath = kmalloc(len, GFP_KERNEL);
328 scnprintf(refpath, len, "%s", target);
331 refpath = kmalloc(len, GFP_KERNEL);
335 scnprintf(refpath, len, "\\%s", target);
338 npath = dfs_cache_canonical_path(refpath, cifs_sb->local_nls, cifs_remap(cifs_sb));
344 mutex_lock(&server->refpath_lock);
345 kfree(server->leaf_fullpath);
346 server->leaf_fullpath = npath;
347 mutex_unlock(&server->refpath_lock);
348 server->current_fullpath = server->leaf_fullpath;
353 static int target_share_matches_server(struct TCP_Server_Info *server, char *share,
357 const char *dfs_host;
360 *target_match = true;
361 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
363 /* Check if hostnames or addresses match */
364 cifs_server_lock(server);
365 if (dfs_host_len != strlen(server->hostname) ||
366 strncasecmp(dfs_host, server->hostname, dfs_host_len)) {
367 cifs_dbg(FYI, "%s: %.*s doesn't match %s\n", __func__,
368 (int)dfs_host_len, dfs_host, server->hostname);
369 rc = match_target_ip(server, dfs_host, dfs_host_len, target_match);
371 cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
373 cifs_server_unlock(server);
377 static int __tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
378 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
379 struct dfs_cache_tgt_list *tl)
382 struct TCP_Server_Info *server = tcon->ses->server;
383 const struct smb_version_operations *ops = server->ops;
384 struct cifs_ses *root_ses = CIFS_DFS_ROOT_SES(tcon->ses);
385 struct cifs_tcon *ipc = root_ses->tcon_ipc;
386 char *share = NULL, *prefix = NULL;
387 struct dfs_cache_tgt_iterator *tit;
390 tit = dfs_cache_get_tgt_iterator(tl);
396 /* Try to tree connect to all dfs targets */
397 for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
398 const char *target = dfs_cache_get_tgt_name(tit);
399 struct dfs_cache_tgt_list ntl = DFS_CACHE_TGT_LIST_INIT(ntl);
403 share = prefix = NULL;
405 /* Check if share matches with tcp ses */
406 rc = dfs_cache_get_tgt_share(server->current_fullpath + 1, tit, &share, &prefix);
408 cifs_dbg(VFS, "%s: failed to parse target share: %d\n", __func__, rc);
412 rc = target_share_matches_server(server, share, &target_match);
420 dfs_cache_noreq_update_tgthint(server->current_fullpath + 1, tit);
422 if (ipc->need_reconnect) {
423 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
424 rc = ops->tree_connect(xid, ipc->ses, tree, ipc, cifs_sb->local_nls);
425 cifs_dbg(FYI, "%s: reconnect ipc: %d\n", __func__, rc);
428 scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
430 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
434 * If no dfs referrals were returned from link target, then just do a TREE_CONNECT
435 * to it. Otherwise, cache the dfs referral and then mark current tcp ses for
436 * reconnect so either the demultiplex thread or the echo worker will reconnect to
437 * newly resolved target.
439 if (dfs_cache_find(xid, root_ses, cifs_sb->local_nls, cifs_remap(cifs_sb), target,
441 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
445 rc = cifs_update_super_prepath(cifs_sb, prefix);
447 /* Target is another dfs share */
448 rc = update_server_fullpath(server, cifs_sb, target);
449 dfs_cache_free_tgts(tl);
453 list_replace_init(&ntl.tl_list, &tl->tl_list);
455 dfs_cache_free_tgts(&ntl);
467 static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
468 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
469 struct dfs_cache_tgt_list *tl)
473 struct TCP_Server_Info *server = tcon->ses->server;
474 char *old_fullpath = server->leaf_fullpath;
477 rc = __tree_connect_dfs_target(xid, tcon, cifs_sb, tree, islink, tl);
478 if (!rc || rc != -EREMOTE)
480 } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
482 * If we couldn't tree connect to any targets from last referral path, then
483 * retry it from newly resolved dfs referral.
485 if (rc && server->leaf_fullpath != old_fullpath)
486 cifs_signal_cifsd_for_reconnect(server, true);
488 dfs_cache_free_tgts(tl);
492 int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
495 struct TCP_Server_Info *server = tcon->ses->server;
496 const struct smb_version_operations *ops = server->ops;
497 struct super_block *sb = NULL;
498 struct cifs_sb_info *cifs_sb;
499 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
501 struct dfs_info3_param ref = {0};
503 /* only send once per connect */
504 spin_lock(&tcon->tc_lock);
505 if (tcon->status != TID_NEW &&
506 tcon->status != TID_NEED_TCON) {
507 spin_unlock(&tcon->tc_lock);
511 if (tcon->status == TID_GOOD) {
512 spin_unlock(&tcon->tc_lock);
515 tcon->status = TID_IN_TCON;
516 spin_unlock(&tcon->tc_lock);
518 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
525 cifs_server_lock(server);
526 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
527 cifs_server_unlock(server);
528 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
532 sb = cifs_get_tcp_super(server);
535 cifs_dbg(VFS, "%s: could not find superblock: %d\n", __func__, rc);
539 cifs_sb = CIFS_SB(sb);
541 /* If it is not dfs or there was no cached dfs referral, then reconnect to same share */
542 if (!server->current_fullpath ||
543 dfs_cache_noreq_find(server->current_fullpath + 1, &ref, &tl)) {
544 rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon, cifs_sb->local_nls);
548 rc = tree_connect_dfs_target(xid, tcon, cifs_sb, tree, ref.server_type == DFS_TYPE_LINK,
550 free_dfs_info_param(&ref);
554 cifs_put_tcp_super(sb);
557 spin_lock(&tcon->tc_lock);
558 if (tcon->status == TID_IN_TCON)
559 tcon->status = TID_NEED_TCON;
560 spin_unlock(&tcon->tc_lock);
562 spin_lock(&tcon->tc_lock);
563 if (tcon->status == TID_IN_TCON)
564 tcon->status = TID_GOOD;
565 spin_unlock(&tcon->tc_lock);
566 tcon->need_reconnect = false;