xref: /openbmc/linux/fs/smb/client/dfs.c (revision 278002edb19bce2c628fafb0af936e77000f3a5b)
138c8a9a5SSteve French // SPDX-License-Identifier: GPL-2.0
238c8a9a5SSteve French /*
338c8a9a5SSteve French  * Copyright (c) 2022 Paulo Alcantara <palcantara@suse.de>
438c8a9a5SSteve French  */
538c8a9a5SSteve French 
638c8a9a5SSteve French #include "cifsproto.h"
738c8a9a5SSteve French #include "cifs_debug.h"
838c8a9a5SSteve French #include "dns_resolve.h"
938c8a9a5SSteve French #include "fs_context.h"
1038c8a9a5SSteve French #include "dfs.h"
1138c8a9a5SSteve French 
1238c8a9a5SSteve French /**
1338c8a9a5SSteve French  * dfs_parse_target_referral - set fs context for dfs target referral
1438c8a9a5SSteve French  *
1538c8a9a5SSteve French  * @full_path: full path in UNC format.
1638c8a9a5SSteve French  * @ref: dfs referral pointer.
1738c8a9a5SSteve French  * @ctx: smb3 fs context pointer.
1838c8a9a5SSteve French  *
1938c8a9a5SSteve French  * Return zero if dfs referral was parsed correctly, otherwise non-zero.
2038c8a9a5SSteve French  */
dfs_parse_target_referral(const char * full_path,const struct dfs_info3_param * ref,struct smb3_fs_context * ctx)2138c8a9a5SSteve French int dfs_parse_target_referral(const char *full_path, const struct dfs_info3_param *ref,
2238c8a9a5SSteve French 			      struct smb3_fs_context *ctx)
2338c8a9a5SSteve French {
2438c8a9a5SSteve French 	int rc;
2538c8a9a5SSteve French 	const char *prepath = NULL;
2638c8a9a5SSteve French 	char *path;
2738c8a9a5SSteve French 
2838c8a9a5SSteve French 	if (!full_path || !*full_path || !ref || !ctx)
2938c8a9a5SSteve French 		return -EINVAL;
3038c8a9a5SSteve French 
3138c8a9a5SSteve French 	if (WARN_ON_ONCE(!ref->node_name || ref->path_consumed < 0))
3238c8a9a5SSteve French 		return -EINVAL;
3338c8a9a5SSteve French 
3438c8a9a5SSteve French 	if (strlen(full_path) - ref->path_consumed) {
3538c8a9a5SSteve French 		prepath = full_path + ref->path_consumed;
3638c8a9a5SSteve French 		/* skip initial delimiter */
3738c8a9a5SSteve French 		if (*prepath == '/' || *prepath == '\\')
3838c8a9a5SSteve French 			prepath++;
3938c8a9a5SSteve French 	}
4038c8a9a5SSteve French 
4138c8a9a5SSteve French 	path = cifs_build_devname(ref->node_name, prepath);
4238c8a9a5SSteve French 	if (IS_ERR(path))
4338c8a9a5SSteve French 		return PTR_ERR(path);
4438c8a9a5SSteve French 
4538c8a9a5SSteve French 	rc = smb3_parse_devname(path, ctx);
4638c8a9a5SSteve French 	if (rc)
4738c8a9a5SSteve French 		goto out;
4838c8a9a5SSteve French 
4938c8a9a5SSteve French 	rc = dns_resolve_server_name_to_ip(path, (struct sockaddr *)&ctx->dstaddr, NULL);
5038c8a9a5SSteve French 
5138c8a9a5SSteve French out:
5238c8a9a5SSteve French 	kfree(path);
5338c8a9a5SSteve French 	return rc;
5438c8a9a5SSteve French }
5538c8a9a5SSteve French 
get_session(struct cifs_mount_ctx * mnt_ctx,const char * full_path)5638c8a9a5SSteve French static int get_session(struct cifs_mount_ctx *mnt_ctx, const char *full_path)
5738c8a9a5SSteve French {
5838c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
5938c8a9a5SSteve French 	int rc;
6038c8a9a5SSteve French 
6138c8a9a5SSteve French 	ctx->leaf_fullpath = (char *)full_path;
6238c8a9a5SSteve French 	rc = cifs_mount_get_session(mnt_ctx);
6338c8a9a5SSteve French 	ctx->leaf_fullpath = NULL;
6438c8a9a5SSteve French 
6538c8a9a5SSteve French 	return rc;
6638c8a9a5SSteve French }
6738c8a9a5SSteve French 
68bf99f6beSPaulo Alcantara /*
69645f332cSPaulo Alcantara  * Get an active reference of @ses so that next call to cifs_put_tcon() won't
70645f332cSPaulo Alcantara  * release it as any new DFS referrals must go through its IPC tcon.
71bf99f6beSPaulo Alcantara  */
add_root_smb_session(struct cifs_mount_ctx * mnt_ctx)72645f332cSPaulo Alcantara static void add_root_smb_session(struct cifs_mount_ctx *mnt_ctx)
7338c8a9a5SSteve French {
7438c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
7538c8a9a5SSteve French 	struct cifs_ses *ses = mnt_ctx->ses;
7638c8a9a5SSteve French 
7738c8a9a5SSteve French 	if (ses) {
7838c8a9a5SSteve French 		spin_lock(&cifs_tcp_ses_lock);
79bf99f6beSPaulo Alcantara 		cifs_smb_ses_inc_refcount(ses);
8038c8a9a5SSteve French 		spin_unlock(&cifs_tcp_ses_lock);
8138c8a9a5SSteve French 	}
8238c8a9a5SSteve French 	ctx->dfs_root_ses = ses;
8338c8a9a5SSteve French }
8438c8a9a5SSteve French 
parse_dfs_target(struct smb3_fs_context * ctx,struct dfs_ref_walk * rw,struct dfs_info3_param * tgt)85ce04127cSPaulo Alcantara static inline int parse_dfs_target(struct smb3_fs_context *ctx,
86ce04127cSPaulo Alcantara 				   struct dfs_ref_walk *rw,
87ce04127cSPaulo Alcantara 				   struct dfs_info3_param *tgt)
88ce04127cSPaulo Alcantara {
89ce04127cSPaulo Alcantara 	int rc;
90ce04127cSPaulo Alcantara 	const char *fpath = ref_walk_fpath(rw) + 1;
91ce04127cSPaulo Alcantara 
92ce04127cSPaulo Alcantara 	rc = ref_walk_get_tgt(rw, tgt);
93ce04127cSPaulo Alcantara 	if (!rc)
94ce04127cSPaulo Alcantara 		rc = dfs_parse_target_referral(fpath, tgt, ctx);
95ce04127cSPaulo Alcantara 	return rc;
96ce04127cSPaulo Alcantara }
97ce04127cSPaulo Alcantara 
set_ref_paths(struct cifs_mount_ctx * mnt_ctx,struct dfs_info3_param * tgt,struct dfs_ref_walk * rw)98ce04127cSPaulo Alcantara static int set_ref_paths(struct cifs_mount_ctx *mnt_ctx,
99ce04127cSPaulo Alcantara 			 struct dfs_info3_param *tgt,
100ce04127cSPaulo Alcantara 			 struct dfs_ref_walk *rw)
10138c8a9a5SSteve French {
10238c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
103ce04127cSPaulo Alcantara 	struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
104ce04127cSPaulo Alcantara 	char *ref_path, *full_path;
105ce04127cSPaulo Alcantara 	int rc;
10638c8a9a5SSteve French 
107ce04127cSPaulo Alcantara 	full_path = smb3_fs_context_fullpath(ctx, CIFS_DIR_SEP(cifs_sb));
108ce04127cSPaulo Alcantara 	if (IS_ERR(full_path))
109ce04127cSPaulo Alcantara 		return PTR_ERR(full_path);
110ce04127cSPaulo Alcantara 
111ce04127cSPaulo Alcantara 	if (!tgt || (tgt->server_type == DFS_TYPE_LINK &&
112ce04127cSPaulo Alcantara 		     DFS_INTERLINK(tgt->flags)))
113ce04127cSPaulo Alcantara 		ref_path = dfs_get_path(cifs_sb, ctx->UNC);
114ce04127cSPaulo Alcantara 	else
115ce04127cSPaulo Alcantara 		ref_path = dfs_get_path(cifs_sb, full_path);
116ce04127cSPaulo Alcantara 	if (IS_ERR(ref_path)) {
117ce04127cSPaulo Alcantara 		rc = PTR_ERR(ref_path);
118ce04127cSPaulo Alcantara 		kfree(full_path);
11938c8a9a5SSteve French 		return rc;
120ce04127cSPaulo Alcantara 	}
121ce04127cSPaulo Alcantara 	ref_walk_path(rw) = ref_path;
122ce04127cSPaulo Alcantara 	ref_walk_fpath(rw) = full_path;
123ce04127cSPaulo Alcantara 	return 0;
124ce04127cSPaulo Alcantara }
12538c8a9a5SSteve French 
__dfs_referral_walk(struct cifs_mount_ctx * mnt_ctx,struct dfs_ref_walk * rw)126ce04127cSPaulo Alcantara static int __dfs_referral_walk(struct cifs_mount_ctx *mnt_ctx,
127ce04127cSPaulo Alcantara 			       struct dfs_ref_walk *rw)
128ce04127cSPaulo Alcantara {
129ce04127cSPaulo Alcantara 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
130ce04127cSPaulo Alcantara 	struct dfs_info3_param tgt = {};
131ce04127cSPaulo Alcantara 	bool is_refsrv;
132ce04127cSPaulo Alcantara 	int rc = -ENOENT;
133ce04127cSPaulo Alcantara 
134ce04127cSPaulo Alcantara again:
135ce04127cSPaulo Alcantara 	do {
136ce04127cSPaulo Alcantara 		if (ref_walk_empty(rw)) {
137ce04127cSPaulo Alcantara 			rc = dfs_get_referral(mnt_ctx, ref_walk_path(rw) + 1,
138ce04127cSPaulo Alcantara 					      NULL, ref_walk_tl(rw));
139ce04127cSPaulo Alcantara 			if (rc) {
140ce04127cSPaulo Alcantara 				rc = cifs_mount_get_tcon(mnt_ctx);
141ce04127cSPaulo Alcantara 				if (!rc)
142ce04127cSPaulo Alcantara 					rc = cifs_is_path_remote(mnt_ctx);
143ce04127cSPaulo Alcantara 				continue;
144ce04127cSPaulo Alcantara 			}
145ce04127cSPaulo Alcantara 			if (!ref_walk_num_tgts(rw)) {
146ce04127cSPaulo Alcantara 				rc = -ENOENT;
147ce04127cSPaulo Alcantara 				continue;
148ce04127cSPaulo Alcantara 			}
149ce04127cSPaulo Alcantara 		}
150ce04127cSPaulo Alcantara 
151ce04127cSPaulo Alcantara 		while (ref_walk_next_tgt(rw)) {
152ce04127cSPaulo Alcantara 			rc = parse_dfs_target(ctx, rw, &tgt);
15338c8a9a5SSteve French 			if (rc)
154ce04127cSPaulo Alcantara 				continue;
15538c8a9a5SSteve French 
15638c8a9a5SSteve French 			cifs_mount_put_conns(mnt_ctx);
157ce04127cSPaulo Alcantara 			rc = get_session(mnt_ctx, ref_walk_path(rw));
15838c8a9a5SSteve French 			if (rc)
159ce04127cSPaulo Alcantara 				continue;
16038c8a9a5SSteve French 
161ce04127cSPaulo Alcantara 			is_refsrv = tgt.server_type == DFS_TYPE_ROOT ||
162ce04127cSPaulo Alcantara 				DFS_INTERLINK(tgt.flags);
163ce04127cSPaulo Alcantara 			ref_walk_set_tgt_hint(rw);
16438c8a9a5SSteve French 
165ce04127cSPaulo Alcantara 			if (tgt.flags & DFSREF_STORAGE_SERVER) {
16638c8a9a5SSteve French 				rc = cifs_mount_get_tcon(mnt_ctx);
167ce04127cSPaulo Alcantara 				if (!rc)
168ce04127cSPaulo Alcantara 					rc = cifs_is_path_remote(mnt_ctx);
169ce04127cSPaulo Alcantara 				if (!rc)
170ce04127cSPaulo Alcantara 					break;
171ce04127cSPaulo Alcantara 				if (rc != -EREMOTE)
172ce04127cSPaulo Alcantara 					continue;
173ce04127cSPaulo Alcantara 			}
174ce04127cSPaulo Alcantara 
175645f332cSPaulo Alcantara 			if (is_refsrv)
176645f332cSPaulo Alcantara 				add_root_smb_session(mnt_ctx);
17738c8a9a5SSteve French 
178ce04127cSPaulo Alcantara 			rc = ref_walk_advance(rw);
179ce04127cSPaulo Alcantara 			if (!rc) {
180ce04127cSPaulo Alcantara 				rc = set_ref_paths(mnt_ctx, &tgt, rw);
181ce04127cSPaulo Alcantara 				if (!rc) {
182ce04127cSPaulo Alcantara 					rc = -EREMOTE;
183ce04127cSPaulo Alcantara 					goto again;
18438c8a9a5SSteve French 				}
185ce04127cSPaulo Alcantara 			}
186ce04127cSPaulo Alcantara 			if (rc != -ELOOP)
187ce04127cSPaulo Alcantara 				goto out;
188ce04127cSPaulo Alcantara 		}
189ce04127cSPaulo Alcantara 	} while (rc && ref_walk_descend(rw));
19038c8a9a5SSteve French 
19138c8a9a5SSteve French out:
192ce04127cSPaulo Alcantara 	free_dfs_info_param(&tgt);
193ce04127cSPaulo Alcantara 	return rc;
194ce04127cSPaulo Alcantara }
195ce04127cSPaulo Alcantara 
dfs_referral_walk(struct cifs_mount_ctx * mnt_ctx)196ce04127cSPaulo Alcantara static int dfs_referral_walk(struct cifs_mount_ctx *mnt_ctx)
197ce04127cSPaulo Alcantara {
198ce04127cSPaulo Alcantara 	struct dfs_ref_walk *rw;
199ce04127cSPaulo Alcantara 	int rc;
200ce04127cSPaulo Alcantara 
201ce04127cSPaulo Alcantara 	rw = ref_walk_alloc();
202ce04127cSPaulo Alcantara 	if (IS_ERR(rw))
203ce04127cSPaulo Alcantara 		return PTR_ERR(rw);
204ce04127cSPaulo Alcantara 
205ce04127cSPaulo Alcantara 	ref_walk_init(rw);
206ce04127cSPaulo Alcantara 	rc = set_ref_paths(mnt_ctx, NULL, rw);
207ce04127cSPaulo Alcantara 	if (!rc)
208ce04127cSPaulo Alcantara 		rc = __dfs_referral_walk(mnt_ctx, rw);
209ce04127cSPaulo Alcantara 	ref_walk_free(rw);
21038c8a9a5SSteve French 	return rc;
21138c8a9a5SSteve French }
21238c8a9a5SSteve French 
__dfs_mount_share(struct cifs_mount_ctx * mnt_ctx)21338c8a9a5SSteve French static int __dfs_mount_share(struct cifs_mount_ctx *mnt_ctx)
21438c8a9a5SSteve French {
21538c8a9a5SSteve French 	struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb;
21638c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
21738c8a9a5SSteve French 	struct cifs_tcon *tcon;
218ce04127cSPaulo Alcantara 	char *origin_fullpath;
219645f332cSPaulo Alcantara 	bool new_tcon = true;
22038c8a9a5SSteve French 	int rc;
22138c8a9a5SSteve French 
222ce04127cSPaulo Alcantara 	origin_fullpath = dfs_get_path(cifs_sb, ctx->source);
223ce04127cSPaulo Alcantara 	if (IS_ERR(origin_fullpath))
224ce04127cSPaulo Alcantara 		return PTR_ERR(origin_fullpath);
22538c8a9a5SSteve French 
226ce04127cSPaulo Alcantara 	rc = dfs_referral_walk(mnt_ctx);
227645f332cSPaulo Alcantara 	if (!rc) {
228645f332cSPaulo Alcantara 		/*
229645f332cSPaulo Alcantara 		 * Prevent superblock from being created with any missing
230645f332cSPaulo Alcantara 		 * connections.
231645f332cSPaulo Alcantara 		 */
232645f332cSPaulo Alcantara 		if (WARN_ON(!mnt_ctx->server))
233645f332cSPaulo Alcantara 			rc = -EHOSTDOWN;
234645f332cSPaulo Alcantara 		else if (WARN_ON(!mnt_ctx->ses))
235645f332cSPaulo Alcantara 			rc = -EACCES;
236645f332cSPaulo Alcantara 		else if (WARN_ON(!mnt_ctx->tcon))
237645f332cSPaulo Alcantara 			rc = -ENOENT;
238645f332cSPaulo Alcantara 	}
239ce04127cSPaulo Alcantara 	if (rc)
24038c8a9a5SSteve French 		goto out;
24138c8a9a5SSteve French 
24238c8a9a5SSteve French 	tcon = mnt_ctx->tcon;
2433ae872deSPaulo Alcantara 	spin_lock(&tcon->tc_lock);
2443ae872deSPaulo Alcantara 	if (!tcon->origin_fullpath) {
2453ae872deSPaulo Alcantara 		tcon->origin_fullpath = origin_fullpath;
24638c8a9a5SSteve French 		origin_fullpath = NULL;
247645f332cSPaulo Alcantara 	} else {
248645f332cSPaulo Alcantara 		new_tcon = false;
24938c8a9a5SSteve French 	}
2503ae872deSPaulo Alcantara 	spin_unlock(&tcon->tc_lock);
25138c8a9a5SSteve French 
252645f332cSPaulo Alcantara 	if (new_tcon) {
25338c8a9a5SSteve French 		queue_delayed_work(dfscache_wq, &tcon->dfs_cache_work,
25438c8a9a5SSteve French 				   dfs_cache_get_ttl() * HZ);
25538c8a9a5SSteve French 	}
25638c8a9a5SSteve French 
25738c8a9a5SSteve French out:
25838c8a9a5SSteve French 	kfree(origin_fullpath);
25938c8a9a5SSteve French 	return rc;
26038c8a9a5SSteve French }
26138c8a9a5SSteve French 
262390c08fdSPaulo Alcantara /*
263390c08fdSPaulo Alcantara  * If @ctx->dfs_automount, then update @ctx->dstaddr earlier with the DFS root
264390c08fdSPaulo Alcantara  * server from where we'll start following any referrals.  Otherwise rely on the
265390c08fdSPaulo Alcantara  * value provided by mount(2) as the user might not have dns_resolver key set up
266390c08fdSPaulo Alcantara  * and therefore failing to upcall to resolve UNC hostname under @ctx->source.
267390c08fdSPaulo Alcantara  */
update_fs_context_dstaddr(struct smb3_fs_context * ctx)268348a04a8SPaulo Alcantara static int update_fs_context_dstaddr(struct smb3_fs_context *ctx)
269348a04a8SPaulo Alcantara {
270348a04a8SPaulo Alcantara 	struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
271390c08fdSPaulo Alcantara 	int rc = 0;
272348a04a8SPaulo Alcantara 
273390c08fdSPaulo Alcantara 	if (!ctx->nodfs && ctx->dfs_automount) {
274348a04a8SPaulo Alcantara 		rc = dns_resolve_server_name_to_ip(ctx->source, addr, NULL);
275348a04a8SPaulo Alcantara 		if (!rc)
276348a04a8SPaulo Alcantara 			cifs_set_port(addr, ctx->port);
277390c08fdSPaulo Alcantara 		ctx->dfs_automount = false;
278390c08fdSPaulo Alcantara 	}
279348a04a8SPaulo Alcantara 	return rc;
280348a04a8SPaulo Alcantara }
281348a04a8SPaulo Alcantara 
dfs_mount_share(struct cifs_mount_ctx * mnt_ctx,bool * isdfs)28238c8a9a5SSteve French int dfs_mount_share(struct cifs_mount_ctx *mnt_ctx, bool *isdfs)
28338c8a9a5SSteve French {
28438c8a9a5SSteve French 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
28538c8a9a5SSteve French 	bool nodfs = ctx->nodfs;
28638c8a9a5SSteve French 	int rc;
28738c8a9a5SSteve French 
288348a04a8SPaulo Alcantara 	rc = update_fs_context_dstaddr(ctx);
289348a04a8SPaulo Alcantara 	if (rc)
290348a04a8SPaulo Alcantara 		return rc;
291348a04a8SPaulo Alcantara 
29238c8a9a5SSteve French 	*isdfs = false;
29338c8a9a5SSteve French 	rc = get_session(mnt_ctx, NULL);
29438c8a9a5SSteve French 	if (rc)
2953ae872deSPaulo Alcantara 		return rc;
29638c8a9a5SSteve French 
29738c8a9a5SSteve French 	/*
29838c8a9a5SSteve French 	 * If called with 'nodfs' mount option, then skip DFS resolving.  Otherwise unconditionally
29938c8a9a5SSteve French 	 * try to get an DFS referral (even cached) to determine whether it is an DFS mount.
30038c8a9a5SSteve French 	 *
30138c8a9a5SSteve French 	 * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem
30238c8a9a5SSteve French 	 * to respond with PATH_NOT_COVERED to requests that include the prefix.
30338c8a9a5SSteve French 	 */
30438c8a9a5SSteve French 	if (!nodfs) {
30538c8a9a5SSteve French 		rc = dfs_get_referral(mnt_ctx, ctx->UNC + 1, NULL, NULL);
30638c8a9a5SSteve French 		if (rc) {
3075f2a0afaSPaulo Alcantara 			cifs_dbg(FYI, "%s: no dfs referral for %s: %d\n",
3085f2a0afaSPaulo Alcantara 				 __func__, ctx->UNC + 1, rc);
3095f2a0afaSPaulo Alcantara 			cifs_dbg(FYI, "%s: assuming non-dfs mount...\n", __func__);
31038c8a9a5SSteve French 			nodfs = true;
31138c8a9a5SSteve French 		}
31238c8a9a5SSteve French 	}
31338c8a9a5SSteve French 	if (nodfs) {
31438c8a9a5SSteve French 		rc = cifs_mount_get_tcon(mnt_ctx);
31538c8a9a5SSteve French 		if (!rc)
31638c8a9a5SSteve French 			rc = cifs_is_path_remote(mnt_ctx);
3173ae872deSPaulo Alcantara 		return rc;
31838c8a9a5SSteve French 	}
31938c8a9a5SSteve French 
32038c8a9a5SSteve French 	*isdfs = true;
321bf99f6beSPaulo Alcantara 	add_root_smb_session(mnt_ctx);
322645f332cSPaulo Alcantara 	rc = __dfs_mount_share(mnt_ctx);
323645f332cSPaulo Alcantara 	dfs_put_root_smb_sessions(mnt_ctx);
324645f332cSPaulo Alcantara 	return rc;
32538c8a9a5SSteve French }
32638c8a9a5SSteve French 
target_share_matches_server(struct TCP_Server_Info * server,char * share,bool * target_match)32738c8a9a5SSteve French static int target_share_matches_server(struct TCP_Server_Info *server, char *share,
32838c8a9a5SSteve French 				       bool *target_match)
32938c8a9a5SSteve French {
33038c8a9a5SSteve French 	int rc = 0;
33138c8a9a5SSteve French 	const char *dfs_host;
33238c8a9a5SSteve French 	size_t dfs_host_len;
33338c8a9a5SSteve French 
33438c8a9a5SSteve French 	*target_match = true;
33538c8a9a5SSteve French 	extract_unc_hostname(share, &dfs_host, &dfs_host_len);
33638c8a9a5SSteve French 
33738c8a9a5SSteve French 	/* Check if hostnames or addresses match */
33838c8a9a5SSteve French 	cifs_server_lock(server);
33938c8a9a5SSteve French 	if (dfs_host_len != strlen(server->hostname) ||
34038c8a9a5SSteve French 	    strncasecmp(dfs_host, server->hostname, dfs_host_len)) {
34138c8a9a5SSteve French 		cifs_dbg(FYI, "%s: %.*s doesn't match %s\n", __func__,
34238c8a9a5SSteve French 			 (int)dfs_host_len, dfs_host, server->hostname);
34338c8a9a5SSteve French 		rc = match_target_ip(server, dfs_host, dfs_host_len, target_match);
34438c8a9a5SSteve French 		if (rc)
34538c8a9a5SSteve French 			cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
34638c8a9a5SSteve French 	}
34738c8a9a5SSteve French 	cifs_server_unlock(server);
34838c8a9a5SSteve French 	return rc;
34938c8a9a5SSteve French }
35038c8a9a5SSteve French 
tree_connect_dfs_target(const unsigned int xid,struct cifs_tcon * tcon,struct cifs_sb_info * cifs_sb,char * tree,bool islink,struct dfs_cache_tgt_list * tl)351*2102ed90SPaulo Alcantara static int tree_connect_dfs_target(const unsigned int xid,
352*2102ed90SPaulo Alcantara 				   struct cifs_tcon *tcon,
35338c8a9a5SSteve French 				   struct cifs_sb_info *cifs_sb,
354*2102ed90SPaulo Alcantara 				   char *tree, bool islink,
35538c8a9a5SSteve French 				   struct dfs_cache_tgt_list *tl)
35638c8a9a5SSteve French {
357*2102ed90SPaulo Alcantara 	const struct smb_version_operations *ops = tcon->ses->server->ops;
35838c8a9a5SSteve French 	struct TCP_Server_Info *server = tcon->ses->server;
35938c8a9a5SSteve French 	struct dfs_cache_tgt_iterator *tit;
360*2102ed90SPaulo Alcantara 	char *share = NULL, *prefix = NULL;
36138c8a9a5SSteve French 	bool target_match;
362*2102ed90SPaulo Alcantara 	int rc = -ENOENT;
36338c8a9a5SSteve French 
36438c8a9a5SSteve French 	/* Try to tree connect to all dfs targets */
365*2102ed90SPaulo Alcantara 	for (tit = dfs_cache_get_tgt_iterator(tl);
366*2102ed90SPaulo Alcantara 	     tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
36738c8a9a5SSteve French 		kfree(share);
36838c8a9a5SSteve French 		kfree(prefix);
36938c8a9a5SSteve French 		share = prefix = NULL;
37038c8a9a5SSteve French 
37138c8a9a5SSteve French 		/* Check if share matches with tcp ses */
37238c8a9a5SSteve French 		rc = dfs_cache_get_tgt_share(server->leaf_fullpath + 1, tit, &share, &prefix);
37338c8a9a5SSteve French 		if (rc) {
37438c8a9a5SSteve French 			cifs_dbg(VFS, "%s: failed to parse target share: %d\n", __func__, rc);
37538c8a9a5SSteve French 			break;
37638c8a9a5SSteve French 		}
37738c8a9a5SSteve French 
37838c8a9a5SSteve French 		rc = target_share_matches_server(server, share, &target_match);
37938c8a9a5SSteve French 		if (rc)
38038c8a9a5SSteve French 			break;
38138c8a9a5SSteve French 		if (!target_match) {
38238c8a9a5SSteve French 			rc = -EHOSTUNREACH;
38338c8a9a5SSteve French 			continue;
38438c8a9a5SSteve French 		}
38538c8a9a5SSteve French 
38638c8a9a5SSteve French 		dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, tit);
38738c8a9a5SSteve French 		scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
388*2102ed90SPaulo Alcantara 		rc = ops->tree_connect(xid, tcon->ses, tree,
389*2102ed90SPaulo Alcantara 				       tcon, tcon->ses->local_nls);
390*2102ed90SPaulo Alcantara 		if (islink && !rc && cifs_sb)
39138c8a9a5SSteve French 			rc = cifs_update_super_prepath(cifs_sb, prefix);
39238c8a9a5SSteve French 		break;
39338c8a9a5SSteve French 	}
39438c8a9a5SSteve French 
39538c8a9a5SSteve French 	kfree(share);
39638c8a9a5SSteve French 	kfree(prefix);
39738c8a9a5SSteve French 	dfs_cache_free_tgts(tl);
39838c8a9a5SSteve French 	return rc;
39938c8a9a5SSteve French }
40038c8a9a5SSteve French 
cifs_tree_connect(const unsigned int xid,struct cifs_tcon * tcon,const struct nls_table * nlsc)40138c8a9a5SSteve French int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
40238c8a9a5SSteve French {
40338c8a9a5SSteve French 	int rc;
40438c8a9a5SSteve French 	struct TCP_Server_Info *server = tcon->ses->server;
40538c8a9a5SSteve French 	const struct smb_version_operations *ops = server->ops;
4063fea12f3SPaulo Alcantara 	DFS_CACHE_TGT_LIST(tl);
4073ae872deSPaulo Alcantara 	struct cifs_sb_info *cifs_sb = NULL;
4083ae872deSPaulo Alcantara 	struct super_block *sb = NULL;
40938c8a9a5SSteve French 	struct dfs_info3_param ref = {0};
4103ae872deSPaulo Alcantara 	char *tree;
41138c8a9a5SSteve French 
41238c8a9a5SSteve French 	/* only send once per connect */
41338c8a9a5SSteve French 	spin_lock(&tcon->tc_lock);
414cd743cfeSShyam Prasad N 
415cd743cfeSShyam Prasad N 	/* if tcon is marked for needing reconnect, update state */
416cd743cfeSShyam Prasad N 	if (tcon->need_reconnect)
417cd743cfeSShyam Prasad N 		tcon->status = TID_NEED_TCON;
418cd743cfeSShyam Prasad N 
41991f4480cSShyam Prasad N 	if (tcon->status == TID_GOOD) {
42091f4480cSShyam Prasad N 		spin_unlock(&tcon->tc_lock);
42191f4480cSShyam Prasad N 		return 0;
42291f4480cSShyam Prasad N 	}
42391f4480cSShyam Prasad N 
42438c8a9a5SSteve French 	if (tcon->status != TID_NEW &&
42538c8a9a5SSteve French 	    tcon->status != TID_NEED_TCON) {
42638c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
42738c8a9a5SSteve French 		return -EHOSTDOWN;
42838c8a9a5SSteve French 	}
42938c8a9a5SSteve French 
43038c8a9a5SSteve French 	tcon->status = TID_IN_TCON;
43138c8a9a5SSteve French 	spin_unlock(&tcon->tc_lock);
43238c8a9a5SSteve French 
43338c8a9a5SSteve French 	tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
43438c8a9a5SSteve French 	if (!tree) {
43538c8a9a5SSteve French 		rc = -ENOMEM;
43638c8a9a5SSteve French 		goto out;
43738c8a9a5SSteve French 	}
43838c8a9a5SSteve French 
43938c8a9a5SSteve French 	if (tcon->ipc) {
44038c8a9a5SSteve French 		cifs_server_lock(server);
44138c8a9a5SSteve French 		scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
44238c8a9a5SSteve French 		cifs_server_unlock(server);
44338c8a9a5SSteve French 		rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
44438c8a9a5SSteve French 		goto out;
44538c8a9a5SSteve French 	}
44638c8a9a5SSteve French 
4473ae872deSPaulo Alcantara 	sb = cifs_get_dfs_tcon_super(tcon);
4483ae872deSPaulo Alcantara 	if (!IS_ERR(sb))
44938c8a9a5SSteve French 		cifs_sb = CIFS_SB(sb);
45038c8a9a5SSteve French 
451*2102ed90SPaulo Alcantara 	/* Tree connect to last share in @tcon->tree_name if no DFS referral */
452*2102ed90SPaulo Alcantara 	if (!server->leaf_fullpath ||
45338c8a9a5SSteve French 	    dfs_cache_noreq_find(server->leaf_fullpath + 1, &ref, &tl)) {
454*2102ed90SPaulo Alcantara 		rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name,
455*2102ed90SPaulo Alcantara 				       tcon, tcon->ses->local_nls);
45638c8a9a5SSteve French 		goto out;
45738c8a9a5SSteve French 	}
45838c8a9a5SSteve French 
45938c8a9a5SSteve French 	rc = tree_connect_dfs_target(xid, tcon, cifs_sb, tree, ref.server_type == DFS_TYPE_LINK,
46038c8a9a5SSteve French 				     &tl);
46138c8a9a5SSteve French 	free_dfs_info_param(&ref);
46238c8a9a5SSteve French 
46338c8a9a5SSteve French out:
46438c8a9a5SSteve French 	kfree(tree);
46538c8a9a5SSteve French 	cifs_put_tcp_super(sb);
46638c8a9a5SSteve French 
46738c8a9a5SSteve French 	if (rc) {
46838c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
46938c8a9a5SSteve French 		if (tcon->status == TID_IN_TCON)
47038c8a9a5SSteve French 			tcon->status = TID_NEED_TCON;
47138c8a9a5SSteve French 		spin_unlock(&tcon->tc_lock);
47238c8a9a5SSteve French 	} else {
47338c8a9a5SSteve French 		spin_lock(&tcon->tc_lock);
47438c8a9a5SSteve French 		if (tcon->status == TID_IN_TCON)
47538c8a9a5SSteve French 			tcon->status = TID_GOOD;
47638c8a9a5SSteve French 		tcon->need_reconnect = false;
477cd743cfeSShyam Prasad N 		spin_unlock(&tcon->tc_lock);
47838c8a9a5SSteve French 	}
47938c8a9a5SSteve French 
48038c8a9a5SSteve French 	return rc;
48138c8a9a5SSteve French }
482