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);
267 * If @ctx->dfs_automount, then update @ctx->dstaddr earlier with the DFS root
268 * server from where we'll start following any referrals. Otherwise rely on the
269 * value provided by mount(2) as the user might not have dns_resolver key set up
270 * and therefore failing to upcall to resolve UNC hostname under @ctx->source.
272 static int update_fs_context_dstaddr(struct smb3_fs_context *ctx)
274 struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr;
277 if (!ctx->nodfs && ctx->dfs_automount) {
278 rc = dns_resolve_server_name_to_ip(ctx->source, addr, NULL);
280 cifs_set_port(addr, ctx->port);
281 ctx->dfs_automount = false;
286 int dfs_mount_share(struct cifs_mount_ctx *mnt_ctx, bool *isdfs)
288 struct smb3_fs_context *ctx = mnt_ctx->fs_ctx;
289 bool nodfs = ctx->nodfs;
292 rc = update_fs_context_dstaddr(ctx);
297 rc = get_session(mnt_ctx, NULL);
301 ctx->dfs_root_ses = mnt_ctx->ses;
303 * If called with 'nodfs' mount option, then skip DFS resolving. Otherwise unconditionally
304 * try to get an DFS referral (even cached) to determine whether it is an DFS mount.
306 * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem
307 * to respond with PATH_NOT_COVERED to requests that include the prefix.
310 rc = dfs_get_referral(mnt_ctx, ctx->UNC + 1, NULL, NULL);
312 cifs_dbg(FYI, "%s: no dfs referral for %s: %d\n",
313 __func__, ctx->UNC + 1, rc);
314 cifs_dbg(FYI, "%s: assuming non-dfs mount...\n", __func__);
319 rc = cifs_mount_get_tcon(mnt_ctx);
321 rc = cifs_is_path_remote(mnt_ctx);
326 add_root_smb_session(mnt_ctx);
327 return __dfs_mount_share(mnt_ctx);
330 /* Update dfs referral path of superblock */
331 static int update_server_fullpath(struct TCP_Server_Info *server, struct cifs_sb_info *cifs_sb,
335 size_t len = strlen(target);
336 char *refpath, *npath;
338 if (unlikely(len < 2 || *target != '\\'))
341 if (target[1] == '\\') {
343 refpath = kmalloc(len, GFP_KERNEL);
347 scnprintf(refpath, len, "%s", target);
350 refpath = kmalloc(len, GFP_KERNEL);
354 scnprintf(refpath, len, "\\%s", target);
357 npath = dfs_cache_canonical_path(refpath, cifs_sb->local_nls, cifs_remap(cifs_sb));
363 mutex_lock(&server->refpath_lock);
364 spin_lock(&server->srv_lock);
365 kfree(server->leaf_fullpath);
366 server->leaf_fullpath = npath;
367 spin_unlock(&server->srv_lock);
368 mutex_unlock(&server->refpath_lock);
373 static int target_share_matches_server(struct TCP_Server_Info *server, char *share,
377 const char *dfs_host;
380 *target_match = true;
381 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
383 /* Check if hostnames or addresses match */
384 cifs_server_lock(server);
385 if (dfs_host_len != strlen(server->hostname) ||
386 strncasecmp(dfs_host, server->hostname, dfs_host_len)) {
387 cifs_dbg(FYI, "%s: %.*s doesn't match %s\n", __func__,
388 (int)dfs_host_len, dfs_host, server->hostname);
389 rc = match_target_ip(server, dfs_host, dfs_host_len, target_match);
391 cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
393 cifs_server_unlock(server);
397 static void __tree_connect_ipc(const unsigned int xid, char *tree,
398 struct cifs_sb_info *cifs_sb,
399 struct cifs_ses *ses)
401 struct TCP_Server_Info *server = ses->server;
402 struct cifs_tcon *tcon = ses->tcon_ipc;
405 spin_lock(&ses->ses_lock);
406 spin_lock(&ses->chan_lock);
407 if (cifs_chan_needs_reconnect(ses, server) ||
408 ses->ses_status != SES_GOOD) {
409 spin_unlock(&ses->chan_lock);
410 spin_unlock(&ses->ses_lock);
411 cifs_server_dbg(FYI, "%s: skipping ipc reconnect due to disconnected ses\n",
415 spin_unlock(&ses->chan_lock);
416 spin_unlock(&ses->ses_lock);
418 cifs_server_lock(server);
419 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
420 cifs_server_unlock(server);
422 rc = server->ops->tree_connect(xid, ses, tree, tcon,
424 cifs_server_dbg(FYI, "%s: tree_reconnect %s: %d\n", __func__, tree, rc);
425 spin_lock(&tcon->tc_lock);
427 tcon->status = TID_NEED_TCON;
429 tcon->status = TID_GOOD;
430 tcon->need_reconnect = false;
432 spin_unlock(&tcon->tc_lock);
435 static void tree_connect_ipc(const unsigned int xid, char *tree,
436 struct cifs_sb_info *cifs_sb,
437 struct cifs_tcon *tcon)
439 struct cifs_ses *ses = tcon->ses;
441 __tree_connect_ipc(xid, tree, cifs_sb, ses);
442 __tree_connect_ipc(xid, tree, cifs_sb, CIFS_DFS_ROOT_SES(ses));
445 static int __tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
446 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
447 struct dfs_cache_tgt_list *tl)
450 struct TCP_Server_Info *server = tcon->ses->server;
451 const struct smb_version_operations *ops = server->ops;
452 struct cifs_ses *root_ses = CIFS_DFS_ROOT_SES(tcon->ses);
453 char *share = NULL, *prefix = NULL;
454 struct dfs_cache_tgt_iterator *tit;
457 tit = dfs_cache_get_tgt_iterator(tl);
463 /* Try to tree connect to all dfs targets */
464 for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) {
465 const char *target = dfs_cache_get_tgt_name(tit);
466 DFS_CACHE_TGT_LIST(ntl);
470 share = prefix = NULL;
472 /* Check if share matches with tcp ses */
473 rc = dfs_cache_get_tgt_share(server->leaf_fullpath + 1, tit, &share, &prefix);
475 cifs_dbg(VFS, "%s: failed to parse target share: %d\n", __func__, rc);
479 rc = target_share_matches_server(server, share, &target_match);
487 dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, tit);
488 tree_connect_ipc(xid, tree, cifs_sb, tcon);
490 scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
492 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
497 * If no dfs referrals were returned from link target, then just do a TREE_CONNECT
498 * to it. Otherwise, cache the dfs referral and then mark current tcp ses for
499 * reconnect so either the demultiplex thread or the echo worker will reconnect to
500 * newly resolved target.
502 if (dfs_cache_find(xid, root_ses, cifs_sb->local_nls, cifs_remap(cifs_sb), target,
504 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls);
508 rc = cifs_update_super_prepath(cifs_sb, prefix);
510 /* Target is another dfs share */
511 rc = update_server_fullpath(server, cifs_sb, target);
512 dfs_cache_free_tgts(tl);
516 list_replace_init(&ntl.tl_list, &tl->tl_list);
518 dfs_cache_free_tgts(&ntl);
530 static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon,
531 struct cifs_sb_info *cifs_sb, char *tree, bool islink,
532 struct dfs_cache_tgt_list *tl)
536 struct TCP_Server_Info *server = tcon->ses->server;
537 char *old_fullpath = server->leaf_fullpath;
540 rc = __tree_connect_dfs_target(xid, tcon, cifs_sb, tree, islink, tl);
541 if (!rc || rc != -EREMOTE)
543 } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS);
545 * If we couldn't tree connect to any targets from last referral path, then
546 * retry it from newly resolved dfs referral.
548 if (rc && server->leaf_fullpath != old_fullpath)
549 cifs_signal_cifsd_for_reconnect(server, true);
551 dfs_cache_free_tgts(tl);
555 int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
558 struct TCP_Server_Info *server = tcon->ses->server;
559 const struct smb_version_operations *ops = server->ops;
560 DFS_CACHE_TGT_LIST(tl);
561 struct cifs_sb_info *cifs_sb = NULL;
562 struct super_block *sb = NULL;
563 struct dfs_info3_param ref = {0};
566 /* only send once per connect */
567 spin_lock(&tcon->tc_lock);
568 if (tcon->status == TID_GOOD) {
569 spin_unlock(&tcon->tc_lock);
573 if (tcon->status != TID_NEW &&
574 tcon->status != TID_NEED_TCON) {
575 spin_unlock(&tcon->tc_lock);
579 tcon->status = TID_IN_TCON;
580 spin_unlock(&tcon->tc_lock);
582 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
589 cifs_server_lock(server);
590 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
591 cifs_server_unlock(server);
592 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
596 sb = cifs_get_dfs_tcon_super(tcon);
598 cifs_sb = CIFS_SB(sb);
601 * Tree connect to last share in @tcon->tree_name whether dfs super or
602 * cached dfs referral was not found.
604 if (!cifs_sb || !server->leaf_fullpath ||
605 dfs_cache_noreq_find(server->leaf_fullpath + 1, &ref, &tl)) {
606 rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon,
607 cifs_sb ? cifs_sb->local_nls : nlsc);
611 rc = tree_connect_dfs_target(xid, tcon, cifs_sb, tree, ref.server_type == DFS_TYPE_LINK,
613 free_dfs_info_param(&ref);
617 cifs_put_tcp_super(sb);
620 spin_lock(&tcon->tc_lock);
621 if (tcon->status == TID_IN_TCON)
622 tcon->status = TID_NEED_TCON;
623 spin_unlock(&tcon->tc_lock);
625 spin_lock(&tcon->tc_lock);
626 if (tcon->status == TID_IN_TCON)
627 tcon->status = TID_GOOD;
628 spin_unlock(&tcon->tc_lock);
629 tcon->need_reconnect = false;