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);
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);
183 tit = dfs_cache_get_tgt_iterator(&tl);
185 cifs_dbg(VFS, "%s: dfs referral (%s) with no targets\n", __func__,
188 dfs_cache_free_tgts(&tl);
193 rc = get_dfs_conn(mnt_ctx, ref_path, full_path, tit);
196 if (rc == -EREMOTE) {
197 if (++num_links > MAX_NESTED_LINKS) {
203 ref_path = full_path = NULL;
205 full_path = smb3_fs_context_fullpath(ctx, sep);
206 if (IS_ERR(full_path)) {
207 rc = PTR_ERR(full_path);
210 ref_path = dfs_get_path(cifs_sb, full_path);
211 if (IS_ERR(ref_path)) {
212 rc = PTR_ERR(ref_path);
218 } while ((tit = dfs_cache_get_next_tgt(&tl, tit)));
219 dfs_cache_free_tgts(&tl);
220 } while (rc == -EREMOTE);
223 tcon = mnt_ctx->tcon;
225 spin_lock(&tcon->tc_lock);
226 if (!tcon->origin_fullpath) {
227 tcon->origin_fullpath = origin_fullpath;
228 origin_fullpath = NULL;
230 spin_unlock(&tcon->tc_lock);
232 if (list_empty(&tcon->dfs_ses_list)) {
233 list_replace_init(&mnt_ctx->dfs_ses_list,
234 &tcon->dfs_ses_list);
235 queue_delayed_work(dfscache_wq, &tcon->dfs_cache_work,
236 dfs_cache_get_ttl() * HZ);
238 dfs_put_root_smb_sessions(&mnt_ctx->dfs_ses_list);
243 kfree(origin_fullpath);
249 int dfs_mount_share(struct cifs_mount_ctx *mnt_ctx, bool *isdfs)
251 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
252 bool nodfs = ctx->nodfs;
256 rc = get_session(mnt_ctx, NULL);
260 ctx->dfs_root_ses = mnt_ctx->ses;
262 * If called with 'nodfs' mount option, then skip DFS resolving. Otherwise unconditionally
263 * try to get an DFS referral (even cached) to determine whether it is an DFS mount.
265 * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem
266 * to respond with PATH_NOT_COVERED to requests that include the prefix.
269 rc = dfs_get_referral(mnt_ctx, ctx->UNC + 1, NULL, NULL);
271 cifs_dbg(FYI, "%s: no dfs referral for %s: %d\n",
272 __func__, ctx->UNC + 1, rc);
273 cifs_dbg(FYI, "%s: assuming non-dfs mount...\n", __func__);
278 rc = cifs_mount_get_tcon(mnt_ctx);
280 rc = cifs_is_path_remote(mnt_ctx);
285 add_root_smb_session(mnt_ctx);
286 return __dfs_mount_share(mnt_ctx);
289 /* Update dfs referral path of superblock */
290 static int update_server_fullpath(struct TCP_Server_Info *server, struct cifs_sb_info *cifs_sb,
294 size_t len = strlen(target);
295 char *refpath, *npath;
297 if (unlikely(len < 2 || *target != '\\'))
300 if (target[1] == '\\') {
302 refpath = kmalloc(len, GFP_KERNEL);
306 scnprintf(refpath, len, "%s", target);
309 refpath = kmalloc(len, GFP_KERNEL);
313 scnprintf(refpath, len, "\\%s", target);
316 npath = dfs_cache_canonical_path(refpath, cifs_sb->local_nls, cifs_remap(cifs_sb));
322 mutex_lock(&server->refpath_lock);
323 spin_lock(&server->srv_lock);
324 kfree(server->leaf_fullpath);
325 server->leaf_fullpath = npath;
326 spin_unlock(&server->srv_lock);
327 mutex_unlock(&server->refpath_lock);
332 static int target_share_matches_server(struct TCP_Server_Info *server, char *share,
336 const char *dfs_host;
339 *target_match = true;
340 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
342 /* Check if hostnames or addresses match */
343 cifs_server_lock(server);
344 if (dfs_host_len != strlen(server->hostname) ||
345 strncasecmp(dfs_host, server->hostname, dfs_host_len)) {
346 cifs_dbg(FYI, "%s: %.*s doesn't match %s\n", __func__,
347 (int)dfs_host_len, dfs_host, server->hostname);
348 rc = match_target_ip(server, dfs_host, dfs_host_len, target_match);
350 cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
352 cifs_server_unlock(server);
356 static void __tree_connect_ipc(const unsigned int xid, char *tree,
357 struct cifs_sb_info *cifs_sb,
358 struct cifs_ses *ses)
360 struct TCP_Server_Info *server = ses->server;
361 struct cifs_tcon *tcon = ses->tcon_ipc;
364 spin_lock(&ses->ses_lock);
365 spin_lock(&ses->chan_lock);
366 if (cifs_chan_needs_reconnect(ses, server) ||
367 ses->ses_status != SES_GOOD) {
368 spin_unlock(&ses->chan_lock);
369 spin_unlock(&ses->ses_lock);
370 cifs_server_dbg(FYI, "%s: skipping ipc reconnect due to disconnected ses\n",
374 spin_unlock(&ses->chan_lock);
375 spin_unlock(&ses->ses_lock);
377 cifs_server_lock(server);
378 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
379 cifs_server_unlock(server);
381 rc = server->ops->tree_connect(xid, ses, tree, tcon,
383 cifs_server_dbg(FYI, "%s: tree_reconnect %s: %d\n", __func__, tree, rc);
384 spin_lock(&tcon->tc_lock);
386 tcon->status = TID_NEED_TCON;
388 tcon->status = TID_GOOD;
389 tcon->need_reconnect = false;
391 spin_unlock(&tcon->tc_lock);
394 static void tree_connect_ipc(const unsigned int xid, char *tree,
395 struct cifs_sb_info *cifs_sb,
396 struct cifs_tcon *tcon)
398 struct cifs_ses *ses = tcon->ses;
400 __tree_connect_ipc(xid, tree, cifs_sb, ses);
401 __tree_connect_ipc(xid, tree, cifs_sb, CIFS_DFS_ROOT_SES(ses));
404 static int __tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
405 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
406 struct dfs_cache_tgt_list *tl)
409 struct TCP_Server_Info *server = tcon->ses->server;
410 const struct smb_version_operations *ops = server->ops;
411 struct cifs_ses *root_ses = CIFS_DFS_ROOT_SES(tcon->ses);
412 char *share = NULL, *prefix = NULL;
413 struct dfs_cache_tgt_iterator *tit;
416 tit = dfs_cache_get_tgt_iterator(tl);
422 /* Try to tree connect to all dfs targets */
423 for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
424 const char *target = dfs_cache_get_tgt_name(tit);
425 struct dfs_cache_tgt_list ntl = DFS_CACHE_TGT_LIST_INIT(ntl);
429 share = prefix = NULL;
431 /* Check if share matches with tcp ses */
432 rc = dfs_cache_get_tgt_share(server->leaf_fullpath + 1, tit, &share, &prefix);
434 cifs_dbg(VFS, "%s: failed to parse target share: %d\n", __func__, rc);
438 rc = target_share_matches_server(server, share, &target_match);
446 dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, tit);
447 tree_connect_ipc(xid, tree, cifs_sb, tcon);
449 scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
451 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
456 * If no dfs referrals were returned from link target, then just do a TREE_CONNECT
457 * to it. Otherwise, cache the dfs referral and then mark current tcp ses for
458 * reconnect so either the demultiplex thread or the echo worker will reconnect to
459 * newly resolved target.
461 if (dfs_cache_find(xid, root_ses, cifs_sb->local_nls, cifs_remap(cifs_sb), target,
463 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
467 rc = cifs_update_super_prepath(cifs_sb, prefix);
469 /* Target is another dfs share */
470 rc = update_server_fullpath(server, cifs_sb, target);
471 dfs_cache_free_tgts(tl);
475 list_replace_init(&ntl.tl_list, &tl->tl_list);
477 dfs_cache_free_tgts(&ntl);
489 static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
490 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
491 struct dfs_cache_tgt_list *tl)
495 struct TCP_Server_Info *server = tcon->ses->server;
496 char *old_fullpath = server->leaf_fullpath;
499 rc = __tree_connect_dfs_target(xid, tcon, cifs_sb, tree, islink, tl);
500 if (!rc || rc != -EREMOTE)
502 } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
504 * If we couldn't tree connect to any targets from last referral path, then
505 * retry it from newly resolved dfs referral.
507 if (rc && server->leaf_fullpath != old_fullpath)
508 cifs_signal_cifsd_for_reconnect(server, true);
510 dfs_cache_free_tgts(tl);
514 int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
517 struct TCP_Server_Info *server = tcon->ses->server;
518 const struct smb_version_operations *ops = server->ops;
519 struct dfs_cache_tgt_list tl = DFS_CACHE_TGT_LIST_INIT(tl);
520 struct cifs_sb_info *cifs_sb = NULL;
521 struct super_block *sb = NULL;
522 struct dfs_info3_param ref = {0};
525 /* only send once per connect */
526 spin_lock(&tcon->tc_lock);
527 if (tcon->status == TID_GOOD) {
528 spin_unlock(&tcon->tc_lock);
532 if (tcon->status != TID_NEW &&
533 tcon->status != TID_NEED_TCON) {
534 spin_unlock(&tcon->tc_lock);
538 tcon->status = TID_IN_TCON;
539 spin_unlock(&tcon->tc_lock);
541 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
548 cifs_server_lock(server);
549 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
550 cifs_server_unlock(server);
551 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
555 sb = cifs_get_dfs_tcon_super(tcon);
557 cifs_sb = CIFS_SB(sb);
560 * Tree connect to last share in @tcon->tree_name whether dfs super or
561 * cached dfs referral was not found.
563 if (!cifs_sb || !server->leaf_fullpath ||
564 dfs_cache_noreq_find(server->leaf_fullpath + 1, &ref, &tl)) {
565 rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon,
566 cifs_sb ? cifs_sb->local_nls : nlsc);
570 rc = tree_connect_dfs_target(xid, tcon, cifs_sb, tree, ref.server_type == DFS_TYPE_LINK,
572 free_dfs_info_param(&ref);
576 cifs_put_tcp_super(sb);
579 spin_lock(&tcon->tc_lock);
580 if (tcon->status == TID_IN_TCON)
581 tcon->status = TID_NEED_TCON;
582 spin_unlock(&tcon->tc_lock);
584 spin_lock(&tcon->tc_lock);
585 if (tcon->status == TID_IN_TCON)
586 tcon->status = TID_GOOD;
587 spin_unlock(&tcon->tc_lock);
588 tcon->need_reconnect = false;