blob: ad3ecda1314d9bb987a084342db2b7f1201519e2 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/connect.c
3 *
Steve French1080ef72011-02-24 18:07:19 +00004 * Copyright (C) International Business Machines Corp., 2002,2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
Steve Frenchfb8c4b12007-07-10 01:16:18 +000019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21#include <linux/fs.h>
22#include <linux/net.h>
23#include <linux/string.h>
Eric Biggersdc920272020-03-08 22:58:20 -070024#include <linux/sched/mm.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010025#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/list.h>
27#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/pagemap.h>
30#include <linux/ctype.h>
31#include <linux/utsname.h>
32#include <linux/mempool.h>
Steve Frenchb8643e12005-04-28 22:41:07 -070033#include <linux/delay.h>
Steve Frenchf1914012005-08-18 09:37:34 -070034#include <linux/completion.h>
Igor Mammedovaaf737a2007-04-03 19:16:43 +000035#include <linux/kthread.h>
Steve French0ae0efa2005-10-10 10:57:19 -070036#include <linux/pagevec.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080037#include <linux/freezer.h>
Igor Mammedov5c2503a2009-04-21 19:31:05 +040038#include <linux/namei.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020039#include <linux/uuid.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080040#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <asm/processor.h>
Jeff Layton50b64e32009-06-02 06:55:20 -040042#include <linux/inet.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040043#include <linux/module.h>
Jeff Layton8a8798a2012-01-17 16:09:15 -050044#include <keys/user-type.h>
Steve French0e2beda2009-01-30 21:24:41 +000045#include <net/ipv6.h>
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040046#include <linux/parser.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060047#include <linux/bvec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include "cifspdu.h"
49#include "cifsglob.h"
50#include "cifsproto.h"
51#include "cifs_unicode.h"
52#include "cifs_debug.h"
53#include "cifs_fs_sb.h"
54#include "ntlmssp.h"
55#include "nterr.h"
56#include "rfc1002pdu.h"
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +053057#include "fscache.h"
Pavel Shilovsky53e0e112016-11-04 11:50:31 -070058#include "smb2proto.h"
Long Li2f894642017-11-22 17:38:34 -070059#include "smbdirect.h"
Paulo Alcantara1c780222018-11-14 16:24:03 -020060#include "dns_resolve.h"
61#ifdef CONFIG_CIFS_DFS_UPCALL
62#include "dfs_cache.h"
63#endif
Ronnie Sahlberg5c6e5aa2020-10-21 10:37:11 +100064#include "fs_context.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066extern mempool_t *cifs_req_poolp;
Steve Frenchf92a7202018-05-24 04:11:07 -050067extern bool disable_legacy_dialects;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Jeff Layton2de970f2010-10-06 19:51:12 -040069/* FIXME: should these be tunable? */
Jeff Layton9d002df2010-10-06 19:51:11 -040070#define TLINK_ERROR_EXPIRE (1 * HZ)
Jeff Layton2de970f2010-10-06 19:51:12 -040071#define TLINK_IDLE_EXPIRE (600 * HZ)
Jeff Layton9d002df2010-10-06 19:51:11 -040072
Rohith Surabattula8e670f72020-09-18 05:37:28 +000073/* Drop the connection to not overload the server */
74#define NUM_STATUS_IO_TIMEOUT 5
75
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040076enum {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040077 /* Mount options that take no arguments */
78 Opt_user_xattr, Opt_nouser_xattr,
79 Opt_forceuid, Opt_noforceuid,
Jeff Layton72bd4812012-10-03 16:02:36 -040080 Opt_forcegid, Opt_noforcegid,
Steve French3e7a02d2019-09-11 21:46:20 -050081 Opt_noblocksend, Opt_noautotune, Opt_nolease,
Steve French82e93672020-05-19 03:06:57 -050082 Opt_hard, Opt_soft, Opt_perm, Opt_noperm, Opt_nodelete,
Steve French2baa2682014-09-27 02:19:01 -050083 Opt_mapposix, Opt_nomapposix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040084 Opt_mapchars, Opt_nomapchars, Opt_sfu,
85 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
Steve Frenchb3266142018-05-20 23:41:10 -050086 Opt_noposixpaths, Opt_nounix, Opt_unix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040087 Opt_nocase,
88 Opt_brl, Opt_nobrl,
Steve French3d4ef9a2018-04-25 22:19:09 -050089 Opt_handlecache, Opt_nohandlecache,
Steve French95932652016-09-23 01:36:34 -050090 Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040091 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
92 Opt_nohard, Opt_nosoft,
93 Opt_nointr, Opt_intr,
94 Opt_nostrictsync, Opt_strictsync,
95 Opt_serverino, Opt_noserverino,
96 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
97 Opt_acl, Opt_noacl, Opt_locallease,
Steve French4f5c10f2019-09-03 21:18:49 -050098 Opt_sign, Opt_ignore_signature, Opt_seal, Opt_noac,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040099 Opt_fsc, Opt_mfsymlinks,
Jeff Laytona0b3df52013-05-24 07:40:59 -0400100 Opt_multiuser, Opt_sloppy, Opt_nosharesock,
Steve Frenchb2a30772015-09-29 21:49:28 -0500101 Opt_persistent, Opt_nopersistent,
Steve French592fafe2015-11-03 10:08:53 -0600102 Opt_resilient, Opt_noresilient,
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -0300103 Opt_domainauto, Opt_rdma, Opt_modesid, Opt_rootfs,
Aurelien Aptelbcc88802019-09-20 04:32:20 +0200104 Opt_multichannel, Opt_nomultichannel,
Steve French9fe5ff12019-06-24 20:39:04 -0500105 Opt_compress,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400106
107 /* Mount options which take numeric value */
108 Opt_backupuid, Opt_backupgid, Opt_uid,
109 Opt_cruid, Opt_gid, Opt_file_mode,
110 Opt_dirmode, Opt_port,
Steve French563317e2019-09-08 23:22:02 -0500111 Opt_min_enc_offload,
Steve Frenche8506d22019-02-28 21:32:15 -0600112 Opt_blocksize, Opt_rsize, Opt_wsize, Opt_actimeo,
Steve Frenchca567eb2019-03-29 16:31:07 -0500113 Opt_echo_interval, Opt_max_credits, Opt_handletimeout,
Aurelien Aptelbcc88802019-09-20 04:32:20 +0200114 Opt_snapshot, Opt_max_channels,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400115
116 /* Mount options which take string value */
117 Opt_user, Opt_pass, Opt_ip,
Jeff Layton73a999f2013-03-22 08:42:57 -0400118 Opt_domain, Opt_srcaddr, Opt_iocharset,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400119 Opt_netbiosname, Opt_servern,
Jeff Layton23db65f2012-05-15 12:20:51 -0400120 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400121
122 /* Mount options to be ignored */
123 Opt_ignore,
124
125 /* Options which could be blank */
126 Opt_blank_pass,
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100127 Opt_blank_user,
128 Opt_blank_ip,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400129
130 Opt_err
131};
132
133static const match_table_t cifs_mount_option_tokens = {
134
135 { Opt_user_xattr, "user_xattr" },
136 { Opt_nouser_xattr, "nouser_xattr" },
137 { Opt_forceuid, "forceuid" },
138 { Opt_noforceuid, "noforceuid" },
Jeff Layton72bd4812012-10-03 16:02:36 -0400139 { Opt_forcegid, "forcegid" },
140 { Opt_noforcegid, "noforcegid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400141 { Opt_noblocksend, "noblocksend" },
142 { Opt_noautotune, "noautotune" },
Steve French3e7a02d2019-09-11 21:46:20 -0500143 { Opt_nolease, "nolease" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400144 { Opt_hard, "hard" },
145 { Opt_soft, "soft" },
146 { Opt_perm, "perm" },
147 { Opt_noperm, "noperm" },
Steve French82e93672020-05-19 03:06:57 -0500148 { Opt_nodelete, "nodelete" },
Steve French2baa2682014-09-27 02:19:01 -0500149 { Opt_mapchars, "mapchars" }, /* SFU style */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400150 { Opt_nomapchars, "nomapchars" },
Steve French2baa2682014-09-27 02:19:01 -0500151 { Opt_mapposix, "mapposix" }, /* SFM style */
152 { Opt_nomapposix, "nomapposix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400153 { Opt_sfu, "sfu" },
154 { Opt_nosfu, "nosfu" },
155 { Opt_nodfs, "nodfs" },
156 { Opt_posixpaths, "posixpaths" },
157 { Opt_noposixpaths, "noposixpaths" },
158 { Opt_nounix, "nounix" },
159 { Opt_nounix, "nolinux" },
Steve Frenchb3266142018-05-20 23:41:10 -0500160 { Opt_nounix, "noposix" },
161 { Opt_unix, "unix" },
162 { Opt_unix, "linux" },
163 { Opt_unix, "posix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400164 { Opt_nocase, "nocase" },
165 { Opt_nocase, "ignorecase" },
166 { Opt_brl, "brl" },
167 { Opt_nobrl, "nobrl" },
Steve French3d4ef9a2018-04-25 22:19:09 -0500168 { Opt_handlecache, "handlecache" },
169 { Opt_nohandlecache, "nohandlecache" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400170 { Opt_nobrl, "nolock" },
171 { Opt_forcemandatorylock, "forcemandatorylock" },
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +0400172 { Opt_forcemandatorylock, "forcemand" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400173 { Opt_setuids, "setuids" },
174 { Opt_nosetuids, "nosetuids" },
Steve French95932652016-09-23 01:36:34 -0500175 { Opt_setuidfromacl, "idsfromsid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400176 { Opt_dynperm, "dynperm" },
177 { Opt_nodynperm, "nodynperm" },
178 { Opt_nohard, "nohard" },
179 { Opt_nosoft, "nosoft" },
180 { Opt_nointr, "nointr" },
181 { Opt_intr, "intr" },
182 { Opt_nostrictsync, "nostrictsync" },
183 { Opt_strictsync, "strictsync" },
184 { Opt_serverino, "serverino" },
185 { Opt_noserverino, "noserverino" },
186 { Opt_rwpidforward, "rwpidforward" },
Steve French412094a2019-06-24 02:01:42 -0500187 { Opt_modesid, "modefromsid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400188 { Opt_cifsacl, "cifsacl" },
189 { Opt_nocifsacl, "nocifsacl" },
190 { Opt_acl, "acl" },
191 { Opt_noacl, "noacl" },
192 { Opt_locallease, "locallease" },
193 { Opt_sign, "sign" },
Steve French4f5c10f2019-09-03 21:18:49 -0500194 { Opt_ignore_signature, "signloosely" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400195 { Opt_seal, "seal" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400196 { Opt_noac, "noac" },
197 { Opt_fsc, "fsc" },
198 { Opt_mfsymlinks, "mfsymlinks" },
199 { Opt_multiuser, "multiuser" },
Jeff Laytond8162552012-03-23 14:40:56 -0400200 { Opt_sloppy, "sloppy" },
Jeff Laytona0b3df52013-05-24 07:40:59 -0400201 { Opt_nosharesock, "nosharesock" },
Steve Frenchb2a30772015-09-29 21:49:28 -0500202 { Opt_persistent, "persistenthandles"},
203 { Opt_nopersistent, "nopersistenthandles"},
Steve French592fafe2015-11-03 10:08:53 -0600204 { Opt_resilient, "resilienthandles"},
205 { Opt_noresilient, "noresilienthandles"},
Germano Percossi39566442016-12-15 12:31:18 +0530206 { Opt_domainauto, "domainauto"},
Long Li8339dd32017-11-07 01:54:55 -0700207 { Opt_rdma, "rdma"},
Aurelien Aptelbcc88802019-09-20 04:32:20 +0200208 { Opt_multichannel, "multichannel" },
209 { Opt_nomultichannel, "nomultichannel" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400210
211 { Opt_backupuid, "backupuid=%s" },
212 { Opt_backupgid, "backupgid=%s" },
213 { Opt_uid, "uid=%s" },
214 { Opt_cruid, "cruid=%s" },
215 { Opt_gid, "gid=%s" },
216 { Opt_file_mode, "file_mode=%s" },
217 { Opt_dirmode, "dirmode=%s" },
218 { Opt_dirmode, "dir_mode=%s" },
219 { Opt_port, "port=%s" },
Steve French563317e2019-09-08 23:22:02 -0500220 { Opt_min_enc_offload, "esize=%s" },
Steve Frenche8506d22019-02-28 21:32:15 -0600221 { Opt_blocksize, "bsize=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400222 { Opt_rsize, "rsize=%s" },
223 { Opt_wsize, "wsize=%s" },
224 { Opt_actimeo, "actimeo=%s" },
Steve Frenchca567eb2019-03-29 16:31:07 -0500225 { Opt_handletimeout, "handletimeout=%s" },
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600226 { Opt_echo_interval, "echo_interval=%s" },
Steve French141891f2016-09-23 00:44:16 -0500227 { Opt_max_credits, "max_credits=%s" },
Steve French8b217fe2016-11-11 22:36:20 -0600228 { Opt_snapshot, "snapshot=%s" },
Aurelien Aptelbcc88802019-09-20 04:32:20 +0200229 { Opt_max_channels, "max_channels=%s" },
Steve French9fe5ff12019-06-24 20:39:04 -0500230 { Opt_compress, "compress=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400231
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100232 { Opt_blank_user, "user=" },
233 { Opt_blank_user, "username=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400234 { Opt_user, "user=%s" },
235 { Opt_user, "username=%s" },
236 { Opt_blank_pass, "pass=" },
Jesper Nilsson3c15b4c2012-11-29 17:31:16 +0100237 { Opt_blank_pass, "password=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400238 { Opt_pass, "pass=%s" },
239 { Opt_pass, "password=%s" },
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100240 { Opt_blank_ip, "ip=" },
241 { Opt_blank_ip, "addr=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400242 { Opt_ip, "ip=%s" },
243 { Opt_ip, "addr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400244 { Opt_ignore, "unc=%s" },
245 { Opt_ignore, "target=%s" },
246 { Opt_ignore, "path=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400247 { Opt_domain, "dom=%s" },
248 { Opt_domain, "domain=%s" },
249 { Opt_domain, "workgroup=%s" },
250 { Opt_srcaddr, "srcaddr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400251 { Opt_ignore, "prefixpath=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400252 { Opt_iocharset, "iocharset=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400253 { Opt_netbiosname, "netbiosname=%s" },
254 { Opt_servern, "servern=%s" },
255 { Opt_ver, "ver=%s" },
Jeff Layton23db65f2012-05-15 12:20:51 -0400256 { Opt_vers, "vers=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400257 { Opt_sec, "sec=%s" },
Jeff Layton15b6a472012-05-16 07:50:15 -0400258 { Opt_cache, "cache=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400259
260 { Opt_ignore, "cred" },
261 { Opt_ignore, "credentials" },
Jeff Laytona557b972012-05-02 14:02:40 -0400262 { Opt_ignore, "cred=%s" },
263 { Opt_ignore, "credentials=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400264 { Opt_ignore, "guest" },
265 { Opt_ignore, "rw" },
266 { Opt_ignore, "ro" },
267 { Opt_ignore, "suid" },
268 { Opt_ignore, "nosuid" },
269 { Opt_ignore, "exec" },
270 { Opt_ignore, "noexec" },
271 { Opt_ignore, "nodev" },
272 { Opt_ignore, "noauto" },
273 { Opt_ignore, "dev" },
274 { Opt_ignore, "mand" },
275 { Opt_ignore, "nomand" },
Steve French9b9c5be2018-09-22 12:07:06 -0500276 { Opt_ignore, "relatime" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400277 { Opt_ignore, "_netdev" },
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -0300278 { Opt_rootfs, "rootfs" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400279
280 { Opt_err, NULL }
281};
282
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300283static int ip_connect(struct TCP_Server_Info *server);
284static int generic_ip_connect(struct TCP_Server_Info *server);
Jeff Laytonb647c352010-10-28 11:16:44 -0400285static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
Jeff Layton2de970f2010-10-06 19:51:12 -0400286static void cifs_prune_tlinks(struct work_struct *work);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200287static char *extract_hostname(const char *unc);
288
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200289/*
290 * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
291 * get their ip addresses changed at some point.
292 *
293 * This should be called with server->srv_mutex held.
294 */
295#ifdef CONFIG_CIFS_DFS_UPCALL
296static int reconn_set_ipaddr(struct TCP_Server_Info *server)
297{
298 int rc;
299 int len;
300 char *unc, *ipaddr = NULL;
301
302 if (!server->hostname)
303 return -EINVAL;
304
305 len = strlen(server->hostname) + 3;
306
307 unc = kmalloc(len, GFP_KERNEL);
308 if (!unc) {
309 cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
310 return -ENOMEM;
311 }
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000312 scnprintf(unc, len, "\\\\%s", server->hostname);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200313
314 rc = dns_resolve_server_name_to_ip(unc, &ipaddr);
315 kfree(unc);
316
317 if (rc < 0) {
318 cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
319 __func__, server->hostname, rc);
320 return rc;
321 }
322
Ronnie Sahlbergfada37f2020-04-21 12:37:39 +1000323 spin_lock(&cifs_tcp_ses_lock);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200324 rc = cifs_convert_address((struct sockaddr *)&server->dstaddr, ipaddr,
325 strlen(ipaddr));
Ronnie Sahlbergfada37f2020-04-21 12:37:39 +1000326 spin_unlock(&cifs_tcp_ses_lock);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200327 kfree(ipaddr);
328
329 return !rc ? -1 : 0;
330}
331#else
332static inline int reconn_set_ipaddr(struct TCP_Server_Info *server)
333{
334 return 0;
335}
336#endif
337
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200338#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200339/* These functions must be called with server->srv_mutex held */
Paulo Alcantara7d397a02020-07-21 09:36:40 -0300340static void reconn_set_next_dfs_target(struct TCP_Server_Info *server,
341 struct cifs_sb_info *cifs_sb,
342 struct dfs_cache_tgt_list *tgt_list,
343 struct dfs_cache_tgt_iterator **tgt_it)
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200344{
345 const char *name;
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200346
Paulo Alcantara7d397a02020-07-21 09:36:40 -0300347 if (!cifs_sb || !cifs_sb->origin_fullpath)
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200348 return;
349
350 if (!*tgt_it) {
351 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
352 } else {
353 *tgt_it = dfs_cache_get_next_tgt(tgt_list, *tgt_it);
354 if (!*tgt_it)
355 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
356 }
357
358 cifs_dbg(FYI, "%s: UNC: %s\n", __func__, cifs_sb->origin_fullpath);
359
360 name = dfs_cache_get_tgt_name(*tgt_it);
361
362 kfree(server->hostname);
363
364 server->hostname = extract_hostname(name);
Dan Carpenter84288172019-01-05 15:25:29 +0300365 if (IS_ERR(server->hostname)) {
366 cifs_dbg(FYI,
367 "%s: failed to extract hostname from target: %ld\n",
368 __func__, PTR_ERR(server->hostname));
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200369 }
370}
371
372static inline int reconn_setup_dfs_targets(struct cifs_sb_info *cifs_sb,
Paulo Alcantarabaf3f082020-05-19 15:38:29 -0300373 struct dfs_cache_tgt_list *tl)
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200374{
375 if (!cifs_sb->origin_fullpath)
376 return -EOPNOTSUPP;
377 return dfs_cache_noreq_find(cifs_sb->origin_fullpath + 1, NULL, tl);
378}
379#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Jeff Laytond5c56052008-12-01 18:42:33 -0500381/*
382 * cifs tcp session reconnection
383 *
384 * mark tcp session as reconnecting so temporarily locked
385 * mark all smb sessions as reconnecting for tcp session
386 * reconnect tcp session
387 * wake up waiters on reconnection? - (not needed currently)
388 */
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400389int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390cifs_reconnect(struct TCP_Server_Info *server)
391{
392 int rc = 0;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500393 struct list_head *tmp, *tmp2;
Steve French96daf2b2011-05-27 04:34:02 +0000394 struct cifs_ses *ses;
395 struct cifs_tcon *tcon;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000396 struct mid_q_entry *mid_entry;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400397 struct list_head retry_list;
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200398#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300399 struct super_block *sb = NULL;
Paulo Alcantara23324402018-11-20 14:37:18 -0200400 struct cifs_sb_info *cifs_sb = NULL;
401 struct dfs_cache_tgt_list tgt_list = {0};
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200402 struct dfs_cache_tgt_iterator *tgt_it = NULL;
403#endif
Steve French50c2f752007-07-13 00:33:32 +0000404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 spin_lock(&GlobalMid_Lock);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200406 server->nr_targets = 1;
407#ifdef CONFIG_CIFS_DFS_UPCALL
Ronnie Sahlberg61cabc72019-06-14 13:02:29 +1000408 spin_unlock(&GlobalMid_Lock);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300409 sb = cifs_get_tcp_super(server);
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300410 if (IS_ERR(sb)) {
411 rc = PTR_ERR(sb);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200412 cifs_dbg(FYI, "%s: will not do DFS failover: rc = %d\n",
413 __func__, rc);
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300414 sb = NULL;
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200415 } else {
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300416 cifs_sb = CIFS_SB(sb);
Paulo Alcantarabaf3f082020-05-19 15:38:29 -0300417 rc = reconn_setup_dfs_targets(cifs_sb, &tgt_list);
Paulo Alcantaraa5293032020-07-21 09:36:41 -0300418 if (rc) {
419 cifs_sb = NULL;
420 if (rc != -EOPNOTSUPP) {
421 cifs_server_dbg(VFS, "%s: no target servers for DFS failover\n",
422 __func__);
423 }
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200424 } else {
425 server->nr_targets = dfs_cache_get_nr_tgts(&tgt_list);
426 }
427 }
428 cifs_dbg(FYI, "%s: will retry %d target(s)\n", __func__,
429 server->nr_targets);
Ronnie Sahlberg61cabc72019-06-14 13:02:29 +1000430 spin_lock(&GlobalMid_Lock);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200431#endif
Jeff Layton469ee612008-10-16 18:46:39 +0000432 if (server->tcpStatus == CifsExiting) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000433 /* the demux thread will exit normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 next time through the loop */
435 spin_unlock(&GlobalMid_Lock);
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300436#ifdef CONFIG_CIFS_DFS_UPCALL
437 dfs_cache_free_tgts(&tgt_list);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300438 cifs_put_tcp_super(sb);
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300439#endif
Stefan Metzmachere2e87512020-02-24 14:31:02 -0600440 wake_up(&server->response_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return rc;
442 } else
443 server->tcpStatus = CifsNeedReconnect;
444 spin_unlock(&GlobalMid_Lock);
445 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400446 server->max_read = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Steve French6e4d3bb2018-09-22 11:25:04 -0500448 cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
Steve Frenchbf1fdeb2018-07-30 19:23:09 -0500449 trace_smb3_reconnect(server->CurrentMid, server->hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 /* before reconnecting the tcp session, mark the smb session (uid)
452 and the tid bad so they are not used until reconnected */
Joe Perchesf96637b2013-05-04 22:12:25 -0500453 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
454 __func__);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530455 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -0500456 list_for_each(tmp, &server->smb_ses_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000457 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
Jeff Layton14fbf502008-11-14 13:53:46 -0500458 ses->need_reconnect = true;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500459 list_for_each(tmp2, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000460 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500461 tcon->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
Aurelien Aptelb327a712018-01-24 13:46:10 +0100463 if (ses->tcon_ipc)
464 ses->tcon_ipc->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530466 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 /* do not want to be sending data on a socket we are freeing */
Joe Perchesf96637b2013-05-04 22:12:25 -0500469 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500470 mutex_lock(&server->srv_mutex);
Long Li1d2a4f52019-05-13 21:01:28 -0700471 if (server->ssocket) {
472 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
473 server->ssocket->state, server->ssocket->flags);
474 kernel_sock_shutdown(server->ssocket, SHUT_WR);
475 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
476 server->ssocket->state, server->ssocket->flags);
477 sock_release(server->ssocket);
478 server->ssocket = NULL;
479 }
480 server->sequence_number = 0;
481 server->session_estab = false;
482 kfree(server->session_key.response);
483 server->session_key.response = NULL;
484 server->session_key.len = 0;
485 server->lstrp = jiffies;
Long Li214bab42019-04-05 21:36:35 +0000486
487 /* mark submitted MIDs for retry and issue callback */
488 INIT_LIST_HEAD(&retry_list);
489 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
490 spin_lock(&GlobalMid_Lock);
491 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
492 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700493 kref_get(&mid_entry->refcount);
Long Li214bab42019-04-05 21:36:35 +0000494 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
495 mid_entry->mid_state = MID_RETRY_NEEDED;
496 list_move(&mid_entry->qhead, &retry_list);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700497 mid_entry->mid_flags |= MID_DELETED;
Long Li214bab42019-04-05 21:36:35 +0000498 }
499 spin_unlock(&GlobalMid_Lock);
Long Li1d2a4f52019-05-13 21:01:28 -0700500 mutex_unlock(&server->srv_mutex);
Long Li214bab42019-04-05 21:36:35 +0000501
502 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
503 list_for_each_safe(tmp, tmp2, &retry_list) {
504 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
505 list_del_init(&mid_entry->qhead);
506 mid_entry->callback(mid_entry);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700507 cifs_mid_q_entry_release(mid_entry);
Long Li214bab42019-04-05 21:36:35 +0000508 }
509
Long Li1d2a4f52019-05-13 21:01:28 -0700510 if (cifs_rdma_enabled(server)) {
511 mutex_lock(&server->srv_mutex);
Long Li050b8c32019-04-04 11:35:42 -0500512 smbd_destroy(server);
Long Li1d2a4f52019-05-13 21:01:28 -0700513 mutex_unlock(&server->srv_mutex);
514 }
Jeff Layton3c1105d2011-05-22 07:09:13 -0400515
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400516 do {
Steve French6c3d8902006-07-31 22:46:20 +0000517 try_to_freeze();
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300518
Jeff Layton73e216a2013-09-05 08:38:10 -0400519 mutex_lock(&server->srv_mutex);
Paulo Alcantaraaaa3aef2020-05-19 15:38:27 -0300520#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200521 /*
522 * Set up next DFS target server (if any) for reconnect. If DFS
523 * feature is disabled, then we will retry last server we
524 * connected to before.
525 */
Paulo Alcantara7d397a02020-07-21 09:36:40 -0300526 reconn_set_next_dfs_target(server, cifs_sb, &tgt_list, &tgt_it);
Paulo Alcantaraaaa3aef2020-05-19 15:38:27 -0300527#endif
528 rc = reconn_set_ipaddr(server);
529 if (rc) {
530 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
531 __func__, rc);
532 }
533
Long Li781a8052017-11-22 17:38:36 -0700534 if (cifs_rdma_enabled(server))
535 rc = smbd_reconnect(server);
536 else
537 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000538 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500539 cifs_dbg(FYI, "reconnect error %d\n", rc);
Federico Sauter4afe2602015-03-17 17:45:28 +0100540 mutex_unlock(&server->srv_mutex);
Steve French0cb766a2005-04-28 22:41:11 -0700541 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 } else {
543 atomic_inc(&tcpSesReconnectCount);
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800544 set_credits(server, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000546 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000547 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000548 spin_unlock(&GlobalMid_Lock);
Federico Sauter4afe2602015-03-17 17:45:28 +0100549 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400551 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500552
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200553#ifdef CONFIG_CIFS_DFS_UPCALL
554 if (tgt_it) {
555 rc = dfs_cache_noreq_update_tgthint(cifs_sb->origin_fullpath + 1,
556 tgt_it);
557 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000558 cifs_server_dbg(VFS, "%s: failed to update DFS target hint: rc = %d\n",
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200559 __func__, rc);
560 }
561 rc = dfs_cache_update_vol(cifs_sb->origin_fullpath, server);
562 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000563 cifs_server_dbg(VFS, "%s: failed to update vol info in DFS cache: rc = %d\n",
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200564 __func__, rc);
565 }
Paulo Alcantara23324402018-11-20 14:37:18 -0200566 dfs_cache_free_tgts(&tgt_list);
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300567
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200568 }
Paulo Alcantara (SUSE)8354d882019-11-22 12:30:51 -0300569
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -0300570 cifs_put_tcp_super(sb);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200571#endif
Sachin Prabhub8c60012016-10-20 19:52:24 -0400572 if (server->tcpStatus == CifsNeedNegotiate)
573 mod_delayed_work(cifsiod_wq, &server->echo, 0);
574
Stefan Metzmachere2e87512020-02-24 14:31:02 -0600575 wake_up(&server->response_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return rc;
577}
578
Jeff Laytonc74093b2011-01-11 07:24:23 -0500579static void
580cifs_echo_request(struct work_struct *work)
581{
582 int rc;
583 struct TCP_Server_Info *server = container_of(work,
584 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400585 unsigned long echo_interval;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500586
Jeff Layton247ec9b2011-02-04 17:09:50 -0500587 /*
Sachin Prabhub8c60012016-10-20 19:52:24 -0400588 * If we need to renegotiate, set echo interval to zero to
589 * immediately call echo service where we can renegotiate.
590 */
591 if (server->tcpStatus == CifsNeedNegotiate)
592 echo_interval = 0;
593 else
594 echo_interval = server->echo_interval;
595
596 /*
597 * We cannot send an echo if it is disabled.
598 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500599 */
Steve French4fcd1812016-06-22 20:12:05 -0500600
601 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400602 server->tcpStatus == CifsExiting ||
603 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400604 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600605 time_before(jiffies, server->lstrp + echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500606 goto requeue_echo;
607
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400608 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500609 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500610 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
611 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500612
613requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400614 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500615}
616
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400617static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400618allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400619{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400620 if (!server->bigbuf) {
621 server->bigbuf = (char *)cifs_buf_get();
622 if (!server->bigbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000623 cifs_server_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400624 msleep(3000);
625 /* retry will check if exiting */
626 return false;
627 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400628 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400629 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400630 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400631 }
632
Jeff Layton2a37ef92011-10-19 15:29:23 -0400633 if (!server->smallbuf) {
634 server->smallbuf = (char *)cifs_small_buf_get();
635 if (!server->smallbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000636 cifs_server_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400637 msleep(1000);
638 /* retry will check if exiting */
639 return false;
640 }
641 /* beginning of smb buffer is cleared in our buf_get */
642 } else {
643 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400644 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400645 }
646
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400647 return true;
648}
649
Jeff Laytonba749e62011-10-11 06:41:32 -0400650static bool
651server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400652{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300653 /*
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000654 * We need to wait 3 echo intervals to make sure we handle such
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300655 * situations right:
656 * 1s client sends a normal SMB request
Ronnie Sahlbergbecc2ba2019-07-24 11:43:49 +1000657 * 2s client gets a response
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300658 * 30s echo workqueue job pops, and decides we got a response recently
659 * and don't need to send another
660 * ...
661 * 65s kernel_recvmsg times out, and we see that we haven't gotten
662 * a response in >60s.
663 */
Samuel Cabrero76e75272017-07-11 12:44:39 +0200664 if ((server->tcpStatus == CifsGood ||
665 server->tcpStatus == CifsNeedNegotiate) &&
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000666 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000667 cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
668 (3 * server->echo_interval) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400669 cifs_reconnect(server);
Jeff Laytonba749e62011-10-11 06:41:32 -0400670 return true;
671 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400672
Jeff Laytonba749e62011-10-11 06:41:32 -0400673 return false;
674}
675
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800676static inline bool
677zero_credits(struct TCP_Server_Info *server)
678{
679 int val;
680
681 spin_lock(&server->req_lock);
682 val = server->credits + server->echo_credits + server->oplock_credits;
683 if (server->in_flight == 0 && val == 0) {
684 spin_unlock(&server->req_lock);
685 return true;
686 }
687 spin_unlock(&server->req_lock);
688 return false;
689}
690
Al Viro71335662016-01-09 19:54:50 -0500691static int
692cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400693{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400694 int length = 0;
695 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400696
Al Viro71335662016-01-09 19:54:50 -0500697 smb_msg->msg_control = NULL;
698 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400699
Al Viro71335662016-01-09 19:54:50 -0500700 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500701 try_to_freeze();
702
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800703 /* reconnect if no credits and no requests in flight */
704 if (zero_credits(server)) {
705 cifs_reconnect(server);
706 return -ECONNABORTED;
707 }
708
Al Viro71335662016-01-09 19:54:50 -0500709 if (server_unresponsive(server))
710 return -ECONNABORTED;
Long Li2fef1372017-11-22 17:38:41 -0700711 if (cifs_rdma_enabled(server) && server->smbd_conn)
712 length = smbd_recv(server->smbd_conn, smb_msg);
713 else
714 length = sock_recvmsg(server->ssocket, smb_msg, 0);
Al Viro71335662016-01-09 19:54:50 -0500715
716 if (server->tcpStatus == CifsExiting)
717 return -ESHUTDOWN;
718
719 if (server->tcpStatus == CifsNeedReconnect) {
720 cifs_reconnect(server);
721 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400722 }
723
Al Viro71335662016-01-09 19:54:50 -0500724 if (length == -ERESTARTSYS ||
725 length == -EAGAIN ||
726 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400727 /*
728 * Minimum sleep to prevent looping, allowing socket
729 * to clear and app threads to set tcpStatus
730 * CifsNeedReconnect if server hung.
731 */
732 usleep_range(1000, 2000);
733 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400734 continue;
Al Viro71335662016-01-09 19:54:50 -0500735 }
736
737 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500738 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400739 cifs_reconnect(server);
Al Viro71335662016-01-09 19:54:50 -0500740 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400741 }
742 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400743 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400744}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400745
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400746int
747cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
748 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400749{
Al Viro71335662016-01-09 19:54:50 -0500750 struct msghdr smb_msg;
751 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
David Howellsaa563d72018-10-20 00:57:56 +0100752 iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400753
Al Viro71335662016-01-09 19:54:50 -0500754 return cifs_readv_from_socket(server, &smb_msg);
755}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400756
Al Viro71335662016-01-09 19:54:50 -0500757int
758cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
Long Li1dbe3462018-05-30 12:47:55 -0700759 unsigned int page_offset, unsigned int to_read)
Al Viro71335662016-01-09 19:54:50 -0500760{
761 struct msghdr smb_msg;
Long Li1dbe3462018-05-30 12:47:55 -0700762 struct bio_vec bv = {
763 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
David Howellsaa563d72018-10-20 00:57:56 +0100764 iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read);
Al Viro71335662016-01-09 19:54:50 -0500765 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400766}
767
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400768static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400769is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400770{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400771 /*
772 * The first byte big endian of the length field,
773 * is actually not part of the length but the type
774 * with the most common, zero, as regular data.
775 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400776 switch (type) {
777 case RFC1002_SESSION_MESSAGE:
778 /* Regular SMB response */
779 return true;
780 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500781 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400782 break;
783 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500784 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400785 break;
786 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400787 /*
788 * We get this from Windows 98 instead of an error on
789 * SMB negprot response.
790 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500791 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400792 /* give server a second to clean up */
793 msleep(1000);
794 /*
795 * Always try 445 first on reconnect since we get NACK
796 * on some if we ever connected to port 139 (the NACK
797 * is since we do not begin with RFC1001 session
798 * initialize frame).
799 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400800 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400801 cifs_reconnect(server);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400802 break;
803 default:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000804 cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400805 cifs_reconnect(server);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400806 }
807
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400808 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400809}
810
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400811void
812dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400813{
814#ifdef CONFIG_CIFS_STATS2
815 mid->when_received = jiffies;
816#endif
817 spin_lock(&GlobalMid_Lock);
818 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400819 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400820 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400821 mid->mid_state = MID_RESPONSE_MALFORMED;
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000822 /*
823 * Trying to handle/dequeue a mid after the send_recv()
824 * function has finished processing it is a bug.
825 */
826 if (mid->mid_flags & MID_DELETED)
Joe Perchesa0a30362020-04-14 22:42:53 -0700827 pr_warn_once("trying to dequeue a deleted mid\n");
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700828 else {
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000829 list_del_init(&mid->qhead);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700830 mid->mid_flags |= MID_DELETED;
831 }
Jeff Laytonea1f4502011-10-19 15:29:05 -0400832 spin_unlock(&GlobalMid_Lock);
833}
834
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800835static unsigned int
836smb2_get_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
837{
838 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buffer;
839
840 /*
841 * SMB1 does not use credits.
842 */
843 if (server->vals->header_preamble_size)
844 return 0;
845
846 return le16_to_cpu(shdr->CreditRequest);
847}
848
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400849static void
850handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400851 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400852{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400853 if (server->ops->check_trans2 &&
854 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400855 return;
Pavel Shilovsky86a79642019-11-21 11:35:13 -0800856 mid->credits_received = smb2_get_credits_from_hdr(buf, server);
Jeff Laytonea1f4502011-10-19 15:29:05 -0400857 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400858 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400859 /* Was previous buf put in mpx struct for multi-rsp? */
860 if (!mid->multiRsp) {
861 /* smb buffer will be freed by user thread */
862 if (server->large_buf)
863 server->bigbuf = NULL;
864 else
865 server->smallbuf = NULL;
866 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400867 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400868}
869
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400870static void clean_demultiplex_info(struct TCP_Server_Info *server)
871{
872 int length;
873
874 /* take it off the list, if it's not already */
875 spin_lock(&cifs_tcp_ses_lock);
876 list_del_init(&server->tcp_ses_list);
877 spin_unlock(&cifs_tcp_ses_lock);
878
Paulo Alcantara21225332020-11-28 16:54:02 -0300879 cancel_delayed_work_sync(&server->echo);
880
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400881 spin_lock(&GlobalMid_Lock);
882 server->tcpStatus = CifsExiting;
883 spin_unlock(&GlobalMid_Lock);
884 wake_up_all(&server->response_q);
885
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400886 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300887 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400888 if (server->credits <= 0)
889 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300890 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400891 /*
892 * Although there should not be any requests blocked on this queue it
893 * can not hurt to be paranoid and try to wake up requests that may
894 * haven been blocked when more than 50 at time were on the wire to the
895 * same server - they now will see the session is in exit state and get
896 * out of SendReceive.
897 */
898 wake_up_all(&server->request_q);
899 /* give those requests time to exit */
900 msleep(125);
Long Li050b8c32019-04-04 11:35:42 -0500901 if (cifs_rdma_enabled(server))
902 smbd_destroy(server);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400903 if (server->ssocket) {
904 sock_release(server->ssocket);
905 server->ssocket = NULL;
906 }
907
908 if (!list_empty(&server->pending_mid_q)) {
909 struct list_head dispose_list;
910 struct mid_q_entry *mid_entry;
911 struct list_head *tmp, *tmp2;
912
913 INIT_LIST_HEAD(&dispose_list);
914 spin_lock(&GlobalMid_Lock);
915 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
916 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500917 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700918 kref_get(&mid_entry->refcount);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400919 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400920 list_move(&mid_entry->qhead, &dispose_list);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700921 mid_entry->mid_flags |= MID_DELETED;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400922 }
923 spin_unlock(&GlobalMid_Lock);
924
925 /* now walk dispose list and issue callbacks */
926 list_for_each_safe(tmp, tmp2, &dispose_list) {
927 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500928 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400929 list_del_init(&mid_entry->qhead);
930 mid_entry->callback(mid_entry);
Pavel Shilovskyabe57072019-10-22 08:41:42 -0700931 cifs_mid_q_entry_release(mid_entry);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400932 }
933 /* 1/8th of sec is more than enough time for them to exit */
934 msleep(125);
935 }
936
937 if (!list_empty(&server->pending_mid_q)) {
938 /*
939 * mpx threads have not exited yet give them at least the smb
940 * send timeout time for long ops.
941 *
942 * Due to delays on oplock break requests, we need to wait at
943 * least 45 seconds before giving up on a request getting a
944 * response and going ahead and killing cifsd.
945 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500946 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400947 msleep(46000);
948 /*
949 * If threads still have not exited they are probably never
950 * coming home not much else we can do but free the memory.
951 */
952 }
953
954 kfree(server->hostname);
955 kfree(server);
956
957 length = atomic_dec_return(&tcpSesAllocCount);
958 if (length > 0)
David Rientjes11d83362015-04-14 15:48:21 -0700959 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400960}
961
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400962static int
Jeff Laytone9097ab2011-10-19 15:29:40 -0400963standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
964{
965 int length;
966 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +1000967 unsigned int pdu_length = server->pdu_size;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400968
969 /* make sure this will fit in a large buffer */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100970 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
971 server->vals->header_preamble_size) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000972 cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400973 cifs_reconnect(server);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +0400974 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400975 }
976
977 /* switch to large buffer if too big for a small one */
978 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
979 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400980 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400981 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400982 }
983
984 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400985 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100986 pdu_length - HEADER_SIZE(server) + 1
987 + server->vals->header_preamble_size);
988
Jeff Laytone9097ab2011-10-19 15:29:40 -0400989 if (length < 0)
990 return length;
991 server->total_read += length;
992
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400993 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400994
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800995 return cifs_handle_standard(server, mid);
996}
997
998int
999cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1000{
1001 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
1002 int length;
1003
Jeff Laytone9097ab2011-10-19 15:29:40 -04001004 /*
1005 * We know that we received enough to get to the MID as we
1006 * checked the pdu_length earlier. Now check to see
1007 * if the rest of the header is OK. We borrow the length
1008 * var for the rest of the loop to avoid a new stack var.
1009 *
1010 * 48 bytes is enough to display the header and a little bit
1011 * into the payload for debugging purposes.
1012 */
Steve French373512e2015-12-18 13:05:30 -06001013 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -04001014 if (length != 0)
1015 cifs_dump_mem("Bad SMB: ", buf,
1016 min_t(unsigned int, server->total_read, 48));
1017
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001018 if (server->ops->is_session_expired &&
1019 server->ops->is_session_expired(buf)) {
1020 cifs_reconnect(server);
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001021 return -1;
1022 }
1023
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001024 if (server->ops->is_status_pending &&
Pavel Shilovsky66265f12019-01-23 17:11:16 -08001025 server->ops->is_status_pending(buf, server))
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001026 return -1;
1027
Jeff Laytonff4fa4a2012-02-07 06:31:05 -05001028 if (!mid)
1029 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -04001030
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001031 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -05001032 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -04001033}
1034
Ronnie Sahlbergeca00452019-02-05 12:56:44 +10001035static void
1036smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
1037{
1038 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buffer;
1039
1040 /*
1041 * SMB1 does not use credits.
1042 */
1043 if (server->vals->header_preamble_size)
1044 return;
1045
1046 if (shdr->CreditRequest) {
1047 spin_lock(&server->req_lock);
1048 server->credits += le16_to_cpu(shdr->CreditRequest);
1049 spin_unlock(&server->req_lock);
1050 wake_up(&server->request_q);
1051 }
1052}
1053
1054
Jeff Laytone9097ab2011-10-19 15:29:40 -04001055static int
Al Viro7c97c202011-06-21 08:51:28 -04001056cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001058 int i, num_mids, length;
Al Viro7c97c202011-06-21 08:51:28 -04001059 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001060 unsigned int pdu_length;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001061 unsigned int next_offset;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001062 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001063 struct task_struct *task_to_wake = NULL;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001064 struct mid_q_entry *mids[MAX_COMPOUND];
1065 char *bufs[MAX_COMPOUND];
Rohith Surabattula8e670f72020-09-18 05:37:28 +00001066 unsigned int noreclaim_flag, num_io_timeout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Eric Biggersdc920272020-03-08 22:58:20 -07001068 noreclaim_flag = memalloc_noreclaim_save();
Joe Perchesf96637b2013-05-04 22:12:25 -05001069 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -04001070
1071 length = atomic_inc_return(&tcpSesAllocCount);
1072 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -07001073 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001075 set_freezable();
Eric W. Biederman33da8e72019-08-16 12:33:54 -05001076 allow_kernel_signal(SIGKILL);
Jeff Layton469ee612008-10-16 18:46:39 +00001077 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -07001078 if (try_to_freeze())
1079 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -07001080
Jeff Layton2a37ef92011-10-19 15:29:23 -04001081 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +04001082 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -07001083
Jeff Layton2a37ef92011-10-19 15:29:23 -04001084 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001085 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +00001086 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +00001087
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001088 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -04001089 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +00001090 continue;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10001091
1092 if (server->vals->header_preamble_size == 0)
1093 server->total_read = 0;
1094 else
1095 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -07001096
Pavel Shilovsky98bac622011-08-01 13:19:42 +04001097 /*
1098 * The right amount was read from socket - 4 bytes,
1099 * so we can now interpret the length field.
1100 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001101 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -07001102
Joe Perchesf96637b2013-05-04 22:12:25 -05001103 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -04001104 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001105 continue;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001106next_pdu:
1107 server->pdu_size = pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -07001108
Jeff Layton89482a52011-10-19 15:28:57 -04001109 /* make sure we have enough to get to the MID */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001110 if (server->pdu_size < HEADER_SIZE(server) - 1 -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001111 server->vals->header_preamble_size) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001112 cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001113 server->pdu_size);
Jeff Layton89482a52011-10-19 15:28:57 -04001114 cifs_reconnect(server);
Jeff Layton89482a52011-10-19 15:28:57 -04001115 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -07001116 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +04001117
Jeff Layton89482a52011-10-19 15:28:57 -04001118 /* read down to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001119 length = cifs_read_from_socket(server,
1120 buf + server->vals->header_preamble_size,
1121 HEADER_SIZE(server) - 1
1122 - server->vals->header_preamble_size);
Jeff Layton89482a52011-10-19 15:28:57 -04001123 if (length < 0)
1124 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001125 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -04001126
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001127 if (server->ops->next_header) {
1128 next_offset = server->ops->next_header(buf);
1129 if (next_offset)
1130 server->pdu_size = next_offset;
1131 }
1132
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001133 memset(mids, 0, sizeof(mids));
1134 memset(bufs, 0, sizeof(bufs));
1135 num_mids = 0;
1136
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001137 if (server->ops->is_transform_hdr &&
1138 server->ops->receive_transform &&
1139 server->ops->is_transform_hdr(buf)) {
1140 length = server->ops->receive_transform(server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001141 mids,
1142 bufs,
1143 &num_mids);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001144 } else {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001145 mids[0] = server->ops->find_mid(server, buf);
1146 bufs[0] = buf;
Steve French7af929d2018-10-02 18:54:09 -05001147 num_mids = 1;
Jeff Laytonc8054eb2011-10-19 15:29:31 -04001148
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001149 if (!mids[0] || !mids[0]->receive)
1150 length = standard_receive3(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001151 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001152 length = mids[0]->receive(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001153 }
Jeff Layton44d22d82011-10-19 15:29:49 -04001154
Lars Persson696e4202018-06-25 14:05:25 +02001155 if (length < 0) {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001156 for (i = 0; i < num_mids; i++)
1157 if (mids[i])
1158 cifs_mid_q_entry_release(mids[i]);
Steve Frenche4eb2952005-04-28 22:41:09 -07001159 continue;
Lars Persson696e4202018-06-25 14:05:25 +02001160 }
Steve Frenche4eb2952005-04-28 22:41:09 -07001161
Rohith Surabattula8e670f72020-09-18 05:37:28 +00001162 if (server->ops->is_status_io_timeout &&
1163 server->ops->is_status_io_timeout(buf)) {
1164 num_io_timeout++;
1165 if (num_io_timeout > NUM_STATUS_IO_TIMEOUT) {
1166 cifs_reconnect(server);
1167 num_io_timeout = 0;
1168 continue;
1169 }
1170 }
1171
Steve Frenchfda35942011-01-20 18:06:34 +00001172 server->lstrp = jiffies;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001173
1174 for (i = 0; i < num_mids; i++) {
1175 if (mids[i] != NULL) {
1176 mids[i]->resp_buf_size = server->pdu_size;
Sachin Prabhu38bd4902017-03-03 15:41:38 -08001177
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001178 if (!mids[i]->multiRsp || mids[i]->multiEnd)
1179 mids[i]->callback(mids[i]);
Lars Persson696e4202018-06-25 14:05:25 +02001180
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001181 cifs_mid_q_entry_release(mids[i]);
1182 } else if (server->ops->is_oplock_break &&
1183 server->ops->is_oplock_break(bufs[i],
1184 server)) {
Ronnie Sahlbergeca00452019-02-05 12:56:44 +10001185 smb2_add_credits_from_hdr(bufs[i], server);
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001186 cifs_dbg(FYI, "Received oplock break\n");
1187 } else {
Joe Perchesa0a30362020-04-14 22:42:53 -07001188 cifs_server_dbg(VFS, "No task to wake, unknown frame received! NumMids %d\n",
1189 atomic_read(&midCount));
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001190 cifs_dump_mem("Received Data is: ", bufs[i],
1191 HEADER_SIZE(server));
Ronnie Sahlberg3e272572019-07-06 06:43:08 +10001192 smb2_add_credits_from_hdr(bufs[i], server);
Steve French39798772006-05-31 22:40:51 +00001193#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001194 if (server->ops->dump_detail)
1195 server->ops->dump_detail(bufs[i],
1196 server);
1197 cifs_dump_mids(server);
Steve French39798772006-05-31 22:40:51 +00001198#endif /* CIFS_DEBUG2 */
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001199 }
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001200 }
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001201
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001202 if (pdu_length > server->pdu_size) {
1203 if (!allocate_buffers(server))
1204 continue;
1205 pdu_length -= server->pdu_size;
1206 server->total_read = 0;
1207 server->large_buf = false;
1208 buf = server->smallbuf;
1209 goto next_pdu;
Steve Frenche4eb2952005-04-28 22:41:09 -07001210 }
1211 } /* end while !EXITING */
1212
Justin P. Mattockfd62cb72011-02-24 22:15:02 -08001213 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -04001214 cifs_buf_release(server->bigbuf);
1215 if (server->smallbuf) /* no sense logging a debug message if NULL */
1216 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001218 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +04001219 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001220
1221 /* if server->tsk was NULL then wait for a signal before exiting */
1222 if (!task_to_wake) {
1223 set_current_state(TASK_INTERRUPTIBLE);
1224 while (!signal_pending(current)) {
1225 schedule();
1226 set_current_state(TASK_INTERRUPTIBLE);
1227 }
1228 set_current_state(TASK_RUNNING);
1229 }
1230
Eric Biggersdc920272020-03-08 22:58:20 -07001231 memalloc_noreclaim_restore(noreclaim_flag);
Jeff Layton0468a2c2008-12-01 07:09:35 -05001232 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
Jeff Laytonc359cf32007-11-16 22:22:06 +00001235/* extract the host portion of the UNC string */
1236static char *
1237extract_hostname(const char *unc)
1238{
1239 const char *src;
1240 char *dst, *delim;
1241 unsigned int len;
1242
1243 /* skip double chars at beginning of string */
1244 /* BB: check validity of these bytes? */
Paulo Alcantarac34fea52018-11-14 14:03:40 -02001245 if (strlen(unc) < 3)
1246 return ERR_PTR(-EINVAL);
1247 for (src = unc; *src && *src == '\\'; src++)
1248 ;
1249 if (!*src)
1250 return ERR_PTR(-EINVAL);
Jeff Laytonc359cf32007-11-16 22:22:06 +00001251
1252 /* delimiter between hostname and sharename is always '\\' now */
1253 delim = strchr(src, '\\');
1254 if (!delim)
1255 return ERR_PTR(-EINVAL);
1256
1257 len = delim - src;
1258 dst = kmalloc((len + 1), GFP_KERNEL);
1259 if (dst == NULL)
1260 return ERR_PTR(-ENOMEM);
1261
1262 memcpy(dst, src, len);
1263 dst[len] = '\0';
1264
1265 return dst;
1266}
1267
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001268static int get_option_ul(substring_t args[], unsigned long *option)
1269{
1270 int rc;
1271 char *string;
1272
1273 string = match_strdup(args);
1274 if (string == NULL)
1275 return -ENOMEM;
Sachin Prabhubfa890a2012-04-13 14:04:32 +01001276 rc = kstrtoul(string, 0, option);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001277 kfree(string);
1278
1279 return rc;
1280}
1281
Eric W. Biederman3da46562013-02-06 01:37:39 -08001282static int get_option_uid(substring_t args[], kuid_t *result)
1283{
1284 unsigned long value;
1285 kuid_t uid;
1286 int rc;
1287
1288 rc = get_option_ul(args, &value);
1289 if (rc)
1290 return rc;
1291
1292 uid = make_kuid(current_user_ns(), value);
1293 if (!uid_valid(uid))
1294 return -EINVAL;
1295
1296 *result = uid;
1297 return 0;
1298}
1299
1300static int get_option_gid(substring_t args[], kgid_t *result)
1301{
1302 unsigned long value;
1303 kgid_t gid;
1304 int rc;
1305
1306 rc = get_option_ul(args, &value);
1307 if (rc)
1308 return rc;
1309
1310 gid = make_kgid(current_user_ns(), value);
1311 if (!gid_valid(gid))
1312 return -EINVAL;
1313
1314 *result = gid;
1315 return 0;
1316}
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001317
Jeff Laytond387a5c2012-12-10 06:10:46 -05001318/*
1319 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1320 * fields with the result. Returns 0 on success and an error otherwise.
1321 */
1322static int
1323cifs_parse_devname(const char *devname, struct smb_vol *vol)
1324{
1325 char *pos;
1326 const char *delims = "/\\";
1327 size_t len;
1328
Yao Liu68e26722019-01-28 19:47:28 +08001329 if (unlikely(!devname || !*devname)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001330 cifs_dbg(VFS, "Device name not specified\n");
Yao Liu68e26722019-01-28 19:47:28 +08001331 return -EINVAL;
1332 }
1333
Jeff Laytond387a5c2012-12-10 06:10:46 -05001334 /* make sure we have a valid UNC double delimiter prefix */
1335 len = strspn(devname, delims);
1336 if (len != 2)
1337 return -EINVAL;
1338
1339 /* find delimiter between host and sharename */
1340 pos = strpbrk(devname + 2, delims);
1341 if (!pos)
1342 return -EINVAL;
1343
1344 /* skip past delimiter */
1345 ++pos;
1346
1347 /* now go until next delimiter or end of string */
1348 len = strcspn(pos, delims);
1349
1350 /* move "pos" up to delimiter or NULL */
1351 pos += len;
1352 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1353 if (!vol->UNC)
1354 return -ENOMEM;
1355
1356 convert_delimiter(vol->UNC, '\\');
1357
Sachin Prabhu11e31642016-02-08 13:44:01 +05301358 /* skip any delimiter */
1359 if (*pos == '/' || *pos == '\\')
1360 pos++;
1361
1362 /* If pos is NULL then no prepath */
1363 if (!*pos)
Jeff Laytond387a5c2012-12-10 06:10:46 -05001364 return 0;
1365
1366 vol->prepath = kstrdup(pos, GFP_KERNEL);
1367 if (!vol->prepath)
1368 return -ENOMEM;
1369
1370 return 0;
1371}
1372
Jeff Layton23db65f2012-05-15 12:20:51 -04001373static int
Sean Finneyb9468452011-04-11 13:19:32 +00001374cifs_parse_mount_options(const char *mountdata, const char *devname,
Steve Frenchc7c137b2018-06-06 17:59:29 -05001375 struct smb_vol *vol, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376{
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001377 char *data, *end;
Vasily Averin957df452011-06-06 11:33:12 +04001378 char *mountdata_copy = NULL, *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 unsigned int temp_len, i, j;
1380 char separator[2];
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001381 short int override_uid = -1;
1382 short int override_gid = -1;
1383 bool uid_specified = false;
1384 bool gid_specified = false;
Jeff Laytond8162552012-03-23 14:40:56 -04001385 bool sloppy = false;
1386 char *invalid = NULL;
Jeff Layton88463992010-11-22 15:31:03 -05001387 char *nodename = utsname()->nodename;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001388 char *string = NULL;
1389 char *tmp_end, *value;
1390 char delim;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001391 bool got_ip = false;
Steve French7e682f72017-08-31 21:34:24 -05001392 bool got_version = false;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001393 unsigned short port = 0;
1394 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +00001397 separator[1] = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001398 delim = separator[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Jeff Layton6ee95422012-11-26 11:09:57 -05001400 /* ensure we always start with zeroed-out smb_vol */
1401 memset(vol, 0, sizeof(*vol));
1402
Jeff Layton88463992010-11-22 15:31:03 -05001403 /*
1404 * does not have to be perfect mapping since field is
1405 * informational, only used for servers that do not support
1406 * port 445 and it can be overridden at mount time
1407 */
Jeff Layton1397f2e2011-01-07 11:30:28 -05001408 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1409 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
Jeff Layton88463992010-11-22 15:31:03 -05001410 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1411
Jeff Layton1397f2e2011-01-07 11:30:28 -05001412 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -07001413 /* null target name indicates to use *SMBSERVR default called name
1414 if we end up sending RFC1001 session initialize */
1415 vol->target_rfc1001_name[0] = 0;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04001416 vol->cred_uid = current_uid();
1417 vol->linux_uid = current_uid();
David Howellsa001e5b2008-11-14 10:38:47 +11001418 vol->linux_gid = current_gid();
Steve Frenche8506d22019-02-28 21:32:15 -06001419 vol->bsize = 1024 * 1024; /* can improve cp performance significantly */
Steve French2baa2682014-09-27 02:19:01 -05001420 /*
1421 * default to SFM style remapping of seven reserved characters
1422 * unless user overrides it or we negotiate CIFS POSIX where
1423 * it is unnecessary. Can not simultaneously use more than one mapping
1424 * since then readdir could list files that open could not open
1425 */
1426 vol->remap = true;
1427
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001428 /* default to only allowing write access to owner of the mount */
1429 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Jeremy Allisonac670552005-06-22 17:26:35 -07001432 /* default is always to request posix paths. */
1433 vol->posix_paths = 1;
Jeff Laytona0c92172009-05-27 15:40:47 -04001434 /* default to using server inode numbers where available */
1435 vol->server_ino = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001436
Jeff Layton1b359202012-09-19 15:20:27 -07001437 /* default is to use strict cifs caching semantics */
1438 vol->strict_io = true;
1439
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301440 vol->actimeo = CIFS_DEF_ACTIMEO;
1441
Steve Frenchca567eb2019-03-29 16:31:07 -05001442 /* Most clients set timeout to 0, allows server to use its default */
1443 vol->handle_timeout = 0; /* See MS-SMB2 spec section 2.2.14.2.12 */
1444
Steve French9764c022017-09-17 10:41:35 -05001445 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1446 vol->ops = &smb30_operations;
1447 vol->vals = &smbdefault_values;
Jeff Layton23db65f2012-05-15 12:20:51 -04001448
Rabin Vincentb782fcc2016-07-19 09:25:45 +02001449 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1450
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001451 /* default to no multichannel (single server connection) */
1452 vol->multichannel = false;
1453 vol->max_channels = 1;
1454
Sean Finneyb9468452011-04-11 13:19:32 +00001455 if (!mountdata)
1456 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457
Sean Finneyb9468452011-04-11 13:19:32 +00001458 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1459 if (!mountdata_copy)
1460 goto cifs_parse_mount_err;
1461
1462 options = mountdata_copy;
Pavel Shilovsky4906e502011-04-14 22:00:56 +04001463 end = options + strlen(options);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001464
Steve French50c2f752007-07-13 00:33:32 +00001465 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001466 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 separator[0] = options[4];
1468 options += 5;
1469 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001470 cifs_dbg(FYI, "Null separator not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
1472 }
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001473 vol->backupuid_specified = false; /* no backup intent for a user */
1474 vol->backupgid_specified = false; /* no backup intent for a group */
Steve French50c2f752007-07-13 00:33:32 +00001475
Jeff Layton37d4f992013-05-24 07:40:05 -04001476 switch (cifs_parse_devname(devname, vol)) {
1477 case 0:
1478 break;
1479 case -ENOMEM:
Joe Perchesa0a30362020-04-14 22:42:53 -07001480 cifs_dbg(VFS, "Unable to allocate memory for devname\n");
Jeff Layton37d4f992013-05-24 07:40:05 -04001481 goto cifs_parse_mount_err;
1482 case -EINVAL:
Joe Perchesa0a30362020-04-14 22:42:53 -07001483 cifs_dbg(VFS, "Malformed UNC in devname\n");
Jeff Layton37d4f992013-05-24 07:40:05 -04001484 goto cifs_parse_mount_err;
1485 default:
Joe Perchesa0a30362020-04-14 22:42:53 -07001486 cifs_dbg(VFS, "Unknown error parsing devname\n");
Jeff Layton37d4f992013-05-24 07:40:05 -04001487 goto cifs_parse_mount_err;
Jeff Laytond387a5c2012-12-10 06:10:46 -05001488 }
1489
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 while ((data = strsep(&options, separator)) != NULL) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001491 substring_t args[MAX_OPT_ARGS];
1492 unsigned long option;
1493 int token;
1494
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (!*data)
1496 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001498 token = match_token(data, cifs_mount_option_tokens, args);
1499
1500 switch (token) {
1501
1502 /* Ingnore the following */
1503 case Opt_ignore:
1504 break;
1505
1506 /* Boolean values */
1507 case Opt_user_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 vol->no_xattr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001509 break;
1510 case Opt_nouser_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 vol->no_xattr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001512 break;
1513 case Opt_forceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001514 override_uid = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001515 break;
1516 case Opt_noforceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001517 override_uid = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001518 break;
Jeff Layton72bd4812012-10-03 16:02:36 -04001519 case Opt_forcegid:
1520 override_gid = 1;
1521 break;
1522 case Opt_noforcegid:
1523 override_gid = 0;
1524 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001525 case Opt_noblocksend:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001526 vol->noblocksnd = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001527 break;
1528 case Opt_noautotune:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001529 vol->noautotune = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001530 break;
Steve French3e7a02d2019-09-11 21:46:20 -05001531 case Opt_nolease:
1532 vol->no_lease = 1;
1533 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001534 case Opt_hard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001536 break;
1537 case Opt_soft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001539 break;
1540 case Opt_perm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 vol->noperm = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001542 break;
1543 case Opt_noperm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 vol->noperm = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001545 break;
Steve French82e93672020-05-19 03:06:57 -05001546 case Opt_nodelete:
1547 vol->nodelete = 1;
1548 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001549 case Opt_mapchars:
Steve French2baa2682014-09-27 02:19:01 -05001550 vol->sfu_remap = true;
1551 vol->remap = false; /* disable SFM mapping */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001552 break;
1553 case Opt_nomapchars:
Steve French2baa2682014-09-27 02:19:01 -05001554 vol->sfu_remap = false;
1555 break;
1556 case Opt_mapposix:
1557 vol->remap = true;
1558 vol->sfu_remap = false; /* disable SFU mapping */
1559 break;
1560 case Opt_nomapposix:
1561 vol->remap = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001562 break;
1563 case Opt_sfu:
Steve French50c2f752007-07-13 00:33:32 +00001564 vol->sfu_emul = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001565 break;
1566 case Opt_nosfu:
Steve French50c2f752007-07-13 00:33:32 +00001567 vol->sfu_emul = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001568 break;
1569 case Opt_nodfs:
Steve French2c1b8612008-10-16 18:35:21 +00001570 vol->nodfs = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001571 break;
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03001572 case Opt_rootfs:
1573#ifdef CONFIG_CIFS_ROOT
1574 vol->rootfs = true;
1575#endif
1576 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001577 case Opt_posixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001578 vol->posix_paths = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001579 break;
1580 case Opt_noposixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001581 vol->posix_paths = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001582 break;
1583 case Opt_nounix:
Steve Frenchb3266142018-05-20 23:41:10 -05001584 if (vol->linux_ext)
1585 cifs_dbg(VFS,
1586 "conflicting unix mount options\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00001587 vol->no_linux_ext = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001588 break;
Steve Frenchb3266142018-05-20 23:41:10 -05001589 case Opt_unix:
1590 if (vol->no_linux_ext)
1591 cifs_dbg(VFS,
1592 "conflicting unix mount options\n");
1593 vol->linux_ext = 1;
1594 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001595 case Opt_nocase:
Steve French50c2f752007-07-13 00:33:32 +00001596 vol->nocase = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001597 break;
1598 case Opt_brl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001599 vol->nobrl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001600 break;
1601 case Opt_nobrl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001602 vol->nobrl = 1;
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001603 /*
1604 * turn off mandatory locking in mode
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001605 * if remote locking is turned off since the
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001606 * local vfs will do advisory
1607 */
Steve French50c2f752007-07-13 00:33:32 +00001608 if (vol->file_mode ==
1609 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001610 vol->file_mode = S_IALLUGO;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001611 break;
Steve French3d4ef9a2018-04-25 22:19:09 -05001612 case Opt_nohandlecache:
1613 vol->nohandlecache = 1;
1614 break;
1615 case Opt_handlecache:
1616 vol->nohandlecache = 0;
1617 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001618 case Opt_forcemandatorylock:
Steve French13a6e422008-12-02 17:24:33 +00001619 vol->mand_lock = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001620 break;
1621 case Opt_setuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 vol->setuids = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001623 break;
1624 case Opt_nosetuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 vol->setuids = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001626 break;
Steve French95932652016-09-23 01:36:34 -05001627 case Opt_setuidfromacl:
1628 vol->setuidfromacl = 1;
1629 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001630 case Opt_dynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001631 vol->dynperm = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001632 break;
1633 case Opt_nodynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001634 vol->dynperm = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001635 break;
1636 case Opt_nohard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001638 break;
1639 case Opt_nosoft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001641 break;
1642 case Opt_nointr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 vol->intr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001644 break;
1645 case Opt_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 vol->intr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001647 break;
1648 case Opt_nostrictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001649 vol->nostrictsync = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001650 break;
1651 case Opt_strictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001652 vol->nostrictsync = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001653 break;
1654 case Opt_serverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 vol->server_ino = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001656 break;
1657 case Opt_noserverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 vol->server_ino = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001659 break;
1660 case Opt_rwpidforward:
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001661 vol->rwpidforward = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001662 break;
Steve French412094a2019-06-24 02:01:42 -05001663 case Opt_modesid:
1664 vol->mode_ace = 1;
1665 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001666 case Opt_cifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001667 vol->cifs_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001668 break;
1669 case Opt_nocifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001670 vol->cifs_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001671 break;
1672 case Opt_acl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 vol->no_psx_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001674 break;
1675 case Opt_noacl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 vol->no_psx_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001677 break;
1678 case Opt_locallease:
Steve French84210e92008-10-23 04:42:37 +00001679 vol->local_lease = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001680 break;
1681 case Opt_sign:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001682 vol->sign = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001683 break;
Steve French4f5c10f2019-09-03 21:18:49 -05001684 case Opt_ignore_signature:
1685 vol->sign = true;
1686 vol->ignore_signature = true;
1687 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001688 case Opt_seal:
Steve French95b1cb92008-05-15 16:44:38 +00001689 /* we do not do the following in secFlags because seal
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001690 * is a per tree connection (mount) not a per socket
1691 * or per-smb connection option in the protocol
1692 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1693 */
Steve French95b1cb92008-05-15 16:44:38 +00001694 vol->seal = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001695 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001696 case Opt_noac:
Joe Perchesa0a30362020-04-14 22:42:53 -07001697 pr_warn("Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001698 break;
1699 case Opt_fsc:
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301700#ifndef CONFIG_CIFS_FSCACHE
Joe Perchesf96637b2013-05-04 22:12:25 -05001701 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001702 goto cifs_parse_mount_err;
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301703#endif
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05301704 vol->fsc = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001705 break;
1706 case Opt_mfsymlinks:
Stefan Metzmacher736a33202010-07-30 14:56:00 +02001707 vol->mfsymlinks = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001708 break;
1709 case Opt_multiuser:
Jeff Layton0eb8a132010-10-06 19:51:12 -04001710 vol->multiuser = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001711 break;
Jeff Laytond8162552012-03-23 14:40:56 -04001712 case Opt_sloppy:
1713 sloppy = true;
1714 break;
Jeff Laytona0b3df52013-05-24 07:40:59 -04001715 case Opt_nosharesock:
1716 vol->nosharesock = true;
1717 break;
Steve Frenchb2a30772015-09-29 21:49:28 -05001718 case Opt_nopersistent:
1719 vol->nopersistent = true;
1720 if (vol->persistent) {
1721 cifs_dbg(VFS,
1722 "persistenthandles mount options conflict\n");
1723 goto cifs_parse_mount_err;
1724 }
1725 break;
1726 case Opt_persistent:
1727 vol->persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06001728 if ((vol->nopersistent) || (vol->resilient)) {
Steve Frenchb2a30772015-09-29 21:49:28 -05001729 cifs_dbg(VFS,
1730 "persistenthandles mount options conflict\n");
1731 goto cifs_parse_mount_err;
1732 }
1733 break;
Steve French592fafe2015-11-03 10:08:53 -06001734 case Opt_resilient:
1735 vol->resilient = true;
1736 if (vol->persistent) {
1737 cifs_dbg(VFS,
1738 "persistenthandles mount options conflict\n");
1739 goto cifs_parse_mount_err;
1740 }
1741 break;
1742 case Opt_noresilient:
1743 vol->resilient = false; /* already the default */
1744 break;
Germano Percossi39566442016-12-15 12:31:18 +05301745 case Opt_domainauto:
1746 vol->domainauto = true;
1747 break;
Long Li8339dd32017-11-07 01:54:55 -07001748 case Opt_rdma:
1749 vol->rdma = true;
1750 break;
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001751 case Opt_multichannel:
1752 vol->multichannel = true;
Steve French1ee0e6d2020-06-02 23:17:16 -05001753 /* if number of channels not specified, default to 2 */
1754 if (vol->max_channels < 2)
1755 vol->max_channels = 2;
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001756 break;
1757 case Opt_nomultichannel:
1758 vol->multichannel = false;
Steve French1ee0e6d2020-06-02 23:17:16 -05001759 vol->max_channels = 1;
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001760 break;
Steve French9fe5ff12019-06-24 20:39:04 -05001761 case Opt_compress:
1762 vol->compression = UNKNOWN_TYPE;
1763 cifs_dbg(VFS,
1764 "SMB3 compression support is experimental\n");
1765 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001766
1767 /* Numeric Values */
1768 case Opt_backupuid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001769 if (get_option_uid(args, &vol->backupuid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001770 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1771 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001772 goto cifs_parse_mount_err;
1773 }
1774 vol->backupuid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001775 break;
1776 case Opt_backupgid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001777 if (get_option_gid(args, &vol->backupgid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001778 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1779 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001780 goto cifs_parse_mount_err;
1781 }
1782 vol->backupgid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001783 break;
1784 case Opt_uid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001785 if (get_option_uid(args, &vol->linux_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001786 cifs_dbg(VFS, "%s: Invalid uid value\n",
1787 __func__);
Sean Finneyb9468452011-04-11 13:19:32 +00001788 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001790 uid_specified = true;
1791 break;
1792 case Opt_cruid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001793 if (get_option_uid(args, &vol->cred_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001794 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1795 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001796 goto cifs_parse_mount_err;
1797 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001798 break;
1799 case Opt_gid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001800 if (get_option_gid(args, &vol->linux_gid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001801 cifs_dbg(VFS, "%s: Invalid gid value\n",
1802 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001803 goto cifs_parse_mount_err;
1804 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001805 gid_specified = true;
1806 break;
1807 case Opt_file_mode:
1808 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001809 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1810 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001811 goto cifs_parse_mount_err;
1812 }
1813 vol->file_mode = option;
1814 break;
1815 case Opt_dirmode:
1816 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001817 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1818 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001819 goto cifs_parse_mount_err;
1820 }
1821 vol->dir_mode = option;
1822 break;
1823 case Opt_port:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001824 if (get_option_ul(args, &option) ||
1825 option > USHRT_MAX) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001826 cifs_dbg(VFS, "%s: Invalid port value\n",
1827 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001828 goto cifs_parse_mount_err;
1829 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001830 port = (unsigned short)option;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001831 break;
Steve French563317e2019-09-08 23:22:02 -05001832 case Opt_min_enc_offload:
1833 if (get_option_ul(args, &option)) {
1834 cifs_dbg(VFS, "Invalid minimum encrypted read offload size (esize)\n");
1835 goto cifs_parse_mount_err;
1836 }
1837 vol->min_offload = option;
1838 break;
Steve Frenche8506d22019-02-28 21:32:15 -06001839 case Opt_blocksize:
1840 if (get_option_ul(args, &option)) {
1841 cifs_dbg(VFS, "%s: Invalid blocksize value\n",
1842 __func__);
1843 goto cifs_parse_mount_err;
1844 }
1845 /*
1846 * inode blocksize realistically should never need to be
1847 * less than 16K or greater than 16M and default is 1MB.
1848 * Note that small inode block sizes (e.g. 64K) can lead
1849 * to very poor performance of common tools like cp and scp
1850 */
1851 if ((option < CIFS_MAX_MSGSIZE) ||
1852 (option > (4 * SMB3_DEFAULT_IOSIZE))) {
1853 cifs_dbg(VFS, "%s: Invalid blocksize\n",
1854 __func__);
1855 goto cifs_parse_mount_err;
1856 }
1857 vol->bsize = option;
1858 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001859 case Opt_rsize:
1860 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001861 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1862 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001863 goto cifs_parse_mount_err;
1864 }
1865 vol->rsize = option;
1866 break;
1867 case Opt_wsize:
1868 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001869 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1870 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001871 goto cifs_parse_mount_err;
1872 }
1873 vol->wsize = option;
1874 break;
1875 case Opt_actimeo:
1876 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001877 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1878 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001879 goto cifs_parse_mount_err;
1880 }
1881 vol->actimeo = HZ * option;
1882 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001883 cifs_dbg(VFS, "attribute cache timeout too large\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001884 goto cifs_parse_mount_err;
1885 }
1886 break;
Steve Frenchca567eb2019-03-29 16:31:07 -05001887 case Opt_handletimeout:
1888 if (get_option_ul(args, &option)) {
1889 cifs_dbg(VFS, "%s: Invalid handletimeout value\n",
1890 __func__);
1891 goto cifs_parse_mount_err;
1892 }
1893 vol->handle_timeout = option;
1894 if (vol->handle_timeout > SMB3_MAX_HANDLE_TIMEOUT) {
1895 cifs_dbg(VFS, "Invalid handle cache timeout, longer than 16 minutes\n");
1896 goto cifs_parse_mount_err;
1897 }
1898 break;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001899 case Opt_echo_interval:
1900 if (get_option_ul(args, &option)) {
1901 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
1902 __func__);
1903 goto cifs_parse_mount_err;
1904 }
1905 vol->echo_interval = option;
1906 break;
Steve French8b217fe2016-11-11 22:36:20 -06001907 case Opt_snapshot:
1908 if (get_option_ul(args, &option)) {
1909 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
1910 __func__);
1911 goto cifs_parse_mount_err;
1912 }
1913 vol->snapshot_time = option;
1914 break;
Steve French141891f2016-09-23 00:44:16 -05001915 case Opt_max_credits:
1916 if (get_option_ul(args, &option) || (option < 20) ||
1917 (option > 60000)) {
1918 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
1919 __func__);
1920 goto cifs_parse_mount_err;
1921 }
1922 vol->max_credits = option;
1923 break;
Aurelien Aptelbcc88802019-09-20 04:32:20 +02001924 case Opt_max_channels:
1925 if (get_option_ul(args, &option) || option < 1 ||
1926 option > CIFS_MAX_CHANNELS) {
1927 cifs_dbg(VFS, "%s: Invalid max_channels value, needs to be 1-%d\n",
1928 __func__, CIFS_MAX_CHANNELS);
1929 goto cifs_parse_mount_err;
1930 }
1931 vol->max_channels = option;
1932 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001933
1934 /* String Arguments */
1935
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001936 case Opt_blank_user:
1937 /* null user, ie. anonymous authentication */
1938 vol->nullauth = 1;
1939 vol->username = NULL;
1940 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001941 case Opt_user:
1942 string = match_strdup(args);
1943 if (string == NULL)
1944 goto out_nomem;
1945
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001946 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1947 CIFS_MAX_USERNAME_LEN) {
Joe Perchesa0a30362020-04-14 22:42:53 -07001948 pr_warn("username too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001949 goto cifs_parse_mount_err;
1950 }
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001951
1952 kfree(vol->username);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001953 vol->username = kstrdup(string, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -05001954 if (!vol->username)
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001955 goto cifs_parse_mount_err;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001956 break;
1957 case Opt_blank_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001958 /* passwords have to be handled differently
1959 * to allow the character used for deliminator
1960 * to be passed within them
1961 */
1962
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001963 /*
1964 * Check if this is a case where the password
1965 * starts with a delimiter
1966 */
1967 tmp_end = strchr(data, '=');
1968 tmp_end++;
1969 if (!(tmp_end < end && tmp_end[1] == delim)) {
1970 /* No it is not. Set the password to NULL */
Waiman Long453431a2020-08-06 23:18:13 -07001971 kfree_sensitive(vol->password);
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001972 vol->password = NULL;
1973 break;
1974 }
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001975 fallthrough; /* to Opt_pass below */
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001976 case Opt_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001977 /* Obtain the value string */
1978 value = strchr(data, '=');
Sachin Prabhu10238072012-03-28 18:07:08 +01001979 value++;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001980
1981 /* Set tmp_end to end of the string */
1982 tmp_end = (char *) value + strlen(value);
1983
1984 /* Check if following character is the deliminator
1985 * If yes, we have encountered a double deliminator
1986 * reset the NULL character to the deliminator
1987 */
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301988 if (tmp_end < end && tmp_end[1] == delim) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001989 tmp_end[0] = delim;
1990
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301991 /* Keep iterating until we get to a single
1992 * deliminator OR the end
1993 */
1994 while ((tmp_end = strchr(tmp_end, delim))
1995 != NULL && (tmp_end[1] == delim)) {
1996 tmp_end = (char *) &tmp_end[2];
1997 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001998
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301999 /* Reset var options to point to next element */
2000 if (tmp_end) {
2001 tmp_end[0] = '\0';
2002 options = (char *) &tmp_end[1];
2003 } else
2004 /* Reached the end of the mount option
2005 * string */
2006 options = end;
2007 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002008
Waiman Long453431a2020-08-06 23:18:13 -07002009 kfree_sensitive(vol->password);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002010 /* Now build new password string */
2011 temp_len = strlen(value);
2012 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
2013 if (vol->password == NULL) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002014 pr_warn("no memory for password\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002015 goto cifs_parse_mount_err;
2016 }
2017
2018 for (i = 0, j = 0; i < temp_len; i++, j++) {
2019 vol->password[j] = value[i];
2020 if ((value[i] == delim) &&
2021 value[i+1] == delim)
2022 /* skip the second deliminator */
2023 i++;
2024 }
2025 vol->password[j] = '\0';
2026 break;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002027 case Opt_blank_ip:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002028 /* FIXME: should this be an error instead? */
2029 got_ip = false;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002030 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002031 case Opt_ip:
2032 string = match_strdup(args);
2033 if (string == NULL)
2034 goto out_nomem;
2035
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002036 if (!cifs_convert_address(dstaddr, string,
2037 strlen(string))) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002038 pr_err("bad ip= option (%s)\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002039 goto cifs_parse_mount_err;
2040 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002041 got_ip = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002042 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002043 case Opt_domain:
2044 string = match_strdup(args);
2045 if (string == NULL)
2046 goto out_nomem;
2047
Chen Gang057d6332013-07-19 09:01:36 +08002048 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
2049 == CIFS_MAX_DOMAINNAME_LEN) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002050 pr_warn("domain name too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002051 goto cifs_parse_mount_err;
2052 }
2053
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04002054 kfree(vol->domainname);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002055 vol->domainname = kstrdup(string, GFP_KERNEL);
2056 if (!vol->domainname) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002057 pr_warn("no memory for domainname\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002058 goto cifs_parse_mount_err;
2059 }
Joe Perchesf96637b2013-05-04 22:12:25 -05002060 cifs_dbg(FYI, "Domain name set\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002061 break;
2062 case Opt_srcaddr:
2063 string = match_strdup(args);
2064 if (string == NULL)
2065 goto out_nomem;
2066
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002067 if (!cifs_convert_address(
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002068 (struct sockaddr *)&vol->srcaddr,
2069 string, strlen(string))) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002070 pr_warn("Could not parse srcaddr: %s\n",
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002071 string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002072 goto cifs_parse_mount_err;
2073 }
2074 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002075 case Opt_iocharset:
2076 string = match_strdup(args);
2077 if (string == NULL)
2078 goto out_nomem;
2079
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002080 if (strnlen(string, 1024) >= 65) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002081 pr_warn("iocharset name too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002082 goto cifs_parse_mount_err;
2083 }
2084
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002085 if (strncasecmp(string, "default", 7) != 0) {
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04002086 kfree(vol->iocharset);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002087 vol->iocharset = kstrdup(string,
2088 GFP_KERNEL);
2089 if (!vol->iocharset) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002090 pr_warn("no memory for charset\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002091 goto cifs_parse_mount_err;
2092 }
2093 }
2094 /* if iocharset not set then load_nls_default
2095 * is used by caller
2096 */
Joe Perchesf96637b2013-05-04 22:12:25 -05002097 cifs_dbg(FYI, "iocharset set to %s\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002098 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002099 case Opt_netbiosname:
2100 string = match_strdup(args);
2101 if (string == NULL)
2102 goto out_nomem;
2103
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002104 memset(vol->source_rfc1001_name, 0x20,
2105 RFC1001_NAME_LEN);
2106 /*
2107 * FIXME: are there cases in which a comma can
2108 * be valid in workstation netbios name (and
2109 * need special handling)?
2110 */
2111 for (i = 0; i < RFC1001_NAME_LEN; i++) {
2112 /* don't ucase netbiosname for user */
2113 if (string[i] == 0)
2114 break;
2115 vol->source_rfc1001_name[i] = string[i];
2116 }
2117 /* The string has 16th byte zero still from
2118 * set at top of the function
2119 */
2120 if (i == RFC1001_NAME_LEN && string[i] != 0)
Joe Perchesa0a30362020-04-14 22:42:53 -07002121 pr_warn("netbiosname longer than 15 truncated\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002122 break;
2123 case Opt_servern:
2124 /* servernetbiosname specified override *SMBSERVER */
2125 string = match_strdup(args);
2126 if (string == NULL)
2127 goto out_nomem;
2128
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002129 /* last byte, type, is 0x20 for servr type */
2130 memset(vol->target_rfc1001_name, 0x20,
2131 RFC1001_NAME_LEN_WITH_NULL);
2132
2133 /* BB are there cases in which a comma can be
2134 valid in this workstation netbios name
2135 (and need special handling)? */
2136
2137 /* user or mount helper must uppercase the
2138 netbios name */
2139 for (i = 0; i < 15; i++) {
2140 if (string[i] == 0)
2141 break;
2142 vol->target_rfc1001_name[i] = string[i];
2143 }
2144 /* The string has 16th byte zero still from
2145 set at top of the function */
2146 if (i == RFC1001_NAME_LEN && string[i] != 0)
Joe Perchesa0a30362020-04-14 22:42:53 -07002147 pr_warn("server netbiosname longer than 15 truncated\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002148 break;
2149 case Opt_ver:
Steve French7e682f72017-08-31 21:34:24 -05002150 /* version of mount userspace tools, not dialect */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002151 string = match_strdup(args);
2152 if (string == NULL)
2153 goto out_nomem;
2154
Steve French7e682f72017-08-31 21:34:24 -05002155 /* If interface changes in mount.cifs bump to new ver */
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002156 if (strncasecmp(string, "1", 1) == 0) {
Steve French7e682f72017-08-31 21:34:24 -05002157 if (strlen(string) > 1) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002158 pr_warn("Bad mount helper ver=%s. Did you want SMB1 (CIFS) dialect and mean to type vers=1.0 instead?\n",
2159 string);
Steve French7e682f72017-08-31 21:34:24 -05002160 goto cifs_parse_mount_err;
2161 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002162 /* This is the default */
2163 break;
2164 }
2165 /* For all other value, error */
Joe Perchesa0a30362020-04-14 22:42:53 -07002166 pr_warn("Invalid mount helper version specified\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002167 goto cifs_parse_mount_err;
Jeff Layton23db65f2012-05-15 12:20:51 -04002168 case Opt_vers:
Steve French7e682f72017-08-31 21:34:24 -05002169 /* protocol version (dialect) */
Jeff Layton23db65f2012-05-15 12:20:51 -04002170 string = match_strdup(args);
2171 if (string == NULL)
2172 goto out_nomem;
2173
Steve Frenchc7c137b2018-06-06 17:59:29 -05002174 if (cifs_parse_smb_version(string, vol, is_smb3) != 0)
Jeff Layton23db65f2012-05-15 12:20:51 -04002175 goto cifs_parse_mount_err;
Steve French7e682f72017-08-31 21:34:24 -05002176 got_version = true;
Jeff Layton23db65f2012-05-15 12:20:51 -04002177 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002178 case Opt_sec:
2179 string = match_strdup(args);
2180 if (string == NULL)
2181 goto out_nomem;
2182
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002183 if (cifs_parse_security_flavors(string, vol) != 0)
2184 goto cifs_parse_mount_err;
2185 break;
Jeff Layton15b6a472012-05-16 07:50:15 -04002186 case Opt_cache:
2187 string = match_strdup(args);
2188 if (string == NULL)
2189 goto out_nomem;
2190
2191 if (cifs_parse_cache_flavor(string, vol) != 0)
2192 goto cifs_parse_mount_err;
2193 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002194 default:
Jeff Laytond8162552012-03-23 14:40:56 -04002195 /*
2196 * An option we don't recognize. Save it off for later
2197 * if we haven't already found one
2198 */
2199 if (!invalid)
2200 invalid = data;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002201 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002203 /* Free up any allocated string */
2204 kfree(string);
2205 string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002207
Jeff Laytond8162552012-03-23 14:40:56 -04002208 if (!sloppy && invalid) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002209 pr_err("Unknown mount option \"%s\"\n", invalid);
Jeff Laytond8162552012-03-23 14:40:56 -04002210 goto cifs_parse_mount_err;
2211 }
2212
Long Li8339dd32017-11-07 01:54:55 -07002213 if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
2214 cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
2215 goto cifs_parse_mount_err;
2216 }
2217
Jeff Layton8a8798a2012-01-17 16:09:15 -05002218#ifndef CONFIG_KEYS
2219 /* Muliuser mounts require CONFIG_KEYS support */
2220 if (vol->multiuser) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002221 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002222 goto cifs_parse_mount_err;
Jeff Layton0eb8a132010-10-06 19:51:12 -04002223 }
Jeff Layton8a8798a2012-01-17 16:09:15 -05002224#endif
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002225 if (!vol->UNC) {
Jeff Layton37d4f992013-05-24 07:40:05 -04002226 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002227 goto cifs_parse_mount_err;
2228 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002229
Jeff Layton62a1a432012-12-10 06:10:45 -05002230 /* make sure UNC has a share name */
2231 if (!strchr(vol->UNC + 3, '\\')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002232 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
Jeff Layton62a1a432012-12-10 06:10:45 -05002233 goto cifs_parse_mount_err;
2234 }
2235
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002236 if (!got_ip) {
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002237 int len;
2238 const char *slash;
2239
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002240 /* No ip= option specified? Try to get it from UNC */
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002241 /* Use the address part of the UNC. */
2242 slash = strchr(&vol->UNC[2], '\\');
2243 len = slash - &vol->UNC[2];
2244 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002245 pr_err("Unable to determine destination address\n");
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002246 goto cifs_parse_mount_err;
2247 }
2248 }
2249
2250 /* set the port that we got earlier */
2251 cifs_set_port(dstaddr, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002253 if (uid_specified)
2254 vol->override_uid = override_uid;
2255 else if (override_uid == 1)
Joe Perchesa0a30362020-04-14 22:42:53 -07002256 pr_notice("ignoring forceuid mount option specified with no uid= option\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002257
2258 if (gid_specified)
2259 vol->override_gid = override_gid;
2260 else if (override_gid == 1)
Joe Perchesa0a30362020-04-14 22:42:53 -07002261 pr_notice("ignoring forcegid mount option specified with no gid= option\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002262
Steve French7e682f72017-08-31 21:34:24 -05002263 if (got_version == false)
Joe Perchesa0a30362020-04-14 22:42:53 -07002264 pr_warn_once("No dialect specified on mount. Default has changed to a more secure dialect, SMB2.1 or later (e.g. SMB3.1.1), from CIFS (SMB1). To use the less secure SMB1 dialect to access old servers which do not support SMB3.1.1 (or even SMB3 or SMB2.1) specify vers=1.0 on mount.\n");
Steve French7e682f72017-08-31 21:34:24 -05002265
Sean Finneyb9468452011-04-11 13:19:32 +00002266 kfree(mountdata_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 return 0;
Sean Finneyb9468452011-04-11 13:19:32 +00002268
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002269out_nomem:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002270 pr_warn("Could not allocate temporary buffer\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002271cifs_parse_mount_err:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002272 kfree(string);
Sean Finneyb9468452011-04-11 13:19:32 +00002273 kfree(mountdata_copy);
2274 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275}
2276
Ben Greear3eb9a882010-09-01 17:06:02 -07002277/** Returns true if srcaddr isn't specified and rhs isn't
2278 * specified, or if srcaddr is specified and
2279 * matches the IP address of the rhs argument.
2280 */
Paulo Alcantarae4af35f2020-05-19 15:38:28 -03002281bool
2282cifs_match_ipaddr(struct sockaddr *srcaddr, struct sockaddr *rhs)
Ben Greear3eb9a882010-09-01 17:06:02 -07002283{
2284 switch (srcaddr->sa_family) {
2285 case AF_UNSPEC:
2286 return (rhs->sa_family == AF_UNSPEC);
2287 case AF_INET: {
2288 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2289 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2290 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2291 }
2292 case AF_INET6: {
2293 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05002294 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07002295 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2296 }
2297 default:
2298 WARN_ON(1);
2299 return false; /* don't expect to be here */
2300 }
2301}
2302
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002303/*
2304 * If no port is specified in addr structure, we try to match with 445 port
2305 * and if it fails - with 139 ports. It should be called only if address
2306 * families of server and addr are equal.
2307 */
2308static bool
2309match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2310{
Steve French6da97912011-03-13 18:55:55 +00002311 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002312
Long Li3b249112019-05-15 14:09:04 -07002313 /* SMBDirect manages its own ports, don't match it here */
2314 if (server->rdma)
2315 return true;
2316
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002317 switch (addr->sa_family) {
2318 case AF_INET:
2319 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2320 port = ((struct sockaddr_in *) addr)->sin_port;
2321 break;
2322 case AF_INET6:
2323 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2324 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2325 break;
2326 default:
2327 WARN_ON(1);
2328 return false;
2329 }
2330
2331 if (!port) {
2332 port = htons(CIFS_PORT);
2333 if (port == *sport)
2334 return true;
2335
2336 port = htons(RFC1001_PORT);
2337 }
2338
2339 return port == *sport;
2340}
Ben Greear3eb9a882010-09-01 17:06:02 -07002341
2342static bool
2343match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2344 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345{
Jeff Layton45151482010-07-06 20:43:02 -04002346 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002347 case AF_INET: {
2348 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2349 struct sockaddr_in *srv_addr4 =
2350 (struct sockaddr_in *)&server->dstaddr;
2351
2352 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04002353 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002354 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002355 }
2356 case AF_INET6: {
2357 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2358 struct sockaddr_in6 *srv_addr6 =
2359 (struct sockaddr_in6 *)&server->dstaddr;
2360
Jeff Layton45151482010-07-06 20:43:02 -04002361 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002362 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04002363 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002364 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04002365 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002366 break;
2367 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002368 default:
2369 WARN_ON(1);
2370 return false; /* don't expect to be here */
2371 }
Jeff Layton45151482010-07-06 20:43:02 -04002372
Paulo Alcantarae4af35f2020-05-19 15:38:28 -03002373 if (!cifs_match_ipaddr(srcaddr, (struct sockaddr *)&server->srcaddr))
Ben Greear3eb9a882010-09-01 17:06:02 -07002374 return false;
2375
Jeff Layton45151482010-07-06 20:43:02 -04002376 return true;
2377}
2378
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002379static bool
2380match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2381{
Jeff Layton3f618222013-06-12 19:52:14 -05002382 /*
2383 * The select_sectype function should either return the vol->sectype
2384 * that was specified, or "Unspecified" if that sectype was not
2385 * compatible with the given NEGOTIATE request.
2386 */
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302387 if (server->ops->select_sectype(server, vol->sectype)
2388 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002389 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002390
Jeff Layton3f618222013-06-12 19:52:14 -05002391 /*
2392 * Now check if signing mode is acceptable. No need to check
2393 * global_secflags at this point since if MUST_SIGN is set then
2394 * the server->sign had better be too.
2395 */
Jeff Layton38d77c52013-05-26 07:01:00 -04002396 if (vol->sign && !server->sign)
2397 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002398
2399 return true;
2400}
2401
Jeff Layton9fa114f2012-11-26 11:09:57 -05002402static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002403{
Jeff Layton9fa114f2012-11-26 11:09:57 -05002404 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2405
Jeff Laytona0b3df52013-05-24 07:40:59 -04002406 if (vol->nosharesock)
2407 return 0;
2408
Steve French43cdae82019-06-13 14:26:49 -05002409 /* If multidialect negotiation see if existing sessions match one */
2410 if (strcmp(vol->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
2411 if (server->vals->protocol_id < SMB30_PROT_ID)
2412 return 0;
2413 } else if (strcmp(vol->vals->version_string,
2414 SMBDEFAULT_VERSION_STRING) == 0) {
2415 if (server->vals->protocol_id < SMB21_PROT_ID)
2416 return 0;
2417 } else if ((server->vals != vol->vals) || (server->ops != vol->ops))
Jeff Layton23db65f2012-05-15 12:20:51 -04002418 return 0;
2419
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002420 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2421 return 0;
2422
2423 if (!match_address(server, addr,
2424 (struct sockaddr *)&vol->srcaddr))
2425 return 0;
2426
2427 if (!match_port(server, addr))
2428 return 0;
2429
2430 if (!match_security(server, vol))
2431 return 0;
2432
Rabin Vincentb782fcc2016-07-19 09:25:45 +02002433 if (server->echo_interval != vol->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002434 return 0;
2435
Long Li8339dd32017-11-07 01:54:55 -07002436 if (server->rdma != vol->rdma)
2437 return 0;
2438
Steve French4f5c10f2019-09-03 21:18:49 -05002439 if (server->ignore_signature != vol->ignore_signature)
2440 return 0;
2441
Steve French563317e2019-09-08 23:22:02 -05002442 if (server->min_offload != vol->min_offload)
2443 return 0;
2444
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002445 return 1;
2446}
2447
Paulo Alcantara54be1f62018-11-14 16:01:21 -02002448struct TCP_Server_Info *
Jeff Layton9fa114f2012-11-26 11:09:57 -05002449cifs_find_tcp_session(struct smb_vol *vol)
Jeff Layton45151482010-07-06 20:43:02 -04002450{
Jeff Laytone7ddee92008-11-14 13:44:38 -05002451 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302453 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04002454 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Paulo Alcantara (SUSE)3345bb42019-12-04 11:25:06 -03002455 /*
2456 * Skip ses channels since they're only handled in lower layers
2457 * (e.g. cifs_send_recv).
2458 */
2459 if (server->is_channel || !match_server(server, vol))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002460 continue;
2461
Jeff Laytone7ddee92008-11-14 13:44:38 -05002462 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302463 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05002464 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05002465 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302467 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 return NULL;
2469}
2470
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002471void
2472cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002474 struct task_struct *task;
2475
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302476 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002477 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302478 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002479 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
Steve Frenchdea570e02008-05-06 22:05:51 +00002481
Rob Landleyf1d0c992011-01-22 15:44:05 -06002482 put_net(cifs_net_ns(server));
2483
Jeff Laytone7ddee92008-11-14 13:44:38 -05002484 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302485 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002486
Jeff Laytonc74093b2011-01-11 07:24:23 -05002487 cancel_delayed_work_sync(&server->echo);
2488
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002489 if (from_reconnect)
2490 /*
2491 * Avoid deadlock here: reconnect work calls
2492 * cifs_put_tcp_session() at its end. Need to be sure
2493 * that reconnect work does nothing with server pointer after
2494 * that step.
2495 */
2496 cancel_delayed_work(&server->reconnect);
2497 else
2498 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002499
Jeff Laytone7ddee92008-11-14 13:44:38 -05002500 spin_lock(&GlobalMid_Lock);
2501 server->tcpStatus = CifsExiting;
2502 spin_unlock(&GlobalMid_Lock);
2503
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002504 cifs_crypto_secmech_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302505 cifs_fscache_release_client_cookie(server);
2506
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05002507 kfree(server->session_key.response);
2508 server->session_key.response = NULL;
2509 server->session_key.len = 0;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002510
2511 task = xchg(&server->tsk, NULL);
2512 if (task)
Eric W. Biederman72abe3b2019-05-15 12:33:50 -05002513 send_sig(SIGKILL, task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514}
2515
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002516struct TCP_Server_Info *
Jeff Layton63c038c2008-12-01 18:41:46 -05002517cifs_get_tcp_session(struct smb_vol *volume_info)
2518{
2519 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05002520 int rc;
2521
Joe Perchesf96637b2013-05-04 22:12:25 -05002522 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05002523
2524 /* see if we already have a matching tcp_ses */
Jeff Layton9fa114f2012-11-26 11:09:57 -05002525 tcp_ses = cifs_find_tcp_session(volume_info);
Jeff Layton63c038c2008-12-01 18:41:46 -05002526 if (tcp_ses)
2527 return tcp_ses;
2528
2529 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2530 if (!tcp_ses) {
2531 rc = -ENOMEM;
2532 goto out_err;
2533 }
2534
Jeff Layton23db65f2012-05-15 12:20:51 -04002535 tcp_ses->ops = volume_info->ops;
2536 tcp_ses->vals = volume_info->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06002537 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05002538 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2539 if (IS_ERR(tcp_ses->hostname)) {
2540 rc = PTR_ERR(tcp_ses->hostname);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002541 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002542 }
2543
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03002544 tcp_ses->noblockcnt = volume_info->rootfs;
2545 tcp_ses->noblocksnd = volume_info->noblocksnd || volume_info->rootfs;
Jeff Layton63c038c2008-12-01 18:41:46 -05002546 tcp_ses->noautotune = volume_info->noautotune;
Steve French6a5fa2362010-01-01 01:28:43 +00002547 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
Long Li8339dd32017-11-07 01:54:55 -07002548 tcp_ses->rdma = volume_info->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03002549 tcp_ses->in_flight = 0;
Steve French1b63f182019-09-09 22:57:11 -05002550 tcp_ses->max_in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002551 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05002552 init_waitqueue_head(&tcp_ses->response_q);
2553 init_waitqueue_head(&tcp_ses->request_q);
2554 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2555 mutex_init(&tcp_ses->srv_mutex);
2556 memcpy(tcp_ses->workstation_RFC1001_name,
2557 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2558 memcpy(tcp_ses->server_RFC1001_name,
2559 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05002560 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05002561 tcp_ses->sequence_number = 0;
Pavel Shilovsky5b964852019-01-18 11:30:26 -08002562 tcp_ses->reconnect_instance = 1;
Steve Frenchfda35942011-01-20 18:06:34 +00002563 tcp_ses->lstrp = jiffies;
Steve French9fe5ff12019-06-24 20:39:04 -05002564 tcp_ses->compress_algorithm = cpu_to_le16(volume_info->compression);
Jeff Layton58fa0152012-05-01 17:41:16 -04002565 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002566 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2567 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002568 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002569 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2570 mutex_init(&tcp_ses->reconnect_mutex);
Jeff Layton9fa114f2012-11-26 11:09:57 -05002571 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2572 sizeof(tcp_ses->srcaddr));
2573 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2574 sizeof(tcp_ses->dstaddr));
Aurelien Aptelbcc88802019-09-20 04:32:20 +02002575 if (volume_info->use_client_guid)
2576 memcpy(tcp_ses->client_guid, volume_info->client_guid,
2577 SMB2_CLIENT_GUID_SIZE);
2578 else
2579 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05002580 /*
2581 * at this point we are the only ones with the pointer
2582 * to the struct since the kernel thread not created yet
2583 * no need to spinlock this init of tcpStatus or srv_count
2584 */
2585 tcp_ses->tcpStatus = CifsNew;
2586 ++tcp_ses->srv_count;
2587
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002588 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2589 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2590 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2591 else
2592 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07002593 if (tcp_ses->rdma) {
2594#ifndef CONFIG_CIFS_SMB_DIRECT
2595 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
2596 rc = -ENOENT;
2597 goto out_err_crypto_release;
2598#endif
2599 tcp_ses->smbd_conn = smbd_get_connection(
2600 tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
2601 if (tcp_ses->smbd_conn) {
2602 cifs_dbg(VFS, "RDMA transport established\n");
2603 rc = 0;
2604 goto smbd_connected;
2605 } else {
2606 rc = -ENOENT;
2607 goto out_err_crypto_release;
2608 }
2609 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002610 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05002611 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002612 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002613 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002614 }
Long Li2f894642017-11-22 17:38:34 -07002615smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05002616 /*
2617 * since we're in a cifs function already, we know that
2618 * this will succeed. No need for try_module_get().
2619 */
2620 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04002621 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05002622 tcp_ses, "cifsd");
2623 if (IS_ERR(tcp_ses->tsk)) {
2624 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05002625 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05002626 module_put(THIS_MODULE);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002627 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002628 }
Steve French563317e2019-09-08 23:22:02 -05002629 tcp_ses->min_offload = volume_info->min_offload;
Steve Frenchfd88ce92011-04-12 01:01:14 +00002630 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05002631
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02002632 tcp_ses->nr_targets = 1;
Steve French4f5c10f2019-09-03 21:18:49 -05002633 tcp_ses->ignore_signature = volume_info->ignore_signature;
Jeff Layton63c038c2008-12-01 18:41:46 -05002634 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302635 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002636 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302637 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002638
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302639 cifs_fscache_get_client_cookie(tcp_ses);
2640
Jeff Laytonc74093b2011-01-11 07:24:23 -05002641 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002642 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002643
Jeff Layton63c038c2008-12-01 18:41:46 -05002644 return tcp_ses;
2645
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002646out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002647 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002648
Rob Landleyf1d0c992011-01-22 15:44:05 -06002649 put_net(cifs_net_ns(tcp_ses));
2650
Jeff Layton63c038c2008-12-01 18:41:46 -05002651out_err:
2652 if (tcp_ses) {
Steve French8347a5c2009-10-06 18:31:29 +00002653 if (!IS_ERR(tcp_ses->hostname))
2654 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05002655 if (tcp_ses->ssocket)
2656 sock_release(tcp_ses->ssocket);
2657 kfree(tcp_ses);
2658 }
2659 return ERR_PTR(rc);
2660}
2661
Steve French96daf2b2011-05-27 04:34:02 +00002662static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002663{
Jeff Layton3f618222013-06-12 19:52:14 -05002664 if (vol->sectype != Unspecified &&
2665 vol->sectype != ses->sectype)
2666 return 0;
2667
Aurelien Aptelbcc88802019-09-20 04:32:20 +02002668 /*
2669 * If an existing session is limited to less channels than
2670 * requested, it should not be reused
2671 */
2672 if (ses->chan_max < vol->max_channels)
2673 return 0;
2674
Jeff Layton3f618222013-06-12 19:52:14 -05002675 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002676 case Kerberos:
Eric W. Biederman64ed39d2013-02-06 02:30:39 -08002677 if (!uid_eq(vol->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002678 return 0;
2679 break;
2680 default:
Jeff Layton04febab2012-01-17 16:09:15 -05002681 /* NULL username means anonymous session */
2682 if (ses->user_name == NULL) {
2683 if (!vol->nullauth)
2684 return 0;
2685 break;
2686 }
2687
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002688 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05002689 if (strncmp(ses->user_name,
2690 vol->username ? vol->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002691 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002692 return 0;
Jeff Layton08b37d52014-05-23 06:53:10 -04002693 if ((vol->username && strlen(vol->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002694 ses->password != NULL &&
2695 strncmp(ses->password,
2696 vol->password ? vol->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002697 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002698 return 0;
2699 }
2700 return 1;
2701}
2702
Aurelien Aptelb327a712018-01-24 13:46:10 +01002703/**
2704 * cifs_setup_ipc - helper to setup the IPC tcon for the session
2705 *
2706 * A new IPC connection is made and stored in the session
2707 * tcon_ipc. The IPC tcon has the same lifetime as the session.
2708 */
2709static int
2710cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
2711{
2712 int rc = 0, xid;
2713 struct cifs_tcon *tcon;
2714 struct nls_table *nls_codepage;
2715 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
2716 bool seal = false;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002717 struct TCP_Server_Info *server = ses->server;
Aurelien Aptelb327a712018-01-24 13:46:10 +01002718
2719 /*
2720 * If the mount request that resulted in the creation of the
2721 * session requires encryption, force IPC to be encrypted too.
2722 */
2723 if (volume_info->seal) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002724 if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
Aurelien Aptelb327a712018-01-24 13:46:10 +01002725 seal = true;
2726 else {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002727 cifs_server_dbg(VFS,
Aurelien Aptelb327a712018-01-24 13:46:10 +01002728 "IPC: server doesn't support encryption\n");
2729 return -EOPNOTSUPP;
2730 }
2731 }
2732
2733 tcon = tconInfoAlloc();
2734 if (tcon == NULL)
2735 return -ENOMEM;
2736
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002737 scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002738
2739 /* cannot fail */
2740 nls_codepage = load_nls_default();
2741
2742 xid = get_xid();
2743 tcon->ses = ses;
2744 tcon->ipc = true;
2745 tcon->seal = seal;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002746 rc = server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002747 free_xid(xid);
2748
2749 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002750 cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002751 tconInfoFree(tcon);
2752 goto out;
2753 }
2754
2755 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
2756
2757 ses->tcon_ipc = tcon;
2758out:
2759 unload_nls(nls_codepage);
2760 return rc;
2761}
2762
2763/**
2764 * cifs_free_ipc - helper to release the session IPC tcon
2765 *
2766 * Needs to be called everytime a session is destroyed
2767 */
2768static int
2769cifs_free_ipc(struct cifs_ses *ses)
2770{
2771 int rc = 0, xid;
2772 struct cifs_tcon *tcon = ses->tcon_ipc;
2773
2774 if (tcon == NULL)
2775 return 0;
2776
2777 if (ses->server->ops->tree_disconnect) {
2778 xid = get_xid();
2779 rc = ses->server->ops->tree_disconnect(xid, tcon);
2780 free_xid(xid);
2781 }
2782
2783 if (rc)
2784 cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
2785
2786 tconInfoFree(tcon);
2787 ses->tcon_ipc = NULL;
2788 return rc;
2789}
2790
Steve French96daf2b2011-05-27 04:34:02 +00002791static struct cifs_ses *
Jeff Layton4ff67b72010-07-06 20:43:02 -04002792cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793{
Steve French96daf2b2011-05-27 04:34:02 +00002794 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302796 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04002797 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002798 if (ses->status == CifsExiting)
2799 continue;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002800 if (!match_session(ses, vol))
2801 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05002802 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302803 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002804 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302806 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return NULL;
2808}
2809
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03002810void cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05002811{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002812 unsigned int rc, xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05002813 struct TCP_Server_Info *server = ses->server;
2814
Joe Perchesf96637b2013-05-04 22:12:25 -05002815 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002816
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302817 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002818 if (ses->status == CifsExiting) {
2819 spin_unlock(&cifs_tcp_ses_lock);
2820 return;
2821 }
Jeff Layton14fbf502008-11-14 13:53:46 -05002822 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302823 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002824 return;
2825 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002826 if (ses->status == CifsGood)
2827 ses->status = CifsExiting;
2828 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002829
Aurelien Aptelb327a712018-01-24 13:46:10 +01002830 cifs_free_ipc(ses);
2831
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002832 if (ses->status == CifsExiting && server->ops->logoff) {
2833 xid = get_xid();
2834 rc = server->ops->logoff(xid, ses);
2835 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002836 cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002837 __func__, rc);
2838 _free_xid(xid);
2839 }
2840
2841 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002842 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302843 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002844
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02002845 /* close any extra channels */
2846 if (ses->chan_count > 1) {
2847 int i;
2848
2849 for (i = 1; i < ses->chan_count; i++)
2850 cifs_put_tcp_session(ses->chans[i].server, 0);
2851 }
2852
Jeff Layton14fbf502008-11-14 13:53:46 -05002853 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002854 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05002855}
2856
Jeff Layton8a8798a2012-01-17 16:09:15 -05002857#ifdef CONFIG_KEYS
2858
Chen Gang057d6332013-07-19 09:01:36 +08002859/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2860#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05002861
2862/* Populate username and pw fields from keyring if possible */
2863static int
2864cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2865{
2866 int rc = 0;
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10002867 int is_domain = 0;
David Howells146aa8b2015-10-21 14:04:48 +01002868 const char *delim, *payload;
2869 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002870 ssize_t len;
2871 struct key *key;
2872 struct TCP_Server_Info *server = ses->server;
2873 struct sockaddr_in *sa;
2874 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01002875 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002876
2877 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2878 if (!desc)
2879 return -ENOMEM;
2880
2881 /* try to find an address key first */
2882 switch (server->dstaddr.ss_family) {
2883 case AF_INET:
2884 sa = (struct sockaddr_in *)&server->dstaddr;
2885 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2886 break;
2887 case AF_INET6:
2888 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2889 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2890 break;
2891 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002892 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2893 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002894 rc = -EINVAL;
2895 goto out_err;
2896 }
2897
Joe Perchesf96637b2013-05-04 22:12:25 -05002898 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07002899 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05002900 if (IS_ERR(key)) {
2901 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002902 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05002903 rc = PTR_ERR(key);
2904 goto out_err;
2905 }
2906
2907 /* didn't work, try to find a domain key */
2908 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05002909 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07002910 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05002911 if (IS_ERR(key)) {
2912 rc = PTR_ERR(key);
2913 goto out_err;
2914 }
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10002915 is_domain = 1;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002916 }
2917
2918 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00002919 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002920 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05002921 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002922 goto out_key_put;
2923 }
2924
2925 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01002926 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002927 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05002928 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002929 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002930 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2931 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002932 rc = -EINVAL;
2933 goto out_key_put;
2934 }
2935
2936 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002937 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002938 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2939 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002940 rc = -EINVAL;
2941 goto out_key_put;
2942 }
2943
2944 vol->username = kstrndup(payload, len, GFP_KERNEL);
2945 if (!vol->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002946 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2947 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002948 rc = -ENOMEM;
2949 goto out_key_put;
2950 }
Joe Perchesf96637b2013-05-04 22:12:25 -05002951 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002952
2953 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002954 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002955 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002956 rc = -EINVAL;
2957 kfree(vol->username);
2958 vol->username = NULL;
2959 goto out_key_put;
2960 }
2961
2962 ++delim;
2963 vol->password = kstrndup(delim, len, GFP_KERNEL);
2964 if (!vol->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002965 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2966 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002967 rc = -ENOMEM;
2968 kfree(vol->username);
2969 vol->username = NULL;
2970 goto out_key_put;
2971 }
2972
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10002973 /*
2974 * If we have a domain key then we must set the domainName in the
2975 * for the request.
2976 */
2977 if (is_domain && ses->domainName) {
2978 vol->domainname = kstrndup(ses->domainName,
2979 strlen(ses->domainName),
2980 GFP_KERNEL);
2981 if (!vol->domainname) {
Joe Perchesa0a30362020-04-14 22:42:53 -07002982 cifs_dbg(FYI, "Unable to allocate %zd bytes for domain\n",
2983 len);
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10002984 rc = -ENOMEM;
2985 kfree(vol->username);
2986 vol->username = NULL;
Waiman Long453431a2020-08-06 23:18:13 -07002987 kfree_sensitive(vol->password);
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10002988 vol->password = NULL;
2989 goto out_key_put;
2990 }
2991 }
2992
Jeff Layton8a8798a2012-01-17 16:09:15 -05002993out_key_put:
2994 up_read(&key->sem);
2995 key_put(key);
2996out_err:
2997 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05002998 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002999 return rc;
3000}
3001#else /* ! CONFIG_KEYS */
3002static inline int
3003cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
3004 struct cifs_ses *ses __attribute__((unused)))
3005{
3006 return -ENOSYS;
3007}
3008#endif /* CONFIG_KEYS */
3009
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003010/**
3011 * cifs_get_smb_ses - get a session matching @volume_info data from @server
3012 *
3013 * This function assumes it is being called from cifs_mount() where we
3014 * already got a server reference (server refcount +1). See
3015 * cifs_get_tcon() for refcount explanations.
3016 */
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03003017struct cifs_ses *
Jeff Layton36988c72010-04-24 07:57:43 -04003018cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
3019{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003020 int rc = -ENOMEM;
3021 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00003022 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003023 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3024 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04003025
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003026 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04003027
Jeff Layton4ff67b72010-07-06 20:43:02 -04003028 ses = cifs_find_smb_ses(server, volume_info);
Jeff Layton36988c72010-04-24 07:57:43 -04003029 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003030 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
3031 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04003032
Jeff Layton36988c72010-04-24 07:57:43 -04003033 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04003034 rc = cifs_negotiate_protocol(xid, ses);
3035 if (rc) {
3036 mutex_unlock(&ses->session_mutex);
3037 /* problem -- put our ses reference */
3038 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003039 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04003040 return ERR_PTR(rc);
3041 }
Jeff Layton36988c72010-04-24 07:57:43 -04003042 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003043 cifs_dbg(FYI, "Session needs reconnect\n");
Jeff Layton36988c72010-04-24 07:57:43 -04003044 rc = cifs_setup_session(xid, ses,
3045 volume_info->local_nls);
3046 if (rc) {
3047 mutex_unlock(&ses->session_mutex);
3048 /* problem -- put our reference */
3049 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003050 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04003051 return ERR_PTR(rc);
3052 }
3053 }
3054 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04003055
3056 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003057 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003058 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04003059 return ses;
3060 }
3061
Joe Perchesf96637b2013-05-04 22:12:25 -05003062 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04003063 ses = sesInfoAlloc();
3064 if (ses == NULL)
3065 goto get_ses_fail;
3066
3067 /* new SMB session uses our server ref */
3068 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003069 if (server->dstaddr.ss_family == AF_INET6)
3070 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04003071 else
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003072 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04003073
Steve French8727c8a2011-02-25 01:11:56 -06003074 if (volume_info->username) {
3075 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
3076 if (!ses->user_name)
3077 goto get_ses_fail;
3078 }
Jeff Layton36988c72010-04-24 07:57:43 -04003079
3080 /* volume_info->password freed at unmount */
3081 if (volume_info->password) {
3082 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
3083 if (!ses->password)
3084 goto get_ses_fail;
3085 }
3086 if (volume_info->domainname) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05003087 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
3088 if (!ses->domainName)
3089 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04003090 }
Germano Percossi39566442016-12-15 12:31:18 +05303091 if (volume_info->domainauto)
3092 ses->domainAuto = volume_info->domainauto;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04003093 ses->cred_uid = volume_info->cred_uid;
Jeff Layton36988c72010-04-24 07:57:43 -04003094 ses->linux_uid = volume_info->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00003095
Jeff Layton28e11bd2013-05-26 07:01:00 -04003096 ses->sectype = volume_info->sectype;
3097 ses->sign = volume_info->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04003098 mutex_lock(&ses->session_mutex);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003099
3100 /* add server as first channel */
3101 ses->chans[0].server = server;
3102 ses->chan_count = 1;
3103 ses->chan_max = volume_info->multichannel ? volume_info->max_channels:1;
3104
Jeff Layton198b5682010-04-24 07:57:48 -04003105 rc = cifs_negotiate_protocol(xid, ses);
3106 if (!rc)
3107 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003108
3109 /* each channel uses a different signing key */
3110 memcpy(ses->chans[0].signkey, ses->smb3signingkey,
3111 sizeof(ses->smb3signingkey));
3112
Jeff Layton36988c72010-04-24 07:57:43 -04003113 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00003114 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04003115 goto get_ses_fail;
3116
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02003117 /* success, put it on the list and add it as first channel */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303118 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04003119 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303120 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04003121
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003122 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01003123
3124 cifs_setup_ipc(ses, volume_info);
3125
Jeff Layton36988c72010-04-24 07:57:43 -04003126 return ses;
3127
3128get_ses_fail:
3129 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003130 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04003131 return ERR_PTR(rc);
3132}
3133
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003134static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003135{
3136 if (tcon->tidStatus == CifsExiting)
3137 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003138 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003139 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003140 if (tcon->seal != volume_info->seal)
3141 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003142 if (tcon->snapshot_time != volume_info->snapshot_time)
3143 return 0;
Steve Frenchca567eb2019-03-29 16:31:07 -05003144 if (tcon->handle_timeout != volume_info->handle_timeout)
3145 return 0;
Steve French3e7a02d2019-09-11 21:46:20 -05003146 if (tcon->no_lease != volume_info->no_lease)
3147 return 0;
Steve French82e93672020-05-19 03:06:57 -05003148 if (tcon->nodelete != volume_info->nodelete)
3149 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003150 return 1;
3151}
3152
Steve French96daf2b2011-05-27 04:34:02 +00003153static struct cifs_tcon *
Steve French8b217fe2016-11-11 22:36:20 -06003154cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
3156 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00003157 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303159 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003160 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00003161 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Paulo Alcantara65303de2020-04-20 19:42:57 -03003162#ifdef CONFIG_CIFS_DFS_UPCALL
3163 if (tcon->dfs_path)
3164 continue;
3165#endif
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003166 if (!match_tcon(tcon, volume_info))
Jeff Laytonf1987b42008-11-15 11:12:47 -05003167 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05003168 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303169 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 return tcon;
3171 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303172 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 return NULL;
3174}
3175
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003176void
Steve French96daf2b2011-05-27 04:34:02 +00003177cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05003178{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003179 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01003180 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05003181
Aurelien Aptelb327a712018-01-24 13:46:10 +01003182 /*
3183 * IPC tcon share the lifetime of their session and are
3184 * destroyed in the session put function
3185 */
3186 if (tcon == NULL || tcon->ipc)
3187 return;
3188
3189 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05003190 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303191 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003192 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303193 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003194 return;
3195 }
3196
3197 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303198 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003199
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003200 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003201 if (ses->server->ops->tree_disconnect)
3202 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003203 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003204
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303205 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00003206 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003207 cifs_put_smb_ses(ses);
3208}
3209
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003210/**
3211 * cifs_get_tcon - get a tcon matching @volume_info data from @ses
3212 *
3213 * - tcon refcount is the number of mount points using the tcon.
3214 * - ses refcount is the number of tcon using the session.
3215 *
3216 * 1. This function assumes it is being called from cifs_mount() where
3217 * we already got a session reference (ses refcount +1).
3218 *
3219 * 2. Since we're in the context of adding a mount point, the end
3220 * result should be either:
3221 *
3222 * a) a new tcon already allocated with refcount=1 (1 mount point) and
3223 * its session refcount incremented (1 new tcon). This +1 was
3224 * already done in (1).
3225 *
3226 * b) an existing tcon with refcount+1 (add a mount point to it) and
3227 * identical ses refcount (no new tcon). Because of (1) we need to
3228 * decrement the ses refcount.
3229 */
Steve French96daf2b2011-05-27 04:34:02 +00003230static struct cifs_tcon *
3231cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Jeff Laytond00c28d2010-04-24 07:57:44 -04003232{
3233 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00003234 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003235
Steve French8b217fe2016-11-11 22:36:20 -06003236 tcon = cifs_find_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003237 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003238 /*
3239 * tcon has refcount already incremented but we need to
3240 * decrement extra ses reference gotten by caller (case b)
3241 */
Joe Perchesf96637b2013-05-04 22:12:25 -05003242 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04003243 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003244 return tcon;
3245 }
3246
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003247 if (!ses->server->ops->tree_connect) {
3248 rc = -ENOSYS;
3249 goto out_fail;
3250 }
3251
Jeff Laytond00c28d2010-04-24 07:57:44 -04003252 tcon = tconInfoAlloc();
3253 if (tcon == NULL) {
3254 rc = -ENOMEM;
3255 goto out_fail;
3256 }
3257
Steve French8b217fe2016-11-11 22:36:20 -06003258 if (volume_info->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06003259 if (ses->server->vals->protocol_id == 0) {
3260 cifs_dbg(VFS,
3261 "Use SMB2 or later for snapshot mount option\n");
3262 rc = -EOPNOTSUPP;
3263 goto out_fail;
3264 } else
3265 tcon->snapshot_time = volume_info->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06003266 }
3267
Steve Frenchca567eb2019-03-29 16:31:07 -05003268 if (volume_info->handle_timeout) {
3269 if (ses->server->vals->protocol_id == 0) {
3270 cifs_dbg(VFS,
3271 "Use SMB2.1 or later for handle timeout option\n");
3272 rc = -EOPNOTSUPP;
3273 goto out_fail;
3274 } else
3275 tcon->handle_timeout = volume_info->handle_timeout;
3276 }
3277
Jeff Laytond00c28d2010-04-24 07:57:44 -04003278 tcon->ses = ses;
3279 if (volume_info->password) {
3280 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
3281 if (!tcon->password) {
3282 rc = -ENOMEM;
3283 goto out_fail;
3284 }
3285 }
3286
Steve French23657ad2018-04-22 15:14:58 -05003287 if (volume_info->seal) {
3288 if (ses->server->vals->protocol_id == 0) {
3289 cifs_dbg(VFS,
3290 "SMB3 or later required for encryption\n");
3291 rc = -EOPNOTSUPP;
3292 goto out_fail;
3293 } else if (tcon->ses->server->capabilities &
3294 SMB2_GLOBAL_CAP_ENCRYPTION)
3295 tcon->seal = true;
3296 else {
3297 cifs_dbg(VFS, "Encryption is not supported on share\n");
3298 rc = -EOPNOTSUPP;
3299 goto out_fail;
3300 }
3301 }
3302
Steve French8505c8b2018-06-18 14:01:59 -05003303 if (volume_info->linux_ext) {
3304 if (ses->server->posix_ext_supported) {
Steve Frenchb3266142018-05-20 23:41:10 -05003305 tcon->posix_extensions = true;
Joe Perchesa0a30362020-04-14 22:42:53 -07003306 pr_warn_once("SMB3.11 POSIX Extensions are experimental\n");
Steve French8505c8b2018-06-18 14:01:59 -05003307 } else {
Joe Perchesa0a30362020-04-14 22:42:53 -07003308 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions\n");
Steve French8505c8b2018-06-18 14:01:59 -05003309 rc = -EOPNOTSUPP;
3310 goto out_fail;
Steve French2fbb5642018-06-12 12:11:31 -05003311 }
Steve Frenchb3266142018-05-20 23:41:10 -05003312 }
Steve Frenchb3266142018-05-20 23:41:10 -05003313
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003314 /*
3315 * BB Do we need to wrap session_mutex around this TCon call and Unix
3316 * SetFS as we do on SessSetup and reconnect?
3317 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003318 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003319 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
3320 volume_info->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003321 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05003322 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003323 if (rc)
3324 goto out_fail;
3325
Steve Frenchb618f002015-11-03 09:15:03 -06003326 tcon->use_persistent = false;
3327 /* check if SMB2 or later, CIFS does not support persistent handles */
3328 if (volume_info->persistent) {
3329 if (ses->server->vals->protocol_id == 0) {
3330 cifs_dbg(VFS,
3331 "SMB3 or later required for persistent handles\n");
3332 rc = -EOPNOTSUPP;
3333 goto out_fail;
3334 } else if (ses->server->capabilities &
3335 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3336 tcon->use_persistent = true;
3337 else /* persistent handles requested but not supported */ {
3338 cifs_dbg(VFS,
3339 "Persistent handles not supported on share\n");
3340 rc = -EOPNOTSUPP;
3341 goto out_fail;
3342 }
3343 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
3344 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3345 && (volume_info->nopersistent == false)) {
3346 cifs_dbg(FYI, "enabling persistent handles\n");
3347 tcon->use_persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06003348 } else if (volume_info->resilient) {
3349 if (ses->server->vals->protocol_id == 0) {
3350 cifs_dbg(VFS,
3351 "SMB2.1 or later required for resilient handles\n");
3352 rc = -EOPNOTSUPP;
3353 goto out_fail;
3354 }
3355 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06003356 }
3357
Steve Frenchcae53f72019-09-03 17:49:46 -05003358 /* If the user really knows what they are doing they can override */
3359 if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
3360 if (volume_info->cache_ro)
3361 cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
3362 else if (volume_info->cache_rw)
3363 cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
3364 }
3365
Kenneth D'souza8fd6e1d2020-05-18 13:01:34 +05303366 if (volume_info->no_lease) {
3367 if (ses->server->vals->protocol_id == 0) {
3368 cifs_dbg(VFS,
3369 "SMB2 or later required for nolease option\n");
3370 rc = -EOPNOTSUPP;
3371 goto out_fail;
3372 } else
3373 tcon->no_lease = volume_info->no_lease;
3374 }
3375
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003376 /*
3377 * We can have only one retry value for a connection to a share so for
3378 * resources mounted more than once to the same server share the last
3379 * value passed in for the retry flag is used.
3380 */
Jeff Laytond00c28d2010-04-24 07:57:44 -04003381 tcon->retry = volume_info->retry;
3382 tcon->nocase = volume_info->nocase;
Steve French3c6e65e2020-10-21 00:15:42 -05003383 if (ses->server->capabilities & SMB2_GLOBAL_CAP_DIRECTORY_LEASING)
3384 tcon->nohandlecache = volume_info->nohandlecache;
3385 else
3386 tcon->nohandlecache = 1;
Steve French82e93672020-05-19 03:06:57 -05003387 tcon->nodelete = volume_info->nodelete;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003388 tcon->local_lease = volume_info->local_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07003389 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003390
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303391 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003392 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303393 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003394
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303395 cifs_fscache_get_super_cookie(tcon);
3396
Jeff Laytond00c28d2010-04-24 07:57:44 -04003397 return tcon;
3398
3399out_fail:
3400 tconInfoFree(tcon);
3401 return ERR_PTR(rc);
3402}
3403
Jeff Layton9d002df2010-10-06 19:51:11 -04003404void
3405cifs_put_tlink(struct tcon_link *tlink)
3406{
3407 if (!tlink || IS_ERR(tlink))
3408 return;
3409
3410 if (!atomic_dec_and_test(&tlink->tl_count) ||
3411 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
3412 tlink->tl_time = jiffies;
3413 return;
3414 }
3415
3416 if (!IS_ERR(tlink_tcon(tlink)))
3417 cifs_put_tcon(tlink_tcon(tlink));
3418 kfree(tlink);
3419 return;
3420}
Jeff Laytond00c28d2010-04-24 07:57:44 -04003421
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003422static int
3423compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3424{
3425 struct cifs_sb_info *old = CIFS_SB(sb);
3426 struct cifs_sb_info *new = mnt_data->cifs_sb;
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03003427 unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
3428 unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003429
3430 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
3431 return 0;
3432
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03003433 if (old->mnt_cifs_serverino_autodisabled)
3434 newflags &= ~CIFS_MOUNT_SERVER_INUM;
3435
3436 if (oldflags != newflags)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003437 return 0;
3438
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003439 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003440 * We want to share sb only if we don't specify an r/wsize or
3441 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003442 */
3443 if (new->wsize && new->wsize < old->wsize)
3444 return 0;
3445
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003446 if (new->rsize && new->rsize < old->rsize)
3447 return 0;
3448
Eric W. Biederman1f682332013-02-06 01:20:20 -08003449 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003450 return 0;
3451
3452 if (old->mnt_file_mode != new->mnt_file_mode ||
3453 old->mnt_dir_mode != new->mnt_dir_mode)
3454 return 0;
3455
3456 if (strcmp(old->local_nls->charset, new->local_nls->charset))
3457 return 0;
3458
3459 if (old->actimeo != new->actimeo)
3460 return 0;
3461
3462 return 1;
3463}
3464
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003465static int
3466match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3467{
3468 struct cifs_sb_info *old = CIFS_SB(sb);
3469 struct cifs_sb_info *new = mnt_data->cifs_sb;
Ronnie Sahlbergfe129262020-01-22 11:07:56 +10003470 bool old_set = (old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
3471 old->prepath;
3472 bool new_set = (new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH) &&
3473 new->prepath;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003474
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003475 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003476 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003477 else if (!old_set && !new_set)
3478 return 1;
3479
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003480 return 0;
3481}
3482
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003483int
3484cifs_match_super(struct super_block *sb, void *data)
3485{
3486 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
3487 struct smb_vol *volume_info;
3488 struct cifs_sb_info *cifs_sb;
3489 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00003490 struct cifs_ses *ses;
3491 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003492 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003493 int rc = 0;
3494
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003495 spin_lock(&cifs_tcp_ses_lock);
3496 cifs_sb = CIFS_SB(sb);
3497 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3498 if (IS_ERR(tlink)) {
3499 spin_unlock(&cifs_tcp_ses_lock);
3500 return rc;
3501 }
3502 tcon = tlink_tcon(tlink);
3503 ses = tcon->ses;
3504 tcp_srv = ses->server;
3505
3506 volume_info = mnt_data->vol;
3507
Jeff Layton9fa114f2012-11-26 11:09:57 -05003508 if (!match_server(tcp_srv, volume_info) ||
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003509 !match_session(ses, volume_info) ||
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003510 !match_tcon(tcon, volume_info) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003511 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003512 rc = 0;
3513 goto out;
3514 }
3515
3516 rc = compare_mount_options(sb, mnt_data);
3517out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003518 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04003519 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003520 return rc;
3521}
3522
Jeff Layton09e50d52008-07-23 10:11:19 -04003523#ifdef CONFIG_DEBUG_LOCK_ALLOC
3524static struct lock_class_key cifs_key[2];
3525static struct lock_class_key cifs_slock_key[2];
3526
3527static inline void
3528cifs_reclassify_socket4(struct socket *sock)
3529{
3530 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003531 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003532 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3533 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3534}
3535
3536static inline void
3537cifs_reclassify_socket6(struct socket *sock)
3538{
3539 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003540 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003541 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3542 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3543}
3544#else
3545static inline void
3546cifs_reclassify_socket4(struct socket *sock)
3547{
3548}
3549
3550static inline void
3551cifs_reclassify_socket6(struct socket *sock)
3552{
3553}
3554#endif
3555
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00003557static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
Steve French50c2f752007-07-13 00:33:32 +00003559 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560
Steve French50c2f752007-07-13 00:33:32 +00003561 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 /* mask a nibble at a time and encode */
3563 target[j] = 'A' + (0x0F & (source[i] >> 4));
3564 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00003565 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 }
3567
3568}
3569
Ben Greear3eb9a882010-09-01 17:06:02 -07003570static int
3571bind_socket(struct TCP_Server_Info *server)
3572{
3573 int rc = 0;
3574 if (server->srcaddr.ss_family != AF_UNSPEC) {
3575 /* Bind to the specified local IP address */
3576 struct socket *socket = server->ssocket;
3577 rc = socket->ops->bind(socket,
3578 (struct sockaddr *) &server->srcaddr,
3579 sizeof(server->srcaddr));
3580 if (rc < 0) {
3581 struct sockaddr_in *saddr4;
3582 struct sockaddr_in6 *saddr6;
3583 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3584 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3585 if (saddr6->sin6_family == AF_INET6)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003586 cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05003587 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003588 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003589 cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05003590 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003591 }
3592 }
3593 return rc;
3594}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595
3596static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003597ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598{
3599 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003600 /*
3601 * some servers require RFC1001 sessinit before sending
3602 * negprot - BB check reconnection in case where second
3603 * sessinit is sent but no second negprot
3604 */
3605 struct rfc1002_session_packet *ses_init_buf;
3606 struct smb_hdr *smb_buf;
3607 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3608 GFP_KERNEL);
3609 if (ses_init_buf) {
3610 ses_init_buf->trailer.session_req.called_len = 32;
3611
Colin Ian King997152f2016-01-25 16:25:54 +00003612 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003613 rfc1002mangle(ses_init_buf->trailer.
3614 session_req.called_name,
3615 server->server_RFC1001_name,
3616 RFC1001_NAME_LEN_WITH_NULL);
3617 else
3618 rfc1002mangle(ses_init_buf->trailer.
3619 session_req.called_name,
3620 DEFAULT_CIFS_CALLED_NAME,
3621 RFC1001_NAME_LEN_WITH_NULL);
3622
3623 ses_init_buf->trailer.session_req.calling_len = 32;
3624
3625 /*
3626 * calling name ends in null (byte 16) from old smb
3627 * convention.
3628 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05003629 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003630 rfc1002mangle(ses_init_buf->trailer.
3631 session_req.calling_name,
3632 server->workstation_RFC1001_name,
3633 RFC1001_NAME_LEN_WITH_NULL);
3634 else
3635 rfc1002mangle(ses_init_buf->trailer.
3636 session_req.calling_name,
3637 "LINUX_CIFS_CLNT",
3638 RFC1001_NAME_LEN_WITH_NULL);
3639
3640 ses_init_buf->trailer.session_req.scope1 = 0;
3641 ses_init_buf->trailer.session_req.scope2 = 0;
3642 smb_buf = (struct smb_hdr *)ses_init_buf;
3643
3644 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003645 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003646 rc = smb_send(server, smb_buf, 0x44);
3647 kfree(ses_init_buf);
3648 /*
3649 * RFC1001 layer in at least one server
3650 * requires very short break before negprot
3651 * presumably because not expecting negprot
3652 * to follow so fast. This is a simple
3653 * solution that works without
3654 * complicating the code and causes no
3655 * significant slowing down on mount
3656 * for everyone else
3657 */
3658 usleep_range(1000, 2000);
3659 }
3660 /*
3661 * else the negprot may still work without this
3662 * even though malloc failed
3663 */
3664
3665 return rc;
3666}
3667
3668static int
3669generic_ip_connect(struct TCP_Server_Info *server)
3670{
3671 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00003672 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003673 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003674 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003675 struct sockaddr *saddr;
3676
3677 saddr = (struct sockaddr *) &server->dstaddr;
3678
3679 if (server->dstaddr.ss_family == AF_INET6) {
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02003680 struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&server->dstaddr;
3681
3682 sport = ipv6->sin6_port;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003683 slen = sizeof(struct sockaddr_in6);
3684 sfamily = AF_INET6;
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02003685 cifs_dbg(FYI, "%s: connecting to [%pI6]:%d\n", __func__, &ipv6->sin6_addr,
3686 ntohs(sport));
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003687 } else {
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02003688 struct sockaddr_in *ipv4 = (struct sockaddr_in *)&server->dstaddr;
3689
3690 sport = ipv4->sin_port;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003691 slen = sizeof(struct sockaddr_in);
3692 sfamily = AF_INET;
Samuel Cabrerodef6e1d2020-10-16 11:54:55 +02003693 cifs_dbg(FYI, "%s: connecting to %pI4:%d\n", __func__, &ipv4->sin_addr,
3694 ntohs(sport));
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003695 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003697 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06003698 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3699 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 if (rc < 0) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003701 cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003702 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003705
3706 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05003707 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003708 server->ssocket = socket;
3709 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003710 if (sfamily == AF_INET6)
3711 cifs_reclassify_socket6(socket);
3712 else
3713 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 }
3715
Ben Greear3eb9a882010-09-01 17:06:02 -07003716 rc = bind_socket(server);
3717 if (rc < 0)
3718 return rc;
3719
Jeff Laytond5c56052008-12-01 18:42:33 -05003720 /*
3721 * Eventually check for other socket options to change from
3722 * the default. sock_setsockopt not used because it expects
3723 * user space buffer
3724 */
3725 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00003726 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00003727
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003728 /* make the bufsizes depend on wsize/rsize and max requests */
3729 if (server->noautotune) {
3730 if (socket->sk->sk_sndbuf < (200 * 1024))
3731 socket->sk->sk_sndbuf = 200 * 1024;
3732 if (socket->sk->sk_rcvbuf < (140 * 1024))
3733 socket->sk->sk_rcvbuf = 140 * 1024;
3734 }
3735
Christoph Hellwig12abc5e2020-05-28 07:12:19 +02003736 if (server->tcp_nodelay)
3737 tcp_sock_set_nodelay(socket->sk);
Steve French6a5fa2362010-01-01 01:28:43 +00003738
Joe Perchesf96637b2013-05-04 22:12:25 -05003739 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003740 socket->sk->sk_sndbuf,
3741 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3742
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03003743 rc = socket->ops->connect(socket, saddr, slen,
3744 server->noblockcnt ? O_NONBLOCK : 0);
Paulo Alcantara (SUSE)d532cc72019-10-10 12:31:58 -03003745 /*
3746 * When mounting SMB root file systems, we do not want to block in
3747 * connect. Otherwise bail out and then let cifs_reconnect() perform
3748 * reconnect failover - if possible.
3749 */
3750 if (server->noblockcnt && rc == -EINPROGRESS)
Paulo Alcantara (SUSE)8eecd1c2019-07-16 19:04:50 -03003751 rc = 0;
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003752 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003753 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003754 sock_release(socket);
3755 server->ssocket = NULL;
3756 return rc;
3757 }
3758
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003759 if (sport == htons(RFC1001_PORT))
3760 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00003761
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 return rc;
3763}
3764
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003765static int
3766ip_connect(struct TCP_Server_Info *server)
3767{
Steve French6da97912011-03-13 18:55:55 +00003768 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003769 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3770 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3771
3772 if (server->dstaddr.ss_family == AF_INET6)
3773 sport = &addr6->sin6_port;
3774 else
3775 sport = &addr->sin_port;
3776
3777 if (*sport == 0) {
3778 int rc;
3779
3780 /* try with 445 port at first */
3781 *sport = htons(CIFS_PORT);
3782
3783 rc = generic_ip_connect(server);
3784 if (rc >= 0)
3785 return rc;
3786
3787 /* if it failed, try with 139 port */
3788 *sport = htons(RFC1001_PORT);
3789 }
3790
3791 return generic_ip_connect(server);
3792}
3793
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003794void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Al Viro2c6292a2011-06-17 09:05:48 -04003795 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00003796{
3797 /* if we are reconnecting then should we check to see if
3798 * any requested capabilities changed locally e.g. via
3799 * remount but we can not do much about it here
3800 * if they have (even if we could detect it by the following)
3801 * Perhaps we could add a backpointer to array of sb from tcon
3802 * or if we change to make all sb to same share the same
3803 * sb as NFS - then we only have one backpointer to sb.
3804 * What if we wanted to mount the server share twice once with
3805 * and once without posixacls or posix paths? */
3806 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00003807
Steve Frenchc18c8422007-07-18 23:21:09 +00003808 if (vol_info && vol_info->no_linux_ext) {
3809 tcon->fsUnixInfo.Capability = 0;
3810 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05003811 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003812 return;
3813 } else if (vol_info)
3814 tcon->unix_ext = 1; /* Unix Extensions supported */
3815
3816 if (tcon->unix_ext == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003817 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003818 return;
3819 }
Steve French50c2f752007-07-13 00:33:32 +00003820
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003821 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00003822 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05003823 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve French8af18972007-02-14 04:42:51 +00003824 /* check for reconnect case in which we do not
3825 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003826 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00003827 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00003828 originally at mount time */
3829 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3830 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003831 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3832 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003833 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00003834 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003835 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003836 cifs_dbg(VFS, "possible reconnect error\n");
3837 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00003838 }
Steve French8af18972007-02-14 04:42:51 +00003839 }
Steve French50c2f752007-07-13 00:33:32 +00003840
Steve French6848b732011-05-26 18:38:54 +00003841 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003842 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00003843
Steve French8af18972007-02-14 04:42:51 +00003844 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00003845 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00003846 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003847 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003848 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003849 if (cifs_sb)
3850 cifs_sb->mnt_cifs_flags |=
3851 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00003852 }
3853
Steve French75865f8c2007-06-24 18:30:48 +00003854 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00003855 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003856 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003857 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003858 if (cifs_sb)
3859 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00003860 CIFS_MOUNT_POSIX_PATHS;
3861 }
Steve French50c2f752007-07-13 00:33:32 +00003862
Joe Perchesf96637b2013-05-04 22:12:25 -05003863 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00003864#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00003865 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003866 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003867 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003868 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003869 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003870 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003871 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003872 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003873 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003874 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003875 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003876 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003877 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003878 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00003879 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003880 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00003881 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003882 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00003883#endif /* CIFS_DEBUG2 */
3884 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00003885 if (vol_info == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003886 cifs_dbg(FYI, "resetting capabilities failed\n");
Steve French442aa312007-09-24 20:25:46 +00003887 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05003888 cifs_dbg(VFS, "Negotiating Unix capabilities with the server failed. Consider mounting with the Unix Extensions disabled if problems are found by specifying the nounix mount option.\n");
Steve French5a44b312007-09-20 15:16:24 +00003889
Steve French8af18972007-02-14 04:42:51 +00003890 }
3891 }
3892}
3893
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003894int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003895 struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003896{
Jeff Layton2de970f2010-10-06 19:51:12 -04003897 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3898
Al Viro2ced6f62011-06-17 09:20:04 -04003899 spin_lock_init(&cifs_sb->tlink_tree_lock);
3900 cifs_sb->tlink_tree = RB_ROOT;
3901
Steve Frenche8506d22019-02-28 21:32:15 -06003902 cifs_sb->bsize = pvolume_info->bsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003903 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003904 * Temporarily set r/wsize for matching superblock. If we end up using
3905 * new sb then client will later negotiate it downward if needed.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003906 */
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003907 cifs_sb->rsize = pvolume_info->rsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003908 cifs_sb->wsize = pvolume_info->wsize;
3909
Steve French3b795212008-11-13 19:45:32 +00003910 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3911 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3912 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3913 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
Frank Sorensonf52aa792020-02-12 15:31:48 -06003914 cifs_dbg(FYI, "file mode: %04ho dir mode: %04ho\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05003915 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
Steve French3b795212008-11-13 19:45:32 +00003916
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303917 cifs_sb->actimeo = pvolume_info->actimeo;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003918 cifs_sb->local_nls = pvolume_info->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303919
Aurelien Aptel83930722018-09-20 18:10:25 -07003920 if (pvolume_info->nodfs)
3921 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_DFS;
Steve French3b795212008-11-13 19:45:32 +00003922 if (pvolume_info->noperm)
3923 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3924 if (pvolume_info->setuids)
3925 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
Steve French95932652016-09-23 01:36:34 -05003926 if (pvolume_info->setuidfromacl)
3927 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
Steve French3b795212008-11-13 19:45:32 +00003928 if (pvolume_info->server_ino)
3929 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3930 if (pvolume_info->remap)
Steve French2baa2682014-09-27 02:19:01 -05003931 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3932 if (pvolume_info->sfu_remap)
Steve French3b795212008-11-13 19:45:32 +00003933 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3934 if (pvolume_info->no_xattr)
3935 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3936 if (pvolume_info->sfu_emul)
3937 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3938 if (pvolume_info->nobrl)
3939 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve French3d4ef9a2018-04-25 22:19:09 -05003940 if (pvolume_info->nohandlecache)
3941 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
Steve Frenchbe652442009-02-23 15:21:59 +00003942 if (pvolume_info->nostrictsync)
Steve French4717bed2009-02-24 14:44:19 +00003943 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
Steve French13a6e422008-12-02 17:24:33 +00003944 if (pvolume_info->mand_lock)
3945 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003946 if (pvolume_info->rwpidforward)
3947 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
Steve French412094a2019-06-24 02:01:42 -05003948 if (pvolume_info->mode_ace)
3949 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MODE_FROM_SID;
Steve French3b795212008-11-13 19:45:32 +00003950 if (pvolume_info->cifs_acl)
3951 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003952 if (pvolume_info->backupuid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003953 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003954 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3955 }
3956 if (pvolume_info->backupgid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003957 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003958 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3959 }
Steve French3b795212008-11-13 19:45:32 +00003960 if (pvolume_info->override_uid)
3961 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3962 if (pvolume_info->override_gid)
3963 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3964 if (pvolume_info->dynperm)
3965 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05303966 if (pvolume_info->fsc)
3967 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
Jeff Layton0eb8a132010-10-06 19:51:12 -04003968 if (pvolume_info->multiuser)
3969 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3970 CIFS_MOUNT_NO_PERM);
Pavel Shilovskyd39454f2011-01-24 14:16:35 -05003971 if (pvolume_info->strict_io)
3972 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
Steve French3b795212008-11-13 19:45:32 +00003973 if (pvolume_info->direct_io) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003974 cifs_dbg(FYI, "mounting share using direct i/o\n");
Steve French3b795212008-11-13 19:45:32 +00003975 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3976 }
Steve French83bbfa72019-08-27 23:58:54 -05003977 if (pvolume_info->cache_ro) {
3978 cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
3979 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
Steve French41e033f2019-08-30 02:12:41 -05003980 } else if (pvolume_info->cache_rw) {
3981 cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
3982 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
3983 CIFS_MOUNT_RW_CACHE);
Steve French83bbfa72019-08-27 23:58:54 -05003984 }
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003985 if (pvolume_info->mfsymlinks) {
3986 if (pvolume_info->sfu_emul) {
Steve Frenchdb8b6312014-09-22 05:13:55 -05003987 /*
3988 * Our SFU ("Services for Unix" emulation does not allow
3989 * creating symlinks but does allow reading existing SFU
3990 * symlinks (it does allow both creating and reading SFU
3991 * style mknod and FIFOs though). When "mfsymlinks" and
3992 * "sfu" are both enabled at the same time, it allows
3993 * reading both types of symlinks, but will only create
3994 * them with mfsymlinks format. This allows better
3995 * Apple compatibility (probably better for Samba too)
3996 * while still recognizing old Windows style symlinks.
3997 */
3998 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003999 }
Steve Frenchdb8b6312014-09-22 05:13:55 -05004000 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
Stefan Metzmacher736a33202010-07-30 14:56:00 +02004001 }
Steve French3b795212008-11-13 19:45:32 +00004002
4003 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05004004 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01004005
4006 if (pvolume_info->prepath) {
4007 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
4008 if (cifs_sb->prepath == NULL)
4009 return -ENOMEM;
Shyam Prasad Nb8b65312021-02-11 03:26:54 -08004010 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01004011 }
4012
4013 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04004014}
4015
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004016void
4017cifs_cleanup_volume_info_contents(struct smb_vol *volume_info)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004018{
Sean Finneyb9468452011-04-11 13:19:32 +00004019 kfree(volume_info->username);
Waiman Long453431a2020-08-06 23:18:13 -07004020 kfree_sensitive(volume_info->password);
Jesper Juhl95c75452011-08-27 18:58:34 +02004021 kfree(volume_info->UNC);
Sean Finneyb9468452011-04-11 13:19:32 +00004022 kfree(volume_info->domainname);
4023 kfree(volume_info->iocharset);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004024 kfree(volume_info->prepath);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004025}
4026
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004027void
4028cifs_cleanup_volume_info(struct smb_vol *volume_info)
4029{
4030 if (!volume_info)
4031 return;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004032 cifs_cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004033 kfree(volume_info);
4034}
4035
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004036/* Release all succeed connections */
4037static inline void mount_put_conns(struct cifs_sb_info *cifs_sb,
4038 unsigned int xid,
4039 struct TCP_Server_Info *server,
4040 struct cifs_ses *ses, struct cifs_tcon *tcon)
4041{
4042 int rc = 0;
4043
4044 if (tcon)
4045 cifs_put_tcon(tcon);
4046 else if (ses)
4047 cifs_put_smb_ses(ses);
4048 else if (server)
4049 cifs_put_tcp_session(server, 0);
4050 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
4051 free_xid(xid);
4052}
4053
4054/* Get connections for tcp, ses and tcon */
4055static int mount_get_conns(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
4056 unsigned int *xid,
4057 struct TCP_Server_Info **nserver,
4058 struct cifs_ses **nses, struct cifs_tcon **ntcon)
4059{
4060 int rc = 0;
4061 struct TCP_Server_Info *server;
4062 struct cifs_ses *ses;
4063 struct cifs_tcon *tcon;
4064
4065 *nserver = NULL;
4066 *nses = NULL;
4067 *ntcon = NULL;
4068
4069 *xid = get_xid();
4070
4071 /* get a reference to a tcp session */
4072 server = cifs_get_tcp_session(vol);
4073 if (IS_ERR(server)) {
4074 rc = PTR_ERR(server);
4075 return rc;
4076 }
4077
4078 *nserver = server;
4079
4080 if ((vol->max_credits < 20) || (vol->max_credits > 60000))
4081 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
4082 else
4083 server->max_credits = vol->max_credits;
4084
4085 /* get a reference to a SMB session */
4086 ses = cifs_get_smb_ses(server, vol);
4087 if (IS_ERR(ses)) {
4088 rc = PTR_ERR(ses);
4089 return rc;
4090 }
4091
4092 *nses = ses;
4093
4094 if ((vol->persistent == true) && (!(ses->server->capabilities &
4095 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10004096 cifs_server_dbg(VFS, "persistent handles not supported by server\n");
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004097 return -EOPNOTSUPP;
4098 }
4099
4100 /* search for existing tcon to this server share */
4101 tcon = cifs_get_tcon(ses, vol);
4102 if (IS_ERR(tcon)) {
4103 rc = PTR_ERR(tcon);
4104 return rc;
4105 }
4106
4107 *ntcon = tcon;
4108
4109 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4110 if (tcon->posix_extensions)
4111 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
4112
4113 /* tell server which Unix caps we support */
4114 if (cap_unix(tcon->ses)) {
4115 /*
4116 * reset of caps checks mount to see if unix extensions disabled
4117 * for just this mount.
4118 */
4119 reset_cifs_unix_caps(*xid, tcon, cifs_sb, vol);
4120 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
4121 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
4122 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP))
4123 return -EACCES;
4124 } else
4125 tcon->unix_ext = 0; /* server does not support them */
4126
4127 /* do not care if a following call succeed - informational */
Steve French1981eba2019-08-29 22:33:38 -05004128 if (!tcon->pipe && server->ops->qfs_tcon) {
Amir Goldstein0f060932020-02-03 21:46:43 +02004129 server->ops->qfs_tcon(*xid, tcon, cifs_sb);
Steve French1981eba2019-08-29 22:33:38 -05004130 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
4131 if (tcon->fsDevInfo.DeviceCharacteristics &
Steve French52870d52019-10-01 21:25:46 -05004132 cpu_to_le32(FILE_READ_ONLY_DEVICE))
Steve French1981eba2019-08-29 22:33:38 -05004133 cifs_dbg(VFS, "mounted to read only share\n");
Steve French41e033f2019-08-30 02:12:41 -05004134 else if ((cifs_sb->mnt_cifs_flags &
4135 CIFS_MOUNT_RW_CACHE) == 0)
Steve French1981eba2019-08-29 22:33:38 -05004136 cifs_dbg(VFS, "read only mount of RW share\n");
Steve French41e033f2019-08-30 02:12:41 -05004137 /* no need to log a RW mount of a typical RW share */
Steve French1981eba2019-08-29 22:33:38 -05004138 }
4139 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004140
4141 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, vol);
4142 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, vol);
4143
4144 return 0;
4145}
4146
4147static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
4148 struct cifs_tcon *tcon)
4149{
4150 struct tcon_link *tlink;
4151
4152 /* hang the tcon off of the superblock */
4153 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4154 if (tlink == NULL)
4155 return -ENOMEM;
4156
4157 tlink->tl_uid = ses->linux_uid;
4158 tlink->tl_tcon = tcon;
4159 tlink->tl_time = jiffies;
4160 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
4161 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4162
4163 cifs_sb->master_tlink = tlink;
4164 spin_lock(&cifs_sb->tlink_tree_lock);
4165 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4166 spin_unlock(&cifs_sb->tlink_tree_lock);
4167
4168 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
4169 TLINK_IDLE_EXPIRE);
4170 return 0;
4171}
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004172
Steve French2d6d5892009-04-09 00:36:44 +00004173#ifdef CONFIG_CIFS_DFS_UPCALL
Steve French6d3ea7e2012-11-28 22:34:41 -06004174/*
4175 * cifs_build_path_to_root returns full path to root when we do not have an
4176 * exiting connection (tcon)
4177 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004178static char *
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004179build_unc_path_to_root(const struct smb_vol *vol,
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004180 const struct cifs_sb_info *cifs_sb, bool useppath)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004181{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004182 char *full_path, *pos;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004183 unsigned int pplen = useppath && vol->prepath ?
4184 strlen(vol->prepath) + 1 : 0;
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004185 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004186
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004187 if (unc_len > MAX_TREE_SIZE)
4188 return ERR_PTR(-EINVAL);
4189
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004190 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004191 if (full_path == NULL)
4192 return ERR_PTR(-ENOMEM);
4193
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004194 memcpy(full_path, vol->UNC, unc_len);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004195 pos = full_path + unc_len;
4196
4197 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04004198 *pos = CIFS_DIR_SEP(cifs_sb);
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004199 memcpy(pos + 1, vol->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004200 pos += pplen;
4201 }
4202
4203 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00004204 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05004205 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004206 return full_path;
4207}
Sean Finneydd613942011-04-11 13:19:30 +00004208
Paulo Alcantara1c780222018-11-14 16:24:03 -02004209/**
4210 * expand_dfs_referral - Perform a dfs referral query and update the cifs_sb
4211 *
Sean Finneydd613942011-04-11 13:19:30 +00004212 *
Sean Finney046462a2011-04-11 13:19:33 +00004213 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
4214 * to a string containing updated options for the submount. Otherwise it
4215 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00004216 *
4217 * Returns the rc from get_dfs_path to the caller, which can be used to
4218 * determine whether there were referrals.
4219 */
4220static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004221expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Sean Finneydd613942011-04-11 13:19:30 +00004222 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004223 char *ref_path)
Sean Finneydd613942011-04-11 13:19:30 +00004224{
4225 int rc;
Paulo Alcantara1c780222018-11-14 16:24:03 -02004226 struct dfs_info3_param referral = {0};
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004227 char *full_path = NULL, *mdata = NULL;
Sean Finneydd613942011-04-11 13:19:30 +00004228
Aurelien Aptel83930722018-09-20 18:10:25 -07004229 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
4230 return -EREMOTE;
4231
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004232 full_path = build_unc_path_to_root(volume_info, cifs_sb, true);
Sean Finneydd613942011-04-11 13:19:30 +00004233 if (IS_ERR(full_path))
4234 return PTR_ERR(full_path);
4235
Paulo Alcantara1c780222018-11-14 16:24:03 -02004236 rc = dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
4237 ref_path, &referral, NULL);
4238 if (!rc) {
Sean Finneydd613942011-04-11 13:19:30 +00004239 char *fake_devname = NULL;
4240
4241 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
Paulo Alcantara1c780222018-11-14 16:24:03 -02004242 full_path + 1, &referral,
Sean Finneydd613942011-04-11 13:19:30 +00004243 &fake_devname);
Paulo Alcantara1c780222018-11-14 16:24:03 -02004244 free_dfs_info_param(&referral);
Sean Finney046462a2011-04-11 13:19:33 +00004245
Sean Finneydd613942011-04-11 13:19:30 +00004246 if (IS_ERR(mdata)) {
4247 rc = PTR_ERR(mdata);
4248 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004249 } else {
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004250 cifs_cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004251 rc = cifs_setup_volume_info(volume_info, mdata,
Paulo Alcantara1c780222018-11-14 16:24:03 -02004252 fake_devname, false);
Sean Finneydd613942011-04-11 13:19:30 +00004253 }
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004254 kfree(fake_devname);
4255 kfree(cifs_sb->mountdata);
Sean Finney046462a2011-04-11 13:19:33 +00004256 cifs_sb->mountdata = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00004257 }
4258 kfree(full_path);
4259 return rc;
4260}
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004261
4262static inline int get_next_dfs_tgt(const char *path,
4263 struct dfs_cache_tgt_list *tgt_list,
4264 struct dfs_cache_tgt_iterator **tgt_it)
4265{
4266 if (!*tgt_it)
4267 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
4268 else
4269 *tgt_it = dfs_cache_get_next_tgt(tgt_list, *tgt_it);
4270 return !*tgt_it ? -EHOSTDOWN : 0;
4271}
4272
4273static int update_vol_info(const struct dfs_cache_tgt_iterator *tgt_it,
4274 struct smb_vol *fake_vol, struct smb_vol *vol)
4275{
4276 const char *tgt = dfs_cache_get_tgt_name(tgt_it);
4277 int len = strlen(tgt) + 2;
4278 char *new_unc;
4279
4280 new_unc = kmalloc(len, GFP_KERNEL);
4281 if (!new_unc)
4282 return -ENOMEM;
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +10004283 scnprintf(new_unc, len, "\\%s", tgt);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004284
4285 kfree(vol->UNC);
4286 vol->UNC = new_unc;
4287
4288 if (fake_vol->prepath) {
4289 kfree(vol->prepath);
4290 vol->prepath = fake_vol->prepath;
4291 fake_vol->prepath = NULL;
4292 }
4293 memcpy(&vol->dstaddr, &fake_vol->dstaddr, sizeof(vol->dstaddr));
4294
4295 return 0;
4296}
4297
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004298static int setup_dfs_tgt_conn(const char *path, const char *full_path,
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004299 const struct dfs_cache_tgt_iterator *tgt_it,
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004300 struct cifs_sb_info *cifs_sb, struct smb_vol *vol, unsigned int *xid,
4301 struct TCP_Server_Info **server, struct cifs_ses **ses,
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004302 struct cifs_tcon **tcon)
4303{
4304 int rc;
4305 struct dfs_info3_param ref = {0};
4306 char *mdata = NULL, *fake_devname = NULL;
Steve Frenchd0959b02019-10-05 10:53:58 -05004307 struct smb_vol fake_vol = {NULL};
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004308
4309 cifs_dbg(FYI, "%s: dfs path: %s\n", __func__, path);
4310
4311 rc = dfs_cache_get_tgt_referral(path, tgt_it, &ref);
4312 if (rc)
4313 return rc;
4314
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004315 mdata = cifs_compose_mount_options(cifs_sb->mountdata, full_path + 1, &ref, &fake_devname);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004316 free_dfs_info_param(&ref);
4317
4318 if (IS_ERR(mdata)) {
4319 rc = PTR_ERR(mdata);
4320 mdata = NULL;
4321 } else {
4322 cifs_dbg(FYI, "%s: fake_devname: %s\n", __func__, fake_devname);
4323 rc = cifs_setup_volume_info(&fake_vol, mdata, fake_devname,
4324 false);
4325 }
4326 kfree(mdata);
4327 kfree(fake_devname);
4328
4329 if (!rc) {
4330 /*
4331 * We use a 'fake_vol' here because we need pass it down to the
4332 * mount_{get,put} functions to test connection against new DFS
4333 * targets.
4334 */
4335 mount_put_conns(cifs_sb, *xid, *server, *ses, *tcon);
4336 rc = mount_get_conns(&fake_vol, cifs_sb, xid, server, ses,
4337 tcon);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004338 if (!rc || (*server && *ses)) {
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004339 /*
4340 * We were able to connect to new target server.
4341 * Update current volume info with new target server.
4342 */
4343 rc = update_vol_info(tgt_it, &fake_vol, vol);
4344 }
4345 }
4346 cifs_cleanup_volume_info_contents(&fake_vol);
4347 return rc;
4348}
4349
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004350static int do_dfs_failover(const char *path, const char *full_path, struct cifs_sb_info *cifs_sb,
4351 struct smb_vol *vol, struct cifs_ses *root_ses, unsigned int *xid,
4352 struct TCP_Server_Info **server, struct cifs_ses **ses,
4353 struct cifs_tcon **tcon)
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004354{
4355 int rc;
4356 struct dfs_cache_tgt_list tgt_list;
4357 struct dfs_cache_tgt_iterator *tgt_it = NULL;
4358
4359 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
4360 return -EOPNOTSUPP;
4361
4362 rc = dfs_cache_noreq_find(path, NULL, &tgt_list);
4363 if (rc)
4364 return rc;
4365
4366 for (;;) {
4367 /* Get next DFS target server - if any */
4368 rc = get_next_dfs_tgt(path, &tgt_list, &tgt_it);
4369 if (rc)
4370 break;
4371 /* Connect to next DFS target */
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004372 rc = setup_dfs_tgt_conn(path, full_path, tgt_it, cifs_sb, vol, xid, server, ses,
4373 tcon);
4374 if (!rc || (*server && *ses))
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004375 break;
4376 }
4377 if (!rc) {
4378 /*
4379 * Update DFS target hint in DFS referral cache with the target
4380 * server we successfully reconnected to.
4381 */
4382 rc = dfs_cache_update_tgthint(*xid, root_ses ? root_ses : *ses,
4383 cifs_sb->local_nls,
4384 cifs_remap(cifs_sb), path,
4385 tgt_it);
4386 }
4387 dfs_cache_free_tgts(&tgt_list);
4388 return rc;
4389}
Steve French2d6d5892009-04-09 00:36:44 +00004390#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004391
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03004392int
Jeff Layton04db79b2011-07-06 08:10:38 -04004393cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
Steve Frenchc7c137b2018-06-06 17:59:29 -05004394 const char *devname, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395{
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004396 int rc = 0;
Sean Finneydd613942011-04-11 13:19:30 +00004397
Steve Frenchc7c137b2018-06-06 17:59:29 -05004398 if (cifs_parse_mount_options(mount_data, devname, volume_info, is_smb3))
Jeff Layton04db79b2011-07-06 08:10:38 -04004399 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
Jeff Layton7586b762008-12-01 18:41:49 -05004401 if (volume_info->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004402 cifs_dbg(FYI, "Anonymous login\n");
Jeff Layton04febab2012-01-17 16:09:15 -05004403 kfree(volume_info->username);
4404 volume_info->username = NULL;
Jeff Layton7586b762008-12-01 18:41:49 -05004405 } else if (volume_info->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 /* BB fixme parse for domain name here */
Joe Perchesf96637b2013-05-04 22:12:25 -05004407 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05004409 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00004410 /* In userspace mount helper we can get user name from alternate
4411 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04004412 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 }
4414
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 /* this is needed for ASCII cp to Unicode converts */
Jeff Layton7586b762008-12-01 18:41:49 -05004416 if (volume_info->iocharset == NULL) {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04004417 /* load_nls_default cannot return null */
4418 volume_info->local_nls = load_nls_default();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419 } else {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04004420 volume_info->local_nls = load_nls(volume_info->iocharset);
4421 if (volume_info->local_nls == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004422 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00004423 volume_info->iocharset);
Jeff Layton04db79b2011-07-06 08:10:38 -04004424 return -ELIBACC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 }
4426 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004427
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004428 return rc;
Jeff Layton04db79b2011-07-06 08:10:38 -04004429}
4430
4431struct smb_vol *
Steve Frenchc7c137b2018-06-06 17:59:29 -05004432cifs_get_volume_info(char *mount_data, const char *devname, bool is_smb3)
Jeff Layton04db79b2011-07-06 08:10:38 -04004433{
4434 int rc;
4435 struct smb_vol *volume_info;
4436
Jeff Layton6ee95422012-11-26 11:09:57 -05004437 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
Jeff Layton04db79b2011-07-06 08:10:38 -04004438 if (!volume_info)
4439 return ERR_PTR(-ENOMEM);
4440
Steve Frenchc7c137b2018-06-06 17:59:29 -05004441 rc = cifs_setup_volume_info(volume_info, mount_data, devname, is_smb3);
Jeff Layton04db79b2011-07-06 08:10:38 -04004442 if (rc) {
4443 cifs_cleanup_volume_info(volume_info);
4444 volume_info = ERR_PTR(rc);
4445 }
4446
4447 return volume_info;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004448}
4449
Aurelien Aptela6b50582016-05-25 19:59:09 +02004450static int
4451cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
4452 unsigned int xid,
4453 struct cifs_tcon *tcon,
4454 struct cifs_sb_info *cifs_sb,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10004455 char *full_path,
4456 int added_treename)
Aurelien Aptela6b50582016-05-25 19:59:09 +02004457{
4458 int rc;
4459 char *s;
4460 char sep, tmp;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10004461 int skip = added_treename ? 1 : 0;
Aurelien Aptela6b50582016-05-25 19:59:09 +02004462
4463 sep = CIFS_DIR_SEP(cifs_sb);
4464 s = full_path;
4465
4466 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
4467 while (rc == 0) {
4468 /* skip separators */
4469 while (*s == sep)
4470 s++;
4471 if (!*s)
4472 break;
4473 /* next separator */
4474 while (*s && *s != sep)
4475 s++;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10004476 /*
4477 * if the treename is added, we then have to skip the first
4478 * part within the separators
4479 */
4480 if (skip) {
4481 skip = 0;
4482 continue;
4483 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02004484 /*
4485 * temporarily null-terminate the path at the end of
4486 * the current component
4487 */
4488 tmp = *s;
4489 *s = 0;
4490 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4491 full_path);
4492 *s = tmp;
4493 }
4494 return rc;
4495}
4496
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004497/*
4498 * Check if path is remote (e.g. a DFS share). Return -EREMOTE if it is,
4499 * otherwise 0.
4500 */
4501static int is_path_remote(struct cifs_sb_info *cifs_sb, struct smb_vol *vol,
4502 const unsigned int xid,
4503 struct TCP_Server_Info *server,
4504 struct cifs_tcon *tcon)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004505{
Jeff Layton1daaae82012-03-21 06:30:40 -04004506 int rc;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004507 char *full_path;
4508
4509 if (!server->ops->is_path_accessible)
4510 return -EOPNOTSUPP;
4511
4512 /*
4513 * cifs_build_path_to_root works only when we have a valid tcon
4514 */
4515 full_path = cifs_build_path_to_root(vol, cifs_sb, tcon,
4516 tcon->Flags & SMB_SHARE_IS_IN_DFS);
4517 if (full_path == NULL)
4518 return -ENOMEM;
4519
4520 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
4521
4522 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4523 full_path);
4524 if (rc != 0 && rc != -EREMOTE) {
4525 kfree(full_path);
4526 return rc;
4527 }
4528
4529 if (rc != -EREMOTE) {
4530 rc = cifs_are_all_path_components_accessible(server, xid, tcon,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10004531 cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004532 if (rc != 0) {
Joe Perchesa0a30362020-04-14 22:42:53 -07004533 cifs_server_dbg(VFS, "cannot query dirs between root and final path, enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004534 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4535 rc = 0;
4536 }
4537 }
4538
4539 kfree(full_path);
4540 return rc;
4541}
4542
4543#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004544static void set_root_ses(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
4545 struct cifs_ses **root_ses)
Paulo Alcantara (SUSE)5bb30a42019-11-22 12:30:56 -03004546{
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004547 if (ses) {
4548 spin_lock(&cifs_tcp_ses_lock);
4549 ses->ses_count++;
Aurelien Aptel59463eb2020-12-03 19:46:08 +01004550 if (ses->tcon_ipc)
4551 ses->tcon_ipc->remap = cifs_remap(cifs_sb);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004552 spin_unlock(&cifs_tcp_ses_lock);
4553 }
4554 *root_ses = ses;
4555}
4556
4557static void put_root_ses(struct cifs_ses *ses)
4558{
4559 if (ses)
4560 cifs_put_smb_ses(ses);
4561}
4562
4563/* Check if a path component is remote and then update @dfs_path accordingly */
4564static int check_dfs_prepath(struct cifs_sb_info *cifs_sb, struct smb_vol *vol,
4565 const unsigned int xid, struct TCP_Server_Info *server,
4566 struct cifs_tcon *tcon, char **dfs_path)
4567{
4568 char *path, *s;
4569 char sep = CIFS_DIR_SEP(cifs_sb), tmp;
4570 char *npath;
4571 int rc = 0;
4572 int added_treename = tcon->Flags & SMB_SHARE_IS_IN_DFS;
4573 int skip = added_treename;
4574
4575 path = cifs_build_path_to_root(vol, cifs_sb, tcon, added_treename);
4576 if (!path)
4577 return -ENOMEM;
4578
4579 /*
4580 * Walk through the path components in @path and check if they're accessible. In case any of
4581 * the components is -EREMOTE, then update @dfs_path with the next DFS referral request path
4582 * (NOT including the remaining components).
4583 */
4584 s = path;
4585 do {
4586 /* skip separators */
4587 while (*s && *s == sep)
4588 s++;
4589 if (!*s)
4590 break;
4591 /* next separator */
4592 while (*s && *s != sep)
4593 s++;
4594 /*
4595 * if the treename is added, we then have to skip the first
4596 * part within the separators
4597 */
4598 if (skip) {
4599 skip = 0;
4600 continue;
4601 }
4602 tmp = *s;
4603 *s = 0;
4604 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, path);
4605 if (rc && rc == -EREMOTE) {
4606 struct smb_vol v = {NULL};
4607 /* if @path contains a tree name, skip it in the prefix path */
4608 if (added_treename) {
4609 rc = cifs_parse_devname(path, &v);
4610 if (rc)
4611 break;
4612 rc = -EREMOTE;
4613 npath = build_unc_path_to_root(&v, cifs_sb, true);
4614 cifs_cleanup_volume_info_contents(&v);
4615 } else {
4616 v.UNC = vol->UNC;
4617 v.prepath = path + 1;
4618 npath = build_unc_path_to_root(&v, cifs_sb, true);
4619 }
4620 if (IS_ERR(npath)) {
4621 rc = PTR_ERR(npath);
4622 break;
4623 }
4624 kfree(*dfs_path);
4625 *dfs_path = npath;
4626 }
4627 *s = tmp;
4628 } while (rc == 0);
4629
4630 kfree(path);
4631 return rc;
Paulo Alcantara (SUSE)5bb30a42019-11-22 12:30:56 -03004632}
4633
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004634int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
4635{
4636 int rc = 0;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004637 unsigned int xid;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004638 struct TCP_Server_Info *server = NULL;
4639 struct cifs_ses *ses = NULL, *root_ses = NULL;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004640 struct cifs_tcon *tcon = NULL;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004641 int count = 0;
4642 char *ref_path = NULL, *full_path = NULL;
4643 char *oldmnt = NULL;
4644 char *mntdata = NULL;
Al Virodd854462011-06-17 08:24:42 -04004645
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004646 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004647 /*
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004648 * Unconditionally try to get an DFS referral (even cached) to determine whether it is an
4649 * DFS mount.
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004650 *
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004651 * Skip prefix path to provide support for DFS referrals from w2k8 servers which don't seem
4652 * to respond with PATH_NOT_COVERED to requests that include the prefix.
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004653 */
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004654 if (dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb), vol->UNC + 1, NULL,
4655 NULL)) {
4656 /* No DFS referral was returned. Looks like a regular share. */
4657 if (rc)
4658 goto error;
4659 /* Check if it is fully accessible and then mount it */
4660 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4661 if (!rc)
4662 goto out;
4663 if (rc != -EREMOTE)
4664 goto error;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004665 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004666 /* Save mount options */
4667 mntdata = kstrndup(cifs_sb->mountdata, strlen(cifs_sb->mountdata), GFP_KERNEL);
4668 if (!mntdata) {
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03004669 rc = -ENOMEM;
4670 goto error;
4671 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004672 /* Get path of DFS root */
4673 ref_path = build_unc_path_to_root(vol, cifs_sb, false);
4674 if (IS_ERR(ref_path)) {
4675 rc = PTR_ERR(ref_path);
4676 ref_path = NULL;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004677 goto error;
4678 }
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004679
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004680 set_root_ses(cifs_sb, ses, &root_ses);
4681 do {
4682 /* Save full path of last DFS path we used to resolve final target server */
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004683 kfree(full_path);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004684 full_path = build_unc_path_to_root(vol, cifs_sb, !!count);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004685 if (IS_ERR(full_path)) {
4686 rc = PTR_ERR(full_path);
Dan Carpenter20b135e2020-08-05 12:52:07 +03004687 full_path = NULL;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004688 break;
4689 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004690 /* Chase referral */
4691 oldmnt = cifs_sb->mountdata;
4692 rc = expand_dfs_referral(xid, root_ses, vol, cifs_sb, ref_path + 1);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004693 if (rc)
4694 break;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004695 /* Connect to new DFS target only if we were redirected */
4696 if (oldmnt != cifs_sb->mountdata) {
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004697 mount_put_conns(cifs_sb, xid, server, ses, tcon);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004698 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004699 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004700 if (rc && !server && !ses) {
4701 /* Failed to connect. Try to connect to other targets in the referral. */
4702 rc = do_dfs_failover(ref_path + 1, full_path, cifs_sb, vol, root_ses, &xid,
4703 &server, &ses, &tcon);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004704 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004705 if (rc == -EACCES || rc == -EOPNOTSUPP || !server || !ses)
4706 break;
4707 if (!tcon)
4708 continue;
4709 /* Make sure that requests go through new root servers */
Paulo Alcantarae1837852020-08-27 11:20:19 -03004710 if (is_tcon_dfs(tcon)) {
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004711 put_root_ses(root_ses);
4712 set_root_ses(cifs_sb, ses, &root_ses);
4713 }
4714 /* Check for remaining path components and then continue chasing them (-EREMOTE) */
4715 rc = check_dfs_prepath(cifs_sb, vol, xid, server, tcon, &ref_path);
4716 /* Prevent recursion on broken link referrals */
4717 if (rc == -EREMOTE && ++count > MAX_NESTED_LINKS)
4718 rc = -ELOOP;
4719 } while (rc == -EREMOTE);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004720
Jeff Layton9d002df2010-10-06 19:51:11 -04004721 if (rc)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004722 goto error;
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004723 put_root_ses(root_ses);
4724 root_ses = NULL;
4725 kfree(ref_path);
4726 ref_path = NULL;
4727 /*
4728 * Store DFS full path in both superblock and tree connect structures.
4729 *
4730 * For DFS root mounts, the prefix path (cifs_sb->prepath) is preserved during reconnect so
4731 * only the root path is set in cifs_sb->origin_fullpath and tcon->dfs_path. And for DFS
4732 * links, the prefix path is included in both and may be changed during reconnect. See
4733 * cifs_tree_connect().
4734 */
4735 cifs_sb->origin_fullpath = kstrndup(full_path, strlen(full_path), GFP_KERNEL);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02004736 if (!cifs_sb->origin_fullpath) {
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02004737 rc = -ENOMEM;
4738 goto error;
4739 }
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004740 spin_lock(&cifs_tcp_ses_lock);
4741 tcon->dfs_path = full_path;
4742 full_path = NULL;
4743 tcon->remap = cifs_remap(cifs_sb);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02004744 spin_unlock(&cifs_tcp_ses_lock);
4745
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004746 /* Add original volume information for DFS cache to be used when refreshing referrals */
4747 rc = dfs_cache_add_vol(mntdata, vol, cifs_sb->origin_fullpath);
4748 if (rc)
Paulo Alcantarae511d312018-11-14 17:16:44 -02004749 goto error;
Aurelien Aptel5fc7fcd2018-11-16 16:13:25 +01004750 /*
4751 * After reconnecting to a different server, unique ids won't
4752 * match anymore, so we disable serverino. This prevents
4753 * dentry revalidation to think the dentry are stale (ESTALE).
4754 */
4755 cifs_autodisable_serverino(cifs_sb);
Paulo Alcantara (SUSE)bacd7042020-02-20 19:49:34 -03004756 /*
4757 * Force the use of prefix path to support failover on DFS paths that
4758 * resolve to targets that have different prefix paths.
4759 */
4760 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4761 kfree(cifs_sb->prepath);
4762 cifs_sb->prepath = vol->prepath;
4763 vol->prepath = NULL;
4764
Jeff Layton70fe7dc2007-11-16 22:21:07 +00004765out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004766 free_xid(xid);
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02004767 cifs_try_adding_channels(ses);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004768 return mount_setup_tlink(cifs_sb, ses, tcon);
4769
4770error:
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004771 kfree(ref_path);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004772 kfree(full_path);
Paulo Alcantara7efd0812020-07-21 09:36:44 -03004773 kfree(mntdata);
4774 kfree(cifs_sb->origin_fullpath);
4775 put_root_ses(root_ses);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004776 mount_put_conns(cifs_sb, xid, server, ses, tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 return rc;
4778}
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004779#else
4780int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
4781{
4782 int rc = 0;
4783 unsigned int xid;
4784 struct cifs_ses *ses;
4785 struct cifs_tcon *tcon;
4786 struct TCP_Server_Info *server;
4787
4788 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4789 if (rc)
4790 goto error;
4791
4792 if (tcon) {
4793 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4794 if (rc == -EREMOTE)
4795 rc = -EOPNOTSUPP;
4796 if (rc)
4797 goto error;
4798 }
4799
4800 free_xid(xid);
4801
4802 return mount_setup_tlink(cifs_sb, ses, tcon);
4803
4804error:
4805 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4806 return rc;
4807}
4808#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809
Jeff Layton8d1bca32011-06-11 21:17:10 -04004810/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01004811 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04004812 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04004814CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00004815 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 const struct nls_table *nls_codepage)
4817{
4818 struct smb_hdr *smb_buffer;
4819 struct smb_hdr *smb_buffer_response;
4820 TCONX_REQ *pSMB;
4821 TCONX_RSP *pSMBr;
4822 unsigned char *bcc_ptr;
4823 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05004824 int length;
4825 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826
4827 if (ses == NULL)
4828 return -EIO;
4829
4830 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00004831 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00004833
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 smb_buffer_response = smb_buffer;
4835
4836 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
4837 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07004838
Pavel Shilovsky88257362012-05-23 14:01:59 +04004839 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 smb_buffer->Uid = ses->Suid;
4841 pSMB = (TCONX_REQ *) smb_buffer;
4842 pSMBr = (TCONX_RSP *) smb_buffer_response;
4843
4844 pSMB->AndXCommand = 0xFF;
4845 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01004847 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08004848 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00004849 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08004850 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00004851 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08004852 } else {
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004853 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08004854 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
4855 specified as required (when that support is added to
4856 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00004857 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08004858 by Samba (not sure whether other servers allow
4859 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00004860#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton04912d62010-04-24 07:57:45 -04004861 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
Jeff Layton3f618222013-06-12 19:52:14 -05004862 (ses->sectype == LANMAN))
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -05004863 calc_lanman_hash(tcon->password, ses->server->cryptkey,
Steve French96daf2b2011-05-27 04:34:02 +00004864 ses->server->sec_mode &
Jeff Layton4e53a3f2008-12-05 20:41:21 -05004865 SECMODE_PW_ENCRYPT ? true : false,
4866 bcc_ptr);
Steve French7c7b25b2006-06-01 19:20:10 +00004867 else
4868#endif /* CIFS_WEAK_PW_HASH */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -06004869 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -05004870 bcc_ptr, nls_codepage);
Steve Frenchf3a31a22015-03-26 19:23:20 -05004871 if (rc) {
4872 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
4873 __func__, rc);
4874 cifs_buf_release(smb_buffer);
4875 return rc;
4876 }
Steve Frencheeac8042006-01-13 21:34:58 -08004877
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004878 bcc_ptr += CIFS_AUTH_RESP_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004879 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00004880 /* must align unicode strings */
4881 *bcc_ptr = 0; /* null byte password */
4882 bcc_ptr++;
4883 }
Steve Frencheeac8042006-01-13 21:34:58 -08004884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
Jeff Layton38d77c52013-05-26 07:01:00 -04004886 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4888
4889 if (ses->capabilities & CAP_STATUS32) {
4890 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
4891 }
4892 if (ses->capabilities & CAP_DFS) {
4893 smb_buffer->Flags2 |= SMBFLG2_DFS;
4894 }
4895 if (ses->capabilities & CAP_UNICODE) {
4896 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
4897 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06004898 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00004899 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00004900 (/* server len*/ + 256 /* share len */), nls_codepage);
4901 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 bcc_ptr += 2; /* skip trailing null */
4903 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904 strcpy(bcc_ptr, tree);
4905 bcc_ptr += strlen(tree) + 1;
4906 }
4907 strcpy(bcc_ptr, "?????");
4908 bcc_ptr += strlen("?????");
4909 bcc_ptr += 1;
4910 count = bcc_ptr - &pSMB->Password[0];
Qinglang Miao1a0e7f72020-07-25 16:56:01 +08004911 be32_add_cpu(&pSMB->hdr.smb_buf_length, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 pSMB->ByteCount = cpu_to_le16(count);
4913
Steve French133672e2007-11-13 22:41:37 +00004914 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05004915 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01004918 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00004919 bool is_unicode;
4920
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00004922 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 tcon->tid = smb_buffer_response->Tid;
4924 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05004925 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004926 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00004927 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
4928 is_unicode = true;
4929 else
4930 is_unicode = false;
4931
Jeff Laytoncc20c032009-04-30 07:16:21 -04004932
Steve French50c2f752007-07-13 00:33:32 +00004933 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00004934 if (length == 3) {
4935 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
4936 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004937 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01004938 tcon->ipc = true;
4939 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00004940 }
4941 } else if (length == 2) {
4942 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
4943 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05004944 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00004945 }
4946 }
Steve French50c2f752007-07-13 00:33:32 +00004947 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04004948 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05004949 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04004950
4951 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04004952 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06004953 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00004954 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04004955 nls_codepage);
4956
Joe Perchesf96637b2013-05-04 22:12:25 -05004957 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004958
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004959 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00004960 (smb_buffer_response->WordCount == 7))
4961 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00004962 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4963 else
4964 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05004965 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 }
4967
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00004968 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 return rc;
4970}
4971
Al Viro2e32cf52013-10-03 12:53:37 -04004972static void delayed_free(struct rcu_head *p)
4973{
4974 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
4975 unload_nls(sbi->local_nls);
4976 kfree(sbi);
4977}
4978
Al Viro2a9b9952011-06-17 09:27:16 -04004979void
4980cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981{
Jeff Laytonb647c352010-10-28 11:16:44 -04004982 struct rb_root *root = &cifs_sb->tlink_tree;
4983 struct rb_node *node;
4984 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985
Jeff Layton2de970f2010-10-06 19:51:12 -04004986 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4987
Jeff Laytonb647c352010-10-28 11:16:44 -04004988 spin_lock(&cifs_sb->tlink_tree_lock);
4989 while ((node = rb_first(root))) {
4990 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4991 cifs_get_tlink(tlink);
4992 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4993 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00004994
Jeff Laytonb647c352010-10-28 11:16:44 -04004995 spin_unlock(&cifs_sb->tlink_tree_lock);
4996 cifs_put_tlink(tlink);
4997 spin_lock(&cifs_sb->tlink_tree_lock);
4998 }
4999 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005000
Al Virod757d712011-06-17 09:42:43 -04005001 kfree(cifs_sb->mountdata);
Aurelien Aptela6b50582016-05-25 19:59:09 +02005002 kfree(cifs_sb->prepath);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02005003#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarae511d312018-11-14 17:16:44 -02005004 dfs_cache_del_vol(cifs_sb->origin_fullpath);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02005005 kfree(cifs_sb->origin_fullpath);
5006#endif
Al Viro2e32cf52013-10-03 12:53:37 -04005007 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00005008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009
Pavel Shilovsky286170a2012-05-25 10:43:58 +04005010int
5011cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012{
5013 int rc = 0;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02005014 struct TCP_Server_Info *server = cifs_ses_server(ses);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015
Pavel Shilovsky286170a2012-05-25 10:43:58 +04005016 if (!server->ops->need_neg || !server->ops->negotiate)
5017 return -ENOSYS;
5018
Jeff Layton198b5682010-04-24 07:57:48 -04005019 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04005020 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04005021 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022
Pavel Shilovsky286170a2012-05-25 10:43:58 +04005023 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04005024 if (rc == 0) {
5025 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04005026 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04005027 server->tcpStatus = CifsGood;
5028 else
5029 rc = -EHOSTDOWN;
5030 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031 }
Steve French26b994f2008-08-06 05:11:33 +00005032
Jeff Layton198b5682010-04-24 07:57:48 -04005033 return rc;
5034}
Steve French26b994f2008-08-06 05:11:33 +00005035
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04005036int
5037cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
5038 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04005039{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04005040 int rc = -ENOSYS;
Aurelien Aptelf6a6bf72019-09-20 06:22:14 +02005041 struct TCP_Server_Info *server = cifs_ses_server(ses);
Jeff Layton198b5682010-04-24 07:57:48 -04005042
Aurelien Apteld70e9fa2019-09-20 06:31:10 +02005043 if (!ses->binding) {
5044 ses->capabilities = server->capabilities;
5045 if (linuxExtEnabled == 0)
5046 ses->capabilities &= (~server->vals->cap_unix);
5047
5048 if (ses->auth_key.response) {
5049 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
5050 ses->auth_key.response);
5051 kfree(ses->auth_key.response);
5052 ses->auth_key.response = NULL;
5053 ses->auth_key.len = 0;
5054 }
5055 }
Steve French20418ac2009-04-30 16:13:32 +00005056
Joe Perchesf96637b2013-05-04 22:12:25 -05005057 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00005058 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04005059
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04005060 if (server->ops->sess_setup)
5061 rc = server->ops->sess_setup(xid, ses, nls_info);
5062
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05005063 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10005064 cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05005065
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 return rc;
5067}
5068
Jeff Layton8a8798a2012-01-17 16:09:15 -05005069static int
5070cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
5071{
Jeff Layton3f618222013-06-12 19:52:14 -05005072 vol->sectype = ses->sectype;
5073
5074 /* krb5 is special, since we don't need username or pw */
5075 if (vol->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05005076 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05005077
5078 return cifs_set_cifscreds(vol, ses);
5079}
5080
Steve French96daf2b2011-05-27 04:34:02 +00005081static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005082cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04005083{
Jeff Layton8a8798a2012-01-17 16:09:15 -05005084 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00005085 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
5086 struct cifs_ses *ses;
5087 struct cifs_tcon *tcon = NULL;
Jeff Layton9d002df2010-10-06 19:51:11 -04005088 struct smb_vol *vol_info;
Jeff Layton9d002df2010-10-06 19:51:11 -04005089
5090 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
Dan Carpenter803ab972012-01-24 11:39:22 +03005091 if (vol_info == NULL)
5092 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04005093
Jeff Layton9d002df2010-10-06 19:51:11 -04005094 vol_info->local_nls = cifs_sb->local_nls;
5095 vol_info->linux_uid = fsuid;
5096 vol_info->cred_uid = fsuid;
5097 vol_info->UNC = master_tcon->treeName;
5098 vol_info->retry = master_tcon->retry;
5099 vol_info->nocase = master_tcon->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05005100 vol_info->nohandlecache = master_tcon->nohandlecache;
Jeff Layton9d002df2010-10-06 19:51:11 -04005101 vol_info->local_lease = master_tcon->local_lease;
Paul Aurichad35f162020-06-26 12:58:07 -07005102 vol_info->no_lease = master_tcon->no_lease;
Paul Aurich00dfbc22020-06-26 12:58:06 -07005103 vol_info->resilient = master_tcon->use_resilient;
5104 vol_info->persistent = master_tcon->use_persistent;
Paul Aurich6b356f6c2020-06-26 12:58:08 -07005105 vol_info->handle_timeout = master_tcon->handle_timeout;
Jeff Layton9d002df2010-10-06 19:51:11 -04005106 vol_info->no_linux_ext = !master_tcon->unix_ext;
Paul Aurich5391b8e2020-06-26 12:58:09 -07005107 vol_info->linux_ext = master_tcon->posix_extensions;
Jeff Layton28e11bd2013-05-26 07:01:00 -04005108 vol_info->sectype = master_tcon->ses->sectype;
5109 vol_info->sign = master_tcon->ses->sign;
Paul Aurichcc154612020-06-26 12:58:05 -07005110 vol_info->seal = master_tcon->seal;
Jeff Layton9d002df2010-10-06 19:51:11 -04005111
Jeff Layton8a8798a2012-01-17 16:09:15 -05005112 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
5113 if (rc) {
5114 tcon = ERR_PTR(rc);
5115 goto out;
5116 }
Jeff Layton9d002df2010-10-06 19:51:11 -04005117
5118 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05305119 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005120 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05305121 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005122
5123 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
5124 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00005125 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07005126 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04005127 goto out;
5128 }
5129
5130 tcon = cifs_get_tcon(ses, vol_info);
5131 if (IS_ERR(tcon)) {
5132 cifs_put_smb_ses(ses);
5133 goto out;
5134 }
5135
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04005136 if (cap_unix(ses))
Jeff Layton9d002df2010-10-06 19:51:11 -04005137 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
Steve Frenchb3266142018-05-20 23:41:10 -05005138
Jeff Layton9d002df2010-10-06 19:51:11 -04005139out:
Jeff Layton8a8798a2012-01-17 16:09:15 -05005140 kfree(vol_info->username);
Waiman Long453431a2020-08-06 23:18:13 -07005141 kfree_sensitive(vol_info->password);
Jeff Layton9d002df2010-10-06 19:51:11 -04005142 kfree(vol_info);
5143
5144 return tcon;
5145}
5146
Steve French96daf2b2011-05-27 04:34:02 +00005147struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04005148cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
5149{
5150 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
5151}
5152
Jeff Laytonb647c352010-10-28 11:16:44 -04005153/* find and return a tlink with given uid */
5154static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005155tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04005156{
5157 struct rb_node *node = root->rb_node;
5158 struct tcon_link *tlink;
5159
5160 while (node) {
5161 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
5162
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005163 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04005164 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005165 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04005166 node = node->rb_right;
5167 else
5168 return tlink;
5169 }
5170 return NULL;
5171}
5172
5173/* insert a tcon_link into the tree */
5174static void
5175tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
5176{
5177 struct rb_node **new = &(root->rb_node), *parent = NULL;
5178 struct tcon_link *tlink;
5179
5180 while (*new) {
5181 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
5182 parent = *new;
5183
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005184 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04005185 new = &((*new)->rb_left);
5186 else
5187 new = &((*new)->rb_right);
5188 }
5189
5190 rb_link_node(&new_tlink->tl_rbnode, parent, new);
5191 rb_insert_color(&new_tlink->tl_rbnode, root);
5192}
5193
Jeff Layton9d002df2010-10-06 19:51:11 -04005194/*
5195 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
5196 * current task.
5197 *
5198 * If the superblock doesn't refer to a multiuser mount, then just return
5199 * the master tcon for the mount.
5200 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05305201 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04005202 * exists, then check to see if it's pending construction. If it is then wait
5203 * for construction to complete. Once it's no longer pending, check to see if
5204 * it failed and either return an error or retry construction, depending on
5205 * the timeout.
5206 *
5207 * If one doesn't exist then insert a new tcon_link struct into the tree and
5208 * try to construct a new one.
5209 */
5210struct tcon_link *
5211cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
5212{
5213 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005214 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04005215 struct tcon_link *tlink, *newtlink;
5216
5217 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
5218 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
5219
5220 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04005221 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04005222 if (tlink)
5223 cifs_get_tlink(tlink);
5224 spin_unlock(&cifs_sb->tlink_tree_lock);
5225
5226 if (tlink == NULL) {
5227 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
5228 if (newtlink == NULL)
5229 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04005230 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04005231 newtlink->tl_tcon = ERR_PTR(-EACCES);
5232 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
5233 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
5234 cifs_get_tlink(newtlink);
5235
Jeff Layton9d002df2010-10-06 19:51:11 -04005236 spin_lock(&cifs_sb->tlink_tree_lock);
5237 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04005238 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04005239 if (tlink) {
5240 cifs_get_tlink(tlink);
5241 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005242 kfree(newtlink);
5243 goto wait_for_construction;
5244 }
Jeff Layton9d002df2010-10-06 19:51:11 -04005245 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04005246 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
5247 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005248 } else {
5249wait_for_construction:
5250 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04005251 TASK_INTERRUPTIBLE);
5252 if (ret) {
5253 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10005254 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04005255 }
5256
5257 /* if it's good, return it */
5258 if (!IS_ERR(tlink->tl_tcon))
5259 return tlink;
5260
5261 /* return error if we tried this already recently */
5262 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
5263 cifs_put_tlink(tlink);
5264 return ERR_PTR(-EACCES);
5265 }
5266
5267 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
5268 goto wait_for_construction;
5269 }
5270
5271 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
5272 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
5273 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
5274
5275 if (IS_ERR(tlink->tl_tcon)) {
5276 cifs_put_tlink(tlink);
5277 return ERR_PTR(-EACCES);
5278 }
5279
5280 return tlink;
5281}
Jeff Layton2de970f2010-10-06 19:51:12 -04005282
5283/*
5284 * periodic workqueue job that scans tcon_tree for a superblock and closes
5285 * out tcons.
5286 */
5287static void
5288cifs_prune_tlinks(struct work_struct *work)
5289{
5290 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
5291 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04005292 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00005293 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04005294 struct rb_node *tmp;
5295 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04005296
Jeff Laytonb647c352010-10-28 11:16:44 -04005297 /*
5298 * Because we drop the spinlock in the loop in order to put the tlink
5299 * it's not guarded against removal of links from the tree. The only
5300 * places that remove entries from the tree are this function and
5301 * umounts. Because this function is non-reentrant and is canceled
5302 * before umount can proceed, this is safe.
5303 */
5304 spin_lock(&cifs_sb->tlink_tree_lock);
5305 node = rb_first(root);
5306 while (node != NULL) {
5307 tmp = node;
5308 node = rb_next(tmp);
5309 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
5310
5311 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
5312 atomic_read(&tlink->tl_count) != 0 ||
5313 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
5314 continue;
5315
5316 cifs_get_tlink(tlink);
5317 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
5318 rb_erase(tmp, root);
5319
Jeff Layton2de970f2010-10-06 19:51:12 -04005320 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04005321 cifs_put_tlink(tlink);
5322 spin_lock(&cifs_sb->tlink_tree_lock);
5323 }
5324 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04005325
Jeff Laytonda472fc2012-03-23 14:40:53 -04005326 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04005327 TLINK_IDLE_EXPIRE);
5328}
Stefan Metzmacher565674d2020-07-21 09:36:38 -03005329
5330#ifdef CONFIG_CIFS_DFS_UPCALL
5331int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
5332{
5333 int rc;
5334 struct TCP_Server_Info *server = tcon->ses->server;
5335 const struct smb_version_operations *ops = server->ops;
5336 struct dfs_cache_tgt_list tl;
5337 struct dfs_cache_tgt_iterator *it = NULL;
5338 char *tree;
5339 const char *tcp_host;
5340 size_t tcp_host_len;
5341 const char *dfs_host;
5342 size_t dfs_host_len;
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03005343 char *share = NULL, *prefix = NULL;
Paulo Alcantara11375a52020-07-21 09:36:43 -03005344 struct dfs_info3_param ref = {0};
5345 bool isroot;
Stefan Metzmacher565674d2020-07-21 09:36:38 -03005346
5347 tree = kzalloc(MAX_TREE_SIZE, GFP_KERNEL);
5348 if (!tree)
5349 return -ENOMEM;
5350
5351 if (!tcon->dfs_path) {
5352 if (tcon->ipc) {
5353 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", server->hostname);
5354 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
5355 } else {
5356 rc = ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc);
5357 }
5358 goto out;
5359 }
5360
Paulo Alcantara11375a52020-07-21 09:36:43 -03005361 rc = dfs_cache_noreq_find(tcon->dfs_path + 1, &ref, &tl);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03005362 if (rc)
5363 goto out;
Paulo Alcantara11375a52020-07-21 09:36:43 -03005364 isroot = ref.server_type == DFS_TYPE_ROOT;
5365 free_dfs_info_param(&ref);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03005366
5367 extract_unc_hostname(server->hostname, &tcp_host, &tcp_host_len);
5368
5369 for (it = dfs_cache_get_tgt_iterator(&tl); it; it = dfs_cache_get_next_tgt(&tl, it)) {
Stefan Metzmacher565674d2020-07-21 09:36:38 -03005370 bool target_match;
5371
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03005372 kfree(share);
5373 kfree(prefix);
Colin Ian Kingc6a80e12020-07-31 18:13:42 +01005374 share = NULL;
5375 prefix = NULL;
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03005376
5377 rc = dfs_cache_get_tgt_share(tcon->dfs_path + 1, it, &share, &prefix);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03005378 if (rc) {
5379 cifs_dbg(VFS, "%s: failed to parse target share %d\n",
5380 __func__, rc);
5381 continue;
5382 }
5383
5384 extract_unc_hostname(share, &dfs_host, &dfs_host_len);
5385
5386 if (dfs_host_len != tcp_host_len
5387 || strncasecmp(dfs_host, tcp_host, dfs_host_len) != 0) {
5388 cifs_dbg(FYI, "%s: %.*s doesn't match %.*s\n", __func__, (int)dfs_host_len,
5389 dfs_host, (int)tcp_host_len, tcp_host);
5390
5391 rc = match_target_ip(server, dfs_host, dfs_host_len, &target_match);
5392 if (rc) {
5393 cifs_dbg(VFS, "%s: failed to match target ip: %d\n", __func__, rc);
5394 break;
5395 }
5396
5397 if (!target_match) {
5398 cifs_dbg(FYI, "%s: skipping target\n", __func__);
5399 continue;
5400 }
5401 }
5402
5403 if (tcon->ipc) {
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03005404 scnprintf(tree, MAX_TREE_SIZE, "\\\\%s\\IPC$", share);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03005405 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
5406 } else {
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03005407 scnprintf(tree, MAX_TREE_SIZE, "\\%s", share);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03005408 rc = ops->tree_connect(xid, tcon->ses, tree, tcon, nlsc);
Paulo Alcantara11375a52020-07-21 09:36:43 -03005409 /* Only handle prefix paths of DFS link targets */
5410 if (!rc && !isroot) {
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03005411 rc = update_super_prepath(tcon, prefix);
Stefan Metzmacher565674d2020-07-21 09:36:38 -03005412 break;
5413 }
5414 }
5415 if (rc == -EREMOTE)
5416 break;
5417 }
5418
Paulo Alcantara7548e1d2020-07-21 09:36:42 -03005419 kfree(share);
5420 kfree(prefix);
5421
Stefan Metzmacher565674d2020-07-21 09:36:38 -03005422 if (!rc) {
5423 if (it)
5424 rc = dfs_cache_noreq_update_tgthint(tcon->dfs_path + 1, it);
5425 else
5426 rc = -ENOENT;
5427 }
5428 dfs_cache_free_tgts(&tl);
5429out:
5430 kfree(tree);
5431 return rc;
5432}
5433#else
5434int cifs_tree_connect(const unsigned int xid, struct cifs_tcon *tcon, const struct nls_table *nlsc)
5435{
5436 const struct smb_version_operations *ops = tcon->ses->server->ops;
5437
5438 return ops->tree_connect(xid, tcon->ses, tcon->treeName, tcon, nlsc);
5439}
5440#endif