blob: c53a2e86ed544b968deef2920060b7c5af4d2702 [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"
Paulo Alcantarac0be6242018-11-20 15:16:36 -020053#include "dns_resolve.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "ntlmssp.h"
55#include "nterr.h"
56#include "rfc1002pdu.h"
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +053057#include "fscache.h"
Pavel Shilovsky53e0e112016-11-04 11:50:31 -070058#include "smb2proto.h"
Long Li2f894642017-11-22 17:38:34 -070059#include "smbdirect.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061extern mempool_t *cifs_req_poolp;
Steve Frenchf92a7202018-05-24 04:11:07 -050062extern bool disable_legacy_dialects;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Jeff Layton2de970f2010-10-06 19:51:12 -040064/* FIXME: should these be tunable? */
Jeff Layton9d002df2010-10-06 19:51:11 -040065#define TLINK_ERROR_EXPIRE (1 * HZ)
Jeff Layton2de970f2010-10-06 19:51:12 -040066#define TLINK_IDLE_EXPIRE (600 * HZ)
Jeff Layton9d002df2010-10-06 19:51:11 -040067
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040068enum {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040069 /* Mount options that take no arguments */
70 Opt_user_xattr, Opt_nouser_xattr,
71 Opt_forceuid, Opt_noforceuid,
Jeff Layton72bd4812012-10-03 16:02:36 -040072 Opt_forcegid, Opt_noforcegid,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040073 Opt_noblocksend, Opt_noautotune,
74 Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
Steve French2baa2682014-09-27 02:19:01 -050075 Opt_mapposix, Opt_nomapposix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040076 Opt_mapchars, Opt_nomapchars, Opt_sfu,
77 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
Steve Frenchb3266142018-05-20 23:41:10 -050078 Opt_noposixpaths, Opt_nounix, Opt_unix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040079 Opt_nocase,
80 Opt_brl, Opt_nobrl,
Steve French3d4ef9a2018-04-25 22:19:09 -050081 Opt_handlecache, Opt_nohandlecache,
Steve French95932652016-09-23 01:36:34 -050082 Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040083 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
84 Opt_nohard, Opt_nosoft,
85 Opt_nointr, Opt_intr,
86 Opt_nostrictsync, Opt_strictsync,
87 Opt_serverino, Opt_noserverino,
88 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
89 Opt_acl, Opt_noacl, Opt_locallease,
Jeff Layton1b359202012-09-19 15:20:27 -070090 Opt_sign, Opt_seal, Opt_noac,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040091 Opt_fsc, Opt_mfsymlinks,
Jeff Laytona0b3df52013-05-24 07:40:59 -040092 Opt_multiuser, Opt_sloppy, Opt_nosharesock,
Steve Frenchb2a30772015-09-29 21:49:28 -050093 Opt_persistent, Opt_nopersistent,
Steve French592fafe2015-11-03 10:08:53 -060094 Opt_resilient, Opt_noresilient,
Long Li8339dd32017-11-07 01:54:55 -070095 Opt_domainauto, Opt_rdma,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040096
97 /* Mount options which take numeric value */
98 Opt_backupuid, Opt_backupgid, Opt_uid,
99 Opt_cruid, Opt_gid, Opt_file_mode,
100 Opt_dirmode, Opt_port,
101 Opt_rsize, Opt_wsize, Opt_actimeo,
Steve French141891f2016-09-23 00:44:16 -0500102 Opt_echo_interval, Opt_max_credits,
Steve French8b217fe2016-11-11 22:36:20 -0600103 Opt_snapshot,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400104
105 /* Mount options which take string value */
106 Opt_user, Opt_pass, Opt_ip,
Jeff Layton73a999f2013-03-22 08:42:57 -0400107 Opt_domain, Opt_srcaddr, Opt_iocharset,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400108 Opt_netbiosname, Opt_servern,
Jeff Layton23db65f2012-05-15 12:20:51 -0400109 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400110
111 /* Mount options to be ignored */
112 Opt_ignore,
113
114 /* Options which could be blank */
115 Opt_blank_pass,
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100116 Opt_blank_user,
117 Opt_blank_ip,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400118
119 Opt_err
120};
121
122static const match_table_t cifs_mount_option_tokens = {
123
124 { Opt_user_xattr, "user_xattr" },
125 { Opt_nouser_xattr, "nouser_xattr" },
126 { Opt_forceuid, "forceuid" },
127 { Opt_noforceuid, "noforceuid" },
Jeff Layton72bd4812012-10-03 16:02:36 -0400128 { Opt_forcegid, "forcegid" },
129 { Opt_noforcegid, "noforcegid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400130 { Opt_noblocksend, "noblocksend" },
131 { Opt_noautotune, "noautotune" },
132 { Opt_hard, "hard" },
133 { Opt_soft, "soft" },
134 { Opt_perm, "perm" },
135 { Opt_noperm, "noperm" },
Steve French2baa2682014-09-27 02:19:01 -0500136 { Opt_mapchars, "mapchars" }, /* SFU style */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400137 { Opt_nomapchars, "nomapchars" },
Steve French2baa2682014-09-27 02:19:01 -0500138 { Opt_mapposix, "mapposix" }, /* SFM style */
139 { Opt_nomapposix, "nomapposix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400140 { Opt_sfu, "sfu" },
141 { Opt_nosfu, "nosfu" },
142 { Opt_nodfs, "nodfs" },
143 { Opt_posixpaths, "posixpaths" },
144 { Opt_noposixpaths, "noposixpaths" },
145 { Opt_nounix, "nounix" },
146 { Opt_nounix, "nolinux" },
Steve Frenchb3266142018-05-20 23:41:10 -0500147 { Opt_nounix, "noposix" },
148 { Opt_unix, "unix" },
149 { Opt_unix, "linux" },
150 { Opt_unix, "posix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400151 { Opt_nocase, "nocase" },
152 { Opt_nocase, "ignorecase" },
153 { Opt_brl, "brl" },
154 { Opt_nobrl, "nobrl" },
Steve French3d4ef9a2018-04-25 22:19:09 -0500155 { Opt_handlecache, "handlecache" },
156 { Opt_nohandlecache, "nohandlecache" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400157 { Opt_nobrl, "nolock" },
158 { Opt_forcemandatorylock, "forcemandatorylock" },
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +0400159 { Opt_forcemandatorylock, "forcemand" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400160 { Opt_setuids, "setuids" },
161 { Opt_nosetuids, "nosetuids" },
Steve French95932652016-09-23 01:36:34 -0500162 { Opt_setuidfromacl, "idsfromsid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400163 { Opt_dynperm, "dynperm" },
164 { Opt_nodynperm, "nodynperm" },
165 { Opt_nohard, "nohard" },
166 { Opt_nosoft, "nosoft" },
167 { Opt_nointr, "nointr" },
168 { Opt_intr, "intr" },
169 { Opt_nostrictsync, "nostrictsync" },
170 { Opt_strictsync, "strictsync" },
171 { Opt_serverino, "serverino" },
172 { Opt_noserverino, "noserverino" },
173 { Opt_rwpidforward, "rwpidforward" },
174 { Opt_cifsacl, "cifsacl" },
175 { Opt_nocifsacl, "nocifsacl" },
176 { Opt_acl, "acl" },
177 { Opt_noacl, "noacl" },
178 { Opt_locallease, "locallease" },
179 { Opt_sign, "sign" },
180 { Opt_seal, "seal" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400181 { Opt_noac, "noac" },
182 { Opt_fsc, "fsc" },
183 { Opt_mfsymlinks, "mfsymlinks" },
184 { Opt_multiuser, "multiuser" },
Jeff Laytond8162552012-03-23 14:40:56 -0400185 { Opt_sloppy, "sloppy" },
Jeff Laytona0b3df52013-05-24 07:40:59 -0400186 { Opt_nosharesock, "nosharesock" },
Steve Frenchb2a30772015-09-29 21:49:28 -0500187 { Opt_persistent, "persistenthandles"},
188 { Opt_nopersistent, "nopersistenthandles"},
Steve French592fafe2015-11-03 10:08:53 -0600189 { Opt_resilient, "resilienthandles"},
190 { Opt_noresilient, "noresilienthandles"},
Germano Percossi39566442016-12-15 12:31:18 +0530191 { Opt_domainauto, "domainauto"},
Long Li8339dd32017-11-07 01:54:55 -0700192 { Opt_rdma, "rdma"},
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400193
194 { Opt_backupuid, "backupuid=%s" },
195 { Opt_backupgid, "backupgid=%s" },
196 { Opt_uid, "uid=%s" },
197 { Opt_cruid, "cruid=%s" },
198 { Opt_gid, "gid=%s" },
199 { Opt_file_mode, "file_mode=%s" },
200 { Opt_dirmode, "dirmode=%s" },
201 { Opt_dirmode, "dir_mode=%s" },
202 { Opt_port, "port=%s" },
203 { Opt_rsize, "rsize=%s" },
204 { Opt_wsize, "wsize=%s" },
205 { Opt_actimeo, "actimeo=%s" },
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600206 { Opt_echo_interval, "echo_interval=%s" },
Steve French141891f2016-09-23 00:44:16 -0500207 { Opt_max_credits, "max_credits=%s" },
Steve French8b217fe2016-11-11 22:36:20 -0600208 { Opt_snapshot, "snapshot=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400209
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100210 { Opt_blank_user, "user=" },
211 { Opt_blank_user, "username=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400212 { Opt_user, "user=%s" },
213 { Opt_user, "username=%s" },
214 { Opt_blank_pass, "pass=" },
Jesper Nilsson3c15b4c2012-11-29 17:31:16 +0100215 { Opt_blank_pass, "password=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400216 { Opt_pass, "pass=%s" },
217 { Opt_pass, "password=%s" },
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100218 { Opt_blank_ip, "ip=" },
219 { Opt_blank_ip, "addr=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400220 { Opt_ip, "ip=%s" },
221 { Opt_ip, "addr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400222 { Opt_ignore, "unc=%s" },
223 { Opt_ignore, "target=%s" },
224 { Opt_ignore, "path=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400225 { Opt_domain, "dom=%s" },
226 { Opt_domain, "domain=%s" },
227 { Opt_domain, "workgroup=%s" },
228 { Opt_srcaddr, "srcaddr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400229 { Opt_ignore, "prefixpath=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400230 { Opt_iocharset, "iocharset=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400231 { Opt_netbiosname, "netbiosname=%s" },
232 { Opt_servern, "servern=%s" },
233 { Opt_ver, "ver=%s" },
Jeff Layton23db65f2012-05-15 12:20:51 -0400234 { Opt_vers, "vers=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400235 { Opt_sec, "sec=%s" },
Jeff Layton15b6a472012-05-16 07:50:15 -0400236 { Opt_cache, "cache=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400237
238 { Opt_ignore, "cred" },
239 { Opt_ignore, "credentials" },
Jeff Laytona557b972012-05-02 14:02:40 -0400240 { Opt_ignore, "cred=%s" },
241 { Opt_ignore, "credentials=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400242 { Opt_ignore, "guest" },
243 { Opt_ignore, "rw" },
244 { Opt_ignore, "ro" },
245 { Opt_ignore, "suid" },
246 { Opt_ignore, "nosuid" },
247 { Opt_ignore, "exec" },
248 { Opt_ignore, "noexec" },
249 { Opt_ignore, "nodev" },
250 { Opt_ignore, "noauto" },
251 { Opt_ignore, "dev" },
252 { Opt_ignore, "mand" },
253 { Opt_ignore, "nomand" },
254 { Opt_ignore, "_netdev" },
255
256 { Opt_err, NULL }
257};
258
259enum {
260 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
261 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
Jeff Layton76596242012-07-23 20:34:17 -0400262 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
263 Opt_sec_ntlmv2i, Opt_sec_lanman,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400264 Opt_sec_none,
265
266 Opt_sec_err
267};
268
269static const match_table_t cifs_secflavor_tokens = {
270 { Opt_sec_krb5, "krb5" },
271 { Opt_sec_krb5i, "krb5i" },
272 { Opt_sec_krb5p, "krb5p" },
273 { Opt_sec_ntlmsspi, "ntlmsspi" },
274 { Opt_sec_ntlmssp, "ntlmssp" },
275 { Opt_ntlm, "ntlm" },
276 { Opt_sec_ntlmi, "ntlmi" },
Jeff Layton76596242012-07-23 20:34:17 -0400277 { Opt_sec_ntlmv2, "nontlm" },
278 { Opt_sec_ntlmv2, "ntlmv2" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400279 { Opt_sec_ntlmv2i, "ntlmv2i" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400280 { Opt_sec_lanman, "lanman" },
281 { Opt_sec_none, "none" },
282
283 { Opt_sec_err, NULL }
284};
285
Jeff Layton15b6a472012-05-16 07:50:15 -0400286/* cache flavors */
287enum {
288 Opt_cache_loose,
289 Opt_cache_strict,
290 Opt_cache_none,
291 Opt_cache_err
292};
293
294static const match_table_t cifs_cacheflavor_tokens = {
295 { Opt_cache_loose, "loose" },
296 { Opt_cache_strict, "strict" },
297 { Opt_cache_none, "none" },
298 { Opt_cache_err, NULL }
299};
300
Jeff Layton23db65f2012-05-15 12:20:51 -0400301static const match_table_t cifs_smb_version_tokens = {
302 { Smb_1, SMB1_VERSION_STRING },
Steve Frenchdd446b12012-11-28 23:21:06 -0600303 { Smb_20, SMB20_VERSION_STRING},
Steve French1080ef72011-02-24 18:07:19 +0000304 { Smb_21, SMB21_VERSION_STRING },
Steve Frenche4aa25e2012-10-01 12:26:22 -0500305 { Smb_30, SMB30_VERSION_STRING },
Steve French20b6d8b2013-06-12 22:48:41 -0500306 { Smb_302, SMB302_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600307 { Smb_311, SMB311_VERSION_STRING },
Steve Frenchaab18932015-06-23 23:37:11 -0500308 { Smb_311, ALT_SMB311_VERSION_STRING },
Steve French9764c022017-09-17 10:41:35 -0500309 { Smb_3any, SMB3ANY_VERSION_STRING },
310 { Smb_default, SMBDEFAULT_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600311 { Smb_version_err, NULL }
Jeff Layton23db65f2012-05-15 12:20:51 -0400312};
313
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300314static int ip_connect(struct TCP_Server_Info *server);
315static int generic_ip_connect(struct TCP_Server_Info *server);
Jeff Laytonb647c352010-10-28 11:16:44 -0400316static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
Jeff Layton2de970f2010-10-06 19:51:12 -0400317static void cifs_prune_tlinks(struct work_struct *work);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -0400318static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
Steve Frenchc7c137b2018-06-06 17:59:29 -0500319 const char *devname, bool is_smb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Jeff Laytond5c56052008-12-01 18:42:33 -0500321/*
Paulo Alcantarac0be6242018-11-20 15:16:36 -0200322 * Resolve hostname and set ip addr in tcp ses. Useful for hostnames that may
323 * get their ip addresses changed at some point.
324 *
325 * This should be called with server->srv_mutex held.
326 */
327#ifdef CONFIG_CIFS_DFS_UPCALL
328static int reconn_set_ipaddr(struct TCP_Server_Info *server)
329{
330 int rc;
331 int len;
332 char *unc, *ipaddr = NULL;
333
334 if (!server->hostname)
335 return -EINVAL;
336
337 len = strlen(server->hostname) + 3;
338
339 unc = kmalloc(len, GFP_KERNEL);
340 if (!unc) {
341 cifs_dbg(FYI, "%s: failed to create UNC path\n", __func__);
342 return -ENOMEM;
343 }
344 snprintf(unc, len, "\\\\%s", server->hostname);
345
346 rc = dns_resolve_server_name_to_ip(unc, &ipaddr);
347 kfree(unc);
348
349 if (rc < 0) {
350 cifs_dbg(FYI, "%s: failed to resolve server part of %s to IP: %d\n",
351 __func__, server->hostname, rc);
352 return rc;
353 }
354
355 rc = cifs_convert_address((struct sockaddr *)&server->dstaddr, ipaddr,
356 strlen(ipaddr));
357 kfree(ipaddr);
358
359 return !rc ? -1 : 0;
360}
361#else
362static inline int reconn_set_ipaddr(struct TCP_Server_Info *server)
363{
364 return 0;
365}
366#endif
367
368/*
Jeff Laytond5c56052008-12-01 18:42:33 -0500369 * cifs tcp session reconnection
370 *
371 * mark tcp session as reconnecting so temporarily locked
372 * mark all smb sessions as reconnecting for tcp session
373 * reconnect tcp session
374 * wake up waiters on reconnection? - (not needed currently)
375 */
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400376int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377cifs_reconnect(struct TCP_Server_Info *server)
378{
379 int rc = 0;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500380 struct list_head *tmp, *tmp2;
Steve French96daf2b2011-05-27 04:34:02 +0000381 struct cifs_ses *ses;
382 struct cifs_tcon *tcon;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000383 struct mid_q_entry *mid_entry;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400384 struct list_head retry_list;
Steve French50c2f752007-07-13 00:33:32 +0000385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000387 if (server->tcpStatus == CifsExiting) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000388 /* the demux thread will exit normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 next time through the loop */
390 spin_unlock(&GlobalMid_Lock);
391 return rc;
392 } else
393 server->tcpStatus = CifsNeedReconnect;
394 spin_unlock(&GlobalMid_Lock);
395 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400396 server->max_read = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Joe Perchesf96637b2013-05-04 22:12:25 -0500398 cifs_dbg(FYI, "Reconnecting tcp session\n");
Steve Frenchbf1fdeb2018-07-30 19:23:09 -0500399 trace_smb3_reconnect(server->CurrentMid, server->hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /* before reconnecting the tcp session, mark the smb session (uid)
402 and the tid bad so they are not used until reconnected */
Joe Perchesf96637b2013-05-04 22:12:25 -0500403 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
404 __func__);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530405 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -0500406 list_for_each(tmp, &server->smb_ses_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000407 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
Jeff Layton14fbf502008-11-14 13:53:46 -0500408 ses->need_reconnect = true;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500409 list_for_each(tmp2, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000410 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500411 tcon->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
Aurelien Aptelb327a712018-01-24 13:46:10 +0100413 if (ses->tcon_ipc)
414 ses->tcon_ipc->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530416 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /* do not want to be sending data on a socket we are freeing */
Joe Perchesf96637b2013-05-04 22:12:25 -0500419 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500420 mutex_lock(&server->srv_mutex);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000421 if (server->ssocket) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500422 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
423 server->ssocket->state, server->ssocket->flags);
Trond Myklebust91cf45f2007-11-12 18:10:39 -0800424 kernel_sock_shutdown(server->ssocket, SHUT_WR);
Joe Perchesf96637b2013-05-04 22:12:25 -0500425 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
426 server->ssocket->state, server->ssocket->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 sock_release(server->ssocket);
428 server->ssocket = NULL;
429 }
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -0500430 server->sequence_number = 0;
431 server->session_estab = false;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500432 kfree(server->session_key.response);
433 server->session_key.response = NULL;
434 server->session_key.len = 0;
Steve Frenchfda35942011-01-20 18:06:34 +0000435 server->lstrp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500437 /* mark submitted MIDs for retry and issue callback */
Jeff Layton3c1105d2011-05-22 07:09:13 -0400438 INIT_LIST_HEAD(&retry_list);
Joe Perchesf96637b2013-05-04 22:12:25 -0500439 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 spin_lock(&GlobalMid_Lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500441 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
442 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400443 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
444 mid_entry->mid_state = MID_RETRY_NEEDED;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400445 list_move(&mid_entry->qhead, &retry_list);
446 }
447 spin_unlock(&GlobalMid_Lock);
Rabin Vincent820962d2015-12-23 07:32:41 +0100448 mutex_unlock(&server->srv_mutex);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400449
Joe Perchesf96637b2013-05-04 22:12:25 -0500450 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400451 list_for_each_safe(tmp, tmp2, &retry_list) {
452 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500453 list_del_init(&mid_entry->qhead);
454 mid_entry->callback(mid_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400457 do {
Steve French6c3d8902006-07-31 22:46:20 +0000458 try_to_freeze();
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300459
460 /* we should try only the port we connected to before */
Jeff Layton73e216a2013-09-05 08:38:10 -0400461 mutex_lock(&server->srv_mutex);
Long Li781a8052017-11-22 17:38:36 -0700462 if (cifs_rdma_enabled(server))
463 rc = smbd_reconnect(server);
464 else
465 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000466 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500467 cifs_dbg(FYI, "reconnect error %d\n", rc);
Paulo Alcantarac0be6242018-11-20 15:16:36 -0200468 rc = reconn_set_ipaddr(server);
469 if (rc) {
470 cifs_dbg(FYI, "%s: failed to resolve hostname: %d\n",
471 __func__, rc);
472 }
Federico Sauter4afe2602015-03-17 17:45:28 +0100473 mutex_unlock(&server->srv_mutex);
Steve French0cb766a2005-04-28 22:41:11 -0700474 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 } else {
476 atomic_inc(&tcpSesReconnectCount);
477 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000478 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000479 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000480 spin_unlock(&GlobalMid_Lock);
Federico Sauter4afe2602015-03-17 17:45:28 +0100481 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400483 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500484
Sachin Prabhub8c60012016-10-20 19:52:24 -0400485 if (server->tcpStatus == CifsNeedNegotiate)
486 mod_delayed_work(cifsiod_wq, &server->echo, 0);
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return rc;
489}
490
Jeff Laytonc74093b2011-01-11 07:24:23 -0500491static void
492cifs_echo_request(struct work_struct *work)
493{
494 int rc;
495 struct TCP_Server_Info *server = container_of(work,
496 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400497 unsigned long echo_interval;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500498
Jeff Layton247ec9b2011-02-04 17:09:50 -0500499 /*
Sachin Prabhub8c60012016-10-20 19:52:24 -0400500 * If we need to renegotiate, set echo interval to zero to
501 * immediately call echo service where we can renegotiate.
502 */
503 if (server->tcpStatus == CifsNeedNegotiate)
504 echo_interval = 0;
505 else
506 echo_interval = server->echo_interval;
507
508 /*
509 * We cannot send an echo if it is disabled.
510 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500511 */
Steve French4fcd1812016-06-22 20:12:05 -0500512
513 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400514 server->tcpStatus == CifsExiting ||
515 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400516 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600517 time_before(jiffies, server->lstrp + echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500518 goto requeue_echo;
519
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400520 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500521 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500522 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
523 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500524
525requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400526 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500527}
528
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400529static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400530allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400531{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400532 if (!server->bigbuf) {
533 server->bigbuf = (char *)cifs_buf_get();
534 if (!server->bigbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500535 cifs_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400536 msleep(3000);
537 /* retry will check if exiting */
538 return false;
539 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400540 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400541 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400542 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400543 }
544
Jeff Layton2a37ef92011-10-19 15:29:23 -0400545 if (!server->smallbuf) {
546 server->smallbuf = (char *)cifs_small_buf_get();
547 if (!server->smallbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500548 cifs_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400549 msleep(1000);
550 /* retry will check if exiting */
551 return false;
552 }
553 /* beginning of smb buffer is cleared in our buf_get */
554 } else {
555 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400556 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400557 }
558
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400559 return true;
560}
561
Jeff Laytonba749e62011-10-11 06:41:32 -0400562static bool
563server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400564{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300565 /*
Ronnie Sahlbergd29fbf62019-07-06 06:52:46 +1000566 * We need to wait 3 echo intervals to make sure we handle such
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300567 * situations right:
568 * 1s client sends a normal SMB request
Ronnie Sahlbergd29fbf62019-07-06 06:52:46 +1000569 * 3s client gets a response
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300570 * 30s echo workqueue job pops, and decides we got a response recently
571 * and don't need to send another
572 * ...
573 * 65s kernel_recvmsg times out, and we see that we haven't gotten
574 * a response in >60s.
575 */
Samuel Cabrero76e75272017-07-11 12:44:39 +0200576 if ((server->tcpStatus == CifsGood ||
577 server->tcpStatus == CifsNeedNegotiate) &&
Ronnie Sahlbergd29fbf62019-07-06 06:52:46 +1000578 time_after(jiffies, server->lstrp + 3 * server->echo_interval)) {
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600579 cifs_dbg(VFS, "Server %s has not responded in %lu seconds. Reconnecting...\n",
Ronnie Sahlbergd29fbf62019-07-06 06:52:46 +1000580 server->hostname, (3 * server->echo_interval) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400581 cifs_reconnect(server);
582 wake_up(&server->response_q);
583 return true;
584 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400585
Jeff Laytonba749e62011-10-11 06:41:32 -0400586 return false;
587}
588
Pavel Shilovsky779c65b2019-01-18 17:25:36 -0800589static inline bool
590zero_credits(struct TCP_Server_Info *server)
591{
592 int val;
593
594 spin_lock(&server->req_lock);
595 val = server->credits + server->echo_credits + server->oplock_credits;
596 if (server->in_flight == 0 && val == 0) {
597 spin_unlock(&server->req_lock);
598 return true;
599 }
600 spin_unlock(&server->req_lock);
601 return false;
602}
603
Al Viro71335662016-01-09 19:54:50 -0500604static int
605cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400606{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400607 int length = 0;
608 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400609
Al Viro71335662016-01-09 19:54:50 -0500610 smb_msg->msg_control = NULL;
611 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400612
Al Viro71335662016-01-09 19:54:50 -0500613 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500614 try_to_freeze();
615
Pavel Shilovsky779c65b2019-01-18 17:25:36 -0800616 /* reconnect if no credits and no requests in flight */
617 if (zero_credits(server)) {
618 cifs_reconnect(server);
619 return -ECONNABORTED;
620 }
621
Al Viro71335662016-01-09 19:54:50 -0500622 if (server_unresponsive(server))
623 return -ECONNABORTED;
Long Li2fef1372017-11-22 17:38:41 -0700624 if (cifs_rdma_enabled(server) && server->smbd_conn)
625 length = smbd_recv(server->smbd_conn, smb_msg);
626 else
627 length = sock_recvmsg(server->ssocket, smb_msg, 0);
Al Viro71335662016-01-09 19:54:50 -0500628
629 if (server->tcpStatus == CifsExiting)
630 return -ESHUTDOWN;
631
632 if (server->tcpStatus == CifsNeedReconnect) {
633 cifs_reconnect(server);
634 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400635 }
636
Al Viro71335662016-01-09 19:54:50 -0500637 if (length == -ERESTARTSYS ||
638 length == -EAGAIN ||
639 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400640 /*
641 * Minimum sleep to prevent looping, allowing socket
642 * to clear and app threads to set tcpStatus
643 * CifsNeedReconnect if server hung.
644 */
645 usleep_range(1000, 2000);
646 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400647 continue;
Al Viro71335662016-01-09 19:54:50 -0500648 }
649
650 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500651 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400652 cifs_reconnect(server);
Al Viro71335662016-01-09 19:54:50 -0500653 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400654 }
655 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400656 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400657}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400658
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400659int
660cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
661 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400662{
Al Viro71335662016-01-09 19:54:50 -0500663 struct msghdr smb_msg;
664 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
665 iov_iter_kvec(&smb_msg.msg_iter, READ | ITER_KVEC, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400666
Al Viro71335662016-01-09 19:54:50 -0500667 return cifs_readv_from_socket(server, &smb_msg);
668}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400669
Al Viro71335662016-01-09 19:54:50 -0500670int
671cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
Long Li1dbe3462018-05-30 12:47:55 -0700672 unsigned int page_offset, unsigned int to_read)
Al Viro71335662016-01-09 19:54:50 -0500673{
674 struct msghdr smb_msg;
Long Li1dbe3462018-05-30 12:47:55 -0700675 struct bio_vec bv = {
676 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
Al Viro71335662016-01-09 19:54:50 -0500677 iov_iter_bvec(&smb_msg.msg_iter, READ | ITER_BVEC, &bv, 1, to_read);
678 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400679}
680
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400681static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400682is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400683{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400684 /*
685 * The first byte big endian of the length field,
686 * is actually not part of the length but the type
687 * with the most common, zero, as regular data.
688 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400689 switch (type) {
690 case RFC1002_SESSION_MESSAGE:
691 /* Regular SMB response */
692 return true;
693 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500694 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400695 break;
696 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500697 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400698 break;
699 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400700 /*
701 * We get this from Windows 98 instead of an error on
702 * SMB negprot response.
703 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500704 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400705 /* give server a second to clean up */
706 msleep(1000);
707 /*
708 * Always try 445 first on reconnect since we get NACK
709 * on some if we ever connected to port 139 (the NACK
710 * is since we do not begin with RFC1001 session
711 * initialize frame).
712 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400713 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400714 cifs_reconnect(server);
715 wake_up(&server->response_q);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400716 break;
717 default:
Joe Perchesf96637b2013-05-04 22:12:25 -0500718 cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400719 cifs_reconnect(server);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400720 }
721
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400722 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400723}
724
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400725void
726dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400727{
728#ifdef CONFIG_CIFS_STATS2
729 mid->when_received = jiffies;
730#endif
731 spin_lock(&GlobalMid_Lock);
732 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400733 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400734 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400735 mid->mid_state = MID_RESPONSE_MALFORMED;
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000736 /*
737 * Trying to handle/dequeue a mid after the send_recv()
738 * function has finished processing it is a bug.
739 */
740 if (mid->mid_flags & MID_DELETED)
741 printk_once(KERN_WARNING
742 "trying to dequeue a deleted mid\n");
743 else
744 list_del_init(&mid->qhead);
Jeff Laytonea1f4502011-10-19 15:29:05 -0400745 spin_unlock(&GlobalMid_Lock);
746}
747
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400748static void
749handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400750 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400751{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400752 if (server->ops->check_trans2 &&
753 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400754 return;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400755 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400756 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400757 /* Was previous buf put in mpx struct for multi-rsp? */
758 if (!mid->multiRsp) {
759 /* smb buffer will be freed by user thread */
760 if (server->large_buf)
761 server->bigbuf = NULL;
762 else
763 server->smallbuf = NULL;
764 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400765 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400766}
767
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400768static void clean_demultiplex_info(struct TCP_Server_Info *server)
769{
770 int length;
771
772 /* take it off the list, if it's not already */
773 spin_lock(&cifs_tcp_ses_lock);
774 list_del_init(&server->tcp_ses_list);
775 spin_unlock(&cifs_tcp_ses_lock);
776
777 spin_lock(&GlobalMid_Lock);
778 server->tcpStatus = CifsExiting;
779 spin_unlock(&GlobalMid_Lock);
780 wake_up_all(&server->response_q);
781
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400782 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300783 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400784 if (server->credits <= 0)
785 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300786 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400787 /*
788 * Although there should not be any requests blocked on this queue it
789 * can not hurt to be paranoid and try to wake up requests that may
790 * haven been blocked when more than 50 at time were on the wire to the
791 * same server - they now will see the session is in exit state and get
792 * out of SendReceive.
793 */
794 wake_up_all(&server->request_q);
795 /* give those requests time to exit */
796 msleep(125);
Long Libce9ce72017-11-22 17:38:38 -0700797 if (cifs_rdma_enabled(server) && server->smbd_conn) {
798 smbd_destroy(server->smbd_conn);
799 server->smbd_conn = NULL;
800 }
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400801 if (server->ssocket) {
802 sock_release(server->ssocket);
803 server->ssocket = NULL;
804 }
805
806 if (!list_empty(&server->pending_mid_q)) {
807 struct list_head dispose_list;
808 struct mid_q_entry *mid_entry;
809 struct list_head *tmp, *tmp2;
810
811 INIT_LIST_HEAD(&dispose_list);
812 spin_lock(&GlobalMid_Lock);
813 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
814 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500815 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400816 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400817 list_move(&mid_entry->qhead, &dispose_list);
818 }
819 spin_unlock(&GlobalMid_Lock);
820
821 /* now walk dispose list and issue callbacks */
822 list_for_each_safe(tmp, tmp2, &dispose_list) {
823 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500824 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400825 list_del_init(&mid_entry->qhead);
826 mid_entry->callback(mid_entry);
827 }
828 /* 1/8th of sec is more than enough time for them to exit */
829 msleep(125);
830 }
831
832 if (!list_empty(&server->pending_mid_q)) {
833 /*
834 * mpx threads have not exited yet give them at least the smb
835 * send timeout time for long ops.
836 *
837 * Due to delays on oplock break requests, we need to wait at
838 * least 45 seconds before giving up on a request getting a
839 * response and going ahead and killing cifsd.
840 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500841 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400842 msleep(46000);
843 /*
844 * If threads still have not exited they are probably never
845 * coming home not much else we can do but free the memory.
846 */
847 }
848
849 kfree(server->hostname);
850 kfree(server);
851
852 length = atomic_dec_return(&tcpSesAllocCount);
853 if (length > 0)
David Rientjes11d83362015-04-14 15:48:21 -0700854 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400855}
856
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400857static int
Jeff Laytone9097ab2011-10-19 15:29:40 -0400858standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
859{
860 int length;
861 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +1000862 unsigned int pdu_length = server->pdu_size;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400863
864 /* make sure this will fit in a large buffer */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100865 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
866 server->vals->header_preamble_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500867 cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400868 cifs_reconnect(server);
869 wake_up(&server->response_q);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +0400870 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400871 }
872
873 /* switch to large buffer if too big for a small one */
874 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
875 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400876 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400877 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400878 }
879
880 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400881 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100882 pdu_length - HEADER_SIZE(server) + 1
883 + server->vals->header_preamble_size);
884
Jeff Laytone9097ab2011-10-19 15:29:40 -0400885 if (length < 0)
886 return length;
887 server->total_read += length;
888
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400889 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400890
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800891 return cifs_handle_standard(server, mid);
892}
893
894int
895cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
896{
897 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
898 int length;
899
Jeff Laytone9097ab2011-10-19 15:29:40 -0400900 /*
901 * We know that we received enough to get to the MID as we
902 * checked the pdu_length earlier. Now check to see
903 * if the rest of the header is OK. We borrow the length
904 * var for the rest of the loop to avoid a new stack var.
905 *
906 * 48 bytes is enough to display the header and a little bit
907 * into the payload for debugging purposes.
908 */
Steve French373512e2015-12-18 13:05:30 -0600909 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400910 if (length != 0)
911 cifs_dump_mem("Bad SMB: ", buf,
912 min_t(unsigned int, server->total_read, 48));
913
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700914 if (server->ops->is_session_expired &&
915 server->ops->is_session_expired(buf)) {
916 cifs_reconnect(server);
917 wake_up(&server->response_q);
918 return -1;
919 }
920
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700921 if (server->ops->is_status_pending &&
922 server->ops->is_status_pending(buf, server, length))
923 return -1;
924
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500925 if (!mid)
926 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400927
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400928 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500929 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400930}
931
932static int
Al Viro7c97c202011-06-21 08:51:28 -0400933cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000935 int i, num_mids, length;
Al Viro7c97c202011-06-21 08:51:28 -0400936 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400937 unsigned int pdu_length;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000938 unsigned int next_offset;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400939 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500940 struct task_struct *task_to_wake = NULL;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000941 struct mid_q_entry *mids[MAX_COMPOUND];
942 char *bufs[MAX_COMPOUND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 current->flags |= PF_MEMALLOC;
Joe Perchesf96637b2013-05-04 22:12:25 -0500945 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -0400946
947 length = atomic_inc_return(&tcpSesAllocCount);
948 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -0700949 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700951 set_freezable();
Jeff Layton469ee612008-10-16 18:46:39 +0000952 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -0700953 if (try_to_freeze())
954 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700955
Jeff Layton2a37ef92011-10-19 15:29:23 -0400956 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400957 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700958
Jeff Layton2a37ef92011-10-19 15:29:23 -0400959 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400960 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000961 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +0000962
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400963 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400964 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +0000965 continue;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000966
967 if (server->vals->header_preamble_size == 0)
968 server->total_read = 0;
969 else
970 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -0700971
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400972 /*
973 * The right amount was read from socket - 4 bytes,
974 * so we can now interpret the length field.
975 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400976 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -0700977
Joe Perchesf96637b2013-05-04 22:12:25 -0500978 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400979 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000980 continue;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000981next_pdu:
982 server->pdu_size = pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -0700983
Jeff Layton89482a52011-10-19 15:28:57 -0400984 /* make sure we have enough to get to the MID */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000985 if (server->pdu_size < HEADER_SIZE(server) - 1 -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100986 server->vals->header_preamble_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500987 cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000988 server->pdu_size);
Jeff Layton89482a52011-10-19 15:28:57 -0400989 cifs_reconnect(server);
990 wake_up(&server->response_q);
991 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700992 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400993
Jeff Layton89482a52011-10-19 15:28:57 -0400994 /* read down to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100995 length = cifs_read_from_socket(server,
996 buf + server->vals->header_preamble_size,
997 HEADER_SIZE(server) - 1
998 - server->vals->header_preamble_size);
Jeff Layton89482a52011-10-19 15:28:57 -0400999 if (length < 0)
1000 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -04001001 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -04001002
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001003 if (server->ops->next_header) {
1004 next_offset = server->ops->next_header(buf);
1005 if (next_offset)
1006 server->pdu_size = next_offset;
1007 }
1008
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001009 memset(mids, 0, sizeof(mids));
1010 memset(bufs, 0, sizeof(bufs));
1011 num_mids = 0;
1012
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001013 if (server->ops->is_transform_hdr &&
1014 server->ops->receive_transform &&
1015 server->ops->is_transform_hdr(buf)) {
1016 length = server->ops->receive_transform(server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001017 mids,
1018 bufs,
1019 &num_mids);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001020 } else {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001021 mids[0] = server->ops->find_mid(server, buf);
1022 bufs[0] = buf;
Steve French7af929d2018-10-02 18:54:09 -05001023 num_mids = 1;
Jeff Laytonc8054eb2011-10-19 15:29:31 -04001024
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001025 if (!mids[0] || !mids[0]->receive)
1026 length = standard_receive3(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001027 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001028 length = mids[0]->receive(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -08001029 }
Jeff Layton44d22d82011-10-19 15:29:49 -04001030
Lars Persson696e4202018-06-25 14:05:25 +02001031 if (length < 0) {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001032 for (i = 0; i < num_mids; i++)
1033 if (mids[i])
1034 cifs_mid_q_entry_release(mids[i]);
Steve Frenche4eb2952005-04-28 22:41:09 -07001035 continue;
Lars Persson696e4202018-06-25 14:05:25 +02001036 }
Steve Frenche4eb2952005-04-28 22:41:09 -07001037
Pavel Shilovskyd4e48542012-03-23 14:28:02 -04001038 if (server->large_buf)
Jeff Laytone9097ab2011-10-19 15:29:40 -04001039 buf = server->bigbuf;
Steve Frenche4eb2952005-04-28 22:41:09 -07001040
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001041
Steve Frenchfda35942011-01-20 18:06:34 +00001042 server->lstrp = jiffies;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001043
1044 for (i = 0; i < num_mids; i++) {
1045 if (mids[i] != NULL) {
1046 mids[i]->resp_buf_size = server->pdu_size;
1047 if ((mids[i]->mid_flags & MID_WAIT_CANCELLED) &&
1048 mids[i]->mid_state == MID_RESPONSE_RECEIVED &&
1049 server->ops->handle_cancelled_mid)
1050 server->ops->handle_cancelled_mid(
1051 mids[i]->resp_buf,
Sachin Prabhu38bd4902017-03-03 15:41:38 -08001052 server);
1053
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001054 if (!mids[i]->multiRsp || mids[i]->multiEnd)
1055 mids[i]->callback(mids[i]);
Lars Persson696e4202018-06-25 14:05:25 +02001056
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001057 cifs_mid_q_entry_release(mids[i]);
1058 } else if (server->ops->is_oplock_break &&
1059 server->ops->is_oplock_break(bufs[i],
1060 server)) {
1061 cifs_dbg(FYI, "Received oplock break\n");
1062 } else {
1063 cifs_dbg(VFS, "No task to wake, unknown frame "
1064 "received! NumMids %d\n",
1065 atomic_read(&midCount));
1066 cifs_dump_mem("Received Data is: ", bufs[i],
1067 HEADER_SIZE(server));
Steve French39798772006-05-31 22:40:51 +00001068#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001069 if (server->ops->dump_detail)
1070 server->ops->dump_detail(bufs[i],
1071 server);
1072 cifs_dump_mids(server);
Steve French39798772006-05-31 22:40:51 +00001073#endif /* CIFS_DEBUG2 */
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001074 }
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001075 }
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001076
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001077 if (pdu_length > server->pdu_size) {
1078 if (!allocate_buffers(server))
1079 continue;
1080 pdu_length -= server->pdu_size;
1081 server->total_read = 0;
1082 server->large_buf = false;
1083 buf = server->smallbuf;
1084 goto next_pdu;
Steve Frenche4eb2952005-04-28 22:41:09 -07001085 }
1086 } /* end while !EXITING */
1087
Justin P. Mattockfd62cb72011-02-24 22:15:02 -08001088 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -04001089 cifs_buf_release(server->bigbuf);
1090 if (server->smallbuf) /* no sense logging a debug message if NULL */
1091 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001093 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +04001094 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001095
1096 /* if server->tsk was NULL then wait for a signal before exiting */
1097 if (!task_to_wake) {
1098 set_current_state(TASK_INTERRUPTIBLE);
1099 while (!signal_pending(current)) {
1100 schedule();
1101 set_current_state(TASK_INTERRUPTIBLE);
1102 }
1103 set_current_state(TASK_RUNNING);
1104 }
1105
Jeff Layton0468a2c2008-12-01 07:09:35 -05001106 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107}
1108
Jeff Laytonc359cf32007-11-16 22:22:06 +00001109/* extract the host portion of the UNC string */
1110static char *
1111extract_hostname(const char *unc)
1112{
1113 const char *src;
1114 char *dst, *delim;
1115 unsigned int len;
1116
1117 /* skip double chars at beginning of string */
1118 /* BB: check validity of these bytes? */
1119 src = unc + 2;
1120
1121 /* delimiter between hostname and sharename is always '\\' now */
1122 delim = strchr(src, '\\');
1123 if (!delim)
1124 return ERR_PTR(-EINVAL);
1125
1126 len = delim - src;
1127 dst = kmalloc((len + 1), GFP_KERNEL);
1128 if (dst == NULL)
1129 return ERR_PTR(-ENOMEM);
1130
1131 memcpy(dst, src, len);
1132 dst[len] = '\0';
1133
1134 return dst;
1135}
1136
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001137static int get_option_ul(substring_t args[], unsigned long *option)
1138{
1139 int rc;
1140 char *string;
1141
1142 string = match_strdup(args);
1143 if (string == NULL)
1144 return -ENOMEM;
Sachin Prabhubfa890a2012-04-13 14:04:32 +01001145 rc = kstrtoul(string, 0, option);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001146 kfree(string);
1147
1148 return rc;
1149}
1150
Eric W. Biederman3da46562013-02-06 01:37:39 -08001151static int get_option_uid(substring_t args[], kuid_t *result)
1152{
1153 unsigned long value;
1154 kuid_t uid;
1155 int rc;
1156
1157 rc = get_option_ul(args, &value);
1158 if (rc)
1159 return rc;
1160
1161 uid = make_kuid(current_user_ns(), value);
1162 if (!uid_valid(uid))
1163 return -EINVAL;
1164
1165 *result = uid;
1166 return 0;
1167}
1168
1169static int get_option_gid(substring_t args[], kgid_t *result)
1170{
1171 unsigned long value;
1172 kgid_t gid;
1173 int rc;
1174
1175 rc = get_option_ul(args, &value);
1176 if (rc)
1177 return rc;
1178
1179 gid = make_kgid(current_user_ns(), value);
1180 if (!gid_valid(gid))
1181 return -EINVAL;
1182
1183 *result = gid;
1184 return 0;
1185}
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001186
1187static int cifs_parse_security_flavors(char *value,
1188 struct smb_vol *vol)
1189{
1190
1191 substring_t args[MAX_OPT_ARGS];
1192
Jeff Layton1e3cc572013-06-10 17:12:23 -05001193 /*
1194 * With mount options, the last one should win. Reset any existing
1195 * settings back to default.
1196 */
1197 vol->sectype = Unspecified;
1198 vol->sign = false;
1199
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001200 switch (match_token(value, cifs_secflavor_tokens, args)) {
Jeff Layton3f618222013-06-12 19:52:14 -05001201 case Opt_sec_krb5p:
1202 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1203 return 1;
1204 case Opt_sec_krb5i:
1205 vol->sign = true;
1206 /* Fallthrough */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001207 case Opt_sec_krb5:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001208 vol->sectype = Kerberos;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001209 break;
1210 case Opt_sec_ntlmsspi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001211 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001212 /* Fallthrough */
1213 case Opt_sec_ntlmssp:
1214 vol->sectype = RawNTLMSSP;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001215 break;
1216 case Opt_sec_ntlmi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001217 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001218 /* Fallthrough */
1219 case Opt_ntlm:
1220 vol->sectype = NTLM;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001221 break;
1222 case Opt_sec_ntlmv2i:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001223 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001224 /* Fallthrough */
1225 case Opt_sec_ntlmv2:
1226 vol->sectype = NTLMv2;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001227 break;
1228#ifdef CONFIG_CIFS_WEAK_PW_HASH
1229 case Opt_sec_lanman:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001230 vol->sectype = LANMAN;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001231 break;
1232#endif
1233 case Opt_sec_none:
1234 vol->nullauth = 1;
1235 break;
1236 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001237 cifs_dbg(VFS, "bad security option: %s\n", value);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001238 return 1;
1239 }
1240
1241 return 0;
1242}
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244static int
Jeff Layton15b6a472012-05-16 07:50:15 -04001245cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1246{
1247 substring_t args[MAX_OPT_ARGS];
1248
1249 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1250 case Opt_cache_loose:
1251 vol->direct_io = false;
1252 vol->strict_io = false;
1253 break;
1254 case Opt_cache_strict:
1255 vol->direct_io = false;
1256 vol->strict_io = true;
1257 break;
1258 case Opt_cache_none:
1259 vol->direct_io = true;
1260 vol->strict_io = false;
1261 break;
1262 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001263 cifs_dbg(VFS, "bad cache= option: %s\n", value);
Jeff Layton15b6a472012-05-16 07:50:15 -04001264 return 1;
1265 }
1266 return 0;
1267}
1268
1269static int
Steve Frenchc7c137b2018-06-06 17:59:29 -05001270cifs_parse_smb_version(char *value, struct smb_vol *vol, bool is_smb3)
Jeff Layton23db65f2012-05-15 12:20:51 -04001271{
1272 substring_t args[MAX_OPT_ARGS];
1273
1274 switch (match_token(value, cifs_smb_version_tokens, args)) {
Steve French74204512018-06-19 14:34:08 -05001275#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
Jeff Layton23db65f2012-05-15 12:20:51 -04001276 case Smb_1:
Steve Frenchf92a7202018-05-24 04:11:07 -05001277 if (disable_legacy_dialects) {
1278 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1279 return 1;
1280 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001281 if (is_smb3) {
1282 cifs_dbg(VFS, "vers=1.0 (cifs) not permitted when mounting with smb3\n");
1283 return 1;
1284 }
Jeff Layton23db65f2012-05-15 12:20:51 -04001285 vol->ops = &smb1_operations;
1286 vol->vals = &smb1_values;
1287 break;
Steve Frenchdd446b12012-11-28 23:21:06 -06001288 case Smb_20:
Steve Frenchf92a7202018-05-24 04:11:07 -05001289 if (disable_legacy_dialects) {
1290 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1291 return 1;
1292 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001293 if (is_smb3) {
1294 cifs_dbg(VFS, "vers=2.0 not permitted when mounting with smb3\n");
1295 return 1;
1296 }
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001297 vol->ops = &smb20_operations;
Steve Frenchdd446b12012-11-28 23:21:06 -06001298 vol->vals = &smb20_values;
1299 break;
Steve French74204512018-06-19 14:34:08 -05001300#else
1301 case Smb_1:
1302 cifs_dbg(VFS, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
1303 return 1;
1304 case Smb_20:
1305 cifs_dbg(VFS, "vers=2.0 mount not permitted when legacy dialects disabled\n");
1306 return 1;
1307#endif /* CIFS_ALLOW_INSECURE_LEGACY */
Steve French1080ef72011-02-24 18:07:19 +00001308 case Smb_21:
1309 vol->ops = &smb21_operations;
1310 vol->vals = &smb21_values;
1311 break;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001312 case Smb_30:
Steve French38107d42012-12-08 22:08:06 -06001313 vol->ops = &smb30_operations;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001314 vol->vals = &smb30_values;
1315 break;
Steve French20b6d8b2013-06-12 22:48:41 -05001316 case Smb_302:
1317 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1318 vol->vals = &smb302_values;
1319 break;
Steve French5f7fbf72014-12-17 22:52:58 -06001320 case Smb_311:
Steve Frenchaab18932015-06-23 23:37:11 -05001321 vol->ops = &smb311_operations;
Steve French5f7fbf72014-12-17 22:52:58 -06001322 vol->vals = &smb311_values;
1323 break;
Steve French9764c022017-09-17 10:41:35 -05001324 case Smb_3any:
1325 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1326 vol->vals = &smb3any_values;
1327 break;
1328 case Smb_default:
1329 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1330 vol->vals = &smbdefault_values;
1331 break;
Jeff Layton23db65f2012-05-15 12:20:51 -04001332 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001333 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
Jeff Layton23db65f2012-05-15 12:20:51 -04001334 return 1;
1335 }
1336 return 0;
1337}
1338
Jeff Laytond387a5c2012-12-10 06:10:46 -05001339/*
1340 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1341 * fields with the result. Returns 0 on success and an error otherwise.
1342 */
1343static int
1344cifs_parse_devname(const char *devname, struct smb_vol *vol)
1345{
1346 char *pos;
1347 const char *delims = "/\\";
1348 size_t len;
1349
Yao Liu36a32192019-01-28 19:47:28 +08001350 if (unlikely(!devname || !*devname)) {
1351 cifs_dbg(VFS, "Device name not specified.\n");
1352 return -EINVAL;
1353 }
1354
Jeff Laytond387a5c2012-12-10 06:10:46 -05001355 /* make sure we have a valid UNC double delimiter prefix */
1356 len = strspn(devname, delims);
1357 if (len != 2)
1358 return -EINVAL;
1359
1360 /* find delimiter between host and sharename */
1361 pos = strpbrk(devname + 2, delims);
1362 if (!pos)
1363 return -EINVAL;
1364
1365 /* skip past delimiter */
1366 ++pos;
1367
1368 /* now go until next delimiter or end of string */
1369 len = strcspn(pos, delims);
1370
1371 /* move "pos" up to delimiter or NULL */
1372 pos += len;
1373 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1374 if (!vol->UNC)
1375 return -ENOMEM;
1376
1377 convert_delimiter(vol->UNC, '\\');
1378
Sachin Prabhu11e31642016-02-08 13:44:01 +05301379 /* skip any delimiter */
1380 if (*pos == '/' || *pos == '\\')
1381 pos++;
1382
1383 /* If pos is NULL then no prepath */
1384 if (!*pos)
Jeff Laytond387a5c2012-12-10 06:10:46 -05001385 return 0;
1386
1387 vol->prepath = kstrdup(pos, GFP_KERNEL);
1388 if (!vol->prepath)
1389 return -ENOMEM;
1390
1391 return 0;
1392}
1393
Jeff Layton23db65f2012-05-15 12:20:51 -04001394static int
Sean Finneyb9468452011-04-11 13:19:32 +00001395cifs_parse_mount_options(const char *mountdata, const char *devname,
Steve Frenchc7c137b2018-06-06 17:59:29 -05001396 struct smb_vol *vol, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001398 char *data, *end;
Vasily Averin957df452011-06-06 11:33:12 +04001399 char *mountdata_copy = NULL, *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 unsigned int temp_len, i, j;
1401 char separator[2];
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001402 short int override_uid = -1;
1403 short int override_gid = -1;
1404 bool uid_specified = false;
1405 bool gid_specified = false;
Jeff Laytond8162552012-03-23 14:40:56 -04001406 bool sloppy = false;
1407 char *invalid = NULL;
Jeff Layton88463992010-11-22 15:31:03 -05001408 char *nodename = utsname()->nodename;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001409 char *string = NULL;
1410 char *tmp_end, *value;
1411 char delim;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001412 bool got_ip = false;
Steve French7e682f72017-08-31 21:34:24 -05001413 bool got_version = false;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001414 unsigned short port = 0;
1415 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +00001418 separator[1] = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001419 delim = separator[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420
Jeff Layton6ee95422012-11-26 11:09:57 -05001421 /* ensure we always start with zeroed-out smb_vol */
1422 memset(vol, 0, sizeof(*vol));
1423
Jeff Layton88463992010-11-22 15:31:03 -05001424 /*
1425 * does not have to be perfect mapping since field is
1426 * informational, only used for servers that do not support
1427 * port 445 and it can be overridden at mount time
1428 */
Jeff Layton1397f2e2011-01-07 11:30:28 -05001429 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1430 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
Jeff Layton88463992010-11-22 15:31:03 -05001431 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1432
Jeff Layton1397f2e2011-01-07 11:30:28 -05001433 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -07001434 /* null target name indicates to use *SMBSERVR default called name
1435 if we end up sending RFC1001 session initialize */
1436 vol->target_rfc1001_name[0] = 0;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04001437 vol->cred_uid = current_uid();
1438 vol->linux_uid = current_uid();
David Howellsa001e5b2008-11-14 10:38:47 +11001439 vol->linux_gid = current_gid();
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001440
Steve French2baa2682014-09-27 02:19:01 -05001441 /*
1442 * default to SFM style remapping of seven reserved characters
1443 * unless user overrides it or we negotiate CIFS POSIX where
1444 * it is unnecessary. Can not simultaneously use more than one mapping
1445 * since then readdir could list files that open could not open
1446 */
1447 vol->remap = true;
1448
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001449 /* default to only allowing write access to owner of the mount */
1450 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Jeremy Allisonac670552005-06-22 17:26:35 -07001453 /* default is always to request posix paths. */
1454 vol->posix_paths = 1;
Jeff Laytona0c92172009-05-27 15:40:47 -04001455 /* default to using server inode numbers where available */
1456 vol->server_ino = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001457
Jeff Layton1b359202012-09-19 15:20:27 -07001458 /* default is to use strict cifs caching semantics */
1459 vol->strict_io = true;
1460
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301461 vol->actimeo = CIFS_DEF_ACTIMEO;
1462
Steve French9764c022017-09-17 10:41:35 -05001463 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1464 vol->ops = &smb30_operations;
1465 vol->vals = &smbdefault_values;
Jeff Layton23db65f2012-05-15 12:20:51 -04001466
Rabin Vincentb782fcc2016-07-19 09:25:45 +02001467 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1468
Sean Finneyb9468452011-04-11 13:19:32 +00001469 if (!mountdata)
1470 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Sean Finneyb9468452011-04-11 13:19:32 +00001472 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1473 if (!mountdata_copy)
1474 goto cifs_parse_mount_err;
1475
1476 options = mountdata_copy;
Pavel Shilovsky4906e502011-04-14 22:00:56 +04001477 end = options + strlen(options);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001478
Steve French50c2f752007-07-13 00:33:32 +00001479 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001480 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 separator[0] = options[4];
1482 options += 5;
1483 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001484 cifs_dbg(FYI, "Null separator not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486 }
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001487 vol->backupuid_specified = false; /* no backup intent for a user */
1488 vol->backupgid_specified = false; /* no backup intent for a group */
Steve French50c2f752007-07-13 00:33:32 +00001489
Jeff Layton37d4f992013-05-24 07:40:05 -04001490 switch (cifs_parse_devname(devname, vol)) {
1491 case 0:
1492 break;
1493 case -ENOMEM:
1494 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1495 goto cifs_parse_mount_err;
1496 case -EINVAL:
1497 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1498 goto cifs_parse_mount_err;
1499 default:
1500 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1501 goto cifs_parse_mount_err;
Jeff Laytond387a5c2012-12-10 06:10:46 -05001502 }
1503
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 while ((data = strsep(&options, separator)) != NULL) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001505 substring_t args[MAX_OPT_ARGS];
1506 unsigned long option;
1507 int token;
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (!*data)
1510 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001512 token = match_token(data, cifs_mount_option_tokens, args);
1513
1514 switch (token) {
1515
1516 /* Ingnore the following */
1517 case Opt_ignore:
1518 break;
1519
1520 /* Boolean values */
1521 case Opt_user_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 vol->no_xattr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001523 break;
1524 case Opt_nouser_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 vol->no_xattr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001526 break;
1527 case Opt_forceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001528 override_uid = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001529 break;
1530 case Opt_noforceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001531 override_uid = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001532 break;
Jeff Layton72bd4812012-10-03 16:02:36 -04001533 case Opt_forcegid:
1534 override_gid = 1;
1535 break;
1536 case Opt_noforcegid:
1537 override_gid = 0;
1538 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001539 case Opt_noblocksend:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001540 vol->noblocksnd = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001541 break;
1542 case Opt_noautotune:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001543 vol->noautotune = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001544 break;
1545 case Opt_hard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001547 break;
1548 case Opt_soft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001550 break;
1551 case Opt_perm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 vol->noperm = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001553 break;
1554 case Opt_noperm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 vol->noperm = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001556 break;
1557 case Opt_mapchars:
Steve French2baa2682014-09-27 02:19:01 -05001558 vol->sfu_remap = true;
1559 vol->remap = false; /* disable SFM mapping */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001560 break;
1561 case Opt_nomapchars:
Steve French2baa2682014-09-27 02:19:01 -05001562 vol->sfu_remap = false;
1563 break;
1564 case Opt_mapposix:
1565 vol->remap = true;
1566 vol->sfu_remap = false; /* disable SFU mapping */
1567 break;
1568 case Opt_nomapposix:
1569 vol->remap = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001570 break;
1571 case Opt_sfu:
Steve French50c2f752007-07-13 00:33:32 +00001572 vol->sfu_emul = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001573 break;
1574 case Opt_nosfu:
Steve French50c2f752007-07-13 00:33:32 +00001575 vol->sfu_emul = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001576 break;
1577 case Opt_nodfs:
Steve French2c1b8612008-10-16 18:35:21 +00001578 vol->nodfs = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001579 break;
1580 case Opt_posixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001581 vol->posix_paths = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001582 break;
1583 case Opt_noposixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001584 vol->posix_paths = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001585 break;
1586 case Opt_nounix:
Steve Frenchb3266142018-05-20 23:41:10 -05001587 if (vol->linux_ext)
1588 cifs_dbg(VFS,
1589 "conflicting unix mount options\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00001590 vol->no_linux_ext = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001591 break;
Steve Frenchb3266142018-05-20 23:41:10 -05001592 case Opt_unix:
1593 if (vol->no_linux_ext)
1594 cifs_dbg(VFS,
1595 "conflicting unix mount options\n");
1596 vol->linux_ext = 1;
1597 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001598 case Opt_nocase:
Steve French50c2f752007-07-13 00:33:32 +00001599 vol->nocase = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001600 break;
1601 case Opt_brl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001602 vol->nobrl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001603 break;
1604 case Opt_nobrl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001605 vol->nobrl = 1;
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001606 /*
1607 * turn off mandatory locking in mode
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001608 * if remote locking is turned off since the
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001609 * local vfs will do advisory
1610 */
Steve French50c2f752007-07-13 00:33:32 +00001611 if (vol->file_mode ==
1612 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001613 vol->file_mode = S_IALLUGO;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001614 break;
Steve French3d4ef9a2018-04-25 22:19:09 -05001615 case Opt_nohandlecache:
1616 vol->nohandlecache = 1;
1617 break;
1618 case Opt_handlecache:
1619 vol->nohandlecache = 0;
1620 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001621 case Opt_forcemandatorylock:
Steve French13a6e422008-12-02 17:24:33 +00001622 vol->mand_lock = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001623 break;
1624 case Opt_setuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 vol->setuids = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001626 break;
1627 case Opt_nosetuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 vol->setuids = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001629 break;
Steve French95932652016-09-23 01:36:34 -05001630 case Opt_setuidfromacl:
1631 vol->setuidfromacl = 1;
1632 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001633 case Opt_dynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001634 vol->dynperm = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001635 break;
1636 case Opt_nodynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001637 vol->dynperm = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001638 break;
1639 case Opt_nohard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001641 break;
1642 case Opt_nosoft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001644 break;
1645 case Opt_nointr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 vol->intr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001647 break;
1648 case Opt_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 vol->intr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001650 break;
1651 case Opt_nostrictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001652 vol->nostrictsync = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001653 break;
1654 case Opt_strictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001655 vol->nostrictsync = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001656 break;
1657 case Opt_serverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 vol->server_ino = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001659 break;
1660 case Opt_noserverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 vol->server_ino = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001662 break;
1663 case Opt_rwpidforward:
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001664 vol->rwpidforward = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001665 break;
1666 case Opt_cifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001667 vol->cifs_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001668 break;
1669 case Opt_nocifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001670 vol->cifs_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001671 break;
1672 case Opt_acl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 vol->no_psx_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001674 break;
1675 case Opt_noacl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 vol->no_psx_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001677 break;
1678 case Opt_locallease:
Steve French84210e92008-10-23 04:42:37 +00001679 vol->local_lease = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001680 break;
1681 case Opt_sign:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001682 vol->sign = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001683 break;
1684 case Opt_seal:
Steve French95b1cb92008-05-15 16:44:38 +00001685 /* we do not do the following in secFlags because seal
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001686 * is a per tree connection (mount) not a per socket
1687 * or per-smb connection option in the protocol
1688 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1689 */
Steve French95b1cb92008-05-15 16:44:38 +00001690 vol->seal = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001691 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001692 case Opt_noac:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001693 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001694 break;
1695 case Opt_fsc:
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301696#ifndef CONFIG_CIFS_FSCACHE
Joe Perchesf96637b2013-05-04 22:12:25 -05001697 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001698 goto cifs_parse_mount_err;
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301699#endif
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05301700 vol->fsc = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001701 break;
1702 case Opt_mfsymlinks:
Stefan Metzmacher736a3322010-07-30 14:56:00 +02001703 vol->mfsymlinks = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001704 break;
1705 case Opt_multiuser:
Jeff Layton0eb8a132010-10-06 19:51:12 -04001706 vol->multiuser = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001707 break;
Jeff Laytond8162552012-03-23 14:40:56 -04001708 case Opt_sloppy:
1709 sloppy = true;
1710 break;
Jeff Laytona0b3df52013-05-24 07:40:59 -04001711 case Opt_nosharesock:
1712 vol->nosharesock = true;
1713 break;
Steve Frenchb2a30772015-09-29 21:49:28 -05001714 case Opt_nopersistent:
1715 vol->nopersistent = true;
1716 if (vol->persistent) {
1717 cifs_dbg(VFS,
1718 "persistenthandles mount options conflict\n");
1719 goto cifs_parse_mount_err;
1720 }
1721 break;
1722 case Opt_persistent:
1723 vol->persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06001724 if ((vol->nopersistent) || (vol->resilient)) {
Steve Frenchb2a30772015-09-29 21:49:28 -05001725 cifs_dbg(VFS,
1726 "persistenthandles mount options conflict\n");
1727 goto cifs_parse_mount_err;
1728 }
1729 break;
Steve French592fafe2015-11-03 10:08:53 -06001730 case Opt_resilient:
1731 vol->resilient = true;
1732 if (vol->persistent) {
1733 cifs_dbg(VFS,
1734 "persistenthandles mount options conflict\n");
1735 goto cifs_parse_mount_err;
1736 }
1737 break;
1738 case Opt_noresilient:
1739 vol->resilient = false; /* already the default */
1740 break;
Germano Percossi39566442016-12-15 12:31:18 +05301741 case Opt_domainauto:
1742 vol->domainauto = true;
1743 break;
Long Li8339dd32017-11-07 01:54:55 -07001744 case Opt_rdma:
1745 vol->rdma = true;
1746 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001747
1748 /* Numeric Values */
1749 case Opt_backupuid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001750 if (get_option_uid(args, &vol->backupuid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001751 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1752 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001753 goto cifs_parse_mount_err;
1754 }
1755 vol->backupuid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001756 break;
1757 case Opt_backupgid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001758 if (get_option_gid(args, &vol->backupgid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001759 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1760 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001761 goto cifs_parse_mount_err;
1762 }
1763 vol->backupgid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001764 break;
1765 case Opt_uid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001766 if (get_option_uid(args, &vol->linux_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001767 cifs_dbg(VFS, "%s: Invalid uid value\n",
1768 __func__);
Sean Finneyb9468452011-04-11 13:19:32 +00001769 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001771 uid_specified = true;
1772 break;
1773 case Opt_cruid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001774 if (get_option_uid(args, &vol->cred_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001775 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1776 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001777 goto cifs_parse_mount_err;
1778 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001779 break;
1780 case Opt_gid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001781 if (get_option_gid(args, &vol->linux_gid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001782 cifs_dbg(VFS, "%s: Invalid gid value\n",
1783 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001784 goto cifs_parse_mount_err;
1785 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001786 gid_specified = true;
1787 break;
1788 case Opt_file_mode:
1789 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001790 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1791 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001792 goto cifs_parse_mount_err;
1793 }
1794 vol->file_mode = option;
1795 break;
1796 case Opt_dirmode:
1797 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001798 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1799 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001800 goto cifs_parse_mount_err;
1801 }
1802 vol->dir_mode = option;
1803 break;
1804 case Opt_port:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001805 if (get_option_ul(args, &option) ||
1806 option > USHRT_MAX) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001807 cifs_dbg(VFS, "%s: Invalid port value\n",
1808 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001809 goto cifs_parse_mount_err;
1810 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001811 port = (unsigned short)option;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001812 break;
1813 case Opt_rsize:
1814 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001815 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1816 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001817 goto cifs_parse_mount_err;
1818 }
1819 vol->rsize = option;
1820 break;
1821 case Opt_wsize:
1822 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001823 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1824 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001825 goto cifs_parse_mount_err;
1826 }
1827 vol->wsize = option;
1828 break;
1829 case Opt_actimeo:
1830 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001831 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1832 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001833 goto cifs_parse_mount_err;
1834 }
1835 vol->actimeo = HZ * option;
1836 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001837 cifs_dbg(VFS, "attribute cache timeout too large\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001838 goto cifs_parse_mount_err;
1839 }
1840 break;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001841 case Opt_echo_interval:
1842 if (get_option_ul(args, &option)) {
1843 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
1844 __func__);
1845 goto cifs_parse_mount_err;
1846 }
1847 vol->echo_interval = option;
1848 break;
Steve French8b217fe2016-11-11 22:36:20 -06001849 case Opt_snapshot:
1850 if (get_option_ul(args, &option)) {
1851 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
1852 __func__);
1853 goto cifs_parse_mount_err;
1854 }
1855 vol->snapshot_time = option;
1856 break;
Steve French141891f2016-09-23 00:44:16 -05001857 case Opt_max_credits:
1858 if (get_option_ul(args, &option) || (option < 20) ||
1859 (option > 60000)) {
1860 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
1861 __func__);
1862 goto cifs_parse_mount_err;
1863 }
1864 vol->max_credits = option;
1865 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001866
1867 /* String Arguments */
1868
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001869 case Opt_blank_user:
1870 /* null user, ie. anonymous authentication */
1871 vol->nullauth = 1;
1872 vol->username = NULL;
1873 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001874 case Opt_user:
1875 string = match_strdup(args);
1876 if (string == NULL)
1877 goto out_nomem;
1878
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001879 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1880 CIFS_MAX_USERNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001881 pr_warn("CIFS: username too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001882 goto cifs_parse_mount_err;
1883 }
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001884
1885 kfree(vol->username);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001886 vol->username = kstrdup(string, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -05001887 if (!vol->username)
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001888 goto cifs_parse_mount_err;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001889 break;
1890 case Opt_blank_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001891 /* passwords have to be handled differently
1892 * to allow the character used for deliminator
1893 * to be passed within them
1894 */
1895
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001896 /*
1897 * Check if this is a case where the password
1898 * starts with a delimiter
1899 */
1900 tmp_end = strchr(data, '=');
1901 tmp_end++;
1902 if (!(tmp_end < end && tmp_end[1] == delim)) {
1903 /* No it is not. Set the password to NULL */
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001904 kzfree(vol->password);
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001905 vol->password = NULL;
1906 break;
1907 }
1908 /* Yes it is. Drop down to Opt_pass below.*/
1909 case Opt_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001910 /* Obtain the value string */
1911 value = strchr(data, '=');
Sachin Prabhu10238072012-03-28 18:07:08 +01001912 value++;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001913
1914 /* Set tmp_end to end of the string */
1915 tmp_end = (char *) value + strlen(value);
1916
1917 /* Check if following character is the deliminator
1918 * If yes, we have encountered a double deliminator
1919 * reset the NULL character to the deliminator
1920 */
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301921 if (tmp_end < end && tmp_end[1] == delim) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001922 tmp_end[0] = delim;
1923
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301924 /* Keep iterating until we get to a single
1925 * deliminator OR the end
1926 */
1927 while ((tmp_end = strchr(tmp_end, delim))
1928 != NULL && (tmp_end[1] == delim)) {
1929 tmp_end = (char *) &tmp_end[2];
1930 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001931
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301932 /* Reset var options to point to next element */
1933 if (tmp_end) {
1934 tmp_end[0] = '\0';
1935 options = (char *) &tmp_end[1];
1936 } else
1937 /* Reached the end of the mount option
1938 * string */
1939 options = end;
1940 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001941
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001942 kzfree(vol->password);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001943 /* Now build new password string */
1944 temp_len = strlen(value);
1945 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1946 if (vol->password == NULL) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001947 pr_warn("CIFS: no memory for password\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001948 goto cifs_parse_mount_err;
1949 }
1950
1951 for (i = 0, j = 0; i < temp_len; i++, j++) {
1952 vol->password[j] = value[i];
1953 if ((value[i] == delim) &&
1954 value[i+1] == delim)
1955 /* skip the second deliminator */
1956 i++;
1957 }
1958 vol->password[j] = '\0';
1959 break;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001960 case Opt_blank_ip:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001961 /* FIXME: should this be an error instead? */
1962 got_ip = false;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001963 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001964 case Opt_ip:
1965 string = match_strdup(args);
1966 if (string == NULL)
1967 goto out_nomem;
1968
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001969 if (!cifs_convert_address(dstaddr, string,
1970 strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001971 pr_err("CIFS: bad ip= option (%s).\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001972 goto cifs_parse_mount_err;
1973 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001974 got_ip = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001975 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001976 case Opt_domain:
1977 string = match_strdup(args);
1978 if (string == NULL)
1979 goto out_nomem;
1980
Chen Gang057d6332013-07-19 09:01:36 +08001981 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
1982 == CIFS_MAX_DOMAINNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001983 pr_warn("CIFS: domain name too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001984 goto cifs_parse_mount_err;
1985 }
1986
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001987 kfree(vol->domainname);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001988 vol->domainname = kstrdup(string, GFP_KERNEL);
1989 if (!vol->domainname) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001990 pr_warn("CIFS: no memory for domainname\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001991 goto cifs_parse_mount_err;
1992 }
Joe Perchesf96637b2013-05-04 22:12:25 -05001993 cifs_dbg(FYI, "Domain name set\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001994 break;
1995 case Opt_srcaddr:
1996 string = match_strdup(args);
1997 if (string == NULL)
1998 goto out_nomem;
1999
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002000 if (!cifs_convert_address(
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002001 (struct sockaddr *)&vol->srcaddr,
2002 string, strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002003 pr_warn("CIFS: Could not parse srcaddr: %s\n",
2004 string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002005 goto cifs_parse_mount_err;
2006 }
2007 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002008 case Opt_iocharset:
2009 string = match_strdup(args);
2010 if (string == NULL)
2011 goto out_nomem;
2012
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01002013 if (strnlen(string, 1024) >= 65) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002014 pr_warn("CIFS: iocharset name too long.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002015 goto cifs_parse_mount_err;
2016 }
2017
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002018 if (strncasecmp(string, "default", 7) != 0) {
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04002019 kfree(vol->iocharset);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002020 vol->iocharset = kstrdup(string,
2021 GFP_KERNEL);
2022 if (!vol->iocharset) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002023 pr_warn("CIFS: no memory for charset\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002024 goto cifs_parse_mount_err;
2025 }
2026 }
2027 /* if iocharset not set then load_nls_default
2028 * is used by caller
2029 */
Joe Perchesf96637b2013-05-04 22:12:25 -05002030 cifs_dbg(FYI, "iocharset set to %s\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002031 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002032 case Opt_netbiosname:
2033 string = match_strdup(args);
2034 if (string == NULL)
2035 goto out_nomem;
2036
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002037 memset(vol->source_rfc1001_name, 0x20,
2038 RFC1001_NAME_LEN);
2039 /*
2040 * FIXME: are there cases in which a comma can
2041 * be valid in workstation netbios name (and
2042 * need special handling)?
2043 */
2044 for (i = 0; i < RFC1001_NAME_LEN; i++) {
2045 /* don't ucase netbiosname for user */
2046 if (string[i] == 0)
2047 break;
2048 vol->source_rfc1001_name[i] = string[i];
2049 }
2050 /* The string has 16th byte zero still from
2051 * set at top of the function
2052 */
2053 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002054 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002055 break;
2056 case Opt_servern:
2057 /* servernetbiosname specified override *SMBSERVER */
2058 string = match_strdup(args);
2059 if (string == NULL)
2060 goto out_nomem;
2061
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002062 /* last byte, type, is 0x20 for servr type */
2063 memset(vol->target_rfc1001_name, 0x20,
2064 RFC1001_NAME_LEN_WITH_NULL);
2065
2066 /* BB are there cases in which a comma can be
2067 valid in this workstation netbios name
2068 (and need special handling)? */
2069
2070 /* user or mount helper must uppercase the
2071 netbios name */
2072 for (i = 0; i < 15; i++) {
2073 if (string[i] == 0)
2074 break;
2075 vol->target_rfc1001_name[i] = string[i];
2076 }
2077 /* The string has 16th byte zero still from
2078 set at top of the function */
2079 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002080 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002081 break;
2082 case Opt_ver:
Steve French7e682f72017-08-31 21:34:24 -05002083 /* version of mount userspace tools, not dialect */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002084 string = match_strdup(args);
2085 if (string == NULL)
2086 goto out_nomem;
2087
Steve French7e682f72017-08-31 21:34:24 -05002088 /* If interface changes in mount.cifs bump to new ver */
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002089 if (strncasecmp(string, "1", 1) == 0) {
Steve French7e682f72017-08-31 21:34:24 -05002090 if (strlen(string) > 1) {
2091 pr_warn("Bad mount helper ver=%s. Did "
2092 "you want SMB1 (CIFS) dialect "
2093 "and mean to type vers=1.0 "
2094 "instead?\n", string);
2095 goto cifs_parse_mount_err;
2096 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002097 /* This is the default */
2098 break;
2099 }
2100 /* For all other value, error */
Steve French7e682f72017-08-31 21:34:24 -05002101 pr_warn("CIFS: Invalid mount helper version specified\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002102 goto cifs_parse_mount_err;
Jeff Layton23db65f2012-05-15 12:20:51 -04002103 case Opt_vers:
Steve French7e682f72017-08-31 21:34:24 -05002104 /* protocol version (dialect) */
Jeff Layton23db65f2012-05-15 12:20:51 -04002105 string = match_strdup(args);
2106 if (string == NULL)
2107 goto out_nomem;
2108
Steve Frenchc7c137b2018-06-06 17:59:29 -05002109 if (cifs_parse_smb_version(string, vol, is_smb3) != 0)
Jeff Layton23db65f2012-05-15 12:20:51 -04002110 goto cifs_parse_mount_err;
Steve French7e682f72017-08-31 21:34:24 -05002111 got_version = true;
Jeff Layton23db65f2012-05-15 12:20:51 -04002112 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002113 case Opt_sec:
2114 string = match_strdup(args);
2115 if (string == NULL)
2116 goto out_nomem;
2117
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002118 if (cifs_parse_security_flavors(string, vol) != 0)
2119 goto cifs_parse_mount_err;
2120 break;
Jeff Layton15b6a472012-05-16 07:50:15 -04002121 case Opt_cache:
2122 string = match_strdup(args);
2123 if (string == NULL)
2124 goto out_nomem;
2125
2126 if (cifs_parse_cache_flavor(string, vol) != 0)
2127 goto cifs_parse_mount_err;
2128 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002129 default:
Jeff Laytond8162552012-03-23 14:40:56 -04002130 /*
2131 * An option we don't recognize. Save it off for later
2132 * if we haven't already found one
2133 */
2134 if (!invalid)
2135 invalid = data;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002136 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002138 /* Free up any allocated string */
2139 kfree(string);
2140 string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002142
Jeff Laytond8162552012-03-23 14:40:56 -04002143 if (!sloppy && invalid) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002144 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
Jeff Laytond8162552012-03-23 14:40:56 -04002145 goto cifs_parse_mount_err;
2146 }
2147
Long Li8339dd32017-11-07 01:54:55 -07002148 if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
2149 cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
2150 goto cifs_parse_mount_err;
2151 }
2152
Jeff Layton8a8798a2012-01-17 16:09:15 -05002153#ifndef CONFIG_KEYS
2154 /* Muliuser mounts require CONFIG_KEYS support */
2155 if (vol->multiuser) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002156 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002157 goto cifs_parse_mount_err;
Jeff Layton0eb8a132010-10-06 19:51:12 -04002158 }
Jeff Layton8a8798a2012-01-17 16:09:15 -05002159#endif
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002160 if (!vol->UNC) {
Jeff Layton37d4f992013-05-24 07:40:05 -04002161 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002162 goto cifs_parse_mount_err;
2163 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002164
Jeff Layton62a1a432012-12-10 06:10:45 -05002165 /* make sure UNC has a share name */
2166 if (!strchr(vol->UNC + 3, '\\')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002167 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
Jeff Layton62a1a432012-12-10 06:10:45 -05002168 goto cifs_parse_mount_err;
2169 }
2170
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002171 if (!got_ip) {
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002172 int len;
2173 const char *slash;
2174
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002175 /* No ip= option specified? Try to get it from UNC */
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002176 /* Use the address part of the UNC. */
2177 slash = strchr(&vol->UNC[2], '\\');
2178 len = slash - &vol->UNC[2];
2179 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002180 pr_err("Unable to determine destination address.\n");
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002181 goto cifs_parse_mount_err;
2182 }
2183 }
2184
2185 /* set the port that we got earlier */
2186 cifs_set_port(dstaddr, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002188 if (uid_specified)
2189 vol->override_uid = override_uid;
2190 else if (override_uid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002191 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002192
2193 if (gid_specified)
2194 vol->override_gid = override_gid;
2195 else if (override_gid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002196 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002197
Steve French7e682f72017-08-31 21:34:24 -05002198 if (got_version == false)
2199 pr_warn("No dialect specified on mount. Default has changed to "
Steve French9764c022017-09-17 10:41:35 -05002200 "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
Steve French7e682f72017-08-31 21:34:24 -05002201 "(SMB1). To use the less secure SMB1 dialect to access "
Steve French9764c022017-09-17 10:41:35 -05002202 "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
2203 " on mount.\n");
Steve French7e682f72017-08-31 21:34:24 -05002204
Sean Finneyb9468452011-04-11 13:19:32 +00002205 kfree(mountdata_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 return 0;
Sean Finneyb9468452011-04-11 13:19:32 +00002207
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002208out_nomem:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002209 pr_warn("Could not allocate temporary buffer\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002210cifs_parse_mount_err:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002211 kfree(string);
Sean Finneyb9468452011-04-11 13:19:32 +00002212 kfree(mountdata_copy);
2213 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214}
2215
Ben Greear3eb9a882010-09-01 17:06:02 -07002216/** Returns true if srcaddr isn't specified and rhs isn't
2217 * specified, or if srcaddr is specified and
2218 * matches the IP address of the rhs argument.
2219 */
Jeff Layton45151482010-07-06 20:43:02 -04002220static bool
Ben Greear3eb9a882010-09-01 17:06:02 -07002221srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2222{
2223 switch (srcaddr->sa_family) {
2224 case AF_UNSPEC:
2225 return (rhs->sa_family == AF_UNSPEC);
2226 case AF_INET: {
2227 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2228 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2229 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2230 }
2231 case AF_INET6: {
2232 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05002233 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07002234 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2235 }
2236 default:
2237 WARN_ON(1);
2238 return false; /* don't expect to be here */
2239 }
2240}
2241
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002242/*
2243 * If no port is specified in addr structure, we try to match with 445 port
2244 * and if it fails - with 139 ports. It should be called only if address
2245 * families of server and addr are equal.
2246 */
2247static bool
2248match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2249{
Steve French6da97912011-03-13 18:55:55 +00002250 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002251
2252 switch (addr->sa_family) {
2253 case AF_INET:
2254 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2255 port = ((struct sockaddr_in *) addr)->sin_port;
2256 break;
2257 case AF_INET6:
2258 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2259 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2260 break;
2261 default:
2262 WARN_ON(1);
2263 return false;
2264 }
2265
2266 if (!port) {
2267 port = htons(CIFS_PORT);
2268 if (port == *sport)
2269 return true;
2270
2271 port = htons(RFC1001_PORT);
2272 }
2273
2274 return port == *sport;
2275}
Ben Greear3eb9a882010-09-01 17:06:02 -07002276
2277static bool
2278match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2279 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280{
Jeff Layton45151482010-07-06 20:43:02 -04002281 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002282 case AF_INET: {
2283 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2284 struct sockaddr_in *srv_addr4 =
2285 (struct sockaddr_in *)&server->dstaddr;
2286
2287 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04002288 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002289 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002290 }
2291 case AF_INET6: {
2292 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2293 struct sockaddr_in6 *srv_addr6 =
2294 (struct sockaddr_in6 *)&server->dstaddr;
2295
Jeff Layton45151482010-07-06 20:43:02 -04002296 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002297 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04002298 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002299 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04002300 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002301 break;
2302 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002303 default:
2304 WARN_ON(1);
2305 return false; /* don't expect to be here */
2306 }
Jeff Layton45151482010-07-06 20:43:02 -04002307
Ben Greear3eb9a882010-09-01 17:06:02 -07002308 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2309 return false;
2310
Jeff Layton45151482010-07-06 20:43:02 -04002311 return true;
2312}
2313
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002314static bool
2315match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2316{
Jeff Layton3f618222013-06-12 19:52:14 -05002317 /*
2318 * The select_sectype function should either return the vol->sectype
2319 * that was specified, or "Unspecified" if that sectype was not
2320 * compatible with the given NEGOTIATE request.
2321 */
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302322 if (server->ops->select_sectype(server, vol->sectype)
2323 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002324 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002325
Jeff Layton3f618222013-06-12 19:52:14 -05002326 /*
2327 * Now check if signing mode is acceptable. No need to check
2328 * global_secflags at this point since if MUST_SIGN is set then
2329 * the server->sign had better be too.
2330 */
Jeff Layton38d77c52013-05-26 07:01:00 -04002331 if (vol->sign && !server->sign)
2332 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002333
2334 return true;
2335}
2336
Jeff Layton9fa114f2012-11-26 11:09:57 -05002337static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002338{
Jeff Layton9fa114f2012-11-26 11:09:57 -05002339 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2340
Jeff Laytona0b3df52013-05-24 07:40:59 -04002341 if (vol->nosharesock)
2342 return 0;
2343
Steve French9764c022017-09-17 10:41:35 -05002344 /* BB update this for smb3any and default case */
Jeff Layton23db65f2012-05-15 12:20:51 -04002345 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2346 return 0;
2347
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002348 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2349 return 0;
2350
2351 if (!match_address(server, addr,
2352 (struct sockaddr *)&vol->srcaddr))
2353 return 0;
2354
2355 if (!match_port(server, addr))
2356 return 0;
2357
2358 if (!match_security(server, vol))
2359 return 0;
2360
Rabin Vincentb782fcc2016-07-19 09:25:45 +02002361 if (server->echo_interval != vol->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002362 return 0;
2363
Long Li8339dd32017-11-07 01:54:55 -07002364 if (server->rdma != vol->rdma)
2365 return 0;
2366
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002367 return 1;
2368}
2369
Jeff Layton45151482010-07-06 20:43:02 -04002370static struct TCP_Server_Info *
Jeff Layton9fa114f2012-11-26 11:09:57 -05002371cifs_find_tcp_session(struct smb_vol *vol)
Jeff Layton45151482010-07-06 20:43:02 -04002372{
Jeff Laytone7ddee92008-11-14 13:44:38 -05002373 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302375 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04002376 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Jeff Layton9fa114f2012-11-26 11:09:57 -05002377 if (!match_server(server, vol))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002378 continue;
2379
Jeff Laytone7ddee92008-11-14 13:44:38 -05002380 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302381 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05002382 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05002383 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302385 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return NULL;
2387}
2388
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002389void
2390cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002392 struct task_struct *task;
2393
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302394 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002395 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302396 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002397 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 }
Steve Frenchdea570e02008-05-06 22:05:51 +00002399
Rob Landleyf1d0c992011-01-22 15:44:05 -06002400 put_net(cifs_net_ns(server));
2401
Jeff Laytone7ddee92008-11-14 13:44:38 -05002402 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302403 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002404
Jeff Laytonc74093b2011-01-11 07:24:23 -05002405 cancel_delayed_work_sync(&server->echo);
2406
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002407 if (from_reconnect)
2408 /*
2409 * Avoid deadlock here: reconnect work calls
2410 * cifs_put_tcp_session() at its end. Need to be sure
2411 * that reconnect work does nothing with server pointer after
2412 * that step.
2413 */
2414 cancel_delayed_work(&server->reconnect);
2415 else
2416 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002417
Jeff Laytone7ddee92008-11-14 13:44:38 -05002418 spin_lock(&GlobalMid_Lock);
2419 server->tcpStatus = CifsExiting;
2420 spin_unlock(&GlobalMid_Lock);
2421
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002422 cifs_crypto_secmech_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302423 cifs_fscache_release_client_cookie(server);
2424
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05002425 kfree(server->session_key.response);
2426 server->session_key.response = NULL;
2427 server->session_key.len = 0;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002428
2429 task = xchg(&server->tsk, NULL);
2430 if (task)
2431 force_sig(SIGKILL, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432}
2433
Jeff Layton63c038c2008-12-01 18:41:46 -05002434static struct TCP_Server_Info *
2435cifs_get_tcp_session(struct smb_vol *volume_info)
2436{
2437 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05002438 int rc;
2439
Joe Perchesf96637b2013-05-04 22:12:25 -05002440 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05002441
2442 /* see if we already have a matching tcp_ses */
Jeff Layton9fa114f2012-11-26 11:09:57 -05002443 tcp_ses = cifs_find_tcp_session(volume_info);
Jeff Layton63c038c2008-12-01 18:41:46 -05002444 if (tcp_ses)
2445 return tcp_ses;
2446
2447 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2448 if (!tcp_ses) {
2449 rc = -ENOMEM;
2450 goto out_err;
2451 }
2452
Jeff Layton23db65f2012-05-15 12:20:51 -04002453 tcp_ses->ops = volume_info->ops;
2454 tcp_ses->vals = volume_info->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06002455 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05002456 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2457 if (IS_ERR(tcp_ses->hostname)) {
2458 rc = PTR_ERR(tcp_ses->hostname);
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002459 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002460 }
2461
2462 tcp_ses->noblocksnd = volume_info->noblocksnd;
2463 tcp_ses->noautotune = volume_info->noautotune;
Steve French6a5fa2362010-01-01 01:28:43 +00002464 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
Long Li8339dd32017-11-07 01:54:55 -07002465 tcp_ses->rdma = volume_info->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03002466 tcp_ses->in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002467 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05002468 init_waitqueue_head(&tcp_ses->response_q);
2469 init_waitqueue_head(&tcp_ses->request_q);
2470 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2471 mutex_init(&tcp_ses->srv_mutex);
2472 memcpy(tcp_ses->workstation_RFC1001_name,
2473 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2474 memcpy(tcp_ses->server_RFC1001_name,
2475 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05002476 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05002477 tcp_ses->sequence_number = 0;
Steve Frenchfda35942011-01-20 18:06:34 +00002478 tcp_ses->lstrp = jiffies;
Jeff Layton58fa0152012-05-01 17:41:16 -04002479 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002480 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2481 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002482 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002483 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2484 mutex_init(&tcp_ses->reconnect_mutex);
Jeff Layton9fa114f2012-11-26 11:09:57 -05002485 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2486 sizeof(tcp_ses->srcaddr));
2487 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2488 sizeof(tcp_ses->dstaddr));
Steve Frenchfa70b872016-09-22 00:39:34 -05002489 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05002490 /*
2491 * at this point we are the only ones with the pointer
2492 * to the struct since the kernel thread not created yet
2493 * no need to spinlock this init of tcpStatus or srv_count
2494 */
2495 tcp_ses->tcpStatus = CifsNew;
2496 ++tcp_ses->srv_count;
2497
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002498 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2499 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2500 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2501 else
2502 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07002503 if (tcp_ses->rdma) {
2504#ifndef CONFIG_CIFS_SMB_DIRECT
2505 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
2506 rc = -ENOENT;
2507 goto out_err_crypto_release;
2508#endif
2509 tcp_ses->smbd_conn = smbd_get_connection(
2510 tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
2511 if (tcp_ses->smbd_conn) {
2512 cifs_dbg(VFS, "RDMA transport established\n");
2513 rc = 0;
2514 goto smbd_connected;
2515 } else {
2516 rc = -ENOENT;
2517 goto out_err_crypto_release;
2518 }
2519 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002520 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05002521 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002522 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002523 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002524 }
Long Li2f894642017-11-22 17:38:34 -07002525smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05002526 /*
2527 * since we're in a cifs function already, we know that
2528 * this will succeed. No need for try_module_get().
2529 */
2530 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04002531 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05002532 tcp_ses, "cifsd");
2533 if (IS_ERR(tcp_ses->tsk)) {
2534 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05002535 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05002536 module_put(THIS_MODULE);
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002537 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002538 }
Steve Frenchfd88ce92011-04-12 01:01:14 +00002539 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05002540
2541 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302542 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002543 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302544 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002545
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302546 cifs_fscache_get_client_cookie(tcp_ses);
2547
Jeff Laytonc74093b2011-01-11 07:24:23 -05002548 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002549 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002550
Jeff Layton63c038c2008-12-01 18:41:46 -05002551 return tcp_ses;
2552
Shirish Pargaonkarf7c54452010-10-26 18:10:24 -05002553out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002554 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002555
Rob Landleyf1d0c992011-01-22 15:44:05 -06002556 put_net(cifs_net_ns(tcp_ses));
2557
Jeff Layton63c038c2008-12-01 18:41:46 -05002558out_err:
2559 if (tcp_ses) {
Steve French8347a5c2009-10-06 18:31:29 +00002560 if (!IS_ERR(tcp_ses->hostname))
2561 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05002562 if (tcp_ses->ssocket)
2563 sock_release(tcp_ses->ssocket);
2564 kfree(tcp_ses);
2565 }
2566 return ERR_PTR(rc);
2567}
2568
Steve French96daf2b2011-05-27 04:34:02 +00002569static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002570{
Jeff Layton3f618222013-06-12 19:52:14 -05002571 if (vol->sectype != Unspecified &&
2572 vol->sectype != ses->sectype)
2573 return 0;
2574
2575 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002576 case Kerberos:
Eric W. Biederman64ed39d2013-02-06 02:30:39 -08002577 if (!uid_eq(vol->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002578 return 0;
2579 break;
2580 default:
Jeff Layton04febab2012-01-17 16:09:15 -05002581 /* NULL username means anonymous session */
2582 if (ses->user_name == NULL) {
2583 if (!vol->nullauth)
2584 return 0;
2585 break;
2586 }
2587
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002588 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05002589 if (strncmp(ses->user_name,
2590 vol->username ? vol->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002591 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002592 return 0;
Jeff Layton08b37d52014-05-23 06:53:10 -04002593 if ((vol->username && strlen(vol->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002594 ses->password != NULL &&
2595 strncmp(ses->password,
2596 vol->password ? vol->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002597 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002598 return 0;
2599 }
2600 return 1;
2601}
2602
Aurelien Aptelb327a712018-01-24 13:46:10 +01002603/**
2604 * cifs_setup_ipc - helper to setup the IPC tcon for the session
2605 *
2606 * A new IPC connection is made and stored in the session
2607 * tcon_ipc. The IPC tcon has the same lifetime as the session.
2608 */
2609static int
2610cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
2611{
2612 int rc = 0, xid;
2613 struct cifs_tcon *tcon;
2614 struct nls_table *nls_codepage;
2615 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
2616 bool seal = false;
2617
2618 /*
2619 * If the mount request that resulted in the creation of the
2620 * session requires encryption, force IPC to be encrypted too.
2621 */
2622 if (volume_info->seal) {
2623 if (ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
2624 seal = true;
2625 else {
2626 cifs_dbg(VFS,
2627 "IPC: server doesn't support encryption\n");
2628 return -EOPNOTSUPP;
2629 }
2630 }
2631
2632 tcon = tconInfoAlloc();
2633 if (tcon == NULL)
2634 return -ENOMEM;
2635
Thomas Werschlein395a2072018-08-30 18:29:20 +02002636 snprintf(unc, sizeof(unc), "\\\\%s\\IPC$", ses->server->hostname);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002637
2638 /* cannot fail */
2639 nls_codepage = load_nls_default();
2640
2641 xid = get_xid();
2642 tcon->ses = ses;
2643 tcon->ipc = true;
2644 tcon->seal = seal;
2645 rc = ses->server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
2646 free_xid(xid);
2647
2648 if (rc) {
2649 cifs_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
2650 tconInfoFree(tcon);
2651 goto out;
2652 }
2653
2654 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
2655
2656 ses->tcon_ipc = tcon;
2657out:
2658 unload_nls(nls_codepage);
2659 return rc;
2660}
2661
2662/**
2663 * cifs_free_ipc - helper to release the session IPC tcon
2664 *
2665 * Needs to be called everytime a session is destroyed
2666 */
2667static int
2668cifs_free_ipc(struct cifs_ses *ses)
2669{
2670 int rc = 0, xid;
2671 struct cifs_tcon *tcon = ses->tcon_ipc;
2672
2673 if (tcon == NULL)
2674 return 0;
2675
2676 if (ses->server->ops->tree_disconnect) {
2677 xid = get_xid();
2678 rc = ses->server->ops->tree_disconnect(xid, tcon);
2679 free_xid(xid);
2680 }
2681
2682 if (rc)
2683 cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
2684
2685 tconInfoFree(tcon);
2686 ses->tcon_ipc = NULL;
2687 return rc;
2688}
2689
Steve French96daf2b2011-05-27 04:34:02 +00002690static struct cifs_ses *
Jeff Layton4ff67b72010-07-06 20:43:02 -04002691cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692{
Steve French96daf2b2011-05-27 04:34:02 +00002693 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302695 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04002696 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002697 if (ses->status == CifsExiting)
2698 continue;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002699 if (!match_session(ses, vol))
2700 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05002701 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302702 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002703 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302705 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 return NULL;
2707}
2708
Jeff Layton14fbf502008-11-14 13:53:46 -05002709static void
Steve French96daf2b2011-05-27 04:34:02 +00002710cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05002711{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002712 unsigned int rc, xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05002713 struct TCP_Server_Info *server = ses->server;
2714
Joe Perchesf96637b2013-05-04 22:12:25 -05002715 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002716
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302717 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002718 if (ses->status == CifsExiting) {
2719 spin_unlock(&cifs_tcp_ses_lock);
2720 return;
2721 }
Jeff Layton14fbf502008-11-14 13:53:46 -05002722 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302723 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002724 return;
2725 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002726 if (ses->status == CifsGood)
2727 ses->status = CifsExiting;
2728 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002729
Aurelien Aptelb327a712018-01-24 13:46:10 +01002730 cifs_free_ipc(ses);
2731
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002732 if (ses->status == CifsExiting && server->ops->logoff) {
2733 xid = get_xid();
2734 rc = server->ops->logoff(xid, ses);
2735 if (rc)
2736 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2737 __func__, rc);
2738 _free_xid(xid);
2739 }
2740
2741 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002742 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302743 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002744
Jeff Layton14fbf502008-11-14 13:53:46 -05002745 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002746 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05002747}
2748
Jeff Layton8a8798a2012-01-17 16:09:15 -05002749#ifdef CONFIG_KEYS
2750
Chen Gang057d6332013-07-19 09:01:36 +08002751/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2752#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05002753
2754/* Populate username and pw fields from keyring if possible */
2755static int
2756cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2757{
2758 int rc = 0;
David Howells146aa8b2015-10-21 14:04:48 +01002759 const char *delim, *payload;
2760 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002761 ssize_t len;
2762 struct key *key;
2763 struct TCP_Server_Info *server = ses->server;
2764 struct sockaddr_in *sa;
2765 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01002766 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002767
2768 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2769 if (!desc)
2770 return -ENOMEM;
2771
2772 /* try to find an address key first */
2773 switch (server->dstaddr.ss_family) {
2774 case AF_INET:
2775 sa = (struct sockaddr_in *)&server->dstaddr;
2776 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2777 break;
2778 case AF_INET6:
2779 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2780 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2781 break;
2782 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002783 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2784 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002785 rc = -EINVAL;
2786 goto out_err;
2787 }
2788
Joe Perchesf96637b2013-05-04 22:12:25 -05002789 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002790 key = request_key(&key_type_logon, desc, "");
2791 if (IS_ERR(key)) {
2792 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002793 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05002794 rc = PTR_ERR(key);
2795 goto out_err;
2796 }
2797
2798 /* didn't work, try to find a domain key */
2799 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05002800 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002801 key = request_key(&key_type_logon, desc, "");
2802 if (IS_ERR(key)) {
2803 rc = PTR_ERR(key);
2804 goto out_err;
2805 }
2806 }
2807
2808 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00002809 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002810 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05002811 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002812 goto out_key_put;
2813 }
2814
2815 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01002816 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002817 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05002818 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002819 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002820 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2821 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002822 rc = -EINVAL;
2823 goto out_key_put;
2824 }
2825
2826 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002827 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002828 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2829 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002830 rc = -EINVAL;
2831 goto out_key_put;
2832 }
2833
2834 vol->username = kstrndup(payload, len, GFP_KERNEL);
2835 if (!vol->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002836 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2837 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002838 rc = -ENOMEM;
2839 goto out_key_put;
2840 }
Joe Perchesf96637b2013-05-04 22:12:25 -05002841 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002842
2843 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002844 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002845 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002846 rc = -EINVAL;
2847 kfree(vol->username);
2848 vol->username = NULL;
2849 goto out_key_put;
2850 }
2851
2852 ++delim;
2853 vol->password = kstrndup(delim, len, GFP_KERNEL);
2854 if (!vol->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002855 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2856 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002857 rc = -ENOMEM;
2858 kfree(vol->username);
2859 vol->username = NULL;
2860 goto out_key_put;
2861 }
2862
2863out_key_put:
2864 up_read(&key->sem);
2865 key_put(key);
2866out_err:
2867 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05002868 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002869 return rc;
2870}
2871#else /* ! CONFIG_KEYS */
2872static inline int
2873cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2874 struct cifs_ses *ses __attribute__((unused)))
2875{
2876 return -ENOSYS;
2877}
2878#endif /* CONFIG_KEYS */
2879
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002880/**
2881 * cifs_get_smb_ses - get a session matching @volume_info data from @server
2882 *
2883 * This function assumes it is being called from cifs_mount() where we
2884 * already got a server reference (server refcount +1). See
2885 * cifs_get_tcon() for refcount explanations.
2886 */
Steve French96daf2b2011-05-27 04:34:02 +00002887static struct cifs_ses *
Jeff Layton36988c72010-04-24 07:57:43 -04002888cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2889{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04002890 int rc = -ENOMEM;
2891 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00002892 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002893 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2894 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04002895
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002896 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04002897
Jeff Layton4ff67b72010-07-06 20:43:02 -04002898 ses = cifs_find_smb_ses(server, volume_info);
Jeff Layton36988c72010-04-24 07:57:43 -04002899 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002900 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2901 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04002902
Jeff Layton36988c72010-04-24 07:57:43 -04002903 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002904 rc = cifs_negotiate_protocol(xid, ses);
2905 if (rc) {
2906 mutex_unlock(&ses->session_mutex);
2907 /* problem -- put our ses reference */
2908 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002909 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04002910 return ERR_PTR(rc);
2911 }
Jeff Layton36988c72010-04-24 07:57:43 -04002912 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002913 cifs_dbg(FYI, "Session needs reconnect\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002914 rc = cifs_setup_session(xid, ses,
2915 volume_info->local_nls);
2916 if (rc) {
2917 mutex_unlock(&ses->session_mutex);
2918 /* problem -- put our reference */
2919 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002920 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002921 return ERR_PTR(rc);
2922 }
2923 }
2924 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04002925
2926 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002927 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002928 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002929 return ses;
2930 }
2931
Joe Perchesf96637b2013-05-04 22:12:25 -05002932 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002933 ses = sesInfoAlloc();
2934 if (ses == NULL)
2935 goto get_ses_fail;
2936
2937 /* new SMB session uses our server ref */
2938 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002939 if (server->dstaddr.ss_family == AF_INET6)
2940 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002941 else
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002942 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002943
Steve French8727c8a2011-02-25 01:11:56 -06002944 if (volume_info->username) {
2945 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2946 if (!ses->user_name)
2947 goto get_ses_fail;
2948 }
Jeff Layton36988c72010-04-24 07:57:43 -04002949
2950 /* volume_info->password freed at unmount */
2951 if (volume_info->password) {
2952 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
2953 if (!ses->password)
2954 goto get_ses_fail;
2955 }
2956 if (volume_info->domainname) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05002957 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
2958 if (!ses->domainName)
2959 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04002960 }
Germano Percossi39566442016-12-15 12:31:18 +05302961 if (volume_info->domainauto)
2962 ses->domainAuto = volume_info->domainauto;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04002963 ses->cred_uid = volume_info->cred_uid;
Jeff Layton36988c72010-04-24 07:57:43 -04002964 ses->linux_uid = volume_info->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00002965
Jeff Layton28e11bd2013-05-26 07:01:00 -04002966 ses->sectype = volume_info->sectype;
2967 ses->sign = volume_info->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04002968
2969 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002970 rc = cifs_negotiate_protocol(xid, ses);
2971 if (!rc)
2972 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04002973 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00002974 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04002975 goto get_ses_fail;
2976
2977 /* success, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302978 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002979 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302980 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002981
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002982 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002983
2984 cifs_setup_ipc(ses, volume_info);
2985
Jeff Layton36988c72010-04-24 07:57:43 -04002986 return ses;
2987
2988get_ses_fail:
2989 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002990 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002991 return ERR_PTR(rc);
2992}
2993
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002994static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002995{
2996 if (tcon->tidStatus == CifsExiting)
2997 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002998 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002999 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003000 if (tcon->seal != volume_info->seal)
3001 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003002 if (tcon->snapshot_time != volume_info->snapshot_time)
3003 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00003004 return 1;
3005}
3006
Steve French96daf2b2011-05-27 04:34:02 +00003007static struct cifs_tcon *
Steve French8b217fe2016-11-11 22:36:20 -06003008cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
3010 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00003011 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303013 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003014 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00003015 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003016 if (!match_tcon(tcon, volume_info))
Jeff Laytonf1987b42008-11-15 11:12:47 -05003017 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05003018 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303019 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 return tcon;
3021 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303022 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 return NULL;
3024}
3025
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07003026void
Steve French96daf2b2011-05-27 04:34:02 +00003027cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05003028{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003029 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01003030 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05003031
Aurelien Aptelb327a712018-01-24 13:46:10 +01003032 /*
3033 * IPC tcon share the lifetime of their session and are
3034 * destroyed in the session put function
3035 */
3036 if (tcon == NULL || tcon->ipc)
3037 return;
3038
3039 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05003040 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303041 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003042 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303043 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003044 return;
3045 }
3046
3047 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303048 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003049
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003050 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003051 if (ses->server->ops->tree_disconnect)
3052 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003053 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003054
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303055 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00003056 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05003057 cifs_put_smb_ses(ses);
3058}
3059
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003060/**
3061 * cifs_get_tcon - get a tcon matching @volume_info data from @ses
3062 *
3063 * - tcon refcount is the number of mount points using the tcon.
3064 * - ses refcount is the number of tcon using the session.
3065 *
3066 * 1. This function assumes it is being called from cifs_mount() where
3067 * we already got a session reference (ses refcount +1).
3068 *
3069 * 2. Since we're in the context of adding a mount point, the end
3070 * result should be either:
3071 *
3072 * a) a new tcon already allocated with refcount=1 (1 mount point) and
3073 * its session refcount incremented (1 new tcon). This +1 was
3074 * already done in (1).
3075 *
3076 * b) an existing tcon with refcount+1 (add a mount point to it) and
3077 * identical ses refcount (no new tcon). Because of (1) we need to
3078 * decrement the ses refcount.
3079 */
Steve French96daf2b2011-05-27 04:34:02 +00003080static struct cifs_tcon *
3081cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Jeff Laytond00c28d2010-04-24 07:57:44 -04003082{
3083 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00003084 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003085
Steve French8b217fe2016-11-11 22:36:20 -06003086 tcon = cifs_find_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003087 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003088 /*
3089 * tcon has refcount already incremented but we need to
3090 * decrement extra ses reference gotten by caller (case b)
3091 */
Joe Perchesf96637b2013-05-04 22:12:25 -05003092 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04003093 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003094 return tcon;
3095 }
3096
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003097 if (!ses->server->ops->tree_connect) {
3098 rc = -ENOSYS;
3099 goto out_fail;
3100 }
3101
Jeff Laytond00c28d2010-04-24 07:57:44 -04003102 tcon = tconInfoAlloc();
3103 if (tcon == NULL) {
3104 rc = -ENOMEM;
3105 goto out_fail;
3106 }
3107
Steve French8b217fe2016-11-11 22:36:20 -06003108 if (volume_info->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06003109 if (ses->server->vals->protocol_id == 0) {
3110 cifs_dbg(VFS,
3111 "Use SMB2 or later for snapshot mount option\n");
3112 rc = -EOPNOTSUPP;
3113 goto out_fail;
3114 } else
3115 tcon->snapshot_time = volume_info->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06003116 }
3117
Jeff Laytond00c28d2010-04-24 07:57:44 -04003118 tcon->ses = ses;
3119 if (volume_info->password) {
3120 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
3121 if (!tcon->password) {
3122 rc = -ENOMEM;
3123 goto out_fail;
3124 }
3125 }
3126
Steve French23657ad2018-04-22 15:14:58 -05003127 if (volume_info->seal) {
3128 if (ses->server->vals->protocol_id == 0) {
3129 cifs_dbg(VFS,
3130 "SMB3 or later required for encryption\n");
3131 rc = -EOPNOTSUPP;
3132 goto out_fail;
3133 } else if (tcon->ses->server->capabilities &
3134 SMB2_GLOBAL_CAP_ENCRYPTION)
3135 tcon->seal = true;
3136 else {
3137 cifs_dbg(VFS, "Encryption is not supported on share\n");
3138 rc = -EOPNOTSUPP;
3139 goto out_fail;
3140 }
3141 }
3142
Steve French8505c8b2018-06-18 14:01:59 -05003143 if (volume_info->linux_ext) {
3144 if (ses->server->posix_ext_supported) {
Steve Frenchb3266142018-05-20 23:41:10 -05003145 tcon->posix_extensions = true;
Steve French2fbb5642018-06-12 12:11:31 -05003146 printk_once(KERN_WARNING
3147 "SMB3.11 POSIX Extensions are experimental\n");
Steve French8505c8b2018-06-18 14:01:59 -05003148 } else {
3149 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions.\n");
3150 rc = -EOPNOTSUPP;
3151 goto out_fail;
Steve French2fbb5642018-06-12 12:11:31 -05003152 }
Steve Frenchb3266142018-05-20 23:41:10 -05003153 }
Steve Frenchb3266142018-05-20 23:41:10 -05003154
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003155 /*
3156 * BB Do we need to wrap session_mutex around this TCon call and Unix
3157 * SetFS as we do on SessSetup and reconnect?
3158 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003159 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003160 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
3161 volume_info->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003162 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05003163 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003164 if (rc)
3165 goto out_fail;
3166
3167 if (volume_info->nodfs) {
3168 tcon->Flags &= ~SMB_SHARE_IS_IN_DFS;
Joe Perchesf96637b2013-05-04 22:12:25 -05003169 cifs_dbg(FYI, "DFS disabled (%d)\n", tcon->Flags);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003170 }
Steve Frenchb618f002015-11-03 09:15:03 -06003171 tcon->use_persistent = false;
3172 /* check if SMB2 or later, CIFS does not support persistent handles */
3173 if (volume_info->persistent) {
3174 if (ses->server->vals->protocol_id == 0) {
3175 cifs_dbg(VFS,
3176 "SMB3 or later required for persistent handles\n");
3177 rc = -EOPNOTSUPP;
3178 goto out_fail;
3179 } else if (ses->server->capabilities &
3180 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3181 tcon->use_persistent = true;
3182 else /* persistent handles requested but not supported */ {
3183 cifs_dbg(VFS,
3184 "Persistent handles not supported on share\n");
3185 rc = -EOPNOTSUPP;
3186 goto out_fail;
3187 }
3188 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
3189 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3190 && (volume_info->nopersistent == false)) {
3191 cifs_dbg(FYI, "enabling persistent handles\n");
3192 tcon->use_persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06003193 } else if (volume_info->resilient) {
3194 if (ses->server->vals->protocol_id == 0) {
3195 cifs_dbg(VFS,
3196 "SMB2.1 or later required for resilient handles\n");
3197 rc = -EOPNOTSUPP;
3198 goto out_fail;
3199 }
3200 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06003201 }
3202
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003203 /*
3204 * We can have only one retry value for a connection to a share so for
3205 * resources mounted more than once to the same server share the last
3206 * value passed in for the retry flag is used.
3207 */
Jeff Laytond00c28d2010-04-24 07:57:44 -04003208 tcon->retry = volume_info->retry;
3209 tcon->nocase = volume_info->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05003210 tcon->nohandlecache = volume_info->nohandlecache;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003211 tcon->local_lease = volume_info->local_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07003212 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003213
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303214 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003215 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303216 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003217
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303218 cifs_fscache_get_super_cookie(tcon);
3219
Jeff Laytond00c28d2010-04-24 07:57:44 -04003220 return tcon;
3221
3222out_fail:
3223 tconInfoFree(tcon);
3224 return ERR_PTR(rc);
3225}
3226
Jeff Layton9d002df2010-10-06 19:51:11 -04003227void
3228cifs_put_tlink(struct tcon_link *tlink)
3229{
3230 if (!tlink || IS_ERR(tlink))
3231 return;
3232
3233 if (!atomic_dec_and_test(&tlink->tl_count) ||
3234 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
3235 tlink->tl_time = jiffies;
3236 return;
3237 }
3238
3239 if (!IS_ERR(tlink_tcon(tlink)))
3240 cifs_put_tcon(tlink_tcon(tlink));
3241 kfree(tlink);
3242 return;
3243}
Jeff Laytond00c28d2010-04-24 07:57:44 -04003244
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003245static int
3246compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3247{
3248 struct cifs_sb_info *old = CIFS_SB(sb);
3249 struct cifs_sb_info *new = mnt_data->cifs_sb;
3250
3251 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
3252 return 0;
3253
3254 if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
3255 (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
3256 return 0;
3257
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003258 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003259 * We want to share sb only if we don't specify an r/wsize or
3260 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003261 */
3262 if (new->wsize && new->wsize < old->wsize)
3263 return 0;
3264
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003265 if (new->rsize && new->rsize < old->rsize)
3266 return 0;
3267
Eric W. Biederman1f682332013-02-06 01:20:20 -08003268 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003269 return 0;
3270
3271 if (old->mnt_file_mode != new->mnt_file_mode ||
3272 old->mnt_dir_mode != new->mnt_dir_mode)
3273 return 0;
3274
3275 if (strcmp(old->local_nls->charset, new->local_nls->charset))
3276 return 0;
3277
3278 if (old->actimeo != new->actimeo)
3279 return 0;
3280
3281 return 1;
3282}
3283
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003284static int
3285match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3286{
3287 struct cifs_sb_info *old = CIFS_SB(sb);
3288 struct cifs_sb_info *new = mnt_data->cifs_sb;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003289 bool old_set = old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
3290 bool new_set = new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003291
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003292 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003293 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003294 else if (!old_set && !new_set)
3295 return 1;
3296
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003297 return 0;
3298}
3299
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003300int
3301cifs_match_super(struct super_block *sb, void *data)
3302{
3303 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
3304 struct smb_vol *volume_info;
3305 struct cifs_sb_info *cifs_sb;
3306 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00003307 struct cifs_ses *ses;
3308 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003309 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003310 int rc = 0;
3311
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003312 spin_lock(&cifs_tcp_ses_lock);
3313 cifs_sb = CIFS_SB(sb);
3314 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3315 if (IS_ERR(tlink)) {
3316 spin_unlock(&cifs_tcp_ses_lock);
3317 return rc;
3318 }
3319 tcon = tlink_tcon(tlink);
3320 ses = tcon->ses;
3321 tcp_srv = ses->server;
3322
3323 volume_info = mnt_data->vol;
3324
Jeff Layton9fa114f2012-11-26 11:09:57 -05003325 if (!match_server(tcp_srv, volume_info) ||
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003326 !match_session(ses, volume_info) ||
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003327 !match_tcon(tcon, volume_info) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003328 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003329 rc = 0;
3330 goto out;
3331 }
3332
3333 rc = compare_mount_options(sb, mnt_data);
3334out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003335 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04003336 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003337 return rc;
3338}
3339
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003341get_dfs_path(const unsigned int xid, struct cifs_ses *ses, const char *old_path,
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003342 const struct nls_table *nls_codepage, unsigned int *num_referrals,
3343 struct dfs_info3_param **referrals, int remap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 int rc = 0;
3346
Aurelien Aptelb327a712018-01-24 13:46:10 +01003347 if (!ses->server->ops->get_dfs_refer)
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003348 return -ENOSYS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003350 *num_referrals = 0;
3351 *referrals = NULL;
3352
Aurelien Aptelb327a712018-01-24 13:46:10 +01003353 rc = ses->server->ops->get_dfs_refer(xid, ses, old_path,
3354 referrals, num_referrals,
3355 nls_codepage, remap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 return rc;
3357}
3358
Jeff Layton09e50d52008-07-23 10:11:19 -04003359#ifdef CONFIG_DEBUG_LOCK_ALLOC
3360static struct lock_class_key cifs_key[2];
3361static struct lock_class_key cifs_slock_key[2];
3362
3363static inline void
3364cifs_reclassify_socket4(struct socket *sock)
3365{
3366 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003367 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003368 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3369 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3370}
3371
3372static inline void
3373cifs_reclassify_socket6(struct socket *sock)
3374{
3375 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003376 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003377 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3378 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3379}
3380#else
3381static inline void
3382cifs_reclassify_socket4(struct socket *sock)
3383{
3384}
3385
3386static inline void
3387cifs_reclassify_socket6(struct socket *sock)
3388{
3389}
3390#endif
3391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00003393static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394{
Steve French50c2f752007-07-13 00:33:32 +00003395 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396
Steve French50c2f752007-07-13 00:33:32 +00003397 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 /* mask a nibble at a time and encode */
3399 target[j] = 'A' + (0x0F & (source[i] >> 4));
3400 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00003401 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 }
3403
3404}
3405
Ben Greear3eb9a882010-09-01 17:06:02 -07003406static int
3407bind_socket(struct TCP_Server_Info *server)
3408{
3409 int rc = 0;
3410 if (server->srcaddr.ss_family != AF_UNSPEC) {
3411 /* Bind to the specified local IP address */
3412 struct socket *socket = server->ssocket;
3413 rc = socket->ops->bind(socket,
3414 (struct sockaddr *) &server->srcaddr,
3415 sizeof(server->srcaddr));
3416 if (rc < 0) {
3417 struct sockaddr_in *saddr4;
3418 struct sockaddr_in6 *saddr6;
3419 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3420 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3421 if (saddr6->sin6_family == AF_INET6)
Joe Perchesf96637b2013-05-04 22:12:25 -05003422 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
3423 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003424 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003425 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
3426 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003427 }
3428 }
3429 return rc;
3430}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431
3432static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003433ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434{
3435 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003436 /*
3437 * some servers require RFC1001 sessinit before sending
3438 * negprot - BB check reconnection in case where second
3439 * sessinit is sent but no second negprot
3440 */
3441 struct rfc1002_session_packet *ses_init_buf;
3442 struct smb_hdr *smb_buf;
3443 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3444 GFP_KERNEL);
3445 if (ses_init_buf) {
3446 ses_init_buf->trailer.session_req.called_len = 32;
3447
Colin Ian King997152f2016-01-25 16:25:54 +00003448 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003449 rfc1002mangle(ses_init_buf->trailer.
3450 session_req.called_name,
3451 server->server_RFC1001_name,
3452 RFC1001_NAME_LEN_WITH_NULL);
3453 else
3454 rfc1002mangle(ses_init_buf->trailer.
3455 session_req.called_name,
3456 DEFAULT_CIFS_CALLED_NAME,
3457 RFC1001_NAME_LEN_WITH_NULL);
3458
3459 ses_init_buf->trailer.session_req.calling_len = 32;
3460
3461 /*
3462 * calling name ends in null (byte 16) from old smb
3463 * convention.
3464 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05003465 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003466 rfc1002mangle(ses_init_buf->trailer.
3467 session_req.calling_name,
3468 server->workstation_RFC1001_name,
3469 RFC1001_NAME_LEN_WITH_NULL);
3470 else
3471 rfc1002mangle(ses_init_buf->trailer.
3472 session_req.calling_name,
3473 "LINUX_CIFS_CLNT",
3474 RFC1001_NAME_LEN_WITH_NULL);
3475
3476 ses_init_buf->trailer.session_req.scope1 = 0;
3477 ses_init_buf->trailer.session_req.scope2 = 0;
3478 smb_buf = (struct smb_hdr *)ses_init_buf;
3479
3480 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003481 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003482 rc = smb_send(server, smb_buf, 0x44);
3483 kfree(ses_init_buf);
3484 /*
3485 * RFC1001 layer in at least one server
3486 * requires very short break before negprot
3487 * presumably because not expecting negprot
3488 * to follow so fast. This is a simple
3489 * solution that works without
3490 * complicating the code and causes no
3491 * significant slowing down on mount
3492 * for everyone else
3493 */
3494 usleep_range(1000, 2000);
3495 }
3496 /*
3497 * else the negprot may still work without this
3498 * even though malloc failed
3499 */
3500
3501 return rc;
3502}
3503
3504static int
3505generic_ip_connect(struct TCP_Server_Info *server)
3506{
3507 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00003508 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003509 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003510 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003511 struct sockaddr *saddr;
3512
3513 saddr = (struct sockaddr *) &server->dstaddr;
3514
3515 if (server->dstaddr.ss_family == AF_INET6) {
3516 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3517 slen = sizeof(struct sockaddr_in6);
3518 sfamily = AF_INET6;
3519 } else {
3520 sport = ((struct sockaddr_in *) saddr)->sin_port;
3521 slen = sizeof(struct sockaddr_in);
3522 sfamily = AF_INET;
3523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003525 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06003526 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3527 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003529 cifs_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003530 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003533
3534 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05003535 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003536 server->ssocket = socket;
3537 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003538 if (sfamily == AF_INET6)
3539 cifs_reclassify_socket6(socket);
3540 else
3541 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 }
3543
Ben Greear3eb9a882010-09-01 17:06:02 -07003544 rc = bind_socket(server);
3545 if (rc < 0)
3546 return rc;
3547
Jeff Laytond5c56052008-12-01 18:42:33 -05003548 /*
3549 * Eventually check for other socket options to change from
3550 * the default. sock_setsockopt not used because it expects
3551 * user space buffer
3552 */
3553 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00003554 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00003555
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003556 /* make the bufsizes depend on wsize/rsize and max requests */
3557 if (server->noautotune) {
3558 if (socket->sk->sk_sndbuf < (200 * 1024))
3559 socket->sk->sk_sndbuf = 200 * 1024;
3560 if (socket->sk->sk_rcvbuf < (140 * 1024))
3561 socket->sk->sk_rcvbuf = 140 * 1024;
3562 }
3563
Steve French6a5fa2362010-01-01 01:28:43 +00003564 if (server->tcp_nodelay) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003565 int val = 1;
Steve French6a5fa2362010-01-01 01:28:43 +00003566 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3567 (char *)&val, sizeof(val));
3568 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003569 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3570 rc);
Steve French6a5fa2362010-01-01 01:28:43 +00003571 }
3572
Joe Perchesf96637b2013-05-04 22:12:25 -05003573 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003574 socket->sk->sk_sndbuf,
3575 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3576
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003577 rc = socket->ops->connect(socket, saddr, slen, 0);
3578 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003579 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003580 sock_release(socket);
3581 server->ssocket = NULL;
3582 return rc;
3583 }
3584
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003585 if (sport == htons(RFC1001_PORT))
3586 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00003587
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 return rc;
3589}
3590
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003591static int
3592ip_connect(struct TCP_Server_Info *server)
3593{
Steve French6da97912011-03-13 18:55:55 +00003594 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003595 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3596 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3597
3598 if (server->dstaddr.ss_family == AF_INET6)
3599 sport = &addr6->sin6_port;
3600 else
3601 sport = &addr->sin_port;
3602
3603 if (*sport == 0) {
3604 int rc;
3605
3606 /* try with 445 port at first */
3607 *sport = htons(CIFS_PORT);
3608
3609 rc = generic_ip_connect(server);
3610 if (rc >= 0)
3611 return rc;
3612
3613 /* if it failed, try with 139 port */
3614 *sport = htons(RFC1001_PORT);
3615 }
3616
3617 return generic_ip_connect(server);
3618}
3619
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003620void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Al Viro2c6292a2011-06-17 09:05:48 -04003621 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00003622{
3623 /* if we are reconnecting then should we check to see if
3624 * any requested capabilities changed locally e.g. via
3625 * remount but we can not do much about it here
3626 * if they have (even if we could detect it by the following)
3627 * Perhaps we could add a backpointer to array of sb from tcon
3628 * or if we change to make all sb to same share the same
3629 * sb as NFS - then we only have one backpointer to sb.
3630 * What if we wanted to mount the server share twice once with
3631 * and once without posixacls or posix paths? */
3632 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00003633
Steve Frenchc18c8422007-07-18 23:21:09 +00003634 if (vol_info && vol_info->no_linux_ext) {
3635 tcon->fsUnixInfo.Capability = 0;
3636 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05003637 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003638 return;
3639 } else if (vol_info)
3640 tcon->unix_ext = 1; /* Unix Extensions supported */
3641
3642 if (tcon->unix_ext == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003643 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003644 return;
3645 }
Steve French50c2f752007-07-13 00:33:32 +00003646
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003647 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00003648 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05003649 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve French8af18972007-02-14 04:42:51 +00003650 /* check for reconnect case in which we do not
3651 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003652 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00003653 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00003654 originally at mount time */
3655 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3656 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003657 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3658 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003659 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00003660 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003661 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003662 cifs_dbg(VFS, "possible reconnect error\n");
3663 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00003664 }
Steve French8af18972007-02-14 04:42:51 +00003665 }
Steve French50c2f752007-07-13 00:33:32 +00003666
Steve French6848b732011-05-26 18:38:54 +00003667 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003668 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00003669
Steve French8af18972007-02-14 04:42:51 +00003670 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00003671 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00003672 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003673 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003674 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003675 if (cifs_sb)
3676 cifs_sb->mnt_cifs_flags |=
3677 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00003678 }
3679
Steve French75865f8c2007-06-24 18:30:48 +00003680 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00003681 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003682 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003683 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003684 if (cifs_sb)
3685 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00003686 CIFS_MOUNT_POSIX_PATHS;
3687 }
Steve French50c2f752007-07-13 00:33:32 +00003688
Joe Perchesf96637b2013-05-04 22:12:25 -05003689 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00003690#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00003691 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003692 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003693 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003694 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003695 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003696 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003697 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003698 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003699 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003700 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003701 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003702 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003703 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003704 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00003705 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003706 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00003707 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003708 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00003709#endif /* CIFS_DEBUG2 */
3710 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00003711 if (vol_info == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003712 cifs_dbg(FYI, "resetting capabilities failed\n");
Steve French442aa312007-09-24 20:25:46 +00003713 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05003714 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 +00003715
Steve French8af18972007-02-14 04:42:51 +00003716 }
3717 }
3718}
3719
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003720int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003721 struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003722{
Jeff Layton2de970f2010-10-06 19:51:12 -04003723 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3724
Al Viro2ced6f62011-06-17 09:20:04 -04003725 spin_lock_init(&cifs_sb->tlink_tree_lock);
3726 cifs_sb->tlink_tree = RB_ROOT;
3727
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003728 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003729 * Temporarily set r/wsize for matching superblock. If we end up using
3730 * new sb then client will later negotiate it downward if needed.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003731 */
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003732 cifs_sb->rsize = pvolume_info->rsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003733 cifs_sb->wsize = pvolume_info->wsize;
3734
Steve French3b795212008-11-13 19:45:32 +00003735 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3736 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3737 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3738 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
Joe Perchesf96637b2013-05-04 22:12:25 -05003739 cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
3740 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
Steve French3b795212008-11-13 19:45:32 +00003741
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303742 cifs_sb->actimeo = pvolume_info->actimeo;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003743 cifs_sb->local_nls = pvolume_info->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303744
Steve French3b795212008-11-13 19:45:32 +00003745 if (pvolume_info->noperm)
3746 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3747 if (pvolume_info->setuids)
3748 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
Steve French95932652016-09-23 01:36:34 -05003749 if (pvolume_info->setuidfromacl)
3750 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
Steve French3b795212008-11-13 19:45:32 +00003751 if (pvolume_info->server_ino)
3752 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3753 if (pvolume_info->remap)
Steve French2baa2682014-09-27 02:19:01 -05003754 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3755 if (pvolume_info->sfu_remap)
Steve French3b795212008-11-13 19:45:32 +00003756 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3757 if (pvolume_info->no_xattr)
3758 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3759 if (pvolume_info->sfu_emul)
3760 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3761 if (pvolume_info->nobrl)
3762 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve French3d4ef9a2018-04-25 22:19:09 -05003763 if (pvolume_info->nohandlecache)
3764 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
Steve Frenchbe652442009-02-23 15:21:59 +00003765 if (pvolume_info->nostrictsync)
Steve French4717bed2009-02-24 14:44:19 +00003766 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
Steve French13a6e422008-12-02 17:24:33 +00003767 if (pvolume_info->mand_lock)
3768 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003769 if (pvolume_info->rwpidforward)
3770 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
Steve French3b795212008-11-13 19:45:32 +00003771 if (pvolume_info->cifs_acl)
3772 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003773 if (pvolume_info->backupuid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003774 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003775 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3776 }
3777 if (pvolume_info->backupgid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003778 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003779 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3780 }
Steve French3b795212008-11-13 19:45:32 +00003781 if (pvolume_info->override_uid)
3782 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3783 if (pvolume_info->override_gid)
3784 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3785 if (pvolume_info->dynperm)
3786 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05303787 if (pvolume_info->fsc)
3788 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
Jeff Layton0eb8a132010-10-06 19:51:12 -04003789 if (pvolume_info->multiuser)
3790 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3791 CIFS_MOUNT_NO_PERM);
Pavel Shilovskyd39454f2011-01-24 14:16:35 -05003792 if (pvolume_info->strict_io)
3793 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
Steve French3b795212008-11-13 19:45:32 +00003794 if (pvolume_info->direct_io) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003795 cifs_dbg(FYI, "mounting share using direct i/o\n");
Steve French3b795212008-11-13 19:45:32 +00003796 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3797 }
Stefan Metzmacher736a3322010-07-30 14:56:00 +02003798 if (pvolume_info->mfsymlinks) {
3799 if (pvolume_info->sfu_emul) {
Steve Frenchdb8b6312014-09-22 05:13:55 -05003800 /*
3801 * Our SFU ("Services for Unix" emulation does not allow
3802 * creating symlinks but does allow reading existing SFU
3803 * symlinks (it does allow both creating and reading SFU
3804 * style mknod and FIFOs though). When "mfsymlinks" and
3805 * "sfu" are both enabled at the same time, it allows
3806 * reading both types of symlinks, but will only create
3807 * them with mfsymlinks format. This allows better
3808 * Apple compatibility (probably better for Samba too)
3809 * while still recognizing old Windows style symlinks.
3810 */
3811 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
Stefan Metzmacher736a3322010-07-30 14:56:00 +02003812 }
Steve Frenchdb8b6312014-09-22 05:13:55 -05003813 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
Stefan Metzmacher736a3322010-07-30 14:56:00 +02003814 }
Steve French3b795212008-11-13 19:45:32 +00003815
3816 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05003817 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003818
3819 if (pvolume_info->prepath) {
3820 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
3821 if (cifs_sb->prepath == NULL)
3822 return -ENOMEM;
3823 }
3824
3825 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003826}
3827
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003828static void
3829cleanup_volume_info_contents(struct smb_vol *volume_info)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003830{
Sean Finneyb9468452011-04-11 13:19:32 +00003831 kfree(volume_info->username);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003832 kzfree(volume_info->password);
Jesper Juhl95c75452011-08-27 18:58:34 +02003833 kfree(volume_info->UNC);
Sean Finneyb9468452011-04-11 13:19:32 +00003834 kfree(volume_info->domainname);
3835 kfree(volume_info->iocharset);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003836 kfree(volume_info->prepath);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003837}
3838
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003839void
3840cifs_cleanup_volume_info(struct smb_vol *volume_info)
3841{
3842 if (!volume_info)
3843 return;
3844 cleanup_volume_info_contents(volume_info);
3845 kfree(volume_info);
3846}
3847
3848
Steve French2d6d5892009-04-09 00:36:44 +00003849#ifdef CONFIG_CIFS_DFS_UPCALL
Steve French6d3ea7e2012-11-28 22:34:41 -06003850/*
3851 * cifs_build_path_to_root returns full path to root when we do not have an
3852 * exiting connection (tcon)
3853 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003854static char *
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003855build_unc_path_to_root(const struct smb_vol *vol,
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003856 const struct cifs_sb_info *cifs_sb)
3857{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003858 char *full_path, *pos;
Jeff Layton839db3d2012-12-10 06:10:45 -05003859 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003860 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003861
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003862 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003863 if (full_path == NULL)
3864 return ERR_PTR(-ENOMEM);
3865
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003866 strncpy(full_path, vol->UNC, unc_len);
3867 pos = full_path + unc_len;
3868
3869 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04003870 *pos = CIFS_DIR_SEP(cifs_sb);
3871 strncpy(pos + 1, vol->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003872 pos += pplen;
3873 }
3874
3875 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00003876 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05003877 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003878 return full_path;
3879}
Sean Finneydd613942011-04-11 13:19:30 +00003880
3881/*
3882 * Perform a dfs referral query for a share and (optionally) prefix
3883 *
Sean Finney046462a2011-04-11 13:19:33 +00003884 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3885 * to a string containing updated options for the submount. Otherwise it
3886 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00003887 *
3888 * Returns the rc from get_dfs_path to the caller, which can be used to
3889 * determine whether there were referrals.
3890 */
3891static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003892expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Sean Finneydd613942011-04-11 13:19:30 +00003893 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
Sean Finney046462a2011-04-11 13:19:33 +00003894 int check_prefix)
Sean Finneydd613942011-04-11 13:19:30 +00003895{
3896 int rc;
3897 unsigned int num_referrals = 0;
3898 struct dfs_info3_param *referrals = NULL;
3899 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3900
3901 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3902 if (IS_ERR(full_path))
3903 return PTR_ERR(full_path);
3904
3905 /* For DFS paths, skip the first '\' of the UNC */
3906 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3907
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003908 rc = get_dfs_path(xid, ses, ref_path, cifs_sb->local_nls,
Steve French2baa2682014-09-27 02:19:01 -05003909 &num_referrals, &referrals, cifs_remap(cifs_sb));
Sean Finneydd613942011-04-11 13:19:30 +00003910
3911 if (!rc && num_referrals > 0) {
3912 char *fake_devname = NULL;
3913
3914 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
3915 full_path + 1, referrals,
3916 &fake_devname);
3917
3918 free_dfs_info_array(referrals, num_referrals);
Sean Finney046462a2011-04-11 13:19:33 +00003919
Sean Finneydd613942011-04-11 13:19:30 +00003920 if (IS_ERR(mdata)) {
3921 rc = PTR_ERR(mdata);
3922 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003923 } else {
3924 cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003925 rc = cifs_setup_volume_info(volume_info, mdata,
Steve Frenchc7c137b2018-06-06 17:59:29 -05003926 fake_devname, false);
Sean Finneydd613942011-04-11 13:19:30 +00003927 }
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003928 kfree(fake_devname);
3929 kfree(cifs_sb->mountdata);
Sean Finney046462a2011-04-11 13:19:33 +00003930 cifs_sb->mountdata = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00003931 }
3932 kfree(full_path);
3933 return rc;
3934}
Steve French2d6d5892009-04-09 00:36:44 +00003935#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003936
Jeff Layton04db79b2011-07-06 08:10:38 -04003937static int
3938cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
Steve Frenchc7c137b2018-06-06 17:59:29 -05003939 const char *devname, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940{
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003941 int rc = 0;
Sean Finneydd613942011-04-11 13:19:30 +00003942
Steve Frenchc7c137b2018-06-06 17:59:29 -05003943 if (cifs_parse_mount_options(mount_data, devname, volume_info, is_smb3))
Jeff Layton04db79b2011-07-06 08:10:38 -04003944 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
Jeff Layton7586b762008-12-01 18:41:49 -05003946 if (volume_info->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003947 cifs_dbg(FYI, "Anonymous login\n");
Jeff Layton04febab2012-01-17 16:09:15 -05003948 kfree(volume_info->username);
3949 volume_info->username = NULL;
Jeff Layton7586b762008-12-01 18:41:49 -05003950 } else if (volume_info->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 /* BB fixme parse for domain name here */
Joe Perchesf96637b2013-05-04 22:12:25 -05003952 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003954 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00003955 /* In userspace mount helper we can get user name from alternate
3956 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04003957 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 }
3959
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 /* this is needed for ASCII cp to Unicode converts */
Jeff Layton7586b762008-12-01 18:41:49 -05003961 if (volume_info->iocharset == NULL) {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04003962 /* load_nls_default cannot return null */
3963 volume_info->local_nls = load_nls_default();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964 } else {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04003965 volume_info->local_nls = load_nls(volume_info->iocharset);
3966 if (volume_info->local_nls == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003967 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00003968 volume_info->iocharset);
Jeff Layton04db79b2011-07-06 08:10:38 -04003969 return -ELIBACC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 }
3971 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003972
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003973 return rc;
Jeff Layton04db79b2011-07-06 08:10:38 -04003974}
3975
3976struct smb_vol *
Steve Frenchc7c137b2018-06-06 17:59:29 -05003977cifs_get_volume_info(char *mount_data, const char *devname, bool is_smb3)
Jeff Layton04db79b2011-07-06 08:10:38 -04003978{
3979 int rc;
3980 struct smb_vol *volume_info;
3981
Jeff Layton6ee95422012-11-26 11:09:57 -05003982 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
Jeff Layton04db79b2011-07-06 08:10:38 -04003983 if (!volume_info)
3984 return ERR_PTR(-ENOMEM);
3985
Steve Frenchc7c137b2018-06-06 17:59:29 -05003986 rc = cifs_setup_volume_info(volume_info, mount_data, devname, is_smb3);
Jeff Layton04db79b2011-07-06 08:10:38 -04003987 if (rc) {
3988 cifs_cleanup_volume_info(volume_info);
3989 volume_info = ERR_PTR(rc);
3990 }
3991
3992 return volume_info;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003993}
3994
Aurelien Aptela6b50582016-05-25 19:59:09 +02003995static int
3996cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
3997 unsigned int xid,
3998 struct cifs_tcon *tcon,
3999 struct cifs_sb_info *cifs_sb,
4000 char *full_path)
4001{
4002 int rc;
4003 char *s;
4004 char sep, tmp;
4005
4006 sep = CIFS_DIR_SEP(cifs_sb);
4007 s = full_path;
4008
4009 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
4010 while (rc == 0) {
4011 /* skip separators */
4012 while (*s == sep)
4013 s++;
4014 if (!*s)
4015 break;
4016 /* next separator */
4017 while (*s && *s != sep)
4018 s++;
4019
4020 /*
4021 * temporarily null-terminate the path at the end of
4022 * the current component
4023 */
4024 tmp = *s;
4025 *s = 0;
4026 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4027 full_path);
4028 *s = tmp;
4029 }
4030 return rc;
4031}
4032
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004033int
Al Viro2c6292a2011-06-17 09:05:48 -04004034cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *volume_info)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004035{
Jeff Layton1daaae82012-03-21 06:30:40 -04004036 int rc;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004037 unsigned int xid;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004038 struct cifs_ses *ses;
Steve French96daf2b2011-05-27 04:34:02 +00004039 struct cifs_tcon *tcon;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004040 struct TCP_Server_Info *server;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004041 char *full_path;
4042 struct tcon_link *tlink;
4043#ifdef CONFIG_CIFS_DFS_UPCALL
4044 int referral_walks_count = 0;
Jeff Layton20547492011-07-09 12:21:07 -04004045#endif
Al Virodd854462011-06-17 08:24:42 -04004046
Jeff Layton20547492011-07-09 12:21:07 -04004047#ifdef CONFIG_CIFS_DFS_UPCALL
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004048try_mount_again:
4049 /* cleanup activities if we're chasing a referral */
4050 if (referral_walks_count) {
4051 if (tcon)
4052 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004053 else if (ses)
4054 cifs_put_smb_ses(ses);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004055
Sachin Prabhu1dfd18d2015-06-16 16:36:17 +01004056 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
4057
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004058 free_xid(xid);
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004059 }
4060#endif
Jeff Layton1daaae82012-03-21 06:30:40 -04004061 rc = 0;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004062 tcon = NULL;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004063 ses = NULL;
4064 server = NULL;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004065 full_path = NULL;
4066 tlink = NULL;
4067
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004068 xid = get_xid();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069
Jeff Layton63c038c2008-12-01 18:41:46 -05004070 /* get a reference to a tcp session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004071 server = cifs_get_tcp_session(volume_info);
4072 if (IS_ERR(server)) {
4073 rc = PTR_ERR(server);
Jeff Layton63c038c2008-12-01 18:41:46 -05004074 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 }
Steve French141891f2016-09-23 00:44:16 -05004076 if ((volume_info->max_credits < 20) ||
4077 (volume_info->max_credits > 60000))
4078 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
4079 else
4080 server->max_credits = volume_info->max_credits;
Jeff Layton36988c72010-04-24 07:57:43 -04004081 /* get a reference to a SMB session */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004082 ses = cifs_get_smb_ses(server, volume_info);
4083 if (IS_ERR(ses)) {
4084 rc = PTR_ERR(ses);
4085 ses = NULL;
Jeff Layton36988c72010-04-24 07:57:43 -04004086 goto mount_fail_check;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 }
Steve French50c2f752007-07-13 00:33:32 +00004088
Steve Frenchb618f002015-11-03 09:15:03 -06004089 if ((volume_info->persistent == true) && ((ses->server->capabilities &
4090 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES) == 0)) {
4091 cifs_dbg(VFS, "persistent handles not supported by server\n");
4092 rc = -EOPNOTSUPP;
4093 goto mount_fail_check;
4094 }
Steve French592fafe2015-11-03 10:08:53 -06004095
Jeff Laytond00c28d2010-04-24 07:57:44 -04004096 /* search for existing tcon to this server share */
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004097 tcon = cifs_get_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04004098 if (IS_ERR(tcon)) {
4099 rc = PTR_ERR(tcon);
4100 tcon = NULL;
Mark Syms40920c22016-11-29 11:36:46 +00004101 if (rc == -EACCES)
4102 goto mount_fail_check;
4103
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004104 goto remote_path_check;
Jeff Laytond00c28d2010-04-24 07:57:44 -04004105 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004106
Steve Frenchce558b02018-05-31 19:16:54 -05004107 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4108 if (tcon->posix_extensions)
4109 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
Steve Frenchce558b02018-05-31 19:16:54 -05004110
Steve French6848b732011-05-26 18:38:54 +00004111 /* tell server which Unix caps we support */
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004112 if (cap_unix(tcon->ses)) {
Steve French6848b732011-05-26 18:38:54 +00004113 /* reset of caps checks mount to see if unix extensions
4114 disabled for just this mount */
Al Viro2c6292a2011-06-17 09:05:48 -04004115 reset_cifs_unix_caps(xid, tcon, cifs_sb, volume_info);
Steve French6848b732011-05-26 18:38:54 +00004116 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
4117 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
4118 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)) {
4119 rc = -EACCES;
4120 goto mount_fail_check;
4121 }
4122 } else
4123 tcon->unix_ext = 0; /* server does not support them */
4124
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004125 /* do not care if a following call succeed - informational */
Aurelien Aptelb327a712018-01-24 13:46:10 +01004126 if (!tcon->pipe && server->ops->qfs_tcon)
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004127 server->ops->qfs_tcon(xid, tcon);
Steve Frenchd82c2df2008-11-15 00:07:26 +00004128
Pavel Shilovsky24985c52012-09-18 16:20:28 -07004129 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, volume_info);
4130 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, volume_info);
Jeff Laytonf7910cb2011-05-19 16:22:58 -04004131
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004132remote_path_check:
Sean Finneyc1508ca2011-04-11 13:19:31 +00004133#ifdef CONFIG_CIFS_DFS_UPCALL
4134 /*
4135 * Perform an unconditional check for whether there are DFS
4136 * referrals for this path without prefix, to provide support
4137 * for DFS referrals from w2k8 servers which don't seem to respond
4138 * with PATH_NOT_COVERED to requests that include the prefix.
4139 * Chase the referral if found, otherwise continue normally.
4140 */
4141 if (referral_walks_count == 0) {
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004142 int refrc = expand_dfs_referral(xid, ses, volume_info, cifs_sb,
4143 false);
Sean Finneyc1508ca2011-04-11 13:19:31 +00004144 if (!refrc) {
4145 referral_walks_count++;
4146 goto try_mount_again;
4147 }
4148 }
4149#endif
4150
Steve Frenchf87d39d2011-05-27 03:50:55 +00004151 /* check if a whole path is not remote */
Jeff Layton70945642011-03-14 13:48:08 -04004152 if (!rc && tcon) {
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004153 if (!server->ops->is_path_accessible) {
4154 rc = -ENOSYS;
4155 goto mount_fail_check;
4156 }
Steve French6d3ea7e2012-11-28 22:34:41 -06004157 /*
4158 * cifs_build_path_to_root works only when we have a valid tcon
4159 */
Sachin Prabhu374402a2016-12-15 12:31:19 +05304160 full_path = cifs_build_path_to_root(volume_info, cifs_sb, tcon,
4161 tcon->Flags & SMB_SHARE_IS_IN_DFS);
Igor Mammedove4cce942009-02-10 14:10:26 +03004162 if (full_path == NULL) {
4163 rc = -ENOMEM;
4164 goto mount_fail_check;
4165 }
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004166 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4167 full_path);
Jeff Layton03ceace2010-12-06 21:07:33 -05004168 if (rc != 0 && rc != -EREMOTE) {
Igor Mammedove4cce942009-02-10 14:10:26 +03004169 kfree(full_path);
4170 goto mount_fail_check;
4171 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02004172
Sachin Prabhud1713562016-09-06 13:22:34 +01004173 if (rc != -EREMOTE) {
4174 rc = cifs_are_all_path_components_accessible(server,
Aurelien Aptela6b50582016-05-25 19:59:09 +02004175 xid, tcon, cifs_sb,
4176 full_path);
Sachin Prabhud1713562016-09-06 13:22:34 +01004177 if (rc != 0) {
4178 cifs_dbg(VFS, "cannot query dirs between root and final path, "
4179 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
4180 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4181 rc = 0;
4182 }
Aurelien Aptela6b50582016-05-25 19:59:09 +02004183 }
Igor Mammedove4cce942009-02-10 14:10:26 +03004184 kfree(full_path);
4185 }
4186
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004187 /* get referral if needed */
4188 if (rc == -EREMOTE) {
Steve Frenchd036f502009-04-03 03:12:08 +00004189#ifdef CONFIG_CIFS_DFS_UPCALL
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004190 if (referral_walks_count > MAX_NESTED_LINKS) {
4191 /*
4192 * BB: when we implement proper loop detection,
4193 * we will remove this check. But now we need it
4194 * to prevent an indefinite loop if 'DFS tree' is
4195 * misconfigured (i.e. has loops).
4196 */
4197 rc = -ELOOP;
4198 goto mount_fail_check;
4199 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004200
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004201 rc = expand_dfs_referral(xid, ses, volume_info, cifs_sb, true);
Jeff Layton7b91e262009-07-23 15:22:30 -04004202
Sean Finneydd613942011-04-11 13:19:30 +00004203 if (!rc) {
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004204 referral_walks_count++;
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004205 goto try_mount_again;
4206 }
Sean Finneydd613942011-04-11 13:19:30 +00004207 goto mount_fail_check;
Steve Frenchd036f502009-04-03 03:12:08 +00004208#else /* No DFS support, return error on mount */
4209 rc = -EOPNOTSUPP;
4210#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004211 }
4212
Jeff Layton9d002df2010-10-06 19:51:11 -04004213 if (rc)
4214 goto mount_fail_check;
4215
4216 /* now, hang the tcon off of the superblock */
4217 tlink = kzalloc(sizeof *tlink, GFP_KERNEL);
4218 if (tlink == NULL) {
4219 rc = -ENOMEM;
4220 goto mount_fail_check;
4221 }
4222
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004223 tlink->tl_uid = ses->linux_uid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004224 tlink->tl_tcon = tcon;
4225 tlink->tl_time = jiffies;
4226 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
4227 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4228
Jeff Layton413e6612010-10-28 13:33:38 -04004229 cifs_sb->master_tlink = tlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004230 spin_lock(&cifs_sb->tlink_tree_lock);
4231 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4232 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton413e6612010-10-28 13:33:38 -04004233
Jeff Laytonda472fc2012-03-23 14:40:53 -04004234 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004235 TLINK_IDLE_EXPIRE);
4236
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004237mount_fail_check:
4238 /* on error free sesinfo and tcon struct if needed */
4239 if (rc) {
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004240 /* If find_unc succeeded then rc == 0 so we can not end */
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004241 /* up accidentally freeing someone elses tcon struct */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004242 if (tcon)
4243 cifs_put_tcon(tcon);
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004244 else if (ses)
4245 cifs_put_smb_ses(ses);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004246 else
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07004247 cifs_put_tcp_session(server, 0);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004248 }
4249
Jeff Layton70fe7dc2007-11-16 22:21:07 +00004250out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004251 free_xid(xid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 return rc;
4253}
4254
Jeff Layton8d1bca32011-06-11 21:17:10 -04004255/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01004256 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04004257 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04004259CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00004260 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 const struct nls_table *nls_codepage)
4262{
4263 struct smb_hdr *smb_buffer;
4264 struct smb_hdr *smb_buffer_response;
4265 TCONX_REQ *pSMB;
4266 TCONX_RSP *pSMBr;
4267 unsigned char *bcc_ptr;
4268 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05004269 int length;
4270 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
4272 if (ses == NULL)
4273 return -EIO;
4274
4275 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00004276 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00004278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 smb_buffer_response = smb_buffer;
4280
4281 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
4282 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07004283
Pavel Shilovsky88257362012-05-23 14:01:59 +04004284 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 smb_buffer->Uid = ses->Suid;
4286 pSMB = (TCONX_REQ *) smb_buffer;
4287 pSMBr = (TCONX_RSP *) smb_buffer_response;
4288
4289 pSMB->AndXCommand = 0xFF;
4290 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01004292 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08004293 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00004294 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08004295 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00004296 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08004297 } else {
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004298 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08004299 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
4300 specified as required (when that support is added to
4301 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00004302 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08004303 by Samba (not sure whether other servers allow
4304 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00004305#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton04912d62010-04-24 07:57:45 -04004306 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
Jeff Layton3f618222013-06-12 19:52:14 -05004307 (ses->sectype == LANMAN))
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -05004308 calc_lanman_hash(tcon->password, ses->server->cryptkey,
Steve French96daf2b2011-05-27 04:34:02 +00004309 ses->server->sec_mode &
Jeff Layton4e53a3f2008-12-05 20:41:21 -05004310 SECMODE_PW_ENCRYPT ? true : false,
4311 bcc_ptr);
Steve French7c7b25b2006-06-01 19:20:10 +00004312 else
4313#endif /* CIFS_WEAK_PW_HASH */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -06004314 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -05004315 bcc_ptr, nls_codepage);
Steve Frenchf3a31a22015-03-26 19:23:20 -05004316 if (rc) {
4317 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
4318 __func__, rc);
4319 cifs_buf_release(smb_buffer);
4320 return rc;
4321 }
Steve Frencheeac8042006-01-13 21:34:58 -08004322
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004323 bcc_ptr += CIFS_AUTH_RESP_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004324 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00004325 /* must align unicode strings */
4326 *bcc_ptr = 0; /* null byte password */
4327 bcc_ptr++;
4328 }
Steve Frencheeac8042006-01-13 21:34:58 -08004329 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
Jeff Layton38d77c52013-05-26 07:01:00 -04004331 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4333
4334 if (ses->capabilities & CAP_STATUS32) {
4335 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
4336 }
4337 if (ses->capabilities & CAP_DFS) {
4338 smb_buffer->Flags2 |= SMBFLG2_DFS;
4339 }
4340 if (ses->capabilities & CAP_UNICODE) {
4341 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
4342 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06004343 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00004344 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00004345 (/* server len*/ + 256 /* share len */), nls_codepage);
4346 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 bcc_ptr += 2; /* skip trailing null */
4348 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 strcpy(bcc_ptr, tree);
4350 bcc_ptr += strlen(tree) + 1;
4351 }
4352 strcpy(bcc_ptr, "?????");
4353 bcc_ptr += strlen("?????");
4354 bcc_ptr += 1;
4355 count = bcc_ptr - &pSMB->Password[0];
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004356 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
4357 pSMB->hdr.smb_buf_length) + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 pSMB->ByteCount = cpu_to_le16(count);
4359
Steve French133672e2007-11-13 22:41:37 +00004360 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05004361 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01004364 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00004365 bool is_unicode;
4366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00004368 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 tcon->tid = smb_buffer_response->Tid;
4370 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05004371 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004372 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00004373 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
4374 is_unicode = true;
4375 else
4376 is_unicode = false;
4377
Jeff Laytoncc20c032009-04-30 07:16:21 -04004378
Steve French50c2f752007-07-13 00:33:32 +00004379 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00004380 if (length == 3) {
4381 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
4382 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004383 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01004384 tcon->ipc = true;
4385 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00004386 }
4387 } else if (length == 2) {
4388 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
4389 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05004390 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00004391 }
4392 }
Steve French50c2f752007-07-13 00:33:32 +00004393 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04004394 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05004395 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04004396
4397 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04004398 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06004399 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00004400 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04004401 nls_codepage);
4402
Joe Perchesf96637b2013-05-04 22:12:25 -05004403 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004404
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004405 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00004406 (smb_buffer_response->WordCount == 7))
4407 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00004408 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4409 else
4410 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05004411 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 }
4413
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00004414 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 return rc;
4416}
4417
Al Viro2e32cf52013-10-03 12:53:37 -04004418static void delayed_free(struct rcu_head *p)
4419{
4420 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
4421 unload_nls(sbi->local_nls);
4422 kfree(sbi);
4423}
4424
Al Viro2a9b9952011-06-17 09:27:16 -04004425void
4426cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427{
Jeff Laytonb647c352010-10-28 11:16:44 -04004428 struct rb_root *root = &cifs_sb->tlink_tree;
4429 struct rb_node *node;
4430 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
Jeff Layton2de970f2010-10-06 19:51:12 -04004432 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4433
Jeff Laytonb647c352010-10-28 11:16:44 -04004434 spin_lock(&cifs_sb->tlink_tree_lock);
4435 while ((node = rb_first(root))) {
4436 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4437 cifs_get_tlink(tlink);
4438 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4439 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00004440
Jeff Laytonb647c352010-10-28 11:16:44 -04004441 spin_unlock(&cifs_sb->tlink_tree_lock);
4442 cifs_put_tlink(tlink);
4443 spin_lock(&cifs_sb->tlink_tree_lock);
4444 }
4445 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004446
Al Virod757d712011-06-17 09:42:43 -04004447 kfree(cifs_sb->mountdata);
Aurelien Aptela6b50582016-05-25 19:59:09 +02004448 kfree(cifs_sb->prepath);
Al Viro2e32cf52013-10-03 12:53:37 -04004449 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00004450}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004452int
4453cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454{
4455 int rc = 0;
Jeff Layton198b5682010-04-24 07:57:48 -04004456 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004458 if (!server->ops->need_neg || !server->ops->negotiate)
4459 return -ENOSYS;
4460
Jeff Layton198b5682010-04-24 07:57:48 -04004461 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004462 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04004463 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
Pavel Shilovsky45275782012-05-17 17:53:29 +04004465 set_credits(server, 1);
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004466
4467 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04004468 if (rc == 0) {
4469 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04004470 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04004471 server->tcpStatus = CifsGood;
4472 else
4473 rc = -EHOSTDOWN;
4474 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 }
Steve French26b994f2008-08-06 05:11:33 +00004476
Jeff Layton198b5682010-04-24 07:57:48 -04004477 return rc;
4478}
Steve French26b994f2008-08-06 05:11:33 +00004479
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004480int
4481cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4482 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04004483{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004484 int rc = -ENOSYS;
Jeff Layton198b5682010-04-24 07:57:48 -04004485 struct TCP_Server_Info *server = ses->server;
4486
Jeff Layton198b5682010-04-24 07:57:48 -04004487 ses->capabilities = server->capabilities;
Steve French26b994f2008-08-06 05:11:33 +00004488 if (linuxExtEnabled == 0)
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004489 ses->capabilities &= (~server->vals->cap_unix);
Steve French20418ac2009-04-30 16:13:32 +00004490
Joe Perchesf96637b2013-05-04 22:12:25 -05004491 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00004492 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04004493
Shu Wangf5c4ba82017-09-08 18:48:33 +08004494 if (ses->auth_key.response) {
Steve French2a182872018-03-29 12:16:34 -05004495 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
Shu Wangf5c4ba82017-09-08 18:48:33 +08004496 ses->auth_key.response);
4497 kfree(ses->auth_key.response);
4498 ses->auth_key.response = NULL;
4499 ses->auth_key.len = 0;
4500 }
4501
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004502 if (server->ops->sess_setup)
4503 rc = server->ops->sess_setup(xid, ses, nls_info);
4504
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05004505 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004506 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05004507
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 return rc;
4509}
4510
Jeff Layton8a8798a2012-01-17 16:09:15 -05004511static int
4512cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4513{
Jeff Layton3f618222013-06-12 19:52:14 -05004514 vol->sectype = ses->sectype;
4515
4516 /* krb5 is special, since we don't need username or pw */
4517 if (vol->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05004518 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05004519
4520 return cifs_set_cifscreds(vol, ses);
4521}
4522
Steve French96daf2b2011-05-27 04:34:02 +00004523static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004524cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04004525{
Jeff Layton8a8798a2012-01-17 16:09:15 -05004526 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00004527 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4528 struct cifs_ses *ses;
4529 struct cifs_tcon *tcon = NULL;
Jeff Layton9d002df2010-10-06 19:51:11 -04004530 struct smb_vol *vol_info;
Jeff Layton9d002df2010-10-06 19:51:11 -04004531
4532 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
Dan Carpenter803ab972012-01-24 11:39:22 +03004533 if (vol_info == NULL)
4534 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04004535
Jeff Layton9d002df2010-10-06 19:51:11 -04004536 vol_info->local_nls = cifs_sb->local_nls;
4537 vol_info->linux_uid = fsuid;
4538 vol_info->cred_uid = fsuid;
4539 vol_info->UNC = master_tcon->treeName;
4540 vol_info->retry = master_tcon->retry;
4541 vol_info->nocase = master_tcon->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05004542 vol_info->nohandlecache = master_tcon->nohandlecache;
Jeff Layton9d002df2010-10-06 19:51:11 -04004543 vol_info->local_lease = master_tcon->local_lease;
4544 vol_info->no_linux_ext = !master_tcon->unix_ext;
Jeff Layton28e11bd2013-05-26 07:01:00 -04004545 vol_info->sectype = master_tcon->ses->sectype;
4546 vol_info->sign = master_tcon->ses->sign;
Jeff Layton9d002df2010-10-06 19:51:11 -04004547
Jeff Layton8a8798a2012-01-17 16:09:15 -05004548 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4549 if (rc) {
4550 tcon = ERR_PTR(rc);
4551 goto out;
4552 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004553
4554 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304555 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004556 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304557 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004558
4559 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4560 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00004561 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07004562 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04004563 goto out;
4564 }
4565
4566 tcon = cifs_get_tcon(ses, vol_info);
4567 if (IS_ERR(tcon)) {
4568 cifs_put_smb_ses(ses);
4569 goto out;
4570 }
4571
Steve Frenchce558b02018-05-31 19:16:54 -05004572 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4573 if (tcon->posix_extensions)
4574 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
Steve French0fdfef92018-06-28 19:30:23 -05004575
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004576 if (cap_unix(ses))
Jeff Layton9d002df2010-10-06 19:51:11 -04004577 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
Steve Frenchb3266142018-05-20 23:41:10 -05004578
Jeff Layton9d002df2010-10-06 19:51:11 -04004579out:
Jeff Layton8a8798a2012-01-17 16:09:15 -05004580 kfree(vol_info->username);
Aurelien Aptel97f4b722018-01-25 15:59:39 +01004581 kzfree(vol_info->password);
Jeff Layton9d002df2010-10-06 19:51:11 -04004582 kfree(vol_info);
4583
4584 return tcon;
4585}
4586
Steve French96daf2b2011-05-27 04:34:02 +00004587struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04004588cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4589{
4590 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4591}
4592
Jeff Laytonb647c352010-10-28 11:16:44 -04004593/* find and return a tlink with given uid */
4594static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004595tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04004596{
4597 struct rb_node *node = root->rb_node;
4598 struct tcon_link *tlink;
4599
4600 while (node) {
4601 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4602
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004603 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004604 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004605 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004606 node = node->rb_right;
4607 else
4608 return tlink;
4609 }
4610 return NULL;
4611}
4612
4613/* insert a tcon_link into the tree */
4614static void
4615tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4616{
4617 struct rb_node **new = &(root->rb_node), *parent = NULL;
4618 struct tcon_link *tlink;
4619
4620 while (*new) {
4621 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4622 parent = *new;
4623
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004624 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004625 new = &((*new)->rb_left);
4626 else
4627 new = &((*new)->rb_right);
4628 }
4629
4630 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4631 rb_insert_color(&new_tlink->tl_rbnode, root);
4632}
4633
Jeff Layton9d002df2010-10-06 19:51:11 -04004634/*
4635 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4636 * current task.
4637 *
4638 * If the superblock doesn't refer to a multiuser mount, then just return
4639 * the master tcon for the mount.
4640 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05304641 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04004642 * exists, then check to see if it's pending construction. If it is then wait
4643 * for construction to complete. Once it's no longer pending, check to see if
4644 * it failed and either return an error or retry construction, depending on
4645 * the timeout.
4646 *
4647 * If one doesn't exist then insert a new tcon_link struct into the tree and
4648 * try to construct a new one.
4649 */
4650struct tcon_link *
4651cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4652{
4653 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004654 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04004655 struct tcon_link *tlink, *newtlink;
4656
4657 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4658 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4659
4660 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004661 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004662 if (tlink)
4663 cifs_get_tlink(tlink);
4664 spin_unlock(&cifs_sb->tlink_tree_lock);
4665
4666 if (tlink == NULL) {
4667 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4668 if (newtlink == NULL)
4669 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04004670 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004671 newtlink->tl_tcon = ERR_PTR(-EACCES);
4672 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4673 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4674 cifs_get_tlink(newtlink);
4675
Jeff Layton9d002df2010-10-06 19:51:11 -04004676 spin_lock(&cifs_sb->tlink_tree_lock);
4677 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04004678 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004679 if (tlink) {
4680 cifs_get_tlink(tlink);
4681 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004682 kfree(newtlink);
4683 goto wait_for_construction;
4684 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004685 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004686 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4687 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004688 } else {
4689wait_for_construction:
4690 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04004691 TASK_INTERRUPTIBLE);
4692 if (ret) {
4693 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10004694 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04004695 }
4696
4697 /* if it's good, return it */
4698 if (!IS_ERR(tlink->tl_tcon))
4699 return tlink;
4700
4701 /* return error if we tried this already recently */
4702 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4703 cifs_put_tlink(tlink);
4704 return ERR_PTR(-EACCES);
4705 }
4706
4707 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4708 goto wait_for_construction;
4709 }
4710
4711 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4712 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4713 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4714
4715 if (IS_ERR(tlink->tl_tcon)) {
4716 cifs_put_tlink(tlink);
4717 return ERR_PTR(-EACCES);
4718 }
4719
4720 return tlink;
4721}
Jeff Layton2de970f2010-10-06 19:51:12 -04004722
4723/*
4724 * periodic workqueue job that scans tcon_tree for a superblock and closes
4725 * out tcons.
4726 */
4727static void
4728cifs_prune_tlinks(struct work_struct *work)
4729{
4730 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4731 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04004732 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00004733 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04004734 struct rb_node *tmp;
4735 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04004736
Jeff Laytonb647c352010-10-28 11:16:44 -04004737 /*
4738 * Because we drop the spinlock in the loop in order to put the tlink
4739 * it's not guarded against removal of links from the tree. The only
4740 * places that remove entries from the tree are this function and
4741 * umounts. Because this function is non-reentrant and is canceled
4742 * before umount can proceed, this is safe.
4743 */
4744 spin_lock(&cifs_sb->tlink_tree_lock);
4745 node = rb_first(root);
4746 while (node != NULL) {
4747 tmp = node;
4748 node = rb_next(tmp);
4749 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4750
4751 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4752 atomic_read(&tlink->tl_count) != 0 ||
4753 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4754 continue;
4755
4756 cifs_get_tlink(tlink);
4757 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4758 rb_erase(tmp, root);
4759
Jeff Layton2de970f2010-10-06 19:51:12 -04004760 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004761 cifs_put_tlink(tlink);
4762 spin_lock(&cifs_sb->tlink_tree_lock);
4763 }
4764 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04004765
Jeff Laytonda472fc2012-03-23 14:40:53 -04004766 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004767 TLINK_IDLE_EXPIRE);
4768}