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 98*ce04127cSPaulo Alcantara static inline int parse_dfs_target(struct smb3_fs_context *ctx, 99*ce04127cSPaulo Alcantara struct dfs_ref_walk *rw, 100*ce04127cSPaulo Alcantara struct dfs_info3_param *tgt) 101*ce04127cSPaulo Alcantara { 102*ce04127cSPaulo Alcantara int rc; 103*ce04127cSPaulo Alcantara const char *fpath = ref_walk_fpath(rw) + 1; 104*ce04127cSPaulo Alcantara 105*ce04127cSPaulo Alcantara rc = ref_walk_get_tgt(rw, tgt); 106*ce04127cSPaulo Alcantara if (!rc) 107*ce04127cSPaulo Alcantara rc = dfs_parse_target_referral(fpath, tgt, ctx); 108*ce04127cSPaulo Alcantara return rc; 109*ce04127cSPaulo Alcantara } 110*ce04127cSPaulo Alcantara 111*ce04127cSPaulo Alcantara static int set_ref_paths(struct cifs_mount_ctx *mnt_ctx, 112*ce04127cSPaulo Alcantara struct dfs_info3_param *tgt, 113*ce04127cSPaulo Alcantara struct dfs_ref_walk *rw) 11438c8a9a5SSteve French { 11538c8a9a5SSteve French struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; 116*ce04127cSPaulo Alcantara struct cifs_sb_info *cifs_sb = mnt_ctx->cifs_sb; 117*ce04127cSPaulo Alcantara char *ref_path, *full_path; 118*ce04127cSPaulo Alcantara int rc; 11938c8a9a5SSteve French 120*ce04127cSPaulo Alcantara full_path = smb3_fs_context_fullpath(ctx, CIFS_DIR_SEP(cifs_sb)); 121*ce04127cSPaulo Alcantara if (IS_ERR(full_path)) 122*ce04127cSPaulo Alcantara return PTR_ERR(full_path); 123*ce04127cSPaulo Alcantara 124*ce04127cSPaulo Alcantara if (!tgt || (tgt->server_type == DFS_TYPE_LINK && 125*ce04127cSPaulo Alcantara DFS_INTERLINK(tgt->flags))) 126*ce04127cSPaulo Alcantara ref_path = dfs_get_path(cifs_sb, ctx->UNC); 127*ce04127cSPaulo Alcantara else 128*ce04127cSPaulo Alcantara ref_path = dfs_get_path(cifs_sb, full_path); 129*ce04127cSPaulo Alcantara if (IS_ERR(ref_path)) { 130*ce04127cSPaulo Alcantara rc = PTR_ERR(ref_path); 131*ce04127cSPaulo Alcantara kfree(full_path); 13238c8a9a5SSteve French return rc; 133*ce04127cSPaulo Alcantara } 134*ce04127cSPaulo Alcantara ref_walk_path(rw) = ref_path; 135*ce04127cSPaulo Alcantara ref_walk_fpath(rw) = full_path; 136*ce04127cSPaulo Alcantara return 0; 137*ce04127cSPaulo Alcantara } 13838c8a9a5SSteve French 139*ce04127cSPaulo Alcantara static int __dfs_referral_walk(struct cifs_mount_ctx *mnt_ctx, 140*ce04127cSPaulo Alcantara struct dfs_ref_walk *rw) 141*ce04127cSPaulo Alcantara { 142*ce04127cSPaulo Alcantara struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; 143*ce04127cSPaulo Alcantara struct dfs_info3_param tgt = {}; 144*ce04127cSPaulo Alcantara bool is_refsrv; 145*ce04127cSPaulo Alcantara int rc = -ENOENT; 146*ce04127cSPaulo Alcantara 147*ce04127cSPaulo Alcantara again: 148*ce04127cSPaulo Alcantara do { 149*ce04127cSPaulo Alcantara if (ref_walk_empty(rw)) { 150*ce04127cSPaulo Alcantara rc = dfs_get_referral(mnt_ctx, ref_walk_path(rw) + 1, 151*ce04127cSPaulo Alcantara NULL, ref_walk_tl(rw)); 152*ce04127cSPaulo Alcantara if (rc) { 153*ce04127cSPaulo Alcantara rc = cifs_mount_get_tcon(mnt_ctx); 154*ce04127cSPaulo Alcantara if (!rc) 155*ce04127cSPaulo Alcantara rc = cifs_is_path_remote(mnt_ctx); 156*ce04127cSPaulo Alcantara continue; 157*ce04127cSPaulo Alcantara } 158*ce04127cSPaulo Alcantara if (!ref_walk_num_tgts(rw)) { 159*ce04127cSPaulo Alcantara rc = -ENOENT; 160*ce04127cSPaulo Alcantara continue; 161*ce04127cSPaulo Alcantara } 162*ce04127cSPaulo Alcantara } 163*ce04127cSPaulo Alcantara 164*ce04127cSPaulo Alcantara while (ref_walk_next_tgt(rw)) { 165*ce04127cSPaulo Alcantara rc = parse_dfs_target(ctx, rw, &tgt); 16638c8a9a5SSteve French if (rc) 167*ce04127cSPaulo Alcantara continue; 16838c8a9a5SSteve French 16938c8a9a5SSteve French cifs_mount_put_conns(mnt_ctx); 170*ce04127cSPaulo Alcantara rc = get_session(mnt_ctx, ref_walk_path(rw)); 17138c8a9a5SSteve French if (rc) 172*ce04127cSPaulo Alcantara continue; 17338c8a9a5SSteve French 174*ce04127cSPaulo Alcantara is_refsrv = tgt.server_type == DFS_TYPE_ROOT || 175*ce04127cSPaulo Alcantara DFS_INTERLINK(tgt.flags); 176*ce04127cSPaulo Alcantara ref_walk_set_tgt_hint(rw); 17738c8a9a5SSteve French 178*ce04127cSPaulo Alcantara if (tgt.flags & DFSREF_STORAGE_SERVER) { 17938c8a9a5SSteve French rc = cifs_mount_get_tcon(mnt_ctx); 180*ce04127cSPaulo Alcantara if (!rc) 181*ce04127cSPaulo Alcantara rc = cifs_is_path_remote(mnt_ctx); 182*ce04127cSPaulo Alcantara if (!rc) 183*ce04127cSPaulo Alcantara break; 184*ce04127cSPaulo Alcantara if (rc != -EREMOTE) 185*ce04127cSPaulo Alcantara continue; 186*ce04127cSPaulo Alcantara } 187*ce04127cSPaulo Alcantara 188*ce04127cSPaulo Alcantara if (is_refsrv) { 189*ce04127cSPaulo Alcantara rc = add_root_smb_session(mnt_ctx); 19038c8a9a5SSteve French if (rc) 19138c8a9a5SSteve French goto out; 19238c8a9a5SSteve French } 19338c8a9a5SSteve French 194*ce04127cSPaulo Alcantara rc = ref_walk_advance(rw); 195*ce04127cSPaulo Alcantara if (!rc) { 196*ce04127cSPaulo Alcantara rc = set_ref_paths(mnt_ctx, &tgt, rw); 197*ce04127cSPaulo Alcantara if (!rc) { 198*ce04127cSPaulo Alcantara rc = -EREMOTE; 199*ce04127cSPaulo Alcantara goto again; 20038c8a9a5SSteve French } 201*ce04127cSPaulo Alcantara } 202*ce04127cSPaulo Alcantara if (rc != -ELOOP) 203*ce04127cSPaulo Alcantara goto out; 204*ce04127cSPaulo Alcantara } 205*ce04127cSPaulo Alcantara } while (rc && ref_walk_descend(rw)); 20638c8a9a5SSteve French 20738c8a9a5SSteve French out: 208*ce04127cSPaulo Alcantara free_dfs_info_param(&tgt); 209*ce04127cSPaulo Alcantara return rc; 210*ce04127cSPaulo Alcantara } 211*ce04127cSPaulo Alcantara 212*ce04127cSPaulo Alcantara static int dfs_referral_walk(struct cifs_mount_ctx *mnt_ctx) 213*ce04127cSPaulo Alcantara { 214*ce04127cSPaulo Alcantara struct dfs_ref_walk *rw; 215*ce04127cSPaulo Alcantara int rc; 216*ce04127cSPaulo Alcantara 217*ce04127cSPaulo Alcantara rw = ref_walk_alloc(); 218*ce04127cSPaulo Alcantara if (IS_ERR(rw)) 219*ce04127cSPaulo Alcantara return PTR_ERR(rw); 220*ce04127cSPaulo Alcantara 221*ce04127cSPaulo Alcantara ref_walk_init(rw); 222*ce04127cSPaulo Alcantara rc = set_ref_paths(mnt_ctx, NULL, rw); 223*ce04127cSPaulo Alcantara if (!rc) 224*ce04127cSPaulo Alcantara rc = __dfs_referral_walk(mnt_ctx, rw); 225*ce04127cSPaulo 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; 234*ce04127cSPaulo Alcantara char *origin_fullpath; 23538c8a9a5SSteve French int rc; 23638c8a9a5SSteve French 237*ce04127cSPaulo Alcantara origin_fullpath = dfs_get_path(cifs_sb, ctx->source); 238*ce04127cSPaulo Alcantara if (IS_ERR(origin_fullpath)) 239*ce04127cSPaulo Alcantara return PTR_ERR(origin_fullpath); 24038c8a9a5SSteve French 241*ce04127cSPaulo Alcantara rc = dfs_referral_walk(mnt_ctx); 242*ce04127cSPaulo 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)) { 254*ce04127cSPaulo 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 26638c8a9a5SSteve French int dfs_mount_share(struct cifs_mount_ctx *mnt_ctx, bool *isdfs) 26738c8a9a5SSteve French { 26838c8a9a5SSteve French struct smb3_fs_context *ctx = mnt_ctx->fs_ctx; 26938c8a9a5SSteve French bool nodfs = ctx->nodfs; 27038c8a9a5SSteve French int rc; 27138c8a9a5SSteve French 27238c8a9a5SSteve French *isdfs = false; 27338c8a9a5SSteve French rc = get_session(mnt_ctx, NULL); 27438c8a9a5SSteve French if (rc) 2753ae872deSPaulo Alcantara return rc; 27638c8a9a5SSteve French 27738c8a9a5SSteve French ctx->dfs_root_ses = mnt_ctx->ses; 27838c8a9a5SSteve French /* 27938c8a9a5SSteve French * If called with 'nodfs' mount option, then skip DFS resolving. Otherwise unconditionally 28038c8a9a5SSteve French * try to get an DFS referral (even cached) to determine whether it is an DFS mount. 28138c8a9a5SSteve French * 28238c8a9a5SSteve French * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem 28338c8a9a5SSteve French * to respond with PATH_NOT_COVERED to requests that include the prefix. 28438c8a9a5SSteve French */ 28538c8a9a5SSteve French if (!nodfs) { 28638c8a9a5SSteve French rc = dfs_get_referral(mnt_ctx, ctx->UNC + 1, NULL, NULL); 28738c8a9a5SSteve French if (rc) { 2885f2a0afaSPaulo Alcantara cifs_dbg(FYI, "%s: no dfs referral for %s: %d\n", 2895f2a0afaSPaulo Alcantara __func__, ctx->UNC + 1, rc); 2905f2a0afaSPaulo Alcantara cifs_dbg(FYI, "%s: assuming non-dfs mount...\n", __func__); 29138c8a9a5SSteve French nodfs = true; 29238c8a9a5SSteve French } 29338c8a9a5SSteve French } 29438c8a9a5SSteve French if (nodfs) { 29538c8a9a5SSteve French rc = cifs_mount_get_tcon(mnt_ctx); 29638c8a9a5SSteve French if (!rc) 29738c8a9a5SSteve French rc = cifs_is_path_remote(mnt_ctx); 2983ae872deSPaulo Alcantara return rc; 29938c8a9a5SSteve French } 30038c8a9a5SSteve French 30138c8a9a5SSteve French *isdfs = true; 302bf99f6beSPaulo Alcantara add_root_smb_session(mnt_ctx); 303bf99f6beSPaulo Alcantara return __dfs_mount_share(mnt_ctx); 30438c8a9a5SSteve French } 30538c8a9a5SSteve French 30638c8a9a5SSteve French /* Update dfs referral path of superblock */ 30738c8a9a5SSteve French static int update_server_fullpath(struct TCP_Server_Info *server, struct cifs_sb_info *cifs_sb, 30838c8a9a5SSteve French const char *target) 30938c8a9a5SSteve French { 31038c8a9a5SSteve French int rc = 0; 31138c8a9a5SSteve French size_t len = strlen(target); 31238c8a9a5SSteve French char *refpath, *npath; 31338c8a9a5SSteve French 31438c8a9a5SSteve French if (unlikely(len < 2 || *target != '\\')) 31538c8a9a5SSteve French return -EINVAL; 31638c8a9a5SSteve French 31738c8a9a5SSteve French if (target[1] == '\\') { 31838c8a9a5SSteve French len += 1; 31938c8a9a5SSteve French refpath = kmalloc(len, GFP_KERNEL); 32038c8a9a5SSteve French if (!refpath) 32138c8a9a5SSteve French return -ENOMEM; 32238c8a9a5SSteve French 32338c8a9a5SSteve French scnprintf(refpath, len, "%s", target); 32438c8a9a5SSteve French } else { 32538c8a9a5SSteve French len += sizeof("\\"); 32638c8a9a5SSteve French refpath = kmalloc(len, GFP_KERNEL); 32738c8a9a5SSteve French if (!refpath) 32838c8a9a5SSteve French return -ENOMEM; 32938c8a9a5SSteve French 33038c8a9a5SSteve French scnprintf(refpath, len, "\\%s", target); 33138c8a9a5SSteve French } 33238c8a9a5SSteve French 33338c8a9a5SSteve French npath = dfs_cache_canonical_path(refpath, cifs_sb->local_nls, cifs_remap(cifs_sb)); 33438c8a9a5SSteve French kfree(refpath); 33538c8a9a5SSteve French 33638c8a9a5SSteve French if (IS_ERR(npath)) { 33738c8a9a5SSteve French rc = PTR_ERR(npath); 33838c8a9a5SSteve French } else { 33938c8a9a5SSteve French mutex_lock(&server->refpath_lock); 34038c8a9a5SSteve French spin_lock(&server->srv_lock); 34138c8a9a5SSteve French kfree(server->leaf_fullpath); 34238c8a9a5SSteve French server->leaf_fullpath = npath; 34338c8a9a5SSteve French spin_unlock(&server->srv_lock); 34438c8a9a5SSteve French mutex_unlock(&server->refpath_lock); 34538c8a9a5SSteve French } 34638c8a9a5SSteve French return rc; 34738c8a9a5SSteve French } 34838c8a9a5SSteve French 34938c8a9a5SSteve French static int target_share_matches_server(struct TCP_Server_Info *server, char *share, 35038c8a9a5SSteve French bool *target_match) 35138c8a9a5SSteve French { 35238c8a9a5SSteve French int rc = 0; 35338c8a9a5SSteve French const char *dfs_host; 35438c8a9a5SSteve French size_t dfs_host_len; 35538c8a9a5SSteve French 35638c8a9a5SSteve French *target_match = true; 35738c8a9a5SSteve French extract_unc_hostname(share, &dfs_host, &dfs_host_len); 35838c8a9a5SSteve French 35938c8a9a5SSteve French /* Check if hostnames or addresses match */ 36038c8a9a5SSteve French cifs_server_lock(server); 36138c8a9a5SSteve French if (dfs_host_len != strlen(server->hostname) || 36238c8a9a5SSteve French strncasecmp(dfs_host, server->hostname, dfs_host_len)) { 36338c8a9a5SSteve French cifs_dbg(FYI, "%s: %.*s doesn't match %s\n", __func__, 36438c8a9a5SSteve French (int)dfs_host_len, dfs_host, server->hostname); 36538c8a9a5SSteve French rc = match_target_ip(server, dfs_host, dfs_host_len, target_match); 36638c8a9a5SSteve French if (rc) 36738c8a9a5SSteve French cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc); 36838c8a9a5SSteve French } 36938c8a9a5SSteve French cifs_server_unlock(server); 37038c8a9a5SSteve French return rc; 37138c8a9a5SSteve French } 37238c8a9a5SSteve French 37338c8a9a5SSteve French static void __tree_connect_ipc(const unsigned int xid, char *tree, 37438c8a9a5SSteve French struct cifs_sb_info *cifs_sb, 37538c8a9a5SSteve French struct cifs_ses *ses) 37638c8a9a5SSteve French { 37738c8a9a5SSteve French struct TCP_Server_Info *server = ses->server; 37838c8a9a5SSteve French struct cifs_tcon *tcon = ses->tcon_ipc; 37938c8a9a5SSteve French int rc; 38038c8a9a5SSteve French 38138c8a9a5SSteve French spin_lock(&ses->ses_lock); 38238c8a9a5SSteve French spin_lock(&ses->chan_lock); 38338c8a9a5SSteve French if (cifs_chan_needs_reconnect(ses, server) || 38438c8a9a5SSteve French ses->ses_status != SES_GOOD) { 38538c8a9a5SSteve French spin_unlock(&ses->chan_lock); 38638c8a9a5SSteve French spin_unlock(&ses->ses_lock); 38738c8a9a5SSteve French cifs_server_dbg(FYI, "%s: skipping ipc reconnect due to disconnected ses\n", 38838c8a9a5SSteve French __func__); 38938c8a9a5SSteve French return; 39038c8a9a5SSteve French } 39138c8a9a5SSteve French spin_unlock(&ses->chan_lock); 39238c8a9a5SSteve French spin_unlock(&ses->ses_lock); 39338c8a9a5SSteve French 39438c8a9a5SSteve French cifs_server_lock(server); 39538c8a9a5SSteve French scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname); 39638c8a9a5SSteve French cifs_server_unlock(server); 39738c8a9a5SSteve French 39838c8a9a5SSteve French rc = server->ops->tree_connect(xid, ses, tree, tcon, 39938c8a9a5SSteve French cifs_sb->local_nls); 40038c8a9a5SSteve French cifs_server_dbg(FYI, "%s: tree_reconnect %s: %d\n", __func__, tree, rc); 40138c8a9a5SSteve French spin_lock(&tcon->tc_lock); 40238c8a9a5SSteve French if (rc) { 40338c8a9a5SSteve French tcon->status = TID_NEED_TCON; 40438c8a9a5SSteve French } else { 40538c8a9a5SSteve French tcon->status = TID_GOOD; 40638c8a9a5SSteve French tcon->need_reconnect = false; 40738c8a9a5SSteve French } 40838c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 40938c8a9a5SSteve French } 41038c8a9a5SSteve French 41138c8a9a5SSteve French static void tree_connect_ipc(const unsigned int xid, char *tree, 41238c8a9a5SSteve French struct cifs_sb_info *cifs_sb, 41338c8a9a5SSteve French struct cifs_tcon *tcon) 41438c8a9a5SSteve French { 41538c8a9a5SSteve French struct cifs_ses *ses = tcon->ses; 41638c8a9a5SSteve French 41738c8a9a5SSteve French __tree_connect_ipc(xid, tree, cifs_sb, ses); 41838c8a9a5SSteve French __tree_connect_ipc(xid, tree, cifs_sb, CIFS_DFS_ROOT_SES(ses)); 41938c8a9a5SSteve French } 42038c8a9a5SSteve French 42138c8a9a5SSteve French static int __tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon, 42238c8a9a5SSteve French struct cifs_sb_info *cifs_sb, char *tree, bool islink, 42338c8a9a5SSteve French struct dfs_cache_tgt_list *tl) 42438c8a9a5SSteve French { 42538c8a9a5SSteve French int rc; 42638c8a9a5SSteve French struct TCP_Server_Info *server = tcon->ses->server; 42738c8a9a5SSteve French const struct smb_version_operations *ops = server->ops; 42838c8a9a5SSteve French struct cifs_ses *root_ses = CIFS_DFS_ROOT_SES(tcon->ses); 42938c8a9a5SSteve French char *share = NULL, *prefix = NULL; 43038c8a9a5SSteve French struct dfs_cache_tgt_iterator *tit; 43138c8a9a5SSteve French bool target_match; 43238c8a9a5SSteve French 43338c8a9a5SSteve French tit = dfs_cache_get_tgt_iterator(tl); 43438c8a9a5SSteve French if (!tit) { 43538c8a9a5SSteve French rc = -ENOENT; 43638c8a9a5SSteve French goto out; 43738c8a9a5SSteve French } 43838c8a9a5SSteve French 43938c8a9a5SSteve French /* Try to tree connect to all dfs targets */ 44038c8a9a5SSteve French for (; tit; tit = dfs_cache_get_next_tgt(tl, tit)) { 44138c8a9a5SSteve French const char *target = dfs_cache_get_tgt_name(tit); 4423fea12f3SPaulo Alcantara DFS_CACHE_TGT_LIST(ntl); 44338c8a9a5SSteve French 44438c8a9a5SSteve French kfree(share); 44538c8a9a5SSteve French kfree(prefix); 44638c8a9a5SSteve French share = prefix = NULL; 44738c8a9a5SSteve French 44838c8a9a5SSteve French /* Check if share matches with tcp ses */ 44938c8a9a5SSteve French rc = dfs_cache_get_tgt_share(server->leaf_fullpath + 1, tit, &share, &prefix); 45038c8a9a5SSteve French if (rc) { 45138c8a9a5SSteve French cifs_dbg(VFS, "%s: failed to parse target share: %d\n", __func__, rc); 45238c8a9a5SSteve French break; 45338c8a9a5SSteve French } 45438c8a9a5SSteve French 45538c8a9a5SSteve French rc = target_share_matches_server(server, share, &target_match); 45638c8a9a5SSteve French if (rc) 45738c8a9a5SSteve French break; 45838c8a9a5SSteve French if (!target_match) { 45938c8a9a5SSteve French rc = -EHOSTUNREACH; 46038c8a9a5SSteve French continue; 46138c8a9a5SSteve French } 46238c8a9a5SSteve French 46338c8a9a5SSteve French dfs_cache_noreq_update_tgthint(server->leaf_fullpath + 1, tit); 46438c8a9a5SSteve French tree_connect_ipc(xid, tree, cifs_sb, tcon); 46538c8a9a5SSteve French 46638c8a9a5SSteve French scnprintf(tree, MAX_TREE_SIZE, "\\%s", share); 46738c8a9a5SSteve French if (!islink) { 46838c8a9a5SSteve French rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls); 46938c8a9a5SSteve French break; 47038c8a9a5SSteve French } 47138c8a9a5SSteve French 47238c8a9a5SSteve French /* 47338c8a9a5SSteve French * If no dfs referrals were returned from link target, then just do a TREE_CONNECT 47438c8a9a5SSteve French * to it. Otherwise, cache the dfs referral and then mark current tcp ses for 47538c8a9a5SSteve French * reconnect so either the demultiplex thread or the echo worker will reconnect to 47638c8a9a5SSteve French * newly resolved target. 47738c8a9a5SSteve French */ 47838c8a9a5SSteve French if (dfs_cache_find(xid, root_ses, cifs_sb->local_nls, cifs_remap(cifs_sb), target, 47938c8a9a5SSteve French NULL, &ntl)) { 48038c8a9a5SSteve French rc = ops->tree_connect(xid, tcon->ses, tree, tcon, cifs_sb->local_nls); 48138c8a9a5SSteve French if (rc) 48238c8a9a5SSteve French continue; 48338c8a9a5SSteve French 48438c8a9a5SSteve French rc = cifs_update_super_prepath(cifs_sb, prefix); 48538c8a9a5SSteve French } else { 48638c8a9a5SSteve French /* Target is another dfs share */ 48738c8a9a5SSteve French rc = update_server_fullpath(server, cifs_sb, target); 48838c8a9a5SSteve French dfs_cache_free_tgts(tl); 48938c8a9a5SSteve French 49038c8a9a5SSteve French if (!rc) { 49138c8a9a5SSteve French rc = -EREMOTE; 49238c8a9a5SSteve French list_replace_init(&ntl.tl_list, &tl->tl_list); 49338c8a9a5SSteve French } else 49438c8a9a5SSteve French dfs_cache_free_tgts(&ntl); 49538c8a9a5SSteve French } 49638c8a9a5SSteve French break; 49738c8a9a5SSteve French } 49838c8a9a5SSteve French 49938c8a9a5SSteve French out: 50038c8a9a5SSteve French kfree(share); 50138c8a9a5SSteve French kfree(prefix); 50238c8a9a5SSteve French 50338c8a9a5SSteve French return rc; 50438c8a9a5SSteve French } 50538c8a9a5SSteve French 50638c8a9a5SSteve French static int tree_connect_dfs_target(const unsigned int xid, struct cifs_tcon *tcon, 50738c8a9a5SSteve French struct cifs_sb_info *cifs_sb, char *tree, bool islink, 50838c8a9a5SSteve French struct dfs_cache_tgt_list *tl) 50938c8a9a5SSteve French { 51038c8a9a5SSteve French int rc; 51138c8a9a5SSteve French int num_links = 0; 51238c8a9a5SSteve French struct TCP_Server_Info *server = tcon->ses->server; 51338c8a9a5SSteve French char *old_fullpath = server->leaf_fullpath; 51438c8a9a5SSteve French 51538c8a9a5SSteve French do { 51638c8a9a5SSteve French rc = __tree_connect_dfs_target(xid, tcon, cifs_sb, tree, islink, tl); 51738c8a9a5SSteve French if (!rc || rc != -EREMOTE) 51838c8a9a5SSteve French break; 51938c8a9a5SSteve French } while (rc = -ELOOP, ++num_links < MAX_NESTED_LINKS); 52038c8a9a5SSteve French /* 52138c8a9a5SSteve French * If we couldn't tree connect to any targets from last referral path, then 52238c8a9a5SSteve French * retry it from newly resolved dfs referral. 52338c8a9a5SSteve French */ 52438c8a9a5SSteve French if (rc && server->leaf_fullpath != old_fullpath) 52538c8a9a5SSteve French cifs_signal_cifsd_for_reconnect(server, true); 52638c8a9a5SSteve French 52738c8a9a5SSteve French dfs_cache_free_tgts(tl); 52838c8a9a5SSteve French return rc; 52938c8a9a5SSteve French } 53038c8a9a5SSteve French 53138c8a9a5SSteve French int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc) 53238c8a9a5SSteve French { 53338c8a9a5SSteve French int rc; 53438c8a9a5SSteve French struct TCP_Server_Info *server = tcon->ses->server; 53538c8a9a5SSteve French const struct smb_version_operations *ops = server->ops; 5363fea12f3SPaulo Alcantara DFS_CACHE_TGT_LIST(tl); 5373ae872deSPaulo Alcantara struct cifs_sb_info *cifs_sb = NULL; 5383ae872deSPaulo Alcantara struct super_block *sb = NULL; 53938c8a9a5SSteve French struct dfs_info3_param ref = {0}; 5403ae872deSPaulo Alcantara char *tree; 54138c8a9a5SSteve French 54238c8a9a5SSteve French /* only send once per connect */ 54338c8a9a5SSteve French spin_lock(&tcon->tc_lock); 54491f4480cSShyam Prasad N if (tcon->status == TID_GOOD) { 54591f4480cSShyam Prasad N spin_unlock(&tcon->tc_lock); 54691f4480cSShyam Prasad N return 0; 54791f4480cSShyam Prasad N } 54891f4480cSShyam Prasad N 54938c8a9a5SSteve French if (tcon->status != TID_NEW && 55038c8a9a5SSteve French tcon->status != TID_NEED_TCON) { 55138c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 55238c8a9a5SSteve French return -EHOSTDOWN; 55338c8a9a5SSteve French } 55438c8a9a5SSteve French 55538c8a9a5SSteve French tcon->status = TID_IN_TCON; 55638c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 55738c8a9a5SSteve French 55838c8a9a5SSteve French tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL); 55938c8a9a5SSteve French if (!tree) { 56038c8a9a5SSteve French rc = -ENOMEM; 56138c8a9a5SSteve French goto out; 56238c8a9a5SSteve French } 56338c8a9a5SSteve French 56438c8a9a5SSteve French if (tcon->ipc) { 56538c8a9a5SSteve French cifs_server_lock(server); 56638c8a9a5SSteve French scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname); 56738c8a9a5SSteve French cifs_server_unlock(server); 56838c8a9a5SSteve French rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc); 56938c8a9a5SSteve French goto out; 57038c8a9a5SSteve French } 57138c8a9a5SSteve French 5723ae872deSPaulo Alcantara sb = cifs_get_dfs_tcon_super(tcon); 5733ae872deSPaulo Alcantara if (!IS_ERR(sb)) 57438c8a9a5SSteve French cifs_sb = CIFS_SB(sb); 57538c8a9a5SSteve French 5763ae872deSPaulo Alcantara /* 5773ae872deSPaulo Alcantara * Tree connect to last share in @tcon->tree_name whether dfs super or 5783ae872deSPaulo Alcantara * cached dfs referral was not found. 5793ae872deSPaulo Alcantara */ 5803ae872deSPaulo Alcantara if (!cifs_sb || !server->leaf_fullpath || 58138c8a9a5SSteve French dfs_cache_noreq_find(server->leaf_fullpath + 1, &ref, &tl)) { 5823ae872deSPaulo Alcantara rc = ops->tree_connect(xid, tcon->ses, tcon->tree_name, tcon, 5833ae872deSPaulo Alcantara cifs_sb ? cifs_sb->local_nls : nlsc); 58438c8a9a5SSteve French goto out; 58538c8a9a5SSteve French } 58638c8a9a5SSteve French 58738c8a9a5SSteve French rc = tree_connect_dfs_target(xid, tcon, cifs_sb, tree, ref.server_type == DFS_TYPE_LINK, 58838c8a9a5SSteve French &tl); 58938c8a9a5SSteve French free_dfs_info_param(&ref); 59038c8a9a5SSteve French 59138c8a9a5SSteve French out: 59238c8a9a5SSteve French kfree(tree); 59338c8a9a5SSteve French cifs_put_tcp_super(sb); 59438c8a9a5SSteve French 59538c8a9a5SSteve French if (rc) { 59638c8a9a5SSteve French spin_lock(&tcon->tc_lock); 59738c8a9a5SSteve French if (tcon->status == TID_IN_TCON) 59838c8a9a5SSteve French tcon->status = TID_NEED_TCON; 59938c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 60038c8a9a5SSteve French } else { 60138c8a9a5SSteve French spin_lock(&tcon->tc_lock); 60238c8a9a5SSteve French if (tcon->status == TID_IN_TCON) 60338c8a9a5SSteve French tcon->status = TID_GOOD; 60438c8a9a5SSteve French spin_unlock(&tcon->tc_lock); 60538c8a9a5SSteve French tcon->need_reconnect = false; 60638c8a9a5SSteve French } 60738c8a9a5SSteve French 60838c8a9a5SSteve French return rc; 60938c8a9a5SSteve French } 610