1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2022 Paulo Alcantara <palcantara@suse.de>
7 #include "cifs_debug.h"
8 #include "dns_resolve.h"
9 #include "fs_context.h"
13 * dfs_parse_target_referral - set fs context for dfs target referral
15 * @full_path: full path in UNC format.
16 * @ref: dfs referral pointer.
17 * @ctx: smb3 fs context pointer.
19 * Return zero if dfs referral was parsed correctly, otherwise non-zero.
21 int dfs_parse_target_referral(const char *full_path, const struct dfs_info3_param *ref,
22 struct smb3_fs_context *ctx)
25 const char *prepath = NULL;
28 if (!full_path || !*full_path || !ref || !ctx)
31 if (WARN_ON_ONCE(!ref->node_name || ref->path_consumed < 0))
34 if (strlen(full_path) - ref->path_consumed) {
35 prepath = full_path + ref->path_consumed;
36 /* skip initial delimiter */
37 if (*prepath == '/' || *prepath == '\\')
41 path = cifs_build_devname(ref->node_name, prepath);
45 rc = smb3_parse_devname(path, ctx);
49 rc = dns_resolve_server_name_to_ip(path, (struct sockaddr *)&ctx->dstaddr, NULL);
56 static int get_session(struct cifs_mount_ctx *mnt_ctx, const char *full_path)
58 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
61 ctx->leaf_fullpath = (char *)full_path;
62 rc = cifs_mount_get_session(mnt_ctx);
63 ctx->leaf_fullpath = NULL;
69 * Track individual DFS referral servers used by new DFS mount.
71 * On success, their lifetime will be shared by final tcon (dfs_ses_list).
72 * Otherwise, they will be put by dfs_put_root_smb_sessions() in cifs_mount().
74 static int add_root_smb_session(struct cifs_mount_ctx *mnt_ctx)
76 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
77 struct dfs_root_ses *root_ses;
78 struct cifs_ses *ses = mnt_ctx->ses;
81 root_ses = kmalloc(sizeof(*root_ses), GFP_KERNEL);
85 INIT_LIST_HEAD(&root_ses->list);
87 spin_lock(&cifs_tcp_ses_lock);
88 cifs_smb_ses_inc_refcount(ses);
89 spin_unlock(&cifs_tcp_ses_lock);
91 list_add_tail(&root_ses->list, &mnt_ctx->dfs_ses_list);
93 /* Select new DFS referral server so that new referrals go through it */
94 ctx->dfs_root_ses = ses;
98 static inline int parse_dfs_target(struct smb3_fs_context *ctx,
99 struct dfs_ref_walk *rw,
100 struct dfs_info3_param *tgt)
103 const char *fpath = ref_walk_fpath(rw) + 1;
105 rc = ref_walk_get_tgt(rw, tgt);
107 rc = dfs_parse_target_referral(fpath, tgt, ctx);
111 static int set_ref_paths(struct cifs_mount_ctx *mnt_ctx,
112 struct dfs_info3_param *tgt,
113 struct dfs_ref_walk *rw)
115 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
116 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
117 char *ref_path, *full_path;
120 full_path = smb3_fs_context_fullpath(ctx, CIFS_DIR_SEP(cifs_sb));
121 if (IS_ERR(full_path))
122 return PTR_ERR(full_path);
124 if (!tgt || (tgt->server_type == DFS_TYPE_LINK &&
125 DFS_INTERLINK(tgt->flags)))
126 ref_path = dfs_get_path(cifs_sb, ctx->UNC);
128 ref_path = dfs_get_path(cifs_sb, full_path);
129 if (IS_ERR(ref_path)) {
130 rc = PTR_ERR(ref_path);
134 ref_walk_path(rw) = ref_path;
135 ref_walk_fpath(rw) = full_path;
139 static int __dfs_referral_walk(struct cifs_mount_ctx *mnt_ctx,
140 struct dfs_ref_walk *rw)
142 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
143 struct dfs_info3_param tgt = {};
149 if (ref_walk_empty(rw)) {
150 rc = dfs_get_referral(mnt_ctx, ref_walk_path(rw) + 1,
151 NULL, ref_walk_tl(rw));
153 rc = cifs_mount_get_tcon(mnt_ctx);
155 rc = cifs_is_path_remote(mnt_ctx);
158 if (!ref_walk_num_tgts(rw)) {
164 while (ref_walk_next_tgt(rw)) {
165 rc = parse_dfs_target(ctx, rw, &tgt);
169 cifs_mount_put_conns(mnt_ctx);
170 rc = get_session(mnt_ctx, ref_walk_path(rw));
174 is_refsrv = tgt.server_type == DFS_TYPE_ROOT ||
175 DFS_INTERLINK(tgt.flags);
176 ref_walk_set_tgt_hint(rw);
178 if (tgt.flags & DFSREF_STORAGE_SERVER) {
179 rc = cifs_mount_get_tcon(mnt_ctx);
181 rc = cifs_is_path_remote(mnt_ctx);
189 rc = add_root_smb_session(mnt_ctx);
194 rc = ref_walk_advance(rw);
196 rc = set_ref_paths(mnt_ctx, &tgt, rw);
205 } while (rc && ref_walk_descend(rw));
208 free_dfs_info_param(&tgt);
212 static int dfs_referral_walk(struct cifs_mount_ctx *mnt_ctx)
214 struct dfs_ref_walk *rw;
217 rw = ref_walk_alloc();
222 rc = set_ref_paths(mnt_ctx, NULL, rw);
224 rc = __dfs_referral_walk(mnt_ctx, rw);
229 static int __dfs_mount_share(struct cifs_mount_ctx *mnt_ctx)
231 struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
232 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
233 struct cifs_tcon *tcon;
234 char *origin_fullpath;
237 origin_fullpath = dfs_get_path(cifs_sb, ctx->source);
238 if (IS_ERR(origin_fullpath))
239 return PTR_ERR(origin_fullpath);
241 rc = dfs_referral_walk(mnt_ctx);
245 tcon = mnt_ctx->tcon;
246 spin_lock(&tcon->tc_lock);
247 if (!tcon->origin_fullpath) {
248 tcon->origin_fullpath = origin_fullpath;
249 origin_fullpath = NULL;
251 spin_unlock(&tcon->tc_lock);
253 if (list_empty(&tcon->dfs_ses_list)) {
254 list_replace_init(&mnt_ctx->dfs_ses_list, &tcon->dfs_ses_list);
255 queue_delayed_work(dfscache_wq, &tcon->dfs_cache_work,
256 dfs_cache_get_ttl() * HZ);
258 dfs_put_root_smb_sessions(&mnt_ctx->dfs_ses_list);
262 kfree(origin_fullpath);
266 /* Resolve UNC hostname in @ctx->source and set ip addr in @ctx->dstaddr */
267 static int update_fs_context_dstaddr(struct smb3_fs_context *ctx)
269 struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
272 rc = dns_resolve_server_name_to_ip(ctx->source, addr, NULL);
274 cifs_set_port(addr, ctx->port);
278 int dfs_mount_share(struct cifs_mount_ctx *mnt_ctx, bool *isdfs)
280 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
281 bool nodfs = ctx->nodfs;
284 rc = update_fs_context_dstaddr(ctx);
289 rc = get_session(mnt_ctx, NULL);
293 ctx->dfs_root_ses = mnt_ctx->ses;
295 * If called with 'nodfs' mount option, then skip DFS resolving. Otherwise unconditionally
296 * try to get an DFS referral (even cached) to determine whether it is an DFS mount.
298 * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem
299 * to respond with PATH_NOT_COVERED to requests that include the prefix.
302 rc = dfs_get_referral(mnt_ctx, ctx->UNC + 1, NULL, NULL);
304 cifs_dbg(FYI, "%s: no dfs referral for %s: %d\n",
305 __func__, ctx->UNC + 1, rc);
306 cifs_dbg(FYI, "%s: assuming non-dfs mount...\n", __func__);
311 rc = cifs_mount_get_tcon(mnt_ctx);
313 rc = cifs_is_path_remote(mnt_ctx);
318 add_root_smb_session(mnt_ctx);
319 return __dfs_mount_share(mnt_ctx);
322 /* Update dfs referral path of superblock */
323 static int update_server_fullpath(struct TCP_Server_Info *server, struct cifs_sb_info *cifs_sb,
327 size_t len = strlen(target);
328 char *refpath, *npath;
330 if (unlikely(len < 2 || *target != '\\'))
333 if (target[1] == '\\') {
335 refpath = kmalloc(len, GFP_KERNEL);
339 scnprintf(refpath, len, "%s", target);
342 refpath = kmalloc(len, GFP_KERNEL);
346 scnprintf(refpath, len, "\\%s", target);
349 npath = dfs_cache_canonical_path(refpath, cifs_sb->local_nls, cifs_remap(cifs_sb));
355 mutex_lock(&server->refpath_lock);
356 spin_lock(&server->srv_lock);
357 kfree(server->leaf_fullpath);
358 server->leaf_fullpath = npath;
359 spin_unlock(&server->srv_lock);
360 mutex_unlock(&server->refpath_lock);
365 static int target_share_matches_server(struct TCP_Server_Info *server, char *share,
369 const char *dfs_host;
372 *target_match = true;
373 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
375 /* Check if hostnames or addresses match */
376 cifs_server_lock(server);
377 if (dfs_host_len != strlen(server->hostname) ||
378 strncasecmp(dfs_host, server->hostname, dfs_host_len)) {
379 cifs_dbg(FYI, "%s: %.*s doesn't match %s\n", __func__,
380 (int)dfs_host_len, dfs_host, server->hostname);
381 rc = match_target_ip(server, dfs_host, dfs_host_len, target_match);
383 cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
385 cifs_server_unlock(server);
389 static void __tree_connect_ipc(const unsigned int xid, char *tree,
390 struct cifs_sb_info *cifs_sb,
391 struct cifs_ses *ses)
393 struct TCP_Server_Info *server = ses->server;
394 struct cifs_tcon *tcon = ses->tcon_ipc;
397 spin_lock(&ses->ses_lock);
398 spin_lock(&ses->chan_lock);
399 if (cifs_chan_needs_reconnect(ses, server) ||
400 ses->ses_status != SES_GOOD) {
401 spin_unlock(&ses->chan_lock);
402 spin_unlock(&ses->ses_lock);
403 cifs_server_dbg(FYI, "%s: skipping ipc reconnect due to disconnected ses\n",
407 spin_unlock(&ses->chan_lock);
408 spin_unlock(&ses->ses_lock);
410 cifs_server_lock(server);
411 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
412 cifs_server_unlock(server);
414 rc = server->ops->tree_connect(xid, ses, tree, tcon,
416 cifs_server_dbg(FYI, "%s: tree_reconnect %s: %d\n", __func__, tree, rc);
417 spin_lock(&tcon->tc_lock);
419 tcon->status = TID_NEED_TCON;
421 tcon->status = TID_GOOD;
422 tcon->need_reconnect = false;
424 spin_unlock(&tcon->tc_lock);
427 static void tree_connect_ipc(const unsigned int xid, char *tree,
428 struct cifs_sb_info *cifs_sb,
429 struct cifs_tcon *tcon)
431 struct cifs_ses *ses = tcon->ses;
433 __tree_connect_ipc(xid, tree, cifs_sb, ses);
434 __tree_connect_ipc(xid, tree, cifs_sb, CIFS_DFS_ROOT_SES(ses));
437 static int __tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
438 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
439 struct dfs_cache_tgt_list *tl)
442 struct TCP_Server_Info *server = tcon->ses->server;
443 const struct smb_version_operations *ops = server->ops;
444 struct cifs_ses *root_ses = CIFS_DFS_ROOT_SES(tcon->ses);
445 char *share = NULL, *prefix = NULL;
446 struct dfs_cache_tgt_iterator *tit;
449 tit = dfs_cache_get_tgt_iterator(tl);
455 /* Try to tree connect to all dfs targets */
456 for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
457 const char *target = dfs_cache_get_tgt_name(tit);
458 DFS_CACHE_TGT_LIST(ntl);
462 share = prefix = NULL;
464 /* Check if share matches with tcp ses */
465 rc = dfs_cache_get_tgt_share(server->leaf_fullpath + 1, tit, &share, &prefix);
467 cifs_dbg(VFS, "%s: failed to parse target share: %d\n", __func__, rc);
471 rc = target_share_matches_server(server, share, &target_match);
479 dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, tit);
480 tree_connect_ipc(xid, tree, cifs_sb, tcon);
482 scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
484 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
489 * If no dfs referrals were returned from link target, then just do a TREE_CONNECT
490 * to it. Otherwise, cache the dfs referral and then mark current tcp ses for
491 * reconnect so either the demultiplex thread or the echo worker will reconnect to
492 * newly resolved target.
494 if (dfs_cache_find(xid, root_ses, cifs_sb->local_nls, cifs_remap(cifs_sb), target,
496 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
500 rc = cifs_update_super_prepath(cifs_sb, prefix);
502 /* Target is another dfs share */
503 rc = update_server_fullpath(server, cifs_sb, target);
504 dfs_cache_free_tgts(tl);
508 list_replace_init(&ntl.tl_list, &tl->tl_list);
510 dfs_cache_free_tgts(&ntl);
522 static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
523 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
524 struct dfs_cache_tgt_list *tl)
528 struct TCP_Server_Info *server = tcon->ses->server;
529 char *old_fullpath = server->leaf_fullpath;
532 rc = __tree_connect_dfs_target(xid, tcon, cifs_sb, tree, islink, tl);
533 if (!rc || rc != -EREMOTE)
535 } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
537 * If we couldn't tree connect to any targets from last referral path, then
538 * retry it from newly resolved dfs referral.
540 if (rc && server->leaf_fullpath != old_fullpath)
541 cifs_signal_cifsd_for_reconnect(server, true);
543 dfs_cache_free_tgts(tl);
547 int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
550 struct TCP_Server_Info *server = tcon->ses->server;
551 const struct smb_version_operations *ops = server->ops;
552 DFS_CACHE_TGT_LIST(tl);
553 struct cifs_sb_info *cifs_sb = NULL;
554 struct super_block *sb = NULL;
555 struct dfs_info3_param ref = {0};
558 /* only send once per connect */
559 spin_lock(&tcon->tc_lock);
560 if (tcon->status == TID_GOOD) {
561 spin_unlock(&tcon->tc_lock);
565 if (tcon->status != TID_NEW &&
566 tcon->status != TID_NEED_TCON) {
567 spin_unlock(&tcon->tc_lock);
571 tcon->status = TID_IN_TCON;
572 spin_unlock(&tcon->tc_lock);
574 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
581 cifs_server_lock(server);
582 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
583 cifs_server_unlock(server);
584 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
588 sb = cifs_get_dfs_tcon_super(tcon);
590 cifs_sb = CIFS_SB(sb);
593 * Tree connect to last share in @tcon->tree_name whether dfs super or
594 * cached dfs referral was not found.
596 if (!cifs_sb || !server->leaf_fullpath ||
597 dfs_cache_noreq_find(server->leaf_fullpath + 1, &ref, &tl)) {
598 rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon,
599 cifs_sb ? cifs_sb->local_nls : nlsc);
603 rc = tree_connect_dfs_target(xid, tcon, cifs_sb, tree, ref.server_type == DFS_TYPE_LINK,
605 free_dfs_info_param(&ref);
609 cifs_put_tcp_super(sb);
612 spin_lock(&tcon->tc_lock);
613 if (tcon->status == TID_IN_TCON)
614 tcon->status = TID_NEED_TCON;
615 spin_unlock(&tcon->tc_lock);
617 spin_lock(&tcon->tc_lock);
618 if (tcon->status == TID_IN_TCON)
619 tcon->status = TID_GOOD;
620 spin_unlock(&tcon->tc_lock);
621 tcon->need_reconnect = false;