xref: /openbmc/linux/fs/smb/client/dfs.c (revision 3e8bd1ba)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2022 Paulo Alcantara <palcantara@suse.de>
4  */
5 
6 #include "cifsproto.h"
7 #include "cifs_debug.h"
8 #include "dns_resolve.h"
9 #include "fs_context.h"
10 #include "dfs.h"
11 
12 /**
13  * dfs_parse_target_referral - set fs context for dfs target referral
14  *
15  * @full_path: full path in UNC format.
16  * @ref: dfs referral pointer.
17  * @ctx: smb3 fs context pointer.
18  *
19  * Return zero if dfs referral was parsed correctly, otherwise non-zero.
20  */
21 int dfs_parse_target_referral(const char *full_path, const struct dfs_info3_param *ref,
22 			      struct smb3_fs_context *ctx)
23 {
24 	int rc;
25 	const char *prepath = NULL;
26 	char *path;
27 
28 	if (!full_path || !*full_path || !ref || !ctx)
29 		return -EINVAL;
30 
31 	if (WARN_ON_ONCE(!ref->node_name || ref->path_consumed < 0))
32 		return -EINVAL;
33 
34 	if (strlen(full_path) - ref->path_consumed) {
35 		prepath = full_path + ref->path_consumed;
36 		/* skip initial delimiter */
37 		if (*prepath == '/' || *prepath == '\\')
38 			prepath++;
39 	}
40 
41 	path = cifs_build_devname(ref->node_name, prepath);
42 	if (IS_ERR(path))
43 		return PTR_ERR(path);
44 
45 	rc = smb3_parse_devname(path, ctx);
46 	if (rc)
47 		goto out;
48 
49 	rc = dns_resolve_server_name_to_ip(path, (struct sockaddr *)&ctx->dstaddr, NULL);
50 
51 out:
52 	kfree(path);
53 	return rc;
54 }
55 
56 static int get_session(struct cifs_mount_ctx *mnt_ctx, const char *full_path)
57 {
58 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
59 	int rc;
60 
61 	ctx->leaf_fullpath = (char *)full_path;
62 	rc = cifs_mount_get_session(mnt_ctx);
63 	ctx->leaf_fullpath = NULL;
64 
65 	return rc;
66 }
67 
68 /*
69  * Track individual DFS referral servers used by new DFS mount.
70  *
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().
73  */
74 static int add_root_smb_session(struct cifs_mount_ctx *mnt_ctx)
75 {
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;
79 
80 	if (ses) {
81 		root_ses = kmalloc(sizeof(*root_ses), GFP_KERNEL);
82 		if (!root_ses)
83 			return -ENOMEM;
84 
85 		INIT_LIST_HEAD(&root_ses->list);
86 
87 		spin_lock(&cifs_tcp_ses_lock);
88 		cifs_smb_ses_inc_refcount(ses);
89 		spin_unlock(&cifs_tcp_ses_lock);
90 		root_ses->ses = ses;
91 		list_add_tail(&root_ses->list, &mnt_ctx->dfs_ses_list);
92 	}
93 	/* Select new DFS referral server so that new referrals go through it */
94 	ctx->dfs_root_ses = ses;
95 	return 0;
96 }
97 
98 static inline int parse_dfs_target(struct smb3_fs_context *ctx,
99 				   struct dfs_ref_walk *rw,
100 				   struct dfs_info3_param *tgt)
101 {
102 	int rc;
103 	const char *fpath = ref_walk_fpath(rw) + 1;
104 
105 	rc = ref_walk_get_tgt(rw, tgt);
106 	if (!rc)
107 		rc = dfs_parse_target_referral(fpath, tgt, ctx);
108 	return rc;
109 }
110 
111 static int set_ref_paths(struct cifs_mount_ctx *mnt_ctx,
112 			 struct dfs_info3_param *tgt,
113 			 struct dfs_ref_walk *rw)
114 {
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;
118 	int rc;
119 
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);
123 
124 	if (!tgt || (tgt->server_type == DFS_TYPE_LINK &&
125 		     DFS_INTERLINK(tgt->flags)))
126 		ref_path = dfs_get_path(cifs_sb, ctx->UNC);
127 	else
128 		ref_path = dfs_get_path(cifs_sb, full_path);
129 	if (IS_ERR(ref_path)) {
130 		rc = PTR_ERR(ref_path);
131 		kfree(full_path);
132 		return rc;
133 	}
134 	ref_walk_path(rw) = ref_path;
135 	ref_walk_fpath(rw) = full_path;
136 	return 0;
137 }
138 
139 static int __dfs_referral_walk(struct cifs_mount_ctx *mnt_ctx,
140 			       struct dfs_ref_walk *rw)
141 {
142 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
143 	struct dfs_info3_param tgt = {};
144 	bool is_refsrv;
145 	int rc = -ENOENT;
146 
147 again:
148 	do {
149 		if (ref_walk_empty(rw)) {
150 			rc = dfs_get_referral(mnt_ctx, ref_walk_path(rw) + 1,
151 					      NULL, ref_walk_tl(rw));
152 			if (rc) {
153 				rc = cifs_mount_get_tcon(mnt_ctx);
154 				if (!rc)
155 					rc = cifs_is_path_remote(mnt_ctx);
156 				continue;
157 			}
158 			if (!ref_walk_num_tgts(rw)) {
159 				rc = -ENOENT;
160 				continue;
161 			}
162 		}
163 
164 		while (ref_walk_next_tgt(rw)) {
165 			rc = parse_dfs_target(ctx, rw, &tgt);
166 			if (rc)
167 				continue;
168 
169 			cifs_mount_put_conns(mnt_ctx);
170 			rc = get_session(mnt_ctx, ref_walk_path(rw));
171 			if (rc)
172 				continue;
173 
174 			is_refsrv = tgt.server_type == DFS_TYPE_ROOT ||
175 				DFS_INTERLINK(tgt.flags);
176 			ref_walk_set_tgt_hint(rw);
177 
178 			if (tgt.flags & DFSREF_STORAGE_SERVER) {
179 				rc = cifs_mount_get_tcon(mnt_ctx);
180 				if (!rc)
181 					rc = cifs_is_path_remote(mnt_ctx);
182 				if (!rc)
183 					break;
184 				if (rc != -EREMOTE)
185 					continue;
186 			}
187 
188 			if (is_refsrv) {
189 				rc = add_root_smb_session(mnt_ctx);
190 				if (rc)
191 					goto out;
192 			}
193 
194 			rc = ref_walk_advance(rw);
195 			if (!rc) {
196 				rc = set_ref_paths(mnt_ctx, &tgt, rw);
197 				if (!rc) {
198 					rc = -EREMOTE;
199 					goto again;
200 				}
201 			}
202 			if (rc != -ELOOP)
203 				goto out;
204 		}
205 	} while (rc && ref_walk_descend(rw));
206 
207 out:
208 	free_dfs_info_param(&tgt);
209 	return rc;
210 }
211 
212 static int dfs_referral_walk(struct cifs_mount_ctx *mnt_ctx)
213 {
214 	struct dfs_ref_walk *rw;
215 	int rc;
216 
217 	rw = ref_walk_alloc();
218 	if (IS_ERR(rw))
219 		return PTR_ERR(rw);
220 
221 	ref_walk_init(rw);
222 	rc = set_ref_paths(mnt_ctx, NULL, rw);
223 	if (!rc)
224 		rc = __dfs_referral_walk(mnt_ctx, rw);
225 	ref_walk_free(rw);
226 	return rc;
227 }
228 
229 static int __dfs_mount_share(struct cifs_mount_ctx *mnt_ctx)
230 {
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;
235 	int rc;
236 
237 	origin_fullpath = dfs_get_path(cifs_sb, ctx->source);
238 	if (IS_ERR(origin_fullpath))
239 		return PTR_ERR(origin_fullpath);
240 
241 	rc = dfs_referral_walk(mnt_ctx);
242 	if (rc)
243 		goto out;
244 
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;
250 	}
251 	spin_unlock(&tcon->tc_lock);
252 
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);
257 	} else {
258 		dfs_put_root_smb_sessions(&mnt_ctx->dfs_ses_list);
259 	}
260 
261 out:
262 	kfree(origin_fullpath);
263 	return rc;
264 }
265 
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)
268 {
269 	struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
270 	int rc;
271 
272 	rc = dns_resolve_server_name_to_ip(ctx->source, addr, NULL);
273 	if (!rc)
274 		cifs_set_port(addr, ctx->port);
275 	return rc;
276 }
277 
278 int dfs_mount_share(struct cifs_mount_ctx *mnt_ctx, bool *isdfs)
279 {
280 	struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
281 	bool nodfs = ctx->nodfs;
282 	int rc;
283 
284 	rc = update_fs_context_dstaddr(ctx);
285 	if (rc)
286 		return rc;
287 
288 	*isdfs = false;
289 	rc = get_session(mnt_ctx, NULL);
290 	if (rc)
291 		return rc;
292 
293 	ctx->dfs_root_ses = mnt_ctx->ses;
294 	/*
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.
297 	 *
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.
300 	 */
301 	if (!nodfs) {
302 		rc = dfs_get_referral(mnt_ctx, ctx->UNC + 1, NULL, NULL);
303 		if (rc) {
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__);
307 			nodfs = true;
308 		}
309 	}
310 	if (nodfs) {
311 		rc = cifs_mount_get_tcon(mnt_ctx);
312 		if (!rc)
313 			rc = cifs_is_path_remote(mnt_ctx);
314 		return rc;
315 	}
316 
317 	*isdfs = true;
318 	add_root_smb_session(mnt_ctx);
319 	return __dfs_mount_share(mnt_ctx);
320 }
321 
322 /* Update dfs referral path of superblock */
323 static int update_server_fullpath(struct TCP_Server_Info *server, struct cifs_sb_info *cifs_sb,
324 				  const char *target)
325 {
326 	int rc = 0;
327 	size_t len = strlen(target);
328 	char *refpath, *npath;
329 
330 	if (unlikely(len < 2 || *target != '\\'))
331 		return -EINVAL;
332 
333 	if (target[1] == '\\') {
334 		len += 1;
335 		refpath = kmalloc(len, GFP_KERNEL);
336 		if (!refpath)
337 			return -ENOMEM;
338 
339 		scnprintf(refpath, len, "%s", target);
340 	} else {
341 		len += sizeof("\\");
342 		refpath = kmalloc(len, GFP_KERNEL);
343 		if (!refpath)
344 			return -ENOMEM;
345 
346 		scnprintf(refpath, len, "\\%s", target);
347 	}
348 
349 	npath = dfs_cache_canonical_path(refpath, cifs_sb->local_nls, cifs_remap(cifs_sb));
350 	kfree(refpath);
351 
352 	if (IS_ERR(npath)) {
353 		rc = PTR_ERR(npath);
354 	} else {
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);
361 	}
362 	return rc;
363 }
364 
365 static int target_share_matches_server(struct TCP_Server_Info *server, char *share,
366 				       bool *target_match)
367 {
368 	int rc = 0;
369 	const char *dfs_host;
370 	size_t dfs_host_len;
371 
372 	*target_match = true;
373 	extract_unc_hostname(share, &dfs_host, &dfs_host_len);
374 
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);
382 		if (rc)
383 			cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
384 	}
385 	cifs_server_unlock(server);
386 	return rc;
387 }
388 
389 static void __tree_connect_ipc(const unsigned int xid, char *tree,
390 			       struct cifs_sb_info *cifs_sb,
391 			       struct cifs_ses *ses)
392 {
393 	struct TCP_Server_Info *server = ses->server;
394 	struct cifs_tcon *tcon = ses->tcon_ipc;
395 	int rc;
396 
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",
404 				__func__);
405 		return;
406 	}
407 	spin_unlock(&ses->chan_lock);
408 	spin_unlock(&ses->ses_lock);
409 
410 	cifs_server_lock(server);
411 	scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
412 	cifs_server_unlock(server);
413 
414 	rc = server->ops->tree_connect(xid, ses, tree, tcon,
415 				       cifs_sb->local_nls);
416 	cifs_server_dbg(FYI, "%s: tree_reconnect %s: %d\n", __func__, tree, rc);
417 	spin_lock(&tcon->tc_lock);
418 	if (rc) {
419 		tcon->status = TID_NEED_TCON;
420 	} else {
421 		tcon->status = TID_GOOD;
422 		tcon->need_reconnect = false;
423 	}
424 	spin_unlock(&tcon->tc_lock);
425 }
426 
427 static void tree_connect_ipc(const unsigned int xid, char *tree,
428 			     struct cifs_sb_info *cifs_sb,
429 			     struct cifs_tcon *tcon)
430 {
431 	struct cifs_ses *ses = tcon->ses;
432 
433 	__tree_connect_ipc(xid, tree, cifs_sb, ses);
434 	__tree_connect_ipc(xid, tree, cifs_sb, CIFS_DFS_ROOT_SES(ses));
435 }
436 
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)
440 {
441 	int rc;
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;
447 	bool target_match;
448 
449 	tit = dfs_cache_get_tgt_iterator(tl);
450 	if (!tit) {
451 		rc = -ENOENT;
452 		goto out;
453 	}
454 
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);
459 
460 		kfree(share);
461 		kfree(prefix);
462 		share = prefix = NULL;
463 
464 		/* Check if share matches with tcp ses */
465 		rc = dfs_cache_get_tgt_share(server->leaf_fullpath + 1, tit, &share, &prefix);
466 		if (rc) {
467 			cifs_dbg(VFS, "%s: failed to parse target share: %d\n", __func__, rc);
468 			break;
469 		}
470 
471 		rc = target_share_matches_server(server, share, &target_match);
472 		if (rc)
473 			break;
474 		if (!target_match) {
475 			rc = -EHOSTUNREACH;
476 			continue;
477 		}
478 
479 		dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, tit);
480 		tree_connect_ipc(xid, tree, cifs_sb, tcon);
481 
482 		scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
483 		if (!islink) {
484 			rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
485 			break;
486 		}
487 
488 		/*
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.
493 		 */
494 		if (dfs_cache_find(xid, root_ses, cifs_sb->local_nls, cifs_remap(cifs_sb), target,
495 				   NULL, &ntl)) {
496 			rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
497 			if (rc)
498 				continue;
499 
500 			rc = cifs_update_super_prepath(cifs_sb, prefix);
501 		} else {
502 			/* Target is another dfs share */
503 			rc = update_server_fullpath(server, cifs_sb, target);
504 			dfs_cache_free_tgts(tl);
505 
506 			if (!rc) {
507 				rc = -EREMOTE;
508 				list_replace_init(&ntl.tl_list, &tl->tl_list);
509 			} else
510 				dfs_cache_free_tgts(&ntl);
511 		}
512 		break;
513 	}
514 
515 out:
516 	kfree(share);
517 	kfree(prefix);
518 
519 	return rc;
520 }
521 
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)
525 {
526 	int rc;
527 	int num_links = 0;
528 	struct TCP_Server_Info *server = tcon->ses->server;
529 	char *old_fullpath = server->leaf_fullpath;
530 
531 	do {
532 		rc = __tree_connect_dfs_target(xid, tcon, cifs_sb, tree, islink, tl);
533 		if (!rc || rc != -EREMOTE)
534 			break;
535 	} while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
536 	/*
537 	 * If we couldn't tree connect to any targets from last referral path, then
538 	 * retry it from newly resolved dfs referral.
539 	 */
540 	if (rc && server->leaf_fullpath != old_fullpath)
541 		cifs_signal_cifsd_for_reconnect(server, true);
542 
543 	dfs_cache_free_tgts(tl);
544 	return rc;
545 }
546 
547 int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
548 {
549 	int rc;
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};
556 	char *tree;
557 
558 	/* only send once per connect */
559 	spin_lock(&tcon->tc_lock);
560 	if (tcon->status == TID_GOOD) {
561 		spin_unlock(&tcon->tc_lock);
562 		return 0;
563 	}
564 
565 	if (tcon->status != TID_NEW &&
566 	    tcon->status != TID_NEED_TCON) {
567 		spin_unlock(&tcon->tc_lock);
568 		return -EHOSTDOWN;
569 	}
570 
571 	tcon->status = TID_IN_TCON;
572 	spin_unlock(&tcon->tc_lock);
573 
574 	tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
575 	if (!tree) {
576 		rc = -ENOMEM;
577 		goto out;
578 	}
579 
580 	if (tcon->ipc) {
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);
585 		goto out;
586 	}
587 
588 	sb = cifs_get_dfs_tcon_super(tcon);
589 	if (!IS_ERR(sb))
590 		cifs_sb = CIFS_SB(sb);
591 
592 	/*
593 	 * Tree connect to last share in @tcon->tree_name whether dfs super or
594 	 * cached dfs referral was not found.
595 	 */
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);
600 		goto out;
601 	}
602 
603 	rc = tree_connect_dfs_target(xid, tcon, cifs_sb, tree, ref.server_type == DFS_TYPE_LINK,
604 				     &tl);
605 	free_dfs_info_param(&ref);
606 
607 out:
608 	kfree(tree);
609 	cifs_put_tcp_super(sb);
610 
611 	if (rc) {
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);
616 	} else {
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;
622 	}
623 
624 	return rc;
625 }
626