1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2022 Paulo Alcantara <palcantara@suse.de>
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);
57 static int get_session(struct cifs_mount_ctx *mnt_ctx, const char *full_path)
59 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
62 ctx->leaf_fullpath = (char *)full_path;
63 rc = cifs_mount_get_session(mnt_ctx);
64 ctx->leaf_fullpath = NULL;
70 * Track individual DFS referral servers used by new DFS mount.
72 * On success, their lifetime will be shared by final tcon (dfs_ses_list).
73 * Otherwise, they will be put by dfs_put_root_smb_sessions() in cifs_mount().
75 static int add_root_smb_session(struct cifs_mount_ctx *mnt_ctx)
77 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
78 struct dfs_root_ses *root_ses;
79 struct cifs_ses *ses = mnt_ctx->ses;
82 root_ses = kmalloc(sizeof(*root_ses), GFP_KERNEL);
86 INIT_LIST_HEAD(&root_ses->list);
88 spin_lock(&cifs_tcp_ses_lock);
89 cifs_smb_ses_inc_refcount(ses);
90 spin_unlock(&cifs_tcp_ses_lock);
92 list_add_tail(&root_ses->list, &mnt_ctx->dfs_ses_list);
94 /* Select new DFS referral server so that new referrals go through it */
95 ctx->dfs_root_ses = ses;
99 static int get_dfs_conn(struct cifs_mount_ctx *mnt_ctx, const char *ref_path, const char *full_path,
100 const struct dfs_cache_tgt_iterator *tit)
102 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
103 struct dfs_info3_param ref = {};
107 rc = dfs_cache_get_tgt_referral(ref_path + 1, tit, &ref);
111 rc = dfs_parse_target_referral(full_path + 1, &ref, ctx);
115 cifs_mount_put_conns(mnt_ctx);
116 rc = get_session(mnt_ctx, ref_path);
120 is_refsrv = !!(ref.flags & DFSREF_REFERRAL_SERVER);
123 if (ref.flags & DFSREF_STORAGE_SERVER) {
124 rc = cifs_mount_get_tcon(mnt_ctx);
128 /* some servers may not advertise referral capability under ref.flags */
129 is_refsrv |= is_tcon_dfs(mnt_ctx->tcon);
131 rc = cifs_is_path_remote(mnt_ctx);
134 dfs_cache_noreq_update_tgthint(ref_path + 1, tit);
136 if (rc == -EREMOTE && is_refsrv) {
137 rc2 = add_root_smb_session(mnt_ctx);
143 free_dfs_info_param(&ref);
147 static int __dfs_mount_share(struct cifs_mount_ctx *mnt_ctx)
149 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
150 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
151 char *ref_path = NULL, *full_path = NULL;
152 struct dfs_cache_tgt_iterator *tit;
153 struct cifs_tcon *tcon;
154 char *origin_fullpath = NULL;
155 char sep = CIFS_DIR_SEP(cifs_sb);
159 ref_path = dfs_get_path(cifs_sb, ctx->UNC);
160 if (IS_ERR(ref_path))
161 return PTR_ERR(ref_path);
163 full_path = smb3_fs_context_fullpath(ctx, sep);
164 if (IS_ERR(full_path)) {
165 rc = PTR_ERR(full_path);
170 origin_fullpath = kstrdup(full_path, GFP_KERNEL);
171 if (!origin_fullpath) {
177 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
179 rc = dfs_get_referral(mnt_ctx, ref_path + 1, NULL, &tl);
181 rc = cifs_mount_get_tcon(mnt_ctx);
183 rc = cifs_is_path_remote(mnt_ctx);
187 tit = dfs_cache_get_tgt_iterator(&tl);
189 cifs_dbg(VFS, "%s: dfs referral (%s) with no targets\n", __func__,
192 dfs_cache_free_tgts(&tl);
197 rc = get_dfs_conn(mnt_ctx, ref_path, full_path, tit);
200 if (rc == -EREMOTE) {
201 if (++num_links > MAX_NESTED_LINKS) {
207 ref_path = full_path = NULL;
209 full_path = smb3_fs_context_fullpath(ctx, sep);
210 if (IS_ERR(full_path)) {
211 rc = PTR_ERR(full_path);
214 ref_path = dfs_get_path(cifs_sb, full_path);
215 if (IS_ERR(ref_path)) {
216 rc = PTR_ERR(ref_path);
222 } while ((tit = dfs_cache_get_next_tgt(&tl, tit)));
223 dfs_cache_free_tgts(&tl);
224 } while (rc == -EREMOTE);
227 tcon = mnt_ctx->tcon;
229 spin_lock(&tcon->tc_lock);
230 if (!tcon->origin_fullpath) {
231 tcon->origin_fullpath = origin_fullpath;
232 origin_fullpath = NULL;
234 spin_unlock(&tcon->tc_lock);
236 if (list_empty(&tcon->dfs_ses_list)) {
237 list_replace_init(&mnt_ctx->dfs_ses_list,
238 &tcon->dfs_ses_list);
239 queue_delayed_work(dfscache_wq, &tcon->dfs_cache_work,
240 dfs_cache_get_ttl() * HZ);
242 dfs_put_root_smb_sessions(&mnt_ctx->dfs_ses_list);
247 kfree(origin_fullpath);
253 int dfs_mount_share(struct cifs_mount_ctx *mnt_ctx, bool *isdfs)
255 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
256 bool nodfs = ctx->nodfs;
260 rc = get_session(mnt_ctx, NULL);
264 ctx->dfs_root_ses = mnt_ctx->ses;
266 * If called with 'nodfs' mount option, then skip DFS resolving. Otherwise unconditionally
267 * try to get an DFS referral (even cached) to determine whether it is an DFS mount.
269 * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem
270 * to respond with PATH_NOT_COVERED to requests that include the prefix.
273 rc = dfs_get_referral(mnt_ctx, ctx->UNC + 1, NULL, NULL);
275 cifs_dbg(FYI, "%s: no dfs referral for %s: %d\n",
276 __func__, ctx->UNC + 1, rc);
277 cifs_dbg(FYI, "%s: assuming non-dfs mount...\n", __func__);
282 rc = cifs_mount_get_tcon(mnt_ctx);
284 rc = cifs_is_path_remote(mnt_ctx);
289 add_root_smb_session(mnt_ctx);
290 return __dfs_mount_share(mnt_ctx);
293 /* Update dfs referral path of superblock */
294 static int update_server_fullpath(struct TCP_Server_Info *server, struct cifs_sb_info *cifs_sb,
298 size_t len = strlen(target);
299 char *refpath, *npath;
301 if (unlikely(len < 2 || *target != '\\'))
304 if (target[1] == '\\') {
306 refpath = kmalloc(len, GFP_KERNEL);
310 scnprintf(refpath, len, "%s", target);
313 refpath = kmalloc(len, GFP_KERNEL);
317 scnprintf(refpath, len, "\\%s", target);
320 npath = dfs_cache_canonical_path(refpath, cifs_sb->local_nls, cifs_remap(cifs_sb));
326 mutex_lock(&server->refpath_lock);
327 spin_lock(&server->srv_lock);
328 kfree(server->leaf_fullpath);
329 server->leaf_fullpath = npath;
330 spin_unlock(&server->srv_lock);
331 mutex_unlock(&server->refpath_lock);
336 static int target_share_matches_server(struct TCP_Server_Info *server, char *share,
340 const char *dfs_host;
343 *target_match = true;
344 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
346 /* Check if hostnames or addresses match */
347 cifs_server_lock(server);
348 if (dfs_host_len != strlen(server->hostname) ||
349 strncasecmp(dfs_host, server->hostname, dfs_host_len)) {
350 cifs_dbg(FYI, "%s: %.*s doesn't match %s\n", __func__,
351 (int)dfs_host_len, dfs_host, server->hostname);
352 rc = match_target_ip(server, dfs_host, dfs_host_len, target_match);
354 cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
356 cifs_server_unlock(server);
360 static void __tree_connect_ipc(const unsigned int xid, char *tree,
361 struct cifs_sb_info *cifs_sb,
362 struct cifs_ses *ses)
364 struct TCP_Server_Info *server = ses->server;
365 struct cifs_tcon *tcon = ses->tcon_ipc;
368 spin_lock(&ses->ses_lock);
369 spin_lock(&ses->chan_lock);
370 if (cifs_chan_needs_reconnect(ses, server) ||
371 ses->ses_status != SES_GOOD) {
372 spin_unlock(&ses->chan_lock);
373 spin_unlock(&ses->ses_lock);
374 cifs_server_dbg(FYI, "%s: skipping ipc reconnect due to disconnected ses\n",
378 spin_unlock(&ses->chan_lock);
379 spin_unlock(&ses->ses_lock);
381 cifs_server_lock(server);
382 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
383 cifs_server_unlock(server);
385 rc = server->ops->tree_connect(xid, ses, tree, tcon,
387 cifs_server_dbg(FYI, "%s: tree_reconnect %s: %d\n", __func__, tree, rc);
388 spin_lock(&tcon->tc_lock);
390 tcon->status = TID_NEED_TCON;
392 tcon->status = TID_GOOD;
393 tcon->need_reconnect = false;
395 spin_unlock(&tcon->tc_lock);
398 static void tree_connect_ipc(const unsigned int xid, char *tree,
399 struct cifs_sb_info *cifs_sb,
400 struct cifs_tcon *tcon)
402 struct cifs_ses *ses = tcon->ses;
404 __tree_connect_ipc(xid, tree, cifs_sb, ses);
405 __tree_connect_ipc(xid, tree, cifs_sb, CIFS_DFS_ROOT_SES(ses));
408 static int __tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
409 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
410 struct dfs_cache_tgt_list *tl)
413 struct TCP_Server_Info *server = tcon->ses->server;
414 const struct smb_version_operations *ops = server->ops;
415 struct cifs_ses *root_ses = CIFS_DFS_ROOT_SES(tcon->ses);
416 char *share = NULL, *prefix = NULL;
417 struct dfs_cache_tgt_iterator *tit;
420 tit = dfs_cache_get_tgt_iterator(tl);
426 /* Try to tree connect to all dfs targets */
427 for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
428 const char *target = dfs_cache_get_tgt_name(tit);
429 struct dfs_cache_tgt_list ntl = DFS_CACHE_TGT_LIST_INIT(ntl);
433 share = prefix = NULL;
435 /* Check if share matches with tcp ses */
436 rc = dfs_cache_get_tgt_share(server->leaf_fullpath + 1, tit, &share, &prefix);
438 cifs_dbg(VFS, "%s: failed to parse target share: %d\n", __func__, rc);
442 rc = target_share_matches_server(server, share, &target_match);
450 dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, tit);
451 tree_connect_ipc(xid, tree, cifs_sb, tcon);
453 scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
455 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
460 * If no dfs referrals were returned from link target, then just do a TREE_CONNECT
461 * to it. Otherwise, cache the dfs referral and then mark current tcp ses for
462 * reconnect so either the demultiplex thread or the echo worker will reconnect to
463 * newly resolved target.
465 if (dfs_cache_find(xid, root_ses, cifs_sb->local_nls, cifs_remap(cifs_sb), target,
467 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
471 rc = cifs_update_super_prepath(cifs_sb, prefix);
473 /* Target is another dfs share */
474 rc = update_server_fullpath(server, cifs_sb, target);
475 dfs_cache_free_tgts(tl);
479 list_replace_init(&ntl.tl_list, &tl->tl_list);
481 dfs_cache_free_tgts(&ntl);
493 static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
494 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
495 struct dfs_cache_tgt_list *tl)
499 struct TCP_Server_Info *server = tcon->ses->server;
500 char *old_fullpath = server->leaf_fullpath;
503 rc = __tree_connect_dfs_target(xid, tcon, cifs_sb, tree, islink, tl);
504 if (!rc || rc != -EREMOTE)
506 } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
508 * If we couldn't tree connect to any targets from last referral path, then
509 * retry it from newly resolved dfs referral.
511 if (rc && server->leaf_fullpath != old_fullpath)
512 cifs_signal_cifsd_for_reconnect(server, true);
514 dfs_cache_free_tgts(tl);
518 int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
521 struct TCP_Server_Info *server = tcon->ses->server;
522 const struct smb_version_operations *ops = server->ops;
523 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
524 struct cifs_sb_info *cifs_sb = NULL;
525 struct super_block *sb = NULL;
526 struct dfs_info3_param ref = {0};
529 /* only send once per connect */
530 spin_lock(&tcon->tc_lock);
531 if (tcon->status == TID_GOOD) {
532 spin_unlock(&tcon->tc_lock);
536 if (tcon->status != TID_NEW &&
537 tcon->status != TID_NEED_TCON) {
538 spin_unlock(&tcon->tc_lock);
542 tcon->status = TID_IN_TCON;
543 spin_unlock(&tcon->tc_lock);
545 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
552 cifs_server_lock(server);
553 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
554 cifs_server_unlock(server);
555 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
559 sb = cifs_get_dfs_tcon_super(tcon);
561 cifs_sb = CIFS_SB(sb);
564 * Tree connect to last share in @tcon->tree_name whether dfs super or
565 * cached dfs referral was not found.
567 if (!cifs_sb || !server->leaf_fullpath ||
568 dfs_cache_noreq_find(server->leaf_fullpath + 1, &ref, &tl)) {
569 rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon,
570 cifs_sb ? cifs_sb->local_nls : nlsc);
574 rc = tree_connect_dfs_target(xid, tcon, cifs_sb, tree, ref.server_type == DFS_TYPE_LINK,
576 free_dfs_info_param(&ref);
580 cifs_put_tcp_super(sb);
583 spin_lock(&tcon->tc_lock);
584 if (tcon->status == TID_IN_TCON)
585 tcon->status = TID_NEED_TCON;
586 spin_unlock(&tcon->tc_lock);
588 spin_lock(&tcon->tc_lock);
589 if (tcon->status == TID_IN_TCON)
590 tcon->status = TID_GOOD;
591 spin_unlock(&tcon->tc_lock);
592 tcon->need_reconnect = false;