blob: 85f8d943a05af7f40f8dd3646600727bf1c8ee42 [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>
Ingo Molnar3f07c012017-02-08 18:51:30 +010024#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/list.h>
26#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/pagemap.h>
29#include <linux/ctype.h>
30#include <linux/utsname.h>
31#include <linux/mempool.h>
Steve Frenchb8643e12005-04-28 22:41:07 -070032#include <linux/delay.h>
Steve Frenchf1914012005-08-18 09:37:34 -070033#include <linux/completion.h>
Igor Mammedovaaf737a2007-04-03 19:16:43 +000034#include <linux/kthread.h>
Steve French0ae0efa2005-10-10 10:57:19 -070035#include <linux/pagevec.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Igor Mammedov5c2503a2009-04-21 19:31:05 +040037#include <linux/namei.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020038#include <linux/uuid.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080039#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/processor.h>
Jeff Layton50b64e32009-06-02 06:55:20 -040041#include <linux/inet.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040042#include <linux/module.h>
Jeff Layton8a8798a2012-01-17 16:09:15 -050043#include <keys/user-type.h>
Steve French0e2beda2009-01-30 21:24:41 +000044#include <net/ipv6.h>
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040045#include <linux/parser.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060046#include <linux/bvec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "cifspdu.h"
48#include "cifsglob.h"
49#include "cifsproto.h"
50#include "cifs_unicode.h"
51#include "cifs_debug.h"
52#include "cifs_fs_sb.h"
53#include "ntlmssp.h"
54#include "nterr.h"
55#include "rfc1002pdu.h"
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +053056#include "fscache.h"
Pavel Shilovsky53e0e112016-11-04 11:50:31 -070057#include "smb2proto.h"
Long Li2f894642017-11-22 17:38:34 -070058#include "smbdirect.h"
Paulo Alcantara1c780222018-11-14 16:24:03 -020059#include "dns_resolve.h"
Paulo Alcantara93d5cb52018-11-14 17:13:25 -020060#include "cifsfs.h"
Paulo Alcantara1c780222018-11-14 16:24:03 -020061#ifdef CONFIG_CIFS_DFS_UPCALL
62#include "dfs_cache.h"
63#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065extern mempool_t *cifs_req_poolp;
Steve Frenchf92a7202018-05-24 04:11:07 -050066extern bool disable_legacy_dialects;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Jeff Layton2de970f2010-10-06 19:51:12 -040068/* FIXME: should these be tunable? */
Jeff Layton9d002df2010-10-06 19:51:11 -040069#define TLINK_ERROR_EXPIRE (1 * HZ)
Jeff Layton2de970f2010-10-06 19:51:12 -040070#define TLINK_IDLE_EXPIRE (600 * HZ)
Jeff Layton9d002df2010-10-06 19:51:11 -040071
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040072enum {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040073 /* Mount options that take no arguments */
74 Opt_user_xattr, Opt_nouser_xattr,
75 Opt_forceuid, Opt_noforceuid,
Jeff Layton72bd4812012-10-03 16:02:36 -040076 Opt_forcegid, Opt_noforcegid,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040077 Opt_noblocksend, Opt_noautotune,
78 Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
Steve French2baa2682014-09-27 02:19:01 -050079 Opt_mapposix, Opt_nomapposix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040080 Opt_mapchars, Opt_nomapchars, Opt_sfu,
81 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
Steve Frenchb3266142018-05-20 23:41:10 -050082 Opt_noposixpaths, Opt_nounix, Opt_unix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040083 Opt_nocase,
84 Opt_brl, Opt_nobrl,
Steve French3d4ef9a2018-04-25 22:19:09 -050085 Opt_handlecache, Opt_nohandlecache,
Steve French95932652016-09-23 01:36:34 -050086 Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040087 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
88 Opt_nohard, Opt_nosoft,
89 Opt_nointr, Opt_intr,
90 Opt_nostrictsync, Opt_strictsync,
91 Opt_serverino, Opt_noserverino,
92 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
93 Opt_acl, Opt_noacl, Opt_locallease,
Jeff Layton1b359202012-09-19 15:20:27 -070094 Opt_sign, Opt_seal, Opt_noac,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040095 Opt_fsc, Opt_mfsymlinks,
Jeff Laytona0b3df52013-05-24 07:40:59 -040096 Opt_multiuser, Opt_sloppy, Opt_nosharesock,
Steve Frenchb2a30772015-09-29 21:49:28 -050097 Opt_persistent, Opt_nopersistent,
Steve French592fafe2015-11-03 10:08:53 -060098 Opt_resilient, Opt_noresilient,
Steve French412094a2019-06-24 02:01:42 -050099 Opt_domainauto, Opt_rdma, Opt_modesid,
Steve French9fe5ff12019-06-24 20:39:04 -0500100 Opt_compress,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400101
102 /* Mount options which take numeric value */
103 Opt_backupuid, Opt_backupgid, Opt_uid,
104 Opt_cruid, Opt_gid, Opt_file_mode,
105 Opt_dirmode, Opt_port,
Steve Frenche8506d22019-02-28 21:32:15 -0600106 Opt_blocksize, Opt_rsize, Opt_wsize, Opt_actimeo,
Steve Frenchca567eb2019-03-29 16:31:07 -0500107 Opt_echo_interval, Opt_max_credits, Opt_handletimeout,
Steve French8b217fe2016-11-11 22:36:20 -0600108 Opt_snapshot,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400109
110 /* Mount options which take string value */
111 Opt_user, Opt_pass, Opt_ip,
Jeff Layton73a999f2013-03-22 08:42:57 -0400112 Opt_domain, Opt_srcaddr, Opt_iocharset,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400113 Opt_netbiosname, Opt_servern,
Jeff Layton23db65f2012-05-15 12:20:51 -0400114 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400115
116 /* Mount options to be ignored */
117 Opt_ignore,
118
119 /* Options which could be blank */
120 Opt_blank_pass,
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100121 Opt_blank_user,
122 Opt_blank_ip,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400123
124 Opt_err
125};
126
127static const match_table_t cifs_mount_option_tokens = {
128
129 { Opt_user_xattr, "user_xattr" },
130 { Opt_nouser_xattr, "nouser_xattr" },
131 { Opt_forceuid, "forceuid" },
132 { Opt_noforceuid, "noforceuid" },
Jeff Layton72bd4812012-10-03 16:02:36 -0400133 { Opt_forcegid, "forcegid" },
134 { Opt_noforcegid, "noforcegid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400135 { Opt_noblocksend, "noblocksend" },
136 { Opt_noautotune, "noautotune" },
137 { Opt_hard, "hard" },
138 { Opt_soft, "soft" },
139 { Opt_perm, "perm" },
140 { Opt_noperm, "noperm" },
Steve French2baa2682014-09-27 02:19:01 -0500141 { Opt_mapchars, "mapchars" }, /* SFU style */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400142 { Opt_nomapchars, "nomapchars" },
Steve French2baa2682014-09-27 02:19:01 -0500143 { Opt_mapposix, "mapposix" }, /* SFM style */
144 { Opt_nomapposix, "nomapposix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400145 { Opt_sfu, "sfu" },
146 { Opt_nosfu, "nosfu" },
147 { Opt_nodfs, "nodfs" },
148 { Opt_posixpaths, "posixpaths" },
149 { Opt_noposixpaths, "noposixpaths" },
150 { Opt_nounix, "nounix" },
151 { Opt_nounix, "nolinux" },
Steve Frenchb3266142018-05-20 23:41:10 -0500152 { Opt_nounix, "noposix" },
153 { Opt_unix, "unix" },
154 { Opt_unix, "linux" },
155 { Opt_unix, "posix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400156 { Opt_nocase, "nocase" },
157 { Opt_nocase, "ignorecase" },
158 { Opt_brl, "brl" },
159 { Opt_nobrl, "nobrl" },
Steve French3d4ef9a2018-04-25 22:19:09 -0500160 { Opt_handlecache, "handlecache" },
161 { Opt_nohandlecache, "nohandlecache" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400162 { Opt_nobrl, "nolock" },
163 { Opt_forcemandatorylock, "forcemandatorylock" },
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +0400164 { Opt_forcemandatorylock, "forcemand" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400165 { Opt_setuids, "setuids" },
166 { Opt_nosetuids, "nosetuids" },
Steve French95932652016-09-23 01:36:34 -0500167 { Opt_setuidfromacl, "idsfromsid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400168 { Opt_dynperm, "dynperm" },
169 { Opt_nodynperm, "nodynperm" },
170 { Opt_nohard, "nohard" },
171 { Opt_nosoft, "nosoft" },
172 { Opt_nointr, "nointr" },
173 { Opt_intr, "intr" },
174 { Opt_nostrictsync, "nostrictsync" },
175 { Opt_strictsync, "strictsync" },
176 { Opt_serverino, "serverino" },
177 { Opt_noserverino, "noserverino" },
178 { Opt_rwpidforward, "rwpidforward" },
Steve French412094a2019-06-24 02:01:42 -0500179 { Opt_modesid, "modefromsid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400180 { Opt_cifsacl, "cifsacl" },
181 { Opt_nocifsacl, "nocifsacl" },
182 { Opt_acl, "acl" },
183 { Opt_noacl, "noacl" },
184 { Opt_locallease, "locallease" },
185 { Opt_sign, "sign" },
186 { Opt_seal, "seal" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400187 { Opt_noac, "noac" },
188 { Opt_fsc, "fsc" },
189 { Opt_mfsymlinks, "mfsymlinks" },
190 { Opt_multiuser, "multiuser" },
Jeff Laytond8162552012-03-23 14:40:56 -0400191 { Opt_sloppy, "sloppy" },
Jeff Laytona0b3df52013-05-24 07:40:59 -0400192 { Opt_nosharesock, "nosharesock" },
Steve Frenchb2a30772015-09-29 21:49:28 -0500193 { Opt_persistent, "persistenthandles"},
194 { Opt_nopersistent, "nopersistenthandles"},
Steve French592fafe2015-11-03 10:08:53 -0600195 { Opt_resilient, "resilienthandles"},
196 { Opt_noresilient, "noresilienthandles"},
Germano Percossi39566442016-12-15 12:31:18 +0530197 { Opt_domainauto, "domainauto"},
Long Li8339dd32017-11-07 01:54:55 -0700198 { Opt_rdma, "rdma"},
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400199
200 { Opt_backupuid, "backupuid=%s" },
201 { Opt_backupgid, "backupgid=%s" },
202 { Opt_uid, "uid=%s" },
203 { Opt_cruid, "cruid=%s" },
204 { Opt_gid, "gid=%s" },
205 { Opt_file_mode, "file_mode=%s" },
206 { Opt_dirmode, "dirmode=%s" },
207 { Opt_dirmode, "dir_mode=%s" },
208 { Opt_port, "port=%s" },
Steve Frenche8506d22019-02-28 21:32:15 -0600209 { Opt_blocksize, "bsize=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400210 { Opt_rsize, "rsize=%s" },
211 { Opt_wsize, "wsize=%s" },
212 { Opt_actimeo, "actimeo=%s" },
Steve Frenchca567eb2019-03-29 16:31:07 -0500213 { Opt_handletimeout, "handletimeout=%s" },
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600214 { Opt_echo_interval, "echo_interval=%s" },
Steve French141891f2016-09-23 00:44:16 -0500215 { Opt_max_credits, "max_credits=%s" },
Steve French8b217fe2016-11-11 22:36:20 -0600216 { Opt_snapshot, "snapshot=%s" },
Steve French9fe5ff12019-06-24 20:39:04 -0500217 { Opt_compress, "compress=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400218
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100219 { Opt_blank_user, "user=" },
220 { Opt_blank_user, "username=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400221 { Opt_user, "user=%s" },
222 { Opt_user, "username=%s" },
223 { Opt_blank_pass, "pass=" },
Jesper Nilsson3c15b4c2012-11-29 17:31:16 +0100224 { Opt_blank_pass, "password=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400225 { Opt_pass, "pass=%s" },
226 { Opt_pass, "password=%s" },
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100227 { Opt_blank_ip, "ip=" },
228 { Opt_blank_ip, "addr=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400229 { Opt_ip, "ip=%s" },
230 { Opt_ip, "addr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400231 { Opt_ignore, "unc=%s" },
232 { Opt_ignore, "target=%s" },
233 { Opt_ignore, "path=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400234 { Opt_domain, "dom=%s" },
235 { Opt_domain, "domain=%s" },
236 { Opt_domain, "workgroup=%s" },
237 { Opt_srcaddr, "srcaddr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400238 { Opt_ignore, "prefixpath=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400239 { Opt_iocharset, "iocharset=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400240 { Opt_netbiosname, "netbiosname=%s" },
241 { Opt_servern, "servern=%s" },
242 { Opt_ver, "ver=%s" },
Jeff Layton23db65f2012-05-15 12:20:51 -0400243 { Opt_vers, "vers=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400244 { Opt_sec, "sec=%s" },
Jeff Layton15b6a472012-05-16 07:50:15 -0400245 { Opt_cache, "cache=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400246
247 { Opt_ignore, "cred" },
248 { Opt_ignore, "credentials" },
Jeff Laytona557b972012-05-02 14:02:40 -0400249 { Opt_ignore, "cred=%s" },
250 { Opt_ignore, "credentials=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400251 { Opt_ignore, "guest" },
252 { Opt_ignore, "rw" },
253 { Opt_ignore, "ro" },
254 { Opt_ignore, "suid" },
255 { Opt_ignore, "nosuid" },
256 { Opt_ignore, "exec" },
257 { Opt_ignore, "noexec" },
258 { Opt_ignore, "nodev" },
259 { Opt_ignore, "noauto" },
260 { Opt_ignore, "dev" },
261 { Opt_ignore, "mand" },
262 { Opt_ignore, "nomand" },
Steve French9b9c5be2018-09-22 12:07:06 -0500263 { Opt_ignore, "relatime" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400264 { Opt_ignore, "_netdev" },
265
266 { Opt_err, NULL }
267};
268
269enum {
270 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
271 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
Jeff Layton76596242012-07-23 20:34:17 -0400272 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
273 Opt_sec_ntlmv2i, Opt_sec_lanman,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400274 Opt_sec_none,
275
276 Opt_sec_err
277};
278
279static const match_table_t cifs_secflavor_tokens = {
280 { Opt_sec_krb5, "krb5" },
281 { Opt_sec_krb5i, "krb5i" },
282 { Opt_sec_krb5p, "krb5p" },
283 { Opt_sec_ntlmsspi, "ntlmsspi" },
284 { Opt_sec_ntlmssp, "ntlmssp" },
285 { Opt_ntlm, "ntlm" },
286 { Opt_sec_ntlmi, "ntlmi" },
Jeff Layton76596242012-07-23 20:34:17 -0400287 { Opt_sec_ntlmv2, "nontlm" },
288 { Opt_sec_ntlmv2, "ntlmv2" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400289 { Opt_sec_ntlmv2i, "ntlmv2i" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400290 { Opt_sec_lanman, "lanman" },
291 { Opt_sec_none, "none" },
292
293 { Opt_sec_err, NULL }
294};
295
Jeff Layton15b6a472012-05-16 07:50:15 -0400296/* cache flavors */
297enum {
298 Opt_cache_loose,
299 Opt_cache_strict,
300 Opt_cache_none,
Steve French83bbfa72019-08-27 23:58:54 -0500301 Opt_cache_ro,
Steve French41e033f2019-08-30 02:12:41 -0500302 Opt_cache_rw,
Jeff Layton15b6a472012-05-16 07:50:15 -0400303 Opt_cache_err
304};
305
306static const match_table_t cifs_cacheflavor_tokens = {
307 { Opt_cache_loose, "loose" },
308 { Opt_cache_strict, "strict" },
309 { Opt_cache_none, "none" },
Steve French83bbfa72019-08-27 23:58:54 -0500310 { Opt_cache_ro, "ro" },
Steve French41e033f2019-08-30 02:12:41 -0500311 { Opt_cache_rw, "singleclient" },
Jeff Layton15b6a472012-05-16 07:50:15 -0400312 { Opt_cache_err, NULL }
313};
314
Jeff Layton23db65f2012-05-15 12:20:51 -0400315static const match_table_t cifs_smb_version_tokens = {
316 { Smb_1, SMB1_VERSION_STRING },
Steve Frenchdd446b12012-11-28 23:21:06 -0600317 { Smb_20, SMB20_VERSION_STRING},
Steve French1080ef72011-02-24 18:07:19 +0000318 { Smb_21, SMB21_VERSION_STRING },
Steve Frenche4aa25e2012-10-01 12:26:22 -0500319 { Smb_30, SMB30_VERSION_STRING },
Steve French20b6d8b2013-06-12 22:48:41 -0500320 { Smb_302, SMB302_VERSION_STRING },
Kenneth D'souza4a3b38a2018-11-17 10:33:30 +0530321 { Smb_302, ALT_SMB302_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600322 { Smb_311, SMB311_VERSION_STRING },
Steve Frenchaab18932015-06-23 23:37:11 -0500323 { Smb_311, ALT_SMB311_VERSION_STRING },
Steve French9764c022017-09-17 10:41:35 -0500324 { Smb_3any, SMB3ANY_VERSION_STRING },
325 { Smb_default, SMBDEFAULT_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600326 { Smb_version_err, NULL }
Jeff Layton23db65f2012-05-15 12:20:51 -0400327};
328
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300329static int ip_connect(struct TCP_Server_Info *server);
330static int generic_ip_connect(struct TCP_Server_Info *server);
Jeff Laytonb647c352010-10-28 11:16:44 -0400331static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
Jeff Layton2de970f2010-10-06 19:51:12 -0400332static void cifs_prune_tlinks(struct work_struct *work);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200333static char *extract_hostname(const char *unc);
334
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200335/*
336 * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
337 * get their ip addresses changed at some point.
338 *
339 * This should be called with server->srv_mutex held.
340 */
341#ifdef CONFIG_CIFS_DFS_UPCALL
342static int reconn_set_ipaddr(struct TCP_Server_Info *server)
343{
344 int rc;
345 int len;
346 char *unc, *ipaddr = NULL;
347
348 if (!server->hostname)
349 return -EINVAL;
350
351 len = strlen(server->hostname) + 3;
352
353 unc = kmalloc(len, GFP_KERNEL);
354 if (!unc) {
355 cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
356 return -ENOMEM;
357 }
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +1000358 scnprintf(unc, len, "\\\\%s", server->hostname);
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200359
360 rc = dns_resolve_server_name_to_ip(unc, &ipaddr);
361 kfree(unc);
362
363 if (rc < 0) {
364 cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
365 __func__, server->hostname, rc);
366 return rc;
367 }
368
369 rc = cifs_convert_address((struct sockaddr *)&server->dstaddr, ipaddr,
370 strlen(ipaddr));
371 kfree(ipaddr);
372
373 return !rc ? -1 : 0;
374}
375#else
376static inline int reconn_set_ipaddr(struct TCP_Server_Info *server)
377{
378 return 0;
379}
380#endif
381
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200382#ifdef CONFIG_CIFS_DFS_UPCALL
383struct super_cb_data {
384 struct TCP_Server_Info *server;
385 struct cifs_sb_info *cifs_sb;
386};
387
388/* These functions must be called with server->srv_mutex held */
389
390static void super_cb(struct super_block *sb, void *arg)
391{
392 struct super_cb_data *d = arg;
393 struct cifs_sb_info *cifs_sb;
394 struct cifs_tcon *tcon;
395
396 if (d->cifs_sb)
397 return;
398
399 cifs_sb = CIFS_SB(sb);
400 tcon = cifs_sb_master_tcon(cifs_sb);
401 if (tcon->ses->server == d->server)
402 d->cifs_sb = cifs_sb;
403}
404
405static inline struct cifs_sb_info *
406find_super_by_tcp(struct TCP_Server_Info *server)
407{
408 struct super_cb_data d = {
409 .server = server,
410 .cifs_sb = NULL,
411 };
412
413 iterate_supers_type(&cifs_fs_type, super_cb, &d);
414 return d.cifs_sb ? d.cifs_sb : ERR_PTR(-ENOENT);
415}
416
417static void reconn_inval_dfs_target(struct TCP_Server_Info *server,
418 struct cifs_sb_info *cifs_sb,
419 struct dfs_cache_tgt_list *tgt_list,
420 struct dfs_cache_tgt_iterator **tgt_it)
421{
422 const char *name;
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200423
424 if (!cifs_sb || !cifs_sb->origin_fullpath || !tgt_list ||
425 !server->nr_targets)
426 return;
427
428 if (!*tgt_it) {
429 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
430 } else {
431 *tgt_it = dfs_cache_get_next_tgt(tgt_list, *tgt_it);
432 if (!*tgt_it)
433 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
434 }
435
436 cifs_dbg(FYI, "%s: UNC: %s\n", __func__, cifs_sb->origin_fullpath);
437
438 name = dfs_cache_get_tgt_name(*tgt_it);
439
440 kfree(server->hostname);
441
442 server->hostname = extract_hostname(name);
Dan Carpenter84288172019-01-05 15:25:29 +0300443 if (IS_ERR(server->hostname)) {
444 cifs_dbg(FYI,
445 "%s: failed to extract hostname from target: %ld\n",
446 __func__, PTR_ERR(server->hostname));
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200447 }
448}
449
450static inline int reconn_setup_dfs_targets(struct cifs_sb_info *cifs_sb,
451 struct dfs_cache_tgt_list *tl,
452 struct dfs_cache_tgt_iterator **it)
453{
454 if (!cifs_sb->origin_fullpath)
455 return -EOPNOTSUPP;
456 return dfs_cache_noreq_find(cifs_sb->origin_fullpath + 1, NULL, tl);
457}
458#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Jeff Laytond5c56052008-12-01 18:42:33 -0500460/*
461 * cifs tcp session reconnection
462 *
463 * mark tcp session as reconnecting so temporarily locked
464 * mark all smb sessions as reconnecting for tcp session
465 * reconnect tcp session
466 * wake up waiters on reconnection? - (not needed currently)
467 */
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400468int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469cifs_reconnect(struct TCP_Server_Info *server)
470{
471 int rc = 0;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500472 struct list_head *tmp, *tmp2;
Steve French96daf2b2011-05-27 04:34:02 +0000473 struct cifs_ses *ses;
474 struct cifs_tcon *tcon;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000475 struct mid_q_entry *mid_entry;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400476 struct list_head retry_list;
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200477#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantara23324402018-11-20 14:37:18 -0200478 struct cifs_sb_info *cifs_sb = NULL;
479 struct dfs_cache_tgt_list tgt_list = {0};
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200480 struct dfs_cache_tgt_iterator *tgt_it = NULL;
481#endif
Steve French50c2f752007-07-13 00:33:32 +0000482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 spin_lock(&GlobalMid_Lock);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200484 server->nr_targets = 1;
485#ifdef CONFIG_CIFS_DFS_UPCALL
Ronnie Sahlberg61cabc72019-06-14 13:02:29 +1000486 spin_unlock(&GlobalMid_Lock);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200487 cifs_sb = find_super_by_tcp(server);
488 if (IS_ERR(cifs_sb)) {
489 rc = PTR_ERR(cifs_sb);
490 cifs_dbg(FYI, "%s: will not do DFS failover: rc = %d\n",
491 __func__, rc);
492 cifs_sb = NULL;
493 } else {
494 rc = reconn_setup_dfs_targets(cifs_sb, &tgt_list, &tgt_it);
Steve French55a7f002019-01-01 17:19:45 -0600495 if (rc && (rc != -EOPNOTSUPP)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000496 cifs_server_dbg(VFS, "%s: no target servers for DFS failover\n",
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200497 __func__);
498 } else {
499 server->nr_targets = dfs_cache_get_nr_tgts(&tgt_list);
500 }
501 }
502 cifs_dbg(FYI, "%s: will retry %d target(s)\n", __func__,
503 server->nr_targets);
Ronnie Sahlberg61cabc72019-06-14 13:02:29 +1000504 spin_lock(&GlobalMid_Lock);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200505#endif
Jeff Layton469ee612008-10-16 18:46:39 +0000506 if (server->tcpStatus == CifsExiting) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000507 /* the demux thread will exit normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 next time through the loop */
509 spin_unlock(&GlobalMid_Lock);
510 return rc;
511 } else
512 server->tcpStatus = CifsNeedReconnect;
513 spin_unlock(&GlobalMid_Lock);
514 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400515 server->max_read = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Steve French6e4d3bb2018-09-22 11:25:04 -0500517 cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
Steve Frenchbf1fdeb2018-07-30 19:23:09 -0500518 trace_smb3_reconnect(server->CurrentMid, server->hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /* before reconnecting the tcp session, mark the smb session (uid)
521 and the tid bad so they are not used until reconnected */
Joe Perchesf96637b2013-05-04 22:12:25 -0500522 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
523 __func__);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530524 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -0500525 list_for_each(tmp, &server->smb_ses_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000526 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
Jeff Layton14fbf502008-11-14 13:53:46 -0500527 ses->need_reconnect = true;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500528 list_for_each(tmp2, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000529 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500530 tcon->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Aurelien Aptelb327a712018-01-24 13:46:10 +0100532 if (ses->tcon_ipc)
533 ses->tcon_ipc->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530535 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* do not want to be sending data on a socket we are freeing */
Joe Perchesf96637b2013-05-04 22:12:25 -0500538 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500539 mutex_lock(&server->srv_mutex);
Long Li1d2a4f52019-05-13 21:01:28 -0700540 if (server->ssocket) {
541 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
542 server->ssocket->state, server->ssocket->flags);
543 kernel_sock_shutdown(server->ssocket, SHUT_WR);
544 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
545 server->ssocket->state, server->ssocket->flags);
546 sock_release(server->ssocket);
547 server->ssocket = NULL;
548 }
549 server->sequence_number = 0;
550 server->session_estab = false;
551 kfree(server->session_key.response);
552 server->session_key.response = NULL;
553 server->session_key.len = 0;
554 server->lstrp = jiffies;
Long Li214bab42019-04-05 21:36:35 +0000555
556 /* mark submitted MIDs for retry and issue callback */
557 INIT_LIST_HEAD(&retry_list);
558 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
559 spin_lock(&GlobalMid_Lock);
560 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
561 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
562 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
563 mid_entry->mid_state = MID_RETRY_NEEDED;
564 list_move(&mid_entry->qhead, &retry_list);
565 }
566 spin_unlock(&GlobalMid_Lock);
Long Li1d2a4f52019-05-13 21:01:28 -0700567 mutex_unlock(&server->srv_mutex);
Long Li214bab42019-04-05 21:36:35 +0000568
569 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
570 list_for_each_safe(tmp, tmp2, &retry_list) {
571 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
572 list_del_init(&mid_entry->qhead);
573 mid_entry->callback(mid_entry);
574 }
575
Long Li1d2a4f52019-05-13 21:01:28 -0700576 if (cifs_rdma_enabled(server)) {
577 mutex_lock(&server->srv_mutex);
Long Li050b8c32019-04-04 11:35:42 -0500578 smbd_destroy(server);
Long Li1d2a4f52019-05-13 21:01:28 -0700579 mutex_unlock(&server->srv_mutex);
580 }
Jeff Layton3c1105d2011-05-22 07:09:13 -0400581
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400582 do {
Steve French6c3d8902006-07-31 22:46:20 +0000583 try_to_freeze();
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300584
Jeff Layton73e216a2013-09-05 08:38:10 -0400585 mutex_lock(&server->srv_mutex);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200586 /*
587 * Set up next DFS target server (if any) for reconnect. If DFS
588 * feature is disabled, then we will retry last server we
589 * connected to before.
590 */
Long Li781a8052017-11-22 17:38:36 -0700591 if (cifs_rdma_enabled(server))
592 rc = smbd_reconnect(server);
593 else
594 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000595 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500596 cifs_dbg(FYI, "reconnect error %d\n", rc);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200597#ifdef CONFIG_CIFS_DFS_UPCALL
598 reconn_inval_dfs_target(server, cifs_sb, &tgt_list,
599 &tgt_it);
600#endif
Paulo Alcantara28eb24f2018-11-20 15:16:36 -0200601 rc = reconn_set_ipaddr(server);
602 if (rc) {
603 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
604 __func__, rc);
605 }
Federico Sauter4afe2602015-03-17 17:45:28 +0100606 mutex_unlock(&server->srv_mutex);
Steve French0cb766a2005-04-28 22:41:11 -0700607 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 } else {
609 atomic_inc(&tcpSesReconnectCount);
Pavel Shilovsky335b7b62019-01-16 11:12:41 -0800610 set_credits(server, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000612 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000613 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000614 spin_unlock(&GlobalMid_Lock);
Federico Sauter4afe2602015-03-17 17:45:28 +0100615 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400617 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500618
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200619#ifdef CONFIG_CIFS_DFS_UPCALL
620 if (tgt_it) {
621 rc = dfs_cache_noreq_update_tgthint(cifs_sb->origin_fullpath + 1,
622 tgt_it);
623 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000624 cifs_server_dbg(VFS, "%s: failed to update DFS target hint: rc = %d\n",
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200625 __func__, rc);
626 }
627 rc = dfs_cache_update_vol(cifs_sb->origin_fullpath, server);
628 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000629 cifs_server_dbg(VFS, "%s: failed to update vol info in DFS cache: rc = %d\n",
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200630 __func__, rc);
631 }
Paulo Alcantara23324402018-11-20 14:37:18 -0200632 dfs_cache_free_tgts(&tgt_list);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200633 }
Paulo Alcantara93d5cb52018-11-14 17:13:25 -0200634#endif
Sachin Prabhub8c60012016-10-20 19:52:24 -0400635 if (server->tcpStatus == CifsNeedNegotiate)
636 mod_delayed_work(cifsiod_wq, &server->echo, 0);
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return rc;
639}
640
Jeff Laytonc74093b2011-01-11 07:24:23 -0500641static void
642cifs_echo_request(struct work_struct *work)
643{
644 int rc;
645 struct TCP_Server_Info *server = container_of(work,
646 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400647 unsigned long echo_interval;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500648
Jeff Layton247ec9b2011-02-04 17:09:50 -0500649 /*
Sachin Prabhub8c60012016-10-20 19:52:24 -0400650 * If we need to renegotiate, set echo interval to zero to
651 * immediately call echo service where we can renegotiate.
652 */
653 if (server->tcpStatus == CifsNeedNegotiate)
654 echo_interval = 0;
655 else
656 echo_interval = server->echo_interval;
657
658 /*
659 * We cannot send an echo if it is disabled.
660 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500661 */
Steve French4fcd1812016-06-22 20:12:05 -0500662
663 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400664 server->tcpStatus == CifsExiting ||
665 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400666 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600667 time_before(jiffies, server->lstrp + echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500668 goto requeue_echo;
669
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400670 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500671 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500672 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
673 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500674
675requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400676 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500677}
678
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400679static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400680allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400681{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400682 if (!server->bigbuf) {
683 server->bigbuf = (char *)cifs_buf_get();
684 if (!server->bigbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000685 cifs_server_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400686 msleep(3000);
687 /* retry will check if exiting */
688 return false;
689 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400690 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400691 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400692 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400693 }
694
Jeff Layton2a37ef92011-10-19 15:29:23 -0400695 if (!server->smallbuf) {
696 server->smallbuf = (char *)cifs_small_buf_get();
697 if (!server->smallbuf) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000698 cifs_server_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400699 msleep(1000);
700 /* retry will check if exiting */
701 return false;
702 }
703 /* beginning of smb buffer is cleared in our buf_get */
704 } else {
705 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400706 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400707 }
708
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400709 return true;
710}
711
Jeff Laytonba749e62011-10-11 06:41:32 -0400712static bool
713server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400714{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300715 /*
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000716 * We need to wait 3 echo intervals to make sure we handle such
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300717 * situations right:
718 * 1s client sends a normal SMB request
Ronnie Sahlbergbecc2ba2019-07-24 11:43:49 +1000719 * 2s client gets a response
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300720 * 30s echo workqueue job pops, and decides we got a response recently
721 * and don't need to send another
722 * ...
723 * 65s kernel_recvmsg times out, and we see that we haven't gotten
724 * a response in >60s.
725 */
Samuel Cabrero76e75272017-07-11 12:44:39 +0200726 if ((server->tcpStatus == CifsGood ||
727 server->tcpStatus == CifsNeedNegotiate) &&
Ronnie Sahlbergf2caf902019-07-06 06:52:46 +1000728 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000729 cifs_server_dbg(VFS, "has not responded in %lu seconds. Reconnecting...\n",
730 (3 * server->echo_interval) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400731 cifs_reconnect(server);
732 wake_up(&server->response_q);
733 return true;
734 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400735
Jeff Laytonba749e62011-10-11 06:41:32 -0400736 return false;
737}
738
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800739static inline bool
740zero_credits(struct TCP_Server_Info *server)
741{
742 int val;
743
744 spin_lock(&server->req_lock);
745 val = server->credits + server->echo_credits + server->oplock_credits;
746 if (server->in_flight == 0 && val == 0) {
747 spin_unlock(&server->req_lock);
748 return true;
749 }
750 spin_unlock(&server->req_lock);
751 return false;
752}
753
Al Viro71335662016-01-09 19:54:50 -0500754static int
755cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400756{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400757 int length = 0;
758 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400759
Al Viro71335662016-01-09 19:54:50 -0500760 smb_msg->msg_control = NULL;
761 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400762
Al Viro71335662016-01-09 19:54:50 -0500763 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500764 try_to_freeze();
765
Pavel Shilovskyef68e832019-01-18 17:25:36 -0800766 /* reconnect if no credits and no requests in flight */
767 if (zero_credits(server)) {
768 cifs_reconnect(server);
769 return -ECONNABORTED;
770 }
771
Al Viro71335662016-01-09 19:54:50 -0500772 if (server_unresponsive(server))
773 return -ECONNABORTED;
Long Li2fef1372017-11-22 17:38:41 -0700774 if (cifs_rdma_enabled(server) && server->smbd_conn)
775 length = smbd_recv(server->smbd_conn, smb_msg);
776 else
777 length = sock_recvmsg(server->ssocket, smb_msg, 0);
Al Viro71335662016-01-09 19:54:50 -0500778
779 if (server->tcpStatus == CifsExiting)
780 return -ESHUTDOWN;
781
782 if (server->tcpStatus == CifsNeedReconnect) {
783 cifs_reconnect(server);
784 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400785 }
786
Al Viro71335662016-01-09 19:54:50 -0500787 if (length == -ERESTARTSYS ||
788 length == -EAGAIN ||
789 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400790 /*
791 * Minimum sleep to prevent looping, allowing socket
792 * to clear and app threads to set tcpStatus
793 * CifsNeedReconnect if server hung.
794 */
795 usleep_range(1000, 2000);
796 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400797 continue;
Al Viro71335662016-01-09 19:54:50 -0500798 }
799
800 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500801 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400802 cifs_reconnect(server);
Al Viro71335662016-01-09 19:54:50 -0500803 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400804 }
805 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400806 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400807}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400808
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400809int
810cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
811 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400812{
Al Viro71335662016-01-09 19:54:50 -0500813 struct msghdr smb_msg;
814 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
David Howellsaa563d72018-10-20 00:57:56 +0100815 iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400816
Al Viro71335662016-01-09 19:54:50 -0500817 return cifs_readv_from_socket(server, &smb_msg);
818}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400819
Al Viro71335662016-01-09 19:54:50 -0500820int
821cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
Long Li1dbe3462018-05-30 12:47:55 -0700822 unsigned int page_offset, unsigned int to_read)
Al Viro71335662016-01-09 19:54:50 -0500823{
824 struct msghdr smb_msg;
Long Li1dbe3462018-05-30 12:47:55 -0700825 struct bio_vec bv = {
826 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
David Howellsaa563d72018-10-20 00:57:56 +0100827 iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read);
Al Viro71335662016-01-09 19:54:50 -0500828 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400829}
830
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400831static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400832is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400833{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400834 /*
835 * The first byte big endian of the length field,
836 * is actually not part of the length but the type
837 * with the most common, zero, as regular data.
838 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400839 switch (type) {
840 case RFC1002_SESSION_MESSAGE:
841 /* Regular SMB response */
842 return true;
843 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500844 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400845 break;
846 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500847 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400848 break;
849 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400850 /*
851 * We get this from Windows 98 instead of an error on
852 * SMB negprot response.
853 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500854 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400855 /* give server a second to clean up */
856 msleep(1000);
857 /*
858 * Always try 445 first on reconnect since we get NACK
859 * on some if we ever connected to port 139 (the NACK
860 * is since we do not begin with RFC1001 session
861 * initialize frame).
862 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400863 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400864 cifs_reconnect(server);
865 wake_up(&server->response_q);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400866 break;
867 default:
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +1000868 cifs_server_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400869 cifs_reconnect(server);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400870 }
871
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400872 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400873}
874
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400875void
876dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400877{
878#ifdef CONFIG_CIFS_STATS2
879 mid->when_received = jiffies;
880#endif
881 spin_lock(&GlobalMid_Lock);
882 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400883 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400884 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400885 mid->mid_state = MID_RESPONSE_MALFORMED;
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000886 /*
887 * Trying to handle/dequeue a mid after the send_recv()
888 * function has finished processing it is a bug.
889 */
890 if (mid->mid_flags & MID_DELETED)
891 printk_once(KERN_WARNING
892 "trying to dequeue a deleted mid\n");
893 else
894 list_del_init(&mid->qhead);
Jeff Laytonea1f4502011-10-19 15:29:05 -0400895 spin_unlock(&GlobalMid_Lock);
896}
897
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400898static void
899handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400900 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400901{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400902 if (server->ops->check_trans2 &&
903 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400904 return;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400905 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400906 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400907 /* Was previous buf put in mpx struct for multi-rsp? */
908 if (!mid->multiRsp) {
909 /* smb buffer will be freed by user thread */
910 if (server->large_buf)
911 server->bigbuf = NULL;
912 else
913 server->smallbuf = NULL;
914 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400915 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400916}
917
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400918static void clean_demultiplex_info(struct TCP_Server_Info *server)
919{
920 int length;
921
922 /* take it off the list, if it's not already */
923 spin_lock(&cifs_tcp_ses_lock);
924 list_del_init(&server->tcp_ses_list);
925 spin_unlock(&cifs_tcp_ses_lock);
926
927 spin_lock(&GlobalMid_Lock);
928 server->tcpStatus = CifsExiting;
929 spin_unlock(&GlobalMid_Lock);
930 wake_up_all(&server->response_q);
931
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400932 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300933 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400934 if (server->credits <= 0)
935 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300936 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400937 /*
938 * Although there should not be any requests blocked on this queue it
939 * can not hurt to be paranoid and try to wake up requests that may
940 * haven been blocked when more than 50 at time were on the wire to the
941 * same server - they now will see the session is in exit state and get
942 * out of SendReceive.
943 */
944 wake_up_all(&server->request_q);
945 /* give those requests time to exit */
946 msleep(125);
Long Li050b8c32019-04-04 11:35:42 -0500947 if (cifs_rdma_enabled(server))
948 smbd_destroy(server);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400949 if (server->ssocket) {
950 sock_release(server->ssocket);
951 server->ssocket = NULL;
952 }
953
954 if (!list_empty(&server->pending_mid_q)) {
955 struct list_head dispose_list;
956 struct mid_q_entry *mid_entry;
957 struct list_head *tmp, *tmp2;
958
959 INIT_LIST_HEAD(&dispose_list);
960 spin_lock(&GlobalMid_Lock);
961 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
962 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500963 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400964 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400965 list_move(&mid_entry->qhead, &dispose_list);
966 }
967 spin_unlock(&GlobalMid_Lock);
968
969 /* now walk dispose list and issue callbacks */
970 list_for_each_safe(tmp, tmp2, &dispose_list) {
971 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500972 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400973 list_del_init(&mid_entry->qhead);
974 mid_entry->callback(mid_entry);
975 }
976 /* 1/8th of sec is more than enough time for them to exit */
977 msleep(125);
978 }
979
980 if (!list_empty(&server->pending_mid_q)) {
981 /*
982 * mpx threads have not exited yet give them at least the smb
983 * send timeout time for long ops.
984 *
985 * Due to delays on oplock break requests, we need to wait at
986 * least 45 seconds before giving up on a request getting a
987 * response and going ahead and killing cifsd.
988 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500989 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400990 msleep(46000);
991 /*
992 * If threads still have not exited they are probably never
993 * coming home not much else we can do but free the memory.
994 */
995 }
996
997 kfree(server->hostname);
998 kfree(server);
999
1000 length = atomic_dec_return(&tcpSesAllocCount);
1001 if (length > 0)
David Rientjes11d83362015-04-14 15:48:21 -07001002 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +04001003}
1004
Pavel Shilovskye7015fb2011-08-01 13:19:41 +04001005static int
Jeff Laytone9097ab2011-10-19 15:29:40 -04001006standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1007{
1008 int length;
1009 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +10001010 unsigned int pdu_length = server->pdu_size;
Jeff Laytone9097ab2011-10-19 15:29:40 -04001011
1012 /* make sure this will fit in a large buffer */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001013 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
1014 server->vals->header_preamble_size) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001015 cifs_server_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
Jeff Laytone9097ab2011-10-19 15:29:40 -04001016 cifs_reconnect(server);
1017 wake_up(&server->response_q);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +04001018 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -04001019 }
1020
1021 /* switch to large buffer if too big for a small one */
1022 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
1023 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001024 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -04001025 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -04001026 }
1027
1028 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +04001029 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001030 pdu_length - HEADER_SIZE(server) + 1
1031 + server->vals->header_preamble_size);
1032
Jeff Laytone9097ab2011-10-19 15:29:40 -04001033 if (length < 0)
1034 return length;
1035 server->total_read += length;
1036
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001037 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -04001038
Pavel Shilovsky4326ed22016-11-17 15:24:46 -08001039 return cifs_handle_standard(server, mid);
1040}
1041
1042int
1043cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
1044{
1045 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
1046 int length;
1047
Jeff Laytone9097ab2011-10-19 15:29:40 -04001048 /*
1049 * We know that we received enough to get to the MID as we
1050 * checked the pdu_length earlier. Now check to see
1051 * if the rest of the header is OK. We borrow the length
1052 * var for the rest of the loop to avoid a new stack var.
1053 *
1054 * 48 bytes is enough to display the header and a little bit
1055 * into the payload for debugging purposes.
1056 */
Steve French373512e2015-12-18 13:05:30 -06001057 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -04001058 if (length != 0)
1059 cifs_dump_mem("Bad SMB: ", buf,
1060 min_t(unsigned int, server->total_read, 48));
1061
Pavel Shilovsky511c54a2017-07-08 14:32:00 -07001062 if (server->ops->is_session_expired &&
1063 server->ops->is_session_expired(buf)) {
1064 cifs_reconnect(server);
1065 wake_up(&server->response_q);
1066 return -1;
1067 }
1068
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001069 if (server->ops->is_status_pending &&
Pavel Shilovsky66265f12019-01-23 17:11:16 -08001070 server->ops->is_status_pending(buf, server))
Pavel Shilovsky2e44b282012-09-18 16:20:33 -07001071 return -1;
1072
Jeff Laytonff4fa4a2012-02-07 06:31:05 -05001073 if (!mid)
1074 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -04001075
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001076 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -05001077 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -04001078}
1079
Ronnie Sahlbergeca00452019-02-05 12:56:44 +10001080static void
1081smb2_add_credits_from_hdr(char *buffer, struct TCP_Server_Info *server)
1082{
1083 struct smb2_sync_hdr *shdr = (struct smb2_sync_hdr *)buffer;
1084
1085 /*
1086 * SMB1 does not use credits.
1087 */
1088 if (server->vals->header_preamble_size)
1089 return;
1090
1091 if (shdr->CreditRequest) {
1092 spin_lock(&server->req_lock);
1093 server->credits += le16_to_cpu(shdr->CreditRequest);
1094 spin_unlock(&server->req_lock);
1095 wake_up(&server->request_q);
1096 }
1097}
1098
1099
Jeff Laytone9097ab2011-10-19 15:29:40 -04001100static int
Al Viro7c97c202011-06-21 08:51:28 -04001101cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001103 int i, num_mids, length;
Al Viro7c97c202011-06-21 08:51:28 -04001104 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001105 unsigned int pdu_length;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001106 unsigned int next_offset;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001107 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001108 struct task_struct *task_to_wake = NULL;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001109 struct mid_q_entry *mids[MAX_COMPOUND];
1110 char *bufs[MAX_COMPOUND];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 current->flags |= PF_MEMALLOC;
Joe Perchesf96637b2013-05-04 22:12:25 -05001113 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -04001114
1115 length = atomic_inc_return(&tcpSesAllocCount);
1116 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -07001117 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001119 set_freezable();
Eric W. Biederman33da8e72019-08-16 12:33:54 -05001120 allow_kernel_signal(SIGKILL);
Jeff Layton469ee612008-10-16 18:46:39 +00001121 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -07001122 if (try_to_freeze())
1123 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -07001124
Jeff Layton2a37ef92011-10-19 15:29:23 -04001125 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +04001126 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -07001127
Jeff Layton2a37ef92011-10-19 15:29:23 -04001128 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001129 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +00001130 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +00001131
Jeff Laytone28bc5b2011-10-19 15:30:07 -04001132 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -04001133 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +00001134 continue;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +10001135
1136 if (server->vals->header_preamble_size == 0)
1137 server->total_read = 0;
1138 else
1139 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -07001140
Pavel Shilovsky98bac622011-08-01 13:19:42 +04001141 /*
1142 * The right amount was read from socket - 4 bytes,
1143 * so we can now interpret the length field.
1144 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001145 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -07001146
Joe Perchesf96637b2013-05-04 22:12:25 -05001147 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -04001148 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001149 continue;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001150next_pdu:
1151 server->pdu_size = pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -07001152
Jeff Layton89482a52011-10-19 15:28:57 -04001153 /* make sure we have enough to get to the MID */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001154 if (server->pdu_size < HEADER_SIZE(server) - 1 -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001155 server->vals->header_preamble_size) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001156 cifs_server_dbg(VFS, "SMB response too short (%u bytes)\n",
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001157 server->pdu_size);
Jeff Layton89482a52011-10-19 15:28:57 -04001158 cifs_reconnect(server);
1159 wake_up(&server->response_q);
1160 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -07001161 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +04001162
Jeff Layton89482a52011-10-19 15:28:57 -04001163 /* read down to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +11001164 length = cifs_read_from_socket(server,
1165 buf + server->vals->header_preamble_size,
1166 HEADER_SIZE(server) - 1
1167 - server->vals->header_preamble_size);
Jeff Layton89482a52011-10-19 15:28:57 -04001168 if (length < 0)
1169 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001170 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -04001171
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001172 if (server->ops->next_header) {
1173 next_offset = server->ops->next_header(buf);
1174 if (next_offset)
1175 server->pdu_size = next_offset;
1176 }
1177
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001178 memset(mids, 0, sizeof(mids));
1179 memset(bufs, 0, sizeof(bufs));
1180 num_mids = 0;
1181
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001182 if (server->ops->is_transform_hdr &&
1183 server->ops->receive_transform &&
1184 server->ops->is_transform_hdr(buf)) {
1185 length = server->ops->receive_transform(server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001186 mids,
1187 bufs,
1188 &num_mids);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001189 } else {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001190 mids[0] = server->ops->find_mid(server, buf);
1191 bufs[0] = buf;
Steve French7af929d2018-10-02 18:54:09 -05001192 num_mids = 1;
Jeff Laytonc8054eb2011-10-19 15:29:31 -04001193
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001194 if (!mids[0] || !mids[0]->receive)
1195 length = standard_receive3(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001196 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001197 length = mids[0]->receive(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001198 }
Jeff Layton44d22d82011-10-19 15:29:49 -04001199
Lars Persson696e4202018-06-25 14:05:25 +02001200 if (length < 0) {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001201 for (i = 0; i < num_mids; i++)
1202 if (mids[i])
1203 cifs_mid_q_entry_release(mids[i]);
Steve Frenche4eb2952005-04-28 22:41:09 -07001204 continue;
Lars Persson696e4202018-06-25 14:05:25 +02001205 }
Steve Frenche4eb2952005-04-28 22:41:09 -07001206
Steve Frenchfda35942011-01-20 18:06:34 +00001207 server->lstrp = jiffies;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001208
1209 for (i = 0; i < num_mids; i++) {
1210 if (mids[i] != NULL) {
1211 mids[i]->resp_buf_size = server->pdu_size;
1212 if ((mids[i]->mid_flags & MID_WAIT_CANCELLED) &&
1213 mids[i]->mid_state == MID_RESPONSE_RECEIVED &&
1214 server->ops->handle_cancelled_mid)
1215 server->ops->handle_cancelled_mid(
1216 mids[i]->resp_buf,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08001217 server);
1218
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001219 if (!mids[i]->multiRsp || mids[i]->multiEnd)
1220 mids[i]->callback(mids[i]);
Lars Persson696e4202018-06-25 14:05:25 +02001221
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001222 cifs_mid_q_entry_release(mids[i]);
1223 } else if (server->ops->is_oplock_break &&
1224 server->ops->is_oplock_break(bufs[i],
1225 server)) {
Ronnie Sahlbergeca00452019-02-05 12:56:44 +10001226 smb2_add_credits_from_hdr(bufs[i], server);
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001227 cifs_dbg(FYI, "Received oplock break\n");
1228 } else {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10001229 cifs_server_dbg(VFS, "No task to wake, unknown frame "
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001230 "received! NumMids %d\n",
1231 atomic_read(&midCount));
1232 cifs_dump_mem("Received Data is: ", bufs[i],
1233 HEADER_SIZE(server));
Ronnie Sahlberg3e272572019-07-06 06:43:08 +10001234 smb2_add_credits_from_hdr(bufs[i], server);
Steve French39798772006-05-31 22:40:51 +00001235#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001236 if (server->ops->dump_detail)
1237 server->ops->dump_detail(bufs[i],
1238 server);
1239 cifs_dump_mids(server);
Steve French39798772006-05-31 22:40:51 +00001240#endif /* CIFS_DEBUG2 */
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001241 }
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001242 }
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001243
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001244 if (pdu_length > server->pdu_size) {
1245 if (!allocate_buffers(server))
1246 continue;
1247 pdu_length -= server->pdu_size;
1248 server->total_read = 0;
1249 server->large_buf = false;
1250 buf = server->smallbuf;
1251 goto next_pdu;
Steve Frenche4eb2952005-04-28 22:41:09 -07001252 }
1253 } /* end while !EXITING */
1254
Justin P. Mattockfd62cb72011-02-24 22:15:02 -08001255 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -04001256 cifs_buf_release(server->bigbuf);
1257 if (server->smallbuf) /* no sense logging a debug message if NULL */
1258 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001260 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +04001261 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001262
1263 /* if server->tsk was NULL then wait for a signal before exiting */
1264 if (!task_to_wake) {
1265 set_current_state(TASK_INTERRUPTIBLE);
1266 while (!signal_pending(current)) {
1267 schedule();
1268 set_current_state(TASK_INTERRUPTIBLE);
1269 }
1270 set_current_state(TASK_RUNNING);
1271 }
1272
Jeff Layton0468a2c2008-12-01 07:09:35 -05001273 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
Jeff Laytonc359cf32007-11-16 22:22:06 +00001276/* extract the host portion of the UNC string */
1277static char *
1278extract_hostname(const char *unc)
1279{
1280 const char *src;
1281 char *dst, *delim;
1282 unsigned int len;
1283
1284 /* skip double chars at beginning of string */
1285 /* BB: check validity of these bytes? */
Paulo Alcantarac34fea52018-11-14 14:03:40 -02001286 if (strlen(unc) < 3)
1287 return ERR_PTR(-EINVAL);
1288 for (src = unc; *src && *src == '\\'; src++)
1289 ;
1290 if (!*src)
1291 return ERR_PTR(-EINVAL);
Jeff Laytonc359cf32007-11-16 22:22:06 +00001292
1293 /* delimiter between hostname and sharename is always '\\' now */
1294 delim = strchr(src, '\\');
1295 if (!delim)
1296 return ERR_PTR(-EINVAL);
1297
1298 len = delim - src;
1299 dst = kmalloc((len + 1), GFP_KERNEL);
1300 if (dst == NULL)
1301 return ERR_PTR(-ENOMEM);
1302
1303 memcpy(dst, src, len);
1304 dst[len] = '\0';
1305
1306 return dst;
1307}
1308
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001309static int get_option_ul(substring_t args[], unsigned long *option)
1310{
1311 int rc;
1312 char *string;
1313
1314 string = match_strdup(args);
1315 if (string == NULL)
1316 return -ENOMEM;
Sachin Prabhubfa890a2012-04-13 14:04:32 +01001317 rc = kstrtoul(string, 0, option);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001318 kfree(string);
1319
1320 return rc;
1321}
1322
Eric W. Biederman3da46562013-02-06 01:37:39 -08001323static int get_option_uid(substring_t args[], kuid_t *result)
1324{
1325 unsigned long value;
1326 kuid_t uid;
1327 int rc;
1328
1329 rc = get_option_ul(args, &value);
1330 if (rc)
1331 return rc;
1332
1333 uid = make_kuid(current_user_ns(), value);
1334 if (!uid_valid(uid))
1335 return -EINVAL;
1336
1337 *result = uid;
1338 return 0;
1339}
1340
1341static int get_option_gid(substring_t args[], kgid_t *result)
1342{
1343 unsigned long value;
1344 kgid_t gid;
1345 int rc;
1346
1347 rc = get_option_ul(args, &value);
1348 if (rc)
1349 return rc;
1350
1351 gid = make_kgid(current_user_ns(), value);
1352 if (!gid_valid(gid))
1353 return -EINVAL;
1354
1355 *result = gid;
1356 return 0;
1357}
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001358
1359static int cifs_parse_security_flavors(char *value,
1360 struct smb_vol *vol)
1361{
1362
1363 substring_t args[MAX_OPT_ARGS];
1364
Jeff Layton1e3cc572013-06-10 17:12:23 -05001365 /*
1366 * With mount options, the last one should win. Reset any existing
1367 * settings back to default.
1368 */
1369 vol->sectype = Unspecified;
1370 vol->sign = false;
1371
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001372 switch (match_token(value, cifs_secflavor_tokens, args)) {
Jeff Layton3f618222013-06-12 19:52:14 -05001373 case Opt_sec_krb5p:
1374 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1375 return 1;
1376 case Opt_sec_krb5i:
1377 vol->sign = true;
1378 /* Fallthrough */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001379 case Opt_sec_krb5:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001380 vol->sectype = Kerberos;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001381 break;
1382 case Opt_sec_ntlmsspi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001383 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001384 /* Fallthrough */
1385 case Opt_sec_ntlmssp:
1386 vol->sectype = RawNTLMSSP;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001387 break;
1388 case Opt_sec_ntlmi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001389 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001390 /* Fallthrough */
1391 case Opt_ntlm:
1392 vol->sectype = NTLM;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001393 break;
1394 case Opt_sec_ntlmv2i:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001395 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001396 /* Fallthrough */
1397 case Opt_sec_ntlmv2:
1398 vol->sectype = NTLMv2;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001399 break;
1400#ifdef CONFIG_CIFS_WEAK_PW_HASH
1401 case Opt_sec_lanman:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001402 vol->sectype = LANMAN;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001403 break;
1404#endif
1405 case Opt_sec_none:
1406 vol->nullauth = 1;
1407 break;
1408 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001409 cifs_dbg(VFS, "bad security option: %s\n", value);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001410 return 1;
1411 }
1412
1413 return 0;
1414}
1415
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416static int
Jeff Layton15b6a472012-05-16 07:50:15 -04001417cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1418{
1419 substring_t args[MAX_OPT_ARGS];
1420
1421 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1422 case Opt_cache_loose:
1423 vol->direct_io = false;
1424 vol->strict_io = false;
Steve French83bbfa72019-08-27 23:58:54 -05001425 vol->cache_ro = false;
Steve French41e033f2019-08-30 02:12:41 -05001426 vol->cache_rw = false;
Jeff Layton15b6a472012-05-16 07:50:15 -04001427 break;
1428 case Opt_cache_strict:
1429 vol->direct_io = false;
1430 vol->strict_io = true;
Steve French83bbfa72019-08-27 23:58:54 -05001431 vol->cache_ro = false;
Steve French41e033f2019-08-30 02:12:41 -05001432 vol->cache_rw = false;
Jeff Layton15b6a472012-05-16 07:50:15 -04001433 break;
1434 case Opt_cache_none:
1435 vol->direct_io = true;
1436 vol->strict_io = false;
Steve French83bbfa72019-08-27 23:58:54 -05001437 vol->cache_ro = false;
Steve French41e033f2019-08-30 02:12:41 -05001438 vol->cache_rw = false;
Steve French83bbfa72019-08-27 23:58:54 -05001439 break;
1440 case Opt_cache_ro:
1441 vol->direct_io = false;
1442 vol->strict_io = false;
1443 vol->cache_ro = true;
Steve French41e033f2019-08-30 02:12:41 -05001444 vol->cache_rw = false;
1445 break;
1446 case Opt_cache_rw:
1447 vol->direct_io = false;
1448 vol->strict_io = false;
1449 vol->cache_ro = false;
1450 vol->cache_rw = true;
Jeff Layton15b6a472012-05-16 07:50:15 -04001451 break;
1452 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001453 cifs_dbg(VFS, "bad cache= option: %s\n", value);
Jeff Layton15b6a472012-05-16 07:50:15 -04001454 return 1;
1455 }
1456 return 0;
1457}
1458
1459static int
Steve Frenchc7c137b2018-06-06 17:59:29 -05001460cifs_parse_smb_version(char *value, struct smb_vol *vol, bool is_smb3)
Jeff Layton23db65f2012-05-15 12:20:51 -04001461{
1462 substring_t args[MAX_OPT_ARGS];
1463
1464 switch (match_token(value, cifs_smb_version_tokens, args)) {
Steve French74204512018-06-19 14:34:08 -05001465#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
Jeff Layton23db65f2012-05-15 12:20:51 -04001466 case Smb_1:
Steve Frenchf92a7202018-05-24 04:11:07 -05001467 if (disable_legacy_dialects) {
1468 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1469 return 1;
1470 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001471 if (is_smb3) {
1472 cifs_dbg(VFS, "vers=1.0 (cifs) not permitted when mounting with smb3\n");
1473 return 1;
1474 }
Jeff Layton23db65f2012-05-15 12:20:51 -04001475 vol->ops = &smb1_operations;
1476 vol->vals = &smb1_values;
1477 break;
Steve Frenchdd446b12012-11-28 23:21:06 -06001478 case Smb_20:
Steve Frenchf92a7202018-05-24 04:11:07 -05001479 if (disable_legacy_dialects) {
1480 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1481 return 1;
1482 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001483 if (is_smb3) {
1484 cifs_dbg(VFS, "vers=2.0 not permitted when mounting with smb3\n");
1485 return 1;
1486 }
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001487 vol->ops = &smb20_operations;
Steve Frenchdd446b12012-11-28 23:21:06 -06001488 vol->vals = &smb20_values;
1489 break;
Steve French74204512018-06-19 14:34:08 -05001490#else
1491 case Smb_1:
1492 cifs_dbg(VFS, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
1493 return 1;
1494 case Smb_20:
1495 cifs_dbg(VFS, "vers=2.0 mount not permitted when legacy dialects disabled\n");
1496 return 1;
1497#endif /* CIFS_ALLOW_INSECURE_LEGACY */
Steve French1080ef72011-02-24 18:07:19 +00001498 case Smb_21:
1499 vol->ops = &smb21_operations;
1500 vol->vals = &smb21_values;
1501 break;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001502 case Smb_30:
Steve French38107d42012-12-08 22:08:06 -06001503 vol->ops = &smb30_operations;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001504 vol->vals = &smb30_values;
1505 break;
Steve French20b6d8b2013-06-12 22:48:41 -05001506 case Smb_302:
1507 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1508 vol->vals = &smb302_values;
1509 break;
Steve French5f7fbf72014-12-17 22:52:58 -06001510 case Smb_311:
Steve Frenchaab18932015-06-23 23:37:11 -05001511 vol->ops = &smb311_operations;
Steve French5f7fbf72014-12-17 22:52:58 -06001512 vol->vals = &smb311_values;
1513 break;
Steve French9764c022017-09-17 10:41:35 -05001514 case Smb_3any:
1515 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1516 vol->vals = &smb3any_values;
1517 break;
1518 case Smb_default:
1519 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1520 vol->vals = &smbdefault_values;
1521 break;
Jeff Layton23db65f2012-05-15 12:20:51 -04001522 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001523 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
Jeff Layton23db65f2012-05-15 12:20:51 -04001524 return 1;
1525 }
1526 return 0;
1527}
1528
Jeff Laytond387a5c2012-12-10 06:10:46 -05001529/*
1530 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1531 * fields with the result. Returns 0 on success and an error otherwise.
1532 */
1533static int
1534cifs_parse_devname(const char *devname, struct smb_vol *vol)
1535{
1536 char *pos;
1537 const char *delims = "/\\";
1538 size_t len;
1539
Yao Liu68e26722019-01-28 19:47:28 +08001540 if (unlikely(!devname || !*devname)) {
1541 cifs_dbg(VFS, "Device name not specified.\n");
1542 return -EINVAL;
1543 }
1544
Jeff Laytond387a5c2012-12-10 06:10:46 -05001545 /* make sure we have a valid UNC double delimiter prefix */
1546 len = strspn(devname, delims);
1547 if (len != 2)
1548 return -EINVAL;
1549
1550 /* find delimiter between host and sharename */
1551 pos = strpbrk(devname + 2, delims);
1552 if (!pos)
1553 return -EINVAL;
1554
1555 /* skip past delimiter */
1556 ++pos;
1557
1558 /* now go until next delimiter or end of string */
1559 len = strcspn(pos, delims);
1560
1561 /* move "pos" up to delimiter or NULL */
1562 pos += len;
1563 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1564 if (!vol->UNC)
1565 return -ENOMEM;
1566
1567 convert_delimiter(vol->UNC, '\\');
1568
Sachin Prabhu11e31642016-02-08 13:44:01 +05301569 /* skip any delimiter */
1570 if (*pos == '/' || *pos == '\\')
1571 pos++;
1572
1573 /* If pos is NULL then no prepath */
1574 if (!*pos)
Jeff Laytond387a5c2012-12-10 06:10:46 -05001575 return 0;
1576
1577 vol->prepath = kstrdup(pos, GFP_KERNEL);
1578 if (!vol->prepath)
1579 return -ENOMEM;
1580
1581 return 0;
1582}
1583
Jeff Layton23db65f2012-05-15 12:20:51 -04001584static int
Sean Finneyb9468452011-04-11 13:19:32 +00001585cifs_parse_mount_options(const char *mountdata, const char *devname,
Steve Frenchc7c137b2018-06-06 17:59:29 -05001586 struct smb_vol *vol, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001588 char *data, *end;
Vasily Averin957df452011-06-06 11:33:12 +04001589 char *mountdata_copy = NULL, *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 unsigned int temp_len, i, j;
1591 char separator[2];
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001592 short int override_uid = -1;
1593 short int override_gid = -1;
1594 bool uid_specified = false;
1595 bool gid_specified = false;
Jeff Laytond8162552012-03-23 14:40:56 -04001596 bool sloppy = false;
1597 char *invalid = NULL;
Jeff Layton88463992010-11-22 15:31:03 -05001598 char *nodename = utsname()->nodename;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001599 char *string = NULL;
1600 char *tmp_end, *value;
1601 char delim;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001602 bool got_ip = false;
Steve French7e682f72017-08-31 21:34:24 -05001603 bool got_version = false;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001604 unsigned short port = 0;
1605 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
1607 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +00001608 separator[1] = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001609 delim = separator[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Jeff Layton6ee95422012-11-26 11:09:57 -05001611 /* ensure we always start with zeroed-out smb_vol */
1612 memset(vol, 0, sizeof(*vol));
1613
Jeff Layton88463992010-11-22 15:31:03 -05001614 /*
1615 * does not have to be perfect mapping since field is
1616 * informational, only used for servers that do not support
1617 * port 445 and it can be overridden at mount time
1618 */
Jeff Layton1397f2e2011-01-07 11:30:28 -05001619 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1620 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
Jeff Layton88463992010-11-22 15:31:03 -05001621 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1622
Jeff Layton1397f2e2011-01-07 11:30:28 -05001623 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -07001624 /* null target name indicates to use *SMBSERVR default called name
1625 if we end up sending RFC1001 session initialize */
1626 vol->target_rfc1001_name[0] = 0;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04001627 vol->cred_uid = current_uid();
1628 vol->linux_uid = current_uid();
David Howellsa001e5b2008-11-14 10:38:47 +11001629 vol->linux_gid = current_gid();
Steve Frenche8506d22019-02-28 21:32:15 -06001630 vol->bsize = 1024 * 1024; /* can improve cp performance significantly */
Steve French2baa2682014-09-27 02:19:01 -05001631 /*
1632 * default to SFM style remapping of seven reserved characters
1633 * unless user overrides it or we negotiate CIFS POSIX where
1634 * it is unnecessary. Can not simultaneously use more than one mapping
1635 * since then readdir could list files that open could not open
1636 */
1637 vol->remap = true;
1638
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001639 /* default to only allowing write access to owner of the mount */
1640 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Jeremy Allisonac670552005-06-22 17:26:35 -07001643 /* default is always to request posix paths. */
1644 vol->posix_paths = 1;
Jeff Laytona0c92172009-05-27 15:40:47 -04001645 /* default to using server inode numbers where available */
1646 vol->server_ino = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001647
Jeff Layton1b359202012-09-19 15:20:27 -07001648 /* default is to use strict cifs caching semantics */
1649 vol->strict_io = true;
1650
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301651 vol->actimeo = CIFS_DEF_ACTIMEO;
1652
Steve Frenchca567eb2019-03-29 16:31:07 -05001653 /* Most clients set timeout to 0, allows server to use its default */
1654 vol->handle_timeout = 0; /* See MS-SMB2 spec section 2.2.14.2.12 */
1655
Steve French9764c022017-09-17 10:41:35 -05001656 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1657 vol->ops = &smb30_operations;
1658 vol->vals = &smbdefault_values;
Jeff Layton23db65f2012-05-15 12:20:51 -04001659
Rabin Vincentb782fcc2016-07-19 09:25:45 +02001660 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1661
Sean Finneyb9468452011-04-11 13:19:32 +00001662 if (!mountdata)
1663 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Sean Finneyb9468452011-04-11 13:19:32 +00001665 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1666 if (!mountdata_copy)
1667 goto cifs_parse_mount_err;
1668
1669 options = mountdata_copy;
Pavel Shilovsky4906e502011-04-14 22:00:56 +04001670 end = options + strlen(options);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001671
Steve French50c2f752007-07-13 00:33:32 +00001672 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001673 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 separator[0] = options[4];
1675 options += 5;
1676 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001677 cifs_dbg(FYI, "Null separator not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
1679 }
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001680 vol->backupuid_specified = false; /* no backup intent for a user */
1681 vol->backupgid_specified = false; /* no backup intent for a group */
Steve French50c2f752007-07-13 00:33:32 +00001682
Jeff Layton37d4f992013-05-24 07:40:05 -04001683 switch (cifs_parse_devname(devname, vol)) {
1684 case 0:
1685 break;
1686 case -ENOMEM:
1687 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1688 goto cifs_parse_mount_err;
1689 case -EINVAL:
1690 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1691 goto cifs_parse_mount_err;
1692 default:
1693 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1694 goto cifs_parse_mount_err;
Jeff Laytond387a5c2012-12-10 06:10:46 -05001695 }
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 while ((data = strsep(&options, separator)) != NULL) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001698 substring_t args[MAX_OPT_ARGS];
1699 unsigned long option;
1700 int token;
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (!*data)
1703 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001705 token = match_token(data, cifs_mount_option_tokens, args);
1706
1707 switch (token) {
1708
1709 /* Ingnore the following */
1710 case Opt_ignore:
1711 break;
1712
1713 /* Boolean values */
1714 case Opt_user_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 vol->no_xattr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001716 break;
1717 case Opt_nouser_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 vol->no_xattr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001719 break;
1720 case Opt_forceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001721 override_uid = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001722 break;
1723 case Opt_noforceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001724 override_uid = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001725 break;
Jeff Layton72bd4812012-10-03 16:02:36 -04001726 case Opt_forcegid:
1727 override_gid = 1;
1728 break;
1729 case Opt_noforcegid:
1730 override_gid = 0;
1731 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001732 case Opt_noblocksend:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001733 vol->noblocksnd = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001734 break;
1735 case Opt_noautotune:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001736 vol->noautotune = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001737 break;
1738 case Opt_hard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001740 break;
1741 case Opt_soft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001743 break;
1744 case Opt_perm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 vol->noperm = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001746 break;
1747 case Opt_noperm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 vol->noperm = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001749 break;
1750 case Opt_mapchars:
Steve French2baa2682014-09-27 02:19:01 -05001751 vol->sfu_remap = true;
1752 vol->remap = false; /* disable SFM mapping */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001753 break;
1754 case Opt_nomapchars:
Steve French2baa2682014-09-27 02:19:01 -05001755 vol->sfu_remap = false;
1756 break;
1757 case Opt_mapposix:
1758 vol->remap = true;
1759 vol->sfu_remap = false; /* disable SFU mapping */
1760 break;
1761 case Opt_nomapposix:
1762 vol->remap = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001763 break;
1764 case Opt_sfu:
Steve French50c2f752007-07-13 00:33:32 +00001765 vol->sfu_emul = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001766 break;
1767 case Opt_nosfu:
Steve French50c2f752007-07-13 00:33:32 +00001768 vol->sfu_emul = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001769 break;
1770 case Opt_nodfs:
Steve French2c1b8612008-10-16 18:35:21 +00001771 vol->nodfs = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001772 break;
1773 case Opt_posixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001774 vol->posix_paths = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001775 break;
1776 case Opt_noposixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001777 vol->posix_paths = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001778 break;
1779 case Opt_nounix:
Steve Frenchb3266142018-05-20 23:41:10 -05001780 if (vol->linux_ext)
1781 cifs_dbg(VFS,
1782 "conflicting unix mount options\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00001783 vol->no_linux_ext = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001784 break;
Steve Frenchb3266142018-05-20 23:41:10 -05001785 case Opt_unix:
1786 if (vol->no_linux_ext)
1787 cifs_dbg(VFS,
1788 "conflicting unix mount options\n");
1789 vol->linux_ext = 1;
1790 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001791 case Opt_nocase:
Steve French50c2f752007-07-13 00:33:32 +00001792 vol->nocase = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001793 break;
1794 case Opt_brl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001795 vol->nobrl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001796 break;
1797 case Opt_nobrl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001798 vol->nobrl = 1;
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001799 /*
1800 * turn off mandatory locking in mode
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001801 * if remote locking is turned off since the
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001802 * local vfs will do advisory
1803 */
Steve French50c2f752007-07-13 00:33:32 +00001804 if (vol->file_mode ==
1805 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001806 vol->file_mode = S_IALLUGO;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001807 break;
Steve French3d4ef9a2018-04-25 22:19:09 -05001808 case Opt_nohandlecache:
1809 vol->nohandlecache = 1;
1810 break;
1811 case Opt_handlecache:
1812 vol->nohandlecache = 0;
1813 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001814 case Opt_forcemandatorylock:
Steve French13a6e422008-12-02 17:24:33 +00001815 vol->mand_lock = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001816 break;
1817 case Opt_setuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 vol->setuids = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001819 break;
1820 case Opt_nosetuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 vol->setuids = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001822 break;
Steve French95932652016-09-23 01:36:34 -05001823 case Opt_setuidfromacl:
1824 vol->setuidfromacl = 1;
1825 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001826 case Opt_dynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001827 vol->dynperm = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001828 break;
1829 case Opt_nodynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001830 vol->dynperm = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001831 break;
1832 case Opt_nohard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001834 break;
1835 case Opt_nosoft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001837 break;
1838 case Opt_nointr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 vol->intr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001840 break;
1841 case Opt_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 vol->intr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001843 break;
1844 case Opt_nostrictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001845 vol->nostrictsync = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001846 break;
1847 case Opt_strictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001848 vol->nostrictsync = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001849 break;
1850 case Opt_serverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 vol->server_ino = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001852 break;
1853 case Opt_noserverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 vol->server_ino = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001855 break;
1856 case Opt_rwpidforward:
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001857 vol->rwpidforward = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001858 break;
Steve French412094a2019-06-24 02:01:42 -05001859 case Opt_modesid:
1860 vol->mode_ace = 1;
1861 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001862 case Opt_cifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001863 vol->cifs_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001864 break;
1865 case Opt_nocifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001866 vol->cifs_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001867 break;
1868 case Opt_acl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 vol->no_psx_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001870 break;
1871 case Opt_noacl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 vol->no_psx_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001873 break;
1874 case Opt_locallease:
Steve French84210e92008-10-23 04:42:37 +00001875 vol->local_lease = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001876 break;
1877 case Opt_sign:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001878 vol->sign = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001879 break;
1880 case Opt_seal:
Steve French95b1cb92008-05-15 16:44:38 +00001881 /* we do not do the following in secFlags because seal
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001882 * is a per tree connection (mount) not a per socket
1883 * or per-smb connection option in the protocol
1884 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1885 */
Steve French95b1cb92008-05-15 16:44:38 +00001886 vol->seal = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001887 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001888 case Opt_noac:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001889 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001890 break;
1891 case Opt_fsc:
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301892#ifndef CONFIG_CIFS_FSCACHE
Joe Perchesf96637b2013-05-04 22:12:25 -05001893 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001894 goto cifs_parse_mount_err;
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301895#endif
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05301896 vol->fsc = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001897 break;
1898 case Opt_mfsymlinks:
Stefan Metzmacher736a33202010-07-30 14:56:00 +02001899 vol->mfsymlinks = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001900 break;
1901 case Opt_multiuser:
Jeff Layton0eb8a132010-10-06 19:51:12 -04001902 vol->multiuser = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001903 break;
Jeff Laytond8162552012-03-23 14:40:56 -04001904 case Opt_sloppy:
1905 sloppy = true;
1906 break;
Jeff Laytona0b3df52013-05-24 07:40:59 -04001907 case Opt_nosharesock:
1908 vol->nosharesock = true;
1909 break;
Steve Frenchb2a30772015-09-29 21:49:28 -05001910 case Opt_nopersistent:
1911 vol->nopersistent = true;
1912 if (vol->persistent) {
1913 cifs_dbg(VFS,
1914 "persistenthandles mount options conflict\n");
1915 goto cifs_parse_mount_err;
1916 }
1917 break;
1918 case Opt_persistent:
1919 vol->persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06001920 if ((vol->nopersistent) || (vol->resilient)) {
Steve Frenchb2a30772015-09-29 21:49:28 -05001921 cifs_dbg(VFS,
1922 "persistenthandles mount options conflict\n");
1923 goto cifs_parse_mount_err;
1924 }
1925 break;
Steve French592fafe2015-11-03 10:08:53 -06001926 case Opt_resilient:
1927 vol->resilient = true;
1928 if (vol->persistent) {
1929 cifs_dbg(VFS,
1930 "persistenthandles mount options conflict\n");
1931 goto cifs_parse_mount_err;
1932 }
1933 break;
1934 case Opt_noresilient:
1935 vol->resilient = false; /* already the default */
1936 break;
Germano Percossi39566442016-12-15 12:31:18 +05301937 case Opt_domainauto:
1938 vol->domainauto = true;
1939 break;
Long Li8339dd32017-11-07 01:54:55 -07001940 case Opt_rdma:
1941 vol->rdma = true;
1942 break;
Steve French9fe5ff12019-06-24 20:39:04 -05001943 case Opt_compress:
1944 vol->compression = UNKNOWN_TYPE;
1945 cifs_dbg(VFS,
1946 "SMB3 compression support is experimental\n");
1947 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001948
1949 /* Numeric Values */
1950 case Opt_backupuid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001951 if (get_option_uid(args, &vol->backupuid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001952 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1953 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001954 goto cifs_parse_mount_err;
1955 }
1956 vol->backupuid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001957 break;
1958 case Opt_backupgid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001959 if (get_option_gid(args, &vol->backupgid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001960 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1961 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001962 goto cifs_parse_mount_err;
1963 }
1964 vol->backupgid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001965 break;
1966 case Opt_uid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001967 if (get_option_uid(args, &vol->linux_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001968 cifs_dbg(VFS, "%s: Invalid uid value\n",
1969 __func__);
Sean Finneyb9468452011-04-11 13:19:32 +00001970 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001972 uid_specified = true;
1973 break;
1974 case Opt_cruid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001975 if (get_option_uid(args, &vol->cred_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001976 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1977 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001978 goto cifs_parse_mount_err;
1979 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001980 break;
1981 case Opt_gid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001982 if (get_option_gid(args, &vol->linux_gid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001983 cifs_dbg(VFS, "%s: Invalid gid value\n",
1984 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001985 goto cifs_parse_mount_err;
1986 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001987 gid_specified = true;
1988 break;
1989 case Opt_file_mode:
1990 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001991 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1992 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001993 goto cifs_parse_mount_err;
1994 }
1995 vol->file_mode = option;
1996 break;
1997 case Opt_dirmode:
1998 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001999 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
2000 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002001 goto cifs_parse_mount_err;
2002 }
2003 vol->dir_mode = option;
2004 break;
2005 case Opt_port:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002006 if (get_option_ul(args, &option) ||
2007 option > USHRT_MAX) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002008 cifs_dbg(VFS, "%s: Invalid port value\n",
2009 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002010 goto cifs_parse_mount_err;
2011 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002012 port = (unsigned short)option;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002013 break;
Steve Frenche8506d22019-02-28 21:32:15 -06002014 case Opt_blocksize:
2015 if (get_option_ul(args, &option)) {
2016 cifs_dbg(VFS, "%s: Invalid blocksize value\n",
2017 __func__);
2018 goto cifs_parse_mount_err;
2019 }
2020 /*
2021 * inode blocksize realistically should never need to be
2022 * less than 16K or greater than 16M and default is 1MB.
2023 * Note that small inode block sizes (e.g. 64K) can lead
2024 * to very poor performance of common tools like cp and scp
2025 */
2026 if ((option < CIFS_MAX_MSGSIZE) ||
2027 (option > (4 * SMB3_DEFAULT_IOSIZE))) {
2028 cifs_dbg(VFS, "%s: Invalid blocksize\n",
2029 __func__);
2030 goto cifs_parse_mount_err;
2031 }
2032 vol->bsize = option;
2033 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002034 case Opt_rsize:
2035 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002036 cifs_dbg(VFS, "%s: Invalid rsize value\n",
2037 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002038 goto cifs_parse_mount_err;
2039 }
2040 vol->rsize = option;
2041 break;
2042 case Opt_wsize:
2043 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002044 cifs_dbg(VFS, "%s: Invalid wsize value\n",
2045 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002046 goto cifs_parse_mount_err;
2047 }
2048 vol->wsize = option;
2049 break;
2050 case Opt_actimeo:
2051 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002052 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
2053 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002054 goto cifs_parse_mount_err;
2055 }
2056 vol->actimeo = HZ * option;
2057 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002058 cifs_dbg(VFS, "attribute cache timeout too large\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002059 goto cifs_parse_mount_err;
2060 }
2061 break;
Steve Frenchca567eb2019-03-29 16:31:07 -05002062 case Opt_handletimeout:
2063 if (get_option_ul(args, &option)) {
2064 cifs_dbg(VFS, "%s: Invalid handletimeout value\n",
2065 __func__);
2066 goto cifs_parse_mount_err;
2067 }
2068 vol->handle_timeout = option;
2069 if (vol->handle_timeout > SMB3_MAX_HANDLE_TIMEOUT) {
2070 cifs_dbg(VFS, "Invalid handle cache timeout, longer than 16 minutes\n");
2071 goto cifs_parse_mount_err;
2072 }
2073 break;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002074 case Opt_echo_interval:
2075 if (get_option_ul(args, &option)) {
2076 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
2077 __func__);
2078 goto cifs_parse_mount_err;
2079 }
2080 vol->echo_interval = option;
2081 break;
Steve French8b217fe2016-11-11 22:36:20 -06002082 case Opt_snapshot:
2083 if (get_option_ul(args, &option)) {
2084 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
2085 __func__);
2086 goto cifs_parse_mount_err;
2087 }
2088 vol->snapshot_time = option;
2089 break;
Steve French141891f2016-09-23 00:44:16 -05002090 case Opt_max_credits:
2091 if (get_option_ul(args, &option) || (option < 20) ||
2092 (option > 60000)) {
2093 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
2094 __func__);
2095 goto cifs_parse_mount_err;
2096 }
2097 vol->max_credits = option;
2098 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002099
2100 /* String Arguments */
2101
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002102 case Opt_blank_user:
2103 /* null user, ie. anonymous authentication */
2104 vol->nullauth = 1;
2105 vol->username = NULL;
2106 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002107 case Opt_user:
2108 string = match_strdup(args);
2109 if (string == NULL)
2110 goto out_nomem;
2111
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002112 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
2113 CIFS_MAX_USERNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002114 pr_warn("CIFS: username too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002115 goto cifs_parse_mount_err;
2116 }
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04002117
2118 kfree(vol->username);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002119 vol->username = kstrdup(string, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -05002120 if (!vol->username)
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002121 goto cifs_parse_mount_err;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002122 break;
2123 case Opt_blank_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002124 /* passwords have to be handled differently
2125 * to allow the character used for deliminator
2126 * to be passed within them
2127 */
2128
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01002129 /*
2130 * Check if this is a case where the password
2131 * starts with a delimiter
2132 */
2133 tmp_end = strchr(data, '=');
2134 tmp_end++;
2135 if (!(tmp_end < end && tmp_end[1] == delim)) {
2136 /* No it is not. Set the password to NULL */
Aurelien Aptel97f4b722018-01-25 15:59:39 +01002137 kzfree(vol->password);
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01002138 vol->password = NULL;
2139 break;
2140 }
Gustavo A. R. Silva07fa6012018-11-27 10:01:51 +11002141 /* Fallthrough - to Opt_pass below.*/
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01002142 case Opt_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002143 /* Obtain the value string */
2144 value = strchr(data, '=');
Sachin Prabhu10238072012-03-28 18:07:08 +01002145 value++;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002146
2147 /* Set tmp_end to end of the string */
2148 tmp_end = (char *) value + strlen(value);
2149
2150 /* Check if following character is the deliminator
2151 * If yes, we have encountered a double deliminator
2152 * reset the NULL character to the deliminator
2153 */
Suresh Jayaramane73f8432012-06-12 07:15:50 +05302154 if (tmp_end < end && tmp_end[1] == delim) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002155 tmp_end[0] = delim;
2156
Suresh Jayaramane73f8432012-06-12 07:15:50 +05302157 /* Keep iterating until we get to a single
2158 * deliminator OR the end
2159 */
2160 while ((tmp_end = strchr(tmp_end, delim))
2161 != NULL && (tmp_end[1] == delim)) {
2162 tmp_end = (char *) &tmp_end[2];
2163 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002164
Suresh Jayaramane73f8432012-06-12 07:15:50 +05302165 /* Reset var options to point to next element */
2166 if (tmp_end) {
2167 tmp_end[0] = '\0';
2168 options = (char *) &tmp_end[1];
2169 } else
2170 /* Reached the end of the mount option
2171 * string */
2172 options = end;
2173 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002174
Aurelien Aptel97f4b722018-01-25 15:59:39 +01002175 kzfree(vol->password);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002176 /* Now build new password string */
2177 temp_len = strlen(value);
2178 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
2179 if (vol->password == NULL) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002180 pr_warn("CIFS: no memory for password\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002181 goto cifs_parse_mount_err;
2182 }
2183
2184 for (i = 0, j = 0; i < temp_len; i++, j++) {
2185 vol->password[j] = value[i];
2186 if ((value[i] == delim) &&
2187 value[i+1] == delim)
2188 /* skip the second deliminator */
2189 i++;
2190 }
2191 vol->password[j] = '\0';
2192 break;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002193 case Opt_blank_ip:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002194 /* FIXME: should this be an error instead? */
2195 got_ip = false;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002196 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002197 case Opt_ip:
2198 string = match_strdup(args);
2199 if (string == NULL)
2200 goto out_nomem;
2201
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002202 if (!cifs_convert_address(dstaddr, string,
2203 strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002204 pr_err("CIFS: bad ip= option (%s).\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002205 goto cifs_parse_mount_err;
2206 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002207 got_ip = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002208 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002209 case Opt_domain:
2210 string = match_strdup(args);
2211 if (string == NULL)
2212 goto out_nomem;
2213
Chen Gang057d6332013-07-19 09:01:36 +08002214 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
2215 == CIFS_MAX_DOMAINNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002216 pr_warn("CIFS: domain name too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002217 goto cifs_parse_mount_err;
2218 }
2219
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04002220 kfree(vol->domainname);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002221 vol->domainname = kstrdup(string, GFP_KERNEL);
2222 if (!vol->domainname) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002223 pr_warn("CIFS: no memory for domainname\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002224 goto cifs_parse_mount_err;
2225 }
Joe Perchesf96637b2013-05-04 22:12:25 -05002226 cifs_dbg(FYI, "Domain name set\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002227 break;
2228 case Opt_srcaddr:
2229 string = match_strdup(args);
2230 if (string == NULL)
2231 goto out_nomem;
2232
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002233 if (!cifs_convert_address(
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002234 (struct sockaddr *)&vol->srcaddr,
2235 string, strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002236 pr_warn("CIFS: Could not parse srcaddr: %s\n",
2237 string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002238 goto cifs_parse_mount_err;
2239 }
2240 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002241 case Opt_iocharset:
2242 string = match_strdup(args);
2243 if (string == NULL)
2244 goto out_nomem;
2245
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002246 if (strnlen(string, 1024) >= 65) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002247 pr_warn("CIFS: iocharset name too long.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002248 goto cifs_parse_mount_err;
2249 }
2250
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002251 if (strncasecmp(string, "default", 7) != 0) {
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04002252 kfree(vol->iocharset);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002253 vol->iocharset = kstrdup(string,
2254 GFP_KERNEL);
2255 if (!vol->iocharset) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002256 pr_warn("CIFS: no memory for charset\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002257 goto cifs_parse_mount_err;
2258 }
2259 }
2260 /* if iocharset not set then load_nls_default
2261 * is used by caller
2262 */
Joe Perchesf96637b2013-05-04 22:12:25 -05002263 cifs_dbg(FYI, "iocharset set to %s\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002264 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002265 case Opt_netbiosname:
2266 string = match_strdup(args);
2267 if (string == NULL)
2268 goto out_nomem;
2269
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002270 memset(vol->source_rfc1001_name, 0x20,
2271 RFC1001_NAME_LEN);
2272 /*
2273 * FIXME: are there cases in which a comma can
2274 * be valid in workstation netbios name (and
2275 * need special handling)?
2276 */
2277 for (i = 0; i < RFC1001_NAME_LEN; i++) {
2278 /* don't ucase netbiosname for user */
2279 if (string[i] == 0)
2280 break;
2281 vol->source_rfc1001_name[i] = string[i];
2282 }
2283 /* The string has 16th byte zero still from
2284 * set at top of the function
2285 */
2286 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002287 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002288 break;
2289 case Opt_servern:
2290 /* servernetbiosname specified override *SMBSERVER */
2291 string = match_strdup(args);
2292 if (string == NULL)
2293 goto out_nomem;
2294
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002295 /* last byte, type, is 0x20 for servr type */
2296 memset(vol->target_rfc1001_name, 0x20,
2297 RFC1001_NAME_LEN_WITH_NULL);
2298
2299 /* BB are there cases in which a comma can be
2300 valid in this workstation netbios name
2301 (and need special handling)? */
2302
2303 /* user or mount helper must uppercase the
2304 netbios name */
2305 for (i = 0; i < 15; i++) {
2306 if (string[i] == 0)
2307 break;
2308 vol->target_rfc1001_name[i] = string[i];
2309 }
2310 /* The string has 16th byte zero still from
2311 set at top of the function */
2312 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002313 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002314 break;
2315 case Opt_ver:
Steve French7e682f72017-08-31 21:34:24 -05002316 /* version of mount userspace tools, not dialect */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002317 string = match_strdup(args);
2318 if (string == NULL)
2319 goto out_nomem;
2320
Steve French7e682f72017-08-31 21:34:24 -05002321 /* If interface changes in mount.cifs bump to new ver */
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002322 if (strncasecmp(string, "1", 1) == 0) {
Steve French7e682f72017-08-31 21:34:24 -05002323 if (strlen(string) > 1) {
2324 pr_warn("Bad mount helper ver=%s. Did "
2325 "you want SMB1 (CIFS) dialect "
2326 "and mean to type vers=1.0 "
2327 "instead?\n", string);
2328 goto cifs_parse_mount_err;
2329 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002330 /* This is the default */
2331 break;
2332 }
2333 /* For all other value, error */
Steve French7e682f72017-08-31 21:34:24 -05002334 pr_warn("CIFS: Invalid mount helper version specified\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002335 goto cifs_parse_mount_err;
Jeff Layton23db65f2012-05-15 12:20:51 -04002336 case Opt_vers:
Steve French7e682f72017-08-31 21:34:24 -05002337 /* protocol version (dialect) */
Jeff Layton23db65f2012-05-15 12:20:51 -04002338 string = match_strdup(args);
2339 if (string == NULL)
2340 goto out_nomem;
2341
Steve Frenchc7c137b2018-06-06 17:59:29 -05002342 if (cifs_parse_smb_version(string, vol, is_smb3) != 0)
Jeff Layton23db65f2012-05-15 12:20:51 -04002343 goto cifs_parse_mount_err;
Steve French7e682f72017-08-31 21:34:24 -05002344 got_version = true;
Jeff Layton23db65f2012-05-15 12:20:51 -04002345 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002346 case Opt_sec:
2347 string = match_strdup(args);
2348 if (string == NULL)
2349 goto out_nomem;
2350
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002351 if (cifs_parse_security_flavors(string, vol) != 0)
2352 goto cifs_parse_mount_err;
2353 break;
Jeff Layton15b6a472012-05-16 07:50:15 -04002354 case Opt_cache:
2355 string = match_strdup(args);
2356 if (string == NULL)
2357 goto out_nomem;
2358
2359 if (cifs_parse_cache_flavor(string, vol) != 0)
2360 goto cifs_parse_mount_err;
2361 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002362 default:
Jeff Laytond8162552012-03-23 14:40:56 -04002363 /*
2364 * An option we don't recognize. Save it off for later
2365 * if we haven't already found one
2366 */
2367 if (!invalid)
2368 invalid = data;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002369 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002371 /* Free up any allocated string */
2372 kfree(string);
2373 string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002375
Jeff Laytond8162552012-03-23 14:40:56 -04002376 if (!sloppy && invalid) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002377 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
Jeff Laytond8162552012-03-23 14:40:56 -04002378 goto cifs_parse_mount_err;
2379 }
2380
Long Li8339dd32017-11-07 01:54:55 -07002381 if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
2382 cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
2383 goto cifs_parse_mount_err;
2384 }
2385
Jeff Layton8a8798a2012-01-17 16:09:15 -05002386#ifndef CONFIG_KEYS
2387 /* Muliuser mounts require CONFIG_KEYS support */
2388 if (vol->multiuser) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002389 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002390 goto cifs_parse_mount_err;
Jeff Layton0eb8a132010-10-06 19:51:12 -04002391 }
Jeff Layton8a8798a2012-01-17 16:09:15 -05002392#endif
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002393 if (!vol->UNC) {
Jeff Layton37d4f992013-05-24 07:40:05 -04002394 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002395 goto cifs_parse_mount_err;
2396 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002397
Jeff Layton62a1a432012-12-10 06:10:45 -05002398 /* make sure UNC has a share name */
2399 if (!strchr(vol->UNC + 3, '\\')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002400 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
Jeff Layton62a1a432012-12-10 06:10:45 -05002401 goto cifs_parse_mount_err;
2402 }
2403
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002404 if (!got_ip) {
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002405 int len;
2406 const char *slash;
2407
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002408 /* No ip= option specified? Try to get it from UNC */
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002409 /* Use the address part of the UNC. */
2410 slash = strchr(&vol->UNC[2], '\\');
2411 len = slash - &vol->UNC[2];
2412 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002413 pr_err("Unable to determine destination address.\n");
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002414 goto cifs_parse_mount_err;
2415 }
2416 }
2417
2418 /* set the port that we got earlier */
2419 cifs_set_port(dstaddr, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002421 if (uid_specified)
2422 vol->override_uid = override_uid;
2423 else if (override_uid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002424 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002425
2426 if (gid_specified)
2427 vol->override_gid = override_gid;
2428 else if (override_gid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002429 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002430
Steve French7e682f72017-08-31 21:34:24 -05002431 if (got_version == false)
2432 pr_warn("No dialect specified on mount. Default has changed to "
Steve French9764c022017-09-17 10:41:35 -05002433 "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
Steve French7e682f72017-08-31 21:34:24 -05002434 "(SMB1). To use the less secure SMB1 dialect to access "
Steve French9764c022017-09-17 10:41:35 -05002435 "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
2436 " on mount.\n");
Steve French7e682f72017-08-31 21:34:24 -05002437
Sean Finneyb9468452011-04-11 13:19:32 +00002438 kfree(mountdata_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 return 0;
Sean Finneyb9468452011-04-11 13:19:32 +00002440
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002441out_nomem:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002442 pr_warn("Could not allocate temporary buffer\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002443cifs_parse_mount_err:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002444 kfree(string);
Sean Finneyb9468452011-04-11 13:19:32 +00002445 kfree(mountdata_copy);
2446 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
2448
Ben Greear3eb9a882010-09-01 17:06:02 -07002449/** Returns true if srcaddr isn't specified and rhs isn't
2450 * specified, or if srcaddr is specified and
2451 * matches the IP address of the rhs argument.
2452 */
Jeff Layton45151482010-07-06 20:43:02 -04002453static bool
Ben Greear3eb9a882010-09-01 17:06:02 -07002454srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2455{
2456 switch (srcaddr->sa_family) {
2457 case AF_UNSPEC:
2458 return (rhs->sa_family == AF_UNSPEC);
2459 case AF_INET: {
2460 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2461 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2462 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2463 }
2464 case AF_INET6: {
2465 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05002466 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07002467 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2468 }
2469 default:
2470 WARN_ON(1);
2471 return false; /* don't expect to be here */
2472 }
2473}
2474
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002475/*
2476 * If no port is specified in addr structure, we try to match with 445 port
2477 * and if it fails - with 139 ports. It should be called only if address
2478 * families of server and addr are equal.
2479 */
2480static bool
2481match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2482{
Steve French6da97912011-03-13 18:55:55 +00002483 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002484
Long Li3b249112019-05-15 14:09:04 -07002485 /* SMBDirect manages its own ports, don't match it here */
2486 if (server->rdma)
2487 return true;
2488
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002489 switch (addr->sa_family) {
2490 case AF_INET:
2491 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2492 port = ((struct sockaddr_in *) addr)->sin_port;
2493 break;
2494 case AF_INET6:
2495 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2496 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2497 break;
2498 default:
2499 WARN_ON(1);
2500 return false;
2501 }
2502
2503 if (!port) {
2504 port = htons(CIFS_PORT);
2505 if (port == *sport)
2506 return true;
2507
2508 port = htons(RFC1001_PORT);
2509 }
2510
2511 return port == *sport;
2512}
Ben Greear3eb9a882010-09-01 17:06:02 -07002513
2514static bool
2515match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2516 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
Jeff Layton45151482010-07-06 20:43:02 -04002518 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002519 case AF_INET: {
2520 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2521 struct sockaddr_in *srv_addr4 =
2522 (struct sockaddr_in *)&server->dstaddr;
2523
2524 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04002525 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002526 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002527 }
2528 case AF_INET6: {
2529 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2530 struct sockaddr_in6 *srv_addr6 =
2531 (struct sockaddr_in6 *)&server->dstaddr;
2532
Jeff Layton45151482010-07-06 20:43:02 -04002533 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002534 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04002535 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002536 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04002537 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002538 break;
2539 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002540 default:
2541 WARN_ON(1);
2542 return false; /* don't expect to be here */
2543 }
Jeff Layton45151482010-07-06 20:43:02 -04002544
Ben Greear3eb9a882010-09-01 17:06:02 -07002545 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2546 return false;
2547
Jeff Layton45151482010-07-06 20:43:02 -04002548 return true;
2549}
2550
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002551static bool
2552match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2553{
Jeff Layton3f618222013-06-12 19:52:14 -05002554 /*
2555 * The select_sectype function should either return the vol->sectype
2556 * that was specified, or "Unspecified" if that sectype was not
2557 * compatible with the given NEGOTIATE request.
2558 */
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302559 if (server->ops->select_sectype(server, vol->sectype)
2560 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002561 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002562
Jeff Layton3f618222013-06-12 19:52:14 -05002563 /*
2564 * Now check if signing mode is acceptable. No need to check
2565 * global_secflags at this point since if MUST_SIGN is set then
2566 * the server->sign had better be too.
2567 */
Jeff Layton38d77c52013-05-26 07:01:00 -04002568 if (vol->sign && !server->sign)
2569 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002570
2571 return true;
2572}
2573
Jeff Layton9fa114f2012-11-26 11:09:57 -05002574static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002575{
Jeff Layton9fa114f2012-11-26 11:09:57 -05002576 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2577
Jeff Laytona0b3df52013-05-24 07:40:59 -04002578 if (vol->nosharesock)
2579 return 0;
2580
Steve French43cdae82019-06-13 14:26:49 -05002581 /* If multidialect negotiation see if existing sessions match one */
2582 if (strcmp(vol->vals->version_string, SMB3ANY_VERSION_STRING) == 0) {
2583 if (server->vals->protocol_id < SMB30_PROT_ID)
2584 return 0;
2585 } else if (strcmp(vol->vals->version_string,
2586 SMBDEFAULT_VERSION_STRING) == 0) {
2587 if (server->vals->protocol_id < SMB21_PROT_ID)
2588 return 0;
2589 } else if ((server->vals != vol->vals) || (server->ops != vol->ops))
Jeff Layton23db65f2012-05-15 12:20:51 -04002590 return 0;
2591
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002592 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2593 return 0;
2594
2595 if (!match_address(server, addr,
2596 (struct sockaddr *)&vol->srcaddr))
2597 return 0;
2598
2599 if (!match_port(server, addr))
2600 return 0;
2601
2602 if (!match_security(server, vol))
2603 return 0;
2604
Rabin Vincentb782fcc2016-07-19 09:25:45 +02002605 if (server->echo_interval != vol->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002606 return 0;
2607
Long Li8339dd32017-11-07 01:54:55 -07002608 if (server->rdma != vol->rdma)
2609 return 0;
2610
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002611 return 1;
2612}
2613
Paulo Alcantara54be1f62018-11-14 16:01:21 -02002614struct TCP_Server_Info *
Jeff Layton9fa114f2012-11-26 11:09:57 -05002615cifs_find_tcp_session(struct smb_vol *vol)
Jeff Layton45151482010-07-06 20:43:02 -04002616{
Jeff Laytone7ddee92008-11-14 13:44:38 -05002617 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302619 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04002620 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Jeff Layton9fa114f2012-11-26 11:09:57 -05002621 if (!match_server(server, vol))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002622 continue;
2623
Jeff Laytone7ddee92008-11-14 13:44:38 -05002624 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302625 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05002626 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05002627 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302629 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 return NULL;
2631}
2632
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002633void
2634cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002636 struct task_struct *task;
2637
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302638 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002639 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302640 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002641 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 }
Steve Frenchdea570e02008-05-06 22:05:51 +00002643
Rob Landleyf1d0c992011-01-22 15:44:05 -06002644 put_net(cifs_net_ns(server));
2645
Jeff Laytone7ddee92008-11-14 13:44:38 -05002646 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302647 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002648
Jeff Laytonc74093b2011-01-11 07:24:23 -05002649 cancel_delayed_work_sync(&server->echo);
2650
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002651 if (from_reconnect)
2652 /*
2653 * Avoid deadlock here: reconnect work calls
2654 * cifs_put_tcp_session() at its end. Need to be sure
2655 * that reconnect work does nothing with server pointer after
2656 * that step.
2657 */
2658 cancel_delayed_work(&server->reconnect);
2659 else
2660 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002661
Jeff Laytone7ddee92008-11-14 13:44:38 -05002662 spin_lock(&GlobalMid_Lock);
2663 server->tcpStatus = CifsExiting;
2664 spin_unlock(&GlobalMid_Lock);
2665
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002666 cifs_crypto_secmech_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302667 cifs_fscache_release_client_cookie(server);
2668
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05002669 kfree(server->session_key.response);
2670 server->session_key.response = NULL;
2671 server->session_key.len = 0;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002672
2673 task = xchg(&server->tsk, NULL);
2674 if (task)
Eric W. Biederman72abe3b2019-05-15 12:33:50 -05002675 send_sig(SIGKILL, task, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676}
2677
Jeff Layton63c038c2008-12-01 18:41:46 -05002678static struct TCP_Server_Info *
2679cifs_get_tcp_session(struct smb_vol *volume_info)
2680{
2681 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05002682 int rc;
2683
Joe Perchesf96637b2013-05-04 22:12:25 -05002684 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05002685
2686 /* see if we already have a matching tcp_ses */
Jeff Layton9fa114f2012-11-26 11:09:57 -05002687 tcp_ses = cifs_find_tcp_session(volume_info);
Jeff Layton63c038c2008-12-01 18:41:46 -05002688 if (tcp_ses)
2689 return tcp_ses;
2690
2691 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2692 if (!tcp_ses) {
2693 rc = -ENOMEM;
2694 goto out_err;
2695 }
2696
Jeff Layton23db65f2012-05-15 12:20:51 -04002697 tcp_ses->ops = volume_info->ops;
2698 tcp_ses->vals = volume_info->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06002699 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05002700 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2701 if (IS_ERR(tcp_ses->hostname)) {
2702 rc = PTR_ERR(tcp_ses->hostname);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002703 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002704 }
2705
2706 tcp_ses->noblocksnd = volume_info->noblocksnd;
2707 tcp_ses->noautotune = volume_info->noautotune;
Steve French6a5fa2362010-01-01 01:28:43 +00002708 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
Long Li8339dd32017-11-07 01:54:55 -07002709 tcp_ses->rdma = volume_info->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03002710 tcp_ses->in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002711 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05002712 init_waitqueue_head(&tcp_ses->response_q);
2713 init_waitqueue_head(&tcp_ses->request_q);
2714 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2715 mutex_init(&tcp_ses->srv_mutex);
2716 memcpy(tcp_ses->workstation_RFC1001_name,
2717 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2718 memcpy(tcp_ses->server_RFC1001_name,
2719 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05002720 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05002721 tcp_ses->sequence_number = 0;
Pavel Shilovsky5b964852019-01-18 11:30:26 -08002722 tcp_ses->reconnect_instance = 1;
Steve Frenchfda35942011-01-20 18:06:34 +00002723 tcp_ses->lstrp = jiffies;
Steve French9fe5ff12019-06-24 20:39:04 -05002724 tcp_ses->compress_algorithm = cpu_to_le16(volume_info->compression);
Jeff Layton58fa0152012-05-01 17:41:16 -04002725 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002726 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2727 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002728 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002729 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2730 mutex_init(&tcp_ses->reconnect_mutex);
Jeff Layton9fa114f2012-11-26 11:09:57 -05002731 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2732 sizeof(tcp_ses->srcaddr));
2733 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2734 sizeof(tcp_ses->dstaddr));
Steve Frenchfa70b872016-09-22 00:39:34 -05002735 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05002736 /*
2737 * at this point we are the only ones with the pointer
2738 * to the struct since the kernel thread not created yet
2739 * no need to spinlock this init of tcpStatus or srv_count
2740 */
2741 tcp_ses->tcpStatus = CifsNew;
2742 ++tcp_ses->srv_count;
2743
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002744 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2745 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2746 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2747 else
2748 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07002749 if (tcp_ses->rdma) {
2750#ifndef CONFIG_CIFS_SMB_DIRECT
2751 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
2752 rc = -ENOENT;
2753 goto out_err_crypto_release;
2754#endif
2755 tcp_ses->smbd_conn = smbd_get_connection(
2756 tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
2757 if (tcp_ses->smbd_conn) {
2758 cifs_dbg(VFS, "RDMA transport established\n");
2759 rc = 0;
2760 goto smbd_connected;
2761 } else {
2762 rc = -ENOENT;
2763 goto out_err_crypto_release;
2764 }
2765 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002766 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05002767 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002768 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002769 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002770 }
Long Li2f894642017-11-22 17:38:34 -07002771smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05002772 /*
2773 * since we're in a cifs function already, we know that
2774 * this will succeed. No need for try_module_get().
2775 */
2776 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04002777 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05002778 tcp_ses, "cifsd");
2779 if (IS_ERR(tcp_ses->tsk)) {
2780 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05002781 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05002782 module_put(THIS_MODULE);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002783 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002784 }
Steve Frenchfd88ce92011-04-12 01:01:14 +00002785 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05002786
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02002787 tcp_ses->nr_targets = 1;
2788
Jeff Layton63c038c2008-12-01 18:41:46 -05002789 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302790 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002791 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302792 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002793
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302794 cifs_fscache_get_client_cookie(tcp_ses);
2795
Jeff Laytonc74093b2011-01-11 07:24:23 -05002796 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002797 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002798
Jeff Layton63c038c2008-12-01 18:41:46 -05002799 return tcp_ses;
2800
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002801out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002802 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002803
Rob Landleyf1d0c992011-01-22 15:44:05 -06002804 put_net(cifs_net_ns(tcp_ses));
2805
Jeff Layton63c038c2008-12-01 18:41:46 -05002806out_err:
2807 if (tcp_ses) {
Steve French8347a5c2009-10-06 18:31:29 +00002808 if (!IS_ERR(tcp_ses->hostname))
2809 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05002810 if (tcp_ses->ssocket)
2811 sock_release(tcp_ses->ssocket);
2812 kfree(tcp_ses);
2813 }
2814 return ERR_PTR(rc);
2815}
2816
Steve French96daf2b2011-05-27 04:34:02 +00002817static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002818{
Jeff Layton3f618222013-06-12 19:52:14 -05002819 if (vol->sectype != Unspecified &&
2820 vol->sectype != ses->sectype)
2821 return 0;
2822
2823 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002824 case Kerberos:
Eric W. Biederman64ed39d2013-02-06 02:30:39 -08002825 if (!uid_eq(vol->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002826 return 0;
2827 break;
2828 default:
Jeff Layton04febab2012-01-17 16:09:15 -05002829 /* NULL username means anonymous session */
2830 if (ses->user_name == NULL) {
2831 if (!vol->nullauth)
2832 return 0;
2833 break;
2834 }
2835
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002836 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05002837 if (strncmp(ses->user_name,
2838 vol->username ? vol->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002839 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002840 return 0;
Jeff Layton08b37d52014-05-23 06:53:10 -04002841 if ((vol->username && strlen(vol->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002842 ses->password != NULL &&
2843 strncmp(ses->password,
2844 vol->password ? vol->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002845 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002846 return 0;
2847 }
2848 return 1;
2849}
2850
Aurelien Aptelb327a712018-01-24 13:46:10 +01002851/**
2852 * cifs_setup_ipc - helper to setup the IPC tcon for the session
2853 *
2854 * A new IPC connection is made and stored in the session
2855 * tcon_ipc. The IPC tcon has the same lifetime as the session.
2856 */
2857static int
2858cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
2859{
2860 int rc = 0, xid;
2861 struct cifs_tcon *tcon;
2862 struct nls_table *nls_codepage;
2863 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
2864 bool seal = false;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002865 struct TCP_Server_Info *server = ses->server;
Aurelien Aptelb327a712018-01-24 13:46:10 +01002866
2867 /*
2868 * If the mount request that resulted in the creation of the
2869 * session requires encryption, force IPC to be encrypted too.
2870 */
2871 if (volume_info->seal) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002872 if (server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
Aurelien Aptelb327a712018-01-24 13:46:10 +01002873 seal = true;
2874 else {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002875 cifs_server_dbg(VFS,
Aurelien Aptelb327a712018-01-24 13:46:10 +01002876 "IPC: server doesn't support encryption\n");
2877 return -EOPNOTSUPP;
2878 }
2879 }
2880
2881 tcon = tconInfoAlloc();
2882 if (tcon == NULL)
2883 return -ENOMEM;
2884
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002885 scnprintf(unc, sizeof(unc), "\\\\%s\\IPC$", server->hostname);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002886
2887 /* cannot fail */
2888 nls_codepage = load_nls_default();
2889
2890 xid = get_xid();
2891 tcon->ses = ses;
2892 tcon->ipc = true;
2893 tcon->seal = seal;
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002894 rc = server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002895 free_xid(xid);
2896
2897 if (rc) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002898 cifs_server_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002899 tconInfoFree(tcon);
2900 goto out;
2901 }
2902
2903 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
2904
2905 ses->tcon_ipc = tcon;
2906out:
2907 unload_nls(nls_codepage);
2908 return rc;
2909}
2910
2911/**
2912 * cifs_free_ipc - helper to release the session IPC tcon
2913 *
2914 * Needs to be called everytime a session is destroyed
2915 */
2916static int
2917cifs_free_ipc(struct cifs_ses *ses)
2918{
2919 int rc = 0, xid;
2920 struct cifs_tcon *tcon = ses->tcon_ipc;
2921
2922 if (tcon == NULL)
2923 return 0;
2924
2925 if (ses->server->ops->tree_disconnect) {
2926 xid = get_xid();
2927 rc = ses->server->ops->tree_disconnect(xid, tcon);
2928 free_xid(xid);
2929 }
2930
2931 if (rc)
2932 cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
2933
2934 tconInfoFree(tcon);
2935 ses->tcon_ipc = NULL;
2936 return rc;
2937}
2938
Steve French96daf2b2011-05-27 04:34:02 +00002939static struct cifs_ses *
Jeff Layton4ff67b72010-07-06 20:43:02 -04002940cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941{
Steve French96daf2b2011-05-27 04:34:02 +00002942 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302944 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04002945 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002946 if (ses->status == CifsExiting)
2947 continue;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002948 if (!match_session(ses, vol))
2949 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05002950 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302951 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002952 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302954 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 return NULL;
2956}
2957
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03002958void cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05002959{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002960 unsigned int rc, xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05002961 struct TCP_Server_Info *server = ses->server;
2962
Joe Perchesf96637b2013-05-04 22:12:25 -05002963 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002964
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302965 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002966 if (ses->status == CifsExiting) {
2967 spin_unlock(&cifs_tcp_ses_lock);
2968 return;
2969 }
Jeff Layton14fbf502008-11-14 13:53:46 -05002970 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302971 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002972 return;
2973 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002974 if (ses->status == CifsGood)
2975 ses->status = CifsExiting;
2976 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002977
Aurelien Aptelb327a712018-01-24 13:46:10 +01002978 cifs_free_ipc(ses);
2979
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002980 if (ses->status == CifsExiting && server->ops->logoff) {
2981 xid = get_xid();
2982 rc = server->ops->logoff(xid, ses);
2983 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10002984 cifs_server_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002985 __func__, rc);
2986 _free_xid(xid);
2987 }
2988
2989 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002990 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302991 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002992
Jeff Layton14fbf502008-11-14 13:53:46 -05002993 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002994 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05002995}
2996
Jeff Layton8a8798a2012-01-17 16:09:15 -05002997#ifdef CONFIG_KEYS
2998
Chen Gang057d6332013-07-19 09:01:36 +08002999/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
3000#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05003001
3002/* Populate username and pw fields from keyring if possible */
3003static int
3004cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
3005{
3006 int rc = 0;
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10003007 int is_domain = 0;
David Howells146aa8b2015-10-21 14:04:48 +01003008 const char *delim, *payload;
3009 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003010 ssize_t len;
3011 struct key *key;
3012 struct TCP_Server_Info *server = ses->server;
3013 struct sockaddr_in *sa;
3014 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01003015 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003016
3017 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
3018 if (!desc)
3019 return -ENOMEM;
3020
3021 /* try to find an address key first */
3022 switch (server->dstaddr.ss_family) {
3023 case AF_INET:
3024 sa = (struct sockaddr_in *)&server->dstaddr;
3025 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
3026 break;
3027 case AF_INET6:
3028 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
3029 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
3030 break;
3031 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05003032 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
3033 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003034 rc = -EINVAL;
3035 goto out_err;
3036 }
3037
Joe Perchesf96637b2013-05-04 22:12:25 -05003038 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07003039 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05003040 if (IS_ERR(key)) {
3041 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003042 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05003043 rc = PTR_ERR(key);
3044 goto out_err;
3045 }
3046
3047 /* didn't work, try to find a domain key */
3048 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05003049 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Linus Torvalds028db3e2019-07-10 18:43:43 -07003050 key = request_key(&key_type_logon, desc, "");
Jeff Layton8a8798a2012-01-17 16:09:15 -05003051 if (IS_ERR(key)) {
3052 rc = PTR_ERR(key);
3053 goto out_err;
3054 }
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10003055 is_domain = 1;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003056 }
3057
3058 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00003059 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003060 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05003061 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003062 goto out_key_put;
3063 }
3064
3065 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01003066 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05003067 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05003068 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003069 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003070 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
3071 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003072 rc = -EINVAL;
3073 goto out_key_put;
3074 }
3075
3076 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04003077 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003078 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
3079 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003080 rc = -EINVAL;
3081 goto out_key_put;
3082 }
3083
3084 vol->username = kstrndup(payload, len, GFP_KERNEL);
3085 if (!vol->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003086 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
3087 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003088 rc = -ENOMEM;
3089 goto out_key_put;
3090 }
Joe Perchesf96637b2013-05-04 22:12:25 -05003091 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003092
3093 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04003094 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003095 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003096 rc = -EINVAL;
3097 kfree(vol->username);
3098 vol->username = NULL;
3099 goto out_key_put;
3100 }
3101
3102 ++delim;
3103 vol->password = kstrndup(delim, len, GFP_KERNEL);
3104 if (!vol->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003105 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
3106 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003107 rc = -ENOMEM;
3108 kfree(vol->username);
3109 vol->username = NULL;
3110 goto out_key_put;
3111 }
3112
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10003113 /*
3114 * If we have a domain key then we must set the domainName in the
3115 * for the request.
3116 */
3117 if (is_domain && ses->domainName) {
3118 vol->domainname = kstrndup(ses->domainName,
3119 strlen(ses->domainName),
3120 GFP_KERNEL);
3121 if (!vol->domainname) {
3122 cifs_dbg(FYI, "Unable to allocate %zd bytes for "
3123 "domain\n", len);
3124 rc = -ENOMEM;
3125 kfree(vol->username);
3126 vol->username = NULL;
Dan Carpenter478228e2019-08-27 13:59:17 +03003127 kzfree(vol->password);
Ronnie Sahlbergf2aee322019-08-22 08:09:50 +10003128 vol->password = NULL;
3129 goto out_key_put;
3130 }
3131 }
3132
Jeff Layton8a8798a2012-01-17 16:09:15 -05003133out_key_put:
3134 up_read(&key->sem);
3135 key_put(key);
3136out_err:
3137 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05003138 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05003139 return rc;
3140}
3141#else /* ! CONFIG_KEYS */
3142static inline int
3143cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
3144 struct cifs_ses *ses __attribute__((unused)))
3145{
3146 return -ENOSYS;
3147}
3148#endif /* CONFIG_KEYS */
3149
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003150/**
3151 * cifs_get_smb_ses - get a session matching @volume_info data from @server
3152 *
3153 * This function assumes it is being called from cifs_mount() where we
3154 * already got a server reference (server refcount +1). See
3155 * cifs_get_tcon() for refcount explanations.
3156 */
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03003157struct cifs_ses *
Jeff Layton36988c72010-04-24 07:57:43 -04003158cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
3159{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04003160 int rc = -ENOMEM;
3161 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00003162 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003163 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3164 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04003165
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003166 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04003167
Jeff Layton4ff67b72010-07-06 20:43:02 -04003168 ses = cifs_find_smb_ses(server, volume_info);
Jeff Layton36988c72010-04-24 07:57:43 -04003169 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003170 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
3171 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04003172
Jeff Layton36988c72010-04-24 07:57:43 -04003173 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04003174 rc = cifs_negotiate_protocol(xid, ses);
3175 if (rc) {
3176 mutex_unlock(&ses->session_mutex);
3177 /* problem -- put our ses reference */
3178 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003179 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04003180 return ERR_PTR(rc);
3181 }
Jeff Layton36988c72010-04-24 07:57:43 -04003182 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003183 cifs_dbg(FYI, "Session needs reconnect\n");
Jeff Layton36988c72010-04-24 07:57:43 -04003184 rc = cifs_setup_session(xid, ses,
3185 volume_info->local_nls);
3186 if (rc) {
3187 mutex_unlock(&ses->session_mutex);
3188 /* problem -- put our reference */
3189 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003190 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04003191 return ERR_PTR(rc);
3192 }
3193 }
3194 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04003195
3196 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003197 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003198 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04003199 return ses;
3200 }
3201
Joe Perchesf96637b2013-05-04 22:12:25 -05003202 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04003203 ses = sesInfoAlloc();
3204 if (ses == NULL)
3205 goto get_ses_fail;
3206
3207 /* new SMB session uses our server ref */
3208 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003209 if (server->dstaddr.ss_family == AF_INET6)
3210 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04003211 else
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003212 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04003213
Steve French8727c8a2011-02-25 01:11:56 -06003214 if (volume_info->username) {
3215 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
3216 if (!ses->user_name)
3217 goto get_ses_fail;
3218 }
Jeff Layton36988c72010-04-24 07:57:43 -04003219
3220 /* volume_info->password freed at unmount */
3221 if (volume_info->password) {
3222 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
3223 if (!ses->password)
3224 goto get_ses_fail;
3225 }
3226 if (volume_info->domainname) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05003227 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
3228 if (!ses->domainName)
3229 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04003230 }
Germano Percossi39566442016-12-15 12:31:18 +05303231 if (volume_info->domainauto)
3232 ses->domainAuto = volume_info->domainauto;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04003233 ses->cred_uid = volume_info->cred_uid;
Jeff Layton36988c72010-04-24 07:57:43 -04003234 ses->linux_uid = volume_info->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00003235
Jeff Layton28e11bd2013-05-26 07:01:00 -04003236 ses->sectype = volume_info->sectype;
3237 ses->sign = volume_info->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04003238
3239 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04003240 rc = cifs_negotiate_protocol(xid, ses);
3241 if (!rc)
3242 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04003243 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00003244 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04003245 goto get_ses_fail;
3246
3247 /* success, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303248 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04003249 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303250 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04003251
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003252 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01003253
3254 cifs_setup_ipc(ses, volume_info);
3255
Jeff Layton36988c72010-04-24 07:57:43 -04003256 return ses;
3257
3258get_ses_fail:
3259 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003260 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04003261 return ERR_PTR(rc);
3262}
3263
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003264static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003265{
3266 if (tcon->tidStatus == CifsExiting)
3267 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003268 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003269 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003270 if (tcon->seal != volume_info->seal)
3271 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003272 if (tcon->snapshot_time != volume_info->snapshot_time)
3273 return 0;
Steve Frenchca567eb2019-03-29 16:31:07 -05003274 if (tcon->handle_timeout != volume_info->handle_timeout)
3275 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003276 return 1;
3277}
3278
Steve French96daf2b2011-05-27 04:34:02 +00003279static struct cifs_tcon *
Steve French8b217fe2016-11-11 22:36:20 -06003280cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281{
3282 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00003283 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303285 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003286 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00003287 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003288 if (!match_tcon(tcon, volume_info))
Jeff Laytonf1987b42008-11-15 11:12:47 -05003289 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05003290 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303291 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292 return tcon;
3293 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303294 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 return NULL;
3296}
3297
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003298void
Steve French96daf2b2011-05-27 04:34:02 +00003299cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05003300{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003301 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01003302 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05003303
Aurelien Aptelb327a712018-01-24 13:46:10 +01003304 /*
3305 * IPC tcon share the lifetime of their session and are
3306 * destroyed in the session put function
3307 */
3308 if (tcon == NULL || tcon->ipc)
3309 return;
3310
3311 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05003312 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303313 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003314 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303315 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003316 return;
3317 }
3318
3319 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303320 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003321
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003322 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003323 if (ses->server->ops->tree_disconnect)
3324 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003325 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003326
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303327 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00003328 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003329 cifs_put_smb_ses(ses);
3330}
3331
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003332/**
3333 * cifs_get_tcon - get a tcon matching @volume_info data from @ses
3334 *
3335 * - tcon refcount is the number of mount points using the tcon.
3336 * - ses refcount is the number of tcon using the session.
3337 *
3338 * 1. This function assumes it is being called from cifs_mount() where
3339 * we already got a session reference (ses refcount +1).
3340 *
3341 * 2. Since we're in the context of adding a mount point, the end
3342 * result should be either:
3343 *
3344 * a) a new tcon already allocated with refcount=1 (1 mount point) and
3345 * its session refcount incremented (1 new tcon). This +1 was
3346 * already done in (1).
3347 *
3348 * b) an existing tcon with refcount+1 (add a mount point to it) and
3349 * identical ses refcount (no new tcon). Because of (1) we need to
3350 * decrement the ses refcount.
3351 */
Steve French96daf2b2011-05-27 04:34:02 +00003352static struct cifs_tcon *
3353cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Jeff Laytond00c28d2010-04-24 07:57:44 -04003354{
3355 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00003356 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003357
Steve French8b217fe2016-11-11 22:36:20 -06003358 tcon = cifs_find_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003359 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003360 /*
3361 * tcon has refcount already incremented but we need to
3362 * decrement extra ses reference gotten by caller (case b)
3363 */
Joe Perchesf96637b2013-05-04 22:12:25 -05003364 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04003365 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003366 return tcon;
3367 }
3368
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003369 if (!ses->server->ops->tree_connect) {
3370 rc = -ENOSYS;
3371 goto out_fail;
3372 }
3373
Jeff Laytond00c28d2010-04-24 07:57:44 -04003374 tcon = tconInfoAlloc();
3375 if (tcon == NULL) {
3376 rc = -ENOMEM;
3377 goto out_fail;
3378 }
3379
Steve French8b217fe2016-11-11 22:36:20 -06003380 if (volume_info->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06003381 if (ses->server->vals->protocol_id == 0) {
3382 cifs_dbg(VFS,
3383 "Use SMB2 or later for snapshot mount option\n");
3384 rc = -EOPNOTSUPP;
3385 goto out_fail;
3386 } else
3387 tcon->snapshot_time = volume_info->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06003388 }
3389
Steve Frenchca567eb2019-03-29 16:31:07 -05003390 if (volume_info->handle_timeout) {
3391 if (ses->server->vals->protocol_id == 0) {
3392 cifs_dbg(VFS,
3393 "Use SMB2.1 or later for handle timeout option\n");
3394 rc = -EOPNOTSUPP;
3395 goto out_fail;
3396 } else
3397 tcon->handle_timeout = volume_info->handle_timeout;
3398 }
3399
Jeff Laytond00c28d2010-04-24 07:57:44 -04003400 tcon->ses = ses;
3401 if (volume_info->password) {
3402 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
3403 if (!tcon->password) {
3404 rc = -ENOMEM;
3405 goto out_fail;
3406 }
3407 }
3408
Steve French23657ad2018-04-22 15:14:58 -05003409 if (volume_info->seal) {
3410 if (ses->server->vals->protocol_id == 0) {
3411 cifs_dbg(VFS,
3412 "SMB3 or later required for encryption\n");
3413 rc = -EOPNOTSUPP;
3414 goto out_fail;
3415 } else if (tcon->ses->server->capabilities &
3416 SMB2_GLOBAL_CAP_ENCRYPTION)
3417 tcon->seal = true;
3418 else {
3419 cifs_dbg(VFS, "Encryption is not supported on share\n");
3420 rc = -EOPNOTSUPP;
3421 goto out_fail;
3422 }
3423 }
3424
Steve French8505c8b2018-06-18 14:01:59 -05003425 if (volume_info->linux_ext) {
3426 if (ses->server->posix_ext_supported) {
Steve Frenchb3266142018-05-20 23:41:10 -05003427 tcon->posix_extensions = true;
Steve French2fbb5642018-06-12 12:11:31 -05003428 printk_once(KERN_WARNING
3429 "SMB3.11 POSIX Extensions are experimental\n");
Steve French8505c8b2018-06-18 14:01:59 -05003430 } else {
3431 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions.\n");
3432 rc = -EOPNOTSUPP;
3433 goto out_fail;
Steve French2fbb5642018-06-12 12:11:31 -05003434 }
Steve Frenchb3266142018-05-20 23:41:10 -05003435 }
Steve Frenchb3266142018-05-20 23:41:10 -05003436
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003437 /*
3438 * BB Do we need to wrap session_mutex around this TCon call and Unix
3439 * SetFS as we do on SessSetup and reconnect?
3440 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003441 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003442 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
3443 volume_info->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003444 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05003445 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003446 if (rc)
3447 goto out_fail;
3448
Steve Frenchb618f002015-11-03 09:15:03 -06003449 tcon->use_persistent = false;
3450 /* check if SMB2 or later, CIFS does not support persistent handles */
3451 if (volume_info->persistent) {
3452 if (ses->server->vals->protocol_id == 0) {
3453 cifs_dbg(VFS,
3454 "SMB3 or later required for persistent handles\n");
3455 rc = -EOPNOTSUPP;
3456 goto out_fail;
3457 } else if (ses->server->capabilities &
3458 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3459 tcon->use_persistent = true;
3460 else /* persistent handles requested but not supported */ {
3461 cifs_dbg(VFS,
3462 "Persistent handles not supported on share\n");
3463 rc = -EOPNOTSUPP;
3464 goto out_fail;
3465 }
3466 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
3467 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3468 && (volume_info->nopersistent == false)) {
3469 cifs_dbg(FYI, "enabling persistent handles\n");
3470 tcon->use_persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06003471 } else if (volume_info->resilient) {
3472 if (ses->server->vals->protocol_id == 0) {
3473 cifs_dbg(VFS,
3474 "SMB2.1 or later required for resilient handles\n");
3475 rc = -EOPNOTSUPP;
3476 goto out_fail;
3477 }
3478 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06003479 }
3480
Steve Frenchcae53f72019-09-03 17:49:46 -05003481 /* If the user really knows what they are doing they can override */
3482 if (tcon->share_flags & SMB2_SHAREFLAG_NO_CACHING) {
3483 if (volume_info->cache_ro)
3484 cifs_dbg(VFS, "cache=ro requested on mount but NO_CACHING flag set on share\n");
3485 else if (volume_info->cache_rw)
3486 cifs_dbg(VFS, "cache=singleclient requested on mount but NO_CACHING flag set on share\n");
3487 }
3488
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003489 /*
3490 * We can have only one retry value for a connection to a share so for
3491 * resources mounted more than once to the same server share the last
3492 * value passed in for the retry flag is used.
3493 */
Jeff Laytond00c28d2010-04-24 07:57:44 -04003494 tcon->retry = volume_info->retry;
3495 tcon->nocase = volume_info->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05003496 tcon->nohandlecache = volume_info->nohandlecache;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003497 tcon->local_lease = volume_info->local_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07003498 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003499
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303500 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003501 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303502 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003503
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303504 cifs_fscache_get_super_cookie(tcon);
3505
Jeff Laytond00c28d2010-04-24 07:57:44 -04003506 return tcon;
3507
3508out_fail:
3509 tconInfoFree(tcon);
3510 return ERR_PTR(rc);
3511}
3512
Jeff Layton9d002df2010-10-06 19:51:11 -04003513void
3514cifs_put_tlink(struct tcon_link *tlink)
3515{
3516 if (!tlink || IS_ERR(tlink))
3517 return;
3518
3519 if (!atomic_dec_and_test(&tlink->tl_count) ||
3520 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
3521 tlink->tl_time = jiffies;
3522 return;
3523 }
3524
3525 if (!IS_ERR(tlink_tcon(tlink)))
3526 cifs_put_tcon(tlink_tcon(tlink));
3527 kfree(tlink);
3528 return;
3529}
Jeff Laytond00c28d2010-04-24 07:57:44 -04003530
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003531static int
3532compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3533{
3534 struct cifs_sb_info *old = CIFS_SB(sb);
3535 struct cifs_sb_info *new = mnt_data->cifs_sb;
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03003536 unsigned int oldflags = old->mnt_cifs_flags & CIFS_MOUNT_MASK;
3537 unsigned int newflags = new->mnt_cifs_flags & CIFS_MOUNT_MASK;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003538
3539 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
3540 return 0;
3541
Paulo Alcantara (SUSE)29fbeb72019-06-18 16:16:02 -03003542 if (old->mnt_cifs_serverino_autodisabled)
3543 newflags &= ~CIFS_MOUNT_SERVER_INUM;
3544
3545 if (oldflags != newflags)
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003546 return 0;
3547
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003548 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003549 * We want to share sb only if we don't specify an r/wsize or
3550 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003551 */
3552 if (new->wsize && new->wsize < old->wsize)
3553 return 0;
3554
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003555 if (new->rsize && new->rsize < old->rsize)
3556 return 0;
3557
Eric W. Biederman1f682332013-02-06 01:20:20 -08003558 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003559 return 0;
3560
3561 if (old->mnt_file_mode != new->mnt_file_mode ||
3562 old->mnt_dir_mode != new->mnt_dir_mode)
3563 return 0;
3564
3565 if (strcmp(old->local_nls->charset, new->local_nls->charset))
3566 return 0;
3567
3568 if (old->actimeo != new->actimeo)
3569 return 0;
3570
3571 return 1;
3572}
3573
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003574static int
3575match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3576{
3577 struct cifs_sb_info *old = CIFS_SB(sb);
3578 struct cifs_sb_info *new = mnt_data->cifs_sb;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003579 bool old_set = old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
3580 bool new_set = new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003581
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003582 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003583 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003584 else if (!old_set && !new_set)
3585 return 1;
3586
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003587 return 0;
3588}
3589
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003590int
3591cifs_match_super(struct super_block *sb, void *data)
3592{
3593 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
3594 struct smb_vol *volume_info;
3595 struct cifs_sb_info *cifs_sb;
3596 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00003597 struct cifs_ses *ses;
3598 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003599 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003600 int rc = 0;
3601
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003602 spin_lock(&cifs_tcp_ses_lock);
3603 cifs_sb = CIFS_SB(sb);
3604 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3605 if (IS_ERR(tlink)) {
3606 spin_unlock(&cifs_tcp_ses_lock);
3607 return rc;
3608 }
3609 tcon = tlink_tcon(tlink);
3610 ses = tcon->ses;
3611 tcp_srv = ses->server;
3612
3613 volume_info = mnt_data->vol;
3614
Jeff Layton9fa114f2012-11-26 11:09:57 -05003615 if (!match_server(tcp_srv, volume_info) ||
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003616 !match_session(ses, volume_info) ||
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003617 !match_tcon(tcon, volume_info) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003618 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003619 rc = 0;
3620 goto out;
3621 }
3622
3623 rc = compare_mount_options(sb, mnt_data);
3624out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003625 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04003626 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003627 return rc;
3628}
3629
Jeff Layton09e50d52008-07-23 10:11:19 -04003630#ifdef CONFIG_DEBUG_LOCK_ALLOC
3631static struct lock_class_key cifs_key[2];
3632static struct lock_class_key cifs_slock_key[2];
3633
3634static inline void
3635cifs_reclassify_socket4(struct socket *sock)
3636{
3637 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003638 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003639 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3640 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3641}
3642
3643static inline void
3644cifs_reclassify_socket6(struct socket *sock)
3645{
3646 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003647 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003648 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3649 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3650}
3651#else
3652static inline void
3653cifs_reclassify_socket4(struct socket *sock)
3654{
3655}
3656
3657static inline void
3658cifs_reclassify_socket6(struct socket *sock)
3659{
3660}
3661#endif
3662
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00003664static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
Steve French50c2f752007-07-13 00:33:32 +00003666 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
Steve French50c2f752007-07-13 00:33:32 +00003668 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 /* mask a nibble at a time and encode */
3670 target[j] = 'A' + (0x0F & (source[i] >> 4));
3671 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00003672 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 }
3674
3675}
3676
Ben Greear3eb9a882010-09-01 17:06:02 -07003677static int
3678bind_socket(struct TCP_Server_Info *server)
3679{
3680 int rc = 0;
3681 if (server->srcaddr.ss_family != AF_UNSPEC) {
3682 /* Bind to the specified local IP address */
3683 struct socket *socket = server->ssocket;
3684 rc = socket->ops->bind(socket,
3685 (struct sockaddr *) &server->srcaddr,
3686 sizeof(server->srcaddr));
3687 if (rc < 0) {
3688 struct sockaddr_in *saddr4;
3689 struct sockaddr_in6 *saddr6;
3690 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3691 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3692 if (saddr6->sin6_family == AF_INET6)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003693 cifs_server_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05003694 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003695 else
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003696 cifs_server_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
Joe Perchesf96637b2013-05-04 22:12:25 -05003697 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003698 }
3699 }
3700 return rc;
3701}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
3703static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003704ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705{
3706 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003707 /*
3708 * some servers require RFC1001 sessinit before sending
3709 * negprot - BB check reconnection in case where second
3710 * sessinit is sent but no second negprot
3711 */
3712 struct rfc1002_session_packet *ses_init_buf;
3713 struct smb_hdr *smb_buf;
3714 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3715 GFP_KERNEL);
3716 if (ses_init_buf) {
3717 ses_init_buf->trailer.session_req.called_len = 32;
3718
Colin Ian King997152f2016-01-25 16:25:54 +00003719 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003720 rfc1002mangle(ses_init_buf->trailer.
3721 session_req.called_name,
3722 server->server_RFC1001_name,
3723 RFC1001_NAME_LEN_WITH_NULL);
3724 else
3725 rfc1002mangle(ses_init_buf->trailer.
3726 session_req.called_name,
3727 DEFAULT_CIFS_CALLED_NAME,
3728 RFC1001_NAME_LEN_WITH_NULL);
3729
3730 ses_init_buf->trailer.session_req.calling_len = 32;
3731
3732 /*
3733 * calling name ends in null (byte 16) from old smb
3734 * convention.
3735 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05003736 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003737 rfc1002mangle(ses_init_buf->trailer.
3738 session_req.calling_name,
3739 server->workstation_RFC1001_name,
3740 RFC1001_NAME_LEN_WITH_NULL);
3741 else
3742 rfc1002mangle(ses_init_buf->trailer.
3743 session_req.calling_name,
3744 "LINUX_CIFS_CLNT",
3745 RFC1001_NAME_LEN_WITH_NULL);
3746
3747 ses_init_buf->trailer.session_req.scope1 = 0;
3748 ses_init_buf->trailer.session_req.scope2 = 0;
3749 smb_buf = (struct smb_hdr *)ses_init_buf;
3750
3751 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003752 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003753 rc = smb_send(server, smb_buf, 0x44);
3754 kfree(ses_init_buf);
3755 /*
3756 * RFC1001 layer in at least one server
3757 * requires very short break before negprot
3758 * presumably because not expecting negprot
3759 * to follow so fast. This is a simple
3760 * solution that works without
3761 * complicating the code and causes no
3762 * significant slowing down on mount
3763 * for everyone else
3764 */
3765 usleep_range(1000, 2000);
3766 }
3767 /*
3768 * else the negprot may still work without this
3769 * even though malloc failed
3770 */
3771
3772 return rc;
3773}
3774
3775static int
3776generic_ip_connect(struct TCP_Server_Info *server)
3777{
3778 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00003779 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003780 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003781 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003782 struct sockaddr *saddr;
3783
3784 saddr = (struct sockaddr *) &server->dstaddr;
3785
3786 if (server->dstaddr.ss_family == AF_INET6) {
3787 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3788 slen = sizeof(struct sockaddr_in6);
3789 sfamily = AF_INET6;
3790 } else {
3791 sport = ((struct sockaddr_in *) saddr)->sin_port;
3792 slen = sizeof(struct sockaddr_in);
3793 sfamily = AF_INET;
3794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003796 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06003797 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3798 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 if (rc < 0) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10003800 cifs_server_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003801 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003804
3805 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05003806 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003807 server->ssocket = socket;
3808 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003809 if (sfamily == AF_INET6)
3810 cifs_reclassify_socket6(socket);
3811 else
3812 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 }
3814
Ben Greear3eb9a882010-09-01 17:06:02 -07003815 rc = bind_socket(server);
3816 if (rc < 0)
3817 return rc;
3818
Jeff Laytond5c56052008-12-01 18:42:33 -05003819 /*
3820 * Eventually check for other socket options to change from
3821 * the default. sock_setsockopt not used because it expects
3822 * user space buffer
3823 */
3824 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00003825 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00003826
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003827 /* make the bufsizes depend on wsize/rsize and max requests */
3828 if (server->noautotune) {
3829 if (socket->sk->sk_sndbuf < (200 * 1024))
3830 socket->sk->sk_sndbuf = 200 * 1024;
3831 if (socket->sk->sk_rcvbuf < (140 * 1024))
3832 socket->sk->sk_rcvbuf = 140 * 1024;
3833 }
3834
Steve French6a5fa2362010-01-01 01:28:43 +00003835 if (server->tcp_nodelay) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003836 int val = 1;
Steve French6a5fa2362010-01-01 01:28:43 +00003837 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3838 (char *)&val, sizeof(val));
3839 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003840 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3841 rc);
Steve French6a5fa2362010-01-01 01:28:43 +00003842 }
3843
Joe Perchesf96637b2013-05-04 22:12:25 -05003844 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003845 socket->sk->sk_sndbuf,
3846 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3847
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003848 rc = socket->ops->connect(socket, saddr, slen, 0);
3849 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003850 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003851 sock_release(socket);
3852 server->ssocket = NULL;
3853 return rc;
3854 }
3855
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003856 if (sport == htons(RFC1001_PORT))
3857 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00003858
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 return rc;
3860}
3861
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003862static int
3863ip_connect(struct TCP_Server_Info *server)
3864{
Steve French6da97912011-03-13 18:55:55 +00003865 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003866 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3867 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3868
3869 if (server->dstaddr.ss_family == AF_INET6)
3870 sport = &addr6->sin6_port;
3871 else
3872 sport = &addr->sin_port;
3873
3874 if (*sport == 0) {
3875 int rc;
3876
3877 /* try with 445 port at first */
3878 *sport = htons(CIFS_PORT);
3879
3880 rc = generic_ip_connect(server);
3881 if (rc >= 0)
3882 return rc;
3883
3884 /* if it failed, try with 139 port */
3885 *sport = htons(RFC1001_PORT);
3886 }
3887
3888 return generic_ip_connect(server);
3889}
3890
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003891void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Al Viro2c6292a2011-06-17 09:05:48 -04003892 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00003893{
3894 /* if we are reconnecting then should we check to see if
3895 * any requested capabilities changed locally e.g. via
3896 * remount but we can not do much about it here
3897 * if they have (even if we could detect it by the following)
3898 * Perhaps we could add a backpointer to array of sb from tcon
3899 * or if we change to make all sb to same share the same
3900 * sb as NFS - then we only have one backpointer to sb.
3901 * What if we wanted to mount the server share twice once with
3902 * and once without posixacls or posix paths? */
3903 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00003904
Steve Frenchc18c8422007-07-18 23:21:09 +00003905 if (vol_info && vol_info->no_linux_ext) {
3906 tcon->fsUnixInfo.Capability = 0;
3907 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05003908 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003909 return;
3910 } else if (vol_info)
3911 tcon->unix_ext = 1; /* Unix Extensions supported */
3912
3913 if (tcon->unix_ext == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003914 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003915 return;
3916 }
Steve French50c2f752007-07-13 00:33:32 +00003917
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003918 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00003919 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05003920 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve French8af18972007-02-14 04:42:51 +00003921 /* check for reconnect case in which we do not
3922 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003923 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00003924 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00003925 originally at mount time */
3926 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3927 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003928 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3929 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003930 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00003931 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003932 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003933 cifs_dbg(VFS, "possible reconnect error\n");
3934 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00003935 }
Steve French8af18972007-02-14 04:42:51 +00003936 }
Steve French50c2f752007-07-13 00:33:32 +00003937
Steve French6848b732011-05-26 18:38:54 +00003938 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003939 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00003940
Steve French8af18972007-02-14 04:42:51 +00003941 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00003942 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00003943 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003944 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003945 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003946 if (cifs_sb)
3947 cifs_sb->mnt_cifs_flags |=
3948 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00003949 }
3950
Steve French75865f8c2007-06-24 18:30:48 +00003951 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00003952 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003953 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003954 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003955 if (cifs_sb)
3956 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00003957 CIFS_MOUNT_POSIX_PATHS;
3958 }
Steve French50c2f752007-07-13 00:33:32 +00003959
Joe Perchesf96637b2013-05-04 22:12:25 -05003960 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00003961#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00003962 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003963 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003964 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003965 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003966 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003967 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003968 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003969 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003970 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003971 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003972 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003973 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003974 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003975 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00003976 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003977 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00003978 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003979 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00003980#endif /* CIFS_DEBUG2 */
3981 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00003982 if (vol_info == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003983 cifs_dbg(FYI, "resetting capabilities failed\n");
Steve French442aa312007-09-24 20:25:46 +00003984 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05003985 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 +00003986
Steve French8af18972007-02-14 04:42:51 +00003987 }
3988 }
3989}
3990
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003991int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003992 struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003993{
Jeff Layton2de970f2010-10-06 19:51:12 -04003994 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3995
Al Viro2ced6f62011-06-17 09:20:04 -04003996 spin_lock_init(&cifs_sb->tlink_tree_lock);
3997 cifs_sb->tlink_tree = RB_ROOT;
3998
Steve Frenche8506d22019-02-28 21:32:15 -06003999 cifs_sb->bsize = pvolume_info->bsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04004000 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04004001 * Temporarily set r/wsize for matching superblock. If we end up using
4002 * new sb then client will later negotiate it downward if needed.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04004003 */
Jeff Layton5eba8ab2011-10-19 15:30:26 -04004004 cifs_sb->rsize = pvolume_info->rsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04004005 cifs_sb->wsize = pvolume_info->wsize;
4006
Steve French3b795212008-11-13 19:45:32 +00004007 cifs_sb->mnt_uid = pvolume_info->linux_uid;
4008 cifs_sb->mnt_gid = pvolume_info->linux_gid;
4009 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
4010 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
Joe Perchesf96637b2013-05-04 22:12:25 -05004011 cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
4012 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
Steve French3b795212008-11-13 19:45:32 +00004013
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05304014 cifs_sb->actimeo = pvolume_info->actimeo;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004015 cifs_sb->local_nls = pvolume_info->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05304016
Aurelien Aptel83930722018-09-20 18:10:25 -07004017 if (pvolume_info->nodfs)
4018 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_DFS;
Steve French3b795212008-11-13 19:45:32 +00004019 if (pvolume_info->noperm)
4020 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
4021 if (pvolume_info->setuids)
4022 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
Steve French95932652016-09-23 01:36:34 -05004023 if (pvolume_info->setuidfromacl)
4024 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
Steve French3b795212008-11-13 19:45:32 +00004025 if (pvolume_info->server_ino)
4026 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
4027 if (pvolume_info->remap)
Steve French2baa2682014-09-27 02:19:01 -05004028 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
4029 if (pvolume_info->sfu_remap)
Steve French3b795212008-11-13 19:45:32 +00004030 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
4031 if (pvolume_info->no_xattr)
4032 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
4033 if (pvolume_info->sfu_emul)
4034 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
4035 if (pvolume_info->nobrl)
4036 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve French3d4ef9a2018-04-25 22:19:09 -05004037 if (pvolume_info->nohandlecache)
4038 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
Steve Frenchbe652442009-02-23 15:21:59 +00004039 if (pvolume_info->nostrictsync)
Steve French4717bed2009-02-24 14:44:19 +00004040 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
Steve French13a6e422008-12-02 17:24:33 +00004041 if (pvolume_info->mand_lock)
4042 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00004043 if (pvolume_info->rwpidforward)
4044 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
Steve French412094a2019-06-24 02:01:42 -05004045 if (pvolume_info->mode_ace)
4046 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MODE_FROM_SID;
Steve French3b795212008-11-13 19:45:32 +00004047 if (pvolume_info->cifs_acl)
4048 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01004049 if (pvolume_info->backupuid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05004050 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01004051 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
4052 }
4053 if (pvolume_info->backupgid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05004054 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01004055 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
4056 }
Steve French3b795212008-11-13 19:45:32 +00004057 if (pvolume_info->override_uid)
4058 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
4059 if (pvolume_info->override_gid)
4060 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
4061 if (pvolume_info->dynperm)
4062 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05304063 if (pvolume_info->fsc)
4064 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
Jeff Layton0eb8a132010-10-06 19:51:12 -04004065 if (pvolume_info->multiuser)
4066 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
4067 CIFS_MOUNT_NO_PERM);
Pavel Shilovskyd39454f2011-01-24 14:16:35 -05004068 if (pvolume_info->strict_io)
4069 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
Steve French3b795212008-11-13 19:45:32 +00004070 if (pvolume_info->direct_io) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004071 cifs_dbg(FYI, "mounting share using direct i/o\n");
Steve French3b795212008-11-13 19:45:32 +00004072 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
4073 }
Steve French83bbfa72019-08-27 23:58:54 -05004074 if (pvolume_info->cache_ro) {
4075 cifs_dbg(VFS, "mounting share with read only caching. Ensure that the share will not be modified while in use.\n");
4076 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RO_CACHE;
Steve French41e033f2019-08-30 02:12:41 -05004077 } else if (pvolume_info->cache_rw) {
4078 cifs_dbg(VFS, "mounting share in single client RW caching mode. Ensure that no other systems will be accessing the share.\n");
4079 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_RO_CACHE |
4080 CIFS_MOUNT_RW_CACHE);
Steve French83bbfa72019-08-27 23:58:54 -05004081 }
Stefan Metzmacher736a33202010-07-30 14:56:00 +02004082 if (pvolume_info->mfsymlinks) {
4083 if (pvolume_info->sfu_emul) {
Steve Frenchdb8b6312014-09-22 05:13:55 -05004084 /*
4085 * Our SFU ("Services for Unix" emulation does not allow
4086 * creating symlinks but does allow reading existing SFU
4087 * symlinks (it does allow both creating and reading SFU
4088 * style mknod and FIFOs though). When "mfsymlinks" and
4089 * "sfu" are both enabled at the same time, it allows
4090 * reading both types of symlinks, but will only create
4091 * them with mfsymlinks format. This allows better
4092 * Apple compatibility (probably better for Samba too)
4093 * while still recognizing old Windows style symlinks.
4094 */
4095 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
Stefan Metzmacher736a33202010-07-30 14:56:00 +02004096 }
Steve Frenchdb8b6312014-09-22 05:13:55 -05004097 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
Stefan Metzmacher736a33202010-07-30 14:56:00 +02004098 }
Steve French3b795212008-11-13 19:45:32 +00004099
4100 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05004101 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01004102
4103 if (pvolume_info->prepath) {
4104 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
4105 if (cifs_sb->prepath == NULL)
4106 return -ENOMEM;
4107 }
4108
4109 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04004110}
4111
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004112void
4113cifs_cleanup_volume_info_contents(struct smb_vol *volume_info)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004114{
Sean Finneyb9468452011-04-11 13:19:32 +00004115 kfree(volume_info->username);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004116 kzfree(volume_info->password);
Jesper Juhl95c75452011-08-27 18:58:34 +02004117 kfree(volume_info->UNC);
Sean Finneyb9468452011-04-11 13:19:32 +00004118 kfree(volume_info->domainname);
4119 kfree(volume_info->iocharset);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004120 kfree(volume_info->prepath);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004121}
4122
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004123void
4124cifs_cleanup_volume_info(struct smb_vol *volume_info)
4125{
4126 if (!volume_info)
4127 return;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004128 cifs_cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004129 kfree(volume_info);
4130}
4131
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004132/* Release all succeed connections */
4133static inline void mount_put_conns(struct cifs_sb_info *cifs_sb,
4134 unsigned int xid,
4135 struct TCP_Server_Info *server,
4136 struct cifs_ses *ses, struct cifs_tcon *tcon)
4137{
4138 int rc = 0;
4139
4140 if (tcon)
4141 cifs_put_tcon(tcon);
4142 else if (ses)
4143 cifs_put_smb_ses(ses);
4144 else if (server)
4145 cifs_put_tcp_session(server, 0);
4146 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
4147 free_xid(xid);
4148}
4149
4150/* Get connections for tcp, ses and tcon */
4151static int mount_get_conns(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
4152 unsigned int *xid,
4153 struct TCP_Server_Info **nserver,
4154 struct cifs_ses **nses, struct cifs_tcon **ntcon)
4155{
4156 int rc = 0;
4157 struct TCP_Server_Info *server;
4158 struct cifs_ses *ses;
4159 struct cifs_tcon *tcon;
4160
4161 *nserver = NULL;
4162 *nses = NULL;
4163 *ntcon = NULL;
4164
4165 *xid = get_xid();
4166
4167 /* get a reference to a tcp session */
4168 server = cifs_get_tcp_session(vol);
4169 if (IS_ERR(server)) {
4170 rc = PTR_ERR(server);
4171 return rc;
4172 }
4173
4174 *nserver = server;
4175
4176 if ((vol->max_credits < 20) || (vol->max_credits > 60000))
4177 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
4178 else
4179 server->max_credits = vol->max_credits;
4180
4181 /* get a reference to a SMB session */
4182 ses = cifs_get_smb_ses(server, vol);
4183 if (IS_ERR(ses)) {
4184 rc = PTR_ERR(ses);
4185 return rc;
4186 }
4187
4188 *nses = ses;
4189
4190 if ((vol->persistent == true) && (!(ses->server->capabilities &
4191 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10004192 cifs_server_dbg(VFS, "persistent handles not supported by server\n");
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004193 return -EOPNOTSUPP;
4194 }
4195
4196 /* search for existing tcon to this server share */
4197 tcon = cifs_get_tcon(ses, vol);
4198 if (IS_ERR(tcon)) {
4199 rc = PTR_ERR(tcon);
4200 return rc;
4201 }
4202
4203 *ntcon = tcon;
4204
4205 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4206 if (tcon->posix_extensions)
4207 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
4208
4209 /* tell server which Unix caps we support */
4210 if (cap_unix(tcon->ses)) {
4211 /*
4212 * reset of caps checks mount to see if unix extensions disabled
4213 * for just this mount.
4214 */
4215 reset_cifs_unix_caps(*xid, tcon, cifs_sb, vol);
4216 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
4217 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
4218 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP))
4219 return -EACCES;
4220 } else
4221 tcon->unix_ext = 0; /* server does not support them */
4222
4223 /* do not care if a following call succeed - informational */
Steve French1981eba2019-08-29 22:33:38 -05004224 if (!tcon->pipe && server->ops->qfs_tcon) {
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004225 server->ops->qfs_tcon(*xid, tcon);
Steve French1981eba2019-08-29 22:33:38 -05004226 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RO_CACHE) {
4227 if (tcon->fsDevInfo.DeviceCharacteristics &
4228 FILE_READ_ONLY_DEVICE)
4229 cifs_dbg(VFS, "mounted to read only share\n");
Steve French41e033f2019-08-30 02:12:41 -05004230 else if ((cifs_sb->mnt_cifs_flags &
4231 CIFS_MOUNT_RW_CACHE) == 0)
Steve French1981eba2019-08-29 22:33:38 -05004232 cifs_dbg(VFS, "read only mount of RW share\n");
Steve French41e033f2019-08-30 02:12:41 -05004233 /* no need to log a RW mount of a typical RW share */
Steve French1981eba2019-08-29 22:33:38 -05004234 }
4235 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004236
4237 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, vol);
4238 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, vol);
4239
4240 return 0;
4241}
4242
4243static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
4244 struct cifs_tcon *tcon)
4245{
4246 struct tcon_link *tlink;
4247
4248 /* hang the tcon off of the superblock */
4249 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4250 if (tlink == NULL)
4251 return -ENOMEM;
4252
4253 tlink->tl_uid = ses->linux_uid;
4254 tlink->tl_tcon = tcon;
4255 tlink->tl_time = jiffies;
4256 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
4257 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4258
4259 cifs_sb->master_tlink = tlink;
4260 spin_lock(&cifs_sb->tlink_tree_lock);
4261 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4262 spin_unlock(&cifs_sb->tlink_tree_lock);
4263
4264 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
4265 TLINK_IDLE_EXPIRE);
4266 return 0;
4267}
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004268
Steve French2d6d5892009-04-09 00:36:44 +00004269#ifdef CONFIG_CIFS_DFS_UPCALL
Steve French6d3ea7e2012-11-28 22:34:41 -06004270/*
4271 * cifs_build_path_to_root returns full path to root when we do not have an
4272 * exiting connection (tcon)
4273 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004274static char *
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004275build_unc_path_to_root(const struct smb_vol *vol,
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004276 const struct cifs_sb_info *cifs_sb, bool useppath)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004277{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004278 char *full_path, *pos;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004279 unsigned int pplen = useppath && vol->prepath ?
4280 strlen(vol->prepath) + 1 : 0;
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004281 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004282
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004283 if (unc_len > MAX_TREE_SIZE)
4284 return ERR_PTR(-EINVAL);
4285
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004286 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004287 if (full_path == NULL)
4288 return ERR_PTR(-ENOMEM);
4289
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004290 memcpy(full_path, vol->UNC, unc_len);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004291 pos = full_path + unc_len;
4292
4293 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04004294 *pos = CIFS_DIR_SEP(cifs_sb);
Ronnie Sahlberg340625e2019-08-27 09:30:14 +10004295 memcpy(pos + 1, vol->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04004296 pos += pplen;
4297 }
4298
4299 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00004300 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05004301 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004302 return full_path;
4303}
Sean Finneydd613942011-04-11 13:19:30 +00004304
Paulo Alcantara1c780222018-11-14 16:24:03 -02004305/**
4306 * expand_dfs_referral - Perform a dfs referral query and update the cifs_sb
4307 *
Sean Finneydd613942011-04-11 13:19:30 +00004308 *
Sean Finney046462a2011-04-11 13:19:33 +00004309 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
4310 * to a string containing updated options for the submount. Otherwise it
4311 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00004312 *
4313 * Returns the rc from get_dfs_path to the caller, which can be used to
4314 * determine whether there were referrals.
4315 */
4316static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04004317expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Sean Finneydd613942011-04-11 13:19:30 +00004318 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
Sean Finney046462a2011-04-11 13:19:33 +00004319 int check_prefix)
Sean Finneydd613942011-04-11 13:19:30 +00004320{
4321 int rc;
Paulo Alcantara1c780222018-11-14 16:24:03 -02004322 struct dfs_info3_param referral = {0};
Sean Finneydd613942011-04-11 13:19:30 +00004323 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
4324
Aurelien Aptel83930722018-09-20 18:10:25 -07004325 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
4326 return -EREMOTE;
4327
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004328 full_path = build_unc_path_to_root(volume_info, cifs_sb, true);
Sean Finneydd613942011-04-11 13:19:30 +00004329 if (IS_ERR(full_path))
4330 return PTR_ERR(full_path);
4331
4332 /* For DFS paths, skip the first '\' of the UNC */
4333 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
4334
Paulo Alcantara1c780222018-11-14 16:24:03 -02004335 rc = dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
4336 ref_path, &referral, NULL);
4337 if (!rc) {
Sean Finneydd613942011-04-11 13:19:30 +00004338 char *fake_devname = NULL;
4339
4340 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
Paulo Alcantara1c780222018-11-14 16:24:03 -02004341 full_path + 1, &referral,
Sean Finneydd613942011-04-11 13:19:30 +00004342 &fake_devname);
Paulo Alcantara1c780222018-11-14 16:24:03 -02004343 free_dfs_info_param(&referral);
Sean Finney046462a2011-04-11 13:19:33 +00004344
Sean Finneydd613942011-04-11 13:19:30 +00004345 if (IS_ERR(mdata)) {
4346 rc = PTR_ERR(mdata);
4347 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004348 } else {
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004349 cifs_cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004350 rc = cifs_setup_volume_info(volume_info, mdata,
Paulo Alcantara1c780222018-11-14 16:24:03 -02004351 fake_devname, false);
Sean Finneydd613942011-04-11 13:19:30 +00004352 }
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04004353 kfree(fake_devname);
4354 kfree(cifs_sb->mountdata);
Sean Finney046462a2011-04-11 13:19:33 +00004355 cifs_sb->mountdata = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00004356 }
4357 kfree(full_path);
4358 return rc;
4359}
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004360
4361static inline int get_next_dfs_tgt(const char *path,
4362 struct dfs_cache_tgt_list *tgt_list,
4363 struct dfs_cache_tgt_iterator **tgt_it)
4364{
4365 if (!*tgt_it)
4366 *tgt_it = dfs_cache_get_tgt_iterator(tgt_list);
4367 else
4368 *tgt_it = dfs_cache_get_next_tgt(tgt_list, *tgt_it);
4369 return !*tgt_it ? -EHOSTDOWN : 0;
4370}
4371
4372static int update_vol_info(const struct dfs_cache_tgt_iterator *tgt_it,
4373 struct smb_vol *fake_vol, struct smb_vol *vol)
4374{
4375 const char *tgt = dfs_cache_get_tgt_name(tgt_it);
4376 int len = strlen(tgt) + 2;
4377 char *new_unc;
4378
4379 new_unc = kmalloc(len, GFP_KERNEL);
4380 if (!new_unc)
4381 return -ENOMEM;
Ronnie Sahlberg74ea5f92019-02-09 09:51:11 +10004382 scnprintf(new_unc, len, "\\%s", tgt);
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004383
4384 kfree(vol->UNC);
4385 vol->UNC = new_unc;
4386
4387 if (fake_vol->prepath) {
4388 kfree(vol->prepath);
4389 vol->prepath = fake_vol->prepath;
4390 fake_vol->prepath = NULL;
4391 }
4392 memcpy(&vol->dstaddr, &fake_vol->dstaddr, sizeof(vol->dstaddr));
4393
4394 return 0;
4395}
4396
4397static int setup_dfs_tgt_conn(const char *path,
4398 const struct dfs_cache_tgt_iterator *tgt_it,
4399 struct cifs_sb_info *cifs_sb,
4400 struct smb_vol *vol,
4401 unsigned int *xid,
4402 struct TCP_Server_Info **server,
4403 struct cifs_ses **ses,
4404 struct cifs_tcon **tcon)
4405{
4406 int rc;
4407 struct dfs_info3_param ref = {0};
4408 char *mdata = NULL, *fake_devname = NULL;
4409 struct smb_vol fake_vol = {0};
4410
4411 cifs_dbg(FYI, "%s: dfs path: %s\n", __func__, path);
4412
4413 rc = dfs_cache_get_tgt_referral(path, tgt_it, &ref);
4414 if (rc)
4415 return rc;
4416
4417 mdata = cifs_compose_mount_options(cifs_sb->mountdata, path, &ref,
4418 &fake_devname);
4419 free_dfs_info_param(&ref);
4420
4421 if (IS_ERR(mdata)) {
4422 rc = PTR_ERR(mdata);
4423 mdata = NULL;
4424 } else {
4425 cifs_dbg(FYI, "%s: fake_devname: %s\n", __func__, fake_devname);
4426 rc = cifs_setup_volume_info(&fake_vol, mdata, fake_devname,
4427 false);
4428 }
4429 kfree(mdata);
4430 kfree(fake_devname);
4431
4432 if (!rc) {
4433 /*
4434 * We use a 'fake_vol' here because we need pass it down to the
4435 * mount_{get,put} functions to test connection against new DFS
4436 * targets.
4437 */
4438 mount_put_conns(cifs_sb, *xid, *server, *ses, *tcon);
4439 rc = mount_get_conns(&fake_vol, cifs_sb, xid, server, ses,
4440 tcon);
4441 if (!rc) {
4442 /*
4443 * We were able to connect to new target server.
4444 * Update current volume info with new target server.
4445 */
4446 rc = update_vol_info(tgt_it, &fake_vol, vol);
4447 }
4448 }
4449 cifs_cleanup_volume_info_contents(&fake_vol);
4450 return rc;
4451}
4452
4453static int mount_do_dfs_failover(const char *path,
4454 struct cifs_sb_info *cifs_sb,
4455 struct smb_vol *vol,
4456 struct cifs_ses *root_ses,
4457 unsigned int *xid,
4458 struct TCP_Server_Info **server,
4459 struct cifs_ses **ses,
4460 struct cifs_tcon **tcon)
4461{
4462 int rc;
4463 struct dfs_cache_tgt_list tgt_list;
4464 struct dfs_cache_tgt_iterator *tgt_it = NULL;
4465
4466 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
4467 return -EOPNOTSUPP;
4468
4469 rc = dfs_cache_noreq_find(path, NULL, &tgt_list);
4470 if (rc)
4471 return rc;
4472
4473 for (;;) {
4474 /* Get next DFS target server - if any */
4475 rc = get_next_dfs_tgt(path, &tgt_list, &tgt_it);
4476 if (rc)
4477 break;
4478 /* Connect to next DFS target */
4479 rc = setup_dfs_tgt_conn(path, tgt_it, cifs_sb, vol, xid, server,
4480 ses, tcon);
4481 if (!rc || rc == -EACCES || rc == -EOPNOTSUPP)
4482 break;
4483 }
4484 if (!rc) {
4485 /*
4486 * Update DFS target hint in DFS referral cache with the target
4487 * server we successfully reconnected to.
4488 */
4489 rc = dfs_cache_update_tgthint(*xid, root_ses ? root_ses : *ses,
4490 cifs_sb->local_nls,
4491 cifs_remap(cifs_sb), path,
4492 tgt_it);
4493 }
4494 dfs_cache_free_tgts(&tgt_list);
4495 return rc;
4496}
Steve French2d6d5892009-04-09 00:36:44 +00004497#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004498
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03004499int
Jeff Layton04db79b2011-07-06 08:10:38 -04004500cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
Steve Frenchc7c137b2018-06-06 17:59:29 -05004501 const char *devname, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502{
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004503 int rc = 0;
Sean Finneydd613942011-04-11 13:19:30 +00004504
Steve Frenchc7c137b2018-06-06 17:59:29 -05004505 if (cifs_parse_mount_options(mount_data, devname, volume_info, is_smb3))
Jeff Layton04db79b2011-07-06 08:10:38 -04004506 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
Jeff Layton7586b762008-12-01 18:41:49 -05004508 if (volume_info->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004509 cifs_dbg(FYI, "Anonymous login\n");
Jeff Layton04febab2012-01-17 16:09:15 -05004510 kfree(volume_info->username);
4511 volume_info->username = NULL;
Jeff Layton7586b762008-12-01 18:41:49 -05004512 } else if (volume_info->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 /* BB fixme parse for domain name here */
Joe Perchesf96637b2013-05-04 22:12:25 -05004514 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05004516 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00004517 /* In userspace mount helper we can get user name from alternate
4518 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04004519 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 }
4521
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 /* this is needed for ASCII cp to Unicode converts */
Jeff Layton7586b762008-12-01 18:41:49 -05004523 if (volume_info->iocharset == NULL) {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04004524 /* load_nls_default cannot return null */
4525 volume_info->local_nls = load_nls_default();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 } else {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04004527 volume_info->local_nls = load_nls(volume_info->iocharset);
4528 if (volume_info->local_nls == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004529 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00004530 volume_info->iocharset);
Jeff Layton04db79b2011-07-06 08:10:38 -04004531 return -ELIBACC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 }
4533 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004534
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004535 return rc;
Jeff Layton04db79b2011-07-06 08:10:38 -04004536}
4537
4538struct smb_vol *
Steve Frenchc7c137b2018-06-06 17:59:29 -05004539cifs_get_volume_info(char *mount_data, const char *devname, bool is_smb3)
Jeff Layton04db79b2011-07-06 08:10:38 -04004540{
4541 int rc;
4542 struct smb_vol *volume_info;
4543
Jeff Layton6ee95422012-11-26 11:09:57 -05004544 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
Jeff Layton04db79b2011-07-06 08:10:38 -04004545 if (!volume_info)
4546 return ERR_PTR(-ENOMEM);
4547
Steve Frenchc7c137b2018-06-06 17:59:29 -05004548 rc = cifs_setup_volume_info(volume_info, mount_data, devname, is_smb3);
Jeff Layton04db79b2011-07-06 08:10:38 -04004549 if (rc) {
4550 cifs_cleanup_volume_info(volume_info);
4551 volume_info = ERR_PTR(rc);
4552 }
4553
4554 return volume_info;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004555}
4556
Aurelien Aptela6b50582016-05-25 19:59:09 +02004557static int
4558cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
4559 unsigned int xid,
4560 struct cifs_tcon *tcon,
4561 struct cifs_sb_info *cifs_sb,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10004562 char *full_path,
4563 int added_treename)
Aurelien Aptela6b50582016-05-25 19:59:09 +02004564{
4565 int rc;
4566 char *s;
4567 char sep, tmp;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10004568 int skip = added_treename ? 1 : 0;
Aurelien Aptela6b50582016-05-25 19:59:09 +02004569
4570 sep = CIFS_DIR_SEP(cifs_sb);
4571 s = full_path;
4572
4573 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
4574 while (rc == 0) {
4575 /* skip separators */
4576 while (*s == sep)
4577 s++;
4578 if (!*s)
4579 break;
4580 /* next separator */
4581 while (*s && *s != sep)
4582 s++;
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10004583 /*
4584 * if the treename is added, we then have to skip the first
4585 * part within the separators
4586 */
4587 if (skip) {
4588 skip = 0;
4589 continue;
4590 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02004591 /*
4592 * temporarily null-terminate the path at the end of
4593 * the current component
4594 */
4595 tmp = *s;
4596 *s = 0;
4597 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4598 full_path);
4599 *s = tmp;
4600 }
4601 return rc;
4602}
4603
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004604/*
4605 * Check if path is remote (e.g. a DFS share). Return -EREMOTE if it is,
4606 * otherwise 0.
4607 */
4608static int is_path_remote(struct cifs_sb_info *cifs_sb, struct smb_vol *vol,
4609 const unsigned int xid,
4610 struct TCP_Server_Info *server,
4611 struct cifs_tcon *tcon)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004612{
Jeff Layton1daaae82012-03-21 06:30:40 -04004613 int rc;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004614 char *full_path;
4615
4616 if (!server->ops->is_path_accessible)
4617 return -EOPNOTSUPP;
4618
4619 /*
4620 * cifs_build_path_to_root works only when we have a valid tcon
4621 */
4622 full_path = cifs_build_path_to_root(vol, cifs_sb, tcon,
4623 tcon->Flags & SMB_SHARE_IS_IN_DFS);
4624 if (full_path == NULL)
4625 return -ENOMEM;
4626
4627 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
4628
4629 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4630 full_path);
4631 if (rc != 0 && rc != -EREMOTE) {
4632 kfree(full_path);
4633 return rc;
4634 }
4635
4636 if (rc != -EREMOTE) {
4637 rc = cifs_are_all_path_components_accessible(server, xid, tcon,
Ronnie Sahlbergce465bf2019-07-11 13:46:58 +10004638 cifs_sb, full_path, tcon->Flags & SMB_SHARE_IS_IN_DFS);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004639 if (rc != 0) {
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10004640 cifs_server_dbg(VFS, "cannot query dirs between root and final path, "
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004641 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
4642 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4643 rc = 0;
4644 }
4645 }
4646
4647 kfree(full_path);
4648 return rc;
4649}
4650
4651#ifdef CONFIG_CIFS_DFS_UPCALL
4652int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
4653{
4654 int rc = 0;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004655 unsigned int xid;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004656 struct cifs_ses *ses;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004657 struct cifs_tcon *root_tcon = NULL;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004658 struct cifs_tcon *tcon = NULL;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004659 struct TCP_Server_Info *server;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004660 char *root_path = NULL, *full_path = NULL;
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03004661 char *old_mountdata, *origin_mountdata = NULL;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004662 int count;
Al Virodd854462011-06-17 08:24:42 -04004663
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004664 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4665 if (!rc && tcon) {
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004666 /* If not a standalone DFS root, then check if path is remote */
4667 rc = dfs_cache_find(xid, ses, cifs_sb->local_nls,
4668 cifs_remap(cifs_sb), vol->UNC + 1, NULL,
4669 NULL);
4670 if (rc) {
4671 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4672 if (!rc)
4673 goto out;
4674 if (rc != -EREMOTE)
4675 goto error;
4676 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004677 }
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004678 /*
4679 * If first DFS target server went offline and we failed to connect it,
4680 * server and ses pointers are NULL at this point, though we still have
4681 * chance to get a cached DFS referral in expand_dfs_referral() and
4682 * retry next target available in it.
4683 *
4684 * If a NULL ses ptr is passed to dfs_cache_find(), a lookup will be
4685 * performed against DFS path and *no* requests will be sent to server
4686 * for any new DFS referrals. Hence it's safe to skip checking whether
4687 * server or ses ptr is NULL.
4688 */
4689 if (rc == -EACCES || rc == -EOPNOTSUPP)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004690 goto error;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004691
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004692 root_path = build_unc_path_to_root(vol, cifs_sb, false);
4693 if (IS_ERR(root_path)) {
4694 rc = PTR_ERR(root_path);
4695 root_path = NULL;
4696 goto error;
4697 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02004699 full_path = build_unc_path_to_root(vol, cifs_sb, true);
4700 if (IS_ERR(full_path)) {
4701 rc = PTR_ERR(full_path);
4702 full_path = NULL;
4703 goto error;
4704 }
Sean Finneyc1508ca2011-04-11 13:19:31 +00004705 /*
4706 * Perform an unconditional check for whether there are DFS
4707 * referrals for this path without prefix, to provide support
4708 * for DFS referrals from w2k8 servers which don't seem to respond
4709 * with PATH_NOT_COVERED to requests that include the prefix.
4710 * Chase the referral if found, otherwise continue normally.
4711 */
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004712 old_mountdata = cifs_sb->mountdata;
4713 (void)expand_dfs_referral(xid, ses, vol, cifs_sb, false);
Sean Finneyc1508ca2011-04-11 13:19:31 +00004714
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004715 if (cifs_sb->mountdata == NULL) {
4716 rc = -ENOENT;
4717 goto error;
4718 }
4719
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03004720 /* Save DFS root volume information for DFS refresh worker */
4721 origin_mountdata = kstrndup(cifs_sb->mountdata,
4722 strlen(cifs_sb->mountdata), GFP_KERNEL);
4723 if (!origin_mountdata) {
4724 rc = -ENOMEM;
4725 goto error;
4726 }
4727
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004728 if (cifs_sb->mountdata != old_mountdata) {
4729 /* If we were redirected, reconnect to new target server */
4730 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4731 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4732 }
4733 if (rc) {
4734 if (rc == -EACCES || rc == -EOPNOTSUPP)
4735 goto error;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004736 /* Perform DFS failover to any other DFS targets */
4737 rc = mount_do_dfs_failover(root_path + 1, cifs_sb, vol, NULL,
4738 &xid, &server, &ses, &tcon);
4739 if (rc)
4740 goto error;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004741 }
4742
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004743 kfree(root_path);
4744 root_path = build_unc_path_to_root(vol, cifs_sb, false);
4745 if (IS_ERR(root_path)) {
4746 rc = PTR_ERR(root_path);
4747 root_path = NULL;
4748 goto error;
4749 }
4750 /* Cache out resolved root server */
4751 (void)dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
4752 root_path + 1, NULL, NULL);
4753 /*
4754 * Save root tcon for additional DFS requests to update or create a new
4755 * DFS cache entry, or even perform DFS failover.
4756 */
4757 spin_lock(&cifs_tcp_ses_lock);
4758 tcon->tc_count++;
4759 tcon->dfs_path = root_path;
4760 root_path = NULL;
4761 tcon->remap = cifs_remap(cifs_sb);
4762 spin_unlock(&cifs_tcp_ses_lock);
4763
4764 root_tcon = tcon;
4765
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004766 for (count = 1; ;) {
4767 if (!rc && tcon) {
4768 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4769 if (!rc || rc != -EREMOTE)
4770 break;
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004771 }
Steve French6d3ea7e2012-11-28 22:34:41 -06004772 /*
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004773 * BB: when we implement proper loop detection,
4774 * we will remove this check. But now we need it
4775 * to prevent an indefinite loop if 'DFS tree' is
4776 * misconfigured (i.e. has loops).
Steve French6d3ea7e2012-11-28 22:34:41 -06004777 */
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004778 if (count++ > MAX_NESTED_LINKS) {
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004779 rc = -ELOOP;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004780 break;
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004781 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004782
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004783 kfree(full_path);
4784 full_path = build_unc_path_to_root(vol, cifs_sb, true);
4785 if (IS_ERR(full_path)) {
4786 rc = PTR_ERR(full_path);
4787 full_path = NULL;
4788 break;
4789 }
4790
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004791 old_mountdata = cifs_sb->mountdata;
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004792 rc = expand_dfs_referral(xid, root_tcon->ses, vol, cifs_sb,
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004793 true);
4794 if (rc)
4795 break;
Jeff Layton7b91e262009-07-23 15:22:30 -04004796
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004797 if (cifs_sb->mountdata != old_mountdata) {
4798 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4799 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses,
4800 &tcon);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004801 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004802 if (rc) {
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004803 if (rc == -EACCES || rc == -EOPNOTSUPP)
4804 break;
4805 /* Perform DFS failover to any other DFS targets */
4806 rc = mount_do_dfs_failover(full_path + 1, cifs_sb, vol,
4807 root_tcon->ses, &xid,
4808 &server, &ses, &tcon);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004809 if (rc == -EACCES || rc == -EOPNOTSUPP || !server ||
4810 !ses)
4811 goto error;
4812 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004813 }
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004814 cifs_put_tcon(root_tcon);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004815
Jeff Layton9d002df2010-10-06 19:51:11 -04004816 if (rc)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004817 goto error;
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004818
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02004819 spin_lock(&cifs_tcp_ses_lock);
4820 if (!tcon->dfs_path) {
4821 /* Save full path in new tcon to do failover when reconnecting tcons */
4822 tcon->dfs_path = full_path;
4823 full_path = NULL;
4824 tcon->remap = cifs_remap(cifs_sb);
4825 }
4826 cifs_sb->origin_fullpath = kstrndup(tcon->dfs_path,
YueHaibing2f0a6172018-12-18 01:34:39 +00004827 strlen(tcon->dfs_path),
4828 GFP_ATOMIC);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02004829 if (!cifs_sb->origin_fullpath) {
4830 spin_unlock(&cifs_tcp_ses_lock);
4831 rc = -ENOMEM;
4832 goto error;
4833 }
4834 spin_unlock(&cifs_tcp_ses_lock);
4835
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03004836 rc = dfs_cache_add_vol(origin_mountdata, vol, cifs_sb->origin_fullpath);
Paulo Alcantarae511d312018-11-14 17:16:44 -02004837 if (rc) {
4838 kfree(cifs_sb->origin_fullpath);
4839 goto error;
4840 }
Aurelien Aptel5fc7fcd2018-11-16 16:13:25 +01004841 /*
4842 * After reconnecting to a different server, unique ids won't
4843 * match anymore, so we disable serverino. This prevents
4844 * dentry revalidation to think the dentry are stale (ESTALE).
4845 */
4846 cifs_autodisable_serverino(cifs_sb);
Jeff Layton70fe7dc2007-11-16 22:21:07 +00004847out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004848 free_xid(xid);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004849 return mount_setup_tlink(cifs_sb, ses, tcon);
4850
4851error:
Paulo Alcantara4a367dc2018-11-14 16:53:52 -02004852 kfree(full_path);
4853 kfree(root_path);
Paulo Alcantara (SUSE)50720102019-03-19 16:54:29 -03004854 kfree(origin_mountdata);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004855 mount_put_conns(cifs_sb, xid, server, ses, tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 return rc;
4857}
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004858#else
4859int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
4860{
4861 int rc = 0;
4862 unsigned int xid;
4863 struct cifs_ses *ses;
4864 struct cifs_tcon *tcon;
4865 struct TCP_Server_Info *server;
4866
4867 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4868 if (rc)
4869 goto error;
4870
4871 if (tcon) {
4872 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4873 if (rc == -EREMOTE)
4874 rc = -EOPNOTSUPP;
4875 if (rc)
4876 goto error;
4877 }
4878
4879 free_xid(xid);
4880
4881 return mount_setup_tlink(cifs_sb, ses, tcon);
4882
4883error:
4884 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4885 return rc;
4886}
4887#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888
Jeff Layton8d1bca32011-06-11 21:17:10 -04004889/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01004890 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04004891 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04004893CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00004894 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895 const struct nls_table *nls_codepage)
4896{
4897 struct smb_hdr *smb_buffer;
4898 struct smb_hdr *smb_buffer_response;
4899 TCONX_REQ *pSMB;
4900 TCONX_RSP *pSMBr;
4901 unsigned char *bcc_ptr;
4902 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05004903 int length;
4904 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004905
4906 if (ses == NULL)
4907 return -EIO;
4908
4909 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00004910 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00004912
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 smb_buffer_response = smb_buffer;
4914
4915 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
4916 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07004917
Pavel Shilovsky88257362012-05-23 14:01:59 +04004918 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 smb_buffer->Uid = ses->Suid;
4920 pSMB = (TCONX_REQ *) smb_buffer;
4921 pSMBr = (TCONX_RSP *) smb_buffer_response;
4922
4923 pSMB->AndXCommand = 0xFF;
4924 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01004926 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08004927 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00004928 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08004929 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00004930 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08004931 } else {
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004932 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08004933 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
4934 specified as required (when that support is added to
4935 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00004936 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08004937 by Samba (not sure whether other servers allow
4938 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00004939#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton04912d62010-04-24 07:57:45 -04004940 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
Jeff Layton3f618222013-06-12 19:52:14 -05004941 (ses->sectype == LANMAN))
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -05004942 calc_lanman_hash(tcon->password, ses->server->cryptkey,
Steve French96daf2b2011-05-27 04:34:02 +00004943 ses->server->sec_mode &
Jeff Layton4e53a3f2008-12-05 20:41:21 -05004944 SECMODE_PW_ENCRYPT ? true : false,
4945 bcc_ptr);
Steve French7c7b25b2006-06-01 19:20:10 +00004946 else
4947#endif /* CIFS_WEAK_PW_HASH */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -06004948 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -05004949 bcc_ptr, nls_codepage);
Steve Frenchf3a31a22015-03-26 19:23:20 -05004950 if (rc) {
4951 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
4952 __func__, rc);
4953 cifs_buf_release(smb_buffer);
4954 return rc;
4955 }
Steve Frencheeac8042006-01-13 21:34:58 -08004956
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004957 bcc_ptr += CIFS_AUTH_RESP_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004958 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00004959 /* must align unicode strings */
4960 *bcc_ptr = 0; /* null byte password */
4961 bcc_ptr++;
4962 }
Steve Frencheeac8042006-01-13 21:34:58 -08004963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964
Jeff Layton38d77c52013-05-26 07:01:00 -04004965 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4967
4968 if (ses->capabilities & CAP_STATUS32) {
4969 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
4970 }
4971 if (ses->capabilities & CAP_DFS) {
4972 smb_buffer->Flags2 |= SMBFLG2_DFS;
4973 }
4974 if (ses->capabilities & CAP_UNICODE) {
4975 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
4976 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06004977 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00004978 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00004979 (/* server len*/ + 256 /* share len */), nls_codepage);
4980 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 bcc_ptr += 2; /* skip trailing null */
4982 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983 strcpy(bcc_ptr, tree);
4984 bcc_ptr += strlen(tree) + 1;
4985 }
4986 strcpy(bcc_ptr, "?????");
4987 bcc_ptr += strlen("?????");
4988 bcc_ptr += 1;
4989 count = bcc_ptr - &pSMB->Password[0];
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004990 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
4991 pSMB->hdr.smb_buf_length) + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 pSMB->ByteCount = cpu_to_le16(count);
4993
Steve French133672e2007-11-13 22:41:37 +00004994 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05004995 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01004998 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00004999 bool is_unicode;
5000
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00005002 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005003 tcon->tid = smb_buffer_response->Tid;
5004 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05005005 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04005006 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00005007 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
5008 is_unicode = true;
5009 else
5010 is_unicode = false;
5011
Jeff Laytoncc20c032009-04-30 07:16:21 -04005012
Steve French50c2f752007-07-13 00:33:32 +00005013 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00005014 if (length == 3) {
5015 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
5016 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05005017 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01005018 tcon->ipc = true;
5019 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00005020 }
5021 } else if (length == 2) {
5022 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
5023 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05005024 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00005025 }
5026 }
Steve French50c2f752007-07-13 00:33:32 +00005027 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04005028 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05005029 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04005030
5031 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04005032 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06005033 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00005034 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04005035 nls_codepage);
5036
Joe Perchesf96637b2013-05-04 22:12:25 -05005037 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04005038
Steve Frenchfb8c4b12007-07-10 01:16:18 +00005039 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00005040 (smb_buffer_response->WordCount == 7))
5041 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00005042 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
5043 else
5044 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05005045 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046 }
5047
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00005048 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 return rc;
5050}
5051
Al Viro2e32cf52013-10-03 12:53:37 -04005052static void delayed_free(struct rcu_head *p)
5053{
5054 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
5055 unload_nls(sbi->local_nls);
5056 kfree(sbi);
5057}
5058
Al Viro2a9b9952011-06-17 09:27:16 -04005059void
5060cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061{
Jeff Laytonb647c352010-10-28 11:16:44 -04005062 struct rb_root *root = &cifs_sb->tlink_tree;
5063 struct rb_node *node;
5064 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
Jeff Layton2de970f2010-10-06 19:51:12 -04005066 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
5067
Jeff Laytonb647c352010-10-28 11:16:44 -04005068 spin_lock(&cifs_sb->tlink_tree_lock);
5069 while ((node = rb_first(root))) {
5070 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
5071 cifs_get_tlink(tlink);
5072 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
5073 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00005074
Jeff Laytonb647c352010-10-28 11:16:44 -04005075 spin_unlock(&cifs_sb->tlink_tree_lock);
5076 cifs_put_tlink(tlink);
5077 spin_lock(&cifs_sb->tlink_tree_lock);
5078 }
5079 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005080
Al Virod757d712011-06-17 09:42:43 -04005081 kfree(cifs_sb->mountdata);
Aurelien Aptela6b50582016-05-25 19:59:09 +02005082 kfree(cifs_sb->prepath);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02005083#ifdef CONFIG_CIFS_DFS_UPCALL
Paulo Alcantarae511d312018-11-14 17:16:44 -02005084 dfs_cache_del_vol(cifs_sb->origin_fullpath);
Paulo Alcantara93d5cb52018-11-14 17:13:25 -02005085 kfree(cifs_sb->origin_fullpath);
5086#endif
Al Viro2e32cf52013-10-03 12:53:37 -04005087 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00005088}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089
Pavel Shilovsky286170a2012-05-25 10:43:58 +04005090int
5091cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092{
5093 int rc = 0;
Jeff Layton198b5682010-04-24 07:57:48 -04005094 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
Pavel Shilovsky286170a2012-05-25 10:43:58 +04005096 if (!server->ops->need_neg || !server->ops->negotiate)
5097 return -ENOSYS;
5098
Jeff Layton198b5682010-04-24 07:57:48 -04005099 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04005100 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04005101 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102
Pavel Shilovsky286170a2012-05-25 10:43:58 +04005103 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04005104 if (rc == 0) {
5105 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04005106 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04005107 server->tcpStatus = CifsGood;
5108 else
5109 rc = -EHOSTDOWN;
5110 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111 }
Steve French26b994f2008-08-06 05:11:33 +00005112
Jeff Layton198b5682010-04-24 07:57:48 -04005113 return rc;
5114}
Steve French26b994f2008-08-06 05:11:33 +00005115
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04005116int
5117cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
5118 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04005119{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04005120 int rc = -ENOSYS;
Jeff Layton198b5682010-04-24 07:57:48 -04005121 struct TCP_Server_Info *server = ses->server;
5122
Jeff Layton198b5682010-04-24 07:57:48 -04005123 ses->capabilities = server->capabilities;
Steve French26b994f2008-08-06 05:11:33 +00005124 if (linuxExtEnabled == 0)
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04005125 ses->capabilities &= (~server->vals->cap_unix);
Steve French20418ac2009-04-30 16:13:32 +00005126
Joe Perchesf96637b2013-05-04 22:12:25 -05005127 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00005128 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04005129
Shu Wangf5c4ba82017-09-08 18:48:33 +08005130 if (ses->auth_key.response) {
Steve French2a182872018-03-29 12:16:34 -05005131 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
Shu Wangf5c4ba82017-09-08 18:48:33 +08005132 ses->auth_key.response);
5133 kfree(ses->auth_key.response);
5134 ses->auth_key.response = NULL;
5135 ses->auth_key.len = 0;
5136 }
5137
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04005138 if (server->ops->sess_setup)
5139 rc = server->ops->sess_setup(xid, ses, nls_info);
5140
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05005141 if (rc)
Ronnie Sahlbergafe6f652019-08-28 17:15:35 +10005142 cifs_server_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05005143
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144 return rc;
5145}
5146
Jeff Layton8a8798a2012-01-17 16:09:15 -05005147static int
5148cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
5149{
Jeff Layton3f618222013-06-12 19:52:14 -05005150 vol->sectype = ses->sectype;
5151
5152 /* krb5 is special, since we don't need username or pw */
5153 if (vol->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05005154 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05005155
5156 return cifs_set_cifscreds(vol, ses);
5157}
5158
Steve French96daf2b2011-05-27 04:34:02 +00005159static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005160cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04005161{
Jeff Layton8a8798a2012-01-17 16:09:15 -05005162 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00005163 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
5164 struct cifs_ses *ses;
5165 struct cifs_tcon *tcon = NULL;
Jeff Layton9d002df2010-10-06 19:51:11 -04005166 struct smb_vol *vol_info;
Jeff Layton9d002df2010-10-06 19:51:11 -04005167
5168 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
Dan Carpenter803ab972012-01-24 11:39:22 +03005169 if (vol_info == NULL)
5170 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04005171
Jeff Layton9d002df2010-10-06 19:51:11 -04005172 vol_info->local_nls = cifs_sb->local_nls;
5173 vol_info->linux_uid = fsuid;
5174 vol_info->cred_uid = fsuid;
5175 vol_info->UNC = master_tcon->treeName;
5176 vol_info->retry = master_tcon->retry;
5177 vol_info->nocase = master_tcon->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05005178 vol_info->nohandlecache = master_tcon->nohandlecache;
Jeff Layton9d002df2010-10-06 19:51:11 -04005179 vol_info->local_lease = master_tcon->local_lease;
5180 vol_info->no_linux_ext = !master_tcon->unix_ext;
Jeff Layton28e11bd2013-05-26 07:01:00 -04005181 vol_info->sectype = master_tcon->ses->sectype;
5182 vol_info->sign = master_tcon->ses->sign;
Jeff Layton9d002df2010-10-06 19:51:11 -04005183
Jeff Layton8a8798a2012-01-17 16:09:15 -05005184 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
5185 if (rc) {
5186 tcon = ERR_PTR(rc);
5187 goto out;
5188 }
Jeff Layton9d002df2010-10-06 19:51:11 -04005189
5190 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05305191 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005192 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05305193 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005194
5195 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
5196 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00005197 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07005198 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04005199 goto out;
5200 }
5201
5202 tcon = cifs_get_tcon(ses, vol_info);
5203 if (IS_ERR(tcon)) {
5204 cifs_put_smb_ses(ses);
5205 goto out;
5206 }
5207
Steve Frenchce558b02018-05-31 19:16:54 -05005208 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
5209 if (tcon->posix_extensions)
5210 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
Steve French0fdfef92018-06-28 19:30:23 -05005211
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04005212 if (cap_unix(ses))
Jeff Layton9d002df2010-10-06 19:51:11 -04005213 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
Steve Frenchb3266142018-05-20 23:41:10 -05005214
Jeff Layton9d002df2010-10-06 19:51:11 -04005215out:
Jeff Layton8a8798a2012-01-17 16:09:15 -05005216 kfree(vol_info->username);
Aurelien Aptel97f4b722018-01-25 15:59:39 +01005217 kzfree(vol_info->password);
Jeff Layton9d002df2010-10-06 19:51:11 -04005218 kfree(vol_info);
5219
5220 return tcon;
5221}
5222
Steve French96daf2b2011-05-27 04:34:02 +00005223struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04005224cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
5225{
5226 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
5227}
5228
Jeff Laytonb647c352010-10-28 11:16:44 -04005229/* find and return a tlink with given uid */
5230static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005231tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04005232{
5233 struct rb_node *node = root->rb_node;
5234 struct tcon_link *tlink;
5235
5236 while (node) {
5237 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
5238
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005239 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04005240 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005241 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04005242 node = node->rb_right;
5243 else
5244 return tlink;
5245 }
5246 return NULL;
5247}
5248
5249/* insert a tcon_link into the tree */
5250static void
5251tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
5252{
5253 struct rb_node **new = &(root->rb_node), *parent = NULL;
5254 struct tcon_link *tlink;
5255
5256 while (*new) {
5257 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
5258 parent = *new;
5259
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005260 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04005261 new = &((*new)->rb_left);
5262 else
5263 new = &((*new)->rb_right);
5264 }
5265
5266 rb_link_node(&new_tlink->tl_rbnode, parent, new);
5267 rb_insert_color(&new_tlink->tl_rbnode, root);
5268}
5269
Jeff Layton9d002df2010-10-06 19:51:11 -04005270/*
5271 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
5272 * current task.
5273 *
5274 * If the superblock doesn't refer to a multiuser mount, then just return
5275 * the master tcon for the mount.
5276 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05305277 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04005278 * exists, then check to see if it's pending construction. If it is then wait
5279 * for construction to complete. Once it's no longer pending, check to see if
5280 * it failed and either return an error or retry construction, depending on
5281 * the timeout.
5282 *
5283 * If one doesn't exist then insert a new tcon_link struct into the tree and
5284 * try to construct a new one.
5285 */
5286struct tcon_link *
5287cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
5288{
5289 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08005290 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04005291 struct tcon_link *tlink, *newtlink;
5292
5293 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
5294 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
5295
5296 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04005297 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04005298 if (tlink)
5299 cifs_get_tlink(tlink);
5300 spin_unlock(&cifs_sb->tlink_tree_lock);
5301
5302 if (tlink == NULL) {
5303 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
5304 if (newtlink == NULL)
5305 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04005306 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04005307 newtlink->tl_tcon = ERR_PTR(-EACCES);
5308 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
5309 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
5310 cifs_get_tlink(newtlink);
5311
Jeff Layton9d002df2010-10-06 19:51:11 -04005312 spin_lock(&cifs_sb->tlink_tree_lock);
5313 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04005314 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04005315 if (tlink) {
5316 cifs_get_tlink(tlink);
5317 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005318 kfree(newtlink);
5319 goto wait_for_construction;
5320 }
Jeff Layton9d002df2010-10-06 19:51:11 -04005321 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04005322 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
5323 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04005324 } else {
5325wait_for_construction:
5326 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04005327 TASK_INTERRUPTIBLE);
5328 if (ret) {
5329 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10005330 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04005331 }
5332
5333 /* if it's good, return it */
5334 if (!IS_ERR(tlink->tl_tcon))
5335 return tlink;
5336
5337 /* return error if we tried this already recently */
5338 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
5339 cifs_put_tlink(tlink);
5340 return ERR_PTR(-EACCES);
5341 }
5342
5343 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
5344 goto wait_for_construction;
5345 }
5346
5347 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
5348 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
5349 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
5350
5351 if (IS_ERR(tlink->tl_tcon)) {
5352 cifs_put_tlink(tlink);
5353 return ERR_PTR(-EACCES);
5354 }
5355
5356 return tlink;
5357}
Jeff Layton2de970f2010-10-06 19:51:12 -04005358
5359/*
5360 * periodic workqueue job that scans tcon_tree for a superblock and closes
5361 * out tcons.
5362 */
5363static void
5364cifs_prune_tlinks(struct work_struct *work)
5365{
5366 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
5367 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04005368 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00005369 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04005370 struct rb_node *tmp;
5371 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04005372
Jeff Laytonb647c352010-10-28 11:16:44 -04005373 /*
5374 * Because we drop the spinlock in the loop in order to put the tlink
5375 * it's not guarded against removal of links from the tree. The only
5376 * places that remove entries from the tree are this function and
5377 * umounts. Because this function is non-reentrant and is canceled
5378 * before umount can proceed, this is safe.
5379 */
5380 spin_lock(&cifs_sb->tlink_tree_lock);
5381 node = rb_first(root);
5382 while (node != NULL) {
5383 tmp = node;
5384 node = rb_next(tmp);
5385 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
5386
5387 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
5388 atomic_read(&tlink->tl_count) != 0 ||
5389 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
5390 continue;
5391
5392 cifs_get_tlink(tlink);
5393 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
5394 rb_erase(tmp, root);
5395
Jeff Layton2de970f2010-10-06 19:51:12 -04005396 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04005397 cifs_put_tlink(tlink);
5398 spin_lock(&cifs_sb->tlink_tree_lock);
5399 }
5400 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04005401
Jeff Laytonda472fc2012-03-23 14:40:53 -04005402 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04005403 TLINK_IDLE_EXPIRE);
5404}