blob: 182b16e56749f05019f77149c814907909557700 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/cifs/connect.c
3 *
Steve French1080ef72011-02-24 18:07:19 +00004 * Copyright (C) International Business Machines Corp., 2002,2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Author(s): Steve French (sfrench@us.ibm.com)
6 *
7 * This library is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published
9 * by the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
15 * the GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software
Steve Frenchfb8c4b12007-07-10 01:16:18 +000019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070020 */
21#include <linux/fs.h>
22#include <linux/net.h>
23#include <linux/string.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010024#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/list.h>
26#include <linux/wait.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/pagemap.h>
29#include <linux/ctype.h>
30#include <linux/utsname.h>
31#include <linux/mempool.h>
Steve Frenchb8643e12005-04-28 22:41:07 -070032#include <linux/delay.h>
Steve Frenchf1914012005-08-18 09:37:34 -070033#include <linux/completion.h>
Igor Mammedovaaf737a2007-04-03 19:16:43 +000034#include <linux/kthread.h>
Steve French0ae0efa2005-10-10 10:57:19 -070035#include <linux/pagevec.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Igor Mammedov5c2503a2009-04-21 19:31:05 +040037#include <linux/namei.h>
Andrew Lunnc6e970a2017-03-28 23:45:06 +020038#include <linux/uuid.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080039#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/processor.h>
Jeff Layton50b64e32009-06-02 06:55:20 -040041#include <linux/inet.h>
Paul Gortmaker143cb492011-07-01 14:23:34 -040042#include <linux/module.h>
Jeff Layton8a8798a2012-01-17 16:09:15 -050043#include <keys/user-type.h>
Steve French0e2beda2009-01-30 21:24:41 +000044#include <net/ipv6.h>
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040045#include <linux/parser.h>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060046#include <linux/bvec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include "cifspdu.h"
48#include "cifsglob.h"
49#include "cifsproto.h"
50#include "cifs_unicode.h"
51#include "cifs_debug.h"
52#include "cifs_fs_sb.h"
53#include "ntlmssp.h"
54#include "nterr.h"
55#include "rfc1002pdu.h"
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +053056#include "fscache.h"
Pavel Shilovsky53e0e112016-11-04 11:50:31 -070057#include "smb2proto.h"
Long Li2f894642017-11-22 17:38:34 -070058#include "smbdirect.h"
Paulo Alcantara1c780222018-11-14 16:24:03 -020059#include "dns_resolve.h"
60#ifdef CONFIG_CIFS_DFS_UPCALL
61#include "dfs_cache.h"
62#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Linus Torvalds1da177e2005-04-16 15:20:36 -070064extern mempool_t *cifs_req_poolp;
Steve Frenchf92a7202018-05-24 04:11:07 -050065extern bool disable_legacy_dialects;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Jeff Layton2de970f2010-10-06 19:51:12 -040067/* FIXME: should these be tunable? */
Jeff Layton9d002df2010-10-06 19:51:11 -040068#define TLINK_ERROR_EXPIRE (1 * HZ)
Jeff Layton2de970f2010-10-06 19:51:12 -040069#define TLINK_IDLE_EXPIRE (600 * HZ)
Jeff Layton9d002df2010-10-06 19:51:11 -040070
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040071enum {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040072 /* Mount options that take no arguments */
73 Opt_user_xattr, Opt_nouser_xattr,
74 Opt_forceuid, Opt_noforceuid,
Jeff Layton72bd4812012-10-03 16:02:36 -040075 Opt_forcegid, Opt_noforcegid,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040076 Opt_noblocksend, Opt_noautotune,
77 Opt_hard, Opt_soft, Opt_perm, Opt_noperm,
Steve French2baa2682014-09-27 02:19:01 -050078 Opt_mapposix, Opt_nomapposix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040079 Opt_mapchars, Opt_nomapchars, Opt_sfu,
80 Opt_nosfu, Opt_nodfs, Opt_posixpaths,
Steve Frenchb3266142018-05-20 23:41:10 -050081 Opt_noposixpaths, Opt_nounix, Opt_unix,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040082 Opt_nocase,
83 Opt_brl, Opt_nobrl,
Steve French3d4ef9a2018-04-25 22:19:09 -050084 Opt_handlecache, Opt_nohandlecache,
Steve French95932652016-09-23 01:36:34 -050085 Opt_forcemandatorylock, Opt_setuidfromacl, Opt_setuids,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040086 Opt_nosetuids, Opt_dynperm, Opt_nodynperm,
87 Opt_nohard, Opt_nosoft,
88 Opt_nointr, Opt_intr,
89 Opt_nostrictsync, Opt_strictsync,
90 Opt_serverino, Opt_noserverino,
91 Opt_rwpidforward, Opt_cifsacl, Opt_nocifsacl,
92 Opt_acl, Opt_noacl, Opt_locallease,
Jeff Layton1b359202012-09-19 15:20:27 -070093 Opt_sign, Opt_seal, Opt_noac,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040094 Opt_fsc, Opt_mfsymlinks,
Jeff Laytona0b3df52013-05-24 07:40:59 -040095 Opt_multiuser, Opt_sloppy, Opt_nosharesock,
Steve Frenchb2a30772015-09-29 21:49:28 -050096 Opt_persistent, Opt_nopersistent,
Steve French592fafe2015-11-03 10:08:53 -060097 Opt_resilient, Opt_noresilient,
Long Li8339dd32017-11-07 01:54:55 -070098 Opt_domainauto, Opt_rdma,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -040099
100 /* Mount options which take numeric value */
101 Opt_backupuid, Opt_backupgid, Opt_uid,
102 Opt_cruid, Opt_gid, Opt_file_mode,
103 Opt_dirmode, Opt_port,
104 Opt_rsize, Opt_wsize, Opt_actimeo,
Steve French141891f2016-09-23 00:44:16 -0500105 Opt_echo_interval, Opt_max_credits,
Steve French8b217fe2016-11-11 22:36:20 -0600106 Opt_snapshot,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400107
108 /* Mount options which take string value */
109 Opt_user, Opt_pass, Opt_ip,
Jeff Layton73a999f2013-03-22 08:42:57 -0400110 Opt_domain, Opt_srcaddr, Opt_iocharset,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400111 Opt_netbiosname, Opt_servern,
Jeff Layton23db65f2012-05-15 12:20:51 -0400112 Opt_ver, Opt_vers, Opt_sec, Opt_cache,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400113
114 /* Mount options to be ignored */
115 Opt_ignore,
116
117 /* Options which could be blank */
118 Opt_blank_pass,
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100119 Opt_blank_user,
120 Opt_blank_ip,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400121
122 Opt_err
123};
124
125static const match_table_t cifs_mount_option_tokens = {
126
127 { Opt_user_xattr, "user_xattr" },
128 { Opt_nouser_xattr, "nouser_xattr" },
129 { Opt_forceuid, "forceuid" },
130 { Opt_noforceuid, "noforceuid" },
Jeff Layton72bd4812012-10-03 16:02:36 -0400131 { Opt_forcegid, "forcegid" },
132 { Opt_noforcegid, "noforcegid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400133 { Opt_noblocksend, "noblocksend" },
134 { Opt_noautotune, "noautotune" },
135 { Opt_hard, "hard" },
136 { Opt_soft, "soft" },
137 { Opt_perm, "perm" },
138 { Opt_noperm, "noperm" },
Steve French2baa2682014-09-27 02:19:01 -0500139 { Opt_mapchars, "mapchars" }, /* SFU style */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400140 { Opt_nomapchars, "nomapchars" },
Steve French2baa2682014-09-27 02:19:01 -0500141 { Opt_mapposix, "mapposix" }, /* SFM style */
142 { Opt_nomapposix, "nomapposix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400143 { Opt_sfu, "sfu" },
144 { Opt_nosfu, "nosfu" },
145 { Opt_nodfs, "nodfs" },
146 { Opt_posixpaths, "posixpaths" },
147 { Opt_noposixpaths, "noposixpaths" },
148 { Opt_nounix, "nounix" },
149 { Opt_nounix, "nolinux" },
Steve Frenchb3266142018-05-20 23:41:10 -0500150 { Opt_nounix, "noposix" },
151 { Opt_unix, "unix" },
152 { Opt_unix, "linux" },
153 { Opt_unix, "posix" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400154 { Opt_nocase, "nocase" },
155 { Opt_nocase, "ignorecase" },
156 { Opt_brl, "brl" },
157 { Opt_nobrl, "nobrl" },
Steve French3d4ef9a2018-04-25 22:19:09 -0500158 { Opt_handlecache, "handlecache" },
159 { Opt_nohandlecache, "nohandlecache" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400160 { Opt_nobrl, "nolock" },
161 { Opt_forcemandatorylock, "forcemandatorylock" },
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +0400162 { Opt_forcemandatorylock, "forcemand" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400163 { Opt_setuids, "setuids" },
164 { Opt_nosetuids, "nosetuids" },
Steve French95932652016-09-23 01:36:34 -0500165 { Opt_setuidfromacl, "idsfromsid" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400166 { Opt_dynperm, "dynperm" },
167 { Opt_nodynperm, "nodynperm" },
168 { Opt_nohard, "nohard" },
169 { Opt_nosoft, "nosoft" },
170 { Opt_nointr, "nointr" },
171 { Opt_intr, "intr" },
172 { Opt_nostrictsync, "nostrictsync" },
173 { Opt_strictsync, "strictsync" },
174 { Opt_serverino, "serverino" },
175 { Opt_noserverino, "noserverino" },
176 { Opt_rwpidforward, "rwpidforward" },
177 { Opt_cifsacl, "cifsacl" },
178 { Opt_nocifsacl, "nocifsacl" },
179 { Opt_acl, "acl" },
180 { Opt_noacl, "noacl" },
181 { Opt_locallease, "locallease" },
182 { Opt_sign, "sign" },
183 { Opt_seal, "seal" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400184 { Opt_noac, "noac" },
185 { Opt_fsc, "fsc" },
186 { Opt_mfsymlinks, "mfsymlinks" },
187 { Opt_multiuser, "multiuser" },
Jeff Laytond8162552012-03-23 14:40:56 -0400188 { Opt_sloppy, "sloppy" },
Jeff Laytona0b3df52013-05-24 07:40:59 -0400189 { Opt_nosharesock, "nosharesock" },
Steve Frenchb2a30772015-09-29 21:49:28 -0500190 { Opt_persistent, "persistenthandles"},
191 { Opt_nopersistent, "nopersistenthandles"},
Steve French592fafe2015-11-03 10:08:53 -0600192 { Opt_resilient, "resilienthandles"},
193 { Opt_noresilient, "noresilienthandles"},
Germano Percossi39566442016-12-15 12:31:18 +0530194 { Opt_domainauto, "domainauto"},
Long Li8339dd32017-11-07 01:54:55 -0700195 { Opt_rdma, "rdma"},
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400196
197 { Opt_backupuid, "backupuid=%s" },
198 { Opt_backupgid, "backupgid=%s" },
199 { Opt_uid, "uid=%s" },
200 { Opt_cruid, "cruid=%s" },
201 { Opt_gid, "gid=%s" },
202 { Opt_file_mode, "file_mode=%s" },
203 { Opt_dirmode, "dirmode=%s" },
204 { Opt_dirmode, "dir_mode=%s" },
205 { Opt_port, "port=%s" },
206 { Opt_rsize, "rsize=%s" },
207 { Opt_wsize, "wsize=%s" },
208 { Opt_actimeo, "actimeo=%s" },
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600209 { Opt_echo_interval, "echo_interval=%s" },
Steve French141891f2016-09-23 00:44:16 -0500210 { Opt_max_credits, "max_credits=%s" },
Steve French8b217fe2016-11-11 22:36:20 -0600211 { Opt_snapshot, "snapshot=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400212
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100213 { Opt_blank_user, "user=" },
214 { Opt_blank_user, "username=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400215 { Opt_user, "user=%s" },
216 { Opt_user, "username=%s" },
217 { Opt_blank_pass, "pass=" },
Jesper Nilsson3c15b4c2012-11-29 17:31:16 +0100218 { Opt_blank_pass, "password=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400219 { Opt_pass, "pass=%s" },
220 { Opt_pass, "password=%s" },
Sachin Prabhu4fe9e962012-04-10 18:12:27 +0100221 { Opt_blank_ip, "ip=" },
222 { Opt_blank_ip, "addr=" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400223 { Opt_ip, "ip=%s" },
224 { Opt_ip, "addr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400225 { Opt_ignore, "unc=%s" },
226 { Opt_ignore, "target=%s" },
227 { Opt_ignore, "path=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400228 { Opt_domain, "dom=%s" },
229 { Opt_domain, "domain=%s" },
230 { Opt_domain, "workgroup=%s" },
231 { Opt_srcaddr, "srcaddr=%s" },
Jeff Layton73a999f2013-03-22 08:42:57 -0400232 { Opt_ignore, "prefixpath=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400233 { Opt_iocharset, "iocharset=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400234 { Opt_netbiosname, "netbiosname=%s" },
235 { Opt_servern, "servern=%s" },
236 { Opt_ver, "ver=%s" },
Jeff Layton23db65f2012-05-15 12:20:51 -0400237 { Opt_vers, "vers=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400238 { Opt_sec, "sec=%s" },
Jeff Layton15b6a472012-05-16 07:50:15 -0400239 { Opt_cache, "cache=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400240
241 { Opt_ignore, "cred" },
242 { Opt_ignore, "credentials" },
Jeff Laytona557b972012-05-02 14:02:40 -0400243 { Opt_ignore, "cred=%s" },
244 { Opt_ignore, "credentials=%s" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400245 { Opt_ignore, "guest" },
246 { Opt_ignore, "rw" },
247 { Opt_ignore, "ro" },
248 { Opt_ignore, "suid" },
249 { Opt_ignore, "nosuid" },
250 { Opt_ignore, "exec" },
251 { Opt_ignore, "noexec" },
252 { Opt_ignore, "nodev" },
253 { Opt_ignore, "noauto" },
254 { Opt_ignore, "dev" },
255 { Opt_ignore, "mand" },
256 { Opt_ignore, "nomand" },
Steve French9b9c5be2018-09-22 12:07:06 -0500257 { Opt_ignore, "relatime" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400258 { Opt_ignore, "_netdev" },
259
260 { Opt_err, NULL }
261};
262
263enum {
264 Opt_sec_krb5, Opt_sec_krb5i, Opt_sec_krb5p,
265 Opt_sec_ntlmsspi, Opt_sec_ntlmssp,
Jeff Layton76596242012-07-23 20:34:17 -0400266 Opt_ntlm, Opt_sec_ntlmi, Opt_sec_ntlmv2,
267 Opt_sec_ntlmv2i, Opt_sec_lanman,
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400268 Opt_sec_none,
269
270 Opt_sec_err
271};
272
273static const match_table_t cifs_secflavor_tokens = {
274 { Opt_sec_krb5, "krb5" },
275 { Opt_sec_krb5i, "krb5i" },
276 { Opt_sec_krb5p, "krb5p" },
277 { Opt_sec_ntlmsspi, "ntlmsspi" },
278 { Opt_sec_ntlmssp, "ntlmssp" },
279 { Opt_ntlm, "ntlm" },
280 { Opt_sec_ntlmi, "ntlmi" },
Jeff Layton76596242012-07-23 20:34:17 -0400281 { Opt_sec_ntlmv2, "nontlm" },
282 { Opt_sec_ntlmv2, "ntlmv2" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400283 { Opt_sec_ntlmv2i, "ntlmv2i" },
Sachin Prabhu8830d7e2012-03-23 14:40:56 -0400284 { Opt_sec_lanman, "lanman" },
285 { Opt_sec_none, "none" },
286
287 { Opt_sec_err, NULL }
288};
289
Jeff Layton15b6a472012-05-16 07:50:15 -0400290/* cache flavors */
291enum {
292 Opt_cache_loose,
293 Opt_cache_strict,
294 Opt_cache_none,
295 Opt_cache_err
296};
297
298static const match_table_t cifs_cacheflavor_tokens = {
299 { Opt_cache_loose, "loose" },
300 { Opt_cache_strict, "strict" },
301 { Opt_cache_none, "none" },
302 { Opt_cache_err, NULL }
303};
304
Jeff Layton23db65f2012-05-15 12:20:51 -0400305static const match_table_t cifs_smb_version_tokens = {
306 { Smb_1, SMB1_VERSION_STRING },
Steve Frenchdd446b12012-11-28 23:21:06 -0600307 { Smb_20, SMB20_VERSION_STRING},
Steve French1080ef72011-02-24 18:07:19 +0000308 { Smb_21, SMB21_VERSION_STRING },
Steve Frenche4aa25e2012-10-01 12:26:22 -0500309 { Smb_30, SMB30_VERSION_STRING },
Steve French20b6d8b2013-06-12 22:48:41 -0500310 { Smb_302, SMB302_VERSION_STRING },
Kenneth D'souza4a3b38a2018-11-17 10:33:30 +0530311 { Smb_302, ALT_SMB302_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600312 { Smb_311, SMB311_VERSION_STRING },
Steve Frenchaab18932015-06-23 23:37:11 -0500313 { Smb_311, ALT_SMB311_VERSION_STRING },
Steve French9764c022017-09-17 10:41:35 -0500314 { Smb_3any, SMB3ANY_VERSION_STRING },
315 { Smb_default, SMBDEFAULT_VERSION_STRING },
Steve French5f7fbf72014-12-17 22:52:58 -0600316 { Smb_version_err, NULL }
Jeff Layton23db65f2012-05-15 12:20:51 -0400317};
318
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300319static int ip_connect(struct TCP_Server_Info *server);
320static int generic_ip_connect(struct TCP_Server_Info *server);
Jeff Laytonb647c352010-10-28 11:16:44 -0400321static void tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink);
Jeff Layton2de970f2010-10-06 19:51:12 -0400322static void cifs_prune_tlinks(struct work_struct *work);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -0400323static int cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
Steve Frenchc7c137b2018-06-06 17:59:29 -0500324 const char *devname, bool is_smb3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Jeff Laytond5c56052008-12-01 18:42:33 -0500326/*
327 * cifs tcp session reconnection
328 *
329 * mark tcp session as reconnecting so temporarily locked
330 * mark all smb sessions as reconnecting for tcp session
331 * reconnect tcp session
332 * wake up waiters on reconnection? - (not needed currently)
333 */
Pavel Shilovsky28ea5292012-05-23 16:18:00 +0400334int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335cifs_reconnect(struct TCP_Server_Info *server)
336{
337 int rc = 0;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500338 struct list_head *tmp, *tmp2;
Steve French96daf2b2011-05-27 04:34:02 +0000339 struct cifs_ses *ses;
340 struct cifs_tcon *tcon;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000341 struct mid_q_entry *mid_entry;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400342 struct list_head retry_list;
Steve French50c2f752007-07-13 00:33:32 +0000343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000345 if (server->tcpStatus == CifsExiting) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000346 /* the demux thread will exit normally
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 next time through the loop */
348 spin_unlock(&GlobalMid_Lock);
349 return rc;
350 } else
351 server->tcpStatus = CifsNeedReconnect;
352 spin_unlock(&GlobalMid_Lock);
353 server->maxBuf = 0;
Pavel Shilovskyaa24d1e2011-12-27 16:23:34 +0400354 server->max_read = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Steve French6e4d3bb2018-09-22 11:25:04 -0500356 cifs_dbg(FYI, "Mark tcp session as need reconnect\n");
Steve Frenchbf1fdeb2018-07-30 19:23:09 -0500357 trace_smb3_reconnect(server->CurrentMid, server->hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 /* before reconnecting the tcp session, mark the smb session (uid)
360 and the tid bad so they are not used until reconnected */
Joe Perchesf96637b2013-05-04 22:12:25 -0500361 cifs_dbg(FYI, "%s: marking sessions and tcons for reconnect\n",
362 __func__);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530363 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -0500364 list_for_each(tmp, &server->smb_ses_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000365 ses = list_entry(tmp, struct cifs_ses, smb_ses_list);
Jeff Layton14fbf502008-11-14 13:53:46 -0500366 ses->need_reconnect = true;
Jeff Laytonf1987b42008-11-15 11:12:47 -0500367 list_for_each(tmp2, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +0000368 tcon = list_entry(tmp2, struct cifs_tcon, tcon_list);
Jeff Laytonf1987b42008-11-15 11:12:47 -0500369 tcon->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
Aurelien Aptelb327a712018-01-24 13:46:10 +0100371 if (ses->tcon_ipc)
372 ses->tcon_ipc->need_reconnect = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +0530374 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /* do not want to be sending data on a socket we are freeing */
Joe Perchesf96637b2013-05-04 22:12:25 -0500377 cifs_dbg(FYI, "%s: tearing down socket\n", __func__);
Jeff Layton72ca5452008-12-01 07:09:36 -0500378 mutex_lock(&server->srv_mutex);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000379 if (server->ssocket) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500380 cifs_dbg(FYI, "State: 0x%x Flags: 0x%lx\n",
381 server->ssocket->state, server->ssocket->flags);
Trond Myklebust91cf45f2007-11-12 18:10:39 -0800382 kernel_sock_shutdown(server->ssocket, SHUT_WR);
Joe Perchesf96637b2013-05-04 22:12:25 -0500383 cifs_dbg(FYI, "Post shutdown state: 0x%x Flags: 0x%lx\n",
384 server->ssocket->state, server->ssocket->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 sock_release(server->ssocket);
386 server->ssocket = NULL;
387 }
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -0500388 server->sequence_number = 0;
389 server->session_estab = false;
Shirish Pargaonkar21e73392010-10-21 06:42:55 -0500390 kfree(server->session_key.response);
391 server->session_key.response = NULL;
392 server->session_key.len = 0;
Steve Frenchfda35942011-01-20 18:06:34 +0000393 server->lstrp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500395 /* mark submitted MIDs for retry and issue callback */
Jeff Layton3c1105d2011-05-22 07:09:13 -0400396 INIT_LIST_HEAD(&retry_list);
Joe Perchesf96637b2013-05-04 22:12:25 -0500397 cifs_dbg(FYI, "%s: moving mids to private list\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 spin_lock(&GlobalMid_Lock);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500399 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
400 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400401 if (mid_entry->mid_state == MID_REQUEST_SUBMITTED)
402 mid_entry->mid_state = MID_RETRY_NEEDED;
Jeff Layton3c1105d2011-05-22 07:09:13 -0400403 list_move(&mid_entry->qhead, &retry_list);
404 }
405 spin_unlock(&GlobalMid_Lock);
Rabin Vincent820962d2015-12-23 07:32:41 +0100406 mutex_unlock(&server->srv_mutex);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400407
Joe Perchesf96637b2013-05-04 22:12:25 -0500408 cifs_dbg(FYI, "%s: issuing mid callbacks\n", __func__);
Jeff Layton3c1105d2011-05-22 07:09:13 -0400409 list_for_each_safe(tmp, tmp2, &retry_list) {
410 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500411 list_del_init(&mid_entry->qhead);
412 mid_entry->callback(mid_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400415 do {
Steve French6c3d8902006-07-31 22:46:20 +0000416 try_to_freeze();
Pavel Shilovskya9f1b852010-12-13 19:08:35 +0300417
418 /* we should try only the port we connected to before */
Jeff Layton73e216a2013-09-05 08:38:10 -0400419 mutex_lock(&server->srv_mutex);
Long Li781a8052017-11-22 17:38:36 -0700420 if (cifs_rdma_enabled(server))
421 rc = smbd_reconnect(server);
422 else
423 rc = generic_ip_connect(server);
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000424 if (rc) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500425 cifs_dbg(FYI, "reconnect error %d\n", rc);
Federico Sauter4afe2602015-03-17 17:45:28 +0100426 mutex_unlock(&server->srv_mutex);
Steve French0cb766a2005-04-28 22:41:11 -0700427 msleep(3000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 } else {
429 atomic_inc(&tcpSesReconnectCount);
430 spin_lock(&GlobalMid_Lock);
Jeff Layton469ee612008-10-16 18:46:39 +0000431 if (server->tcpStatus != CifsExiting)
Steve Frenchfd88ce92011-04-12 01:01:14 +0000432 server->tcpStatus = CifsNeedNegotiate;
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000433 spin_unlock(&GlobalMid_Lock);
Federico Sauter4afe2602015-03-17 17:45:28 +0100434 mutex_unlock(&server->srv_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Jeff Layton7fdbaa12011-06-10 16:14:57 -0400436 } while (server->tcpStatus == CifsNeedReconnect);
Jeff Layton2b84a36c2011-01-11 07:24:21 -0500437
Sachin Prabhub8c60012016-10-20 19:52:24 -0400438 if (server->tcpStatus == CifsNeedNegotiate)
439 mod_delayed_work(cifsiod_wq, &server->echo, 0);
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return rc;
442}
443
Jeff Laytonc74093b2011-01-11 07:24:23 -0500444static void
445cifs_echo_request(struct work_struct *work)
446{
447 int rc;
448 struct TCP_Server_Info *server = container_of(work,
449 struct TCP_Server_Info, echo.work);
Sachin Prabhub8c60012016-10-20 19:52:24 -0400450 unsigned long echo_interval;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500451
Jeff Layton247ec9b2011-02-04 17:09:50 -0500452 /*
Sachin Prabhub8c60012016-10-20 19:52:24 -0400453 * If we need to renegotiate, set echo interval to zero to
454 * immediately call echo service where we can renegotiate.
455 */
456 if (server->tcpStatus == CifsNeedNegotiate)
457 echo_interval = 0;
458 else
459 echo_interval = server->echo_interval;
460
461 /*
462 * We cannot send an echo if it is disabled.
463 * Also, no need to ping if we got a response recently.
Jeff Layton247ec9b2011-02-04 17:09:50 -0500464 */
Steve French4fcd1812016-06-22 20:12:05 -0500465
466 if (server->tcpStatus == CifsNeedReconnect ||
Sachin Prabhub8c60012016-10-20 19:52:24 -0400467 server->tcpStatus == CifsExiting ||
468 server->tcpStatus == CifsNew ||
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400469 (server->ops->can_echo && !server->ops->can_echo(server)) ||
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600470 time_before(jiffies, server->lstrp + echo_interval - HZ))
Jeff Laytonc74093b2011-01-11 07:24:23 -0500471 goto requeue_echo;
472
Pavel Shilovskyf6d76172012-05-25 14:47:16 +0400473 rc = server->ops->echo ? server->ops->echo(server) : -ENOSYS;
Jeff Laytonc74093b2011-01-11 07:24:23 -0500474 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -0500475 cifs_dbg(FYI, "Unable to send echo request to server: %s\n",
476 server->hostname);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500477
478requeue_echo:
Sachin Prabhub8c60012016-10-20 19:52:24 -0400479 queue_delayed_work(cifsiod_wq, &server->echo, server->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -0500480}
481
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400482static bool
Jeff Layton2a37ef92011-10-19 15:29:23 -0400483allocate_buffers(struct TCP_Server_Info *server)
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400484{
Jeff Layton2a37ef92011-10-19 15:29:23 -0400485 if (!server->bigbuf) {
486 server->bigbuf = (char *)cifs_buf_get();
487 if (!server->bigbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500488 cifs_dbg(VFS, "No memory for large SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400489 msleep(3000);
490 /* retry will check if exiting */
491 return false;
492 }
Jeff Layton2a37ef92011-10-19 15:29:23 -0400493 } else if (server->large_buf) {
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400494 /* we are reusing a dirty large buf, clear its start */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400495 memset(server->bigbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400496 }
497
Jeff Layton2a37ef92011-10-19 15:29:23 -0400498 if (!server->smallbuf) {
499 server->smallbuf = (char *)cifs_small_buf_get();
500 if (!server->smallbuf) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500501 cifs_dbg(VFS, "No memory for SMB response\n");
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400502 msleep(1000);
503 /* retry will check if exiting */
504 return false;
505 }
506 /* beginning of smb buffer is cleared in our buf_get */
507 } else {
508 /* if existing small buf clear beginning */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400509 memset(server->smallbuf, 0, HEADER_SIZE(server));
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400510 }
511
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400512 return true;
513}
514
Jeff Laytonba749e62011-10-11 06:41:32 -0400515static bool
516server_unresponsive(struct TCP_Server_Info *server)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400517{
Pavel Shilovsky6dae51a2012-02-21 16:50:23 +0300518 /*
519 * We need to wait 2 echo intervals to make sure we handle such
520 * situations right:
521 * 1s client sends a normal SMB request
522 * 2s client gets a response
523 * 30s echo workqueue job pops, and decides we got a response recently
524 * and don't need to send another
525 * ...
526 * 65s kernel_recvmsg times out, and we see that we haven't gotten
527 * a response in >60s.
528 */
Samuel Cabrero76e75272017-07-11 12:44:39 +0200529 if ((server->tcpStatus == CifsGood ||
530 server->tcpStatus == CifsNeedNegotiate) &&
Steve Frenchadfeb3e2015-12-18 12:31:36 -0600531 time_after(jiffies, server->lstrp + 2 * server->echo_interval)) {
532 cifs_dbg(VFS, "Server %s has not responded in %lu seconds. Reconnecting...\n",
533 server->hostname, (2 * server->echo_interval) / HZ);
Jeff Laytonba749e62011-10-11 06:41:32 -0400534 cifs_reconnect(server);
535 wake_up(&server->response_q);
536 return true;
537 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400538
Jeff Laytonba749e62011-10-11 06:41:32 -0400539 return false;
540}
541
Al Viro71335662016-01-09 19:54:50 -0500542static int
543cifs_readv_from_socket(struct TCP_Server_Info *server, struct msghdr *smb_msg)
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400544{
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400545 int length = 0;
546 int total_read;
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400547
Al Viro71335662016-01-09 19:54:50 -0500548 smb_msg->msg_control = NULL;
549 smb_msg->msg_controllen = 0;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400550
Al Viro71335662016-01-09 19:54:50 -0500551 for (total_read = 0; msg_data_left(smb_msg); total_read += length) {
Jeff Layton95edcff2011-12-01 20:22:41 -0500552 try_to_freeze();
553
Al Viro71335662016-01-09 19:54:50 -0500554 if (server_unresponsive(server))
555 return -ECONNABORTED;
Long Li2fef1372017-11-22 17:38:41 -0700556 if (cifs_rdma_enabled(server) && server->smbd_conn)
557 length = smbd_recv(server->smbd_conn, smb_msg);
558 else
559 length = sock_recvmsg(server->ssocket, smb_msg, 0);
Al Viro71335662016-01-09 19:54:50 -0500560
561 if (server->tcpStatus == CifsExiting)
562 return -ESHUTDOWN;
563
564 if (server->tcpStatus == CifsNeedReconnect) {
565 cifs_reconnect(server);
566 return -ECONNABORTED;
Jeff Laytonba749e62011-10-11 06:41:32 -0400567 }
568
Al Viro71335662016-01-09 19:54:50 -0500569 if (length == -ERESTARTSYS ||
570 length == -EAGAIN ||
571 length == -EINTR) {
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400572 /*
573 * Minimum sleep to prevent looping, allowing socket
574 * to clear and app threads to set tcpStatus
575 * CifsNeedReconnect if server hung.
576 */
577 usleep_range(1000, 2000);
578 length = 0;
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400579 continue;
Al Viro71335662016-01-09 19:54:50 -0500580 }
581
582 if (length <= 0) {
Al Viro09aab882015-11-13 03:00:17 -0500583 cifs_dbg(FYI, "Received no data or error: %d\n", length);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400584 cifs_reconnect(server);
Al Viro71335662016-01-09 19:54:50 -0500585 return -ECONNABORTED;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400586 }
587 }
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400588 return total_read;
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400589}
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400590
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400591int
592cifs_read_from_socket(struct TCP_Server_Info *server, char *buf,
593 unsigned int to_read)
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400594{
Al Viro71335662016-01-09 19:54:50 -0500595 struct msghdr smb_msg;
596 struct kvec iov = {.iov_base = buf, .iov_len = to_read};
David Howellsaa563d72018-10-20 00:57:56 +0100597 iov_iter_kvec(&smb_msg.msg_iter, READ, &iov, 1, to_read);
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400598
Al Viro71335662016-01-09 19:54:50 -0500599 return cifs_readv_from_socket(server, &smb_msg);
600}
Jeff Layton42c4dfc2011-10-19 15:28:17 -0400601
Al Viro71335662016-01-09 19:54:50 -0500602int
603cifs_read_page_from_socket(struct TCP_Server_Info *server, struct page *page,
Long Li1dbe3462018-05-30 12:47:55 -0700604 unsigned int page_offset, unsigned int to_read)
Al Viro71335662016-01-09 19:54:50 -0500605{
606 struct msghdr smb_msg;
Long Li1dbe3462018-05-30 12:47:55 -0700607 struct bio_vec bv = {
608 .bv_page = page, .bv_len = to_read, .bv_offset = page_offset};
David Howellsaa563d72018-10-20 00:57:56 +0100609 iov_iter_bvec(&smb_msg.msg_iter, READ, &bv, 1, to_read);
Al Viro71335662016-01-09 19:54:50 -0500610 return cifs_readv_from_socket(server, &smb_msg);
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400611}
612
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400613static bool
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400614is_smb_response(struct TCP_Server_Info *server, unsigned char type)
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400615{
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400616 /*
617 * The first byte big endian of the length field,
618 * is actually not part of the length but the type
619 * with the most common, zero, as regular data.
620 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400621 switch (type) {
622 case RFC1002_SESSION_MESSAGE:
623 /* Regular SMB response */
624 return true;
625 case RFC1002_SESSION_KEEP_ALIVE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500626 cifs_dbg(FYI, "RFC 1002 session keep alive\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400627 break;
628 case RFC1002_POSITIVE_SESSION_RESPONSE:
Joe Perchesf96637b2013-05-04 22:12:25 -0500629 cifs_dbg(FYI, "RFC 1002 positive session response\n");
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400630 break;
631 case RFC1002_NEGATIVE_SESSION_RESPONSE:
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400632 /*
633 * We get this from Windows 98 instead of an error on
634 * SMB negprot response.
635 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500636 cifs_dbg(FYI, "RFC 1002 negative session response\n");
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400637 /* give server a second to clean up */
638 msleep(1000);
639 /*
640 * Always try 445 first on reconnect since we get NACK
641 * on some if we ever connected to port 139 (the NACK
642 * is since we do not begin with RFC1001 session
643 * initialize frame).
644 */
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400645 cifs_set_port((struct sockaddr *)&server->dstaddr, CIFS_PORT);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400646 cifs_reconnect(server);
647 wake_up(&server->response_q);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400648 break;
649 default:
Joe Perchesf96637b2013-05-04 22:12:25 -0500650 cifs_dbg(VFS, "RFC 1002 unknown response type 0x%x\n", type);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400651 cifs_reconnect(server);
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400652 }
653
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400654 return false;
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400655}
656
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400657void
658dequeue_mid(struct mid_q_entry *mid, bool malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400659{
660#ifdef CONFIG_CIFS_STATS2
661 mid->when_received = jiffies;
662#endif
663 spin_lock(&GlobalMid_Lock);
664 if (!malformed)
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400665 mid->mid_state = MID_RESPONSE_RECEIVED;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400666 else
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400667 mid->mid_state = MID_RESPONSE_MALFORMED;
Ronnie Sahlbergddf83af2018-08-30 10:12:59 +1000668 /*
669 * Trying to handle/dequeue a mid after the send_recv()
670 * function has finished processing it is a bug.
671 */
672 if (mid->mid_flags & MID_DELETED)
673 printk_once(KERN_WARNING
674 "trying to dequeue a deleted mid\n");
675 else
676 list_del_init(&mid->qhead);
Jeff Laytonea1f4502011-10-19 15:29:05 -0400677 spin_unlock(&GlobalMid_Lock);
678}
679
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400680static void
681handle_mid(struct mid_q_entry *mid, struct TCP_Server_Info *server,
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400682 char *buf, int malformed)
Jeff Laytonea1f4502011-10-19 15:29:05 -0400683{
Pavel Shilovsky316cf942012-05-23 14:31:03 +0400684 if (server->ops->check_trans2 &&
685 server->ops->check_trans2(mid, server, buf, malformed))
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400686 return;
Jeff Laytonea1f4502011-10-19 15:29:05 -0400687 mid->resp_buf = buf;
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400688 mid->large_buf = server->large_buf;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400689 /* Was previous buf put in mpx struct for multi-rsp? */
690 if (!mid->multiRsp) {
691 /* smb buffer will be freed by user thread */
692 if (server->large_buf)
693 server->bigbuf = NULL;
694 else
695 server->smallbuf = NULL;
696 }
Jeff Laytonffc00e22011-10-19 15:29:13 -0400697 dequeue_mid(mid, malformed);
Pavel Shilovskyad69bae2011-08-01 13:19:43 +0400698}
699
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400700static void clean_demultiplex_info(struct TCP_Server_Info *server)
701{
702 int length;
703
704 /* take it off the list, if it's not already */
705 spin_lock(&cifs_tcp_ses_lock);
706 list_del_init(&server->tcp_ses_list);
707 spin_unlock(&cifs_tcp_ses_lock);
708
709 spin_lock(&GlobalMid_Lock);
710 server->tcpStatus = CifsExiting;
711 spin_unlock(&GlobalMid_Lock);
712 wake_up_all(&server->response_q);
713
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400714 /* check if we have blocked requests that need to free */
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300715 spin_lock(&server->req_lock);
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +0400716 if (server->credits <= 0)
717 server->credits = 1;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +0300718 spin_unlock(&server->req_lock);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400719 /*
720 * Although there should not be any requests blocked on this queue it
721 * can not hurt to be paranoid and try to wake up requests that may
722 * haven been blocked when more than 50 at time were on the wire to the
723 * same server - they now will see the session is in exit state and get
724 * out of SendReceive.
725 */
726 wake_up_all(&server->request_q);
727 /* give those requests time to exit */
728 msleep(125);
Long Libce9ce72017-11-22 17:38:38 -0700729 if (cifs_rdma_enabled(server) && server->smbd_conn) {
730 smbd_destroy(server->smbd_conn);
731 server->smbd_conn = NULL;
732 }
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400733 if (server->ssocket) {
734 sock_release(server->ssocket);
735 server->ssocket = NULL;
736 }
737
738 if (!list_empty(&server->pending_mid_q)) {
739 struct list_head dispose_list;
740 struct mid_q_entry *mid_entry;
741 struct list_head *tmp, *tmp2;
742
743 INIT_LIST_HEAD(&dispose_list);
744 spin_lock(&GlobalMid_Lock);
745 list_for_each_safe(tmp, tmp2, &server->pending_mid_q) {
746 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500747 cifs_dbg(FYI, "Clearing mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky7c9421e2012-03-23 14:28:03 -0400748 mid_entry->mid_state = MID_SHUTDOWN;
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400749 list_move(&mid_entry->qhead, &dispose_list);
750 }
751 spin_unlock(&GlobalMid_Lock);
752
753 /* now walk dispose list and issue callbacks */
754 list_for_each_safe(tmp, tmp2, &dispose_list) {
755 mid_entry = list_entry(tmp, struct mid_q_entry, qhead);
Joe Perchesf96637b2013-05-04 22:12:25 -0500756 cifs_dbg(FYI, "Callback mid 0x%llx\n", mid_entry->mid);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400757 list_del_init(&mid_entry->qhead);
758 mid_entry->callback(mid_entry);
759 }
760 /* 1/8th of sec is more than enough time for them to exit */
761 msleep(125);
762 }
763
764 if (!list_empty(&server->pending_mid_q)) {
765 /*
766 * mpx threads have not exited yet give them at least the smb
767 * send timeout time for long ops.
768 *
769 * Due to delays on oplock break requests, we need to wait at
770 * least 45 seconds before giving up on a request getting a
771 * response and going ahead and killing cifsd.
772 */
Joe Perchesf96637b2013-05-04 22:12:25 -0500773 cifs_dbg(FYI, "Wait for exit from demultiplex thread\n");
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400774 msleep(46000);
775 /*
776 * If threads still have not exited they are probably never
777 * coming home not much else we can do but free the memory.
778 */
779 }
780
781 kfree(server->hostname);
782 kfree(server);
783
784 length = atomic_dec_return(&tcpSesAllocCount);
785 if (length > 0)
David Rientjes11d83362015-04-14 15:48:21 -0700786 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +0400787}
788
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400789static int
Jeff Laytone9097ab2011-10-19 15:29:40 -0400790standard_receive3(struct TCP_Server_Info *server, struct mid_q_entry *mid)
791{
792 int length;
793 char *buf = server->smallbuf;
Ronnie Sahlberg2e964672018-04-09 18:06:26 +1000794 unsigned int pdu_length = server->pdu_size;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400795
796 /* make sure this will fit in a large buffer */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100797 if (pdu_length > CIFSMaxBufSize + MAX_HEADER_SIZE(server) -
798 server->vals->header_preamble_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500799 cifs_dbg(VFS, "SMB response too long (%u bytes)\n", pdu_length);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400800 cifs_reconnect(server);
801 wake_up(&server->response_q);
Pavel Shilovsky3fabaa22014-07-10 09:55:52 +0400802 return -ECONNABORTED;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400803 }
804
805 /* switch to large buffer if too big for a small one */
806 if (pdu_length > MAX_CIFS_SMALL_BUFFER_SIZE - 4) {
807 server->large_buf = true;
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400808 memcpy(server->bigbuf, buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400809 buf = server->bigbuf;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400810 }
811
812 /* now read the rest */
Pavel Shilovsky1887f602012-05-17 12:45:31 +0400813 length = cifs_read_from_socket(server, buf + HEADER_SIZE(server) - 1,
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100814 pdu_length - HEADER_SIZE(server) + 1
815 + server->vals->header_preamble_size);
816
Jeff Laytone9097ab2011-10-19 15:29:40 -0400817 if (length < 0)
818 return length;
819 server->total_read += length;
820
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400821 dump_smb(buf, server->total_read);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400822
Pavel Shilovsky4326ed22016-11-17 15:24:46 -0800823 return cifs_handle_standard(server, mid);
824}
825
826int
827cifs_handle_standard(struct TCP_Server_Info *server, struct mid_q_entry *mid)
828{
829 char *buf = server->large_buf ? server->bigbuf : server->smallbuf;
830 int length;
831
Jeff Laytone9097ab2011-10-19 15:29:40 -0400832 /*
833 * We know that we received enough to get to the MID as we
834 * checked the pdu_length earlier. Now check to see
835 * if the rest of the header is OK. We borrow the length
836 * var for the rest of the loop to avoid a new stack var.
837 *
838 * 48 bytes is enough to display the header and a little bit
839 * into the payload for debugging purposes.
840 */
Steve French373512e2015-12-18 13:05:30 -0600841 length = server->ops->check_message(buf, server->total_read, server);
Jeff Laytone9097ab2011-10-19 15:29:40 -0400842 if (length != 0)
843 cifs_dump_mem("Bad SMB: ", buf,
844 min_t(unsigned int, server->total_read, 48));
845
Pavel Shilovsky511c54a2017-07-08 14:32:00 -0700846 if (server->ops->is_session_expired &&
847 server->ops->is_session_expired(buf)) {
848 cifs_reconnect(server);
849 wake_up(&server->response_q);
850 return -1;
851 }
852
Pavel Shilovsky2e44b282012-09-18 16:20:33 -0700853 if (server->ops->is_status_pending &&
854 server->ops->is_status_pending(buf, server, length))
855 return -1;
856
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500857 if (!mid)
858 return length;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400859
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400860 handle_mid(mid, server, buf, length);
Jeff Laytonff4fa4a2012-02-07 06:31:05 -0500861 return 0;
Jeff Laytone9097ab2011-10-19 15:29:40 -0400862}
863
864static int
Al Viro7c97c202011-06-21 08:51:28 -0400865cifs_demultiplex_thread(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000867 int i, num_mids, length;
Al Viro7c97c202011-06-21 08:51:28 -0400868 struct TCP_Server_Info *server = p;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400869 unsigned int pdu_length;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000870 unsigned int next_offset;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400871 char *buf = NULL;
Steve Frencha5c3e1c2014-09-16 04:16:19 -0500872 struct task_struct *task_to_wake = NULL;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000873 struct mid_q_entry *mids[MAX_COMPOUND];
874 char *bufs[MAX_COMPOUND];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 current->flags |= PF_MEMALLOC;
Joe Perchesf96637b2013-05-04 22:12:25 -0500877 cifs_dbg(FYI, "Demultiplex PID: %d\n", task_pid_nr(current));
Jeff Layton93d0ec82008-08-02 08:00:48 -0400878
879 length = atomic_inc_return(&tcpSesAllocCount);
880 if (length > 1)
David Rientjes11d83362015-04-14 15:48:21 -0700881 mempool_resize(cifs_req_poolp, length + cifs_min_rcv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700883 set_freezable();
Jeff Layton469ee612008-10-16 18:46:39 +0000884 while (server->tcpStatus != CifsExiting) {
Steve Frenchede13272005-08-30 20:10:14 -0700885 if (try_to_freeze())
886 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700887
Jeff Layton2a37ef92011-10-19 15:29:23 -0400888 if (!allocate_buffers(server))
Pavel Shilovsky3d9c2472011-08-01 13:19:40 +0400889 continue;
Steve Frenchb8643e12005-04-28 22:41:07 -0700890
Jeff Layton2a37ef92011-10-19 15:29:23 -0400891 server->large_buf = false;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400892 buf = server->smallbuf;
Steve Frenchf01d5e12007-08-30 21:13:31 +0000893 pdu_length = 4; /* enough to get RFC1001 header */
Steve Frenchfda35942011-01-20 18:06:34 +0000894
Jeff Laytone28bc5b2011-10-19 15:30:07 -0400895 length = cifs_read_from_socket(server, buf, pdu_length);
Jeff Laytona52c1eb2011-10-11 06:41:32 -0400896 if (length < 0)
Steve Frenchfda35942011-01-20 18:06:34 +0000897 continue;
Ronnie Sahlberg977b6172018-06-01 10:53:02 +1000898
899 if (server->vals->header_preamble_size == 0)
900 server->total_read = 0;
901 else
902 server->total_read = length;
Steve French67010fb2005-04-28 22:41:09 -0700903
Pavel Shilovsky98bac622011-08-01 13:19:42 +0400904 /*
905 * The right amount was read from socket - 4 bytes,
906 * so we can now interpret the length field.
907 */
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400908 pdu_length = get_rfc1002_length(buf);
Steve French46810cb2005-04-28 22:41:09 -0700909
Joe Perchesf96637b2013-05-04 22:12:25 -0500910 cifs_dbg(FYI, "RFC1002 header 0x%x\n", pdu_length);
Jeff Laytonfe11e4c2011-10-11 06:41:32 -0400911 if (!is_smb_response(server, buf[0]))
Steve Frenchfb8c4b12007-07-10 01:16:18 +0000912 continue;
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000913next_pdu:
914 server->pdu_size = pdu_length;
Steve Frenche4eb2952005-04-28 22:41:09 -0700915
Jeff Layton89482a52011-10-19 15:28:57 -0400916 /* make sure we have enough to get to the MID */
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000917 if (server->pdu_size < HEADER_SIZE(server) - 1 -
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100918 server->vals->header_preamble_size) {
Joe Perchesf96637b2013-05-04 22:12:25 -0500919 cifs_dbg(VFS, "SMB response too short (%u bytes)\n",
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000920 server->pdu_size);
Jeff Layton89482a52011-10-19 15:28:57 -0400921 cifs_reconnect(server);
922 wake_up(&server->response_q);
923 continue;
Steve Frenche4eb2952005-04-28 22:41:09 -0700924 }
Pavel Shilovskye7015fb2011-08-01 13:19:41 +0400925
Jeff Layton89482a52011-10-19 15:28:57 -0400926 /* read down to the MID */
Ronnie Sahlberg93012bf2018-03-31 11:45:31 +1100927 length = cifs_read_from_socket(server,
928 buf + server->vals->header_preamble_size,
929 HEADER_SIZE(server) - 1
930 - server->vals->header_preamble_size);
Jeff Layton89482a52011-10-19 15:28:57 -0400931 if (length < 0)
932 continue;
Jeff Layton2a37ef92011-10-19 15:29:23 -0400933 server->total_read += length;
Jeff Layton89482a52011-10-19 15:28:57 -0400934
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +1000935 if (server->ops->next_header) {
936 next_offset = server->ops->next_header(buf);
937 if (next_offset)
938 server->pdu_size = next_offset;
939 }
940
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000941 memset(mids, 0, sizeof(mids));
942 memset(bufs, 0, sizeof(bufs));
943 num_mids = 0;
944
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800945 if (server->ops->is_transform_hdr &&
946 server->ops->receive_transform &&
947 server->ops->is_transform_hdr(buf)) {
948 length = server->ops->receive_transform(server,
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000949 mids,
950 bufs,
951 &num_mids);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800952 } else {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000953 mids[0] = server->ops->find_mid(server, buf);
954 bufs[0] = buf;
Steve French7af929d2018-10-02 18:54:09 -0500955 num_mids = 1;
Jeff Laytonc8054eb2011-10-19 15:29:31 -0400956
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000957 if (!mids[0] || !mids[0]->receive)
958 length = standard_receive3(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800959 else
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000960 length = mids[0]->receive(server, mids[0]);
Pavel Shilovsky9bb17e02016-11-17 15:24:34 -0800961 }
Jeff Layton44d22d82011-10-19 15:29:49 -0400962
Lars Persson696e4202018-06-25 14:05:25 +0200963 if (length < 0) {
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000964 for (i = 0; i < num_mids; i++)
965 if (mids[i])
966 cifs_mid_q_entry_release(mids[i]);
Steve Frenche4eb2952005-04-28 22:41:09 -0700967 continue;
Lars Persson696e4202018-06-25 14:05:25 +0200968 }
Steve Frenche4eb2952005-04-28 22:41:09 -0700969
Pavel Shilovskyd4e48542012-03-23 14:28:02 -0400970 if (server->large_buf)
Jeff Laytone9097ab2011-10-19 15:29:40 -0400971 buf = server->bigbuf;
Steve Frenche4eb2952005-04-28 22:41:09 -0700972
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000973
Steve Frenchfda35942011-01-20 18:06:34 +0000974 server->lstrp = jiffies;
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000975
976 for (i = 0; i < num_mids; i++) {
977 if (mids[i] != NULL) {
978 mids[i]->resp_buf_size = server->pdu_size;
979 if ((mids[i]->mid_flags & MID_WAIT_CANCELLED) &&
980 mids[i]->mid_state == MID_RESPONSE_RECEIVED &&
981 server->ops->handle_cancelled_mid)
982 server->ops->handle_cancelled_mid(
983 mids[i]->resp_buf,
Sachin Prabhu38bd4902017-03-03 15:41:38 -0800984 server);
985
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000986 if (!mids[i]->multiRsp || mids[i]->multiEnd)
987 mids[i]->callback(mids[i]);
Lars Persson696e4202018-06-25 14:05:25 +0200988
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +1000989 cifs_mid_q_entry_release(mids[i]);
990 } else if (server->ops->is_oplock_break &&
991 server->ops->is_oplock_break(bufs[i],
992 server)) {
993 cifs_dbg(FYI, "Received oplock break\n");
994 } else {
995 cifs_dbg(VFS, "No task to wake, unknown frame "
996 "received! NumMids %d\n",
997 atomic_read(&midCount));
998 cifs_dump_mem("Received Data is: ", bufs[i],
999 HEADER_SIZE(server));
Steve French39798772006-05-31 22:40:51 +00001000#ifdef CONFIG_CIFS_DEBUG2
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001001 if (server->ops->dump_detail)
1002 server->ops->dump_detail(bufs[i],
1003 server);
1004 cifs_dump_mids(server);
Steve French39798772006-05-31 22:40:51 +00001005#endif /* CIFS_DEBUG2 */
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001006 }
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001007 }
Ronnie Sahlbergb24df3e2018-08-08 15:07:45 +10001008
Ronnie Sahlberg8ce79ec2018-06-01 10:53:08 +10001009 if (pdu_length > server->pdu_size) {
1010 if (!allocate_buffers(server))
1011 continue;
1012 pdu_length -= server->pdu_size;
1013 server->total_read = 0;
1014 server->large_buf = false;
1015 buf = server->smallbuf;
1016 goto next_pdu;
Steve Frenche4eb2952005-04-28 22:41:09 -07001017 }
1018 } /* end while !EXITING */
1019
Justin P. Mattockfd62cb72011-02-24 22:15:02 -08001020 /* buffer usually freed in free_mid - need to free it here on exit */
Jeff Layton2a37ef92011-10-19 15:29:23 -04001021 cifs_buf_release(server->bigbuf);
1022 if (server->smallbuf) /* no sense logging a debug message if NULL */
1023 cifs_small_buf_release(server->smallbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001025 task_to_wake = xchg(&server->tsk, NULL);
Pavel Shilovsky762dfd12011-08-01 13:19:44 +04001026 clean_demultiplex_info(server);
Steve Frencha5c3e1c2014-09-16 04:16:19 -05001027
1028 /* if server->tsk was NULL then wait for a signal before exiting */
1029 if (!task_to_wake) {
1030 set_current_state(TASK_INTERRUPTIBLE);
1031 while (!signal_pending(current)) {
1032 schedule();
1033 set_current_state(TASK_INTERRUPTIBLE);
1034 }
1035 set_current_state(TASK_RUNNING);
1036 }
1037
Jeff Layton0468a2c2008-12-01 07:09:35 -05001038 module_put_and_exit(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039}
1040
Jeff Laytonc359cf32007-11-16 22:22:06 +00001041/* extract the host portion of the UNC string */
1042static char *
1043extract_hostname(const char *unc)
1044{
1045 const char *src;
1046 char *dst, *delim;
1047 unsigned int len;
1048
1049 /* skip double chars at beginning of string */
1050 /* BB: check validity of these bytes? */
Paulo Alcantarac34fea52018-11-14 14:03:40 -02001051 if (strlen(unc) < 3)
1052 return ERR_PTR(-EINVAL);
1053 for (src = unc; *src && *src == '\\'; src++)
1054 ;
1055 if (!*src)
1056 return ERR_PTR(-EINVAL);
Jeff Laytonc359cf32007-11-16 22:22:06 +00001057
1058 /* delimiter between hostname and sharename is always '\\' now */
1059 delim = strchr(src, '\\');
1060 if (!delim)
1061 return ERR_PTR(-EINVAL);
1062
1063 len = delim - src;
1064 dst = kmalloc((len + 1), GFP_KERNEL);
1065 if (dst == NULL)
1066 return ERR_PTR(-ENOMEM);
1067
1068 memcpy(dst, src, len);
1069 dst[len] = '\0';
1070
1071 return dst;
1072}
1073
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001074static int get_option_ul(substring_t args[], unsigned long *option)
1075{
1076 int rc;
1077 char *string;
1078
1079 string = match_strdup(args);
1080 if (string == NULL)
1081 return -ENOMEM;
Sachin Prabhubfa890a2012-04-13 14:04:32 +01001082 rc = kstrtoul(string, 0, option);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001083 kfree(string);
1084
1085 return rc;
1086}
1087
Eric W. Biederman3da46562013-02-06 01:37:39 -08001088static int get_option_uid(substring_t args[], kuid_t *result)
1089{
1090 unsigned long value;
1091 kuid_t uid;
1092 int rc;
1093
1094 rc = get_option_ul(args, &value);
1095 if (rc)
1096 return rc;
1097
1098 uid = make_kuid(current_user_ns(), value);
1099 if (!uid_valid(uid))
1100 return -EINVAL;
1101
1102 *result = uid;
1103 return 0;
1104}
1105
1106static int get_option_gid(substring_t args[], kgid_t *result)
1107{
1108 unsigned long value;
1109 kgid_t gid;
1110 int rc;
1111
1112 rc = get_option_ul(args, &value);
1113 if (rc)
1114 return rc;
1115
1116 gid = make_kgid(current_user_ns(), value);
1117 if (!gid_valid(gid))
1118 return -EINVAL;
1119
1120 *result = gid;
1121 return 0;
1122}
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001123
1124static int cifs_parse_security_flavors(char *value,
1125 struct smb_vol *vol)
1126{
1127
1128 substring_t args[MAX_OPT_ARGS];
1129
Jeff Layton1e3cc572013-06-10 17:12:23 -05001130 /*
1131 * With mount options, the last one should win. Reset any existing
1132 * settings back to default.
1133 */
1134 vol->sectype = Unspecified;
1135 vol->sign = false;
1136
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001137 switch (match_token(value, cifs_secflavor_tokens, args)) {
Jeff Layton3f618222013-06-12 19:52:14 -05001138 case Opt_sec_krb5p:
1139 cifs_dbg(VFS, "sec=krb5p is not supported!\n");
1140 return 1;
1141 case Opt_sec_krb5i:
1142 vol->sign = true;
1143 /* Fallthrough */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001144 case Opt_sec_krb5:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001145 vol->sectype = Kerberos;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001146 break;
1147 case Opt_sec_ntlmsspi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001148 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001149 /* Fallthrough */
1150 case Opt_sec_ntlmssp:
1151 vol->sectype = RawNTLMSSP;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001152 break;
1153 case Opt_sec_ntlmi:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001154 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001155 /* Fallthrough */
1156 case Opt_ntlm:
1157 vol->sectype = NTLM;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001158 break;
1159 case Opt_sec_ntlmv2i:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001160 vol->sign = true;
Jeff Layton3f618222013-06-12 19:52:14 -05001161 /* Fallthrough */
1162 case Opt_sec_ntlmv2:
1163 vol->sectype = NTLMv2;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001164 break;
1165#ifdef CONFIG_CIFS_WEAK_PW_HASH
1166 case Opt_sec_lanman:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001167 vol->sectype = LANMAN;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001168 break;
1169#endif
1170 case Opt_sec_none:
1171 vol->nullauth = 1;
1172 break;
1173 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001174 cifs_dbg(VFS, "bad security option: %s\n", value);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001175 return 1;
1176 }
1177
1178 return 0;
1179}
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181static int
Jeff Layton15b6a472012-05-16 07:50:15 -04001182cifs_parse_cache_flavor(char *value, struct smb_vol *vol)
1183{
1184 substring_t args[MAX_OPT_ARGS];
1185
1186 switch (match_token(value, cifs_cacheflavor_tokens, args)) {
1187 case Opt_cache_loose:
1188 vol->direct_io = false;
1189 vol->strict_io = false;
1190 break;
1191 case Opt_cache_strict:
1192 vol->direct_io = false;
1193 vol->strict_io = true;
1194 break;
1195 case Opt_cache_none:
1196 vol->direct_io = true;
1197 vol->strict_io = false;
1198 break;
1199 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001200 cifs_dbg(VFS, "bad cache= option: %s\n", value);
Jeff Layton15b6a472012-05-16 07:50:15 -04001201 return 1;
1202 }
1203 return 0;
1204}
1205
1206static int
Steve Frenchc7c137b2018-06-06 17:59:29 -05001207cifs_parse_smb_version(char *value, struct smb_vol *vol, bool is_smb3)
Jeff Layton23db65f2012-05-15 12:20:51 -04001208{
1209 substring_t args[MAX_OPT_ARGS];
1210
1211 switch (match_token(value, cifs_smb_version_tokens, args)) {
Steve French74204512018-06-19 14:34:08 -05001212#ifdef CONFIG_CIFS_ALLOW_INSECURE_LEGACY
Jeff Layton23db65f2012-05-15 12:20:51 -04001213 case Smb_1:
Steve Frenchf92a7202018-05-24 04:11:07 -05001214 if (disable_legacy_dialects) {
1215 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1216 return 1;
1217 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001218 if (is_smb3) {
1219 cifs_dbg(VFS, "vers=1.0 (cifs) not permitted when mounting with smb3\n");
1220 return 1;
1221 }
Jeff Layton23db65f2012-05-15 12:20:51 -04001222 vol->ops = &smb1_operations;
1223 vol->vals = &smb1_values;
1224 break;
Steve Frenchdd446b12012-11-28 23:21:06 -06001225 case Smb_20:
Steve Frenchf92a7202018-05-24 04:11:07 -05001226 if (disable_legacy_dialects) {
1227 cifs_dbg(VFS, "mount with legacy dialect disabled\n");
1228 return 1;
1229 }
Steve Frenchc7c137b2018-06-06 17:59:29 -05001230 if (is_smb3) {
1231 cifs_dbg(VFS, "vers=2.0 not permitted when mounting with smb3\n");
1232 return 1;
1233 }
Pavel Shilovsky53ef1012013-09-05 16:11:28 +04001234 vol->ops = &smb20_operations;
Steve Frenchdd446b12012-11-28 23:21:06 -06001235 vol->vals = &smb20_values;
1236 break;
Steve French74204512018-06-19 14:34:08 -05001237#else
1238 case Smb_1:
1239 cifs_dbg(VFS, "vers=1.0 (cifs) mount not permitted when legacy dialects disabled\n");
1240 return 1;
1241 case Smb_20:
1242 cifs_dbg(VFS, "vers=2.0 mount not permitted when legacy dialects disabled\n");
1243 return 1;
1244#endif /* CIFS_ALLOW_INSECURE_LEGACY */
Steve French1080ef72011-02-24 18:07:19 +00001245 case Smb_21:
1246 vol->ops = &smb21_operations;
1247 vol->vals = &smb21_values;
1248 break;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001249 case Smb_30:
Steve French38107d42012-12-08 22:08:06 -06001250 vol->ops = &smb30_operations;
Steve Frenche4aa25e2012-10-01 12:26:22 -05001251 vol->vals = &smb30_values;
1252 break;
Steve French20b6d8b2013-06-12 22:48:41 -05001253 case Smb_302:
1254 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1255 vol->vals = &smb302_values;
1256 break;
Steve French5f7fbf72014-12-17 22:52:58 -06001257 case Smb_311:
Steve Frenchaab18932015-06-23 23:37:11 -05001258 vol->ops = &smb311_operations;
Steve French5f7fbf72014-12-17 22:52:58 -06001259 vol->vals = &smb311_values;
1260 break;
Steve French9764c022017-09-17 10:41:35 -05001261 case Smb_3any:
1262 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1263 vol->vals = &smb3any_values;
1264 break;
1265 case Smb_default:
1266 vol->ops = &smb30_operations; /* currently identical with 3.0 */
1267 vol->vals = &smbdefault_values;
1268 break;
Jeff Layton23db65f2012-05-15 12:20:51 -04001269 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05001270 cifs_dbg(VFS, "Unknown vers= option specified: %s\n", value);
Jeff Layton23db65f2012-05-15 12:20:51 -04001271 return 1;
1272 }
1273 return 0;
1274}
1275
Jeff Laytond387a5c2012-12-10 06:10:46 -05001276/*
1277 * Parse a devname into substrings and populate the vol->UNC and vol->prepath
1278 * fields with the result. Returns 0 on success and an error otherwise.
1279 */
1280static int
1281cifs_parse_devname(const char *devname, struct smb_vol *vol)
1282{
1283 char *pos;
1284 const char *delims = "/\\";
1285 size_t len;
1286
1287 /* make sure we have a valid UNC double delimiter prefix */
1288 len = strspn(devname, delims);
1289 if (len != 2)
1290 return -EINVAL;
1291
1292 /* find delimiter between host and sharename */
1293 pos = strpbrk(devname + 2, delims);
1294 if (!pos)
1295 return -EINVAL;
1296
1297 /* skip past delimiter */
1298 ++pos;
1299
1300 /* now go until next delimiter or end of string */
1301 len = strcspn(pos, delims);
1302
1303 /* move "pos" up to delimiter or NULL */
1304 pos += len;
1305 vol->UNC = kstrndup(devname, pos - devname, GFP_KERNEL);
1306 if (!vol->UNC)
1307 return -ENOMEM;
1308
1309 convert_delimiter(vol->UNC, '\\');
1310
Sachin Prabhu11e31642016-02-08 13:44:01 +05301311 /* skip any delimiter */
1312 if (*pos == '/' || *pos == '\\')
1313 pos++;
1314
1315 /* If pos is NULL then no prepath */
1316 if (!*pos)
Jeff Laytond387a5c2012-12-10 06:10:46 -05001317 return 0;
1318
1319 vol->prepath = kstrdup(pos, GFP_KERNEL);
1320 if (!vol->prepath)
1321 return -ENOMEM;
1322
1323 return 0;
1324}
1325
Jeff Layton23db65f2012-05-15 12:20:51 -04001326static int
Sean Finneyb9468452011-04-11 13:19:32 +00001327cifs_parse_mount_options(const char *mountdata, const char *devname,
Steve Frenchc7c137b2018-06-06 17:59:29 -05001328 struct smb_vol *vol, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001330 char *data, *end;
Vasily Averin957df452011-06-06 11:33:12 +04001331 char *mountdata_copy = NULL, *options;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 unsigned int temp_len, i, j;
1333 char separator[2];
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001334 short int override_uid = -1;
1335 short int override_gid = -1;
1336 bool uid_specified = false;
1337 bool gid_specified = false;
Jeff Laytond8162552012-03-23 14:40:56 -04001338 bool sloppy = false;
1339 char *invalid = NULL;
Jeff Layton88463992010-11-22 15:31:03 -05001340 char *nodename = utsname()->nodename;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001341 char *string = NULL;
1342 char *tmp_end, *value;
1343 char delim;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001344 bool got_ip = false;
Steve French7e682f72017-08-31 21:34:24 -05001345 bool got_version = false;
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001346 unsigned short port = 0;
1347 struct sockaddr *dstaddr = (struct sockaddr *)&vol->dstaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
1349 separator[0] = ',';
Steve French50c2f752007-07-13 00:33:32 +00001350 separator[1] = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001351 delim = separator[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Jeff Layton6ee95422012-11-26 11:09:57 -05001353 /* ensure we always start with zeroed-out smb_vol */
1354 memset(vol, 0, sizeof(*vol));
1355
Jeff Layton88463992010-11-22 15:31:03 -05001356 /*
1357 * does not have to be perfect mapping since field is
1358 * informational, only used for servers that do not support
1359 * port 445 and it can be overridden at mount time
1360 */
Jeff Layton1397f2e2011-01-07 11:30:28 -05001361 memset(vol->source_rfc1001_name, 0x20, RFC1001_NAME_LEN);
1362 for (i = 0; i < strnlen(nodename, RFC1001_NAME_LEN); i++)
Jeff Layton88463992010-11-22 15:31:03 -05001363 vol->source_rfc1001_name[i] = toupper(nodename[i]);
1364
Jeff Layton1397f2e2011-01-07 11:30:28 -05001365 vol->source_rfc1001_name[RFC1001_NAME_LEN] = 0;
Steve Frencha10faeb22005-08-22 21:38:31 -07001366 /* null target name indicates to use *SMBSERVR default called name
1367 if we end up sending RFC1001 session initialize */
1368 vol->target_rfc1001_name[0] = 0;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04001369 vol->cred_uid = current_uid();
1370 vol->linux_uid = current_uid();
David Howellsa001e5b2008-11-14 10:38:47 +11001371 vol->linux_gid = current_gid();
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001372
Steve French2baa2682014-09-27 02:19:01 -05001373 /*
1374 * default to SFM style remapping of seven reserved characters
1375 * unless user overrides it or we negotiate CIFS POSIX where
1376 * it is unnecessary. Can not simultaneously use more than one mapping
1377 * since then readdir could list files that open could not open
1378 */
1379 vol->remap = true;
1380
Jeff Laytonf55ed1a2009-05-26 16:28:11 -04001381 /* default to only allowing write access to owner of the mount */
1382 vol->dir_mode = vol->file_mode = S_IRUGO | S_IXUGO | S_IWUSR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 /* vol->retry default is 0 (i.e. "soft" limited retry not hard retry) */
Jeremy Allisonac670552005-06-22 17:26:35 -07001385 /* default is always to request posix paths. */
1386 vol->posix_paths = 1;
Jeff Laytona0c92172009-05-27 15:40:47 -04001387 /* default to using server inode numbers where available */
1388 vol->server_ino = 1;
Jeremy Allisonac670552005-06-22 17:26:35 -07001389
Jeff Layton1b359202012-09-19 15:20:27 -07001390 /* default is to use strict cifs caching semantics */
1391 vol->strict_io = true;
1392
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05301393 vol->actimeo = CIFS_DEF_ACTIMEO;
1394
Steve French9764c022017-09-17 10:41:35 -05001395 /* offer SMB2.1 and later (SMB3 etc). Secure and widely accepted */
1396 vol->ops = &smb30_operations;
1397 vol->vals = &smbdefault_values;
Jeff Layton23db65f2012-05-15 12:20:51 -04001398
Rabin Vincentb782fcc2016-07-19 09:25:45 +02001399 vol->echo_interval = SMB_ECHO_INTERVAL_DEFAULT;
1400
Sean Finneyb9468452011-04-11 13:19:32 +00001401 if (!mountdata)
1402 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
Sean Finneyb9468452011-04-11 13:19:32 +00001404 mountdata_copy = kstrndup(mountdata, PAGE_SIZE, GFP_KERNEL);
1405 if (!mountdata_copy)
1406 goto cifs_parse_mount_err;
1407
1408 options = mountdata_copy;
Pavel Shilovsky4906e502011-04-14 22:00:56 +04001409 end = options + strlen(options);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001410
Steve French50c2f752007-07-13 00:33:32 +00001411 if (strncmp(options, "sep=", 4) == 0) {
Steve Frenchfb8c4b12007-07-10 01:16:18 +00001412 if (options[4] != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 separator[0] = options[4];
1414 options += 5;
1415 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05001416 cifs_dbg(FYI, "Null separator not allowed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 }
1418 }
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001419 vol->backupuid_specified = false; /* no backup intent for a user */
1420 vol->backupgid_specified = false; /* no backup intent for a group */
Steve French50c2f752007-07-13 00:33:32 +00001421
Jeff Layton37d4f992013-05-24 07:40:05 -04001422 switch (cifs_parse_devname(devname, vol)) {
1423 case 0:
1424 break;
1425 case -ENOMEM:
1426 cifs_dbg(VFS, "Unable to allocate memory for devname.\n");
1427 goto cifs_parse_mount_err;
1428 case -EINVAL:
1429 cifs_dbg(VFS, "Malformed UNC in devname.\n");
1430 goto cifs_parse_mount_err;
1431 default:
1432 cifs_dbg(VFS, "Unknown error parsing devname.\n");
1433 goto cifs_parse_mount_err;
Jeff Laytond387a5c2012-12-10 06:10:46 -05001434 }
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 while ((data = strsep(&options, separator)) != NULL) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001437 substring_t args[MAX_OPT_ARGS];
1438 unsigned long option;
1439 int token;
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (!*data)
1442 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001444 token = match_token(data, cifs_mount_option_tokens, args);
1445
1446 switch (token) {
1447
1448 /* Ingnore the following */
1449 case Opt_ignore:
1450 break;
1451
1452 /* Boolean values */
1453 case Opt_user_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 vol->no_xattr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001455 break;
1456 case Opt_nouser_xattr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 vol->no_xattr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001458 break;
1459 case Opt_forceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001460 override_uid = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001461 break;
1462 case Opt_noforceuid:
Jeff Layton9b9d6b242009-07-31 06:56:09 -04001463 override_uid = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001464 break;
Jeff Layton72bd4812012-10-03 16:02:36 -04001465 case Opt_forcegid:
1466 override_gid = 1;
1467 break;
1468 case Opt_noforcegid:
1469 override_gid = 0;
1470 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001471 case Opt_noblocksend:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001472 vol->noblocksnd = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001473 break;
1474 case Opt_noautotune:
Steve Frenchedf1ae42008-10-29 00:47:57 +00001475 vol->noautotune = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001476 break;
1477 case Opt_hard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001479 break;
1480 case Opt_soft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001482 break;
1483 case Opt_perm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 vol->noperm = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001485 break;
1486 case Opt_noperm:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 vol->noperm = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001488 break;
1489 case Opt_mapchars:
Steve French2baa2682014-09-27 02:19:01 -05001490 vol->sfu_remap = true;
1491 vol->remap = false; /* disable SFM mapping */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001492 break;
1493 case Opt_nomapchars:
Steve French2baa2682014-09-27 02:19:01 -05001494 vol->sfu_remap = false;
1495 break;
1496 case Opt_mapposix:
1497 vol->remap = true;
1498 vol->sfu_remap = false; /* disable SFU mapping */
1499 break;
1500 case Opt_nomapposix:
1501 vol->remap = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001502 break;
1503 case Opt_sfu:
Steve French50c2f752007-07-13 00:33:32 +00001504 vol->sfu_emul = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001505 break;
1506 case Opt_nosfu:
Steve French50c2f752007-07-13 00:33:32 +00001507 vol->sfu_emul = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001508 break;
1509 case Opt_nodfs:
Steve French2c1b8612008-10-16 18:35:21 +00001510 vol->nodfs = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001511 break;
1512 case Opt_posixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001513 vol->posix_paths = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001514 break;
1515 case Opt_noposixpaths:
Jeremy Allisonac670552005-06-22 17:26:35 -07001516 vol->posix_paths = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001517 break;
1518 case Opt_nounix:
Steve Frenchb3266142018-05-20 23:41:10 -05001519 if (vol->linux_ext)
1520 cifs_dbg(VFS,
1521 "conflicting unix mount options\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00001522 vol->no_linux_ext = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001523 break;
Steve Frenchb3266142018-05-20 23:41:10 -05001524 case Opt_unix:
1525 if (vol->no_linux_ext)
1526 cifs_dbg(VFS,
1527 "conflicting unix mount options\n");
1528 vol->linux_ext = 1;
1529 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001530 case Opt_nocase:
Steve French50c2f752007-07-13 00:33:32 +00001531 vol->nocase = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001532 break;
1533 case Opt_brl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001534 vol->nobrl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001535 break;
1536 case Opt_nobrl:
Steve Frenchc46fa8a2005-08-18 20:49:57 -07001537 vol->nobrl = 1;
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001538 /*
1539 * turn off mandatory locking in mode
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001540 * if remote locking is turned off since the
Pavel Shilovsky5cfdddc2012-03-27 20:51:15 +04001541 * local vfs will do advisory
1542 */
Steve French50c2f752007-07-13 00:33:32 +00001543 if (vol->file_mode ==
1544 (S_IALLUGO & ~(S_ISUID | S_IXGRP)))
Steve Frenchd3485d32005-08-19 11:04:29 -07001545 vol->file_mode = S_IALLUGO;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001546 break;
Steve French3d4ef9a2018-04-25 22:19:09 -05001547 case Opt_nohandlecache:
1548 vol->nohandlecache = 1;
1549 break;
1550 case Opt_handlecache:
1551 vol->nohandlecache = 0;
1552 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001553 case Opt_forcemandatorylock:
Steve French13a6e422008-12-02 17:24:33 +00001554 vol->mand_lock = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001555 break;
1556 case Opt_setuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 vol->setuids = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001558 break;
1559 case Opt_nosetuids:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 vol->setuids = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001561 break;
Steve French95932652016-09-23 01:36:34 -05001562 case Opt_setuidfromacl:
1563 vol->setuidfromacl = 1;
1564 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001565 case Opt_dynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001566 vol->dynperm = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001567 break;
1568 case Opt_nodynperm:
Jeff Laytond0a9c072008-05-12 22:23:49 +00001569 vol->dynperm = false;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001570 break;
1571 case Opt_nohard:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 vol->retry = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001573 break;
1574 case Opt_nosoft:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 vol->retry = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001576 break;
1577 case Opt_nointr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 vol->intr = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001579 break;
1580 case Opt_intr:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 vol->intr = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001582 break;
1583 case Opt_nostrictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001584 vol->nostrictsync = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001585 break;
1586 case Opt_strictsync:
Steve Frenchbe652442009-02-23 15:21:59 +00001587 vol->nostrictsync = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001588 break;
1589 case Opt_serverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 vol->server_ino = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001591 break;
1592 case Opt_noserverino:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 vol->server_ino = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001594 break;
1595 case Opt_rwpidforward:
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00001596 vol->rwpidforward = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001597 break;
1598 case Opt_cifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001599 vol->cifs_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001600 break;
1601 case Opt_nocifsacl:
Steve French0a4b92c2006-01-12 15:44:21 -08001602 vol->cifs_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001603 break;
1604 case Opt_acl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 vol->no_psx_acl = 0;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001606 break;
1607 case Opt_noacl:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 vol->no_psx_acl = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001609 break;
1610 case Opt_locallease:
Steve French84210e92008-10-23 04:42:37 +00001611 vol->local_lease = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001612 break;
1613 case Opt_sign:
Jeff Layton1e3cc572013-06-10 17:12:23 -05001614 vol->sign = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001615 break;
1616 case Opt_seal:
Steve French95b1cb92008-05-15 16:44:38 +00001617 /* we do not do the following in secFlags because seal
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001618 * is a per tree connection (mount) not a per socket
1619 * or per-smb connection option in the protocol
1620 * vol->secFlg |= CIFSSEC_MUST_SEAL;
1621 */
Steve French95b1cb92008-05-15 16:44:38 +00001622 vol->seal = 1;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001623 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001624 case Opt_noac:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001625 pr_warn("CIFS: Mount option noac not supported. Instead set /proc/fs/cifs/LookupCacheEnabled to 0\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001626 break;
1627 case Opt_fsc:
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301628#ifndef CONFIG_CIFS_FSCACHE
Joe Perchesf96637b2013-05-04 22:12:25 -05001629 cifs_dbg(VFS, "FS-Cache support needs CONFIG_CIFS_FSCACHE kernel config option set\n");
Sean Finneyb9468452011-04-11 13:19:32 +00001630 goto cifs_parse_mount_err;
Suresh Jayaraman607a5692010-11-24 17:49:05 +05301631#endif
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05301632 vol->fsc = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001633 break;
1634 case Opt_mfsymlinks:
Stefan Metzmacher736a33202010-07-30 14:56:00 +02001635 vol->mfsymlinks = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001636 break;
1637 case Opt_multiuser:
Jeff Layton0eb8a132010-10-06 19:51:12 -04001638 vol->multiuser = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001639 break;
Jeff Laytond8162552012-03-23 14:40:56 -04001640 case Opt_sloppy:
1641 sloppy = true;
1642 break;
Jeff Laytona0b3df52013-05-24 07:40:59 -04001643 case Opt_nosharesock:
1644 vol->nosharesock = true;
1645 break;
Steve Frenchb2a30772015-09-29 21:49:28 -05001646 case Opt_nopersistent:
1647 vol->nopersistent = true;
1648 if (vol->persistent) {
1649 cifs_dbg(VFS,
1650 "persistenthandles mount options conflict\n");
1651 goto cifs_parse_mount_err;
1652 }
1653 break;
1654 case Opt_persistent:
1655 vol->persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06001656 if ((vol->nopersistent) || (vol->resilient)) {
Steve Frenchb2a30772015-09-29 21:49:28 -05001657 cifs_dbg(VFS,
1658 "persistenthandles mount options conflict\n");
1659 goto cifs_parse_mount_err;
1660 }
1661 break;
Steve French592fafe2015-11-03 10:08:53 -06001662 case Opt_resilient:
1663 vol->resilient = true;
1664 if (vol->persistent) {
1665 cifs_dbg(VFS,
1666 "persistenthandles mount options conflict\n");
1667 goto cifs_parse_mount_err;
1668 }
1669 break;
1670 case Opt_noresilient:
1671 vol->resilient = false; /* already the default */
1672 break;
Germano Percossi39566442016-12-15 12:31:18 +05301673 case Opt_domainauto:
1674 vol->domainauto = true;
1675 break;
Long Li8339dd32017-11-07 01:54:55 -07001676 case Opt_rdma:
1677 vol->rdma = true;
1678 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001679
1680 /* Numeric Values */
1681 case Opt_backupuid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001682 if (get_option_uid(args, &vol->backupuid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001683 cifs_dbg(VFS, "%s: Invalid backupuid value\n",
1684 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001685 goto cifs_parse_mount_err;
1686 }
1687 vol->backupuid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001688 break;
1689 case Opt_backupgid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001690 if (get_option_gid(args, &vol->backupgid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001691 cifs_dbg(VFS, "%s: Invalid backupgid value\n",
1692 __func__);
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05001693 goto cifs_parse_mount_err;
1694 }
1695 vol->backupgid_specified = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001696 break;
1697 case Opt_uid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001698 if (get_option_uid(args, &vol->linux_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001699 cifs_dbg(VFS, "%s: Invalid uid value\n",
1700 __func__);
Sean Finneyb9468452011-04-11 13:19:32 +00001701 goto cifs_parse_mount_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001703 uid_specified = true;
1704 break;
1705 case Opt_cruid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001706 if (get_option_uid(args, &vol->cred_uid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001707 cifs_dbg(VFS, "%s: Invalid cruid value\n",
1708 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001709 goto cifs_parse_mount_err;
1710 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001711 break;
1712 case Opt_gid:
Eric W. Biederman3da46562013-02-06 01:37:39 -08001713 if (get_option_gid(args, &vol->linux_gid)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001714 cifs_dbg(VFS, "%s: Invalid gid value\n",
1715 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001716 goto cifs_parse_mount_err;
1717 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001718 gid_specified = true;
1719 break;
1720 case Opt_file_mode:
1721 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001722 cifs_dbg(VFS, "%s: Invalid file_mode value\n",
1723 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001724 goto cifs_parse_mount_err;
1725 }
1726 vol->file_mode = option;
1727 break;
1728 case Opt_dirmode:
1729 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001730 cifs_dbg(VFS, "%s: Invalid dir_mode value\n",
1731 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001732 goto cifs_parse_mount_err;
1733 }
1734 vol->dir_mode = option;
1735 break;
1736 case Opt_port:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001737 if (get_option_ul(args, &option) ||
1738 option > USHRT_MAX) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001739 cifs_dbg(VFS, "%s: Invalid port value\n",
1740 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001741 goto cifs_parse_mount_err;
1742 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001743 port = (unsigned short)option;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001744 break;
1745 case Opt_rsize:
1746 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001747 cifs_dbg(VFS, "%s: Invalid rsize value\n",
1748 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001749 goto cifs_parse_mount_err;
1750 }
1751 vol->rsize = option;
1752 break;
1753 case Opt_wsize:
1754 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001755 cifs_dbg(VFS, "%s: Invalid wsize value\n",
1756 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001757 goto cifs_parse_mount_err;
1758 }
1759 vol->wsize = option;
1760 break;
1761 case Opt_actimeo:
1762 if (get_option_ul(args, &option)) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001763 cifs_dbg(VFS, "%s: Invalid actimeo value\n",
1764 __func__);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001765 goto cifs_parse_mount_err;
1766 }
1767 vol->actimeo = HZ * option;
1768 if (vol->actimeo > CIFS_MAX_ACTIMEO) {
Joe Perchesf96637b2013-05-04 22:12:25 -05001769 cifs_dbg(VFS, "attribute cache timeout too large\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001770 goto cifs_parse_mount_err;
1771 }
1772 break;
Steve Frenchadfeb3e2015-12-18 12:31:36 -06001773 case Opt_echo_interval:
1774 if (get_option_ul(args, &option)) {
1775 cifs_dbg(VFS, "%s: Invalid echo interval value\n",
1776 __func__);
1777 goto cifs_parse_mount_err;
1778 }
1779 vol->echo_interval = option;
1780 break;
Steve French8b217fe2016-11-11 22:36:20 -06001781 case Opt_snapshot:
1782 if (get_option_ul(args, &option)) {
1783 cifs_dbg(VFS, "%s: Invalid snapshot time\n",
1784 __func__);
1785 goto cifs_parse_mount_err;
1786 }
1787 vol->snapshot_time = option;
1788 break;
Steve French141891f2016-09-23 00:44:16 -05001789 case Opt_max_credits:
1790 if (get_option_ul(args, &option) || (option < 20) ||
1791 (option > 60000)) {
1792 cifs_dbg(VFS, "%s: Invalid max_credits value\n",
1793 __func__);
1794 goto cifs_parse_mount_err;
1795 }
1796 vol->max_credits = option;
1797 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001798
1799 /* String Arguments */
1800
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001801 case Opt_blank_user:
1802 /* null user, ie. anonymous authentication */
1803 vol->nullauth = 1;
1804 vol->username = NULL;
1805 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001806 case Opt_user:
1807 string = match_strdup(args);
1808 if (string == NULL)
1809 goto out_nomem;
1810
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04001811 if (strnlen(string, CIFS_MAX_USERNAME_LEN) >
1812 CIFS_MAX_USERNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001813 pr_warn("CIFS: username too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001814 goto cifs_parse_mount_err;
1815 }
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001816
1817 kfree(vol->username);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001818 vol->username = kstrdup(string, GFP_KERNEL);
Joe Perchesf96637b2013-05-04 22:12:25 -05001819 if (!vol->username)
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001820 goto cifs_parse_mount_err;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001821 break;
1822 case Opt_blank_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001823 /* passwords have to be handled differently
1824 * to allow the character used for deliminator
1825 * to be passed within them
1826 */
1827
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001828 /*
1829 * Check if this is a case where the password
1830 * starts with a delimiter
1831 */
1832 tmp_end = strchr(data, '=');
1833 tmp_end++;
1834 if (!(tmp_end < end && tmp_end[1] == delim)) {
1835 /* No it is not. Set the password to NULL */
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001836 kzfree(vol->password);
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001837 vol->password = NULL;
1838 break;
1839 }
Gustavo A. R. Silva07fa6012018-11-27 10:01:51 +11001840 /* Fallthrough - to Opt_pass below.*/
Sachin Prabhuc369c9a2013-04-09 18:17:41 +01001841 case Opt_pass:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001842 /* Obtain the value string */
1843 value = strchr(data, '=');
Sachin Prabhu10238072012-03-28 18:07:08 +01001844 value++;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001845
1846 /* Set tmp_end to end of the string */
1847 tmp_end = (char *) value + strlen(value);
1848
1849 /* Check if following character is the deliminator
1850 * If yes, we have encountered a double deliminator
1851 * reset the NULL character to the deliminator
1852 */
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301853 if (tmp_end < end && tmp_end[1] == delim) {
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001854 tmp_end[0] = delim;
1855
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301856 /* Keep iterating until we get to a single
1857 * deliminator OR the end
1858 */
1859 while ((tmp_end = strchr(tmp_end, delim))
1860 != NULL && (tmp_end[1] == delim)) {
1861 tmp_end = (char *) &tmp_end[2];
1862 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001863
Suresh Jayaramane73f8432012-06-12 07:15:50 +05301864 /* Reset var options to point to next element */
1865 if (tmp_end) {
1866 tmp_end[0] = '\0';
1867 options = (char *) &tmp_end[1];
1868 } else
1869 /* Reached the end of the mount option
1870 * string */
1871 options = end;
1872 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001873
Aurelien Aptel97f4b722018-01-25 15:59:39 +01001874 kzfree(vol->password);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001875 /* Now build new password string */
1876 temp_len = strlen(value);
1877 vol->password = kzalloc(temp_len+1, GFP_KERNEL);
1878 if (vol->password == NULL) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001879 pr_warn("CIFS: no memory for password\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001880 goto cifs_parse_mount_err;
1881 }
1882
1883 for (i = 0, j = 0; i < temp_len; i++, j++) {
1884 vol->password[j] = value[i];
1885 if ((value[i] == delim) &&
1886 value[i+1] == delim)
1887 /* skip the second deliminator */
1888 i++;
1889 }
1890 vol->password[j] = '\0';
1891 break;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001892 case Opt_blank_ip:
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001893 /* FIXME: should this be an error instead? */
1894 got_ip = false;
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001895 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001896 case Opt_ip:
1897 string = match_strdup(args);
1898 if (string == NULL)
1899 goto out_nomem;
1900
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001901 if (!cifs_convert_address(dstaddr, string,
1902 strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001903 pr_err("CIFS: bad ip= option (%s).\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001904 goto cifs_parse_mount_err;
1905 }
Jeff Laytonb979aaa2012-11-26 11:09:55 -05001906 got_ip = true;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001907 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001908 case Opt_domain:
1909 string = match_strdup(args);
1910 if (string == NULL)
1911 goto out_nomem;
1912
Chen Gang057d6332013-07-19 09:01:36 +08001913 if (strnlen(string, CIFS_MAX_DOMAINNAME_LEN)
1914 == CIFS_MAX_DOMAINNAME_LEN) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001915 pr_warn("CIFS: domain name too long\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001916 goto cifs_parse_mount_err;
1917 }
1918
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001919 kfree(vol->domainname);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001920 vol->domainname = kstrdup(string, GFP_KERNEL);
1921 if (!vol->domainname) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001922 pr_warn("CIFS: no memory for domainname\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001923 goto cifs_parse_mount_err;
1924 }
Joe Perchesf96637b2013-05-04 22:12:25 -05001925 cifs_dbg(FYI, "Domain name set\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001926 break;
1927 case Opt_srcaddr:
1928 string = match_strdup(args);
1929 if (string == NULL)
1930 goto out_nomem;
1931
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001932 if (!cifs_convert_address(
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001933 (struct sockaddr *)&vol->srcaddr,
1934 string, strlen(string))) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001935 pr_warn("CIFS: Could not parse srcaddr: %s\n",
1936 string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001937 goto cifs_parse_mount_err;
1938 }
1939 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001940 case Opt_iocharset:
1941 string = match_strdup(args);
1942 if (string == NULL)
1943 goto out_nomem;
1944
Sachin Prabhu4fe9e962012-04-10 18:12:27 +01001945 if (strnlen(string, 1024) >= 65) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001946 pr_warn("CIFS: iocharset name too long.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001947 goto cifs_parse_mount_err;
1948 }
1949
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07001950 if (strncasecmp(string, "default", 7) != 0) {
Taesoo Kim2bd50fb2015-03-21 19:08:30 -04001951 kfree(vol->iocharset);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001952 vol->iocharset = kstrdup(string,
1953 GFP_KERNEL);
1954 if (!vol->iocharset) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001955 pr_warn("CIFS: no memory for charset\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001956 goto cifs_parse_mount_err;
1957 }
1958 }
1959 /* if iocharset not set then load_nls_default
1960 * is used by caller
1961 */
Joe Perchesf96637b2013-05-04 22:12:25 -05001962 cifs_dbg(FYI, "iocharset set to %s\n", string);
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001963 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001964 case Opt_netbiosname:
1965 string = match_strdup(args);
1966 if (string == NULL)
1967 goto out_nomem;
1968
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001969 memset(vol->source_rfc1001_name, 0x20,
1970 RFC1001_NAME_LEN);
1971 /*
1972 * FIXME: are there cases in which a comma can
1973 * be valid in workstation netbios name (and
1974 * need special handling)?
1975 */
1976 for (i = 0; i < RFC1001_NAME_LEN; i++) {
1977 /* don't ucase netbiosname for user */
1978 if (string[i] == 0)
1979 break;
1980 vol->source_rfc1001_name[i] = string[i];
1981 }
1982 /* The string has 16th byte zero still from
1983 * set at top of the function
1984 */
1985 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03001986 pr_warn("CIFS: netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001987 break;
1988 case Opt_servern:
1989 /* servernetbiosname specified override *SMBSERVER */
1990 string = match_strdup(args);
1991 if (string == NULL)
1992 goto out_nomem;
1993
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04001994 /* last byte, type, is 0x20 for servr type */
1995 memset(vol->target_rfc1001_name, 0x20,
1996 RFC1001_NAME_LEN_WITH_NULL);
1997
1998 /* BB are there cases in which a comma can be
1999 valid in this workstation netbios name
2000 (and need special handling)? */
2001
2002 /* user or mount helper must uppercase the
2003 netbios name */
2004 for (i = 0; i < 15; i++) {
2005 if (string[i] == 0)
2006 break;
2007 vol->target_rfc1001_name[i] = string[i];
2008 }
2009 /* The string has 16th byte zero still from
2010 set at top of the function */
2011 if (i == RFC1001_NAME_LEN && string[i] != 0)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002012 pr_warn("CIFS: server netbiosname longer than 15 truncated.\n");
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002013 break;
2014 case Opt_ver:
Steve French7e682f72017-08-31 21:34:24 -05002015 /* version of mount userspace tools, not dialect */
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002016 string = match_strdup(args);
2017 if (string == NULL)
2018 goto out_nomem;
2019
Steve French7e682f72017-08-31 21:34:24 -05002020 /* If interface changes in mount.cifs bump to new ver */
Rasmus Villemoes87e747c2014-10-13 15:54:35 -07002021 if (strncasecmp(string, "1", 1) == 0) {
Steve French7e682f72017-08-31 21:34:24 -05002022 if (strlen(string) > 1) {
2023 pr_warn("Bad mount helper ver=%s. Did "
2024 "you want SMB1 (CIFS) dialect "
2025 "and mean to type vers=1.0 "
2026 "instead?\n", string);
2027 goto cifs_parse_mount_err;
2028 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002029 /* This is the default */
2030 break;
2031 }
2032 /* For all other value, error */
Steve French7e682f72017-08-31 21:34:24 -05002033 pr_warn("CIFS: Invalid mount helper version specified\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002034 goto cifs_parse_mount_err;
Jeff Layton23db65f2012-05-15 12:20:51 -04002035 case Opt_vers:
Steve French7e682f72017-08-31 21:34:24 -05002036 /* protocol version (dialect) */
Jeff Layton23db65f2012-05-15 12:20:51 -04002037 string = match_strdup(args);
2038 if (string == NULL)
2039 goto out_nomem;
2040
Steve Frenchc7c137b2018-06-06 17:59:29 -05002041 if (cifs_parse_smb_version(string, vol, is_smb3) != 0)
Jeff Layton23db65f2012-05-15 12:20:51 -04002042 goto cifs_parse_mount_err;
Steve French7e682f72017-08-31 21:34:24 -05002043 got_version = true;
Jeff Layton23db65f2012-05-15 12:20:51 -04002044 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002045 case Opt_sec:
2046 string = match_strdup(args);
2047 if (string == NULL)
2048 goto out_nomem;
2049
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002050 if (cifs_parse_security_flavors(string, vol) != 0)
2051 goto cifs_parse_mount_err;
2052 break;
Jeff Layton15b6a472012-05-16 07:50:15 -04002053 case Opt_cache:
2054 string = match_strdup(args);
2055 if (string == NULL)
2056 goto out_nomem;
2057
2058 if (cifs_parse_cache_flavor(string, vol) != 0)
2059 goto cifs_parse_mount_err;
2060 break;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002061 default:
Jeff Laytond8162552012-03-23 14:40:56 -04002062 /*
2063 * An option we don't recognize. Save it off for later
2064 * if we haven't already found one
2065 */
2066 if (!invalid)
2067 invalid = data;
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002068 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002070 /* Free up any allocated string */
2071 kfree(string);
2072 string = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002074
Jeff Laytond8162552012-03-23 14:40:56 -04002075 if (!sloppy && invalid) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002076 pr_err("CIFS: Unknown mount option \"%s\"\n", invalid);
Jeff Laytond8162552012-03-23 14:40:56 -04002077 goto cifs_parse_mount_err;
2078 }
2079
Long Li8339dd32017-11-07 01:54:55 -07002080 if (vol->rdma && vol->vals->protocol_id < SMB30_PROT_ID) {
2081 cifs_dbg(VFS, "SMB Direct requires Version >=3.0\n");
2082 goto cifs_parse_mount_err;
2083 }
2084
Jeff Layton8a8798a2012-01-17 16:09:15 -05002085#ifndef CONFIG_KEYS
2086 /* Muliuser mounts require CONFIG_KEYS support */
2087 if (vol->multiuser) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002088 cifs_dbg(VFS, "Multiuser mounts require kernels with CONFIG_KEYS enabled\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002089 goto cifs_parse_mount_err;
Jeff Layton0eb8a132010-10-06 19:51:12 -04002090 }
Jeff Layton8a8798a2012-01-17 16:09:15 -05002091#endif
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002092 if (!vol->UNC) {
Jeff Layton37d4f992013-05-24 07:40:05 -04002093 cifs_dbg(VFS, "CIFS mount error: No usable UNC path provided in device string!\n");
Jeff Laytone5e69ab2012-11-25 08:00:42 -05002094 goto cifs_parse_mount_err;
2095 }
Jeff Layton0eb8a132010-10-06 19:51:12 -04002096
Jeff Layton62a1a432012-12-10 06:10:45 -05002097 /* make sure UNC has a share name */
2098 if (!strchr(vol->UNC + 3, '\\')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002099 cifs_dbg(VFS, "Malformed UNC. Unable to find share name.\n");
Jeff Layton62a1a432012-12-10 06:10:45 -05002100 goto cifs_parse_mount_err;
2101 }
2102
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002103 if (!got_ip) {
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002104 int len;
2105 const char *slash;
2106
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002107 /* No ip= option specified? Try to get it from UNC */
Daniel N Pettersson29bb3152017-04-27 11:32:36 +02002108 /* Use the address part of the UNC. */
2109 slash = strchr(&vol->UNC[2], '\\');
2110 len = slash - &vol->UNC[2];
2111 if (!cifs_convert_address(dstaddr, &vol->UNC[2], len)) {
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002112 pr_err("Unable to determine destination address.\n");
Jeff Laytonb979aaa2012-11-26 11:09:55 -05002113 goto cifs_parse_mount_err;
2114 }
2115 }
2116
2117 /* set the port that we got earlier */
2118 cifs_set_port(dstaddr, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002120 if (uid_specified)
2121 vol->override_uid = override_uid;
2122 else if (override_uid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002123 pr_notice("CIFS: ignoring forceuid mount option specified with no uid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002124
2125 if (gid_specified)
2126 vol->override_gid = override_gid;
2127 else if (override_gid == 1)
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002128 pr_notice("CIFS: ignoring forcegid mount option specified with no gid= option.\n");
Jeff Layton9b9d6b242009-07-31 06:56:09 -04002129
Steve French7e682f72017-08-31 21:34:24 -05002130 if (got_version == false)
2131 pr_warn("No dialect specified on mount. Default has changed to "
Steve French9764c022017-09-17 10:41:35 -05002132 "a more secure dialect, SMB2.1 or later (e.g. SMB3), from CIFS "
Steve French7e682f72017-08-31 21:34:24 -05002133 "(SMB1). To use the less secure SMB1 dialect to access "
Steve French9764c022017-09-17 10:41:35 -05002134 "old servers which do not support SMB3 (or SMB2.1) specify vers=1.0"
2135 " on mount.\n");
Steve French7e682f72017-08-31 21:34:24 -05002136
Sean Finneyb9468452011-04-11 13:19:32 +00002137 kfree(mountdata_copy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 return 0;
Sean Finneyb9468452011-04-11 13:19:32 +00002139
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002140out_nomem:
Andy Shevchenko0b456f02014-08-27 16:49:44 +03002141 pr_warn("Could not allocate temporary buffer\n");
Sean Finneyb9468452011-04-11 13:19:32 +00002142cifs_parse_mount_err:
Sachin Prabhu8830d7e2012-03-23 14:40:56 -04002143 kfree(string);
Sean Finneyb9468452011-04-11 13:19:32 +00002144 kfree(mountdata_copy);
2145 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146}
2147
Ben Greear3eb9a882010-09-01 17:06:02 -07002148/** Returns true if srcaddr isn't specified and rhs isn't
2149 * specified, or if srcaddr is specified and
2150 * matches the IP address of the rhs argument.
2151 */
Jeff Layton45151482010-07-06 20:43:02 -04002152static bool
Ben Greear3eb9a882010-09-01 17:06:02 -07002153srcip_matches(struct sockaddr *srcaddr, struct sockaddr *rhs)
2154{
2155 switch (srcaddr->sa_family) {
2156 case AF_UNSPEC:
2157 return (rhs->sa_family == AF_UNSPEC);
2158 case AF_INET: {
2159 struct sockaddr_in *saddr4 = (struct sockaddr_in *)srcaddr;
2160 struct sockaddr_in *vaddr4 = (struct sockaddr_in *)rhs;
2161 return (saddr4->sin_addr.s_addr == vaddr4->sin_addr.s_addr);
2162 }
2163 case AF_INET6: {
2164 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *)srcaddr;
Nickolai Zeldoviche3e27752013-01-16 21:36:17 -05002165 struct sockaddr_in6 *vaddr6 = (struct sockaddr_in6 *)rhs;
Ben Greear3eb9a882010-09-01 17:06:02 -07002166 return ipv6_addr_equal(&saddr6->sin6_addr, &vaddr6->sin6_addr);
2167 }
2168 default:
2169 WARN_ON(1);
2170 return false; /* don't expect to be here */
2171 }
2172}
2173
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002174/*
2175 * If no port is specified in addr structure, we try to match with 445 port
2176 * and if it fails - with 139 ports. It should be called only if address
2177 * families of server and addr are equal.
2178 */
2179static bool
2180match_port(struct TCP_Server_Info *server, struct sockaddr *addr)
2181{
Steve French6da97912011-03-13 18:55:55 +00002182 __be16 port, *sport;
Pavel Shilovsky4b886132010-12-13 22:18:07 +03002183
2184 switch (addr->sa_family) {
2185 case AF_INET:
2186 sport = &((struct sockaddr_in *) &server->dstaddr)->sin_port;
2187 port = ((struct sockaddr_in *) addr)->sin_port;
2188 break;
2189 case AF_INET6:
2190 sport = &((struct sockaddr_in6 *) &server->dstaddr)->sin6_port;
2191 port = ((struct sockaddr_in6 *) addr)->sin6_port;
2192 break;
2193 default:
2194 WARN_ON(1);
2195 return false;
2196 }
2197
2198 if (!port) {
2199 port = htons(CIFS_PORT);
2200 if (port == *sport)
2201 return true;
2202
2203 port = htons(RFC1001_PORT);
2204 }
2205
2206 return port == *sport;
2207}
Ben Greear3eb9a882010-09-01 17:06:02 -07002208
2209static bool
2210match_address(struct TCP_Server_Info *server, struct sockaddr *addr,
2211 struct sockaddr *srcaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212{
Jeff Layton45151482010-07-06 20:43:02 -04002213 switch (addr->sa_family) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002214 case AF_INET: {
2215 struct sockaddr_in *addr4 = (struct sockaddr_in *)addr;
2216 struct sockaddr_in *srv_addr4 =
2217 (struct sockaddr_in *)&server->dstaddr;
2218
2219 if (addr4->sin_addr.s_addr != srv_addr4->sin_addr.s_addr)
Jeff Layton45151482010-07-06 20:43:02 -04002220 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002221 break;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002222 }
2223 case AF_INET6: {
2224 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)addr;
2225 struct sockaddr_in6 *srv_addr6 =
2226 (struct sockaddr_in6 *)&server->dstaddr;
2227
Jeff Layton45151482010-07-06 20:43:02 -04002228 if (!ipv6_addr_equal(&addr6->sin6_addr,
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002229 &srv_addr6->sin6_addr))
Jeff Layton45151482010-07-06 20:43:02 -04002230 return false;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002231 if (addr6->sin6_scope_id != srv_addr6->sin6_scope_id)
Jeff Layton45151482010-07-06 20:43:02 -04002232 return false;
Jeff Layton45151482010-07-06 20:43:02 -04002233 break;
2234 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002235 default:
2236 WARN_ON(1);
2237 return false; /* don't expect to be here */
2238 }
Jeff Layton45151482010-07-06 20:43:02 -04002239
Ben Greear3eb9a882010-09-01 17:06:02 -07002240 if (!srcip_matches(srcaddr, (struct sockaddr *)&server->srcaddr))
2241 return false;
2242
Jeff Layton45151482010-07-06 20:43:02 -04002243 return true;
2244}
2245
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002246static bool
2247match_security(struct TCP_Server_Info *server, struct smb_vol *vol)
2248{
Jeff Layton3f618222013-06-12 19:52:14 -05002249 /*
2250 * The select_sectype function should either return the vol->sectype
2251 * that was specified, or "Unspecified" if that sectype was not
2252 * compatible with the given NEGOTIATE request.
2253 */
Sachin Prabhuef65aae2017-01-18 15:35:57 +05302254 if (server->ops->select_sectype(server, vol->sectype)
2255 == Unspecified)
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002256 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002257
Jeff Layton3f618222013-06-12 19:52:14 -05002258 /*
2259 * Now check if signing mode is acceptable. No need to check
2260 * global_secflags at this point since if MUST_SIGN is set then
2261 * the server->sign had better be too.
2262 */
Jeff Layton38d77c52013-05-26 07:01:00 -04002263 if (vol->sign && !server->sign)
2264 return false;
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002265
2266 return true;
2267}
2268
Jeff Layton9fa114f2012-11-26 11:09:57 -05002269static int match_server(struct TCP_Server_Info *server, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002270{
Jeff Layton9fa114f2012-11-26 11:09:57 -05002271 struct sockaddr *addr = (struct sockaddr *)&vol->dstaddr;
2272
Jeff Laytona0b3df52013-05-24 07:40:59 -04002273 if (vol->nosharesock)
2274 return 0;
2275
Steve French9764c022017-09-17 10:41:35 -05002276 /* BB update this for smb3any and default case */
Jeff Layton23db65f2012-05-15 12:20:51 -04002277 if ((server->vals != vol->vals) || (server->ops != vol->ops))
2278 return 0;
2279
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002280 if (!net_eq(cifs_net_ns(server), current->nsproxy->net_ns))
2281 return 0;
2282
2283 if (!match_address(server, addr,
2284 (struct sockaddr *)&vol->srcaddr))
2285 return 0;
2286
2287 if (!match_port(server, addr))
2288 return 0;
2289
2290 if (!match_security(server, vol))
2291 return 0;
2292
Rabin Vincentb782fcc2016-07-19 09:25:45 +02002293 if (server->echo_interval != vol->echo_interval * HZ)
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002294 return 0;
2295
Long Li8339dd32017-11-07 01:54:55 -07002296 if (server->rdma != vol->rdma)
2297 return 0;
2298
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002299 return 1;
2300}
2301
Paulo Alcantara54be1f62018-11-14 16:01:21 -02002302struct TCP_Server_Info *
Jeff Layton9fa114f2012-11-26 11:09:57 -05002303cifs_find_tcp_session(struct smb_vol *vol)
Jeff Layton45151482010-07-06 20:43:02 -04002304{
Jeff Laytone7ddee92008-11-14 13:44:38 -05002305 struct TCP_Server_Info *server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302307 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton45151482010-07-06 20:43:02 -04002308 list_for_each_entry(server, &cifs_tcp_ses_list, tcp_ses_list) {
Jeff Layton9fa114f2012-11-26 11:09:57 -05002309 if (!match_server(server, vol))
Jeff Laytondaf5b0b2010-07-06 20:43:02 -04002310 continue;
2311
Jeff Laytone7ddee92008-11-14 13:44:38 -05002312 ++server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302313 spin_unlock(&cifs_tcp_ses_lock);
Joe Perchesf96637b2013-05-04 22:12:25 -05002314 cifs_dbg(FYI, "Existing tcp session with server found\n");
Jeff Laytone7ddee92008-11-14 13:44:38 -05002315 return server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302317 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 return NULL;
2319}
2320
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002321void
2322cifs_put_tcp_session(struct TCP_Server_Info *server, int from_reconnect)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323{
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002324 struct task_struct *task;
2325
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302326 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002327 if (--server->srv_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302328 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002329 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
Steve Frenchdea570e02008-05-06 22:05:51 +00002331
Rob Landleyf1d0c992011-01-22 15:44:05 -06002332 put_net(cifs_net_ns(server));
2333
Jeff Laytone7ddee92008-11-14 13:44:38 -05002334 list_del_init(&server->tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302335 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytone7ddee92008-11-14 13:44:38 -05002336
Jeff Laytonc74093b2011-01-11 07:24:23 -05002337 cancel_delayed_work_sync(&server->echo);
2338
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002339 if (from_reconnect)
2340 /*
2341 * Avoid deadlock here: reconnect work calls
2342 * cifs_put_tcp_session() at its end. Need to be sure
2343 * that reconnect work does nothing with server pointer after
2344 * that step.
2345 */
2346 cancel_delayed_work(&server->reconnect);
2347 else
2348 cancel_delayed_work_sync(&server->reconnect);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002349
Jeff Laytone7ddee92008-11-14 13:44:38 -05002350 spin_lock(&GlobalMid_Lock);
2351 server->tcpStatus = CifsExiting;
2352 spin_unlock(&GlobalMid_Lock);
2353
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002354 cifs_crypto_secmech_release(server);
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302355 cifs_fscache_release_client_cookie(server);
2356
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05002357 kfree(server->session_key.response);
2358 server->session_key.response = NULL;
2359 server->session_key.len = 0;
Steve Frencha5c3e1c2014-09-16 04:16:19 -05002360
2361 task = xchg(&server->tsk, NULL);
2362 if (task)
2363 force_sig(SIGKILL, task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364}
2365
Jeff Layton63c038c2008-12-01 18:41:46 -05002366static struct TCP_Server_Info *
2367cifs_get_tcp_session(struct smb_vol *volume_info)
2368{
2369 struct TCP_Server_Info *tcp_ses = NULL;
Jeff Layton63c038c2008-12-01 18:41:46 -05002370 int rc;
2371
Joe Perchesf96637b2013-05-04 22:12:25 -05002372 cifs_dbg(FYI, "UNC: %s\n", volume_info->UNC);
Jeff Layton63c038c2008-12-01 18:41:46 -05002373
2374 /* see if we already have a matching tcp_ses */
Jeff Layton9fa114f2012-11-26 11:09:57 -05002375 tcp_ses = cifs_find_tcp_session(volume_info);
Jeff Layton63c038c2008-12-01 18:41:46 -05002376 if (tcp_ses)
2377 return tcp_ses;
2378
2379 tcp_ses = kzalloc(sizeof(struct TCP_Server_Info), GFP_KERNEL);
2380 if (!tcp_ses) {
2381 rc = -ENOMEM;
2382 goto out_err;
2383 }
2384
Jeff Layton23db65f2012-05-15 12:20:51 -04002385 tcp_ses->ops = volume_info->ops;
2386 tcp_ses->vals = volume_info->vals;
Rob Landleyf1d0c992011-01-22 15:44:05 -06002387 cifs_set_net_ns(tcp_ses, get_net(current->nsproxy->net_ns));
Jeff Layton63c038c2008-12-01 18:41:46 -05002388 tcp_ses->hostname = extract_hostname(volume_info->UNC);
2389 if (IS_ERR(tcp_ses->hostname)) {
2390 rc = PTR_ERR(tcp_ses->hostname);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002391 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002392 }
2393
2394 tcp_ses->noblocksnd = volume_info->noblocksnd;
2395 tcp_ses->noautotune = volume_info->noautotune;
Steve French6a5fa2362010-01-01 01:28:43 +00002396 tcp_ses->tcp_nodelay = volume_info->sockopt_tcp_nodelay;
Long Li8339dd32017-11-07 01:54:55 -07002397 tcp_ses->rdma = volume_info->rdma;
Pavel Shilovskyfc40f9c2012-02-17 17:09:12 +03002398 tcp_ses->in_flight = 0;
Pavel Shilovsky2d86dbc2012-02-06 15:59:18 +04002399 tcp_ses->credits = 1;
Jeff Layton63c038c2008-12-01 18:41:46 -05002400 init_waitqueue_head(&tcp_ses->response_q);
2401 init_waitqueue_head(&tcp_ses->request_q);
2402 INIT_LIST_HEAD(&tcp_ses->pending_mid_q);
2403 mutex_init(&tcp_ses->srv_mutex);
2404 memcpy(tcp_ses->workstation_RFC1001_name,
2405 volume_info->source_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
2406 memcpy(tcp_ses->server_RFC1001_name,
2407 volume_info->target_rfc1001_name, RFC1001_NAME_LEN_WITH_NULL);
Shirish Pargaonkar5d0d2882010-10-13 18:15:00 -05002408 tcp_ses->session_estab = false;
Jeff Layton63c038c2008-12-01 18:41:46 -05002409 tcp_ses->sequence_number = 0;
Steve French9e1a37d2018-09-19 02:38:17 -05002410 tcp_ses->reconnect_instance = 0;
Steve Frenchfda35942011-01-20 18:06:34 +00002411 tcp_ses->lstrp = jiffies;
Jeff Layton58fa0152012-05-01 17:41:16 -04002412 spin_lock_init(&tcp_ses->req_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002413 INIT_LIST_HEAD(&tcp_ses->tcp_ses_list);
2414 INIT_LIST_HEAD(&tcp_ses->smb_ses_list);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002415 INIT_DELAYED_WORK(&tcp_ses->echo, cifs_echo_request);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002416 INIT_DELAYED_WORK(&tcp_ses->reconnect, smb2_reconnect_server);
2417 mutex_init(&tcp_ses->reconnect_mutex);
Jeff Layton9fa114f2012-11-26 11:09:57 -05002418 memcpy(&tcp_ses->srcaddr, &volume_info->srcaddr,
2419 sizeof(tcp_ses->srcaddr));
2420 memcpy(&tcp_ses->dstaddr, &volume_info->dstaddr,
2421 sizeof(tcp_ses->dstaddr));
Steve Frenchfa70b872016-09-22 00:39:34 -05002422 generate_random_uuid(tcp_ses->client_guid);
Jeff Layton63c038c2008-12-01 18:41:46 -05002423 /*
2424 * at this point we are the only ones with the pointer
2425 * to the struct since the kernel thread not created yet
2426 * no need to spinlock this init of tcpStatus or srv_count
2427 */
2428 tcp_ses->tcpStatus = CifsNew;
2429 ++tcp_ses->srv_count;
2430
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002431 if (volume_info->echo_interval >= SMB_ECHO_INTERVAL_MIN &&
2432 volume_info->echo_interval <= SMB_ECHO_INTERVAL_MAX)
2433 tcp_ses->echo_interval = volume_info->echo_interval * HZ;
2434 else
2435 tcp_ses->echo_interval = SMB_ECHO_INTERVAL_DEFAULT * HZ;
Long Li2f894642017-11-22 17:38:34 -07002436 if (tcp_ses->rdma) {
2437#ifndef CONFIG_CIFS_SMB_DIRECT
2438 cifs_dbg(VFS, "CONFIG_CIFS_SMB_DIRECT is not enabled\n");
2439 rc = -ENOENT;
2440 goto out_err_crypto_release;
2441#endif
2442 tcp_ses->smbd_conn = smbd_get_connection(
2443 tcp_ses, (struct sockaddr *)&volume_info->dstaddr);
2444 if (tcp_ses->smbd_conn) {
2445 cifs_dbg(VFS, "RDMA transport established\n");
2446 rc = 0;
2447 goto smbd_connected;
2448 } else {
2449 rc = -ENOENT;
2450 goto out_err_crypto_release;
2451 }
2452 }
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002453 rc = ip_connect(tcp_ses);
Jeff Layton63c038c2008-12-01 18:41:46 -05002454 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002455 cifs_dbg(VFS, "Error connecting to socket. Aborting operation.\n");
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002456 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002457 }
Long Li2f894642017-11-22 17:38:34 -07002458smbd_connected:
Jeff Layton63c038c2008-12-01 18:41:46 -05002459 /*
2460 * since we're in a cifs function already, we know that
2461 * this will succeed. No need for try_module_get().
2462 */
2463 __module_get(THIS_MODULE);
Al Viro7c97c202011-06-21 08:51:28 -04002464 tcp_ses->tsk = kthread_run(cifs_demultiplex_thread,
Jeff Layton63c038c2008-12-01 18:41:46 -05002465 tcp_ses, "cifsd");
2466 if (IS_ERR(tcp_ses->tsk)) {
2467 rc = PTR_ERR(tcp_ses->tsk);
Joe Perchesf96637b2013-05-04 22:12:25 -05002468 cifs_dbg(VFS, "error %d create cifsd thread\n", rc);
Jeff Layton63c038c2008-12-01 18:41:46 -05002469 module_put(THIS_MODULE);
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002470 goto out_err_crypto_release;
Jeff Layton63c038c2008-12-01 18:41:46 -05002471 }
Steve Frenchfd88ce92011-04-12 01:01:14 +00002472 tcp_ses->tcpStatus = CifsNeedNegotiate;
Jeff Layton63c038c2008-12-01 18:41:46 -05002473
2474 /* thread spawned, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302475 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002476 list_add(&tcp_ses->tcp_ses_list, &cifs_tcp_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302477 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton63c038c2008-12-01 18:41:46 -05002478
Suresh Jayaraman488f1d2d2010-07-05 18:12:15 +05302479 cifs_fscache_get_client_cookie(tcp_ses);
2480
Jeff Laytonc74093b2011-01-11 07:24:23 -05002481 /* queue echo request delayed work */
Steve Frenchadfeb3e2015-12-18 12:31:36 -06002482 queue_delayed_work(cifsiod_wq, &tcp_ses->echo, tcp_ses->echo_interval);
Jeff Laytonc74093b2011-01-11 07:24:23 -05002483
Jeff Layton63c038c2008-12-01 18:41:46 -05002484 return tcp_ses;
2485
Shirish Pargaonkarf7c5445a2010-10-26 18:10:24 -05002486out_err_crypto_release:
Pavel Shilovsky026e93d2016-11-03 16:47:37 -07002487 cifs_crypto_secmech_release(tcp_ses);
Shirish Pargaonkard2b91522010-10-21 14:25:08 -05002488
Rob Landleyf1d0c992011-01-22 15:44:05 -06002489 put_net(cifs_net_ns(tcp_ses));
2490
Jeff Layton63c038c2008-12-01 18:41:46 -05002491out_err:
2492 if (tcp_ses) {
Steve French8347a5c2009-10-06 18:31:29 +00002493 if (!IS_ERR(tcp_ses->hostname))
2494 kfree(tcp_ses->hostname);
Jeff Layton63c038c2008-12-01 18:41:46 -05002495 if (tcp_ses->ssocket)
2496 sock_release(tcp_ses->ssocket);
2497 kfree(tcp_ses);
2498 }
2499 return ERR_PTR(rc);
2500}
2501
Steve French96daf2b2011-05-27 04:34:02 +00002502static int match_session(struct cifs_ses *ses, struct smb_vol *vol)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002503{
Jeff Layton3f618222013-06-12 19:52:14 -05002504 if (vol->sectype != Unspecified &&
2505 vol->sectype != ses->sectype)
2506 return 0;
2507
2508 switch (ses->sectype) {
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002509 case Kerberos:
Eric W. Biederman64ed39d2013-02-06 02:30:39 -08002510 if (!uid_eq(vol->cred_uid, ses->cred_uid))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002511 return 0;
2512 break;
2513 default:
Jeff Layton04febab2012-01-17 16:09:15 -05002514 /* NULL username means anonymous session */
2515 if (ses->user_name == NULL) {
2516 if (!vol->nullauth)
2517 return 0;
2518 break;
2519 }
2520
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002521 /* anything else takes username/password */
Jeff Layton04febab2012-01-17 16:09:15 -05002522 if (strncmp(ses->user_name,
2523 vol->username ? vol->username : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002524 CIFS_MAX_USERNAME_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002525 return 0;
Jeff Layton08b37d52014-05-23 06:53:10 -04002526 if ((vol->username && strlen(vol->username) != 0) &&
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002527 ses->password != NULL &&
2528 strncmp(ses->password,
2529 vol->password ? vol->password : "",
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002530 CIFS_MAX_PASSWORD_LEN))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002531 return 0;
2532 }
2533 return 1;
2534}
2535
Aurelien Aptelb327a712018-01-24 13:46:10 +01002536/**
2537 * cifs_setup_ipc - helper to setup the IPC tcon for the session
2538 *
2539 * A new IPC connection is made and stored in the session
2540 * tcon_ipc. The IPC tcon has the same lifetime as the session.
2541 */
2542static int
2543cifs_setup_ipc(struct cifs_ses *ses, struct smb_vol *volume_info)
2544{
2545 int rc = 0, xid;
2546 struct cifs_tcon *tcon;
2547 struct nls_table *nls_codepage;
2548 char unc[SERVER_NAME_LENGTH + sizeof("//x/IPC$")] = {0};
2549 bool seal = false;
2550
2551 /*
2552 * If the mount request that resulted in the creation of the
2553 * session requires encryption, force IPC to be encrypted too.
2554 */
2555 if (volume_info->seal) {
2556 if (ses->server->capabilities & SMB2_GLOBAL_CAP_ENCRYPTION)
2557 seal = true;
2558 else {
2559 cifs_dbg(VFS,
2560 "IPC: server doesn't support encryption\n");
2561 return -EOPNOTSUPP;
2562 }
2563 }
2564
2565 tcon = tconInfoAlloc();
2566 if (tcon == NULL)
2567 return -ENOMEM;
2568
Thomas Werschlein395a2072018-08-30 18:29:20 +02002569 snprintf(unc, sizeof(unc), "\\\\%s\\IPC$", ses->server->hostname);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002570
2571 /* cannot fail */
2572 nls_codepage = load_nls_default();
2573
2574 xid = get_xid();
2575 tcon->ses = ses;
2576 tcon->ipc = true;
2577 tcon->seal = seal;
2578 rc = ses->server->ops->tree_connect(xid, ses, unc, tcon, nls_codepage);
2579 free_xid(xid);
2580
2581 if (rc) {
2582 cifs_dbg(VFS, "failed to connect to IPC (rc=%d)\n", rc);
2583 tconInfoFree(tcon);
2584 goto out;
2585 }
2586
2587 cifs_dbg(FYI, "IPC tcon rc = %d ipc tid = %d\n", rc, tcon->tid);
2588
2589 ses->tcon_ipc = tcon;
2590out:
2591 unload_nls(nls_codepage);
2592 return rc;
2593}
2594
2595/**
2596 * cifs_free_ipc - helper to release the session IPC tcon
2597 *
2598 * Needs to be called everytime a session is destroyed
2599 */
2600static int
2601cifs_free_ipc(struct cifs_ses *ses)
2602{
2603 int rc = 0, xid;
2604 struct cifs_tcon *tcon = ses->tcon_ipc;
2605
2606 if (tcon == NULL)
2607 return 0;
2608
2609 if (ses->server->ops->tree_disconnect) {
2610 xid = get_xid();
2611 rc = ses->server->ops->tree_disconnect(xid, tcon);
2612 free_xid(xid);
2613 }
2614
2615 if (rc)
2616 cifs_dbg(FYI, "failed to disconnect IPC tcon (rc=%d)\n", rc);
2617
2618 tconInfoFree(tcon);
2619 ses->tcon_ipc = NULL;
2620 return rc;
2621}
2622
Steve French96daf2b2011-05-27 04:34:02 +00002623static struct cifs_ses *
Jeff Layton4ff67b72010-07-06 20:43:02 -04002624cifs_find_smb_ses(struct TCP_Server_Info *server, struct smb_vol *vol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
Steve French96daf2b2011-05-27 04:34:02 +00002626 struct cifs_ses *ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302628 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton4ff67b72010-07-06 20:43:02 -04002629 list_for_each_entry(ses, &server->smb_ses_list, smb_ses_list) {
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002630 if (ses->status == CifsExiting)
2631 continue;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002632 if (!match_session(ses, vol))
2633 continue;
Jeff Layton14fbf502008-11-14 13:53:46 -05002634 ++ses->ses_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302635 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002636 return ses;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302638 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 return NULL;
2640}
2641
Jeff Layton14fbf502008-11-14 13:53:46 -05002642static void
Steve French96daf2b2011-05-27 04:34:02 +00002643cifs_put_smb_ses(struct cifs_ses *ses)
Jeff Layton14fbf502008-11-14 13:53:46 -05002644{
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002645 unsigned int rc, xid;
Jeff Layton14fbf502008-11-14 13:53:46 -05002646 struct TCP_Server_Info *server = ses->server;
2647
Joe Perchesf96637b2013-05-04 22:12:25 -05002648 cifs_dbg(FYI, "%s: ses_count=%d\n", __func__, ses->ses_count);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002649
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302650 spin_lock(&cifs_tcp_ses_lock);
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002651 if (ses->status == CifsExiting) {
2652 spin_unlock(&cifs_tcp_ses_lock);
2653 return;
2654 }
Jeff Layton14fbf502008-11-14 13:53:46 -05002655 if (--ses->ses_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302656 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002657 return;
2658 }
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002659 if (ses->status == CifsGood)
2660 ses->status = CifsExiting;
2661 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002662
Aurelien Aptelb327a712018-01-24 13:46:10 +01002663 cifs_free_ipc(ses);
2664
Shirish Pargaonkar7f485582013-10-12 10:06:03 -05002665 if (ses->status == CifsExiting && server->ops->logoff) {
2666 xid = get_xid();
2667 rc = server->ops->logoff(xid, ses);
2668 if (rc)
2669 cifs_dbg(VFS, "%s: Session Logoff failure rc=%d\n",
2670 __func__, rc);
2671 _free_xid(xid);
2672 }
2673
2674 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002675 list_del_init(&ses->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302676 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton14fbf502008-11-14 13:53:46 -05002677
Jeff Layton14fbf502008-11-14 13:53:46 -05002678 sesInfoFree(ses);
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002679 cifs_put_tcp_session(server, 0);
Jeff Layton14fbf502008-11-14 13:53:46 -05002680}
2681
Jeff Layton8a8798a2012-01-17 16:09:15 -05002682#ifdef CONFIG_KEYS
2683
Chen Gang057d6332013-07-19 09:01:36 +08002684/* strlen("cifs:a:") + CIFS_MAX_DOMAINNAME_LEN + 1 */
2685#define CIFSCREDS_DESC_SIZE (7 + CIFS_MAX_DOMAINNAME_LEN + 1)
Jeff Layton8a8798a2012-01-17 16:09:15 -05002686
2687/* Populate username and pw fields from keyring if possible */
2688static int
2689cifs_set_cifscreds(struct smb_vol *vol, struct cifs_ses *ses)
2690{
2691 int rc = 0;
David Howells146aa8b2015-10-21 14:04:48 +01002692 const char *delim, *payload;
2693 char *desc;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002694 ssize_t len;
2695 struct key *key;
2696 struct TCP_Server_Info *server = ses->server;
2697 struct sockaddr_in *sa;
2698 struct sockaddr_in6 *sa6;
David Howells146aa8b2015-10-21 14:04:48 +01002699 const struct user_key_payload *upayload;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002700
2701 desc = kmalloc(CIFSCREDS_DESC_SIZE, GFP_KERNEL);
2702 if (!desc)
2703 return -ENOMEM;
2704
2705 /* try to find an address key first */
2706 switch (server->dstaddr.ss_family) {
2707 case AF_INET:
2708 sa = (struct sockaddr_in *)&server->dstaddr;
2709 sprintf(desc, "cifs:a:%pI4", &sa->sin_addr.s_addr);
2710 break;
2711 case AF_INET6:
2712 sa6 = (struct sockaddr_in6 *)&server->dstaddr;
2713 sprintf(desc, "cifs:a:%pI6c", &sa6->sin6_addr.s6_addr);
2714 break;
2715 default:
Joe Perchesf96637b2013-05-04 22:12:25 -05002716 cifs_dbg(FYI, "Bad ss_family (%hu)\n",
2717 server->dstaddr.ss_family);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002718 rc = -EINVAL;
2719 goto out_err;
2720 }
2721
Joe Perchesf96637b2013-05-04 22:12:25 -05002722 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002723 key = request_key(&key_type_logon, desc, "");
2724 if (IS_ERR(key)) {
2725 if (!ses->domainName) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002726 cifs_dbg(FYI, "domainName is NULL\n");
Jeff Layton8a8798a2012-01-17 16:09:15 -05002727 rc = PTR_ERR(key);
2728 goto out_err;
2729 }
2730
2731 /* didn't work, try to find a domain key */
2732 sprintf(desc, "cifs:d:%s", ses->domainName);
Joe Perchesf96637b2013-05-04 22:12:25 -05002733 cifs_dbg(FYI, "%s: desc=%s\n", __func__, desc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002734 key = request_key(&key_type_logon, desc, "");
2735 if (IS_ERR(key)) {
2736 rc = PTR_ERR(key);
2737 goto out_err;
2738 }
2739 }
2740
2741 down_read(&key->sem);
David Howells0837e492017-03-01 15:11:23 +00002742 upayload = user_key_payload_locked(key);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002743 if (IS_ERR_OR_NULL(upayload)) {
Jeff Layton4edc53c2012-02-07 06:30:51 -05002744 rc = upayload ? PTR_ERR(upayload) : -EINVAL;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002745 goto out_key_put;
2746 }
2747
2748 /* find first : in payload */
David Howells146aa8b2015-10-21 14:04:48 +01002749 payload = upayload->data;
Jeff Layton8a8798a2012-01-17 16:09:15 -05002750 delim = strnchr(payload, upayload->datalen, ':');
Joe Perchesf96637b2013-05-04 22:12:25 -05002751 cifs_dbg(FYI, "payload=%s\n", payload);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002752 if (!delim) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002753 cifs_dbg(FYI, "Unable to find ':' in payload (datalen=%d)\n",
2754 upayload->datalen);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002755 rc = -EINVAL;
2756 goto out_key_put;
2757 }
2758
2759 len = delim - payload;
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002760 if (len > CIFS_MAX_USERNAME_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002761 cifs_dbg(FYI, "Bad value from username search (len=%zd)\n",
2762 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002763 rc = -EINVAL;
2764 goto out_key_put;
2765 }
2766
2767 vol->username = kstrndup(payload, len, GFP_KERNEL);
2768 if (!vol->username) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002769 cifs_dbg(FYI, "Unable to allocate %zd bytes for username\n",
2770 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002771 rc = -ENOMEM;
2772 goto out_key_put;
2773 }
Joe Perchesf96637b2013-05-04 22:12:25 -05002774 cifs_dbg(FYI, "%s: username=%s\n", __func__, vol->username);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002775
2776 len = key->datalen - (len + 1);
Scott Lovenberg8c3a2b42013-08-09 08:47:17 -04002777 if (len > CIFS_MAX_PASSWORD_LEN || len <= 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002778 cifs_dbg(FYI, "Bad len for password search (len=%zd)\n", len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002779 rc = -EINVAL;
2780 kfree(vol->username);
2781 vol->username = NULL;
2782 goto out_key_put;
2783 }
2784
2785 ++delim;
2786 vol->password = kstrndup(delim, len, GFP_KERNEL);
2787 if (!vol->password) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002788 cifs_dbg(FYI, "Unable to allocate %zd bytes for password\n",
2789 len);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002790 rc = -ENOMEM;
2791 kfree(vol->username);
2792 vol->username = NULL;
2793 goto out_key_put;
2794 }
2795
2796out_key_put:
2797 up_read(&key->sem);
2798 key_put(key);
2799out_err:
2800 kfree(desc);
Joe Perchesf96637b2013-05-04 22:12:25 -05002801 cifs_dbg(FYI, "%s: returning %d\n", __func__, rc);
Jeff Layton8a8798a2012-01-17 16:09:15 -05002802 return rc;
2803}
2804#else /* ! CONFIG_KEYS */
2805static inline int
2806cifs_set_cifscreds(struct smb_vol *vol __attribute__((unused)),
2807 struct cifs_ses *ses __attribute__((unused)))
2808{
2809 return -ENOSYS;
2810}
2811#endif /* CONFIG_KEYS */
2812
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002813/**
2814 * cifs_get_smb_ses - get a session matching @volume_info data from @server
2815 *
2816 * This function assumes it is being called from cifs_mount() where we
2817 * already got a server reference (server refcount +1). See
2818 * cifs_get_tcon() for refcount explanations.
2819 */
Steve French96daf2b2011-05-27 04:34:02 +00002820static struct cifs_ses *
Jeff Layton36988c72010-04-24 07:57:43 -04002821cifs_get_smb_ses(struct TCP_Server_Info *server, struct smb_vol *volume_info)
2822{
Pavel Shilovsky286170a2012-05-25 10:43:58 +04002823 int rc = -ENOMEM;
2824 unsigned int xid;
Steve French96daf2b2011-05-27 04:34:02 +00002825 struct cifs_ses *ses;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002826 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
2827 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
Jeff Layton36988c72010-04-24 07:57:43 -04002828
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002829 xid = get_xid();
Jeff Layton36988c72010-04-24 07:57:43 -04002830
Jeff Layton4ff67b72010-07-06 20:43:02 -04002831 ses = cifs_find_smb_ses(server, volume_info);
Jeff Layton36988c72010-04-24 07:57:43 -04002832 if (ses) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002833 cifs_dbg(FYI, "Existing smb sess found (status=%d)\n",
2834 ses->status);
Jeff Layton36988c72010-04-24 07:57:43 -04002835
Jeff Layton36988c72010-04-24 07:57:43 -04002836 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002837 rc = cifs_negotiate_protocol(xid, ses);
2838 if (rc) {
2839 mutex_unlock(&ses->session_mutex);
2840 /* problem -- put our ses reference */
2841 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002842 free_xid(xid);
Jeff Layton198b5682010-04-24 07:57:48 -04002843 return ERR_PTR(rc);
2844 }
Jeff Layton36988c72010-04-24 07:57:43 -04002845 if (ses->need_reconnect) {
Joe Perchesf96637b2013-05-04 22:12:25 -05002846 cifs_dbg(FYI, "Session needs reconnect\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002847 rc = cifs_setup_session(xid, ses,
2848 volume_info->local_nls);
2849 if (rc) {
2850 mutex_unlock(&ses->session_mutex);
2851 /* problem -- put our reference */
2852 cifs_put_smb_ses(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002853 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002854 return ERR_PTR(rc);
2855 }
2856 }
2857 mutex_unlock(&ses->session_mutex);
Jeff Layton460cf342010-09-14 11:38:24 -04002858
2859 /* existing SMB ses has a server reference already */
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002860 cifs_put_tcp_session(server, 0);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002861 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002862 return ses;
2863 }
2864
Joe Perchesf96637b2013-05-04 22:12:25 -05002865 cifs_dbg(FYI, "Existing smb sess not found\n");
Jeff Layton36988c72010-04-24 07:57:43 -04002866 ses = sesInfoAlloc();
2867 if (ses == NULL)
2868 goto get_ses_fail;
2869
2870 /* new SMB session uses our server ref */
2871 ses->server = server;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002872 if (server->dstaddr.ss_family == AF_INET6)
2873 sprintf(ses->serverName, "%pI6", &addr6->sin6_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002874 else
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03002875 sprintf(ses->serverName, "%pI4", &addr->sin_addr);
Jeff Layton36988c72010-04-24 07:57:43 -04002876
Steve French8727c8a2011-02-25 01:11:56 -06002877 if (volume_info->username) {
2878 ses->user_name = kstrdup(volume_info->username, GFP_KERNEL);
2879 if (!ses->user_name)
2880 goto get_ses_fail;
2881 }
Jeff Layton36988c72010-04-24 07:57:43 -04002882
2883 /* volume_info->password freed at unmount */
2884 if (volume_info->password) {
2885 ses->password = kstrdup(volume_info->password, GFP_KERNEL);
2886 if (!ses->password)
2887 goto get_ses_fail;
2888 }
2889 if (volume_info->domainname) {
Shirish Pargaonkard3686d52010-10-28 09:53:07 -05002890 ses->domainName = kstrdup(volume_info->domainname, GFP_KERNEL);
2891 if (!ses->domainName)
2892 goto get_ses_fail;
Jeff Layton36988c72010-04-24 07:57:43 -04002893 }
Germano Percossi39566442016-12-15 12:31:18 +05302894 if (volume_info->domainauto)
2895 ses->domainAuto = volume_info->domainauto;
Jeff Layton3e4b3e12010-07-19 18:00:17 -04002896 ses->cred_uid = volume_info->cred_uid;
Jeff Layton36988c72010-04-24 07:57:43 -04002897 ses->linux_uid = volume_info->linux_uid;
Steve Frenchd9b94202011-04-12 01:24:57 +00002898
Jeff Layton28e11bd2013-05-26 07:01:00 -04002899 ses->sectype = volume_info->sectype;
2900 ses->sign = volume_info->sign;
Jeff Layton36988c72010-04-24 07:57:43 -04002901
2902 mutex_lock(&ses->session_mutex);
Jeff Layton198b5682010-04-24 07:57:48 -04002903 rc = cifs_negotiate_protocol(xid, ses);
2904 if (!rc)
2905 rc = cifs_setup_session(xid, ses, volume_info->local_nls);
Jeff Layton36988c72010-04-24 07:57:43 -04002906 mutex_unlock(&ses->session_mutex);
Steve Frenchc8e56f12010-09-08 21:10:58 +00002907 if (rc)
Jeff Layton36988c72010-04-24 07:57:43 -04002908 goto get_ses_fail;
2909
2910 /* success, put it on the list */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302911 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002912 list_add(&ses->smb_ses_list, &server->smb_ses_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302913 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton36988c72010-04-24 07:57:43 -04002914
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002915 free_xid(xid);
Aurelien Aptelb327a712018-01-24 13:46:10 +01002916
2917 cifs_setup_ipc(ses, volume_info);
2918
Jeff Layton36988c72010-04-24 07:57:43 -04002919 return ses;
2920
2921get_ses_fail:
2922 sesInfoFree(ses);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002923 free_xid(xid);
Jeff Layton36988c72010-04-24 07:57:43 -04002924 return ERR_PTR(rc);
2925}
2926
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002927static int match_tcon(struct cifs_tcon *tcon, struct smb_vol *volume_info)
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002928{
2929 if (tcon->tidStatus == CifsExiting)
2930 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002931 if (strncmp(tcon->treeName, volume_info->UNC, MAX_TREE_SIZE))
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002932 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002933 if (tcon->seal != volume_info->seal)
2934 return 0;
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002935 if (tcon->snapshot_time != volume_info->snapshot_time)
2936 return 0;
Pavel Shilovsky37bb04e2011-05-05 09:55:11 +00002937 return 1;
2938}
2939
Steve French96daf2b2011-05-27 04:34:02 +00002940static struct cifs_tcon *
Steve French8b217fe2016-11-11 22:36:20 -06002941cifs_find_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
2943 struct list_head *tmp;
Steve French96daf2b2011-05-27 04:34:02 +00002944 struct cifs_tcon *tcon;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302946 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002947 list_for_each(tmp, &ses->tcon_list) {
Steve French96daf2b2011-05-27 04:34:02 +00002948 tcon = list_entry(tmp, struct cifs_tcon, tcon_list);
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08002949 if (!match_tcon(tcon, volume_info))
Jeff Laytonf1987b42008-11-15 11:12:47 -05002950 continue;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002951 ++tcon->tc_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302952 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 return tcon;
2954 }
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302955 spin_unlock(&cifs_tcp_ses_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 return NULL;
2957}
2958
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07002959void
Steve French96daf2b2011-05-27 04:34:02 +00002960cifs_put_tcon(struct cifs_tcon *tcon)
Jeff Laytonf1987b42008-11-15 11:12:47 -05002961{
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002962 unsigned int xid;
Aurelien Aptelb327a712018-01-24 13:46:10 +01002963 struct cifs_ses *ses;
Jeff Laytonf1987b42008-11-15 11:12:47 -05002964
Aurelien Aptelb327a712018-01-24 13:46:10 +01002965 /*
2966 * IPC tcon share the lifetime of their session and are
2967 * destroyed in the session put function
2968 */
2969 if (tcon == NULL || tcon->ipc)
2970 return;
2971
2972 ses = tcon->ses;
Joe Perchesf96637b2013-05-04 22:12:25 -05002973 cifs_dbg(FYI, "%s: tc_count=%d\n", __func__, tcon->tc_count);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302974 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002975 if (--tcon->tc_count > 0) {
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302976 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002977 return;
2978 }
2979
2980 list_del_init(&tcon->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05302981 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002982
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002983 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04002984 if (ses->server->ops->tree_disconnect)
2985 ses->server->ops->tree_disconnect(xid, tcon);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04002986 _free_xid(xid);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002987
Suresh Jayaramand03382c2010-07-05 18:12:27 +05302988 cifs_fscache_release_super_cookie(tcon);
Steve French9f841592010-07-23 20:37:53 +00002989 tconInfoFree(tcon);
Jeff Laytonf1987b42008-11-15 11:12:47 -05002990 cifs_put_smb_ses(ses);
2991}
2992
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01002993/**
2994 * cifs_get_tcon - get a tcon matching @volume_info data from @ses
2995 *
2996 * - tcon refcount is the number of mount points using the tcon.
2997 * - ses refcount is the number of tcon using the session.
2998 *
2999 * 1. This function assumes it is being called from cifs_mount() where
3000 * we already got a session reference (ses refcount +1).
3001 *
3002 * 2. Since we're in the context of adding a mount point, the end
3003 * result should be either:
3004 *
3005 * a) a new tcon already allocated with refcount=1 (1 mount point) and
3006 * its session refcount incremented (1 new tcon). This +1 was
3007 * already done in (1).
3008 *
3009 * b) an existing tcon with refcount+1 (add a mount point to it) and
3010 * identical ses refcount (no new tcon). Because of (1) we need to
3011 * decrement the ses refcount.
3012 */
Steve French96daf2b2011-05-27 04:34:02 +00003013static struct cifs_tcon *
3014cifs_get_tcon(struct cifs_ses *ses, struct smb_vol *volume_info)
Jeff Laytond00c28d2010-04-24 07:57:44 -04003015{
3016 int rc, xid;
Steve French96daf2b2011-05-27 04:34:02 +00003017 struct cifs_tcon *tcon;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003018
Steve French8b217fe2016-11-11 22:36:20 -06003019 tcon = cifs_find_tcon(ses, volume_info);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003020 if (tcon) {
Aurelien Aptel4a1360d2018-01-25 18:47:52 +01003021 /*
3022 * tcon has refcount already incremented but we need to
3023 * decrement extra ses reference gotten by caller (case b)
3024 */
Joe Perchesf96637b2013-05-04 22:12:25 -05003025 cifs_dbg(FYI, "Found match on UNC path\n");
Jeff Laytond00c28d2010-04-24 07:57:44 -04003026 cifs_put_smb_ses(ses);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003027 return tcon;
3028 }
3029
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003030 if (!ses->server->ops->tree_connect) {
3031 rc = -ENOSYS;
3032 goto out_fail;
3033 }
3034
Jeff Laytond00c28d2010-04-24 07:57:44 -04003035 tcon = tconInfoAlloc();
3036 if (tcon == NULL) {
3037 rc = -ENOMEM;
3038 goto out_fail;
3039 }
3040
Steve French8b217fe2016-11-11 22:36:20 -06003041 if (volume_info->snapshot_time) {
Steve French8b217fe2016-11-11 22:36:20 -06003042 if (ses->server->vals->protocol_id == 0) {
3043 cifs_dbg(VFS,
3044 "Use SMB2 or later for snapshot mount option\n");
3045 rc = -EOPNOTSUPP;
3046 goto out_fail;
3047 } else
3048 tcon->snapshot_time = volume_info->snapshot_time;
Steve French8b217fe2016-11-11 22:36:20 -06003049 }
3050
Jeff Laytond00c28d2010-04-24 07:57:44 -04003051 tcon->ses = ses;
3052 if (volume_info->password) {
3053 tcon->password = kstrdup(volume_info->password, GFP_KERNEL);
3054 if (!tcon->password) {
3055 rc = -ENOMEM;
3056 goto out_fail;
3057 }
3058 }
3059
Steve French23657ad2018-04-22 15:14:58 -05003060 if (volume_info->seal) {
3061 if (ses->server->vals->protocol_id == 0) {
3062 cifs_dbg(VFS,
3063 "SMB3 or later required for encryption\n");
3064 rc = -EOPNOTSUPP;
3065 goto out_fail;
3066 } else if (tcon->ses->server->capabilities &
3067 SMB2_GLOBAL_CAP_ENCRYPTION)
3068 tcon->seal = true;
3069 else {
3070 cifs_dbg(VFS, "Encryption is not supported on share\n");
3071 rc = -EOPNOTSUPP;
3072 goto out_fail;
3073 }
3074 }
3075
Steve French8505c8b2018-06-18 14:01:59 -05003076 if (volume_info->linux_ext) {
3077 if (ses->server->posix_ext_supported) {
Steve Frenchb3266142018-05-20 23:41:10 -05003078 tcon->posix_extensions = true;
Steve French2fbb5642018-06-12 12:11:31 -05003079 printk_once(KERN_WARNING
3080 "SMB3.11 POSIX Extensions are experimental\n");
Steve French8505c8b2018-06-18 14:01:59 -05003081 } else {
3082 cifs_dbg(VFS, "Server does not support mounting with posix SMB3.11 extensions.\n");
3083 rc = -EOPNOTSUPP;
3084 goto out_fail;
Steve French2fbb5642018-06-12 12:11:31 -05003085 }
Steve Frenchb3266142018-05-20 23:41:10 -05003086 }
Steve Frenchb3266142018-05-20 23:41:10 -05003087
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003088 /*
3089 * BB Do we need to wrap session_mutex around this TCon call and Unix
3090 * SetFS as we do on SessSetup and reconnect?
3091 */
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003092 xid = get_xid();
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003093 rc = ses->server->ops->tree_connect(xid, ses, volume_info->UNC, tcon,
3094 volume_info->local_nls);
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003095 free_xid(xid);
Joe Perchesf96637b2013-05-04 22:12:25 -05003096 cifs_dbg(FYI, "Tcon rc = %d\n", rc);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003097 if (rc)
3098 goto out_fail;
3099
Steve Frenchb618f002015-11-03 09:15:03 -06003100 tcon->use_persistent = false;
3101 /* check if SMB2 or later, CIFS does not support persistent handles */
3102 if (volume_info->persistent) {
3103 if (ses->server->vals->protocol_id == 0) {
3104 cifs_dbg(VFS,
3105 "SMB3 or later required for persistent handles\n");
3106 rc = -EOPNOTSUPP;
3107 goto out_fail;
3108 } else if (ses->server->capabilities &
3109 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3110 tcon->use_persistent = true;
3111 else /* persistent handles requested but not supported */ {
3112 cifs_dbg(VFS,
3113 "Persistent handles not supported on share\n");
3114 rc = -EOPNOTSUPP;
3115 goto out_fail;
3116 }
3117 } else if ((tcon->capabilities & SMB2_SHARE_CAP_CONTINUOUS_AVAILABILITY)
3118 && (ses->server->capabilities & SMB2_GLOBAL_CAP_PERSISTENT_HANDLES)
3119 && (volume_info->nopersistent == false)) {
3120 cifs_dbg(FYI, "enabling persistent handles\n");
3121 tcon->use_persistent = true;
Steve French592fafe2015-11-03 10:08:53 -06003122 } else if (volume_info->resilient) {
3123 if (ses->server->vals->protocol_id == 0) {
3124 cifs_dbg(VFS,
3125 "SMB2.1 or later required for resilient handles\n");
3126 rc = -EOPNOTSUPP;
3127 goto out_fail;
3128 }
3129 tcon->use_resilient = true;
Steve Frenchb618f002015-11-03 09:15:03 -06003130 }
3131
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04003132 /*
3133 * We can have only one retry value for a connection to a share so for
3134 * resources mounted more than once to the same server share the last
3135 * value passed in for the retry flag is used.
3136 */
Jeff Laytond00c28d2010-04-24 07:57:44 -04003137 tcon->retry = volume_info->retry;
3138 tcon->nocase = volume_info->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05003139 tcon->nohandlecache = volume_info->nohandlecache;
Jeff Laytond00c28d2010-04-24 07:57:44 -04003140 tcon->local_lease = volume_info->local_lease;
Pavel Shilovsky233839b2012-09-19 06:22:45 -07003141 INIT_LIST_HEAD(&tcon->pending_opens);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003142
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303143 spin_lock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003144 list_add(&tcon->tcon_list, &ses->tcon_list);
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05303145 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytond00c28d2010-04-24 07:57:44 -04003146
Suresh Jayaramand03382c2010-07-05 18:12:27 +05303147 cifs_fscache_get_super_cookie(tcon);
3148
Jeff Laytond00c28d2010-04-24 07:57:44 -04003149 return tcon;
3150
3151out_fail:
3152 tconInfoFree(tcon);
3153 return ERR_PTR(rc);
3154}
3155
Jeff Layton9d002df2010-10-06 19:51:11 -04003156void
3157cifs_put_tlink(struct tcon_link *tlink)
3158{
3159 if (!tlink || IS_ERR(tlink))
3160 return;
3161
3162 if (!atomic_dec_and_test(&tlink->tl_count) ||
3163 test_bit(TCON_LINK_IN_TREE, &tlink->tl_flags)) {
3164 tlink->tl_time = jiffies;
3165 return;
3166 }
3167
3168 if (!IS_ERR(tlink_tcon(tlink)))
3169 cifs_put_tcon(tlink_tcon(tlink));
3170 kfree(tlink);
3171 return;
3172}
Jeff Laytond00c28d2010-04-24 07:57:44 -04003173
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003174static int
3175compare_mount_options(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3176{
3177 struct cifs_sb_info *old = CIFS_SB(sb);
3178 struct cifs_sb_info *new = mnt_data->cifs_sb;
3179
3180 if ((sb->s_flags & CIFS_MS_MASK) != (mnt_data->flags & CIFS_MS_MASK))
3181 return 0;
3182
3183 if ((old->mnt_cifs_flags & CIFS_MOUNT_MASK) !=
3184 (new->mnt_cifs_flags & CIFS_MOUNT_MASK))
3185 return 0;
3186
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003187 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003188 * We want to share sb only if we don't specify an r/wsize or
3189 * specified r/wsize is greater than or equal to existing one.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003190 */
3191 if (new->wsize && new->wsize < old->wsize)
3192 return 0;
3193
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003194 if (new->rsize && new->rsize < old->rsize)
3195 return 0;
3196
Eric W. Biederman1f682332013-02-06 01:20:20 -08003197 if (!uid_eq(old->mnt_uid, new->mnt_uid) || !gid_eq(old->mnt_gid, new->mnt_gid))
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003198 return 0;
3199
3200 if (old->mnt_file_mode != new->mnt_file_mode ||
3201 old->mnt_dir_mode != new->mnt_dir_mode)
3202 return 0;
3203
3204 if (strcmp(old->local_nls->charset, new->local_nls->charset))
3205 return 0;
3206
3207 if (old->actimeo != new->actimeo)
3208 return 0;
3209
3210 return 1;
3211}
3212
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003213static int
3214match_prepath(struct super_block *sb, struct cifs_mnt_data *mnt_data)
3215{
3216 struct cifs_sb_info *old = CIFS_SB(sb);
3217 struct cifs_sb_info *new = mnt_data->cifs_sb;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003218 bool old_set = old->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
3219 bool new_set = new->mnt_cifs_flags & CIFS_MOUNT_USE_PREFIX_PATH;
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003220
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003221 if (old_set && new_set && !strcmp(new->prepath, old->prepath))
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003222 return 1;
Sachin Prabhucd8c4292017-04-26 14:05:46 +01003223 else if (!old_set && !new_set)
3224 return 1;
3225
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003226 return 0;
3227}
3228
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003229int
3230cifs_match_super(struct super_block *sb, void *data)
3231{
3232 struct cifs_mnt_data *mnt_data = (struct cifs_mnt_data *)data;
3233 struct smb_vol *volume_info;
3234 struct cifs_sb_info *cifs_sb;
3235 struct TCP_Server_Info *tcp_srv;
Steve French96daf2b2011-05-27 04:34:02 +00003236 struct cifs_ses *ses;
3237 struct cifs_tcon *tcon;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003238 struct tcon_link *tlink;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003239 int rc = 0;
3240
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003241 spin_lock(&cifs_tcp_ses_lock);
3242 cifs_sb = CIFS_SB(sb);
3243 tlink = cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
3244 if (IS_ERR(tlink)) {
3245 spin_unlock(&cifs_tcp_ses_lock);
3246 return rc;
3247 }
3248 tcon = tlink_tcon(tlink);
3249 ses = tcon->ses;
3250 tcp_srv = ses->server;
3251
3252 volume_info = mnt_data->vol;
3253
Jeff Layton9fa114f2012-11-26 11:09:57 -05003254 if (!match_server(tcp_srv, volume_info) ||
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003255 !match_session(ses, volume_info) ||
Pavel Shilovskyae6f8dd2016-11-17 13:59:23 -08003256 !match_tcon(tcon, volume_info) ||
Sachin Prabhuc1d8b242016-07-29 22:38:20 +01003257 !match_prepath(sb, mnt_data)) {
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003258 rc = 0;
3259 goto out;
3260 }
3261
3262 rc = compare_mount_options(sb, mnt_data);
3263out:
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003264 spin_unlock(&cifs_tcp_ses_lock);
Jeff Laytonf484b5d02011-07-11 10:16:34 -04003265 cifs_put_tlink(tlink);
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003266 return rc;
3267}
3268
Jeff Layton09e50d52008-07-23 10:11:19 -04003269#ifdef CONFIG_DEBUG_LOCK_ALLOC
3270static struct lock_class_key cifs_key[2];
3271static struct lock_class_key cifs_slock_key[2];
3272
3273static inline void
3274cifs_reclassify_socket4(struct socket *sock)
3275{
3276 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003277 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003278 sock_lock_init_class_and_name(sk, "slock-AF_INET-CIFS",
3279 &cifs_slock_key[0], "sk_lock-AF_INET-CIFS", &cifs_key[0]);
3280}
3281
3282static inline void
3283cifs_reclassify_socket6(struct socket *sock)
3284{
3285 struct sock *sk = sock->sk;
Hannes Frederic Sowafafc4e12016-04-08 15:11:27 +02003286 BUG_ON(!sock_allow_reclassification(sk));
Jeff Layton09e50d52008-07-23 10:11:19 -04003287 sock_lock_init_class_and_name(sk, "slock-AF_INET6-CIFS",
3288 &cifs_slock_key[1], "sk_lock-AF_INET6-CIFS", &cifs_key[1]);
3289}
3290#else
3291static inline void
3292cifs_reclassify_socket4(struct socket *sock)
3293{
3294}
3295
3296static inline void
3297cifs_reclassify_socket6(struct socket *sock)
3298{
3299}
3300#endif
3301
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302/* See RFC1001 section 14 on representation of Netbios names */
Steve French50c2f752007-07-13 00:33:32 +00003303static void rfc1002mangle(char *target, char *source, unsigned int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304{
Steve French50c2f752007-07-13 00:33:32 +00003305 unsigned int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Steve French50c2f752007-07-13 00:33:32 +00003307 for (i = 0, j = 0; i < (length); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 /* mask a nibble at a time and encode */
3309 target[j] = 'A' + (0x0F & (source[i] >> 4));
3310 target[j+1] = 'A' + (0x0F & source[i]);
Steve French50c2f752007-07-13 00:33:32 +00003311 j += 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 }
3313
3314}
3315
Ben Greear3eb9a882010-09-01 17:06:02 -07003316static int
3317bind_socket(struct TCP_Server_Info *server)
3318{
3319 int rc = 0;
3320 if (server->srcaddr.ss_family != AF_UNSPEC) {
3321 /* Bind to the specified local IP address */
3322 struct socket *socket = server->ssocket;
3323 rc = socket->ops->bind(socket,
3324 (struct sockaddr *) &server->srcaddr,
3325 sizeof(server->srcaddr));
3326 if (rc < 0) {
3327 struct sockaddr_in *saddr4;
3328 struct sockaddr_in6 *saddr6;
3329 saddr4 = (struct sockaddr_in *)&server->srcaddr;
3330 saddr6 = (struct sockaddr_in6 *)&server->srcaddr;
3331 if (saddr6->sin6_family == AF_INET6)
Joe Perchesf96637b2013-05-04 22:12:25 -05003332 cifs_dbg(VFS, "Failed to bind to: %pI6c, error: %d\n",
3333 &saddr6->sin6_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003334 else
Joe Perchesf96637b2013-05-04 22:12:25 -05003335 cifs_dbg(VFS, "Failed to bind to: %pI4, error: %d\n",
3336 &saddr4->sin_addr.s_addr, rc);
Ben Greear3eb9a882010-09-01 17:06:02 -07003337 }
3338 }
3339 return rc;
3340}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
3342static int
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003343ip_rfc1001_connect(struct TCP_Server_Info *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344{
3345 int rc = 0;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003346 /*
3347 * some servers require RFC1001 sessinit before sending
3348 * negprot - BB check reconnection in case where second
3349 * sessinit is sent but no second negprot
3350 */
3351 struct rfc1002_session_packet *ses_init_buf;
3352 struct smb_hdr *smb_buf;
3353 ses_init_buf = kzalloc(sizeof(struct rfc1002_session_packet),
3354 GFP_KERNEL);
3355 if (ses_init_buf) {
3356 ses_init_buf->trailer.session_req.called_len = 32;
3357
Colin Ian King997152f2016-01-25 16:25:54 +00003358 if (server->server_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003359 rfc1002mangle(ses_init_buf->trailer.
3360 session_req.called_name,
3361 server->server_RFC1001_name,
3362 RFC1001_NAME_LEN_WITH_NULL);
3363 else
3364 rfc1002mangle(ses_init_buf->trailer.
3365 session_req.called_name,
3366 DEFAULT_CIFS_CALLED_NAME,
3367 RFC1001_NAME_LEN_WITH_NULL);
3368
3369 ses_init_buf->trailer.session_req.calling_len = 32;
3370
3371 /*
3372 * calling name ends in null (byte 16) from old smb
3373 * convention.
3374 */
Steve Frenchc85c35f2015-03-27 01:15:02 -05003375 if (server->workstation_RFC1001_name[0] != 0)
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003376 rfc1002mangle(ses_init_buf->trailer.
3377 session_req.calling_name,
3378 server->workstation_RFC1001_name,
3379 RFC1001_NAME_LEN_WITH_NULL);
3380 else
3381 rfc1002mangle(ses_init_buf->trailer.
3382 session_req.calling_name,
3383 "LINUX_CIFS_CLNT",
3384 RFC1001_NAME_LEN_WITH_NULL);
3385
3386 ses_init_buf->trailer.session_req.scope1 = 0;
3387 ses_init_buf->trailer.session_req.scope2 = 0;
3388 smb_buf = (struct smb_hdr *)ses_init_buf;
3389
3390 /* sizeof RFC1002_SESSION_REQUEST with no scope */
Steve Frenchbe8e3b02011-04-29 05:40:20 +00003391 smb_buf->smb_buf_length = cpu_to_be32(0x81000044);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003392 rc = smb_send(server, smb_buf, 0x44);
3393 kfree(ses_init_buf);
3394 /*
3395 * RFC1001 layer in at least one server
3396 * requires very short break before negprot
3397 * presumably because not expecting negprot
3398 * to follow so fast. This is a simple
3399 * solution that works without
3400 * complicating the code and causes no
3401 * significant slowing down on mount
3402 * for everyone else
3403 */
3404 usleep_range(1000, 2000);
3405 }
3406 /*
3407 * else the negprot may still work without this
3408 * even though malloc failed
3409 */
3410
3411 return rc;
3412}
3413
3414static int
3415generic_ip_connect(struct TCP_Server_Info *server)
3416{
3417 int rc = 0;
Steve French6da97912011-03-13 18:55:55 +00003418 __be16 sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003419 int slen, sfamily;
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003420 struct socket *socket = server->ssocket;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003421 struct sockaddr *saddr;
3422
3423 saddr = (struct sockaddr *) &server->dstaddr;
3424
3425 if (server->dstaddr.ss_family == AF_INET6) {
3426 sport = ((struct sockaddr_in6 *) saddr)->sin6_port;
3427 slen = sizeof(struct sockaddr_in6);
3428 sfamily = AF_INET6;
3429 } else {
3430 sport = ((struct sockaddr_in *) saddr)->sin_port;
3431 slen = sizeof(struct sockaddr_in);
3432 sfamily = AF_INET;
3433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003435 if (socket == NULL) {
Rob Landleyf1d0c992011-01-22 15:44:05 -06003436 rc = __sock_create(cifs_net_ns(server), sfamily, SOCK_STREAM,
3437 IPPROTO_TCP, &socket, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003439 cifs_dbg(VFS, "Error %d creating socket\n", rc);
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003440 server->ssocket = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 }
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003443
3444 /* BB other socket options to set KEEPALIVE, NODELAY? */
Joe Perchesf96637b2013-05-04 22:12:25 -05003445 cifs_dbg(FYI, "Socket created\n");
Jeff Laytonbcf4b102008-12-01 18:42:15 -05003446 server->ssocket = socket;
3447 socket->sk->sk_allocation = GFP_NOFS;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003448 if (sfamily == AF_INET6)
3449 cifs_reclassify_socket6(socket);
3450 else
3451 cifs_reclassify_socket4(socket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 }
3453
Ben Greear3eb9a882010-09-01 17:06:02 -07003454 rc = bind_socket(server);
3455 if (rc < 0)
3456 return rc;
3457
Jeff Laytond5c56052008-12-01 18:42:33 -05003458 /*
3459 * Eventually check for other socket options to change from
3460 * the default. sock_setsockopt not used because it expects
3461 * user space buffer
3462 */
3463 socket->sk->sk_rcvtimeo = 7 * HZ;
Steve Frenchda505c32009-01-19 03:49:35 +00003464 socket->sk->sk_sndtimeo = 5 * HZ;
Steve French6a5fa2362010-01-01 01:28:43 +00003465
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003466 /* make the bufsizes depend on wsize/rsize and max requests */
3467 if (server->noautotune) {
3468 if (socket->sk->sk_sndbuf < (200 * 1024))
3469 socket->sk->sk_sndbuf = 200 * 1024;
3470 if (socket->sk->sk_rcvbuf < (140 * 1024))
3471 socket->sk->sk_rcvbuf = 140 * 1024;
3472 }
3473
Steve French6a5fa2362010-01-01 01:28:43 +00003474 if (server->tcp_nodelay) {
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003475 int val = 1;
Steve French6a5fa2362010-01-01 01:28:43 +00003476 rc = kernel_setsockopt(socket, SOL_TCP, TCP_NODELAY,
3477 (char *)&val, sizeof(val));
3478 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05003479 cifs_dbg(FYI, "set TCP_NODELAY socket option error %d\n",
3480 rc);
Steve French6a5fa2362010-01-01 01:28:43 +00003481 }
3482
Joe Perchesf96637b2013-05-04 22:12:25 -05003483 cifs_dbg(FYI, "sndbuf %d rcvbuf %d rcvtimeo 0x%lx\n",
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003484 socket->sk->sk_sndbuf,
3485 socket->sk->sk_rcvbuf, socket->sk->sk_rcvtimeo);
3486
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003487 rc = socket->ops->connect(socket, saddr, slen, 0);
3488 if (rc < 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003489 cifs_dbg(FYI, "Error %d connecting to server\n", rc);
Jeff Laytonee1b3ea2011-06-21 07:18:26 -04003490 sock_release(socket);
3491 server->ssocket = NULL;
3492 return rc;
3493 }
3494
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003495 if (sport == htons(RFC1001_PORT))
3496 rc = ip_rfc1001_connect(server);
Steve French50c2f752007-07-13 00:33:32 +00003497
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 return rc;
3499}
3500
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003501static int
3502ip_connect(struct TCP_Server_Info *server)
3503{
Steve French6da97912011-03-13 18:55:55 +00003504 __be16 *sport;
Pavel Shilovskya9f1b852010-12-13 19:08:35 +03003505 struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&server->dstaddr;
3506 struct sockaddr_in *addr = (struct sockaddr_in *)&server->dstaddr;
3507
3508 if (server->dstaddr.ss_family == AF_INET6)
3509 sport = &addr6->sin6_port;
3510 else
3511 sport = &addr->sin_port;
3512
3513 if (*sport == 0) {
3514 int rc;
3515
3516 /* try with 445 port at first */
3517 *sport = htons(CIFS_PORT);
3518
3519 rc = generic_ip_connect(server);
3520 if (rc >= 0)
3521 return rc;
3522
3523 /* if it failed, try with 139 port */
3524 *sport = htons(RFC1001_PORT);
3525 }
3526
3527 return generic_ip_connect(server);
3528}
3529
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04003530void reset_cifs_unix_caps(unsigned int xid, struct cifs_tcon *tcon,
Al Viro2c6292a2011-06-17 09:05:48 -04003531 struct cifs_sb_info *cifs_sb, struct smb_vol *vol_info)
Steve French8af18972007-02-14 04:42:51 +00003532{
3533 /* if we are reconnecting then should we check to see if
3534 * any requested capabilities changed locally e.g. via
3535 * remount but we can not do much about it here
3536 * if they have (even if we could detect it by the following)
3537 * Perhaps we could add a backpointer to array of sb from tcon
3538 * or if we change to make all sb to same share the same
3539 * sb as NFS - then we only have one backpointer to sb.
3540 * What if we wanted to mount the server share twice once with
3541 * and once without posixacls or posix paths? */
3542 __u64 saved_cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Steve French50c2f752007-07-13 00:33:32 +00003543
Steve Frenchc18c8422007-07-18 23:21:09 +00003544 if (vol_info && vol_info->no_linux_ext) {
3545 tcon->fsUnixInfo.Capability = 0;
3546 tcon->unix_ext = 0; /* Unix Extensions disabled */
Joe Perchesf96637b2013-05-04 22:12:25 -05003547 cifs_dbg(FYI, "Linux protocol extensions disabled\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003548 return;
3549 } else if (vol_info)
3550 tcon->unix_ext = 1; /* Unix Extensions supported */
3551
3552 if (tcon->unix_ext == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003553 cifs_dbg(FYI, "Unix extensions disabled so not set on reconnect\n");
Steve Frenchc18c8422007-07-18 23:21:09 +00003554 return;
3555 }
Steve French50c2f752007-07-13 00:33:32 +00003556
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003557 if (!CIFSSMBQFSUnixInfo(xid, tcon)) {
Steve French8af18972007-02-14 04:42:51 +00003558 __u64 cap = le64_to_cpu(tcon->fsUnixInfo.Capability);
Joe Perchesf96637b2013-05-04 22:12:25 -05003559 cifs_dbg(FYI, "unix caps which server supports %lld\n", cap);
Steve French8af18972007-02-14 04:42:51 +00003560 /* check for reconnect case in which we do not
3561 want to change the mount behavior if we can avoid it */
Steve Frenchfb8c4b12007-07-10 01:16:18 +00003562 if (vol_info == NULL) {
Steve French50c2f752007-07-13 00:33:32 +00003563 /* turn off POSIX ACL and PATHNAMES if not set
Steve French8af18972007-02-14 04:42:51 +00003564 originally at mount time */
3565 if ((saved_cap & CIFS_UNIX_POSIX_ACL_CAP) == 0)
3566 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003567 if ((saved_cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
3568 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003569 cifs_dbg(VFS, "POSIXPATH support change\n");
Steve French8af18972007-02-14 04:42:51 +00003570 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Igor Mammedov11b6d642008-02-15 19:06:04 +00003571 } else if ((cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) == 0) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003572 cifs_dbg(VFS, "possible reconnect error\n");
3573 cifs_dbg(VFS, "server disabled POSIX path support\n");
Igor Mammedov11b6d642008-02-15 19:06:04 +00003574 }
Steve French8af18972007-02-14 04:42:51 +00003575 }
Steve French50c2f752007-07-13 00:33:32 +00003576
Steve French6848b732011-05-26 18:38:54 +00003577 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003578 cifs_dbg(VFS, "per-share encryption not supported yet\n");
Steve French6848b732011-05-26 18:38:54 +00003579
Steve French8af18972007-02-14 04:42:51 +00003580 cap &= CIFS_UNIX_CAP_MASK;
Steve French75865f8c2007-06-24 18:30:48 +00003581 if (vol_info && vol_info->no_psx_acl)
Steve French8af18972007-02-14 04:42:51 +00003582 cap &= ~CIFS_UNIX_POSIX_ACL_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003583 else if (CIFS_UNIX_POSIX_ACL_CAP & cap) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003584 cifs_dbg(FYI, "negotiated posix acl support\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003585 if (cifs_sb)
3586 cifs_sb->mnt_cifs_flags |=
3587 CIFS_MOUNT_POSIXACL;
Steve French8af18972007-02-14 04:42:51 +00003588 }
3589
Steve French75865f8c2007-06-24 18:30:48 +00003590 if (vol_info && vol_info->posix_paths == 0)
Steve French8af18972007-02-14 04:42:51 +00003591 cap &= ~CIFS_UNIX_POSIX_PATHNAMES_CAP;
Steve French75865f8c2007-06-24 18:30:48 +00003592 else if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003593 cifs_dbg(FYI, "negotiate posix pathnames\n");
Al Viro2c6292a2011-06-17 09:05:48 -04003594 if (cifs_sb)
3595 cifs_sb->mnt_cifs_flags |=
Steve French8af18972007-02-14 04:42:51 +00003596 CIFS_MOUNT_POSIX_PATHS;
3597 }
Steve French50c2f752007-07-13 00:33:32 +00003598
Joe Perchesf96637b2013-05-04 22:12:25 -05003599 cifs_dbg(FYI, "Negotiate caps 0x%x\n", (int)cap);
Steve French8af18972007-02-14 04:42:51 +00003600#ifdef CONFIG_CIFS_DEBUG2
Steve French75865f8c2007-06-24 18:30:48 +00003601 if (cap & CIFS_UNIX_FCNTL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003602 cifs_dbg(FYI, "FCNTL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003603 if (cap & CIFS_UNIX_EXTATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003604 cifs_dbg(FYI, "EXTATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003605 if (cap & CIFS_UNIX_POSIX_PATHNAMES_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003606 cifs_dbg(FYI, "POSIX path cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003607 if (cap & CIFS_UNIX_XATTR_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003608 cifs_dbg(FYI, "XATTR cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003609 if (cap & CIFS_UNIX_POSIX_ACL_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003610 cifs_dbg(FYI, "POSIX ACL cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003611 if (cap & CIFS_UNIX_LARGE_READ_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003612 cifs_dbg(FYI, "very large read cap\n");
Steve French75865f8c2007-06-24 18:30:48 +00003613 if (cap & CIFS_UNIX_LARGE_WRITE_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003614 cifs_dbg(FYI, "very large write cap\n");
Steve French6848b732011-05-26 18:38:54 +00003615 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003616 cifs_dbg(FYI, "transport encryption cap\n");
Steve French6848b732011-05-26 18:38:54 +00003617 if (cap & CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP)
Joe Perchesf96637b2013-05-04 22:12:25 -05003618 cifs_dbg(FYI, "mandatory transport encryption cap\n");
Steve French8af18972007-02-14 04:42:51 +00003619#endif /* CIFS_DEBUG2 */
3620 if (CIFSSMBSetFSUnixInfo(xid, tcon, cap)) {
Steve French442aa312007-09-24 20:25:46 +00003621 if (vol_info == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003622 cifs_dbg(FYI, "resetting capabilities failed\n");
Steve French442aa312007-09-24 20:25:46 +00003623 } else
Joe Perchesf96637b2013-05-04 22:12:25 -05003624 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 +00003625
Steve French8af18972007-02-14 04:42:51 +00003626 }
3627 }
3628}
3629
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003630int cifs_setup_cifs_sb(struct smb_vol *pvolume_info,
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003631 struct cifs_sb_info *cifs_sb)
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003632{
Jeff Layton2de970f2010-10-06 19:51:12 -04003633 INIT_DELAYED_WORK(&cifs_sb->prune_tlinks, cifs_prune_tlinks);
3634
Al Viro2ced6f62011-06-17 09:20:04 -04003635 spin_lock_init(&cifs_sb->tlink_tree_lock);
3636 cifs_sb->tlink_tree = RB_ROOT;
3637
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003638 /*
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003639 * Temporarily set r/wsize for matching superblock. If we end up using
3640 * new sb then client will later negotiate it downward if needed.
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003641 */
Jeff Layton5eba8ab2011-10-19 15:30:26 -04003642 cifs_sb->rsize = pvolume_info->rsize;
Pavel Shilovsky25c7f412011-05-26 23:35:47 +04003643 cifs_sb->wsize = pvolume_info->wsize;
3644
Steve French3b795212008-11-13 19:45:32 +00003645 cifs_sb->mnt_uid = pvolume_info->linux_uid;
3646 cifs_sb->mnt_gid = pvolume_info->linux_gid;
3647 cifs_sb->mnt_file_mode = pvolume_info->file_mode;
3648 cifs_sb->mnt_dir_mode = pvolume_info->dir_mode;
Joe Perchesf96637b2013-05-04 22:12:25 -05003649 cifs_dbg(FYI, "file mode: 0x%hx dir mode: 0x%hx\n",
3650 cifs_sb->mnt_file_mode, cifs_sb->mnt_dir_mode);
Steve French3b795212008-11-13 19:45:32 +00003651
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303652 cifs_sb->actimeo = pvolume_info->actimeo;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003653 cifs_sb->local_nls = pvolume_info->local_nls;
Suresh Jayaraman6d20e842010-12-01 14:42:28 +05303654
Aurelien Aptel83930722018-09-20 18:10:25 -07003655 if (pvolume_info->nodfs)
3656 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_DFS;
Steve French3b795212008-11-13 19:45:32 +00003657 if (pvolume_info->noperm)
3658 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_PERM;
3659 if (pvolume_info->setuids)
3660 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SET_UID;
Steve French95932652016-09-23 01:36:34 -05003661 if (pvolume_info->setuidfromacl)
3662 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UID_FROM_ACL;
Steve French3b795212008-11-13 19:45:32 +00003663 if (pvolume_info->server_ino)
3664 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_SERVER_INUM;
3665 if (pvolume_info->remap)
Steve French2baa2682014-09-27 02:19:01 -05003666 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SFM_CHR;
3667 if (pvolume_info->sfu_remap)
Steve French3b795212008-11-13 19:45:32 +00003668 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MAP_SPECIAL_CHR;
3669 if (pvolume_info->no_xattr)
3670 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_XATTR;
3671 if (pvolume_info->sfu_emul)
3672 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_UNX_EMUL;
3673 if (pvolume_info->nobrl)
3674 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_BRL;
Steve French3d4ef9a2018-04-25 22:19:09 -05003675 if (pvolume_info->nohandlecache)
3676 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NO_HANDLE_CACHE;
Steve Frenchbe652442009-02-23 15:21:59 +00003677 if (pvolume_info->nostrictsync)
Steve French4717bed2009-02-24 14:44:19 +00003678 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOSSYNC;
Steve French13a6e422008-12-02 17:24:33 +00003679 if (pvolume_info->mand_lock)
3680 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_NOPOSIXBRL;
Pavel Shilovskyd4ffff12011-05-26 06:02:00 +00003681 if (pvolume_info->rwpidforward)
3682 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_RWPIDFORWARD;
Steve French3b795212008-11-13 19:45:32 +00003683 if (pvolume_info->cifs_acl)
3684 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_ACL;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003685 if (pvolume_info->backupuid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003686 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPUID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003687 cifs_sb->mnt_backupuid = pvolume_info->backupuid;
3688 }
3689 if (pvolume_info->backupgid_specified) {
Shirish Pargaonkar3d3ea8e2011-09-26 09:56:44 -05003690 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_CIFS_BACKUPGID;
Sachin Prabhu3c7c87f2012-04-24 15:28:14 +01003691 cifs_sb->mnt_backupgid = pvolume_info->backupgid;
3692 }
Steve French3b795212008-11-13 19:45:32 +00003693 if (pvolume_info->override_uid)
3694 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_UID;
3695 if (pvolume_info->override_gid)
3696 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_OVERR_GID;
3697 if (pvolume_info->dynperm)
3698 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DYNPERM;
Suresh Jayaramanfa1df752010-07-05 18:13:36 +05303699 if (pvolume_info->fsc)
3700 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_FSCACHE;
Jeff Layton0eb8a132010-10-06 19:51:12 -04003701 if (pvolume_info->multiuser)
3702 cifs_sb->mnt_cifs_flags |= (CIFS_MOUNT_MULTIUSER |
3703 CIFS_MOUNT_NO_PERM);
Pavel Shilovskyd39454f2011-01-24 14:16:35 -05003704 if (pvolume_info->strict_io)
3705 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_STRICT_IO;
Steve French3b795212008-11-13 19:45:32 +00003706 if (pvolume_info->direct_io) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003707 cifs_dbg(FYI, "mounting share using direct i/o\n");
Steve French3b795212008-11-13 19:45:32 +00003708 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_DIRECT_IO;
3709 }
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003710 if (pvolume_info->mfsymlinks) {
3711 if (pvolume_info->sfu_emul) {
Steve Frenchdb8b6312014-09-22 05:13:55 -05003712 /*
3713 * Our SFU ("Services for Unix" emulation does not allow
3714 * creating symlinks but does allow reading existing SFU
3715 * symlinks (it does allow both creating and reading SFU
3716 * style mknod and FIFOs though). When "mfsymlinks" and
3717 * "sfu" are both enabled at the same time, it allows
3718 * reading both types of symlinks, but will only create
3719 * them with mfsymlinks format. This allows better
3720 * Apple compatibility (probably better for Samba too)
3721 * while still recognizing old Windows style symlinks.
3722 */
3723 cifs_dbg(VFS, "mount options mfsymlinks and sfu both enabled\n");
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003724 }
Steve Frenchdb8b6312014-09-22 05:13:55 -05003725 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_MF_SYMLINKS;
Stefan Metzmacher736a33202010-07-30 14:56:00 +02003726 }
Steve French3b795212008-11-13 19:45:32 +00003727
3728 if ((pvolume_info->cifs_acl) && (pvolume_info->dynperm))
Joe Perchesf96637b2013-05-04 22:12:25 -05003729 cifs_dbg(VFS, "mount option dynperm ignored if cifsacl mount option supported\n");
Sachin Prabhu4214ebf2016-07-29 22:38:19 +01003730
3731 if (pvolume_info->prepath) {
3732 cifs_sb->prepath = kstrdup(pvolume_info->prepath, GFP_KERNEL);
3733 if (cifs_sb->prepath == NULL)
3734 return -ENOMEM;
3735 }
3736
3737 return 0;
Jeff Laytonb1c8d2b2008-10-22 13:57:07 -04003738}
3739
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003740void
3741cifs_cleanup_volume_info_contents(struct smb_vol *volume_info)
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003742{
Sean Finneyb9468452011-04-11 13:19:32 +00003743 kfree(volume_info->username);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003744 kzfree(volume_info->password);
Jesper Juhl95c75452011-08-27 18:58:34 +02003745 kfree(volume_info->UNC);
Sean Finneyb9468452011-04-11 13:19:32 +00003746 kfree(volume_info->domainname);
3747 kfree(volume_info->iocharset);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003748 kfree(volume_info->prepath);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003749}
3750
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003751void
3752cifs_cleanup_volume_info(struct smb_vol *volume_info)
3753{
3754 if (!volume_info)
3755 return;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003756 cifs_cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003757 kfree(volume_info);
3758}
3759
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003760/* Release all succeed connections */
3761static inline void mount_put_conns(struct cifs_sb_info *cifs_sb,
3762 unsigned int xid,
3763 struct TCP_Server_Info *server,
3764 struct cifs_ses *ses, struct cifs_tcon *tcon)
3765{
3766 int rc = 0;
3767
3768 if (tcon)
3769 cifs_put_tcon(tcon);
3770 else if (ses)
3771 cifs_put_smb_ses(ses);
3772 else if (server)
3773 cifs_put_tcp_session(server, 0);
3774 cifs_sb->mnt_cifs_flags &= ~CIFS_MOUNT_POSIX_PATHS;
3775 free_xid(xid);
3776}
3777
3778/* Get connections for tcp, ses and tcon */
3779static int mount_get_conns(struct smb_vol *vol, struct cifs_sb_info *cifs_sb,
3780 unsigned int *xid,
3781 struct TCP_Server_Info **nserver,
3782 struct cifs_ses **nses, struct cifs_tcon **ntcon)
3783{
3784 int rc = 0;
3785 struct TCP_Server_Info *server;
3786 struct cifs_ses *ses;
3787 struct cifs_tcon *tcon;
3788
3789 *nserver = NULL;
3790 *nses = NULL;
3791 *ntcon = NULL;
3792
3793 *xid = get_xid();
3794
3795 /* get a reference to a tcp session */
3796 server = cifs_get_tcp_session(vol);
3797 if (IS_ERR(server)) {
3798 rc = PTR_ERR(server);
3799 return rc;
3800 }
3801
3802 *nserver = server;
3803
3804 if ((vol->max_credits < 20) || (vol->max_credits > 60000))
3805 server->max_credits = SMB2_MAX_CREDITS_AVAILABLE;
3806 else
3807 server->max_credits = vol->max_credits;
3808
3809 /* get a reference to a SMB session */
3810 ses = cifs_get_smb_ses(server, vol);
3811 if (IS_ERR(ses)) {
3812 rc = PTR_ERR(ses);
3813 return rc;
3814 }
3815
3816 *nses = ses;
3817
3818 if ((vol->persistent == true) && (!(ses->server->capabilities &
3819 SMB2_GLOBAL_CAP_PERSISTENT_HANDLES))) {
3820 cifs_dbg(VFS, "persistent handles not supported by server\n");
3821 return -EOPNOTSUPP;
3822 }
3823
3824 /* search for existing tcon to this server share */
3825 tcon = cifs_get_tcon(ses, vol);
3826 if (IS_ERR(tcon)) {
3827 rc = PTR_ERR(tcon);
3828 return rc;
3829 }
3830
3831 *ntcon = tcon;
3832
3833 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
3834 if (tcon->posix_extensions)
3835 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
3836
3837 /* tell server which Unix caps we support */
3838 if (cap_unix(tcon->ses)) {
3839 /*
3840 * reset of caps checks mount to see if unix extensions disabled
3841 * for just this mount.
3842 */
3843 reset_cifs_unix_caps(*xid, tcon, cifs_sb, vol);
3844 if ((tcon->ses->server->tcpStatus == CifsNeedReconnect) &&
3845 (le64_to_cpu(tcon->fsUnixInfo.Capability) &
3846 CIFS_UNIX_TRANSPORT_ENCRYPTION_MANDATORY_CAP))
3847 return -EACCES;
3848 } else
3849 tcon->unix_ext = 0; /* server does not support them */
3850
3851 /* do not care if a following call succeed - informational */
3852 if (!tcon->pipe && server->ops->qfs_tcon)
3853 server->ops->qfs_tcon(*xid, tcon);
3854
3855 cifs_sb->wsize = server->ops->negotiate_wsize(tcon, vol);
3856 cifs_sb->rsize = server->ops->negotiate_rsize(tcon, vol);
3857
3858 return 0;
3859}
3860
3861static int mount_setup_tlink(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses,
3862 struct cifs_tcon *tcon)
3863{
3864 struct tcon_link *tlink;
3865
3866 /* hang the tcon off of the superblock */
3867 tlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
3868 if (tlink == NULL)
3869 return -ENOMEM;
3870
3871 tlink->tl_uid = ses->linux_uid;
3872 tlink->tl_tcon = tcon;
3873 tlink->tl_time = jiffies;
3874 set_bit(TCON_LINK_MASTER, &tlink->tl_flags);
3875 set_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
3876
3877 cifs_sb->master_tlink = tlink;
3878 spin_lock(&cifs_sb->tlink_tree_lock);
3879 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
3880 spin_unlock(&cifs_sb->tlink_tree_lock);
3881
3882 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
3883 TLINK_IDLE_EXPIRE);
3884 return 0;
3885}
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003886
Steve French2d6d5892009-04-09 00:36:44 +00003887#ifdef CONFIG_CIFS_DFS_UPCALL
Steve French6d3ea7e2012-11-28 22:34:41 -06003888/*
3889 * cifs_build_path_to_root returns full path to root when we do not have an
3890 * exiting connection (tcon)
3891 */
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003892static char *
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003893build_unc_path_to_root(const struct smb_vol *vol,
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003894 const struct cifs_sb_info *cifs_sb)
3895{
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003896 char *full_path, *pos;
Jeff Layton839db3d2012-12-10 06:10:45 -05003897 unsigned int pplen = vol->prepath ? strlen(vol->prepath) + 1 : 0;
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003898 unsigned int unc_len = strnlen(vol->UNC, MAX_TREE_SIZE + 1);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003899
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003900 full_path = kmalloc(unc_len + pplen + 1, GFP_KERNEL);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003901 if (full_path == NULL)
3902 return ERR_PTR(-ENOMEM);
3903
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003904 strncpy(full_path, vol->UNC, unc_len);
3905 pos = full_path + unc_len;
3906
3907 if (pplen) {
Jeff Layton1fc29ba2013-05-31 10:00:18 -04003908 *pos = CIFS_DIR_SEP(cifs_sb);
3909 strncpy(pos + 1, vol->prepath, pplen);
Jeff Laytonb2a0fa12011-07-06 08:10:36 -04003910 pos += pplen;
3911 }
3912
3913 *pos = '\0'; /* add trailing null */
Steve Frenchf87d39d2011-05-27 03:50:55 +00003914 convert_delimiter(full_path, CIFS_DIR_SEP(cifs_sb));
Joe Perchesf96637b2013-05-04 22:12:25 -05003915 cifs_dbg(FYI, "%s: full_path=%s\n", __func__, full_path);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003916 return full_path;
3917}
Sean Finneydd613942011-04-11 13:19:30 +00003918
Paulo Alcantara1c780222018-11-14 16:24:03 -02003919/**
3920 * expand_dfs_referral - Perform a dfs referral query and update the cifs_sb
3921 *
Sean Finneydd613942011-04-11 13:19:30 +00003922 *
Sean Finney046462a2011-04-11 13:19:33 +00003923 * If a referral is found, cifs_sb->mountdata will be (re-)allocated
3924 * to a string containing updated options for the submount. Otherwise it
3925 * will be left untouched.
Sean Finneydd613942011-04-11 13:19:30 +00003926 *
3927 * Returns the rc from get_dfs_path to the caller, which can be used to
3928 * determine whether there were referrals.
3929 */
3930static int
Pavel Shilovskyb669f332012-05-27 20:21:53 +04003931expand_dfs_referral(const unsigned int xid, struct cifs_ses *ses,
Sean Finneydd613942011-04-11 13:19:30 +00003932 struct smb_vol *volume_info, struct cifs_sb_info *cifs_sb,
Sean Finney046462a2011-04-11 13:19:33 +00003933 int check_prefix)
Sean Finneydd613942011-04-11 13:19:30 +00003934{
3935 int rc;
Paulo Alcantara1c780222018-11-14 16:24:03 -02003936 struct dfs_info3_param referral = {0};
Sean Finneydd613942011-04-11 13:19:30 +00003937 char *full_path = NULL, *ref_path = NULL, *mdata = NULL;
3938
Aurelien Aptel83930722018-09-20 18:10:25 -07003939 if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_NO_DFS)
3940 return -EREMOTE;
3941
Sean Finneydd613942011-04-11 13:19:30 +00003942 full_path = build_unc_path_to_root(volume_info, cifs_sb);
3943 if (IS_ERR(full_path))
3944 return PTR_ERR(full_path);
3945
3946 /* For DFS paths, skip the first '\' of the UNC */
3947 ref_path = check_prefix ? full_path + 1 : volume_info->UNC + 1;
3948
Paulo Alcantara1c780222018-11-14 16:24:03 -02003949 rc = dfs_cache_find(xid, ses, cifs_sb->local_nls, cifs_remap(cifs_sb),
3950 ref_path, &referral, NULL);
3951 if (!rc) {
Sean Finneydd613942011-04-11 13:19:30 +00003952 char *fake_devname = NULL;
3953
3954 mdata = cifs_compose_mount_options(cifs_sb->mountdata,
Paulo Alcantara1c780222018-11-14 16:24:03 -02003955 full_path + 1, &referral,
Sean Finneydd613942011-04-11 13:19:30 +00003956 &fake_devname);
Paulo Alcantara1c780222018-11-14 16:24:03 -02003957 free_dfs_info_param(&referral);
Sean Finney046462a2011-04-11 13:19:33 +00003958
Sean Finneydd613942011-04-11 13:19:30 +00003959 if (IS_ERR(mdata)) {
3960 rc = PTR_ERR(mdata);
3961 mdata = NULL;
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003962 } else {
Paulo Alcantara56c762e2018-11-14 13:03:14 -02003963 cifs_cleanup_volume_info_contents(volume_info);
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003964 rc = cifs_setup_volume_info(volume_info, mdata,
Paulo Alcantara1c780222018-11-14 16:24:03 -02003965 fake_devname, false);
Sean Finneydd613942011-04-11 13:19:30 +00003966 }
Jeff Laytonb9bce2e2011-07-06 08:10:39 -04003967 kfree(fake_devname);
3968 kfree(cifs_sb->mountdata);
Sean Finney046462a2011-04-11 13:19:33 +00003969 cifs_sb->mountdata = mdata;
Sean Finneydd613942011-04-11 13:19:30 +00003970 }
3971 kfree(full_path);
3972 return rc;
3973}
Steve French2d6d5892009-04-09 00:36:44 +00003974#endif
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04003975
Jeff Layton04db79b2011-07-06 08:10:38 -04003976static int
3977cifs_setup_volume_info(struct smb_vol *volume_info, char *mount_data,
Steve Frenchc7c137b2018-06-06 17:59:29 -05003978 const char *devname, bool is_smb3)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979{
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00003980 int rc = 0;
Sean Finneydd613942011-04-11 13:19:30 +00003981
Steve Frenchc7c137b2018-06-06 17:59:29 -05003982 if (cifs_parse_mount_options(mount_data, devname, volume_info, is_smb3))
Jeff Layton04db79b2011-07-06 08:10:38 -04003983 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984
Jeff Layton7586b762008-12-01 18:41:49 -05003985 if (volume_info->nullauth) {
Joe Perchesf96637b2013-05-04 22:12:25 -05003986 cifs_dbg(FYI, "Anonymous login\n");
Jeff Layton04febab2012-01-17 16:09:15 -05003987 kfree(volume_info->username);
3988 volume_info->username = NULL;
Jeff Layton7586b762008-12-01 18:41:49 -05003989 } else if (volume_info->username) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 /* BB fixme parse for domain name here */
Joe Perchesf96637b2013-05-04 22:12:25 -05003991 cifs_dbg(FYI, "Username: %s\n", volume_info->username);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 } else {
Joe Perchesf96637b2013-05-04 22:12:25 -05003993 cifs_dbg(VFS, "No username specified\n");
Steve French50c2f752007-07-13 00:33:32 +00003994 /* In userspace mount helper we can get user name from alternate
3995 locations such as env variables and files on disk */
Jeff Layton04db79b2011-07-06 08:10:38 -04003996 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 }
3998
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 /* this is needed for ASCII cp to Unicode converts */
Jeff Layton7586b762008-12-01 18:41:49 -05004000 if (volume_info->iocharset == NULL) {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04004001 /* load_nls_default cannot return null */
4002 volume_info->local_nls = load_nls_default();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 } else {
Jeff Laytona5fc4ce2010-04-24 07:57:42 -04004004 volume_info->local_nls = load_nls(volume_info->iocharset);
4005 if (volume_info->local_nls == NULL) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004006 cifs_dbg(VFS, "CIFS mount error: iocharset %s not found\n",
Joe Perchesb6b38f72010-04-21 03:50:45 +00004007 volume_info->iocharset);
Jeff Layton04db79b2011-07-06 08:10:38 -04004008 return -ELIBACC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 }
4010 }
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004011
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004012 return rc;
Jeff Layton04db79b2011-07-06 08:10:38 -04004013}
4014
4015struct smb_vol *
Steve Frenchc7c137b2018-06-06 17:59:29 -05004016cifs_get_volume_info(char *mount_data, const char *devname, bool is_smb3)
Jeff Layton04db79b2011-07-06 08:10:38 -04004017{
4018 int rc;
4019 struct smb_vol *volume_info;
4020
Jeff Layton6ee95422012-11-26 11:09:57 -05004021 volume_info = kmalloc(sizeof(struct smb_vol), GFP_KERNEL);
Jeff Layton04db79b2011-07-06 08:10:38 -04004022 if (!volume_info)
4023 return ERR_PTR(-ENOMEM);
4024
Steve Frenchc7c137b2018-06-06 17:59:29 -05004025 rc = cifs_setup_volume_info(volume_info, mount_data, devname, is_smb3);
Jeff Layton04db79b2011-07-06 08:10:38 -04004026 if (rc) {
4027 cifs_cleanup_volume_info(volume_info);
4028 volume_info = ERR_PTR(rc);
4029 }
4030
4031 return volume_info;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004032}
4033
Aurelien Aptela6b50582016-05-25 19:59:09 +02004034static int
4035cifs_are_all_path_components_accessible(struct TCP_Server_Info *server,
4036 unsigned int xid,
4037 struct cifs_tcon *tcon,
4038 struct cifs_sb_info *cifs_sb,
4039 char *full_path)
4040{
4041 int rc;
4042 char *s;
4043 char sep, tmp;
4044
4045 sep = CIFS_DIR_SEP(cifs_sb);
4046 s = full_path;
4047
4048 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb, "");
4049 while (rc == 0) {
4050 /* skip separators */
4051 while (*s == sep)
4052 s++;
4053 if (!*s)
4054 break;
4055 /* next separator */
4056 while (*s && *s != sep)
4057 s++;
4058
4059 /*
4060 * temporarily null-terminate the path at the end of
4061 * the current component
4062 */
4063 tmp = *s;
4064 *s = 0;
4065 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4066 full_path);
4067 *s = tmp;
4068 }
4069 return rc;
4070}
4071
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004072/*
4073 * Check if path is remote (e.g. a DFS share). Return -EREMOTE if it is,
4074 * otherwise 0.
4075 */
4076static int is_path_remote(struct cifs_sb_info *cifs_sb, struct smb_vol *vol,
4077 const unsigned int xid,
4078 struct TCP_Server_Info *server,
4079 struct cifs_tcon *tcon)
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004080{
Jeff Layton1daaae82012-03-21 06:30:40 -04004081 int rc;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004082 char *full_path;
4083
4084 if (!server->ops->is_path_accessible)
4085 return -EOPNOTSUPP;
4086
4087 /*
4088 * cifs_build_path_to_root works only when we have a valid tcon
4089 */
4090 full_path = cifs_build_path_to_root(vol, cifs_sb, tcon,
4091 tcon->Flags & SMB_SHARE_IS_IN_DFS);
4092 if (full_path == NULL)
4093 return -ENOMEM;
4094
4095 cifs_dbg(FYI, "%s: full_path: %s\n", __func__, full_path);
4096
4097 rc = server->ops->is_path_accessible(xid, tcon, cifs_sb,
4098 full_path);
4099 if (rc != 0 && rc != -EREMOTE) {
4100 kfree(full_path);
4101 return rc;
4102 }
4103
4104 if (rc != -EREMOTE) {
4105 rc = cifs_are_all_path_components_accessible(server, xid, tcon,
4106 cifs_sb,
4107 full_path);
4108 if (rc != 0) {
4109 cifs_dbg(VFS, "cannot query dirs between root and final path, "
4110 "enabling CIFS_MOUNT_USE_PREFIX_PATH\n");
4111 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_USE_PREFIX_PATH;
4112 rc = 0;
4113 }
4114 }
4115
4116 kfree(full_path);
4117 return rc;
4118}
4119
4120#ifdef CONFIG_CIFS_DFS_UPCALL
4121int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
4122{
4123 int rc = 0;
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004124 unsigned int xid;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004125 struct cifs_ses *ses;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004126 struct cifs_tcon *tcon = NULL;
Pavel Shilovskyaf4281d2012-05-27 20:48:35 +04004127 struct TCP_Server_Info *server;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004128 char *old_mountdata;
4129 int count;
Al Virodd854462011-06-17 08:24:42 -04004130
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004131 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4132 if (!rc && tcon) {
4133 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4134 if (!rc)
4135 goto out;
4136 if (rc != -EREMOTE)
4137 goto error;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004138 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004139 if ((rc == -EACCES) || (rc == -EOPNOTSUPP) || (ses == NULL) || (server == NULL))
4140 goto error;
Pavel Shilovsky724d9f12011-05-05 09:55:12 +00004141
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142
Sean Finneyc1508ca2011-04-11 13:19:31 +00004143 /*
4144 * Perform an unconditional check for whether there are DFS
4145 * referrals for this path without prefix, to provide support
4146 * for DFS referrals from w2k8 servers which don't seem to respond
4147 * with PATH_NOT_COVERED to requests that include the prefix.
4148 * Chase the referral if found, otherwise continue normally.
4149 */
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004150 old_mountdata = cifs_sb->mountdata;
4151 (void)expand_dfs_referral(xid, ses, vol, cifs_sb, false);
Sean Finneyc1508ca2011-04-11 13:19:31 +00004152
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004153 if (cifs_sb->mountdata == NULL) {
4154 rc = -ENOENT;
4155 goto error;
4156 }
4157
4158 if (cifs_sb->mountdata != old_mountdata) {
4159 /* If we were redirected, reconnect to new target server */
4160 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4161 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4162 }
4163 if (rc) {
4164 if (rc == -EACCES || rc == -EOPNOTSUPP)
4165 goto error;
4166 }
4167
4168 for (count = 1; ;) {
4169 if (!rc && tcon) {
4170 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4171 if (!rc || rc != -EREMOTE)
4172 break;
Pavel Shilovsky68889f22012-05-25 14:40:22 +04004173 }
Steve French6d3ea7e2012-11-28 22:34:41 -06004174 /*
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004175 * BB: when we implement proper loop detection,
4176 * we will remove this check. But now we need it
4177 * to prevent an indefinite loop if 'DFS tree' is
4178 * misconfigured (i.e. has loops).
Steve French6d3ea7e2012-11-28 22:34:41 -06004179 */
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004180 if (count++ > MAX_NESTED_LINKS) {
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004181 rc = -ELOOP;
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004182 break;
Igor Mammedov5c2503a2009-04-21 19:31:05 +04004183 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004184
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004185 old_mountdata = cifs_sb->mountdata;
4186 rc = expand_dfs_referral(xid, tcon->ses, vol, cifs_sb,
4187 true);
4188 if (rc)
4189 break;
Jeff Layton7b91e262009-07-23 15:22:30 -04004190
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004191 if (cifs_sb->mountdata != old_mountdata) {
4192 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4193 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses,
4194 &tcon);
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004195 }
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004196 if (rc) {
4197 if (rc == -EACCES || rc == -EOPNOTSUPP || !server ||
4198 !ses)
4199 goto error;
4200 }
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004201 }
4202
Jeff Layton9d002df2010-10-06 19:51:11 -04004203 if (rc)
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004204 goto error;
Igor Mammedov1bfe73c2009-04-01 17:54:42 +04004205
Aurelien Aptel5fc7fcd2018-11-16 16:13:25 +01004206 /*
4207 * After reconnecting to a different server, unique ids won't
4208 * match anymore, so we disable serverino. This prevents
4209 * dentry revalidation to think the dentry are stale (ESTALE).
4210 */
4211 cifs_autodisable_serverino(cifs_sb);
Jeff Layton70fe7dc2007-11-16 22:21:07 +00004212out:
Pavel Shilovsky6d5786a2012-06-20 11:21:16 +04004213 free_xid(xid);
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004214 return mount_setup_tlink(cifs_sb, ses, tcon);
4215
4216error:
4217 mount_put_conns(cifs_sb, xid, server, ses, tcon);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 return rc;
4219}
Paulo Alcantara56c762e2018-11-14 13:03:14 -02004220#else
4221int cifs_mount(struct cifs_sb_info *cifs_sb, struct smb_vol *vol)
4222{
4223 int rc = 0;
4224 unsigned int xid;
4225 struct cifs_ses *ses;
4226 struct cifs_tcon *tcon;
4227 struct TCP_Server_Info *server;
4228
4229 rc = mount_get_conns(vol, cifs_sb, &xid, &server, &ses, &tcon);
4230 if (rc)
4231 goto error;
4232
4233 if (tcon) {
4234 rc = is_path_remote(cifs_sb, vol, xid, server, tcon);
4235 if (rc == -EREMOTE)
4236 rc = -EOPNOTSUPP;
4237 if (rc)
4238 goto error;
4239 }
4240
4241 free_xid(xid);
4242
4243 return mount_setup_tlink(cifs_sb, ses, tcon);
4244
4245error:
4246 mount_put_conns(cifs_sb, xid, server, ses, tcon);
4247 return rc;
4248}
4249#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250
Jeff Layton8d1bca32011-06-11 21:17:10 -04004251/*
Aurelien Aptelb327a712018-01-24 13:46:10 +01004252 * Issue a TREE_CONNECT request.
Jeff Layton8d1bca32011-06-11 21:17:10 -04004253 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254int
Pavel Shilovsky2e6e02a2012-05-25 11:11:39 +04004255CIFSTCon(const unsigned int xid, struct cifs_ses *ses,
Steve French96daf2b2011-05-27 04:34:02 +00004256 const char *tree, struct cifs_tcon *tcon,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 const struct nls_table *nls_codepage)
4258{
4259 struct smb_hdr *smb_buffer;
4260 struct smb_hdr *smb_buffer_response;
4261 TCONX_REQ *pSMB;
4262 TCONX_RSP *pSMBr;
4263 unsigned char *bcc_ptr;
4264 int rc = 0;
Jeff Layton690c5222011-01-20 13:36:51 -05004265 int length;
4266 __u16 bytes_left, count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
4268 if (ses == NULL)
4269 return -EIO;
4270
4271 smb_buffer = cifs_buf_get();
Steve Frenchca43e3b2009-09-01 17:20:50 +00004272 if (smb_buffer == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 return -ENOMEM;
Steve Frenchca43e3b2009-09-01 17:20:50 +00004274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 smb_buffer_response = smb_buffer;
4276
4277 header_assemble(smb_buffer, SMB_COM_TREE_CONNECT_ANDX,
4278 NULL /*no tid */ , 4 /*wct */ );
Steve French1982c342005-08-17 12:38:22 -07004279
Pavel Shilovsky88257362012-05-23 14:01:59 +04004280 smb_buffer->Mid = get_next_mid(ses->server);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 smb_buffer->Uid = ses->Suid;
4282 pSMB = (TCONX_REQ *) smb_buffer;
4283 pSMBr = (TCONX_RSP *) smb_buffer_response;
4284
4285 pSMB->AndXCommand = 0xFF;
4286 pSMB->Flags = cpu_to_le16(TCON_EXTENDED_SECINFO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 bcc_ptr = &pSMB->Password[0];
Aurelien Aptelb327a712018-01-24 13:46:10 +01004288 if (tcon->pipe || (ses->server->sec_mode & SECMODE_USER)) {
Steve Frencheeac8042006-01-13 21:34:58 -08004289 pSMB->PasswordLength = cpu_to_le16(1); /* minimum */
Steve French7c7b25b2006-06-01 19:20:10 +00004290 *bcc_ptr = 0; /* password is null byte */
Steve Frencheeac8042006-01-13 21:34:58 -08004291 bcc_ptr++; /* skip password */
Steve French7c7b25b2006-06-01 19:20:10 +00004292 /* already aligned so no need to do it below */
Steve Frencheeac8042006-01-13 21:34:58 -08004293 } else {
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004294 pSMB->PasswordLength = cpu_to_le16(CIFS_AUTH_RESP_SIZE);
Steve Frencheeac8042006-01-13 21:34:58 -08004295 /* BB FIXME add code to fail this if NTLMv2 or Kerberos
4296 specified as required (when that support is added to
4297 the vfs in the future) as only NTLM or the much
Steve French7c7b25b2006-06-01 19:20:10 +00004298 weaker LANMAN (which we do not send by default) is accepted
Steve Frencheeac8042006-01-13 21:34:58 -08004299 by Samba (not sure whether other servers allow
4300 NTLMv2 password here) */
Steve French7c7b25b2006-06-01 19:20:10 +00004301#ifdef CONFIG_CIFS_WEAK_PW_HASH
Jeff Layton04912d62010-04-24 07:57:45 -04004302 if ((global_secflags & CIFSSEC_MAY_LANMAN) &&
Jeff Layton3f618222013-06-12 19:52:14 -05004303 (ses->sectype == LANMAN))
Shirish Pargaonkard3ba50b2010-10-27 15:20:36 -05004304 calc_lanman_hash(tcon->password, ses->server->cryptkey,
Steve French96daf2b2011-05-27 04:34:02 +00004305 ses->server->sec_mode &
Jeff Layton4e53a3f2008-12-05 20:41:21 -05004306 SECMODE_PW_ENCRYPT ? true : false,
4307 bcc_ptr);
Steve French7c7b25b2006-06-01 19:20:10 +00004308 else
4309#endif /* CIFS_WEAK_PW_HASH */
Shirish Pargaonkaree2c9252011-01-27 09:58:04 -06004310 rc = SMBNTencrypt(tcon->password, ses->server->cryptkey,
Shirish Pargaonkar9ef59922011-10-20 13:21:59 -05004311 bcc_ptr, nls_codepage);
Steve Frenchf3a31a22015-03-26 19:23:20 -05004312 if (rc) {
4313 cifs_dbg(FYI, "%s Can't generate NTLM rsp. Error: %d\n",
4314 __func__, rc);
4315 cifs_buf_release(smb_buffer);
4316 return rc;
4317 }
Steve Frencheeac8042006-01-13 21:34:58 -08004318
Shirish Pargaonkar540b2e32011-01-18 22:33:54 -06004319 bcc_ptr += CIFS_AUTH_RESP_SIZE;
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004320 if (ses->capabilities & CAP_UNICODE) {
Steve French7c7b25b2006-06-01 19:20:10 +00004321 /* must align unicode strings */
4322 *bcc_ptr = 0; /* null byte password */
4323 bcc_ptr++;
4324 }
Steve Frencheeac8042006-01-13 21:34:58 -08004325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326
Jeff Layton38d77c52013-05-26 07:01:00 -04004327 if (ses->server->sign)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 smb_buffer->Flags2 |= SMBFLG2_SECURITY_SIGNATURE;
4329
4330 if (ses->capabilities & CAP_STATUS32) {
4331 smb_buffer->Flags2 |= SMBFLG2_ERR_STATUS;
4332 }
4333 if (ses->capabilities & CAP_DFS) {
4334 smb_buffer->Flags2 |= SMBFLG2_DFS;
4335 }
4336 if (ses->capabilities & CAP_UNICODE) {
4337 smb_buffer->Flags2 |= SMBFLG2_UNICODE;
4338 length =
Steve Frenchacbbb762012-01-18 22:32:33 -06004339 cifs_strtoUTF16((__le16 *) bcc_ptr, tree,
Steve French50c2f752007-07-13 00:33:32 +00004340 6 /* max utf8 char length in bytes */ *
Steve Frencha878fb22006-05-30 18:04:19 +00004341 (/* server len*/ + 256 /* share len */), nls_codepage);
4342 bcc_ptr += 2 * length; /* convert num 16 bit words to bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 bcc_ptr += 2; /* skip trailing null */
4344 } else { /* ASCII */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 strcpy(bcc_ptr, tree);
4346 bcc_ptr += strlen(tree) + 1;
4347 }
4348 strcpy(bcc_ptr, "?????");
4349 bcc_ptr += strlen("?????");
4350 bcc_ptr += 1;
4351 count = bcc_ptr - &pSMB->Password[0];
Steve Frenchbe8e3b02011-04-29 05:40:20 +00004352 pSMB->hdr.smb_buf_length = cpu_to_be32(be32_to_cpu(
4353 pSMB->hdr.smb_buf_length) + count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 pSMB->ByteCount = cpu_to_le16(count);
4355
Steve French133672e2007-11-13 22:41:37 +00004356 rc = SendReceive(xid, ses, smb_buffer, smb_buffer_response, &length,
Jeff Layton77499812011-01-11 07:24:23 -05004357 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 /* above now done in SendReceive */
Aurelien Aptelb327a712018-01-24 13:46:10 +01004360 if (rc == 0) {
Steve French0e0d2cf2009-05-01 05:27:32 +00004361 bool is_unicode;
4362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 tcon->tidStatus = CifsGood;
Steve French3b795212008-11-13 19:45:32 +00004364 tcon->need_reconnect = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 tcon->tid = smb_buffer_response->Tid;
4366 bcc_ptr = pByteArea(smb_buffer_response);
Jeff Layton690c5222011-01-20 13:36:51 -05004367 bytes_left = get_bcc(smb_buffer_response);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004368 length = strnlen(bcc_ptr, bytes_left - 2);
Steve French0e0d2cf2009-05-01 05:27:32 +00004369 if (smb_buffer->Flags2 & SMBFLG2_UNICODE)
4370 is_unicode = true;
4371 else
4372 is_unicode = false;
4373
Jeff Laytoncc20c032009-04-30 07:16:21 -04004374
Steve French50c2f752007-07-13 00:33:32 +00004375 /* skip service field (NB: this field is always ASCII) */
Steve French7f8ed422007-09-28 22:28:55 +00004376 if (length == 3) {
4377 if ((bcc_ptr[0] == 'I') && (bcc_ptr[1] == 'P') &&
4378 (bcc_ptr[2] == 'C')) {
Joe Perchesf96637b2013-05-04 22:12:25 -05004379 cifs_dbg(FYI, "IPC connection\n");
Aurelien Aptelb327a712018-01-24 13:46:10 +01004380 tcon->ipc = true;
4381 tcon->pipe = true;
Steve French7f8ed422007-09-28 22:28:55 +00004382 }
4383 } else if (length == 2) {
4384 if ((bcc_ptr[0] == 'A') && (bcc_ptr[1] == ':')) {
4385 /* the most common case */
Joe Perchesf96637b2013-05-04 22:12:25 -05004386 cifs_dbg(FYI, "disk share connection\n");
Steve French7f8ed422007-09-28 22:28:55 +00004387 }
4388 }
Steve French50c2f752007-07-13 00:33:32 +00004389 bcc_ptr += length + 1;
Jeff Laytoncc20c032009-04-30 07:16:21 -04004390 bytes_left -= (length + 1);
Zhao Hongjiang46b51d02013-06-24 01:57:47 -05004391 strlcpy(tcon->treeName, tree, sizeof(tcon->treeName));
Jeff Laytoncc20c032009-04-30 07:16:21 -04004392
4393 /* mostly informational -- no need to fail on error here */
Jeff Layton90a98b22009-07-20 13:40:52 -04004394 kfree(tcon->nativeFileSystem);
Steve Frenchacbbb762012-01-18 22:32:33 -06004395 tcon->nativeFileSystem = cifs_strndup_from_utf16(bcc_ptr,
Steve French0e0d2cf2009-05-01 05:27:32 +00004396 bytes_left, is_unicode,
Jeff Laytoncc20c032009-04-30 07:16:21 -04004397 nls_codepage);
4398
Joe Perchesf96637b2013-05-04 22:12:25 -05004399 cifs_dbg(FYI, "nativeFileSystem=%s\n", tcon->nativeFileSystem);
Jeff Laytoncc20c032009-04-30 07:16:21 -04004400
Steve Frenchfb8c4b12007-07-10 01:16:18 +00004401 if ((smb_buffer_response->WordCount == 3) ||
Steve French1a4e15a2006-10-12 21:33:51 +00004402 (smb_buffer_response->WordCount == 7))
4403 /* field is in same location */
Steve French39798772006-05-31 22:40:51 +00004404 tcon->Flags = le16_to_cpu(pSMBr->OptionalSupport);
4405 else
4406 tcon->Flags = 0;
Joe Perchesf96637b2013-05-04 22:12:25 -05004407 cifs_dbg(FYI, "Tcon flags: 0x%x\n", tcon->Flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 }
4409
Mariusz Kozlowskia8a11d32007-10-03 16:41:24 +00004410 cifs_buf_release(smb_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 return rc;
4412}
4413
Al Viro2e32cf52013-10-03 12:53:37 -04004414static void delayed_free(struct rcu_head *p)
4415{
4416 struct cifs_sb_info *sbi = container_of(p, struct cifs_sb_info, rcu);
4417 unload_nls(sbi->local_nls);
4418 kfree(sbi);
4419}
4420
Al Viro2a9b9952011-06-17 09:27:16 -04004421void
4422cifs_umount(struct cifs_sb_info *cifs_sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423{
Jeff Laytonb647c352010-10-28 11:16:44 -04004424 struct rb_root *root = &cifs_sb->tlink_tree;
4425 struct rb_node *node;
4426 struct tcon_link *tlink;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427
Jeff Layton2de970f2010-10-06 19:51:12 -04004428 cancel_delayed_work_sync(&cifs_sb->prune_tlinks);
4429
Jeff Laytonb647c352010-10-28 11:16:44 -04004430 spin_lock(&cifs_sb->tlink_tree_lock);
4431 while ((node = rb_first(root))) {
4432 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4433 cifs_get_tlink(tlink);
4434 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4435 rb_erase(node, root);
Steve French50c2f752007-07-13 00:33:32 +00004436
Jeff Laytonb647c352010-10-28 11:16:44 -04004437 spin_unlock(&cifs_sb->tlink_tree_lock);
4438 cifs_put_tlink(tlink);
4439 spin_lock(&cifs_sb->tlink_tree_lock);
4440 }
4441 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004442
Al Virod757d712011-06-17 09:42:43 -04004443 kfree(cifs_sb->mountdata);
Aurelien Aptela6b50582016-05-25 19:59:09 +02004444 kfree(cifs_sb->prepath);
Al Viro2e32cf52013-10-03 12:53:37 -04004445 call_rcu(&cifs_sb->rcu, delayed_free);
Steve French50c2f752007-07-13 00:33:32 +00004446}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004448int
4449cifs_negotiate_protocol(const unsigned int xid, struct cifs_ses *ses)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450{
4451 int rc = 0;
Jeff Layton198b5682010-04-24 07:57:48 -04004452 struct TCP_Server_Info *server = ses->server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004454 if (!server->ops->need_neg || !server->ops->negotiate)
4455 return -ENOSYS;
4456
Jeff Layton198b5682010-04-24 07:57:48 -04004457 /* only send once per connect */
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004458 if (!server->ops->need_neg(server))
Jeff Layton198b5682010-04-24 07:57:48 -04004459 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
Pavel Shilovsky45275782012-05-17 17:53:29 +04004461 set_credits(server, 1);
Pavel Shilovsky286170a2012-05-25 10:43:58 +04004462
4463 rc = server->ops->negotiate(xid, ses);
Jeff Layton198b5682010-04-24 07:57:48 -04004464 if (rc == 0) {
4465 spin_lock(&GlobalMid_Lock);
Jeff Layton7fdbaa12011-06-10 16:14:57 -04004466 if (server->tcpStatus == CifsNeedNegotiate)
Jeff Layton198b5682010-04-24 07:57:48 -04004467 server->tcpStatus = CifsGood;
4468 else
4469 rc = -EHOSTDOWN;
4470 spin_unlock(&GlobalMid_Lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 }
Steve French26b994f2008-08-06 05:11:33 +00004472
Jeff Layton198b5682010-04-24 07:57:48 -04004473 return rc;
4474}
Steve French26b994f2008-08-06 05:11:33 +00004475
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004476int
4477cifs_setup_session(const unsigned int xid, struct cifs_ses *ses,
4478 struct nls_table *nls_info)
Jeff Layton198b5682010-04-24 07:57:48 -04004479{
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004480 int rc = -ENOSYS;
Jeff Layton198b5682010-04-24 07:57:48 -04004481 struct TCP_Server_Info *server = ses->server;
4482
Jeff Layton198b5682010-04-24 07:57:48 -04004483 ses->capabilities = server->capabilities;
Steve French26b994f2008-08-06 05:11:33 +00004484 if (linuxExtEnabled == 0)
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004485 ses->capabilities &= (~server->vals->cap_unix);
Steve French20418ac2009-04-30 16:13:32 +00004486
Joe Perchesf96637b2013-05-04 22:12:25 -05004487 cifs_dbg(FYI, "Security Mode: 0x%x Capabilities: 0x%x TimeAdjust: %d\n",
Steve French96daf2b2011-05-27 04:34:02 +00004488 server->sec_mode, server->capabilities, server->timeAdj);
Jeff Laytoncb7691b2008-08-18 15:41:05 -04004489
Shu Wangf5c4ba82017-09-08 18:48:33 +08004490 if (ses->auth_key.response) {
Steve French2a182872018-03-29 12:16:34 -05004491 cifs_dbg(FYI, "Free previous auth_key.response = %p\n",
Shu Wangf5c4ba82017-09-08 18:48:33 +08004492 ses->auth_key.response);
4493 kfree(ses->auth_key.response);
4494 ses->auth_key.response = NULL;
4495 ses->auth_key.len = 0;
4496 }
4497
Pavel Shilovsky58c45c52012-05-25 10:54:49 +04004498 if (server->ops->sess_setup)
4499 rc = server->ops->sess_setup(xid, ses, nls_info);
4500
Shirish Pargaonkard4e63bd2013-08-29 08:35:09 -05004501 if (rc)
Joe Perchesf96637b2013-05-04 22:12:25 -05004502 cifs_dbg(VFS, "Send error in SessSetup = %d\n", rc);
Shirish Pargaonkar21e73392010-10-21 06:42:55 -05004503
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 return rc;
4505}
4506
Jeff Layton8a8798a2012-01-17 16:09:15 -05004507static int
4508cifs_set_vol_auth(struct smb_vol *vol, struct cifs_ses *ses)
4509{
Jeff Layton3f618222013-06-12 19:52:14 -05004510 vol->sectype = ses->sectype;
4511
4512 /* krb5 is special, since we don't need username or pw */
4513 if (vol->sectype == Kerberos)
Jeff Layton8a8798a2012-01-17 16:09:15 -05004514 return 0;
Jeff Layton8a8798a2012-01-17 16:09:15 -05004515
4516 return cifs_set_cifscreds(vol, ses);
4517}
4518
Steve French96daf2b2011-05-27 04:34:02 +00004519static struct cifs_tcon *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004520cifs_construct_tcon(struct cifs_sb_info *cifs_sb, kuid_t fsuid)
Jeff Layton9d002df2010-10-06 19:51:11 -04004521{
Jeff Layton8a8798a2012-01-17 16:09:15 -05004522 int rc;
Steve French96daf2b2011-05-27 04:34:02 +00004523 struct cifs_tcon *master_tcon = cifs_sb_master_tcon(cifs_sb);
4524 struct cifs_ses *ses;
4525 struct cifs_tcon *tcon = NULL;
Jeff Layton9d002df2010-10-06 19:51:11 -04004526 struct smb_vol *vol_info;
Jeff Layton9d002df2010-10-06 19:51:11 -04004527
4528 vol_info = kzalloc(sizeof(*vol_info), GFP_KERNEL);
Dan Carpenter803ab972012-01-24 11:39:22 +03004529 if (vol_info == NULL)
4530 return ERR_PTR(-ENOMEM);
Jeff Layton9d002df2010-10-06 19:51:11 -04004531
Jeff Layton9d002df2010-10-06 19:51:11 -04004532 vol_info->local_nls = cifs_sb->local_nls;
4533 vol_info->linux_uid = fsuid;
4534 vol_info->cred_uid = fsuid;
4535 vol_info->UNC = master_tcon->treeName;
4536 vol_info->retry = master_tcon->retry;
4537 vol_info->nocase = master_tcon->nocase;
Steve French3d4ef9a2018-04-25 22:19:09 -05004538 vol_info->nohandlecache = master_tcon->nohandlecache;
Jeff Layton9d002df2010-10-06 19:51:11 -04004539 vol_info->local_lease = master_tcon->local_lease;
4540 vol_info->no_linux_ext = !master_tcon->unix_ext;
Jeff Layton28e11bd2013-05-26 07:01:00 -04004541 vol_info->sectype = master_tcon->ses->sectype;
4542 vol_info->sign = master_tcon->ses->sign;
Jeff Layton9d002df2010-10-06 19:51:11 -04004543
Jeff Layton8a8798a2012-01-17 16:09:15 -05004544 rc = cifs_set_vol_auth(vol_info, master_tcon->ses);
4545 if (rc) {
4546 tcon = ERR_PTR(rc);
4547 goto out;
4548 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004549
4550 /* get a reference for the same TCP session */
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304551 spin_lock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004552 ++master_tcon->ses->server->srv_count;
Suresh Jayaraman3f9bcca2010-10-18 23:29:37 +05304553 spin_unlock(&cifs_tcp_ses_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004554
4555 ses = cifs_get_smb_ses(master_tcon->ses->server, vol_info);
4556 if (IS_ERR(ses)) {
Steve French96daf2b2011-05-27 04:34:02 +00004557 tcon = (struct cifs_tcon *)ses;
Pavel Shilovsky53e0e112016-11-04 11:50:31 -07004558 cifs_put_tcp_session(master_tcon->ses->server, 0);
Jeff Layton9d002df2010-10-06 19:51:11 -04004559 goto out;
4560 }
4561
4562 tcon = cifs_get_tcon(ses, vol_info);
4563 if (IS_ERR(tcon)) {
4564 cifs_put_smb_ses(ses);
4565 goto out;
4566 }
4567
Steve Frenchce558b02018-05-31 19:16:54 -05004568 /* if new SMB3.11 POSIX extensions are supported do not remap / and \ */
4569 if (tcon->posix_extensions)
4570 cifs_sb->mnt_cifs_flags |= CIFS_MOUNT_POSIX_PATHS;
Steve French0fdfef92018-06-28 19:30:23 -05004571
Pavel Shilovsky29e20f92012-07-13 13:58:14 +04004572 if (cap_unix(ses))
Jeff Layton9d002df2010-10-06 19:51:11 -04004573 reset_cifs_unix_caps(0, tcon, NULL, vol_info);
Steve Frenchb3266142018-05-20 23:41:10 -05004574
Jeff Layton9d002df2010-10-06 19:51:11 -04004575out:
Jeff Layton8a8798a2012-01-17 16:09:15 -05004576 kfree(vol_info->username);
Aurelien Aptel97f4b722018-01-25 15:59:39 +01004577 kzfree(vol_info->password);
Jeff Layton9d002df2010-10-06 19:51:11 -04004578 kfree(vol_info);
4579
4580 return tcon;
4581}
4582
Steve French96daf2b2011-05-27 04:34:02 +00004583struct cifs_tcon *
Jeff Layton9d002df2010-10-06 19:51:11 -04004584cifs_sb_master_tcon(struct cifs_sb_info *cifs_sb)
4585{
4586 return tlink_tcon(cifs_sb_master_tlink(cifs_sb));
4587}
4588
Jeff Laytonb647c352010-10-28 11:16:44 -04004589/* find and return a tlink with given uid */
4590static struct tcon_link *
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004591tlink_rb_search(struct rb_root *root, kuid_t uid)
Jeff Laytonb647c352010-10-28 11:16:44 -04004592{
4593 struct rb_node *node = root->rb_node;
4594 struct tcon_link *tlink;
4595
4596 while (node) {
4597 tlink = rb_entry(node, struct tcon_link, tl_rbnode);
4598
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004599 if (uid_gt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004600 node = node->rb_left;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004601 else if (uid_lt(tlink->tl_uid, uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004602 node = node->rb_right;
4603 else
4604 return tlink;
4605 }
4606 return NULL;
4607}
4608
4609/* insert a tcon_link into the tree */
4610static void
4611tlink_rb_insert(struct rb_root *root, struct tcon_link *new_tlink)
4612{
4613 struct rb_node **new = &(root->rb_node), *parent = NULL;
4614 struct tcon_link *tlink;
4615
4616 while (*new) {
4617 tlink = rb_entry(*new, struct tcon_link, tl_rbnode);
4618 parent = *new;
4619
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004620 if (uid_gt(tlink->tl_uid, new_tlink->tl_uid))
Jeff Laytonb647c352010-10-28 11:16:44 -04004621 new = &((*new)->rb_left);
4622 else
4623 new = &((*new)->rb_right);
4624 }
4625
4626 rb_link_node(&new_tlink->tl_rbnode, parent, new);
4627 rb_insert_color(&new_tlink->tl_rbnode, root);
4628}
4629
Jeff Layton9d002df2010-10-06 19:51:11 -04004630/*
4631 * Find or construct an appropriate tcon given a cifs_sb and the fsuid of the
4632 * current task.
4633 *
4634 * If the superblock doesn't refer to a multiuser mount, then just return
4635 * the master tcon for the mount.
4636 *
Suresh Jayaraman6ef933a2010-11-03 10:53:49 +05304637 * First, search the rbtree for an existing tcon for this fsuid. If one
Jeff Layton9d002df2010-10-06 19:51:11 -04004638 * exists, then check to see if it's pending construction. If it is then wait
4639 * for construction to complete. Once it's no longer pending, check to see if
4640 * it failed and either return an error or retry construction, depending on
4641 * the timeout.
4642 *
4643 * If one doesn't exist then insert a new tcon_link struct into the tree and
4644 * try to construct a new one.
4645 */
4646struct tcon_link *
4647cifs_sb_tlink(struct cifs_sb_info *cifs_sb)
4648{
4649 int ret;
Eric W. Biederman6d4a0832013-02-06 01:48:56 -08004650 kuid_t fsuid = current_fsuid();
Jeff Layton9d002df2010-10-06 19:51:11 -04004651 struct tcon_link *tlink, *newtlink;
4652
4653 if (!(cifs_sb->mnt_cifs_flags & CIFS_MOUNT_MULTIUSER))
4654 return cifs_get_tlink(cifs_sb_master_tlink(cifs_sb));
4655
4656 spin_lock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004657 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004658 if (tlink)
4659 cifs_get_tlink(tlink);
4660 spin_unlock(&cifs_sb->tlink_tree_lock);
4661
4662 if (tlink == NULL) {
4663 newtlink = kzalloc(sizeof(*tlink), GFP_KERNEL);
4664 if (newtlink == NULL)
4665 return ERR_PTR(-ENOMEM);
Jeff Laytonb647c352010-10-28 11:16:44 -04004666 newtlink->tl_uid = fsuid;
Jeff Layton9d002df2010-10-06 19:51:11 -04004667 newtlink->tl_tcon = ERR_PTR(-EACCES);
4668 set_bit(TCON_LINK_PENDING, &newtlink->tl_flags);
4669 set_bit(TCON_LINK_IN_TREE, &newtlink->tl_flags);
4670 cifs_get_tlink(newtlink);
4671
Jeff Layton9d002df2010-10-06 19:51:11 -04004672 spin_lock(&cifs_sb->tlink_tree_lock);
4673 /* was one inserted after previous search? */
Jeff Laytonb647c352010-10-28 11:16:44 -04004674 tlink = tlink_rb_search(&cifs_sb->tlink_tree, fsuid);
Jeff Layton9d002df2010-10-06 19:51:11 -04004675 if (tlink) {
4676 cifs_get_tlink(tlink);
4677 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004678 kfree(newtlink);
4679 goto wait_for_construction;
4680 }
Jeff Layton9d002df2010-10-06 19:51:11 -04004681 tlink = newtlink;
Jeff Laytonb647c352010-10-28 11:16:44 -04004682 tlink_rb_insert(&cifs_sb->tlink_tree, tlink);
4683 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton9d002df2010-10-06 19:51:11 -04004684 } else {
4685wait_for_construction:
4686 ret = wait_on_bit(&tlink->tl_flags, TCON_LINK_PENDING,
Jeff Layton9d002df2010-10-06 19:51:11 -04004687 TASK_INTERRUPTIBLE);
4688 if (ret) {
4689 cifs_put_tlink(tlink);
NeilBrown74316202014-07-07 15:16:04 +10004690 return ERR_PTR(-ERESTARTSYS);
Jeff Layton9d002df2010-10-06 19:51:11 -04004691 }
4692
4693 /* if it's good, return it */
4694 if (!IS_ERR(tlink->tl_tcon))
4695 return tlink;
4696
4697 /* return error if we tried this already recently */
4698 if (time_before(jiffies, tlink->tl_time + TLINK_ERROR_EXPIRE)) {
4699 cifs_put_tlink(tlink);
4700 return ERR_PTR(-EACCES);
4701 }
4702
4703 if (test_and_set_bit(TCON_LINK_PENDING, &tlink->tl_flags))
4704 goto wait_for_construction;
4705 }
4706
4707 tlink->tl_tcon = cifs_construct_tcon(cifs_sb, fsuid);
4708 clear_bit(TCON_LINK_PENDING, &tlink->tl_flags);
4709 wake_up_bit(&tlink->tl_flags, TCON_LINK_PENDING);
4710
4711 if (IS_ERR(tlink->tl_tcon)) {
4712 cifs_put_tlink(tlink);
4713 return ERR_PTR(-EACCES);
4714 }
4715
4716 return tlink;
4717}
Jeff Layton2de970f2010-10-06 19:51:12 -04004718
4719/*
4720 * periodic workqueue job that scans tcon_tree for a superblock and closes
4721 * out tcons.
4722 */
4723static void
4724cifs_prune_tlinks(struct work_struct *work)
4725{
4726 struct cifs_sb_info *cifs_sb = container_of(work, struct cifs_sb_info,
4727 prune_tlinks.work);
Jeff Laytonb647c352010-10-28 11:16:44 -04004728 struct rb_root *root = &cifs_sb->tlink_tree;
Colin Ian King37e12f52018-01-17 09:52:39 +00004729 struct rb_node *node;
Jeff Laytonb647c352010-10-28 11:16:44 -04004730 struct rb_node *tmp;
4731 struct tcon_link *tlink;
Jeff Layton2de970f2010-10-06 19:51:12 -04004732
Jeff Laytonb647c352010-10-28 11:16:44 -04004733 /*
4734 * Because we drop the spinlock in the loop in order to put the tlink
4735 * it's not guarded against removal of links from the tree. The only
4736 * places that remove entries from the tree are this function and
4737 * umounts. Because this function is non-reentrant and is canceled
4738 * before umount can proceed, this is safe.
4739 */
4740 spin_lock(&cifs_sb->tlink_tree_lock);
4741 node = rb_first(root);
4742 while (node != NULL) {
4743 tmp = node;
4744 node = rb_next(tmp);
4745 tlink = rb_entry(tmp, struct tcon_link, tl_rbnode);
4746
4747 if (test_bit(TCON_LINK_MASTER, &tlink->tl_flags) ||
4748 atomic_read(&tlink->tl_count) != 0 ||
4749 time_after(tlink->tl_time + TLINK_IDLE_EXPIRE, jiffies))
4750 continue;
4751
4752 cifs_get_tlink(tlink);
4753 clear_bit(TCON_LINK_IN_TREE, &tlink->tl_flags);
4754 rb_erase(tmp, root);
4755
Jeff Layton2de970f2010-10-06 19:51:12 -04004756 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Laytonb647c352010-10-28 11:16:44 -04004757 cifs_put_tlink(tlink);
4758 spin_lock(&cifs_sb->tlink_tree_lock);
4759 }
4760 spin_unlock(&cifs_sb->tlink_tree_lock);
Jeff Layton2de970f2010-10-06 19:51:12 -04004761
Jeff Laytonda472fc2012-03-23 14:40:53 -04004762 queue_delayed_work(cifsiod_wq, &cifs_sb->prune_tlinks,
Jeff Layton2de970f2010-10-06 19:51:12 -04004763 TLINK_IDLE_EXPIRE);
4764}