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 */ 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 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 /* 69bf99f6beSPaulo Alcantara * Track individual DFS referral servers used by new DFS mount. 70bf99f6beSPaulo Alcantara * 71bf99f6beSPaulo Alcantara * On success, their lifetime will be shared by final tcon (dfs_ses_list). 72bf99f6beSPaulo Alcantara * Otherwise, they will be put by dfs_put_root_smb_sessions() in cifs_mount(). 73bf99f6beSPaulo Alcantara */ 7438c8a9a5SSteve French static int add_root_smb_session(struct cifs_mount_ctx *mnt_ctx) 7538c8a9a5SSteve French { 7638c8a9a5SSteve French struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; 7738c8a9a5SSteve French struct dfs_root_ses *root_ses; 7838c8a9a5SSteve French struct cifs_ses *ses = mnt_ctx->ses; 7938c8a9a5SSteve French 8038c8a9a5SSteve French if (ses) { 8138c8a9a5SSteve French root_ses = kmalloc(sizeof(*root_ses), GFP_KERNEL); 8238c8a9a5SSteve French if (!root_ses) 8338c8a9a5SSteve French return -ENOMEM; 8438c8a9a5SSteve French 8538c8a9a5SSteve French INIT_LIST_HEAD(&root_ses->list); 8638c8a9a5SSteve French 8738c8a9a5SSteve French spin_lock(&cifs_tcp_ses_lock); 88bf99f6beSPaulo Alcantara cifs_smb_ses_inc_refcount(ses); 8938c8a9a5SSteve French spin_unlock(&cifs_tcp_ses_lock); 9038c8a9a5SSteve French root_ses->ses = ses; 9138c8a9a5SSteve French list_add_tail(&root_ses->list, &mnt_ctx->dfs_ses_list); 9238c8a9a5SSteve French } 93bf99f6beSPaulo Alcantara /* Select new DFS referral server so that new referrals go through it */ 9438c8a9a5SSteve French ctx->dfs_root_ses = ses; 9538c8a9a5SSteve French return 0; 9638c8a9a5SSteve French } 9738c8a9a5SSteve French 98ce04127cSPaulo Alcantara static inline int parse_dfs_target(struct smb3_fs_context *ctx, 99ce04127cSPaulo Alcantara struct dfs_ref_walk *rw, 100ce04127cSPaulo Alcantara struct dfs_info3_param *tgt) 101ce04127cSPaulo Alcantara { 102ce04127cSPaulo Alcantara int rc; 103ce04127cSPaulo Alcantara const char *fpath = ref_walk_fpath(rw) + 1; 104ce04127cSPaulo Alcantara 105ce04127cSPaulo Alcantara rc = ref_walk_get_tgt(rw, tgt); 106ce04127cSPaulo Alcantara if (!rc) 107ce04127cSPaulo Alcantara rc = dfs_parse_target_referral(fpath, tgt, ctx); 108ce04127cSPaulo Alcantara return rc; 109ce04127cSPaulo Alcantara } 110ce04127cSPaulo Alcantara 111ce04127cSPaulo Alcantara static int set_ref_paths(struct cifs_mount_ctx *mnt_ctx, 112ce04127cSPaulo Alcantara struct dfs_info3_param *tgt, 113ce04127cSPaulo Alcantara struct dfs_ref_walk *rw) 11438c8a9a5SSteve French { 11538c8a9a5SSteve French struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; 116ce04127cSPaulo Alcantara struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb; 117ce04127cSPaulo Alcantara char *ref_path, *full_path; 118ce04127cSPaulo Alcantara int rc; 11938c8a9a5SSteve French 120ce04127cSPaulo Alcantara full_path = smb3_fs_context_fullpath(ctx, CIFS_DIR_SEP(cifs_sb)); 121ce04127cSPaulo Alcantara if (IS_ERR(full_path)) 122ce04127cSPaulo Alcantara return PTR_ERR(full_path); 123ce04127cSPaulo Alcantara 124ce04127cSPaulo Alcantara if (!tgt || (tgt->server_type == DFS_TYPE_LINK && 125ce04127cSPaulo Alcantara DFS_INTERLINK(tgt->flags))) 126ce04127cSPaulo Alcantara ref_path = dfs_get_path(cifs_sb, ctx->UNC); 127ce04127cSPaulo Alcantara else 128ce04127cSPaulo Alcantara ref_path = dfs_get_path(cifs_sb, full_path); 129ce04127cSPaulo Alcantara if (IS_ERR(ref_path)) { 130ce04127cSPaulo Alcantara rc = PTR_ERR(ref_path); 131ce04127cSPaulo Alcantara kfree(full_path); 13238c8a9a5SSteve French return rc; 133ce04127cSPaulo Alcantara } 134ce04127cSPaulo Alcantara ref_walk_path(rw) = ref_path; 135ce04127cSPaulo Alcantara ref_walk_fpath(rw) = full_path; 136ce04127cSPaulo Alcantara return 0; 137ce04127cSPaulo Alcantara } 13838c8a9a5SSteve French 139ce04127cSPaulo Alcantara static int __dfs_referral_walk(struct cifs_mount_ctx *mnt_ctx, 140ce04127cSPaulo Alcantara struct dfs_ref_walk *rw) 141ce04127cSPaulo Alcantara { 142ce04127cSPaulo Alcantara struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; 143ce04127cSPaulo Alcantara struct dfs_info3_param tgt = {}; 144ce04127cSPaulo Alcantara bool is_refsrv; 145ce04127cSPaulo Alcantara int rc = -ENOENT; 146ce04127cSPaulo Alcantara 147ce04127cSPaulo Alcantara again: 148ce04127cSPaulo Alcantara do { 149ce04127cSPaulo Alcantara if (ref_walk_empty(rw)) { 150ce04127cSPaulo Alcantara rc = dfs_get_referral(mnt_ctx, ref_walk_path(rw) + 1, 151ce04127cSPaulo Alcantara NULL, ref_walk_tl(rw)); 152ce04127cSPaulo Alcantara if (rc) { 153ce04127cSPaulo Alcantara rc = cifs_mount_get_tcon(mnt_ctx); 154ce04127cSPaulo Alcantara if (!rc) 155ce04127cSPaulo Alcantara rc = cifs_is_path_remote(mnt_ctx); 156ce04127cSPaulo Alcantara continue; 157ce04127cSPaulo Alcantara } 158ce04127cSPaulo Alcantara if (!ref_walk_num_tgts(rw)) { 159ce04127cSPaulo Alcantara rc = -ENOENT; 160ce04127cSPaulo Alcantara continue; 161ce04127cSPaulo Alcantara } 162ce04127cSPaulo Alcantara } 163ce04127cSPaulo Alcantara 164ce04127cSPaulo Alcantara while (ref_walk_next_tgt(rw)) { 165ce04127cSPaulo Alcantara rc = parse_dfs_target(ctx, rw, &tgt); 16638c8a9a5SSteve French if (rc) 167ce04127cSPaulo Alcantara continue; 16838c8a9a5SSteve French 16938c8a9a5SSteve French cifs_mount_put_conns(mnt_ctx); 170ce04127cSPaulo Alcantara rc = get_session(mnt_ctx, ref_walk_path(rw)); 17138c8a9a5SSteve French if (rc) 172ce04127cSPaulo Alcantara continue; 17338c8a9a5SSteve French 174ce04127cSPaulo Alcantara is_refsrv = tgt.server_type == DFS_TYPE_ROOT || 175ce04127cSPaulo Alcantara DFS_INTERLINK(tgt.flags); 176ce04127cSPaulo Alcantara ref_walk_set_tgt_hint(rw); 17738c8a9a5SSteve French 178ce04127cSPaulo Alcantara if (tgt.flags & DFSREF_STORAGE_SERVER) { 17938c8a9a5SSteve French rc = cifs_mount_get_tcon(mnt_ctx); 180ce04127cSPaulo Alcantara if (!rc) 181ce04127cSPaulo Alcantara rc = cifs_is_path_remote(mnt_ctx); 182ce04127cSPaulo Alcantara if (!rc) 183ce04127cSPaulo Alcantara break; 184ce04127cSPaulo Alcantara if (rc != -EREMOTE) 185ce04127cSPaulo Alcantara continue; 186ce04127cSPaulo Alcantara } 187ce04127cSPaulo Alcantara 188ce04127cSPaulo Alcantara if (is_refsrv) { 189ce04127cSPaulo Alcantara rc = add_root_smb_session(mnt_ctx); 19038c8a9a5SSteve French if (rc) 19138c8a9a5SSteve French goto out; 19238c8a9a5SSteve French } 19338c8a9a5SSteve French 194ce04127cSPaulo Alcantara rc = ref_walk_advance(rw); 195ce04127cSPaulo Alcantara if (!rc) { 196ce04127cSPaulo Alcantara rc = set_ref_paths(mnt_ctx, &tgt, rw); 197ce04127cSPaulo Alcantara if (!rc) { 198ce04127cSPaulo Alcantara rc = -EREMOTE; 199ce04127cSPaulo Alcantara goto again; 20038c8a9a5SSteve French } 201ce04127cSPaulo Alcantara } 202ce04127cSPaulo Alcantara if (rc != -ELOOP) 203ce04127cSPaulo Alcantara goto out; 204ce04127cSPaulo Alcantara } 205ce04127cSPaulo Alcantara } while (rc && ref_walk_descend(rw)); 20638c8a9a5SSteve French 20738c8a9a5SSteve French out: 208ce04127cSPaulo Alcantara free_dfs_info_param(&tgt); 209ce04127cSPaulo Alcantara return rc; 210ce04127cSPaulo Alcantara } 211ce04127cSPaulo Alcantara 212ce04127cSPaulo Alcantara static int dfs_referral_walk(struct cifs_mount_ctx *mnt_ctx) 213ce04127cSPaulo Alcantara { 214ce04127cSPaulo Alcantara struct dfs_ref_walk *rw; 215ce04127cSPaulo Alcantara int rc; 216ce04127cSPaulo Alcantara 217ce04127cSPaulo Alcantara rw = ref_walk_alloc(); 218ce04127cSPaulo Alcantara if (IS_ERR(rw)) 219ce04127cSPaulo Alcantara return PTR_ERR(rw); 220ce04127cSPaulo Alcantara 221ce04127cSPaulo Alcantara ref_walk_init(rw); 222ce04127cSPaulo Alcantara rc = set_ref_paths(mnt_ctx, NULL, rw); 223ce04127cSPaulo Alcantara if (!rc) 224ce04127cSPaulo Alcantara rc = __dfs_referral_walk(mnt_ctx, rw); 225ce04127cSPaulo Alcantara ref_walk_free(rw); 22638c8a9a5SSteve French return rc; 22738c8a9a5SSteve French } 22838c8a9a5SSteve French 22938c8a9a5SSteve French static int __dfs_mount_share(struct cifs_mount_ctx *mnt_ctx) 23038c8a9a5SSteve French { 23138c8a9a5SSteve French struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb; 23238c8a9a5SSteve French struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; 23338c8a9a5SSteve French struct cifs_tcon *tcon; 234ce04127cSPaulo Alcantara char *origin_fullpath; 23538c8a9a5SSteve French int rc; 23638c8a9a5SSteve French 237ce04127cSPaulo Alcantara origin_fullpath = dfs_get_path(cifs_sb, ctx->source); 238ce04127cSPaulo Alcantara if (IS_ERR(origin_fullpath)) 239ce04127cSPaulo Alcantara return PTR_ERR(origin_fullpath); 24038c8a9a5SSteve French 241ce04127cSPaulo Alcantara rc = dfs_referral_walk(mnt_ctx); 242ce04127cSPaulo Alcantara if (rc) 24338c8a9a5SSteve French goto out; 24438c8a9a5SSteve French 24538c8a9a5SSteve French tcon = mnt_ctx->tcon; 2463ae872deSPaulo Alcantara spin_lock(&tcon->tc_lock); 2473ae872deSPaulo Alcantara if (!tcon->origin_fullpath) { 2483ae872deSPaulo Alcantara tcon->origin_fullpath = origin_fullpath; 24938c8a9a5SSteve French origin_fullpath = NULL; 25038c8a9a5SSteve French } 2513ae872deSPaulo Alcantara spin_unlock(&tcon->tc_lock); 25238c8a9a5SSteve French 25338c8a9a5SSteve French if (list_empty(&tcon->dfs_ses_list)) { 254ce04127cSPaulo Alcantara list_replace_init(&mnt_ctx->dfs_ses_list, &tcon->dfs_ses_list); 25538c8a9a5SSteve French queue_delayed_work(dfscache_wq, &tcon->dfs_cache_work, 25638c8a9a5SSteve French dfs_cache_get_ttl() * HZ); 25738c8a9a5SSteve French } else { 25838c8a9a5SSteve French dfs_put_root_smb_sessions(&mnt_ctx->dfs_ses_list); 25938c8a9a5SSteve French } 26038c8a9a5SSteve French 26138c8a9a5SSteve French out: 26238c8a9a5SSteve French kfree(origin_fullpath); 26338c8a9a5SSteve French return rc; 26438c8a9a5SSteve French } 26538c8a9a5SSteve French 266390c08fdSPaulo Alcantara /* 267390c08fdSPaulo Alcantara * If @ctx->dfs_automount, then update @ctx->dstaddr earlier with the DFS root 268390c08fdSPaulo Alcantara * server from where we'll start following any referrals. Otherwise rely on the 269390c08fdSPaulo Alcantara * value provided by mount(2) as the user might not have dns_resolver key set up 270390c08fdSPaulo Alcantara * and therefore failing to upcall to resolve UNC hostname under @ctx->source. 271390c08fdSPaulo Alcantara */ 272348a04a8SPaulo Alcantara static int update_fs_context_dstaddr(struct smb3_fs_context *ctx) 273348a04a8SPaulo Alcantara { 274348a04a8SPaulo Alcantara struct sockaddr *addr = (struct sockaddr *)&ctx->dstaddr; 275390c08fdSPaulo Alcantara int rc = 0; 276348a04a8SPaulo Alcantara 277390c08fdSPaulo Alcantara if (!ctx->nodfs && ctx->dfs_automount) { 278348a04a8SPaulo Alcantara rc = dns_resolve_server_name_to_ip(ctx->source, addr, NULL); 279348a04a8SPaulo Alcantara if (!rc) 280348a04a8SPaulo Alcantara cifs_set_port(addr, ctx->port); 281390c08fdSPaulo Alcantara ctx->dfs_automount = false; 282390c08fdSPaulo Alcantara } 283348a04a8SPaulo Alcantara return rc; 284348a04a8SPaulo Alcantara } 285348a04a8SPaulo Alcantara 28638c8a9a5SSteve French int dfs_mount_share(struct cifs_mount_ctx *mnt_ctx, bool *isdfs) 28738c8a9a5SSteve French { 28838c8a9a5SSteve French struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; 28938c8a9a5SSteve French bool nodfs = ctx->nodfs; 29038c8a9a5SSteve French int rc; 29138c8a9a5SSteve French 292348a04a8SPaulo Alcantara rc = update_fs_context_dstaddr(ctx); 293348a04a8SPaulo Alcantara if (rc) 294348a04a8SPaulo Alcantara return rc; 295348a04a8SPaulo Alcantara 29638c8a9a5SSteve French *isdfs = false; 29738c8a9a5SSteve French rc = get_session(mnt_ctx, NULL); 29838c8a9a5SSteve French if (rc) 2993ae872deSPaulo Alcantara return rc; 30038c8a9a5SSteve French 30138c8a9a5SSteve French ctx->dfs_root_ses = mnt_ctx->ses; 30238c8a9a5SSteve French /* 30338c8a9a5SSteve French * If called with 'nodfs' mount option, then skip DFS resolving. Otherwise unconditionally 30438c8a9a5SSteve French * try to get an DFS referral (even cached) to determine whether it is an DFS mount. 30538c8a9a5SSteve French * 30638c8a9a5SSteve French * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem 30738c8a9a5SSteve French * to respond with PATH_NOT_COVERED to requests that include the prefix. 30838c8a9a5SSteve French */ 30938c8a9a5SSteve French if (!nodfs) { 31038c8a9a5SSteve French rc = dfs_get_referral(mnt_ctx, ctx->UNC + 1, NULL, NULL); 31138c8a9a5SSteve French if (rc) { 3125f2a0afaSPaulo Alcantara cifs_dbg(FYI, "%s: no dfs referral for %s: %d\n", 3135f2a0afaSPaulo Alcantara __func__, ctx->UNC + 1, rc); 3145f2a0afaSPaulo Alcantara cifs_dbg(FYI, "%s: assuming non-dfs mount...\n", __func__); 31538c8a9a5SSteve French nodfs = true; 31638c8a9a5SSteve French } 31738c8a9a5SSteve French } 31838c8a9a5SSteve French if (nodfs) { 31938c8a9a5SSteve French rc = cifs_mount_get_tcon(mnt_ctx); 32038c8a9a5SSteve French if (!rc) 32138c8a9a5SSteve French rc = cifs_is_path_remote(mnt_ctx); 3223ae872deSPaulo Alcantara return rc; 32338c8a9a5SSteve French } 32438c8a9a5SSteve French 32538c8a9a5SSteve French *isdfs = true; 326bf99f6beSPaulo Alcantara add_root_smb_session(mnt_ctx); 327bf99f6beSPaulo Alcantara return __dfs_mount_share(mnt_ctx); 32838c8a9a5SSteve French } 32938c8a9a5SSteve French 33038c8a9a5SSteve French /* Update dfs referral path of superblock */ 33138c8a9a5SSteve French static int update_server_fullpath(struct TCP_Server_Info *server, struct cifs_sb_info *cifs_sb, 33238c8a9a5SSteve French const char *target) 33338c8a9a5SSteve French { 33438c8a9a5SSteve French int rc = 0; 33538c8a9a5SSteve French size_t len = strlen(target); 33638c8a9a5SSteve French char *refpath, *npath; 33738c8a9a5SSteve French 33838c8a9a5SSteve French if (unlikely(len < 2 || *target != '\\')) 33938c8a9a5SSteve French return -EINVAL; 34038c8a9a5SSteve French 34138c8a9a5SSteve French if (target[1] == '\\') { 34238c8a9a5SSteve French len += 1; 34338c8a9a5SSteve French refpath = kmalloc(len, GFP_KERNEL); 34438c8a9a5SSteve French if (!refpath) 34538c8a9a5SSteve French return -ENOMEM; 34638c8a9a5SSteve French 34738c8a9a5SSteve French scnprintf(refpath, len, "%s", target); 34838c8a9a5SSteve French } else { 34938c8a9a5SSteve French len += sizeof("\\"); 35038c8a9a5SSteve French refpath = kmalloc(len, GFP_KERNEL); 35138c8a9a5SSteve French if (!refpath) 35238c8a9a5SSteve French return -ENOMEM; 35338c8a9a5SSteve French 35438c8a9a5SSteve French scnprintf(refpath, len, "\\%s", target); 35538c8a9a5SSteve French } 35638c8a9a5SSteve French 35738c8a9a5SSteve French npath = dfs_cache_canonical_path(refpath, cifs_sb->local_nls, cifs_remap(cifs_sb)); 35838c8a9a5SSteve French kfree(refpath); 35938c8a9a5SSteve French 36038c8a9a5SSteve French if (IS_ERR(npath)) { 36138c8a9a5SSteve French rc = PTR_ERR(npath); 36238c8a9a5SSteve French } else { 36338c8a9a5SSteve French mutex_lock(&server->refpath_lock); 36438c8a9a5SSteve French spin_lock(&server->srv_lock); 36538c8a9a5SSteve French kfree(server->leaf_fullpath); 36638c8a9a5SSteve French server->leaf_fullpath = npath; 36738c8a9a5SSteve French spin_unlock(&server->srv_lock); 36838c8a9a5SSteve French mutex_unlock(&server->refpath_lock); 36938c8a9a5SSteve French } 37038c8a9a5SSteve French return rc; 37138c8a9a5SSteve French } 37238c8a9a5SSteve French 37338c8a9a5SSteve French static int target_share_matches_server(struct TCP_Server_Info *server, char *share, 37438c8a9a5SSteve French bool *target_match) 37538c8a9a5SSteve French { 37638c8a9a5SSteve French int rc = 0; 37738c8a9a5SSteve French const char *dfs_host; 37838c8a9a5SSteve French size_t dfs_host_len; 37938c8a9a5SSteve French 38038c8a9a5SSteve French *target_match = true; 38138c8a9a5SSteve French extract_unc_hostname(share, &dfs_host, &dfs_host_len); 38238c8a9a5SSteve French 38338c8a9a5SSteve French /* Check if hostnames or addresses match */ 38438c8a9a5SSteve French cifs_server_lock(server); 38538c8a9a5SSteve French if (dfs_host_len != strlen(server->hostname) || 38638c8a9a5SSteve French strncasecmp(dfs_host, server->hostname, dfs_host_len)) { 38738c8a9a5SSteve French cifs_dbg(FYI, "%s: %.*s doesn't match %s\n", __func__, 38838c8a9a5SSteve French (int)dfs_host_len, dfs_host, server->hostname); 38938c8a9a5SSteve French rc = match_target_ip(server, dfs_host, dfs_host_len, target_match); 39038c8a9a5SSteve French if (rc) 39138c8a9a5SSteve French cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc); 39238c8a9a5SSteve French } 39338c8a9a5SSteve French cifs_server_unlock(server); 39438c8a9a5SSteve French return rc; 39538c8a9a5SSteve French } 39638c8a9a5SSteve French 39738c8a9a5SSteve French static void __tree_connect_ipc(const unsigned int xid, char *tree, 39838c8a9a5SSteve French struct cifs_sb_info *cifs_sb, 39938c8a9a5SSteve French struct cifs_ses *ses) 40038c8a9a5SSteve French { 40138c8a9a5SSteve French struct TCP_Server_Info *server = ses->server; 40238c8a9a5SSteve French struct cifs_tcon *tcon = ses->tcon_ipc; 40338c8a9a5SSteve French int rc; 40438c8a9a5SSteve French 40538c8a9a5SSteve French spin_lock(&ses->ses_lock); 40638c8a9a5SSteve French spin_lock(&ses->chan_lock); 40738c8a9a5SSteve French if (cifs_chan_needs_reconnect(ses, server) || 40838c8a9a5SSteve French ses->ses_status != SES_GOOD) { 40938c8a9a5SSteve French spin_unlock(&ses->chan_lock); 41038c8a9a5SSteve French spin_unlock(&ses->ses_lock); 41138c8a9a5SSteve French cifs_server_dbg(FYI, "%s: skipping ipc reconnect due to disconnected ses\n", 41238c8a9a5SSteve French __func__); 41338c8a9a5SSteve French return; 41438c8a9a5SSteve French } 41538c8a9a5SSteve French spin_unlock(&ses->chan_lock); 41638c8a9a5SSteve French spin_unlock(&ses->ses_lock); 41738c8a9a5SSteve French 41838c8a9a5SSteve French cifs_server_lock(server); 41938c8a9a5SSteve French scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname); 42038c8a9a5SSteve French cifs_server_unlock(server); 42138c8a9a5SSteve French 42238c8a9a5SSteve French rc = server->ops->tree_connect(xid, ses, tree, tcon, 42338c8a9a5SSteve French cifs_sb->local_nls); 42438c8a9a5SSteve French cifs_server_dbg(FYI, "%s: tree_reconnect %s: %d\n", __func__, tree, rc); 42538c8a9a5SSteve French spin_lock(&tcon->tc_lock); 42638c8a9a5SSteve French if (rc) { 42738c8a9a5SSteve French tcon->status = TID_NEED_TCON; 42838c8a9a5SSteve French } else { 42938c8a9a5SSteve French tcon->status = TID_GOOD; 43038c8a9a5SSteve French tcon->need_reconnect = false; 43138c8a9a5SSteve French } 43238c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 43338c8a9a5SSteve French } 43438c8a9a5SSteve French 43538c8a9a5SSteve French static void tree_connect_ipc(const unsigned int xid, char *tree, 43638c8a9a5SSteve French struct cifs_sb_info *cifs_sb, 43738c8a9a5SSteve French struct cifs_tcon *tcon) 43838c8a9a5SSteve French { 43938c8a9a5SSteve French struct cifs_ses *ses = tcon->ses; 44038c8a9a5SSteve French 44138c8a9a5SSteve French __tree_connect_ipc(xid, tree, cifs_sb, ses); 44238c8a9a5SSteve French __tree_connect_ipc(xid, tree, cifs_sb, CIFS_DFS_ROOT_SES(ses)); 44338c8a9a5SSteve French } 44438c8a9a5SSteve French 44538c8a9a5SSteve French static int __tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon, 44638c8a9a5SSteve French struct cifs_sb_info *cifs_sb, char *tree, bool islink, 44738c8a9a5SSteve French struct dfs_cache_tgt_list *tl) 44838c8a9a5SSteve French { 44938c8a9a5SSteve French int rc; 45038c8a9a5SSteve French struct TCP_Server_Info *server = tcon->ses->server; 45138c8a9a5SSteve French const struct smb_version_operations *ops = server->ops; 45238c8a9a5SSteve French struct cifs_ses *root_ses = CIFS_DFS_ROOT_SES(tcon->ses); 45338c8a9a5SSteve French char *share = NULL, *prefix = NULL; 45438c8a9a5SSteve French struct dfs_cache_tgt_iterator *tit; 45538c8a9a5SSteve French bool target_match; 45638c8a9a5SSteve French 45738c8a9a5SSteve French tit = dfs_cache_get_tgt_iterator(tl); 45838c8a9a5SSteve French if (!tit) { 45938c8a9a5SSteve French rc = -ENOENT; 46038c8a9a5SSteve French goto out; 46138c8a9a5SSteve French } 46238c8a9a5SSteve French 46338c8a9a5SSteve French /* Try to tree connect to all dfs targets */ 46438c8a9a5SSteve French for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) { 46538c8a9a5SSteve French const char *target = dfs_cache_get_tgt_name(tit); 4663fea12f3SPaulo Alcantara DFS_CACHE_TGT_LIST(ntl); 46738c8a9a5SSteve French 46838c8a9a5SSteve French kfree(share); 46938c8a9a5SSteve French kfree(prefix); 47038c8a9a5SSteve French share = prefix = NULL; 47138c8a9a5SSteve French 47238c8a9a5SSteve French /* Check if share matches with tcp ses */ 47338c8a9a5SSteve French rc = dfs_cache_get_tgt_share(server->leaf_fullpath + 1, tit, &share, &prefix); 47438c8a9a5SSteve French if (rc) { 47538c8a9a5SSteve French cifs_dbg(VFS, "%s: failed to parse target share: %d\n", __func__, rc); 47638c8a9a5SSteve French break; 47738c8a9a5SSteve French } 47838c8a9a5SSteve French 47938c8a9a5SSteve French rc = target_share_matches_server(server, share, &target_match); 48038c8a9a5SSteve French if (rc) 48138c8a9a5SSteve French break; 48238c8a9a5SSteve French if (!target_match) { 48338c8a9a5SSteve French rc = -EHOSTUNREACH; 48438c8a9a5SSteve French continue; 48538c8a9a5SSteve French } 48638c8a9a5SSteve French 48738c8a9a5SSteve French dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, tit); 48838c8a9a5SSteve French tree_connect_ipc(xid, tree, cifs_sb, tcon); 48938c8a9a5SSteve French 49038c8a9a5SSteve French scnprintf(tree, MAX_TREE_SIZE, "\\%s", share); 49138c8a9a5SSteve French if (!islink) { 49238c8a9a5SSteve French rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls); 49338c8a9a5SSteve French break; 49438c8a9a5SSteve French } 49538c8a9a5SSteve French 49638c8a9a5SSteve French /* 49738c8a9a5SSteve French * If no dfs referrals were returned from link target, then just do a TREE_CONNECT 49838c8a9a5SSteve French * to it. Otherwise, cache the dfs referral and then mark current tcp ses for 49938c8a9a5SSteve French * reconnect so either the demultiplex thread or the echo worker will reconnect to 50038c8a9a5SSteve French * newly resolved target. 50138c8a9a5SSteve French */ 50238c8a9a5SSteve French if (dfs_cache_find(xid, root_ses, cifs_sb->local_nls, cifs_remap(cifs_sb), target, 50338c8a9a5SSteve French NULL, &ntl)) { 50438c8a9a5SSteve French rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls); 50538c8a9a5SSteve French if (rc) 50638c8a9a5SSteve French continue; 50738c8a9a5SSteve French 50838c8a9a5SSteve French rc = cifs_update_super_prepath(cifs_sb, prefix); 50938c8a9a5SSteve French } else { 51038c8a9a5SSteve French /* Target is another dfs share */ 51138c8a9a5SSteve French rc = update_server_fullpath(server, cifs_sb, target); 51238c8a9a5SSteve French dfs_cache_free_tgts(tl); 51338c8a9a5SSteve French 51438c8a9a5SSteve French if (!rc) { 51538c8a9a5SSteve French rc = -EREMOTE; 51638c8a9a5SSteve French list_replace_init(&ntl.tl_list, &tl->tl_list); 51738c8a9a5SSteve French } else 51838c8a9a5SSteve French dfs_cache_free_tgts(&ntl); 51938c8a9a5SSteve French } 52038c8a9a5SSteve French break; 52138c8a9a5SSteve French } 52238c8a9a5SSteve French 52338c8a9a5SSteve French out: 52438c8a9a5SSteve French kfree(share); 52538c8a9a5SSteve French kfree(prefix); 52638c8a9a5SSteve French 52738c8a9a5SSteve French return rc; 52838c8a9a5SSteve French } 52938c8a9a5SSteve French 53038c8a9a5SSteve French static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon, 53138c8a9a5SSteve French struct cifs_sb_info *cifs_sb, char *tree, bool islink, 53238c8a9a5SSteve French struct dfs_cache_tgt_list *tl) 53338c8a9a5SSteve French { 53438c8a9a5SSteve French int rc; 53538c8a9a5SSteve French int num_links = 0; 53638c8a9a5SSteve French struct TCP_Server_Info *server = tcon->ses->server; 53738c8a9a5SSteve French char *old_fullpath = server->leaf_fullpath; 53838c8a9a5SSteve French 53938c8a9a5SSteve French do { 54038c8a9a5SSteve French rc = __tree_connect_dfs_target(xid, tcon, cifs_sb, tree, islink, tl); 54138c8a9a5SSteve French if (!rc || rc != -EREMOTE) 54238c8a9a5SSteve French break; 54338c8a9a5SSteve French } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS); 54438c8a9a5SSteve French /* 54538c8a9a5SSteve French * If we couldn't tree connect to any targets from last referral path, then 54638c8a9a5SSteve French * retry it from newly resolved dfs referral. 54738c8a9a5SSteve French */ 54838c8a9a5SSteve French if (rc && server->leaf_fullpath != old_fullpath) 54938c8a9a5SSteve French cifs_signal_cifsd_for_reconnect(server, true); 55038c8a9a5SSteve French 55138c8a9a5SSteve French dfs_cache_free_tgts(tl); 55238c8a9a5SSteve French return rc; 55338c8a9a5SSteve French } 55438c8a9a5SSteve French 55538c8a9a5SSteve French int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc) 55638c8a9a5SSteve French { 55738c8a9a5SSteve French int rc; 55838c8a9a5SSteve French struct TCP_Server_Info *server = tcon->ses->server; 55938c8a9a5SSteve French const struct smb_version_operations *ops = server->ops; 5603fea12f3SPaulo Alcantara DFS_CACHE_TGT_LIST(tl); 5613ae872deSPaulo Alcantara struct cifs_sb_info *cifs_sb = NULL; 5623ae872deSPaulo Alcantara struct super_block *sb = NULL; 56338c8a9a5SSteve French struct dfs_info3_param ref = {0}; 5643ae872deSPaulo Alcantara char *tree; 56538c8a9a5SSteve French 56638c8a9a5SSteve French /* only send once per connect */ 56738c8a9a5SSteve French spin_lock(&tcon->tc_lock); 568*cd743cfeSShyam Prasad N 569*cd743cfeSShyam Prasad N /* if tcon is marked for needing reconnect, update state */ 570*cd743cfeSShyam Prasad N if (tcon->need_reconnect) 571*cd743cfeSShyam Prasad N tcon->status = TID_NEED_TCON; 572*cd743cfeSShyam Prasad N 57391f4480cSShyam Prasad N if (tcon->status == TID_GOOD) { 57491f4480cSShyam Prasad N spin_unlock(&tcon->tc_lock); 57591f4480cSShyam Prasad N return 0; 57691f4480cSShyam Prasad N } 57791f4480cSShyam Prasad N 57838c8a9a5SSteve French if (tcon->status != TID_NEW && 57938c8a9a5SSteve French tcon->status != TID_NEED_TCON) { 58038c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 58138c8a9a5SSteve French return -EHOSTDOWN; 58238c8a9a5SSteve French } 58338c8a9a5SSteve French 58438c8a9a5SSteve French tcon->status = TID_IN_TCON; 58538c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 58638c8a9a5SSteve French 58738c8a9a5SSteve French tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL); 58838c8a9a5SSteve French if (!tree) { 58938c8a9a5SSteve French rc = -ENOMEM; 59038c8a9a5SSteve French goto out; 59138c8a9a5SSteve French } 59238c8a9a5SSteve French 59338c8a9a5SSteve French if (tcon->ipc) { 59438c8a9a5SSteve French cifs_server_lock(server); 59538c8a9a5SSteve French scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname); 59638c8a9a5SSteve French cifs_server_unlock(server); 59738c8a9a5SSteve French rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc); 59838c8a9a5SSteve French goto out; 59938c8a9a5SSteve French } 60038c8a9a5SSteve French 6013ae872deSPaulo Alcantara sb = cifs_get_dfs_tcon_super(tcon); 6023ae872deSPaulo Alcantara if (!IS_ERR(sb)) 60338c8a9a5SSteve French cifs_sb = CIFS_SB(sb); 60438c8a9a5SSteve French 6053ae872deSPaulo Alcantara /* 6063ae872deSPaulo Alcantara * Tree connect to last share in @tcon->tree_name whether dfs super or 6073ae872deSPaulo Alcantara * cached dfs referral was not found. 6083ae872deSPaulo Alcantara */ 6093ae872deSPaulo Alcantara if (!cifs_sb || !server->leaf_fullpath || 61038c8a9a5SSteve French dfs_cache_noreq_find(server->leaf_fullpath + 1, &ref, &tl)) { 6113ae872deSPaulo Alcantara rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon, 6123ae872deSPaulo Alcantara cifs_sb ? cifs_sb->local_nls : nlsc); 61338c8a9a5SSteve French goto out; 61438c8a9a5SSteve French } 61538c8a9a5SSteve French 61638c8a9a5SSteve French rc = tree_connect_dfs_target(xid, tcon, cifs_sb, tree, ref.server_type == DFS_TYPE_LINK, 61738c8a9a5SSteve French &tl); 61838c8a9a5SSteve French free_dfs_info_param(&ref); 61938c8a9a5SSteve French 62038c8a9a5SSteve French out: 62138c8a9a5SSteve French kfree(tree); 62238c8a9a5SSteve French cifs_put_tcp_super(sb); 62338c8a9a5SSteve French 62438c8a9a5SSteve French if (rc) { 62538c8a9a5SSteve French spin_lock(&tcon->tc_lock); 62638c8a9a5SSteve French if (tcon->status == TID_IN_TCON) 62738c8a9a5SSteve French tcon->status = TID_NEED_TCON; 62838c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 62938c8a9a5SSteve French } else { 63038c8a9a5SSteve French spin_lock(&tcon->tc_lock); 63138c8a9a5SSteve French if (tcon->status == TID_IN_TCON) 63238c8a9a5SSteve French tcon->status = TID_GOOD; 63338c8a9a5SSteve French tcon->need_reconnect = false; 634*cd743cfeSShyam Prasad N spin_unlock(&tcon->tc_lock); 63538c8a9a5SSteve French } 63638c8a9a5SSteve French 63738c8a9a5SSteve French return rc; 63838c8a9a5SSteve French } 639